2
0

v3_ncons.c 21 KB

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