v3_ncons.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702
  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 "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. 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. if (ncons->permittedSubtrees && ncons->excludedSubtrees)
  140. BIO_puts(bp, "\n");
  141. do_i2r_name_constraints(method, ncons->excludedSubtrees,
  142. bp, ind, "Excluded");
  143. return 1;
  144. }
  145. static int do_i2r_name_constraints(const X509V3_EXT_METHOD *method,
  146. STACK_OF(GENERAL_SUBTREE) *trees,
  147. BIO *bp, int ind, const char *name)
  148. {
  149. GENERAL_SUBTREE *tree;
  150. int i;
  151. if (sk_GENERAL_SUBTREE_num(trees) > 0)
  152. BIO_printf(bp, "%*s%s:\n", ind, "", name);
  153. for (i = 0; i < sk_GENERAL_SUBTREE_num(trees); i++) {
  154. if (i > 0)
  155. BIO_puts(bp, "\n");
  156. tree = sk_GENERAL_SUBTREE_value(trees, i);
  157. BIO_printf(bp, "%*s", ind + 2, "");
  158. if (tree->base->type == GEN_IPADD)
  159. print_nc_ipadd(bp, tree->base->d.ip);
  160. else
  161. GENERAL_NAME_print(bp, tree->base);
  162. }
  163. return 1;
  164. }
  165. static int print_nc_ipadd(BIO *bp, ASN1_OCTET_STRING *ip)
  166. {
  167. int i, len;
  168. unsigned char *p;
  169. p = ip->data;
  170. len = ip->length;
  171. BIO_puts(bp, "IP:");
  172. if (len == 8) {
  173. BIO_printf(bp, "%d.%d.%d.%d/%d.%d.%d.%d",
  174. p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7]);
  175. } else if (len == 32) {
  176. for (i = 0; i < 16; i++) {
  177. BIO_printf(bp, "%X", p[0] << 8 | p[1]);
  178. p += 2;
  179. if (i == 7)
  180. BIO_puts(bp, "/");
  181. else if (i != 15)
  182. BIO_puts(bp, ":");
  183. }
  184. } else
  185. BIO_printf(bp, "IP Address:<invalid>");
  186. return 1;
  187. }
  188. #define NAME_CHECK_MAX (1 << 20)
  189. static int add_lengths(int *out, int a, int b)
  190. {
  191. /* sk_FOO_num(NULL) returns -1 but is effectively 0 when iterating. */
  192. if (a < 0)
  193. a = 0;
  194. if (b < 0)
  195. b = 0;
  196. if (a > INT_MAX - b)
  197. return 0;
  198. *out = a + b;
  199. return 1;
  200. }
  201. /*-
  202. * Check a certificate conforms to a specified set of constraints.
  203. * Return values:
  204. * X509_V_OK: All constraints obeyed.
  205. * X509_V_ERR_PERMITTED_VIOLATION: Permitted subtree violation.
  206. * X509_V_ERR_EXCLUDED_VIOLATION: Excluded subtree violation.
  207. * X509_V_ERR_SUBTREE_MINMAX: Min or max values present and matching type.
  208. * X509_V_ERR_UNSUPPORTED_CONSTRAINT_TYPE: Unsupported constraint type.
  209. * X509_V_ERR_UNSUPPORTED_CONSTRAINT_SYNTAX: bad unsupported constraint syntax.
  210. * X509_V_ERR_UNSUPPORTED_NAME_SYNTAX: bad or unsupported syntax of name
  211. */
  212. int NAME_CONSTRAINTS_check(X509 *x, NAME_CONSTRAINTS *nc)
  213. {
  214. int r, i, name_count, constraint_count;
  215. X509_NAME *nm;
  216. nm = X509_get_subject_name(x);
  217. /*
  218. * Guard against certificates with an excessive number of names or
  219. * constraints causing a computationally expensive name constraints check.
  220. */
  221. if (!add_lengths(&name_count, X509_NAME_entry_count(nm),
  222. sk_GENERAL_NAME_num(x->altname))
  223. || !add_lengths(&constraint_count,
  224. sk_GENERAL_SUBTREE_num(nc->permittedSubtrees),
  225. sk_GENERAL_SUBTREE_num(nc->excludedSubtrees))
  226. || (name_count > 0 && constraint_count > NAME_CHECK_MAX / name_count))
  227. return X509_V_ERR_UNSPECIFIED;
  228. if (X509_NAME_entry_count(nm) > 0) {
  229. GENERAL_NAME gntmp;
  230. gntmp.type = GEN_DIRNAME;
  231. gntmp.d.directoryName = nm;
  232. r = nc_match(&gntmp, nc);
  233. if (r != X509_V_OK)
  234. return r;
  235. gntmp.type = GEN_EMAIL;
  236. /* Process any email address attributes in subject name */
  237. for (i = -1;;) {
  238. const X509_NAME_ENTRY *ne;
  239. i = X509_NAME_get_index_by_NID(nm, NID_pkcs9_emailAddress, i);
  240. if (i == -1)
  241. break;
  242. ne = X509_NAME_get_entry(nm, i);
  243. gntmp.d.rfc822Name = X509_NAME_ENTRY_get_data(ne);
  244. if (gntmp.d.rfc822Name->type != V_ASN1_IA5STRING)
  245. return X509_V_ERR_UNSUPPORTED_NAME_SYNTAX;
  246. r = nc_match(&gntmp, nc);
  247. if (r != X509_V_OK)
  248. return r;
  249. }
  250. }
  251. for (i = 0; i < sk_GENERAL_NAME_num(x->altname); i++) {
  252. GENERAL_NAME *gen = sk_GENERAL_NAME_value(x->altname, i);
  253. r = nc_match(gen, nc);
  254. if (r != X509_V_OK)
  255. return r;
  256. }
  257. return X509_V_OK;
  258. }
  259. static int cn2dnsid(ASN1_STRING *cn, unsigned char **dnsid, size_t *idlen)
  260. {
  261. int utf8_length;
  262. unsigned char *utf8_value;
  263. int i;
  264. int isdnsname = 0;
  265. /* Don't leave outputs uninitialized */
  266. *dnsid = NULL;
  267. *idlen = 0;
  268. /*-
  269. * Per RFC 6125, DNS-IDs representing internationalized domain names appear
  270. * in certificates in A-label encoded form:
  271. *
  272. * https://tools.ietf.org/html/rfc6125#section-6.4.2
  273. *
  274. * The same applies to CNs which are intended to represent DNS names.
  275. * However, while in the SAN DNS-IDs are IA5Strings, as CNs they may be
  276. * needlessly encoded in 16-bit Unicode. We perform a conversion to UTF-8
  277. * to ensure that we get an ASCII representation of any CNs that are
  278. * representable as ASCII, but just not encoded as ASCII. The UTF-8 form
  279. * may contain some non-ASCII octets, and that's fine, such CNs are not
  280. * valid legacy DNS names.
  281. *
  282. * Note, 'int' is the return type of ASN1_STRING_to_UTF8() so that's what
  283. * we must use for 'utf8_length'.
  284. */
  285. if ((utf8_length = ASN1_STRING_to_UTF8(&utf8_value, cn)) < 0)
  286. return X509_V_ERR_OUT_OF_MEM;
  287. /*
  288. * Some certificates have had names that include a *trailing* NUL byte.
  289. * Remove these harmless NUL characters. They would otherwise yield false
  290. * alarms with the following embedded NUL check.
  291. */
  292. while (utf8_length > 0 && utf8_value[utf8_length - 1] == '\0')
  293. --utf8_length;
  294. /* Reject *embedded* NULs */
  295. if ((size_t)utf8_length != strlen((char *)utf8_value)) {
  296. OPENSSL_free(utf8_value);
  297. return X509_V_ERR_UNSUPPORTED_NAME_SYNTAX;
  298. }
  299. /*
  300. * XXX: Deviation from strict DNS name syntax, also check names with '_'
  301. * Check DNS name syntax, any '-' or '.' must be internal,
  302. * and on either side of each '.' we can't have a '-' or '.'.
  303. *
  304. * If the name has just one label, we don't consider it a DNS name. This
  305. * means that "CN=sometld" cannot be precluded by DNS name constraints, but
  306. * that is not a problem.
  307. */
  308. for (i = 0; i < utf8_length; ++i) {
  309. unsigned char c = utf8_value[i];
  310. if ((c >= 'a' && c <= 'z')
  311. || (c >= 'A' && c <= 'Z')
  312. || (c >= '0' && c <= '9')
  313. || c == '_')
  314. continue;
  315. /* Dot and hyphen cannot be first or last. */
  316. if (i > 0 && i < utf8_length - 1) {
  317. if (c == '-')
  318. continue;
  319. /*
  320. * Next to a dot the preceding and following characters must not be
  321. * another dot or a hyphen. Otherwise, record that the name is
  322. * plausible, since it has two or more labels.
  323. */
  324. if (c == '.'
  325. && utf8_value[i + 1] != '.'
  326. && utf8_value[i - 1] != '-'
  327. && utf8_value[i + 1] != '-') {
  328. isdnsname = 1;
  329. continue;
  330. }
  331. }
  332. isdnsname = 0;
  333. break;
  334. }
  335. if (isdnsname) {
  336. *dnsid = utf8_value;
  337. *idlen = (size_t)utf8_length;
  338. return X509_V_OK;
  339. }
  340. OPENSSL_free(utf8_value);
  341. return X509_V_OK;
  342. }
  343. /*
  344. * Check CN against DNS-ID name constraints.
  345. */
  346. int NAME_CONSTRAINTS_check_CN(X509 *x, NAME_CONSTRAINTS *nc)
  347. {
  348. int r, i;
  349. X509_NAME *nm = X509_get_subject_name(x);
  350. ASN1_STRING stmp;
  351. GENERAL_NAME gntmp;
  352. stmp.flags = 0;
  353. stmp.type = V_ASN1_IA5STRING;
  354. gntmp.type = GEN_DNS;
  355. gntmp.d.dNSName = &stmp;
  356. /* Process any commonName attributes in subject name */
  357. for (i = -1;;) {
  358. X509_NAME_ENTRY *ne;
  359. ASN1_STRING *cn;
  360. unsigned char *idval;
  361. size_t idlen;
  362. i = X509_NAME_get_index_by_NID(nm, NID_commonName, i);
  363. if (i == -1)
  364. break;
  365. ne = X509_NAME_get_entry(nm, i);
  366. cn = X509_NAME_ENTRY_get_data(ne);
  367. /* Only process attributes that look like host names */
  368. if ((r = cn2dnsid(cn, &idval, &idlen)) != X509_V_OK)
  369. return r;
  370. if (idlen == 0)
  371. continue;
  372. stmp.length = idlen;
  373. stmp.data = idval;
  374. r = nc_match(&gntmp, nc);
  375. OPENSSL_free(idval);
  376. if (r != X509_V_OK)
  377. return r;
  378. }
  379. return X509_V_OK;
  380. }
  381. /*
  382. * Return nonzero if the GeneralSubtree has valid 'minimum' field
  383. * (must be absent or 0) and valid 'maximum' field (must be absent).
  384. */
  385. static int nc_minmax_valid(GENERAL_SUBTREE *sub) {
  386. BIGNUM *bn = NULL;
  387. int ok = 1;
  388. if (sub->maximum)
  389. ok = 0;
  390. if (sub->minimum) {
  391. bn = ASN1_INTEGER_to_BN(sub->minimum, NULL);
  392. if (bn == NULL || !BN_is_zero(bn))
  393. ok = 0;
  394. BN_free(bn);
  395. }
  396. return ok;
  397. }
  398. static int nc_match(GENERAL_NAME *gen, NAME_CONSTRAINTS *nc)
  399. {
  400. GENERAL_SUBTREE *sub;
  401. int i, r, match = 0;
  402. /*
  403. * Permitted subtrees: if any subtrees exist of matching the type at
  404. * least one subtree must match.
  405. */
  406. for (i = 0; i < sk_GENERAL_SUBTREE_num(nc->permittedSubtrees); i++) {
  407. sub = sk_GENERAL_SUBTREE_value(nc->permittedSubtrees, i);
  408. if (gen->type != sub->base->type)
  409. continue;
  410. if (!nc_minmax_valid(sub))
  411. return X509_V_ERR_SUBTREE_MINMAX;
  412. /* If we already have a match don't bother trying any more */
  413. if (match == 2)
  414. continue;
  415. if (match == 0)
  416. match = 1;
  417. r = nc_match_single(gen, sub->base);
  418. if (r == X509_V_OK)
  419. match = 2;
  420. else if (r != X509_V_ERR_PERMITTED_VIOLATION)
  421. return r;
  422. }
  423. if (match == 1)
  424. return X509_V_ERR_PERMITTED_VIOLATION;
  425. /* Excluded subtrees: must not match any of these */
  426. for (i = 0; i < sk_GENERAL_SUBTREE_num(nc->excludedSubtrees); i++) {
  427. sub = sk_GENERAL_SUBTREE_value(nc->excludedSubtrees, i);
  428. if (gen->type != sub->base->type)
  429. continue;
  430. if (!nc_minmax_valid(sub))
  431. return X509_V_ERR_SUBTREE_MINMAX;
  432. r = nc_match_single(gen, sub->base);
  433. if (r == X509_V_OK)
  434. return X509_V_ERR_EXCLUDED_VIOLATION;
  435. else if (r != X509_V_ERR_PERMITTED_VIOLATION)
  436. return r;
  437. }
  438. return X509_V_OK;
  439. }
  440. static int nc_match_single(GENERAL_NAME *gen, GENERAL_NAME *base)
  441. {
  442. switch (base->type) {
  443. case GEN_DIRNAME:
  444. return nc_dn(gen->d.directoryName, base->d.directoryName);
  445. case GEN_DNS:
  446. return nc_dns(gen->d.dNSName, base->d.dNSName);
  447. case GEN_EMAIL:
  448. return nc_email(gen->d.rfc822Name, base->d.rfc822Name);
  449. case GEN_URI:
  450. return nc_uri(gen->d.uniformResourceIdentifier,
  451. base->d.uniformResourceIdentifier);
  452. case GEN_IPADD:
  453. return nc_ip(gen->d.iPAddress, base->d.iPAddress);
  454. default:
  455. return X509_V_ERR_UNSUPPORTED_CONSTRAINT_TYPE;
  456. }
  457. }
  458. /*
  459. * directoryName name constraint matching. The canonical encoding of
  460. * X509_NAME makes this comparison easy. It is matched if the subtree is a
  461. * subset of the name.
  462. */
  463. static int nc_dn(X509_NAME *nm, X509_NAME *base)
  464. {
  465. /* Ensure canonical encodings are up to date. */
  466. if (nm->modified && i2d_X509_NAME(nm, NULL) < 0)
  467. return X509_V_ERR_OUT_OF_MEM;
  468. if (base->modified && i2d_X509_NAME(base, NULL) < 0)
  469. return X509_V_ERR_OUT_OF_MEM;
  470. if (base->canon_enclen > nm->canon_enclen)
  471. return X509_V_ERR_PERMITTED_VIOLATION;
  472. if (memcmp(base->canon_enc, nm->canon_enc, base->canon_enclen))
  473. return X509_V_ERR_PERMITTED_VIOLATION;
  474. return X509_V_OK;
  475. }
  476. static int nc_dns(ASN1_IA5STRING *dns, ASN1_IA5STRING *base)
  477. {
  478. char *baseptr = (char *)base->data;
  479. char *dnsptr = (char *)dns->data;
  480. /* Empty matches everything */
  481. if (*baseptr == '\0')
  482. return X509_V_OK;
  483. /*
  484. * Otherwise can add zero or more components on the left so compare RHS
  485. * and if dns is longer and expect '.' as preceding character.
  486. */
  487. if (dns->length > base->length) {
  488. dnsptr += dns->length - base->length;
  489. if (*baseptr != '.' && dnsptr[-1] != '.')
  490. return X509_V_ERR_PERMITTED_VIOLATION;
  491. }
  492. if (ia5casecmp(baseptr, dnsptr))
  493. return X509_V_ERR_PERMITTED_VIOLATION;
  494. return X509_V_OK;
  495. }
  496. static int nc_email(ASN1_IA5STRING *eml, ASN1_IA5STRING *base)
  497. {
  498. const char *baseptr = (char *)base->data;
  499. const char *emlptr = (char *)eml->data;
  500. const char *baseat = strchr(baseptr, '@');
  501. const char *emlat = strchr(emlptr, '@');
  502. if (!emlat)
  503. return X509_V_ERR_UNSUPPORTED_NAME_SYNTAX;
  504. /* Special case: initial '.' is RHS match */
  505. if (!baseat && (*baseptr == '.')) {
  506. if (eml->length > base->length) {
  507. emlptr += eml->length - base->length;
  508. if (ia5casecmp(baseptr, emlptr) == 0)
  509. return X509_V_OK;
  510. }
  511. return X509_V_ERR_PERMITTED_VIOLATION;
  512. }
  513. /* If we have anything before '@' match local part */
  514. if (baseat) {
  515. if (baseat != baseptr) {
  516. if ((baseat - baseptr) != (emlat - emlptr))
  517. return X509_V_ERR_PERMITTED_VIOLATION;
  518. /* Case sensitive match of local part */
  519. if (strncmp(baseptr, emlptr, emlat - emlptr))
  520. return X509_V_ERR_PERMITTED_VIOLATION;
  521. }
  522. /* Position base after '@' */
  523. baseptr = baseat + 1;
  524. }
  525. emlptr = emlat + 1;
  526. /* Just have hostname left to match: case insensitive */
  527. if (ia5casecmp(baseptr, emlptr))
  528. return X509_V_ERR_PERMITTED_VIOLATION;
  529. return X509_V_OK;
  530. }
  531. static int nc_uri(ASN1_IA5STRING *uri, ASN1_IA5STRING *base)
  532. {
  533. const char *baseptr = (char *)base->data;
  534. const char *hostptr = (char *)uri->data;
  535. const char *p = strchr(hostptr, ':');
  536. int hostlen;
  537. /* Check for foo:// and skip past it */
  538. if (p == NULL || p[1] != '/' || p[2] != '/')
  539. return X509_V_ERR_UNSUPPORTED_NAME_SYNTAX;
  540. hostptr = p + 3;
  541. /* Determine length of hostname part of URI */
  542. /* Look for a port indicator as end of hostname first */
  543. p = strchr(hostptr, ':');
  544. /* Otherwise look for trailing slash */
  545. if (p == NULL)
  546. p = strchr(hostptr, '/');
  547. if (p == NULL)
  548. hostlen = strlen(hostptr);
  549. else
  550. hostlen = p - hostptr;
  551. if (hostlen == 0)
  552. return X509_V_ERR_UNSUPPORTED_NAME_SYNTAX;
  553. /* Special case: initial '.' is RHS match */
  554. if (*baseptr == '.') {
  555. if (hostlen > base->length) {
  556. p = hostptr + hostlen - base->length;
  557. if (ia5ncasecmp(p, baseptr, base->length) == 0)
  558. return X509_V_OK;
  559. }
  560. return X509_V_ERR_PERMITTED_VIOLATION;
  561. }
  562. if ((base->length != (int)hostlen)
  563. || ia5ncasecmp(hostptr, baseptr, hostlen))
  564. return X509_V_ERR_PERMITTED_VIOLATION;
  565. return X509_V_OK;
  566. }
  567. static int nc_ip(ASN1_OCTET_STRING *ip, ASN1_OCTET_STRING *base)
  568. {
  569. int hostlen, baselen, i;
  570. unsigned char *hostptr, *baseptr, *maskptr;
  571. hostptr = ip->data;
  572. hostlen = ip->length;
  573. baseptr = base->data;
  574. baselen = base->length;
  575. /* Invalid if not IPv4 or IPv6 */
  576. if (!((hostlen == 4) || (hostlen == 16)))
  577. return X509_V_ERR_UNSUPPORTED_NAME_SYNTAX;
  578. if (!((baselen == 8) || (baselen == 32)))
  579. return X509_V_ERR_UNSUPPORTED_NAME_SYNTAX;
  580. /* Do not match IPv4 with IPv6 */
  581. if (hostlen * 2 != baselen)
  582. return X509_V_ERR_PERMITTED_VIOLATION;
  583. maskptr = base->data + hostlen;
  584. /* Considering possible not aligned base ipAddress */
  585. /* Not checking for wrong mask definition: i.e.: 255.0.255.0 */
  586. for (i = 0; i < hostlen; i++)
  587. if ((hostptr[i] & maskptr[i]) != (baseptr[i] & maskptr[i]))
  588. return X509_V_ERR_PERMITTED_VIOLATION;
  589. return X509_V_OK;
  590. }