25-test_verify.t 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591
  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 File::Spec::Functions qw/canonpath/;
  11. use File::Copy;
  12. use OpenSSL::Test qw/:DEFAULT srctop_file bldtop_dir ok_nofips with/;
  13. use OpenSSL::Test::Utils;
  14. setup("test_verify");
  15. sub verify {
  16. my ($cert, $purpose, $trusted, $untrusted, @opts) = @_;
  17. my @path = qw(test certs);
  18. my @args = qw(openssl verify -auth_level 1);
  19. push(@args, "-purpose", $purpose) if $purpose ne "";
  20. push(@args, @opts);
  21. for (@$trusted) { push(@args, "-trusted", srctop_file(@path, "$_.pem")) }
  22. for (@$untrusted) { push(@args, "-untrusted", srctop_file(@path, "$_.pem")) }
  23. push(@args, srctop_file(@path, "$cert.pem"));
  24. run(app([@args]));
  25. }
  26. plan tests => 193;
  27. # Canonical success
  28. ok(verify("ee-cert", "sslserver", ["root-cert"], ["ca-cert"]),
  29. "accept compat trust");
  30. # Root CA variants
  31. ok(!verify("ee-cert", "sslserver", [qw(root-nonca)], [qw(ca-cert)]),
  32. "fail trusted non-ca root");
  33. ok(!verify("ee-cert", "sslserver", [qw(nroot+serverAuth)], [qw(ca-cert)]),
  34. "fail server trust non-ca root");
  35. ok(!verify("ee-cert", "sslserver", [qw(nroot+anyEKU)], [qw(ca-cert)]),
  36. "fail wildcard trust non-ca root");
  37. ok(!verify("ee-cert", "sslserver", [qw(root-cert2)], [qw(ca-cert)]),
  38. "fail wrong root key");
  39. ok(!verify("ee-cert", "sslserver", [qw(root-name2)], [qw(ca-cert)]),
  40. "fail wrong root DN");
  41. # Critical extensions
  42. ok(verify("ee-cert-noncrit-unknown-ext", "", ["root-cert"], ["ca-cert"]),
  43. "accept non-critical unknown extension");
  44. ok(!verify("ee-cert-crit-unknown-ext", "", ["root-cert"], ["ca-cert"]),
  45. "reject critical unknown extension");
  46. ok(verify("ee-cert-ocsp-nocheck", "", ["root-cert"], ["ca-cert"]),
  47. "accept critical OCSP No Check");
  48. # Explicit trust/purpose combinations
  49. #
  50. ok(verify("ee-cert", "sslserver", [qw(sroot-cert)], [qw(ca-cert)]),
  51. "accept server purpose");
  52. ok(!verify("ee-cert", "sslserver", [qw(croot-cert)], [qw(ca-cert)]),
  53. "fail client purpose");
  54. ok(verify("ee-cert", "sslserver", [qw(root+serverAuth)], [qw(ca-cert)]),
  55. "accept server trust");
  56. ok(verify("ee-cert", "sslserver", [qw(sroot+serverAuth)], [qw(ca-cert)]),
  57. "accept server trust with server purpose");
  58. ok(verify("ee-cert", "sslserver", [qw(croot+serverAuth)], [qw(ca-cert)]),
  59. "accept server trust with client purpose");
  60. # Wildcard trust
  61. ok(verify("ee-cert", "sslserver", [qw(root+anyEKU)], [qw(ca-cert)]),
  62. "accept wildcard trust");
  63. ok(verify("ee-cert", "sslserver", [qw(sroot+anyEKU)], [qw(ca-cert)]),
  64. "accept wildcard trust with server purpose");
  65. ok(verify("ee-cert", "sslserver", [qw(croot+anyEKU)], [qw(ca-cert)]),
  66. "accept wildcard trust with client purpose");
  67. # Inapplicable mistrust
  68. ok(verify("ee-cert", "sslserver", [qw(root-clientAuth)], [qw(ca-cert)]),
  69. "accept client mistrust");
  70. ok(verify("ee-cert", "sslserver", [qw(sroot-clientAuth)], [qw(ca-cert)]),
  71. "accept client mistrust with server purpose");
  72. ok(!verify("ee-cert", "sslserver", [qw(croot-clientAuth)], [qw(ca-cert)]),
  73. "fail client mistrust with client purpose");
  74. # Inapplicable trust
  75. ok(!verify("ee-cert", "sslserver", [qw(root+clientAuth)], [qw(ca-cert)]),
  76. "fail client trust");
  77. ok(!verify("ee-cert", "sslserver", [qw(sroot+clientAuth)], [qw(ca-cert)]),
  78. "fail client trust with server purpose");
  79. ok(!verify("ee-cert", "sslserver", [qw(croot+clientAuth)], [qw(ca-cert)]),
  80. "fail client trust with client purpose");
  81. # Server mistrust
  82. ok(!verify("ee-cert", "sslserver", [qw(root-serverAuth)], [qw(ca-cert)]),
  83. "fail rejected EKU");
  84. ok(!verify("ee-cert", "sslserver", [qw(sroot-serverAuth)], [qw(ca-cert)]),
  85. "fail server mistrust with server purpose");
  86. ok(!verify("ee-cert", "sslserver", [qw(croot-serverAuth)], [qw(ca-cert)]),
  87. "fail server mistrust with client purpose");
  88. # Wildcard mistrust
  89. ok(!verify("ee-cert", "sslserver", [qw(root-anyEKU)], [qw(ca-cert)]),
  90. "fail wildcard mistrust");
  91. ok(!verify("ee-cert", "sslserver", [qw(sroot-anyEKU)], [qw(ca-cert)]),
  92. "fail wildcard mistrust with server purpose");
  93. ok(!verify("ee-cert", "sslserver", [qw(croot-anyEKU)], [qw(ca-cert)]),
  94. "fail wildcard mistrust with client purpose");
  95. # Check that trusted-first is on by setting up paths to different roots
  96. # depending on whether the intermediate is the trusted or untrusted one.
  97. #
  98. ok(verify("ee-cert", "sslserver", [qw(root-serverAuth root-cert2 ca-root2)],
  99. [qw(ca-cert)]),
  100. "accept trusted-first path");
  101. ok(verify("ee-cert", "sslserver", [qw(root-cert root2+serverAuth ca-root2)],
  102. [qw(ca-cert)]),
  103. "accept trusted-first path with server trust");
  104. ok(!verify("ee-cert", "sslserver", [qw(root-cert root2-serverAuth ca-root2)],
  105. [qw(ca-cert)]),
  106. "fail trusted-first path with server mistrust");
  107. ok(!verify("ee-cert", "sslserver", [qw(root-cert root2+clientAuth ca-root2)],
  108. [qw(ca-cert)]),
  109. "fail trusted-first path with client trust");
  110. # CA variants
  111. ok(!verify("ee-cert", "sslserver", [qw(root-cert)], [qw(ca-nonca)]),
  112. "fail non-CA untrusted intermediate");
  113. ok(!verify("ee-cert", "sslserver", [qw(root-cert)], [qw(ca-nonbc)]),
  114. "fail non-CA untrusted intermediate");
  115. ok(!verify("ee-cert", "sslserver", [qw(root-cert ca-nonca)], []),
  116. "fail non-CA trust-store intermediate");
  117. ok(!verify("ee-cert", "sslserver", [qw(root-cert ca-nonbc)], []),
  118. "fail non-CA trust-store intermediate");
  119. ok(!verify("ee-cert", "sslserver", [qw(root-cert nca+serverAuth)], []),
  120. "fail non-CA server trust intermediate");
  121. ok(!verify("ee-cert", "sslserver", [qw(root-cert nca+anyEKU)], []),
  122. "fail non-CA wildcard trust intermediate");
  123. ok(!verify("ee-cert", "sslserver", [qw(root-cert)], [qw(ca-cert2)]),
  124. "fail wrong intermediate CA key");
  125. ok(!verify("ee-cert", "sslserver", [qw(root-cert)], [qw(ca-name2)]),
  126. "fail wrong intermediate CA DN");
  127. ok(!verify("ee-cert", "sslserver", [qw(root-cert)], [qw(ca-root2)]),
  128. "fail wrong intermediate CA issuer");
  129. ok(!verify("ee-cert", "sslserver", [], [qw(ca-cert)], "-partial_chain"),
  130. "fail untrusted partial chain");
  131. ok(verify("ee-cert", "sslserver", [qw(ca-cert)], [], "-partial_chain"),
  132. "accept trusted partial chain");
  133. ok(!verify("ee-cert", "sslserver", [qw(ca-expired)], [], "-partial_chain"),
  134. "reject expired trusted partial chain"); # this check is beyond RFC 5280
  135. ok(!verify("ee-cert", "sslserver", [qw(root-expired)], [qw(ca-cert)]),
  136. "reject expired trusted root"); # this check is beyond RFC 5280
  137. ok(verify("ee-cert", "sslserver", [qw(sca-cert)], [], "-partial_chain"),
  138. "accept partial chain with server purpose");
  139. ok(!verify("ee-cert", "sslserver", [qw(cca-cert)], [], "-partial_chain"),
  140. "fail partial chain with client purpose");
  141. ok(verify("ee-cert", "sslserver", [qw(ca+serverAuth)], [], "-partial_chain"),
  142. "accept server trust partial chain");
  143. ok(verify("ee-cert", "sslserver", [qw(cca+serverAuth)], [], "-partial_chain"),
  144. "accept server trust client purpose partial chain");
  145. ok(verify("ee-cert", "sslserver", [qw(ca-clientAuth)], [], "-partial_chain"),
  146. "accept client mistrust partial chain");
  147. ok(verify("ee-cert", "sslserver", [qw(ca+anyEKU)], [], "-partial_chain"),
  148. "accept wildcard trust partial chain");
  149. ok(!verify("ee-cert", "sslserver", [], [qw(ca+serverAuth)], "-partial_chain"),
  150. "fail untrusted partial issuer with ignored server trust");
  151. ok(!verify("ee-cert", "sslserver", [qw(ca-serverAuth)], [], "-partial_chain"),
  152. "fail server mistrust partial chain");
  153. ok(!verify("ee-cert", "sslserver", [qw(ca+clientAuth)], [], "-partial_chain"),
  154. "fail client trust partial chain");
  155. ok(!verify("ee-cert", "sslserver", [qw(ca-anyEKU)], [], "-partial_chain"),
  156. "fail wildcard mistrust partial chain");
  157. # We now test auxiliary trust even for intermediate trusted certs without
  158. # -partial_chain. Note that "-trusted_first" is now always on and cannot
  159. # be disabled.
  160. ok(verify("ee-cert", "sslserver", [qw(root-cert ca+serverAuth)], [qw(ca-cert)]),
  161. "accept server trust");
  162. ok(verify("ee-cert", "sslserver", [qw(root-cert ca+anyEKU)], [qw(ca-cert)]),
  163. "accept wildcard trust");
  164. ok(verify("ee-cert", "sslserver", [qw(root-cert sca-cert)], [qw(ca-cert)]),
  165. "accept server purpose");
  166. ok(verify("ee-cert", "sslserver", [qw(root-cert sca+serverAuth)], [qw(ca-cert)]),
  167. "accept server trust and purpose");
  168. ok(verify("ee-cert", "sslserver", [qw(root-cert sca+anyEKU)], [qw(ca-cert)]),
  169. "accept wildcard trust and server purpose");
  170. ok(verify("ee-cert", "sslserver", [qw(root-cert sca-clientAuth)], [qw(ca-cert)]),
  171. "accept client mistrust and server purpose");
  172. ok(verify("ee-cert", "sslserver", [qw(root-cert cca+serverAuth)], [qw(ca-cert)]),
  173. "accept server trust and client purpose");
  174. ok(verify("ee-cert", "sslserver", [qw(root-cert cca+anyEKU)], [qw(ca-cert)]),
  175. "accept wildcard trust and client purpose");
  176. ok(!verify("ee-cert", "sslserver", [qw(root-cert cca-cert)], [qw(ca-cert)]),
  177. "fail client purpose");
  178. ok(!verify("ee-cert", "sslserver", [qw(root-cert ca-anyEKU)], [qw(ca-cert)]),
  179. "fail wildcard mistrust");
  180. ok(!verify("ee-cert", "sslserver", [qw(root-cert ca-serverAuth)], [qw(ca-cert)]),
  181. "fail server mistrust");
  182. ok(!verify("ee-cert", "sslserver", [qw(root-cert ca+clientAuth)], [qw(ca-cert)]),
  183. "fail client trust");
  184. ok(!verify("ee-cert", "sslserver", [qw(root-cert sca+clientAuth)], [qw(ca-cert)]),
  185. "fail client trust and server purpose");
  186. ok(!verify("ee-cert", "sslserver", [qw(root-cert cca+clientAuth)], [qw(ca-cert)]),
  187. "fail client trust and client purpose");
  188. ok(!verify("ee-cert", "sslserver", [qw(root-cert cca-serverAuth)], [qw(ca-cert)]),
  189. "fail server mistrust and client purpose");
  190. ok(!verify("ee-cert", "sslserver", [qw(root-cert cca-clientAuth)], [qw(ca-cert)]),
  191. "fail client mistrust and client purpose");
  192. ok(!verify("ee-cert", "sslserver", [qw(root-cert sca-serverAuth)], [qw(ca-cert)]),
  193. "fail server mistrust and server purpose");
  194. ok(!verify("ee-cert", "sslserver", [qw(root-cert sca-anyEKU)], [qw(ca-cert)]),
  195. "fail wildcard mistrust and server purpose");
  196. ok(!verify("ee-cert", "sslserver", [qw(root-cert cca-anyEKU)], [qw(ca-cert)]),
  197. "fail wildcard mistrust and client purpose");
  198. # EE variants
  199. ok(verify("ee-client", "sslclient", [qw(root-cert)], [qw(ca-cert)]),
  200. "accept client chain");
  201. ok(!verify("ee-client", "sslserver", [qw(root-cert)], [qw(ca-cert)]),
  202. "fail server leaf purpose");
  203. ok(!verify("ee-cert", "sslclient", [qw(root-cert)], [qw(ca-cert)]),
  204. "fail client leaf purpose");
  205. ok(!verify("ee-cert2", "sslserver", [qw(root-cert)], [qw(ca-cert)]),
  206. "fail wrong intermediate CA key");
  207. ok(!verify("ee-name2", "sslserver", [qw(root-cert)], [qw(ca-cert)]),
  208. "fail wrong intermediate CA DN");
  209. ok(!verify("ee-expired", "sslserver", [qw(root-cert)], [qw(ca-cert)]),
  210. "fail expired leaf");
  211. ok(verify("ee-cert", "sslserver", [qw(ee-cert)], [], "-partial_chain"),
  212. "accept last-resort direct leaf match");
  213. ok(verify("ee-client", "sslclient", [qw(ee-client)], [], "-partial_chain"),
  214. "accept last-resort direct leaf match");
  215. ok(!verify("ee-cert", "sslserver", [qw(ee-client)], [], "-partial_chain"),
  216. "fail last-resort direct leaf non-match");
  217. ok(verify("ee-cert", "sslserver", [qw(ee+serverAuth)], [], "-partial_chain"),
  218. "accept direct match with server trust");
  219. ok(!verify("ee-cert", "sslserver", [qw(ee-serverAuth)], [], "-partial_chain"),
  220. "fail direct match with server mistrust");
  221. ok(verify("ee-client", "sslclient", [qw(ee+clientAuth)], [], "-partial_chain"),
  222. "accept direct match with client trust");
  223. ok(!verify("ee-client", "sslclient", [qw(ee-clientAuth)], [], "-partial_chain"),
  224. "reject direct match with client mistrust");
  225. ok(verify("ee-pathlen", "sslserver", [qw(root-cert)], [qw(ca-cert)]),
  226. "accept non-ca with pathlen:0 by default");
  227. ok(!verify("ee-pathlen", "sslserver", [qw(root-cert)], [qw(ca-cert)], "-x509_strict"),
  228. "reject non-ca with pathlen:0 with strict flag");
  229. # EE veaiants wrt timestamp signing
  230. ok(verify("ee-timestampsign-CABforum", "timestampsign", [qw(root-cert)], [qw(ca-cert)]),
  231. "accept timestampsign according to CAB forum");
  232. ok(!verify("ee-timestampsign-CABforum-noncritxku", "timestampsign", [qw(root-cert)], [qw(ca-cert)]),
  233. "fail timestampsign according to CAB forum with extendedKeyUsage not critical");
  234. ok(!verify("ee-timestampsign-CABforum-serverauth", "timestampsign", [qw(root-cert)], [qw(ca-cert)]),
  235. "fail timestampsign according to CAB forum with serverAuth");
  236. ok(!verify("ee-timestampsign-CABforum-anyextkeyusage", "timestampsign", [qw(root-cert)], [qw(ca-cert)]),
  237. "fail timestampsign according to CAB forum with anyExtendedKeyUsage");
  238. ok(!verify("ee-timestampsign-CABforum-crlsign", "timestampsign", [qw(root-cert)], [qw(ca-cert)]),
  239. "fail timestampsign according to CAB forum with cRLSign");
  240. ok(!verify("ee-timestampsign-CABforum-keycertsign", "timestampsign", [qw(root-cert)], [qw(ca-cert)]),
  241. "fail timestampsign according to CAB forum with keyCertSign");
  242. ok(verify("ee-timestampsign-rfc3161", "timestampsign", [qw(root-cert)], [qw(ca-cert)]),
  243. "accept timestampsign according to RFC 3161");
  244. ok(!verify("ee-timestampsign-rfc3161-noncritxku", "timestampsign", [qw(root-cert)], [qw(ca-cert)]),
  245. "fail timestampsign according to RFC 3161 with extendedKeyUsage not critical");
  246. ok(verify("ee-timestampsign-rfc3161-digsig", "timestampsign", [qw(root-cert)], [qw(ca-cert)]),
  247. "accept timestampsign according to RFC 3161 with digitalSignature");
  248. # EE variants wrt code signing
  249. ok(verify("ee-codesign", "codesign", [qw(root-cert)], [qw(ca-cert)]),
  250. "accept codesign");
  251. ok(!verify("ee-codesign-serverauth", "codesign", [qw(root-cert)], [qw(ca-cert)]),
  252. "fail codesign with additional serverAuth");
  253. ok(!verify("ee-codesign-anyextkeyusage", "codesign", [qw(root-cert)], [qw(ca-cert)]),
  254. "fail codesign with additional anyExtendedKeyUsage");
  255. ok(!verify("ee-codesign-crlsign", "codesign", [qw(root-cert)], [qw(ca-cert)]),
  256. "fail codesign with additional cRLSign");
  257. ok(!verify("ee-codesign-keycertsign", "codesign", [qw(root-cert)], [qw(ca-cert)]),
  258. "fail codesign with additional keyCertSign");
  259. ok(!verify("ee-codesign-noncritical", "codesign", [qw(root-cert)], [qw(ca-cert)]),
  260. "fail codesign without critical KU");
  261. ok(!verify("ee-cert", "codesign", [qw(root-cert)], [qw(ca-cert)]),
  262. "fail sslserver as code sign");
  263. ok(!verify("ee-client", "codesign", [qw(root-cert)], [qw(ca-cert)]),
  264. "fail sslclient as codesign");
  265. ok(!verify("ee-timestampsign-CABforum", "codesign", [qw(root-cert)], [qw(ca-cert)]),
  266. "fail timestampsign according to CAB forum as codesign");
  267. ok(!verify("ee-timestampsign-rfc3161", "codesign", [qw(root-cert)], [qw(ca-cert)]),
  268. "fail timestampsign according to RFC 3161 as codesign");
  269. # Proxy certificates
  270. ok(!verify("pc1-cert", "sslclient", [qw(root-cert)], [qw(ee-client ca-cert)]),
  271. "fail to accept proxy cert without -allow_proxy_certs");
  272. ok(verify("pc1-cert", "sslclient", [qw(root-cert)], [qw(ee-client ca-cert)],
  273. "-allow_proxy_certs"),
  274. "accept proxy cert 1");
  275. ok(verify("pc2-cert", "sslclient", [qw(root-cert)], [qw(pc1-cert ee-client ca-cert)],
  276. "-allow_proxy_certs"),
  277. "accept proxy cert 2");
  278. ok(!verify("bad-pc3-cert", "sslclient", [qw(root-cert)], [qw(pc1-cert ee-client ca-cert)],
  279. "-allow_proxy_certs"),
  280. "fail proxy cert with incorrect subject");
  281. ok(!verify("bad-pc4-cert", "sslclient", [qw(root-cert)], [qw(pc1-cert ee-client ca-cert)],
  282. "-allow_proxy_certs"),
  283. "fail proxy cert with incorrect pathlen");
  284. ok(verify("pc5-cert", "sslclient", [qw(root-cert)], [qw(pc1-cert ee-client ca-cert)],
  285. "-allow_proxy_certs"),
  286. "accept proxy cert missing proxy policy");
  287. ok(!verify("pc6-cert", "sslclient", [qw(root-cert)], [qw(pc1-cert ee-client ca-cert)],
  288. "-allow_proxy_certs"),
  289. "failed proxy cert where last CN was added as a multivalue RDN component");
  290. # Security level tests
  291. ok(verify("ee-cert", "", ["root-cert"], ["ca-cert"], "-auth_level", "2"),
  292. "accept RSA 2048 chain at auth level 2");
  293. ok(!verify("ee-cert", "", ["root-cert"], ["ca-cert"], "-auth_level", "3"),
  294. "reject RSA 2048 root at auth level 3");
  295. ok(verify("ee-cert", "", ["root-cert-768"], ["ca-cert-768i"], "-auth_level", "0"),
  296. "accept RSA 768 root at auth level 0");
  297. ok(!verify("ee-cert", "", ["root-cert-768"], ["ca-cert-768i"]),
  298. "reject RSA 768 root at auth level 1");
  299. ok(verify("ee-cert-768i", "", ["root-cert"], ["ca-cert-768"], "-auth_level", "0"),
  300. "accept RSA 768 intermediate at auth level 0");
  301. ok(!verify("ee-cert-768i", "", ["root-cert"], ["ca-cert-768"]),
  302. "reject RSA 768 intermediate at auth level 1");
  303. ok(verify("ee-cert-768", "", ["root-cert"], ["ca-cert"], "-auth_level", "0"),
  304. "accept RSA 768 leaf at auth level 0");
  305. ok(!verify("ee-cert-768", "", ["root-cert"], ["ca-cert"]),
  306. "reject RSA 768 leaf at auth level 1");
  307. #
  308. ok(verify("ee-cert", "", ["root-cert-md5"], ["ca-cert"], "-auth_level", "2"),
  309. "accept md5 self-signed TA at auth level 2");
  310. ok(verify("ee-cert", "", ["ca-cert-md5-any"], [], "-auth_level", "2"),
  311. "accept md5 intermediate TA at auth level 2");
  312. ok(verify("ee-cert", "", ["root-cert"], ["ca-cert-md5"], "-auth_level", "0"),
  313. "accept md5 intermediate at auth level 0");
  314. ok(!verify("ee-cert", "", ["root-cert"], ["ca-cert-md5"]),
  315. "reject md5 intermediate at auth level 1");
  316. ok(verify("ee-cert-md5", "", ["root-cert"], ["ca-cert"], "-auth_level", "0"),
  317. "accept md5 leaf at auth level 0");
  318. ok(!verify("ee-cert-md5", "", ["root-cert"], ["ca-cert"]),
  319. "reject md5 leaf at auth level 1");
  320. # Explicit vs named curve tests
  321. SKIP: {
  322. skip "EC is not supported by this OpenSSL build", 7
  323. if disabled("ec");
  324. ok(!verify("ee-cert-ec-explicit", "", ["root-cert"],
  325. ["ca-cert-ec-named"]),
  326. "reject explicit curve leaf with named curve intermediate");
  327. ok(!verify("ee-cert-ec-named-explicit", "", ["root-cert"],
  328. ["ca-cert-ec-explicit"]),
  329. "reject named curve leaf with explicit curve intermediate");
  330. ok(verify("ee-cert-ec-named-named", "", ["root-cert"],
  331. ["ca-cert-ec-named"]),
  332. "accept named curve leaf with named curve intermediate");
  333. ok(verify("ee-cert-ec-sha3-224", "", ["root-cert"], ["ca-cert-ec-named"], ),
  334. "accept cert generated with EC and SHA3-224");
  335. ok(verify("ee-cert-ec-sha3-256", "", ["root-cert"], ["ca-cert-ec-named"], ),
  336. "accept cert generated with EC and SHA3-256");
  337. ok(verify("ee-cert-ec-sha3-384", "", ["root-cert"], ["ca-cert-ec-named"], ),
  338. "accept cert generated with EC and SHA3-384");
  339. ok(verify("ee-cert-ec-sha3-512", "", ["root-cert"], ["ca-cert-ec-named"], ),
  340. "accept cert generated with EC and SHA3-512");
  341. }
  342. # Same as above but with base provider used for decoding
  343. SKIP: {
  344. my $no_fips = disabled('fips') || ($ENV{NO_FIPS} // 0);
  345. my $provconf = srctop_file("test", "fips-and-base.cnf");
  346. my $provpath = bldtop_dir("providers");
  347. my @prov = ("-provider-path", $provpath);
  348. skip "EC is not supported or FIPS is disabled", 7
  349. if disabled("ec") || $no_fips;
  350. $ENV{OPENSSL_CONF} = $provconf;
  351. ok(verify("ee-cert-ec-sha3-224", "", ["root-cert"], ["ca-cert-ec-named"], @prov),
  352. "accept cert generated with EC and SHA3-224 w/fips");
  353. ok(verify("ee-cert-ec-sha3-256", "", ["root-cert"], ["ca-cert-ec-named"], @prov),
  354. "accept cert generated with EC and SHA3-256 w/fips");
  355. ok(verify("ee-cert-ec-sha3-384", "", ["root-cert"], ["ca-cert-ec-named"], @prov),
  356. "accept cert generated with EC and SHA3-384 w/fips");
  357. ok(verify("ee-cert-ec-sha3-512", "", ["root-cert"], ["ca-cert-ec-named"], @prov),
  358. "accept cert generated with EC and SHA3-512 w/fips");
  359. delete $ENV{OPENSSL_CONF};
  360. run(test(["fips_version_test", "-config", $provconf, ">3.0.0"]),
  361. capture => 1, statusvar => \my $exit);
  362. skip "FIPS provider version is too old", 3
  363. if !$exit;
  364. $ENV{OPENSSL_CONF} = $provconf;
  365. ok(!verify("ee-cert-ec-explicit", "", ["root-cert"],
  366. ["ca-cert-ec-named"], @prov),
  367. "reject explicit curve leaf with named curve intermediate w/fips");
  368. ok(!verify("ee-cert-ec-named-explicit", "", ["root-cert"],
  369. ["ca-cert-ec-explicit"], @prov),
  370. "reject named curve leaf with explicit curve intermediate w/fips");
  371. ok(verify("ee-cert-ec-named-named", "", ["root-cert"],
  372. ["ca-cert-ec-named"], @prov),
  373. "accept named curve leaf with named curve intermediate w/fips");
  374. delete $ENV{OPENSSL_CONF};
  375. }
  376. # Depth tests, note the depth limit bounds the number of CA certificates
  377. # between the trust-anchor and the leaf, so, for example, with a root->ca->leaf
  378. # chain, depth = 1 is sufficient, but depth == 0 is not.
  379. #
  380. ok(verify("ee-cert", "", ["root-cert"], ["ca-cert"], "-verify_depth", "2"),
  381. "accept chain with verify_depth 2");
  382. ok(verify("ee-cert", "", ["root-cert"], ["ca-cert"], "-verify_depth", "1"),
  383. "accept chain with verify_depth 1");
  384. ok(!verify("ee-cert", "", ["root-cert"], ["ca-cert"], "-verify_depth", "0"),
  385. "reject chain with verify_depth 0");
  386. ok(verify("ee-cert", "", ["ca-cert-md5-any"], [], "-verify_depth", "0"),
  387. "accept md5 intermediate TA with verify_depth 0");
  388. # Name Constraints tests.
  389. ok(verify("alt1-cert", "", ["root-cert"], ["ncca1-cert"], ),
  390. "Name Constraints everything permitted");
  391. ok(verify("alt2-cert", "", ["root-cert"], ["ncca2-cert"], ),
  392. "Name Constraints nothing excluded");
  393. ok(verify("alt3-cert", "", ["root-cert"], ["ncca1-cert", "ncca3-cert"], ),
  394. "Name Constraints nested test all permitted");
  395. ok(verify("goodcn1-cert", "", ["root-cert"], ["ncca1-cert"], ),
  396. "Name Constraints CNs permitted");
  397. ok(verify("goodcn2-cert", "", ["root-cert"], ["ncca1-cert"], ),
  398. "Name Constraints CNs permitted - no SAN extension");
  399. ok(!verify("badcn1-cert", "", ["root-cert"], ["ncca1-cert"], ),
  400. "Name Constraints CNs not permitted");
  401. ok(!verify("badalt1-cert", "", ["root-cert"], ["ncca1-cert"], ),
  402. "Name Constraints hostname not permitted");
  403. ok(!verify("badalt2-cert", "", ["root-cert"], ["ncca2-cert"], ),
  404. "Name Constraints hostname excluded");
  405. ok(!verify("badalt3-cert", "", ["root-cert"], ["ncca1-cert"], ),
  406. "Name Constraints email address not permitted");
  407. ok(!verify("badalt4-cert", "", ["root-cert"], ["ncca1-cert"], ),
  408. "Name Constraints subject email address not permitted");
  409. ok(!verify("badalt5-cert", "", ["root-cert"], ["ncca1-cert"], ),
  410. "Name Constraints IP address not permitted");
  411. ok(!verify("badalt6-cert", "", ["root-cert"], ["ncca1-cert"], ),
  412. "Name Constraints CN hostname not permitted");
  413. ok(!verify("badalt7-cert", "", ["root-cert"], ["ncca1-cert"], ),
  414. "Name Constraints CN BMPSTRING hostname not permitted");
  415. ok(!verify("badalt8-cert", "", ["root-cert"], ["ncca1-cert", "ncca3-cert"], ),
  416. "Name constraints nested DNS name not permitted 1");
  417. ok(!verify("badalt9-cert", "", ["root-cert"], ["ncca1-cert", "ncca3-cert"], ),
  418. "Name constraints nested DNS name not permitted 2");
  419. ok(!verify("badalt10-cert", "", ["root-cert"], ["ncca1-cert", "ncca3-cert"], ),
  420. "Name constraints nested DNS name excluded");
  421. ok(!verify("bad-othername-cert", "", ["root-cert"], ["nccaothername-cert"], ),
  422. "CVE-2022-4203 type confusion test");
  423. #Check that we get the expected failure return code
  424. with({ exit_checker => sub { return shift == 2; } },
  425. sub {
  426. ok(verify("bad-othername-namec", "", ["bad-othername-namec-inter"], [],
  427. "-partial_chain", "-attime", "1623060000"),
  428. "Name constraints bad othername name constraint");
  429. });
  430. ok(verify("ee-pss-sha1-cert", "", ["root-cert"], ["ca-cert"], "-auth_level", "0"),
  431. "Accept PSS signature using SHA1 at auth level 0");
  432. ok(verify("ee-pss-sha256-cert", "", ["root-cert"], ["ca-cert"], ),
  433. "CA with PSS signature using SHA256");
  434. ok(!verify("ee-pss-sha1-cert", "", ["root-cert"], ["ca-cert"], "-auth_level", "1"),
  435. "Reject PSS signature using SHA1 and auth level 1");
  436. ok(verify("ee-pss-sha256-cert", "", ["root-cert"], ["ca-cert"], "-auth_level", "2"),
  437. "PSS signature using SHA256 and auth level 2");
  438. ok(verify("ee-pss-cert", "", ["root-cert"], ["ca-pss-cert"], ),
  439. "CA PSS signature");
  440. ok(!verify("ee-pss-wrong1.5-cert", "", ["root-cert"], ["ca-pss-cert"], ),
  441. "CA producing regular PKCS#1 v1.5 signature with PSA-PSS key");
  442. ok(!verify("many-names1", "", ["many-constraints"], ["many-constraints"], ),
  443. "Too many names and constraints to check (1)");
  444. ok(!verify("many-names2", "", ["many-constraints"], ["many-constraints"], ),
  445. "Too many names and constraints to check (2)");
  446. ok(!verify("many-names3", "", ["many-constraints"], ["many-constraints"], ),
  447. "Too many names and constraints to check (3)");
  448. ok(verify("some-names1", "", ["many-constraints"], ["many-constraints"], ),
  449. "Not too many names and constraints to check (1)");
  450. ok(verify("some-names2", "", ["many-constraints"], ["many-constraints"], ),
  451. "Not too many names and constraints to check (2)");
  452. ok(verify("some-names2", "", ["many-constraints"], ["many-constraints"], ),
  453. "Not too many names and constraints to check (3)");
  454. ok(verify("root-cert-rsa2", "", ["root-cert-rsa2"], [], "-check_ss_sig"),
  455. "Public Key Algorithm rsa instead of rsaEncryption");
  456. ok(verify("ee-self-signed", "", ["ee-self-signed"], [], "-attime", "1593565200"),
  457. "accept trusted self-signed EE cert excluding key usage keyCertSign");
  458. ok(verify("ee-ss-with-keyCertSign", "", ["ee-ss-with-keyCertSign"], []),
  459. "accept trusted self-signed EE cert with key usage keyCertSign also when strict");
  460. SKIP: {
  461. skip "Ed25519 is not supported by this OpenSSL build", 6
  462. if disabled("ecx");
  463. # ED25519 certificate from draft-ietf-curdle-pkix-04
  464. ok(verify("ee-ed25519", "", ["root-ed25519"], []),
  465. "accept X25519 EE cert issued by trusted Ed25519 self-signed CA cert");
  466. ok(!verify("ee-ed25519", "", ["root-ed25519"], [], "-x509_strict"),
  467. "reject X25519 EE cert in strict mode since AKID is missing");
  468. ok(!verify("root-ed25519", "", ["ee-ed25519"], []),
  469. "fail Ed25519 CA and EE certs swapped");
  470. ok(verify("root-ed25519", "", ["root-ed25519"], []),
  471. "accept trusted Ed25519 self-signed CA cert");
  472. ok(!verify("ee-ed25519", "", ["ee-ed25519"], []),
  473. "fail trusted Ed25519-signed self-issued X25519 cert");
  474. ok(verify("ee-ed25519", "", ["ee-ed25519"], [], "-partial_chain"),
  475. "accept last-resort direct leaf match Ed25519-signed self-issued cert");
  476. }
  477. SKIP: {
  478. skip "SM2 is not supported by this OpenSSL build", 2 if disabled("sm2");
  479. ok_nofips(verify("sm2", "", ["sm2-ca-cert"], [], "-vfyopt", "distid:1234567812345678"),
  480. "SM2 ID test");
  481. ok_nofips(verify("sm2", "", ["sm2-ca-cert"], [], "-vfyopt", "hexdistid:31323334353637383132333435363738"),
  482. "SM2 hex ID test");
  483. }
  484. # Mixed content tests
  485. my $cert_file = srctop_file('test', 'certs', 'root-cert.pem');
  486. my $rsa_file = srctop_file('test', 'certs', 'key-pass-12345.pem');
  487. SKIP: {
  488. my $certplusrsa_file = 'certplusrsa.pem';
  489. my $certplusrsa;
  490. skip "Couldn't create certplusrsa.pem", 1
  491. unless ( open $certplusrsa, '>', $certplusrsa_file
  492. and copy($cert_file, $certplusrsa)
  493. and copy($rsa_file, $certplusrsa)
  494. and close $certplusrsa );
  495. ok(run(app([ qw(openssl verify -trusted), $certplusrsa_file, $cert_file ])),
  496. 'Mixed cert + key file test');
  497. }
  498. SKIP: {
  499. my $rsapluscert_file = 'rsapluscert.pem';
  500. my $rsapluscert;
  501. skip "Couldn't create rsapluscert.pem", 1
  502. unless ( open $rsapluscert, '>', $rsapluscert_file
  503. and copy($rsa_file, $rsapluscert)
  504. and copy($cert_file, $rsapluscert)
  505. and close $rsapluscert );
  506. ok(run(app([ qw(openssl verify -trusted), $rsapluscert_file, $cert_file ])),
  507. 'Mixed key + cert file test');
  508. }
  509. # Certificate Policies
  510. ok(verify("ee-cert-policies", "", ["root-cert"], ["ca-pol-cert"],
  511. "-policy_check", "-policy", "1.3.6.1.4.1.16604.998855.1",
  512. "-explicit_policy"),
  513. "Certificate policy");
  514. ok(!verify("ee-cert-policies-bad", "", ["root-cert"], ["ca-pol-cert"],
  515. "-policy_check", "-policy", "1.3.6.1.4.1.16604.998855.1",
  516. "-explicit_policy"),
  517. "Bad certificate policy");