v3_ncons.c 17 KB

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