OpenSSL: update to 1.0.2a
[tomato.git] / release / src / router / openssl / test / cms-test.pl
blob51abeef2c9cb21ba100a99a7998db9a9b306154b
1 # test/cms-test.pl
2 # Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3 # project.
5 # ====================================================================
6 # Copyright (c) 2008 The OpenSSL Project. All rights reserved.
8 # Redistribution and use in source and binary forms, with or without
9 # modification, are permitted provided that the following conditions
10 # are met:
12 # 1. Redistributions of source code must retain the above copyright
13 # notice, this list of conditions and the following disclaimer.
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.
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/)"
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.
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.
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/)"
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 # ====================================================================
53 # CMS, PKCS7 consistency test script. Run extensive tests on
54 # OpenSSL PKCS#7 and CMS implementations.
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 $null_path = "NL:";
62 # On VMS, the lowest 3 bits of the exit code indicates severity
63 # 1 is success (perl translates it to 0 for $?), 2 is error
64 # (perl doesn't translate it)
65 $failure_code = 512; # 2 << 8 = 512
67 # Make MSYS work
68 elsif ( $^O eq "MSWin32" && -f "../apps/openssl.exe" ) {
69 $ossl_path = "cmd /c ..\\apps\\openssl";
70 $null_path = "NUL";
71 $failure_code = 256;
73 elsif ( -f "../apps/openssl$ENV{EXE_EXT}" ) {
74 $ossl_path = "../util/shlib_wrap.sh ../apps/openssl";
75 $null_path = "/dev/null";
76 $failure_code = 256;
78 elsif ( -f "..\\out32dll\\openssl.exe" ) {
79 $ossl_path = "..\\out32dll\\openssl.exe";
80 $null_path = "NUL";
81 $failure_code = 256;
83 elsif ( -f "..\\out32\\openssl.exe" ) {
84 $ossl_path = "..\\out32\\openssl.exe";
85 $null_path = "NUL";
86 $failure_code = 256;
88 else {
89 die "Can't find OpenSSL executable";
92 my $pk7cmd = "$ossl_path smime ";
93 my $cmscmd = "$ossl_path cms ";
94 my $smdir = "smime-certs";
95 my $halt_err = 1;
97 my $badcmd = 0;
98 my $no_ec;
99 my $no_ec2m;
100 my $no_ecdh;
101 my $ossl8 = `$ossl_path version -v` =~ /0\.9\.8/;
103 system ("$ossl_path no-ec > $null_path");
104 if ($? == 0)
106 $no_ec = 1;
108 elsif ($? == $failure_code)
110 $no_ec = 0;
112 else
114 die "Error checking for EC support\n";
117 system ("$ossl_path no-ec2m > $null_path");
118 if ($? == 0)
120 $no_ec2m = 1;
122 elsif ($? == $failure_code)
124 $no_ec2m = 0;
126 else
128 die "Error checking for EC2M support\n";
131 system ("$ossl_path no-ecdh > $null_path");
132 if ($? == 0)
134 $no_ecdh = 1;
136 elsif ($? == $failure_code)
138 $no_ecdh = 0;
140 else
142 die "Error checking for ECDH support\n";
145 my @smime_pkcs7_tests = (
148 "signed content DER format, RSA key",
149 "-sign -in smcont.txt -outform \"DER\" -nodetach"
150 . " -certfile $smdir/smroot.pem"
151 . " -signer $smdir/smrsa1.pem -out test.cms",
152 "-verify -in test.cms -inform \"DER\" "
153 . " \"-CAfile\" $smdir/smroot.pem -out smtst.txt"
157 "signed detached content DER format, RSA key",
158 "-sign -in smcont.txt -outform \"DER\""
159 . " -signer $smdir/smrsa1.pem -out test.cms",
160 "-verify -in test.cms -inform \"DER\" "
161 . " \"-CAfile\" $smdir/smroot.pem -out smtst.txt -content smcont.txt"
165 "signed content test streaming BER format, RSA",
166 "-sign -in smcont.txt -outform \"DER\" -nodetach"
167 . " -stream -signer $smdir/smrsa1.pem -out test.cms",
168 "-verify -in test.cms -inform \"DER\" "
169 . " \"-CAfile\" $smdir/smroot.pem -out smtst.txt"
173 "signed content DER format, DSA key",
174 "-sign -in smcont.txt -outform \"DER\" -nodetach"
175 . " -signer $smdir/smdsa1.pem -out test.cms",
176 "-verify -in test.cms -inform \"DER\" "
177 . " \"-CAfile\" $smdir/smroot.pem -out smtst.txt"
181 "signed detached content DER format, DSA key",
182 "-sign -in smcont.txt -outform \"DER\""
183 . " -signer $smdir/smdsa1.pem -out test.cms",
184 "-verify -in test.cms -inform \"DER\" "
185 . " \"-CAfile\" $smdir/smroot.pem -out smtst.txt -content smcont.txt"
189 "signed detached content DER format, add RSA signer",
190 "-resign -inform \"DER\" -in test.cms -outform \"DER\""
191 . " -signer $smdir/smrsa1.pem -out test2.cms",
192 "-verify -in test2.cms -inform \"DER\" "
193 . " \"-CAfile\" $smdir/smroot.pem -out smtst.txt -content smcont.txt"
197 "signed content test streaming BER format, DSA key",
198 "-sign -in smcont.txt -outform \"DER\" -nodetach"
199 . " -stream -signer $smdir/smdsa1.pem -out test.cms",
200 "-verify -in test.cms -inform \"DER\" "
201 . " \"-CAfile\" $smdir/smroot.pem -out smtst.txt"
205 "signed content test streaming BER format, 2 DSA and 2 RSA keys",
206 "-sign -in smcont.txt -outform \"DER\" -nodetach"
207 . " -signer $smdir/smrsa1.pem -signer $smdir/smrsa2.pem"
208 . " -signer $smdir/smdsa1.pem -signer $smdir/smdsa2.pem"
209 . " -stream -out test.cms",
210 "-verify -in test.cms -inform \"DER\" "
211 . " \"-CAfile\" $smdir/smroot.pem -out smtst.txt"
215 "signed content test streaming BER format, 2 DSA and 2 RSA keys, no attributes",
216 "-sign -in smcont.txt -outform \"DER\" -noattr -nodetach"
217 . " -signer $smdir/smrsa1.pem -signer $smdir/smrsa2.pem"
218 . " -signer $smdir/smdsa1.pem -signer $smdir/smdsa2.pem"
219 . " -stream -out test.cms",
220 "-verify -in test.cms -inform \"DER\" "
221 . " \"-CAfile\" $smdir/smroot.pem -out smtst.txt"
225 "signed content test streaming S/MIME format, 2 DSA and 2 RSA keys",
226 "-sign -in smcont.txt -nodetach"
227 . " -signer $smdir/smrsa1.pem -signer $smdir/smrsa2.pem"
228 . " -signer $smdir/smdsa1.pem -signer $smdir/smdsa2.pem"
229 . " -stream -out test.cms",
230 "-verify -in test.cms " . " \"-CAfile\" $smdir/smroot.pem -out smtst.txt"
234 "signed content test streaming multipart S/MIME format, 2 DSA and 2 RSA keys",
235 "-sign -in smcont.txt"
236 . " -signer $smdir/smrsa1.pem -signer $smdir/smrsa2.pem"
237 . " -signer $smdir/smdsa1.pem -signer $smdir/smdsa2.pem"
238 . " -stream -out test.cms",
239 "-verify -in test.cms " . " \"-CAfile\" $smdir/smroot.pem -out smtst.txt"
243 "enveloped content test streaming S/MIME format, 3 recipients",
244 "-encrypt -in smcont.txt"
245 . " -stream -out test.cms"
246 . " $smdir/smrsa1.pem $smdir/smrsa2.pem $smdir/smrsa3.pem ",
247 "-decrypt -recip $smdir/smrsa1.pem -in test.cms -out smtst.txt"
251 "enveloped content test streaming S/MIME format, 3 recipients, 3rd used",
252 "-encrypt -in smcont.txt"
253 . " -stream -out test.cms"
254 . " $smdir/smrsa1.pem $smdir/smrsa2.pem $smdir/smrsa3.pem ",
255 "-decrypt -recip $smdir/smrsa3.pem -in test.cms -out smtst.txt"
259 "enveloped content test streaming S/MIME format, 3 recipients, key only used",
260 "-encrypt -in smcont.txt"
261 . " -stream -out test.cms"
262 . " $smdir/smrsa1.pem $smdir/smrsa2.pem $smdir/smrsa3.pem ",
263 "-decrypt -inkey $smdir/smrsa3.pem -in test.cms -out smtst.txt"
267 "enveloped content test streaming S/MIME format, AES-256 cipher, 3 recipients",
268 "-encrypt -in smcont.txt"
269 . " -aes256 -stream -out test.cms"
270 . " $smdir/smrsa1.pem $smdir/smrsa2.pem $smdir/smrsa3.pem ",
271 "-decrypt -recip $smdir/smrsa1.pem -in test.cms -out smtst.txt"
276 my @smime_cms_tests = (
279 "signed content test streaming BER format, 2 DSA and 2 RSA keys, keyid",
280 "-sign -in smcont.txt -outform \"DER\" -nodetach -keyid"
281 . " -signer $smdir/smrsa1.pem -signer $smdir/smrsa2.pem"
282 . " -signer $smdir/smdsa1.pem -signer $smdir/smdsa2.pem"
283 . " -stream -out test.cms",
284 "-verify -in test.cms -inform \"DER\" "
285 . " \"-CAfile\" $smdir/smroot.pem -out smtst.txt"
289 "signed content test streaming PEM format, 2 DSA and 2 RSA keys",
290 "-sign -in smcont.txt -outform PEM -nodetach"
291 . " -signer $smdir/smrsa1.pem -signer $smdir/smrsa2.pem"
292 . " -signer $smdir/smdsa1.pem -signer $smdir/smdsa2.pem"
293 . " -stream -out test.cms",
294 "-verify -in test.cms -inform PEM "
295 . " \"-CAfile\" $smdir/smroot.pem -out smtst.txt"
299 "signed content MIME format, RSA key, signed receipt request",
300 "-sign -in smcont.txt -signer $smdir/smrsa1.pem -nodetach"
301 . " -receipt_request_to test\@openssl.org -receipt_request_all"
302 . " -out test.cms",
303 "-verify -in test.cms "
304 . " \"-CAfile\" $smdir/smroot.pem -out smtst.txt"
308 "signed receipt MIME format, RSA key",
309 "-sign_receipt -in test.cms"
310 . " -signer $smdir/smrsa2.pem"
311 . " -out test2.cms",
312 "-verify_receipt test2.cms -in test.cms"
313 . " \"-CAfile\" $smdir/smroot.pem"
317 "enveloped content test streaming S/MIME format, 3 recipients, keyid",
318 "-encrypt -in smcont.txt"
319 . " -stream -out test.cms -keyid"
320 . " $smdir/smrsa1.pem $smdir/smrsa2.pem $smdir/smrsa3.pem ",
321 "-decrypt -recip $smdir/smrsa1.pem -in test.cms -out smtst.txt"
325 "enveloped content test streaming PEM format, KEK",
326 "-encrypt -in smcont.txt -outform PEM -aes128"
327 . " -stream -out test.cms "
328 . " -secretkey 000102030405060708090A0B0C0D0E0F "
329 . " -secretkeyid C0FEE0",
330 "-decrypt -in test.cms -out smtst.txt -inform PEM"
331 . " -secretkey 000102030405060708090A0B0C0D0E0F "
332 . " -secretkeyid C0FEE0"
336 "enveloped content test streaming PEM format, KEK, key only",
337 "-encrypt -in smcont.txt -outform PEM -aes128"
338 . " -stream -out test.cms "
339 . " -secretkey 000102030405060708090A0B0C0D0E0F "
340 . " -secretkeyid C0FEE0",
341 "-decrypt -in test.cms -out smtst.txt -inform PEM"
342 . " -secretkey 000102030405060708090A0B0C0D0E0F "
346 "data content test streaming PEM format",
347 "-data_create -in smcont.txt -outform PEM -nodetach"
348 . " -stream -out test.cms",
349 "-data_out -in test.cms -inform PEM -out smtst.txt"
353 "encrypted content test streaming PEM format, 128 bit RC2 key",
354 "\"-EncryptedData_encrypt\" -in smcont.txt -outform PEM"
355 . " -rc2 -secretkey 000102030405060708090A0B0C0D0E0F"
356 . " -stream -out test.cms",
357 "\"-EncryptedData_decrypt\" -in test.cms -inform PEM "
358 . " -secretkey 000102030405060708090A0B0C0D0E0F -out smtst.txt"
362 "encrypted content test streaming PEM format, 40 bit RC2 key",
363 "\"-EncryptedData_encrypt\" -in smcont.txt -outform PEM"
364 . " -rc2 -secretkey 0001020304"
365 . " -stream -out test.cms",
366 "\"-EncryptedData_decrypt\" -in test.cms -inform PEM "
367 . " -secretkey 0001020304 -out smtst.txt"
371 "encrypted content test streaming PEM format, triple DES key",
372 "\"-EncryptedData_encrypt\" -in smcont.txt -outform PEM"
373 . " -des3 -secretkey 000102030405060708090A0B0C0D0E0F1011121314151617"
374 . " -stream -out test.cms",
375 "\"-EncryptedData_decrypt\" -in test.cms -inform PEM "
376 . " -secretkey 000102030405060708090A0B0C0D0E0F1011121314151617"
377 . " -out smtst.txt"
381 "encrypted content test streaming PEM format, 128 bit AES key",
382 "\"-EncryptedData_encrypt\" -in smcont.txt -outform PEM"
383 . " -aes128 -secretkey 000102030405060708090A0B0C0D0E0F"
384 . " -stream -out test.cms",
385 "\"-EncryptedData_decrypt\" -in test.cms -inform PEM "
386 . " -secretkey 000102030405060708090A0B0C0D0E0F -out smtst.txt"
391 my @smime_cms_comp_tests = (
394 "compressed content test streaming PEM format",
395 "-compress -in smcont.txt -outform PEM -nodetach"
396 . " -stream -out test.cms",
397 "-uncompress -in test.cms -inform PEM -out smtst.txt"
402 my @smime_cms_param_tests = (
404 "signed content test streaming PEM format, RSA keys, PSS signature",
405 "-sign -in smcont.txt -outform PEM -nodetach"
406 . " -signer $smdir/smrsa1.pem -keyopt rsa_padding_mode:pss"
407 . " -out test.cms",
408 "-verify -in test.cms -inform PEM "
409 . " \"-CAfile\" $smdir/smroot.pem -out smtst.txt"
413 "signed content test streaming PEM format, RSA keys, PSS signature, no attributes",
414 "-sign -in smcont.txt -outform PEM -nodetach -noattr"
415 . " -signer $smdir/smrsa1.pem -keyopt rsa_padding_mode:pss"
416 . " -out test.cms",
417 "-verify -in test.cms -inform PEM "
418 . " \"-CAfile\" $smdir/smroot.pem -out smtst.txt"
422 "signed content test streaming PEM format, RSA keys, PSS signature, SHA384 MGF1",
423 "-sign -in smcont.txt -outform PEM -nodetach"
424 . " -signer $smdir/smrsa1.pem -keyopt rsa_padding_mode:pss"
425 . " -keyopt rsa_mgf1_md:sha384 -out test.cms",
426 "-verify -in test.cms -inform PEM "
427 . " \"-CAfile\" $smdir/smroot.pem -out smtst.txt"
431 "enveloped content test streaming S/MIME format, OAEP default parameters",
432 "-encrypt -in smcont.txt"
433 . " -stream -out test.cms"
434 . " -recip $smdir/smrsa1.pem -keyopt rsa_padding_mode:oaep",
435 "-decrypt -recip $smdir/smrsa1.pem -in test.cms -out smtst.txt"
439 "enveloped content test streaming S/MIME format, OAEP SHA256",
440 "-encrypt -in smcont.txt"
441 . " -stream -out test.cms"
442 . " -recip $smdir/smrsa1.pem -keyopt rsa_padding_mode:oaep"
443 . " -keyopt rsa_oaep_md:sha256",
444 "-decrypt -recip $smdir/smrsa1.pem -in test.cms -out smtst.txt"
448 "enveloped content test streaming S/MIME format, ECDH",
449 "-encrypt -in smcont.txt"
450 . " -stream -out test.cms"
451 . " -recip $smdir/smec1.pem",
452 "-decrypt -recip $smdir/smec1.pem -in test.cms -out smtst.txt"
456 "enveloped content test streaming S/MIME format, ECDH, AES128, SHA256 KDF",
457 "-encrypt -in smcont.txt"
458 . " -stream -out test.cms"
459 . " -recip $smdir/smec1.pem -aes128 -keyopt ecdh_kdf_md:sha256",
460 "-decrypt -recip $smdir/smec1.pem -in test.cms -out smtst.txt"
464 "enveloped content test streaming S/MIME format, ECDH, K-283, cofactor DH",
465 "-encrypt -in smcont.txt"
466 . " -stream -out test.cms"
467 . " -recip $smdir/smec2.pem -aes128"
468 . " -keyopt ecdh_kdf_md:sha256 -keyopt ecdh_cofactor_mode:1",
469 "-decrypt -recip $smdir/smec2.pem -in test.cms -out smtst.txt"
473 "enveloped content test streaming S/MIME format, X9.42 DH",
474 "-encrypt -in smcont.txt"
475 . " -stream -out test.cms"
476 . " -recip $smdir/smdh.pem -aes128",
477 "-decrypt -recip $smdir/smdh.pem -in test.cms -out smtst.txt"
481 print "CMS => PKCS#7 compatibility tests\n";
483 run_smime_tests( \$badcmd, \@smime_pkcs7_tests, $cmscmd, $pk7cmd );
485 print "CMS <= PKCS#7 compatibility tests\n";
487 run_smime_tests( \$badcmd, \@smime_pkcs7_tests, $pk7cmd, $cmscmd );
489 print "CMS <=> CMS consistency tests\n";
491 run_smime_tests( \$badcmd, \@smime_pkcs7_tests, $cmscmd, $cmscmd );
492 run_smime_tests( \$badcmd, \@smime_cms_tests, $cmscmd, $cmscmd );
494 print "CMS <=> CMS consistency tests, modified key parameters\n";
495 run_smime_tests( \$badcmd, \@smime_cms_param_tests, $cmscmd, $cmscmd );
497 if ( `$ossl_path version -f` =~ /ZLIB/ ) {
498 run_smime_tests( \$badcmd, \@smime_cms_comp_tests, $cmscmd, $cmscmd );
500 else {
501 print "Zlib not supported: compression tests skipped\n";
504 print "Running modified tests for OpenSSL 0.9.8 cms backport\n" if($ossl8);
506 if ($badcmd) {
507 print "$badcmd TESTS FAILED!!\n";
509 else {
510 print "ALL TESTS SUCCESSFUL.\n";
513 unlink "test.cms";
514 unlink "test2.cms";
515 unlink "smtst.txt";
516 unlink "cms.out";
517 unlink "cms.err";
519 sub run_smime_tests {
520 my ( $rv, $aref, $scmd, $vcmd ) = @_;
522 foreach $smtst (@$aref) {
523 my ( $tnam, $rscmd, $rvcmd ) = @$smtst;
524 if ($ossl8)
526 # Skip smime resign: 0.9.8 smime doesn't support -resign
527 next if ($scmd =~ /smime/ && $rscmd =~ /-resign/);
528 # Disable streaming: option not supported in 0.9.8
529 $tnam =~ s/streaming//;
530 $rscmd =~ s/-stream//;
531 $rvcmd =~ s/-stream//;
533 if ($no_ec && $tnam =~ /ECDH/)
535 print "$tnam: skipped, EC disabled\n";
536 next;
538 if ($no_ecdh && $tnam =~ /ECDH/)
540 print "$tnam: skipped, ECDH disabled\n";
541 next;
543 if ($no_ec2m && $tnam =~ /K-283/)
545 print "$tnam: skipped, EC2M disabled\n";
546 next;
548 system("$scmd$rscmd$redir");
549 if ($?) {
550 print "$tnam: generation error\n";
551 $$rv++;
552 exit 1 if $halt_err;
553 next;
555 system("$vcmd$rvcmd$redir");
556 if ($?) {
557 print "$tnam: verify error\n";
558 $$rv++;
559 exit 1 if $halt_err;
560 next;
562 if (!cmp_files("smtst.txt", "smcont.txt")) {
563 print "$tnam: content verify error\n";
564 $$rv++;
565 exit 1 if $halt_err;
566 next;
568 print "$tnam: OK\n";
572 sub cmp_files {
573 use FileHandle;
574 my ( $f1, $f2 ) = @_;
575 my $fp1 = FileHandle->new();
576 my $fp2 = FileHandle->new();
578 my ( $rd1, $rd2 );
580 if ( !open( $fp1, "<$f1" ) ) {
581 print STDERR "Can't Open file $f1\n";
582 return 0;
585 if ( !open( $fp2, "<$f2" ) ) {
586 print STDERR "Can't Open file $f2\n";
587 return 0;
590 binmode $fp1;
591 binmode $fp2;
593 my $ret = 0;
595 for ( ; ; ) {
596 $n1 = sysread $fp1, $rd1, 4096;
597 $n2 = sysread $fp2, $rd2, 4096;
598 last if ( $n1 != $n2 );
599 last if ( $rd1 ne $rd2 );
601 if ( $n1 == 0 ) {
602 $ret = 1;
603 last;
608 close $fp1;
609 close $fp2;
611 return $ret;