Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.

tasn_new.c 11 KiB

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