x_name.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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 "cryptlib.h"
  60. #include <openssl/asn1t.h>
  61. #include <openssl/x509.h>
  62. #include "asn1_locl.h"
  63. static int x509_name_ex_d2i(ASN1_VALUE **val, const unsigned char **in, long len, const ASN1_ITEM *it,
  64. int tag, int aclass, char opt, ASN1_TLC *ctx);
  65. static int x509_name_ex_i2d(ASN1_VALUE **val, unsigned char **out, const ASN1_ITEM *it, int tag, int aclass);
  66. static int x509_name_ex_new(ASN1_VALUE **val, const ASN1_ITEM *it);
  67. static void x509_name_ex_free(ASN1_VALUE **val, const ASN1_ITEM *it);
  68. static int x509_name_encode(X509_NAME *a);
  69. static int x509_name_ex_print(BIO *out, ASN1_VALUE **pval,
  70. int indent,
  71. const char *fname,
  72. const ASN1_PCTX *pctx);
  73. ASN1_SEQUENCE(X509_NAME_ENTRY) = {
  74. ASN1_SIMPLE(X509_NAME_ENTRY, object, ASN1_OBJECT),
  75. ASN1_SIMPLE(X509_NAME_ENTRY, value, ASN1_PRINTABLE)
  76. } ASN1_SEQUENCE_END(X509_NAME_ENTRY)
  77. IMPLEMENT_ASN1_FUNCTIONS(X509_NAME_ENTRY)
  78. IMPLEMENT_ASN1_DUP_FUNCTION(X509_NAME_ENTRY)
  79. /* For the "Name" type we need a SEQUENCE OF { SET OF X509_NAME_ENTRY }
  80. * so declare two template wrappers for this
  81. */
  82. ASN1_ITEM_TEMPLATE(X509_NAME_ENTRIES) =
  83. ASN1_EX_TEMPLATE_TYPE(ASN1_TFLG_SET_OF, 0, RDNS, X509_NAME_ENTRY)
  84. ASN1_ITEM_TEMPLATE_END(X509_NAME_ENTRIES)
  85. ASN1_ITEM_TEMPLATE(X509_NAME_INTERNAL) =
  86. ASN1_EX_TEMPLATE_TYPE(ASN1_TFLG_SEQUENCE_OF, 0, Name, X509_NAME_ENTRIES)
  87. ASN1_ITEM_TEMPLATE_END(X509_NAME_INTERNAL)
  88. /* Normally that's where it would end: we'd have two nested STACK structures
  89. * representing the ASN1. Unfortunately X509_NAME uses a completely different
  90. * form and caches encodings so we have to process the internal form and convert
  91. * to the external form.
  92. */
  93. const ASN1_EXTERN_FUNCS x509_name_ff = {
  94. NULL,
  95. x509_name_ex_new,
  96. x509_name_ex_free,
  97. 0, /* Default clear behaviour is OK */
  98. x509_name_ex_d2i,
  99. x509_name_ex_i2d,
  100. x509_name_ex_print
  101. };
  102. IMPLEMENT_EXTERN_ASN1(X509_NAME, V_ASN1_SEQUENCE, x509_name_ff)
  103. IMPLEMENT_ASN1_FUNCTIONS(X509_NAME)
  104. IMPLEMENT_ASN1_DUP_FUNCTION(X509_NAME)
  105. static int x509_name_ex_new(ASN1_VALUE **val, const ASN1_ITEM *it)
  106. {
  107. X509_NAME *ret = NULL;
  108. ret = OPENSSL_malloc(sizeof(X509_NAME));
  109. if(!ret) goto memerr;
  110. if ((ret->entries=sk_X509_NAME_ENTRY_new_null()) == NULL)
  111. goto memerr;
  112. if((ret->bytes = BUF_MEM_new()) == NULL) goto memerr;
  113. ret->modified=1;
  114. *val = (ASN1_VALUE *)ret;
  115. return 1;
  116. memerr:
  117. ASN1err(ASN1_F_X509_NAME_EX_NEW, ERR_R_MALLOC_FAILURE);
  118. if (ret)
  119. {
  120. if (ret->entries)
  121. sk_X509_NAME_ENTRY_free(ret->entries);
  122. OPENSSL_free(ret);
  123. }
  124. return 0;
  125. }
  126. static void x509_name_ex_free(ASN1_VALUE **pval, const ASN1_ITEM *it)
  127. {
  128. X509_NAME *a;
  129. if(!pval || !*pval)
  130. return;
  131. a = (X509_NAME *)*pval;
  132. BUF_MEM_free(a->bytes);
  133. sk_X509_NAME_ENTRY_pop_free(a->entries,X509_NAME_ENTRY_free);
  134. OPENSSL_free(a);
  135. *pval = NULL;
  136. }
  137. /* Used with sk_pop_free() to free up the internal representation.
  138. * NB: we only free the STACK and not its contents because it is
  139. * already present in the X509_NAME structure.
  140. */
  141. static void sk_internal_free(void *a)
  142. {
  143. sk_free(a);
  144. }
  145. static int x509_name_ex_d2i(ASN1_VALUE **val, const unsigned char **in, long len, const ASN1_ITEM *it,
  146. int tag, int aclass, char opt, ASN1_TLC *ctx)
  147. {
  148. const unsigned char *p = *in, *q;
  149. STACK *intname = NULL, **intname_pp = &intname;
  150. int i, j, ret;
  151. X509_NAME *nm = NULL, **nm_pp = &nm;
  152. STACK_OF(X509_NAME_ENTRY) *entries;
  153. X509_NAME_ENTRY *entry;
  154. q = p;
  155. /* Get internal representation of Name */
  156. ret = ASN1_item_ex_d2i((ASN1_VALUE **)intname_pp,
  157. &p, len, ASN1_ITEM_rptr(X509_NAME_INTERNAL),
  158. tag, aclass, opt, ctx);
  159. if(ret <= 0) return ret;
  160. if(*val) x509_name_ex_free(val, NULL);
  161. if(!x509_name_ex_new((ASN1_VALUE **)nm_pp, NULL)) goto err;
  162. /* We've decoded it: now cache encoding */
  163. if(!BUF_MEM_grow(nm->bytes, p - q)) goto err;
  164. memcpy(nm->bytes->data, q, p - q);
  165. /* Convert internal representation to X509_NAME structure */
  166. for(i = 0; i < sk_num(intname); i++) {
  167. entries = (STACK_OF(X509_NAME_ENTRY) *)sk_value(intname, i);
  168. for(j = 0; j < sk_X509_NAME_ENTRY_num(entries); j++) {
  169. entry = sk_X509_NAME_ENTRY_value(entries, j);
  170. entry->set = i;
  171. if(!sk_X509_NAME_ENTRY_push(nm->entries, entry))
  172. goto err;
  173. }
  174. sk_X509_NAME_ENTRY_free(entries);
  175. }
  176. sk_free(intname);
  177. nm->modified = 0;
  178. *val = (ASN1_VALUE *)nm;
  179. *in = p;
  180. return ret;
  181. err:
  182. ASN1err(ASN1_F_X509_NAME_EX_D2I, ERR_R_NESTED_ASN1_ERROR);
  183. return 0;
  184. }
  185. static int x509_name_ex_i2d(ASN1_VALUE **val, unsigned char **out, const ASN1_ITEM *it, int tag, int aclass)
  186. {
  187. int ret;
  188. X509_NAME *a = (X509_NAME *)*val;
  189. if(a->modified) {
  190. ret = x509_name_encode((X509_NAME *)a);
  191. if(ret < 0) return ret;
  192. }
  193. ret = a->bytes->length;
  194. if(out != NULL) {
  195. memcpy(*out,a->bytes->data,ret);
  196. *out+=ret;
  197. }
  198. return ret;
  199. }
  200. static int x509_name_encode(X509_NAME *a)
  201. {
  202. STACK *intname = NULL, **intname_pp = &intname;
  203. int len;
  204. unsigned char *p;
  205. STACK_OF(X509_NAME_ENTRY) *entries = NULL;
  206. X509_NAME_ENTRY *entry;
  207. int i, set = -1;
  208. intname = sk_new_null();
  209. if(!intname) goto memerr;
  210. for(i = 0; i < sk_X509_NAME_ENTRY_num(a->entries); i++) {
  211. entry = sk_X509_NAME_ENTRY_value(a->entries, i);
  212. if(entry->set != set) {
  213. entries = sk_X509_NAME_ENTRY_new_null();
  214. if(!entries) goto memerr;
  215. if(!sk_push(intname, (char *)entries)) goto memerr;
  216. set = entry->set;
  217. }
  218. if(!sk_X509_NAME_ENTRY_push(entries, entry)) goto memerr;
  219. }
  220. len = ASN1_item_ex_i2d((ASN1_VALUE **)intname_pp, NULL,
  221. ASN1_ITEM_rptr(X509_NAME_INTERNAL), -1, -1);
  222. if (!BUF_MEM_grow(a->bytes,len)) goto memerr;
  223. p=(unsigned char *)a->bytes->data;
  224. ASN1_item_ex_i2d((ASN1_VALUE **)intname_pp,
  225. &p, ASN1_ITEM_rptr(X509_NAME_INTERNAL), -1, -1);
  226. sk_pop_free(intname, sk_internal_free);
  227. a->modified = 0;
  228. return len;
  229. memerr:
  230. sk_pop_free(intname, sk_internal_free);
  231. ASN1err(ASN1_F_X509_NAME_ENCODE, ERR_R_MALLOC_FAILURE);
  232. return -1;
  233. }
  234. static int x509_name_ex_print(BIO *out, ASN1_VALUE **pval,
  235. int indent,
  236. const char *fname,
  237. const ASN1_PCTX *pctx)
  238. {
  239. if (X509_NAME_print_ex(out, (X509_NAME *)*pval,
  240. indent, pctx->nm_flags) <= 0)
  241. return 0;
  242. return 2;
  243. }
  244. int X509_NAME_set(X509_NAME **xn, X509_NAME *name)
  245. {
  246. X509_NAME *in;
  247. if (!xn || !name) return(0);
  248. if (*xn != name)
  249. {
  250. in=X509_NAME_dup(name);
  251. if (in != NULL)
  252. {
  253. X509_NAME_free(*xn);
  254. *xn=in;
  255. }
  256. }
  257. return(*xn != NULL);
  258. }
  259. IMPLEMENT_STACK_OF(X509_NAME_ENTRY)
  260. IMPLEMENT_ASN1_SET_OF(X509_NAME_ENTRY)