chiark / gitweb /
cryptop.in, keyfunc.sh.in: Move userv variable setup into keyfunc.sh.
[distorted-keys] / ktype.gnupg
CommitLineData
c47f2aba
MW
1### -*-sh-*-
2###
3### Key type for GNU Privacy Guard
4###
5### (c) 2011 Mark Wooding
6###
7
8###----- Licensing notice ---------------------------------------------------
9###
10### This file is part of the distorted.org.uk key management suite.
11###
12### distorted-keys is free software; you can redistribute it and/or modify
13### it under the terms of the GNU General Public License as published by
14### the Free Software Foundation; either version 2 of the License, or
15### (at your option) any later version.
16###
17### distorted-keys is distributed in the hope that it will be useful,
18### but WITHOUT ANY WARRANTY; without even the implied warranty of
19### MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20### GNU General Public License for more details.
21###
22### You should have received a copy of the GNU General Public License
23### along with distorted-keys; if not, write to the Free Software Foundation,
24### Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
25
26run_gnupg () {
27 base=$1; shift
28 ## Run GnuPG with some standard options.
29
30 gpg --homedir="$base" --no-permission-warning -q --batch \
31 --always-trust \
32 "$@"
33}
34
35defprops k_props <<EOF
36main_type t $R_WORD
37main_length t $R_NUMERIC
38sub_type t $R_WORD
39sub_length t $R_NUMERIC
40s2k_cipher t $R_WORD
41s2k_digest t $R_WORD
42cipher_prefs t $R_WORDSEQ
43digest_prefs t $R_WORDSEQ
44compress_prefs t $R_WORDSEQ
45realname t $R_LINE
46comment t $R_LINE
47email t $R_LINE
48EOF
49
50: ${kprop_main_type=RSA} ${kprop_main_length=3072}
51: ${kprop_sub_type=ELG-E} ${kprop_sub_length=3072}
52: ${kprop_cipher_prefs=AES256 AES TWOFISH 3DES BLOWFISH CAST5}
53: ${kprop_digest_prefs=SHA256 SHA1 RIPEMD160}
54: ${kprop_compress_prefs=ZLIB ZIP}
55
56: ${kprop_realname=%{realname\}} ${kprop_email=%{email\}}
57: ${kprop_comment=%{comment-nil\}}
58
59k_generate () {
60 base=$1 nub=$2
61
62 makenub >"$nub"
63 prefs="$kprop_cipher_prefs $kprop_digest_prefs $kprop_compress_prefs"
64
65 case ${kprop_s2k_cipher+t} in
66 t) ;;
67 *) set -- $kprop_cipher_prefs; kprop_s2k_cipher=$1 ;;
68 esac
69 case ${kprop_s2k_digest+t} in
70 t) ;;
71 *) set -- $kprop_digest_prefs; kprop_s2k_digest=$1 ;;
72 esac
73
74 cat >"$base/gpg.conf" <<EOF
75### GnuPG configuration
76
77## Annoying copyright notice and other tedious warnings.
78no-greeting
79expert
80always-trust
81
82## Algorithm selection
83s2k-cipher-algo $kprop_s2k_cipher
84s2k-digest-algo $kprop_s2k_digest
85personal-cipher-preferences $kprop_cipher_prefs
86personal-digest-preferences $kprop_digest_prefs
87personal-compress-preferences $kprop_compress_prefs
88default-preference-list $prefs
89EOF
90
91 { cat <<EOF
92Key-Type: $kprop_main_type
93Key-Length: $kprop_main_length
94Passphrase: $(cat "$nub")
95EOF
96 case ${kprop_sub_type-nil} in
97 nil) ;;
98 *) cat <<EOF
99Subkey-Type: $kprop_sub_type
100Subkey-Length: $kprop_sub_length
101EOF
102 esac
103 real=$(subst "\`realname' value" "$kprop_realname" kopt_ "$R_LINE")
104 email=$(subst "\`email' value" "$kprop_email" kopt_ "$R_LINE")
105 cat <<EOF
106Name-Real: $real
107Name-Email: $email
108EOF
109 comment=$(subst "\`comment' value" "$kprop_comment" kopt_ "$R_LINE")
110 case "$comment" in
111 ?*) cat <<EOF
112Name-Comment: $comment
113EOF
114 ;;
115 esac
116 } | run_gnupg "$base" --gen-key
117
118 ## Commit the new key.
119 run_gnupg "$base" --fingerprint --with-colons | \
120 grep '^fpr:' | cut -d: -f10 >"$base/fpr"
121 run_gnupg "$base" --export --armor --output="$base/pub"
122}
123
124k_encrypt () {
125 base=$1
126 run_gnupg "$base" --encrypt --armor --recipient=$(cat "$base/fpr")
127}
128
129k_decrypt () {
130 base=$1 nub=$2
131 run_gnupg "$base" --passphrase-file "$nub" --decrypt
132}
133
134k_sign () {
135 base=$1 nub=$2
136 run_gnupg "$base" --passphrase-file "$nub" --detach-sign --armor
137}
138
139k_verify () {
140 base=$1 sig=$3
141 echo "$sig" >$tmp/sig
142 if run_gnupg "$base" --verify $tmp/sig - >/dev/null 2>$tmp/err
143 then :; else
144 rc=$?
145 cat >&2 $tmp/err
146 return $rc
147 fi
148}
149
150###----- That's all, folks --------------------------------------------------