]>
git.ipfire.org Git - thirdparty/openssl.git/blob - test/recipes/80-test_cms.t
2 # Copyright 2015-2022 The OpenSSL Project Authors. All Rights Reserved.
4 # Licensed under the Apache License 2.0 (the "License"). You may not use
5 # this file except in compliance with the License. You can obtain a copy
6 # in the file LICENSE in the source distribution or at
7 # https://www.openssl.org/source/license.html
14 use File
::Spec
::Functions qw
/catfile/;
15 use File
::Compare qw
/compare_text compare/;
16 use OpenSSL
::Test qw
/:DEFAULT srctop_dir srctop_file bldtop_dir bldtop_file/;
18 use OpenSSL
::Test
::Utils
;
24 use lib srctop_dir
('Configurations');
25 use lib bldtop_dir
('.');
27 my $no_fips = disabled
('fips') || ($ENV{NO_FIPS
} // 0);
29 plan skip_all
=> "CMS is not supported by this OpenSSL build"
32 my $provpath = bldtop_dir
("providers");
34 # Some tests require legacy algorithms to be included.
35 my @legacyprov = ("-provider-path", $provpath,
36 "-provider", "default",
37 "-provider", "legacy" );
38 my @defaultprov = ("-provider-path", $provpath,
39 "-provider", "default");
42 my $provname = 'default';
44 my $datadir = srctop_dir
("test", "recipes", "80-test_cms_data");
45 my $smdir = srctop_dir
("test", "smime-certs");
46 my $smcont = srctop_file
("test", "smcont.txt");
47 my $smcont_zero = srctop_file
("test", "smcont_zero.txt");
48 my ($no_des, $no_dh, $no_dsa, $no_ec, $no_ec2m, $no_rc2, $no_zlib)
49 = disabled qw
/des dh dsa ec ec2m rc2 zlib/;
51 $no_rc2 = 1 if disabled
("legacy");
55 ok
(run
(test
(["pkcs7_test"])), "test pkcs7");
58 @config = ( "-config", srctop_file
("test", "fips-and-base.cnf") );
62 $ENV{OPENSSL_TEST_LIBCTX
} = "1";
63 my @prov = ("-provider-path", $provpath,
65 "-provider", $provname);
67 my $smrsa1 = catfile
($smdir, "smrsa1.pem");
68 my $smroot = catfile
($smdir, "smroot.pem");
70 my @smime_pkcs7_tests = (
72 [ "signed content DER format, RSA key",
73 [ "{cmd1}", @prov, "-sign", "-in", $smcont, "-outform", "DER", "-nodetach",
74 "-certfile", $smroot, "-signer", $smrsa1, "-out", "{output}.cms" ],
75 [ "{cmd2}", @prov, "-verify", "-in", "{output}.cms", "-inform", "DER",
76 "-CAfile", $smroot, "-out", "{output}.txt" ],
80 [ "signed detached content DER format, RSA key",
81 [ "{cmd1}", @prov, "-sign", "-in", $smcont, "-outform", "DER",
82 "-signer", $smrsa1, "-out", "{output}.cms" ],
83 [ "{cmd2}", @prov, "-verify", "-in", "{output}.cms", "-inform", "DER",
84 "-CAfile", $smroot, "-out", "{output}.txt",
85 "-content", $smcont ],
89 [ "signed content test streaming BER format, RSA",
90 [ "{cmd1}", @prov, "-sign", "-in", $smcont, "-outform", "DER", "-nodetach",
92 "-signer", $smrsa1, "-out", "{output}.cms" ],
93 [ "{cmd2}", @prov, "-verify", "-in", "{output}.cms", "-inform", "DER",
94 "-CAfile", $smroot, "-out", "{output}.txt" ],
98 [ "signed content DER format, DSA key",
99 [ "{cmd1}", @prov, "-sign", "-in", $smcont, "-outform", "DER", "-nodetach",
100 "-signer", catfile
($smdir, "smdsa1.pem"), "-out", "{output}.cms" ],
101 [ "{cmd2}", @prov, "-verify", "-in", "{output}.cms", "-inform", "DER",
102 "-CAfile", $smroot, "-out", "{output}.txt" ],
106 [ "signed detached content DER format, DSA key",
107 [ "{cmd1}", @prov, "-sign", "-in", $smcont, "-outform", "DER",
108 "-signer", catfile
($smdir, "smdsa1.pem"), "-out", "{output}.cms" ],
109 [ "{cmd2}", @prov, "-verify", "-in", "{output}.cms", "-inform", "DER",
110 "-CAfile", $smroot, "-out", "{output}.txt",
111 "-content", $smcont ],
115 [ "signed detached content DER format, add RSA signer (with DSA existing)",
116 [ "{cmd1}", @prov, "-sign", "-in", $smcont, "-outform", "DER",
117 "-signer", catfile
($smdir, "smdsa1.pem"), "-out", "{output}.cms" ],
118 [ "{cmd1}", @prov, "-resign", "-in", "{output}.cms", "-inform", "DER", "-outform", "DER",
119 "-signer", $smrsa1, "-out", "{output}2.cms" ],
120 [ "{cmd2}", @prov, "-verify", "-in", "{output}2.cms", "-inform", "DER",
121 "-CAfile", $smroot, "-out", "{output}.txt",
122 "-content", $smcont ],
126 [ "signed content test streaming BER format, DSA key",
127 [ "{cmd1}", @prov, "-sign", "-in", $smcont, "-outform", "DER",
128 "-nodetach", "-stream",
129 "-signer", catfile
($smdir, "smdsa1.pem"), "-out", "{output}.cms" ],
130 [ "{cmd2}", @prov, "-verify", "-in", "{output}.cms", "-inform", "DER",
131 "-CAfile", $smroot, "-out", "{output}.txt" ],
135 [ "signed content test streaming BER format, 2 DSA and 2 RSA keys",
136 [ "{cmd1}", @prov, "-sign", "-in", $smcont, "-outform", "DER",
137 "-nodetach", "-stream",
139 "-signer", catfile
($smdir, "smrsa2.pem"),
140 "-signer", catfile
($smdir, "smdsa1.pem"),
141 "-signer", catfile
($smdir, "smdsa2.pem"),
142 "-out", "{output}.cms" ],
143 [ "{cmd2}", @prov, "-verify", "-in", "{output}.cms", "-inform", "DER",
144 "-CAfile", $smroot, "-out", "{output}.txt" ],
148 [ "signed content test streaming BER format, 2 DSA and 2 RSA keys, no attributes",
149 [ "{cmd1}", @prov, "-sign", "-in", $smcont, "-outform", "DER",
150 "-noattr", "-nodetach", "-stream",
152 "-signer", catfile
($smdir, "smrsa2.pem"),
153 "-signer", catfile
($smdir, "smdsa1.pem"),
154 "-signer", catfile
($smdir, "smdsa2.pem"),
155 "-out", "{output}.cms" ],
156 [ "{cmd2}", @prov, "-verify", "-in", "{output}.cms", "-inform", "DER",
157 "-CAfile", $smroot, "-out", "{output}.txt" ],
161 [ "signed content S/MIME format, RSA key SHA1",
162 [ "{cmd1}", @defaultprov, "-sign", "-in", $smcont, "-md", "sha1",
163 "-certfile", $smroot,
164 "-signer", $smrsa1, "-out", "{output}.cms" ],
165 [ "{cmd2}", @prov, "-verify", "-in", "{output}.cms",
166 "-CAfile", $smroot, "-out", "{output}.txt" ],
170 [ "signed zero-length content S/MIME format, RSA key SHA1",
171 [ "{cmd1}", @defaultprov, "-sign", "-in", $smcont_zero, "-md", "sha1",
172 "-certfile", $smroot, "-signer", $smrsa1, "-out", "{output}.cms" ],
173 [ "{cmd2}", @prov, "-verify", "-in", "{output}.cms",
174 "-CAfile", $smroot, "-out", "{output}.txt" ],
178 [ "signed content test streaming S/MIME format, 2 DSA and 2 RSA keys",
179 [ "{cmd1}", @prov, "-sign", "-in", $smcont, "-nodetach",
181 "-signer", catfile
($smdir, "smrsa2.pem"),
182 "-signer", catfile
($smdir, "smdsa1.pem"),
183 "-signer", catfile
($smdir, "smdsa2.pem"),
184 "-stream", "-out", "{output}.cms" ],
185 [ "{cmd2}", @prov, "-verify", "-in", "{output}.cms",
186 "-CAfile", $smroot, "-out", "{output}.txt" ],
190 [ "signed content test streaming multipart S/MIME format, 2 DSA and 2 RSA keys",
191 [ "{cmd1}", @prov, "-sign", "-in", $smcont,
193 "-signer", catfile
($smdir, "smrsa2.pem"),
194 "-signer", catfile
($smdir, "smdsa1.pem"),
195 "-signer", catfile
($smdir, "smdsa2.pem"),
196 "-stream", "-out", "{output}.cms" ],
197 [ "{cmd2}", @prov, "-verify", "-in", "{output}.cms",
198 "-CAfile", $smroot, "-out", "{output}.txt" ],
202 [ "enveloped content test streaming S/MIME format, DES, 3 recipients",
203 [ "{cmd1}", @defaultprov, "-encrypt", "-in", $smcont,
204 "-stream", "-out", "{output}.cms",
206 catfile
($smdir, "smrsa2.pem"),
207 catfile
($smdir, "smrsa3.pem") ],
208 [ "{cmd2}", @defaultprov, "-decrypt", "-recip", $smrsa1,
209 "-in", "{output}.cms", "-out", "{output}.txt" ],
213 [ "enveloped content test streaming S/MIME format, DES, 3 recipients, 3rd used",
214 [ "{cmd1}", @defaultprov, "-encrypt", "-in", $smcont,
215 "-stream", "-out", "{output}.cms",
217 catfile
($smdir, "smrsa2.pem"),
218 catfile
($smdir, "smrsa3.pem") ],
219 [ "{cmd2}", @defaultprov, "-decrypt", "-recip", catfile
($smdir, "smrsa3.pem"),
220 "-in", "{output}.cms", "-out", "{output}.txt" ],
224 [ "enveloped content test streaming S/MIME format, DES, 3 recipients, key only used",
225 [ "{cmd1}", @defaultprov, "-encrypt", "-in", $smcont,
226 "-stream", "-out", "{output}.cms",
228 catfile
($smdir, "smrsa2.pem"),
229 catfile
($smdir, "smrsa3.pem") ],
230 [ "{cmd2}", @defaultprov, "-decrypt", "-inkey", catfile
($smdir, "smrsa3.pem"),
231 "-in", "{output}.cms", "-out", "{output}.txt" ],
235 [ "enveloped content test streaming S/MIME format, AES-256 cipher, 3 recipients",
236 [ "{cmd1}", @prov, "-encrypt", "-in", $smcont,
237 "-aes256", "-stream", "-out", "{output}.cms",
239 catfile
($smdir, "smrsa2.pem"),
240 catfile
($smdir, "smrsa3.pem") ],
241 [ "{cmd2}", @prov, "-decrypt", "-recip", $smrsa1,
242 "-in", "{output}.cms", "-out", "{output}.txt" ],
248 my @smime_cms_tests = (
250 [ "signed content test streaming BER format, 2 DSA and 2 RSA keys, keyid",
251 [ "{cmd1}", @prov, "-sign", "-in", $smcont, "-outform", "DER",
252 "-nodetach", "-keyid",
254 "-signer", catfile
($smdir, "smrsa2.pem"),
255 "-signer", catfile
($smdir, "smdsa1.pem"),
256 "-signer", catfile
($smdir, "smdsa2.pem"),
257 "-stream", "-out", "{output}.cms" ],
258 [ "{cmd2}", @prov, "-verify", "-in", "{output}.cms", "-inform", "DER",
259 "-CAfile", $smroot, "-out", "{output}.txt" ],
263 [ "signed content test streaming PEM format, 2 DSA and 2 RSA keys",
264 [ "{cmd1}", @prov, "-sign", "-in", $smcont, "-outform", "PEM", "-nodetach",
266 "-signer", catfile
($smdir, "smrsa2.pem"),
267 "-signer", catfile
($smdir, "smdsa1.pem"),
268 "-signer", catfile
($smdir, "smdsa2.pem"),
269 "-stream", "-out", "{output}.cms" ],
270 [ "{cmd2}", @prov, "-verify", "-in", "{output}.cms", "-inform", "PEM",
271 "-CAfile", $smroot, "-out", "{output}.txt" ],
275 [ "signed content MIME format, RSA key, signed receipt request",
276 [ "{cmd1}", @prov, "-sign", "-in", $smcont, "-nodetach",
278 "-receipt_request_to", "test\@openssl.org", "-receipt_request_all",
279 "-out", "{output}.cms" ],
280 [ "{cmd2}", @prov, "-verify", "-in", "{output}.cms",
281 "-CAfile", $smroot, "-out", "{output}.txt" ],
285 [ "signed receipt MIME format, RSA key",
286 [ "{cmd1}", @prov, "-sign", "-in", $smcont, "-nodetach",
288 "-receipt_request_to", "test\@openssl.org", "-receipt_request_all",
289 "-out", "{output}.cms" ],
290 [ "{cmd1}", @prov, "-sign_receipt", "-in", "{output}.cms",
291 "-signer", catfile
($smdir, "smrsa2.pem"), "-out", "{output}2.cms" ],
292 [ "{cmd2}", @prov, "-verify_receipt", "{output}2.cms", "-in", "{output}.cms",
296 [ "enveloped content test streaming S/MIME format, DES, 3 recipients, keyid",
297 [ "{cmd1}", @defaultprov, "-encrypt", "-in", $smcont,
298 "-stream", "-out", "{output}.cms", "-keyid",
300 catfile
($smdir, "smrsa2.pem"),
301 catfile
($smdir, "smrsa3.pem") ],
302 [ "{cmd2}", @defaultprov, "-decrypt", "-recip", $smrsa1,
303 "-in", "{output}.cms", "-out", "{output}.txt" ],
307 [ "enveloped content test streaming PEM format, AES-256-CBC cipher, KEK",
308 [ "{cmd1}", @prov, "-encrypt", "-in", $smcont, "-outform", "PEM", "-aes128",
309 "-stream", "-out", "{output}.cms",
310 "-secretkey", "000102030405060708090A0B0C0D0E0F",
311 "-secretkeyid", "C0FEE0" ],
312 [ "{cmd2}", @prov, "-decrypt", "-in", "{output}.cms", "-out", "{output}.txt",
314 "-secretkey", "000102030405060708090A0B0C0D0E0F",
315 "-secretkeyid", "C0FEE0" ],
319 [ "enveloped content test streaming PEM format, AES-256-GCM cipher, KEK",
320 [ "{cmd1}", @prov, "-encrypt", "-in", $smcont, "-outform", "PEM", "-aes-128-gcm",
321 "-stream", "-out", "{output}.cms",
322 "-secretkey", "000102030405060708090A0B0C0D0E0F",
323 "-secretkeyid", "C0FEE0" ],
324 [ "{cmd2}", "-decrypt", "-in", "{output}.cms", "-out", "{output}.txt",
326 "-secretkey", "000102030405060708090A0B0C0D0E0F",
327 "-secretkeyid", "C0FEE0" ],
331 [ "enveloped content test streaming PEM format, KEK, key only",
332 [ "{cmd1}", @prov, "-encrypt", "-in", $smcont, "-outform", "PEM", "-aes128",
333 "-stream", "-out", "{output}.cms",
334 "-secretkey", "000102030405060708090A0B0C0D0E0F",
335 "-secretkeyid", "C0FEE0" ],
336 [ "{cmd2}", @prov, "-decrypt", "-in", "{output}.cms", "-out", "{output}.txt",
338 "-secretkey", "000102030405060708090A0B0C0D0E0F" ],
342 [ "data content test streaming PEM format",
343 [ "{cmd1}", @prov, "-data_create", "-in", $smcont, "-outform", "PEM",
344 "-nodetach", "-stream", "-out", "{output}.cms" ],
345 [ "{cmd2}", @prov, "-data_out", "-in", "{output}.cms", "-inform", "PEM",
346 "-out", "{output}.txt" ],
350 [ "encrypted content test streaming PEM format, 128 bit RC2 key",
351 [ "{cmd1}", @legacyprov, "-EncryptedData_encrypt",
352 "-in", $smcont, "-outform", "PEM",
353 "-rc2", "-secretkey", "000102030405060708090A0B0C0D0E0F",
354 "-stream", "-out", "{output}.cms" ],
355 [ "{cmd2}", @legacyprov, "-EncryptedData_decrypt", "-in", "{output}.cms",
357 "-secretkey", "000102030405060708090A0B0C0D0E0F",
358 "-out", "{output}.txt" ],
362 [ "encrypted content test streaming PEM format, 40 bit RC2 key",
363 [ "{cmd1}", @legacyprov, "-EncryptedData_encrypt",
364 "-in", $smcont, "-outform", "PEM",
365 "-rc2", "-secretkey", "0001020304",
366 "-stream", "-out", "{output}.cms" ],
367 [ "{cmd2}", @legacyprov, "-EncryptedData_decrypt", "-in", "{output}.cms",
369 "-secretkey", "0001020304", "-out", "{output}.txt" ],
373 [ "encrypted content test streaming PEM format, triple DES key",
374 [ "{cmd1}", @prov, "-EncryptedData_encrypt", "-in", $smcont, "-outform", "PEM",
375 "-des3", "-secretkey", "000102030405060708090A0B0C0D0E0F1011121314151617",
376 "-stream", "-out", "{output}.cms" ],
377 [ "{cmd2}", @prov, "-EncryptedData_decrypt", "-in", "{output}.cms",
379 "-secretkey", "000102030405060708090A0B0C0D0E0F1011121314151617",
380 "-out", "{output}.txt" ],
384 [ "encrypted content test streaming PEM format, 128 bit AES key",
385 [ "{cmd1}", @prov, "-EncryptedData_encrypt", "-in", $smcont, "-outform", "PEM",
386 "-aes128", "-secretkey", "000102030405060708090A0B0C0D0E0F",
387 "-stream", "-out", "{output}.cms" ],
388 [ "{cmd2}", @prov, "-EncryptedData_decrypt", "-in", "{output}.cms",
390 "-secretkey", "000102030405060708090A0B0C0D0E0F",
391 "-out", "{output}.txt" ],
396 my @smime_cms_cades_tests = (
398 [ "signed content DER format, RSA key, CAdES-BES compatible",
399 [ "{cmd1}", @prov, "-sign", "-cades", "-in", $smcont, "-outform", "DER",
401 "-certfile", $smroot, "-signer", $smrsa1, "-out", "{output}.cms" ],
402 [ "{cmd2}", @prov, "-verify", "-cades", "-in", "{output}.cms", "-inform", "DER",
403 "-CAfile", $smroot, "-out", "{output}.txt" ],
407 [ "signed content DER format, RSA key, SHA256 md, CAdES-BES compatible",
408 [ "{cmd1}", @prov, "-sign", "-cades", "-md", "sha256", "-in", $smcont, "-outform",
409 "DER", "-nodetach", "-certfile", $smroot,
410 "-signer", $smrsa1, "-out", "{output}.cms" ],
411 [ "{cmd2}", @prov, "-verify", "-cades", "-in", "{output}.cms", "-inform", "DER",
412 "-CAfile", $smroot, "-out", "{output}.txt" ],
416 [ "signed content DER format, RSA key, SHA512 md, CAdES-BES compatible",
417 [ "{cmd1}", @prov, "-sign", "-cades", "-md", "sha512", "-in", $smcont, "-outform",
418 "DER", "-nodetach", "-certfile", $smroot,
419 "-signer", $smrsa1, "-out", "{output}.cms" ],
420 [ "{cmd2}", @prov, "-verify", "-cades", "-in", "{output}.cms", "-inform", "DER",
421 "-CAfile", $smroot, "-out", "{output}.txt" ],
425 [ "signed content DER format, RSA key, SHA256 md, CAdES-BES compatible",
426 [ "{cmd1}", @prov, "-sign", "-cades", "-binary", "-nodetach", "-nosmimecap", "-md", "sha256",
427 "-in", $smcont, "-outform", "DER",
428 "-certfile", $smroot, "-signer", $smrsa1,
429 "-outform", "DER", "-out", "{output}.cms" ],
430 [ "{cmd2}", @prov, "-verify", "-cades", "-in", "{output}.cms", "-inform", "DER",
431 "-CAfile", $smroot, "-out", "{output}.txt" ],
435 [ "resigned content DER format, RSA key, SHA256 md, CAdES-BES compatible",
436 [ "{cmd1}", @prov, "-sign", "-cades", "-binary", "-nodetach", "-nosmimecap", "-md", "sha256",
437 "-in", $smcont, "-outform", "DER",
438 "-certfile", $smroot, "-signer", $smrsa1,
439 "-outform", "DER", "-out", "{output}.cms" ],
440 [ "{cmd1}", @prov, "-resign", "-cades", "-binary", "-nodetach", "-nosmimecap", "-md", "sha256",
441 "-inform", "DER", "-in", "{output}.cms",
442 "-certfile", $smroot, "-signer", catfile
($smdir, "smrsa2.pem"),
443 "-outform", "DER", "-out", "{output}2.cms" ],
445 [ "{cmd2}", @prov, "-verify", "-cades", "-in", "{output}2.cms", "-inform", "DER",
446 "-CAfile", $smroot, "-out", "{output}.txt" ],
451 my @smime_cms_cades_ko_tests = (
452 [ "sign content DER format, RSA key, not CAdES-BES compatible",
453 [ @prov, "-sign", "-in", $smcont, "-outform", "DER", "-nodetach",
454 "-certfile", $smroot, "-signer", $smrsa1, "-out", "{output}.cms" ],
455 "fail to verify token since requiring CAdES-BES compatibility",
456 [ @prov, "-verify", "-cades", "-in", "{output}.cms", "-inform", "DER",
457 "-CAfile", $smroot, "-out", "{output}.txt" ],
462 # cades options test - check that some combinations are rejected
463 my @smime_cms_cades_invalid_option_tests = (
465 [ "-cades", "-noattr" ],
467 [ "-verify", "-cades", "-noattr" ],
469 [ "-verify", "-cades", "-noverify" ],
473 my @smime_cms_comp_tests = (
475 [ "compressed content test streaming PEM format",
476 [ "{cmd1}", @prov, "-compress", "-in", $smcont, "-outform", "PEM", "-nodetach",
477 "-stream", "-out", "{output}.cms" ],
478 [ "{cmd2}", @prov, "-uncompress", "-in", "{output}.cms", "-inform", "PEM",
479 "-out", "{output}.txt" ],
485 my @smime_cms_param_tests = (
486 [ "signed content test streaming PEM format, RSA keys, PSS signature",
487 [ "{cmd1}", @prov, "-sign", "-in", $smcont, "-outform", "PEM", "-nodetach",
489 "-keyopt", "rsa_padding_mode:pss",
490 "-out", "{output}.cms" ],
491 [ "{cmd2}", @prov, "-verify", "-in", "{output}.cms", "-inform", "PEM",
492 "-CAfile", $smroot, "-out", "{output}.txt" ],
496 [ "signed content test streaming PEM format, RSA keys, PSS signature, saltlen=max",
497 [ "{cmd1}", @prov, "-sign", "-in", $smcont, "-outform", "PEM", "-nodetach",
499 "-keyopt", "rsa_padding_mode:pss", "-keyopt", "rsa_pss_saltlen:max",
500 "-out", "{output}.cms" ],
501 [ "{cmd2}", @prov, "-verify", "-in", "{output}.cms", "-inform", "PEM",
502 "-CAfile", $smroot, "-out", "{output}.txt" ],
506 [ "signed content test streaming PEM format, RSA keys, PSS signature, no attributes",
507 [ "{cmd1}", @prov, "-sign", "-in", $smcont, "-outform", "PEM", "-nodetach",
508 "-noattr", "-signer", $smrsa1,
509 "-keyopt", "rsa_padding_mode:pss",
510 "-out", "{output}.cms" ],
511 [ "{cmd2}", @prov, "-verify", "-in", "{output}.cms", "-inform", "PEM",
512 "-CAfile", $smroot, "-out", "{output}.txt" ],
516 [ "signed content test streaming PEM format, RSA keys, PSS signature, SHA384 MGF1",
517 [ "{cmd1}", @prov, "-sign", "-in", $smcont, "-outform", "PEM", "-nodetach",
519 "-keyopt", "rsa_padding_mode:pss", "-keyopt", "rsa_mgf1_md:sha384",
520 "-out", "{output}.cms" ],
521 [ "{cmd2}", @prov, "-verify", "-in", "{output}.cms", "-inform", "PEM",
522 "-CAfile", $smroot, "-out", "{output}.txt" ],
526 [ "enveloped content test streaming S/MIME format, DES, OAEP default parameters",
527 [ "{cmd1}", @defaultprov, "-encrypt", "-in", $smcont,
528 "-stream", "-out", "{output}.cms",
530 "-keyopt", "rsa_padding_mode:oaep" ],
531 [ "{cmd2}", @defaultprov, "-decrypt", "-recip", $smrsa1,
532 "-in", "{output}.cms", "-out", "{output}.txt" ],
536 [ "enveloped content test streaming S/MIME format, DES, OAEP SHA256",
537 [ "{cmd1}", @defaultprov, "-encrypt", "-in", $smcont,
538 "-stream", "-out", "{output}.cms",
540 "-keyopt", "rsa_padding_mode:oaep",
541 "-keyopt", "rsa_oaep_md:sha256" ],
542 [ "{cmd2}", @defaultprov, "-decrypt", "-recip", $smrsa1,
543 "-in", "{output}.cms", "-out", "{output}.txt" ],
547 [ "enveloped content test streaming S/MIME format, DES, ECDH",
548 [ "{cmd1}", @defaultprov, "-encrypt", "-in", $smcont,
549 "-stream", "-out", "{output}.cms",
550 "-recip", catfile
($smdir, "smec1.pem") ],
551 [ "{cmd2}", @defaultprov, "-decrypt", "-recip", catfile
($smdir, "smec1.pem"),
552 "-in", "{output}.cms", "-out", "{output}.txt" ],
556 [ "enveloped content test streaming S/MIME format, DES, ECDH, 2 recipients, key only used",
557 [ "{cmd1}", @defaultprov, "-encrypt", "-in", $smcont,
558 "-stream", "-out", "{output}.cms",
559 catfile
($smdir, "smec1.pem"),
560 catfile
($smdir, "smec3.pem") ],
561 [ "{cmd2}", @defaultprov, "-decrypt", "-inkey", catfile
($smdir, "smec3.pem"),
562 "-in", "{output}.cms", "-out", "{output}.txt" ],
566 [ "enveloped content test streaming S/MIME format, ECDH, DES, key identifier",
567 [ "{cmd1}", @defaultprov, "-encrypt", "-keyid", "-in", $smcont,
568 "-stream", "-out", "{output}.cms",
569 "-recip", catfile
($smdir, "smec1.pem") ],
570 [ "{cmd2}", @defaultprov, "-decrypt", "-recip", catfile
($smdir, "smec1.pem"),
571 "-in", "{output}.cms", "-out", "{output}.txt" ],
575 [ "enveloped content test streaming S/MIME format, ECDH, AES-128-CBC, SHA256 KDF",
576 [ "{cmd1}", @prov, "-encrypt", "-in", $smcont,
577 "-stream", "-out", "{output}.cms",
578 "-recip", catfile
($smdir, "smec1.pem"), "-aes128",
579 "-keyopt", "ecdh_kdf_md:sha256" ],
580 [ "{cmd2}", @prov, "-decrypt", "-recip", catfile
($smdir, "smec1.pem"),
581 "-in", "{output}.cms", "-out", "{output}.txt" ],
585 [ "enveloped content test streaming S/MIME format, ECDH, AES-128-GCM cipher, SHA256 KDF",
586 [ "{cmd1}", @prov, "-encrypt", "-in", $smcont,
587 "-stream", "-out", "{output}.cms",
588 "-recip", catfile
($smdir, "smec1.pem"), "-aes-128-gcm", "-keyopt", "ecdh_kdf_md:sha256" ],
589 [ "{cmd2}", "-decrypt", "-recip", catfile
($smdir, "smec1.pem"),
590 "-in", "{output}.cms", "-out", "{output}.txt" ],
594 [ "enveloped content test streaming S/MIME format, ECDH, K-283, cofactor DH",
595 [ "{cmd1}", @prov, "-encrypt", "-in", $smcont,
596 "-stream", "-out", "{output}.cms",
597 "-recip", catfile
($smdir, "smec2.pem"), "-aes128",
598 "-keyopt", "ecdh_kdf_md:sha256", "-keyopt", "ecdh_cofactor_mode:1" ],
599 [ "{cmd2}", @prov, "-decrypt", "-recip", catfile
($smdir, "smec2.pem"),
600 "-in", "{output}.cms", "-out", "{output}.txt" ],
604 [ "enveloped content test streaming S/MIME format, X9.42 DH",
605 [ "{cmd1}", @prov, "-encrypt", "-in", $smcont,
606 "-stream", "-out", "{output}.cms",
607 "-recip", catfile
($smdir, "smdh.pem"), "-aes128" ],
608 [ "{cmd2}", @prov, "-decrypt", "-recip", catfile
($smdir, "smdh.pem"),
609 "-in", "{output}.cms", "-out", "{output}.txt" ],
614 my @contenttype_cms_test = (
615 [ "signed content test - check that content type is added to additional signerinfo, RSA keys",
616 [ "{cmd1}", @prov, "-sign", "-binary", "-nodetach", "-stream", "-in", $smcont,
617 "-outform", "DER", "-signer", $smrsa1, "-md", "SHA256",
618 "-out", "{output}.cms" ],
619 [ "{cmd1}", @prov, "-resign", "-binary", "-nodetach", "-in", "{output}.cms",
620 "-inform", "DER", "-outform", "DER",
621 "-signer", catfile
($smdir, "smrsa2.pem"), "-md", "SHA256",
622 "-out", "{output}2.cms" ],
623 sub { my %opts = @_; contentType_matches
("$opts{output}2.cms") == 2; },
624 [ "{cmd2}", @prov, "-verify", "-in", "{output}2.cms", "-inform", "DER",
625 "-CAfile", $smroot, "-out", "{output}.txt" ]
629 my @incorrect_attribute_cms_test = (
630 "bad_signtime_attr.cms",
633 "ct_multiple_attr.cms"
636 # Runs a standard loop on the input array
641 foreach (@
{$opts{tests
}}) {
643 $opts{output
} = "$opts{prefix}-$cnt1";
645 my $skip_reason = check_availability
($$_[0]);
646 skip
$skip_reason, 1 if $skip_reason;
648 1 while unlink "$opts{output}.txt";
650 foreach (@
$_[1..$#$_]) {
651 if (ref $_ eq 'CODE') {
656 while ($x =~ /\{([^\}]+)\}/) {
657 $x = $`.$opts{$1}.$' if exists $opts{$1};
662 diag "CMD: openssl ", join(" ", @cmd);
663 $ok &&= run(app(["openssl", @cmd]));
664 $opts{input} = $opts{output};
676 diag "Comparing $smcont with $opts{output}.txt";
677 return compare_text($smcont, "$opts{output}.txt") == 0;
683 diag "Checking for zero-length file";
684 return (-e "$opts{output}.txt" && -z "$opts{output}.txt");
687 subtest "CMS => PKCS#7 compatibility tests\n" => sub {
688 plan tests => scalar @smime_pkcs7_tests;
690 runner_loop(prefix => 'cms2pkcs7', cmd1 => 'cms', cmd2 => 'smime',
691 tests => [ @smime_pkcs7_tests ]);
693 subtest "CMS <= PKCS#7 compatibility tests\n" => sub {
694 plan tests => scalar @smime_pkcs7_tests;
696 runner_loop(prefix => 'pkcs72cms', cmd1 => 'smime', cmd2 => 'cms',
697 tests => [ @smime_pkcs7_tests ]);
700 subtest "CMS <=> CMS consistency tests\n" => sub {
701 plan tests => (scalar @smime_pkcs7_tests) + (scalar @smime_cms_tests);
703 runner_loop(prefix => 'cms2cms-1', cmd1 => 'cms', cmd2 => 'cms',
704 tests => [ @smime_pkcs7_tests ]);
705 runner_loop(prefix => 'cms2cms-2', cmd1 => 'cms', cmd2 => 'cms',
706 tests => [ @smime_cms_tests ]);
709 subtest "CMS <=> CMS consistency tests, modified key parameters\n" => sub {
711 (scalar @smime_cms_param_tests) + (scalar @smime_cms_comp_tests);
713 runner_loop(prefix => 'cms2cms-mod', cmd1 => 'cms', cmd2 => 'cms',
714 tests => [ @smime_cms_param_tests ]);
716 skip("Zlib not supported: compression tests skipped",
717 scalar @smime_cms_comp_tests)
720 runner_loop(prefix => 'cms2cms-comp', cmd1 => 'cms', cmd2 => 'cms',
721 tests => [ @smime_cms_comp_tests ]);
725 # Returns the number of matches of a Content Type Attribute in a binary file.
726 sub contentType_matches {
727 # Read in a binary file
729 open (HEX_IN, "$in") or die("open failed for $in : $!");
734 # Find ASN1 data for a Content Type Attribute (with a OID of PKCS7 data)
735 my @c = $str =~ /\x30\x18\x06\x09\x2A\x86\x48\x86\xF7\x0D\x01\x09\x03\x31\x0B\x06\x09\x2A\x86\x48\x86\xF7\x0D\x01\x07\x01/gs;
741 subtest "CMS Check the content type attribute is added for additional signers\n" => sub {
742 plan tests => (scalar @contenttype_cms_test);
744 runner_loop(prefix => 'cms2cms-added', cmd1 => 'cms', cmd2 => 'cms',
745 tests => [ @contenttype_cms_test ]);
748 subtest "CMS Check that bad attributes fail when verifying signers\n" => sub {
750 (scalar @incorrect_attribute_cms_test);
753 foreach my $name (@incorrect_attribute_cms_test) {
754 my $out = "incorrect-$cnt.txt";
756 ok(!run(app(["openssl", "cms", @prov, "-verify", "-in",
757 catfile($datadir, $name), "-inform", "DER", "-CAfile",
758 $smroot, "-out", $out ])),
763 subtest "CMS Decrypt message encrypted with OpenSSL 1.1.1\n" => sub {
767 skip "EC or DES isn't supported in this build", 1
768 if disabled("ec") || disabled("des");
770 my $out = "smtst.txt";
772 ok(run(app(["openssl", "cms", @defaultprov, "-decrypt",
773 "-inkey", catfile($smdir, "smec3.pem"),
774 "-in", catfile($datadir, "ciphertext_from_1_1_1.cms"),
776 && compare_text($smcont, $out) == 0,
777 "Decrypt message from OpenSSL 1.1.1");
781 subtest "CAdES <=> CAdES consistency tests\n" => sub {
782 plan tests => (scalar @smime_cms_cades_tests);
784 runner_loop(prefix => 'cms-cades', cmd1 => 'cms', cmd2 => 'cms',
785 tests => [ @smime_cms_cades_tests ]);
788 subtest "CAdES; cms incompatible arguments tests\n" => sub {
789 plan tests => (scalar @smime_cms_cades_invalid_option_tests);
791 foreach (@smime_cms_cades_invalid_option_tests) {
792 ok(!run(app(["openssl", "cms", @{$$_[0]} ] )));
796 subtest "CAdES ko tests\n" => sub {
797 plan tests => 2 * scalar @smime_cms_cades_ko_tests;
799 foreach (@smime_cms_cades_ko_tests) {
801 my $skip_reason = check_availability($$_[0]);
802 skip $skip_reason, 1 if $skip_reason;
804 ok(run(app(["openssl", "cms", @{$$_[1]}])), $$_[0]);
805 ok(!run(app(["openssl", "cms", @{$$_[3]}])), $$_[2]);
810 subtest "CMS binary input tests\n" => sub {
811 my $input = srctop_file("test", "smcont.bin");
812 my $signed = "smcont.signed";
813 my $verified = "smcont.verified";
817 ok(run(app(["openssl", "cms", "-sign", "-md", "sha256", "-signer", $smrsa1,
818 "-binary", "-in", $input, "-out", $signed])),
819 "sign binary input with -binary");
820 ok(run(app(["openssl", "cms", "-verify", "-CAfile", $smroot,
821 "-binary", "-in", $signed, "-out", $verified])),
822 "verify binary input with -binary");
823 is(compare($input, $verified), 0, "binary input retained with -binary");
825 ok(run(app(["openssl", "cms", "-sign", "-md", "sha256", "-signer", $smrsa1,
826 "-in", $input, "-out", $signed.".nobin"])),
827 "sign binary input without -binary");
828 ok(run(app(["openssl", "cms", "-verify", "-CAfile", $smroot,
829 "-in", $signed.".nobin", "-out", $verified.".nobin"])),
830 "verify binary input without -binary");
831 is(compare($input, $verified.".nobin"), 1, "binary input not retained without -binary");
832 ok(!run(app(["openssl", "cms", "-verify", "-CAfile", $smroot, "-crlfeol",
833 "-binary", "-in", $signed, "-out", $verified.".crlfeol"])),
834 "verify binary input wrong crlfeol");
836 ok(run(app(["openssl", "cms", "-sign", "-md", "sha256", "-signer", $smrsa1,
838 "-binary", "-in", $input, "-out", $signed.".crlf"])),
839 "sign binary input with -binary -crlfeol");
840 ok(run(app(["openssl", "cms", "-verify", "-CAfile", $smroot, "-crlfeol",
841 "-binary", "-in", $signed.".crlf", "-out", $verified.".crlf"])),
842 "verify binary input with -binary -crlfeol");
843 is(compare($input, $verified.".crlf"), 0,
844 "binary input retained with -binary -crlfeol");
845 ok(!run(app(["openssl", "cms", "-verify", "-CAfile", $smroot,
846 "-binary", "-in", $signed.".crlf", "-out", $verified.".crlf2"])),
847 "verify binary input with -binary missing -crlfeol");
850 subtest "CMS signed digest, DER format" => sub {
853 # Pre-computed SHA256 digest of $smcont in hexadecimal form
854 my $digest = "ff236ef61b396355f75a4cc6e1c306d4c309084ae271a9e2ad6888f10a101b32";
856 my $sig_file = "signature.der";
857 ok(run(app(["openssl", "cms", @prov, "-sign", "-digest", $digest,
859 "-certfile", catfile($smdir, "smroot.pem"),
860 "-signer", catfile($smdir, "smrsa1.pem"),
861 "-out", $sig_file])),
862 "CMS sign pre-computed digest, DER format");
864 ok(run(app(["openssl", "cms", @prov, "-verify", "-in", $sig_file,
866 "-CAfile", catfile($smdir, "smroot.pem"),
867 "-content", $smcont])),
868 "Verify CMS signed digest, DER format");
871 subtest "CMS signed digest, S/MIME format" => sub {
874 # Pre-computed SHA256 digest of $smcont in hexadecimal form
875 my $digest = "ff236ef61b396355f75a4cc6e1c306d4c309084ae271a9e2ad6888f10a101b32";
877 my $sig_file = "signature.smime";
878 ok(run(app(["openssl", "cms", @prov, "-sign", "-digest", $digest,
880 "-certfile", catfile($smdir, "smroot.pem"),
881 "-signer", catfile($smdir, "smrsa1.pem"),
882 "-out", $sig_file])),
883 "CMS sign pre-computed digest, S/MIME format");
885 ok(run(app(["openssl", "cms", @prov, "-verify", "-in", $sig_file,
887 "-CAfile", catfile($smdir, "smroot.pem"),
888 "-content", $smcont])),
889 "Verify CMS signed digest, S/MIME format");
892 sub check_availability {
895 return "$tnam: skipped, EC disabled\n"
896 if ($no_ec && $tnam =~ /ECDH/);
897 return "$tnam: skipped, ECDH disabled\n"
898 if ($no_ec && $tnam =~ /ECDH/);
899 return "$tnam: skipped, EC2M disabled\n"
900 if ($no_ec2m && $tnam =~ /K-283/);
901 return "$tnam: skipped, DH disabled\n"
902 if ($no_dh && $tnam =~ /X9\.42/);
903 return "$tnam: skipped, RC2 disabled\n"
904 if ($no_rc2 && $tnam =~ /RC2/);
905 return "$tnam: skipped, DES disabled\n"
906 if ($no_des && $tnam =~ /DES/);
907 return "$tnam: skipped, DSA disabled\n"
908 if ($no_dsa && $tnam =~ / DSA/);