80-test_cms.t 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217
  1. #! /usr/bin/env perl
  2. # Copyright 2015-2023 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 compare/;
  13. use OpenSSL::Test qw/:DEFAULT srctop_dir srctop_file bldtop_dir bldtop_file with data_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. my $no_fips = disabled('fips') || ($ENV{NO_FIPS} // 0);
  21. plan skip_all => "CMS is not supported by this OpenSSL build"
  22. if disabled("cms");
  23. my $provpath = bldtop_dir("providers");
  24. # Some tests require legacy algorithms to be included.
  25. my @legacyprov = ("-provider-path", $provpath,
  26. "-provider", "default",
  27. "-provider", "legacy" );
  28. my @defaultprov = ("-provider-path", $provpath,
  29. "-provider", "default");
  30. my @config = ( );
  31. my $provname = 'default';
  32. my $datadir = srctop_dir("test", "recipes", "80-test_cms_data");
  33. my $smdir = srctop_dir("test", "smime-certs");
  34. my $smcont = srctop_file("test", "smcont.txt");
  35. my $smcont_zero = srctop_file("test", "smcont_zero.txt");
  36. my ($no_des, $no_dh, $no_dsa, $no_ec, $no_ec2m, $no_rc2, $no_zlib)
  37. = disabled qw/des dh dsa ec ec2m rc2 zlib/;
  38. $no_rc2 = 1 if disabled("legacy");
  39. plan tests => 23;
  40. ok(run(test(["pkcs7_test"])), "test pkcs7");
  41. unless ($no_fips) {
  42. @config = ( "-config", srctop_file("test", "fips-and-base.cnf") );
  43. $provname = 'fips';
  44. }
  45. $ENV{OPENSSL_TEST_LIBCTX} = "1";
  46. my @prov = ("-provider-path", $provpath,
  47. @config,
  48. "-provider", $provname);
  49. my $smrsa1024 = catfile($smdir, "smrsa1024.pem");
  50. my $smrsa1 = catfile($smdir, "smrsa1.pem");
  51. my $smroot = catfile($smdir, "smroot.pem");
  52. my @smime_pkcs7_tests = (
  53. [ "signed content DER format, RSA key",
  54. [ "{cmd1}", @prov, "-sign", "-in", $smcont, "-outform", "DER", "-nodetach",
  55. "-certfile", $smroot, "-signer", $smrsa1, "-out", "{output}.cms" ],
  56. [ "{cmd2}", @prov, "-verify", "-in", "{output}.cms", "-inform", "DER",
  57. "-CAfile", $smroot, "-out", "{output}.txt" ],
  58. \&final_compare
  59. ],
  60. [ "signed detached content DER format, RSA key",
  61. [ "{cmd1}", @prov, "-sign", "-in", $smcont, "-outform", "DER",
  62. "-signer", $smrsa1, "-out", "{output}.cms" ],
  63. [ "{cmd2}", @prov, "-verify", "-in", "{output}.cms", "-inform", "DER",
  64. "-CAfile", $smroot, "-out", "{output}.txt",
  65. "-content", $smcont ],
  66. \&final_compare
  67. ],
  68. [ "signed content test streaming BER format, RSA",
  69. [ "{cmd1}", @prov, "-sign", "-in", $smcont, "-outform", "DER", "-nodetach",
  70. "-stream",
  71. "-signer", $smrsa1, "-out", "{output}.cms" ],
  72. [ "{cmd2}", @prov, "-verify", "-in", "{output}.cms", "-inform", "DER",
  73. "-CAfile", $smroot, "-out", "{output}.txt" ],
  74. \&final_compare
  75. ],
  76. [ "signed content DER format, DSA key",
  77. [ "{cmd1}", @prov, "-sign", "-in", $smcont, "-outform", "DER", "-nodetach",
  78. "-signer", catfile($smdir, "smdsa1.pem"), "-out", "{output}.cms" ],
  79. [ "{cmd2}", @prov, "-verify", "-in", "{output}.cms", "-inform", "DER",
  80. "-CAfile", $smroot, "-out", "{output}.txt" ],
  81. \&final_compare
  82. ],
  83. [ "signed detached content DER format, DSA key",
  84. [ "{cmd1}", @prov, "-sign", "-in", $smcont, "-outform", "DER",
  85. "-signer", catfile($smdir, "smdsa1.pem"), "-out", "{output}.cms" ],
  86. [ "{cmd2}", @prov, "-verify", "-in", "{output}.cms", "-inform", "DER",
  87. "-CAfile", $smroot, "-out", "{output}.txt",
  88. "-content", $smcont ],
  89. \&final_compare
  90. ],
  91. [ "signed detached content DER format, add RSA signer (with DSA existing)",
  92. [ "{cmd1}", @prov, "-sign", "-in", $smcont, "-outform", "DER",
  93. "-signer", catfile($smdir, "smdsa1.pem"), "-out", "{output}.cms" ],
  94. [ "{cmd1}", @prov, "-resign", "-in", "{output}.cms", "-inform", "DER", "-outform", "DER",
  95. "-signer", $smrsa1, "-out", "{output}2.cms" ],
  96. [ "{cmd2}", @prov, "-verify", "-in", "{output}2.cms", "-inform", "DER",
  97. "-CAfile", $smroot, "-out", "{output}.txt",
  98. "-content", $smcont ],
  99. \&final_compare
  100. ],
  101. [ "signed content test streaming BER format, DSA key",
  102. [ "{cmd1}", @prov, "-sign", "-in", $smcont, "-outform", "DER",
  103. "-nodetach", "-stream",
  104. "-signer", catfile($smdir, "smdsa1.pem"), "-out", "{output}.cms" ],
  105. [ "{cmd2}", @prov, "-verify", "-in", "{output}.cms", "-inform", "DER",
  106. "-CAfile", $smroot, "-out", "{output}.txt" ],
  107. \&final_compare
  108. ],
  109. [ "signed content test streaming BER format, 2 DSA and 2 RSA keys",
  110. [ "{cmd1}", @prov, "-sign", "-in", $smcont, "-outform", "DER",
  111. "-nodetach", "-stream",
  112. "-signer", $smrsa1,
  113. "-signer", catfile($smdir, "smrsa2.pem"),
  114. "-signer", catfile($smdir, "smdsa1.pem"),
  115. "-signer", catfile($smdir, "smdsa2.pem"),
  116. "-out", "{output}.cms" ],
  117. [ "{cmd2}", @prov, "-verify", "-in", "{output}.cms", "-inform", "DER",
  118. "-CAfile", $smroot, "-out", "{output}.txt" ],
  119. \&final_compare
  120. ],
  121. [ "signed content test streaming BER format, 2 DSA and 2 RSA keys, no attributes",
  122. [ "{cmd1}", @prov, "-sign", "-in", $smcont, "-outform", "DER",
  123. "-noattr", "-nodetach", "-stream",
  124. "-signer", $smrsa1,
  125. "-signer", catfile($smdir, "smrsa2.pem"),
  126. "-signer", catfile($smdir, "smdsa1.pem"),
  127. "-signer", catfile($smdir, "smdsa2.pem"),
  128. "-out", "{output}.cms" ],
  129. [ "{cmd2}", @prov, "-verify", "-in", "{output}.cms", "-inform", "DER",
  130. "-CAfile", $smroot, "-out", "{output}.txt" ],
  131. \&final_compare
  132. ],
  133. [ "signed content S/MIME format, RSA key SHA1",
  134. [ "{cmd1}", @defaultprov, "-sign", "-in", $smcont, "-md", "sha1",
  135. "-certfile", $smroot,
  136. "-signer", $smrsa1, "-out", "{output}.cms" ],
  137. [ "{cmd2}", @prov, "-verify", "-in", "{output}.cms",
  138. "-CAfile", $smroot, "-out", "{output}.txt" ],
  139. \&final_compare
  140. ],
  141. [ "signed zero-length content S/MIME format, RSA key SHA1",
  142. [ "{cmd1}", @defaultprov, "-sign", "-in", $smcont_zero, "-md", "sha1",
  143. "-certfile", $smroot, "-signer", $smrsa1, "-out", "{output}.cms" ],
  144. [ "{cmd2}", @prov, "-verify", "-in", "{output}.cms",
  145. "-CAfile", $smroot, "-out", "{output}.txt" ],
  146. \&zero_compare
  147. ],
  148. [ "signed content test streaming S/MIME format, 2 DSA and 2 RSA keys",
  149. [ "{cmd1}", @prov, "-sign", "-in", $smcont, "-nodetach",
  150. "-signer", $smrsa1,
  151. "-signer", catfile($smdir, "smrsa2.pem"),
  152. "-signer", catfile($smdir, "smdsa1.pem"),
  153. "-signer", catfile($smdir, "smdsa2.pem"),
  154. "-stream", "-out", "{output}.cms" ],
  155. [ "{cmd2}", @prov, "-verify", "-in", "{output}.cms",
  156. "-CAfile", $smroot, "-out", "{output}.txt" ],
  157. \&final_compare
  158. ],
  159. [ "signed content test streaming multipart S/MIME format, 2 DSA and 2 RSA keys",
  160. [ "{cmd1}", @prov, "-sign", "-in", $smcont,
  161. "-signer", $smrsa1,
  162. "-signer", catfile($smdir, "smrsa2.pem"),
  163. "-signer", catfile($smdir, "smdsa1.pem"),
  164. "-signer", catfile($smdir, "smdsa2.pem"),
  165. "-stream", "-out", "{output}.cms" ],
  166. [ "{cmd2}", @prov, "-verify", "-in", "{output}.cms",
  167. "-CAfile", $smroot, "-out", "{output}.txt" ],
  168. \&final_compare
  169. ],
  170. [ "enveloped content test streaming S/MIME format, DES, 3 recipients",
  171. [ "{cmd1}", @defaultprov, "-encrypt", "-in", $smcont,
  172. "-stream", "-out", "{output}.cms",
  173. $smrsa1,
  174. catfile($smdir, "smrsa2.pem"),
  175. catfile($smdir, "smrsa3.pem") ],
  176. [ "{cmd2}", @defaultprov, "-decrypt", "-recip", $smrsa1,
  177. "-in", "{output}.cms", "-out", "{output}.txt" ],
  178. \&final_compare
  179. ],
  180. [ "enveloped content test streaming S/MIME format, DES, 3 recipients, 3rd used",
  181. [ "{cmd1}", @defaultprov, "-encrypt", "-in", $smcont,
  182. "-stream", "-out", "{output}.cms",
  183. $smrsa1,
  184. catfile($smdir, "smrsa2.pem"),
  185. catfile($smdir, "smrsa3.pem") ],
  186. [ "{cmd2}", @defaultprov, "-decrypt", "-recip", catfile($smdir, "smrsa3.pem"),
  187. "-in", "{output}.cms", "-out", "{output}.txt" ],
  188. \&final_compare
  189. ],
  190. [ "enveloped content test streaming S/MIME format, DES, 3 recipients, cert and key files used",
  191. [ "{cmd1}", @defaultprov, "-encrypt", "-in", $smcont,
  192. "-stream", "-out", "{output}.cms",
  193. $smrsa1,
  194. catfile($smdir, "smrsa2.pem"),
  195. catfile($smdir, "smrsa3-cert.pem") ],
  196. [ "{cmd2}", @defaultprov, "-decrypt",
  197. "-recip", catfile($smdir, "smrsa3-cert.pem"),
  198. "-inkey", catfile($smdir, "smrsa3-key.pem"),
  199. "-in", "{output}.cms", "-out", "{output}.txt" ],
  200. \&final_compare
  201. ],
  202. [ "enveloped content test streaming S/MIME format, AES-256 cipher, 3 recipients",
  203. [ "{cmd1}", @prov, "-encrypt", "-in", $smcont,
  204. "-aes256", "-stream", "-out", "{output}.cms",
  205. $smrsa1,
  206. catfile($smdir, "smrsa2.pem"),
  207. catfile($smdir, "smrsa3.pem") ],
  208. [ "{cmd2}", @prov, "-decrypt", "-recip", $smrsa1,
  209. "-in", "{output}.cms", "-out", "{output}.txt" ],
  210. \&final_compare
  211. ],
  212. );
  213. my @smime_cms_tests = (
  214. [ "signed content test streaming BER format, 2 DSA and 2 RSA keys, keyid",
  215. [ "{cmd1}", @prov, "-sign", "-in", $smcont, "-outform", "DER",
  216. "-nodetach", "-keyid",
  217. "-signer", $smrsa1,
  218. "-signer", catfile($smdir, "smrsa2.pem"),
  219. "-signer", catfile($smdir, "smdsa1.pem"),
  220. "-signer", catfile($smdir, "smdsa2.pem"),
  221. "-stream", "-out", "{output}.cms" ],
  222. [ "{cmd2}", @prov, "-verify", "-in", "{output}.cms", "-inform", "DER",
  223. "-CAfile", $smroot, "-out", "{output}.txt" ],
  224. \&final_compare
  225. ],
  226. [ "signed content test streaming PEM format, 2 DSA and 2 RSA keys",
  227. [ "{cmd1}", @prov, "-sign", "-in", $smcont, "-outform", "PEM", "-nodetach",
  228. "-signer", $smrsa1,
  229. "-signer", catfile($smdir, "smrsa2.pem"),
  230. "-signer", catfile($smdir, "smdsa1.pem"),
  231. "-signer", catfile($smdir, "smdsa2.pem"),
  232. "-stream", "-out", "{output}.cms" ],
  233. [ "{cmd2}", @prov, "-verify", "-in", "{output}.cms", "-inform", "PEM",
  234. "-CAfile", $smroot, "-out", "{output}.txt" ],
  235. \&final_compare
  236. ],
  237. [ "signed content MIME format, RSA key, signed receipt request",
  238. [ "{cmd1}", @prov, "-sign", "-in", $smcont, "-nodetach",
  239. "-signer", $smrsa1,
  240. "-receipt_request_to", "test\@openssl.org", "-receipt_request_all",
  241. "-out", "{output}.cms" ],
  242. [ "{cmd2}", @prov, "-verify", "-in", "{output}.cms",
  243. "-CAfile", $smroot, "-out", "{output}.txt" ],
  244. \&final_compare
  245. ],
  246. [ "signed receipt MIME format, RSA key",
  247. [ "{cmd1}", @prov, "-sign", "-in", $smcont, "-nodetach",
  248. "-signer", $smrsa1,
  249. "-receipt_request_to", "test\@openssl.org", "-receipt_request_all",
  250. "-out", "{output}.cms" ],
  251. [ "{cmd1}", @prov, "-sign_receipt", "-in", "{output}.cms",
  252. "-signer", catfile($smdir, "smrsa2.pem"), "-out", "{output}2.cms" ],
  253. [ "{cmd2}", @prov, "-verify_receipt", "{output}2.cms", "-in", "{output}.cms",
  254. "-CAfile", $smroot ]
  255. ],
  256. [ "enveloped content test streaming S/MIME format, DES, 3 recipients, keyid",
  257. [ "{cmd1}", @defaultprov, "-encrypt", "-in", $smcont,
  258. "-stream", "-out", "{output}.cms", "-keyid",
  259. $smrsa1,
  260. catfile($smdir, "smrsa2.pem"),
  261. catfile($smdir, "smrsa3.pem") ],
  262. [ "{cmd2}", @defaultprov, "-decrypt", "-recip", $smrsa1,
  263. "-in", "{output}.cms", "-out", "{output}.txt" ],
  264. \&final_compare
  265. ],
  266. [ "enveloped content test streaming PEM format, AES-256-CBC cipher, KEK",
  267. [ "{cmd1}", @prov, "-encrypt", "-in", $smcont, "-outform", "PEM", "-aes128",
  268. "-stream", "-out", "{output}.cms",
  269. "-secretkey", "000102030405060708090A0B0C0D0E0F",
  270. "-secretkeyid", "C0FEE0" ],
  271. [ "{cmd2}", @prov, "-decrypt", "-in", "{output}.cms", "-out", "{output}.txt",
  272. "-inform", "PEM",
  273. "-secretkey", "000102030405060708090A0B0C0D0E0F",
  274. "-secretkeyid", "C0FEE0" ],
  275. \&final_compare
  276. ],
  277. [ "enveloped content test streaming PEM format, AES-256-GCM cipher, KEK",
  278. [ "{cmd1}", @prov, "-encrypt", "-in", $smcont, "-outform", "PEM", "-aes-128-gcm",
  279. "-stream", "-out", "{output}.cms",
  280. "-secretkey", "000102030405060708090A0B0C0D0E0F",
  281. "-secretkeyid", "C0FEE0" ],
  282. [ "{cmd2}", "-decrypt", "-in", "{output}.cms", "-out", "{output}.txt",
  283. "-inform", "PEM",
  284. "-secretkey", "000102030405060708090A0B0C0D0E0F",
  285. "-secretkeyid", "C0FEE0" ],
  286. \&final_compare
  287. ],
  288. [ "enveloped content test streaming PEM format, KEK, key only",
  289. [ "{cmd1}", @prov, "-encrypt", "-in", $smcont, "-outform", "PEM", "-aes128",
  290. "-stream", "-out", "{output}.cms",
  291. "-secretkey", "000102030405060708090A0B0C0D0E0F",
  292. "-secretkeyid", "C0FEE0" ],
  293. [ "{cmd2}", @prov, "-decrypt", "-in", "{output}.cms", "-out", "{output}.txt",
  294. "-inform", "PEM",
  295. "-secretkey", "000102030405060708090A0B0C0D0E0F" ],
  296. \&final_compare
  297. ],
  298. [ "data content test streaming PEM format",
  299. [ "{cmd1}", @prov, "-data_create", "-in", $smcont, "-outform", "PEM",
  300. "-nodetach", "-stream", "-out", "{output}.cms" ],
  301. [ "{cmd2}", @prov, "-data_out", "-in", "{output}.cms", "-inform", "PEM",
  302. "-out", "{output}.txt" ],
  303. \&final_compare
  304. ],
  305. [ "encrypted content test streaming PEM format, 128 bit RC2 key",
  306. [ "{cmd1}", @legacyprov, "-EncryptedData_encrypt",
  307. "-in", $smcont, "-outform", "PEM",
  308. "-rc2", "-secretkey", "000102030405060708090A0B0C0D0E0F",
  309. "-stream", "-out", "{output}.cms" ],
  310. [ "{cmd2}", @legacyprov, "-EncryptedData_decrypt", "-in", "{output}.cms",
  311. "-inform", "PEM",
  312. "-secretkey", "000102030405060708090A0B0C0D0E0F",
  313. "-out", "{output}.txt" ],
  314. \&final_compare
  315. ],
  316. [ "encrypted content test streaming PEM format, 40 bit RC2 key",
  317. [ "{cmd1}", @legacyprov, "-EncryptedData_encrypt",
  318. "-in", $smcont, "-outform", "PEM",
  319. "-rc2", "-secretkey", "0001020304",
  320. "-stream", "-out", "{output}.cms" ],
  321. [ "{cmd2}", @legacyprov, "-EncryptedData_decrypt", "-in", "{output}.cms",
  322. "-inform", "PEM",
  323. "-secretkey", "0001020304", "-out", "{output}.txt" ],
  324. \&final_compare
  325. ],
  326. [ "encrypted content test streaming PEM format, triple DES key",
  327. [ "{cmd1}", @prov, "-EncryptedData_encrypt", "-in", $smcont, "-outform", "PEM",
  328. "-des3", "-secretkey", "000102030405060708090A0B0C0D0E0F1011121314151617",
  329. "-stream", "-out", "{output}.cms" ],
  330. [ "{cmd2}", @prov, "-EncryptedData_decrypt", "-in", "{output}.cms",
  331. "-inform", "PEM",
  332. "-secretkey", "000102030405060708090A0B0C0D0E0F1011121314151617",
  333. "-out", "{output}.txt" ],
  334. \&final_compare
  335. ],
  336. [ "encrypted content test streaming PEM format, 128 bit AES key",
  337. [ "{cmd1}", @prov, "-EncryptedData_encrypt", "-in", $smcont, "-outform", "PEM",
  338. "-aes128", "-secretkey", "000102030405060708090A0B0C0D0E0F",
  339. "-stream", "-out", "{output}.cms" ],
  340. [ "{cmd2}", @prov, "-EncryptedData_decrypt", "-in", "{output}.cms",
  341. "-inform", "PEM",
  342. "-secretkey", "000102030405060708090A0B0C0D0E0F",
  343. "-out", "{output}.txt" ],
  344. \&final_compare
  345. ],
  346. );
  347. my @smime_cms_cades_tests = (
  348. [ "signed content DER format, RSA key, CAdES-BES compatible",
  349. [ "{cmd1}", @prov, "-sign", "-cades", "-in", $smcont, "-outform", "DER",
  350. "-nodetach",
  351. "-certfile", $smroot, "-signer", $smrsa1, "-out", "{output}.cms" ],
  352. [ "{cmd2}", @prov, "-verify", "-cades", "-in", "{output}.cms", "-inform", "DER",
  353. "-CAfile", $smroot, "-out", "{output}.txt" ],
  354. \&final_compare
  355. ],
  356. [ "signed content DER format, RSA key, SHA256 md, CAdES-BES compatible",
  357. [ "{cmd1}", @prov, "-sign", "-cades", "-md", "sha256", "-in", $smcont, "-outform",
  358. "DER", "-nodetach", "-certfile", $smroot,
  359. "-signer", $smrsa1, "-out", "{output}.cms" ],
  360. [ "{cmd2}", @prov, "-verify", "-cades", "-in", "{output}.cms", "-inform", "DER",
  361. "-CAfile", $smroot, "-out", "{output}.txt" ],
  362. \&final_compare
  363. ],
  364. [ "signed content DER format, RSA key, SHA512 md, CAdES-BES compatible",
  365. [ "{cmd1}", @prov, "-sign", "-cades", "-md", "sha512", "-in", $smcont, "-outform",
  366. "DER", "-nodetach", "-certfile", $smroot,
  367. "-signer", $smrsa1, "-out", "{output}.cms" ],
  368. [ "{cmd2}", @prov, "-verify", "-cades", "-in", "{output}.cms", "-inform", "DER",
  369. "-CAfile", $smroot, "-out", "{output}.txt" ],
  370. \&final_compare
  371. ],
  372. [ "signed content DER format, RSA key, SHA256 md, CAdES-BES compatible",
  373. [ "{cmd1}", @prov, "-sign", "-cades", "-binary", "-nodetach", "-nosmimecap", "-md", "sha256",
  374. "-in", $smcont, "-outform", "DER",
  375. "-certfile", $smroot, "-signer", $smrsa1,
  376. "-outform", "DER", "-out", "{output}.cms" ],
  377. [ "{cmd2}", @prov, "-verify", "-cades", "-in", "{output}.cms", "-inform", "DER",
  378. "-CAfile", $smroot, "-out", "{output}.txt" ],
  379. \&final_compare
  380. ],
  381. [ "resigned content DER format, RSA key, SHA256 md, CAdES-BES compatible",
  382. [ "{cmd1}", @prov, "-sign", "-cades", "-binary", "-nodetach", "-nosmimecap", "-md", "sha256",
  383. "-in", $smcont, "-outform", "DER",
  384. "-certfile", $smroot, "-signer", $smrsa1,
  385. "-outform", "DER", "-out", "{output}.cms" ],
  386. [ "{cmd1}", @prov, "-resign", "-cades", "-binary", "-nodetach", "-nosmimecap", "-md", "sha256",
  387. "-inform", "DER", "-in", "{output}.cms",
  388. "-certfile", $smroot, "-signer", catfile($smdir, "smrsa2.pem"),
  389. "-outform", "DER", "-out", "{output}2.cms" ],
  390. [ "{cmd2}", @prov, "-verify", "-cades", "-in", "{output}2.cms", "-inform", "DER",
  391. "-CAfile", $smroot, "-out", "{output}.txt" ],
  392. \&final_compare
  393. ],
  394. );
  395. my @smime_cms_cades_ko_tests = (
  396. [ "sign content DER format, RSA key, not CAdES-BES compatible",
  397. [ @prov, "-sign", "-in", $smcont, "-outform", "DER", "-nodetach",
  398. "-certfile", $smroot, "-signer", $smrsa1, "-out", "cades-ko.cms" ],
  399. "fail to verify token since requiring CAdES-BES compatibility",
  400. [ @prov, "-verify", "-cades", "-in", "cades-ko.cms", "-inform", "DER",
  401. "-CAfile", $smroot, "-out", "cades-ko.txt" ],
  402. \&final_compare
  403. ]
  404. );
  405. # cades options test - check that some combinations are rejected
  406. my @smime_cms_cades_invalid_option_tests = (
  407. [
  408. [ "-cades", "-noattr" ],
  409. ],[
  410. [ "-verify", "-cades", "-noattr" ],
  411. ],[
  412. [ "-verify", "-cades", "-noverify" ],
  413. ],
  414. );
  415. my @smime_cms_comp_tests = (
  416. [ "compressed content test streaming PEM format",
  417. [ "{cmd1}", @prov, "-compress", "-in", $smcont, "-outform", "PEM", "-nodetach",
  418. "-stream", "-out", "{output}.cms" ],
  419. [ "{cmd2}", @prov, "-uncompress", "-in", "{output}.cms", "-inform", "PEM",
  420. "-out", "{output}.txt" ],
  421. \&final_compare
  422. ]
  423. );
  424. my @smime_cms_param_tests = (
  425. [ "signed content test streaming PEM format, RSA keys, PSS signature",
  426. [ "{cmd1}", @prov, "-sign", "-in", $smcont, "-outform", "PEM", "-nodetach",
  427. "-signer", $smrsa1,
  428. "-keyopt", "rsa_padding_mode:pss",
  429. "-out", "{output}.cms" ],
  430. [ "{cmd2}", @prov, "-verify", "-in", "{output}.cms", "-inform", "PEM",
  431. "-CAfile", $smroot, "-out", "{output}.txt" ],
  432. \&final_compare
  433. ],
  434. [ "signed content test streaming PEM format, RSA keys, PSS signature, saltlen=max",
  435. [ "{cmd1}", @prov, "-sign", "-in", $smcont, "-outform", "PEM", "-nodetach",
  436. "-signer", $smrsa1,
  437. "-keyopt", "rsa_padding_mode:pss", "-keyopt", "rsa_pss_saltlen:max",
  438. "-out", "{output}.cms" ],
  439. sub { my %opts = @_; rsapssSaltlen("$opts{output}.cms") == 222; },
  440. [ "{cmd2}", @prov, "-verify", "-in", "{output}.cms", "-inform", "PEM",
  441. "-CAfile", $smroot, "-out", "{output}.txt" ],
  442. \&final_compare
  443. ],
  444. [ "signed content test streaming PEM format, RSA keys, PSS signature, no attributes",
  445. [ "{cmd1}", @prov, "-sign", "-in", $smcont, "-outform", "PEM", "-nodetach",
  446. "-noattr", "-signer", $smrsa1,
  447. "-keyopt", "rsa_padding_mode:pss",
  448. "-out", "{output}.cms" ],
  449. [ "{cmd2}", @prov, "-verify", "-in", "{output}.cms", "-inform", "PEM",
  450. "-CAfile", $smroot, "-out", "{output}.txt" ],
  451. \&final_compare
  452. ],
  453. [ "signed content test streaming PEM format, RSA keys, PSS signature, SHA384 MGF1",
  454. [ "{cmd1}", @prov, "-sign", "-in", $smcont, "-outform", "PEM", "-nodetach",
  455. "-signer", $smrsa1,
  456. "-keyopt", "rsa_padding_mode:pss", "-keyopt", "rsa_mgf1_md:sha384",
  457. "-out", "{output}.cms" ],
  458. [ "{cmd2}", @prov, "-verify", "-in", "{output}.cms", "-inform", "PEM",
  459. "-CAfile", $smroot, "-out", "{output}.txt" ],
  460. \&final_compare
  461. ],
  462. [ "signed content test streaming PEM format, RSA keys, PSS signature, saltlen=16",
  463. [ "{cmd1}", @prov, "-sign", "-in", $smcont, "-outform", "PEM", "-nodetach",
  464. "-signer", $smrsa1, "-md", "sha256",
  465. "-keyopt", "rsa_padding_mode:pss", "-keyopt", "rsa_pss_saltlen:16",
  466. "-out", "{output}.cms" ],
  467. sub { my %opts = @_; rsapssSaltlen("$opts{output}.cms") == 16; },
  468. [ "{cmd2}", @prov, "-verify", "-in", "{output}.cms", "-inform", "PEM",
  469. "-CAfile", $smroot, "-out", "{output}.txt" ],
  470. \&final_compare
  471. ],
  472. [ "signed content test streaming PEM format, RSA keys, PSS signature, saltlen=digest",
  473. [ "{cmd1}", @prov, "-sign", "-in", $smcont, "-outform", "PEM", "-nodetach",
  474. "-signer", $smrsa1, "-md", "sha256",
  475. "-keyopt", "rsa_padding_mode:pss", "-keyopt", "rsa_pss_saltlen:digest",
  476. "-out", "{output}.cms" ],
  477. # digest is SHA-256, which produces 32 bytes of output
  478. sub { my %opts = @_; rsapssSaltlen("$opts{output}.cms") == 32; },
  479. [ "{cmd2}", @prov, "-verify", "-in", "{output}.cms", "-inform", "PEM",
  480. "-CAfile", $smroot, "-out", "{output}.txt" ],
  481. \&final_compare
  482. ],
  483. [ "enveloped content test streaming S/MIME format, DES, OAEP default parameters",
  484. [ "{cmd1}", @defaultprov, "-encrypt", "-in", $smcont,
  485. "-stream", "-out", "{output}.cms",
  486. "-recip", $smrsa1,
  487. "-keyopt", "rsa_padding_mode:oaep" ],
  488. [ "{cmd2}", @defaultprov, "-decrypt", "-recip", $smrsa1,
  489. "-in", "{output}.cms", "-out", "{output}.txt" ],
  490. \&final_compare
  491. ],
  492. [ "enveloped content test streaming S/MIME format, DES, OAEP SHA256",
  493. [ "{cmd1}", @defaultprov, "-encrypt", "-in", $smcont,
  494. "-stream", "-out", "{output}.cms",
  495. "-recip", $smrsa1,
  496. "-keyopt", "rsa_padding_mode:oaep",
  497. "-keyopt", "rsa_oaep_md:sha256" ],
  498. [ "{cmd2}", @defaultprov, "-decrypt", "-recip", $smrsa1,
  499. "-in", "{output}.cms", "-out", "{output}.txt" ],
  500. \&final_compare
  501. ],
  502. [ "enveloped content test streaming S/MIME format, DES, ECDH",
  503. [ "{cmd1}", @defaultprov, "-encrypt", "-in", $smcont,
  504. "-stream", "-out", "{output}.cms",
  505. "-recip", catfile($smdir, "smec1.pem") ],
  506. [ "{cmd2}", @defaultprov, "-decrypt", "-recip", catfile($smdir, "smec1.pem"),
  507. "-in", "{output}.cms", "-out", "{output}.txt" ],
  508. \&final_compare
  509. ],
  510. [ "enveloped content test streaming S/MIME format, DES, ECDH, 2 recipients, key only used",
  511. [ "{cmd1}", @defaultprov, "-encrypt", "-in", $smcont,
  512. "-stream", "-out", "{output}.cms",
  513. catfile($smdir, "smec1.pem"),
  514. catfile($smdir, "smec3.pem") ],
  515. [ "{cmd2}", @defaultprov, "-decrypt", "-inkey", catfile($smdir, "smec3.pem"),
  516. "-in", "{output}.cms", "-out", "{output}.txt" ],
  517. \&final_compare
  518. ],
  519. [ "enveloped content test streaming S/MIME format, ECDH, DES, key identifier",
  520. [ "{cmd1}", @defaultprov, "-encrypt", "-keyid", "-in", $smcont,
  521. "-stream", "-out", "{output}.cms",
  522. "-recip", catfile($smdir, "smec1.pem") ],
  523. [ "{cmd2}", @defaultprov, "-decrypt", "-recip", catfile($smdir, "smec1.pem"),
  524. "-in", "{output}.cms", "-out", "{output}.txt" ],
  525. \&final_compare
  526. ],
  527. [ "enveloped content test streaming S/MIME format, ECDH, AES-128-CBC, SHA256 KDF",
  528. [ "{cmd1}", @prov, "-encrypt", "-in", $smcont,
  529. "-stream", "-out", "{output}.cms",
  530. "-recip", catfile($smdir, "smec1.pem"), "-aes128",
  531. "-keyopt", "ecdh_kdf_md:sha256" ],
  532. [ "{cmd2}", @prov, "-decrypt", "-recip", catfile($smdir, "smec1.pem"),
  533. "-in", "{output}.cms", "-out", "{output}.txt" ],
  534. \&final_compare
  535. ],
  536. [ "enveloped content test streaming S/MIME format, ECDH, AES-128-GCM cipher, SHA256 KDF",
  537. [ "{cmd1}", @prov, "-encrypt", "-in", $smcont,
  538. "-stream", "-out", "{output}.cms",
  539. "-recip", catfile($smdir, "smec1.pem"), "-aes-128-gcm", "-keyopt", "ecdh_kdf_md:sha256" ],
  540. [ "{cmd2}", "-decrypt", "-recip", catfile($smdir, "smec1.pem"),
  541. "-in", "{output}.cms", "-out", "{output}.txt" ],
  542. \&final_compare
  543. ],
  544. [ "enveloped content test streaming S/MIME format, ECDH, K-283, cofactor DH",
  545. [ "{cmd1}", @prov, "-encrypt", "-in", $smcont,
  546. "-stream", "-out", "{output}.cms",
  547. "-recip", catfile($smdir, "smec2.pem"), "-aes128",
  548. "-keyopt", "ecdh_kdf_md:sha256", "-keyopt", "ecdh_cofactor_mode:1" ],
  549. [ "{cmd2}", @prov, "-decrypt", "-recip", catfile($smdir, "smec2.pem"),
  550. "-in", "{output}.cms", "-out", "{output}.txt" ],
  551. \&final_compare
  552. ],
  553. [ "enveloped content test streaming S/MIME format, X9.42 DH",
  554. [ "{cmd1}", @prov, "-encrypt", "-in", $smcont,
  555. "-stream", "-out", "{output}.cms",
  556. "-recip", catfile($smdir, "smdh.pem"), "-aes128" ],
  557. [ "{cmd2}", @prov, "-decrypt", "-recip", catfile($smdir, "smdh.pem"),
  558. "-in", "{output}.cms", "-out", "{output}.txt" ],
  559. \&final_compare
  560. ]
  561. );
  562. my @smime_cms_param_tests_autodigestmax = (
  563. [ "signed content test streaming PEM format, RSA keys, PSS signature, saltlen=auto-digestmax, digestsize < maximum salt length",
  564. [ "{cmd1}", @prov, "-sign", "-in", $smcont, "-outform", "PEM", "-nodetach",
  565. "-signer", $smrsa1, "-md", "sha256",
  566. "-keyopt", "rsa_padding_mode:pss", "-keyopt", "rsa_pss_saltlen:auto-digestmax",
  567. "-out", "{output}.cms" ],
  568. # digest is SHA-256, which produces 32, bytes of output
  569. sub { my %opts = @_; rsapssSaltlen("$opts{output}.cms") == 32; },
  570. [ "{cmd2}", @defaultprov, "-verify", "-in", "{output}.cms", "-inform", "PEM",
  571. "-CAfile", $smroot, "-out", "{output}.txt" ],
  572. \&final_compare
  573. ],
  574. [ "signed content test streaming PEM format, RSA keys, PSS signature, saltlen=auto-digestmax, digestsize > maximum salt length",
  575. [ "{cmd1}", @defaultprov, "-sign", "-in", $smcont, "-outform", "PEM", "-nodetach",
  576. "-signer", $smrsa1024, "-md", "sha512",
  577. "-keyopt", "rsa_padding_mode:pss", "-keyopt", "rsa_pss_saltlen:auto-digestmax",
  578. "-out", "{output}.cms" ],
  579. # digest is SHA-512, which produces 64, bytes of output, but an RSA-PSS
  580. # signature with a 1024 bit RSA key can only accommodate 62
  581. sub { my %opts = @_; rsapssSaltlen("$opts{output}.cms") == 62; },
  582. [ "{cmd2}", @defaultprov, "-verify", "-in", "{output}.cms", "-inform", "PEM",
  583. "-CAfile", $smroot, "-out", "{output}.txt" ],
  584. \&final_compare
  585. ]
  586. );
  587. my @contenttype_cms_test = (
  588. [ "signed content test - check that content type is added to additional signerinfo, RSA keys",
  589. [ "{cmd1}", @prov, "-sign", "-binary", "-nodetach", "-stream", "-in", $smcont,
  590. "-outform", "DER", "-signer", $smrsa1, "-md", "SHA256",
  591. "-out", "{output}.cms" ],
  592. [ "{cmd1}", @prov, "-resign", "-binary", "-nodetach", "-in", "{output}.cms",
  593. "-inform", "DER", "-outform", "DER",
  594. "-signer", catfile($smdir, "smrsa2.pem"), "-md", "SHA256",
  595. "-out", "{output}2.cms" ],
  596. sub { my %opts = @_; contentType_matches("$opts{output}2.cms") == 2; },
  597. [ "{cmd2}", @prov, "-verify", "-in", "{output}2.cms", "-inform", "DER",
  598. "-CAfile", $smroot, "-out", "{output}.txt" ]
  599. ],
  600. );
  601. my @incorrect_attribute_cms_test = (
  602. "bad_signtime_attr.cms",
  603. "no_ct_attr.cms",
  604. "no_md_attr.cms",
  605. "ct_multiple_attr.cms"
  606. );
  607. # Runs a standard loop on the input array
  608. sub runner_loop {
  609. my %opts = ( @_ );
  610. my $cnt1 = 0;
  611. foreach (@{$opts{tests}}) {
  612. $cnt1++;
  613. $opts{output} = "$opts{prefix}-$cnt1";
  614. SKIP: {
  615. my $skip_reason = check_availability($$_[0]);
  616. skip $skip_reason, 1 if $skip_reason;
  617. my $ok = 1;
  618. 1 while unlink "$opts{output}.txt";
  619. foreach (@$_[1..$#$_]) {
  620. if (ref $_ eq 'CODE') {
  621. $ok &&= $_->(%opts);
  622. } else {
  623. my @cmd = map {
  624. my $x = $_;
  625. while ($x =~ /\{([^\}]+)\}/) {
  626. $x = $`.$opts{$1}.$' if exists $opts{$1};
  627. }
  628. $x;
  629. } @$_;
  630. diag "CMD: openssl ", join(" ", @cmd);
  631. $ok &&= run(app(["openssl", @cmd]));
  632. $opts{input} = $opts{output};
  633. }
  634. }
  635. ok($ok, $$_[0]);
  636. }
  637. }
  638. }
  639. sub final_compare {
  640. my %opts = @_;
  641. diag "Comparing $smcont with $opts{output}.txt";
  642. return compare_text($smcont, "$opts{output}.txt") == 0;
  643. }
  644. sub zero_compare {
  645. my %opts = @_;
  646. diag "Checking for zero-length file";
  647. return (-e "$opts{output}.txt" && -z "$opts{output}.txt");
  648. }
  649. subtest "CMS => PKCS#7 compatibility tests\n" => sub {
  650. plan tests => scalar @smime_pkcs7_tests;
  651. runner_loop(prefix => 'cms2pkcs7', cmd1 => 'cms', cmd2 => 'smime',
  652. tests => [ @smime_pkcs7_tests ]);
  653. };
  654. subtest "CMS <= PKCS#7 compatibility tests\n" => sub {
  655. plan tests => scalar @smime_pkcs7_tests;
  656. runner_loop(prefix => 'pkcs72cms', cmd1 => 'smime', cmd2 => 'cms',
  657. tests => [ @smime_pkcs7_tests ]);
  658. };
  659. subtest "CMS <=> CMS consistency tests\n" => sub {
  660. plan tests => (scalar @smime_pkcs7_tests) + (scalar @smime_cms_tests);
  661. runner_loop(prefix => 'cms2cms-1', cmd1 => 'cms', cmd2 => 'cms',
  662. tests => [ @smime_pkcs7_tests ]);
  663. runner_loop(prefix => 'cms2cms-2', cmd1 => 'cms', cmd2 => 'cms',
  664. tests => [ @smime_cms_tests ]);
  665. };
  666. subtest "CMS <=> CMS consistency tests, modified key parameters\n" => sub {
  667. plan tests =>
  668. (scalar @smime_cms_param_tests) + (scalar @smime_cms_comp_tests) +
  669. (scalar @smime_cms_param_tests_autodigestmax) + 1;
  670. ok(run(app(["openssl", "cms", @prov,
  671. "-sign", "-in", $smcont,
  672. "-outform", "PEM",
  673. "-nodetach",
  674. "-signer", $smrsa1,
  675. "-keyopt", "rsa_padding_mode:pss",
  676. "-keyopt", "rsa_pss_saltlen:auto-digestmax",
  677. "-out", "digestmaxtest.cms"])));
  678. # Providers that do not support rsa_pss_saltlen:auto-digestmax will parse
  679. # it as 0
  680. my $no_autodigestmax = rsapssSaltlen("digestmaxtest.cms") == 0;
  681. 1 while unlink "digestmaxtest.cms";
  682. runner_loop(prefix => 'cms2cms-mod', cmd1 => 'cms', cmd2 => 'cms',
  683. tests => [ @smime_cms_param_tests ]);
  684. SKIP: {
  685. skip("Zlib not supported: compression tests skipped",
  686. scalar @smime_cms_comp_tests)
  687. if $no_zlib;
  688. runner_loop(prefix => 'cms2cms-comp', cmd1 => 'cms', cmd2 => 'cms',
  689. tests => [ @smime_cms_comp_tests ]);
  690. }
  691. SKIP: {
  692. skip("rsa_pss_saltlen:auto-digestmax not supported",
  693. scalar @smime_cms_param_tests_autodigestmax)
  694. if $no_autodigestmax;
  695. runner_loop(prefix => 'cms2cms-comp', 'cmd1' => 'cms', cmd2 => 'cms',
  696. tests => [ @smime_cms_param_tests_autodigestmax ]);
  697. }
  698. };
  699. # Returns the number of matches of a Content Type Attribute in a binary file.
  700. sub contentType_matches {
  701. # Read in a binary file
  702. my ($in) = @_;
  703. open (HEX_IN, "$in") or die("open failed for $in : $!");
  704. binmode(HEX_IN);
  705. local $/;
  706. my $str = <HEX_IN>;
  707. # Find ASN1 data for a Content Type Attribute (with a OID of PKCS7 data)
  708. 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;
  709. close(HEX_IN);
  710. return scalar(@c);
  711. }
  712. sub rsapssSaltlen {
  713. my ($in) = @_;
  714. my $exit = 0;
  715. my @asn1parse = run(app(["openssl", "asn1parse", "-in", $in, "-dump"]),
  716. capture => 1,
  717. statusvar => $exit);
  718. return -1 if $exit != 0;
  719. my $pssparam_offset = -1;
  720. while ($_ = shift @asn1parse) {
  721. chomp;
  722. next unless /:rsassaPss/;
  723. # This line contains :rsassaPss, the next line contains a raw dump of the
  724. # RSA_PSS_PARAMS sequence; obtain its offset
  725. $_ = shift @asn1parse;
  726. if (/^\s*(\d+):/) {
  727. $pssparam_offset = int($1);
  728. }
  729. }
  730. if ($pssparam_offset == -1) {
  731. note "Failed to determine RSA_PSS_PARAM offset in CMS. " +
  732. "Was the file correctly signed with RSASSA-PSS?";
  733. return -1;
  734. }
  735. my @pssparam = run(app(["openssl", "asn1parse", "-in", $in,
  736. "-strparse", $pssparam_offset]),
  737. capture => 1,
  738. statusvar => $exit);
  739. return -1 if $exit != 0;
  740. my $saltlen = -1;
  741. # Can't use asn1parse -item RSA_PSS_PARAMS here, because that's deprecated.
  742. # This assumes the salt length is the last field, which may possibly be
  743. # incorrect if there is a non-standard trailer field, but there almost never
  744. # is in PSS.
  745. if ($pssparam[-1] =~ /prim:\s+INTEGER\s+:([A-Fa-f0-9]+)/) {
  746. $saltlen = hex($1);
  747. }
  748. if ($saltlen == -1) {
  749. note "Failed to determine salt length from RSA_PSS_PARAM struct. " +
  750. "Was the file correctly signed with RSASSA-PSS?";
  751. return -1;
  752. }
  753. return $saltlen;
  754. }
  755. subtest "CMS Check the content type attribute is added for additional signers\n" => sub {
  756. plan tests => (scalar @contenttype_cms_test);
  757. runner_loop(prefix => 'cms2cms-added', cmd1 => 'cms', cmd2 => 'cms',
  758. tests => [ @contenttype_cms_test ]);
  759. };
  760. subtest "CMS Check that bad attributes fail when verifying signers\n" => sub {
  761. plan tests =>
  762. (scalar @incorrect_attribute_cms_test);
  763. my $cnt = 0;
  764. foreach my $name (@incorrect_attribute_cms_test) {
  765. my $out = "incorrect-$cnt.txt";
  766. ok(!run(app(["openssl", "cms", @prov, "-verify", "-in",
  767. catfile($datadir, $name), "-inform", "DER", "-CAfile",
  768. $smroot, "-out", $out ])),
  769. $name);
  770. }
  771. };
  772. subtest "CMS Check that bad encryption algorithm fails\n" => sub {
  773. plan tests => 1;
  774. SKIP: {
  775. skip "DES or Legacy isn't supported in this build", 1
  776. if disabled("des") || disabled("legacy");
  777. my $out = "smtst.txt";
  778. ok(!run(app(["openssl", "cms", @legacyprov, "-encrypt",
  779. "-in", $smcont,
  780. "-stream", "-recip", $smrsa1,
  781. "-des-ede3",
  782. "-out", $out ])),
  783. "Decrypt message from OpenSSL 1.1.1");
  784. }
  785. };
  786. subtest "CMS Decrypt message encrypted with OpenSSL 1.1.1\n" => sub {
  787. plan tests => 1;
  788. SKIP: {
  789. skip "EC or DES isn't supported in this build", 1
  790. if disabled("ec") || disabled("des");
  791. my $out = "smtst.txt";
  792. ok(run(app(["openssl", "cms", @defaultprov, "-decrypt",
  793. "-inkey", catfile($smdir, "smec3.pem"),
  794. "-in", catfile($datadir, "ciphertext_from_1_1_1.cms"),
  795. "-out", $out ]))
  796. && compare_text($smcont, $out) == 0,
  797. "Decrypt message from OpenSSL 1.1.1");
  798. }
  799. };
  800. subtest "CAdES <=> CAdES consistency tests\n" => sub {
  801. plan tests => (scalar @smime_cms_cades_tests);
  802. runner_loop(prefix => 'cms-cades', cmd1 => 'cms', cmd2 => 'cms',
  803. tests => [ @smime_cms_cades_tests ]);
  804. };
  805. subtest "CAdES; cms incompatible arguments tests\n" => sub {
  806. plan tests => (scalar @smime_cms_cades_invalid_option_tests);
  807. foreach (@smime_cms_cades_invalid_option_tests) {
  808. ok(!run(app(["openssl", "cms", @{$$_[0]} ] )));
  809. }
  810. };
  811. subtest "CAdES ko tests\n" => sub {
  812. plan tests => 2 * scalar @smime_cms_cades_ko_tests;
  813. foreach (@smime_cms_cades_ko_tests) {
  814. SKIP: {
  815. my $skip_reason = check_availability($$_[0]);
  816. skip $skip_reason, 1 if $skip_reason;
  817. 1 while unlink "cades-ko.txt";
  818. ok(run(app(["openssl", "cms", @{$$_[1]}])), $$_[0]);
  819. ok(!run(app(["openssl", "cms", @{$$_[3]}])), $$_[2]);
  820. }
  821. }
  822. };
  823. subtest "CMS binary input tests\n" => sub {
  824. my $input = srctop_file("test", "smcont.bin");
  825. my $signed = "smcont.signed";
  826. my $verified = "smcont.verified";
  827. plan tests => 11;
  828. ok(run(app(["openssl", "cms", "-sign", "-md", "sha256", "-signer", $smrsa1,
  829. "-binary", "-in", $input, "-out", $signed])),
  830. "sign binary input with -binary");
  831. ok(run(app(["openssl", "cms", "-verify", "-CAfile", $smroot,
  832. "-binary", "-in", $signed, "-out", $verified])),
  833. "verify binary input with -binary");
  834. is(compare($input, $verified), 0, "binary input retained with -binary");
  835. ok(run(app(["openssl", "cms", "-sign", "-md", "sha256", "-signer", $smrsa1,
  836. "-in", $input, "-out", $signed.".nobin"])),
  837. "sign binary input without -binary");
  838. ok(run(app(["openssl", "cms", "-verify", "-CAfile", $smroot,
  839. "-in", $signed.".nobin", "-out", $verified.".nobin"])),
  840. "verify binary input without -binary");
  841. is(compare($input, $verified.".nobin"), 1, "binary input not retained without -binary");
  842. ok(!run(app(["openssl", "cms", "-verify", "-CAfile", $smroot, "-crlfeol",
  843. "-binary", "-in", $signed, "-out", $verified.".crlfeol"])),
  844. "verify binary input wrong crlfeol");
  845. ok(run(app(["openssl", "cms", "-sign", "-md", "sha256", "-signer", $smrsa1,
  846. "-crlfeol",
  847. "-binary", "-in", $input, "-out", $signed.".crlf"])),
  848. "sign binary input with -binary -crlfeol");
  849. ok(run(app(["openssl", "cms", "-verify", "-CAfile", $smroot, "-crlfeol",
  850. "-binary", "-in", $signed.".crlf", "-out", $verified.".crlf"])),
  851. "verify binary input with -binary -crlfeol");
  852. is(compare($input, $verified.".crlf"), 0,
  853. "binary input retained with -binary -crlfeol");
  854. ok(!run(app(["openssl", "cms", "-verify", "-CAfile", $smroot,
  855. "-binary", "-in", $signed.".crlf", "-out", $verified.".crlf2"])),
  856. "verify binary input with -binary missing -crlfeol");
  857. };
  858. subtest "CMS signed digest, DER format" => sub {
  859. plan tests => 2;
  860. # Pre-computed SHA256 digest of $smcont in hexadecimal form
  861. my $digest = "ff236ef61b396355f75a4cc6e1c306d4c309084ae271a9e2ad6888f10a101b32";
  862. my $sig_file = "signature.der";
  863. ok(run(app(["openssl", "cms", @prov, "-sign", "-digest", $digest,
  864. "-outform", "DER",
  865. "-certfile", catfile($smdir, "smroot.pem"),
  866. "-signer", catfile($smdir, "smrsa1.pem"),
  867. "-out", $sig_file])),
  868. "CMS sign pre-computed digest, DER format");
  869. ok(run(app(["openssl", "cms", @prov, "-verify", "-in", $sig_file,
  870. "-inform", "DER",
  871. "-CAfile", catfile($smdir, "smroot.pem"),
  872. "-content", $smcont])),
  873. "Verify CMS signed digest, DER format");
  874. };
  875. subtest "CMS signed digest, S/MIME format" => sub {
  876. plan tests => 2;
  877. # Pre-computed SHA256 digest of $smcont in hexadecimal form
  878. my $digest = "ff236ef61b396355f75a4cc6e1c306d4c309084ae271a9e2ad6888f10a101b32";
  879. my $sig_file = "signature.smime";
  880. ok(run(app(["openssl", "cms", @prov, "-sign", "-digest", $digest,
  881. "-outform", "SMIME",
  882. "-certfile", catfile($smdir, "smroot.pem"),
  883. "-signer", catfile($smdir, "smrsa1.pem"),
  884. "-out", $sig_file])),
  885. "CMS sign pre-computed digest, S/MIME format");
  886. ok(run(app(["openssl", "cms", @prov, "-verify", "-in", $sig_file,
  887. "-inform", "SMIME",
  888. "-CAfile", catfile($smdir, "smroot.pem"),
  889. "-content", $smcont])),
  890. "Verify CMS signed digest, S/MIME format");
  891. };
  892. subtest "CMS code signing test" => sub {
  893. plan tests => 7;
  894. my $sig_file = "signature.p7s";
  895. ok(run(app(["openssl", "cms", @prov, "-sign", "-in", $smcont,
  896. "-certfile", catfile($smdir, "smroot.pem"),
  897. "-signer", catfile($smdir, "smrsa1.pem"),
  898. "-out", $sig_file])),
  899. "accept perform CMS signature with smime certificate");
  900. ok(run(app(["openssl", "cms", @prov, "-verify", "-in", $sig_file,
  901. "-CAfile", catfile($smdir, "smroot.pem"),
  902. "-content", $smcont])),
  903. "accept verify CMS signature with smime certificate");
  904. ok(!run(app(["openssl", "cms", @prov, "-verify", "-in", $sig_file,
  905. "-CAfile", catfile($smdir, "smroot.pem"),
  906. "-purpose", "codesign",
  907. "-content", $smcont])),
  908. "fail verify CMS signature with smime certificate for purpose code signing");
  909. ok(!run(app(["openssl", "cms", @prov, "-verify", "-in", $sig_file,
  910. "-CAfile", catfile($smdir, "smroot.pem"),
  911. "-purpose", "football",
  912. "-content", $smcont])),
  913. "fail verify CMS signature with invalid purpose argument");
  914. ok(run(app(["openssl", "cms", @prov, "-sign", "-in", $smcont,
  915. "-certfile", catfile($smdir, "smroot.pem"),
  916. "-signer", catfile($smdir, "csrsa1.pem"),
  917. "-out", $sig_file])),
  918. "accept perform CMS signature with code signing certificate");
  919. ok(run(app(["openssl", "cms", @prov, "-verify", "-in", $sig_file,
  920. "-CAfile", catfile($smdir, "smroot.pem"),
  921. "-purpose", "codesign",
  922. "-content", $smcont])),
  923. "accept verify CMS signature with code signing certificate for purpose code signing");
  924. ok(!run(app(["openssl", "cms", @prov, "-verify", "-in", $sig_file,
  925. "-CAfile", catfile($smdir, "smroot.pem"),
  926. "-content", $smcont])),
  927. "fail verify CMS signature with code signing certificate for purpose smime_sign");
  928. };
  929. # Test case for missing MD algorithm (must not segfault)
  930. with({ exit_checker => sub { return shift == 4; } },
  931. sub {
  932. ok(run(app(['openssl', 'smime', '-verify', '-noverify',
  933. '-inform', 'PEM',
  934. '-in', data_file("pkcs7-md4.pem"),
  935. ])),
  936. "Check failure of EVP_DigestInit in PKCS7 signed is handled");
  937. ok(run(app(['openssl', 'smime', '-decrypt',
  938. '-inform', 'PEM',
  939. '-in', data_file("pkcs7-md4-encrypted.pem"),
  940. '-recip', srctop_file("test", "certs", "ee-cert.pem"),
  941. '-inkey', srctop_file("test", "certs", "ee-key.pem")
  942. ])),
  943. "Check failure of EVP_DigestInit in PKCS7 signedAndEnveloped is handled");
  944. });
  945. sub check_availability {
  946. my $tnam = shift;
  947. return "$tnam: skipped, EC disabled\n"
  948. if ($no_ec && $tnam =~ /ECDH/);
  949. return "$tnam: skipped, ECDH disabled\n"
  950. if ($no_ec && $tnam =~ /ECDH/);
  951. return "$tnam: skipped, EC2M disabled\n"
  952. if ($no_ec2m && $tnam =~ /K-283/);
  953. return "$tnam: skipped, DH disabled\n"
  954. if ($no_dh && $tnam =~ /X9\.42/);
  955. return "$tnam: skipped, RC2 disabled\n"
  956. if ($no_rc2 && $tnam =~ /RC2/);
  957. return "$tnam: skipped, DES disabled\n"
  958. if ($no_des && $tnam =~ /DES/);
  959. return "$tnam: skipped, DSA disabled\n"
  960. if ($no_dsa && $tnam =~ / DSA/);
  961. return "";
  962. }
  963. # Test case for the locking problem reported in #19643.
  964. # This will fail if the fix is in and deadlock on Windows (and possibly
  965. # other platforms) if not.
  966. ok(!run(app(['openssl', 'cms', '-verify',
  967. '-CAfile', srctop_file("test/certs", "pkitsta.pem"),
  968. '-policy', 'anyPolicy',
  969. '-in', srctop_file("test/smime-eml",
  970. "SignedInvalidMappingFromanyPolicyTest7.eml")
  971. ])),
  972. "issue#19643");
  973. # Check that we get the expected failure return code
  974. with({ exit_checker => sub { return shift == 6; } },
  975. sub {
  976. ok(run(app(['openssl', 'cms', '-encrypt',
  977. '-in', srctop_file("test", "smcont.txt"),
  978. '-aes128', '-stream', '-recip',
  979. srctop_file("test/smime-certs", "badrsa.pem"),
  980. ])),
  981. "Check failure during BIO setup with -stream is handled correctly");
  982. });
  983. # Test case for return value mis-check reported in #21986
  984. with({ exit_checker => sub { return shift == 3; } },
  985. sub {
  986. SKIP: {
  987. skip "DSA is not supported in this build", 1 if $no_dsa;
  988. ok(run(app(['openssl', 'cms', '-sign',
  989. '-in', srctop_file("test", "smcont.txt"),
  990. '-signer', srctop_file("test/smime-certs", "smdsa1.pem"),
  991. '-md', 'SHAKE256'])),
  992. "issue#21986");
  993. }
  994. });
  995. # Test for problem reported in #22225
  996. with({ exit_checker => sub { return shift == 3; } },
  997. sub {
  998. ok(run(app(['openssl', 'cms', '-encrypt',
  999. '-in', srctop_file("test", "smcont.txt"),
  1000. '-aes-256-ctr', '-recip',
  1001. catfile($smdir, "smec1.pem"),
  1002. ])),
  1003. "Check for failure when cipher does not have an assigned OID (issue#22225)");
  1004. });
  1005. # Test encrypt to three recipients, and decrypt using key-only;
  1006. # i.e. do not follow the recommended practice of providing the
  1007. # recipient cert in the decrypt op.
  1008. #
  1009. # Use RSAES-OAEP for key-transport, not RSAES-PKCS-v1_5.
  1010. #
  1011. # Because the cert is not provided during decrypt, all RSA ciphertexts
  1012. # are decrypted in turn, and when/if there is a valid decryption, it
  1013. # is assumed the correct content-key has been recovered.
  1014. #
  1015. # That process may fail with RSAES-PKCS-v1_5 b/c there is a
  1016. # non-negligible chance that decrypting a random input using
  1017. # RSAES-PKCS-v1_5 can result in a valid plaintext (so two content-keys
  1018. # could be recovered and the wrong one might be used).
  1019. #
  1020. # See https://github.com/openssl/project/issues/380
  1021. subtest "encrypt to three recipients with RSA-OAEP, key only decrypt" => sub {
  1022. plan tests => 3;
  1023. my $pt = srctop_file("test", "smcont.txt");
  1024. my $ct = "smtst.cms";
  1025. my $ptpt = "smtst.txt";
  1026. ok(run(app(['openssl', 'cms',
  1027. @defaultprov,
  1028. '-encrypt', '-aes128',
  1029. '-in', $pt,
  1030. '-out', $ct,
  1031. '-stream',
  1032. '-recip', catfile($smdir, "smrsa1.pem"),
  1033. '-keyopt', 'rsa_padding_mode:oaep',
  1034. '-recip', catfile($smdir, "smrsa2.pem"),
  1035. '-keyopt', 'rsa_padding_mode:oaep',
  1036. '-recip', catfile($smdir, "smrsa3-cert.pem"),
  1037. '-keyopt', 'rsa_padding_mode:oaep',
  1038. ])),
  1039. "encrypt to three recipients with RSA-OAEP (avoid openssl/project issue#380)");
  1040. ok(run(app(['openssl', 'cms',
  1041. @defaultprov,
  1042. '-decrypt', '-aes128',
  1043. '-in', $ct,
  1044. '-out', $ptpt,
  1045. '-inkey', catfile($smdir, "smrsa3-key.pem"),
  1046. ])),
  1047. "decrypt with key only");
  1048. is(compare($pt, $ptpt), 0, "compare original message with decrypted ciphertext");
  1049. };