New CMS tests.
[openssl.git] / test / cms-test.pl
1 # test/cms-test.pl
2 # Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3 # project.
4 #
5 # ====================================================================
6 # Copyright (c) 2008 The OpenSSL Project.  All rights reserved.
7 #
8 # Redistribution and use in source and binary forms, with or without
9 # modification, are permitted provided that the following conditions
10 # are met:
11 #
12 # 1. Redistributions of source code must retain the above copyright
13 #    notice, this list of conditions and the following disclaimer.
14 #
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
18 #    distribution.
19 #
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/)"
24 #
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.
29 #
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.
33 #
34 # 6. Redistributions of any form whatsoever must retain the following
35 #    acknowledgment:
36 #    "This product includes software developed by the OpenSSL Project
37 #    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
38 #
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 # ====================================================================
52
53 # CMS, PKCS7 consistency test script. Run extensive tests on
54 # OpenSSL PKCS#7 and CMS implementations.
55
56 my $ossl_path;
57 my $redir = " 2> cms.err > cms.out";
58 # Make VMS work
59 if ( $^O eq "VMS" && -f "OSSLX:openssl.exe" ) {
60     $ossl_path = "pipe mcr OSSLX:openssl";
61 }
62 # Make MSYS work
63 elsif ( $^O eq "MSWin32" && -f "../apps/openssl.exe" ) {
64     $ossl_path = "cmd /c ..\\apps\\openssl";
65 }
66 elsif ( -f "../apps/openssl$ENV{EXE_EXT}" ) {
67     $ossl_path = "../util/shlib_wrap.sh ../apps/openssl";
68 }
69 elsif ( -f "..\\out32dll\\openssl.exe" ) {
70     $ossl_path = "..\\out32dll\\openssl.exe";
71 }
72 elsif ( -f "..\\out32\\openssl.exe" ) {
73     $ossl_path = "..\\out32\\openssl.exe";
74 }
75 else {
76     die "Can't find OpenSSL executable";
77 }
78
79 my $pk7cmd   = "$ossl_path smime ";
80 my $cmscmd   = "$ossl_path cms ";
81 my $smdir    = "smime-certs";
82 my $halt_err = 1;
83
84 my $badcmd = 0;
85 my $ossl8 = `$ossl_path version -v` =~ /0\.9\.8/;
86
87 my @smime_pkcs7_tests = (
88
89     [
90         "signed content DER format, RSA key",
91         "-sign -in smcont.txt -outform \"DER\" -nodetach"
92           . " -certfile $smdir/smroot.pem"
93           . " -signer $smdir/smrsa1.pem -out test.cms",
94         "-verify -in test.cms -inform \"DER\" "
95           . " \"-CAfile\" $smdir/smroot.pem -out smtst.txt"
96     ],
97
98     [
99         "signed detached content DER format, RSA key",
100         "-sign -in smcont.txt -outform \"DER\""
101           . " -signer $smdir/smrsa1.pem -out test.cms",
102         "-verify -in test.cms -inform \"DER\" "
103           . " \"-CAfile\" $smdir/smroot.pem -out smtst.txt -content smcont.txt"
104     ],
105
106     [
107         "signed content test streaming BER format, RSA",
108         "-sign -in smcont.txt -outform \"DER\" -nodetach"
109           . " -stream -signer $smdir/smrsa1.pem -out test.cms",
110         "-verify -in test.cms -inform \"DER\" "
111           . " \"-CAfile\" $smdir/smroot.pem -out smtst.txt"
112     ],
113
114     [
115         "signed content DER format, DSA key",
116         "-sign -in smcont.txt -outform \"DER\" -nodetach"
117           . " -signer $smdir/smdsa1.pem -out test.cms",
118         "-verify -in test.cms -inform \"DER\" "
119           . " \"-CAfile\" $smdir/smroot.pem -out smtst.txt"
120     ],
121
122     [
123         "signed detached content DER format, DSA key",
124         "-sign -in smcont.txt -outform \"DER\""
125           . " -signer $smdir/smdsa1.pem -out test.cms",
126         "-verify -in test.cms -inform \"DER\" "
127           . " \"-CAfile\" $smdir/smroot.pem -out smtst.txt -content smcont.txt"
128     ],
129
130     [
131         "signed detached content DER format, add RSA signer",
132         "-resign -inform \"DER\" -in test.cms -outform \"DER\""
133           . " -signer $smdir/smrsa1.pem -out test2.cms",
134         "-verify -in test2.cms -inform \"DER\" "
135           . " \"-CAfile\" $smdir/smroot.pem -out smtst.txt -content smcont.txt"
136     ],
137
138     [
139         "signed content test streaming BER format, DSA key",
140         "-sign -in smcont.txt -outform \"DER\" -nodetach"
141           . " -stream -signer $smdir/smdsa1.pem -out test.cms",
142         "-verify -in test.cms -inform \"DER\" "
143           . " \"-CAfile\" $smdir/smroot.pem -out smtst.txt"
144     ],
145
146     [
147         "signed content test streaming BER format, 2 DSA and 2 RSA keys",
148         "-sign -in smcont.txt -outform \"DER\" -nodetach"
149           . " -signer $smdir/smrsa1.pem -signer $smdir/smrsa2.pem"
150           . " -signer $smdir/smdsa1.pem -signer $smdir/smdsa2.pem"
151           . " -stream -out test.cms",
152         "-verify -in test.cms -inform \"DER\" "
153           . " \"-CAfile\" $smdir/smroot.pem -out smtst.txt"
154     ],
155
156     [
157 "signed content test streaming BER format, 2 DSA and 2 RSA keys, no attributes",
158         "-sign -in smcont.txt -outform \"DER\" -noattr -nodetach"
159           . " -signer $smdir/smrsa1.pem -signer $smdir/smrsa2.pem"
160           . " -signer $smdir/smdsa1.pem -signer $smdir/smdsa2.pem"
161           . " -stream -out test.cms",
162         "-verify -in test.cms -inform \"DER\" "
163           . " \"-CAfile\" $smdir/smroot.pem -out smtst.txt"
164     ],
165
166     [
167         "signed content test streaming S/MIME format, 2 DSA and 2 RSA keys",
168         "-sign -in smcont.txt -nodetach"
169           . " -signer $smdir/smrsa1.pem -signer $smdir/smrsa2.pem"
170           . " -signer $smdir/smdsa1.pem -signer $smdir/smdsa2.pem"
171           . " -stream -out test.cms",
172         "-verify -in test.cms " . " \"-CAfile\" $smdir/smroot.pem -out smtst.txt"
173     ],
174
175     [
176 "signed content test streaming multipart S/MIME format, 2 DSA and 2 RSA keys",
177         "-sign -in smcont.txt"
178           . " -signer $smdir/smrsa1.pem -signer $smdir/smrsa2.pem"
179           . " -signer $smdir/smdsa1.pem -signer $smdir/smdsa2.pem"
180           . " -stream -out test.cms",
181         "-verify -in test.cms " . " \"-CAfile\" $smdir/smroot.pem -out smtst.txt"
182     ],
183
184     [
185         "enveloped content test streaming S/MIME format, 3 recipients",
186         "-encrypt -in smcont.txt"
187           . " -stream -out test.cms"
188           . " $smdir/smrsa1.pem $smdir/smrsa2.pem $smdir/smrsa3.pem ",
189         "-decrypt -recip $smdir/smrsa1.pem -in test.cms -out smtst.txt"
190     ],
191
192     [
193 "enveloped content test streaming S/MIME format, 3 recipients, 3rd used",
194         "-encrypt -in smcont.txt"
195           . " -stream -out test.cms"
196           . " $smdir/smrsa1.pem $smdir/smrsa2.pem $smdir/smrsa3.pem ",
197         "-decrypt -recip $smdir/smrsa3.pem -in test.cms -out smtst.txt"
198     ],
199
200     [
201 "enveloped content test streaming S/MIME format, 3 recipients, key only used",
202         "-encrypt -in smcont.txt"
203           . " -stream -out test.cms"
204           . " $smdir/smrsa1.pem $smdir/smrsa2.pem $smdir/smrsa3.pem ",
205         "-decrypt -inkey $smdir/smrsa3.pem -in test.cms -out smtst.txt"
206     ],
207
208     [
209 "enveloped content test streaming S/MIME format, AES-256 cipher, 3 recipients",
210         "-encrypt -in smcont.txt"
211           . " -aes256 -stream -out test.cms"
212           . " $smdir/smrsa1.pem $smdir/smrsa2.pem $smdir/smrsa3.pem ",
213         "-decrypt -recip $smdir/smrsa1.pem -in test.cms -out smtst.txt"
214     ],
215
216 );
217
218 my @smime_cms_tests = (
219
220     [
221         "signed content test streaming BER format, 2 DSA and 2 RSA keys, keyid",
222         "-sign -in smcont.txt -outform \"DER\" -nodetach -keyid"
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 -inform \"DER\" "
227           . " \"-CAfile\" $smdir/smroot.pem -out smtst.txt"
228     ],
229
230     [
231         "signed content test streaming PEM format, 2 DSA and 2 RSA keys",
232         "-sign -in smcont.txt -outform PEM -nodetach"
233           . " -signer $smdir/smrsa1.pem -signer $smdir/smrsa2.pem"
234           . " -signer $smdir/smdsa1.pem -signer $smdir/smdsa2.pem"
235           . " -stream -out test.cms",
236         "-verify -in test.cms -inform PEM "
237           . " \"-CAfile\" $smdir/smroot.pem -out smtst.txt"
238     ],
239
240     [
241         "signed content MIME format, RSA key, signed receipt request",
242         "-sign -in smcont.txt -signer $smdir/smrsa1.pem -nodetach"
243           . " -receipt_request_to test\@openssl.org -receipt_request_all"
244           . " -out test.cms",
245         "-verify -in test.cms "
246           . " \"-CAfile\" $smdir/smroot.pem -out smtst.txt"
247     ],
248
249     [
250         "signed receipt MIME format, RSA key",
251         "-sign_receipt -in test.cms"
252           . " -signer $smdir/smrsa2.pem"
253           . " -out test2.cms",
254         "-verify_receipt test2.cms -in test.cms"
255           . " \"-CAfile\" $smdir/smroot.pem"
256     ],
257
258     [
259         "enveloped content test streaming S/MIME format, 3 recipients, keyid",
260         "-encrypt -in smcont.txt"
261           . " -stream -out test.cms -keyid"
262           . " $smdir/smrsa1.pem $smdir/smrsa2.pem $smdir/smrsa3.pem ",
263         "-decrypt -recip $smdir/smrsa1.pem -in test.cms -out smtst.txt"
264     ],
265
266     [
267         "enveloped content test streaming PEM format, KEK",
268         "-encrypt -in smcont.txt -outform PEM -aes128"
269           . " -stream -out test.cms "
270           . " -secretkey 000102030405060708090A0B0C0D0E0F "
271           . " -secretkeyid C0FEE0",
272         "-decrypt -in test.cms -out smtst.txt -inform PEM"
273           . " -secretkey 000102030405060708090A0B0C0D0E0F "
274           . " -secretkeyid C0FEE0"
275     ],
276
277     [
278         "enveloped content test streaming PEM format, KEK, key only",
279         "-encrypt -in smcont.txt -outform PEM -aes128"
280           . " -stream -out test.cms "
281           . " -secretkey 000102030405060708090A0B0C0D0E0F "
282           . " -secretkeyid C0FEE0",
283         "-decrypt -in test.cms -out smtst.txt -inform PEM"
284           . " -secretkey 000102030405060708090A0B0C0D0E0F "
285     ],
286
287     [
288         "data content test streaming PEM format",
289         "-data_create -in smcont.txt -outform PEM -nodetach"
290           . " -stream -out test.cms",
291         "-data_out -in test.cms -inform PEM -out smtst.txt"
292     ],
293
294     [
295         "encrypted content test streaming PEM format, 128 bit RC2 key",
296         "\"-EncryptedData_encrypt\" -in smcont.txt -outform PEM"
297           . " -rc2 -secretkey 000102030405060708090A0B0C0D0E0F"
298           . " -stream -out test.cms",
299         "\"-EncryptedData_decrypt\" -in test.cms -inform PEM "
300           . " -secretkey 000102030405060708090A0B0C0D0E0F -out smtst.txt"
301     ],
302
303     [
304         "encrypted content test streaming PEM format, 40 bit RC2 key",
305         "\"-EncryptedData_encrypt\" -in smcont.txt -outform PEM"
306           . " -rc2 -secretkey 0001020304"
307           . " -stream -out test.cms",
308         "\"-EncryptedData_decrypt\" -in test.cms -inform PEM "
309           . " -secretkey 0001020304 -out smtst.txt"
310     ],
311
312     [
313         "encrypted content test streaming PEM format, triple DES key",
314         "\"-EncryptedData_encrypt\" -in smcont.txt -outform PEM"
315           . " -des3 -secretkey 000102030405060708090A0B0C0D0E0F1011121314151617"
316           . " -stream -out test.cms",
317         "\"-EncryptedData_decrypt\" -in test.cms -inform PEM "
318           . " -secretkey 000102030405060708090A0B0C0D0E0F1011121314151617"
319           . " -out smtst.txt"
320     ],
321
322     [
323         "encrypted content test streaming PEM format, 128 bit AES key",
324         "\"-EncryptedData_encrypt\" -in smcont.txt -outform PEM"
325           . " -aes128 -secretkey 000102030405060708090A0B0C0D0E0F"
326           . " -stream -out test.cms",
327         "\"-EncryptedData_decrypt\" -in test.cms -inform PEM "
328           . " -secretkey 000102030405060708090A0B0C0D0E0F -out smtst.txt"
329     ],
330
331 );
332
333 my @smime_cms_comp_tests = (
334
335     [
336         "compressed content test streaming PEM format",
337         "-compress -in smcont.txt -outform PEM -nodetach"
338           . " -stream -out test.cms",
339         "-uncompress -in test.cms -inform PEM -out smtst.txt"
340     ]
341
342 );
343
344 my @smime_cms_param_tests = (
345     [
346         "signed content test streaming PEM format, RSA keys, PSS signature",
347         "-sign -in smcont.txt -outform PEM -nodetach"
348           . " -signer $smdir/smrsa1.pem -keyopt rsa_padding_mode:pss"
349           . " -out test.cms",
350         "-verify -in test.cms -inform PEM "
351           . " \"-CAfile\" $smdir/smroot.pem -out smtst.txt"
352     ],
353
354     [
355         "signed content test streaming PEM format, RSA keys, PSS signature, no attributes",
356         "-sign -in smcont.txt -outform PEM -nodetach -noattr"
357           . " -signer $smdir/smrsa1.pem -keyopt rsa_padding_mode:pss"
358           . " -out test.cms",
359         "-verify -in test.cms -inform PEM "
360           . " \"-CAfile\" $smdir/smroot.pem -out smtst.txt"
361     ],
362
363     [
364         "signed content test streaming PEM format, RSA keys, PSS signature, SHA384 MGF1",
365         "-sign -in smcont.txt -outform PEM -nodetach"
366           . " -signer $smdir/smrsa1.pem -keyopt rsa_padding_mode:pss"
367           . " -keyopt rsa_mgf1_md:sha384 -out test.cms",
368         "-verify -in test.cms -inform PEM "
369           . " \"-CAfile\" $smdir/smroot.pem -out smtst.txt"
370     ],
371
372     [
373 "enveloped content test streaming S/MIME format, OAEP default parameters",
374         "-encrypt -in smcont.txt"
375           . " -stream -out test.cms"
376           . " -recip $smdir/smrsa1.pem -keyopt rsa_padding_mode:oaep",
377         "-decrypt -recip $smdir/smrsa1.pem -in test.cms -out smtst.txt"
378     ],
379
380     [
381 "enveloped content test streaming S/MIME format, OAEP SHA256",
382         "-encrypt -in smcont.txt"
383           . " -stream -out test.cms"
384           . " -recip $smdir/smrsa1.pem -keyopt rsa_padding_mode:oaep"
385           . " -keyopt rsa_oaep_md:sha256",
386         "-decrypt -recip $smdir/smrsa1.pem -in test.cms -out smtst.txt"
387     ],
388
389     [
390 "enveloped content test streaming S/MIME format, ECDH",
391         "-encrypt -in smcont.txt"
392           . " -stream -out test.cms"
393           . " -recip $smdir/smec1.pem",
394         "-decrypt -recip $smdir/smec1.pem -in test.cms -out smtst.txt"
395     ],
396
397     [
398 "enveloped content test streaming S/MIME format, ECDH, AES128, SHA256 KDF",
399         "-encrypt -in smcont.txt"
400           . " -stream -out test.cms"
401           . " -recip $smdir/smec1.pem -aes128 -keyopt ecdh_kdf_md:sha256",
402         "-decrypt -recip $smdir/smec1.pem -in test.cms -out smtst.txt"
403     ],
404
405     [
406 "enveloped content test streaming S/MIME format, ECDH, K-283, cofactor DH",
407         "-encrypt -in smcont.txt"
408           . " -stream -out test.cms"
409           . " -recip $smdir/smec2.pem -aes128"
410           . " -keyopt ecdh_kdf_md:sha256 -keyopt ecdh_cofactor_mode:1",
411         "-decrypt -recip $smdir/smec2.pem -in test.cms -out smtst.txt"
412     ]
413 );
414
415 print "CMS => PKCS#7 compatibility tests\n";
416
417 run_smime_tests( \$badcmd, \@smime_pkcs7_tests, $cmscmd, $pk7cmd );
418
419 print "CMS <= PKCS#7 compatibility tests\n";
420
421 run_smime_tests( \$badcmd, \@smime_pkcs7_tests, $pk7cmd, $cmscmd );
422
423 print "CMS <=> CMS consistency tests\n";
424
425 run_smime_tests( \$badcmd, \@smime_pkcs7_tests, $cmscmd, $cmscmd );
426 run_smime_tests( \$badcmd, \@smime_cms_tests,   $cmscmd, $cmscmd );
427
428 print "CMS <=> CMS consistency tests, modified key parameters\n";
429 run_smime_tests( \$badcmd, \@smime_cms_param_tests,   $cmscmd, $cmscmd );
430
431 if ( `$ossl_path version -f` =~ /ZLIB/ ) {
432     run_smime_tests( \$badcmd, \@smime_cms_comp_tests, $cmscmd, $cmscmd );
433 }
434 else {
435     print "Zlib not supported: compression tests skipped\n";
436 }
437
438 print "Running modified tests for OpenSSL 0.9.8 cms backport\n" if($ossl8);
439
440 if ($badcmd) {
441     print "$badcmd TESTS FAILED!!\n";
442 }
443 else {
444     print "ALL TESTS SUCCESSFUL.\n";
445 }
446
447 unlink "test.cms";
448 unlink "test2.cms";
449 unlink "smtst.txt";
450 unlink "cms.out";
451 unlink "cms.err";
452
453 sub run_smime_tests {
454     my ( $rv, $aref, $scmd, $vcmd ) = @_;
455
456     foreach $smtst (@$aref) {
457         my ( $tnam, $rscmd, $rvcmd ) = @$smtst;
458         if ($ossl8)
459                 {
460                 # Skip smime resign: 0.9.8 smime doesn't support -resign        
461                 next if ($scmd =~ /smime/ && $rscmd =~ /-resign/);
462                 # Disable streaming: option not supported in 0.9.8
463                 $tnam =~ s/streaming//; 
464                 $rscmd =~ s/-stream//;  
465                 $rvcmd =~ s/-stream//;
466                 }
467         system("$scmd$rscmd$redir");
468         if ($?) {
469             print "$tnam: generation error\n";
470             $$rv++;
471             exit 1 if $halt_err;
472             next;
473         }
474         system("$vcmd$rvcmd$redir");
475         if ($?) {
476             print "$tnam: verify error\n";
477             $$rv++;
478             exit 1 if $halt_err;
479             next;
480         }
481         if (!cmp_files("smtst.txt", "smcont.txt")) {
482             print "$tnam: content verify error\n";
483             $$rv++;
484             exit 1 if $halt_err;
485             next;
486         }
487         print "$tnam: OK\n";
488     }
489 }
490
491 sub cmp_files {
492     use FileHandle;
493     my ( $f1, $f2 ) = @_;
494     my $fp1 = FileHandle->new();
495     my $fp2 = FileHandle->new();
496
497     my ( $rd1, $rd2 );
498
499     if ( !open( $fp1, "<$f1" ) ) {
500         print STDERR "Can't Open file $f1\n";
501         return 0;
502     }
503
504     if ( !open( $fp2, "<$f2" ) ) {
505         print STDERR "Can't Open file $f2\n";
506         return 0;
507     }
508
509     binmode $fp1;
510     binmode $fp2;
511
512     my $ret = 0;
513
514     for ( ; ; ) {
515         $n1 = sysread $fp1, $rd1, 4096;
516         $n2 = sysread $fp2, $rd2, 4096;
517         last if ( $n1 != $n2 );
518         last if ( $rd1 ne $rd2 );
519
520         if ( $n1 == 0 ) {
521             $ret = 1;
522             last;
523         }
524
525     }
526
527     close $fp1;
528     close $fp2;
529
530     return $ret;
531
532 }
533