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";
86 my $ossl8 = `$ossl_path version -v` =~ /0\.9\.8/;
88 system ("$ossl_path no-ec >/dev/null");
99 die "Error checking for EC support\n";
102 my @smime_pkcs7_tests = (
105 "signed content DER format, RSA key",
106 "-sign -in smcont.txt -outform \"DER\" -nodetach"
107 . " -certfile $smdir/smroot.pem"
108 . " -signer $smdir/smrsa1.pem -out test.cms",
109 "-verify -in test.cms -inform \"DER\" "
110 . " \"-CAfile\" $smdir/smroot.pem -out smtst.txt"
114 "signed detached content DER format, RSA key",
115 "-sign -in smcont.txt -outform \"DER\""
116 . " -signer $smdir/smrsa1.pem -out test.cms",
117 "-verify -in test.cms -inform \"DER\" "
118 . " \"-CAfile\" $smdir/smroot.pem -out smtst.txt -content smcont.txt"
122 "signed content test streaming BER format, RSA",
123 "-sign -in smcont.txt -outform \"DER\" -nodetach"
124 . " -stream -signer $smdir/smrsa1.pem -out test.cms",
125 "-verify -in test.cms -inform \"DER\" "
126 . " \"-CAfile\" $smdir/smroot.pem -out smtst.txt"
130 "signed content DER format, DSA key",
131 "-sign -in smcont.txt -outform \"DER\" -nodetach"
132 . " -signer $smdir/smdsa1.pem -out test.cms",
133 "-verify -in test.cms -inform \"DER\" "
134 . " \"-CAfile\" $smdir/smroot.pem -out smtst.txt"
138 "signed detached content DER format, DSA key",
139 "-sign -in smcont.txt -outform \"DER\""
140 . " -signer $smdir/smdsa1.pem -out test.cms",
141 "-verify -in test.cms -inform \"DER\" "
142 . " \"-CAfile\" $smdir/smroot.pem -out smtst.txt -content smcont.txt"
146 "signed detached content DER format, add RSA signer",
147 "-resign -inform \"DER\" -in test.cms -outform \"DER\""
148 . " -signer $smdir/smrsa1.pem -out test2.cms",
149 "-verify -in test2.cms -inform \"DER\" "
150 . " \"-CAfile\" $smdir/smroot.pem -out smtst.txt -content smcont.txt"
154 "signed content test streaming BER format, DSA key",
155 "-sign -in smcont.txt -outform \"DER\" -nodetach"
156 . " -stream -signer $smdir/smdsa1.pem -out test.cms",
157 "-verify -in test.cms -inform \"DER\" "
158 . " \"-CAfile\" $smdir/smroot.pem -out smtst.txt"
162 "signed content test streaming BER format, 2 DSA and 2 RSA keys",
163 "-sign -in smcont.txt -outform \"DER\" -nodetach"
164 . " -signer $smdir/smrsa1.pem -signer $smdir/smrsa2.pem"
165 . " -signer $smdir/smdsa1.pem -signer $smdir/smdsa2.pem"
166 . " -stream -out test.cms",
167 "-verify -in test.cms -inform \"DER\" "
168 . " \"-CAfile\" $smdir/smroot.pem -out smtst.txt"
172 "signed content test streaming BER format, 2 DSA and 2 RSA keys, no attributes",
173 "-sign -in smcont.txt -outform \"DER\" -noattr -nodetach"
174 . " -signer $smdir/smrsa1.pem -signer $smdir/smrsa2.pem"
175 . " -signer $smdir/smdsa1.pem -signer $smdir/smdsa2.pem"
176 . " -stream -out test.cms",
177 "-verify -in test.cms -inform \"DER\" "
178 . " \"-CAfile\" $smdir/smroot.pem -out smtst.txt"
182 "signed content test streaming S/MIME format, 2 DSA and 2 RSA keys",
183 "-sign -in smcont.txt -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 " . " \"-CAfile\" $smdir/smroot.pem -out smtst.txt"
191 "signed content test streaming multipart S/MIME format, 2 DSA and 2 RSA keys",
192 "-sign -in smcont.txt"
193 . " -signer $smdir/smrsa1.pem -signer $smdir/smrsa2.pem"
194 . " -signer $smdir/smdsa1.pem -signer $smdir/smdsa2.pem"
195 . " -stream -out test.cms",
196 "-verify -in test.cms " . " \"-CAfile\" $smdir/smroot.pem -out smtst.txt"
200 "enveloped content test streaming S/MIME format, 3 recipients",
201 "-encrypt -in smcont.txt"
202 . " -stream -out test.cms"
203 . " $smdir/smrsa1.pem $smdir/smrsa2.pem $smdir/smrsa3.pem ",
204 "-decrypt -recip $smdir/smrsa1.pem -in test.cms -out smtst.txt"
208 "enveloped content test streaming S/MIME format, 3 recipients, 3rd used",
209 "-encrypt -in smcont.txt"
210 . " -stream -out test.cms"
211 . " $smdir/smrsa1.pem $smdir/smrsa2.pem $smdir/smrsa3.pem ",
212 "-decrypt -recip $smdir/smrsa3.pem -in test.cms -out smtst.txt"
216 "enveloped content test streaming S/MIME format, 3 recipients, key only used",
217 "-encrypt -in smcont.txt"
218 . " -stream -out test.cms"
219 . " $smdir/smrsa1.pem $smdir/smrsa2.pem $smdir/smrsa3.pem ",
220 "-decrypt -inkey $smdir/smrsa3.pem -in test.cms -out smtst.txt"
224 "enveloped content test streaming S/MIME format, AES-256 cipher, 3 recipients",
225 "-encrypt -in smcont.txt"
226 . " -aes256 -stream -out test.cms"
227 . " $smdir/smrsa1.pem $smdir/smrsa2.pem $smdir/smrsa3.pem ",
228 "-decrypt -recip $smdir/smrsa1.pem -in test.cms -out smtst.txt"
233 my @smime_cms_tests = (
236 "signed content test streaming BER format, 2 DSA and 2 RSA keys, keyid",
237 "-sign -in smcont.txt -outform \"DER\" -nodetach -keyid"
238 . " -signer $smdir/smrsa1.pem -signer $smdir/smrsa2.pem"
239 . " -signer $smdir/smdsa1.pem -signer $smdir/smdsa2.pem"
240 . " -stream -out test.cms",
241 "-verify -in test.cms -inform \"DER\" "
242 . " \"-CAfile\" $smdir/smroot.pem -out smtst.txt"
246 "signed content test streaming PEM format, 2 DSA and 2 RSA keys",
247 "-sign -in smcont.txt -outform PEM -nodetach"
248 . " -signer $smdir/smrsa1.pem -signer $smdir/smrsa2.pem"
249 . " -signer $smdir/smdsa1.pem -signer $smdir/smdsa2.pem"
250 . " -stream -out test.cms",
251 "-verify -in test.cms -inform PEM "
252 . " \"-CAfile\" $smdir/smroot.pem -out smtst.txt"
256 "signed content MIME format, RSA key, signed receipt request",
257 "-sign -in smcont.txt -signer $smdir/smrsa1.pem -nodetach"
258 . " -receipt_request_to test\@openssl.org -receipt_request_all"
260 "-verify -in test.cms "
261 . " \"-CAfile\" $smdir/smroot.pem -out smtst.txt"
265 "signed receipt MIME format, RSA key",
266 "-sign_receipt -in test.cms"
267 . " -signer $smdir/smrsa2.pem"
269 "-verify_receipt test2.cms -in test.cms"
270 . " \"-CAfile\" $smdir/smroot.pem"
274 "enveloped content test streaming S/MIME format, 3 recipients, keyid",
275 "-encrypt -in smcont.txt"
276 . " -stream -out test.cms -keyid"
277 . " $smdir/smrsa1.pem $smdir/smrsa2.pem $smdir/smrsa3.pem ",
278 "-decrypt -recip $smdir/smrsa1.pem -in test.cms -out smtst.txt"
282 "enveloped content test streaming PEM format, KEK",
283 "-encrypt -in smcont.txt -outform PEM -aes128"
284 . " -stream -out test.cms "
285 . " -secretkey 000102030405060708090A0B0C0D0E0F "
286 . " -secretkeyid C0FEE0",
287 "-decrypt -in test.cms -out smtst.txt -inform PEM"
288 . " -secretkey 000102030405060708090A0B0C0D0E0F "
289 . " -secretkeyid C0FEE0"
293 "enveloped content test streaming PEM format, KEK, key only",
294 "-encrypt -in smcont.txt -outform PEM -aes128"
295 . " -stream -out test.cms "
296 . " -secretkey 000102030405060708090A0B0C0D0E0F "
297 . " -secretkeyid C0FEE0",
298 "-decrypt -in test.cms -out smtst.txt -inform PEM"
299 . " -secretkey 000102030405060708090A0B0C0D0E0F "
303 "data content test streaming PEM format",
304 "-data_create -in smcont.txt -outform PEM -nodetach"
305 . " -stream -out test.cms",
306 "-data_out -in test.cms -inform PEM -out smtst.txt"
310 "encrypted content test streaming PEM format, 128 bit RC2 key",
311 "\"-EncryptedData_encrypt\" -in smcont.txt -outform PEM"
312 . " -rc2 -secretkey 000102030405060708090A0B0C0D0E0F"
313 . " -stream -out test.cms",
314 "\"-EncryptedData_decrypt\" -in test.cms -inform PEM "
315 . " -secretkey 000102030405060708090A0B0C0D0E0F -out smtst.txt"
319 "encrypted content test streaming PEM format, 40 bit RC2 key",
320 "\"-EncryptedData_encrypt\" -in smcont.txt -outform PEM"
321 . " -rc2 -secretkey 0001020304"
322 . " -stream -out test.cms",
323 "\"-EncryptedData_decrypt\" -in test.cms -inform PEM "
324 . " -secretkey 0001020304 -out smtst.txt"
328 "encrypted content test streaming PEM format, triple DES key",
329 "\"-EncryptedData_encrypt\" -in smcont.txt -outform PEM"
330 . " -des3 -secretkey 000102030405060708090A0B0C0D0E0F1011121314151617"
331 . " -stream -out test.cms",
332 "\"-EncryptedData_decrypt\" -in test.cms -inform PEM "
333 . " -secretkey 000102030405060708090A0B0C0D0E0F1011121314151617"
338 "encrypted content test streaming PEM format, 128 bit AES key",
339 "\"-EncryptedData_encrypt\" -in smcont.txt -outform PEM"
340 . " -aes128 -secretkey 000102030405060708090A0B0C0D0E0F"
341 . " -stream -out test.cms",
342 "\"-EncryptedData_decrypt\" -in test.cms -inform PEM "
343 . " -secretkey 000102030405060708090A0B0C0D0E0F -out smtst.txt"
348 my @smime_cms_comp_tests = (
351 "compressed content test streaming PEM format",
352 "-compress -in smcont.txt -outform PEM -nodetach"
353 . " -stream -out test.cms",
354 "-uncompress -in test.cms -inform PEM -out smtst.txt"
359 my @smime_cms_param_tests = (
361 "signed content test streaming PEM format, RSA keys, PSS signature",
362 "-sign -in smcont.txt -outform PEM -nodetach"
363 . " -signer $smdir/smrsa1.pem -keyopt rsa_padding_mode:pss"
365 "-verify -in test.cms -inform PEM "
366 . " \"-CAfile\" $smdir/smroot.pem -out smtst.txt"
370 "signed content test streaming PEM format, RSA keys, PSS signature, no attributes",
371 "-sign -in smcont.txt -outform PEM -nodetach -noattr"
372 . " -signer $smdir/smrsa1.pem -keyopt rsa_padding_mode:pss"
374 "-verify -in test.cms -inform PEM "
375 . " \"-CAfile\" $smdir/smroot.pem -out smtst.txt"
379 "signed content test streaming PEM format, RSA keys, PSS signature, SHA384 MGF1",
380 "-sign -in smcont.txt -outform PEM -nodetach"
381 . " -signer $smdir/smrsa1.pem -keyopt rsa_padding_mode:pss"
382 . " -keyopt rsa_mgf1_md:sha384 -out test.cms",
383 "-verify -in test.cms -inform PEM "
384 . " \"-CAfile\" $smdir/smroot.pem -out smtst.txt"
388 "enveloped content test streaming S/MIME format, OAEP default parameters",
389 "-encrypt -in smcont.txt"
390 . " -stream -out test.cms"
391 . " -recip $smdir/smrsa1.pem -keyopt rsa_padding_mode:oaep",
392 "-decrypt -recip $smdir/smrsa1.pem -in test.cms -out smtst.txt"
396 "enveloped content test streaming S/MIME format, OAEP SHA256",
397 "-encrypt -in smcont.txt"
398 . " -stream -out test.cms"
399 . " -recip $smdir/smrsa1.pem -keyopt rsa_padding_mode:oaep"
400 . " -keyopt rsa_oaep_md:sha256",
401 "-decrypt -recip $smdir/smrsa1.pem -in test.cms -out smtst.txt"
405 "enveloped content test streaming S/MIME format, ECDH",
406 "-encrypt -in smcont.txt"
407 . " -stream -out test.cms"
408 . " -recip $smdir/smec1.pem",
409 "-decrypt -recip $smdir/smec1.pem -in test.cms -out smtst.txt"
413 "enveloped content test streaming S/MIME format, ECDH, AES128, SHA256 KDF",
414 "-encrypt -in smcont.txt"
415 . " -stream -out test.cms"
416 . " -recip $smdir/smec1.pem -aes128 -keyopt ecdh_kdf_md:sha256",
417 "-decrypt -recip $smdir/smec1.pem -in test.cms -out smtst.txt"
421 "enveloped content test streaming S/MIME format, ECDH, K-283, cofactor DH",
422 "-encrypt -in smcont.txt"
423 . " -stream -out test.cms"
424 . " -recip $smdir/smec2.pem -aes128"
425 . " -keyopt ecdh_kdf_md:sha256 -keyopt ecdh_cofactor_mode:1",
426 "-decrypt -recip $smdir/smec2.pem -in test.cms -out smtst.txt"
430 "enveloped content test streaming S/MIME format, X9.42 DH",
431 "-encrypt -in smcont.txt"
432 . " -stream -out test.cms"
433 . " -recip $smdir/smdh.pem -aes128",
434 "-decrypt -recip $smdir/smdh.pem -in test.cms -out smtst.txt"
438 print "CMS => PKCS#7 compatibility tests\n";
440 run_smime_tests( \$badcmd, \@smime_pkcs7_tests, $cmscmd, $pk7cmd );
442 print "CMS <= PKCS#7 compatibility tests\n";
444 run_smime_tests( \$badcmd, \@smime_pkcs7_tests, $pk7cmd, $cmscmd );
446 print "CMS <=> CMS consistency tests\n";
448 run_smime_tests( \$badcmd, \@smime_pkcs7_tests, $cmscmd, $cmscmd );
449 run_smime_tests( \$badcmd, \@smime_cms_tests, $cmscmd, $cmscmd );
451 print "CMS <=> CMS consistency tests, modified key parameters\n";
452 run_smime_tests( \$badcmd, \@smime_cms_param_tests, $cmscmd, $cmscmd );
454 if ( `$ossl_path version -f` =~ /ZLIB/ ) {
455 run_smime_tests( \$badcmd, \@smime_cms_comp_tests, $cmscmd, $cmscmd );
458 print "Zlib not supported: compression tests skipped\n";
461 print "Running modified tests for OpenSSL 0.9.8 cms backport\n" if($ossl8);
464 print "$badcmd TESTS FAILED!!\n";
467 print "ALL TESTS SUCCESSFUL.\n";
476 sub run_smime_tests {
477 my ( $rv, $aref, $scmd, $vcmd ) = @_;
479 foreach $smtst (@$aref) {
480 my ( $tnam, $rscmd, $rvcmd ) = @$smtst;
483 # Skip smime resign: 0.9.8 smime doesn't support -resign
484 next if ($scmd =~ /smime/ && $rscmd =~ /-resign/);
485 # Disable streaming: option not supported in 0.9.8
486 $tnam =~ s/streaming//;
487 $rscmd =~ s/-stream//;
488 $rvcmd =~ s/-stream//;
490 if ($no_ec && $tnam =~ /ECDH/)
492 print "$tnam: skipped, EC disabled\n";
495 system("$scmd$rscmd$redir");
497 print "$tnam: generation error\n";
502 system("$vcmd$rvcmd$redir");
504 print "$tnam: verify error\n";
509 if (!cmp_files("smtst.txt", "smcont.txt")) {
510 print "$tnam: content verify error\n";
521 my ( $f1, $f2 ) = @_;
522 my $fp1 = FileHandle->new();
523 my $fp2 = FileHandle->new();
527 if ( !open( $fp1, "<$f1" ) ) {
528 print STDERR "Can't Open file $f1\n";
532 if ( !open( $fp2, "<$f2" ) ) {
533 print STDERR "Can't Open file $f2\n";
543 $n1 = sysread $fp1, $rd1, 4096;
544 $n2 = sysread $fp2, $rd2, 4096;
545 last if ( $n1 != $n2 );
546 last if ( $rd1 ne $rd2 );