Add support for KEK decrypt in cms utility.
[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
78 my @smime_pkcs7_tests = (
79
80     [
81         "signed content DER format, RSA key",
82         "-sign -in smcont.txt -outform DER -nodetach"
83           . " -signer $smdir/smrsa1.pem -out test.cms",
84         "-verify -in test.cms -inform DER "
85           . " -CAfile $smdir/smroot.pem -out smtst.txt"
86     ],
87
88     [
89         "signed detached content DER format, RSA key",
90         "-sign -in smcont.txt -outform DER"
91           . " -signer $smdir/smrsa1.pem -out test.cms",
92         "-verify -in test.cms -inform DER "
93           . " -CAfile $smdir/smroot.pem -out smtst.txt -content smcont.txt"
94     ],
95
96     [
97         "signed content test streaming BER format, RSA",
98         "-sign -in smcont.txt -outform DER -nodetach"
99           . " -stream -signer $smdir/smrsa1.pem -out test.cms",
100         "-verify -in test.cms -inform DER "
101           . " -CAfile $smdir/smroot.pem -out smtst.txt"
102     ],
103
104     [
105         "signed content DER format, DSA key",
106         "-sign -in smcont.txt -outform DER -nodetach"
107           . " -signer $smdir/smdsa1.pem -out test.cms",
108         "-verify -in test.cms -inform DER "
109           . " -CAfile $smdir/smroot.pem -out smtst.txt"
110     ],
111
112     [
113         "signed detached content DER format, DSA key",
114         "-sign -in smcont.txt -outform DER"
115           . " -signer $smdir/smdsa1.pem -out test.cms",
116         "-verify -in test.cms -inform DER "
117           . " -CAfile $smdir/smroot.pem -out smtst.txt -content smcont.txt"
118     ],
119
120     [
121         "signed detached content DER format, add RSA signer",
122         "-resign -inform DER -in test.cms -outform DER"
123           . " -signer $smdir/smrsa1.pem -out test2.cms",
124         "-verify -in test2.cms -inform DER "
125           . " -CAfile $smdir/smroot.pem -out smtst.txt -content smcont.txt"
126     ],
127
128     [
129         "signed content test streaming BER format, DSA key",
130         "-sign -in smcont.txt -outform DER -nodetach"
131           . " -stream -signer $smdir/smdsa1.pem -out test.cms",
132         "-verify -in test.cms -inform DER "
133           . " -CAfile $smdir/smroot.pem -out smtst.txt"
134     ],
135
136     [
137         "signed content test streaming BER format, 2 DSA and 2 RSA keys",
138         "-sign -in smcont.txt -outform DER -nodetach"
139           . " -signer $smdir/smrsa1.pem -signer $smdir/smrsa2.pem"
140           . " -signer $smdir/smdsa1.pem -signer $smdir/smdsa2.pem"
141           . " -stream -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, no attributes",
148         "-sign -in smcont.txt -outform DER -noattr -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 S/MIME format, 2 DSA and 2 RSA keys",
158         "-sign -in smcont.txt -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 " . " -CAfile $smdir/smroot.pem -out smtst.txt"
163     ],
164
165     [
166 "signed content test streaming multipart S/MIME format, 2 DSA and 2 RSA keys",
167         "-sign -in smcont.txt"
168           . " -signer $smdir/smrsa1.pem -signer $smdir/smrsa2.pem"
169           . " -signer $smdir/smdsa1.pem -signer $smdir/smdsa2.pem"
170           . " -stream -out test.cms",
171         "-verify -in test.cms " . " -CAfile $smdir/smroot.pem -out smtst.txt"
172     ],
173
174     [
175         "enveloped content test streaming S/MIME format, 3 recipients",
176         "-encrypt -in smcont.txt"
177           . " -stream -out test.cms"
178           . " $smdir/smrsa1.pem $smdir/smrsa2.pem $smdir/smrsa3.pem ",
179         "-decrypt -recip $smdir/smrsa1.pem -in test.cms -out smtst.txt"
180     ],
181
182     [
183 "enveloped content test streaming S/MIME format, 3 recipients, 3rd used",
184         "-encrypt -in smcont.txt"
185           . " -stream -out test.cms"
186           . " $smdir/smrsa1.pem $smdir/smrsa2.pem $smdir/smrsa3.pem ",
187         "-decrypt -recip $smdir/smrsa3.pem -in test.cms -out smtst.txt"
188     ],
189
190     [
191 "enveloped content test streaming S/MIME format, 3 recipients, key only used",
192         "-encrypt -in smcont.txt"
193           . " -stream -out test.cms"
194           . " $smdir/smrsa1.pem $smdir/smrsa2.pem $smdir/smrsa3.pem ",
195         "-decrypt -inkey $smdir/smrsa3.pem -in test.cms -out smtst.txt"
196     ],
197
198     [
199 "enveloped content test streaming S/MIME format, AES-256 cipher, 3 recipients",
200         "-encrypt -in smcont.txt"
201           . " -aes256 -stream -out test.cms"
202           . " $smdir/smrsa1.pem $smdir/smrsa2.pem $smdir/smrsa3.pem ",
203         "-decrypt -recip $smdir/smrsa1.pem -in test.cms -out smtst.txt"
204     ],
205
206 );
207
208 my @smime_cms_tests = (
209
210     [
211         "signed content test streaming BER format, 2 DSA and 2 RSA keys, keyid",
212         "-sign -in smcont.txt -outform DER -nodetach -keyid"
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 -inform DER "
217           . " -CAfile $smdir/smroot.pem -out smtst.txt"
218     ],
219
220     [
221         "signed content test streaming PEM format, 2 DSA and 2 RSA keys",
222         "-sign -in smcont.txt -outform PEM -nodetach"
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 PEM "
227           . " -CAfile $smdir/smroot.pem -out smtst.txt"
228     ],
229
230     [
231         "enveloped content test streaming S/MIME format, 3 recipients, keyid",
232         "-encrypt -in smcont.txt"
233           . " -stream -out test.cms -keyid"
234           . " $smdir/smrsa1.pem $smdir/smrsa2.pem $smdir/smrsa3.pem ",
235         "-decrypt -recip $smdir/smrsa1.pem -in test.cms -out smtst.txt"
236     ],
237
238     [
239         "enveloped content test streaming PEM format, KEK",
240         "-encrypt -in smcont.txt -outform PEM -aes128"
241           . " -stream -out test.cms "
242           . " -secretkey 000102030405060708090A0B0C0D0E0F "
243           . " -secretkeyid C0FEE0",
244         "-decrypt -in test.cms -out smtst.txt -inform PEM"
245           . " -secretkey 000102030405060708090A0B0C0D0E0F "
246           . " -secretkeyid C0FEE0"
247     ],
248
249     [
250         "enveloped content test streaming PEM format, KEK, key only",
251         "-encrypt -in smcont.txt -outform PEM -aes128"
252           . " -stream -out test.cms "
253           . " -secretkey 000102030405060708090A0B0C0D0E0F "
254           . " -secretkeyid C0FEE0",
255         "-decrypt -in test.cms -out smtst.txt -inform PEM"
256           . " -secretkey 000102030405060708090A0B0C0D0E0F "
257     ],
258
259     [
260         "data content test streaming PEM format",
261         "-data_create -in smcont.txt -outform PEM -nodetach"
262           . " -stream -out test.cms",
263         "-data_out -in test.cms -inform PEM -out smtst.txt"
264     ],
265
266     [
267         "encrypted content test streaming PEM format, 128 bit RC2 key",
268         "-EncryptedData_encrypt -in smcont.txt -outform PEM"
269           . " -rc2 -secretkey 000102030405060708090A0B0C0D0E0F"
270           . " -stream -out test.cms",
271         "-EncryptedData_decrypt -in test.cms -inform PEM "
272           . " -secretkey 000102030405060708090A0B0C0D0E0F -out smtst.txt"
273     ],
274
275     [
276         "encrypted content test streaming PEM format, 40 bit RC2 key",
277         "-EncryptedData_encrypt -in smcont.txt -outform PEM"
278           . " -rc2 -secretkey 0001020304"
279           . " -stream -out test.cms",
280         "-EncryptedData_decrypt -in test.cms -inform PEM "
281           . " -secretkey 0001020304 -out smtst.txt"
282     ],
283
284     [
285         "encrypted content test streaming PEM format, triple DES key",
286         "-EncryptedData_encrypt -in smcont.txt -outform PEM"
287           . " -des3 -secretkey 000102030405060708090A0B0C0D0E0F1011121314151617"
288           . " -stream -out test.cms",
289         "-EncryptedData_decrypt -in test.cms -inform PEM "
290           . " -secretkey 000102030405060708090A0B0C0D0E0F1011121314151617"
291           . " -out smtst.txt"
292     ],
293
294     [
295         "encrypted content test streaming PEM format, 128 bit AES key",
296         "-EncryptedData_encrypt -in smcont.txt -outform PEM"
297           . " -aes128 -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
305 my @smime_cms_comp_tests = (
306
307     [
308         "compressed content test streaming PEM format",
309         "-compress -in smcont.txt -outform PEM -nodetach"
310           . " -stream -out test.cms",
311         "-uncompress -in test.cms -inform PEM -out smtst.txt"
312     ]
313
314 );
315
316 print "CMS => PKCS#7 compatibility tests\n";
317
318 run_smime_tests( \$badcmd, \@smime_pkcs7_tests, $cmscmd, $pk7cmd );
319
320 print "CMS <= PKCS#7 compatibility tests\n";
321
322 run_smime_tests( \$badcmd, \@smime_pkcs7_tests, $pk7cmd, $cmscmd );
323
324 print "CMS <=> CMS consistency tests\n";
325
326 run_smime_tests( \$badcmd, \@smime_pkcs7_tests, $cmscmd, $cmscmd );
327 run_smime_tests( \$badcmd, \@smime_cms_tests,   $cmscmd, $cmscmd );
328
329 if ( `$ossl_path version -f` =~ /ZLIB/ ) {
330     run_smime_tests( \$badcmd, \@smime_cms_comp_tests, $cmscmd, $cmscmd );
331 }
332 else {
333     print "Zlib not supported: compression tests skipped\n";
334 }
335
336 if ($badcmd) {
337     print "$badcmd TESTS FAILED!!\n";
338 }
339 else {
340     print "ALL TESTS SUCCESSFUL.\n";
341 }
342
343 unlink "test.cms";
344 unlink "test2.cms";
345 unlink "smtst.txt";
346 unlink "cms.out";
347 unlink "cms.err";
348
349 sub run_smime_tests {
350     my ( $rv, $aref, $scmd, $vcmd ) = @_;
351
352     foreach $smtst (@$aref) {
353         my ( $tnam, $rscmd, $rvcmd ) = @$smtst;
354         system("$scmd$rscmd 2>cms.err 1>cms.out");
355         if ($?) {
356             print "$tnam: generation error\n";
357             $$rv++;
358             exit 1 if $halt_err;
359             next;
360         }
361         system("$vcmd$rvcmd 2>cms.err 1>cms.out");
362         if ($?) {
363             print "$tnam: verify error\n";
364             $$rv++;
365             exit 1 if $halt_err;
366             next;
367         }
368         if (!cmp_files("smtst.txt", "smcont.txt")) {
369             print "$tnam: content verify error\n";
370             $$rv++;
371             exit 1 if $halt_err;
372             next;
373         }
374         print "$tnam: OK\n";
375     }
376 }
377
378 sub cmp_files {
379     my ( $f1, $f2 ) = @_;
380     my ( $fp1, $fp2 );
381
382     my ( $rd1, $rd2 );
383
384     if ( !open( $fp1, "<$f1" ) ) {
385         print STDERR "Can't Open file $f1\n";
386         return 0;
387     }
388
389     if ( !open( $fp2, "<$f2" ) ) {
390         print STDERR "Can't Open file $f2\n";
391         return 0;
392     }
393
394     binmode $fp1;
395     binmode $fp2;
396
397     my $ret = 0;
398
399     for ( ; ; ) {
400         $n1 = sysread $fp1, $rd1, 4096;
401         $n2 = sysread $fp2, $rd2, 4096;
402         last if ( $n1 != $n2 );
403         last if ( $rd1 ne $rd2 );
404
405         if ( $n1 == 0 ) {
406             $ret = 1;
407             last;
408         }
409
410     }
411
412     close $fp1;
413     close $fp2;
414
415     return $ret;
416
417 }
418