2 # Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
5 # ====================================================================
6 # Copyright (c) 2008-2018 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
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
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
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.
57 my $redir = " 2> cms.err > cms.out";
59 if ( $^O eq "VMS" && -f "OSSLX:openssl.exe" ) {
60 $ossl_path = "pipe mcr OSSLX:openssl";
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
68 elsif ( $^O eq "MSWin32" && -f "../apps/openssl.exe" ) {
69 $ossl_path = "cmd /c ..\\apps\\openssl";
73 elsif ( -f "../apps/openssl$ENV{EXE_EXT}" ) {
74 $ossl_path = "../util/shlib_wrap.sh ../apps/openssl";
75 $null_path = "/dev/null";
78 elsif ( -f "..\\out32dll\\openssl.exe" ) {
79 $ossl_path = "..\\out32dll\\openssl.exe";
83 elsif ( -f "..\\out32\\openssl.exe" ) {
84 $ossl_path = "..\\out32\\openssl.exe";
89 die "Can't find OpenSSL executable";
92 my $pk7cmd = "$ossl_path smime ";
93 my $cmscmd = "$ossl_path cms ";
94 my $smdir = "smime-certs";
101 my $ossl8 = `$ossl_path version -v` =~ /0\.9\.8/;
103 system ("$ossl_path no-cms > $null_path");
106 print STDERR "CMS disabled. skipping...\n";
110 system ("$ossl_path no-ec > $null_path");
115 elsif ($? == $failure_code)
121 die "Error checking for EC support\n";
124 system ("$ossl_path no-ec2m > $null_path");
129 elsif ($? == $failure_code)
135 die "Error checking for EC2M support\n";
138 system ("$ossl_path no-ec > $null_path");
143 elsif ($? == $failure_code)
149 die "Error checking for ECDH support\n";
152 my @smime_pkcs7_tests = (
155 "signed content DER format, RSA key",
156 "-sign -in smcont.txt -outform \"DER\" -nodetach"
157 . " -certfile $smdir/smroot.pem"
158 . " -signer $smdir/smrsa1.pem -out test.cms",
159 "-verify -in test.cms -inform \"DER\" "
160 . " \"-CAfile\" $smdir/smroot.pem -out smtst.txt"
164 "signed detached content DER format, RSA key",
165 "-sign -in smcont.txt -outform \"DER\""
166 . " -signer $smdir/smrsa1.pem -out test.cms",
167 "-verify -in test.cms -inform \"DER\" "
168 . " \"-CAfile\" $smdir/smroot.pem -out smtst.txt -content smcont.txt"
172 "signed content test streaming BER format, RSA",
173 "-sign -in smcont.txt -outform \"DER\" -nodetach"
174 . " -stream -signer $smdir/smrsa1.pem -out test.cms",
175 "-verify -in test.cms -inform \"DER\" "
176 . " \"-CAfile\" $smdir/smroot.pem -out smtst.txt"
180 "signed content DER format, DSA key",
181 "-sign -in smcont.txt -outform \"DER\" -nodetach"
182 . " -signer $smdir/smdsa1.pem -out test.cms",
183 "-verify -in test.cms -inform \"DER\" "
184 . " \"-CAfile\" $smdir/smroot.pem -out smtst.txt"
188 "signed detached content DER format, DSA key",
189 "-sign -in smcont.txt -outform \"DER\""
190 . " -signer $smdir/smdsa1.pem -out test.cms",
191 "-verify -in test.cms -inform \"DER\" "
192 . " \"-CAfile\" $smdir/smroot.pem -out smtst.txt -content smcont.txt"
196 "signed detached content DER format, add RSA signer",
197 "-resign -inform \"DER\" -in test.cms -outform \"DER\""
198 . " -signer $smdir/smrsa1.pem -out test2.cms",
199 "-verify -in test2.cms -inform \"DER\" "
200 . " \"-CAfile\" $smdir/smroot.pem -out smtst.txt -content smcont.txt"
204 "signed content test streaming BER format, DSA key",
205 "-sign -in smcont.txt -outform \"DER\" -nodetach"
206 . " -stream -signer $smdir/smdsa1.pem -out test.cms",
207 "-verify -in test.cms -inform \"DER\" "
208 . " \"-CAfile\" $smdir/smroot.pem -out smtst.txt"
212 "signed content test streaming BER format, 2 DSA and 2 RSA keys",
213 "-sign -in smcont.txt -outform \"DER\" -nodetach"
214 . " -signer $smdir/smrsa1.pem -signer $smdir/smrsa2.pem"
215 . " -signer $smdir/smdsa1.pem -signer $smdir/smdsa2.pem"
216 . " -stream -out test.cms",
217 "-verify -in test.cms -inform \"DER\" "
218 . " \"-CAfile\" $smdir/smroot.pem -out smtst.txt"
222 "signed content test streaming BER format, 2 DSA and 2 RSA keys, no attributes",
223 "-sign -in smcont.txt -outform \"DER\" -noattr -nodetach"
224 . " -signer $smdir/smrsa1.pem -signer $smdir/smrsa2.pem"
225 . " -signer $smdir/smdsa1.pem -signer $smdir/smdsa2.pem"
226 . " -stream -out test.cms",
227 "-verify -in test.cms -inform \"DER\" "
228 . " \"-CAfile\" $smdir/smroot.pem -out smtst.txt"
232 "signed content test streaming S/MIME format, 2 DSA and 2 RSA keys",
233 "-sign -in smcont.txt -nodetach"
234 . " -signer $smdir/smrsa1.pem -signer $smdir/smrsa2.pem"
235 . " -signer $smdir/smdsa1.pem -signer $smdir/smdsa2.pem"
236 . " -stream -out test.cms",
237 "-verify -in test.cms " . " \"-CAfile\" $smdir/smroot.pem -out smtst.txt"
241 "signed content test streaming multipart S/MIME format, 2 DSA and 2 RSA keys",
242 "-sign -in smcont.txt"
243 . " -signer $smdir/smrsa1.pem -signer $smdir/smrsa2.pem"
244 . " -signer $smdir/smdsa1.pem -signer $smdir/smdsa2.pem"
245 . " -stream -out test.cms",
246 "-verify -in test.cms " . " \"-CAfile\" $smdir/smroot.pem -out smtst.txt"
250 "enveloped content test streaming S/MIME format, 3 recipients",
251 "-encrypt -in smcont.txt"
252 . " -stream -out test.cms"
253 . " $smdir/smrsa1.pem $smdir/smrsa2.pem $smdir/smrsa3.pem ",
254 "-decrypt -recip $smdir/smrsa1.pem -in test.cms -out smtst.txt"
258 "enveloped content test streaming S/MIME format, 3 recipients, 3rd used",
259 "-encrypt -in smcont.txt"
260 . " -stream -out test.cms"
261 . " $smdir/smrsa1.pem $smdir/smrsa2.pem $smdir/smrsa3.pem ",
262 "-decrypt -recip $smdir/smrsa3.pem -in test.cms -out smtst.txt"
266 "enveloped content test streaming S/MIME format, 3 recipients, key only used",
267 "-encrypt -in smcont.txt"
268 . " -stream -out test.cms"
269 . " $smdir/smrsa1.pem $smdir/smrsa2.pem $smdir/smrsa3.pem ",
270 "-decrypt -inkey $smdir/smrsa3.pem -in test.cms -out smtst.txt"
274 "enveloped content test streaming S/MIME format, AES-256 cipher, 3 recipients",
275 "-encrypt -in smcont.txt"
276 . " -aes256 -stream -out test.cms"
277 . " $smdir/smrsa1.pem $smdir/smrsa2.pem $smdir/smrsa3.pem ",
278 "-decrypt -recip $smdir/smrsa1.pem -in test.cms -out smtst.txt"
283 my @smime_cms_tests = (
286 "signed content test streaming BER format, 2 DSA and 2 RSA keys, keyid",
287 "-sign -in smcont.txt -outform \"DER\" -nodetach -keyid"
288 . " -signer $smdir/smrsa1.pem -signer $smdir/smrsa2.pem"
289 . " -signer $smdir/smdsa1.pem -signer $smdir/smdsa2.pem"
290 . " -stream -out test.cms",
291 "-verify -in test.cms -inform \"DER\" "
292 . " \"-CAfile\" $smdir/smroot.pem -out smtst.txt"
296 "signed content test streaming PEM format, 2 DSA and 2 RSA keys",
297 "-sign -in smcont.txt -outform PEM -nodetach"
298 . " -signer $smdir/smrsa1.pem -signer $smdir/smrsa2.pem"
299 . " -signer $smdir/smdsa1.pem -signer $smdir/smdsa2.pem"
300 . " -stream -out test.cms",
301 "-verify -in test.cms -inform PEM "
302 . " \"-CAfile\" $smdir/smroot.pem -out smtst.txt"
306 "signed content MIME format, RSA key, signed receipt request",
307 "-sign -in smcont.txt -signer $smdir/smrsa1.pem -nodetach"
308 . " -receipt_request_to test\@openssl.org -receipt_request_all"
310 "-verify -in test.cms "
311 . " \"-CAfile\" $smdir/smroot.pem -out smtst.txt"
315 "signed receipt MIME format, RSA key",
316 "-sign_receipt -in test.cms"
317 . " -signer $smdir/smrsa2.pem"
319 "-verify_receipt test2.cms -in test.cms"
320 . " \"-CAfile\" $smdir/smroot.pem"
324 "enveloped content test streaming S/MIME format, 3 recipients, keyid",
325 "-encrypt -in smcont.txt"
326 . " -stream -out test.cms -keyid"
327 . " $smdir/smrsa1.pem $smdir/smrsa2.pem $smdir/smrsa3.pem ",
328 "-decrypt -recip $smdir/smrsa1.pem -in test.cms -out smtst.txt"
332 "enveloped content test streaming PEM format, KEK",
333 "-encrypt -in smcont.txt -outform PEM -aes128"
334 . " -stream -out test.cms "
335 . " -secretkey 000102030405060708090A0B0C0D0E0F "
336 . " -secretkeyid C0FEE0",
337 "-decrypt -in test.cms -out smtst.txt -inform PEM"
338 . " -secretkey 000102030405060708090A0B0C0D0E0F "
339 . " -secretkeyid C0FEE0"
343 "enveloped content test streaming PEM format, KEK, key only",
344 "-encrypt -in smcont.txt -outform PEM -aes128"
345 . " -stream -out test.cms "
346 . " -secretkey 000102030405060708090A0B0C0D0E0F "
347 . " -secretkeyid C0FEE0",
348 "-decrypt -in test.cms -out smtst.txt -inform PEM"
349 . " -secretkey 000102030405060708090A0B0C0D0E0F "
353 "data content test streaming PEM format",
354 "-data_create -in smcont.txt -outform PEM -nodetach"
355 . " -stream -out test.cms",
356 "-data_out -in test.cms -inform PEM -out smtst.txt"
360 "encrypted content test streaming PEM format, 128 bit RC2 key",
361 "\"-EncryptedData_encrypt\" -in smcont.txt -outform PEM"
362 . " -rc2 -secretkey 000102030405060708090A0B0C0D0E0F"
363 . " -stream -out test.cms",
364 "\"-EncryptedData_decrypt\" -in test.cms -inform PEM "
365 . " -secretkey 000102030405060708090A0B0C0D0E0F -out smtst.txt"
369 "encrypted content test streaming PEM format, 40 bit RC2 key",
370 "\"-EncryptedData_encrypt\" -in smcont.txt -outform PEM"
371 . " -rc2 -secretkey 0001020304"
372 . " -stream -out test.cms",
373 "\"-EncryptedData_decrypt\" -in test.cms -inform PEM "
374 . " -secretkey 0001020304 -out smtst.txt"
378 "encrypted content test streaming PEM format, triple DES key",
379 "\"-EncryptedData_encrypt\" -in smcont.txt -outform PEM"
380 . " -des3 -secretkey 000102030405060708090A0B0C0D0E0F1011121314151617"
381 . " -stream -out test.cms",
382 "\"-EncryptedData_decrypt\" -in test.cms -inform PEM "
383 . " -secretkey 000102030405060708090A0B0C0D0E0F1011121314151617"
388 "encrypted content test streaming PEM format, 128 bit AES key",
389 "\"-EncryptedData_encrypt\" -in smcont.txt -outform PEM"
390 . " -aes128 -secretkey 000102030405060708090A0B0C0D0E0F"
391 . " -stream -out test.cms",
392 "\"-EncryptedData_decrypt\" -in test.cms -inform PEM "
393 . " -secretkey 000102030405060708090A0B0C0D0E0F -out smtst.txt"
398 my @smime_cms_comp_tests = (
401 "compressed content test streaming PEM format",
402 "-compress -in smcont.txt -outform PEM -nodetach"
403 . " -stream -out test.cms",
404 "-uncompress -in test.cms -inform PEM -out smtst.txt"
409 my @smime_cms_param_tests = (
411 "signed content test streaming PEM format, RSA keys, PSS signature",
412 "-sign -in smcont.txt -outform PEM -nodetach"
413 . " -signer $smdir/smrsa1.pem -keyopt rsa_padding_mode:pss"
415 "-verify -in test.cms -inform PEM "
416 . " \"-CAfile\" $smdir/smroot.pem -out smtst.txt"
420 "signed content test streaming PEM format, RSA keys, PSS signature, no attributes",
421 "-sign -in smcont.txt -outform PEM -nodetach -noattr"
422 . " -signer $smdir/smrsa1.pem -keyopt rsa_padding_mode:pss"
424 "-verify -in test.cms -inform PEM "
425 . " \"-CAfile\" $smdir/smroot.pem -out smtst.txt"
429 "signed content test streaming PEM format, RSA keys, PSS signature, SHA384 MGF1",
430 "-sign -in smcont.txt -outform PEM -nodetach"
431 . " -signer $smdir/smrsa1.pem -keyopt rsa_padding_mode:pss"
432 . " -keyopt rsa_mgf1_md:sha384 -out test.cms",
433 "-verify -in test.cms -inform PEM "
434 . " \"-CAfile\" $smdir/smroot.pem -out smtst.txt"
438 "enveloped content test streaming S/MIME format, OAEP default parameters",
439 "-encrypt -in smcont.txt"
440 . " -stream -out test.cms"
441 . " -recip $smdir/smrsa1.pem -keyopt rsa_padding_mode:oaep",
442 "-decrypt -recip $smdir/smrsa1.pem -in test.cms -out smtst.txt"
446 "enveloped content test streaming S/MIME format, OAEP SHA256",
447 "-encrypt -in smcont.txt"
448 . " -stream -out test.cms"
449 . " -recip $smdir/smrsa1.pem -keyopt rsa_padding_mode:oaep"
450 . " -keyopt rsa_oaep_md:sha256",
451 "-decrypt -recip $smdir/smrsa1.pem -in test.cms -out smtst.txt"
455 "enveloped content test streaming S/MIME format, ECDH",
456 "-encrypt -in smcont.txt"
457 . " -stream -out test.cms"
458 . " -recip $smdir/smec1.pem",
459 "-decrypt -recip $smdir/smec1.pem -in test.cms -out smtst.txt"
463 "enveloped content test streaming S/MIME format, ECDH, key identifier",
464 "-encrypt -keyid -in smcont.txt"
465 . " -stream -out test.cms"
466 . " -recip $smdir/smec1.pem",
467 "-decrypt -recip $smdir/smec1.pem -in test.cms -out smtst.txt"
471 "enveloped content test streaming S/MIME format, ECDH, AES128, SHA256 KDF",
472 "-encrypt -in smcont.txt"
473 . " -stream -out test.cms"
474 . " -recip $smdir/smec1.pem -aes128 -keyopt ecdh_kdf_md:sha256",
475 "-decrypt -recip $smdir/smec1.pem -in test.cms -out smtst.txt"
479 "enveloped content test streaming S/MIME format, ECDH, K-283, cofactor DH",
480 "-encrypt -in smcont.txt"
481 . " -stream -out test.cms"
482 . " -recip $smdir/smec2.pem -aes128"
483 . " -keyopt ecdh_kdf_md:sha256 -keyopt ecdh_cofactor_mode:1",
484 "-decrypt -recip $smdir/smec2.pem -in test.cms -out smtst.txt"
488 "enveloped content test streaming S/MIME format, X9.42 DH",
489 "-encrypt -in smcont.txt"
490 . " -stream -out test.cms"
491 . " -recip $smdir/smdh.pem -aes128",
492 "-decrypt -recip $smdir/smdh.pem -in test.cms -out smtst.txt"
496 print "CMS => PKCS#7 compatibility tests\n";
498 run_smime_tests( \$badcmd, \@smime_pkcs7_tests, $cmscmd, $pk7cmd );
500 print "CMS <= PKCS#7 compatibility tests\n";
502 run_smime_tests( \$badcmd, \@smime_pkcs7_tests, $pk7cmd, $cmscmd );
504 print "CMS <=> CMS consistency tests\n";
506 run_smime_tests( \$badcmd, \@smime_pkcs7_tests, $cmscmd, $cmscmd );
507 run_smime_tests( \$badcmd, \@smime_cms_tests, $cmscmd, $cmscmd );
509 print "CMS <=> CMS consistency tests, modified key parameters\n";
510 run_smime_tests( \$badcmd, \@smime_cms_param_tests, $cmscmd, $cmscmd );
512 if ( `$ossl_path version -f` =~ /ZLIB/ ) {
513 run_smime_tests( \$badcmd, \@smime_cms_comp_tests, $cmscmd, $cmscmd );
516 print "Zlib not supported: compression tests skipped\n";
519 print "Running modified tests for OpenSSL 0.9.8 cms backport\n" if($ossl8);
522 print "$badcmd TESTS FAILED!!\n";
525 print "ALL TESTS SUCCESSFUL.\n";
534 sub run_smime_tests {
535 my ( $rv, $aref, $scmd, $vcmd ) = @_;
537 foreach $smtst (@$aref) {
538 my ( $tnam, $rscmd, $rvcmd ) = @$smtst;
541 # Skip smime resign: 0.9.8 smime doesn't support -resign
542 next if ($scmd =~ /smime/ && $rscmd =~ /-resign/);
543 # Disable streaming: option not supported in 0.9.8
544 $tnam =~ s/streaming//;
545 $rscmd =~ s/-stream//;
546 $rvcmd =~ s/-stream//;
548 if ($no_ec && $tnam =~ /ECDH/)
550 print "$tnam: skipped, EC disabled\n";
553 if ($no_ecdh && $tnam =~ /ECDH/)
555 print "$tnam: skipped, ECDH disabled\n";
558 if ($no_ec2m && $tnam =~ /K-283/)
560 print "$tnam: skipped, EC2M disabled\n";
563 system("$scmd$rscmd$redir");
565 print "$tnam: generation error\n";
570 system("$vcmd$rvcmd$redir");
572 print "$tnam: verify error\n";
577 if (!cmp_files("smtst.txt", "smcont.txt")) {
578 print "$tnam: content verify error\n";
589 my ( $f1, $f2 ) = @_;
590 my $fp1 = FileHandle->new();
591 my $fp2 = FileHandle->new();
595 if ( !open( $fp1, "<$f1" ) ) {
596 print STDERR "Can't Open file $f1\n";
600 if ( !open( $fp2, "<$f2" ) ) {
601 print STDERR "Can't Open file $f2\n";
611 $n1 = sysread $fp1, $rd1, 4096;
612 $n2 = sysread $fp2, $rd2, 4096;
613 last if ( $n1 != $n2 );
614 last if ( $rd1 ne $rd2 );