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.
58 if ( -f "../apps/openssl" ) {
59 $ossl_path = "../util/shlib_wrap.sh ../apps/openssl";
61 elsif ( -f "..\\out32dll\\openssl.exe" ) {
62 $ossl_path = "..\\out32dll\\openssl.exe";
64 elsif ( -f "..\\out32\\openssl.exe" ) {
65 $ossl_path = "..\\out32\\openssl.exe";
68 die "Can't find OpenSSL executable";
71 my $pk7cmd = "$ossl_path smime ";
72 my $cmscmd = "$ossl_path cms ";
73 my $smdir = "smime-certs";
77 my $ossl8 = `$ossl_path version -v` =~ /0\.9\.8/;
79 my @smime_pkcs7_tests = (
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"
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"
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"
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"
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"
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"
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"
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"
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"
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"
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"
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"
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"
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"
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"
209 my @smime_cms_tests = (
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"
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"
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"
236 "-verify -in test.cms "
237 . " -CAfile $smdir/smroot.pem -out smtst.txt"
241 "signed receipt MIME format, RSA key",
242 "-sign_receipt -in test.cms"
243 . " -signer $smdir/smrsa2.pem"
245 "-verify_receipt test2.cms -in test.cms"
246 . " -CAfile $smdir/smroot.pem"
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"
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"
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 "
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"
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"
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"
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"
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"
324 my @smime_cms_comp_tests = (
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"
335 print "CMS => PKCS#7 compatibility tests\n";
337 run_smime_tests( \$badcmd, \@smime_pkcs7_tests, $cmscmd, $pk7cmd );
339 print "CMS <= PKCS#7 compatibility tests\n";
341 run_smime_tests( \$badcmd, \@smime_pkcs7_tests, $pk7cmd, $cmscmd );
343 print "CMS <=> CMS consistency tests\n";
345 run_smime_tests( \$badcmd, \@smime_pkcs7_tests, $cmscmd, $cmscmd );
346 run_smime_tests( \$badcmd, \@smime_cms_tests, $cmscmd, $cmscmd );
348 if ( `$ossl_path version -f` =~ /ZLIB/ ) {
349 run_smime_tests( \$badcmd, \@smime_cms_comp_tests, $cmscmd, $cmscmd );
352 print "Zlib not supported: compression tests skipped\n";
355 print "Running modified tests for OpenSSL 0.9.8 cms backport\n" if($ossl8);
358 print "$badcmd TESTS FAILED!!\n";
361 print "ALL TESTS SUCCESSFUL.\n";
370 sub run_smime_tests {
371 my ( $rv, $aref, $scmd, $vcmd ) = @_;
373 foreach $smtst (@$aref) {
374 my ( $tnam, $rscmd, $rvcmd ) = @$smtst;
377 # Skip smime resign: 0.9.8 smime doesn't support -resign
378 next if ($scmd =~ /smime/ && $rscmd =~ /-resign/);
379 # Disable streaming: option not supported in 0.9.8
380 $tnam =~ s/streaming//;
381 $rscmd =~ s/-stream//;
382 $rvcmd =~ s/-stream//;
384 system("$scmd$rscmd 2>cms.err 1>cms.out");
386 print "$tnam: generation error\n";
391 system("$vcmd$rvcmd 2>cms.err 1>cms.out");
393 print "$tnam: verify error\n";
398 if (!cmp_files("smtst.txt", "smcont.txt")) {
399 print "$tnam: content verify error\n";
409 my ( $f1, $f2 ) = @_;
414 if ( !open( $fp1, "<$f1" ) ) {
415 print STDERR "Can't Open file $f1\n";
419 if ( !open( $fp2, "<$f2" ) ) {
420 print STDERR "Can't Open file $f2\n";
430 $n1 = sysread $fp1, $rd1, 4096;
431 $n2 = sysread $fp2, $rd2, 4096;
432 last if ( $n1 != $n2 );
433 last if ( $rd1 ne $rd2 );