x_name.c 17 KB

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