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";
63 elsif ( $^O eq "MSWin32" && -f "../apps/openssl.exe" ) {
64 $ossl_path = "cmd /c ..\\apps\\openssl";
66 elsif ( -f "../apps/openssl$ENV{EXE_EXT}" ) {
67 $ossl_path = "../util/shlib_wrap.sh ../apps/openssl";
69 elsif ( -f "..\\out32dll\\openssl.exe" ) {
70 $ossl_path = "..\\out32dll\\openssl.exe";
72 elsif ( -f "..\\out32\\openssl.exe" ) {
73 $ossl_path = "..\\out32\\openssl.exe";
76 die "Can't find OpenSSL executable";
79 my $pk7cmd = "$ossl_path smime ";
80 my $cmscmd = "$ossl_path cms ";
81 my $smdir = "smime-certs";
88 my $ossl8 = `$ossl_path version -v` =~ /0\.9\.8/;
90 system ("$ossl_path no-ec >/dev/null");
101 die "Error checking for EC support\n";
104 system ("$ossl_path no-ec2m >/dev/null");
115 die "Error checking for EC2M support\n";
118 system ("$ossl_path no-ecdh >/dev/null");
129 die "Error checking for ECDH support\n";
132 my @smime_pkcs7_tests = (
135 "signed content DER format, RSA key",
136 "-sign -in smcont.txt -outform \"DER\" -nodetach"
137 . " -certfile $smdir/smroot.pem"
138 . " -signer $smdir/smrsa1.pem -out test.cms",
139 "-verify -in test.cms -inform \"DER\" "
140 . " \"-CAfile\" $smdir/smroot.pem -out smtst.txt"
144 "signed detached content DER format, RSA key",
145 "-sign -in smcont.txt -outform \"DER\""
146 . " -signer $smdir/smrsa1.pem -out test.cms",
147 "-verify -in test.cms -inform \"DER\" "
148 . " \"-CAfile\" $smdir/smroot.pem -out smtst.txt -content smcont.txt"
152 "signed content test streaming BER format, RSA",
153 "-sign -in smcont.txt -outform \"DER\" -nodetach"
154 . " -stream -signer $smdir/smrsa1.pem -out test.cms",
155 "-verify -in test.cms -inform \"DER\" "
156 . " \"-CAfile\" $smdir/smroot.pem -out smtst.txt"
160 "signed content DER format, DSA key",
161 "-sign -in smcont.txt -outform \"DER\" -nodetach"
162 . " -signer $smdir/smdsa1.pem -out test.cms",
163 "-verify -in test.cms -inform \"DER\" "
164 . " \"-CAfile\" $smdir/smroot.pem -out smtst.txt"
168 "signed detached content DER format, DSA key",
169 "-sign -in smcont.txt -outform \"DER\""
170 . " -signer $smdir/smdsa1.pem -out test.cms",
171 "-verify -in test.cms -inform \"DER\" "
172 . " \"-CAfile\" $smdir/smroot.pem -out smtst.txt -content smcont.txt"
176 "signed detached content DER format, add RSA signer",
177 "-resign -inform \"DER\" -in test.cms -outform \"DER\""
178 . " -signer $smdir/smrsa1.pem -out test2.cms",
179 "-verify -in test2.cms -inform \"DER\" "
180 . " \"-CAfile\" $smdir/smroot.pem -out smtst.txt -content smcont.txt"
184 "signed content test streaming BER format, DSA key",
185 "-sign -in smcont.txt -outform \"DER\" -nodetach"
186 . " -stream -signer $smdir/smdsa1.pem -out test.cms",
187 "-verify -in test.cms -inform \"DER\" "
188 . " \"-CAfile\" $smdir/smroot.pem -out smtst.txt"
192 "signed content test streaming BER format, 2 DSA and 2 RSA keys",
193 "-sign -in smcont.txt -outform \"DER\" -nodetach"
194 . " -signer $smdir/smrsa1.pem -signer $smdir/smrsa2.pem"
195 . " -signer $smdir/smdsa1.pem -signer $smdir/smdsa2.pem"
196 . " -stream -out test.cms",
197 "-verify -in test.cms -inform \"DER\" "
198 . " \"-CAfile\" $smdir/smroot.pem -out smtst.txt"
202 "signed content test streaming BER format, 2 DSA and 2 RSA keys, no attributes",
203 "-sign -in smcont.txt -outform \"DER\" -noattr -nodetach"
204 . " -signer $smdir/smrsa1.pem -signer $smdir/smrsa2.pem"
205 . " -signer $smdir/smdsa1.pem -signer $smdir/smdsa2.pem"
206 . " -stream -out test.cms",
207 "-verify -in test.cms -inform \"DER\" "
208 . " \"-CAfile\" $smdir/smroot.pem -out smtst.txt"
212 "signed content test streaming S/MIME format, 2 DSA and 2 RSA keys",
213 "-sign -in smcont.txt -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 " . " \"-CAfile\" $smdir/smroot.pem -out smtst.txt"
221 "signed content test streaming multipart S/MIME format, 2 DSA and 2 RSA keys",
222 "-sign -in smcont.txt"
223 . " -signer $smdir/smrsa1.pem -signer $smdir/smrsa2.pem"
224 . " -signer $smdir/smdsa1.pem -signer $smdir/smdsa2.pem"
225 . " -stream -out test.cms",
226 "-verify -in test.cms " . " \"-CAfile\" $smdir/smroot.pem -out smtst.txt"
230 "enveloped content test streaming S/MIME format, 3 recipients",
231 "-encrypt -in smcont.txt"
232 . " -stream -out test.cms"
233 . " $smdir/smrsa1.pem $smdir/smrsa2.pem $smdir/smrsa3.pem ",
234 "-decrypt -recip $smdir/smrsa1.pem -in test.cms -out smtst.txt"
238 "enveloped content test streaming S/MIME format, 3 recipients, 3rd used",
239 "-encrypt -in smcont.txt"
240 . " -stream -out test.cms"
241 . " $smdir/smrsa1.pem $smdir/smrsa2.pem $smdir/smrsa3.pem ",
242 "-decrypt -recip $smdir/smrsa3.pem -in test.cms -out smtst.txt"
246 "enveloped content test streaming S/MIME format, 3 recipients, key only used",
247 "-encrypt -in smcont.txt"
248 . " -stream -out test.cms"
249 . " $smdir/smrsa1.pem $smdir/smrsa2.pem $smdir/smrsa3.pem ",
250 "-decrypt -inkey $smdir/smrsa3.pem -in test.cms -out smtst.txt"
254 "enveloped content test streaming S/MIME format, AES-256 cipher, 3 recipients",
255 "-encrypt -in smcont.txt"
256 . " -aes256 -stream -out test.cms"
257 . " $smdir/smrsa1.pem $smdir/smrsa2.pem $smdir/smrsa3.pem ",
258 "-decrypt -recip $smdir/smrsa1.pem -in test.cms -out smtst.txt"
263 my @smime_cms_tests = (
266 "signed content test streaming BER format, 2 DSA and 2 RSA keys, keyid",
267 "-sign -in smcont.txt -outform \"DER\" -nodetach -keyid"
268 . " -signer $smdir/smrsa1.pem -signer $smdir/smrsa2.pem"
269 . " -signer $smdir/smdsa1.pem -signer $smdir/smdsa2.pem"
270 . " -stream -out test.cms",
271 "-verify -in test.cms -inform \"DER\" "
272 . " \"-CAfile\" $smdir/smroot.pem -out smtst.txt"
276 "signed content test streaming PEM format, 2 DSA and 2 RSA keys",
277 "-sign -in smcont.txt -outform PEM -nodetach"
278 . " -signer $smdir/smrsa1.pem -signer $smdir/smrsa2.pem"
279 . " -signer $smdir/smdsa1.pem -signer $smdir/smdsa2.pem"
280 . " -stream -out test.cms",
281 "-verify -in test.cms -inform PEM "
282 . " \"-CAfile\" $smdir/smroot.pem -out smtst.txt"
286 "signed content MIME format, RSA key, signed receipt request",
287 "-sign -in smcont.txt -signer $smdir/smrsa1.pem -nodetach"
288 . " -receipt_request_to test\@openssl.org -receipt_request_all"
290 "-verify -in test.cms "
291 . " \"-CAfile\" $smdir/smroot.pem -out smtst.txt"
295 "signed receipt MIME format, RSA key",
296 "-sign_receipt -in test.cms"
297 . " -signer $smdir/smrsa2.pem"
299 "-verify_receipt test2.cms -in test.cms"
300 . " \"-CAfile\" $smdir/smroot.pem"
304 "enveloped content test streaming S/MIME format, 3 recipients, keyid",
305 "-encrypt -in smcont.txt"
306 . " -stream -out test.cms -keyid"
307 . " $smdir/smrsa1.pem $smdir/smrsa2.pem $smdir/smrsa3.pem ",
308 "-decrypt -recip $smdir/smrsa1.pem -in test.cms -out smtst.txt"
312 "enveloped content test streaming PEM format, KEK",
313 "-encrypt -in smcont.txt -outform PEM -aes128"
314 . " -stream -out test.cms "
315 . " -secretkey 000102030405060708090A0B0C0D0E0F "
316 . " -secretkeyid C0FEE0",
317 "-decrypt -in test.cms -out smtst.txt -inform PEM"
318 . " -secretkey 000102030405060708090A0B0C0D0E0F "
319 . " -secretkeyid C0FEE0"
323 "enveloped content test streaming PEM format, KEK, key only",
324 "-encrypt -in smcont.txt -outform PEM -aes128"
325 . " -stream -out test.cms "
326 . " -secretkey 000102030405060708090A0B0C0D0E0F "
327 . " -secretkeyid C0FEE0",
328 "-decrypt -in test.cms -out smtst.txt -inform PEM"
329 . " -secretkey 000102030405060708090A0B0C0D0E0F "
333 "data content test streaming PEM format",
334 "-data_create -in smcont.txt -outform PEM -nodetach"
335 . " -stream -out test.cms",
336 "-data_out -in test.cms -inform PEM -out smtst.txt"
340 "encrypted content test streaming PEM format, 128 bit RC2 key",
341 "\"-EncryptedData_encrypt\" -in smcont.txt -outform PEM"
342 . " -rc2 -secretkey 000102030405060708090A0B0C0D0E0F"
343 . " -stream -out test.cms",
344 "\"-EncryptedData_decrypt\" -in test.cms -inform PEM "
345 . " -secretkey 000102030405060708090A0B0C0D0E0F -out smtst.txt"
349 "encrypted content test streaming PEM format, 40 bit RC2 key",
350 "\"-EncryptedData_encrypt\" -in smcont.txt -outform PEM"
351 . " -rc2 -secretkey 0001020304"
352 . " -stream -out test.cms",
353 "\"-EncryptedData_decrypt\" -in test.cms -inform PEM "
354 . " -secretkey 0001020304 -out smtst.txt"
358 "encrypted content test streaming PEM format, triple DES key",
359 "\"-EncryptedData_encrypt\" -in smcont.txt -outform PEM"
360 . " -des3 -secretkey 000102030405060708090A0B0C0D0E0F1011121314151617"
361 . " -stream -out test.cms",
362 "\"-EncryptedData_decrypt\" -in test.cms -inform PEM "
363 . " -secretkey 000102030405060708090A0B0C0D0E0F1011121314151617"
368 "encrypted content test streaming PEM format, 128 bit AES key",
369 "\"-EncryptedData_encrypt\" -in smcont.txt -outform PEM"
370 . " -aes128 -secretkey 000102030405060708090A0B0C0D0E0F"
371 . " -stream -out test.cms",
372 "\"-EncryptedData_decrypt\" -in test.cms -inform PEM "
373 . " -secretkey 000102030405060708090A0B0C0D0E0F -out smtst.txt"
378 my @smime_cms_comp_tests = (
381 "compressed content test streaming PEM format",
382 "-compress -in smcont.txt -outform PEM -nodetach"
383 . " -stream -out test.cms",
384 "-uncompress -in test.cms -inform PEM -out smtst.txt"
389 my @smime_cms_param_tests = (
391 "signed content test streaming PEM format, RSA keys, PSS signature",
392 "-sign -in smcont.txt -outform PEM -nodetach"
393 . " -signer $smdir/smrsa1.pem -keyopt rsa_padding_mode:pss"
395 "-verify -in test.cms -inform PEM "
396 . " \"-CAfile\" $smdir/smroot.pem -out smtst.txt"
400 "signed content test streaming PEM format, RSA keys, PSS signature, no attributes",
401 "-sign -in smcont.txt -outform PEM -nodetach -noattr"
402 . " -signer $smdir/smrsa1.pem -keyopt rsa_padding_mode:pss"
404 "-verify -in test.cms -inform PEM "
405 . " \"-CAfile\" $smdir/smroot.pem -out smtst.txt"
409 "signed content test streaming PEM format, RSA keys, PSS signature, SHA384 MGF1",
410 "-sign -in smcont.txt -outform PEM -nodetach"
411 . " -signer $smdir/smrsa1.pem -keyopt rsa_padding_mode:pss"
412 . " -keyopt rsa_mgf1_md:sha384 -out test.cms",
413 "-verify -in test.cms -inform PEM "
414 . " \"-CAfile\" $smdir/smroot.pem -out smtst.txt"
418 "enveloped content test streaming S/MIME format, OAEP default parameters",
419 "-encrypt -in smcont.txt"
420 . " -stream -out test.cms"
421 . " -recip $smdir/smrsa1.pem -keyopt rsa_padding_mode:oaep",
422 "-decrypt -recip $smdir/smrsa1.pem -in test.cms -out smtst.txt"
426 "enveloped content test streaming S/MIME format, OAEP SHA256",
427 "-encrypt -in smcont.txt"
428 . " -stream -out test.cms"
429 . " -recip $smdir/smrsa1.pem -keyopt rsa_padding_mode:oaep"
430 . " -keyopt rsa_oaep_md:sha256",
431 "-decrypt -recip $smdir/smrsa1.pem -in test.cms -out smtst.txt"
435 "enveloped content test streaming S/MIME format, ECDH",
436 "-encrypt -in smcont.txt"
437 . " -stream -out test.cms"
438 . " -recip $smdir/smec1.pem",
439 "-decrypt -recip $smdir/smec1.pem -in test.cms -out smtst.txt"
443 "enveloped content test streaming S/MIME format, ECDH, AES128, SHA256 KDF",
444 "-encrypt -in smcont.txt"
445 . " -stream -out test.cms"
446 . " -recip $smdir/smec1.pem -aes128 -keyopt ecdh_kdf_md:sha256",
447 "-decrypt -recip $smdir/smec1.pem -in test.cms -out smtst.txt"
451 "enveloped content test streaming S/MIME format, ECDH, K-283, cofactor DH",
452 "-encrypt -in smcont.txt"
453 . " -stream -out test.cms"
454 . " -recip $smdir/smec2.pem -aes128"
455 . " -keyopt ecdh_kdf_md:sha256 -keyopt ecdh_cofactor_mode:1",
456 "-decrypt -recip $smdir/smec2.pem -in test.cms -out smtst.txt"
460 "enveloped content test streaming S/MIME format, X9.42 DH",
461 "-encrypt -in smcont.txt"
462 . " -stream -out test.cms"
463 . " -recip $smdir/smdh.pem -aes128",
464 "-decrypt -recip $smdir/smdh.pem -in test.cms -out smtst.txt"
468 print "CMS => PKCS#7 compatibility tests\n";
470 run_smime_tests( \$badcmd, \@smime_pkcs7_tests, $cmscmd, $pk7cmd );
472 print "CMS <= PKCS#7 compatibility tests\n";
474 run_smime_tests( \$badcmd, \@smime_pkcs7_tests, $pk7cmd, $cmscmd );
476 print "CMS <=> CMS consistency tests\n";
478 run_smime_tests( \$badcmd, \@smime_pkcs7_tests, $cmscmd, $cmscmd );
479 run_smime_tests( \$badcmd, \@smime_cms_tests, $cmscmd, $cmscmd );
481 print "CMS <=> CMS consistency tests, modified key parameters\n";
482 run_smime_tests( \$badcmd, \@smime_cms_param_tests, $cmscmd, $cmscmd );
484 if ( `$ossl_path version -f` =~ /ZLIB/ ) {
485 run_smime_tests( \$badcmd, \@smime_cms_comp_tests, $cmscmd, $cmscmd );
488 print "Zlib not supported: compression tests skipped\n";
491 print "Running modified tests for OpenSSL 0.9.8 cms backport\n" if($ossl8);
494 print "$badcmd TESTS FAILED!!\n";
497 print "ALL TESTS SUCCESSFUL.\n";
506 sub run_smime_tests {
507 my ( $rv, $aref, $scmd, $vcmd ) = @_;
509 foreach $smtst (@$aref) {
510 my ( $tnam, $rscmd, $rvcmd ) = @$smtst;
513 # Skip smime resign: 0.9.8 smime doesn't support -resign
514 next if ($scmd =~ /smime/ && $rscmd =~ /-resign/);
515 # Disable streaming: option not supported in 0.9.8
516 $tnam =~ s/streaming//;
517 $rscmd =~ s/-stream//;
518 $rvcmd =~ s/-stream//;
520 if ($no_ec && $tnam =~ /ECDH/)
522 print "$tnam: skipped, EC disabled\n";
525 if ($no_ecdh && $tnam =~ /ECDH/)
527 print "$tnam: skipped, ECDH disabled\n";
530 if ($no_ec2m && $tnam =~ /K-283/)
532 print "$tnam: skipped, EC2M disabled\n";
535 system("$scmd$rscmd$redir");
537 print "$tnam: generation error\n";
542 system("$vcmd$rvcmd$redir");
544 print "$tnam: verify error\n";
549 if (!cmp_files("smtst.txt", "smcont.txt")) {
550 print "$tnam: content verify error\n";
561 my ( $f1, $f2 ) = @_;
562 my $fp1 = FileHandle->new();
563 my $fp2 = FileHandle->new();
567 if ( !open( $fp1, "<$f1" ) ) {
568 print STDERR "Can't Open file $f1\n";
572 if ( !open( $fp2, "<$f2" ) ) {
573 print STDERR "Can't Open file $f2\n";
583 $n1 = sysread $fp1, $rd1, 4096;
584 $n2 = sysread $fp2, $rd2, 4096;
585 last if ( $n1 != $n2 );
586 last if ( $rd1 ne $rd2 );