tasn_new.c 11 KB

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