x_name.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535
  1. /* crypto/asn1/x_name.c */
  2. /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  3. * All rights reserved.
  4. *
  5. * This package is an SSL implementation written
  6. * by Eric Young (eay@cryptsoft.com).
  7. * The implementation was written so as to conform with Netscapes SSL.
  8. *
  9. * This library is free for commercial and non-commercial use as long as
  10. * the following conditions are aheared to. The following conditions
  11. * apply to all code found in this distribution, be it the RC4, RSA,
  12. * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  13. * included with this distribution is covered by the same copyright terms
  14. * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  15. *
  16. * Copyright remains Eric Young's, and as such any Copyright notices in
  17. * the code are not to be removed.
  18. * If this package is used in a product, Eric Young should be given attribution
  19. * as the author of the parts of the library used.
  20. * This can be in the form of a textual message at program startup or
  21. * in documentation (online or textual) provided with the package.
  22. *
  23. * Redistribution and use in source and binary forms, with or without
  24. * modification, are permitted provided that the following conditions
  25. * are met:
  26. * 1. Redistributions of source code must retain the copyright
  27. * notice, this list of conditions and the following disclaimer.
  28. * 2. Redistributions in binary form must reproduce the above copyright
  29. * notice, this list of conditions and the following disclaimer in the
  30. * documentation and/or other materials provided with the distribution.
  31. * 3. All advertising materials mentioning features or use of this software
  32. * must display the following acknowledgement:
  33. * "This product includes cryptographic software written by
  34. * Eric Young (eay@cryptsoft.com)"
  35. * The word 'cryptographic' can be left out if the rouines from the library
  36. * being used are not cryptographic related :-).
  37. * 4. If you include any Windows specific code (or a derivative thereof) from
  38. * the apps directory (application code) you must include an acknowledgement:
  39. * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  40. *
  41. * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  42. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  43. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  44. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  45. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  46. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  47. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  48. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  49. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  50. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  51. * SUCH DAMAGE.
  52. *
  53. * The licence and distribution terms for any publically available version or
  54. * derivative of this code cannot be changed. i.e. this code cannot simply be
  55. * copied and put under another distribution licence
  56. * [including the GNU Public Licence.]
  57. */
  58. #include <stdio.h>
  59. #include <ctype.h>
  60. #include "cryptlib.h"
  61. #include <openssl/asn1t.h>
  62. #include <openssl/x509.h>
  63. #include "asn1_locl.h"
  64. typedef STACK_OF(X509_NAME_ENTRY) STACK_OF_X509_NAME_ENTRY;
  65. DECLARE_STACK_OF(STACK_OF_X509_NAME_ENTRY)
  66. /*
  67. * Maximum length of X509_NAME: much larger than anything we should
  68. * ever see in practice.
  69. */
  70. #define X509_NAME_MAX (1024 * 1024)
  71. static int x509_name_ex_d2i(ASN1_VALUE **val,
  72. const unsigned char **in, long len,
  73. const ASN1_ITEM *it,
  74. int tag, int aclass, char opt, ASN1_TLC *ctx);
  75. static int x509_name_ex_i2d(ASN1_VALUE **val, unsigned char **out,
  76. const ASN1_ITEM *it, int tag, int aclass);
  77. static int x509_name_ex_new(ASN1_VALUE **val, const ASN1_ITEM *it);
  78. static void x509_name_ex_free(ASN1_VALUE **val, const ASN1_ITEM *it);
  79. static int x509_name_encode(X509_NAME *a);
  80. static int x509_name_canon(X509_NAME *a);
  81. static int asn1_string_canon(ASN1_STRING *out, ASN1_STRING *in);
  82. static int i2d_name_canon(STACK_OF(STACK_OF_X509_NAME_ENTRY) * intname,
  83. unsigned char **in);
  84. static int x509_name_ex_print(BIO *out, ASN1_VALUE **pval,
  85. int indent,
  86. const char *fname, const ASN1_PCTX *pctx);
  87. ASN1_SEQUENCE(X509_NAME_ENTRY) = {
  88. ASN1_SIMPLE(X509_NAME_ENTRY, object, ASN1_OBJECT),
  89. ASN1_SIMPLE(X509_NAME_ENTRY, value, ASN1_PRINTABLE)
  90. } ASN1_SEQUENCE_END(X509_NAME_ENTRY)
  91. IMPLEMENT_ASN1_FUNCTIONS(X509_NAME_ENTRY)
  92. IMPLEMENT_ASN1_DUP_FUNCTION(X509_NAME_ENTRY)
  93. /*
  94. * For the "Name" type we need a SEQUENCE OF { SET OF X509_NAME_ENTRY } so
  95. * declare two template wrappers for this
  96. */
  97. ASN1_ITEM_TEMPLATE(X509_NAME_ENTRIES) =
  98. ASN1_EX_TEMPLATE_TYPE(ASN1_TFLG_SET_OF, 0, RDNS, X509_NAME_ENTRY)
  99. ASN1_ITEM_TEMPLATE_END(X509_NAME_ENTRIES)
  100. ASN1_ITEM_TEMPLATE(X509_NAME_INTERNAL) =
  101. ASN1_EX_TEMPLATE_TYPE(ASN1_TFLG_SEQUENCE_OF, 0, Name, X509_NAME_ENTRIES)
  102. ASN1_ITEM_TEMPLATE_END(X509_NAME_INTERNAL)
  103. /*
  104. * Normally that's where it would end: we'd have two nested STACK structures
  105. * representing the ASN1. Unfortunately X509_NAME uses a completely different
  106. * form and caches encodings so we have to process the internal form and
  107. * convert to the external form.
  108. */
  109. const ASN1_EXTERN_FUNCS x509_name_ff = {
  110. NULL,
  111. x509_name_ex_new,
  112. x509_name_ex_free,
  113. 0, /* Default clear behaviour is OK */
  114. x509_name_ex_d2i,
  115. x509_name_ex_i2d,
  116. x509_name_ex_print
  117. };
  118. IMPLEMENT_EXTERN_ASN1(X509_NAME, V_ASN1_SEQUENCE, x509_name_ff)
  119. IMPLEMENT_ASN1_FUNCTIONS(X509_NAME)
  120. IMPLEMENT_ASN1_DUP_FUNCTION(X509_NAME)
  121. static int x509_name_ex_new(ASN1_VALUE **val, const ASN1_ITEM *it)
  122. {
  123. X509_NAME *ret = NULL;
  124. ret = OPENSSL_malloc(sizeof(X509_NAME));
  125. if (!ret)
  126. goto memerr;
  127. if ((ret->entries = sk_X509_NAME_ENTRY_new_null()) == NULL)
  128. goto memerr;
  129. if ((ret->bytes = BUF_MEM_new()) == NULL)
  130. goto memerr;
  131. ret->canon_enc = NULL;
  132. ret->canon_enclen = 0;
  133. ret->modified = 1;
  134. *val = (ASN1_VALUE *)ret;
  135. return 1;
  136. memerr:
  137. ASN1err(ASN1_F_X509_NAME_EX_NEW, ERR_R_MALLOC_FAILURE);
  138. if (ret) {
  139. if (ret->entries)
  140. sk_X509_NAME_ENTRY_free(ret->entries);
  141. OPENSSL_free(ret);
  142. }
  143. return 0;
  144. }
  145. static void x509_name_ex_free(ASN1_VALUE **pval, const ASN1_ITEM *it)
  146. {
  147. X509_NAME *a;
  148. if (!pval || !*pval)
  149. return;
  150. a = (X509_NAME *)*pval;
  151. BUF_MEM_free(a->bytes);
  152. sk_X509_NAME_ENTRY_pop_free(a->entries, X509_NAME_ENTRY_free);
  153. if (a->canon_enc)
  154. OPENSSL_free(a->canon_enc);
  155. OPENSSL_free(a);
  156. *pval = NULL;
  157. }
  158. static void local_sk_X509_NAME_ENTRY_free(STACK_OF(X509_NAME_ENTRY) *ne)
  159. {
  160. sk_X509_NAME_ENTRY_free(ne);
  161. }
  162. static void local_sk_X509_NAME_ENTRY_pop_free(STACK_OF(X509_NAME_ENTRY) *ne)
  163. {
  164. sk_X509_NAME_ENTRY_pop_free(ne, X509_NAME_ENTRY_free);
  165. }
  166. static int x509_name_ex_d2i(ASN1_VALUE **val,
  167. const unsigned char **in, long len,
  168. const ASN1_ITEM *it, int tag, int aclass,
  169. char opt, ASN1_TLC *ctx)
  170. {
  171. const unsigned char *p = *in, *q;
  172. union {
  173. STACK_OF(STACK_OF_X509_NAME_ENTRY) *s;
  174. ASN1_VALUE *a;
  175. } intname = {
  176. NULL
  177. };
  178. union {
  179. X509_NAME *x;
  180. ASN1_VALUE *a;
  181. } nm = {
  182. NULL
  183. };
  184. int i, j, ret;
  185. STACK_OF(X509_NAME_ENTRY) *entries;
  186. X509_NAME_ENTRY *entry;
  187. if (len > X509_NAME_MAX)
  188. len = X509_NAME_MAX;
  189. q = p;
  190. /* Get internal representation of Name */
  191. ret = ASN1_item_ex_d2i(&intname.a,
  192. &p, len, ASN1_ITEM_rptr(X509_NAME_INTERNAL),
  193. tag, aclass, opt, ctx);
  194. if (ret <= 0)
  195. return ret;
  196. if (*val)
  197. x509_name_ex_free(val, NULL);
  198. if (!x509_name_ex_new(&nm.a, NULL))
  199. goto err;
  200. /* We've decoded it: now cache encoding */
  201. if (!BUF_MEM_grow(nm.x->bytes, p - q))
  202. goto err;
  203. memcpy(nm.x->bytes->data, q, p - q);
  204. /* Convert internal representation to X509_NAME structure */
  205. for (i = 0; i < sk_STACK_OF_X509_NAME_ENTRY_num(intname.s); i++) {
  206. entries = sk_STACK_OF_X509_NAME_ENTRY_value(intname.s, i);
  207. for (j = 0; j < sk_X509_NAME_ENTRY_num(entries); j++) {
  208. entry = sk_X509_NAME_ENTRY_value(entries, j);
  209. entry->set = i;
  210. if (!sk_X509_NAME_ENTRY_push(nm.x->entries, entry))
  211. goto err;
  212. sk_X509_NAME_ENTRY_set(entries, j, NULL);
  213. }
  214. }
  215. ret = x509_name_canon(nm.x);
  216. if (!ret)
  217. goto err;
  218. sk_STACK_OF_X509_NAME_ENTRY_pop_free(intname.s,
  219. local_sk_X509_NAME_ENTRY_free);
  220. nm.x->modified = 0;
  221. *val = nm.a;
  222. *in = p;
  223. return ret;
  224. err:
  225. if (nm.x != NULL)
  226. X509_NAME_free(nm.x);
  227. sk_STACK_OF_X509_NAME_ENTRY_pop_free(intname.s,
  228. local_sk_X509_NAME_ENTRY_pop_free);
  229. ASN1err(ASN1_F_X509_NAME_EX_D2I, ERR_R_NESTED_ASN1_ERROR);
  230. return 0;
  231. }
  232. static int x509_name_ex_i2d(ASN1_VALUE **val, unsigned char **out,
  233. const ASN1_ITEM *it, int tag, int aclass)
  234. {
  235. int ret;
  236. X509_NAME *a = (X509_NAME *)*val;
  237. if (a->modified) {
  238. ret = x509_name_encode(a);
  239. if (ret < 0)
  240. return ret;
  241. ret = x509_name_canon(a);
  242. if (ret < 0)
  243. return ret;
  244. }
  245. ret = a->bytes->length;
  246. if (out != NULL) {
  247. memcpy(*out, a->bytes->data, ret);
  248. *out += ret;
  249. }
  250. return ret;
  251. }
  252. static int x509_name_encode(X509_NAME *a)
  253. {
  254. union {
  255. STACK_OF(STACK_OF_X509_NAME_ENTRY) *s;
  256. ASN1_VALUE *a;
  257. } intname = {
  258. NULL
  259. };
  260. int len;
  261. unsigned char *p;
  262. STACK_OF(X509_NAME_ENTRY) *entries = NULL;
  263. X509_NAME_ENTRY *entry;
  264. int i, set = -1;
  265. intname.s = sk_STACK_OF_X509_NAME_ENTRY_new_null();
  266. if (!intname.s)
  267. goto memerr;
  268. for (i = 0; i < sk_X509_NAME_ENTRY_num(a->entries); i++) {
  269. entry = sk_X509_NAME_ENTRY_value(a->entries, i);
  270. if (entry->set != set) {
  271. entries = sk_X509_NAME_ENTRY_new_null();
  272. if (!entries)
  273. goto memerr;
  274. if (!sk_STACK_OF_X509_NAME_ENTRY_push(intname.s, entries)) {
  275. sk_X509_NAME_ENTRY_free(entries);
  276. goto memerr;
  277. }
  278. set = entry->set;
  279. }
  280. if (!sk_X509_NAME_ENTRY_push(entries, entry))
  281. goto memerr;
  282. }
  283. len = ASN1_item_ex_i2d(&intname.a, NULL,
  284. ASN1_ITEM_rptr(X509_NAME_INTERNAL), -1, -1);
  285. if (!BUF_MEM_grow(a->bytes, len))
  286. goto memerr;
  287. p = (unsigned char *)a->bytes->data;
  288. ASN1_item_ex_i2d(&intname.a,
  289. &p, ASN1_ITEM_rptr(X509_NAME_INTERNAL), -1, -1);
  290. sk_STACK_OF_X509_NAME_ENTRY_pop_free(intname.s,
  291. local_sk_X509_NAME_ENTRY_free);
  292. a->modified = 0;
  293. return len;
  294. memerr:
  295. sk_STACK_OF_X509_NAME_ENTRY_pop_free(intname.s,
  296. local_sk_X509_NAME_ENTRY_free);
  297. ASN1err(ASN1_F_X509_NAME_ENCODE, ERR_R_MALLOC_FAILURE);
  298. return -1;
  299. }
  300. static int x509_name_ex_print(BIO *out, ASN1_VALUE **pval,
  301. int indent,
  302. const char *fname, const ASN1_PCTX *pctx)
  303. {
  304. if (X509_NAME_print_ex(out, (X509_NAME *)*pval,
  305. indent, pctx->nm_flags) <= 0)
  306. return 0;
  307. return 2;
  308. }
  309. /*
  310. * This function generates the canonical encoding of the Name structure. In
  311. * it all strings are converted to UTF8, leading, trailing and multiple
  312. * spaces collapsed, converted to lower case and the leading SEQUENCE header
  313. * removed. In future we could also normalize the UTF8 too. By doing this
  314. * comparison of Name structures can be rapidly perfomed by just using
  315. * memcmp() of the canonical encoding. By omitting the leading SEQUENCE name
  316. * constraints of type dirName can also be checked with a simple memcmp().
  317. */
  318. static int x509_name_canon(X509_NAME *a)
  319. {
  320. unsigned char *p;
  321. STACK_OF(STACK_OF_X509_NAME_ENTRY) *intname = NULL;
  322. STACK_OF(X509_NAME_ENTRY) *entries = NULL;
  323. X509_NAME_ENTRY *entry, *tmpentry = NULL;
  324. int i, set = -1, ret = 0;
  325. if (a->canon_enc) {
  326. OPENSSL_free(a->canon_enc);
  327. a->canon_enc = NULL;
  328. }
  329. /* Special case: empty X509_NAME => null encoding */
  330. if (sk_X509_NAME_ENTRY_num(a->entries) == 0) {
  331. a->canon_enclen = 0;
  332. return 1;
  333. }
  334. intname = sk_STACK_OF_X509_NAME_ENTRY_new_null();
  335. if (!intname)
  336. goto err;
  337. for (i = 0; i < sk_X509_NAME_ENTRY_num(a->entries); i++) {
  338. entry = sk_X509_NAME_ENTRY_value(a->entries, i);
  339. if (entry->set != set) {
  340. entries = sk_X509_NAME_ENTRY_new_null();
  341. if (!entries)
  342. goto err;
  343. if (!sk_STACK_OF_X509_NAME_ENTRY_push(intname, entries)) {
  344. sk_X509_NAME_ENTRY_free(entries);
  345. goto err;
  346. }
  347. set = entry->set;
  348. }
  349. tmpentry = X509_NAME_ENTRY_new();
  350. if (!tmpentry)
  351. goto err;
  352. tmpentry->object = OBJ_dup(entry->object);
  353. if (!asn1_string_canon(tmpentry->value, entry->value))
  354. goto err;
  355. if (!sk_X509_NAME_ENTRY_push(entries, tmpentry))
  356. goto err;
  357. tmpentry = NULL;
  358. }
  359. /* Finally generate encoding */
  360. a->canon_enclen = i2d_name_canon(intname, NULL);
  361. p = OPENSSL_malloc(a->canon_enclen);
  362. if (!p)
  363. goto err;
  364. a->canon_enc = p;
  365. i2d_name_canon(intname, &p);
  366. ret = 1;
  367. err:
  368. if (tmpentry)
  369. X509_NAME_ENTRY_free(tmpentry);
  370. if (intname)
  371. sk_STACK_OF_X509_NAME_ENTRY_pop_free(intname,
  372. local_sk_X509_NAME_ENTRY_pop_free);
  373. return ret;
  374. }
  375. /* Bitmap of all the types of string that will be canonicalized. */
  376. #define ASN1_MASK_CANON \
  377. (B_ASN1_UTF8STRING | B_ASN1_BMPSTRING | B_ASN1_UNIVERSALSTRING \
  378. | B_ASN1_PRINTABLESTRING | B_ASN1_T61STRING | B_ASN1_IA5STRING \
  379. | B_ASN1_VISIBLESTRING)
  380. static int asn1_string_canon(ASN1_STRING *out, ASN1_STRING *in)
  381. {
  382. unsigned char *to, *from;
  383. int len, i;
  384. /* If type not in bitmask just copy string across */
  385. if (!(ASN1_tag2bit(in->type) & ASN1_MASK_CANON)) {
  386. if (!ASN1_STRING_copy(out, in))
  387. return 0;
  388. return 1;
  389. }
  390. out->type = V_ASN1_UTF8STRING;
  391. out->length = ASN1_STRING_to_UTF8(&out->data, in);
  392. if (out->length == -1)
  393. return 0;
  394. to = out->data;
  395. from = to;
  396. len = out->length;
  397. /*
  398. * Convert string in place to canonical form. Ultimately we may need to
  399. * handle a wider range of characters but for now ignore anything with
  400. * MSB set and rely on the isspace() and tolower() functions.
  401. */
  402. /* Ignore leading spaces */
  403. while ((len > 0) && !(*from & 0x80) && isspace(*from)) {
  404. from++;
  405. len--;
  406. }
  407. to = from + len - 1;
  408. /* Ignore trailing spaces */
  409. while ((len > 0) && !(*to & 0x80) && isspace(*to)) {
  410. to--;
  411. len--;
  412. }
  413. to = out->data;
  414. i = 0;
  415. while (i < len) {
  416. /* If MSB set just copy across */
  417. if (*from & 0x80) {
  418. *to++ = *from++;
  419. i++;
  420. }
  421. /* Collapse multiple spaces */
  422. else if (isspace(*from)) {
  423. /* Copy one space across */
  424. *to++ = ' ';
  425. /*
  426. * Ignore subsequent spaces. Note: don't need to check len here
  427. * because we know the last character is a non-space so we can't
  428. * overflow.
  429. */
  430. do {
  431. from++;
  432. i++;
  433. }
  434. while (!(*from & 0x80) && isspace(*from));
  435. } else {
  436. *to++ = tolower(*from);
  437. from++;
  438. i++;
  439. }
  440. }
  441. out->length = to - out->data;
  442. return 1;
  443. }
  444. static int i2d_name_canon(STACK_OF(STACK_OF_X509_NAME_ENTRY) * _intname,
  445. unsigned char **in)
  446. {
  447. int i, len, ltmp;
  448. ASN1_VALUE *v;
  449. STACK_OF(ASN1_VALUE) *intname = (STACK_OF(ASN1_VALUE) *)_intname;
  450. len = 0;
  451. for (i = 0; i < sk_ASN1_VALUE_num(intname); i++) {
  452. v = sk_ASN1_VALUE_value(intname, i);
  453. ltmp = ASN1_item_ex_i2d(&v, in,
  454. ASN1_ITEM_rptr(X509_NAME_ENTRIES), -1, -1);
  455. if (ltmp < 0)
  456. return ltmp;
  457. len += ltmp;
  458. }
  459. return len;
  460. }
  461. int X509_NAME_set(X509_NAME **xn, X509_NAME *name)
  462. {
  463. if ((name = X509_NAME_dup(name)) == NULL)
  464. return 0;
  465. X509_NAME_free(*xn);
  466. *xn = name;
  467. return 1;
  468. }
  469. IMPLEMENT_STACK_OF(X509_NAME_ENTRY)
  470. IMPLEMENT_ASN1_SET_OF(X509_NAME_ENTRY)