tasn_utl.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. /*
  2. * Copyright 2000-2023 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 <string.h>
  11. #include "internal/cryptlib.h"
  12. #include "internal/refcount.h"
  13. #include <openssl/asn1.h>
  14. #include <openssl/asn1t.h>
  15. #include <openssl/objects.h>
  16. #include <openssl/err.h>
  17. #include "asn1_local.h"
  18. /* Utility functions for manipulating fields and offsets */
  19. /* Add 'offset' to 'addr' */
  20. #define offset2ptr(addr, offset) (void *)(((char *) addr) + offset)
  21. /*
  22. * Given an ASN1_ITEM CHOICE type return the selector value
  23. */
  24. int ossl_asn1_get_choice_selector(ASN1_VALUE **pval, const ASN1_ITEM *it)
  25. {
  26. int *sel = offset2ptr(*pval, it->utype);
  27. return *sel;
  28. }
  29. int ossl_asn1_get_choice_selector_const(const ASN1_VALUE **pval,
  30. const ASN1_ITEM *it)
  31. {
  32. int *sel = offset2ptr(*pval, it->utype);
  33. return *sel;
  34. }
  35. /*
  36. * Given an ASN1_ITEM CHOICE type set the selector value, return old value.
  37. */
  38. int ossl_asn1_set_choice_selector(ASN1_VALUE **pval, int value,
  39. const ASN1_ITEM *it)
  40. {
  41. int *sel, ret;
  42. sel = offset2ptr(*pval, it->utype);
  43. ret = *sel;
  44. *sel = value;
  45. return ret;
  46. }
  47. /*
  48. * Do atomic reference counting. The value 'op' decides what to do.
  49. * If it is +1 then the count is incremented.
  50. * If |op| is 0, count is initialised and set to 1.
  51. * If |op| is -1, count is decremented and the return value is the current
  52. * reference count or 0 if no reference count is active.
  53. * It returns -1 on initialisation error.
  54. * Used by ASN1_SEQUENCE construct of X509, X509_REQ, X509_CRL objects
  55. */
  56. int ossl_asn1_do_lock(ASN1_VALUE **pval, int op, const ASN1_ITEM *it)
  57. {
  58. const ASN1_AUX *aux;
  59. CRYPTO_RWLOCK **lock;
  60. CRYPTO_REF_COUNT *refcnt;
  61. int ret = -1;
  62. if ((it->itype != ASN1_ITYPE_SEQUENCE)
  63. && (it->itype != ASN1_ITYPE_NDEF_SEQUENCE))
  64. return 0;
  65. aux = it->funcs;
  66. if (aux == NULL || (aux->flags & ASN1_AFLG_REFCOUNT) == 0)
  67. return 0;
  68. lock = offset2ptr(*pval, aux->ref_lock);
  69. refcnt = offset2ptr(*pval, aux->ref_offset);
  70. switch (op) {
  71. case 0:
  72. if (!CRYPTO_NEW_REF(refcnt, 1))
  73. return -1;
  74. *lock = CRYPTO_THREAD_lock_new();
  75. if (*lock == NULL) {
  76. CRYPTO_FREE_REF(refcnt);
  77. ERR_raise(ERR_LIB_ASN1, ERR_R_CRYPTO_LIB);
  78. return -1;
  79. }
  80. ret = 1;
  81. break;
  82. case 1:
  83. if (!CRYPTO_UP_REF(refcnt, &ret))
  84. return -1;
  85. break;
  86. case -1:
  87. if (!CRYPTO_DOWN_REF(refcnt, &ret))
  88. return -1; /* failed */
  89. REF_PRINT_EX(it->sname, ret, (void *)it);
  90. REF_ASSERT_ISNT(ret < 0);
  91. if (ret == 0) {
  92. CRYPTO_THREAD_lock_free(*lock);
  93. *lock = NULL;
  94. CRYPTO_FREE_REF(refcnt);
  95. }
  96. break;
  97. }
  98. return ret;
  99. }
  100. static ASN1_ENCODING *asn1_get_enc_ptr(ASN1_VALUE **pval, const ASN1_ITEM *it)
  101. {
  102. const ASN1_AUX *aux;
  103. if (pval == NULL || *pval == NULL)
  104. return NULL;
  105. aux = it->funcs;
  106. if (aux == NULL || (aux->flags & ASN1_AFLG_ENCODING) == 0)
  107. return NULL;
  108. return offset2ptr(*pval, aux->enc_offset);
  109. }
  110. static const ASN1_ENCODING *asn1_get_const_enc_ptr(const ASN1_VALUE **pval,
  111. const ASN1_ITEM *it)
  112. {
  113. const ASN1_AUX *aux;
  114. if (pval == NULL || *pval == NULL)
  115. return NULL;
  116. aux = it->funcs;
  117. if (aux == NULL || (aux->flags & ASN1_AFLG_ENCODING) == 0)
  118. return NULL;
  119. return offset2ptr(*pval, aux->enc_offset);
  120. }
  121. void ossl_asn1_enc_init(ASN1_VALUE **pval, const ASN1_ITEM *it)
  122. {
  123. ASN1_ENCODING *enc = asn1_get_enc_ptr(pval, it);
  124. if (enc != NULL) {
  125. enc->enc = NULL;
  126. enc->len = 0;
  127. enc->modified = 1;
  128. }
  129. }
  130. void ossl_asn1_enc_free(ASN1_VALUE **pval, const ASN1_ITEM *it)
  131. {
  132. ASN1_ENCODING *enc = asn1_get_enc_ptr(pval, it);
  133. if (enc != NULL) {
  134. OPENSSL_free(enc->enc);
  135. enc->enc = NULL;
  136. enc->len = 0;
  137. enc->modified = 1;
  138. }
  139. }
  140. int ossl_asn1_enc_save(ASN1_VALUE **pval, const unsigned char *in, int inlen,
  141. const ASN1_ITEM *it)
  142. {
  143. ASN1_ENCODING *enc = asn1_get_enc_ptr(pval, it);
  144. if (enc == NULL)
  145. return 1;
  146. OPENSSL_free(enc->enc);
  147. if (inlen <= 0)
  148. return 0;
  149. if ((enc->enc = OPENSSL_malloc(inlen)) == NULL)
  150. return 0;
  151. memcpy(enc->enc, in, inlen);
  152. enc->len = inlen;
  153. enc->modified = 0;
  154. return 1;
  155. }
  156. int ossl_asn1_enc_restore(int *len, unsigned char **out, const ASN1_VALUE **pval,
  157. const ASN1_ITEM *it)
  158. {
  159. const ASN1_ENCODING *enc = asn1_get_const_enc_ptr(pval, it);
  160. if (enc == NULL || enc->modified)
  161. return 0;
  162. if (out) {
  163. memcpy(*out, enc->enc, enc->len);
  164. *out += enc->len;
  165. }
  166. if (len != NULL)
  167. *len = enc->len;
  168. return 1;
  169. }
  170. /* Given an ASN1_TEMPLATE get a pointer to a field */
  171. ASN1_VALUE **ossl_asn1_get_field_ptr(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt)
  172. {
  173. ASN1_VALUE **pvaltmp = offset2ptr(*pval, tt->offset);
  174. /*
  175. * NOTE for BOOLEAN types the field is just a plain int so we can't
  176. * return int **, so settle for (int *).
  177. */
  178. return pvaltmp;
  179. }
  180. /* Given an ASN1_TEMPLATE get a const pointer to a field */
  181. const ASN1_VALUE **ossl_asn1_get_const_field_ptr(const ASN1_VALUE **pval,
  182. const ASN1_TEMPLATE *tt)
  183. {
  184. return offset2ptr(*pval, tt->offset);
  185. }
  186. /*
  187. * Handle ANY DEFINED BY template, find the selector, look up the relevant
  188. * ASN1_TEMPLATE in the table and return it.
  189. */
  190. const ASN1_TEMPLATE *ossl_asn1_do_adb(const ASN1_VALUE *val,
  191. const ASN1_TEMPLATE *tt,
  192. int nullerr)
  193. {
  194. const ASN1_ADB *adb;
  195. const ASN1_ADB_TABLE *atbl;
  196. long selector;
  197. const ASN1_VALUE **sfld;
  198. int i;
  199. if ((tt->flags & ASN1_TFLG_ADB_MASK) == 0)
  200. return tt;
  201. /* Else ANY DEFINED BY ... get the table */
  202. adb = ASN1_ADB_ptr(tt->item);
  203. /* Get the selector field */
  204. sfld = offset2ptr(val, adb->offset);
  205. /* Check if NULL */
  206. if (*sfld == NULL) {
  207. if (adb->null_tt == NULL)
  208. goto err;
  209. return adb->null_tt;
  210. }
  211. /*
  212. * Convert type to a long: NB: don't check for NID_undef here because it
  213. * might be a legitimate value in the table
  214. */
  215. if ((tt->flags & ASN1_TFLG_ADB_OID) != 0)
  216. selector = OBJ_obj2nid((ASN1_OBJECT *)*sfld);
  217. else
  218. selector = ASN1_INTEGER_get((ASN1_INTEGER *)*sfld);
  219. /* Let application callback translate value */
  220. if (adb->adb_cb != NULL && adb->adb_cb(&selector) == 0) {
  221. ERR_raise(ERR_LIB_ASN1, ASN1_R_UNSUPPORTED_ANY_DEFINED_BY_TYPE);
  222. return NULL;
  223. }
  224. /*
  225. * Try to find matching entry in table Maybe should check application
  226. * types first to allow application override? Might also be useful to
  227. * have a flag which indicates table is sorted and we can do a binary
  228. * search. For now stick to a linear search.
  229. */
  230. for (atbl = adb->tbl, i = 0; i < adb->tblcount; i++, atbl++)
  231. if (atbl->value == selector)
  232. return &atbl->tt;
  233. /* FIXME: need to search application table too */
  234. /* No match, return default type */
  235. if (!adb->default_tt)
  236. goto err;
  237. return adb->default_tt;
  238. err:
  239. /* FIXME: should log the value or OID of unsupported type */
  240. if (nullerr)
  241. ERR_raise(ERR_LIB_ASN1, ASN1_R_UNSUPPORTED_ANY_DEFINED_BY_TYPE);
  242. return NULL;
  243. }