You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

502 lines
16 KiB

  1. /* v3_ncons.c */
  2. /*
  3. * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
  4. * project.
  5. */
  6. /* ====================================================================
  7. * Copyright (c) 2003 The OpenSSL Project. All rights reserved.
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions
  11. * are met:
  12. *
  13. * 1. Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions and the following disclaimer.
  15. *
  16. * 2. Redistributions in binary form must reproduce the above copyright
  17. * notice, this list of conditions and the following disclaimer in
  18. * the documentation and/or other materials provided with the
  19. * distribution.
  20. *
  21. * 3. All advertising materials mentioning features or use of this
  22. * software must display the following acknowledgment:
  23. * "This product includes software developed by the OpenSSL Project
  24. * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
  25. *
  26. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  27. * endorse or promote products derived from this software without
  28. * prior written permission. For written permission, please contact
  29. * licensing@OpenSSL.org.
  30. *
  31. * 5. Products derived from this software may not be called "OpenSSL"
  32. * nor may "OpenSSL" appear in their names without prior written
  33. * permission of the OpenSSL Project.
  34. *
  35. * 6. Redistributions of any form whatsoever must retain the following
  36. * acknowledgment:
  37. * "This product includes software developed by the OpenSSL Project
  38. * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
  39. *
  40. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  41. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  42. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  43. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  44. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  45. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  46. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  47. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  48. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  49. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  50. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  51. * OF THE POSSIBILITY OF SUCH DAMAGE.
  52. * ====================================================================
  53. *
  54. * This product includes cryptographic software written by Eric Young
  55. * (eay@cryptsoft.com). This product includes software written by Tim
  56. * Hudson (tjh@cryptsoft.com). */
  57. #include <stdio.h>
  58. #include <string.h>
  59. #include <openssl/asn1t.h>
  60. #include <openssl/conf.h>
  61. #include <openssl/err.h>
  62. #include <openssl/mem.h>
  63. #include <openssl/obj.h>
  64. #include <openssl/x509v3.h>
  65. #include "../internal.h"
  66. static void *v2i_NAME_CONSTRAINTS(const X509V3_EXT_METHOD *method,
  67. X509V3_CTX *ctx,
  68. STACK_OF(CONF_VALUE) *nval);
  69. static int i2r_NAME_CONSTRAINTS(const X509V3_EXT_METHOD *method, void *a,
  70. BIO *bp, int ind);
  71. static int do_i2r_name_constraints(const X509V3_EXT_METHOD *method,
  72. STACK_OF(GENERAL_SUBTREE) *trees, BIO *bp,
  73. int ind, const char *name);
  74. static int print_nc_ipadd(BIO *bp, ASN1_OCTET_STRING *ip);
  75. static int nc_match(GENERAL_NAME *gen, NAME_CONSTRAINTS *nc);
  76. static int nc_match_single(GENERAL_NAME *sub, GENERAL_NAME *gen);
  77. static int nc_dn(X509_NAME *sub, X509_NAME *nm);
  78. static int nc_dns(ASN1_IA5STRING *sub, ASN1_IA5STRING *dns);
  79. static int nc_email(ASN1_IA5STRING *sub, ASN1_IA5STRING *eml);
  80. static int nc_uri(ASN1_IA5STRING *uri, ASN1_IA5STRING *base);
  81. const X509V3_EXT_METHOD v3_name_constraints = {
  82. NID_name_constraints, 0,
  83. ASN1_ITEM_ref(NAME_CONSTRAINTS),
  84. 0, 0, 0, 0,
  85. 0, 0,
  86. 0, v2i_NAME_CONSTRAINTS,
  87. i2r_NAME_CONSTRAINTS, 0,
  88. NULL
  89. };
  90. ASN1_SEQUENCE(GENERAL_SUBTREE) = {
  91. ASN1_SIMPLE(GENERAL_SUBTREE, base, GENERAL_NAME),
  92. ASN1_IMP_OPT(GENERAL_SUBTREE, minimum, ASN1_INTEGER, 0),
  93. ASN1_IMP_OPT(GENERAL_SUBTREE, maximum, ASN1_INTEGER, 1)
  94. } ASN1_SEQUENCE_END(GENERAL_SUBTREE)
  95. ASN1_SEQUENCE(NAME_CONSTRAINTS) = {
  96. ASN1_IMP_SEQUENCE_OF_OPT(NAME_CONSTRAINTS, permittedSubtrees,
  97. GENERAL_SUBTREE, 0),
  98. ASN1_IMP_SEQUENCE_OF_OPT(NAME_CONSTRAINTS, excludedSubtrees,
  99. GENERAL_SUBTREE, 1),
  100. } ASN1_SEQUENCE_END(NAME_CONSTRAINTS)
  101. IMPLEMENT_ASN1_ALLOC_FUNCTIONS(GENERAL_SUBTREE)
  102. IMPLEMENT_ASN1_ALLOC_FUNCTIONS(NAME_CONSTRAINTS)
  103. static void *v2i_NAME_CONSTRAINTS(const X509V3_EXT_METHOD *method,
  104. X509V3_CTX *ctx, STACK_OF(CONF_VALUE) *nval)
  105. {
  106. size_t i;
  107. CONF_VALUE tval, *val;
  108. STACK_OF(GENERAL_SUBTREE) **ptree = NULL;
  109. NAME_CONSTRAINTS *ncons = NULL;
  110. GENERAL_SUBTREE *sub = NULL;
  111. ncons = NAME_CONSTRAINTS_new();
  112. if (!ncons)
  113. goto memerr;
  114. for (i = 0; i < sk_CONF_VALUE_num(nval); i++) {
  115. val = sk_CONF_VALUE_value(nval, i);
  116. if (!strncmp(val->name, "permitted", 9) && val->name[9]) {
  117. ptree = &ncons->permittedSubtrees;
  118. tval.name = val->name + 10;
  119. } else if (!strncmp(val->name, "excluded", 8) && val->name[8]) {
  120. ptree = &ncons->excludedSubtrees;
  121. tval.name = val->name + 9;
  122. } else {
  123. OPENSSL_PUT_ERROR(X509V3, X509V3_R_INVALID_SYNTAX);
  124. goto err;
  125. }
  126. tval.value = val->value;
  127. sub = GENERAL_SUBTREE_new();
  128. if (!v2i_GENERAL_NAME_ex(sub->base, method, ctx, &tval, 1))
  129. goto err;
  130. if (!*ptree)
  131. *ptree = sk_GENERAL_SUBTREE_new_null();
  132. if (!*ptree || !sk_GENERAL_SUBTREE_push(*ptree, sub))
  133. goto memerr;
  134. sub = NULL;
  135. }
  136. return ncons;
  137. memerr:
  138. OPENSSL_PUT_ERROR(X509V3, ERR_R_MALLOC_FAILURE);
  139. err:
  140. if (ncons)
  141. NAME_CONSTRAINTS_free(ncons);
  142. if (sub)
  143. GENERAL_SUBTREE_free(sub);
  144. return NULL;
  145. }
  146. static int i2r_NAME_CONSTRAINTS(const X509V3_EXT_METHOD *method, void *a,
  147. BIO *bp, int ind)
  148. {
  149. NAME_CONSTRAINTS *ncons = a;
  150. do_i2r_name_constraints(method, ncons->permittedSubtrees,
  151. bp, ind, "Permitted");
  152. do_i2r_name_constraints(method, ncons->excludedSubtrees,
  153. bp, ind, "Excluded");
  154. return 1;
  155. }
  156. static int do_i2r_name_constraints(const X509V3_EXT_METHOD *method,
  157. STACK_OF(GENERAL_SUBTREE) *trees,
  158. BIO *bp, int ind, const char *name)
  159. {
  160. GENERAL_SUBTREE *tree;
  161. size_t i;
  162. if (sk_GENERAL_SUBTREE_num(trees) > 0)
  163. BIO_printf(bp, "%*s%s:\n", ind, "", name);
  164. for (i = 0; i < sk_GENERAL_SUBTREE_num(trees); i++) {
  165. tree = sk_GENERAL_SUBTREE_value(trees, i);
  166. BIO_printf(bp, "%*s", ind + 2, "");
  167. if (tree->base->type == GEN_IPADD)
  168. print_nc_ipadd(bp, tree->base->d.ip);
  169. else
  170. GENERAL_NAME_print(bp, tree->base);
  171. BIO_puts(bp, "\n");
  172. }
  173. return 1;
  174. }
  175. static int print_nc_ipadd(BIO *bp, ASN1_OCTET_STRING *ip)
  176. {
  177. int i, len;
  178. unsigned char *p;
  179. p = ip->data;
  180. len = ip->length;
  181. BIO_puts(bp, "IP:");
  182. if (len == 8) {
  183. BIO_printf(bp, "%d.%d.%d.%d/%d.%d.%d.%d",
  184. p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7]);
  185. } else if (len == 32) {
  186. for (i = 0; i < 16; i++) {
  187. BIO_printf(bp, "%X", p[0] << 8 | p[1]);
  188. p += 2;
  189. if (i == 7)
  190. BIO_puts(bp, "/");
  191. else if (i != 15)
  192. BIO_puts(bp, ":");
  193. }
  194. } else
  195. BIO_printf(bp, "IP Address:<invalid>");
  196. return 1;
  197. }
  198. /*-
  199. * Check a certificate conforms to a specified set of constraints.
  200. * Return values:
  201. * X509_V_OK: All constraints obeyed.
  202. * X509_V_ERR_PERMITTED_VIOLATION: Permitted subtree violation.
  203. * X509_V_ERR_EXCLUDED_VIOLATION: Excluded subtree violation.
  204. * X509_V_ERR_SUBTREE_MINMAX: Min or max values present and matching type.
  205. * X509_V_ERR_UNSPECIFIED: Unspecified error.
  206. * X509_V_ERR_UNSUPPORTED_CONSTRAINT_TYPE: Unsupported constraint type.
  207. * X509_V_ERR_UNSUPPORTED_CONSTRAINT_SYNTAX: Bad or unsupported constraint
  208. * syntax.
  209. * X509_V_ERR_UNSUPPORTED_NAME_SYNTAX: Bad or unsupported syntax of name.
  210. */
  211. int NAME_CONSTRAINTS_check(X509 *x, NAME_CONSTRAINTS *nc)
  212. {
  213. int r, i;
  214. size_t j;
  215. X509_NAME *nm;
  216. nm = X509_get_subject_name(x);
  217. /* Guard against certificates with an excessive number of names or
  218. * constraints causing a computationally expensive name constraints
  219. * check. */
  220. size_t name_count =
  221. X509_NAME_entry_count(nm) + sk_GENERAL_NAME_num(x->altname);
  222. size_t constraint_count = sk_GENERAL_SUBTREE_num(nc->permittedSubtrees) +
  223. sk_GENERAL_SUBTREE_num(nc->excludedSubtrees);
  224. size_t check_count = constraint_count * name_count;
  225. if (name_count < (size_t)X509_NAME_entry_count(nm) ||
  226. constraint_count < sk_GENERAL_SUBTREE_num(nc->permittedSubtrees) ||
  227. (constraint_count && check_count / constraint_count != name_count) ||
  228. check_count > 1 << 20) {
  229. return X509_V_ERR_UNSPECIFIED;
  230. }
  231. if (X509_NAME_entry_count(nm) > 0) {
  232. GENERAL_NAME gntmp;
  233. gntmp.type = GEN_DIRNAME;
  234. gntmp.d.directoryName = nm;
  235. r = nc_match(&gntmp, nc);
  236. if (r != X509_V_OK)
  237. return r;
  238. gntmp.type = GEN_EMAIL;
  239. /* Process any email address attributes in subject name */
  240. for (i = -1;;) {
  241. X509_NAME_ENTRY *ne;
  242. i = X509_NAME_get_index_by_NID(nm, NID_pkcs9_emailAddress, i);
  243. if (i == -1)
  244. break;
  245. ne = X509_NAME_get_entry(nm, i);
  246. gntmp.d.rfc822Name = X509_NAME_ENTRY_get_data(ne);
  247. if (gntmp.d.rfc822Name->type != V_ASN1_IA5STRING)
  248. return X509_V_ERR_UNSUPPORTED_NAME_SYNTAX;
  249. r = nc_match(&gntmp, nc);
  250. if (r != X509_V_OK)
  251. return r;
  252. }
  253. }
  254. for (j = 0; j < sk_GENERAL_NAME_num(x->altname); j++) {
  255. GENERAL_NAME *gen = sk_GENERAL_NAME_value(x->altname, j);
  256. r = nc_match(gen, nc);
  257. if (r != X509_V_OK)
  258. return r;
  259. }
  260. return X509_V_OK;
  261. }
  262. static int nc_match(GENERAL_NAME *gen, NAME_CONSTRAINTS *nc)
  263. {
  264. GENERAL_SUBTREE *sub;
  265. int r, match = 0;
  266. size_t i;
  267. /*
  268. * Permitted subtrees: if any subtrees exist of matching the type at
  269. * least one subtree must match.
  270. */
  271. for (i = 0; i < sk_GENERAL_SUBTREE_num(nc->permittedSubtrees); i++) {
  272. sub = sk_GENERAL_SUBTREE_value(nc->permittedSubtrees, i);
  273. if (gen->type != sub->base->type)
  274. continue;
  275. if (sub->minimum || sub->maximum)
  276. return X509_V_ERR_SUBTREE_MINMAX;
  277. /* If we already have a match don't bother trying any more */
  278. if (match == 2)
  279. continue;
  280. if (match == 0)
  281. match = 1;
  282. r = nc_match_single(gen, sub->base);
  283. if (r == X509_V_OK)
  284. match = 2;
  285. else if (r != X509_V_ERR_PERMITTED_VIOLATION)
  286. return r;
  287. }
  288. if (match == 1)
  289. return X509_V_ERR_PERMITTED_VIOLATION;
  290. /* Excluded subtrees: must not match any of these */
  291. for (i = 0; i < sk_GENERAL_SUBTREE_num(nc->excludedSubtrees); i++) {
  292. sub = sk_GENERAL_SUBTREE_value(nc->excludedSubtrees, i);
  293. if (gen->type != sub->base->type)
  294. continue;
  295. if (sub->minimum || sub->maximum)
  296. return X509_V_ERR_SUBTREE_MINMAX;
  297. r = nc_match_single(gen, sub->base);
  298. if (r == X509_V_OK)
  299. return X509_V_ERR_EXCLUDED_VIOLATION;
  300. else if (r != X509_V_ERR_PERMITTED_VIOLATION)
  301. return r;
  302. }
  303. return X509_V_OK;
  304. }
  305. static int nc_match_single(GENERAL_NAME *gen, GENERAL_NAME *base)
  306. {
  307. switch (base->type) {
  308. case GEN_DIRNAME:
  309. return nc_dn(gen->d.directoryName, base->d.directoryName);
  310. case GEN_DNS:
  311. return nc_dns(gen->d.dNSName, base->d.dNSName);
  312. case GEN_EMAIL:
  313. return nc_email(gen->d.rfc822Name, base->d.rfc822Name);
  314. case GEN_URI:
  315. return nc_uri(gen->d.uniformResourceIdentifier,
  316. base->d.uniformResourceIdentifier);
  317. default:
  318. return X509_V_ERR_UNSUPPORTED_CONSTRAINT_TYPE;
  319. }
  320. }
  321. /*
  322. * directoryName name constraint matching. The canonical encoding of
  323. * X509_NAME makes this comparison easy. It is matched if the subtree is a
  324. * subset of the name.
  325. */
  326. static int nc_dn(X509_NAME *nm, X509_NAME *base)
  327. {
  328. /* Ensure canonical encodings are up to date. */
  329. if (nm->modified && i2d_X509_NAME(nm, NULL) < 0)
  330. return X509_V_ERR_OUT_OF_MEM;
  331. if (base->modified && i2d_X509_NAME(base, NULL) < 0)
  332. return X509_V_ERR_OUT_OF_MEM;
  333. if (base->canon_enclen > nm->canon_enclen)
  334. return X509_V_ERR_PERMITTED_VIOLATION;
  335. if (OPENSSL_memcmp(base->canon_enc, nm->canon_enc, base->canon_enclen))
  336. return X509_V_ERR_PERMITTED_VIOLATION;
  337. return X509_V_OK;
  338. }
  339. static int nc_dns(ASN1_IA5STRING *dns, ASN1_IA5STRING *base)
  340. {
  341. char *baseptr = (char *)base->data;
  342. char *dnsptr = (char *)dns->data;
  343. /* Empty matches everything */
  344. if (!*baseptr)
  345. return X509_V_OK;
  346. /*
  347. * Otherwise can add zero or more components on the left so compare RHS
  348. * and if dns is longer and expect '.' as preceding character.
  349. */
  350. if (dns->length > base->length) {
  351. dnsptr += dns->length - base->length;
  352. if (*baseptr != '.' && dnsptr[-1] != '.')
  353. return X509_V_ERR_PERMITTED_VIOLATION;
  354. }
  355. if (OPENSSL_strcasecmp(baseptr, dnsptr))
  356. return X509_V_ERR_PERMITTED_VIOLATION;
  357. return X509_V_OK;
  358. }
  359. static int nc_email(ASN1_IA5STRING *eml, ASN1_IA5STRING *base)
  360. {
  361. const char *baseptr = (char *)base->data;
  362. const char *emlptr = (char *)eml->data;
  363. const char *baseat = strchr(baseptr, '@');
  364. const char *emlat = strchr(emlptr, '@');
  365. if (!emlat)
  366. return X509_V_ERR_UNSUPPORTED_NAME_SYNTAX;
  367. /* Special case: inital '.' is RHS match */
  368. if (!baseat && (*baseptr == '.')) {
  369. if (eml->length > base->length) {
  370. emlptr += eml->length - base->length;
  371. if (!OPENSSL_strcasecmp(baseptr, emlptr))
  372. return X509_V_OK;
  373. }
  374. return X509_V_ERR_PERMITTED_VIOLATION;
  375. }
  376. /* If we have anything before '@' match local part */
  377. if (baseat) {
  378. if (baseat != baseptr) {
  379. if ((baseat - baseptr) != (emlat - emlptr))
  380. return X509_V_ERR_PERMITTED_VIOLATION;
  381. /* Case sensitive match of local part */
  382. if (strncmp(baseptr, emlptr, emlat - emlptr))
  383. return X509_V_ERR_PERMITTED_VIOLATION;
  384. }
  385. /* Position base after '@' */
  386. baseptr = baseat + 1;
  387. }
  388. emlptr = emlat + 1;
  389. /* Just have hostname left to match: case insensitive */
  390. if (OPENSSL_strcasecmp(baseptr, emlptr))
  391. return X509_V_ERR_PERMITTED_VIOLATION;
  392. return X509_V_OK;
  393. }
  394. static int nc_uri(ASN1_IA5STRING *uri, ASN1_IA5STRING *base)
  395. {
  396. const char *baseptr = (char *)base->data;
  397. const char *hostptr = (char *)uri->data;
  398. const char *p = strchr(hostptr, ':');
  399. int hostlen;
  400. /* Check for foo:// and skip past it */
  401. if (!p || (p[1] != '/') || (p[2] != '/'))
  402. return X509_V_ERR_UNSUPPORTED_NAME_SYNTAX;
  403. hostptr = p + 3;
  404. /* Determine length of hostname part of URI */
  405. /* Look for a port indicator as end of hostname first */
  406. p = strchr(hostptr, ':');
  407. /* Otherwise look for trailing slash */
  408. if (!p)
  409. p = strchr(hostptr, '/');
  410. if (!p)
  411. hostlen = strlen(hostptr);
  412. else
  413. hostlen = p - hostptr;
  414. if (hostlen == 0)
  415. return X509_V_ERR_UNSUPPORTED_NAME_SYNTAX;
  416. /* Special case: inital '.' is RHS match */
  417. if (*baseptr == '.') {
  418. if (hostlen > base->length) {
  419. p = hostptr + hostlen - base->length;
  420. if (!OPENSSL_strncasecmp(p, baseptr, base->length))
  421. return X509_V_OK;
  422. }
  423. return X509_V_ERR_PERMITTED_VIOLATION;
  424. }
  425. if ((base->length != (int)hostlen)
  426. || OPENSSL_strncasecmp(hostptr, baseptr, hostlen))
  427. return X509_V_ERR_PERMITTED_VIOLATION;
  428. return X509_V_OK;
  429. }