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";
78 my @smime_pkcs7_tests = (
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"
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"
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"
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"
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"
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"
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"
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"
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"
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"
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"
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"
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"
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"
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"
208 my @smime_cms_tests = (
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"
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"
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"
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"
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 "
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"
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"
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"
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"
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"
305 my @smime_cms_comp_tests = (
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"
316 print "CMS => PKCS#7 compatibility tests\n";
318 run_smime_tests( \$badcmd, \@smime_pkcs7_tests, $cmscmd, $pk7cmd );
320 print "CMS <= PKCS#7 compatibility tests\n";
322 run_smime_tests( \$badcmd, \@smime_pkcs7_tests, $pk7cmd, $cmscmd );
324 print "CMS <=> CMS consistency tests\n";
326 run_smime_tests( \$badcmd, \@smime_pkcs7_tests, $cmscmd, $cmscmd );
327 run_smime_tests( \$badcmd, \@smime_cms_tests, $cmscmd, $cmscmd );
329 if ( `$ossl_path version -f` =~ /ZLIB/ ) {
330 run_smime_tests( \$badcmd, \@smime_cms_comp_tests, $cmscmd, $cmscmd );
333 print "Zlib not supported: compression tests skipped\n";
337 print "$badcmd TESTS FAILED!!\n";
340 print "ALL TESTS SUCCESSFUL.\n";
349 sub run_smime_tests {
350 my ( $rv, $aref, $scmd, $vcmd ) = @_;
352 foreach $smtst (@$aref) {
353 my ( $tnam, $rscmd, $rvcmd ) = @$smtst;
354 system("$scmd$rscmd 2>cms.err 1>cms.out");
356 print "$tnam: generation error\n";
361 system("$vcmd$rvcmd 2>cms.err 1>cms.out");
363 print "$tnam: verify error\n";
368 if (!cmp_files("smtst.txt", "smcont.txt")) {
369 print "$tnam: content verify error\n";
379 my ( $f1, $f2 ) = @_;
384 if ( !open( $fp1, "<$f1" ) ) {
385 print STDERR "Can't Open file $f1\n";
389 if ( !open( $fp2, "<$f2" ) ) {
390 print STDERR "Can't Open file $f2\n";
400 $n1 = sysread $fp1, $rd1, 4096;
401 $n2 = sysread $fp2, $rd2, 4096;
402 last if ( $n1 != $n2 );
403 last if ( $rd1 ne $rd2 );