2
0

v3_ncons.c 25 KB

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