1
0

v3_ncons.c 15 KB

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