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, 128 bit AES key",
374 [ "{cmd1}", @prov, "-EncryptedData_encrypt", "-in", $smcont, "-outform", "PEM",
375 "-aes128", "-secretkey", "000102030405060708090A0B0C0D0E0F",
376 "-stream", "-out", "{output}.cms" ],
377 [ "{cmd2}", @prov, "-EncryptedData_decrypt", "-in", "{output}.cms",
379 "-secretkey", "000102030405060708090A0B0C0D0E0F",
380 "-out", "{output}.txt" ],
385 # FIPS 140-3: DES is unavailable
386 push @smime_cms_tests, (
387 [ "encrypted content test streaming PEM format, triple DES key",
388 [ "{cmd1}", @prov, "-EncryptedData_encrypt", "-in", $smcont, "-outform", "PEM",
389 "-des3", "-secretkey", "000102030405060708090A0B0C0D0E0F1011121314151617",
390 "-stream", "-out", "{output}.cms" ],
391 [ "{cmd2}", @prov, "-EncryptedData_decrypt", "-in", "{output}.cms",
393 "-secretkey", "000102030405060708090A0B0C0D0E0F1011121314151617",
394 "-out", "{output}.txt" ],
399 my @smime_cms_cades_tests = (
401 [ "signed content DER format, RSA key, CAdES-BES compatible",
402 [ "{cmd1}", @prov, "-sign", "-cades", "-in", $smcont, "-outform", "DER",
404 "-certfile", $smroot, "-signer", $smrsa1, "-out", "{output}.cms" ],
405 [ "{cmd2}", @prov, "-verify", "-cades", "-in", "{output}.cms", "-inform", "DER",
406 "-CAfile", $smroot, "-out", "{output}.txt" ],
410 [ "signed content DER format, RSA key, SHA256 md, CAdES-BES compatible",
411 [ "{cmd1}", @prov, "-sign", "-cades", "-md", "sha256", "-in", $smcont, "-outform",
412 "DER", "-nodetach", "-certfile", $smroot,
413 "-signer", $smrsa1, "-out", "{output}.cms" ],
414 [ "{cmd2}", @prov, "-verify", "-cades", "-in", "{output}.cms", "-inform", "DER",
415 "-CAfile", $smroot, "-out", "{output}.txt" ],
419 [ "signed content DER format, RSA key, SHA512 md, CAdES-BES compatible",
420 [ "{cmd1}", @prov, "-sign", "-cades", "-md", "sha512", "-in", $smcont, "-outform",
421 "DER", "-nodetach", "-certfile", $smroot,
422 "-signer", $smrsa1, "-out", "{output}.cms" ],
423 [ "{cmd2}", @prov, "-verify", "-cades", "-in", "{output}.cms", "-inform", "DER",
424 "-CAfile", $smroot, "-out", "{output}.txt" ],
428 [ "signed content DER format, RSA key, SHA256 md, CAdES-BES compatible",
429 [ "{cmd1}", @prov, "-sign", "-cades", "-binary", "-nodetach", "-nosmimecap", "-md", "sha256",
430 "-in", $smcont, "-outform", "DER",
431 "-certfile", $smroot, "-signer", $smrsa1,
432 "-outform", "DER", "-out", "{output}.cms" ],
433 [ "{cmd2}", @prov, "-verify", "-cades", "-in", "{output}.cms", "-inform", "DER",
434 "-CAfile", $smroot, "-out", "{output}.txt" ],
438 [ "resigned content DER format, RSA key, SHA256 md, CAdES-BES compatible",
439 [ "{cmd1}", @prov, "-sign", "-cades", "-binary", "-nodetach", "-nosmimecap", "-md", "sha256",
440 "-in", $smcont, "-outform", "DER",
441 "-certfile", $smroot, "-signer", $smrsa1,
442 "-outform", "DER", "-out", "{output}.cms" ],
443 [ "{cmd1}", @prov, "-resign", "-cades", "-binary", "-nodetach", "-nosmimecap", "-md", "sha256",
444 "-inform", "DER", "-in", "{output}.cms",
445 "-certfile", $smroot, "-signer", catfile($smdir, "smrsa2.pem"),
446 "-outform", "DER", "-out", "{output}2.cms" ],
448 [ "{cmd2}", @prov, "-verify", "-cades", "-in", "{output}2.cms", "-inform", "DER",
449 "-CAfile", $smroot, "-out", "{output}.txt" ],
454 my @smime_cms_cades_ko_tests = (
455 [ "sign content DER format, RSA key, not CAdES-BES compatible",
456 [ @prov, "-sign", "-in", $smcont, "-outform", "DER", "-nodetach",
457 "-certfile", $smroot, "-signer", $smrsa1, "-out", "{output}.cms" ],
458 "fail to verify token since requiring CAdES-BES compatibility",
459 [ @prov, "-verify", "-cades", "-in", "{output}.cms", "-inform", "DER",
460 "-CAfile", $smroot, "-out", "{output}.txt" ],
465 # cades options test - check that some combinations are rejected
466 my @smime_cms_cades_invalid_option_tests = (
468 [ "-cades", "-noattr" ],
470 [ "-verify", "-cades", "-noattr" ],
472 [ "-verify", "-cades", "-noverify" ],
476 my @smime_cms_comp_tests = (
478 [ "compressed content test streaming PEM format",
479 [ "{cmd1}", @prov, "-compress", "-in", $smcont, "-outform", "PEM", "-nodetach",
480 "-stream", "-out", "{output}.cms" ],
481 [ "{cmd2}", @prov, "-uncompress", "-in", "{output}.cms", "-inform", "PEM",
482 "-out", "{output}.txt" ],
488 my @smime_cms_param_tests = (
489 [ "signed content test streaming PEM format, RSA keys, PSS signature",
490 [ "{cmd1}", @prov, "-sign", "-in", $smcont, "-outform", "PEM", "-nodetach",
492 "-keyopt", "rsa_padding_mode:pss",
493 "-out", "{output}.cms" ],
494 [ "{cmd2}", @prov, "-verify", "-in", "{output}.cms", "-inform", "PEM",
495 "-CAfile", $smroot, "-out", "{output}.txt" ],
499 [ "signed content test streaming PEM format, RSA keys, PSS signature, saltlen=max",
500 [ "{cmd1}", @prov, "-sign", "-in", $smcont, "-outform", "PEM", "-nodetach",
502 "-keyopt", "rsa_padding_mode:pss", "-keyopt", "rsa_pss_saltlen:max",
503 "-out", "{output}.cms" ],
504 [ "{cmd2}", @prov, "-verify", "-in", "{output}.cms", "-inform", "PEM",
505 "-CAfile", $smroot, "-out", "{output}.txt" ],
509 [ "signed content test streaming PEM format, RSA keys, PSS signature, no attributes",
510 [ "{cmd1}", @prov, "-sign", "-in", $smcont, "-outform", "PEM", "-nodetach",
511 "-noattr", "-signer", $smrsa1,
512 "-keyopt", "rsa_padding_mode:pss",
513 "-out", "{output}.cms" ],
514 [ "{cmd2}", @prov, "-verify", "-in", "{output}.cms", "-inform", "PEM",
515 "-CAfile", $smroot, "-out", "{output}.txt" ],
519 [ "signed content test streaming PEM format, RSA keys, PSS signature, SHA384 MGF1",
520 [ "{cmd1}", @prov, "-sign", "-in", $smcont, "-outform", "PEM", "-nodetach",
522 "-keyopt", "rsa_padding_mode:pss", "-keyopt", "rsa_mgf1_md:sha384",
523 "-out", "{output}.cms" ],
524 [ "{cmd2}", @prov, "-verify", "-in", "{output}.cms", "-inform", "PEM",
525 "-CAfile", $smroot, "-out", "{output}.txt" ],
529 [ "enveloped content test streaming S/MIME format, DES, OAEP default parameters",
530 [ "{cmd1}", @defaultprov, "-encrypt", "-in", $smcont,
531 "-stream", "-out", "{output}.cms",
533 "-keyopt", "rsa_padding_mode:oaep" ],
534 [ "{cmd2}", @defaultprov, "-decrypt", "-recip", $smrsa1,
535 "-in", "{output}.cms", "-out", "{output}.txt" ],
539 [ "enveloped content test streaming S/MIME format, DES, OAEP SHA256",
540 [ "{cmd1}", @defaultprov, "-encrypt", "-in", $smcont,
541 "-stream", "-out", "{output}.cms",
543 "-keyopt", "rsa_padding_mode:oaep",
544 "-keyopt", "rsa_oaep_md:sha256" ],
545 [ "{cmd2}", @defaultprov, "-decrypt", "-recip", $smrsa1,
546 "-in", "{output}.cms", "-out", "{output}.txt" ],
550 [ "enveloped content test streaming S/MIME format, DES, ECDH",
551 [ "{cmd1}", @defaultprov, "-encrypt", "-in", $smcont,
552 "-stream", "-out", "{output}.cms",
553 "-recip", catfile($smdir, "smec1.pem") ],
554 [ "{cmd2}", @defaultprov, "-decrypt", "-recip", catfile($smdir, "smec1.pem"),
555 "-in", "{output}.cms", "-out", "{output}.txt" ],
559 [ "enveloped content test streaming S/MIME format, DES, ECDH, 2 recipients, key only used",
560 [ "{cmd1}", @defaultprov, "-encrypt", "-in", $smcont,
561 "-stream", "-out", "{output}.cms",
562 catfile($smdir, "smec1.pem"),
563 catfile($smdir, "smec3.pem") ],
564 [ "{cmd2}", @defaultprov, "-decrypt", "-inkey", catfile($smdir, "smec3.pem"),
565 "-in", "{output}.cms", "-out", "{output}.txt" ],
569 [ "enveloped content test streaming S/MIME format, ECDH, DES, key identifier",
570 [ "{cmd1}", @defaultprov, "-encrypt", "-keyid", "-in", $smcont,
571 "-stream", "-out", "{output}.cms",
572 "-recip", catfile($smdir, "smec1.pem") ],
573 [ "{cmd2}", @defaultprov, "-decrypt", "-recip", catfile($smdir, "smec1.pem"),
574 "-in", "{output}.cms", "-out", "{output}.txt" ],
578 [ "enveloped content test streaming S/MIME format, ECDH, AES-128-CBC, SHA256 KDF",
579 [ "{cmd1}", @prov, "-encrypt", "-in", $smcont,
580 "-stream", "-out", "{output}.cms",
581 "-recip", catfile($smdir, "smec1.pem"), "-aes128",
582 "-keyopt", "ecdh_kdf_md:sha256" ],
583 [ "{cmd2}", @prov, "-decrypt", "-recip", catfile($smdir, "smec1.pem"),
584 "-in", "{output}.cms", "-out", "{output}.txt" ],
588 [ "enveloped content test streaming S/MIME format, ECDH, AES-128-GCM cipher, SHA256 KDF",
589 [ "{cmd1}", @prov, "-encrypt", "-in", $smcont,
590 "-stream", "-out", "{output}.cms",
591 "-recip", catfile($smdir, "smec1.pem"), "-aes-128-gcm", "-keyopt", "ecdh_kdf_md:sha256" ],
592 [ "{cmd2}", "-decrypt", "-recip", catfile($smdir, "smec1.pem"),
593 "-in", "{output}.cms", "-out", "{output}.txt" ],
597 [ "enveloped content test streaming S/MIME format, ECDH, K-283, cofactor DH",
598 [ "{cmd1}", @prov, "-encrypt", "-in", $smcont,
599 "-stream", "-out", "{output}.cms",
600 "-recip", catfile($smdir, "smec2.pem"), "-aes128",
601 "-keyopt", "ecdh_kdf_md:sha256", "-keyopt", "ecdh_cofactor_mode:1" ],
602 [ "{cmd2}", @prov, "-decrypt", "-recip", catfile($smdir, "smec2.pem"),
603 "-in", "{output}.cms", "-out", "{output}.txt" ],
607 [ "enveloped content test streaming S/MIME format, X9.42 DH",
608 [ "{cmd1}", @prov, "-encrypt", "-in", $smcont,
609 "-stream", "-out", "{output}.cms",
610 "-recip", catfile($smdir, "smdh.pem"), "-aes128" ],
611 [ "{cmd2}", @prov, "-decrypt", "-recip", catfile($smdir, "smdh.pem"),
612 "-in", "{output}.cms", "-out", "{output}.txt" ],
617 my @contenttype_cms_test = (
618 [ "signed content test - check that content type is added to additional signerinfo, RSA keys",
619 [ "{cmd1}", @prov, "-sign", "-binary", "-nodetach", "-stream", "-in", $smcont,
620 "-outform", "DER", "-signer", $smrsa1, "-md", "SHA256",
621 "-out", "{output}.cms" ],
622 [ "{cmd1}", @prov, "-resign", "-binary", "-nodetach", "-in", "{output}.cms",
623 "-inform", "DER", "-outform", "DER",
624 "-signer", catfile($smdir, "smrsa2.pem"), "-md", "SHA256",
625 "-out", "{output}2.cms" ],
626 sub { my %opts = @_; contentType_matches("$opts{output}2.cms") == 2; },
627 [ "{cmd2}", @prov, "-verify", "-in", "{output}2.cms", "-inform", "DER",
628 "-CAfile", $smroot, "-out", "{output}.txt" ]
632 my @incorrect_attribute_cms_test = (
633 "bad_signtime_attr.cms",
636 "ct_multiple_attr.cms"
639 # Runs a standard loop on the input array
644 foreach (@{$opts{tests}}) {
646 $opts{output} = "$opts{prefix}-$cnt1";
648 my $skip_reason = check_availability($$_[0]);
649 skip $skip_reason, 1 if $skip_reason;
651 1 while unlink "$opts{output}.txt";
653 foreach (@$_[1..$#$_]) {
654 if (ref $_ eq 'CODE') {
659 while ($x =~ /\{([^\}]+)\}/) {
660 $x = $`.$opts{$1}.$' if exists $opts{$1};
665 diag "CMD: openssl ", join(" ", @cmd);
666 $ok &&= run(app(["openssl", @cmd]));
667 $opts{input} = $opts{output};
679 diag "Comparing $smcont with $opts{output}.txt";
680 return compare_text($smcont, "$opts{output}.txt") == 0;
686 diag "Checking for zero-length file";
687 return (-e "$opts{output}.txt" && -z "$opts{output}.txt");
690 subtest "CMS => PKCS#7 compatibility tests\n" => sub {
691 plan tests => scalar @smime_pkcs7_tests;
693 runner_loop(prefix => 'cms2pkcs7', cmd1 => 'cms', cmd2 => 'smime',
694 tests => [ @smime_pkcs7_tests ]);
696 subtest "CMS <= PKCS#7 compatibility tests\n" => sub {
697 plan tests => scalar @smime_pkcs7_tests;
699 runner_loop(prefix => 'pkcs72cms', cmd1 => 'smime', cmd2 => 'cms',
700 tests => [ @smime_pkcs7_tests ]);
703 subtest "CMS <=> CMS consistency tests\n" => sub {
704 plan tests => (scalar @smime_pkcs7_tests) + (scalar @smime_cms_tests);
706 runner_loop(prefix => 'cms2cms-1', cmd1 => 'cms', cmd2 => 'cms',
707 tests => [ @smime_pkcs7_tests ]);
708 runner_loop(prefix => 'cms2cms-2', cmd1 => 'cms', cmd2 => 'cms',
709 tests => [ @smime_cms_tests ]);
712 subtest "CMS <=> CMS consistency tests, modified key parameters\n" => sub {
714 (scalar @smime_cms_param_tests) + (scalar @smime_cms_comp_tests);
716 runner_loop(prefix => 'cms2cms-mod', cmd1 => 'cms', cmd2 => 'cms',
717 tests => [ @smime_cms_param_tests ]);
719 skip("Zlib not supported: compression tests skipped",
720 scalar @smime_cms_comp_tests)
723 runner_loop(prefix => 'cms2cms-comp', cmd1 => 'cms', cmd2 => 'cms',
724 tests => [ @smime_cms_comp_tests ]);
728 # Returns the number of matches of a Content Type Attribute in a binary file.
729 sub contentType_matches {
730 # Read in a binary file
732 open (HEX_IN, "$in") or die("open failed for $in : $!");
737 # Find ASN1 data for a Content Type Attribute (with a OID of PKCS7 data)
738 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;
744 subtest "CMS Check the content type attribute is added for additional signers\n" => sub {
745 plan tests => (scalar @contenttype_cms_test);
747 runner_loop(prefix => 'cms2cms-added', cmd1 => 'cms', cmd2 => 'cms',
748 tests => [ @contenttype_cms_test ]);
751 subtest "CMS Check that bad attributes fail when verifying signers\n" => sub {
753 (scalar @incorrect_attribute_cms_test);
756 foreach my $name (@incorrect_attribute_cms_test) {
757 my $out = "incorrect-$cnt.txt";
759 ok(!run(app(["openssl", "cms", @prov, "-verify", "-in",
760 catfile($datadir, $name), "-inform", "DER", "-CAfile",
761 $smroot, "-out", $out ])),
766 subtest "CMS Decrypt message encrypted with OpenSSL 1.1.1\n" => sub {
770 skip "EC or DES isn't supported in this build", 1
771 if disabled("ec") || disabled("des");
773 my $out = "smtst.txt";
775 ok(run(app(["openssl", "cms", @defaultprov, "-decrypt",
776 "-inkey", catfile($smdir, "smec3.pem"),
777 "-in", catfile($datadir, "ciphertext_from_1_1_1.cms"),
779 && compare_text($smcont, $out) == 0,
780 "Decrypt message from OpenSSL 1.1.1");
784 subtest "CAdES <=> CAdES consistency tests\n" => sub {
785 plan tests => (scalar @smime_cms_cades_tests);
787 runner_loop(prefix => 'cms-cades', cmd1 => 'cms', cmd2 => 'cms',
788 tests => [ @smime_cms_cades_tests ]);
791 subtest "CAdES; cms incompatible arguments tests\n" => sub {
792 plan tests => (scalar @smime_cms_cades_invalid_option_tests);
794 foreach (@smime_cms_cades_invalid_option_tests) {
795 ok(!run(app(["openssl", "cms", @{$$_[0]} ] )));
799 subtest "CAdES ko tests\n" => sub {
800 plan tests => 2 * scalar @smime_cms_cades_ko_tests;
802 foreach (@smime_cms_cades_ko_tests) {
804 my $skip_reason = check_availability($$_[0]);
805 skip $skip_reason, 1 if $skip_reason;
807 ok(run(app(["openssl", "cms", @{$$_[1]}])), $$_[0]);
808 ok(!run(app(["openssl", "cms", @{$$_[3]}])), $$_[2]);
813 subtest "CMS binary input tests\n" => sub {
814 my $input = srctop_file("test", "smcont.bin");
815 my $signed = "smcont.signed";
816 my $verified = "smcont.verified";
820 ok(run(app(["openssl", "cms", "-sign", "-md", "sha256", "-signer", $smrsa1,
821 "-binary", "-in", $input, "-out", $signed])),
822 "sign binary input with -binary");
823 ok(run(app(["openssl", "cms", "-verify", "-CAfile", $smroot,
824 "-binary", "-in", $signed, "-out", $verified])),
825 "verify binary input with -binary");
826 is(compare($input, $verified), 0, "binary input retained with -binary");
828 ok(run(app(["openssl", "cms", "-sign", "-md", "sha256", "-signer", $smrsa1,
829 "-in", $input, "-out", $signed.".nobin"])),
830 "sign binary input without -binary");
831 ok(run(app(["openssl", "cms", "-verify", "-CAfile", $smroot,
832 "-in", $signed.".nobin", "-out", $verified.".nobin"])),
833 "verify binary input without -binary");
834 is(compare($input, $verified.".nobin"), 1, "binary input not retained without -binary");
835 ok(!run(app(["openssl", "cms", "-verify", "-CAfile", $smroot, "-crlfeol",
836 "-binary", "-in", $signed, "-out", $verified.".crlfeol"])),
837 "verify binary input wrong crlfeol");
839 ok(run(app(["openssl", "cms", "-sign", "-md", "sha256", "-signer", $smrsa1,
841 "-binary", "-in", $input, "-out", $signed.".crlf"])),
842 "sign binary input with -binary -crlfeol");
843 ok(run(app(["openssl", "cms", "-verify", "-CAfile", $smroot, "-crlfeol",
844 "-binary", "-in", $signed.".crlf", "-out", $verified.".crlf"])),
845 "verify binary input with -binary -crlfeol");
846 is(compare($input, $verified.".crlf"), 0,
847 "binary input retained with -binary -crlfeol");
848 ok(!run(app(["openssl", "cms", "-verify", "-CAfile", $smroot,
849 "-binary", "-in", $signed.".crlf", "-out", $verified.".crlf2"])),
850 "verify binary input with -binary missing -crlfeol");
853 subtest "CMS signed digest, DER format" => sub {
856 # Pre-computed SHA256 digest of $smcont in hexadecimal form
857 my $digest = "ff236ef61b396355f75a4cc6e1c306d4c309084ae271a9e2ad6888f10a101b32";
859 my $sig_file = "signature.der";
860 ok(run(app(["openssl", "cms", @prov, "-sign", "-digest", $digest,
862 "-certfile", catfile($smdir, "smroot.pem"),
863 "-signer", catfile($smdir, "smrsa1.pem"),
864 "-out", $sig_file])),
865 "CMS sign pre-computed digest, DER format");
867 ok(run(app(["openssl", "cms", @prov, "-verify", "-in", $sig_file,
869 "-CAfile", catfile($smdir, "smroot.pem"),
870 "-content", $smcont])),
871 "Verify CMS signed digest, DER format");
874 subtest "CMS signed digest, S/MIME format" => sub {
877 # Pre-computed SHA256 digest of $smcont in hexadecimal form
878 my $digest = "ff236ef61b396355f75a4cc6e1c306d4c309084ae271a9e2ad6888f10a101b32";
880 my $sig_file = "signature.smime";
881 ok(run(app(["openssl", "cms", @prov, "-sign", "-digest", $digest,
883 "-certfile", catfile($smdir, "smroot.pem"),
884 "-signer", catfile($smdir, "smrsa1.pem"),
885 "-out", $sig_file])),
886 "CMS sign pre-computed digest, S/MIME format");
888 ok(run(app(["openssl", "cms", @prov, "-verify", "-in", $sig_file,
890 "-CAfile", catfile($smdir, "smroot.pem"),
891 "-content", $smcont])),
892 "Verify CMS signed digest, S/MIME format");
895 subtest "CMS code signing test" => sub {
897 my $sig_file = "signature.p7s";
898 ok(run(app(["openssl", "cms", @prov, "-sign", "-in", $smcont,
899 "-certfile", catfile($smdir, "smroot.pem"),
900 "-signer", catfile($smdir, "smrsa1.pem"),
901 "-out", $sig_file])),
902 "accept perform CMS signature with smime certificate");
904 ok(run(app(["openssl", "cms", @prov, "-verify", "-in", $sig_file,
905 "-CAfile", catfile($smdir, "smroot.pem"),
906 "-content", $smcont])),
907 "accept verify CMS signature with smime certificate");
909 ok(!run(app(["openssl", "cms", @prov, "-verify", "-in", $sig_file,
910 "-CAfile", catfile($smdir, "smroot.pem"),
911 "-purpose", "codesign",
912 "-content", $smcont])),
913 "fail verify CMS signature with smime certificate for purpose code signing");
915 ok(!run(app(["openssl", "cms", @prov, "-verify", "-in", $sig_file,
916 "-CAfile", catfile($smdir, "smroot.pem"),
917 "-purpose", "football",
918 "-content", $smcont])),
919 "fail verify CMS signature with invalid purpose argument");
921 ok(run(app(["openssl", "cms", @prov, "-sign", "-in", $smcont,
922 "-certfile", catfile($smdir, "smroot.pem"),
923 "-signer", catfile($smdir, "csrsa1.pem"),
924 "-out", $sig_file])),
925 "accept perform CMS signature with code signing certificate");
927 ok(run(app(["openssl", "cms", @prov, "-verify", "-in", $sig_file,
928 "-CAfile", catfile($smdir, "smroot.pem"),
929 "-purpose", "codesign",
930 "-content", $smcont])),
931 "accept verify CMS signature with code signing certificate for purpose code signing");
933 ok(!run(app(["openssl", "cms", @prov, "-verify", "-in", $sig_file,
934 "-CAfile", catfile($smdir, "smroot.pem"),
935 "-content", $smcont])),
936 "fail verify CMS signature with code signing certificate for purpose smime_sign");
939 sub check_availability {
942 return "$tnam: skipped, EC disabled\n"
943 if ($no_ec && $tnam =~ /ECDH/);
944 return "$tnam: skipped, ECDH disabled\n"
945 if ($no_ec && $tnam =~ /ECDH/);
946 return "$tnam: skipped, EC2M disabled\n"
947 if ($no_ec2m && $tnam =~ /K-283/);
948 return "$tnam: skipped, DH disabled\n"
949 if ($no_dh && $tnam =~ /X9\.42/);
950 return "$tnam: skipped, RC2 disabled\n"
951 if ($no_rc2 && $tnam =~ /RC2/);
952 return "$tnam: skipped, DES disabled\n"
953 if ($no_des && $tnam =~ /DES/);
954 return "$tnam: skipped, DSA disabled\n"
955 if ($no_dsa && $tnam =~ / DSA/);