about summary refs log tree commit diff stats
path: root/scripts/mk_key.sh
blob: 1e38025a54d37999cf8626170b1ec52b21088b07 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#!/usr/bin/env sh

# pgp-wkd - A web key directory for pgp-keys
#
# Copyright (C) 2025 Benedikt Peetz <benedikt.peetz@b-peetz.de>
# SPDX-License-Identifier: CC-BY-SA-4.0
#
# This file is part of pgp-wkd.
#
# You should have received a copy of the License along with this program.
# If not, see <https://creativecommons.org/licenses/by-sa/4.0/legalcode.txt>.

die() {
    echo "$@"
    exit 1
}

help() {
    cat <<EOF
A helper script to add an gpg key to the wkd.

USAGE:
    mk_key KEY_ID KEY_EMAIL_DOMAIN KEY_HASH

OPTIONS:
    --help | -h
                    Display this help and exit.
ARGUMENTS:
    KEY_ID := [[ gpg --list-keys --with-colons | awk -F: '/^uid:/ { print \$10 }' ]]
                    The key to use, as specified by the key id.

    KEY_EMAIL_DOMAIN
                    The email domain of this key. For example 'key@example.org' would
                    have an KEY_EMAIL_DOMAIN of 'example.org'.

    KEY_HASH
                    This is the hashed name of the key as specified in the WKD spec.
                    Get this by running: 'gpg --with-wkd-hash --list-keys <KEY_ID>'.
EOF
}

add() {
    key_id="$1"
    key_email="$2"
    key_hash="$3"

    cd "$(git rev-parse --show-toplevel)" || die "No source dir!"

    dir="./src/.well-known/openpgpkey/$key_email/hu"

    full_key_id="$(gpg --list-keys --with-colons "$key_id" | awk -F: '/^uid:/ { print $10 }' | tail -n 1)"

    mkdir --parents "$dir"
    gpg --no-armor --export "$key_id" >"$dir/$key_hash" &&
        printf "%s%s%s%s\n" '`' "$dir/$key_hash" '`' " -> $full_key_id" >>stored_keys.md &&
        echo "Key export done!"
}

for arg in "$@"; do
    case "$arg" in
    "--help" | "-h")
        help
        exit 0
        ;;
    esac
done

key_id="$1"
key_email="$2"
key_hash="$3"
shift 3

[ -z "$key_id" ] && die "No KEY_ID specified, see '--help'!"
[ -z "$key_email" ] && die "No KEY_EMAIL_DOMAIN specified, see '--help'!"
[ -z "$key_hash" ] && die "No KEY_HASH specified, see '--help'!"

[ -n "$*" ] && die "The arguments '$*' are not recognized; see '--help' for a list!"

add "$key_id" "$key_email" "$key_hash"

# vim: ft=sh