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.
 
 
 
 
 
 

511 line
14 KiB

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