7421f1c19d42493f4aff66dddff07ccfff942694
[oweals/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
58 if ( -f "../apps/openssl" ) {
59     $ossl_path = "../util/shlib_wrap.sh ../apps/openssl";
60 }
61 elsif ( -f "..\\out32dll\\openssl.exe" ) {
62     $ossl_path = "..\\out32dll\\openssl.exe";
63 }
64 elsif ( -f "..\\out32\\openssl.exe" ) {
65     $ossl_path = "..\\out32\\openssl.exe";
66 }
67 else {
68     die "Can't find OpenSSL executable";
69 }
70
71 my $pk7cmd   = "$ossl_path smime ";
72 my $cmscmd   = "$ossl_path cms ";
73 my $smdir    = "smime-certs";
74 my $halt_err = 1;
75
76 my $badcmd = 0;
77 my $ossl8 = `$ossl_path version -v` =~ /0\.9\.8/;
78
79 my @smime_pkcs7_tests = (
80
81     [
82         "signed content DER format, RSA key",
83         "-sign -in smcont.txt -outform DER -nodetach"
84           . " -signer $smdir/smrsa1.pem -out test.cms",
85         "-verify -in test.cms -inform DER "
86           . " -CAfile $smdir/smroot.pem -out smtst.txt"
87     ],
88
89     [
90         "signed detached content DER format, RSA key",
91         "-sign -in smcont.txt -outform DER"
92           . " -signer $smdir/smrsa1.pem -out test.cms",
93         "-verify -in test.cms -inform DER "
94           . " -CAfile $smdir/smroot.pem -out smtst.txt -content smcont.txt"
95     ],
96
97     [
98         "signed content test streaming BER format, RSA",
99         "-sign -in smcont.txt -outform DER -nodetach"
100           . " -stream -signer $smdir/smrsa1.pem -out test.cms",
101         "-verify -in test.cms -inform DER "
102           . " -CAfile $smdir/smroot.pem -out smtst.txt"
103     ],
104
105     [
106         "signed content DER format, DSA key",
107         "-sign -in smcont.txt -outform DER -nodetach"
108           . " -signer $smdir/smdsa1.pem -out test.cms",
109         "-verify -in test.cms -inform DER "
110           . " -CAfile $smdir/smroot.pem -out smtst.txt"
111     ],
112
113     [
114         "signed detached content DER format, DSA key",
115         "-sign -in smcont.txt -outform DER"
116           . " -signer $smdir/smdsa1.pem -out test.cms",
117         "-verify -in test.cms -inform DER "
118           . " -CAfile $smdir/smroot.pem -out smtst.txt -content smcont.txt"
119     ],
120
121     [
122         "signed detached content DER format, add RSA signer",
123         "-resign -inform DER -in test.cms -outform DER"
124           . " -signer $smdir/smrsa1.pem -out test2.cms",
125         "-verify -in test2.cms -inform DER "
126           . " -CAfile $smdir/smroot.pem -out smtst.txt -content smcont.txt"
127     ],
128
129     [
130         "signed content test streaming BER format, DSA key",
131         "-sign -in smcont.txt -outform DER -nodetach"
132           . " -stream -signer $smdir/smdsa1.pem -out test.cms",
133         "-verify -in test.cms -inform DER "
134           . " -CAfile $smdir/smroot.pem -out smtst.txt"
135     ],
136
137     [
138         "signed content test streaming BER format, 2 DSA and 2 RSA keys",
139         "-sign -in smcont.txt -outform DER -nodetach"
140           . " -signer $smdir/smrsa1.pem -signer $smdir/smrsa2.pem"
141           . " -signer $smdir/smdsa1.pem -signer $smdir/smdsa2.pem"
142           . " -stream -out test.cms",
143         "-verify -in test.cms -inform DER "
144           . " -CAfile $smdir/smroot.pem -out smtst.txt"
145     ],
146
147     [
148 "signed content test streaming BER format, 2 DSA and 2 RSA keys, no attributes",
149         "-sign -in smcont.txt -outform DER -noattr -nodetach"
150           . " -signer $smdir/smrsa1.pem -signer $smdir/smrsa2.pem"
151           . " -signer $smdir/smdsa1.pem -signer $smdir/smdsa2.pem"
152           . " -stream -out test.cms",
153         "-verify -in test.cms -inform DER "
154           . " -CAfile $smdir/smroot.pem -out smtst.txt"
155     ],
156
157     [
158         "signed content test streaming S/MIME format, 2 DSA and 2 RSA keys",
159         "-sign -in smcont.txt -nodetach"
160           . " -signer $smdir/smrsa1.pem -signer $smdir/smrsa2.pem"
161           . " -signer $smdir/smdsa1.pem -signer $smdir/smdsa2.pem"
162           . " -stream -out test.cms",
163         "-verify -in test.cms " . " -CAfile $smdir/smroot.pem -out smtst.txt"
164     ],
165
166     [
167 "signed content test streaming multipart S/MIME format, 2 DSA and 2 RSA keys",
168         "-sign -in smcont.txt"
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         "enveloped content test streaming S/MIME format, 3 recipients",
177         "-encrypt -in smcont.txt"
178           . " -stream -out test.cms"
179           . " $smdir/smrsa1.pem $smdir/smrsa2.pem $smdir/smrsa3.pem ",
180         "-decrypt -recip $smdir/smrsa1.pem -in test.cms -out smtst.txt"
181     ],
182
183     [
184 "enveloped content test streaming S/MIME format, 3 recipients, 3rd used",
185         "-encrypt -in smcont.txt"
186           . " -stream -out test.cms"
187           . " $smdir/smrsa1.pem $smdir/smrsa2.pem $smdir/smrsa3.pem ",
188         "-decrypt -recip $smdir/smrsa3.pem -in test.cms -out smtst.txt"
189     ],
190
191     [
192 "enveloped content test streaming S/MIME format, 3 recipients, key only used",
193         "-encrypt -in smcont.txt"
194           . " -stream -out test.cms"
195           . " $smdir/smrsa1.pem $smdir/smrsa2.pem $smdir/smrsa3.pem ",
196         "-decrypt -inkey $smdir/smrsa3.pem -in test.cms -out smtst.txt"
197     ],
198
199     [
200 "enveloped content test streaming S/MIME format, AES-256 cipher, 3 recipients",
201         "-encrypt -in smcont.txt"
202           . " -aes256 -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"
205     ],
206
207 );
208
209 my @smime_cms_tests = (
210
211     [
212         "signed content test streaming BER format, 2 DSA and 2 RSA keys, keyid",
213         "-sign -in smcont.txt -outform DER -nodetach -keyid"
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 -inform DER "
218           . " -CAfile $smdir/smroot.pem -out smtst.txt"
219     ],
220
221     [
222         "signed content test streaming PEM format, 2 DSA and 2 RSA keys",
223         "-sign -in smcont.txt -outform PEM -nodetach"
224           . " -signer $smdir/smrsa1.pem -signer $smdir/smrsa2.pem"
225           . " -signer $smdir/smdsa1.pem -signer $smdir/smdsa2.pem"
226           . " -stream -out test.cms",
227         "-verify -in test.cms -inform PEM "
228           . " -CAfile $smdir/smroot.pem -out smtst.txt"
229     ],
230
231     [
232         "signed content MIME format, RSA key, signed receipt request",
233         "-sign -in smcont.txt -signer $smdir/smrsa1.pem -nodetach"
234           . " -receipt_request_to test@openssl.org -receipt_request_all"
235           . " -out test.cms",
236         "-verify -in test.cms "
237           . " -CAfile $smdir/smroot.pem -out smtst.txt"
238     ],
239
240     [
241         "signed receipt MIME format, RSA key",
242         "-sign_receipt -in test.cms"
243           . " -signer $smdir/smrsa2.pem"
244           . " -out test2.cms",
245         "-verify_receipt test2.cms -in test.cms"
246           . " -CAfile $smdir/smroot.pem"
247     ],
248
249     [
250         "enveloped content test streaming S/MIME format, 3 recipients, keyid",
251         "-encrypt -in smcont.txt"
252           . " -stream -out test.cms -keyid"
253           . " $smdir/smrsa1.pem $smdir/smrsa2.pem $smdir/smrsa3.pem ",
254         "-decrypt -recip $smdir/smrsa1.pem -in test.cms -out smtst.txt"
255     ],
256
257     [
258         "enveloped content test streaming PEM format, KEK",
259         "-encrypt -in smcont.txt -outform PEM -aes128"
260           . " -stream -out test.cms "
261           . " -secretkey 000102030405060708090A0B0C0D0E0F "
262           . " -secretkeyid C0FEE0",
263         "-decrypt -in test.cms -out smtst.txt -inform PEM"
264           . " -secretkey 000102030405060708090A0B0C0D0E0F "
265           . " -secretkeyid C0FEE0"
266     ],
267
268     [
269         "enveloped content test streaming PEM format, KEK, key only",
270         "-encrypt -in smcont.txt -outform PEM -aes128"
271           . " -stream -out test.cms "
272           . " -secretkey 000102030405060708090A0B0C0D0E0F "
273           . " -secretkeyid C0FEE0",
274         "-decrypt -in test.cms -out smtst.txt -inform PEM"
275           . " -secretkey 000102030405060708090A0B0C0D0E0F "
276     ],
277
278     [
279         "data content test streaming PEM format",
280         "-data_create -in smcont.txt -outform PEM -nodetach"
281           . " -stream -out test.cms",
282         "-data_out -in test.cms -inform PEM -out smtst.txt"
283     ],
284
285     [
286         "encrypted content test streaming PEM format, 128 bit RC2 key",
287         "-EncryptedData_encrypt -in smcont.txt -outform PEM"
288           . " -rc2 -secretkey 000102030405060708090A0B0C0D0E0F"
289           . " -stream -out test.cms",
290         "-EncryptedData_decrypt -in test.cms -inform PEM "
291           . " -secretkey 000102030405060708090A0B0C0D0E0F -out smtst.txt"
292     ],
293
294     [
295         "encrypted content test streaming PEM format, 40 bit RC2 key",
296         "-EncryptedData_encrypt -in smcont.txt -outform PEM"
297           . " -rc2 -secretkey 0001020304"
298           . " -stream -out test.cms",
299         "-EncryptedData_decrypt -in test.cms -inform PEM "
300           . " -secretkey 0001020304 -out smtst.txt"
301     ],
302
303     [
304         "encrypted content test streaming PEM format, triple DES key",
305         "-EncryptedData_encrypt -in smcont.txt -outform PEM"
306           . " -des3 -secretkey 000102030405060708090A0B0C0D0E0F1011121314151617"
307           . " -stream -out test.cms",
308         "-EncryptedData_decrypt -in test.cms -inform PEM "
309           . " -secretkey 000102030405060708090A0B0C0D0E0F1011121314151617"
310           . " -out smtst.txt"
311     ],
312
313     [
314         "encrypted content test streaming PEM format, 128 bit AES key",
315         "-EncryptedData_encrypt -in smcont.txt -outform PEM"
316           . " -aes128 -secretkey 000102030405060708090A0B0C0D0E0F"
317           . " -stream -out test.cms",
318         "-EncryptedData_decrypt -in test.cms -inform PEM "
319           . " -secretkey 000102030405060708090A0B0C0D0E0F -out smtst.txt"
320     ],
321
322 );
323
324 my @smime_cms_comp_tests = (
325
326     [
327         "compressed content test streaming PEM format",
328         "-compress -in smcont.txt -outform PEM -nodetach"
329           . " -stream -out test.cms",
330         "-uncompress -in test.cms -inform PEM -out smtst.txt"
331     ]
332
333 );
334
335 print "PKCS#7 <=> PKCS#7 consistency tests\n";
336
337 run_smime_tests( \$badcmd, \@smime_pkcs7_tests, $pk7cmd, $pk7cmd );
338
339 print "CMS => PKCS#7 compatibility tests\n";
340
341 run_smime_tests( \$badcmd, \@smime_pkcs7_tests, $cmscmd, $pk7cmd );
342
343 print "CMS <= PKCS#7 compatibility tests\n";
344
345 run_smime_tests( \$badcmd, \@smime_pkcs7_tests, $pk7cmd, $cmscmd );
346
347 print "CMS <=> CMS consistency tests\n";
348
349 run_smime_tests( \$badcmd, \@smime_pkcs7_tests, $cmscmd, $cmscmd );
350 run_smime_tests( \$badcmd, \@smime_cms_tests,   $cmscmd, $cmscmd );
351
352 if ( `$ossl_path version -f` =~ /ZLIB/ ) {
353     run_smime_tests( \$badcmd, \@smime_cms_comp_tests, $cmscmd, $cmscmd );
354 }
355 else {
356     print "Zlib not supported: compression tests skipped\n";
357 }
358
359 print "Running modified tests for OpenSSL 0.9.8 cms backport\n" if($ossl8);
360
361 if ($badcmd) {
362     print "$badcmd TESTS FAILED!!\n";
363 }
364 else {
365     print "ALL TESTS SUCCESSFUL.\n";
366 }
367
368 unlink "test.cms";
369 unlink "test2.cms";
370 unlink "smtst.txt";
371 unlink "cms.out";
372 unlink "cms.err";
373
374 sub run_smime_tests {
375     my ( $rv, $aref, $scmd, $vcmd ) = @_;
376
377     foreach $smtst (@$aref) {
378         my ( $tnam, $rscmd, $rvcmd ) = @$smtst;
379         if ($ossl8)
380                 {
381                 # Skip smime resign: 0.9.8 smime doesn't support -resign        
382                 next if ($scmd =~ /smime/ && $rscmd =~ /-resign/);
383                 # Disable streaming: option not supported in 0.9.8
384                 $tnam =~ s/streaming//; 
385                 $rscmd =~ s/-stream//;  
386                 $rvcmd =~ s/-stream//;
387                 }
388         system("$scmd$rscmd 2>cms.err 1>cms.out");
389         if ($?) {
390             print "$tnam: generation error\n";
391             $$rv++;
392             exit 1 if $halt_err;
393             next;
394         }
395         system("$vcmd$rvcmd 2>cms.err 1>cms.out");
396         if ($?) {
397             print "$tnam: verify error\n";
398             $$rv++;
399             exit 1 if $halt_err;
400             next;
401         }
402         if (!cmp_files("smtst.txt", "smcont.txt")) {
403             print "$tnam: content verify error\n";
404             $$rv++;
405             exit 1 if $halt_err;
406             next;
407         }
408         print "$tnam: OK\n";
409     }
410 }
411
412 sub cmp_files {
413     my ( $f1, $f2 ) = @_;
414     my ( $fp1, $fp2 );
415
416     my ( $rd1, $rd2 );
417
418     if ( !open( $fp1, "<$f1" ) ) {
419         print STDERR "Can't Open file $f1\n";
420         return 0;
421     }
422
423     if ( !open( $fp2, "<$f2" ) ) {
424         print STDERR "Can't Open file $f2\n";
425         return 0;
426     }
427
428     binmode $fp1;
429     binmode $fp2;
430
431     my $ret = 0;
432
433     for ( ; ; ) {
434         $n1 = sysread $fp1, $rd1, 4096;
435         $n2 = sysread $fp2, $rd2, 4096;
436         last if ( $n1 != $n2 );
437         last if ( $rd1 ne $rd2 );
438
439         if ( $n1 == 0 ) {
440             $ret = 1;
441             last;
442         }
443
444     }
445
446     close $fp1;
447     close $fp2;
448
449     return $ret;
450
451 }
452