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