x_name.c 16 KB

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