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";
92 my $ossl8 = `$ossl_path version -v` =~ /0\.9\.8/;
94 system ("$ossl_path no-ec > $null_path");
99 elsif ($^O eq "VMS" ? $? == 512 : $? == 256)
105 die "Error checking for EC support\n";
108 system ("$ossl_path no-ec2m >/dev/null");
119 die "Error checking for EC2M support\n";
122 my @smime_pkcs7_tests = (
125 "signed content DER format, RSA key",
126 "-sign -in smcont.txt -outform \"DER\" -nodetach"
127 . " -certfile $smdir/smroot.pem"
128 . " -signer $smdir/smrsa1.pem -out test.cms",
129 "-verify -in test.cms -inform \"DER\" "
130 . " \"-CAfile\" $smdir/smroot.pem -out smtst.txt"
134 "signed detached content DER format, RSA key",
135 "-sign -in smcont.txt -outform \"DER\""
136 . " -signer $smdir/smrsa1.pem -out test.cms",
137 "-verify -in test.cms -inform \"DER\" "
138 . " \"-CAfile\" $smdir/smroot.pem -out smtst.txt -content smcont.txt"
142 "signed content test streaming BER format, RSA",
143 "-sign -in smcont.txt -outform \"DER\" -nodetach"
144 . " -stream -signer $smdir/smrsa1.pem -out test.cms",
145 "-verify -in test.cms -inform \"DER\" "
146 . " \"-CAfile\" $smdir/smroot.pem -out smtst.txt"
150 "signed content DER format, DSA key",
151 "-sign -in smcont.txt -outform \"DER\" -nodetach"
152 . " -signer $smdir/smdsa1.pem -out test.cms",
153 "-verify -in test.cms -inform \"DER\" "
154 . " \"-CAfile\" $smdir/smroot.pem -out smtst.txt"
158 "signed detached content DER format, DSA key",
159 "-sign -in smcont.txt -outform \"DER\""
160 . " -signer $smdir/smdsa1.pem -out test.cms",
161 "-verify -in test.cms -inform \"DER\" "
162 . " \"-CAfile\" $smdir/smroot.pem -out smtst.txt -content smcont.txt"
166 "signed detached content DER format, add RSA signer",
167 "-resign -inform \"DER\" -in test.cms -outform \"DER\""
168 . " -signer $smdir/smrsa1.pem -out test2.cms",
169 "-verify -in test2.cms -inform \"DER\" "
170 . " \"-CAfile\" $smdir/smroot.pem -out smtst.txt -content smcont.txt"
174 "signed content test streaming BER format, DSA key",
175 "-sign -in smcont.txt -outform \"DER\" -nodetach"
176 . " -stream -signer $smdir/smdsa1.pem -out test.cms",
177 "-verify -in test.cms -inform \"DER\" "
178 . " \"-CAfile\" $smdir/smroot.pem -out smtst.txt"
182 "signed content test streaming BER format, 2 DSA and 2 RSA keys",
183 "-sign -in smcont.txt -outform \"DER\" -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 -inform \"DER\" "
188 . " \"-CAfile\" $smdir/smroot.pem -out smtst.txt"
192 "signed content test streaming BER format, 2 DSA and 2 RSA keys, no attributes",
193 "-sign -in smcont.txt -outform \"DER\" -noattr -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 S/MIME format, 2 DSA and 2 RSA keys",
203 "-sign -in smcont.txt -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 " . " \"-CAfile\" $smdir/smroot.pem -out smtst.txt"
211 "signed content test streaming multipart S/MIME format, 2 DSA and 2 RSA keys",
212 "-sign -in smcont.txt"
213 . " -signer $smdir/smrsa1.pem -signer $smdir/smrsa2.pem"
214 . " -signer $smdir/smdsa1.pem -signer $smdir/smdsa2.pem"
215 . " -stream -out test.cms",
216 "-verify -in test.cms " . " \"-CAfile\" $smdir/smroot.pem -out smtst.txt"
220 "enveloped content test streaming S/MIME format, 3 recipients",
221 "-encrypt -in smcont.txt"
222 . " -stream -out test.cms"
223 . " $smdir/smrsa1.pem $smdir/smrsa2.pem $smdir/smrsa3.pem ",
224 "-decrypt -recip $smdir/smrsa1.pem -in test.cms -out smtst.txt"
228 "enveloped content test streaming S/MIME format, 3 recipients, 3rd used",
229 "-encrypt -in smcont.txt"
230 . " -stream -out test.cms"
231 . " $smdir/smrsa1.pem $smdir/smrsa2.pem $smdir/smrsa3.pem ",
232 "-decrypt -recip $smdir/smrsa3.pem -in test.cms -out smtst.txt"
236 "enveloped content test streaming S/MIME format, 3 recipients, key only used",
237 "-encrypt -in smcont.txt"
238 . " -stream -out test.cms"
239 . " $smdir/smrsa1.pem $smdir/smrsa2.pem $smdir/smrsa3.pem ",
240 "-decrypt -inkey $smdir/smrsa3.pem -in test.cms -out smtst.txt"
244 "enveloped content test streaming S/MIME format, AES-256 cipher, 3 recipients",
245 "-encrypt -in smcont.txt"
246 . " -aes256 -stream -out test.cms"
247 . " $smdir/smrsa1.pem $smdir/smrsa2.pem $smdir/smrsa3.pem ",
248 "-decrypt -recip $smdir/smrsa1.pem -in test.cms -out smtst.txt"
253 my @smime_cms_tests = (
256 "signed content test streaming BER format, 2 DSA and 2 RSA keys, keyid",
257 "-sign -in smcont.txt -outform \"DER\" -nodetach -keyid"
258 . " -signer $smdir/smrsa1.pem -signer $smdir/smrsa2.pem"
259 . " -signer $smdir/smdsa1.pem -signer $smdir/smdsa2.pem"
260 . " -stream -out test.cms",
261 "-verify -in test.cms -inform \"DER\" "
262 . " \"-CAfile\" $smdir/smroot.pem -out smtst.txt"
266 "signed content test streaming PEM format, 2 DSA and 2 RSA keys",
267 "-sign -in smcont.txt -outform PEM -nodetach"
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 PEM "
272 . " \"-CAfile\" $smdir/smroot.pem -out smtst.txt"
276 "signed content MIME format, RSA key, signed receipt request",
277 "-sign -in smcont.txt -signer $smdir/smrsa1.pem -nodetach"
278 . " -receipt_request_to test\@openssl.org -receipt_request_all"
280 "-verify -in test.cms "
281 . " \"-CAfile\" $smdir/smroot.pem -out smtst.txt"
285 "signed receipt MIME format, RSA key",
286 "-sign_receipt -in test.cms"
287 . " -signer $smdir/smrsa2.pem"
289 "-verify_receipt test2.cms -in test.cms"
290 . " \"-CAfile\" $smdir/smroot.pem"
294 "enveloped content test streaming S/MIME format, 3 recipients, keyid",
295 "-encrypt -in smcont.txt"
296 . " -stream -out test.cms -keyid"
297 . " $smdir/smrsa1.pem $smdir/smrsa2.pem $smdir/smrsa3.pem ",
298 "-decrypt -recip $smdir/smrsa1.pem -in test.cms -out smtst.txt"
302 "enveloped content test streaming PEM format, KEK",
303 "-encrypt -in smcont.txt -outform PEM -aes128"
304 . " -stream -out test.cms "
305 . " -secretkey 000102030405060708090A0B0C0D0E0F "
306 . " -secretkeyid C0FEE0",
307 "-decrypt -in test.cms -out smtst.txt -inform PEM"
308 . " -secretkey 000102030405060708090A0B0C0D0E0F "
309 . " -secretkeyid C0FEE0"
313 "enveloped content test streaming PEM format, KEK, key only",
314 "-encrypt -in smcont.txt -outform PEM -aes128"
315 . " -stream -out test.cms "
316 . " -secretkey 000102030405060708090A0B0C0D0E0F "
317 . " -secretkeyid C0FEE0",
318 "-decrypt -in test.cms -out smtst.txt -inform PEM"
319 . " -secretkey 000102030405060708090A0B0C0D0E0F "
323 "data content test streaming PEM format",
324 "-data_create -in smcont.txt -outform PEM -nodetach"
325 . " -stream -out test.cms",
326 "-data_out -in test.cms -inform PEM -out smtst.txt"
330 "encrypted content test streaming PEM format, 128 bit RC2 key",
331 "\"-EncryptedData_encrypt\" -in smcont.txt -outform PEM"
332 . " -rc2 -secretkey 000102030405060708090A0B0C0D0E0F"
333 . " -stream -out test.cms",
334 "\"-EncryptedData_decrypt\" -in test.cms -inform PEM "
335 . " -secretkey 000102030405060708090A0B0C0D0E0F -out smtst.txt"
339 "encrypted content test streaming PEM format, 40 bit RC2 key",
340 "\"-EncryptedData_encrypt\" -in smcont.txt -outform PEM"
341 . " -rc2 -secretkey 0001020304"
342 . " -stream -out test.cms",
343 "\"-EncryptedData_decrypt\" -in test.cms -inform PEM "
344 . " -secretkey 0001020304 -out smtst.txt"
348 "encrypted content test streaming PEM format, triple DES key",
349 "\"-EncryptedData_encrypt\" -in smcont.txt -outform PEM"
350 . " -des3 -secretkey 000102030405060708090A0B0C0D0E0F1011121314151617"
351 . " -stream -out test.cms",
352 "\"-EncryptedData_decrypt\" -in test.cms -inform PEM "
353 . " -secretkey 000102030405060708090A0B0C0D0E0F1011121314151617"
358 "encrypted content test streaming PEM format, 128 bit AES key",
359 "\"-EncryptedData_encrypt\" -in smcont.txt -outform PEM"
360 . " -aes128 -secretkey 000102030405060708090A0B0C0D0E0F"
361 . " -stream -out test.cms",
362 "\"-EncryptedData_decrypt\" -in test.cms -inform PEM "
363 . " -secretkey 000102030405060708090A0B0C0D0E0F -out smtst.txt"
368 my @smime_cms_comp_tests = (
371 "compressed content test streaming PEM format",
372 "-compress -in smcont.txt -outform PEM -nodetach"
373 . " -stream -out test.cms",
374 "-uncompress -in test.cms -inform PEM -out smtst.txt"
379 my @smime_cms_param_tests = (
381 "signed content test streaming PEM format, RSA keys, PSS signature",
382 "-sign -in smcont.txt -outform PEM -nodetach"
383 . " -signer $smdir/smrsa1.pem -keyopt rsa_padding_mode:pss"
385 "-verify -in test.cms -inform PEM "
386 . " \"-CAfile\" $smdir/smroot.pem -out smtst.txt"
390 "signed content test streaming PEM format, RSA keys, PSS signature, no attributes",
391 "-sign -in smcont.txt -outform PEM -nodetach -noattr"
392 . " -signer $smdir/smrsa1.pem -keyopt rsa_padding_mode:pss"
394 "-verify -in test.cms -inform PEM "
395 . " \"-CAfile\" $smdir/smroot.pem -out smtst.txt"
399 "signed content test streaming PEM format, RSA keys, PSS signature, SHA384 MGF1",
400 "-sign -in smcont.txt -outform PEM -nodetach"
401 . " -signer $smdir/smrsa1.pem -keyopt rsa_padding_mode:pss"
402 . " -keyopt rsa_mgf1_md:sha384 -out test.cms",
403 "-verify -in test.cms -inform PEM "
404 . " \"-CAfile\" $smdir/smroot.pem -out smtst.txt"
408 "enveloped content test streaming S/MIME format, OAEP default parameters",
409 "-encrypt -in smcont.txt"
410 . " -stream -out test.cms"
411 . " -recip $smdir/smrsa1.pem -keyopt rsa_padding_mode:oaep",
412 "-decrypt -recip $smdir/smrsa1.pem -in test.cms -out smtst.txt"
416 "enveloped content test streaming S/MIME format, OAEP SHA256",
417 "-encrypt -in smcont.txt"
418 . " -stream -out test.cms"
419 . " -recip $smdir/smrsa1.pem -keyopt rsa_padding_mode:oaep"
420 . " -keyopt rsa_oaep_md:sha256",
421 "-decrypt -recip $smdir/smrsa1.pem -in test.cms -out smtst.txt"
425 "enveloped content test streaming S/MIME format, ECDH",
426 "-encrypt -in smcont.txt"
427 . " -stream -out test.cms"
428 . " -recip $smdir/smec1.pem",
429 "-decrypt -recip $smdir/smec1.pem -in test.cms -out smtst.txt"
433 "enveloped content test streaming S/MIME format, ECDH, AES128, SHA256 KDF",
434 "-encrypt -in smcont.txt"
435 . " -stream -out test.cms"
436 . " -recip $smdir/smec1.pem -aes128 -keyopt ecdh_kdf_md:sha256",
437 "-decrypt -recip $smdir/smec1.pem -in test.cms -out smtst.txt"
441 "enveloped content test streaming S/MIME format, ECDH, K-283, cofactor DH",
442 "-encrypt -in smcont.txt"
443 . " -stream -out test.cms"
444 . " -recip $smdir/smec2.pem -aes128"
445 . " -keyopt ecdh_kdf_md:sha256 -keyopt ecdh_cofactor_mode:1",
446 "-decrypt -recip $smdir/smec2.pem -in test.cms -out smtst.txt"
450 "enveloped content test streaming S/MIME format, X9.42 DH",
451 "-encrypt -in smcont.txt"
452 . " -stream -out test.cms"
453 . " -recip $smdir/smdh.pem -aes128",
454 "-decrypt -recip $smdir/smdh.pem -in test.cms -out smtst.txt"
458 print "CMS => PKCS#7 compatibility tests\n";
460 run_smime_tests( \$badcmd, \@smime_pkcs7_tests, $cmscmd, $pk7cmd );
462 print "CMS <= PKCS#7 compatibility tests\n";
464 run_smime_tests( \$badcmd, \@smime_pkcs7_tests, $pk7cmd, $cmscmd );
466 print "CMS <=> CMS consistency tests\n";
468 run_smime_tests( \$badcmd, \@smime_pkcs7_tests, $cmscmd, $cmscmd );
469 run_smime_tests( \$badcmd, \@smime_cms_tests, $cmscmd, $cmscmd );
471 print "CMS <=> CMS consistency tests, modified key parameters\n";
472 run_smime_tests( \$badcmd, \@smime_cms_param_tests, $cmscmd, $cmscmd );
474 if ( `$ossl_path version -f` =~ /ZLIB/ ) {
475 run_smime_tests( \$badcmd, \@smime_cms_comp_tests, $cmscmd, $cmscmd );
478 print "Zlib not supported: compression tests skipped\n";
481 print "Running modified tests for OpenSSL 0.9.8 cms backport\n" if($ossl8);
484 print "$badcmd TESTS FAILED!!\n";
487 print "ALL TESTS SUCCESSFUL.\n";
496 sub run_smime_tests {
497 my ( $rv, $aref, $scmd, $vcmd ) = @_;
499 foreach $smtst (@$aref) {
500 my ( $tnam, $rscmd, $rvcmd ) = @$smtst;
503 # Skip smime resign: 0.9.8 smime doesn't support -resign
504 next if ($scmd =~ /smime/ && $rscmd =~ /-resign/);
505 # Disable streaming: option not supported in 0.9.8
506 $tnam =~ s/streaming//;
507 $rscmd =~ s/-stream//;
508 $rvcmd =~ s/-stream//;
510 if ($no_ec && $tnam =~ /ECDH/)
512 print "$tnam: skipped, EC disabled\n";
515 if ($no_ec2m && $tnam =~ /K-283/)
517 print "$tnam: skipped, EC2M disabled\n";
520 system("$scmd$rscmd$redir");
522 print "$tnam: generation error\n";
527 system("$vcmd$rvcmd$redir");
529 print "$tnam: verify error\n";
534 if (!cmp_files("smtst.txt", "smcont.txt")) {
535 print "$tnam: content verify error\n";
546 my ( $f1, $f2 ) = @_;
547 my $fp1 = FileHandle->new();
548 my $fp2 = FileHandle->new();
552 if ( !open( $fp1, "<$f1" ) ) {
553 print STDERR "Can't Open file $f1\n";
557 if ( !open( $fp2, "<$f2" ) ) {
558 print STDERR "Can't Open file $f2\n";
568 $n1 = sysread $fp1, $rd1, 4096;
569 $n2 = sysread $fp2, $rd2, 4096;
570 last if ( $n1 != $n2 );
571 last if ( $rd1 ne $rd2 );