25-test_verify.t 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. #! /usr/bin/env perl
  2. # Copyright 2015-2016 The OpenSSL Project Authors. All Rights Reserved.
  3. #
  4. # Licensed under the OpenSSL license (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 OpenSSL::Test qw/:DEFAULT srctop_file/;
  12. setup("test_verify");
  13. sub verify {
  14. my ($cert, $purpose, $trusted, $untrusted, @opts) = @_;
  15. my @args = qw(openssl verify -auth_level 1 -purpose);
  16. my @path = qw(test certs);
  17. push(@args, "$purpose", @opts);
  18. for (@$trusted) { push(@args, "-trusted", srctop_file(@path, "$_.pem")) }
  19. for (@$untrusted) { push(@args, "-untrusted", srctop_file(@path, "$_.pem")) }
  20. push(@args, srctop_file(@path, "$cert.pem"));
  21. run(app([@args]));
  22. }
  23. plan tests => 125;
  24. # Canonical success
  25. ok(verify("ee-cert", "sslserver", ["root-cert"], ["ca-cert"]),
  26. "accept compat trust");
  27. # Root CA variants
  28. ok(!verify("ee-cert", "sslserver", [qw(root-nonca)], [qw(ca-cert)]),
  29. "fail trusted non-ca root");
  30. ok(!verify("ee-cert", "sslserver", [qw(nroot+serverAuth)], [qw(ca-cert)]),
  31. "fail server trust non-ca root");
  32. ok(!verify("ee-cert", "sslserver", [qw(nroot+anyEKU)], [qw(ca-cert)]),
  33. "fail wildcard trust non-ca root");
  34. ok(!verify("ee-cert", "sslserver", [qw(root-cert2)], [qw(ca-cert)]),
  35. "fail wrong root key");
  36. ok(!verify("ee-cert", "sslserver", [qw(root-name2)], [qw(ca-cert)]),
  37. "fail wrong root DN");
  38. # Explicit trust/purpose combinations
  39. #
  40. ok(verify("ee-cert", "sslserver", [qw(sroot-cert)], [qw(ca-cert)]),
  41. "accept server purpose");
  42. ok(!verify("ee-cert", "sslserver", [qw(croot-cert)], [qw(ca-cert)]),
  43. "fail client purpose");
  44. ok(verify("ee-cert", "sslserver", [qw(root+serverAuth)], [qw(ca-cert)]),
  45. "accept server trust");
  46. ok(verify("ee-cert", "sslserver", [qw(sroot+serverAuth)], [qw(ca-cert)]),
  47. "accept server trust with server purpose");
  48. ok(verify("ee-cert", "sslserver", [qw(croot+serverAuth)], [qw(ca-cert)]),
  49. "accept server trust with client purpose");
  50. # Wildcard trust
  51. ok(verify("ee-cert", "sslserver", [qw(root+anyEKU)], [qw(ca-cert)]),
  52. "accept wildcard trust");
  53. ok(verify("ee-cert", "sslserver", [qw(sroot+anyEKU)], [qw(ca-cert)]),
  54. "accept wildcard trust with server purpose");
  55. ok(verify("ee-cert", "sslserver", [qw(croot+anyEKU)], [qw(ca-cert)]),
  56. "accept wildcard trust with client purpose");
  57. # Inapplicable mistrust
  58. ok(verify("ee-cert", "sslserver", [qw(root-clientAuth)], [qw(ca-cert)]),
  59. "accept client mistrust");
  60. ok(verify("ee-cert", "sslserver", [qw(sroot-clientAuth)], [qw(ca-cert)]),
  61. "accept client mistrust with server purpose");
  62. ok(!verify("ee-cert", "sslserver", [qw(croot-clientAuth)], [qw(ca-cert)]),
  63. "fail client mistrust with client purpose");
  64. # Inapplicable trust
  65. ok(!verify("ee-cert", "sslserver", [qw(root+clientAuth)], [qw(ca-cert)]),
  66. "fail client trust");
  67. ok(!verify("ee-cert", "sslserver", [qw(sroot+clientAuth)], [qw(ca-cert)]),
  68. "fail client trust with server purpose");
  69. ok(!verify("ee-cert", "sslserver", [qw(croot+clientAuth)], [qw(ca-cert)]),
  70. "fail client trust with client purpose");
  71. # Server mistrust
  72. ok(!verify("ee-cert", "sslserver", [qw(root-serverAuth)], [qw(ca-cert)]),
  73. "fail rejected EKU");
  74. ok(!verify("ee-cert", "sslserver", [qw(sroot-serverAuth)], [qw(ca-cert)]),
  75. "fail server mistrust with server purpose");
  76. ok(!verify("ee-cert", "sslserver", [qw(croot-serverAuth)], [qw(ca-cert)]),
  77. "fail server mistrust with client purpose");
  78. # Wildcard mistrust
  79. ok(!verify("ee-cert", "sslserver", [qw(root-anyEKU)], [qw(ca-cert)]),
  80. "fail wildcard mistrust");
  81. ok(!verify("ee-cert", "sslserver", [qw(sroot-anyEKU)], [qw(ca-cert)]),
  82. "fail wildcard mistrust with server purpose");
  83. ok(!verify("ee-cert", "sslserver", [qw(croot-anyEKU)], [qw(ca-cert)]),
  84. "fail wildcard mistrust with client purpose");
  85. # Check that trusted-first is on by setting up paths to different roots
  86. # depending on whether the intermediate is the trusted or untrusted one.
  87. #
  88. ok(verify("ee-cert", "sslserver", [qw(root-serverAuth root-cert2 ca-root2)],
  89. [qw(ca-cert)]),
  90. "accept trusted-first path");
  91. ok(verify("ee-cert", "sslserver", [qw(root-cert root2+serverAuth ca-root2)],
  92. [qw(ca-cert)]),
  93. "accept trusted-first path with server trust");
  94. ok(!verify("ee-cert", "sslserver", [qw(root-cert root2-serverAuth ca-root2)],
  95. [qw(ca-cert)]),
  96. "fail trusted-first path with server mistrust");
  97. ok(!verify("ee-cert", "sslserver", [qw(root-cert root2+clientAuth ca-root2)],
  98. [qw(ca-cert)]),
  99. "fail trusted-first path with client trust");
  100. # CA variants
  101. ok(!verify("ee-cert", "sslserver", [qw(root-cert)], [qw(ca-nonca)]),
  102. "fail non-CA untrusted intermediate");
  103. ok(!verify("ee-cert", "sslserver", [qw(root-cert)], [qw(ca-nonbc)]),
  104. "fail non-CA untrusted intermediate");
  105. ok(!verify("ee-cert", "sslserver", [qw(root-cert ca-nonca)], []),
  106. "fail non-CA trust-store intermediate");
  107. ok(!verify("ee-cert", "sslserver", [qw(root-cert ca-nonbc)], []),
  108. "fail non-CA trust-store intermediate");
  109. ok(!verify("ee-cert", "sslserver", [qw(root-cert nca+serverAuth)], []),
  110. "fail non-CA server trust intermediate");
  111. ok(!verify("ee-cert", "sslserver", [qw(root-cert nca+anyEKU)], []),
  112. "fail non-CA wildcard trust intermediate");
  113. ok(!verify("ee-cert", "sslserver", [qw(root-cert)], [qw(ca-cert2)]),
  114. "fail wrong intermediate CA key");
  115. ok(!verify("ee-cert", "sslserver", [qw(root-cert)], [qw(ca-name2)]),
  116. "fail wrong intermediate CA DN");
  117. ok(!verify("ee-cert", "sslserver", [qw(root-cert)], [qw(ca-root2)]),
  118. "fail wrong intermediate CA issuer");
  119. ok(!verify("ee-cert", "sslserver", [], [qw(ca-cert)], "-partial_chain"),
  120. "fail untrusted partial chain");
  121. ok(verify("ee-cert", "sslserver", [qw(ca-cert)], [], "-partial_chain"),
  122. "accept trusted partial chain");
  123. ok(verify("ee-cert", "sslserver", [qw(sca-cert)], [], "-partial_chain"),
  124. "accept partial chain with server purpose");
  125. ok(!verify("ee-cert", "sslserver", [qw(cca-cert)], [], "-partial_chain"),
  126. "fail partial chain with client purpose");
  127. ok(verify("ee-cert", "sslserver", [qw(ca+serverAuth)], [], "-partial_chain"),
  128. "accept server trust partial chain");
  129. ok(verify("ee-cert", "sslserver", [qw(cca+serverAuth)], [], "-partial_chain"),
  130. "accept server trust client purpose partial chain");
  131. ok(verify("ee-cert", "sslserver", [qw(ca-clientAuth)], [], "-partial_chain"),
  132. "accept client mistrust partial chain");
  133. ok(verify("ee-cert", "sslserver", [qw(ca+anyEKU)], [], "-partial_chain"),
  134. "accept wildcard trust partial chain");
  135. ok(!verify("ee-cert", "sslserver", [], [qw(ca+serverAuth)], "-partial_chain"),
  136. "fail untrusted partial issuer with ignored server trust");
  137. ok(!verify("ee-cert", "sslserver", [qw(ca-serverAuth)], [], "-partial_chain"),
  138. "fail server mistrust partial chain");
  139. ok(!verify("ee-cert", "sslserver", [qw(ca+clientAuth)], [], "-partial_chain"),
  140. "fail client trust partial chain");
  141. ok(!verify("ee-cert", "sslserver", [qw(ca-anyEKU)], [], "-partial_chain"),
  142. "fail wildcard mistrust partial chain");
  143. # We now test auxiliary trust even for intermediate trusted certs without
  144. # -partial_chain. Note that "-trusted_first" is now always on and cannot
  145. # be disabled.
  146. ok(verify("ee-cert", "sslserver", [qw(root-cert ca+serverAuth)], [qw(ca-cert)]),
  147. "accept server trust");
  148. ok(verify("ee-cert", "sslserver", [qw(root-cert ca+anyEKU)], [qw(ca-cert)]),
  149. "accept wildcard trust");
  150. ok(verify("ee-cert", "sslserver", [qw(root-cert sca-cert)], [qw(ca-cert)]),
  151. "accept server purpose");
  152. ok(verify("ee-cert", "sslserver", [qw(root-cert sca+serverAuth)], [qw(ca-cert)]),
  153. "accept server trust and purpose");
  154. ok(verify("ee-cert", "sslserver", [qw(root-cert sca+anyEKU)], [qw(ca-cert)]),
  155. "accept wildcard trust and server purpose");
  156. ok(verify("ee-cert", "sslserver", [qw(root-cert sca-clientAuth)], [qw(ca-cert)]),
  157. "accept client mistrust and server purpose");
  158. ok(verify("ee-cert", "sslserver", [qw(root-cert cca+serverAuth)], [qw(ca-cert)]),
  159. "accept server trust and client purpose");
  160. ok(verify("ee-cert", "sslserver", [qw(root-cert cca+anyEKU)], [qw(ca-cert)]),
  161. "accept wildcard trust and client purpose");
  162. ok(!verify("ee-cert", "sslserver", [qw(root-cert cca-cert)], [qw(ca-cert)]),
  163. "fail client purpose");
  164. ok(!verify("ee-cert", "sslserver", [qw(root-cert ca-anyEKU)], [qw(ca-cert)]),
  165. "fail wildcard mistrust");
  166. ok(!verify("ee-cert", "sslserver", [qw(root-cert ca-serverAuth)], [qw(ca-cert)]),
  167. "fail server mistrust");
  168. ok(!verify("ee-cert", "sslserver", [qw(root-cert ca+clientAuth)], [qw(ca-cert)]),
  169. "fail client trust");
  170. ok(!verify("ee-cert", "sslserver", [qw(root-cert sca+clientAuth)], [qw(ca-cert)]),
  171. "fail client trust and server purpose");
  172. ok(!verify("ee-cert", "sslserver", [qw(root-cert cca+clientAuth)], [qw(ca-cert)]),
  173. "fail client trust and client purpose");
  174. ok(!verify("ee-cert", "sslserver", [qw(root-cert cca-serverAuth)], [qw(ca-cert)]),
  175. "fail server mistrust and client purpose");
  176. ok(!verify("ee-cert", "sslserver", [qw(root-cert cca-clientAuth)], [qw(ca-cert)]),
  177. "fail client mistrust and client purpose");
  178. ok(!verify("ee-cert", "sslserver", [qw(root-cert sca-serverAuth)], [qw(ca-cert)]),
  179. "fail server mistrust and server purpose");
  180. ok(!verify("ee-cert", "sslserver", [qw(root-cert sca-anyEKU)], [qw(ca-cert)]),
  181. "fail wildcard mistrust and server purpose");
  182. ok(!verify("ee-cert", "sslserver", [qw(root-cert cca-anyEKU)], [qw(ca-cert)]),
  183. "fail wildcard mistrust and client purpose");
  184. # EE variants
  185. ok(verify("ee-client", "sslclient", [qw(root-cert)], [qw(ca-cert)]),
  186. "accept client chain");
  187. ok(!verify("ee-client", "sslserver", [qw(root-cert)], [qw(ca-cert)]),
  188. "fail server leaf purpose");
  189. ok(!verify("ee-cert", "sslclient", [qw(root-cert)], [qw(ca-cert)]),
  190. "fail client leaf purpose");
  191. ok(!verify("ee-cert2", "sslserver", [qw(root-cert)], [qw(ca-cert)]),
  192. "fail wrong intermediate CA key");
  193. ok(!verify("ee-name2", "sslserver", [qw(root-cert)], [qw(ca-cert)]),
  194. "fail wrong intermediate CA DN");
  195. ok(!verify("ee-expired", "sslserver", [qw(root-cert)], [qw(ca-cert)]),
  196. "fail expired leaf");
  197. ok(verify("ee-cert", "sslserver", [qw(ee-cert)], [], "-partial_chain"),
  198. "accept last-resort direct leaf match");
  199. ok(verify("ee-client", "sslclient", [qw(ee-client)], [], "-partial_chain"),
  200. "accept last-resort direct leaf match");
  201. ok(!verify("ee-cert", "sslserver", [qw(ee-client)], [], "-partial_chain"),
  202. "fail last-resort direct leaf non-match");
  203. ok(verify("ee-cert", "sslserver", [qw(ee+serverAuth)], [], "-partial_chain"),
  204. "accept direct match with server trust");
  205. ok(!verify("ee-cert", "sslserver", [qw(ee-serverAuth)], [], "-partial_chain"),
  206. "fail direct match with server mistrust");
  207. ok(verify("ee-client", "sslclient", [qw(ee+clientAuth)], [], "-partial_chain"),
  208. "accept direct match with client trust");
  209. ok(!verify("ee-client", "sslclient", [qw(ee-clientAuth)], [], "-partial_chain"),
  210. "reject direct match with client mistrust");
  211. # Proxy certificates
  212. ok(!verify("pc1-cert", "sslclient", [qw(root-cert)], [qw(ee-client ca-cert)]),
  213. "fail to accept proxy cert without -allow_proxy_certs");
  214. ok(verify("pc1-cert", "sslclient", [qw(root-cert)], [qw(ee-client ca-cert)],
  215. "-allow_proxy_certs"),
  216. "accept proxy cert 1");
  217. ok(verify("pc2-cert", "sslclient", [qw(root-cert)], [qw(pc1-cert ee-client ca-cert)],
  218. "-allow_proxy_certs"),
  219. "accept proxy cert 2");
  220. ok(!verify("bad-pc3-cert", "sslclient", [qw(root-cert)], [qw(pc1-cert ee-client ca-cert)],
  221. "-allow_proxy_certs"),
  222. "fail proxy cert with incorrect subject");
  223. ok(!verify("bad-pc4-cert", "sslclient", [qw(root-cert)], [qw(pc1-cert ee-client ca-cert)],
  224. "-allow_proxy_certs"),
  225. "fail proxy cert with incorrect pathlen");
  226. ok(verify("pc5-cert", "sslclient", [qw(root-cert)], [qw(pc1-cert ee-client ca-cert)],
  227. "-allow_proxy_certs"),
  228. "accept proxy cert missing proxy policy");
  229. ok(!verify("pc6-cert", "sslclient", [qw(root-cert)], [qw(pc1-cert ee-client ca-cert)],
  230. "-allow_proxy_certs"),
  231. "failed proxy cert where last CN was added as a multivalue RDN component");
  232. # Security level tests
  233. ok(verify("ee-cert", "sslserver", ["root-cert"], ["ca-cert"], "-auth_level", "2"),
  234. "accept RSA 2048 chain at auth level 2");
  235. ok(!verify("ee-cert", "sslserver", ["root-cert"], ["ca-cert"], "-auth_level", "3"),
  236. "reject RSA 2048 root at auth level 3");
  237. ok(verify("ee-cert", "sslserver", ["root-cert-768"], ["ca-cert-768i"], "-auth_level", "0"),
  238. "accept RSA 768 root at auth level 0");
  239. ok(!verify("ee-cert", "sslserver", ["root-cert-768"], ["ca-cert-768i"]),
  240. "reject RSA 768 root at auth level 1");
  241. ok(verify("ee-cert-768i", "sslserver", ["root-cert"], ["ca-cert-768"], "-auth_level", "0"),
  242. "accept RSA 768 intermediate at auth level 0");
  243. ok(!verify("ee-cert-768i", "sslserver", ["root-cert"], ["ca-cert-768"]),
  244. "reject RSA 768 intermediate at auth level 1");
  245. ok(verify("ee-cert-768", "sslserver", ["root-cert"], ["ca-cert"], "-auth_level", "0"),
  246. "accept RSA 768 leaf at auth level 0");
  247. ok(!verify("ee-cert-768", "sslserver", ["root-cert"], ["ca-cert"]),
  248. "reject RSA 768 leaf at auth level 1");
  249. #
  250. ok(verify("ee-cert", "sslserver", ["root-cert-md5"], ["ca-cert"], "-auth_level", "2"),
  251. "accept md5 self-signed TA at auth level 2");
  252. ok(verify("ee-cert", "sslserver", ["ca-cert-md5-any"], [], "-auth_level", "2"),
  253. "accept md5 intermediate TA at auth level 2");
  254. ok(verify("ee-cert", "sslserver", ["root-cert"], ["ca-cert-md5"], "-auth_level", "0"),
  255. "accept md5 intermediate at auth level 0");
  256. ok(!verify("ee-cert", "sslserver", ["root-cert"], ["ca-cert-md5"]),
  257. "reject md5 intermediate at auth level 1");
  258. ok(verify("ee-cert-md5", "sslserver", ["root-cert"], ["ca-cert"], "-auth_level", "0"),
  259. "accept md5 leaf at auth level 0");
  260. ok(!verify("ee-cert-md5", "sslserver", ["root-cert"], ["ca-cert"]),
  261. "reject md5 leaf at auth level 1");
  262. # Depth tests, note the depth limit bounds the number of CA certificates
  263. # between the trust-anchor and the leaf, so, for example, with a root->ca->leaf
  264. # chain, depth = 1 is sufficient, but depth == 0 is not.
  265. #
  266. ok(verify("ee-cert", "sslserver", ["root-cert"], ["ca-cert"], "-verify_depth", "2"),
  267. "accept chain with verify_depth 2");
  268. ok(verify("ee-cert", "sslserver", ["root-cert"], ["ca-cert"], "-verify_depth", "1"),
  269. "accept chain with verify_depth 1");
  270. ok(!verify("ee-cert", "sslserver", ["root-cert"], ["ca-cert"], "-verify_depth", "0"),
  271. "accept chain with verify_depth 0");
  272. ok(verify("ee-cert", "sslserver", ["ca-cert-md5-any"], [], "-verify_depth", "0"),
  273. "accept md5 intermediate TA with verify_depth 0");
  274. # Name Constraints tests.
  275. ok(verify("alt1-cert", "sslserver", ["root-cert"], ["ncca1-cert"], ),
  276. "Name Constraints everything permitted");
  277. ok(verify("alt2-cert", "sslserver", ["root-cert"], ["ncca2-cert"], ),
  278. "Name Constraints nothing excluded");
  279. ok(verify("alt3-cert", "sslserver", ["root-cert"], ["ncca1-cert", "ncca3-cert"], ),
  280. "Name Constraints nested test all permitted");
  281. ok(!verify("badalt1-cert", "sslserver", ["root-cert"], ["ncca1-cert"], ),
  282. "Name Constraints hostname not permitted");
  283. ok(!verify("badalt2-cert", "sslserver", ["root-cert"], ["ncca2-cert"], ),
  284. "Name Constraints hostname excluded");
  285. ok(!verify("badalt3-cert", "sslserver", ["root-cert"], ["ncca1-cert"], ),
  286. "Name Constraints email address not permitted");
  287. ok(!verify("badalt4-cert", "sslserver", ["root-cert"], ["ncca1-cert"], ),
  288. "Name Constraints subject email address not permitted");
  289. ok(!verify("badalt5-cert", "sslserver", ["root-cert"], ["ncca1-cert"], ),
  290. "Name Constraints IP address not permitted");
  291. ok(!verify("badalt6-cert", "sslserver", ["root-cert"], ["ncca1-cert"], ),
  292. "Name Constraints CN hostname not permitted");
  293. ok(!verify("badalt7-cert", "sslserver", ["root-cert"], ["ncca1-cert"], ),
  294. "Name Constraints CN BMPSTRING hostname not permitted");
  295. ok(!verify("badalt8-cert", "sslserver", ["root-cert"], ["ncca1-cert", "ncca3-cert"], ),
  296. "Name constaints nested DNS name not permitted 1");
  297. ok(!verify("badalt9-cert", "sslserver", ["root-cert"], ["ncca1-cert", "ncca3-cert"], ),
  298. "Name constaints nested DNS name not permitted 2");
  299. ok(!verify("badalt10-cert", "sslserver", ["root-cert"], ["ncca1-cert", "ncca3-cert"], ),
  300. "Name constaints nested DNS name excluded");
  301. ok(verify("ee-pss-sha1-cert", "sslserver", ["root-cert"], ["ca-cert"], ),
  302. "Certificate PSS signature using SHA1");
  303. ok(verify("ee-pss-sha256-cert", "sslserver", ["root-cert"], ["ca-cert"], ),
  304. "CA with PSS signature using SHA256");
  305. ok(!verify("ee-pss-sha1-cert", "sslserver", ["root-cert"], ["ca-cert"], "-auth_level", "2"),
  306. "Reject PSS signature using SHA1 and auth level 2");
  307. ok(verify("ee-pss-sha256-cert", "sslserver", ["root-cert"], ["ca-cert"], "-auth_level", "2"),
  308. "PSS signature using SHA256 and auth level 2");