You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

397 lines
9.7 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 <openssl/asn1.h>
  57. #include <string.h>
  58. #include <openssl/asn1t.h>
  59. #include <openssl/err.h>
  60. #include <openssl/mem.h>
  61. #include <openssl/obj.h>
  62. static int asn1_item_ex_combine_new(ASN1_VALUE **pval, const ASN1_ITEM *it,
  63. int combine);
  64. static void asn1_item_clear(ASN1_VALUE **pval, const ASN1_ITEM *it);
  65. static void asn1_template_clear(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt);
  66. static void asn1_primitive_clear(ASN1_VALUE **pval, const ASN1_ITEM *it);
  67. ASN1_VALUE *ASN1_item_new(const ASN1_ITEM *it)
  68. {
  69. ASN1_VALUE *ret = NULL;
  70. if (ASN1_item_ex_new(&ret, it) > 0)
  71. return ret;
  72. return NULL;
  73. }
  74. /* Allocate an ASN1 structure */
  75. int ASN1_item_ex_new(ASN1_VALUE **pval, const ASN1_ITEM *it)
  76. {
  77. return asn1_item_ex_combine_new(pval, it, 0);
  78. }
  79. static int asn1_item_ex_combine_new(ASN1_VALUE **pval, const ASN1_ITEM *it,
  80. int combine)
  81. {
  82. const ASN1_TEMPLATE *tt = NULL;
  83. const ASN1_COMPAT_FUNCS *cf;
  84. const ASN1_EXTERN_FUNCS *ef;
  85. const ASN1_AUX *aux = it->funcs;
  86. ASN1_aux_cb *asn1_cb;
  87. ASN1_VALUE **pseqval;
  88. int i;
  89. if (aux && aux->asn1_cb)
  90. asn1_cb = aux->asn1_cb;
  91. else
  92. asn1_cb = 0;
  93. if (!combine) *pval = NULL;
  94. #ifdef CRYPTO_MDEBUG
  95. if (it->sname)
  96. CRYPTO_push_info(it->sname);
  97. #endif
  98. switch(it->itype)
  99. {
  100. case ASN1_ITYPE_EXTERN:
  101. ef = it->funcs;
  102. if (ef && ef->asn1_ex_new)
  103. {
  104. if (!ef->asn1_ex_new(pval, it))
  105. goto memerr;
  106. }
  107. break;
  108. case ASN1_ITYPE_COMPAT:
  109. cf = it->funcs;
  110. if (cf && cf->asn1_new) {
  111. *pval = cf->asn1_new();
  112. if (!*pval)
  113. goto memerr;
  114. }
  115. break;
  116. case ASN1_ITYPE_PRIMITIVE:
  117. if (it->templates)
  118. {
  119. if (!ASN1_template_new(pval, it->templates))
  120. goto memerr;
  121. }
  122. else if (!ASN1_primitive_new(pval, it))
  123. goto memerr;
  124. break;
  125. case ASN1_ITYPE_MSTRING:
  126. if (!ASN1_primitive_new(pval, it))
  127. goto memerr;
  128. break;
  129. case ASN1_ITYPE_CHOICE:
  130. if (asn1_cb)
  131. {
  132. i = asn1_cb(ASN1_OP_NEW_PRE, pval, it, NULL);
  133. if (!i)
  134. goto auxerr;
  135. if (i==2)
  136. {
  137. #ifdef CRYPTO_MDEBUG
  138. if (it->sname)
  139. CRYPTO_pop_info();
  140. #endif
  141. return 1;
  142. }
  143. }
  144. if (!combine)
  145. {
  146. *pval = OPENSSL_malloc(it->size);
  147. if (!*pval)
  148. goto memerr;
  149. memset(*pval, 0, it->size);
  150. }
  151. asn1_set_choice_selector(pval, -1, it);
  152. if (asn1_cb && !asn1_cb(ASN1_OP_NEW_POST, pval, it, NULL))
  153. goto auxerr;
  154. break;
  155. case ASN1_ITYPE_NDEF_SEQUENCE:
  156. case ASN1_ITYPE_SEQUENCE:
  157. if (asn1_cb)
  158. {
  159. i = asn1_cb(ASN1_OP_NEW_PRE, pval, it, NULL);
  160. if (!i)
  161. goto auxerr;
  162. if (i==2)
  163. {
  164. #ifdef CRYPTO_MDEBUG
  165. if (it->sname)
  166. CRYPTO_pop_info();
  167. #endif
  168. return 1;
  169. }
  170. }
  171. if (!combine)
  172. {
  173. *pval = OPENSSL_malloc(it->size);
  174. if (!*pval)
  175. goto memerr;
  176. memset(*pval, 0, it->size);
  177. asn1_do_lock(pval, 0, it);
  178. asn1_enc_init(pval, it);
  179. }
  180. for (i = 0, tt = it->templates; i < it->tcount; tt++, i++)
  181. {
  182. pseqval = asn1_get_field_ptr(pval, tt);
  183. if (!ASN1_template_new(pseqval, tt))
  184. goto memerr;
  185. }
  186. if (asn1_cb && !asn1_cb(ASN1_OP_NEW_POST, pval, it, NULL))
  187. goto auxerr;
  188. break;
  189. }
  190. #ifdef CRYPTO_MDEBUG
  191. if (it->sname) CRYPTO_pop_info();
  192. #endif
  193. return 1;
  194. memerr:
  195. OPENSSL_PUT_ERROR(ASN1, asn1_item_ex_combine_new, ERR_R_MALLOC_FAILURE);
  196. #ifdef CRYPTO_MDEBUG
  197. if (it->sname) CRYPTO_pop_info();
  198. #endif
  199. return 0;
  200. auxerr:
  201. OPENSSL_PUT_ERROR(ASN1, asn1_item_ex_combine_new, ASN1_R_AUX_ERROR);
  202. ASN1_item_ex_free(pval, it);
  203. #ifdef CRYPTO_MDEBUG
  204. if (it->sname) CRYPTO_pop_info();
  205. #endif
  206. return 0;
  207. }
  208. static void asn1_item_clear(ASN1_VALUE **pval, const ASN1_ITEM *it)
  209. {
  210. const ASN1_EXTERN_FUNCS *ef;
  211. switch(it->itype)
  212. {
  213. case ASN1_ITYPE_EXTERN:
  214. ef = it->funcs;
  215. if (ef && ef->asn1_ex_clear)
  216. ef->asn1_ex_clear(pval, it);
  217. else *pval = NULL;
  218. break;
  219. case ASN1_ITYPE_PRIMITIVE:
  220. if (it->templates)
  221. asn1_template_clear(pval, it->templates);
  222. else
  223. asn1_primitive_clear(pval, it);
  224. break;
  225. case ASN1_ITYPE_MSTRING:
  226. asn1_primitive_clear(pval, it);
  227. break;
  228. case ASN1_ITYPE_COMPAT:
  229. case ASN1_ITYPE_CHOICE:
  230. case ASN1_ITYPE_SEQUENCE:
  231. case ASN1_ITYPE_NDEF_SEQUENCE:
  232. *pval = NULL;
  233. break;
  234. }
  235. }
  236. int ASN1_template_new(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt)
  237. {
  238. const ASN1_ITEM *it = ASN1_ITEM_ptr(tt->item);
  239. int ret;
  240. if (tt->flags & ASN1_TFLG_OPTIONAL)
  241. {
  242. asn1_template_clear(pval, tt);
  243. return 1;
  244. }
  245. /* If ANY DEFINED BY nothing to do */
  246. if (tt->flags & ASN1_TFLG_ADB_MASK)
  247. {
  248. *pval = NULL;
  249. return 1;
  250. }
  251. #ifdef CRYPTO_MDEBUG
  252. if (tt->field_name)
  253. CRYPTO_push_info(tt->field_name);
  254. #endif
  255. /* If SET OF or SEQUENCE OF, its a STACK */
  256. if (tt->flags & ASN1_TFLG_SK_MASK)
  257. {
  258. STACK_OF(ASN1_VALUE) *skval;
  259. skval = sk_ASN1_VALUE_new_null();
  260. if (!skval)
  261. {
  262. OPENSSL_PUT_ERROR(ASN1, ASN1_template_new, ERR_R_MALLOC_FAILURE);
  263. ret = 0;
  264. goto done;
  265. }
  266. *pval = (ASN1_VALUE *)skval;
  267. ret = 1;
  268. goto done;
  269. }
  270. /* Otherwise pass it back to the item routine */
  271. ret = asn1_item_ex_combine_new(pval, it, tt->flags & ASN1_TFLG_COMBINE);
  272. done:
  273. #ifdef CRYPTO_MDEBUG
  274. if (it->sname)
  275. CRYPTO_pop_info();
  276. #endif
  277. return ret;
  278. }
  279. static void asn1_template_clear(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt)
  280. {
  281. /* If ADB or STACK just NULL the field */
  282. if (tt->flags & (ASN1_TFLG_ADB_MASK|ASN1_TFLG_SK_MASK))
  283. *pval = NULL;
  284. else
  285. asn1_item_clear(pval, ASN1_ITEM_ptr(tt->item));
  286. }
  287. /* NB: could probably combine most of the real XXX_new() behaviour and junk
  288. * all the old functions.
  289. */
  290. int ASN1_primitive_new(ASN1_VALUE **pval, const ASN1_ITEM *it)
  291. {
  292. ASN1_TYPE *typ;
  293. ASN1_STRING *str;
  294. int utype;
  295. if (it && it->funcs)
  296. {
  297. const ASN1_PRIMITIVE_FUNCS *pf = it->funcs;
  298. if (pf->prim_new)
  299. return pf->prim_new(pval, it);
  300. }
  301. if (!it || (it->itype == ASN1_ITYPE_MSTRING))
  302. utype = -1;
  303. else
  304. utype = it->utype;
  305. switch(utype)
  306. {
  307. case V_ASN1_OBJECT:
  308. *pval = (ASN1_VALUE *)OBJ_nid2obj(NID_undef);
  309. return 1;
  310. case V_ASN1_BOOLEAN:
  311. *(ASN1_BOOLEAN *)pval = it->size;
  312. return 1;
  313. case V_ASN1_NULL:
  314. *pval = (ASN1_VALUE *)1;
  315. return 1;
  316. case V_ASN1_ANY:
  317. typ = OPENSSL_malloc(sizeof(ASN1_TYPE));
  318. if (!typ)
  319. return 0;
  320. typ->value.ptr = NULL;
  321. typ->type = -1;
  322. *pval = (ASN1_VALUE *)typ;
  323. break;
  324. default:
  325. str = ASN1_STRING_type_new(utype);
  326. if (it->itype == ASN1_ITYPE_MSTRING && str)
  327. str->flags |= ASN1_STRING_FLAG_MSTRING;
  328. *pval = (ASN1_VALUE *)str;
  329. break;
  330. }
  331. if (*pval)
  332. return 1;
  333. return 0;
  334. }
  335. static void asn1_primitive_clear(ASN1_VALUE **pval, const ASN1_ITEM *it)
  336. {
  337. int utype;
  338. if (it && it->funcs)
  339. {
  340. const ASN1_PRIMITIVE_FUNCS *pf = it->funcs;
  341. if (pf->prim_clear)
  342. pf->prim_clear(pval, it);
  343. else
  344. *pval = NULL;
  345. return;
  346. }
  347. if (!it || (it->itype == ASN1_ITYPE_MSTRING))
  348. utype = -1;
  349. else
  350. utype = it->utype;
  351. if (utype == V_ASN1_BOOLEAN)
  352. *(ASN1_BOOLEAN *)pval = it->size;
  353. else *pval = NULL;
  354. }