v3_ncons.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  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. *
  58. */
  59. #include <stdio.h>
  60. #include "cryptlib.h"
  61. #include <openssl/asn1t.h>
  62. #include <openssl/conf.h>
  63. #include <openssl/x509v3.h>
  64. static void *v2i_NAME_CONSTRAINTS(const X509V3_EXT_METHOD *method,
  65. X509V3_CTX *ctx,
  66. STACK_OF(CONF_VALUE) *nval);
  67. static int i2r_NAME_CONSTRAINTS(const X509V3_EXT_METHOD *method, void *a,
  68. BIO *bp, int ind);
  69. static int do_i2r_name_constraints(const X509V3_EXT_METHOD *method,
  70. STACK_OF(GENERAL_SUBTREE) *trees, BIO *bp,
  71. int ind, char *name);
  72. static int print_nc_ipadd(BIO *bp, ASN1_OCTET_STRING *ip);
  73. static int nc_match(GENERAL_NAME *gen, NAME_CONSTRAINTS *nc);
  74. static int nc_match_single(GENERAL_NAME *sub, GENERAL_NAME *gen);
  75. static int nc_dn(X509_NAME *sub, X509_NAME *nm);
  76. static int nc_dns(ASN1_IA5STRING *sub, ASN1_IA5STRING *dns);
  77. static int nc_email(ASN1_IA5STRING *sub, ASN1_IA5STRING *eml);
  78. static int nc_uri(ASN1_IA5STRING *uri, ASN1_IA5STRING *base);
  79. const X509V3_EXT_METHOD v3_name_constraints = {
  80. NID_name_constraints, 0,
  81. ASN1_ITEM_ref(NAME_CONSTRAINTS),
  82. 0, 0, 0, 0,
  83. 0, 0,
  84. 0, v2i_NAME_CONSTRAINTS,
  85. i2r_NAME_CONSTRAINTS, 0,
  86. NULL
  87. };
  88. ASN1_SEQUENCE(GENERAL_SUBTREE) = {
  89. ASN1_SIMPLE(GENERAL_SUBTREE, base, GENERAL_NAME),
  90. ASN1_IMP_OPT(GENERAL_SUBTREE, minimum, ASN1_INTEGER, 0),
  91. ASN1_IMP_OPT(GENERAL_SUBTREE, maximum, ASN1_INTEGER, 1)
  92. } ASN1_SEQUENCE_END(GENERAL_SUBTREE)
  93. ASN1_SEQUENCE(NAME_CONSTRAINTS) = {
  94. ASN1_IMP_SEQUENCE_OF_OPT(NAME_CONSTRAINTS, permittedSubtrees,
  95. GENERAL_SUBTREE, 0),
  96. ASN1_IMP_SEQUENCE_OF_OPT(NAME_CONSTRAINTS, excludedSubtrees,
  97. GENERAL_SUBTREE, 1),
  98. } ASN1_SEQUENCE_END(NAME_CONSTRAINTS)
  99. IMPLEMENT_ASN1_ALLOC_FUNCTIONS(GENERAL_SUBTREE)
  100. IMPLEMENT_ASN1_ALLOC_FUNCTIONS(NAME_CONSTRAINTS)
  101. static void *v2i_NAME_CONSTRAINTS(const X509V3_EXT_METHOD *method,
  102. X509V3_CTX *ctx, STACK_OF(CONF_VALUE) *nval)
  103. {
  104. int i;
  105. CONF_VALUE tval, *val;
  106. STACK_OF(GENERAL_SUBTREE) **ptree = NULL;
  107. NAME_CONSTRAINTS *ncons = NULL;
  108. GENERAL_SUBTREE *sub = NULL;
  109. ncons = NAME_CONSTRAINTS_new();
  110. if (!ncons)
  111. goto memerr;
  112. for (i = 0; i < sk_CONF_VALUE_num(nval); i++) {
  113. val = sk_CONF_VALUE_value(nval, i);
  114. if (!strncmp(val->name, "permitted", 9) && val->name[9]) {
  115. ptree = &ncons->permittedSubtrees;
  116. tval.name = val->name + 10;
  117. } else if (!strncmp(val->name, "excluded", 8) && val->name[8]) {
  118. ptree = &ncons->excludedSubtrees;
  119. tval.name = val->name + 9;
  120. } else {
  121. X509V3err(X509V3_F_V2I_NAME_CONSTRAINTS, X509V3_R_INVALID_SYNTAX);
  122. goto err;
  123. }
  124. tval.value = val->value;
  125. sub = GENERAL_SUBTREE_new();
  126. if (sub == NULL)
  127. goto memerr;
  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. X509V3err(X509V3_F_V2I_NAME_CONSTRAINTS, 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, char *name)
  159. {
  160. GENERAL_SUBTREE *tree;
  161. int 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_UNSUPPORTED_CONSTRAINT_TYPE: Unsupported constraint type.
  206. * X509_V_ERR_UNSUPPORTED_CONSTRAINT_SYNTAX: bad unsupported constraint syntax.
  207. * X509_V_ERR_UNSUPPORTED_NAME_SYNTAX: bad or unsupported syntax of name
  208. */
  209. int NAME_CONSTRAINTS_check(X509 *x, NAME_CONSTRAINTS *nc)
  210. {
  211. int r, i;
  212. X509_NAME *nm;
  213. nm = X509_get_subject_name(x);
  214. if (X509_NAME_entry_count(nm) > 0) {
  215. GENERAL_NAME gntmp;
  216. gntmp.type = GEN_DIRNAME;
  217. gntmp.d.directoryName = nm;
  218. r = nc_match(&gntmp, nc);
  219. if (r != X509_V_OK)
  220. return r;
  221. gntmp.type = GEN_EMAIL;
  222. /* Process any email address attributes in subject name */
  223. for (i = -1;;) {
  224. X509_NAME_ENTRY *ne;
  225. i = X509_NAME_get_index_by_NID(nm, NID_pkcs9_emailAddress, i);
  226. if (i == -1)
  227. break;
  228. ne = X509_NAME_get_entry(nm, i);
  229. gntmp.d.rfc822Name = X509_NAME_ENTRY_get_data(ne);
  230. if (gntmp.d.rfc822Name->type != V_ASN1_IA5STRING)
  231. return X509_V_ERR_UNSUPPORTED_NAME_SYNTAX;
  232. r = nc_match(&gntmp, nc);
  233. if (r != X509_V_OK)
  234. return r;
  235. }
  236. }
  237. for (i = 0; i < sk_GENERAL_NAME_num(x->altname); i++) {
  238. GENERAL_NAME *gen = sk_GENERAL_NAME_value(x->altname, i);
  239. r = nc_match(gen, nc);
  240. if (r != X509_V_OK)
  241. return r;
  242. }
  243. return X509_V_OK;
  244. }
  245. static int nc_match(GENERAL_NAME *gen, NAME_CONSTRAINTS *nc)
  246. {
  247. GENERAL_SUBTREE *sub;
  248. int i, r, match = 0;
  249. /*
  250. * Permitted subtrees: if any subtrees exist of matching the type at
  251. * least one subtree must match.
  252. */
  253. for (i = 0; i < sk_GENERAL_SUBTREE_num(nc->permittedSubtrees); i++) {
  254. sub = sk_GENERAL_SUBTREE_value(nc->permittedSubtrees, i);
  255. if (gen->type != sub->base->type)
  256. continue;
  257. if (sub->minimum || sub->maximum)
  258. return X509_V_ERR_SUBTREE_MINMAX;
  259. /* If we already have a match don't bother trying any more */
  260. if (match == 2)
  261. continue;
  262. if (match == 0)
  263. match = 1;
  264. r = nc_match_single(gen, sub->base);
  265. if (r == X509_V_OK)
  266. match = 2;
  267. else if (r != X509_V_ERR_PERMITTED_VIOLATION)
  268. return r;
  269. }
  270. if (match == 1)
  271. return X509_V_ERR_PERMITTED_VIOLATION;
  272. /* Excluded subtrees: must not match any of these */
  273. for (i = 0; i < sk_GENERAL_SUBTREE_num(nc->excludedSubtrees); i++) {
  274. sub = sk_GENERAL_SUBTREE_value(nc->excludedSubtrees, i);
  275. if (gen->type != sub->base->type)
  276. continue;
  277. if (sub->minimum || sub->maximum)
  278. return X509_V_ERR_SUBTREE_MINMAX;
  279. r = nc_match_single(gen, sub->base);
  280. if (r == X509_V_OK)
  281. return X509_V_ERR_EXCLUDED_VIOLATION;
  282. else if (r != X509_V_ERR_PERMITTED_VIOLATION)
  283. return r;
  284. }
  285. return X509_V_OK;
  286. }
  287. static int nc_match_single(GENERAL_NAME *gen, GENERAL_NAME *base)
  288. {
  289. switch (base->type) {
  290. case GEN_DIRNAME:
  291. return nc_dn(gen->d.directoryName, base->d.directoryName);
  292. case GEN_DNS:
  293. return nc_dns(gen->d.dNSName, base->d.dNSName);
  294. case GEN_EMAIL:
  295. return nc_email(gen->d.rfc822Name, base->d.rfc822Name);
  296. case GEN_URI:
  297. return nc_uri(gen->d.uniformResourceIdentifier,
  298. base->d.uniformResourceIdentifier);
  299. default:
  300. return X509_V_ERR_UNSUPPORTED_CONSTRAINT_TYPE;
  301. }
  302. }
  303. /*
  304. * directoryName name constraint matching. The canonical encoding of
  305. * X509_NAME makes this comparison easy. It is matched if the subtree is a
  306. * subset of the name.
  307. */
  308. static int nc_dn(X509_NAME *nm, X509_NAME *base)
  309. {
  310. /* Ensure canonical encodings are up to date. */
  311. if (nm->modified && i2d_X509_NAME(nm, NULL) < 0)
  312. return X509_V_ERR_OUT_OF_MEM;
  313. if (base->modified && i2d_X509_NAME(base, NULL) < 0)
  314. return X509_V_ERR_OUT_OF_MEM;
  315. if (base->canon_enclen > nm->canon_enclen)
  316. return X509_V_ERR_PERMITTED_VIOLATION;
  317. if (memcmp(base->canon_enc, nm->canon_enc, base->canon_enclen))
  318. return X509_V_ERR_PERMITTED_VIOLATION;
  319. return X509_V_OK;
  320. }
  321. static int nc_dns(ASN1_IA5STRING *dns, ASN1_IA5STRING *base)
  322. {
  323. char *baseptr = (char *)base->data;
  324. char *dnsptr = (char *)dns->data;
  325. /* Empty matches everything */
  326. if (!*baseptr)
  327. return X509_V_OK;
  328. /*
  329. * Otherwise can add zero or more components on the left so compare RHS
  330. * and if dns is longer and expect '.' as preceding character.
  331. */
  332. if (dns->length > base->length) {
  333. dnsptr += dns->length - base->length;
  334. if (*baseptr != '.' && dnsptr[-1] != '.')
  335. return X509_V_ERR_PERMITTED_VIOLATION;
  336. }
  337. if (strcasecmp(baseptr, dnsptr))
  338. return X509_V_ERR_PERMITTED_VIOLATION;
  339. return X509_V_OK;
  340. }
  341. static int nc_email(ASN1_IA5STRING *eml, ASN1_IA5STRING *base)
  342. {
  343. const char *baseptr = (char *)base->data;
  344. const char *emlptr = (char *)eml->data;
  345. const char *baseat = strchr(baseptr, '@');
  346. const char *emlat = strchr(emlptr, '@');
  347. if (!emlat)
  348. return X509_V_ERR_UNSUPPORTED_NAME_SYNTAX;
  349. /* Special case: inital '.' is RHS match */
  350. if (!baseat && (*baseptr == '.')) {
  351. if (eml->length > base->length) {
  352. emlptr += eml->length - base->length;
  353. if (!strcasecmp(baseptr, emlptr))
  354. return X509_V_OK;
  355. }
  356. return X509_V_ERR_PERMITTED_VIOLATION;
  357. }
  358. /* If we have anything before '@' match local part */
  359. if (baseat) {
  360. if (baseat != baseptr) {
  361. if ((baseat - baseptr) != (emlat - emlptr))
  362. return X509_V_ERR_PERMITTED_VIOLATION;
  363. /* Case sensitive match of local part */
  364. if (strncmp(baseptr, emlptr, emlat - emlptr))
  365. return X509_V_ERR_PERMITTED_VIOLATION;
  366. }
  367. /* Position base after '@' */
  368. baseptr = baseat + 1;
  369. }
  370. emlptr = emlat + 1;
  371. /* Just have hostname left to match: case insensitive */
  372. if (strcasecmp(baseptr, emlptr))
  373. return X509_V_ERR_PERMITTED_VIOLATION;
  374. return X509_V_OK;
  375. }
  376. static int nc_uri(ASN1_IA5STRING *uri, ASN1_IA5STRING *base)
  377. {
  378. const char *baseptr = (char *)base->data;
  379. const char *hostptr = (char *)uri->data;
  380. const char *p = strchr(hostptr, ':');
  381. int hostlen;
  382. /* Check for foo:// and skip past it */
  383. if (!p || (p[1] != '/') || (p[2] != '/'))
  384. return X509_V_ERR_UNSUPPORTED_NAME_SYNTAX;
  385. hostptr = p + 3;
  386. /* Determine length of hostname part of URI */
  387. /* Look for a port indicator as end of hostname first */
  388. p = strchr(hostptr, ':');
  389. /* Otherwise look for trailing slash */
  390. if (!p)
  391. p = strchr(hostptr, '/');
  392. if (!p)
  393. hostlen = strlen(hostptr);
  394. else
  395. hostlen = p - hostptr;
  396. if (hostlen == 0)
  397. return X509_V_ERR_UNSUPPORTED_NAME_SYNTAX;
  398. /* Special case: inital '.' is RHS match */
  399. if (*baseptr == '.') {
  400. if (hostlen > base->length) {
  401. p = hostptr + hostlen - base->length;
  402. if (!strncasecmp(p, baseptr, base->length))
  403. return X509_V_OK;
  404. }
  405. return X509_V_ERR_PERMITTED_VIOLATION;
  406. }
  407. if ((base->length != (int)hostlen)
  408. || strncasecmp(hostptr, baseptr, hostlen))
  409. return X509_V_ERR_PERMITTED_VIOLATION;
  410. return X509_V_OK;
  411. }