diff --git a/.env.example b/.env.example new file mode 100644 index 0000000..6d3b99e --- /dev/null +++ b/.env.example @@ -0,0 +1,3 @@ +HTTP_PORT=8088 +# Review/pin a known image version or digest before production. +HAGRID_IMAGE=jumper78/hagrid-openpgp:latest diff --git a/Dockerfile b/Dockerfile index 37f3090..42806dd 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,8 +1,5 @@ FROM nginx:alpine -LABEL description="Static OpenPGP public key and Web Key Directory server" -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 && mkdir -p /usr/share/nginx/html/keys /usr/share/nginx/html/.well-known/openpgpkey COPY nginx.conf /etc/nginx/conf.d/default.conf -RUN chown -R nginx:nginx /usr/share/nginx/html EXPOSE 80 CMD ["nginx", "-g", "daemon off;"] diff --git a/README.md b/README.md index 3f7f5f6..10c4071 100644 --- a/README.md +++ b/README.md @@ -1,123 +1,126 @@ -# GPG Public Key Server with Web Key Directory (WKD) +# OpenPGP Key Distribution: WKD + HKP + VKS -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. +This project combines an Nginx frontend with a Hagrid backend. -> 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. +- **WKD:** static files under `/.well-known/openpgpkey/`. +- **HKP:** `/pks/lookup` and `/pks/add`, handled by Hagrid. +- **VKS:** `/vks/v1/` API, handled by Hagrid. +- **Static downloads:** optional public-key files under `/keys/`. -## Files +Hagrid supports a subset of HKP and its native VKS API. It is not full SKS/HKP feature parity. See https://keys.openpgp.org/about/api/ -```text -gpg-keyserver-wkd/ -├── Dockerfile -├── docker-compose.yml -├── nginx.conf -├── publish-wkd-key.sh -├── README.md -├── keys/ # Optional ASCII-armored downloads -└── wkd/ - ├── policy # WKD policy marker (created by script) - └── hu/ # Binary public keys named by WKD hash -``` +## Important deployment caveat -## Requirements +The Compose file uses the community-maintained `jumper78/hagrid-openpgp` image as an example, not an official production image published by keys.openpgp.org. Confirm its current image tag, internal listen port, persistent-data path, and configuration requirements before deploying. The sample assumes port `8000` and `/data`; change these to match the selected image. The scaffold does not configure TLS, DNS, or SMTP automatically. -- Docker Engine and Docker Compose v2 -- GnuPG installed on the host for key export -- A domain you control and HTTPS for production +## Start -## Start the container - -From this directory: +1. Copy `.env.example` to `.env`. +2. Review the Hagrid image's documentation and verify port and volume settings. +3. Run: ```bash docker compose up -d --build docker compose ps -curl -f http://localhost:8088/healthz +docker compose logs -f ``` -Expected health response: `OK`. The service is available locally on port `8088`. +Frontend: `http://localhost:8088`. `/healthz` returns `OK`. Use HTTPS in production by placing this behind a trusted TLS reverse proxy. -## Publish a WKD key +## HKP -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: +Lookup a key: ```bash -./publish-wkd-key.sh user@example.com +curl -i 'http://localhost:8088/pks/lookup?op=get&search=user%40example.com' ``` -The script uses `gpg-wks-client --print-wkd-hash` to compute the WKD filename, exports a binary public key into `wkd/hu/`, creates the `wkd/policy` marker, and exports an optional ASCII-armored copy into `keys/user@example.com.asc`. - -The Docker container mounts `wkd/` and `keys/` read-only. Files added or updated on the host are served without rebuilding the image. - -### Manual export (if you prefer) +GnuPG lookup: ```bash -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" +gpg --keyserver hkp://localhost:8088 --search-keys FINGERPRINT ``` -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. +Submit an ASCII-armored public key: -## URL layout and DNS/HTTPS +```bash +gpg --armor --export user@example.com > publickey.asc +curl -X POST -H 'Content-Type: application/x-www-form-urlencoded' \ + --data-urlencode keytext@publickey.asc http://localhost:8088/pks/add +``` -This configuration implements the **direct method**. For `user@example.com`, the path is: +HKP upload does not verify email ownership. Use VKS for the verification workflow. + +## VKS + +Fetch by uppercase fingerprint (without `0x`): + +```bash +curl -i 'http://localhost:8088/vks/v1/by-fingerprint/UPPERCASE_FINGERPRINT' +``` + +Fetch by key ID: + +```bash +curl -i 'http://localhost:8088/vks/v1/by-keyid/UPPERCASE_KEY_ID' +``` + +Fetch by URL-encoded email (only published/verified addresses are discoverable): + +```bash +curl -i 'http://localhost:8088/vks/v1/by-email/user%40example.com' +``` + +Upload a public key: + +```bash +gpg --armor --export user@example.com > publickey.asc +python3 -c 'import json,pathlib; print(json.dumps({"keytext":pathlib.Path("publickey.asc").read_text()}))' > upload.json +curl -i -H 'Content-Type: application/json' --data-binary @upload.json http://localhost:8088/vks/v1/upload +``` + +The owner may need to complete an email-verification step before email lookup works. Configure SMTP as documented by the selected Hagrid image if using verification emails. + +## WKD + +The direct-method URL usually has this form: ```text -https://example.com/.well-known/openpgpkey/hu/?l=user +https://example.com/.well-known/openpgpkey/hu/ ``` -The policy marker must be available at: - -```text -https://example.com/.well-known/openpgpkey/policy -``` - -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: +Use GnuPG to display the correct WKD hash: ```bash -gpg --locate-keys --auto-key-locate clear,nodefault,wkd user@example.com +gpg --with-wkd-hash --list-keys user@example.com ``` -You can also check the raw key URL locally after publishing. Substitute the hash printed by the script: +Place the **binary public-key export** at the matching path beneath `wkd/`, for example `wkd/example.com/hu/`. WKD must be served from the correct email domain over HTTPS; a `keys.example.com` hostname does not automatically enable WKD for `example.com`. + +Static downloadable key example: ```bash -curl -f http://localhost:8088/.well-known/openpgpkey/hu/ +gpg --armor --export user@example.com > keys/user@example.com.asc +curl -f http://localhost:8088/keys/user@example.com.asc ``` -A successful local HTTP request verifies file serving only; final validation must use the public HTTPS URL and your real email domain. +## Security -## Security and maintenance +- Never publish private/secret keys. +- Use HTTPS in production and verify fingerprints via a separate trusted channel. +- Review and pin the Hagrid image rather than blindly using `latest`. +- Back up Hagrid's persistent data and `keys/` / `wkd/`. +- Monitor logs, rate limits and email verification; configure SMTP if required. +- This sample has not been tested against your actual image, reverse proxy, DNS or mail setup. -- 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. -- 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 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. +## Troubleshooting -## Useful commands - -```bash -docker compose logs -f gpg-keyserver -docker compose restart -docker compose down -docker compose up -d --build -``` +- **502:** Hagrid may not be running or may use a different port; update `nginx.conf`. +- **404 VKS email lookup:** the key may be absent or the address not verified/published. +- **WKD failure:** check the HTTPS origin, hash, domain-specific path and binary key format. ## References -- [GnuPG Wiki: WKD](https://wiki.gnupg.org/WKD) -- [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/) +- Hagrid API (HKP/VKS): https://keys.openpgp.org/about/api/ +- WKD: https://wiki.gnupg.org/WKD +- Example community Docker image: https://hub.docker.com/r/jumper78/hagrid-openpgp diff --git a/docker-compose.yml b/docker-compose.yml index 8d18b4d..b2be4cd 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,20 +1,24 @@ services: - gpg-keyserver: - build: - context: . - dockerfile: Dockerfile - container_name: gpg-keyserver + # Community-maintained Hagrid image; verify its port/config before production. + hagrid: + image: ${HAGRID_IMAGE:-jumper78/hagrid-openpgp:latest} + restart: unless-stopped + expose: + - "8000" + volumes: + - hagrid-data:/data + networks: [openpgp] + + frontend: + build: . restart: unless-stopped ports: - - "8088:80" + - "${HTTP_PORT:-8088}:80" volumes: - ./keys:/usr/share/nginx/html/keys:ro - ./wkd:/usr/share/nginx/html/.well-known/openpgpkey:ro - healthcheck: - test: ["CMD", "wget", "-q", "-O", "/dev/null", "http://127.0.0.1/healthz"] - interval: 30s - timeout: 5s - retries: 3 + depends_on: [hagrid] + networks: [openpgp] read_only: true tmpfs: - /var/cache/nginx @@ -22,3 +26,9 @@ services: - /tmp security_opt: - no-new-privileges:true + +volumes: + hagrid-data: +networks: + openpgp: + driver: bridge diff --git a/nginx.conf b/nginx.conf index 0854809..8fe3e9c 100644 --- a/nginx.conf +++ b/nginx.conf @@ -1,36 +1,48 @@ +upstream hagrid_backend { + # Verify this matches the selected Hagrid image's internal listen port. + server hagrid:8000; + keepalive 16; +} server { listen 80; server_name _; root /usr/share/nginx/html; server_tokens off; autoindex off; - add_header X-Content-Type-Options "nosniff" always; add_header X-Frame-Options "DENY" always; + add_header Referrer-Policy "no-referrer" always; location = /healthz { access_log off; default_type text/plain; return 200 "OK\n"; } - - # Direct-method Web Key Directory (WKD). - # Files live under ./wkd, mounted at /.well-known/openpgpkey/. + # Hagrid handles HKP and VKS. + location ^~ /pks/ { + proxy_pass http://hagrid_backend; + proxy_http_version 1.1; + proxy_set_header Host $host; + proxy_set_header X-Real-IP $remote_addr; + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + proxy_set_header X-Forwarded-Proto $scheme; + } + location ^~ /vks/ { + proxy_pass http://hagrid_backend; + proxy_http_version 1.1; + proxy_set_header Host $host; + proxy_set_header X-Real-IP $remote_addr; + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + proxy_set_header X-Forwarded-Proto $scheme; + } location ^~ /.well-known/openpgpkey/ { - limit_except GET HEAD { deny all; } default_type application/octet-stream; - add_header Access-Control-Allow-Origin "*" always; try_files $uri =404; } - - # Optional legacy/static download URLs. location ^~ /keys/ { limit_except GET HEAD { deny all; } - default_type application/octet-stream; + default_type application/pgp-keys; try_files $uri =404; } - - location / { - return 404; - } + location / { return 404; } } diff --git a/wkd/example.com/policy b/wkd/example.com/policy new file mode 100644 index 0000000..e4e07ad --- /dev/null +++ b/wkd/example.com/policy @@ -0,0 +1 @@ +This is a public OpenPGP key directory.