tasn_new.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. /* tasn_new.c */
  2. /* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL
  3. * project 2000.
  4. */
  5. /* ====================================================================
  6. * Copyright (c) 2000 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. static int asn1_item_ex_combine_new(ASN1_VALUE **pval, const ASN1_ITEM *it, int combine);
  65. static void asn1_item_clear(ASN1_VALUE **pval, const ASN1_ITEM *it);
  66. static void asn1_template_clear(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt);
  67. void asn1_primitive_clear(ASN1_VALUE **pval, const ASN1_ITEM *it);
  68. ASN1_VALUE *ASN1_item_new(const ASN1_ITEM *it)
  69. {
  70. ASN1_VALUE *ret = NULL;
  71. if(ASN1_item_ex_new(&ret, it) > 0) return ret;
  72. return NULL;
  73. }
  74. /* Allocate an ASN1 structure */
  75. int ASN1_item_ex_new(ASN1_VALUE **pval, const ASN1_ITEM *it)
  76. {
  77. return asn1_item_ex_combine_new(pval, it, 0);
  78. }
  79. static int asn1_item_ex_combine_new(ASN1_VALUE **pval, const ASN1_ITEM *it, int combine)
  80. {
  81. const ASN1_TEMPLATE *tt = NULL;
  82. const ASN1_COMPAT_FUNCS *cf;
  83. const ASN1_EXTERN_FUNCS *ef;
  84. const ASN1_AUX *aux = it->funcs;
  85. ASN1_aux_cb *asn1_cb;
  86. ASN1_VALUE **pseqval;
  87. int i;
  88. if(aux && aux->asn1_cb) asn1_cb = aux->asn1_cb;
  89. else asn1_cb = 0;
  90. if(!combine) *pval = NULL;
  91. #ifdef CRYPTO_MDEBUG
  92. if(it->sname) CRYPTO_push_info(it->sname);
  93. #endif
  94. switch(it->itype) {
  95. case ASN1_ITYPE_EXTERN:
  96. ef = it->funcs;
  97. if(ef && ef->asn1_ex_new) {
  98. if(!ef->asn1_ex_new(pval, it))
  99. goto memerr;
  100. }
  101. break;
  102. case ASN1_ITYPE_COMPAT:
  103. cf = it->funcs;
  104. if(cf && cf->asn1_new) {
  105. *pval = cf->asn1_new();
  106. if(!*pval) goto memerr;
  107. }
  108. break;
  109. case ASN1_ITYPE_PRIMITIVE:
  110. if(it->templates) {
  111. if(!ASN1_template_new(pval, it->templates))
  112. goto memerr;
  113. } else {
  114. if(!ASN1_primitive_new(pval, it))
  115. goto memerr;
  116. }
  117. break;
  118. case ASN1_ITYPE_MSTRING:
  119. if(!ASN1_primitive_new(pval, it))
  120. goto memerr;
  121. break;
  122. case ASN1_ITYPE_CHOICE:
  123. if(asn1_cb) {
  124. i = asn1_cb(ASN1_OP_NEW_PRE, pval, it);
  125. if(!i) goto auxerr;
  126. if(i==2) {
  127. #ifdef CRYPTO_MDEBUG
  128. if(it->sname) CRYPTO_pop_info();
  129. #endif
  130. return 1;
  131. }
  132. }
  133. if(!combine) {
  134. *pval = OPENSSL_malloc(it->size);
  135. if(!*pval) goto memerr;
  136. memset(*pval, 0, it->size);
  137. }
  138. asn1_set_choice_selector(pval, -1, it);
  139. if(asn1_cb && !asn1_cb(ASN1_OP_NEW_POST, pval, it))
  140. goto auxerr;
  141. break;
  142. case ASN1_ITYPE_SEQUENCE:
  143. if(asn1_cb) {
  144. i = asn1_cb(ASN1_OP_NEW_PRE, pval, it);
  145. if(!i) goto auxerr;
  146. if(i==2) {
  147. #ifdef CRYPTO_MDEBUG
  148. if(it->sname) CRYPTO_pop_info();
  149. #endif
  150. return 1;
  151. }
  152. }
  153. if(!combine) {
  154. *pval = OPENSSL_malloc(it->size);
  155. if(!*pval) goto memerr;
  156. memset(*pval, 0, it->size);
  157. asn1_do_lock(pval, 0, it);
  158. asn1_enc_init(pval, it);
  159. }
  160. for(i = 0, tt = it->templates; i < it->tcount; tt++, i++) {
  161. pseqval = asn1_get_field_ptr(pval, tt);
  162. if(!ASN1_template_new(pseqval, tt)) goto memerr;
  163. }
  164. if(asn1_cb && !asn1_cb(ASN1_OP_NEW_POST, pval, it))
  165. goto auxerr;
  166. break;
  167. }
  168. #ifdef CRYPTO_MDEBUG
  169. if(it->sname) CRYPTO_pop_info();
  170. #endif
  171. return 1;
  172. memerr:
  173. ASN1err(ASN1_F_ASN1_ITEM_NEW, ERR_R_MALLOC_FAILURE);
  174. #ifdef CRYPTO_MDEBUG
  175. if(it->sname) CRYPTO_pop_info();
  176. #endif
  177. return 0;
  178. auxerr:
  179. ASN1err(ASN1_F_ASN1_ITEM_NEW, ASN1_R_AUX_ERROR);
  180. ASN1_item_ex_free(pval, it);
  181. #ifdef CRYPTO_MDEBUG
  182. if(it->sname) CRYPTO_pop_info();
  183. #endif
  184. return 0;
  185. }
  186. static void asn1_item_clear(ASN1_VALUE **pval, const ASN1_ITEM *it)
  187. {
  188. const ASN1_EXTERN_FUNCS *ef;
  189. switch(it->itype) {
  190. case ASN1_ITYPE_EXTERN:
  191. ef = it->funcs;
  192. if(ef && ef->asn1_ex_clear)
  193. ef->asn1_ex_clear(pval, it);
  194. else *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_COMPAT:
  206. case ASN1_ITYPE_CHOICE:
  207. case ASN1_ITYPE_SEQUENCE:
  208. *pval = NULL;
  209. break;
  210. }
  211. }
  212. 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) CRYPTO_push_info(tt->field_name);
  227. #endif
  228. /* If SET OF or SEQUENCE OF, its a STACK */
  229. if(tt->flags & ASN1_TFLG_SK_MASK) {
  230. STACK_OF(ASN1_VALUE) *skval;
  231. skval = sk_ASN1_VALUE_new_null();
  232. if(!skval) {
  233. ASN1err(ASN1_F_ASN1_TEMPLATE_NEW, ERR_R_MALLOC_FAILURE);
  234. ret = 0;
  235. goto done;
  236. }
  237. *pval = (ASN1_VALUE *)skval;
  238. ret = 1;
  239. goto done;
  240. }
  241. /* Otherwise pass it back to the item routine */
  242. ret = asn1_item_ex_combine_new(pval, it, tt->flags & ASN1_TFLG_COMBINE);
  243. done:
  244. #ifdef CRYPTO_MDEBUG
  245. if(it->sname) CRYPTO_pop_info();
  246. #endif
  247. return ret;
  248. }
  249. void asn1_template_clear(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt)
  250. {
  251. /* If ADB or STACK just NULL the field */
  252. if(tt->flags & (ASN1_TFLG_ADB_MASK|ASN1_TFLG_SK_MASK))
  253. *pval = NULL;
  254. else
  255. asn1_item_clear(pval, ASN1_ITEM_ptr(tt->item));
  256. }
  257. /* NB: could probably combine most of the real XXX_new() behaviour and junk all the old
  258. * functions.
  259. */
  260. int ASN1_primitive_new(ASN1_VALUE **pval, const ASN1_ITEM *it)
  261. {
  262. ASN1_TYPE *typ;
  263. int utype;
  264. const ASN1_PRIMITIVE_FUNCS *pf;
  265. pf = it->funcs;
  266. if(pf && pf->prim_new) return pf->prim_new(pval, it);
  267. if(!it || (it->itype == ASN1_ITYPE_MSTRING)) utype = -1;
  268. else utype = it->utype;
  269. switch(utype) {
  270. case V_ASN1_OBJECT:
  271. *pval = (ASN1_VALUE *)OBJ_nid2obj(NID_undef);
  272. return 1;
  273. case V_ASN1_BOOLEAN:
  274. *(ASN1_BOOLEAN *)pval = it->size;
  275. return 1;
  276. case V_ASN1_NULL:
  277. *pval = (ASN1_VALUE *)1;
  278. return 1;
  279. case V_ASN1_ANY:
  280. typ = OPENSSL_malloc(sizeof(ASN1_TYPE));
  281. if(!typ) return 0;
  282. typ->value.ptr = NULL;
  283. typ->type = -1;
  284. *pval = (ASN1_VALUE *)typ;
  285. break;
  286. default:
  287. *pval = (ASN1_VALUE *)ASN1_STRING_type_new(utype);
  288. break;
  289. }
  290. if(*pval) return 1;
  291. return 0;
  292. }
  293. void asn1_primitive_clear(ASN1_VALUE **pval, const ASN1_ITEM *it)
  294. {
  295. int utype;
  296. const ASN1_PRIMITIVE_FUNCS *pf;
  297. pf = it->funcs;
  298. if(pf) {
  299. if(pf->prim_clear)
  300. pf->prim_clear(pval, it);
  301. else
  302. *pval = NULL;
  303. return;
  304. }
  305. if(!it || (it->itype == ASN1_ITYPE_MSTRING)) utype = -1;
  306. else utype = it->utype;
  307. if(utype == V_ASN1_BOOLEAN)
  308. *(ASN1_BOOLEAN *)pval = it->size;
  309. else *pval = NULL;
  310. }