Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 
 
 

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