tests: Improve handling of Windows newlines.
[gnupg.git] / tests / openpgp / verify.scm
1 #!/usr/bin/env gpgscm
2
3 ;; Copyright (C) 2016 g10 Code GmbH
4 ;;
5 ;; This file is part of GnuPG.
6 ;;
7 ;; GnuPG is free software; you can redistribute it and/or modify
8 ;; it under the terms of the GNU General Public License as published by
9 ;; the Free Software Foundation; either version 3 of the License, or
10 ;; (at your option) any later version.
11 ;;
12 ;; GnuPG is distributed in the hope that it will be useful,
13 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
14 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 ;; GNU General Public License for more details.
16 ;;
17 ;; You should have received a copy of the GNU General Public License
18 ;; along with this program; if not, see <http://www.gnu.org/licenses/>.
19
20 (load (with-path "defs.scm"))
21
22 ;;
23 ;; Two simple tests to check that verify fails for bad input data
24 ;;
25 (for-each-p
26  "Checking bogus signature"
27  (lambda (char)
28    (lettmp (x)
29      (pipe:do
30       (pipe:spawn `(,(tool 'mktdata) --char ,char "64"))
31       (pipe:write-to x (logior O_WRONLY O_CREAT O_BINARY) #o600))
32      (if (= 0 (call `(,@GPG --verify ,x data-500)))
33          (error "no error code from verify"))))
34  '("0x2d" "0xca"))
35
36 ;; A plain signed message created using
37 ;;  echo abc | gpg --homedir . --passphrase-fd 0 -u Alpha -z0 -sa msg
38 (define msg_ols_asc "
39 -----BEGIN PGP MESSAGE-----
40
41 kA0DAAIRLXJ8x2hpdzQBrQEHYgNtc2dEDFJaSSB0aGluayB0aGF0IGFsbCByaWdo
42 dC10aGlua2luZyBwZW9wbGUgaW4gdGhpcyBjb3VudHJ5IGFyZSBzaWNrIGFuZAp0
43 aXJlZCBvZiBiZWluZyB0b2xkIHRoYXQgb3JkaW5hcnkgZGVjZW50IHBlb3BsZSBh
44 cmUgZmVkIHVwIGluIHRoaXMKY291bnRyeSB3aXRoIGJlaW5nIHNpY2sgYW5kIHRp
45 cmVkLiAgSSdtIGNlcnRhaW5seSBub3QuICBCdXQgSSdtCnNpY2sgYW5kIHRpcmVk
46 IG9mIGJlaW5nIHRvbGQgdGhhdCBJIGFtLgotIE1vbnR5IFB5dGhvbgqIPwMFAEQM
47 UlotcnzHaGl3NBECR4IAoJlEGTY+bHjD2HYuCixLQCmk01pbAKCIjkzLOAmkZNm0
48 D8luT78c/1x45Q==
49 =a29i
50 -----END PGP MESSAGE-----
51 ")
52
53 ;; A plain signed message created using
54 ;;  echo abc | gpg --homedir . --passphrase-fd 0 -u Alpha -sa msg
55 (define msg_cols_asc "
56 -----BEGIN PGP MESSAGE-----
57
58 owGbwMvMwCSoW1RzPCOz3IRxLSN7EnNucboLT6Cgp0JJRmZeNpBMLFFIzMlRKMpM
59 zyjRBQtm5qUrFKTmF+SkKmTmgdQVKyTnl+aVFFUqJBalKhRnJmcrJOalcJVkFqWm
60 KOSnKSSlgrSU5OekQMzLL0rJzEsEKk9JTU7NK4EZBtKcBtRRWgAzlwtmbnlmSQbU
61 GJjxCmDj9RQUPNVzFZJTi0oSM/NyKhXy8kuAYk6lJSBxLlTF2NziqZCYq8elq+Cb
62 n1dSqRBQWZKRn8fVYc/MygAKBljYCDIFiTDMT+9seu836Q+bevyHTJ0dzPNuvCjn
63 ZpgrwX38z58rJsfYDhwOSS4SkN/d6vUAAA==
64 =s6sY
65 -----END PGP MESSAGE-----
66 ")
67
68 ;; A PGP 2 style message.
69 (define msg_sl_asc "
70 -----BEGIN PGP MESSAGE-----
71
72 iD8DBQBEDFJaLXJ8x2hpdzQRAkeCAKCZRBk2Pmx4w9h2LgosS0AppNNaWwCgiI5M
73 yzgJpGTZtA/Jbk+/HP9ceOWtAQdiA21zZ0QMUlpJIHRoaW5rIHRoYXQgYWxsIHJp
74 Z2h0LXRoaW5raW5nIHBlb3BsZSBpbiB0aGlzIGNvdW50cnkgYXJlIHNpY2sgYW5k
75 CnRpcmVkIG9mIGJlaW5nIHRvbGQgdGhhdCBvcmRpbmFyeSBkZWNlbnQgcGVvcGxl
76 IGFyZSBmZWQgdXAgaW4gdGhpcwpjb3VudHJ5IHdpdGggYmVpbmcgc2ljayBhbmQg
77 dGlyZWQuICBJJ20gY2VydGFpbmx5IG5vdC4gIEJ1dCBJJ20Kc2ljayBhbmQgdGly
78 ZWQgb2YgYmVpbmcgdG9sZCB0aGF0IEkgYW0uCi0gTW9udHkgUHl0aG9uCg==
79 =0ukK
80 -----END PGP MESSAGE-----
81 ")
82
83 ;; An OpenPGP message lacking the onepass packet.  We used to accept
84 ;; such messages but now consider them invalid.
85 (define bad_ls_asc "
86 -----BEGIN PGP MESSAGE-----
87
88 rQEHYgNtc2dEDFJaSSB0aGluayB0aGF0IGFsbCByaWdodC10aGlua2luZyBwZW9w
89 bGUgaW4gdGhpcyBjb3VudHJ5IGFyZSBzaWNrIGFuZAp0aXJlZCBvZiBiZWluZyB0
90 b2xkIHRoYXQgb3JkaW5hcnkgZGVjZW50IHBlb3BsZSBhcmUgZmVkIHVwIGluIHRo
91 aXMKY291bnRyeSB3aXRoIGJlaW5nIHNpY2sgYW5kIHRpcmVkLiAgSSdtIGNlcnRh
92 aW5seSBub3QuICBCdXQgSSdtCnNpY2sgYW5kIHRpcmVkIG9mIGJlaW5nIHRvbGQg
93 dGhhdCBJIGFtLgotIE1vbnR5IFB5dGhvbgqIPwMFAEQMUlotcnzHaGl3NBECR4IA
94 oJlEGTY+bHjD2HYuCixLQCmk01pbAKCIjkzLOAmkZNm0D8luT78c/1x45Q==
95 =Mpiu
96 -----END PGP MESSAGE-----
97 ")
98
99
100 ;; A signed message prefixed with an unsigned literal packet.
101 ;; (fols = faked-literal-data, one-pass, literal-data, signature)
102 ;; This should throw an error because running gpg to extract the
103 ;; signed data will return both literal data packets
104 (define bad_fols_asc "
105 -----BEGIN PGP MESSAGE-----
106
107 rF1iDG1zZy51bnNpZ25lZEQMY0x0aW1lc2hhcmluZywgbjoKCUFuIGFjY2VzcyBt
108 ZXRob2Qgd2hlcmVieSBvbmUgY29tcHV0ZXIgYWJ1c2VzIG1hbnkgcGVvcGxlLgqQ
109 DQMAAhEtcnzHaGl3NAGtAQdiA21zZ0QMUlpJIHRoaW5rIHRoYXQgYWxsIHJpZ2h0
110 LXRoaW5raW5nIHBlb3BsZSBpbiB0aGlzIGNvdW50cnkgYXJlIHNpY2sgYW5kCnRp
111 cmVkIG9mIGJlaW5nIHRvbGQgdGhhdCBvcmRpbmFyeSBkZWNlbnQgcGVvcGxlIGFy
112 ZSBmZWQgdXAgaW4gdGhpcwpjb3VudHJ5IHdpdGggYmVpbmcgc2ljayBhbmQgdGly
113 ZWQuICBJJ20gY2VydGFpbmx5IG5vdC4gIEJ1dCBJJ20Kc2ljayBhbmQgdGlyZWQg
114 b2YgYmVpbmcgdG9sZCB0aGF0IEkgYW0uCi0gTW9udHkgUHl0aG9uCog/AwUARAxS
115 Wi1yfMdoaXc0EQJHggCgmUQZNj5seMPYdi4KLEtAKaTTWlsAoIiOTMs4CaRk2bQP
116 yW5Pvxz/XHjl
117 =UNM4
118 -----END PGP MESSAGE-----
119 ")
120
121 ;; A signed message suffixed with an unsigned literal packet.
122 ;; (fols = faked-literal-data, one-pass, literal-data, signature)
123 ;; This should throw an error because running gpg to extract the
124 ;; signed data will return both literal data packets
125 (define bad_olsf_asc "
126 -----BEGIN PGP MESSAGE-----
127
128 kA0DAAIRLXJ8x2hpdzQBrQEHYgNtc2dEDFJaSSB0aGluayB0aGF0IGFsbCByaWdo
129 dC10aGlua2luZyBwZW9wbGUgaW4gdGhpcyBjb3VudHJ5IGFyZSBzaWNrIGFuZAp0
130 aXJlZCBvZiBiZWluZyB0b2xkIHRoYXQgb3JkaW5hcnkgZGVjZW50IHBlb3BsZSBh
131 cmUgZmVkIHVwIGluIHRoaXMKY291bnRyeSB3aXRoIGJlaW5nIHNpY2sgYW5kIHRp
132 cmVkLiAgSSdtIGNlcnRhaW5seSBub3QuICBCdXQgSSdtCnNpY2sgYW5kIHRpcmVk
133 IG9mIGJlaW5nIHRvbGQgdGhhdCBJIGFtLgotIE1vbnR5IFB5dGhvbgqIPwMFAEQM
134 UlotcnzHaGl3NBECR4IAoJlEGTY+bHjD2HYuCixLQCmk01pbAKCIjkzLOAmkZNm0
135 D8luT78c/1x45axdYgxtc2cudW5zaWduZWREDGNMdGltZXNoYXJpbmcsIG46CglB
136 biBhY2Nlc3MgbWV0aG9kIHdoZXJlYnkgb25lIGNvbXB1dGVyIGFidXNlcyBtYW55
137 IHBlb3BsZS4K
138 =3gnG
139 -----END PGP MESSAGE-----
140 ")
141
142
143 ;; Two standard signed messages in a row
144 (define msg_olsols_asc_multiple "
145 -----BEGIN PGP MESSAGE-----
146
147 kA0DAAIRLXJ8x2hpdzQBrQEHYgNtc2dEDFJaSSB0aGluayB0aGF0IGFsbCByaWdo
148 dC10aGlua2luZyBwZW9wbGUgaW4gdGhpcyBjb3VudHJ5IGFyZSBzaWNrIGFuZAp0
149 aXJlZCBvZiBiZWluZyB0b2xkIHRoYXQgb3JkaW5hcnkgZGVjZW50IHBlb3BsZSBh
150 cmUgZmVkIHVwIGluIHRoaXMKY291bnRyeSB3aXRoIGJlaW5nIHNpY2sgYW5kIHRp
151 cmVkLiAgSSdtIGNlcnRhaW5seSBub3QuICBCdXQgSSdtCnNpY2sgYW5kIHRpcmVk
152 IG9mIGJlaW5nIHRvbGQgdGhhdCBJIGFtLgotIE1vbnR5IFB5dGhvbgqIPwMFAEQM
153 UlotcnzHaGl3NBECR4IAoJlEGTY+bHjD2HYuCixLQCmk01pbAKCIjkzLOAmkZNm0
154 D8luT78c/1x45ZANAwACES1yfMdoaXc0Aa0BB2IDbXNnRAxSWkkgdGhpbmsgdGhh
155 dCBhbGwgcmlnaHQtdGhpbmtpbmcgcGVvcGxlIGluIHRoaXMgY291bnRyeSBhcmUg
156 c2ljayBhbmQKdGlyZWQgb2YgYmVpbmcgdG9sZCB0aGF0IG9yZGluYXJ5IGRlY2Vu
157 dCBwZW9wbGUgYXJlIGZlZCB1cCBpbiB0aGlzCmNvdW50cnkgd2l0aCBiZWluZyBz
158 aWNrIGFuZCB0aXJlZC4gIEknbSBjZXJ0YWlubHkgbm90LiAgQnV0IEknbQpzaWNr
159 IGFuZCB0aXJlZCBvZiBiZWluZyB0b2xkIHRoYXQgSSBhbS4KLSBNb250eSBQeXRo
160 b24KiD8DBQBEDFJaLXJ8x2hpdzQRAkeCAKCZRBk2Pmx4w9h2LgosS0AppNNaWwCg
161 iI5MyzgJpGTZtA/Jbk+/HP9ceOU=
162 =8nLN
163 -----END PGP MESSAGE-----
164 ")
165
166 ;; A standard message with two signatures (actually the same signature
167 ;; duplicated).
168 (define msg_oolss_asc "
169 -----BEGIN PGP MESSAGE-----
170
171 kA0DAAIRLXJ8x2hpdzQBkA0DAAIRLXJ8x2hpdzQBrQEHYgNtc2dEDFJaSSB0aGlu
172 ayB0aGF0IGFsbCByaWdodC10aGlua2luZyBwZW9wbGUgaW4gdGhpcyBjb3VudHJ5
173 IGFyZSBzaWNrIGFuZAp0aXJlZCBvZiBiZWluZyB0b2xkIHRoYXQgb3JkaW5hcnkg
174 ZGVjZW50IHBlb3BsZSBhcmUgZmVkIHVwIGluIHRoaXMKY291bnRyeSB3aXRoIGJl
175 aW5nIHNpY2sgYW5kIHRpcmVkLiAgSSdtIGNlcnRhaW5seSBub3QuICBCdXQgSSdt
176 CnNpY2sgYW5kIHRpcmVkIG9mIGJlaW5nIHRvbGQgdGhhdCBJIGFtLgotIE1vbnR5
177 IFB5dGhvbgqIPwMFAEQMUlotcnzHaGl3NBECR4IAoJlEGTY+bHjD2HYuCixLQCmk
178 01pbAKCIjkzLOAmkZNm0D8luT78c/1x45Yg/AwUARAxSWi1yfMdoaXc0EQJHggCg
179 mUQZNj5seMPYdi4KLEtAKaTTWlsAoIiOTMs4CaRk2bQPyW5Pvxz/XHjl
180 =KVw5
181 -----END PGP MESSAGE-----
182 ")
183
184 ;; A standard message with two one-pass packet but only one signature
185 ;; packet
186 (define bad_ools_asc "
187 -----BEGIN PGP MESSAGE-----
188
189 kA0DAAIRLXJ8x2hpdzQBkA0DAAIRLXJ8x2hpdzQBrQEHYgNtc2dEDFJaSSB0aGlu
190 ayB0aGF0IGFsbCByaWdodC10aGlua2luZyBwZW9wbGUgaW4gdGhpcyBjb3VudHJ5
191 IGFyZSBzaWNrIGFuZAp0aXJlZCBvZiBiZWluZyB0b2xkIHRoYXQgb3JkaW5hcnkg
192 ZGVjZW50IHBlb3BsZSBhcmUgZmVkIHVwIGluIHRoaXMKY291bnRyeSB3aXRoIGJl
193 aW5nIHNpY2sgYW5kIHRpcmVkLiAgSSdtIGNlcnRhaW5seSBub3QuICBCdXQgSSdt
194 CnNpY2sgYW5kIHRpcmVkIG9mIGJlaW5nIHRvbGQgdGhhdCBJIGFtLgotIE1vbnR5
195 IFB5dGhvbgqIPwMFAEQMUlotcnzHaGl3NBECR4IAoJlEGTY+bHjD2HYuCixLQCmk
196 01pbAKCIjkzLOAmkZNm0D8luT78c/1x45Q==
197 =1/ix
198 -----END PGP MESSAGE-----
199 ")
200
201 ;; Standard cleartext signature
202 (define msg_cls_asc "
203 -----BEGIN PGP SIGNED MESSAGE-----
204 Hash: SHA1
205
206 I think that all right-thinking people in this country are sick and
207 tired of being told that ordinary decent people are fed up in this
208 country with being sick and tired.  I'm certainly not.  But I'm
209 sick and tired of being told that I am.
210 - - Monty Python
211 -----BEGIN PGP SIGNATURE-----
212
213 iD8DBQFEDVp1LXJ8x2hpdzQRAplUAKCMfpG3GPw/TLN52tosgXP5lNECkwCfQhAa
214 emmev7IuQjWYrGF9Lxj+zj8=
215 =qJsY
216 -----END PGP SIGNATURE-----
217 ")
218
219 ;; Cleartext signature with two signatures
220 (define msg_clss_asc "
221 -----BEGIN PGP SIGNED MESSAGE-----
222 Hash: SHA1
223
224 What is the difference between a Turing machine and the modern computer?
225 It's the same as that between Hillary's ascent of Everest and the
226 establishment of a Hilton on its peak.
227 -----BEGIN PGP SIGNATURE-----
228
229 iD8DBQFEDVz6LXJ8x2hpdzQRAtkGAKCeMhNbHnh339fpjNj9owsYcC4zBwCfYO5l
230 2u+KEfXX0FKyk8SMzLjZ536IPwMFAUQNXPr+GAsdqeOwshEC2QYAoPOWAiQm0EF/
231 FWIAQUplk7JWbyRKAJ92ZJyJpWfzb0yc1s7MY65r2qEHrg==
232 =1Xvv
233 -----END PGP SIGNATURE-----
234 ")
235
236 ;; Two clear text signatures in a row
237 (define msg_clsclss_asc_multiple (string-append msg_cls_asc msg_clss_asc))
238
239
240 ;; An Ed25519 cleartext message with an R parameter of only 247 bits
241 ;; so that the code to re-insert the stripped zero byte kicks in.  The
242 ;; S parameter has 253 bits but that does not strip a full byte.
243 (define msg_ed25519_rshort "
244 -----BEGIN PGP SIGNED MESSAGE-----
245 Hash: SHA256
246
247 Dear Emily:
248         I'm still confused as to what groups articles should be posted
249 to.  How about an example?
250                 -- Still Confused
251
252 Dear Still:
253         Ok.  Let's say you want to report that Gretzky has been traded from
254 the Oilers to the Kings.  Now right away you might think rec.sport.hockey
255 would be enough.  WRONG.  Many more people might be interested.  This is a
256 big trade!  Since it's a NEWS article, it belongs in the news.* hierarchy
257 as well.  If you are a news admin, or there is one on your machine, try
258 news.admin.  If not, use news.misc.
259         The Oilers are probably interested in geology, so try sci.physics.
260 He is a big star, so post to sci.astro, and sci.space because they are also
261 interested in stars.  Next, his name is Polish sounding.  So post to
262 soc.culture.polish.  But that group doesn't exist, so cross-post to
263 news.groups suggesting it should be created.  With this many groups of
264 interest, your article will be quite bizarre, so post to talk.bizarre as
265 well.  (And post to comp.std.mumps, since they hardly get any articles
266 there, and a \"comp\" group will propagate your article further.)
267         You may also find it is more fun to post the article once in each
268 group.  If you list all the newsgroups in the same article, some newsreaders
269 will only show the the article to the reader once!  Don't tolerate this.
270                 -- Emily Postnews Answers Your Questions on Netiquette
271 -----BEGIN PGP SIGNATURE-----
272
273 iJEEARYIADoWIQSyHeq0+HX7PaQvHR0TlWNoKgINCgUCV772DhwccGF0cmljZS5s
274 dW11bWJhQGV4YW1wbGUubmV0AAoJEBOVY2gqAg0KMAIA90EtUwAja0iJGpO91wyz
275 GLh9pS5v495V0r94yU6uUyUA/RT/StyPWe1wbnEZuacZnLbUV6Yy/aTXCVAlxf0r
276 TusO
277 =vQ3f
278 -----END PGP SIGNATURE-----
279 ")
280
281 ;; An Ed25519 cleartext message with an S parameter of only 248 bits
282 ;; so that the code to re-insert the stripped zero byte kicks in.
283 (define msg_ed25519_sshort "
284 -----BEGIN PGP SIGNED MESSAGE-----
285 Hash: SHA256
286
287 All articles that coruscate with resplendence are not truly auriferous.
288 -----BEGIN PGP SIGNATURE-----
289
290 iJEEARYIADoWIQSyHeq0+HX7PaQvHR0TlWNoKgINCgUCV771QhwccGF0cmljZS5s
291 dW11bWJhQGV4YW1wbGUubmV0AAoJEBOVY2gqAg0KHVEBAI66OPDYXKWO3r6SaFT+
292 uxmh8x4ZerW41vMA9gkJ4AEKAPjoe/Z7fDqo1lCptIFutFAGbfNxcm/53prfx2fT
293 GisM
294 =L7sk
295 -----END PGP SIGNATURE-----
296 ")
297
298
299
300 ;; Fixme:  We need more tests with manipulated cleartext signatures.
301
302 ;;
303 ;; Now run the tests.
304 ;;
305 (for-each-p
306  "Checking that a valid signature is verified as such"
307  (lambda (armored-file)
308    (pipe:do
309     (pipe:echo (eval armored-file (current-environment)))
310     (pipe:spawn `(,@GPG --verify))))
311  '(msg_ols_asc msg_cols_asc msg_sl_asc msg_oolss_asc msg_cls_asc msg_clss_asc))
312
313 (for-each-p
314  "Checking that a valid signature over multiple messages is verified as such"
315  (lambda (armored-file)
316    (pipe:do
317     (pipe:echo (eval armored-file (current-environment)))
318     (pipe:spawn `(,@GPG --verify --allow-multiple-messages)))
319    (catch '()
320           (pipe:do
321            (pipe:defer (lambda (sink)
322                          (display armored-file (fdopen sink "w"))))
323            (pipe:spawn `(,@GPG --verify)))
324           (error "verification succeeded but should not")))
325  '(msg_olsols_asc_multiple msg_clsclss_asc_multiple))
326
327 (for-each-p
328  "Checking that an invalid signature is verified as such"
329  (lambda (armored-file)
330    (catch '()
331           (pipe:do
332            (pipe:echo (eval armored-file (current-environment)))
333            (pipe:spawn `(,@GPG --verify)))
334           (error "verification succeeded but should not")))
335  '(bad_ls_asc bad_fols_asc bad_olsf_asc bad_ools_asc))
336
337
338 ;;; Need to import the ed25519 sample key used for
339 ;;; the next two tests.
340 (call-check `(,@GPG --quiet --yes --import ,(in-srcdir key-file2)))
341 (for-each-p
342  "Checking that a valid Ed25519 signature is verified as such"
343  (lambda (armored-file)
344    (pipe:do
345     (pipe:echo (eval armored-file (current-environment)))
346     (pipe:spawn `(,@GPG --verify))))
347  '(msg_ed25519_rshort msg_ed25519_sshort))