v3_ncons.c 23 KB

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