2
0

tasn_new.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. /*
  2. * Copyright 2000-2021 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, OSSL_LIB_CTX *libctx,
  18. const char *propq);
  19. static int asn1_primitive_new(ASN1_VALUE **pval, const ASN1_ITEM *it,
  20. int embed);
  21. static void asn1_item_clear(ASN1_VALUE **pval, const ASN1_ITEM *it);
  22. static int asn1_template_new(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt,
  23. OSSL_LIB_CTX *libctx, const char *propq);
  24. static void asn1_template_clear(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt);
  25. static void asn1_primitive_clear(ASN1_VALUE **pval, const ASN1_ITEM *it);
  26. ASN1_VALUE *ASN1_item_new(const ASN1_ITEM *it)
  27. {
  28. ASN1_VALUE *ret = NULL;
  29. if (ASN1_item_ex_new(&ret, it) > 0)
  30. return ret;
  31. return NULL;
  32. }
  33. ASN1_VALUE *ASN1_item_new_ex(const ASN1_ITEM *it, OSSL_LIB_CTX *libctx,
  34. const char *propq)
  35. {
  36. ASN1_VALUE *ret = NULL;
  37. if (asn1_item_embed_new(&ret, it, 0, libctx, propq) > 0)
  38. return ret;
  39. return NULL;
  40. }
  41. /* Allocate an ASN1 structure */
  42. int ossl_asn1_item_ex_new_intern(ASN1_VALUE **pval, const ASN1_ITEM *it,
  43. OSSL_LIB_CTX *libctx, const char *propq)
  44. {
  45. return asn1_item_embed_new(pval, it, 0, libctx, propq);
  46. }
  47. int ASN1_item_ex_new(ASN1_VALUE **pval, const ASN1_ITEM *it)
  48. {
  49. return asn1_item_embed_new(pval, it, 0, NULL, NULL);
  50. }
  51. int asn1_item_embed_new(ASN1_VALUE **pval, const ASN1_ITEM *it, int embed,
  52. OSSL_LIB_CTX *libctx, const char *propq)
  53. {
  54. const ASN1_TEMPLATE *tt = NULL;
  55. const ASN1_EXTERN_FUNCS *ef;
  56. const ASN1_AUX *aux = it->funcs;
  57. ASN1_aux_cb *asn1_cb;
  58. ASN1_VALUE **pseqval;
  59. int i;
  60. if (aux && aux->asn1_cb)
  61. asn1_cb = aux->asn1_cb;
  62. else
  63. asn1_cb = 0;
  64. switch (it->itype) {
  65. case ASN1_ITYPE_EXTERN:
  66. ef = it->funcs;
  67. if (ef != NULL) {
  68. if (ef->asn1_ex_new_ex != NULL) {
  69. if (!ef->asn1_ex_new_ex(pval, it, libctx, propq))
  70. goto asn1err;
  71. } else if (ef->asn1_ex_new != NULL) {
  72. if (!ef->asn1_ex_new(pval, it))
  73. goto asn1err;
  74. }
  75. }
  76. break;
  77. case ASN1_ITYPE_PRIMITIVE:
  78. if (it->templates) {
  79. if (!asn1_template_new(pval, it->templates, libctx, propq))
  80. goto asn1err;
  81. } else if (!asn1_primitive_new(pval, it, embed))
  82. goto asn1err;
  83. break;
  84. case ASN1_ITYPE_MSTRING:
  85. if (!asn1_primitive_new(pval, it, embed))
  86. goto asn1err;
  87. break;
  88. case ASN1_ITYPE_CHOICE:
  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. return 0;
  103. }
  104. ossl_asn1_set_choice_selector(pval, -1, it);
  105. if (asn1_cb && !asn1_cb(ASN1_OP_NEW_POST, pval, it, NULL))
  106. goto auxerr2;
  107. break;
  108. case ASN1_ITYPE_NDEF_SEQUENCE:
  109. case ASN1_ITYPE_SEQUENCE:
  110. if (asn1_cb) {
  111. i = asn1_cb(ASN1_OP_NEW_PRE, pval, it, NULL);
  112. if (!i)
  113. goto auxerr;
  114. if (i == 2) {
  115. return 1;
  116. }
  117. }
  118. if (embed) {
  119. memset(*pval, 0, it->size);
  120. } else {
  121. *pval = OPENSSL_zalloc(it->size);
  122. if (*pval == NULL)
  123. return 0;
  124. }
  125. /* 0 : init. lock */
  126. if (ossl_asn1_do_lock(pval, 0, it) < 0) {
  127. if (!embed) {
  128. OPENSSL_free(*pval);
  129. *pval = NULL;
  130. }
  131. goto asn1err;
  132. }
  133. ossl_asn1_enc_init(pval, it);
  134. for (i = 0, tt = it->templates; i < it->tcount; tt++, i++) {
  135. pseqval = ossl_asn1_get_field_ptr(pval, tt);
  136. if (!asn1_template_new(pseqval, tt, libctx, propq))
  137. goto asn1err2;
  138. }
  139. if (asn1_cb && !asn1_cb(ASN1_OP_NEW_POST, pval, it, NULL))
  140. goto auxerr2;
  141. break;
  142. }
  143. return 1;
  144. asn1err2:
  145. ossl_asn1_item_embed_free(pval, it, embed);
  146. asn1err:
  147. ERR_raise(ERR_LIB_ASN1, ERR_R_ASN1_LIB);
  148. return 0;
  149. auxerr2:
  150. ossl_asn1_item_embed_free(pval, it, embed);
  151. auxerr:
  152. ERR_raise(ERR_LIB_ASN1, ASN1_R_AUX_ERROR);
  153. return 0;
  154. }
  155. static void asn1_item_clear(ASN1_VALUE **pval, const ASN1_ITEM *it)
  156. {
  157. const ASN1_EXTERN_FUNCS *ef;
  158. switch (it->itype) {
  159. case ASN1_ITYPE_EXTERN:
  160. ef = it->funcs;
  161. if (ef && ef->asn1_ex_clear)
  162. ef->asn1_ex_clear(pval, it);
  163. else
  164. *pval = NULL;
  165. break;
  166. case ASN1_ITYPE_PRIMITIVE:
  167. if (it->templates)
  168. asn1_template_clear(pval, it->templates);
  169. else
  170. asn1_primitive_clear(pval, it);
  171. break;
  172. case ASN1_ITYPE_MSTRING:
  173. asn1_primitive_clear(pval, it);
  174. break;
  175. case ASN1_ITYPE_CHOICE:
  176. case ASN1_ITYPE_SEQUENCE:
  177. case ASN1_ITYPE_NDEF_SEQUENCE:
  178. *pval = NULL;
  179. break;
  180. }
  181. }
  182. static int asn1_template_new(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt,
  183. OSSL_LIB_CTX *libctx, const char *propq)
  184. {
  185. const ASN1_ITEM *it = ASN1_ITEM_ptr(tt->item);
  186. int embed = tt->flags & ASN1_TFLG_EMBED;
  187. ASN1_VALUE *tval;
  188. int ret;
  189. if (embed) {
  190. tval = (ASN1_VALUE *)pval;
  191. pval = &tval;
  192. }
  193. if (tt->flags & ASN1_TFLG_OPTIONAL) {
  194. asn1_template_clear(pval, tt);
  195. return 1;
  196. }
  197. /* If ANY DEFINED BY nothing to do */
  198. if (tt->flags & ASN1_TFLG_ADB_MASK) {
  199. *pval = NULL;
  200. return 1;
  201. }
  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. ERR_raise(ERR_LIB_ASN1, ERR_R_CRYPTO_LIB);
  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, libctx, propq);
  217. done:
  218. return ret;
  219. }
  220. static void asn1_template_clear(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt)
  221. {
  222. /* If ADB or STACK just NULL the field */
  223. if (tt->flags & (ASN1_TFLG_ADB_MASK | ASN1_TFLG_SK_MASK))
  224. *pval = NULL;
  225. else
  226. asn1_item_clear(pval, ASN1_ITEM_ptr(tt->item));
  227. }
  228. /*
  229. * NB: could probably combine most of the real XXX_new() behaviour and junk
  230. * all the old functions.
  231. */
  232. static int asn1_primitive_new(ASN1_VALUE **pval, const ASN1_ITEM *it,
  233. int embed)
  234. {
  235. ASN1_TYPE *typ;
  236. ASN1_STRING *str;
  237. int utype;
  238. if (!it)
  239. return 0;
  240. if (it->funcs) {
  241. const ASN1_PRIMITIVE_FUNCS *pf = it->funcs;
  242. if (embed) {
  243. if (pf->prim_clear) {
  244. pf->prim_clear(pval, it);
  245. return 1;
  246. }
  247. } else if (pf->prim_new) {
  248. return pf->prim_new(pval, it);
  249. }
  250. }
  251. if (it->itype == ASN1_ITYPE_MSTRING)
  252. utype = -1;
  253. else
  254. utype = it->utype;
  255. switch (utype) {
  256. case V_ASN1_OBJECT:
  257. *pval = (ASN1_VALUE *)OBJ_nid2obj(NID_undef);
  258. return 1;
  259. case V_ASN1_BOOLEAN:
  260. *(ASN1_BOOLEAN *)pval = it->size;
  261. return 1;
  262. case V_ASN1_NULL:
  263. *pval = (ASN1_VALUE *)1;
  264. return 1;
  265. case V_ASN1_ANY:
  266. if ((typ = OPENSSL_malloc(sizeof(*typ))) == NULL)
  267. return 0;
  268. typ->value.ptr = NULL;
  269. typ->type = -1;
  270. *pval = (ASN1_VALUE *)typ;
  271. break;
  272. default:
  273. if (embed) {
  274. str = *(ASN1_STRING **)pval;
  275. memset(str, 0, sizeof(*str));
  276. str->type = utype;
  277. str->flags = ASN1_STRING_FLAG_EMBED;
  278. } else {
  279. str = ASN1_STRING_type_new(utype);
  280. *pval = (ASN1_VALUE *)str;
  281. }
  282. if (it->itype == ASN1_ITYPE_MSTRING && str)
  283. str->flags |= ASN1_STRING_FLAG_MSTRING;
  284. break;
  285. }
  286. if (*pval)
  287. return 1;
  288. return 0;
  289. }
  290. static void asn1_primitive_clear(ASN1_VALUE **pval, const ASN1_ITEM *it)
  291. {
  292. int utype;
  293. if (it && it->funcs) {
  294. const ASN1_PRIMITIVE_FUNCS *pf = it->funcs;
  295. if (pf->prim_clear)
  296. pf->prim_clear(pval, it);
  297. else
  298. *pval = NULL;
  299. return;
  300. }
  301. if (!it || (it->itype == ASN1_ITYPE_MSTRING))
  302. utype = -1;
  303. else
  304. utype = it->utype;
  305. if (utype == V_ASN1_BOOLEAN)
  306. *(ASN1_BOOLEAN *)pval = it->size;
  307. else
  308. *pval = NULL;
  309. }