v3_ncons.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697
  1. /*
  2. * Copyright 2003-2020 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include "internal/cryptlib.h"
  10. #include "internal/numbers.h"
  11. #include <stdio.h>
  12. #include "crypto/asn1.h"
  13. #include <openssl/asn1t.h>
  14. #include <openssl/conf.h>
  15. #include <openssl/x509v3.h>
  16. #include <openssl/bn.h>
  17. #include "crypto/x509.h"
  18. #include "ext_dat.h"
  19. DEFINE_STACK_OF(CONF_VALUE)
  20. DEFINE_STACK_OF(GENERAL_NAME)
  21. DEFINE_STACK_OF(GENERAL_SUBTREE)
  22. static void *v2i_NAME_CONSTRAINTS(const X509V3_EXT_METHOD *method,
  23. X509V3_CTX *ctx,
  24. STACK_OF(CONF_VALUE) *nval);
  25. static int i2r_NAME_CONSTRAINTS(const X509V3_EXT_METHOD *method, void *a,
  26. BIO *bp, int ind);
  27. static int do_i2r_name_constraints(const X509V3_EXT_METHOD *method,
  28. STACK_OF(GENERAL_SUBTREE) *trees, BIO *bp,
  29. int ind, const char *name);
  30. static int print_nc_ipadd(BIO *bp, ASN1_OCTET_STRING *ip);
  31. static int nc_match(GENERAL_NAME *gen, NAME_CONSTRAINTS *nc);
  32. static int nc_match_single(GENERAL_NAME *sub, GENERAL_NAME *gen);
  33. static int nc_dn(const X509_NAME *sub, const X509_NAME *nm);
  34. static int nc_dns(ASN1_IA5STRING *sub, ASN1_IA5STRING *dns);
  35. static int nc_email(ASN1_IA5STRING *sub, ASN1_IA5STRING *eml);
  36. static int nc_uri(ASN1_IA5STRING *uri, ASN1_IA5STRING *base);
  37. static int nc_ip(ASN1_OCTET_STRING *ip, ASN1_OCTET_STRING *base);
  38. const X509V3_EXT_METHOD v3_name_constraints = {
  39. NID_name_constraints, 0,
  40. ASN1_ITEM_ref(NAME_CONSTRAINTS),
  41. 0, 0, 0, 0,
  42. 0, 0,
  43. 0, v2i_NAME_CONSTRAINTS,
  44. i2r_NAME_CONSTRAINTS, 0,
  45. NULL
  46. };
  47. ASN1_SEQUENCE(GENERAL_SUBTREE) = {
  48. ASN1_SIMPLE(GENERAL_SUBTREE, base, GENERAL_NAME),
  49. ASN1_IMP_OPT(GENERAL_SUBTREE, minimum, ASN1_INTEGER, 0),
  50. ASN1_IMP_OPT(GENERAL_SUBTREE, maximum, ASN1_INTEGER, 1)
  51. } ASN1_SEQUENCE_END(GENERAL_SUBTREE)
  52. ASN1_SEQUENCE(NAME_CONSTRAINTS) = {
  53. ASN1_IMP_SEQUENCE_OF_OPT(NAME_CONSTRAINTS, permittedSubtrees,
  54. GENERAL_SUBTREE, 0),
  55. ASN1_IMP_SEQUENCE_OF_OPT(NAME_CONSTRAINTS, excludedSubtrees,
  56. GENERAL_SUBTREE, 1),
  57. } ASN1_SEQUENCE_END(NAME_CONSTRAINTS)
  58. IMPLEMENT_ASN1_ALLOC_FUNCTIONS(GENERAL_SUBTREE)
  59. IMPLEMENT_ASN1_ALLOC_FUNCTIONS(NAME_CONSTRAINTS)
  60. /*
  61. * We cannot use strncasecmp here because that applies locale specific rules.
  62. * For example in Turkish 'I' is not the uppercase character for 'i'. We need to
  63. * do a simple ASCII case comparison ignoring the locale (that is why we use
  64. * numeric constants below).
  65. */
  66. static int ia5ncasecmp(const char *s1, const char *s2, size_t n)
  67. {
  68. for (; n > 0; n--, s1++, s2++) {
  69. if (*s1 != *s2) {
  70. unsigned char c1 = (unsigned char)*s1, c2 = (unsigned char)*s2;
  71. /* Convert to lower case */
  72. if (c1 >= 0x41 /* A */ && c1 <= 0x5A /* Z */)
  73. c1 += 0x20;
  74. if (c2 >= 0x41 /* A */ && c2 <= 0x5A /* Z */)
  75. c2 += 0x20;
  76. if (c1 == c2)
  77. continue;
  78. if (c1 < c2)
  79. return -1;
  80. /* c1 > c2 */
  81. return 1;
  82. } else if (*s1 == 0) {
  83. /* If we get here we know that *s2 == 0 too */
  84. return 0;
  85. }
  86. }
  87. return 0;
  88. }
  89. static int ia5casecmp(const char *s1, const char *s2)
  90. {
  91. return ia5ncasecmp(s1, s2, SIZE_MAX);
  92. }
  93. static void *v2i_NAME_CONSTRAINTS(const X509V3_EXT_METHOD *method,
  94. X509V3_CTX *ctx, STACK_OF(CONF_VALUE) *nval)
  95. {
  96. int i;
  97. CONF_VALUE tval, *val;
  98. STACK_OF(GENERAL_SUBTREE) **ptree = NULL;
  99. NAME_CONSTRAINTS *ncons = NULL;
  100. GENERAL_SUBTREE *sub = NULL;
  101. ncons = NAME_CONSTRAINTS_new();
  102. if (ncons == NULL)
  103. goto memerr;
  104. for (i = 0; i < sk_CONF_VALUE_num(nval); i++) {
  105. val = sk_CONF_VALUE_value(nval, i);
  106. if (strncmp(val->name, "permitted", 9) == 0 && val->name[9]) {
  107. ptree = &ncons->permittedSubtrees;
  108. tval.name = val->name + 10;
  109. } else if (strncmp(val->name, "excluded", 8) == 0 && val->name[8]) {
  110. ptree = &ncons->excludedSubtrees;
  111. tval.name = val->name + 9;
  112. } else {
  113. X509V3err(X509V3_F_V2I_NAME_CONSTRAINTS, X509V3_R_INVALID_SYNTAX);
  114. goto err;
  115. }
  116. tval.value = val->value;
  117. sub = GENERAL_SUBTREE_new();
  118. if (sub == NULL)
  119. goto memerr;
  120. if (!v2i_GENERAL_NAME_ex(sub->base, method, ctx, &tval, 1))
  121. goto err;
  122. if (*ptree == NULL)
  123. *ptree = sk_GENERAL_SUBTREE_new_null();
  124. if (*ptree == NULL || !sk_GENERAL_SUBTREE_push(*ptree, sub))
  125. goto memerr;
  126. sub = NULL;
  127. }
  128. return ncons;
  129. memerr:
  130. X509V3err(X509V3_F_V2I_NAME_CONSTRAINTS, ERR_R_MALLOC_FAILURE);
  131. err:
  132. NAME_CONSTRAINTS_free(ncons);
  133. GENERAL_SUBTREE_free(sub);
  134. return NULL;
  135. }
  136. static int i2r_NAME_CONSTRAINTS(const X509V3_EXT_METHOD *method, void *a,
  137. BIO *bp, int ind)
  138. {
  139. NAME_CONSTRAINTS *ncons = a;
  140. do_i2r_name_constraints(method, ncons->permittedSubtrees,
  141. bp, ind, "Permitted");
  142. if (ncons->permittedSubtrees && ncons->excludedSubtrees)
  143. BIO_puts(bp, "\n");
  144. do_i2r_name_constraints(method, ncons->excludedSubtrees,
  145. bp, ind, "Excluded");
  146. return 1;
  147. }
  148. static int do_i2r_name_constraints(const X509V3_EXT_METHOD *method,
  149. STACK_OF(GENERAL_SUBTREE) *trees,
  150. BIO *bp, int ind, const char *name)
  151. {
  152. GENERAL_SUBTREE *tree;
  153. int i;
  154. if (sk_GENERAL_SUBTREE_num(trees) > 0)
  155. BIO_printf(bp, "%*s%s:\n", ind, "", name);
  156. for (i = 0; i < sk_GENERAL_SUBTREE_num(trees); i++) {
  157. if (i > 0)
  158. BIO_puts(bp, "\n");
  159. tree = sk_GENERAL_SUBTREE_value(trees, i);
  160. BIO_printf(bp, "%*s", ind + 2, "");
  161. if (tree->base->type == GEN_IPADD)
  162. print_nc_ipadd(bp, tree->base->d.ip);
  163. else
  164. GENERAL_NAME_print(bp, tree->base);
  165. }
  166. return 1;
  167. }
  168. static int print_nc_ipadd(BIO *bp, ASN1_OCTET_STRING *ip)
  169. {
  170. /* ip->length should be 8 or 32 and len1 == len2 == 4 or len1 == len2 == 16 */
  171. int len1 = ip->length >= 16 ? 16 : ip->length >= 4 ? 4 : ip->length;
  172. int len2 = ip->length - len1;
  173. char *ip1 = ipaddr_to_asc(ip->data, len1);
  174. char *ip2 = ipaddr_to_asc(ip->data + len1, len2);
  175. int ret = ip1 != NULL && ip2 != NULL
  176. && BIO_printf(bp, "IP:%s/%s", ip1, ip2) > 0;
  177. OPENSSL_free(ip1);
  178. OPENSSL_free(ip2);
  179. return ret;
  180. }
  181. #define NAME_CHECK_MAX (1 << 20)
  182. static int add_lengths(int *out, int a, int b)
  183. {
  184. /* sk_FOO_num(NULL) returns -1 but is effectively 0 when iterating. */
  185. if (a < 0)
  186. a = 0;
  187. if (b < 0)
  188. b = 0;
  189. if (a > INT_MAX - b)
  190. return 0;
  191. *out = a + b;
  192. return 1;
  193. }
  194. /*-
  195. * Check a certificate conforms to a specified set of constraints.
  196. * Return values:
  197. * X509_V_OK: All constraints obeyed.
  198. * X509_V_ERR_PERMITTED_VIOLATION: Permitted subtree violation.
  199. * X509_V_ERR_EXCLUDED_VIOLATION: Excluded subtree violation.
  200. * X509_V_ERR_SUBTREE_MINMAX: Min or max values present and matching type.
  201. * X509_V_ERR_UNSUPPORTED_CONSTRAINT_TYPE: Unsupported constraint type.
  202. * X509_V_ERR_UNSUPPORTED_CONSTRAINT_SYNTAX: bad unsupported constraint syntax.
  203. * X509_V_ERR_UNSUPPORTED_NAME_SYNTAX: bad or unsupported syntax of name
  204. */
  205. int NAME_CONSTRAINTS_check(X509 *x, NAME_CONSTRAINTS *nc)
  206. {
  207. int r, i, name_count, constraint_count;
  208. X509_NAME *nm;
  209. nm = X509_get_subject_name(x);
  210. /*
  211. * Guard against certificates with an excessive number of names or
  212. * constraints causing a computationally expensive name constraints check.
  213. */
  214. if (!add_lengths(&name_count, X509_NAME_entry_count(nm),
  215. sk_GENERAL_NAME_num(x->altname))
  216. || !add_lengths(&constraint_count,
  217. sk_GENERAL_SUBTREE_num(nc->permittedSubtrees),
  218. sk_GENERAL_SUBTREE_num(nc->excludedSubtrees))
  219. || (name_count > 0 && constraint_count > NAME_CHECK_MAX / name_count))
  220. return X509_V_ERR_UNSPECIFIED;
  221. if (X509_NAME_entry_count(nm) > 0) {
  222. GENERAL_NAME gntmp;
  223. gntmp.type = GEN_DIRNAME;
  224. gntmp.d.directoryName = nm;
  225. r = nc_match(&gntmp, nc);
  226. if (r != X509_V_OK)
  227. return r;
  228. gntmp.type = GEN_EMAIL;
  229. /* Process any email address attributes in subject name */
  230. for (i = -1;;) {
  231. const X509_NAME_ENTRY *ne;
  232. i = X509_NAME_get_index_by_NID(nm, NID_pkcs9_emailAddress, i);
  233. if (i == -1)
  234. break;
  235. ne = X509_NAME_get_entry(nm, i);
  236. gntmp.d.rfc822Name = X509_NAME_ENTRY_get_data(ne);
  237. if (gntmp.d.rfc822Name->type != V_ASN1_IA5STRING)
  238. return X509_V_ERR_UNSUPPORTED_NAME_SYNTAX;
  239. r = nc_match(&gntmp, nc);
  240. if (r != X509_V_OK)
  241. return r;
  242. }
  243. }
  244. for (i = 0; i < sk_GENERAL_NAME_num(x->altname); i++) {
  245. GENERAL_NAME *gen = sk_GENERAL_NAME_value(x->altname, i);
  246. r = nc_match(gen, nc);
  247. if (r != X509_V_OK)
  248. return r;
  249. }
  250. return X509_V_OK;
  251. }
  252. static int cn2dnsid(ASN1_STRING *cn, unsigned char **dnsid, size_t *idlen)
  253. {
  254. int utf8_length;
  255. unsigned char *utf8_value;
  256. int i;
  257. int isdnsname = 0;
  258. /* Don't leave outputs uninitialized */
  259. *dnsid = NULL;
  260. *idlen = 0;
  261. /*-
  262. * Per RFC 6125, DNS-IDs representing internationalized domain names appear
  263. * in certificates in A-label encoded form:
  264. *
  265. * https://tools.ietf.org/html/rfc6125#section-6.4.2
  266. *
  267. * The same applies to CNs which are intended to represent DNS names.
  268. * However, while in the SAN DNS-IDs are IA5Strings, as CNs they may be
  269. * needlessly encoded in 16-bit Unicode. We perform a conversion to UTF-8
  270. * to ensure that we get an ASCII representation of any CNs that are
  271. * representable as ASCII, but just not encoded as ASCII. The UTF-8 form
  272. * may contain some non-ASCII octets, and that's fine, such CNs are not
  273. * valid legacy DNS names.
  274. *
  275. * Note, 'int' is the return type of ASN1_STRING_to_UTF8() so that's what
  276. * we must use for 'utf8_length'.
  277. */
  278. if ((utf8_length = ASN1_STRING_to_UTF8(&utf8_value, cn)) < 0)
  279. return X509_V_ERR_OUT_OF_MEM;
  280. /*
  281. * Some certificates have had names that include a *trailing* NUL byte.
  282. * Remove these harmless NUL characters. They would otherwise yield false
  283. * alarms with the following embedded NUL check.
  284. */
  285. while (utf8_length > 0 && utf8_value[utf8_length - 1] == '\0')
  286. --utf8_length;
  287. /* Reject *embedded* NULs */
  288. if ((size_t)utf8_length != strlen((char *)utf8_value)) {
  289. OPENSSL_free(utf8_value);
  290. return X509_V_ERR_UNSUPPORTED_NAME_SYNTAX;
  291. }
  292. /*
  293. * XXX: Deviation from strict DNS name syntax, also check names with '_'
  294. * Check DNS name syntax, any '-' or '.' must be internal,
  295. * and on either side of each '.' we can't have a '-' or '.'.
  296. *
  297. * If the name has just one label, we don't consider it a DNS name. This
  298. * means that "CN=sometld" cannot be precluded by DNS name constraints, but
  299. * that is not a problem.
  300. */
  301. for (i = 0; i < utf8_length; ++i) {
  302. unsigned char c = utf8_value[i];
  303. if ((c >= 'a' && c <= 'z')
  304. || (c >= 'A' && c <= 'Z')
  305. || (c >= '0' && c <= '9')
  306. || c == '_')
  307. continue;
  308. /* Dot and hyphen cannot be first or last. */
  309. if (i > 0 && i < utf8_length - 1) {
  310. if (c == '-')
  311. continue;
  312. /*
  313. * Next to a dot the preceding and following characters must not be
  314. * another dot or a hyphen. Otherwise, record that the name is
  315. * plausible, since it has two or more labels.
  316. */
  317. if (c == '.'
  318. && utf8_value[i + 1] != '.'
  319. && utf8_value[i - 1] != '-'
  320. && utf8_value[i + 1] != '-') {
  321. isdnsname = 1;
  322. continue;
  323. }
  324. }
  325. isdnsname = 0;
  326. break;
  327. }
  328. if (isdnsname) {
  329. *dnsid = utf8_value;
  330. *idlen = (size_t)utf8_length;
  331. return X509_V_OK;
  332. }
  333. OPENSSL_free(utf8_value);
  334. return X509_V_OK;
  335. }
  336. /*
  337. * Check CN against DNS-ID name constraints.
  338. */
  339. int NAME_CONSTRAINTS_check_CN(X509 *x, NAME_CONSTRAINTS *nc)
  340. {
  341. int r, i;
  342. const X509_NAME *nm = X509_get_subject_name(x);
  343. ASN1_STRING stmp;
  344. GENERAL_NAME gntmp;
  345. stmp.flags = 0;
  346. stmp.type = V_ASN1_IA5STRING;
  347. gntmp.type = GEN_DNS;
  348. gntmp.d.dNSName = &stmp;
  349. /* Process any commonName attributes in subject name */
  350. for (i = -1;;) {
  351. X509_NAME_ENTRY *ne;
  352. ASN1_STRING *cn;
  353. unsigned char *idval;
  354. size_t idlen;
  355. i = X509_NAME_get_index_by_NID(nm, NID_commonName, i);
  356. if (i == -1)
  357. break;
  358. ne = X509_NAME_get_entry(nm, i);
  359. cn = X509_NAME_ENTRY_get_data(ne);
  360. /* Only process attributes that look like host names */
  361. if ((r = cn2dnsid(cn, &idval, &idlen)) != X509_V_OK)
  362. return r;
  363. if (idlen == 0)
  364. continue;
  365. stmp.length = idlen;
  366. stmp.data = idval;
  367. r = nc_match(&gntmp, nc);
  368. OPENSSL_free(idval);
  369. if (r != X509_V_OK)
  370. return r;
  371. }
  372. return X509_V_OK;
  373. }
  374. /*
  375. * Return nonzero if the GeneralSubtree has valid 'minimum' field
  376. * (must be absent or 0) and valid 'maximum' field (must be absent).
  377. */
  378. static int nc_minmax_valid(GENERAL_SUBTREE *sub) {
  379. BIGNUM *bn = NULL;
  380. int ok = 1;
  381. if (sub->maximum)
  382. ok = 0;
  383. if (sub->minimum) {
  384. bn = ASN1_INTEGER_to_BN(sub->minimum, NULL);
  385. if (bn == NULL || !BN_is_zero(bn))
  386. ok = 0;
  387. BN_free(bn);
  388. }
  389. return ok;
  390. }
  391. static int nc_match(GENERAL_NAME *gen, NAME_CONSTRAINTS *nc)
  392. {
  393. GENERAL_SUBTREE *sub;
  394. int i, r, match = 0;
  395. /*
  396. * Permitted subtrees: if any subtrees exist of matching the type at
  397. * least one subtree must match.
  398. */
  399. for (i = 0; i < sk_GENERAL_SUBTREE_num(nc->permittedSubtrees); i++) {
  400. sub = sk_GENERAL_SUBTREE_value(nc->permittedSubtrees, i);
  401. if (gen->type != sub->base->type)
  402. continue;
  403. if (!nc_minmax_valid(sub))
  404. return X509_V_ERR_SUBTREE_MINMAX;
  405. /* If we already have a match don't bother trying any more */
  406. if (match == 2)
  407. continue;
  408. if (match == 0)
  409. match = 1;
  410. r = nc_match_single(gen, sub->base);
  411. if (r == X509_V_OK)
  412. match = 2;
  413. else if (r != X509_V_ERR_PERMITTED_VIOLATION)
  414. return r;
  415. }
  416. if (match == 1)
  417. return X509_V_ERR_PERMITTED_VIOLATION;
  418. /* Excluded subtrees: must not match any of these */
  419. for (i = 0; i < sk_GENERAL_SUBTREE_num(nc->excludedSubtrees); i++) {
  420. sub = sk_GENERAL_SUBTREE_value(nc->excludedSubtrees, i);
  421. if (gen->type != sub->base->type)
  422. continue;
  423. if (!nc_minmax_valid(sub))
  424. return X509_V_ERR_SUBTREE_MINMAX;
  425. r = nc_match_single(gen, sub->base);
  426. if (r == X509_V_OK)
  427. return X509_V_ERR_EXCLUDED_VIOLATION;
  428. else if (r != X509_V_ERR_PERMITTED_VIOLATION)
  429. return r;
  430. }
  431. return X509_V_OK;
  432. }
  433. static int nc_match_single(GENERAL_NAME *gen, GENERAL_NAME *base)
  434. {
  435. switch (base->type) {
  436. case GEN_DIRNAME:
  437. return nc_dn(gen->d.directoryName, base->d.directoryName);
  438. case GEN_DNS:
  439. return nc_dns(gen->d.dNSName, base->d.dNSName);
  440. case GEN_EMAIL:
  441. return nc_email(gen->d.rfc822Name, base->d.rfc822Name);
  442. case GEN_URI:
  443. return nc_uri(gen->d.uniformResourceIdentifier,
  444. base->d.uniformResourceIdentifier);
  445. case GEN_IPADD:
  446. return nc_ip(gen->d.iPAddress, base->d.iPAddress);
  447. default:
  448. return X509_V_ERR_UNSUPPORTED_CONSTRAINT_TYPE;
  449. }
  450. }
  451. /*
  452. * directoryName name constraint matching. The canonical encoding of
  453. * X509_NAME makes this comparison easy. It is matched if the subtree is a
  454. * subset of the name.
  455. */
  456. static int nc_dn(const X509_NAME *nm, const X509_NAME *base)
  457. {
  458. /* Ensure canonical encodings are up to date. */
  459. if (nm->modified && i2d_X509_NAME(nm, NULL) < 0)
  460. return X509_V_ERR_OUT_OF_MEM;
  461. if (base->modified && i2d_X509_NAME(base, NULL) < 0)
  462. return X509_V_ERR_OUT_OF_MEM;
  463. if (base->canon_enclen > nm->canon_enclen)
  464. return X509_V_ERR_PERMITTED_VIOLATION;
  465. if (memcmp(base->canon_enc, nm->canon_enc, base->canon_enclen))
  466. return X509_V_ERR_PERMITTED_VIOLATION;
  467. return X509_V_OK;
  468. }
  469. static int nc_dns(ASN1_IA5STRING *dns, ASN1_IA5STRING *base)
  470. {
  471. char *baseptr = (char *)base->data;
  472. char *dnsptr = (char *)dns->data;
  473. /* Empty matches everything */
  474. if (*baseptr == '\0')
  475. return X509_V_OK;
  476. /*
  477. * Otherwise can add zero or more components on the left so compare RHS
  478. * and if dns is longer and expect '.' as preceding character.
  479. */
  480. if (dns->length > base->length) {
  481. dnsptr += dns->length - base->length;
  482. if (*baseptr != '.' && dnsptr[-1] != '.')
  483. return X509_V_ERR_PERMITTED_VIOLATION;
  484. }
  485. if (ia5casecmp(baseptr, dnsptr))
  486. return X509_V_ERR_PERMITTED_VIOLATION;
  487. return X509_V_OK;
  488. }
  489. static int nc_email(ASN1_IA5STRING *eml, ASN1_IA5STRING *base)
  490. {
  491. const char *baseptr = (char *)base->data;
  492. const char *emlptr = (char *)eml->data;
  493. const char *baseat = strchr(baseptr, '@');
  494. const char *emlat = strchr(emlptr, '@');
  495. if (!emlat)
  496. return X509_V_ERR_UNSUPPORTED_NAME_SYNTAX;
  497. /* Special case: initial '.' is RHS match */
  498. if (!baseat && (*baseptr == '.')) {
  499. if (eml->length > base->length) {
  500. emlptr += eml->length - base->length;
  501. if (ia5casecmp(baseptr, emlptr) == 0)
  502. return X509_V_OK;
  503. }
  504. return X509_V_ERR_PERMITTED_VIOLATION;
  505. }
  506. /* If we have anything before '@' match local part */
  507. if (baseat) {
  508. if (baseat != baseptr) {
  509. if ((baseat - baseptr) != (emlat - emlptr))
  510. return X509_V_ERR_PERMITTED_VIOLATION;
  511. /* Case sensitive match of local part */
  512. if (strncmp(baseptr, emlptr, emlat - emlptr))
  513. return X509_V_ERR_PERMITTED_VIOLATION;
  514. }
  515. /* Position base after '@' */
  516. baseptr = baseat + 1;
  517. }
  518. emlptr = emlat + 1;
  519. /* Just have hostname left to match: case insensitive */
  520. if (ia5casecmp(baseptr, emlptr))
  521. return X509_V_ERR_PERMITTED_VIOLATION;
  522. return X509_V_OK;
  523. }
  524. static int nc_uri(ASN1_IA5STRING *uri, ASN1_IA5STRING *base)
  525. {
  526. const char *baseptr = (char *)base->data;
  527. const char *hostptr = (char *)uri->data;
  528. const char *p = strchr(hostptr, ':');
  529. int hostlen;
  530. /* Check for foo:// and skip past it */
  531. if (p == NULL || p[1] != '/' || p[2] != '/')
  532. return X509_V_ERR_UNSUPPORTED_NAME_SYNTAX;
  533. hostptr = p + 3;
  534. /* Determine length of hostname part of URI */
  535. /* Look for a port indicator as end of hostname first */
  536. p = strchr(hostptr, ':');
  537. /* Otherwise look for trailing slash */
  538. if (p == NULL)
  539. p = strchr(hostptr, '/');
  540. if (p == NULL)
  541. hostlen = strlen(hostptr);
  542. else
  543. hostlen = p - hostptr;
  544. if (hostlen == 0)
  545. return X509_V_ERR_UNSUPPORTED_NAME_SYNTAX;
  546. /* Special case: initial '.' is RHS match */
  547. if (*baseptr == '.') {
  548. if (hostlen > base->length) {
  549. p = hostptr + hostlen - base->length;
  550. if (ia5ncasecmp(p, baseptr, base->length) == 0)
  551. return X509_V_OK;
  552. }
  553. return X509_V_ERR_PERMITTED_VIOLATION;
  554. }
  555. if ((base->length != (int)hostlen)
  556. || ia5ncasecmp(hostptr, baseptr, hostlen))
  557. return X509_V_ERR_PERMITTED_VIOLATION;
  558. return X509_V_OK;
  559. }
  560. static int nc_ip(ASN1_OCTET_STRING *ip, ASN1_OCTET_STRING *base)
  561. {
  562. int hostlen, baselen, i;
  563. unsigned char *hostptr, *baseptr, *maskptr;
  564. hostptr = ip->data;
  565. hostlen = ip->length;
  566. baseptr = base->data;
  567. baselen = base->length;
  568. /* Invalid if not IPv4 or IPv6 */
  569. if (!((hostlen == 4) || (hostlen == 16)))
  570. return X509_V_ERR_UNSUPPORTED_NAME_SYNTAX;
  571. if (!((baselen == 8) || (baselen == 32)))
  572. return X509_V_ERR_UNSUPPORTED_NAME_SYNTAX;
  573. /* Do not match IPv4 with IPv6 */
  574. if (hostlen * 2 != baselen)
  575. return X509_V_ERR_PERMITTED_VIOLATION;
  576. maskptr = base->data + hostlen;
  577. /* Considering possible not aligned base ipAddress */
  578. /* Not checking for wrong mask definition: i.e.: 255.0.255.0 */
  579. for (i = 0; i < hostlen; i++)
  580. if ((hostptr[i] & maskptr[i]) != (baseptr[i] & maskptr[i]))
  581. return X509_V_ERR_PERMITTED_VIOLATION;
  582. return X509_V_OK;
  583. }