v3_ncons.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  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. Return
  200. * values: X509_V_OK: All constraints obeyed.
  201. * X509_V_ERR_PERMITTED_VIOLATION: Permitted subtree violation.
  202. * X509_V_ERR_EXCLUDED_VIOLATION: Excluded subtree violation.
  203. * X509_V_ERR_SUBTREE_MINMAX: Min or max values present and matching type.
  204. * X509_V_ERR_UNSUPPORTED_CONSTRAINT_TYPE: Unsupported constraint type.
  205. * X509_V_ERR_UNSUPPORTED_CONSTRAINT_SYNTAX: bad unsupported constraint
  206. * syntax. X509_V_ERR_UNSUPPORTED_NAME_SYNTAX: bad or unsupported syntax of
  207. * name
  208. *
  209. */
  210. int NAME_CONSTRAINTS_check(X509 *x, NAME_CONSTRAINTS *nc)
  211. {
  212. int r, i;
  213. size_t j;
  214. X509_NAME *nm;
  215. nm = X509_get_subject_name(x);
  216. if (X509_NAME_entry_count(nm) > 0) {
  217. GENERAL_NAME gntmp;
  218. gntmp.type = GEN_DIRNAME;
  219. gntmp.d.directoryName = nm;
  220. r = nc_match(&gntmp, nc);
  221. if (r != X509_V_OK)
  222. return r;
  223. gntmp.type = GEN_EMAIL;
  224. /* Process any email address attributes in subject name */
  225. for (i = -1;;) {
  226. X509_NAME_ENTRY *ne;
  227. i = X509_NAME_get_index_by_NID(nm, NID_pkcs9_emailAddress, i);
  228. if (i == -1)
  229. break;
  230. ne = X509_NAME_get_entry(nm, i);
  231. gntmp.d.rfc822Name = X509_NAME_ENTRY_get_data(ne);
  232. if (gntmp.d.rfc822Name->type != V_ASN1_IA5STRING)
  233. return X509_V_ERR_UNSUPPORTED_NAME_SYNTAX;
  234. r = nc_match(&gntmp, nc);
  235. if (r != X509_V_OK)
  236. return r;
  237. }
  238. }
  239. for (j = 0; j < sk_GENERAL_NAME_num(x->altname); j++) {
  240. GENERAL_NAME *gen = sk_GENERAL_NAME_value(x->altname, j);
  241. r = nc_match(gen, nc);
  242. if (r != X509_V_OK)
  243. return r;
  244. }
  245. return X509_V_OK;
  246. }
  247. static int nc_match(GENERAL_NAME *gen, NAME_CONSTRAINTS *nc)
  248. {
  249. GENERAL_SUBTREE *sub;
  250. int r, match = 0;
  251. size_t i;
  252. /*
  253. * Permitted subtrees: if any subtrees exist of matching the type at
  254. * least one subtree must match.
  255. */
  256. for (i = 0; i < sk_GENERAL_SUBTREE_num(nc->permittedSubtrees); i++) {
  257. sub = sk_GENERAL_SUBTREE_value(nc->permittedSubtrees, i);
  258. if (gen->type != sub->base->type)
  259. continue;
  260. if (sub->minimum || sub->maximum)
  261. return X509_V_ERR_SUBTREE_MINMAX;
  262. /* If we already have a match don't bother trying any more */
  263. if (match == 2)
  264. continue;
  265. if (match == 0)
  266. match = 1;
  267. r = nc_match_single(gen, sub->base);
  268. if (r == X509_V_OK)
  269. match = 2;
  270. else if (r != X509_V_ERR_PERMITTED_VIOLATION)
  271. return r;
  272. }
  273. if (match == 1)
  274. return X509_V_ERR_PERMITTED_VIOLATION;
  275. /* Excluded subtrees: must not match any of these */
  276. for (i = 0; i < sk_GENERAL_SUBTREE_num(nc->excludedSubtrees); i++) {
  277. sub = sk_GENERAL_SUBTREE_value(nc->excludedSubtrees, i);
  278. if (gen->type != sub->base->type)
  279. continue;
  280. if (sub->minimum || sub->maximum)
  281. return X509_V_ERR_SUBTREE_MINMAX;
  282. r = nc_match_single(gen, sub->base);
  283. if (r == X509_V_OK)
  284. return X509_V_ERR_EXCLUDED_VIOLATION;
  285. else if (r != X509_V_ERR_PERMITTED_VIOLATION)
  286. return r;
  287. }
  288. return X509_V_OK;
  289. }
  290. static int nc_match_single(GENERAL_NAME *gen, GENERAL_NAME *base)
  291. {
  292. switch (base->type) {
  293. case GEN_DIRNAME:
  294. return nc_dn(gen->d.directoryName, base->d.directoryName);
  295. case GEN_DNS:
  296. return nc_dns(gen->d.dNSName, base->d.dNSName);
  297. case GEN_EMAIL:
  298. return nc_email(gen->d.rfc822Name, base->d.rfc822Name);
  299. case GEN_URI:
  300. return nc_uri(gen->d.uniformResourceIdentifier,
  301. base->d.uniformResourceIdentifier);
  302. default:
  303. return X509_V_ERR_UNSUPPORTED_CONSTRAINT_TYPE;
  304. }
  305. }
  306. /*
  307. * directoryName name constraint matching. The canonical encoding of
  308. * X509_NAME makes this comparison easy. It is matched if the subtree is a
  309. * subset of the name.
  310. */
  311. static int nc_dn(X509_NAME *nm, X509_NAME *base)
  312. {
  313. /* Ensure canonical encodings are up to date. */
  314. if (nm->modified && i2d_X509_NAME(nm, NULL) < 0)
  315. return X509_V_ERR_OUT_OF_MEM;
  316. if (base->modified && i2d_X509_NAME(base, NULL) < 0)
  317. return X509_V_ERR_OUT_OF_MEM;
  318. if (base->canon_enclen > nm->canon_enclen)
  319. return X509_V_ERR_PERMITTED_VIOLATION;
  320. if (OPENSSL_memcmp(base->canon_enc, nm->canon_enc, base->canon_enclen))
  321. return X509_V_ERR_PERMITTED_VIOLATION;
  322. return X509_V_OK;
  323. }
  324. static int nc_dns(ASN1_IA5STRING *dns, ASN1_IA5STRING *base)
  325. {
  326. char *baseptr = (char *)base->data;
  327. char *dnsptr = (char *)dns->data;
  328. /* Empty matches everything */
  329. if (!*baseptr)
  330. return X509_V_OK;
  331. /*
  332. * Otherwise can add zero or more components on the left so compare RHS
  333. * and if dns is longer and expect '.' as preceding character.
  334. */
  335. if (dns->length > base->length) {
  336. dnsptr += dns->length - base->length;
  337. if (*baseptr != '.' && dnsptr[-1] != '.')
  338. return X509_V_ERR_PERMITTED_VIOLATION;
  339. }
  340. if (OPENSSL_strcasecmp(baseptr, dnsptr))
  341. return X509_V_ERR_PERMITTED_VIOLATION;
  342. return X509_V_OK;
  343. }
  344. static int nc_email(ASN1_IA5STRING *eml, ASN1_IA5STRING *base)
  345. {
  346. const char *baseptr = (char *)base->data;
  347. const char *emlptr = (char *)eml->data;
  348. const char *baseat = strchr(baseptr, '@');
  349. const char *emlat = strchr(emlptr, '@');
  350. if (!emlat)
  351. return X509_V_ERR_UNSUPPORTED_NAME_SYNTAX;
  352. /* Special case: inital '.' is RHS match */
  353. if (!baseat && (*baseptr == '.')) {
  354. if (eml->length > base->length) {
  355. emlptr += eml->length - base->length;
  356. if (!OPENSSL_strcasecmp(baseptr, emlptr))
  357. return X509_V_OK;
  358. }
  359. return X509_V_ERR_PERMITTED_VIOLATION;
  360. }
  361. /* If we have anything before '@' match local part */
  362. if (baseat) {
  363. if (baseat != baseptr) {
  364. if ((baseat - baseptr) != (emlat - emlptr))
  365. return X509_V_ERR_PERMITTED_VIOLATION;
  366. /* Case sensitive match of local part */
  367. if (strncmp(baseptr, emlptr, emlat - emlptr))
  368. return X509_V_ERR_PERMITTED_VIOLATION;
  369. }
  370. /* Position base after '@' */
  371. baseptr = baseat + 1;
  372. }
  373. emlptr = emlat + 1;
  374. /* Just have hostname left to match: case insensitive */
  375. if (OPENSSL_strcasecmp(baseptr, emlptr))
  376. return X509_V_ERR_PERMITTED_VIOLATION;
  377. return X509_V_OK;
  378. }
  379. static int nc_uri(ASN1_IA5STRING *uri, ASN1_IA5STRING *base)
  380. {
  381. const char *baseptr = (char *)base->data;
  382. const char *hostptr = (char *)uri->data;
  383. const char *p = strchr(hostptr, ':');
  384. int hostlen;
  385. /* Check for foo:// and skip past it */
  386. if (!p || (p[1] != '/') || (p[2] != '/'))
  387. return X509_V_ERR_UNSUPPORTED_NAME_SYNTAX;
  388. hostptr = p + 3;
  389. /* Determine length of hostname part of URI */
  390. /* Look for a port indicator as end of hostname first */
  391. p = strchr(hostptr, ':');
  392. /* Otherwise look for trailing slash */
  393. if (!p)
  394. p = strchr(hostptr, '/');
  395. if (!p)
  396. hostlen = strlen(hostptr);
  397. else
  398. hostlen = p - hostptr;
  399. if (hostlen == 0)
  400. return X509_V_ERR_UNSUPPORTED_NAME_SYNTAX;
  401. /* Special case: inital '.' is RHS match */
  402. if (*baseptr == '.') {
  403. if (hostlen > base->length) {
  404. p = hostptr + hostlen - base->length;
  405. if (!OPENSSL_strncasecmp(p, baseptr, base->length))
  406. return X509_V_OK;
  407. }
  408. return X509_V_ERR_PERMITTED_VIOLATION;
  409. }
  410. if ((base->length != (int)hostlen)
  411. || OPENSSL_strncasecmp(hostptr, baseptr, hostlen))
  412. return X509_V_ERR_PERMITTED_VIOLATION;
  413. return X509_V_OK;
  414. }