tasn_new.c 10.0 KB

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