openpgp: Decode the issuer-fingerprint signature subpacket.
[jackhill/guix/guix.git] / tests / openpgp.scm
CommitLineData
43408e30
LC
1;;; GNU Guix --- Functional package management for GNU
2;;; Copyright © 2020 Ludovic Courtès <ludo@gnu.org>
3;;;
4;;; This file is part of GNU Guix.
5;;;
6;;; GNU Guix is free software; you can redistribute it and/or modify it
7;;; under the terms of the GNU General Public License as published by
8;;; the Free Software Foundation; either version 3 of the License, or (at
9;;; your option) any later version.
10;;;
11;;; GNU Guix is distributed in the hope that it will be useful, but
12;;; WITHOUT ANY WARRANTY; without even the implied warranty of
13;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14;;; GNU General Public License for more details.
15;;;
16;;; You should have received a copy of the GNU General Public License
17;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
18
19(define-module (tests-openpgp)
20 #:use-module (guix openpgp)
4459c785 21 #:use-module (gcrypt base16)
43408e30
LC
22 #:use-module (gcrypt hash)
23 #:use-module (gcrypt pk-crypto)
24 #:use-module (ice-9 binary-ports)
25 #:use-module (ice-9 match)
26 #:use-module (rnrs bytevectors)
27 #:use-module (srfi srfi-1)
28 #:use-module (srfi srfi-11)
29 #:use-module (srfi srfi-64)
30 #:use-module (srfi srfi-71))
31
32(define %radix-64-sample
33 ;; Example of Radix-64 encoding from Section 6.6 of RFC4880.
34 "\
35-----BEGIN PGP MESSAGE-----
36Version: OpenPrivacy 0.99
37
38yDgBO22WxBHv7O8X7O/jygAEzol56iUKiXmV+XmpCtmpqQUKiQrFqclFqUDBovzS
39vBSFjNSiVHsuAA==
40=njUN
41-----END PGP MESSAGE-----\n")
42
43(define %radix-64-sample/crc-mismatch
44 ;; This time with a wrong CRC24 value.
45 "\
46-----BEGIN PGP MESSAGE-----
47
48yDgBO22WxBHv7O8X7O/jygAEzol56iUKiXmV+XmpCtmpqQUKiQrFqclFqUDBovzS
49vBSFjNSiVHsuAA==
50=AAAA
51-----END PGP MESSAGE-----\n")
52
53(define %civodul-fingerprint
54 "3CE4 6455 8A84 FDC6 9DB4 0CFB 090B 1199 3D9A EBB5")
55
56(define %civodul-key-id #x090B11993D9AEBB5) ;civodul.key
57
58;; Test keys. They were generated in a container along these lines:
59;; guix environment -CP --ad-hoc gnupg pinentry
60;; then, within the container:
61;; mkdir ~/.gnupg
62;; echo pinentry-program ~/.guix-profile/bin/pinentry-tty > ~/.gnupg/gpg-agent.conf
63;; gpg --quick-gen-key '<ludo+test-rsa@chbouib.org>' rsa
64;; or similar.
65(define %rsa-key-id #xAE25DA2A70DEED59) ;rsa.key
66(define %dsa-key-id #x587918047BE8BD2C) ;dsa.key
67(define %ed25519-key-id #x771F49CBFAAE072D) ;ed25519.key
68
4459c785
LC
69(define %rsa-key-fingerprint
70 (base16-string->bytevector
71 (string-downcase "385F86CFC86B665A5C165E6BAE25DA2A70DEED59")))
72(define %dsa-key-fingerprint
73 (base16-string->bytevector
74 (string-downcase "2884A980422330A4F33DD97F587918047BE8BD2C")))
75(define %ed25519-key-fingerprint
76 (base16-string->bytevector
77 (string-downcase "44D31E21AF7138F9B632280A771F49CBFAAE072D")))
78
43408e30
LC
79\f
80;;; The following are detached signatures created commands like:
81;;; echo 'Hello!' | gpg -sba --digest-algo sha512
82;;; They are detached (no PACKET-ONE-PASS-SIGNATURE) and uncompressed.
83
84(define %hello-signature/rsa
85 ;; Signature of the ASCII string "Hello!\n".
86 "\
87-----BEGIN PGP SIGNATURE-----
88
89iQEzBAABCAAdFiEEOF+Gz8hrZlpcFl5rriXaKnDe7VkFAl4SRF0ACgkQriXaKnDe
907VlIyQf/TU5rGUK42/C1ULoWvvm25Mjwh6xxoPPkuBxvos8bE6yKr/vJZePU3aSE
91mjbVFcO7DioxHMqLd49j803bUtdllJVU18ex9MkKbKjapkgEGkJsuTTzqyONprgk
927xtZGBWuxkP1M6hJICJkA3Ys+sTdKalux/pzr5OWAe+gxytTF/vr/EyJzdmBxbJv
93/fhd1SeVIXSw4c5gf2Wcvcgfy4N5CiLaUb7j4646KBTvDvmUMcDZ+vmKqC/XdQeQ
94PrjArGKt40ErVd98fwvNHZnw7VQMx0A3nL3joL5g7/RckDOUb4mqKoqLsLd0wPHP
95y32DiDUY9s3sy5OMzX4Y49em8vxvlg==
96=ASEm
97-----END PGP SIGNATURE-----")
98
99
100(define %hello-signature/dsa
101 "\
102-----BEGIN PGP SIGNATURE-----
103
104iHUEABEIAB0WIQQohKmAQiMwpPM92X9YeRgEe+i9LAUCXhJFpQAKCRBYeRgEe+i9
105LDAaAQC0lXPQepvZBANAUtRLMZuOwL9NQPkfhIwUXtLEBBzyFQD/So8DcybXpRBi
106JKOiyAQQjMs/GJ6qMEQpRAhyyJRAock=
107=iAEc
108-----END PGP SIGNATURE-----")
109
110
111(define %hello-signature/ed25519/sha256 ;digest-algo: sha256
112 "\
113-----BEGIN PGP SIGNATURE-----
114
115iHUEABYIAB0WIQRE0x4hr3E4+bYyKAp3H0nL+q4HLQUCXqRADAAKCRB3H0nL+q4H
116LUImAP9/foaSjPFC/MSr52LNV5ROSL9haea4jPpUP+N6ViFGowEA+AE/xpXPIqsz
117R6CdxMevURuqUpqQ7rHeiMmdUepeewU=
118=tLXy
119-----END PGP SIGNATURE-----")
120
121(define %hello-signature/ed25519/sha512 ;digest-algo: sha512
122 "\
123-----BEGIN PGP SIGNATURE-----
124
125iHUEABYKAB0WIQRE0x4hr3E4+bYyKAp3H0nL+q4HLQUCXqRAGgAKCRB3H0nL+q4H
126LTeKAP0S8LiiosJXOARlYNdhfGw9j26lHrbwJh5CORGlaqqIJAEAoMYcmtNa2b6O
127inlEwB/KQM88O9RwA8xH7X5a0rodOw4=
128=68r/
129-----END PGP SIGNATURE-----")
130
131(define %hello-signature/ed25519/sha1 ;digest-algo: sha1
132 "\
133-----BEGIN PGP SIGNATURE-----
134
135iHUEABYCAB0WIQRE0x4hr3E4+bYyKAp3H0nL+q4HLQUCXqRALQAKCRB3H0nL+q4H
136LdhEAQCfkdYhIVRa43oTNw9EL/TDFGQjXSHNRFVU0ktjkWbkQwEAjIXhvj2sqy79
137Pz7oopeN72xgggYUNT37ezqN3MeCqw0=
138=AE4G
139-----END PGP SIGNATURE-----")
140
141\f
142(test-begin "openpgp")
143
144(test-equal "read-radix-64"
145 '(#t "PGP MESSAGE")
146 (let-values (((data type)
147 (call-with-input-string %radix-64-sample read-radix-64)))
148 (list (bytevector? data) type)))
149
150(test-equal "read-radix-64, CRC mismatch"
151 '(#f "PGP MESSAGE")
152 (call-with-values
153 (lambda ()
154 (call-with-input-string %radix-64-sample/crc-mismatch
155 read-radix-64))
156 list))
157
158(test-assert "get-openpgp-keyring"
159 (let* ((key (search-path %load-path "tests/civodul.key"))
160 (keyring (get-openpgp-keyring
161 (open-bytevector-input-port
162 (call-with-input-file key read-radix-64)))))
163 (match (lookup-key-by-id keyring %civodul-key-id)
164 (((? openpgp-public-key? primary) packets ...)
165 (and (= (openpgp-public-key-id primary) %civodul-key-id)
166 (not (openpgp-public-key-subkey? primary))
167 (string=? (openpgp-format-fingerprint
168 (openpgp-public-key-fingerprint primary))
169 %civodul-fingerprint)
170 (string=? (openpgp-user-id-value (find openpgp-user-id? packets))
171 "Ludovic Courtès <ludo@gnu.org>"))))))
172
173(test-equal "get-openpgp-detached-signature/ascii"
4459c785
LC
174 (list `(,%dsa-key-id ,%dsa-key-fingerprint dsa sha256)
175 `(,%rsa-key-id ,%rsa-key-fingerprint rsa sha256)
176 `(,%ed25519-key-id ,%ed25519-key-fingerprint eddsa sha256)
177 `(,%ed25519-key-id ,%ed25519-key-fingerprint eddsa sha512)
178 `(,%ed25519-key-id ,%ed25519-key-fingerprint eddsa sha1))
43408e30
LC
179 (map (lambda (str)
180 (let ((signature (get-openpgp-detached-signature/ascii
181 (open-input-string str))))
182 (list (openpgp-signature-issuer signature)
4459c785 183 (openpgp-signature-issuer-fingerprint signature)
43408e30
LC
184 (openpgp-signature-public-key-algorithm signature)
185 (openpgp-signature-hash-algorithm signature))))
186 (list %hello-signature/dsa
187 %hello-signature/rsa
188 %hello-signature/ed25519/sha256
189 %hello-signature/ed25519/sha512
190 %hello-signature/ed25519/sha1)))
191
192(test-equal "verify-openpgp-signature, missing key"
193 `(missing-key ,%rsa-key-id)
194 (let* ((keyring (get-openpgp-keyring (%make-void-port "r")))
195 (signature (get-openpgp-packet
196 (open-bytevector-input-port
197 (call-with-input-string %hello-signature/rsa
198 read-radix-64)))))
199 (let-values (((status key)
200 (verify-openpgp-signature signature keyring
201 (open-input-string "Hello!\n"))))
202 (list status key))))
203
204(test-equal "verify-openpgp-signature, good signatures"
205 `((good-signature ,%rsa-key-id)
206 (good-signature ,%dsa-key-id)
207 (good-signature ,%ed25519-key-id)
208 (good-signature ,%ed25519-key-id)
209 (good-signature ,%ed25519-key-id))
210 (map (lambda (key signature)
211 (let* ((key (search-path %load-path key))
212 (keyring (get-openpgp-keyring
213 (open-bytevector-input-port
214 (call-with-input-file key read-radix-64))))
215 (signature (get-openpgp-packet
216 (open-bytevector-input-port
217 (call-with-input-string signature
218 read-radix-64)))))
219 (let-values (((status key)
220 (verify-openpgp-signature signature keyring
221 (open-input-string "Hello!\n"))))
222 (list status (openpgp-public-key-id key)))))
223 (list "tests/rsa.key" "tests/dsa.key"
224 "tests/ed25519.key" "tests/ed25519.key" "tests/ed25519.key")
225 (list %hello-signature/rsa %hello-signature/dsa
226 %hello-signature/ed25519/sha256
227 %hello-signature/ed25519/sha512
228 %hello-signature/ed25519/sha1)))
229
230(test-equal "verify-openpgp-signature, bad signature"
231 `((bad-signature ,%rsa-key-id)
232 (bad-signature ,%dsa-key-id)
233 (bad-signature ,%ed25519-key-id)
234 (bad-signature ,%ed25519-key-id)
235 (bad-signature ,%ed25519-key-id))
236 (let ((keyring (fold (lambda (key keyring)
237 (let ((key (search-path %load-path key)))
238 (get-openpgp-keyring
239 (open-bytevector-input-port
240 (call-with-input-file key read-radix-64))
241 keyring)))
242 %empty-keyring
243 '("tests/rsa.key" "tests/dsa.key"
244 "tests/ed25519.key" "tests/ed25519.key"
245 "tests/ed25519.key"))))
246 (map (lambda (signature)
247 (let ((signature (get-openpgp-packet
248 (open-bytevector-input-port
249 (call-with-input-string signature
250 read-radix-64)))))
251 (let-values (((status key)
252 (verify-openpgp-signature signature keyring
253 (open-input-string "What?!"))))
254 (list status (openpgp-public-key-id key)))))
255 (list %hello-signature/rsa %hello-signature/dsa
256 %hello-signature/ed25519/sha256
257 %hello-signature/ed25519/sha512
258 %hello-signature/ed25519/sha1))))
259
260(test-end "openpgp")