Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 
 
 
 

539 wiersze
15 KiB

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