You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

tasn_new.c 9.7 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  2. * All rights reserved.
  3. *
  4. * This package is an SSL implementation written
  5. * by Eric Young (eay@cryptsoft.com).
  6. * The implementation was written so as to conform with Netscapes SSL.
  7. *
  8. * This library is free for commercial and non-commercial use as long as
  9. * the following conditions are aheared to. The following conditions
  10. * apply to all code found in this distribution, be it the RC4, RSA,
  11. * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  12. * included with this distribution is covered by the same copyright terms
  13. * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  14. *
  15. * Copyright remains Eric Young's, and as such any Copyright notices in
  16. * the code are not to be removed.
  17. * If this package is used in a product, Eric Young should be given attribution
  18. * as the author of the parts of the library used.
  19. * This can be in the form of a textual message at program startup or
  20. * in documentation (online or textual) provided with the package.
  21. *
  22. * Redistribution and use in source and binary forms, with or without
  23. * modification, are permitted provided that the following conditions
  24. * are met:
  25. * 1. Redistributions of source code must retain the copyright
  26. * notice, this list of conditions and the following disclaimer.
  27. * 2. Redistributions in binary form must reproduce the above copyright
  28. * notice, this list of conditions and the following disclaimer in the
  29. * documentation and/or other materials provided with the distribution.
  30. * 3. All advertising materials mentioning features or use of this software
  31. * must display the following acknowledgement:
  32. * "This product includes cryptographic software written by
  33. * Eric Young (eay@cryptsoft.com)"
  34. * The word 'cryptographic' can be left out if the rouines from the library
  35. * being used are not cryptographic related :-).
  36. * 4. If you include any Windows specific code (or a derivative thereof) from
  37. * the apps directory (application code) you must include an acknowledgement:
  38. * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  39. *
  40. * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  41. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  42. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  43. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  44. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  45. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  46. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  47. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  48. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  49. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  50. * SUCH DAMAGE.
  51. *
  52. * The licence and distribution terms for any publically available version or
  53. * derivative of this code cannot be changed. i.e. this code cannot simply be
  54. * copied and put under another distribution licence
  55. * [including the GNU Public Licence.] */
  56. #include <openssl/asn1.h>
  57. #include <string.h>
  58. #include <openssl/asn1t.h>
  59. #include <openssl/err.h>
  60. #include <openssl/mem.h>
  61. #include <openssl/obj.h>
  62. static int asn1_item_ex_combine_new(ASN1_VALUE **pval, const ASN1_ITEM *it,
  63. int combine);
  64. static void asn1_item_clear(ASN1_VALUE **pval, const ASN1_ITEM *it);
  65. static void asn1_template_clear(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt);
  66. static void asn1_primitive_clear(ASN1_VALUE **pval, const ASN1_ITEM *it);
  67. ASN1_VALUE *ASN1_item_new(const ASN1_ITEM *it)
  68. {
  69. ASN1_VALUE *ret = NULL;
  70. if (ASN1_item_ex_new(&ret, it) > 0)
  71. 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,
  80. int combine)
  81. {
  82. const ASN1_TEMPLATE *tt = NULL;
  83. const ASN1_COMPAT_FUNCS *cf;
  84. const ASN1_EXTERN_FUNCS *ef;
  85. const ASN1_AUX *aux = it->funcs;
  86. ASN1_aux_cb *asn1_cb;
  87. ASN1_VALUE **pseqval;
  88. int i;
  89. if (aux && aux->asn1_cb)
  90. asn1_cb = aux->asn1_cb;
  91. else
  92. asn1_cb = 0;
  93. #ifdef CRYPTO_MDEBUG
  94. if (it->sname)
  95. CRYPTO_push_info(it->sname);
  96. #endif
  97. switch(it->itype)
  98. {
  99. case ASN1_ITYPE_EXTERN:
  100. ef = it->funcs;
  101. if (ef && ef->asn1_ex_new)
  102. {
  103. if (!ef->asn1_ex_new(pval, it))
  104. goto memerr;
  105. }
  106. break;
  107. case ASN1_ITYPE_COMPAT:
  108. cf = it->funcs;
  109. if (cf && cf->asn1_new) {
  110. *pval = cf->asn1_new();
  111. if (!*pval)
  112. goto memerr;
  113. }
  114. break;
  115. case ASN1_ITYPE_PRIMITIVE:
  116. if (it->templates)
  117. {
  118. if (!ASN1_template_new(pval, it->templates))
  119. goto memerr;
  120. }
  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. {
  131. i = asn1_cb(ASN1_OP_NEW_PRE, pval, it, NULL);
  132. if (!i)
  133. goto auxerr;
  134. if (i==2)
  135. {
  136. #ifdef CRYPTO_MDEBUG
  137. if (it->sname)
  138. CRYPTO_pop_info();
  139. #endif
  140. return 1;
  141. }
  142. }
  143. if (!combine)
  144. {
  145. *pval = OPENSSL_malloc(it->size);
  146. if (!*pval)
  147. goto memerr;
  148. memset(*pval, 0, it->size);
  149. }
  150. asn1_set_choice_selector(pval, -1, it);
  151. if (asn1_cb && !asn1_cb(ASN1_OP_NEW_POST, pval, it, NULL))
  152. goto auxerr;
  153. break;
  154. case ASN1_ITYPE_NDEF_SEQUENCE:
  155. case ASN1_ITYPE_SEQUENCE:
  156. if (asn1_cb)
  157. {
  158. i = asn1_cb(ASN1_OP_NEW_PRE, pval, it, NULL);
  159. if (!i)
  160. goto auxerr;
  161. if (i==2)
  162. {
  163. #ifdef CRYPTO_MDEBUG
  164. if (it->sname)
  165. CRYPTO_pop_info();
  166. #endif
  167. return 1;
  168. }
  169. }
  170. if (!combine)
  171. {
  172. *pval = OPENSSL_malloc(it->size);
  173. if (!*pval)
  174. goto memerr;
  175. memset(*pval, 0, it->size);
  176. asn1_refcount_set_one(pval, it);
  177. asn1_enc_init(pval, it);
  178. }
  179. for (i = 0, tt = it->templates; i < it->tcount; tt++, i++)
  180. {
  181. pseqval = asn1_get_field_ptr(pval, tt);
  182. if (!ASN1_template_new(pseqval, tt))
  183. goto memerr;
  184. }
  185. if (asn1_cb && !asn1_cb(ASN1_OP_NEW_POST, pval, it, NULL))
  186. goto auxerr;
  187. break;
  188. }
  189. #ifdef CRYPTO_MDEBUG
  190. if (it->sname) CRYPTO_pop_info();
  191. #endif
  192. return 1;
  193. memerr:
  194. OPENSSL_PUT_ERROR(ASN1, ERR_R_MALLOC_FAILURE);
  195. ASN1_item_ex_free(pval, it);
  196. #ifdef CRYPTO_MDEBUG
  197. if (it->sname) CRYPTO_pop_info();
  198. #endif
  199. return 0;
  200. auxerr:
  201. OPENSSL_PUT_ERROR(ASN1, ASN1_R_AUX_ERROR);
  202. ASN1_item_ex_free(pval, it);
  203. #ifdef CRYPTO_MDEBUG
  204. if (it->sname) CRYPTO_pop_info();
  205. #endif
  206. return 0;
  207. }
  208. static void asn1_item_clear(ASN1_VALUE **pval, const ASN1_ITEM *it)
  209. {
  210. const ASN1_EXTERN_FUNCS *ef;
  211. switch(it->itype)
  212. {
  213. case ASN1_ITYPE_EXTERN:
  214. ef = it->funcs;
  215. if (ef && ef->asn1_ex_clear)
  216. ef->asn1_ex_clear(pval, it);
  217. else *pval = NULL;
  218. break;
  219. case ASN1_ITYPE_PRIMITIVE:
  220. if (it->templates)
  221. asn1_template_clear(pval, it->templates);
  222. else
  223. asn1_primitive_clear(pval, it);
  224. break;
  225. case ASN1_ITYPE_MSTRING:
  226. asn1_primitive_clear(pval, it);
  227. break;
  228. case ASN1_ITYPE_COMPAT:
  229. case ASN1_ITYPE_CHOICE:
  230. case ASN1_ITYPE_SEQUENCE:
  231. case ASN1_ITYPE_NDEF_SEQUENCE:
  232. *pval = NULL;
  233. break;
  234. }
  235. }
  236. int ASN1_template_new(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt)
  237. {
  238. const ASN1_ITEM *it = ASN1_ITEM_ptr(tt->item);
  239. int ret;
  240. if (tt->flags & ASN1_TFLG_OPTIONAL)
  241. {
  242. asn1_template_clear(pval, tt);
  243. return 1;
  244. }
  245. /* If ANY DEFINED BY nothing to do */
  246. if (tt->flags & ASN1_TFLG_ADB_MASK)
  247. {
  248. *pval = NULL;
  249. return 1;
  250. }
  251. #ifdef CRYPTO_MDEBUG
  252. if (tt->field_name)
  253. CRYPTO_push_info(tt->field_name);
  254. #endif
  255. /* If SET OF or SEQUENCE OF, its a STACK */
  256. if (tt->flags & ASN1_TFLG_SK_MASK)
  257. {
  258. STACK_OF(ASN1_VALUE) *skval;
  259. skval = sk_ASN1_VALUE_new_null();
  260. if (!skval)
  261. {
  262. OPENSSL_PUT_ERROR(ASN1, ERR_R_MALLOC_FAILURE);
  263. ret = 0;
  264. goto done;
  265. }
  266. *pval = (ASN1_VALUE *)skval;
  267. ret = 1;
  268. goto done;
  269. }
  270. /* Otherwise pass it back to the item routine */
  271. ret = asn1_item_ex_combine_new(pval, it, tt->flags & ASN1_TFLG_COMBINE);
  272. done:
  273. #ifdef CRYPTO_MDEBUG
  274. if (it->sname)
  275. CRYPTO_pop_info();
  276. #endif
  277. return ret;
  278. }
  279. static void asn1_template_clear(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt)
  280. {
  281. /* If ADB or STACK just NULL the field */
  282. if (tt->flags & (ASN1_TFLG_ADB_MASK|ASN1_TFLG_SK_MASK))
  283. *pval = NULL;
  284. else
  285. asn1_item_clear(pval, ASN1_ITEM_ptr(tt->item));
  286. }
  287. /* NB: could probably combine most of the real XXX_new() behaviour and junk
  288. * all the old functions.
  289. */
  290. int ASN1_primitive_new(ASN1_VALUE **pval, const ASN1_ITEM *it)
  291. {
  292. ASN1_TYPE *typ;
  293. ASN1_STRING *str;
  294. int utype;
  295. if (!it)
  296. return 0;
  297. if (it->funcs)
  298. {
  299. const ASN1_PRIMITIVE_FUNCS *pf = it->funcs;
  300. if (pf->prim_new)
  301. return pf->prim_new(pval, it);
  302. }
  303. if (it->itype == ASN1_ITYPE_MSTRING)
  304. utype = -1;
  305. else
  306. utype = it->utype;
  307. switch(utype)
  308. {
  309. case V_ASN1_OBJECT:
  310. *pval = (ASN1_VALUE *)OBJ_nid2obj(NID_undef);
  311. return 1;
  312. case V_ASN1_BOOLEAN:
  313. *(ASN1_BOOLEAN *)pval = it->size;
  314. return 1;
  315. case V_ASN1_NULL:
  316. *pval = (ASN1_VALUE *)1;
  317. return 1;
  318. case V_ASN1_ANY:
  319. typ = OPENSSL_malloc(sizeof(ASN1_TYPE));
  320. if (!typ)
  321. return 0;
  322. typ->value.ptr = NULL;
  323. typ->type = -1;
  324. *pval = (ASN1_VALUE *)typ;
  325. break;
  326. default:
  327. str = ASN1_STRING_type_new(utype);
  328. if (it->itype == ASN1_ITYPE_MSTRING && str)
  329. str->flags |= ASN1_STRING_FLAG_MSTRING;
  330. *pval = (ASN1_VALUE *)str;
  331. break;
  332. }
  333. if (*pval)
  334. return 1;
  335. return 0;
  336. }
  337. static void asn1_primitive_clear(ASN1_VALUE **pval, const ASN1_ITEM *it)
  338. {
  339. int utype;
  340. if (it && it->funcs)
  341. {
  342. const ASN1_PRIMITIVE_FUNCS *pf = it->funcs;
  343. if (pf->prim_clear)
  344. pf->prim_clear(pval, it);
  345. else
  346. *pval = NULL;
  347. return;
  348. }
  349. if (!it || (it->itype == ASN1_ITYPE_MSTRING))
  350. utype = -1;
  351. else
  352. utype = it->utype;
  353. if (utype == V_ASN1_BOOLEAN)
  354. *(ASN1_BOOLEAN *)pval = it->size;
  355. else *pval = NULL;
  356. }