Fix for CVE-2014-0076
[openssl.git] / test / cms-test.pl
1 # test/cms-test.pl
2 # Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3 # project.
4 #
5 # ====================================================================
6 # Copyright (c) 2008 The OpenSSL Project.  All rights reserved.
7 #
8 # Redistribution and use in source and binary forms, with or without
9 # modification, are permitted provided that the following conditions
10 # are met:
11 #
12 # 1. Redistributions of source code must retain the above copyright
13 #    notice, this list of conditions and the following disclaimer.
14 #
15 # 2. Redistributions in binary form must reproduce the above copyright
16 #    notice, this list of conditions and the following disclaimer in
17 #    the documentation and/or other materials provided with the
18 #    distribution.
19 #
20 # 3. All advertising materials mentioning features or use of this
21 #    software must display the following acknowledgment:
22 #    "This product includes software developed by the OpenSSL Project
23 #    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
24 #
25 # 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26 #    endorse or promote products derived from this software without
27 #    prior written permission. For written permission, please contact
28 #    licensing@OpenSSL.org.
29 #
30 # 5. Products derived from this software may not be called "OpenSSL"
31 #    nor may "OpenSSL" appear in their names without prior written
32 #    permission of the OpenSSL Project.
33 #
34 # 6. Redistributions of any form whatsoever must retain the following
35 #    acknowledgment:
36 #    "This product includes software developed by the OpenSSL Project
37 #    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
38 #
39 # THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40 # EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42 # PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
43 # ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45 # NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48 # STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49 # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50 # OF THE POSSIBILITY OF SUCH DAMAGE.
51 # ====================================================================
52
53 # CMS, PKCS7 consistency test script. Run extensive tests on
54 # OpenSSL PKCS#7 and CMS implementations.
55
56 my $ossl_path;
57 my $redir = " 2> cms.err > cms.out";
58 # Make VMS work
59 if ( $^O eq "VMS" && -f "OSSLX:openssl.exe" ) {
60     $ossl_path = "pipe mcr OSSLX:openssl";
61 }
62 # Make MSYS work
63 elsif ( $^O eq "MSWin32" && -f "../apps/openssl.exe" ) {
64     $ossl_path = "cmd /c ..\\apps\\openssl";
65 }
66 elsif ( -f "../apps/openssl$ENV{EXE_EXT}" ) {
67     $ossl_path = "../util/shlib_wrap.sh ../apps/openssl";
68 }
69 elsif ( -f "..\\out32dll\\openssl.exe" ) {
70     $ossl_path = "..\\out32dll\\openssl.exe";
71 }
72 elsif ( -f "..\\out32\\openssl.exe" ) {
73     $ossl_path = "..\\out32\\openssl.exe";
74 }
75 else {
76     die "Can't find OpenSSL executable";
77 }
78
79 my $pk7cmd   = "$ossl_path smime ";
80 my $cmscmd   = "$ossl_path cms ";
81 my $smdir    = "smime-certs";
82 my $halt_err = 1;
83
84 my $badcmd = 0;
85 my $no_ec;
86 my $ossl8 = `$ossl_path version -v` =~ /0\.9\.8/;
87
88 system ("$ossl_path no-ec >/dev/null");
89 if ($? == 0)
90         {
91         $no_ec = 1;
92         }
93 elsif ($? == 256)
94         {
95         $no_ec = 0;
96         }
97 else
98         {
99         die "Error checking for EC support\n";
100         }
101
102 my @smime_pkcs7_tests = (
103
104     [
105         "signed content DER format, RSA key",
106         "-sign -in smcont.txt -outform \"DER\" -nodetach"
107           . " -certfile $smdir/smroot.pem"
108           . " -signer $smdir/smrsa1.pem -out test.cms",
109         "-verify -in test.cms -inform \"DER\" "
110           . " \"-CAfile\" $smdir/smroot.pem -out smtst.txt"
111     ],
112
113     [
114         "signed detached content DER format, RSA key",
115         "-sign -in smcont.txt -outform \"DER\""
116           . " -signer $smdir/smrsa1.pem -out test.cms",
117         "-verify -in test.cms -inform \"DER\" "
118           . " \"-CAfile\" $smdir/smroot.pem -out smtst.txt -content smcont.txt"
119     ],
120
121     [
122         "signed content test streaming BER format, RSA",
123         "-sign -in smcont.txt -outform \"DER\" -nodetach"
124           . " -stream -signer $smdir/smrsa1.pem -out test.cms",
125         "-verify -in test.cms -inform \"DER\" "
126           . " \"-CAfile\" $smdir/smroot.pem -out smtst.txt"
127     ],
128
129     [
130         "signed content DER format, DSA key",
131         "-sign -in smcont.txt -outform \"DER\" -nodetach"
132           . " -signer $smdir/smdsa1.pem -out test.cms",
133         "-verify -in test.cms -inform \"DER\" "
134           . " \"-CAfile\" $smdir/smroot.pem -out smtst.txt"
135     ],
136
137     [
138         "signed detached content DER format, DSA key",
139         "-sign -in smcont.txt -outform \"DER\""
140           . " -signer $smdir/smdsa1.pem -out test.cms",
141         "-verify -in test.cms -inform \"DER\" "
142           . " \"-CAfile\" $smdir/smroot.pem -out smtst.txt -content smcont.txt"
143     ],
144
145     [
146         "signed detached content DER format, add RSA signer",
147         "-resign -inform \"DER\" -in test.cms -outform \"DER\""
148           . " -signer $smdir/smrsa1.pem -out test2.cms",
149         "-verify -in test2.cms -inform \"DER\" "
150           . " \"-CAfile\" $smdir/smroot.pem -out smtst.txt -content smcont.txt"
151     ],
152
153     [
154         "signed content test streaming BER format, DSA key",
155         "-sign -in smcont.txt -outform \"DER\" -nodetach"
156           . " -stream -signer $smdir/smdsa1.pem -out test.cms",
157         "-verify -in test.cms -inform \"DER\" "
158           . " \"-CAfile\" $smdir/smroot.pem -out smtst.txt"
159     ],
160
161     [
162         "signed content test streaming BER format, 2 DSA and 2 RSA keys",
163         "-sign -in smcont.txt -outform \"DER\" -nodetach"
164           . " -signer $smdir/smrsa1.pem -signer $smdir/smrsa2.pem"
165           . " -signer $smdir/smdsa1.pem -signer $smdir/smdsa2.pem"
166           . " -stream -out test.cms",
167         "-verify -in test.cms -inform \"DER\" "
168           . " \"-CAfile\" $smdir/smroot.pem -out smtst.txt"
169     ],
170
171     [
172 "signed content test streaming BER format, 2 DSA and 2 RSA keys, no attributes",
173         "-sign -in smcont.txt -outform \"DER\" -noattr -nodetach"
174           . " -signer $smdir/smrsa1.pem -signer $smdir/smrsa2.pem"
175           . " -signer $smdir/smdsa1.pem -signer $smdir/smdsa2.pem"
176           . " -stream -out test.cms",
177         "-verify -in test.cms -inform \"DER\" "
178           . " \"-CAfile\" $smdir/smroot.pem -out smtst.txt"
179     ],
180
181     [
182         "signed content test streaming S/MIME format, 2 DSA and 2 RSA keys",
183         "-sign -in smcont.txt -nodetach"
184           . " -signer $smdir/smrsa1.pem -signer $smdir/smrsa2.pem"
185           . " -signer $smdir/smdsa1.pem -signer $smdir/smdsa2.pem"
186           . " -stream -out test.cms",
187         "-verify -in test.cms " . " \"-CAfile\" $smdir/smroot.pem -out smtst.txt"
188     ],
189
190     [
191 "signed content test streaming multipart S/MIME format, 2 DSA and 2 RSA keys",
192         "-sign -in smcont.txt"
193           . " -signer $smdir/smrsa1.pem -signer $smdir/smrsa2.pem"
194           . " -signer $smdir/smdsa1.pem -signer $smdir/smdsa2.pem"
195           . " -stream -out test.cms",
196         "-verify -in test.cms " . " \"-CAfile\" $smdir/smroot.pem -out smtst.txt"
197     ],
198
199     [
200         "enveloped content test streaming S/MIME format, 3 recipients",
201         "-encrypt -in smcont.txt"
202           . " -stream -out test.cms"
203           . " $smdir/smrsa1.pem $smdir/smrsa2.pem $smdir/smrsa3.pem ",
204         "-decrypt -recip $smdir/smrsa1.pem -in test.cms -out smtst.txt"
205     ],
206
207     [
208 "enveloped content test streaming S/MIME format, 3 recipients, 3rd used",
209         "-encrypt -in smcont.txt"
210           . " -stream -out test.cms"
211           . " $smdir/smrsa1.pem $smdir/smrsa2.pem $smdir/smrsa3.pem ",
212         "-decrypt -recip $smdir/smrsa3.pem -in test.cms -out smtst.txt"
213     ],
214
215     [
216 "enveloped content test streaming S/MIME format, 3 recipients, key only used",
217         "-encrypt -in smcont.txt"
218           . " -stream -out test.cms"
219           . " $smdir/smrsa1.pem $smdir/smrsa2.pem $smdir/smrsa3.pem ",
220         "-decrypt -inkey $smdir/smrsa3.pem -in test.cms -out smtst.txt"
221     ],
222
223     [
224 "enveloped content test streaming S/MIME format, AES-256 cipher, 3 recipients",
225         "-encrypt -in smcont.txt"
226           . " -aes256 -stream -out test.cms"
227           . " $smdir/smrsa1.pem $smdir/smrsa2.pem $smdir/smrsa3.pem ",
228         "-decrypt -recip $smdir/smrsa1.pem -in test.cms -out smtst.txt"
229     ],
230
231 );
232
233 my @smime_cms_tests = (
234
235     [
236         "signed content test streaming BER format, 2 DSA and 2 RSA keys, keyid",
237         "-sign -in smcont.txt -outform \"DER\" -nodetach -keyid"
238           . " -signer $smdir/smrsa1.pem -signer $smdir/smrsa2.pem"
239           . " -signer $smdir/smdsa1.pem -signer $smdir/smdsa2.pem"
240           . " -stream -out test.cms",
241         "-verify -in test.cms -inform \"DER\" "
242           . " \"-CAfile\" $smdir/smroot.pem -out smtst.txt"
243     ],
244
245     [
246         "signed content test streaming PEM format, 2 DSA and 2 RSA keys",
247         "-sign -in smcont.txt -outform PEM -nodetach"
248           . " -signer $smdir/smrsa1.pem -signer $smdir/smrsa2.pem"
249           . " -signer $smdir/smdsa1.pem -signer $smdir/smdsa2.pem"
250           . " -stream -out test.cms",
251         "-verify -in test.cms -inform PEM "
252           . " \"-CAfile\" $smdir/smroot.pem -out smtst.txt"
253     ],
254
255     [
256         "signed content MIME format, RSA key, signed receipt request",
257         "-sign -in smcont.txt -signer $smdir/smrsa1.pem -nodetach"
258           . " -receipt_request_to test\@openssl.org -receipt_request_all"
259           . " -out test.cms",
260         "-verify -in test.cms "
261           . " \"-CAfile\" $smdir/smroot.pem -out smtst.txt"
262     ],
263
264     [
265         "signed receipt MIME format, RSA key",
266         "-sign_receipt -in test.cms"
267           . " -signer $smdir/smrsa2.pem"
268           . " -out test2.cms",
269         "-verify_receipt test2.cms -in test.cms"
270           . " \"-CAfile\" $smdir/smroot.pem"
271     ],
272
273     [
274         "enveloped content test streaming S/MIME format, 3 recipients, keyid",
275         "-encrypt -in smcont.txt"
276           . " -stream -out test.cms -keyid"
277           . " $smdir/smrsa1.pem $smdir/smrsa2.pem $smdir/smrsa3.pem ",
278         "-decrypt -recip $smdir/smrsa1.pem -in test.cms -out smtst.txt"
279     ],
280
281     [
282         "enveloped content test streaming PEM format, KEK",
283         "-encrypt -in smcont.txt -outform PEM -aes128"
284           . " -stream -out test.cms "
285           . " -secretkey 000102030405060708090A0B0C0D0E0F "
286           . " -secretkeyid C0FEE0",
287         "-decrypt -in test.cms -out smtst.txt -inform PEM"
288           . " -secretkey 000102030405060708090A0B0C0D0E0F "
289           . " -secretkeyid C0FEE0"
290     ],
291
292     [
293         "enveloped content test streaming PEM format, KEK, key only",
294         "-encrypt -in smcont.txt -outform PEM -aes128"
295           . " -stream -out test.cms "
296           . " -secretkey 000102030405060708090A0B0C0D0E0F "
297           . " -secretkeyid C0FEE0",
298         "-decrypt -in test.cms -out smtst.txt -inform PEM"
299           . " -secretkey 000102030405060708090A0B0C0D0E0F "
300     ],
301
302     [
303         "data content test streaming PEM format",
304         "-data_create -in smcont.txt -outform PEM -nodetach"
305           . " -stream -out test.cms",
306         "-data_out -in test.cms -inform PEM -out smtst.txt"
307     ],
308
309     [
310         "encrypted content test streaming PEM format, 128 bit RC2 key",
311         "\"-EncryptedData_encrypt\" -in smcont.txt -outform PEM"
312           . " -rc2 -secretkey 000102030405060708090A0B0C0D0E0F"
313           . " -stream -out test.cms",
314         "\"-EncryptedData_decrypt\" -in test.cms -inform PEM "
315           . " -secretkey 000102030405060708090A0B0C0D0E0F -out smtst.txt"
316     ],
317
318     [
319         "encrypted content test streaming PEM format, 40 bit RC2 key",
320         "\"-EncryptedData_encrypt\" -in smcont.txt -outform PEM"
321           . " -rc2 -secretkey 0001020304"
322           . " -stream -out test.cms",
323         "\"-EncryptedData_decrypt\" -in test.cms -inform PEM "
324           . " -secretkey 0001020304 -out smtst.txt"
325     ],
326
327     [
328         "encrypted content test streaming PEM format, triple DES key",
329         "\"-EncryptedData_encrypt\" -in smcont.txt -outform PEM"
330           . " -des3 -secretkey 000102030405060708090A0B0C0D0E0F1011121314151617"
331           . " -stream -out test.cms",
332         "\"-EncryptedData_decrypt\" -in test.cms -inform PEM "
333           . " -secretkey 000102030405060708090A0B0C0D0E0F1011121314151617"
334           . " -out smtst.txt"
335     ],
336
337     [
338         "encrypted content test streaming PEM format, 128 bit AES key",
339         "\"-EncryptedData_encrypt\" -in smcont.txt -outform PEM"
340           . " -aes128 -secretkey 000102030405060708090A0B0C0D0E0F"
341           . " -stream -out test.cms",
342         "\"-EncryptedData_decrypt\" -in test.cms -inform PEM "
343           . " -secretkey 000102030405060708090A0B0C0D0E0F -out smtst.txt"
344     ],
345
346 );
347
348 my @smime_cms_comp_tests = (
349
350     [
351         "compressed content test streaming PEM format",
352         "-compress -in smcont.txt -outform PEM -nodetach"
353           . " -stream -out test.cms",
354         "-uncompress -in test.cms -inform PEM -out smtst.txt"
355     ]
356
357 );
358
359 my @smime_cms_param_tests = (
360     [
361         "signed content test streaming PEM format, RSA keys, PSS signature",
362         "-sign -in smcont.txt -outform PEM -nodetach"
363           . " -signer $smdir/smrsa1.pem -keyopt rsa_padding_mode:pss"
364           . " -out test.cms",
365         "-verify -in test.cms -inform PEM "
366           . " \"-CAfile\" $smdir/smroot.pem -out smtst.txt"
367     ],
368
369     [
370         "signed content test streaming PEM format, RSA keys, PSS signature, no attributes",
371         "-sign -in smcont.txt -outform PEM -nodetach -noattr"
372           . " -signer $smdir/smrsa1.pem -keyopt rsa_padding_mode:pss"
373           . " -out test.cms",
374         "-verify -in test.cms -inform PEM "
375           . " \"-CAfile\" $smdir/smroot.pem -out smtst.txt"
376     ],
377
378     [
379         "signed content test streaming PEM format, RSA keys, PSS signature, SHA384 MGF1",
380         "-sign -in smcont.txt -outform PEM -nodetach"
381           . " -signer $smdir/smrsa1.pem -keyopt rsa_padding_mode:pss"
382           . " -keyopt rsa_mgf1_md:sha384 -out test.cms",
383         "-verify -in test.cms -inform PEM "
384           . " \"-CAfile\" $smdir/smroot.pem -out smtst.txt"
385     ],
386
387     [
388 "enveloped content test streaming S/MIME format, OAEP default parameters",
389         "-encrypt -in smcont.txt"
390           . " -stream -out test.cms"
391           . " -recip $smdir/smrsa1.pem -keyopt rsa_padding_mode:oaep",
392         "-decrypt -recip $smdir/smrsa1.pem -in test.cms -out smtst.txt"
393     ],
394
395     [
396 "enveloped content test streaming S/MIME format, OAEP SHA256",
397         "-encrypt -in smcont.txt"
398           . " -stream -out test.cms"
399           . " -recip $smdir/smrsa1.pem -keyopt rsa_padding_mode:oaep"
400           . " -keyopt rsa_oaep_md:sha256",
401         "-decrypt -recip $smdir/smrsa1.pem -in test.cms -out smtst.txt"
402     ],
403
404     [
405 "enveloped content test streaming S/MIME format, ECDH",
406         "-encrypt -in smcont.txt"
407           . " -stream -out test.cms"
408           . " -recip $smdir/smec1.pem",
409         "-decrypt -recip $smdir/smec1.pem -in test.cms -out smtst.txt"
410     ],
411
412     [
413 "enveloped content test streaming S/MIME format, ECDH, AES128, SHA256 KDF",
414         "-encrypt -in smcont.txt"
415           . " -stream -out test.cms"
416           . " -recip $smdir/smec1.pem -aes128 -keyopt ecdh_kdf_md:sha256",
417         "-decrypt -recip $smdir/smec1.pem -in test.cms -out smtst.txt"
418     ],
419
420     [
421 "enveloped content test streaming S/MIME format, ECDH, K-283, cofactor DH",
422         "-encrypt -in smcont.txt"
423           . " -stream -out test.cms"
424           . " -recip $smdir/smec2.pem -aes128"
425           . " -keyopt ecdh_kdf_md:sha256 -keyopt ecdh_cofactor_mode:1",
426         "-decrypt -recip $smdir/smec2.pem -in test.cms -out smtst.txt"
427     ],
428
429     [
430 "enveloped content test streaming S/MIME format, X9.42 DH",
431         "-encrypt -in smcont.txt"
432           . " -stream -out test.cms"
433           . " -recip $smdir/smdh.pem -aes128",
434         "-decrypt -recip $smdir/smdh.pem -in test.cms -out smtst.txt"
435     ]
436 );
437
438 print "CMS => PKCS#7 compatibility tests\n";
439
440 run_smime_tests( \$badcmd, \@smime_pkcs7_tests, $cmscmd, $pk7cmd );
441
442 print "CMS <= PKCS#7 compatibility tests\n";
443
444 run_smime_tests( \$badcmd, \@smime_pkcs7_tests, $pk7cmd, $cmscmd );
445
446 print "CMS <=> CMS consistency tests\n";
447
448 run_smime_tests( \$badcmd, \@smime_pkcs7_tests, $cmscmd, $cmscmd );
449 run_smime_tests( \$badcmd, \@smime_cms_tests,   $cmscmd, $cmscmd );
450
451 print "CMS <=> CMS consistency tests, modified key parameters\n";
452 run_smime_tests( \$badcmd, \@smime_cms_param_tests,   $cmscmd, $cmscmd );
453
454 if ( `$ossl_path version -f` =~ /ZLIB/ ) {
455     run_smime_tests( \$badcmd, \@smime_cms_comp_tests, $cmscmd, $cmscmd );
456 }
457 else {
458     print "Zlib not supported: compression tests skipped\n";
459 }
460
461 print "Running modified tests for OpenSSL 0.9.8 cms backport\n" if($ossl8);
462
463 if ($badcmd) {
464     print "$badcmd TESTS FAILED!!\n";
465 }
466 else {
467     print "ALL TESTS SUCCESSFUL.\n";
468 }
469
470 unlink "test.cms";
471 unlink "test2.cms";
472 unlink "smtst.txt";
473 unlink "cms.out";
474 unlink "cms.err";
475
476 sub run_smime_tests {
477     my ( $rv, $aref, $scmd, $vcmd ) = @_;
478
479     foreach $smtst (@$aref) {
480         my ( $tnam, $rscmd, $rvcmd ) = @$smtst;
481         if ($ossl8)
482                 {
483                 # Skip smime resign: 0.9.8 smime doesn't support -resign        
484                 next if ($scmd =~ /smime/ && $rscmd =~ /-resign/);
485                 # Disable streaming: option not supported in 0.9.8
486                 $tnam =~ s/streaming//; 
487                 $rscmd =~ s/-stream//;  
488                 $rvcmd =~ s/-stream//;
489                 }
490         if ($no_ec && $tnam =~ /ECDH/)
491                 {
492                 print "$tnam: skipped, EC disabled\n";
493                 next;
494                 }
495         system("$scmd$rscmd$redir");
496         if ($?) {
497             print "$tnam: generation error\n";
498             $$rv++;
499             exit 1 if $halt_err;
500             next;
501         }
502         system("$vcmd$rvcmd$redir");
503         if ($?) {
504             print "$tnam: verify error\n";
505             $$rv++;
506             exit 1 if $halt_err;
507             next;
508         }
509         if (!cmp_files("smtst.txt", "smcont.txt")) {
510             print "$tnam: content verify error\n";
511             $$rv++;
512             exit 1 if $halt_err;
513             next;
514         }
515         print "$tnam: OK\n";
516     }
517 }
518
519 sub cmp_files {
520     use FileHandle;
521     my ( $f1, $f2 ) = @_;
522     my $fp1 = FileHandle->new();
523     my $fp2 = FileHandle->new();
524
525     my ( $rd1, $rd2 );
526
527     if ( !open( $fp1, "<$f1" ) ) {
528         print STDERR "Can't Open file $f1\n";
529         return 0;
530     }
531
532     if ( !open( $fp2, "<$f2" ) ) {
533         print STDERR "Can't Open file $f2\n";
534         return 0;
535     }
536
537     binmode $fp1;
538     binmode $fp2;
539
540     my $ret = 0;
541
542     for ( ; ; ) {
543         $n1 = sysread $fp1, $rd1, 4096;
544         $n2 = sysread $fp2, $rd2, 4096;
545         last if ( $n1 != $n2 );
546         last if ( $rd1 ne $rd2 );
547
548         if ( $n1 == 0 ) {
549             $ret = 1;
550             last;
551         }
552
553     }
554
555     close $fp1;
556     close $fp2;
557
558     return $ret;
559
560 }
561