2
0

tasn_new.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. /* tasn_new.c */
  2. /* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
  3. * project 2000.
  4. */
  5. /* ====================================================================
  6. * Copyright (c) 2000-2004 The OpenSSL Project. All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. *
  15. * 2. Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in
  17. * the documentation and/or other materials provided with the
  18. * distribution.
  19. *
  20. * 3. All advertising materials mentioning features or use of this
  21. * software must display the following acknowledgment:
  22. * "This product includes software developed by the OpenSSL Project
  23. * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
  24. *
  25. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  26. * endorse or promote products derived from this software without
  27. * prior written permission. For written permission, please contact
  28. * licensing@OpenSSL.org.
  29. *
  30. * 5. Products derived from this software may not be called "OpenSSL"
  31. * nor may "OpenSSL" appear in their names without prior written
  32. * permission of the OpenSSL Project.
  33. *
  34. * 6. Redistributions of any form whatsoever must retain the following
  35. * acknowledgment:
  36. * "This product includes software developed by the OpenSSL Project
  37. * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
  38. *
  39. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  40. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  41. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  42. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  43. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  44. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  45. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  46. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  47. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  48. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  49. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  50. * OF THE POSSIBILITY OF SUCH DAMAGE.
  51. * ====================================================================
  52. *
  53. * This product includes cryptographic software written by Eric Young
  54. * (eay@cryptsoft.com). This product includes software written by Tim
  55. * Hudson (tjh@cryptsoft.com).
  56. *
  57. */
  58. #include <stddef.h>
  59. #include <openssl/asn1.h>
  60. #include <openssl/objects.h>
  61. #include <openssl/err.h>
  62. #include <openssl/asn1t.h>
  63. #include <string.h>
  64. static int asn1_item_ex_combine_new(ASN1_VALUE **pval, const ASN1_ITEM *it,
  65. int combine);
  66. static void asn1_item_clear(ASN1_VALUE **pval, const ASN1_ITEM *it);
  67. static void asn1_template_clear(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt);
  68. static void asn1_primitive_clear(ASN1_VALUE **pval, const ASN1_ITEM *it);
  69. ASN1_VALUE *ASN1_item_new(const ASN1_ITEM *it)
  70. {
  71. ASN1_VALUE *ret = NULL;
  72. if (ASN1_item_ex_new(&ret, it) > 0)
  73. return ret;
  74. return NULL;
  75. }
  76. /* Allocate an ASN1 structure */
  77. int ASN1_item_ex_new(ASN1_VALUE **pval, const ASN1_ITEM *it)
  78. {
  79. return asn1_item_ex_combine_new(pval, it, 0);
  80. }
  81. static int asn1_item_ex_combine_new(ASN1_VALUE **pval, const ASN1_ITEM *it,
  82. int combine)
  83. {
  84. const ASN1_TEMPLATE *tt = NULL;
  85. const ASN1_COMPAT_FUNCS *cf;
  86. const ASN1_EXTERN_FUNCS *ef;
  87. const ASN1_AUX *aux = it->funcs;
  88. ASN1_aux_cb *asn1_cb;
  89. ASN1_VALUE **pseqval;
  90. int i;
  91. if (aux && aux->asn1_cb)
  92. asn1_cb = aux->asn1_cb;
  93. else
  94. asn1_cb = 0;
  95. if (!combine) *pval = NULL;
  96. #ifdef CRYPTO_MDEBUG
  97. if (it->sname)
  98. CRYPTO_push_info(it->sname);
  99. #endif
  100. switch(it->itype)
  101. {
  102. case ASN1_ITYPE_EXTERN:
  103. ef = it->funcs;
  104. if (ef && ef->asn1_ex_new)
  105. {
  106. if (!ef->asn1_ex_new(pval, it))
  107. goto memerr;
  108. }
  109. break;
  110. case ASN1_ITYPE_COMPAT:
  111. cf = it->funcs;
  112. if (cf && cf->asn1_new) {
  113. *pval = cf->asn1_new();
  114. if (!*pval)
  115. goto memerr;
  116. }
  117. break;
  118. case ASN1_ITYPE_PRIMITIVE:
  119. if (it->templates)
  120. {
  121. if (!ASN1_template_new(pval, it->templates))
  122. goto memerr;
  123. }
  124. else if (!ASN1_primitive_new(pval, it))
  125. goto memerr;
  126. break;
  127. case ASN1_ITYPE_MSTRING:
  128. if (!ASN1_primitive_new(pval, it))
  129. goto memerr;
  130. break;
  131. case ASN1_ITYPE_CHOICE:
  132. if (asn1_cb)
  133. {
  134. i = asn1_cb(ASN1_OP_NEW_PRE, pval, it, NULL);
  135. if (!i)
  136. goto auxerr;
  137. if (i==2)
  138. {
  139. #ifdef CRYPTO_MDEBUG
  140. if (it->sname)
  141. CRYPTO_pop_info();
  142. #endif
  143. return 1;
  144. }
  145. }
  146. if (!combine)
  147. {
  148. *pval = OPENSSL_malloc(it->size);
  149. if (!*pval)
  150. goto memerr;
  151. memset(*pval, 0, it->size);
  152. }
  153. asn1_set_choice_selector(pval, -1, it);
  154. if (asn1_cb && !asn1_cb(ASN1_OP_NEW_POST, pval, it, NULL))
  155. goto auxerr;
  156. break;
  157. case ASN1_ITYPE_NDEF_SEQUENCE:
  158. case ASN1_ITYPE_SEQUENCE:
  159. if (asn1_cb)
  160. {
  161. i = asn1_cb(ASN1_OP_NEW_PRE, pval, it, NULL);
  162. if (!i)
  163. goto auxerr;
  164. if (i==2)
  165. {
  166. #ifdef CRYPTO_MDEBUG
  167. if (it->sname)
  168. CRYPTO_pop_info();
  169. #endif
  170. return 1;
  171. }
  172. }
  173. if (!combine)
  174. {
  175. *pval = OPENSSL_malloc(it->size);
  176. if (!*pval)
  177. goto memerr;
  178. memset(*pval, 0, it->size);
  179. asn1_do_lock(pval, 0, it);
  180. asn1_enc_init(pval, it);
  181. }
  182. for (i = 0, tt = it->templates; i < it->tcount; tt++, i++)
  183. {
  184. pseqval = asn1_get_field_ptr(pval, tt);
  185. if (!ASN1_template_new(pseqval, tt))
  186. goto memerr;
  187. }
  188. if (asn1_cb && !asn1_cb(ASN1_OP_NEW_POST, pval, it, NULL))
  189. goto auxerr;
  190. break;
  191. }
  192. #ifdef CRYPTO_MDEBUG
  193. if (it->sname) CRYPTO_pop_info();
  194. #endif
  195. return 1;
  196. memerr:
  197. ASN1err(ASN1_F_ASN1_ITEM_EX_COMBINE_NEW, ERR_R_MALLOC_FAILURE);
  198. #ifdef CRYPTO_MDEBUG
  199. if (it->sname) CRYPTO_pop_info();
  200. #endif
  201. return 0;
  202. auxerr:
  203. ASN1err(ASN1_F_ASN1_ITEM_EX_COMBINE_NEW, ASN1_R_AUX_ERROR);
  204. ASN1_item_ex_free(pval, it);
  205. #ifdef CRYPTO_MDEBUG
  206. if (it->sname) CRYPTO_pop_info();
  207. #endif
  208. return 0;
  209. }
  210. static void asn1_item_clear(ASN1_VALUE **pval, const ASN1_ITEM *it)
  211. {
  212. const ASN1_EXTERN_FUNCS *ef;
  213. switch(it->itype)
  214. {
  215. case ASN1_ITYPE_EXTERN:
  216. ef = it->funcs;
  217. if (ef && ef->asn1_ex_clear)
  218. ef->asn1_ex_clear(pval, it);
  219. else *pval = NULL;
  220. break;
  221. case ASN1_ITYPE_PRIMITIVE:
  222. if (it->templates)
  223. asn1_template_clear(pval, it->templates);
  224. else
  225. asn1_primitive_clear(pval, it);
  226. break;
  227. case ASN1_ITYPE_MSTRING:
  228. asn1_primitive_clear(pval, it);
  229. break;
  230. case ASN1_ITYPE_COMPAT:
  231. case ASN1_ITYPE_CHOICE:
  232. case ASN1_ITYPE_SEQUENCE:
  233. case ASN1_ITYPE_NDEF_SEQUENCE:
  234. *pval = NULL;
  235. break;
  236. }
  237. }
  238. int ASN1_template_new(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt)
  239. {
  240. const ASN1_ITEM *it = ASN1_ITEM_ptr(tt->item);
  241. int ret;
  242. if (tt->flags & ASN1_TFLG_OPTIONAL)
  243. {
  244. asn1_template_clear(pval, tt);
  245. return 1;
  246. }
  247. /* If ANY DEFINED BY nothing to do */
  248. if (tt->flags & ASN1_TFLG_ADB_MASK)
  249. {
  250. *pval = NULL;
  251. return 1;
  252. }
  253. #ifdef CRYPTO_MDEBUG
  254. if (tt->field_name)
  255. CRYPTO_push_info(tt->field_name);
  256. #endif
  257. /* If SET OF or SEQUENCE OF, its a STACK */
  258. if (tt->flags & ASN1_TFLG_SK_MASK)
  259. {
  260. STACK_OF(ASN1_VALUE) *skval;
  261. skval = sk_ASN1_VALUE_new_null();
  262. if (!skval)
  263. {
  264. ASN1err(ASN1_F_ASN1_TEMPLATE_NEW, ERR_R_MALLOC_FAILURE);
  265. ret = 0;
  266. goto done;
  267. }
  268. *pval = (ASN1_VALUE *)skval;
  269. ret = 1;
  270. goto done;
  271. }
  272. /* Otherwise pass it back to the item routine */
  273. ret = asn1_item_ex_combine_new(pval, it, tt->flags & ASN1_TFLG_COMBINE);
  274. done:
  275. #ifdef CRYPTO_MDEBUG
  276. if (it->sname)
  277. CRYPTO_pop_info();
  278. #endif
  279. return ret;
  280. }
  281. static void asn1_template_clear(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt)
  282. {
  283. /* If ADB or STACK just NULL the field */
  284. if (tt->flags & (ASN1_TFLG_ADB_MASK|ASN1_TFLG_SK_MASK))
  285. *pval = NULL;
  286. else
  287. asn1_item_clear(pval, ASN1_ITEM_ptr(tt->item));
  288. }
  289. /* NB: could probably combine most of the real XXX_new() behaviour and junk
  290. * all the old functions.
  291. */
  292. int ASN1_primitive_new(ASN1_VALUE **pval, const ASN1_ITEM *it)
  293. {
  294. ASN1_TYPE *typ;
  295. ASN1_STRING *str;
  296. int utype;
  297. if (it && it->funcs)
  298. {
  299. const ASN1_PRIMITIVE_FUNCS *pf = it->funcs;
  300. if (pf->prim_new)
  301. return pf->prim_new(pval, it);
  302. }
  303. if (!it || (it->itype == ASN1_ITYPE_MSTRING))
  304. utype = -1;
  305. else
  306. utype = it->utype;
  307. switch(utype)
  308. {
  309. case V_ASN1_OBJECT:
  310. *pval = (ASN1_VALUE *)OBJ_nid2obj(NID_undef);
  311. return 1;
  312. case V_ASN1_BOOLEAN:
  313. *(ASN1_BOOLEAN *)pval = it->size;
  314. return 1;
  315. case V_ASN1_NULL:
  316. *pval = (ASN1_VALUE *)1;
  317. return 1;
  318. case V_ASN1_ANY:
  319. typ = OPENSSL_malloc(sizeof(ASN1_TYPE));
  320. if (!typ)
  321. return 0;
  322. typ->value.ptr = NULL;
  323. typ->type = -1;
  324. *pval = (ASN1_VALUE *)typ;
  325. break;
  326. default:
  327. str = ASN1_STRING_type_new(utype);
  328. if (it->itype == ASN1_ITYPE_MSTRING && str)
  329. str->flags |= ASN1_STRING_FLAG_MSTRING;
  330. *pval = (ASN1_VALUE *)str;
  331. break;
  332. }
  333. if (*pval)
  334. return 1;
  335. return 0;
  336. }
  337. static void asn1_primitive_clear(ASN1_VALUE **pval, const ASN1_ITEM *it)
  338. {
  339. int utype;
  340. if (it && it->funcs)
  341. {
  342. const ASN1_PRIMITIVE_FUNCS *pf = it->funcs;
  343. if (pf->prim_clear)
  344. pf->prim_clear(pval, it);
  345. else
  346. *pval = NULL;
  347. return;
  348. }
  349. if (!it || (it->itype == ASN1_ITYPE_MSTRING))
  350. utype = -1;
  351. else
  352. utype = it->utype;
  353. if (utype == V_ASN1_BOOLEAN)
  354. *(ASN1_BOOLEAN *)pval = it->size;
  355. else *pval = NULL;
  356. }