tasn_new.c 11 KB

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