2
0

v3_ncons.c 26 KB

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