tasn_new.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. /*
  2. * Copyright 2000-2018 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL license (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include <stddef.h>
  10. #include <openssl/asn1.h>
  11. #include <openssl/objects.h>
  12. #include <openssl/err.h>
  13. #include <openssl/asn1t.h>
  14. #include <string.h>
  15. #include "asn1_local.h"
  16. static int asn1_item_embed_new(ASN1_VALUE **pval, const ASN1_ITEM *it,
  17. int embed);
  18. static int asn1_primitive_new(ASN1_VALUE **pval, const ASN1_ITEM *it,
  19. int embed);
  20. static void asn1_item_clear(ASN1_VALUE **pval, const ASN1_ITEM *it);
  21. static int asn1_template_new(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt);
  22. static void asn1_template_clear(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt);
  23. static void asn1_primitive_clear(ASN1_VALUE **pval, const ASN1_ITEM *it);
  24. ASN1_VALUE *ASN1_item_new(const ASN1_ITEM *it)
  25. {
  26. ASN1_VALUE *ret = NULL;
  27. if (ASN1_item_ex_new(&ret, it) > 0)
  28. return ret;
  29. return NULL;
  30. }
  31. /* Allocate an ASN1 structure */
  32. int ASN1_item_ex_new(ASN1_VALUE **pval, const ASN1_ITEM *it)
  33. {
  34. return asn1_item_embed_new(pval, it, 0);
  35. }
  36. int asn1_item_embed_new(ASN1_VALUE **pval, const ASN1_ITEM *it, int embed)
  37. {
  38. const ASN1_TEMPLATE *tt = NULL;
  39. const ASN1_EXTERN_FUNCS *ef;
  40. const ASN1_AUX *aux = it->funcs;
  41. ASN1_aux_cb *asn1_cb;
  42. ASN1_VALUE **pseqval;
  43. int i;
  44. if (aux && aux->asn1_cb)
  45. asn1_cb = aux->asn1_cb;
  46. else
  47. asn1_cb = 0;
  48. #ifndef OPENSSL_NO_CRYPTO_MDEBUG
  49. OPENSSL_mem_debug_push(it->sname ? it->sname : "asn1_item_embed_new");
  50. #endif
  51. switch (it->itype) {
  52. case ASN1_ITYPE_EXTERN:
  53. ef = it->funcs;
  54. if (ef && ef->asn1_ex_new) {
  55. if (!ef->asn1_ex_new(pval, it))
  56. goto memerr;
  57. }
  58. break;
  59. case ASN1_ITYPE_PRIMITIVE:
  60. if (it->templates) {
  61. if (!asn1_template_new(pval, it->templates))
  62. goto memerr;
  63. } else if (!asn1_primitive_new(pval, it, embed))
  64. goto memerr;
  65. break;
  66. case ASN1_ITYPE_MSTRING:
  67. if (!asn1_primitive_new(pval, it, embed))
  68. goto memerr;
  69. break;
  70. case ASN1_ITYPE_CHOICE:
  71. if (asn1_cb) {
  72. i = asn1_cb(ASN1_OP_NEW_PRE, pval, it, NULL);
  73. if (!i)
  74. goto auxerr;
  75. if (i == 2) {
  76. #ifndef OPENSSL_NO_CRYPTO_MDEBUG
  77. OPENSSL_mem_debug_pop();
  78. #endif
  79. return 1;
  80. }
  81. }
  82. if (embed) {
  83. memset(*pval, 0, it->size);
  84. } else {
  85. *pval = OPENSSL_zalloc(it->size);
  86. if (*pval == NULL)
  87. goto memerr;
  88. }
  89. asn1_set_choice_selector(pval, -1, it);
  90. if (asn1_cb && !asn1_cb(ASN1_OP_NEW_POST, pval, it, NULL))
  91. goto auxerr2;
  92. break;
  93. case ASN1_ITYPE_NDEF_SEQUENCE:
  94. case ASN1_ITYPE_SEQUENCE:
  95. if (asn1_cb) {
  96. i = asn1_cb(ASN1_OP_NEW_PRE, pval, it, NULL);
  97. if (!i)
  98. goto auxerr;
  99. if (i == 2) {
  100. #ifndef OPENSSL_NO_CRYPTO_MDEBUG
  101. OPENSSL_mem_debug_pop();
  102. #endif
  103. return 1;
  104. }
  105. }
  106. if (embed) {
  107. memset(*pval, 0, it->size);
  108. } else {
  109. *pval = OPENSSL_zalloc(it->size);
  110. if (*pval == NULL)
  111. goto memerr;
  112. }
  113. /* 0 : init. lock */
  114. if (asn1_do_lock(pval, 0, it) < 0) {
  115. if (!embed) {
  116. OPENSSL_free(*pval);
  117. *pval = NULL;
  118. }
  119. goto memerr;
  120. }
  121. asn1_enc_init(pval, it);
  122. for (i = 0, tt = it->templates; i < it->tcount; tt++, i++) {
  123. pseqval = asn1_get_field_ptr(pval, tt);
  124. if (!asn1_template_new(pseqval, tt))
  125. goto memerr2;
  126. }
  127. if (asn1_cb && !asn1_cb(ASN1_OP_NEW_POST, pval, it, NULL))
  128. goto auxerr2;
  129. break;
  130. }
  131. #ifndef OPENSSL_NO_CRYPTO_MDEBUG
  132. OPENSSL_mem_debug_pop();
  133. #endif
  134. return 1;
  135. memerr2:
  136. asn1_item_embed_free(pval, it, embed);
  137. memerr:
  138. ASN1err(ASN1_F_ASN1_ITEM_EMBED_NEW, ERR_R_MALLOC_FAILURE);
  139. #ifndef OPENSSL_NO_CRYPTO_MDEBUG
  140. OPENSSL_mem_debug_pop();
  141. #endif
  142. return 0;
  143. auxerr2:
  144. asn1_item_embed_free(pval, it, embed);
  145. auxerr:
  146. ASN1err(ASN1_F_ASN1_ITEM_EMBED_NEW, ASN1_R_AUX_ERROR);
  147. #ifndef OPENSSL_NO_CRYPTO_MDEBUG
  148. OPENSSL_mem_debug_pop();
  149. #endif
  150. return 0;
  151. }
  152. static void asn1_item_clear(ASN1_VALUE **pval, const ASN1_ITEM *it)
  153. {
  154. const ASN1_EXTERN_FUNCS *ef;
  155. switch (it->itype) {
  156. case ASN1_ITYPE_EXTERN:
  157. ef = it->funcs;
  158. if (ef && ef->asn1_ex_clear)
  159. ef->asn1_ex_clear(pval, it);
  160. else
  161. *pval = NULL;
  162. break;
  163. case ASN1_ITYPE_PRIMITIVE:
  164. if (it->templates)
  165. asn1_template_clear(pval, it->templates);
  166. else
  167. asn1_primitive_clear(pval, it);
  168. break;
  169. case ASN1_ITYPE_MSTRING:
  170. asn1_primitive_clear(pval, it);
  171. break;
  172. case ASN1_ITYPE_CHOICE:
  173. case ASN1_ITYPE_SEQUENCE:
  174. case ASN1_ITYPE_NDEF_SEQUENCE:
  175. *pval = NULL;
  176. break;
  177. }
  178. }
  179. static int asn1_template_new(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt)
  180. {
  181. const ASN1_ITEM *it = ASN1_ITEM_ptr(tt->item);
  182. int embed = tt->flags & ASN1_TFLG_EMBED;
  183. ASN1_VALUE *tval;
  184. int ret;
  185. if (embed) {
  186. tval = (ASN1_VALUE *)pval;
  187. pval = &tval;
  188. }
  189. if (tt->flags & ASN1_TFLG_OPTIONAL) {
  190. asn1_template_clear(pval, tt);
  191. return 1;
  192. }
  193. /* If ANY DEFINED BY nothing to do */
  194. if (tt->flags & ASN1_TFLG_ADB_MASK) {
  195. *pval = NULL;
  196. return 1;
  197. }
  198. #ifndef OPENSSL_NO_CRYPTO_MDEBUG
  199. OPENSSL_mem_debug_push(tt->field_name
  200. ? tt->field_name : "asn1_template_new");
  201. #endif
  202. /* If SET OF or SEQUENCE OF, its a STACK */
  203. if (tt->flags & ASN1_TFLG_SK_MASK) {
  204. STACK_OF(ASN1_VALUE) *skval;
  205. skval = sk_ASN1_VALUE_new_null();
  206. if (!skval) {
  207. ASN1err(ASN1_F_ASN1_TEMPLATE_NEW, ERR_R_MALLOC_FAILURE);
  208. ret = 0;
  209. goto done;
  210. }
  211. *pval = (ASN1_VALUE *)skval;
  212. ret = 1;
  213. goto done;
  214. }
  215. /* Otherwise pass it back to the item routine */
  216. ret = asn1_item_embed_new(pval, it, embed);
  217. done:
  218. #ifndef OPENSSL_NO_CRYPTO_MDEBUG
  219. OPENSSL_mem_debug_pop();
  220. #endif
  221. return ret;
  222. }
  223. static void asn1_template_clear(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt)
  224. {
  225. /* If ADB or STACK just NULL the field */
  226. if (tt->flags & (ASN1_TFLG_ADB_MASK | ASN1_TFLG_SK_MASK))
  227. *pval = NULL;
  228. else
  229. asn1_item_clear(pval, ASN1_ITEM_ptr(tt->item));
  230. }
  231. /*
  232. * NB: could probably combine most of the real XXX_new() behaviour and junk
  233. * all the old functions.
  234. */
  235. static int asn1_primitive_new(ASN1_VALUE **pval, const ASN1_ITEM *it,
  236. int embed)
  237. {
  238. ASN1_TYPE *typ;
  239. ASN1_STRING *str;
  240. int utype;
  241. if (!it)
  242. return 0;
  243. if (it->funcs) {
  244. const ASN1_PRIMITIVE_FUNCS *pf = it->funcs;
  245. if (embed) {
  246. if (pf->prim_clear) {
  247. pf->prim_clear(pval, it);
  248. return 1;
  249. }
  250. } else if (pf->prim_new) {
  251. return pf->prim_new(pval, it);
  252. }
  253. }
  254. if (it->itype == ASN1_ITYPE_MSTRING)
  255. utype = -1;
  256. else
  257. utype = it->utype;
  258. switch (utype) {
  259. case V_ASN1_OBJECT:
  260. *pval = (ASN1_VALUE *)OBJ_nid2obj(NID_undef);
  261. return 1;
  262. case V_ASN1_BOOLEAN:
  263. *(ASN1_BOOLEAN *)pval = it->size;
  264. return 1;
  265. case V_ASN1_NULL:
  266. *pval = (ASN1_VALUE *)1;
  267. return 1;
  268. case V_ASN1_ANY:
  269. if ((typ = OPENSSL_malloc(sizeof(*typ))) == NULL) {
  270. ASN1err(ASN1_F_ASN1_PRIMITIVE_NEW, ERR_R_MALLOC_FAILURE);
  271. return 0;
  272. }
  273. typ->value.ptr = NULL;
  274. typ->type = -1;
  275. *pval = (ASN1_VALUE *)typ;
  276. break;
  277. default:
  278. if (embed) {
  279. str = *(ASN1_STRING **)pval;
  280. memset(str, 0, sizeof(*str));
  281. str->type = utype;
  282. str->flags = ASN1_STRING_FLAG_EMBED;
  283. } else {
  284. str = ASN1_STRING_type_new(utype);
  285. *pval = (ASN1_VALUE *)str;
  286. }
  287. if (it->itype == ASN1_ITYPE_MSTRING && str)
  288. str->flags |= ASN1_STRING_FLAG_MSTRING;
  289. break;
  290. }
  291. if (*pval)
  292. return 1;
  293. return 0;
  294. }
  295. static void asn1_primitive_clear(ASN1_VALUE **pval, const ASN1_ITEM *it)
  296. {
  297. int utype;
  298. if (it && it->funcs) {
  299. const ASN1_PRIMITIVE_FUNCS *pf = it->funcs;
  300. if (pf->prim_clear)
  301. pf->prim_clear(pval, it);
  302. else
  303. *pval = NULL;
  304. return;
  305. }
  306. if (!it || (it->itype == ASN1_ITYPE_MSTRING))
  307. utype = -1;
  308. else
  309. utype = it->utype;
  310. if (utype == V_ASN1_BOOLEAN)
  311. *(ASN1_BOOLEAN *)pval = it->size;
  312. else
  313. *pval = NULL;
  314. }