Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 
 
 
 
 

395 Zeilen
9.7 KiB

  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 <openssl/asn1t.h>
  58. #include <openssl/err.h>
  59. #include <openssl/mem.h>
  60. #include <openssl/obj.h>
  61. static int asn1_item_ex_combine_new(ASN1_VALUE **pval, const ASN1_ITEM *it,
  62. int combine);
  63. static void asn1_item_clear(ASN1_VALUE **pval, const ASN1_ITEM *it);
  64. static void asn1_template_clear(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt);
  65. static void asn1_primitive_clear(ASN1_VALUE **pval, const ASN1_ITEM *it);
  66. ASN1_VALUE *ASN1_item_new(const ASN1_ITEM *it)
  67. {
  68. ASN1_VALUE *ret = NULL;
  69. if (ASN1_item_ex_new(&ret, it) > 0)
  70. return ret;
  71. return NULL;
  72. }
  73. /* Allocate an ASN1 structure */
  74. int ASN1_item_ex_new(ASN1_VALUE **pval, const ASN1_ITEM *it)
  75. {
  76. return asn1_item_ex_combine_new(pval, it, 0);
  77. }
  78. static int asn1_item_ex_combine_new(ASN1_VALUE **pval, const ASN1_ITEM *it,
  79. 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)
  89. asn1_cb = aux->asn1_cb;
  90. else
  91. asn1_cb = 0;
  92. if (!combine) *pval = NULL;
  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_do_lock(pval, 0, 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, asn1_item_ex_combine_new, ERR_R_MALLOC_FAILURE);
  195. #ifdef CRYPTO_MDEBUG
  196. if (it->sname) CRYPTO_pop_info();
  197. #endif
  198. return 0;
  199. auxerr:
  200. OPENSSL_PUT_ERROR(ASN1, asn1_item_ex_combine_new, ASN1_R_AUX_ERROR);
  201. ASN1_item_ex_free(pval, it);
  202. #ifdef CRYPTO_MDEBUG
  203. if (it->sname) CRYPTO_pop_info();
  204. #endif
  205. return 0;
  206. }
  207. static void asn1_item_clear(ASN1_VALUE **pval, const ASN1_ITEM *it)
  208. {
  209. const ASN1_EXTERN_FUNCS *ef;
  210. switch(it->itype)
  211. {
  212. case ASN1_ITYPE_EXTERN:
  213. ef = it->funcs;
  214. if (ef && ef->asn1_ex_clear)
  215. ef->asn1_ex_clear(pval, it);
  216. else *pval = NULL;
  217. break;
  218. case ASN1_ITYPE_PRIMITIVE:
  219. if (it->templates)
  220. asn1_template_clear(pval, it->templates);
  221. else
  222. asn1_primitive_clear(pval, it);
  223. break;
  224. case ASN1_ITYPE_MSTRING:
  225. asn1_primitive_clear(pval, it);
  226. break;
  227. case ASN1_ITYPE_COMPAT:
  228. case ASN1_ITYPE_CHOICE:
  229. case ASN1_ITYPE_SEQUENCE:
  230. case ASN1_ITYPE_NDEF_SEQUENCE:
  231. *pval = NULL;
  232. break;
  233. }
  234. }
  235. int ASN1_template_new(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt)
  236. {
  237. const ASN1_ITEM *it = ASN1_ITEM_ptr(tt->item);
  238. int ret;
  239. if (tt->flags & ASN1_TFLG_OPTIONAL)
  240. {
  241. asn1_template_clear(pval, tt);
  242. return 1;
  243. }
  244. /* If ANY DEFINED BY nothing to do */
  245. if (tt->flags & ASN1_TFLG_ADB_MASK)
  246. {
  247. *pval = NULL;
  248. return 1;
  249. }
  250. #ifdef CRYPTO_MDEBUG
  251. if (tt->field_name)
  252. CRYPTO_push_info(tt->field_name);
  253. #endif
  254. /* If SET OF or SEQUENCE OF, its a STACK */
  255. if (tt->flags & ASN1_TFLG_SK_MASK)
  256. {
  257. STACK_OF(ASN1_VALUE) *skval;
  258. skval = sk_ASN1_VALUE_new_null();
  259. if (!skval)
  260. {
  261. OPENSSL_PUT_ERROR(ASN1, ASN1_template_new, ERR_R_MALLOC_FAILURE);
  262. ret = 0;
  263. goto done;
  264. }
  265. *pval = (ASN1_VALUE *)skval;
  266. ret = 1;
  267. goto done;
  268. }
  269. /* Otherwise pass it back to the item routine */
  270. ret = asn1_item_ex_combine_new(pval, it, tt->flags & ASN1_TFLG_COMBINE);
  271. done:
  272. #ifdef CRYPTO_MDEBUG
  273. if (it->sname)
  274. CRYPTO_pop_info();
  275. #endif
  276. return ret;
  277. }
  278. static void asn1_template_clear(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt)
  279. {
  280. /* If ADB or STACK just NULL the field */
  281. if (tt->flags & (ASN1_TFLG_ADB_MASK|ASN1_TFLG_SK_MASK))
  282. *pval = NULL;
  283. else
  284. asn1_item_clear(pval, ASN1_ITEM_ptr(tt->item));
  285. }
  286. /* NB: could probably combine most of the real XXX_new() behaviour and junk
  287. * all the old functions.
  288. */
  289. int ASN1_primitive_new(ASN1_VALUE **pval, const ASN1_ITEM *it)
  290. {
  291. ASN1_TYPE *typ;
  292. ASN1_STRING *str;
  293. int utype;
  294. if (it && it->funcs)
  295. {
  296. const ASN1_PRIMITIVE_FUNCS *pf = it->funcs;
  297. if (pf->prim_new)
  298. return pf->prim_new(pval, it);
  299. }
  300. if (!it || (it->itype == ASN1_ITYPE_MSTRING))
  301. utype = -1;
  302. else
  303. utype = it->utype;
  304. switch(utype)
  305. {
  306. case V_ASN1_OBJECT:
  307. *pval = (ASN1_VALUE *)OBJ_nid2obj(NID_undef);
  308. return 1;
  309. case V_ASN1_BOOLEAN:
  310. *(ASN1_BOOLEAN *)pval = it->size;
  311. return 1;
  312. case V_ASN1_NULL:
  313. *pval = (ASN1_VALUE *)1;
  314. return 1;
  315. case V_ASN1_ANY:
  316. typ = OPENSSL_malloc(sizeof(ASN1_TYPE));
  317. if (!typ)
  318. return 0;
  319. typ->value.ptr = NULL;
  320. typ->type = -1;
  321. *pval = (ASN1_VALUE *)typ;
  322. break;
  323. default:
  324. str = ASN1_STRING_type_new(utype);
  325. if (it->itype == ASN1_ITYPE_MSTRING && str)
  326. str->flags |= ASN1_STRING_FLAG_MSTRING;
  327. *pval = (ASN1_VALUE *)str;
  328. break;
  329. }
  330. if (*pval)
  331. return 1;
  332. return 0;
  333. }
  334. static void asn1_primitive_clear(ASN1_VALUE **pval, const ASN1_ITEM *it)
  335. {
  336. int utype;
  337. if (it && it->funcs)
  338. {
  339. const ASN1_PRIMITIVE_FUNCS *pf = it->funcs;
  340. if (pf->prim_clear)
  341. pf->prim_clear(pval, it);
  342. else
  343. *pval = NULL;
  344. return;
  345. }
  346. if (!it || (it->itype == ASN1_ITYPE_MSTRING))
  347. utype = -1;
  348. else
  349. utype = it->utype;
  350. if (utype == V_ASN1_BOOLEAN)
  351. *(ASN1_BOOLEAN *)pval = it->size;
  352. else *pval = NULL;
  353. }