tasn_new.c 11 KB

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