80-test_cms.t 30 KB

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