2
0

80-test_cms.t 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842
  1. #! /usr/bin/env perl
  2. # Copyright 2015-2020 The OpenSSL Project Authors. All Rights Reserved.
  3. #
  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
  8. use strict;
  9. use warnings;
  10. use POSIX;
  11. use File::Spec::Functions qw/catfile/;
  12. use File::Compare qw/compare_text/;
  13. use OpenSSL::Test qw/:DEFAULT srctop_dir srctop_file bldtop_dir bldtop_file/;
  14. use OpenSSL::Test::Utils;
  15. BEGIN {
  16. setup("test_cms");
  17. }
  18. use lib srctop_dir('Configurations');
  19. use lib bldtop_dir('.');
  20. use platform;
  21. my $no_fips = disabled('fips') || ($ENV{NO_FIPS} // 0);
  22. plan skip_all => "CMS is not supported by this OpenSSL build"
  23. if disabled("cms");
  24. my $provpath = bldtop_dir("providers");
  25. # Some tests require legacy algorithms to be included.
  26. my @legacyprov = ("-provider-path", $provpath,
  27. "-provider", "default",
  28. "-provider", "legacy" );
  29. my @defaultprov = ("-provider-path", $provpath,
  30. "-provider", "default");
  31. my @config = ( );
  32. my $provname = 'default';
  33. my $datadir = srctop_dir("test", "recipes", "80-test_cms_data");
  34. my $smdir = srctop_dir("test", "smime-certs");
  35. my $smcont = srctop_file("test", "smcont.txt");
  36. my $smcont_zero = srctop_file("test", "smcont_zero.txt");
  37. my ($no_des, $no_dh, $no_dsa, $no_ec, $no_ec2m, $no_rc2, $no_zlib)
  38. = disabled qw/des dh dsa ec ec2m rc2 zlib/;
  39. plan tests =>
  40. ($no_fips ? 0 : 1) # FIPS install test
  41. + 10;
  42. unless ($no_fips) {
  43. my $infile = bldtop_file('providers', platform->dso('fips'));
  44. ok(run(app(['openssl', 'fipsinstall',
  45. '-out', bldtop_file('providers', 'fipsmodule.cnf'),
  46. '-module', $infile])),
  47. "fipsinstall");
  48. @config = ( "-config", srctop_file("test", "fips-and-base.cnf") );
  49. $provname = 'fips';
  50. }
  51. $ENV{OPENSSL_TEST_LIBCTX} = "1";
  52. my @prov = ("-provider-path", $provpath,
  53. @config,
  54. "-provider", $provname);
  55. my @smime_pkcs7_tests = (
  56. [ "signed content DER format, RSA key",
  57. [ "{cmd1}", @prov, "-sign", "-in", $smcont, "-outform", "DER", "-nodetach",
  58. "-certfile", catfile($smdir, "smroot.pem"),
  59. "-signer", catfile($smdir, "smrsa1.pem"), "-out", "{output}.cms" ],
  60. [ "{cmd2}", @prov, "-verify", "-in", "{output}.cms", "-inform", "DER",
  61. "-CAfile", catfile($smdir, "smroot.pem"), "-out", "{output}.txt" ],
  62. \&final_compare
  63. ],
  64. [ "signed detached content DER format, RSA key",
  65. [ "{cmd1}", @prov, "-sign", "-in", $smcont, "-outform", "DER",
  66. "-signer", catfile($smdir, "smrsa1.pem"), "-out", "{output}.cms" ],
  67. [ "{cmd2}", @prov, "-verify", "-in", "{output}.cms", "-inform", "DER",
  68. "-CAfile", catfile($smdir, "smroot.pem"), "-out", "{output}.txt",
  69. "-content", $smcont ],
  70. \&final_compare
  71. ],
  72. [ "signed content test streaming BER format, RSA",
  73. [ "{cmd1}", @prov, "-sign", "-in", $smcont, "-outform", "DER", "-nodetach",
  74. "-stream",
  75. "-signer", catfile($smdir, "smrsa1.pem"), "-out", "{output}.cms" ],
  76. [ "{cmd2}", @prov, "-verify", "-in", "{output}.cms", "-inform", "DER",
  77. "-CAfile", catfile($smdir, "smroot.pem"), "-out", "{output}.txt" ],
  78. \&final_compare
  79. ],
  80. [ "signed content DER format, DSA key",
  81. [ "{cmd1}", @prov, "-sign", "-in", $smcont, "-outform", "DER", "-nodetach",
  82. "-signer", catfile($smdir, "smdsa1.pem"), "-out", "{output}.cms" ],
  83. [ "{cmd2}", @prov, "-verify", "-in", "{output}.cms", "-inform", "DER",
  84. "-CAfile", catfile($smdir, "smroot.pem"), "-out", "{output}.txt" ],
  85. \&final_compare
  86. ],
  87. [ "signed detached content DER format, DSA key",
  88. [ "{cmd1}", @prov, "-sign", "-in", $smcont, "-outform", "DER",
  89. "-signer", catfile($smdir, "smdsa1.pem"), "-out", "{output}.cms" ],
  90. [ "{cmd2}", @prov, "-verify", "-in", "{output}.cms", "-inform", "DER",
  91. "-CAfile", catfile($smdir, "smroot.pem"), "-out", "{output}.txt",
  92. "-content", $smcont ],
  93. \&final_compare
  94. ],
  95. [ "signed detached content DER format, add RSA signer (with DSA existing)",
  96. [ "{cmd1}", @prov, "-sign", "-in", $smcont, "-outform", "DER",
  97. "-signer", catfile($smdir, "smdsa1.pem"), "-out", "{output}.cms" ],
  98. [ "{cmd1}", @prov, "-resign", "-in", "{output}.cms", "-inform", "DER", "-outform", "DER",
  99. "-signer", catfile($smdir, "smrsa1.pem"), "-out", "{output}2.cms" ],
  100. [ "{cmd2}", @prov, "-verify", "-in", "{output}2.cms", "-inform", "DER",
  101. "-CAfile", catfile($smdir, "smroot.pem"), "-out", "{output}.txt",
  102. "-content", $smcont ],
  103. \&final_compare
  104. ],
  105. [ "signed content test streaming BER format, DSA key",
  106. [ "{cmd1}", @prov, "-sign", "-in", $smcont, "-outform", "DER",
  107. "-nodetach", "-stream",
  108. "-signer", catfile($smdir, "smdsa1.pem"), "-out", "{output}.cms" ],
  109. [ "{cmd2}", @prov, "-verify", "-in", "{output}.cms", "-inform", "DER",
  110. "-CAfile", catfile($smdir, "smroot.pem"), "-out", "{output}.txt" ],
  111. \&final_compare
  112. ],
  113. [ "signed content test streaming BER format, 2 DSA and 2 RSA keys",
  114. [ "{cmd1}", @prov, "-sign", "-in", $smcont, "-outform", "DER",
  115. "-nodetach", "-stream",
  116. "-signer", catfile($smdir, "smrsa1.pem"),
  117. "-signer", catfile($smdir, "smrsa2.pem"),
  118. "-signer", catfile($smdir, "smdsa1.pem"),
  119. "-signer", catfile($smdir, "smdsa2.pem"),
  120. "-out", "{output}.cms" ],
  121. [ "{cmd2}", @prov, "-verify", "-in", "{output}.cms", "-inform", "DER",
  122. "-CAfile", catfile($smdir, "smroot.pem"), "-out", "{output}.txt" ],
  123. \&final_compare
  124. ],
  125. [ "signed content test streaming BER format, 2 DSA and 2 RSA keys, no attributes",
  126. [ "{cmd1}", @prov, "-sign", "-in", $smcont, "-outform", "DER",
  127. "-noattr", "-nodetach", "-stream",
  128. "-signer", catfile($smdir, "smrsa1.pem"),
  129. "-signer", catfile($smdir, "smrsa2.pem"),
  130. "-signer", catfile($smdir, "smdsa1.pem"),
  131. "-signer", catfile($smdir, "smdsa2.pem"),
  132. "-out", "{output}.cms" ],
  133. [ "{cmd2}", @prov, "-verify", "-in", "{output}.cms", "-inform", "DER",
  134. "-CAfile", catfile($smdir, "smroot.pem"), "-out", "{output}.txt" ],
  135. \&final_compare
  136. ],
  137. [ "signed content S/MIME format, RSA key SHA1",
  138. [ "{cmd1}", @defaultprov, "-sign", "-in", $smcont, "-md", "sha1",
  139. "-certfile", catfile($smdir, "smroot.pem"),
  140. "-signer", catfile($smdir, "smrsa1.pem"), "-out", "{output}.cms" ],
  141. [ "{cmd2}", @prov, "-verify", "-in", "{output}.cms",
  142. "-CAfile", catfile($smdir, "smroot.pem"), "-out", "{output}.txt" ],
  143. \&final_compare
  144. ],
  145. [ "signed zero-length content S/MIME format, RSA key SHA1",
  146. [ "{cmd1}", @defaultprov, "-sign", "-in", $smcont_zero, "-md", "sha1",
  147. "-certfile", catfile($smdir, "smroot.pem"),
  148. "-signer", catfile($smdir, "smrsa1.pem"), "-out", "{output}.cms" ],
  149. [ "{cmd2}", @prov, "-verify", "-in", "{output}.cms",
  150. "-CAfile", catfile($smdir, "smroot.pem"), "-out", "{output}.txt" ],
  151. \&zero_compare
  152. ],
  153. [ "signed content test streaming S/MIME format, 2 DSA and 2 RSA keys",
  154. [ "{cmd1}", @prov, "-sign", "-in", $smcont, "-nodetach",
  155. "-signer", catfile($smdir, "smrsa1.pem"),
  156. "-signer", catfile($smdir, "smrsa2.pem"),
  157. "-signer", catfile($smdir, "smdsa1.pem"),
  158. "-signer", catfile($smdir, "smdsa2.pem"),
  159. "-stream", "-out", "{output}.cms" ],
  160. [ "{cmd2}", @prov, "-verify", "-in", "{output}.cms",
  161. "-CAfile", catfile($smdir, "smroot.pem"), "-out", "{output}.txt" ],
  162. \&final_compare
  163. ],
  164. [ "signed content test streaming multipart S/MIME format, 2 DSA and 2 RSA keys",
  165. [ "{cmd1}", @prov, "-sign", "-in", $smcont,
  166. "-signer", catfile($smdir, "smrsa1.pem"),
  167. "-signer", catfile($smdir, "smrsa2.pem"),
  168. "-signer", catfile($smdir, "smdsa1.pem"),
  169. "-signer", catfile($smdir, "smdsa2.pem"),
  170. "-stream", "-out", "{output}.cms" ],
  171. [ "{cmd2}", @prov, "-verify", "-in", "{output}.cms",
  172. "-CAfile", catfile($smdir, "smroot.pem"), "-out", "{output}.txt" ],
  173. \&final_compare
  174. ],
  175. [ "enveloped content test streaming S/MIME format, DES, 3 recipients",
  176. [ "{cmd1}", @defaultprov, "-encrypt", "-in", $smcont,
  177. "-stream", "-out", "{output}.cms",
  178. catfile($smdir, "smrsa1.pem"),
  179. catfile($smdir, "smrsa2.pem"),
  180. catfile($smdir, "smrsa3.pem") ],
  181. [ "{cmd2}", @defaultprov, "-decrypt", "-recip", catfile($smdir, "smrsa1.pem"),
  182. "-in", "{output}.cms", "-out", "{output}.txt" ],
  183. \&final_compare
  184. ],
  185. [ "enveloped content test streaming S/MIME format, DES, 3 recipients, 3rd used",
  186. [ "{cmd1}", @defaultprov, "-encrypt", "-in", $smcont,
  187. "-stream", "-out", "{output}.cms",
  188. catfile($smdir, "smrsa1.pem"),
  189. catfile($smdir, "smrsa2.pem"),
  190. catfile($smdir, "smrsa3.pem") ],
  191. [ "{cmd2}", @defaultprov, "-decrypt", "-recip", catfile($smdir, "smrsa3.pem"),
  192. "-in", "{output}.cms", "-out", "{output}.txt" ],
  193. \&final_compare
  194. ],
  195. [ "enveloped content test streaming S/MIME format, DES, 3 recipients, key only used",
  196. [ "{cmd1}", @defaultprov, "-encrypt", "-in", $smcont,
  197. "-stream", "-out", "{output}.cms",
  198. catfile($smdir, "smrsa1.pem"),
  199. catfile($smdir, "smrsa2.pem"),
  200. catfile($smdir, "smrsa3.pem") ],
  201. [ "{cmd2}", @defaultprov, "-decrypt", "-inkey", catfile($smdir, "smrsa3.pem"),
  202. "-in", "{output}.cms", "-out", "{output}.txt" ],
  203. \&final_compare
  204. ],
  205. [ "enveloped content test streaming S/MIME format, AES-256 cipher, 3 recipients",
  206. [ "{cmd1}", @prov, "-encrypt", "-in", $smcont,
  207. "-aes256", "-stream", "-out", "{output}.cms",
  208. catfile($smdir, "smrsa1.pem"),
  209. catfile($smdir, "smrsa2.pem"),
  210. catfile($smdir, "smrsa3.pem") ],
  211. [ "{cmd2}", @prov, "-decrypt", "-recip", catfile($smdir, "smrsa1.pem"),
  212. "-in", "{output}.cms", "-out", "{output}.txt" ],
  213. \&final_compare
  214. ],
  215. );
  216. my @smime_cms_tests = (
  217. [ "signed content test streaming BER format, 2 DSA and 2 RSA keys, keyid",
  218. [ "{cmd1}", @prov, "-sign", "-in", $smcont, "-outform", "DER",
  219. "-nodetach", "-keyid",
  220. "-signer", catfile($smdir, "smrsa1.pem"),
  221. "-signer", catfile($smdir, "smrsa2.pem"),
  222. "-signer", catfile($smdir, "smdsa1.pem"),
  223. "-signer", catfile($smdir, "smdsa2.pem"),
  224. "-stream", "-out", "{output}.cms" ],
  225. [ "{cmd2}", @prov, "-verify", "-in", "{output}.cms", "-inform", "DER",
  226. "-CAfile", catfile($smdir, "smroot.pem"), "-out", "{output}.txt" ],
  227. \&final_compare
  228. ],
  229. [ "signed content test streaming PEM format, 2 DSA and 2 RSA keys",
  230. [ "{cmd1}", @prov, "-sign", "-in", $smcont, "-outform", "PEM", "-nodetach",
  231. "-signer", catfile($smdir, "smrsa1.pem"),
  232. "-signer", catfile($smdir, "smrsa2.pem"),
  233. "-signer", catfile($smdir, "smdsa1.pem"),
  234. "-signer", catfile($smdir, "smdsa2.pem"),
  235. "-stream", "-out", "{output}.cms" ],
  236. [ "{cmd2}", @prov, "-verify", "-in", "{output}.cms", "-inform", "PEM",
  237. "-CAfile", catfile($smdir, "smroot.pem"), "-out", "{output}.txt" ],
  238. \&final_compare
  239. ],
  240. [ "signed content MIME format, RSA key, signed receipt request",
  241. [ "{cmd1}", @prov, "-sign", "-in", $smcont, "-nodetach",
  242. "-signer", catfile($smdir, "smrsa1.pem"),
  243. "-receipt_request_to", "test\@openssl.org", "-receipt_request_all",
  244. "-out", "{output}.cms" ],
  245. [ "{cmd2}", @prov, "-verify", "-in", "{output}.cms",
  246. "-CAfile", catfile($smdir, "smroot.pem"), "-out", "{output}.txt" ],
  247. \&final_compare
  248. ],
  249. [ "signed receipt MIME format, RSA key",
  250. [ "{cmd1}", @prov, "-sign", "-in", $smcont, "-nodetach",
  251. "-signer", catfile($smdir, "smrsa1.pem"),
  252. "-receipt_request_to", "test\@openssl.org", "-receipt_request_all",
  253. "-out", "{output}.cms" ],
  254. [ "{cmd1}", @prov, "-sign_receipt", "-in", "{output}.cms",
  255. "-signer", catfile($smdir, "smrsa2.pem"), "-out", "{output}2.cms" ],
  256. [ "{cmd2}", @prov, "-verify_receipt", "{output}2.cms", "-in", "{output}.cms",
  257. "-CAfile", catfile($smdir, "smroot.pem") ]
  258. ],
  259. [ "enveloped content test streaming S/MIME format, DES, 3 recipients, keyid",
  260. [ "{cmd1}", @defaultprov, "-encrypt", "-in", $smcont,
  261. "-stream", "-out", "{output}.cms", "-keyid",
  262. catfile($smdir, "smrsa1.pem"),
  263. catfile($smdir, "smrsa2.pem"),
  264. catfile($smdir, "smrsa3.pem") ],
  265. [ "{cmd2}", @defaultprov, "-decrypt", "-recip", catfile($smdir, "smrsa1.pem"),
  266. "-in", "{output}.cms", "-out", "{output}.txt" ],
  267. \&final_compare
  268. ],
  269. [ "enveloped content test streaming PEM format, AES-256-CBC cipher, KEK",
  270. [ "{cmd1}", @prov, "-encrypt", "-in", $smcont, "-outform", "PEM", "-aes128",
  271. "-stream", "-out", "{output}.cms",
  272. "-secretkey", "000102030405060708090A0B0C0D0E0F",
  273. "-secretkeyid", "C0FEE0" ],
  274. [ "{cmd2}", @prov, "-decrypt", "-in", "{output}.cms", "-out", "{output}.txt",
  275. "-inform", "PEM",
  276. "-secretkey", "000102030405060708090A0B0C0D0E0F",
  277. "-secretkeyid", "C0FEE0" ],
  278. \&final_compare
  279. ],
  280. [ "enveloped content test streaming PEM format, AES-256-GCM cipher, KEK",
  281. [ "{cmd1}", @prov, "-encrypt", "-in", $smcont, "-outform", "PEM", "-aes-128-gcm",
  282. "-stream", "-out", "{output}.cms",
  283. "-secretkey", "000102030405060708090A0B0C0D0E0F",
  284. "-secretkeyid", "C0FEE0" ],
  285. [ "{cmd2}", "-decrypt", "-in", "{output}.cms", "-out", "{output}.txt",
  286. "-inform", "PEM",
  287. "-secretkey", "000102030405060708090A0B0C0D0E0F",
  288. "-secretkeyid", "C0FEE0" ],
  289. \&final_compare
  290. ],
  291. [ "enveloped content test streaming PEM format, KEK, key only",
  292. [ "{cmd1}", @prov, "-encrypt", "-in", $smcont, "-outform", "PEM", "-aes128",
  293. "-stream", "-out", "{output}.cms",
  294. "-secretkey", "000102030405060708090A0B0C0D0E0F",
  295. "-secretkeyid", "C0FEE0" ],
  296. [ "{cmd2}", @prov, "-decrypt", "-in", "{output}.cms", "-out", "{output}.txt",
  297. "-inform", "PEM",
  298. "-secretkey", "000102030405060708090A0B0C0D0E0F" ],
  299. \&final_compare
  300. ],
  301. [ "data content test streaming PEM format",
  302. [ "{cmd1}", @prov, "-data_create", "-in", $smcont, "-outform", "PEM",
  303. "-nodetach", "-stream", "-out", "{output}.cms" ],
  304. [ "{cmd2}", @prov, "-data_out", "-in", "{output}.cms", "-inform", "PEM",
  305. "-out", "{output}.txt" ],
  306. \&final_compare
  307. ],
  308. [ "encrypted content test streaming PEM format, 128 bit RC2 key",
  309. [ "{cmd1}", @legacyprov, "-EncryptedData_encrypt",
  310. "-in", $smcont, "-outform", "PEM",
  311. "-rc2", "-secretkey", "000102030405060708090A0B0C0D0E0F",
  312. "-stream", "-out", "{output}.cms" ],
  313. [ "{cmd2}", @legacyprov, "-EncryptedData_decrypt", "-in", "{output}.cms",
  314. "-inform", "PEM",
  315. "-secretkey", "000102030405060708090A0B0C0D0E0F",
  316. "-out", "{output}.txt" ],
  317. \&final_compare
  318. ],
  319. [ "encrypted content test streaming PEM format, 40 bit RC2 key",
  320. [ "{cmd1}", @legacyprov, "-EncryptedData_encrypt",
  321. "-in", $smcont, "-outform", "PEM",
  322. "-rc2", "-secretkey", "0001020304",
  323. "-stream", "-out", "{output}.cms" ],
  324. [ "{cmd2}", @legacyprov, "-EncryptedData_decrypt", "-in", "{output}.cms",
  325. "-inform", "PEM",
  326. "-secretkey", "0001020304", "-out", "{output}.txt" ],
  327. \&final_compare
  328. ],
  329. [ "encrypted content test streaming PEM format, triple DES key",
  330. [ "{cmd1}", @prov, "-EncryptedData_encrypt", "-in", $smcont, "-outform", "PEM",
  331. "-des3", "-secretkey", "000102030405060708090A0B0C0D0E0F1011121314151617",
  332. "-stream", "-out", "{output}.cms" ],
  333. [ "{cmd2}", @prov, "-EncryptedData_decrypt", "-in", "{output}.cms",
  334. "-inform", "PEM",
  335. "-secretkey", "000102030405060708090A0B0C0D0E0F1011121314151617",
  336. "-out", "{output}.txt" ],
  337. \&final_compare
  338. ],
  339. [ "encrypted content test streaming PEM format, 128 bit AES key",
  340. [ "{cmd1}", @prov, "-EncryptedData_encrypt", "-in", $smcont, "-outform", "PEM",
  341. "-aes128", "-secretkey", "000102030405060708090A0B0C0D0E0F",
  342. "-stream", "-out", "{output}.cms" ],
  343. [ "{cmd2}", @prov, "-EncryptedData_decrypt", "-in", "{output}.cms",
  344. "-inform", "PEM",
  345. "-secretkey", "000102030405060708090A0B0C0D0E0F",
  346. "-out", "{output}.txt" ],
  347. \&final_compare
  348. ],
  349. );
  350. my @smime_cms_cades_tests = (
  351. [ "signed content DER format, RSA key, CAdES-BES compatible",
  352. [ "{cmd1}", @prov, "-sign", "-cades", "-in", $smcont, "-outform", "DER",
  353. "-nodetach",
  354. "-certfile", catfile($smdir, "smroot.pem"),
  355. "-signer", catfile($smdir, "smrsa1.pem"), "-out", "{output}.cms" ],
  356. [ "{cmd2}", @prov, "-verify", "-cades", "-in", "{output}.cms", "-inform", "DER",
  357. "-CAfile", catfile($smdir, "smroot.pem"), "-out", "{output}.txt" ],
  358. \&final_compare
  359. ],
  360. [ "signed content DER format, RSA key, SHA256 md, CAdES-BES compatible",
  361. [ "{cmd1}", @prov, "-sign", "-cades", "-md", "sha256", "-in", $smcont, "-outform",
  362. "DER", "-nodetach", "-certfile", catfile($smdir, "smroot.pem"),
  363. "-signer", catfile($smdir, "smrsa1.pem"), "-out", "{output}.cms" ],
  364. [ "{cmd2}", @prov, "-verify", "-cades", "-in", "{output}.cms", "-inform", "DER",
  365. "-CAfile", catfile($smdir, "smroot.pem"), "-out", "{output}.txt" ],
  366. \&final_compare
  367. ],
  368. [ "signed content DER format, RSA key, SHA512 md, CAdES-BES compatible",
  369. [ "{cmd1}", @prov, "-sign", "-cades", "-md", "sha512", "-in", $smcont, "-outform",
  370. "DER", "-nodetach", "-certfile", catfile($smdir, "smroot.pem"),
  371. "-signer", catfile($smdir, "smrsa1.pem"), "-out", "{output}.cms" ],
  372. [ "{cmd2}", @prov, "-verify", "-cades", "-in", "{output}.cms", "-inform", "DER",
  373. "-CAfile", catfile($smdir, "smroot.pem"), "-out", "{output}.txt" ],
  374. \&final_compare
  375. ],
  376. [ "signed content DER format, RSA key, SHA256 md, CAdES-BES compatible",
  377. [ "{cmd1}", @prov, "-sign", "-cades", "-binary", "-nodetach", "-nosmimecap", "-md", "sha256",
  378. "-in", $smcont, "-outform", "DER",
  379. "-certfile", catfile($smdir, "smroot.pem"),
  380. "-signer", catfile($smdir, "smrsa1.pem"),
  381. "-outform", "DER", "-out", "{output}.cms" ],
  382. [ "{cmd2}", @prov, "-verify", "-cades", "-in", "{output}.cms", "-inform", "DER",
  383. "-CAfile", catfile($smdir, "smroot.pem"), "-out", "{output}.txt" ],
  384. \&final_compare
  385. ],
  386. [ "resigned content DER format, RSA key, SHA256 md, CAdES-BES compatible",
  387. [ "{cmd1}", @prov, "-sign", "-cades", "-binary", "-nodetach", "-nosmimecap", "-md", "sha256",
  388. "-in", $smcont, "-outform", "DER",
  389. "-certfile", catfile($smdir, "smroot.pem"),
  390. "-signer", catfile($smdir, "smrsa1.pem"),
  391. "-outform", "DER", "-out", "{output}.cms" ],
  392. [ "{cmd1}", @prov, "-resign", "-cades", "-binary", "-nodetach", "-nosmimecap", "-md", "sha256",
  393. "-inform", "DER", "-in", "{output}.cms",
  394. "-certfile", catfile($smdir, "smroot.pem"),
  395. "-signer", catfile($smdir, "smrsa2.pem"),
  396. "-outform", "DER", "-out", "{output}2.cms" ],
  397. [ "{cmd2}", @prov, "-verify", "-cades", "-in", "{output}2.cms", "-inform", "DER",
  398. "-CAfile", catfile($smdir, "smroot.pem"), "-out", "{output}.txt" ],
  399. \&final_compare
  400. ],
  401. );
  402. my @smime_cms_cades_ko_tests = (
  403. [ "signed content DER format, RSA key, but verified as CAdES-BES compatible",
  404. [ @prov, "-sign", "-in", $smcont, "-outform", "DER", "-nodetach",
  405. "-certfile", catfile($smdir, "smroot.pem"),
  406. "-signer", catfile($smdir, "smrsa1.pem"), "-out", "{output}.cms" ],
  407. [ @prov, "-verify", "-cades", "-in", "{output}.cms", "-inform", "DER",
  408. "-CAfile", catfile($smdir, "smroot.pem"), "-out", "{output}.txt" ],
  409. \&final_compare
  410. ]
  411. );
  412. # cades options test - check that some combinations are rejected
  413. my @smime_cms_cades_invalid_option_tests = (
  414. [
  415. [ "-cades", "-noattr" ],
  416. ],[
  417. [ "-verify", "-cades", "-noattr" ],
  418. ],[
  419. [ "-verify", "-cades", "-noverify" ],
  420. ],
  421. );
  422. my @smime_cms_comp_tests = (
  423. [ "compressed content test streaming PEM format",
  424. [ "{cmd1}", @prov, "-compress", "-in", $smcont, "-outform", "PEM", "-nodetach",
  425. "-stream", "-out", "{output}.cms" ],
  426. [ "{cmd2}", @prov, "-uncompress", "-in", "{output}.cms", "-inform", "PEM",
  427. "-out", "{output}.txt" ],
  428. \&final_compare
  429. ]
  430. );
  431. my @smime_cms_param_tests = (
  432. [ "signed content test streaming PEM format, RSA keys, PSS signature",
  433. [ "{cmd1}", @prov, "-sign", "-in", $smcont, "-outform", "PEM", "-nodetach",
  434. "-signer", catfile($smdir, "smrsa1.pem"),
  435. "-keyopt", "rsa_padding_mode:pss",
  436. "-out", "{output}.cms" ],
  437. [ "{cmd2}", @prov, "-verify", "-in", "{output}.cms", "-inform", "PEM",
  438. "-CAfile", catfile($smdir, "smroot.pem"), "-out", "{output}.txt" ],
  439. \&final_compare
  440. ],
  441. [ "signed content test streaming PEM format, RSA keys, PSS signature, saltlen=max",
  442. [ "{cmd1}", @prov, "-sign", "-in", $smcont, "-outform", "PEM", "-nodetach",
  443. "-signer", catfile($smdir, "smrsa1.pem"),
  444. "-keyopt", "rsa_padding_mode:pss", "-keyopt", "rsa_pss_saltlen:max",
  445. "-out", "{output}.cms" ],
  446. [ "{cmd2}", @prov, "-verify", "-in", "{output}.cms", "-inform", "PEM",
  447. "-CAfile", catfile($smdir, "smroot.pem"), "-out", "{output}.txt" ],
  448. \&final_compare
  449. ],
  450. [ "signed content test streaming PEM format, RSA keys, PSS signature, no attributes",
  451. [ "{cmd1}", @prov, "-sign", "-in", $smcont, "-outform", "PEM", "-nodetach",
  452. "-noattr",
  453. "-signer", catfile($smdir, "smrsa1.pem"),
  454. "-keyopt", "rsa_padding_mode:pss",
  455. "-out", "{output}.cms" ],
  456. [ "{cmd2}", @prov, "-verify", "-in", "{output}.cms", "-inform", "PEM",
  457. "-CAfile", catfile($smdir, "smroot.pem"), "-out", "{output}.txt" ],
  458. \&final_compare
  459. ],
  460. [ "signed content test streaming PEM format, RSA keys, PSS signature, SHA384 MGF1",
  461. [ "{cmd1}", @prov, "-sign", "-in", $smcont, "-outform", "PEM", "-nodetach",
  462. "-signer", catfile($smdir, "smrsa1.pem"),
  463. "-keyopt", "rsa_padding_mode:pss", "-keyopt", "rsa_mgf1_md:sha384",
  464. "-out", "{output}.cms" ],
  465. [ "{cmd2}", @prov, "-verify", "-in", "{output}.cms", "-inform", "PEM",
  466. "-CAfile", catfile($smdir, "smroot.pem"), "-out", "{output}.txt" ],
  467. \&final_compare
  468. ],
  469. [ "enveloped content test streaming S/MIME format, DES, OAEP default parameters",
  470. [ "{cmd1}", @defaultprov, "-encrypt", "-in", $smcont,
  471. "-stream", "-out", "{output}.cms",
  472. "-recip", catfile($smdir, "smrsa1.pem"),
  473. "-keyopt", "rsa_padding_mode:oaep" ],
  474. [ "{cmd2}", @defaultprov, "-decrypt", "-recip", catfile($smdir, "smrsa1.pem"),
  475. "-in", "{output}.cms", "-out", "{output}.txt" ],
  476. \&final_compare
  477. ],
  478. [ "enveloped content test streaming S/MIME format, DES, OAEP SHA256",
  479. [ "{cmd1}", @defaultprov, "-encrypt", "-in", $smcont,
  480. "-stream", "-out", "{output}.cms",
  481. "-recip", catfile($smdir, "smrsa1.pem"),
  482. "-keyopt", "rsa_padding_mode:oaep",
  483. "-keyopt", "rsa_oaep_md:sha256" ],
  484. [ "{cmd2}", @defaultprov, "-decrypt", "-recip", catfile($smdir, "smrsa1.pem"),
  485. "-in", "{output}.cms", "-out", "{output}.txt" ],
  486. \&final_compare
  487. ],
  488. [ "enveloped content test streaming S/MIME format, DES, ECDH",
  489. [ "{cmd1}", @defaultprov, "-encrypt", "-in", $smcont,
  490. "-stream", "-out", "{output}.cms",
  491. "-recip", catfile($smdir, "smec1.pem") ],
  492. [ "{cmd2}", @defaultprov, "-decrypt", "-recip", catfile($smdir, "smec1.pem"),
  493. "-in", "{output}.cms", "-out", "{output}.txt" ],
  494. \&final_compare
  495. ],
  496. [ "enveloped content test streaming S/MIME format, DES, ECDH, 2 recipients, key only used",
  497. [ "{cmd1}", @defaultprov, "-encrypt", "-in", $smcont,
  498. "-stream", "-out", "{output}.cms",
  499. catfile($smdir, "smec1.pem"),
  500. catfile($smdir, "smec3.pem") ],
  501. [ "{cmd2}", @defaultprov, "-decrypt", "-inkey", catfile($smdir, "smec3.pem"),
  502. "-in", "{output}.cms", "-out", "{output}.txt" ],
  503. \&final_compare
  504. ],
  505. [ "enveloped content test streaming S/MIME format, ECDH, DES, key identifier",
  506. [ "{cmd1}", @defaultprov, "-encrypt", "-keyid", "-in", $smcont,
  507. "-stream", "-out", "{output}.cms",
  508. "-recip", catfile($smdir, "smec1.pem") ],
  509. [ "{cmd2}", @defaultprov, "-decrypt", "-recip", catfile($smdir, "smec1.pem"),
  510. "-in", "{output}.cms", "-out", "{output}.txt" ],
  511. \&final_compare
  512. ],
  513. [ "enveloped content test streaming S/MIME format, ECDH, AES-128-CBC, SHA256 KDF",
  514. [ "{cmd1}", @prov, "-encrypt", "-in", $smcont,
  515. "-stream", "-out", "{output}.cms",
  516. "-recip", catfile($smdir, "smec1.pem"), "-aes128",
  517. "-keyopt", "ecdh_kdf_md:sha256" ],
  518. [ "{cmd2}", @prov, "-decrypt", "-recip", catfile($smdir, "smec1.pem"),
  519. "-in", "{output}.cms", "-out", "{output}.txt" ],
  520. \&final_compare
  521. ],
  522. [ "enveloped content test streaming S/MIME format, ECDH, AES-128-GCM cipher, SHA256 KDF",
  523. [ "{cmd1}", @prov, "-encrypt", "-in", $smcont,
  524. "-stream", "-out", "{output}.cms",
  525. "-recip", catfile($smdir, "smec1.pem"), "-aes-128-gcm", "-keyopt", "ecdh_kdf_md:sha256" ],
  526. [ "{cmd2}", "-decrypt", "-recip", catfile($smdir, "smec1.pem"),
  527. "-in", "{output}.cms", "-out", "{output}.txt" ],
  528. \&final_compare
  529. ],
  530. [ "enveloped content test streaming S/MIME format, ECDH, K-283, cofactor DH",
  531. [ "{cmd1}", @prov, "-encrypt", "-in", $smcont,
  532. "-stream", "-out", "{output}.cms",
  533. "-recip", catfile($smdir, "smec2.pem"), "-aes128",
  534. "-keyopt", "ecdh_kdf_md:sha256", "-keyopt", "ecdh_cofactor_mode:1" ],
  535. [ "{cmd2}", @prov, "-decrypt", "-recip", catfile($smdir, "smec2.pem"),
  536. "-in", "{output}.cms", "-out", "{output}.txt" ],
  537. \&final_compare
  538. ]
  539. # TODO(3.0) Add this test back in when "dhpublicnumber" is supported
  540. # in the keymanger.
  541. #[ "enveloped content test streaming S/MIME format, X9.42 DH",
  542. # [ "{cmd1}", @prov, "-encrypt", "-in", $smcont,
  543. # "-stream", "-out", "{output}.cms",
  544. # "-recip", catfile($smdir, "smdh.pem"), "-aes128" ],
  545. # [ "{cmd2}", "-decrypt", "-recip", catfile($smdir, "smdh.pem"),
  546. # "-in", "{output}.cms", "-out", "{output}.txt" ],
  547. # \&final_compare
  548. #]
  549. );
  550. my @contenttype_cms_test = (
  551. [ "signed content test - check that content type is added to additional signerinfo, RSA keys",
  552. [ "{cmd1}", @prov, "-sign", "-binary", "-nodetach", "-stream", "-in", $smcont,
  553. "-outform", "DER",
  554. "-signer", catfile($smdir, "smrsa1.pem"), "-md", "SHA256",
  555. "-out", "{output}.cms" ],
  556. [ "{cmd1}", @prov, "-resign", "-binary", "-nodetach", "-in", "{output}.cms",
  557. "-inform", "DER", "-outform", "DER",
  558. "-signer", catfile($smdir, "smrsa2.pem"), "-md", "SHA256",
  559. "-out", "{output}2.cms" ],
  560. sub { my %opts = @_; contentType_matches("$opts{output}2.cms") == 2; },
  561. [ "{cmd2}", @prov, "-verify", "-in", "{output}2.cms", "-inform", "DER",
  562. "-CAfile", catfile($smdir, "smroot.pem"), "-out", "{output}.txt" ]
  563. ],
  564. );
  565. my @incorrect_attribute_cms_test = (
  566. "bad_signtime_attr.cms",
  567. "no_ct_attr.cms",
  568. "no_md_attr.cms",
  569. "ct_multiple_attr.cms"
  570. );
  571. # Runs a standard loop on the input array
  572. sub runner_loop {
  573. my %opts = ( @_ );
  574. my $cnt1 = 0;
  575. foreach (@{$opts{tests}}) {
  576. $cnt1++;
  577. $opts{output} = "$opts{prefix}-$cnt1";
  578. SKIP: {
  579. my $skip_reason = check_availability($$_[0]);
  580. skip $skip_reason, 1 if $skip_reason;
  581. my $ok = 1;
  582. 1 while unlink "$opts{output}.txt";
  583. foreach (@$_[1..$#$_]) {
  584. if (ref $_ eq 'CODE') {
  585. $ok &&= $_->(%opts);
  586. } else {
  587. my @cmd = map {
  588. my $x = $_;
  589. while ($x =~ /\{([^\}]+)\}/) {
  590. $x = $`.$opts{$1}.$' if exists $opts{$1};
  591. }
  592. $x;
  593. } @$_;
  594. diag "CMD: openssl ", join(" ", @cmd);
  595. $ok &&= run(app(["openssl", @cmd]));
  596. $opts{input} = $opts{output};
  597. }
  598. }
  599. ok($ok, $$_[0]);
  600. }
  601. }
  602. }
  603. sub final_compare {
  604. my %opts = @_;
  605. diag "Comparing $smcont with $opts{output}.txt";
  606. return compare_text($smcont, "$opts{output}.txt") == 0;
  607. }
  608. sub zero_compare {
  609. my %opts = @_;
  610. diag "Checking for zero-length file";
  611. return (-e "$opts{output}.txt" && -z "$opts{output}.txt");
  612. }
  613. subtest "CMS => PKCS#7 compatibility tests\n" => sub {
  614. plan tests => scalar @smime_pkcs7_tests;
  615. runner_loop(prefix => 'cms2pkcs7', cmd1 => 'cms', cmd2 => 'smime',
  616. tests => [ @smime_pkcs7_tests ]);
  617. };
  618. subtest "CMS <= PKCS#7 compatibility tests\n" => sub {
  619. plan tests => scalar @smime_pkcs7_tests;
  620. runner_loop(prefix => 'pkcs72cms', cmd1 => 'smime', cmd2 => 'cms',
  621. tests => [ @smime_pkcs7_tests ]);
  622. };
  623. subtest "CMS <=> CMS consistency tests\n" => sub {
  624. plan tests => (scalar @smime_pkcs7_tests) + (scalar @smime_cms_tests);
  625. runner_loop(prefix => 'cms2cms-1', cmd1 => 'cms', cmd2 => 'cms',
  626. tests => [ @smime_pkcs7_tests ]);
  627. runner_loop(prefix => 'cms2cms-2', cmd1 => 'cms', cmd2 => 'cms',
  628. tests => [ @smime_cms_tests ]);
  629. };
  630. subtest "CMS <=> CMS consistency tests, modified key parameters\n" => sub {
  631. plan tests =>
  632. (scalar @smime_cms_param_tests) + (scalar @smime_cms_comp_tests);
  633. runner_loop(prefix => 'cms2cms-mod', cmd1 => 'cms', cmd2 => 'cms',
  634. tests => [ @smime_cms_param_tests ]);
  635. SKIP: {
  636. skip("Zlib not supported: compression tests skipped",
  637. scalar @smime_cms_comp_tests)
  638. if $no_zlib;
  639. runner_loop(prefix => 'cms2cms-comp', cmd1 => 'cms', cmd2 => 'cms',
  640. tests => [ @smime_cms_comp_tests ]);
  641. }
  642. };
  643. # Returns the number of matches of a Content Type Attribute in a binary file.
  644. sub contentType_matches {
  645. # Read in a binary file
  646. my ($in) = @_;
  647. open (HEX_IN, "$in") or die("open failed for $in : $!");
  648. binmode(HEX_IN);
  649. local $/;
  650. my $str = <HEX_IN>;
  651. # Find ASN1 data for a Content Type Attribute (with a OID of PKCS7 data)
  652. 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;
  653. close(HEX_IN);
  654. return scalar(@c);
  655. }
  656. subtest "CMS Check the content type attribute is added for additional signers\n" => sub {
  657. plan tests => (scalar @contenttype_cms_test);
  658. runner_loop(prefix => 'cms2cms-added', cmd1 => 'cms', cmd2 => 'cms',
  659. tests => [ @contenttype_cms_test ]);
  660. };
  661. subtest "CMS Check that bad attributes fail when verifying signers\n" => sub {
  662. plan tests =>
  663. (scalar @incorrect_attribute_cms_test);
  664. my $cnt = 0;
  665. foreach my $name (@incorrect_attribute_cms_test) {
  666. my $out = "incorrect-$cnt.txt";
  667. ok(!run(app(["openssl", "cms", @prov, "-verify", "-in",
  668. catfile($datadir, $name), "-inform", "DER", "-CAfile",
  669. catfile($smdir, "smroot.pem"), "-out", $out ])),
  670. $name);
  671. }
  672. };
  673. subtest "CMS Decrypt message encrypted with OpenSSL 1.1.1\n" => sub {
  674. plan tests => 1;
  675. SKIP: {
  676. skip "EC or DES isn't supported in this build", 1
  677. if disabled("ec") || disabled("des");
  678. my $out = "smtst.txt";
  679. ok(run(app(["openssl", "cms", @defaultprov, "-decrypt",
  680. "-inkey", catfile($smdir, "smec3.pem"),
  681. "-in", catfile($datadir, "ciphertext_from_1_1_1.cms"),
  682. "-out", $out ]))
  683. && compare_text($smcont, $out) == 0,
  684. "Decrypt message from OpenSSL 1.1.1");
  685. }
  686. };
  687. subtest "CAdES <=> CAdES consistency tests\n" => sub {
  688. plan tests => (scalar @smime_cms_cades_tests);
  689. runner_loop(prefix => 'cms-cades', cmd1 => 'cms', cmd2 => 'cms',
  690. tests => [ @smime_cms_cades_tests ]);
  691. };
  692. subtest "CAdES; cms incompatible arguments tests\n" => sub {
  693. plan tests => (scalar @smime_cms_cades_invalid_option_tests);
  694. foreach (@smime_cms_cades_invalid_option_tests) {
  695. ok(!run(app(["openssl", "cms", @{$$_[0]} ] )));
  696. }
  697. };
  698. subtest "CAdES ko tests\n" => sub {
  699. plan tests => (scalar @smime_cms_cades_ko_tests);
  700. foreach (@smime_cms_cades_ko_tests) {
  701. SKIP: {
  702. my $skip_reason = check_availability($$_[0]);
  703. skip $skip_reason, 1 if $skip_reason;
  704. ok(run(app(["openssl", "cms", @{$$_[1]}]))
  705. && !run(app(["openssl", "cms", @{$$_[2]}])),
  706. $$_[0]);
  707. }
  708. }
  709. };
  710. sub check_availability {
  711. my $tnam = shift;
  712. return "$tnam: skipped, EC disabled\n"
  713. if ($no_ec && $tnam =~ /ECDH/);
  714. return "$tnam: skipped, ECDH disabled\n"
  715. if ($no_ec && $tnam =~ /ECDH/);
  716. return "$tnam: skipped, EC2M disabled\n"
  717. if ($no_ec2m && $tnam =~ /K-283/);
  718. return "$tnam: skipped, DH disabled\n"
  719. if ($no_dh && $tnam =~ /X9\.42/);
  720. return "$tnam: skipped, RC2 disabled\n"
  721. if ($no_rc2 && $tnam =~ /RC2/);
  722. return "$tnam: skipped, DES disabled\n"
  723. if ($no_des && $tnam =~ /DES/);
  724. return "$tnam: skipped, DSA disabled\n"
  725. if ($no_dsa && $tnam =~ / DSA/);
  726. return "";
  727. }