tasn_new.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  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 auxerr2;
  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 memerr2;
  176. }
  177. if (asn1_cb && !asn1_cb(ASN1_OP_NEW_POST, pval, it, NULL))
  178. goto auxerr2;
  179. break;
  180. }
  181. #ifdef CRYPTO_MDEBUG
  182. if (it->sname)
  183. CRYPTO_pop_info();
  184. #endif
  185. return 1;
  186. memerr2:
  187. ASN1_item_ex_free(pval, it);
  188. memerr:
  189. ASN1err(ASN1_F_ASN1_ITEM_EX_COMBINE_NEW, ERR_R_MALLOC_FAILURE);
  190. #ifdef CRYPTO_MDEBUG
  191. if (it->sname)
  192. CRYPTO_pop_info();
  193. #endif
  194. return 0;
  195. auxerr2:
  196. ASN1_item_ex_free(pval, it);
  197. auxerr:
  198. ASN1err(ASN1_F_ASN1_ITEM_EX_COMBINE_NEW, ASN1_R_AUX_ERROR);
  199. #ifdef CRYPTO_MDEBUG
  200. if (it->sname)
  201. CRYPTO_pop_info();
  202. #endif
  203. return 0;
  204. }
  205. static void asn1_item_clear(ASN1_VALUE **pval, const ASN1_ITEM *it)
  206. {
  207. const ASN1_EXTERN_FUNCS *ef;
  208. switch (it->itype) {
  209. case ASN1_ITYPE_EXTERN:
  210. ef = it->funcs;
  211. if (ef && ef->asn1_ex_clear)
  212. ef->asn1_ex_clear(pval, it);
  213. else
  214. *pval = NULL;
  215. break;
  216. case ASN1_ITYPE_PRIMITIVE:
  217. if (it->templates)
  218. asn1_template_clear(pval, it->templates);
  219. else
  220. asn1_primitive_clear(pval, it);
  221. break;
  222. case ASN1_ITYPE_MSTRING:
  223. asn1_primitive_clear(pval, it);
  224. break;
  225. case ASN1_ITYPE_COMPAT:
  226. case ASN1_ITYPE_CHOICE:
  227. case ASN1_ITYPE_SEQUENCE:
  228. case ASN1_ITYPE_NDEF_SEQUENCE:
  229. *pval = NULL;
  230. break;
  231. }
  232. }
  233. int ASN1_template_new(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt)
  234. {
  235. const ASN1_ITEM *it = ASN1_ITEM_ptr(tt->item);
  236. int ret;
  237. if (tt->flags & ASN1_TFLG_OPTIONAL) {
  238. asn1_template_clear(pval, tt);
  239. return 1;
  240. }
  241. /* If ANY DEFINED BY nothing to do */
  242. if (tt->flags & ASN1_TFLG_ADB_MASK) {
  243. *pval = NULL;
  244. return 1;
  245. }
  246. #ifdef CRYPTO_MDEBUG
  247. if (tt->field_name)
  248. CRYPTO_push_info(tt->field_name);
  249. #endif
  250. /* If SET OF or SEQUENCE OF, its a STACK */
  251. if (tt->flags & ASN1_TFLG_SK_MASK) {
  252. STACK_OF(ASN1_VALUE) *skval;
  253. skval = sk_ASN1_VALUE_new_null();
  254. if (!skval) {
  255. ASN1err(ASN1_F_ASN1_TEMPLATE_NEW, ERR_R_MALLOC_FAILURE);
  256. ret = 0;
  257. goto done;
  258. }
  259. *pval = (ASN1_VALUE *)skval;
  260. ret = 1;
  261. goto done;
  262. }
  263. /* Otherwise pass it back to the item routine */
  264. ret = asn1_item_ex_combine_new(pval, it, tt->flags & ASN1_TFLG_COMBINE);
  265. done:
  266. #ifdef CRYPTO_MDEBUG
  267. if (it->sname)
  268. CRYPTO_pop_info();
  269. #endif
  270. return ret;
  271. }
  272. static void asn1_template_clear(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt)
  273. {
  274. /* If ADB or STACK just NULL the field */
  275. if (tt->flags & (ASN1_TFLG_ADB_MASK | ASN1_TFLG_SK_MASK))
  276. *pval = NULL;
  277. else
  278. asn1_item_clear(pval, ASN1_ITEM_ptr(tt->item));
  279. }
  280. /*
  281. * NB: could probably combine most of the real XXX_new() behaviour and junk
  282. * all the old functions.
  283. */
  284. int ASN1_primitive_new(ASN1_VALUE **pval, const ASN1_ITEM *it)
  285. {
  286. ASN1_TYPE *typ;
  287. ASN1_STRING *str;
  288. int utype;
  289. if (!it)
  290. return 0;
  291. if (it->funcs) {
  292. const ASN1_PRIMITIVE_FUNCS *pf = it->funcs;
  293. if (pf->prim_new)
  294. return pf->prim_new(pval, it);
  295. }
  296. if (it->itype == ASN1_ITYPE_MSTRING)
  297. utype = -1;
  298. else
  299. utype = it->utype;
  300. switch (utype) {
  301. case V_ASN1_OBJECT:
  302. *pval = (ASN1_VALUE *)OBJ_nid2obj(NID_undef);
  303. return 1;
  304. case V_ASN1_BOOLEAN:
  305. *(ASN1_BOOLEAN *)pval = it->size;
  306. return 1;
  307. case V_ASN1_NULL:
  308. *pval = (ASN1_VALUE *)1;
  309. return 1;
  310. case V_ASN1_ANY:
  311. typ = OPENSSL_malloc(sizeof(ASN1_TYPE));
  312. if (!typ)
  313. return 0;
  314. typ->value.ptr = NULL;
  315. typ->type = -1;
  316. *pval = (ASN1_VALUE *)typ;
  317. break;
  318. default:
  319. str = ASN1_STRING_type_new(utype);
  320. if (it->itype == ASN1_ITYPE_MSTRING && str)
  321. str->flags |= ASN1_STRING_FLAG_MSTRING;
  322. *pval = (ASN1_VALUE *)str;
  323. break;
  324. }
  325. if (*pval)
  326. return 1;
  327. return 0;
  328. }
  329. static void asn1_primitive_clear(ASN1_VALUE **pval, const ASN1_ITEM *it)
  330. {
  331. int utype;
  332. if (it && it->funcs) {
  333. const ASN1_PRIMITIVE_FUNCS *pf = it->funcs;
  334. if (pf->prim_clear)
  335. pf->prim_clear(pval, it);
  336. else
  337. *pval = NULL;
  338. return;
  339. }
  340. if (!it || (it->itype == ASN1_ITYPE_MSTRING))
  341. utype = -1;
  342. else
  343. utype = it->utype;
  344. if (utype == V_ASN1_BOOLEAN)
  345. *(ASN1_BOOLEAN *)pval = it->size;
  346. else
  347. *pval = NULL;
  348. }