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.
 
 
 
 
 
 

483 line
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. static void *v2i_NAME_CONSTRAINTS(const X509V3_EXT_METHOD *method,
  66. X509V3_CTX *ctx,
  67. STACK_OF(CONF_VALUE) *nval);
  68. static int i2r_NAME_CONSTRAINTS(const X509V3_EXT_METHOD *method, void *a,
  69. BIO *bp, int ind);
  70. static int do_i2r_name_constraints(const X509V3_EXT_METHOD *method,
  71. STACK_OF(GENERAL_SUBTREE) *trees, BIO *bp,
  72. int ind, const char *name);
  73. static int print_nc_ipadd(BIO *bp, ASN1_OCTET_STRING *ip);
  74. static int nc_match(GENERAL_NAME *gen, NAME_CONSTRAINTS *nc);
  75. static int nc_match_single(GENERAL_NAME *sub, GENERAL_NAME *gen);
  76. static int nc_dn(X509_NAME *sub, X509_NAME *nm);
  77. static int nc_dns(ASN1_IA5STRING *sub, ASN1_IA5STRING *dns);
  78. static int nc_email(ASN1_IA5STRING *sub, ASN1_IA5STRING *eml);
  79. static int nc_uri(ASN1_IA5STRING *uri, ASN1_IA5STRING *base);
  80. const X509V3_EXT_METHOD v3_name_constraints = {
  81. NID_name_constraints, 0,
  82. ASN1_ITEM_ref(NAME_CONSTRAINTS),
  83. 0, 0, 0, 0,
  84. 0, 0,
  85. 0, v2i_NAME_CONSTRAINTS,
  86. i2r_NAME_CONSTRAINTS, 0,
  87. NULL
  88. };
  89. ASN1_SEQUENCE(GENERAL_SUBTREE) = {
  90. ASN1_SIMPLE(GENERAL_SUBTREE, base, GENERAL_NAME),
  91. ASN1_IMP_OPT(GENERAL_SUBTREE, minimum, ASN1_INTEGER, 0),
  92. ASN1_IMP_OPT(GENERAL_SUBTREE, maximum, ASN1_INTEGER, 1)
  93. } ASN1_SEQUENCE_END(GENERAL_SUBTREE)
  94. ASN1_SEQUENCE(NAME_CONSTRAINTS) = {
  95. ASN1_IMP_SEQUENCE_OF_OPT(NAME_CONSTRAINTS, permittedSubtrees,
  96. GENERAL_SUBTREE, 0),
  97. ASN1_IMP_SEQUENCE_OF_OPT(NAME_CONSTRAINTS, excludedSubtrees,
  98. GENERAL_SUBTREE, 1),
  99. } ASN1_SEQUENCE_END(NAME_CONSTRAINTS)
  100. IMPLEMENT_ASN1_ALLOC_FUNCTIONS(GENERAL_SUBTREE)
  101. IMPLEMENT_ASN1_ALLOC_FUNCTIONS(NAME_CONSTRAINTS)
  102. static void *v2i_NAME_CONSTRAINTS(const X509V3_EXT_METHOD *method,
  103. X509V3_CTX *ctx, STACK_OF(CONF_VALUE) *nval)
  104. {
  105. size_t i;
  106. CONF_VALUE tval, *val;
  107. STACK_OF(GENERAL_SUBTREE) **ptree = NULL;
  108. NAME_CONSTRAINTS *ncons = NULL;
  109. GENERAL_SUBTREE *sub = NULL;
  110. ncons = NAME_CONSTRAINTS_new();
  111. if (!ncons)
  112. goto memerr;
  113. for (i = 0; i < sk_CONF_VALUE_num(nval); i++) {
  114. val = sk_CONF_VALUE_value(nval, i);
  115. if (!strncmp(val->name, "permitted", 9) && val->name[9]) {
  116. ptree = &ncons->permittedSubtrees;
  117. tval.name = val->name + 10;
  118. } else if (!strncmp(val->name, "excluded", 8) && val->name[8]) {
  119. ptree = &ncons->excludedSubtrees;
  120. tval.name = val->name + 9;
  121. } else {
  122. OPENSSL_PUT_ERROR(X509V3, X509V3_R_INVALID_SYNTAX);
  123. goto err;
  124. }
  125. tval.value = val->value;
  126. sub = GENERAL_SUBTREE_new();
  127. if (!v2i_GENERAL_NAME_ex(sub->base, method, ctx, &tval, 1))
  128. goto err;
  129. if (!*ptree)
  130. *ptree = sk_GENERAL_SUBTREE_new_null();
  131. if (!*ptree || !sk_GENERAL_SUBTREE_push(*ptree, sub))
  132. goto memerr;
  133. sub = NULL;
  134. }
  135. return ncons;
  136. memerr:
  137. OPENSSL_PUT_ERROR(X509V3, ERR_R_MALLOC_FAILURE);
  138. err:
  139. if (ncons)
  140. NAME_CONSTRAINTS_free(ncons);
  141. if (sub)
  142. GENERAL_SUBTREE_free(sub);
  143. return NULL;
  144. }
  145. static int i2r_NAME_CONSTRAINTS(const X509V3_EXT_METHOD *method, void *a,
  146. BIO *bp, int ind)
  147. {
  148. NAME_CONSTRAINTS *ncons = a;
  149. do_i2r_name_constraints(method, ncons->permittedSubtrees,
  150. bp, ind, "Permitted");
  151. do_i2r_name_constraints(method, ncons->excludedSubtrees,
  152. bp, ind, "Excluded");
  153. return 1;
  154. }
  155. static int do_i2r_name_constraints(const X509V3_EXT_METHOD *method,
  156. STACK_OF(GENERAL_SUBTREE) *trees,
  157. BIO *bp, int ind, const char *name)
  158. {
  159. GENERAL_SUBTREE *tree;
  160. size_t i;
  161. if (sk_GENERAL_SUBTREE_num(trees) > 0)
  162. BIO_printf(bp, "%*s%s:\n", ind, "", name);
  163. for (i = 0; i < sk_GENERAL_SUBTREE_num(trees); i++) {
  164. tree = sk_GENERAL_SUBTREE_value(trees, i);
  165. BIO_printf(bp, "%*s", ind + 2, "");
  166. if (tree->base->type == GEN_IPADD)
  167. print_nc_ipadd(bp, tree->base->d.ip);
  168. else
  169. GENERAL_NAME_print(bp, tree->base);
  170. BIO_puts(bp, "\n");
  171. }
  172. return 1;
  173. }
  174. static int print_nc_ipadd(BIO *bp, ASN1_OCTET_STRING *ip)
  175. {
  176. int i, len;
  177. unsigned char *p;
  178. p = ip->data;
  179. len = ip->length;
  180. BIO_puts(bp, "IP:");
  181. if (len == 8) {
  182. BIO_printf(bp, "%d.%d.%d.%d/%d.%d.%d.%d",
  183. p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7]);
  184. } else if (len == 32) {
  185. for (i = 0; i < 16; i++) {
  186. BIO_printf(bp, "%X", p[0] << 8 | p[1]);
  187. p += 2;
  188. if (i == 7)
  189. BIO_puts(bp, "/");
  190. else if (i != 15)
  191. BIO_puts(bp, ":");
  192. }
  193. } else
  194. BIO_printf(bp, "IP Address:<invalid>");
  195. return 1;
  196. }
  197. /*
  198. * Check a certificate conforms to a specified set of constraints. Return
  199. * values: X509_V_OK: All constraints obeyed.
  200. * X509_V_ERR_PERMITTED_VIOLATION: Permitted subtree violation.
  201. * X509_V_ERR_EXCLUDED_VIOLATION: Excluded subtree violation.
  202. * X509_V_ERR_SUBTREE_MINMAX: Min or max values present and matching type.
  203. * X509_V_ERR_UNSUPPORTED_CONSTRAINT_TYPE: Unsupported constraint type.
  204. * X509_V_ERR_UNSUPPORTED_CONSTRAINT_SYNTAX: bad unsupported constraint
  205. * syntax. X509_V_ERR_UNSUPPORTED_NAME_SYNTAX: bad or unsupported syntax of
  206. * name
  207. *
  208. */
  209. int NAME_CONSTRAINTS_check(X509 *x, NAME_CONSTRAINTS *nc)
  210. {
  211. int r, i;
  212. size_t j;
  213. X509_NAME *nm;
  214. nm = X509_get_subject_name(x);
  215. if (X509_NAME_entry_count(nm) > 0) {
  216. GENERAL_NAME gntmp;
  217. gntmp.type = GEN_DIRNAME;
  218. gntmp.d.directoryName = nm;
  219. r = nc_match(&gntmp, nc);
  220. if (r != X509_V_OK)
  221. return r;
  222. gntmp.type = GEN_EMAIL;
  223. /* Process any email address attributes in subject name */
  224. for (i = -1;;) {
  225. X509_NAME_ENTRY *ne;
  226. i = X509_NAME_get_index_by_NID(nm, NID_pkcs9_emailAddress, i);
  227. if (i == -1)
  228. break;
  229. ne = X509_NAME_get_entry(nm, i);
  230. gntmp.d.rfc822Name = X509_NAME_ENTRY_get_data(ne);
  231. if (gntmp.d.rfc822Name->type != V_ASN1_IA5STRING)
  232. return X509_V_ERR_UNSUPPORTED_NAME_SYNTAX;
  233. r = nc_match(&gntmp, nc);
  234. if (r != X509_V_OK)
  235. return r;
  236. }
  237. }
  238. for (j = 0; j < sk_GENERAL_NAME_num(x->altname); j++) {
  239. GENERAL_NAME *gen = sk_GENERAL_NAME_value(x->altname, j);
  240. r = nc_match(gen, nc);
  241. if (r != X509_V_OK)
  242. return r;
  243. }
  244. return X509_V_OK;
  245. }
  246. static int nc_match(GENERAL_NAME *gen, NAME_CONSTRAINTS *nc)
  247. {
  248. GENERAL_SUBTREE *sub;
  249. int r, match = 0;
  250. size_t i;
  251. /*
  252. * Permitted subtrees: if any subtrees exist of matching the type at
  253. * least one subtree must match.
  254. */
  255. for (i = 0; i < sk_GENERAL_SUBTREE_num(nc->permittedSubtrees); i++) {
  256. sub = sk_GENERAL_SUBTREE_value(nc->permittedSubtrees, i);
  257. if (gen->type != sub->base->type)
  258. continue;
  259. if (sub->minimum || sub->maximum)
  260. return X509_V_ERR_SUBTREE_MINMAX;
  261. /* If we already have a match don't bother trying any more */
  262. if (match == 2)
  263. continue;
  264. if (match == 0)
  265. match = 1;
  266. r = nc_match_single(gen, sub->base);
  267. if (r == X509_V_OK)
  268. match = 2;
  269. else if (r != X509_V_ERR_PERMITTED_VIOLATION)
  270. return r;
  271. }
  272. if (match == 1)
  273. return X509_V_ERR_PERMITTED_VIOLATION;
  274. /* Excluded subtrees: must not match any of these */
  275. for (i = 0; i < sk_GENERAL_SUBTREE_num(nc->excludedSubtrees); i++) {
  276. sub = sk_GENERAL_SUBTREE_value(nc->excludedSubtrees, i);
  277. if (gen->type != sub->base->type)
  278. continue;
  279. if (sub->minimum || sub->maximum)
  280. return X509_V_ERR_SUBTREE_MINMAX;
  281. r = nc_match_single(gen, sub->base);
  282. if (r == X509_V_OK)
  283. return X509_V_ERR_EXCLUDED_VIOLATION;
  284. else if (r != X509_V_ERR_PERMITTED_VIOLATION)
  285. return r;
  286. }
  287. return X509_V_OK;
  288. }
  289. static int nc_match_single(GENERAL_NAME *gen, GENERAL_NAME *base)
  290. {
  291. switch (base->type) {
  292. case GEN_DIRNAME:
  293. return nc_dn(gen->d.directoryName, base->d.directoryName);
  294. case GEN_DNS:
  295. return nc_dns(gen->d.dNSName, base->d.dNSName);
  296. case GEN_EMAIL:
  297. return nc_email(gen->d.rfc822Name, base->d.rfc822Name);
  298. case GEN_URI:
  299. return nc_uri(gen->d.uniformResourceIdentifier,
  300. base->d.uniformResourceIdentifier);
  301. default:
  302. return X509_V_ERR_UNSUPPORTED_CONSTRAINT_TYPE;
  303. }
  304. }
  305. /*
  306. * directoryName name constraint matching. The canonical encoding of
  307. * X509_NAME makes this comparison easy. It is matched if the subtree is a
  308. * subset of the name.
  309. */
  310. static int nc_dn(X509_NAME *nm, X509_NAME *base)
  311. {
  312. /* Ensure canonical encodings are up to date. */
  313. if (nm->modified && i2d_X509_NAME(nm, NULL) < 0)
  314. return X509_V_ERR_OUT_OF_MEM;
  315. if (base->modified && i2d_X509_NAME(base, NULL) < 0)
  316. return X509_V_ERR_OUT_OF_MEM;
  317. if (base->canon_enclen > nm->canon_enclen)
  318. return X509_V_ERR_PERMITTED_VIOLATION;
  319. if (memcmp(base->canon_enc, nm->canon_enc, base->canon_enclen))
  320. return X509_V_ERR_PERMITTED_VIOLATION;
  321. return X509_V_OK;
  322. }
  323. static int nc_dns(ASN1_IA5STRING *dns, ASN1_IA5STRING *base)
  324. {
  325. char *baseptr = (char *)base->data;
  326. char *dnsptr = (char *)dns->data;
  327. /* Empty matches everything */
  328. if (!*baseptr)
  329. return X509_V_OK;
  330. /*
  331. * Otherwise can add zero or more components on the left so compare RHS
  332. * and if dns is longer and expect '.' as preceding character.
  333. */
  334. if (dns->length > base->length) {
  335. dnsptr += dns->length - base->length;
  336. if (*baseptr != '.' && dnsptr[-1] != '.')
  337. return X509_V_ERR_PERMITTED_VIOLATION;
  338. }
  339. if (OPENSSL_strcasecmp(baseptr, dnsptr))
  340. return X509_V_ERR_PERMITTED_VIOLATION;
  341. return X509_V_OK;
  342. }
  343. static int nc_email(ASN1_IA5STRING *eml, ASN1_IA5STRING *base)
  344. {
  345. const char *baseptr = (char *)base->data;
  346. const char *emlptr = (char *)eml->data;
  347. const char *baseat = strchr(baseptr, '@');
  348. const char *emlat = strchr(emlptr, '@');
  349. if (!emlat)
  350. return X509_V_ERR_UNSUPPORTED_NAME_SYNTAX;
  351. /* Special case: inital '.' is RHS match */
  352. if (!baseat && (*baseptr == '.')) {
  353. if (eml->length > base->length) {
  354. emlptr += eml->length - base->length;
  355. if (!OPENSSL_strcasecmp(baseptr, emlptr))
  356. return X509_V_OK;
  357. }
  358. return X509_V_ERR_PERMITTED_VIOLATION;
  359. }
  360. /* If we have anything before '@' match local part */
  361. if (baseat) {
  362. if (baseat != baseptr) {
  363. if ((baseat - baseptr) != (emlat - emlptr))
  364. return X509_V_ERR_PERMITTED_VIOLATION;
  365. /* Case sensitive match of local part */
  366. if (strncmp(baseptr, emlptr, emlat - emlptr))
  367. return X509_V_ERR_PERMITTED_VIOLATION;
  368. }
  369. /* Position base after '@' */
  370. baseptr = baseat + 1;
  371. }
  372. emlptr = emlat + 1;
  373. /* Just have hostname left to match: case insensitive */
  374. if (OPENSSL_strcasecmp(baseptr, emlptr))
  375. return X509_V_ERR_PERMITTED_VIOLATION;
  376. return X509_V_OK;
  377. }
  378. static int nc_uri(ASN1_IA5STRING *uri, ASN1_IA5STRING *base)
  379. {
  380. const char *baseptr = (char *)base->data;
  381. const char *hostptr = (char *)uri->data;
  382. const char *p = strchr(hostptr, ':');
  383. int hostlen;
  384. /* Check for foo:// and skip past it */
  385. if (!p || (p[1] != '/') || (p[2] != '/'))
  386. return X509_V_ERR_UNSUPPORTED_NAME_SYNTAX;
  387. hostptr = p + 3;
  388. /* Determine length of hostname part of URI */
  389. /* Look for a port indicator as end of hostname first */
  390. p = strchr(hostptr, ':');
  391. /* Otherwise look for trailing slash */
  392. if (!p)
  393. p = strchr(hostptr, '/');
  394. if (!p)
  395. hostlen = strlen(hostptr);
  396. else
  397. hostlen = p - hostptr;
  398. if (hostlen == 0)
  399. return X509_V_ERR_UNSUPPORTED_NAME_SYNTAX;
  400. /* Special case: inital '.' is RHS match */
  401. if (*baseptr == '.') {
  402. if (hostlen > base->length) {
  403. p = hostptr + hostlen - base->length;
  404. if (!OPENSSL_strncasecmp(p, baseptr, base->length))
  405. return X509_V_OK;
  406. }
  407. return X509_V_ERR_PERMITTED_VIOLATION;
  408. }
  409. if ((base->length != (int)hostlen)
  410. || OPENSSL_strncasecmp(hostptr, baseptr, hostlen))
  411. return X509_V_ERR_PERMITTED_VIOLATION;
  412. return X509_V_OK;
  413. }