Add extensive PCKS7 and CMS consistency test script.
[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
57 my $ossl_path = "../apps/openssl";
58 my $cmd       = "$ossl_path cms ";
59 my $cmd2      = "$ossl_path smime ";
60 my $smdir     = "smime-certs";
61
62 my $badcmd = 0;
63
64 my @smime_pkcs7_tests = (
65
66     [
67         "signed content DER format, RSA key",
68         "-sign -in smcont.txt -outform DER -nodetach"
69           . " -signer $smdir/smrsa1.pem -out test.cms",
70         "-verify -in test.cms -inform DER "
71           . " -CAfile $smdir/smroot.pem -out smtst.txt"
72     ],
73
74     [
75         "signed detached content DER format, RSA key",
76
77         "-sign -in smcont.txt -outform DER"
78           . " -signer $smdir/smrsa1.pem -out test.cms",
79         "-verify -in test.cms -inform DER "
80           . " -CAfile $smdir/smroot.pem -out smtst.txt -content smcont.txt"
81     ],
82
83     [
84         "signed content test streaming BER format, RSA",
85         "-sign -in smcont.txt -outform DER -nodetach"
86           . " -stream -signer $smdir/smrsa1.pem -out test.cms",
87         "-verify -in test.cms -inform DER "
88           . " -CAfile $smdir/smroot.pem -out smtst.txt"
89     ],
90
91     [
92         "signed content DER format, DSA key",
93         "-sign -in smcont.txt -outform DER -nodetach"
94           . " -signer $smdir/smdsa1.pem -out test.cms",
95         "-verify -in test.cms -inform DER "
96           . " -CAfile $smdir/smroot.pem -out smtst.txt"
97     ],
98
99     [
100         "signed detached content DER format, DSA key",
101
102         "-sign -in smcont.txt -outform DER"
103           . " -signer $smdir/smdsa1.pem -out test.cms",
104         "-verify -in test.cms -inform DER "
105           . " -CAfile $smdir/smroot.pem -out smtst.txt -content smcont.txt"
106     ],
107
108     [
109         "signed detached content DER format, add RSA signer",
110
111         "-resign -inform DER -in test.cms -outform DER"
112           . " -signer $smdir/smrsa1.pem -out test2.cms",
113         "-verify -in test2.cms -inform DER "
114           . " -CAfile $smdir/smroot.pem -out smtst.txt -content smcont.txt"
115     ],
116
117     [
118         "signed content test streaming BER format, DSA key",
119         "-sign -in smcont.txt -outform DER -nodetach"
120           . " -stream -signer $smdir/smdsa1.pem -out test.cms",
121         "-verify -in test.cms -inform DER "
122           . " -CAfile $smdir/smroot.pem -out smtst.txt"
123     ],
124
125     [
126         "signed content test streaming BER format, 2 DSA and 2 RSA keys",
127         "-sign -in smcont.txt -outform DER -nodetach"
128           . " -signer $smdir/smrsa1.pem -signer $smdir/smrsa2.pem"
129           . " -signer $smdir/smdsa1.pem -signer $smdir/smdsa2.pem"
130           . " -stream -out test.cms",
131         "-verify -in test.cms -inform DER "
132           . " -CAfile $smdir/smroot.pem -out smtst.txt"
133     ],
134
135     [
136 "signed content test streaming BER format, 2 DSA and 2 RSA keys, no attributes",
137         "-sign -in smcont.txt -outform DER -noattr -nodetach"
138           . " -signer $smdir/smrsa1.pem -signer $smdir/smrsa2.pem"
139           . " -signer $smdir/smdsa1.pem -signer $smdir/smdsa2.pem"
140           . " -stream -out test.cms",
141         "-verify -in test.cms -inform DER "
142           . " -CAfile $smdir/smroot.pem -out smtst.txt"
143     ],
144
145     [
146         "signed content test streaming S/MIME format, 2 DSA and 2 RSA keys",
147         "-sign -in smcont.txt -nodetach"
148           . " -signer $smdir/smrsa1.pem -signer $smdir/smrsa2.pem"
149           . " -signer $smdir/smdsa1.pem -signer $smdir/smdsa2.pem"
150           . " -stream -out test.cms",
151         "-verify -in test.cms "
152           . " -CAfile $smdir/smroot.pem -out smtst.txt"
153     ],
154
155     [
156 "signed content test streaming multipart S/MIME format, 2 DSA and 2 RSA keys",
157         "-sign -in smcont.txt"
158           . " -signer $smdir/smrsa1.pem -signer $smdir/smrsa2.pem"
159           . " -signer $smdir/smdsa1.pem -signer $smdir/smdsa2.pem"
160           . " -stream -out test.cms",
161         "-verify -in test.cms "
162           . " -CAfile $smdir/smroot.pem -out smtst.txt"
163     ],
164
165     [
166         "enveloped content test streaming S/MIME format, 3 recipients",
167         "-encrypt -in smcont.txt"
168           . " -stream -out test.cms"
169           . " $smdir/smrsa1.pem $smdir/smrsa2.pem $smdir/smrsa3.pem ",
170         "-decrypt -recip $smdir/smrsa1.pem -in test.cms -out smtst.txt"
171     ],
172
173     [
174 "enveloped content test streaming S/MIME format, 3 recipients, 3rd used",
175         "-encrypt -in smcont.txt"
176           . " -stream -out test.cms"
177           . " $smdir/smrsa1.pem $smdir/smrsa2.pem $smdir/smrsa3.pem ",
178         "-decrypt -recip $smdir/smrsa3.pem -in test.cms -out smtst.txt"
179     ],
180
181     [
182 "enveloped content test streaming S/MIME format, 3 recipients, key only used",
183         "-encrypt -in smcont.txt"
184           . " -stream -out test.cms"
185           . " $smdir/smrsa1.pem $smdir/smrsa2.pem $smdir/smrsa3.pem ",
186         "-decrypt -inkey $smdir/smrsa3.pem -in test.cms -out smtst.txt"
187     ],
188
189     [
190 "enveloped content test streaming S/MIME format, AES-256 cipher, 3 recipients",
191         "-encrypt -in smcont.txt"
192           . " -aes256 -stream -out test.cms"
193           . " $smdir/smrsa1.pem $smdir/smrsa2.pem $smdir/smrsa3.pem ",
194         "-decrypt -recip $smdir/smrsa1.pem -in test.cms -out smtst.txt"
195     ],
196
197 );
198
199 my @smime_cms_tests = (
200
201     [
202         "signed content test streaming BER format, 2 DSA and 2 RSA keys, keyid",
203         "-sign -in smcont.txt -outform DER -nodetach -keyid"
204           . " -signer $smdir/smrsa1.pem -signer $smdir/smrsa2.pem"
205           . " -signer $smdir/smdsa1.pem -signer $smdir/smdsa2.pem"
206           . " -stream -out test.cms",
207         "-verify -in test.cms -inform DER "
208           . " -CAfile $smdir/smroot.pem -out smtst.txt"
209     ],
210
211     [
212         "signed content test streaming PEM format, 2 DSA and 2 RSA keys",
213         "-sign -in smcont.txt -outform PEM -nodetach"
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 PEM "
218           . " -CAfile $smdir/smroot.pem -out smtst.txt"
219     ],
220
221     [
222         "data content test streaming PEM format",
223         "-data_create -in smcont.txt -outform PEM -nodetach"
224           . " -stream -out test.cms",
225         "-data_out -in test.cms -inform PEM -out smtst.txt"
226     ],
227
228     [
229         "encrypted content test streaming PEM format, 128 bit RC2 key",
230         "-EncryptedData_encrypt -in smcont.txt -outform PEM"
231           . " -rc2 -secretkey 000102030405060708090A0B0C0D0E0F"
232           . " -stream -out test.cms",
233         "-EncryptedData_decrypt -in test.cms -inform PEM "
234           . " -secretkey 000102030405060708090A0B0C0D0E0F -out smtst.txt"
235     ],
236
237     [
238         "encrypted content test streaming PEM format, 40 bit RC2 key",
239         "-EncryptedData_encrypt -in smcont.txt -outform PEM"
240           . " -rc2 -secretkey 0001020304"
241           . " -stream -out test.cms",
242         "-EncryptedData_decrypt -in test.cms -inform PEM "
243           . " -secretkey 0001020304 -out smtst.txt"
244     ],
245
246     [
247         "encrypted content test streaming PEM format, triple DES key",
248         "-EncryptedData_encrypt -in smcont.txt -outform PEM"
249           . " -des3 -secretkey 000102030405060708090A0B0C0D0E0F1011121314151617"
250           . " -stream -out test.cms",
251         "-EncryptedData_decrypt -in test.cms -inform PEM "
252           . " -secretkey 000102030405060708090A0B0C0D0E0F1011121314151617"
253           . " -out smtst.txt"
254     ],
255
256     [
257         "encrypted content test streaming PEM format, 128 bit AES key",
258         "-EncryptedData_encrypt -in smcont.txt -outform PEM"
259           . " -aes128 -secretkey 000102030405060708090A0B0C0D0E0F"
260           . " -stream -out test.cms",
261         "-EncryptedData_decrypt -in test.cms -inform PEM "
262           . " -secretkey 000102030405060708090A0B0C0D0E0F -out smtst.txt"
263     ],
264
265 );
266
267 my @smime_cms_comp_tests = (
268
269     [
270         "compressed content test streaming PEM format",
271         "-compress -in smcont.txt -outform PEM -nodetach"
272           . " -stream -out test.cms",
273         "-uncompress -in test.cms -inform PEM -out smtst.txt"
274     ]
275
276 );
277
278 print "CMS => PKCS#7 compatibility tests\n";
279
280 run_smime_tests( \$badcmd, \@smime_pkcs7_tests, $cmd, $cmd2 );
281
282 print "CMS <= PKCS#7 compatibility tests\n";
283
284 run_smime_tests( \$badcmd, \@smime_pkcs7_tests, $cmd2, $cmd );
285
286 print "CMS <=> CMS consistency tests\n";
287
288 run_smime_tests( \$badcmd, \@smime_pkcs7_tests, $cmd, $cmd );
289 run_smime_tests( \$badcmd, \@smime_cms_tests,   $cmd, $cmd );
290
291 if ( `$ossl_path version -f` =~ /ZLIB/ ) {
292     run_smime_tests( \$badcmd, \@smime_cms_comp_tests, $cmd, $cmd );
293 }
294 else {
295     print "Zlib not supported: compression tests skipped\n";
296 }
297
298 if ($badcmd) {
299     print "$badcmd TESTS FAILED!!\n";
300 }
301 else {
302     print "ALL TESTS SUCCESSFUL.\n";
303 }
304
305 sub run_smime_tests {
306     my ( $rv, $aref, $scmd, $vcmd ) = @_;
307
308     foreach $smtst (@$aref) {
309         my ( $tnam, $rscmd, $rvcmd ) = @$smtst;
310         system( $scmd . $rscmd );
311         if ($?) {
312             print "$tnam: generation error\n";
313             $$rv++;
314             next;
315         }
316         system( $vcmd . $rvcmd );
317         if ($?) {
318             print "$tnam: verify error\n";
319             $$rv++;
320             next;
321         }
322         print "$tnam: OK\n";
323     }
324 }
325