2 # Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
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
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";
64 elsif ( $^O eq "MSWin32" && -f "../apps/openssl.exe" ) {
65 $ossl_path = "cmd /c ..\\apps\\openssl";
66 $null_path = "/dev/null";
68 elsif ( -f "../apps/openssl$ENV{EXE_EXT}" ) {
69 $ossl_path = "../util/shlib_wrap.sh ../apps/openssl";
70 $null_path = "/dev/null";
72 elsif ( -f "..\\out32dll\\openssl.exe" ) {
73 $ossl_path = "..\\out32dll\\openssl.exe";
74 $null_path = "/dev/null";
76 elsif ( -f "..\\out32\\openssl.exe" ) {
77 $ossl_path = "..\\out32\\openssl.exe";
78 $null_path = "/dev/null";
81 die "Can't find OpenSSL executable";
84 my $pk7cmd = "$ossl_path smime ";
85 my $cmscmd = "$ossl_path cms ";
86 my $smdir = "smime-certs";
93 my $ossl8 = `$ossl_path version -v` =~ /0\.9\.8/;
95 system ("$ossl_path no-ec > $null_path");
100 elsif ($^O eq "VMS" ? $? == 512 : $? == 256)
106 die "Error checking for EC support\n";
109 system ("$ossl_path no-ec2m > $null_path");
120 die "Error checking for EC2M support\n";
123 system ("$ossl_path no-ecdh > $null_path");
134 die "Error checking for ECDH support\n";
137 my @smime_pkcs7_tests = (
140 "signed content DER format, RSA key",
141 "-sign -in smcont.txt -outform \"DER\" -nodetach"
142 . " -certfile $smdir/smroot.pem"
143 . " -signer $smdir/smrsa1.pem -out test.cms",
144 "-verify -in test.cms -inform \"DER\" "
145 . " \"-CAfile\" $smdir/smroot.pem -out smtst.txt"
149 "signed detached content DER format, RSA key",
150 "-sign -in smcont.txt -outform \"DER\""
151 . " -signer $smdir/smrsa1.pem -out test.cms",
152 "-verify -in test.cms -inform \"DER\" "
153 . " \"-CAfile\" $smdir/smroot.pem -out smtst.txt -content smcont.txt"
157 "signed content test streaming BER format, RSA",
158 "-sign -in smcont.txt -outform \"DER\" -nodetach"
159 . " -stream -signer $smdir/smrsa1.pem -out test.cms",
160 "-verify -in test.cms -inform \"DER\" "
161 . " \"-CAfile\" $smdir/smroot.pem -out smtst.txt"
165 "signed content DER format, DSA key",
166 "-sign -in smcont.txt -outform \"DER\" -nodetach"
167 . " -signer $smdir/smdsa1.pem -out test.cms",
168 "-verify -in test.cms -inform \"DER\" "
169 . " \"-CAfile\" $smdir/smroot.pem -out smtst.txt"
173 "signed detached content DER format, DSA key",
174 "-sign -in smcont.txt -outform \"DER\""
175 . " -signer $smdir/smdsa1.pem -out test.cms",
176 "-verify -in test.cms -inform \"DER\" "
177 . " \"-CAfile\" $smdir/smroot.pem -out smtst.txt -content smcont.txt"
181 "signed detached content DER format, add RSA signer",
182 "-resign -inform \"DER\" -in test.cms -outform \"DER\""
183 . " -signer $smdir/smrsa1.pem -out test2.cms",
184 "-verify -in test2.cms -inform \"DER\" "
185 . " \"-CAfile\" $smdir/smroot.pem -out smtst.txt -content smcont.txt"
189 "signed content test streaming BER format, DSA key",
190 "-sign -in smcont.txt -outform \"DER\" -nodetach"
191 . " -stream -signer $smdir/smdsa1.pem -out test.cms",
192 "-verify -in test.cms -inform \"DER\" "
193 . " \"-CAfile\" $smdir/smroot.pem -out smtst.txt"
197 "signed content test streaming BER format, 2 DSA and 2 RSA keys",
198 "-sign -in smcont.txt -outform \"DER\" -nodetach"
199 . " -signer $smdir/smrsa1.pem -signer $smdir/smrsa2.pem"
200 . " -signer $smdir/smdsa1.pem -signer $smdir/smdsa2.pem"
201 . " -stream -out test.cms",
202 "-verify -in test.cms -inform \"DER\" "
203 . " \"-CAfile\" $smdir/smroot.pem -out smtst.txt"
207 "signed content test streaming BER format, 2 DSA and 2 RSA keys, no attributes",
208 "-sign -in smcont.txt -outform \"DER\" -noattr -nodetach"
209 . " -signer $smdir/smrsa1.pem -signer $smdir/smrsa2.pem"
210 . " -signer $smdir/smdsa1.pem -signer $smdir/smdsa2.pem"
211 . " -stream -out test.cms",
212 "-verify -in test.cms -inform \"DER\" "
213 . " \"-CAfile\" $smdir/smroot.pem -out smtst.txt"
217 "signed content test streaming S/MIME format, 2 DSA and 2 RSA keys",
218 "-sign -in smcont.txt -nodetach"
219 . " -signer $smdir/smrsa1.pem -signer $smdir/smrsa2.pem"
220 . " -signer $smdir/smdsa1.pem -signer $smdir/smdsa2.pem"
221 . " -stream -out test.cms",
222 "-verify -in test.cms " . " \"-CAfile\" $smdir/smroot.pem -out smtst.txt"
226 "signed content test streaming multipart S/MIME format, 2 DSA and 2 RSA keys",
227 "-sign -in smcont.txt"
228 . " -signer $smdir/smrsa1.pem -signer $smdir/smrsa2.pem"
229 . " -signer $smdir/smdsa1.pem -signer $smdir/smdsa2.pem"
230 . " -stream -out test.cms",
231 "-verify -in test.cms " . " \"-CAfile\" $smdir/smroot.pem -out smtst.txt"
235 "enveloped content test streaming S/MIME format, 3 recipients",
236 "-encrypt -in smcont.txt"
237 . " -stream -out test.cms"
238 . " $smdir/smrsa1.pem $smdir/smrsa2.pem $smdir/smrsa3.pem ",
239 "-decrypt -recip $smdir/smrsa1.pem -in test.cms -out smtst.txt"
243 "enveloped content test streaming S/MIME format, 3 recipients, 3rd used",
244 "-encrypt -in smcont.txt"
245 . " -stream -out test.cms"
246 . " $smdir/smrsa1.pem $smdir/smrsa2.pem $smdir/smrsa3.pem ",
247 "-decrypt -recip $smdir/smrsa3.pem -in test.cms -out smtst.txt"
251 "enveloped content test streaming S/MIME format, 3 recipients, key only used",
252 "-encrypt -in smcont.txt"
253 . " -stream -out test.cms"
254 . " $smdir/smrsa1.pem $smdir/smrsa2.pem $smdir/smrsa3.pem ",
255 "-decrypt -inkey $smdir/smrsa3.pem -in test.cms -out smtst.txt"
259 "enveloped content test streaming S/MIME format, AES-256 cipher, 3 recipients",
260 "-encrypt -in smcont.txt"
261 . " -aes256 -stream -out test.cms"
262 . " $smdir/smrsa1.pem $smdir/smrsa2.pem $smdir/smrsa3.pem ",
263 "-decrypt -recip $smdir/smrsa1.pem -in test.cms -out smtst.txt"
268 my @smime_cms_tests = (
271 "signed content test streaming BER format, 2 DSA and 2 RSA keys, keyid",
272 "-sign -in smcont.txt -outform \"DER\" -nodetach -keyid"
273 . " -signer $smdir/smrsa1.pem -signer $smdir/smrsa2.pem"
274 . " -signer $smdir/smdsa1.pem -signer $smdir/smdsa2.pem"
275 . " -stream -out test.cms",
276 "-verify -in test.cms -inform \"DER\" "
277 . " \"-CAfile\" $smdir/smroot.pem -out smtst.txt"
281 "signed content test streaming PEM format, 2 DSA and 2 RSA keys",
282 "-sign -in smcont.txt -outform PEM -nodetach"
283 . " -signer $smdir/smrsa1.pem -signer $smdir/smrsa2.pem"
284 . " -signer $smdir/smdsa1.pem -signer $smdir/smdsa2.pem"
285 . " -stream -out test.cms",
286 "-verify -in test.cms -inform PEM "
287 . " \"-CAfile\" $smdir/smroot.pem -out smtst.txt"
291 "signed content MIME format, RSA key, signed receipt request",
292 "-sign -in smcont.txt -signer $smdir/smrsa1.pem -nodetach"
293 . " -receipt_request_to test\@openssl.org -receipt_request_all"
295 "-verify -in test.cms "
296 . " \"-CAfile\" $smdir/smroot.pem -out smtst.txt"
300 "signed receipt MIME format, RSA key",
301 "-sign_receipt -in test.cms"
302 . " -signer $smdir/smrsa2.pem"
304 "-verify_receipt test2.cms -in test.cms"
305 . " \"-CAfile\" $smdir/smroot.pem"
309 "enveloped content test streaming S/MIME format, 3 recipients, keyid",
310 "-encrypt -in smcont.txt"
311 . " -stream -out test.cms -keyid"
312 . " $smdir/smrsa1.pem $smdir/smrsa2.pem $smdir/smrsa3.pem ",
313 "-decrypt -recip $smdir/smrsa1.pem -in test.cms -out smtst.txt"
317 "enveloped content test streaming PEM format, KEK",
318 "-encrypt -in smcont.txt -outform PEM -aes128"
319 . " -stream -out test.cms "
320 . " -secretkey 000102030405060708090A0B0C0D0E0F "
321 . " -secretkeyid C0FEE0",
322 "-decrypt -in test.cms -out smtst.txt -inform PEM"
323 . " -secretkey 000102030405060708090A0B0C0D0E0F "
324 . " -secretkeyid C0FEE0"
328 "enveloped content test streaming PEM format, KEK, key only",
329 "-encrypt -in smcont.txt -outform PEM -aes128"
330 . " -stream -out test.cms "
331 . " -secretkey 000102030405060708090A0B0C0D0E0F "
332 . " -secretkeyid C0FEE0",
333 "-decrypt -in test.cms -out smtst.txt -inform PEM"
334 . " -secretkey 000102030405060708090A0B0C0D0E0F "
338 "data content test streaming PEM format",
339 "-data_create -in smcont.txt -outform PEM -nodetach"
340 . " -stream -out test.cms",
341 "-data_out -in test.cms -inform PEM -out smtst.txt"
345 "encrypted content test streaming PEM format, 128 bit RC2 key",
346 "\"-EncryptedData_encrypt\" -in smcont.txt -outform PEM"
347 . " -rc2 -secretkey 000102030405060708090A0B0C0D0E0F"
348 . " -stream -out test.cms",
349 "\"-EncryptedData_decrypt\" -in test.cms -inform PEM "
350 . " -secretkey 000102030405060708090A0B0C0D0E0F -out smtst.txt"
354 "encrypted content test streaming PEM format, 40 bit RC2 key",
355 "\"-EncryptedData_encrypt\" -in smcont.txt -outform PEM"
356 . " -rc2 -secretkey 0001020304"
357 . " -stream -out test.cms",
358 "\"-EncryptedData_decrypt\" -in test.cms -inform PEM "
359 . " -secretkey 0001020304 -out smtst.txt"
363 "encrypted content test streaming PEM format, triple DES key",
364 "\"-EncryptedData_encrypt\" -in smcont.txt -outform PEM"
365 . " -des3 -secretkey 000102030405060708090A0B0C0D0E0F1011121314151617"
366 . " -stream -out test.cms",
367 "\"-EncryptedData_decrypt\" -in test.cms -inform PEM "
368 . " -secretkey 000102030405060708090A0B0C0D0E0F1011121314151617"
373 "encrypted content test streaming PEM format, 128 bit AES key",
374 "\"-EncryptedData_encrypt\" -in smcont.txt -outform PEM"
375 . " -aes128 -secretkey 000102030405060708090A0B0C0D0E0F"
376 . " -stream -out test.cms",
377 "\"-EncryptedData_decrypt\" -in test.cms -inform PEM "
378 . " -secretkey 000102030405060708090A0B0C0D0E0F -out smtst.txt"
383 my @smime_cms_comp_tests = (
386 "compressed content test streaming PEM format",
387 "-compress -in smcont.txt -outform PEM -nodetach"
388 . " -stream -out test.cms",
389 "-uncompress -in test.cms -inform PEM -out smtst.txt"
394 my @smime_cms_param_tests = (
396 "signed content test streaming PEM format, RSA keys, PSS signature",
397 "-sign -in smcont.txt -outform PEM -nodetach"
398 . " -signer $smdir/smrsa1.pem -keyopt rsa_padding_mode:pss"
400 "-verify -in test.cms -inform PEM "
401 . " \"-CAfile\" $smdir/smroot.pem -out smtst.txt"
405 "signed content test streaming PEM format, RSA keys, PSS signature, no attributes",
406 "-sign -in smcont.txt -outform PEM -nodetach -noattr"
407 . " -signer $smdir/smrsa1.pem -keyopt rsa_padding_mode:pss"
409 "-verify -in test.cms -inform PEM "
410 . " \"-CAfile\" $smdir/smroot.pem -out smtst.txt"
414 "signed content test streaming PEM format, RSA keys, PSS signature, SHA384 MGF1",
415 "-sign -in smcont.txt -outform PEM -nodetach"
416 . " -signer $smdir/smrsa1.pem -keyopt rsa_padding_mode:pss"
417 . " -keyopt rsa_mgf1_md:sha384 -out test.cms",
418 "-verify -in test.cms -inform PEM "
419 . " \"-CAfile\" $smdir/smroot.pem -out smtst.txt"
423 "enveloped content test streaming S/MIME format, OAEP default parameters",
424 "-encrypt -in smcont.txt"
425 . " -stream -out test.cms"
426 . " -recip $smdir/smrsa1.pem -keyopt rsa_padding_mode:oaep",
427 "-decrypt -recip $smdir/smrsa1.pem -in test.cms -out smtst.txt"
431 "enveloped content test streaming S/MIME format, OAEP SHA256",
432 "-encrypt -in smcont.txt"
433 . " -stream -out test.cms"
434 . " -recip $smdir/smrsa1.pem -keyopt rsa_padding_mode:oaep"
435 . " -keyopt rsa_oaep_md:sha256",
436 "-decrypt -recip $smdir/smrsa1.pem -in test.cms -out smtst.txt"
440 "enveloped content test streaming S/MIME format, ECDH",
441 "-encrypt -in smcont.txt"
442 . " -stream -out test.cms"
443 . " -recip $smdir/smec1.pem",
444 "-decrypt -recip $smdir/smec1.pem -in test.cms -out smtst.txt"
448 "enveloped content test streaming S/MIME format, ECDH, AES128, SHA256 KDF",
449 "-encrypt -in smcont.txt"
450 . " -stream -out test.cms"
451 . " -recip $smdir/smec1.pem -aes128 -keyopt ecdh_kdf_md:sha256",
452 "-decrypt -recip $smdir/smec1.pem -in test.cms -out smtst.txt"
456 "enveloped content test streaming S/MIME format, ECDH, K-283, cofactor DH",
457 "-encrypt -in smcont.txt"
458 . " -stream -out test.cms"
459 . " -recip $smdir/smec2.pem -aes128"
460 . " -keyopt ecdh_kdf_md:sha256 -keyopt ecdh_cofactor_mode:1",
461 "-decrypt -recip $smdir/smec2.pem -in test.cms -out smtst.txt"
465 "enveloped content test streaming S/MIME format, X9.42 DH",
466 "-encrypt -in smcont.txt"
467 . " -stream -out test.cms"
468 . " -recip $smdir/smdh.pem -aes128",
469 "-decrypt -recip $smdir/smdh.pem -in test.cms -out smtst.txt"
473 print "CMS => PKCS#7 compatibility tests\n";
475 run_smime_tests( \$badcmd, \@smime_pkcs7_tests, $cmscmd, $pk7cmd );
477 print "CMS <= PKCS#7 compatibility tests\n";
479 run_smime_tests( \$badcmd, \@smime_pkcs7_tests, $pk7cmd, $cmscmd );
481 print "CMS <=> CMS consistency tests\n";
483 run_smime_tests( \$badcmd, \@smime_pkcs7_tests, $cmscmd, $cmscmd );
484 run_smime_tests( \$badcmd, \@smime_cms_tests, $cmscmd, $cmscmd );
486 print "CMS <=> CMS consistency tests, modified key parameters\n";
487 run_smime_tests( \$badcmd, \@smime_cms_param_tests, $cmscmd, $cmscmd );
489 if ( `$ossl_path version -f` =~ /ZLIB/ ) {
490 run_smime_tests( \$badcmd, \@smime_cms_comp_tests, $cmscmd, $cmscmd );
493 print "Zlib not supported: compression tests skipped\n";
496 print "Running modified tests for OpenSSL 0.9.8 cms backport\n" if($ossl8);
499 print "$badcmd TESTS FAILED!!\n";
502 print "ALL TESTS SUCCESSFUL.\n";
511 sub run_smime_tests {
512 my ( $rv, $aref, $scmd, $vcmd ) = @_;
514 foreach $smtst (@$aref) {
515 my ( $tnam, $rscmd, $rvcmd ) = @$smtst;
518 # Skip smime resign: 0.9.8 smime doesn't support -resign
519 next if ($scmd =~ /smime/ && $rscmd =~ /-resign/);
520 # Disable streaming: option not supported in 0.9.8
521 $tnam =~ s/streaming//;
522 $rscmd =~ s/-stream//;
523 $rvcmd =~ s/-stream//;
525 if ($no_ec && $tnam =~ /ECDH/)
527 print "$tnam: skipped, EC disabled\n";
530 if ($no_ecdh && $tnam =~ /ECDH/)
532 print "$tnam: skipped, ECDH disabled\n";
535 if ($no_ec2m && $tnam =~ /K-283/)
537 print "$tnam: skipped, EC2M disabled\n";
540 system("$scmd$rscmd$redir");
542 print "$tnam: generation error\n";
547 system("$vcmd$rvcmd$redir");
549 print "$tnam: verify error\n";
554 if (!cmp_files("smtst.txt", "smcont.txt")) {
555 print "$tnam: content verify error\n";
566 my ( $f1, $f2 ) = @_;
567 my $fp1 = FileHandle->new();
568 my $fp2 = FileHandle->new();
572 if ( !open( $fp1, "<$f1" ) ) {
573 print STDERR "Can't Open file $f1\n";
577 if ( !open( $fp2, "<$f2" ) ) {
578 print STDERR "Can't Open file $f2\n";
588 $n1 = sysread $fp1, $rd1, 4096;
589 $n2 = sysread $fp2, $rd2, 4096;
590 last if ( $n1 != $n2 );
591 last if ( $rd1 ne $rd2 );