tasn_new.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. /*
  2. * Copyright 2000-2020 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (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. switch (it->itype) {
  49. case ASN1_ITYPE_EXTERN:
  50. ef = it->funcs;
  51. if (ef && ef->asn1_ex_new) {
  52. if (!ef->asn1_ex_new(pval, it))
  53. goto memerr;
  54. }
  55. break;
  56. case ASN1_ITYPE_PRIMITIVE:
  57. if (it->templates) {
  58. if (!asn1_template_new(pval, it->templates))
  59. goto memerr;
  60. } else if (!asn1_primitive_new(pval, it, embed))
  61. goto memerr;
  62. break;
  63. case ASN1_ITYPE_MSTRING:
  64. if (!asn1_primitive_new(pval, it, embed))
  65. goto memerr;
  66. break;
  67. case ASN1_ITYPE_CHOICE:
  68. if (asn1_cb) {
  69. i = asn1_cb(ASN1_OP_NEW_PRE, pval, it, NULL);
  70. if (!i)
  71. goto auxerr;
  72. if (i == 2) {
  73. return 1;
  74. }
  75. }
  76. if (embed) {
  77. memset(*pval, 0, it->size);
  78. } else {
  79. *pval = OPENSSL_zalloc(it->size);
  80. if (*pval == NULL)
  81. goto memerr;
  82. }
  83. asn1_set_choice_selector(pval, -1, it);
  84. if (asn1_cb && !asn1_cb(ASN1_OP_NEW_POST, pval, it, NULL))
  85. goto auxerr2;
  86. break;
  87. case ASN1_ITYPE_NDEF_SEQUENCE:
  88. case ASN1_ITYPE_SEQUENCE:
  89. if (asn1_cb) {
  90. i = asn1_cb(ASN1_OP_NEW_PRE, pval, it, NULL);
  91. if (!i)
  92. goto auxerr;
  93. if (i == 2) {
  94. return 1;
  95. }
  96. }
  97. if (embed) {
  98. memset(*pval, 0, it->size);
  99. } else {
  100. *pval = OPENSSL_zalloc(it->size);
  101. if (*pval == NULL)
  102. goto memerr;
  103. }
  104. /* 0 : init. lock */
  105. if (asn1_do_lock(pval, 0, it) < 0) {
  106. if (!embed) {
  107. OPENSSL_free(*pval);
  108. *pval = NULL;
  109. }
  110. goto memerr;
  111. }
  112. asn1_enc_init(pval, it);
  113. for (i = 0, tt = it->templates; i < it->tcount; tt++, i++) {
  114. pseqval = asn1_get_field_ptr(pval, tt);
  115. if (!asn1_template_new(pseqval, tt))
  116. goto memerr2;
  117. }
  118. if (asn1_cb && !asn1_cb(ASN1_OP_NEW_POST, pval, it, NULL))
  119. goto auxerr2;
  120. break;
  121. }
  122. return 1;
  123. memerr2:
  124. asn1_item_embed_free(pval, it, embed);
  125. memerr:
  126. ASN1err(ASN1_F_ASN1_ITEM_EMBED_NEW, ERR_R_MALLOC_FAILURE);
  127. return 0;
  128. auxerr2:
  129. asn1_item_embed_free(pval, it, embed);
  130. auxerr:
  131. ASN1err(ASN1_F_ASN1_ITEM_EMBED_NEW, ASN1_R_AUX_ERROR);
  132. return 0;
  133. }
  134. static void asn1_item_clear(ASN1_VALUE **pval, const ASN1_ITEM *it)
  135. {
  136. const ASN1_EXTERN_FUNCS *ef;
  137. switch (it->itype) {
  138. case ASN1_ITYPE_EXTERN:
  139. ef = it->funcs;
  140. if (ef && ef->asn1_ex_clear)
  141. ef->asn1_ex_clear(pval, it);
  142. else
  143. *pval = NULL;
  144. break;
  145. case ASN1_ITYPE_PRIMITIVE:
  146. if (it->templates)
  147. asn1_template_clear(pval, it->templates);
  148. else
  149. asn1_primitive_clear(pval, it);
  150. break;
  151. case ASN1_ITYPE_MSTRING:
  152. asn1_primitive_clear(pval, it);
  153. break;
  154. case ASN1_ITYPE_CHOICE:
  155. case ASN1_ITYPE_SEQUENCE:
  156. case ASN1_ITYPE_NDEF_SEQUENCE:
  157. *pval = NULL;
  158. break;
  159. }
  160. }
  161. static int asn1_template_new(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt)
  162. {
  163. const ASN1_ITEM *it = ASN1_ITEM_ptr(tt->item);
  164. int embed = tt->flags & ASN1_TFLG_EMBED;
  165. ASN1_VALUE *tval;
  166. int ret;
  167. if (embed) {
  168. tval = (ASN1_VALUE *)pval;
  169. pval = &tval;
  170. }
  171. if (tt->flags & ASN1_TFLG_OPTIONAL) {
  172. asn1_template_clear(pval, tt);
  173. return 1;
  174. }
  175. /* If ANY DEFINED BY nothing to do */
  176. if (tt->flags & ASN1_TFLG_ADB_MASK) {
  177. *pval = NULL;
  178. return 1;
  179. }
  180. /* If SET OF or SEQUENCE OF, its a STACK */
  181. if (tt->flags & ASN1_TFLG_SK_MASK) {
  182. STACK_OF(ASN1_VALUE) *skval;
  183. skval = sk_ASN1_VALUE_new_null();
  184. if (!skval) {
  185. ASN1err(ASN1_F_ASN1_TEMPLATE_NEW, ERR_R_MALLOC_FAILURE);
  186. ret = 0;
  187. goto done;
  188. }
  189. *pval = (ASN1_VALUE *)skval;
  190. ret = 1;
  191. goto done;
  192. }
  193. /* Otherwise pass it back to the item routine */
  194. ret = asn1_item_embed_new(pval, it, embed);
  195. done:
  196. return ret;
  197. }
  198. static void asn1_template_clear(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt)
  199. {
  200. /* If ADB or STACK just NULL the field */
  201. if (tt->flags & (ASN1_TFLG_ADB_MASK | ASN1_TFLG_SK_MASK))
  202. *pval = NULL;
  203. else
  204. asn1_item_clear(pval, ASN1_ITEM_ptr(tt->item));
  205. }
  206. /*
  207. * NB: could probably combine most of the real XXX_new() behaviour and junk
  208. * all the old functions.
  209. */
  210. static int asn1_primitive_new(ASN1_VALUE **pval, const ASN1_ITEM *it,
  211. int embed)
  212. {
  213. ASN1_TYPE *typ;
  214. ASN1_STRING *str;
  215. int utype;
  216. if (!it)
  217. return 0;
  218. if (it->funcs) {
  219. const ASN1_PRIMITIVE_FUNCS *pf = it->funcs;
  220. if (embed) {
  221. if (pf->prim_clear) {
  222. pf->prim_clear(pval, it);
  223. return 1;
  224. }
  225. } else if (pf->prim_new) {
  226. return pf->prim_new(pval, it);
  227. }
  228. }
  229. if (it->itype == ASN1_ITYPE_MSTRING)
  230. utype = -1;
  231. else
  232. utype = it->utype;
  233. switch (utype) {
  234. case V_ASN1_OBJECT:
  235. *pval = (ASN1_VALUE *)OBJ_nid2obj(NID_undef);
  236. return 1;
  237. case V_ASN1_BOOLEAN:
  238. *(ASN1_BOOLEAN *)pval = it->size;
  239. return 1;
  240. case V_ASN1_NULL:
  241. *pval = (ASN1_VALUE *)1;
  242. return 1;
  243. case V_ASN1_ANY:
  244. if ((typ = OPENSSL_malloc(sizeof(*typ))) == NULL) {
  245. ASN1err(ASN1_F_ASN1_PRIMITIVE_NEW, ERR_R_MALLOC_FAILURE);
  246. return 0;
  247. }
  248. typ->value.ptr = NULL;
  249. typ->type = -1;
  250. *pval = (ASN1_VALUE *)typ;
  251. break;
  252. default:
  253. if (embed) {
  254. str = *(ASN1_STRING **)pval;
  255. memset(str, 0, sizeof(*str));
  256. str->type = utype;
  257. str->flags = ASN1_STRING_FLAG_EMBED;
  258. } else {
  259. str = ASN1_STRING_type_new(utype);
  260. *pval = (ASN1_VALUE *)str;
  261. }
  262. if (it->itype == ASN1_ITYPE_MSTRING && str)
  263. str->flags |= ASN1_STRING_FLAG_MSTRING;
  264. break;
  265. }
  266. if (*pval)
  267. return 1;
  268. return 0;
  269. }
  270. static void asn1_primitive_clear(ASN1_VALUE **pval, const ASN1_ITEM *it)
  271. {
  272. int utype;
  273. if (it && it->funcs) {
  274. const ASN1_PRIMITIVE_FUNCS *pf = it->funcs;
  275. if (pf->prim_clear)
  276. pf->prim_clear(pval, it);
  277. else
  278. *pval = NULL;
  279. return;
  280. }
  281. if (!it || (it->itype == ASN1_ITYPE_MSTRING))
  282. utype = -1;
  283. else
  284. utype = it->utype;
  285. if (utype == V_ASN1_BOOLEAN)
  286. *(ASN1_BOOLEAN *)pval = it->size;
  287. else
  288. *pval = NULL;
  289. }