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 "data content test streaming PEM format",
240 "-data_create -in smcont.txt -outform PEM -nodetach"
241 . " -stream -out test.cms",
242 "-data_out -in test.cms -inform PEM -out smtst.txt"
246 "encrypted content test streaming PEM format, 128 bit RC2 key",
247 "-EncryptedData_encrypt -in smcont.txt -outform PEM"
248 . " -rc2 -secretkey 000102030405060708090A0B0C0D0E0F"
249 . " -stream -out test.cms",
250 "-EncryptedData_decrypt -in test.cms -inform PEM "
251 . " -secretkey 000102030405060708090A0B0C0D0E0F -out smtst.txt"
255 "encrypted content test streaming PEM format, 40 bit RC2 key",
256 "-EncryptedData_encrypt -in smcont.txt -outform PEM"
257 . " -rc2 -secretkey 0001020304"
258 . " -stream -out test.cms",
259 "-EncryptedData_decrypt -in test.cms -inform PEM "
260 . " -secretkey 0001020304 -out smtst.txt"
264 "encrypted content test streaming PEM format, triple DES key",
265 "-EncryptedData_encrypt -in smcont.txt -outform PEM"
266 . " -des3 -secretkey 000102030405060708090A0B0C0D0E0F1011121314151617"
267 . " -stream -out test.cms",
268 "-EncryptedData_decrypt -in test.cms -inform PEM "
269 . " -secretkey 000102030405060708090A0B0C0D0E0F1011121314151617"
274 "encrypted content test streaming PEM format, 128 bit AES key",
275 "-EncryptedData_encrypt -in smcont.txt -outform PEM"
276 . " -aes128 -secretkey 000102030405060708090A0B0C0D0E0F"
277 . " -stream -out test.cms",
278 "-EncryptedData_decrypt -in test.cms -inform PEM "
279 . " -secretkey 000102030405060708090A0B0C0D0E0F -out smtst.txt"
284 my @smime_cms_comp_tests = (
287 "compressed content test streaming PEM format",
288 "-compress -in smcont.txt -outform PEM -nodetach"
289 . " -stream -out test.cms",
290 "-uncompress -in test.cms -inform PEM -out smtst.txt"
295 print "CMS => PKCS#7 compatibility tests\n";
297 run_smime_tests( \$badcmd, \@smime_pkcs7_tests, $cmscmd, $pk7cmd );
299 print "CMS <= PKCS#7 compatibility tests\n";
301 run_smime_tests( \$badcmd, \@smime_pkcs7_tests, $pk7cmd, $cmscmd );
303 print "CMS <=> CMS consistency tests\n";
305 run_smime_tests( \$badcmd, \@smime_pkcs7_tests, $cmscmd, $cmscmd );
306 run_smime_tests( \$badcmd, \@smime_cms_tests, $cmscmd, $cmscmd );
308 if ( `$ossl_path version -f` =~ /ZLIB/ ) {
309 run_smime_tests( \$badcmd, \@smime_cms_comp_tests, $cmscmd, $cmscmd );
312 print "Zlib not supported: compression tests skipped\n";
316 print "$badcmd TESTS FAILED!!\n";
319 print "ALL TESTS SUCCESSFUL.\n";
328 sub run_smime_tests {
329 my ( $rv, $aref, $scmd, $vcmd ) = @_;
331 foreach $smtst (@$aref) {
332 my ( $tnam, $rscmd, $rvcmd ) = @$smtst;
333 system("$scmd$rscmd 2>cms.err 1>cms.out");
335 print "$tnam: generation error\n";
340 system("$vcmd$rvcmd 2>cms.err 1>cms.out");
342 print "$tnam: verify error\n";
347 if (!cmp_files("smtst.txt", "smcont.txt")) {
348 print "$tnam: content verify error\n";
358 my ( $f1, $f2 ) = @_;
363 if ( !open( $fp1, "<$f1" ) ) {
364 print STDERR "Can't Open file $f1\n";
368 if ( !open( $fp2, "<$f2" ) ) {
369 print STDERR "Can't Open file $f2\n";
379 $n1 = sysread $fp1, $rd1, 4096;
380 $n2 = sysread $fp2, $rd2, 4096;
381 last if ( $n1 != $n2 );
382 last if ( $rd1 ne $rd2 );