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.
 
 
 
 
 
 

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