Add Web Key Directory (WKD)

This commit is contained in:
2026-10-10 22:30:42 +03:00
parent 4bbcd31694
commit 9ef912049a
7 changed files with 120 additions and 167 deletions
+4 -10
View File
@@ -1,14 +1,8 @@
FROM nginx:alpine FROM nginx:alpine
LABEL description="Static OpenPGP public key and Web Key Directory server"
LABEL description="HTTP server for OpenPGP public keys" RUN rm -f /etc/nginx/conf.d/default.conf \
&& mkdir -p /usr/share/nginx/html/keys /usr/share/nginx/html/.well-known/openpgpkey
RUN rm -f /etc/nginx/conf.d/default.conf
COPY nginx.conf /etc/nginx/conf.d/default.conf COPY nginx.conf /etc/nginx/conf.d/default.conf
RUN chown -R nginx:nginx /usr/share/nginx/html
RUN mkdir -p /usr/share/nginx/html/keys \
&& chown -R nginx:nginx /usr/share/nginx/html
EXPOSE 80 EXPOSE 80
CMD ["nginx", "-g", "daemon off;"] CMD ["nginx", "-g", "daemon off;"]
+67 -130
View File
@@ -1,186 +1,123 @@
# GPG Public Key Server # GPG Public Key Server with Web Key Directory (WKD)
A lightweight Dockerized Nginx service for publishing **OpenPGP/GPG public keys** over HTTP. It provides a simple, read-only directory of downloadable public-key files and can run behind an existing HTTPS reverse proxy. A small Docker/Nginx service that publishes OpenPGP public keys as static files. It supports the **WKD direct method** as well as optional human-downloadable armored keys. WKD lets compatible OpenPGP clients discover a public key from an email address and its domain.
> **Scope:** This project serves static files. It is not a full HKP/HKPS OpenPGP keyserver and does not implement Web Key Directory (WKD), key submission, key search, or key synchronization. > This is a static WKD publisher, not a full HKP/HKPS keyserver or a key-submission service. It does not automatically verify ownership of email addresses or update/revoke published keys.
## Features ## Files
- Nginx-based Docker image
- Read-only mount for published key files
- Serves keys from `/keys/`
- Directory listing disabled
- Health-check endpoint at `/healthz`
- Basic security headers
- Read-only container filesystem
## Project structure
```text ```text
gpg-keyserver/ gpg-keyserver-wkd/
├── Dockerfile ├── Dockerfile
├── docker-compose.yml ├── docker-compose.yml
├── nginx.conf ├── nginx.conf
└── keys/ ├── publish-wkd-key.sh
└── .gitkeep ├── README.md
├── keys/ # Optional ASCII-armored downloads
└── wkd/
├── policy # WKD policy marker (created by script)
└── hu/ # Binary public keys named by WKD hash
``` ```
Create the `keys/` directory if it does not already exist.
## Requirements ## Requirements
- Docker Engine - Docker Engine and Docker Compose v2
- Docker Compose v2 (`docker compose`) - GnuPG installed on the host for key export
- GPG installed on the machine where you export keys - A domain you control and HTTPS for production
## Build and start ## Start the container
From the project directory, run: From this directory:
```bash ```bash
docker compose up -d --build docker compose up -d --build
```
Check the container and logs:
```bash
docker compose ps docker compose ps
docker compose logs -f gpg-keyserver curl -f http://localhost:8088/healthz
``` ```
Check the health endpoint: Expected health response: `OK`. The service is available locally on port `8088`.
## Publish a WKD key
First ensure the public key for the target email address is in your local GPG keyring. Export **public keys only**; never export secret/private keys to this service.
On Linux, macOS, or WSL, run from this project directory:
```bash ```bash
curl http://localhost:8088/healthz ./publish-wkd-key.sh user@example.com
``` ```
Expected response: The script uses `gpg-wks-client --print-wkd-hash` to compute the WKD filename, exports a binary public key into `wkd/hu/<hash>`, creates the `wkd/policy` marker, and exports an optional ASCII-armored copy into `keys/user@example.com.asc`.
```text The Docker container mounts `wkd/` and `keys/` read-only. Files added or updated on the host are served without rebuilding the image.
OK
```
The sample Compose configuration maps host port `8088` to container port `80`, making the service available locally at `http://localhost:8088`. ### Manual export (if you prefer)
## Export and publish a public key
Export only the public key. Replace the example address with the email address associated with your key:
```bash ```bash
gpg --armor --export user@example.com > keys/user@example.com.asc HASH="$(gpg-wks-client --print-wkd-hash user@example.com | awk 'NR==1 {print $1}')"
mkdir -p wkd/hu
: > wkd/policy
gpg --batch --export user@example.com > "wkd/hu/$HASH"
``` ```
An ASCII-armored public key should begin with: The WKD object should be a binary OpenPGP export. The public key must contain the intended email address in a user ID, and you should verify its fingerprint independently before publishing.
## URL layout and DNS/HTTPS
This configuration implements the **direct method**. For `user@example.com`, the path is:
```text ```text
-----BEGIN PGP PUBLIC KEY BLOCK----- https://example.com/.well-known/openpgpkey/hu/<WKD_HASH>?l=user
``` ```
The key will be available at: The policy marker must be available at:
```text ```text
http://localhost:8088/keys/user@example.com.asc https://example.com/.well-known/openpgpkey/policy
``` ```
Test retrieval: Replace `example.com` with your actual email domain. The Nginx container itself only speaks HTTP on port 80; put it behind your existing HTTPS reverse proxy and route the domain's requests to this service. The certificate must be valid for the email domain. WKD requires HTTPS; do not treat plain HTTP testing as production validation.
For the direct method, the email domain itself must serve `/.well-known/openpgpkey/`. If your main website already owns that domain, configure its reverse proxy to route that path to this service, or merge the WKD files and Nginx location into the existing web server. Do not simply point the whole domain at this container if that would replace your existing website.
The alternative **advanced method** uses `https://openpgpkey.example.com/.well-known/openpgpkey/example.com/...` and requires a dedicated `openpgpkey` subdomain with HTTPS. This sample is configured for the direct method only.
## Test WKD
After configuring DNS and HTTPS, use GnuPG from a client machine:
```bash ```bash
curl -f http://localhost:8088/keys/user@example.com.asc gpg --locate-keys --auto-key-locate clear,nodefault,wkd user@example.com
``` ```
The `keys/` directory is bind-mounted read-only into the container. Add, replace, or remove files on the host; a container rebuild is not required for file changes. You can also check the raw key URL locally after publishing. Substitute the hash printed by the script:
## Configuration ```bash
curl -f http://localhost:8088/.well-known/openpgpkey/hu/<WKD_HASH>
### Change the host port
In `docker-compose.yml`, change:
```yaml
ports:
- "8088:80"
``` ```
For example, use `"8090:80"` to expose the service on host port 8090. A successful local HTTP request verifies file serving only; final validation must use the public HTTPS URL and your real email domain.
### Add keys ## Security and maintenance
Place exported public-key files in `./keys/`. The URL path after `/keys/` matches the filename, for example `keys/user@example.com.asc` is served at `/keys/user@example.com.asc`. - Never put private keys, secret-key exports, passwords, or private key backups in `keys/` or `wkd/`.
- Public keys are public, but their association with an email address should be verified before publication.
### HTTPS and production deployment - Directory listing is disabled; only GET and HEAD are allowed for key paths.
- WKD does not itself prove a key is safe or current. Publish updates and revocations when appropriate, and verify fingerprints through a separate trusted channel.
The sample Nginx configuration listens on HTTP inside the container. For production, put the service behind your existing HTTPS reverse proxy and publish it under a domain you control, such as `keys.example.com`. - The sample grants `Access-Control-Allow-Origin: *` only for WKD paths because browser-based clients may need cross-origin reads.
- Keep the reverse proxy and TLS certificates correctly configured and renewed.
1. Configure TLS certificates and renewal at the reverse proxy.
2. Route requests to the container on port 80 over a trusted Docker network.
3. Verify key fingerprints through a trusted, separate channel before relying on a key.
4. Keep private keys out of `keys/`, out of the Docker image, and out of source control.
If your reverse proxy runs in Docker, connect both services to a shared Docker network and route to the `gpg-keyserver` service on port 80. Adapt the Compose file to your proxy architecture.
## Security notes
- **Never publish private keys.** Do not use `gpg --export-secret-keys` for this service.
- Public-key files are not secret, but hosting a key does not prove that it belongs to the email address in its filename.
- Verify fingerprints independently before using a key to encrypt sensitive email.
- Nginx directory listings are disabled, and the Compose configuration mounts `./keys` as read-only.
- The Compose configuration enables a read-only container filesystem.
- The sample `Access-Control-Allow-Origin: *` header permits browser-based cross-origin reads. Remove it from `nginx.conf` if browser clients do not need cross-origin access.
- This service has no upload endpoint, key validation, revocation processing, or key-management interface.
## Useful commands ## Useful commands
Stop the service:
```bash
docker compose down
```
Restart it:
```bash ```bash
docker compose logs -f gpg-keyserver
docker compose restart docker compose restart
``` docker compose down
Rebuild after changing the Dockerfile or Nginx configuration:
```bash
docker compose up -d --build docker compose up -d --build
``` ```
Follow logs: ## References
```bash - [GnuPG Wiki: WKD](https://wiki.gnupg.org/WKD)
docker compose logs -f gpg-keyserver - [GnuPG Wiki: WKD hosting](https://wiki.gnupg.org/WKDHosting)
``` - [OpenPGP Web Key Directory specification draft](https://datatracker.ietf.org/doc/draft-koch-openpgp-webkey-service/)
## Troubleshooting
### `404 Not Found`
- Confirm the key file exists under `./keys/`.
- Check the filename and URL spelling, including the `.asc` suffix.
- Inspect logs with `docker compose logs gpg-keyserver`.
### Port already in use
Change the host-side port in `docker-compose.yml`, for example from `"8088:80"` to `"8090:80"`, then run:
```bash
docker compose up -d
```
### Health check fails
Check the container state and logs:
```bash
docker compose ps
docker compose logs gpg-keyserver
```
Confirm that `nginx.conf` contains the `/healthz` endpoint and that Nginx starts successfully.
## Possible next step: Web Key Directory (WKD)
If you need automatic email-address-based public-key discovery, implement **OpenPGP Web Key Directory (WKD)** rather than relying on arbitrary static URLs. If you need keyserver-protocol lookup and submission, deploy an HKP/HKPS-compatible keyserver. Neither capability is provided by this static Nginx service.
+3 -11
View File
@@ -3,30 +3,22 @@ services:
build: build:
context: . context: .
dockerfile: Dockerfile dockerfile: Dockerfile
container_name: gpg-keyserver
container_name: ep-gpg-keyserver
restart: unless-stopped restart: unless-stopped
ports: ports:
- "8088:80" - "8088:80"
volumes: volumes:
- ./keys:/usr/share/nginx/html/keys:ro - ./keys:/usr/share/nginx/html/keys:ro
- ./wkd:/usr/share/nginx/html/.well-known/openpgpkey:ro
healthcheck: healthcheck:
test: ["CMD", "wget", "-q", "-O", "/dev/null", test: ["CMD", "wget", "-q", "-O", "/dev/null", "http://127.0.0.1/healthz"]
"http://127.0.0.1/healthz"]
interval: 30s interval: 30s
timeout: 5s timeout: 5s
retries: 3 retries: 3
read_only: true read_only: true
tmpfs: tmpfs:
- /var/cache/nginx - /var/cache/nginx
- /var/run - /var/run
- /tmp - /tmp
security_opt: security_opt:
- no-new-privileges:true - no-new-privileges:true
+10 -13
View File
@@ -1,16 +1,12 @@
```nginx
server { server {
listen 80; listen 80;
server_name _; server_name _;
root /usr/share/nginx/html; root /usr/share/nginx/html;
server_tokens off; server_tokens off;
autoindex off; autoindex off;
add_header X-Content-Type-Options "nosniff" always; add_header X-Content-Type-Options "nosniff" always;
add_header X-Frame-Options "DENY" always; add_header X-Frame-Options "DENY" always;
add_header Access-Control-Allow-Origin "*" always;
location = /healthz { location = /healthz {
access_log off; access_log off;
@@ -18,16 +14,18 @@ server {
return 200 "OK\n"; return 200 "OK\n";
} }
location /keys/ { # Direct-method Web Key Directory (WKD).
limit_except GET HEAD { # Files live under ./wkd, mounted at /.well-known/openpgpkey/.
deny all; location ^~ /.well-known/openpgpkey/ {
} limit_except GET HEAD { deny all; }
default_type application/octet-stream;
types { add_header Access-Control-Allow-Origin "*" always;
text/plain asc; try_files $uri =404;
text/plain txt;
} }
# Optional legacy/static download URLs.
location ^~ /keys/ {
limit_except GET HEAD { deny all; }
default_type application/octet-stream; default_type application/octet-stream;
try_files $uri =404; try_files $uri =404;
} }
@@ -36,4 +34,3 @@ server {
return 404; return 404;
} }
} }
```
+33
View File
@@ -0,0 +1,33 @@
#!/usr/bin/env bash
set -euo pipefail
if [[ $# -ne 1 ]]; then
echo "Usage: $0 user@example.com" >&2
exit 2
fi
email="$1"
command -v gpg >/dev/null || { echo "gpg is required" >&2; exit 1; }
command -v gpg-wks-client >/dev/null || { echo "gpg-wks-client is required (usually included with GnuPG)" >&2; exit 1; }
# GnuPG computes the standardized SHA-1 + z-base-32 WKD local-part hash.
hash="$(gpg-wks-client --print-wkd-hash "$email" | awk 'NR==1 {print $1}')"
if [[ ! "$hash" =~ ^[a-z0-9]{32}$ ]]; then
echo "Could not determine a valid WKD hash for: $email" >&2
exit 1
fi
mkdir -p "wkd/hu" "keys"
# WKD serves the binary OpenPGP public-key export, not an ASCII-armored file.
gpg --batch --export "$email" > "wkd/hu/$hash"
if [[ ! -s "wkd/hu/$hash" ]]; then
rm -f "wkd/hu/$hash"
echo "No public key exported for $email. Check that the key is in your GPG keyring." >&2
exit 1
fi
: > wkd/policy
# Also retain an optional human-downloadable armored copy.
gpg --armor --batch --export "$email" > "keys/$email.asc"
echo "Published WKD key for $email"
echo "WKD hash: $hash"
echo "Direct-method URL path: /.well-known/openpgpkey/hu/$hash?l=${email%@*}"
echo "Local test: curl -f http://localhost:8088/.well-known/openpgpkey/hu/$hash"
View File
View File