x_name.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557
  1. /*
  2. * Copyright 1995-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 <stdio.h>
  10. #include "crypto/ctype.h"
  11. #include "internal/cryptlib.h"
  12. #include <openssl/asn1t.h>
  13. #include <openssl/x509.h>
  14. #include "crypto/x509.h"
  15. #include "crypto/asn1.h"
  16. #include "x509_local.h"
  17. /*
  18. * Maximum length of X509_NAME: much larger than anything we should
  19. * ever see in practice.
  20. */
  21. #define X509_NAME_MAX (1024 * 1024)
  22. static int x509_name_ex_d2i(ASN1_VALUE **val,
  23. const unsigned char **in, long len,
  24. const ASN1_ITEM *it,
  25. int tag, int aclass, char opt, ASN1_TLC *ctx);
  26. static int x509_name_ex_i2d(const ASN1_VALUE **val, unsigned char **out,
  27. const ASN1_ITEM *it, int tag, int aclass);
  28. static int x509_name_ex_new(ASN1_VALUE **val, const ASN1_ITEM *it);
  29. static void x509_name_ex_free(ASN1_VALUE **val, const ASN1_ITEM *it);
  30. static int x509_name_encode(X509_NAME *a);
  31. static int x509_name_canon(X509_NAME *a);
  32. static int asn1_string_canon(ASN1_STRING *out, const ASN1_STRING *in);
  33. static int i2d_name_canon(const STACK_OF(STACK_OF_X509_NAME_ENTRY) * intname,
  34. unsigned char **in);
  35. static int x509_name_ex_print(BIO *out, const ASN1_VALUE **pval,
  36. int indent,
  37. const char *fname, const ASN1_PCTX *pctx);
  38. ASN1_SEQUENCE(X509_NAME_ENTRY) = {
  39. ASN1_SIMPLE(X509_NAME_ENTRY, object, ASN1_OBJECT),
  40. ASN1_SIMPLE(X509_NAME_ENTRY, value, ASN1_PRINTABLE)
  41. } ASN1_SEQUENCE_END(X509_NAME_ENTRY)
  42. IMPLEMENT_ASN1_FUNCTIONS(X509_NAME_ENTRY)
  43. IMPLEMENT_ASN1_DUP_FUNCTION(X509_NAME_ENTRY)
  44. /*
  45. * For the "Name" type we need a SEQUENCE OF { SET OF X509_NAME_ENTRY } so
  46. * declare two template wrappers for this
  47. */
  48. ASN1_ITEM_TEMPLATE(X509_NAME_ENTRIES) =
  49. ASN1_EX_TEMPLATE_TYPE(ASN1_TFLG_SET_OF, 0, RDNS, X509_NAME_ENTRY)
  50. static_ASN1_ITEM_TEMPLATE_END(X509_NAME_ENTRIES)
  51. ASN1_ITEM_TEMPLATE(X509_NAME_INTERNAL) =
  52. ASN1_EX_TEMPLATE_TYPE(ASN1_TFLG_SEQUENCE_OF, 0, Name, X509_NAME_ENTRIES)
  53. static_ASN1_ITEM_TEMPLATE_END(X509_NAME_INTERNAL)
  54. /*
  55. * Normally that's where it would end: we'd have two nested STACK structures
  56. * representing the ASN1. Unfortunately X509_NAME uses a completely different
  57. * form and caches encodings so we have to process the internal form and
  58. * convert to the external form.
  59. */
  60. static const ASN1_EXTERN_FUNCS x509_name_ff = {
  61. NULL,
  62. x509_name_ex_new,
  63. x509_name_ex_free,
  64. 0, /* Default clear behaviour is OK */
  65. x509_name_ex_d2i,
  66. x509_name_ex_i2d,
  67. x509_name_ex_print
  68. };
  69. IMPLEMENT_EXTERN_ASN1(X509_NAME, V_ASN1_SEQUENCE, x509_name_ff)
  70. IMPLEMENT_ASN1_FUNCTIONS(X509_NAME)
  71. IMPLEMENT_ASN1_DUP_FUNCTION(X509_NAME)
  72. static int x509_name_ex_new(ASN1_VALUE **val, const ASN1_ITEM *it)
  73. {
  74. X509_NAME *ret = OPENSSL_zalloc(sizeof(*ret));
  75. if (ret == NULL)
  76. goto memerr;
  77. if ((ret->entries = sk_X509_NAME_ENTRY_new_null()) == NULL)
  78. goto memerr;
  79. if ((ret->bytes = BUF_MEM_new()) == NULL)
  80. goto memerr;
  81. ret->modified = 1;
  82. *val = (ASN1_VALUE *)ret;
  83. return 1;
  84. memerr:
  85. ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
  86. if (ret) {
  87. sk_X509_NAME_ENTRY_free(ret->entries);
  88. OPENSSL_free(ret);
  89. }
  90. return 0;
  91. }
  92. static void x509_name_ex_free(ASN1_VALUE **pval, const ASN1_ITEM *it)
  93. {
  94. X509_NAME *a;
  95. if (pval == NULL || *pval == NULL)
  96. return;
  97. a = (X509_NAME *)*pval;
  98. BUF_MEM_free(a->bytes);
  99. sk_X509_NAME_ENTRY_pop_free(a->entries, X509_NAME_ENTRY_free);
  100. OPENSSL_free(a->canon_enc);
  101. OPENSSL_free(a);
  102. *pval = NULL;
  103. }
  104. static void local_sk_X509_NAME_ENTRY_free(STACK_OF(X509_NAME_ENTRY) *ne)
  105. {
  106. sk_X509_NAME_ENTRY_free(ne);
  107. }
  108. static void local_sk_X509_NAME_ENTRY_pop_free(STACK_OF(X509_NAME_ENTRY) *ne)
  109. {
  110. sk_X509_NAME_ENTRY_pop_free(ne, X509_NAME_ENTRY_free);
  111. }
  112. static int x509_name_ex_d2i(ASN1_VALUE **val,
  113. const unsigned char **in, long len,
  114. const ASN1_ITEM *it, int tag, int aclass,
  115. char opt, ASN1_TLC *ctx)
  116. {
  117. const unsigned char *p = *in, *q;
  118. union {
  119. STACK_OF(STACK_OF_X509_NAME_ENTRY) *s;
  120. ASN1_VALUE *a;
  121. } intname = {
  122. NULL
  123. };
  124. union {
  125. X509_NAME *x;
  126. ASN1_VALUE *a;
  127. } nm = {
  128. NULL
  129. };
  130. int i, j, ret;
  131. STACK_OF(X509_NAME_ENTRY) *entries;
  132. X509_NAME_ENTRY *entry;
  133. if (len > X509_NAME_MAX)
  134. len = X509_NAME_MAX;
  135. q = p;
  136. /* Get internal representation of Name */
  137. ret = ASN1_item_ex_d2i(&intname.a,
  138. &p, len, ASN1_ITEM_rptr(X509_NAME_INTERNAL),
  139. tag, aclass, opt, ctx);
  140. if (ret <= 0)
  141. return ret;
  142. if (*val)
  143. x509_name_ex_free(val, NULL);
  144. if (!x509_name_ex_new(&nm.a, NULL))
  145. goto err;
  146. /* We've decoded it: now cache encoding */
  147. if (!BUF_MEM_grow(nm.x->bytes, p - q))
  148. goto err;
  149. memcpy(nm.x->bytes->data, q, p - q);
  150. /* Convert internal representation to X509_NAME structure */
  151. for (i = 0; i < sk_STACK_OF_X509_NAME_ENTRY_num(intname.s); i++) {
  152. entries = sk_STACK_OF_X509_NAME_ENTRY_value(intname.s, i);
  153. for (j = 0; j < sk_X509_NAME_ENTRY_num(entries); j++) {
  154. entry = sk_X509_NAME_ENTRY_value(entries, j);
  155. entry->set = i;
  156. if (!sk_X509_NAME_ENTRY_push(nm.x->entries, entry))
  157. goto err;
  158. (void)sk_X509_NAME_ENTRY_set(entries, j, NULL);
  159. }
  160. }
  161. ret = x509_name_canon(nm.x);
  162. if (!ret)
  163. goto err;
  164. sk_STACK_OF_X509_NAME_ENTRY_pop_free(intname.s,
  165. local_sk_X509_NAME_ENTRY_free);
  166. nm.x->modified = 0;
  167. *val = nm.a;
  168. *in = p;
  169. return ret;
  170. err:
  171. if (nm.x != NULL)
  172. X509_NAME_free(nm.x);
  173. sk_STACK_OF_X509_NAME_ENTRY_pop_free(intname.s,
  174. local_sk_X509_NAME_ENTRY_pop_free);
  175. ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
  176. return 0;
  177. }
  178. static int x509_name_ex_i2d(const ASN1_VALUE **val, unsigned char **out,
  179. const ASN1_ITEM *it, int tag, int aclass)
  180. {
  181. int ret;
  182. X509_NAME *a = (X509_NAME *)*val;
  183. if (a->modified) {
  184. ret = x509_name_encode(a);
  185. if (ret < 0)
  186. return ret;
  187. ret = x509_name_canon(a);
  188. if (ret < 0)
  189. return ret;
  190. }
  191. ret = a->bytes->length;
  192. if (out != NULL) {
  193. memcpy(*out, a->bytes->data, ret);
  194. *out += ret;
  195. }
  196. return ret;
  197. }
  198. static int x509_name_encode(X509_NAME *a)
  199. {
  200. union {
  201. STACK_OF(STACK_OF_X509_NAME_ENTRY) *s;
  202. const ASN1_VALUE *a;
  203. } intname = {
  204. NULL
  205. };
  206. int len;
  207. unsigned char *p;
  208. STACK_OF(X509_NAME_ENTRY) *entries = NULL;
  209. X509_NAME_ENTRY *entry;
  210. int i, set = -1;
  211. intname.s = sk_STACK_OF_X509_NAME_ENTRY_new_null();
  212. if (!intname.s)
  213. goto memerr;
  214. for (i = 0; i < sk_X509_NAME_ENTRY_num(a->entries); i++) {
  215. entry = sk_X509_NAME_ENTRY_value(a->entries, i);
  216. if (entry->set != set) {
  217. entries = sk_X509_NAME_ENTRY_new_null();
  218. if (!entries)
  219. goto memerr;
  220. if (!sk_STACK_OF_X509_NAME_ENTRY_push(intname.s, entries)) {
  221. sk_X509_NAME_ENTRY_free(entries);
  222. goto memerr;
  223. }
  224. set = entry->set;
  225. }
  226. if (!sk_X509_NAME_ENTRY_push(entries, entry))
  227. goto memerr;
  228. }
  229. len = ASN1_item_ex_i2d(&intname.a, NULL,
  230. ASN1_ITEM_rptr(X509_NAME_INTERNAL), -1, -1);
  231. if (!BUF_MEM_grow(a->bytes, len))
  232. goto memerr;
  233. p = (unsigned char *)a->bytes->data;
  234. ASN1_item_ex_i2d(&intname.a,
  235. &p, ASN1_ITEM_rptr(X509_NAME_INTERNAL), -1, -1);
  236. sk_STACK_OF_X509_NAME_ENTRY_pop_free(intname.s,
  237. local_sk_X509_NAME_ENTRY_free);
  238. a->modified = 0;
  239. return len;
  240. memerr:
  241. sk_STACK_OF_X509_NAME_ENTRY_pop_free(intname.s,
  242. local_sk_X509_NAME_ENTRY_free);
  243. ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
  244. return -1;
  245. }
  246. static int x509_name_ex_print(BIO *out, const ASN1_VALUE **pval,
  247. int indent,
  248. const char *fname, const ASN1_PCTX *pctx)
  249. {
  250. if (X509_NAME_print_ex(out, (const X509_NAME *)*pval,
  251. indent, pctx->nm_flags) <= 0)
  252. return 0;
  253. return 2;
  254. }
  255. /*
  256. * This function generates the canonical encoding of the Name structure. In
  257. * it all strings are converted to UTF8, leading, trailing and multiple
  258. * spaces collapsed, converted to lower case and the leading SEQUENCE header
  259. * removed. In future we could also normalize the UTF8 too. By doing this
  260. * comparison of Name structures can be rapidly performed by just using
  261. * memcmp() of the canonical encoding. By omitting the leading SEQUENCE name
  262. * constraints of type dirName can also be checked with a simple memcmp().
  263. * NOTE: For empty X509_NAME (NULL-DN), canon_enclen == 0 && canon_enc == NULL
  264. */
  265. static int x509_name_canon(X509_NAME *a)
  266. {
  267. unsigned char *p;
  268. STACK_OF(STACK_OF_X509_NAME_ENTRY) *intname;
  269. STACK_OF(X509_NAME_ENTRY) *entries = NULL;
  270. X509_NAME_ENTRY *entry, *tmpentry = NULL;
  271. int i, set = -1, ret = 0, len;
  272. OPENSSL_free(a->canon_enc);
  273. a->canon_enc = NULL;
  274. /* Special case: empty X509_NAME => null encoding */
  275. if (sk_X509_NAME_ENTRY_num(a->entries) == 0) {
  276. a->canon_enclen = 0;
  277. return 1;
  278. }
  279. intname = sk_STACK_OF_X509_NAME_ENTRY_new_null();
  280. if (intname == NULL) {
  281. ERR_raise(ERR_LIB_X509, ERR_R_MALLOC_FAILURE);
  282. goto err;
  283. }
  284. for (i = 0; i < sk_X509_NAME_ENTRY_num(a->entries); i++) {
  285. entry = sk_X509_NAME_ENTRY_value(a->entries, i);
  286. if (entry->set != set) {
  287. entries = sk_X509_NAME_ENTRY_new_null();
  288. if (entries == NULL)
  289. goto err;
  290. if (!sk_STACK_OF_X509_NAME_ENTRY_push(intname, entries)) {
  291. sk_X509_NAME_ENTRY_free(entries);
  292. ERR_raise(ERR_LIB_X509, ERR_R_MALLOC_FAILURE);
  293. goto err;
  294. }
  295. set = entry->set;
  296. }
  297. tmpentry = X509_NAME_ENTRY_new();
  298. if (tmpentry == NULL) {
  299. ERR_raise(ERR_LIB_X509, ERR_R_MALLOC_FAILURE);
  300. goto err;
  301. }
  302. tmpentry->object = OBJ_dup(entry->object);
  303. if (tmpentry->object == NULL) {
  304. ERR_raise(ERR_LIB_X509, ERR_R_MALLOC_FAILURE);
  305. goto err;
  306. }
  307. if (!asn1_string_canon(tmpentry->value, entry->value))
  308. goto err;
  309. if (!sk_X509_NAME_ENTRY_push(entries, tmpentry)) {
  310. ERR_raise(ERR_LIB_X509, ERR_R_MALLOC_FAILURE);
  311. goto err;
  312. }
  313. tmpentry = NULL;
  314. }
  315. /* Finally generate encoding */
  316. len = i2d_name_canon(intname, NULL);
  317. if (len < 0)
  318. goto err;
  319. a->canon_enclen = len;
  320. p = OPENSSL_malloc(a->canon_enclen);
  321. if (p == NULL) {
  322. ERR_raise(ERR_LIB_X509, ERR_R_MALLOC_FAILURE);
  323. goto err;
  324. }
  325. a->canon_enc = p;
  326. i2d_name_canon(intname, &p);
  327. ret = 1;
  328. err:
  329. X509_NAME_ENTRY_free(tmpentry);
  330. sk_STACK_OF_X509_NAME_ENTRY_pop_free(intname,
  331. local_sk_X509_NAME_ENTRY_pop_free);
  332. return ret;
  333. }
  334. /* Bitmap of all the types of string that will be canonicalized. */
  335. #define ASN1_MASK_CANON \
  336. (B_ASN1_UTF8STRING | B_ASN1_BMPSTRING | B_ASN1_UNIVERSALSTRING \
  337. | B_ASN1_PRINTABLESTRING | B_ASN1_T61STRING | B_ASN1_IA5STRING \
  338. | B_ASN1_VISIBLESTRING)
  339. static int asn1_string_canon(ASN1_STRING *out, const ASN1_STRING *in)
  340. {
  341. unsigned char *to, *from;
  342. int len, i;
  343. /* If type not in bitmask just copy string across */
  344. if (!(ASN1_tag2bit(in->type) & ASN1_MASK_CANON)) {
  345. if (!ASN1_STRING_copy(out, in))
  346. return 0;
  347. return 1;
  348. }
  349. out->type = V_ASN1_UTF8STRING;
  350. out->length = ASN1_STRING_to_UTF8(&out->data, in);
  351. if (out->length == -1)
  352. return 0;
  353. to = out->data;
  354. from = to;
  355. len = out->length;
  356. /*
  357. * Convert string in place to canonical form. Ultimately we may need to
  358. * handle a wider range of characters but for now ignore anything with
  359. * MSB set and rely on the ossl_isspace() to fail on bad characters without
  360. * needing isascii or range checks as well.
  361. */
  362. /* Ignore leading spaces */
  363. while (len > 0 && ossl_isspace(*from)) {
  364. from++;
  365. len--;
  366. }
  367. to = from + len;
  368. /* Ignore trailing spaces */
  369. while (len > 0 && ossl_isspace(to[-1])) {
  370. to--;
  371. len--;
  372. }
  373. to = out->data;
  374. i = 0;
  375. while (i < len) {
  376. /* If not ASCII set just copy across */
  377. if (!ossl_isascii(*from)) {
  378. *to++ = *from++;
  379. i++;
  380. }
  381. /* Collapse multiple spaces */
  382. else if (ossl_isspace(*from)) {
  383. /* Copy one space across */
  384. *to++ = ' ';
  385. /*
  386. * Ignore subsequent spaces. Note: don't need to check len here
  387. * because we know the last character is a non-space so we can't
  388. * overflow.
  389. */
  390. do {
  391. from++;
  392. i++;
  393. }
  394. while (ossl_isspace(*from));
  395. } else {
  396. *to++ = ossl_tolower(*from);
  397. from++;
  398. i++;
  399. }
  400. }
  401. out->length = to - out->data;
  402. return 1;
  403. }
  404. static int i2d_name_canon(const STACK_OF(STACK_OF_X509_NAME_ENTRY) * _intname,
  405. unsigned char **in)
  406. {
  407. int i, len, ltmp;
  408. const ASN1_VALUE *v;
  409. STACK_OF(ASN1_VALUE) *intname = (STACK_OF(ASN1_VALUE) *)_intname;
  410. len = 0;
  411. for (i = 0; i < sk_ASN1_VALUE_num(intname); i++) {
  412. v = sk_ASN1_VALUE_value(intname, i);
  413. ltmp = ASN1_item_ex_i2d(&v, in,
  414. ASN1_ITEM_rptr(X509_NAME_ENTRIES), -1, -1);
  415. if (ltmp < 0)
  416. return ltmp;
  417. len += ltmp;
  418. }
  419. return len;
  420. }
  421. int X509_NAME_set(X509_NAME **xn, const X509_NAME *name)
  422. {
  423. X509_NAME *name_copy;
  424. if (*xn == name)
  425. return *xn != NULL;
  426. if ((name_copy = X509_NAME_dup(name)) == NULL)
  427. return 0;
  428. X509_NAME_free(*xn);
  429. *xn = name_copy;
  430. return 1;
  431. }
  432. int X509_NAME_print(BIO *bp, const X509_NAME *name, int obase)
  433. {
  434. char *s, *c, *b;
  435. int l, i;
  436. l = 80 - 2 - obase;
  437. b = X509_NAME_oneline(name, NULL, 0);
  438. if (b == NULL)
  439. return 0;
  440. if (*b == '\0') {
  441. OPENSSL_free(b);
  442. return 1;
  443. }
  444. s = b + 1; /* skip the first slash */
  445. c = s;
  446. for (;;) {
  447. if (((*s == '/') &&
  448. (ossl_isupper(s[1]) && ((s[2] == '=') ||
  449. (ossl_isupper(s[2]) && (s[3] == '='))
  450. ))) || (*s == '\0'))
  451. {
  452. i = s - c;
  453. if (BIO_write(bp, c, i) != i)
  454. goto err;
  455. c = s + 1; /* skip following slash */
  456. if (*s != '\0') {
  457. if (BIO_write(bp, ", ", 2) != 2)
  458. goto err;
  459. }
  460. l--;
  461. }
  462. if (*s == '\0')
  463. break;
  464. s++;
  465. l--;
  466. }
  467. OPENSSL_free(b);
  468. return 1;
  469. err:
  470. ERR_raise(ERR_LIB_X509, ERR_R_BUF_LIB);
  471. OPENSSL_free(b);
  472. return 0;
  473. }
  474. int X509_NAME_get0_der(const X509_NAME *nm, const unsigned char **pder,
  475. size_t *pderlen)
  476. {
  477. /* Make sure encoding is valid */
  478. if (i2d_X509_NAME(nm, NULL) <= 0)
  479. return 0;
  480. if (pder != NULL)
  481. *pder = (unsigned char *)nm->bytes->data;
  482. if (pderlen != NULL)
  483. *pderlen = nm->bytes->length;
  484. return 1;
  485. }