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";
87 my $ossl8 = `$ossl_path version -v` =~ /0\.9\.8/;
89 system ("$ossl_path no-ec >/dev/null");
100 die "Error checking for EC support\n";
103 system ("$ossl_path no-ec2m >/dev/null");
114 die "Error checking for EC2M support\n";
117 my @smime_pkcs7_tests = (
120 "signed content DER format, RSA key",
121 "-sign -in smcont.txt -outform \"DER\" -nodetach"
122 . " -certfile $smdir/smroot.pem"
123 . " -signer $smdir/smrsa1.pem -out test.cms",
124 "-verify -in test.cms -inform \"DER\" "
125 . " \"-CAfile\" $smdir/smroot.pem -out smtst.txt"
129 "signed detached content DER format, RSA key",
130 "-sign -in smcont.txt -outform \"DER\""
131 . " -signer $smdir/smrsa1.pem -out test.cms",
132 "-verify -in test.cms -inform \"DER\" "
133 . " \"-CAfile\" $smdir/smroot.pem -out smtst.txt -content smcont.txt"
137 "signed content test streaming BER format, RSA",
138 "-sign -in smcont.txt -outform \"DER\" -nodetach"
139 . " -stream -signer $smdir/smrsa1.pem -out test.cms",
140 "-verify -in test.cms -inform \"DER\" "
141 . " \"-CAfile\" $smdir/smroot.pem -out smtst.txt"
145 "signed content DER format, DSA key",
146 "-sign -in smcont.txt -outform \"DER\" -nodetach"
147 . " -signer $smdir/smdsa1.pem -out test.cms",
148 "-verify -in test.cms -inform \"DER\" "
149 . " \"-CAfile\" $smdir/smroot.pem -out smtst.txt"
153 "signed detached content DER format, DSA key",
154 "-sign -in smcont.txt -outform \"DER\""
155 . " -signer $smdir/smdsa1.pem -out test.cms",
156 "-verify -in test.cms -inform \"DER\" "
157 . " \"-CAfile\" $smdir/smroot.pem -out smtst.txt -content smcont.txt"
161 "signed detached content DER format, add RSA signer",
162 "-resign -inform \"DER\" -in test.cms -outform \"DER\""
163 . " -signer $smdir/smrsa1.pem -out test2.cms",
164 "-verify -in test2.cms -inform \"DER\" "
165 . " \"-CAfile\" $smdir/smroot.pem -out smtst.txt -content smcont.txt"
169 "signed content test streaming BER format, DSA key",
170 "-sign -in smcont.txt -outform \"DER\" -nodetach"
171 . " -stream -signer $smdir/smdsa1.pem -out test.cms",
172 "-verify -in test.cms -inform \"DER\" "
173 . " \"-CAfile\" $smdir/smroot.pem -out smtst.txt"
177 "signed content test streaming BER format, 2 DSA and 2 RSA keys",
178 "-sign -in smcont.txt -outform \"DER\" -nodetach"
179 . " -signer $smdir/smrsa1.pem -signer $smdir/smrsa2.pem"
180 . " -signer $smdir/smdsa1.pem -signer $smdir/smdsa2.pem"
181 . " -stream -out test.cms",
182 "-verify -in test.cms -inform \"DER\" "
183 . " \"-CAfile\" $smdir/smroot.pem -out smtst.txt"
187 "signed content test streaming BER format, 2 DSA and 2 RSA keys, no attributes",
188 "-sign -in smcont.txt -outform \"DER\" -noattr -nodetach"
189 . " -signer $smdir/smrsa1.pem -signer $smdir/smrsa2.pem"
190 . " -signer $smdir/smdsa1.pem -signer $smdir/smdsa2.pem"
191 . " -stream -out test.cms",
192 "-verify -in test.cms -inform \"DER\" "
193 . " \"-CAfile\" $smdir/smroot.pem -out smtst.txt"
197 "signed content test streaming S/MIME format, 2 DSA and 2 RSA keys",
198 "-sign -in smcont.txt -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 " . " \"-CAfile\" $smdir/smroot.pem -out smtst.txt"
206 "signed content test streaming multipart S/MIME format, 2 DSA and 2 RSA keys",
207 "-sign -in smcont.txt"
208 . " -signer $smdir/smrsa1.pem -signer $smdir/smrsa2.pem"
209 . " -signer $smdir/smdsa1.pem -signer $smdir/smdsa2.pem"
210 . " -stream -out test.cms",
211 "-verify -in test.cms " . " \"-CAfile\" $smdir/smroot.pem -out smtst.txt"
215 "enveloped content test streaming S/MIME format, 3 recipients",
216 "-encrypt -in smcont.txt"
217 . " -stream -out test.cms"
218 . " $smdir/smrsa1.pem $smdir/smrsa2.pem $smdir/smrsa3.pem ",
219 "-decrypt -recip $smdir/smrsa1.pem -in test.cms -out smtst.txt"
223 "enveloped content test streaming S/MIME format, 3 recipients, 3rd used",
224 "-encrypt -in smcont.txt"
225 . " -stream -out test.cms"
226 . " $smdir/smrsa1.pem $smdir/smrsa2.pem $smdir/smrsa3.pem ",
227 "-decrypt -recip $smdir/smrsa3.pem -in test.cms -out smtst.txt"
231 "enveloped content test streaming S/MIME format, 3 recipients, key only used",
232 "-encrypt -in smcont.txt"
233 . " -stream -out test.cms"
234 . " $smdir/smrsa1.pem $smdir/smrsa2.pem $smdir/smrsa3.pem ",
235 "-decrypt -inkey $smdir/smrsa3.pem -in test.cms -out smtst.txt"
239 "enveloped content test streaming S/MIME format, AES-256 cipher, 3 recipients",
240 "-encrypt -in smcont.txt"
241 . " -aes256 -stream -out test.cms"
242 . " $smdir/smrsa1.pem $smdir/smrsa2.pem $smdir/smrsa3.pem ",
243 "-decrypt -recip $smdir/smrsa1.pem -in test.cms -out smtst.txt"
248 my @smime_cms_tests = (
251 "signed content test streaming BER format, 2 DSA and 2 RSA keys, keyid",
252 "-sign -in smcont.txt -outform \"DER\" -nodetach -keyid"
253 . " -signer $smdir/smrsa1.pem -signer $smdir/smrsa2.pem"
254 . " -signer $smdir/smdsa1.pem -signer $smdir/smdsa2.pem"
255 . " -stream -out test.cms",
256 "-verify -in test.cms -inform \"DER\" "
257 . " \"-CAfile\" $smdir/smroot.pem -out smtst.txt"
261 "signed content test streaming PEM format, 2 DSA and 2 RSA keys",
262 "-sign -in smcont.txt -outform PEM -nodetach"
263 . " -signer $smdir/smrsa1.pem -signer $smdir/smrsa2.pem"
264 . " -signer $smdir/smdsa1.pem -signer $smdir/smdsa2.pem"
265 . " -stream -out test.cms",
266 "-verify -in test.cms -inform PEM "
267 . " \"-CAfile\" $smdir/smroot.pem -out smtst.txt"
271 "signed content MIME format, RSA key, signed receipt request",
272 "-sign -in smcont.txt -signer $smdir/smrsa1.pem -nodetach"
273 . " -receipt_request_to test\@openssl.org -receipt_request_all"
275 "-verify -in test.cms "
276 . " \"-CAfile\" $smdir/smroot.pem -out smtst.txt"
280 "signed receipt MIME format, RSA key",
281 "-sign_receipt -in test.cms"
282 . " -signer $smdir/smrsa2.pem"
284 "-verify_receipt test2.cms -in test.cms"
285 . " \"-CAfile\" $smdir/smroot.pem"
289 "enveloped content test streaming S/MIME format, 3 recipients, keyid",
290 "-encrypt -in smcont.txt"
291 . " -stream -out test.cms -keyid"
292 . " $smdir/smrsa1.pem $smdir/smrsa2.pem $smdir/smrsa3.pem ",
293 "-decrypt -recip $smdir/smrsa1.pem -in test.cms -out smtst.txt"
297 "enveloped content test streaming PEM format, KEK",
298 "-encrypt -in smcont.txt -outform PEM -aes128"
299 . " -stream -out test.cms "
300 . " -secretkey 000102030405060708090A0B0C0D0E0F "
301 . " -secretkeyid C0FEE0",
302 "-decrypt -in test.cms -out smtst.txt -inform PEM"
303 . " -secretkey 000102030405060708090A0B0C0D0E0F "
304 . " -secretkeyid C0FEE0"
308 "enveloped content test streaming PEM format, KEK, key only",
309 "-encrypt -in smcont.txt -outform PEM -aes128"
310 . " -stream -out test.cms "
311 . " -secretkey 000102030405060708090A0B0C0D0E0F "
312 . " -secretkeyid C0FEE0",
313 "-decrypt -in test.cms -out smtst.txt -inform PEM"
314 . " -secretkey 000102030405060708090A0B0C0D0E0F "
318 "data content test streaming PEM format",
319 "-data_create -in smcont.txt -outform PEM -nodetach"
320 . " -stream -out test.cms",
321 "-data_out -in test.cms -inform PEM -out smtst.txt"
325 "encrypted content test streaming PEM format, 128 bit RC2 key",
326 "\"-EncryptedData_encrypt\" -in smcont.txt -outform PEM"
327 . " -rc2 -secretkey 000102030405060708090A0B0C0D0E0F"
328 . " -stream -out test.cms",
329 "\"-EncryptedData_decrypt\" -in test.cms -inform PEM "
330 . " -secretkey 000102030405060708090A0B0C0D0E0F -out smtst.txt"
334 "encrypted content test streaming PEM format, 40 bit RC2 key",
335 "\"-EncryptedData_encrypt\" -in smcont.txt -outform PEM"
336 . " -rc2 -secretkey 0001020304"
337 . " -stream -out test.cms",
338 "\"-EncryptedData_decrypt\" -in test.cms -inform PEM "
339 . " -secretkey 0001020304 -out smtst.txt"
343 "encrypted content test streaming PEM format, triple DES key",
344 "\"-EncryptedData_encrypt\" -in smcont.txt -outform PEM"
345 . " -des3 -secretkey 000102030405060708090A0B0C0D0E0F1011121314151617"
346 . " -stream -out test.cms",
347 "\"-EncryptedData_decrypt\" -in test.cms -inform PEM "
348 . " -secretkey 000102030405060708090A0B0C0D0E0F1011121314151617"
353 "encrypted content test streaming PEM format, 128 bit AES key",
354 "\"-EncryptedData_encrypt\" -in smcont.txt -outform PEM"
355 . " -aes128 -secretkey 000102030405060708090A0B0C0D0E0F"
356 . " -stream -out test.cms",
357 "\"-EncryptedData_decrypt\" -in test.cms -inform PEM "
358 . " -secretkey 000102030405060708090A0B0C0D0E0F -out smtst.txt"
363 my @smime_cms_comp_tests = (
366 "compressed content test streaming PEM format",
367 "-compress -in smcont.txt -outform PEM -nodetach"
368 . " -stream -out test.cms",
369 "-uncompress -in test.cms -inform PEM -out smtst.txt"
374 my @smime_cms_param_tests = (
376 "signed content test streaming PEM format, RSA keys, PSS signature",
377 "-sign -in smcont.txt -outform PEM -nodetach"
378 . " -signer $smdir/smrsa1.pem -keyopt rsa_padding_mode:pss"
380 "-verify -in test.cms -inform PEM "
381 . " \"-CAfile\" $smdir/smroot.pem -out smtst.txt"
385 "signed content test streaming PEM format, RSA keys, PSS signature, no attributes",
386 "-sign -in smcont.txt -outform PEM -nodetach -noattr"
387 . " -signer $smdir/smrsa1.pem -keyopt rsa_padding_mode:pss"
389 "-verify -in test.cms -inform PEM "
390 . " \"-CAfile\" $smdir/smroot.pem -out smtst.txt"
394 "signed content test streaming PEM format, RSA keys, PSS signature, SHA384 MGF1",
395 "-sign -in smcont.txt -outform PEM -nodetach"
396 . " -signer $smdir/smrsa1.pem -keyopt rsa_padding_mode:pss"
397 . " -keyopt rsa_mgf1_md:sha384 -out test.cms",
398 "-verify -in test.cms -inform PEM "
399 . " \"-CAfile\" $smdir/smroot.pem -out smtst.txt"
403 "enveloped content test streaming S/MIME format, OAEP default parameters",
404 "-encrypt -in smcont.txt"
405 . " -stream -out test.cms"
406 . " -recip $smdir/smrsa1.pem -keyopt rsa_padding_mode:oaep",
407 "-decrypt -recip $smdir/smrsa1.pem -in test.cms -out smtst.txt"
411 "enveloped content test streaming S/MIME format, OAEP SHA256",
412 "-encrypt -in smcont.txt"
413 . " -stream -out test.cms"
414 . " -recip $smdir/smrsa1.pem -keyopt rsa_padding_mode:oaep"
415 . " -keyopt rsa_oaep_md:sha256",
416 "-decrypt -recip $smdir/smrsa1.pem -in test.cms -out smtst.txt"
420 "enveloped content test streaming S/MIME format, ECDH",
421 "-encrypt -in smcont.txt"
422 . " -stream -out test.cms"
423 . " -recip $smdir/smec1.pem",
424 "-decrypt -recip $smdir/smec1.pem -in test.cms -out smtst.txt"
428 "enveloped content test streaming S/MIME format, ECDH, AES128, SHA256 KDF",
429 "-encrypt -in smcont.txt"
430 . " -stream -out test.cms"
431 . " -recip $smdir/smec1.pem -aes128 -keyopt ecdh_kdf_md:sha256",
432 "-decrypt -recip $smdir/smec1.pem -in test.cms -out smtst.txt"
436 "enveloped content test streaming S/MIME format, ECDH, K-283, cofactor DH",
437 "-encrypt -in smcont.txt"
438 . " -stream -out test.cms"
439 . " -recip $smdir/smec2.pem -aes128"
440 . " -keyopt ecdh_kdf_md:sha256 -keyopt ecdh_cofactor_mode:1",
441 "-decrypt -recip $smdir/smec2.pem -in test.cms -out smtst.txt"
445 "enveloped content test streaming S/MIME format, X9.42 DH",
446 "-encrypt -in smcont.txt"
447 . " -stream -out test.cms"
448 . " -recip $smdir/smdh.pem -aes128",
449 "-decrypt -recip $smdir/smdh.pem -in test.cms -out smtst.txt"
453 print "CMS => PKCS#7 compatibility tests\n";
455 run_smime_tests( \$badcmd, \@smime_pkcs7_tests, $cmscmd, $pk7cmd );
457 print "CMS <= PKCS#7 compatibility tests\n";
459 run_smime_tests( \$badcmd, \@smime_pkcs7_tests, $pk7cmd, $cmscmd );
461 print "CMS <=> CMS consistency tests\n";
463 run_smime_tests( \$badcmd, \@smime_pkcs7_tests, $cmscmd, $cmscmd );
464 run_smime_tests( \$badcmd, \@smime_cms_tests, $cmscmd, $cmscmd );
466 print "CMS <=> CMS consistency tests, modified key parameters\n";
467 run_smime_tests( \$badcmd, \@smime_cms_param_tests, $cmscmd, $cmscmd );
469 if ( `$ossl_path version -f` =~ /ZLIB/ ) {
470 run_smime_tests( \$badcmd, \@smime_cms_comp_tests, $cmscmd, $cmscmd );
473 print "Zlib not supported: compression tests skipped\n";
476 print "Running modified tests for OpenSSL 0.9.8 cms backport\n" if($ossl8);
479 print "$badcmd TESTS FAILED!!\n";
482 print "ALL TESTS SUCCESSFUL.\n";
491 sub run_smime_tests {
492 my ( $rv, $aref, $scmd, $vcmd ) = @_;
494 foreach $smtst (@$aref) {
495 my ( $tnam, $rscmd, $rvcmd ) = @$smtst;
498 # Skip smime resign: 0.9.8 smime doesn't support -resign
499 next if ($scmd =~ /smime/ && $rscmd =~ /-resign/);
500 # Disable streaming: option not supported in 0.9.8
501 $tnam =~ s/streaming//;
502 $rscmd =~ s/-stream//;
503 $rvcmd =~ s/-stream//;
505 if ($no_ec && $tnam =~ /ECDH/)
507 print "$tnam: skipped, EC disabled\n";
510 if ($no_ec2m && $tnam =~ /K-283/)
512 print "$tnam: skipped, EC2M disabled\n";
515 system("$scmd$rscmd$redir");
517 print "$tnam: generation error\n";
522 system("$vcmd$rvcmd$redir");
524 print "$tnam: verify error\n";
529 if (!cmp_files("smtst.txt", "smcont.txt")) {
530 print "$tnam: content verify error\n";
541 my ( $f1, $f2 ) = @_;
542 my $fp1 = FileHandle->new();
543 my $fp2 = FileHandle->new();
547 if ( !open( $fp1, "<$f1" ) ) {
548 print STDERR "Can't Open file $f1\n";
552 if ( !open( $fp2, "<$f2" ) ) {
553 print STDERR "Can't Open file $f2\n";
563 $n1 = sysread $fp1, $rd1, 4096;
564 $n2 = sysread $fp2, $rd2, 4096;
565 last if ( $n1 != $n2 );
566 last if ( $rd1 ne $rd2 );