x_name.c 14 KB

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