tasn_utl.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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 <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, lock is initialised and count is 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_REF_COUNT *lck;
  60. CRYPTO_RWLOCK **lock;
  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. lck = offset2ptr(*pval, aux->ref_offset);
  69. lock = offset2ptr(*pval, aux->ref_lock);
  70. switch (op) {
  71. case 0:
  72. *lck = ret = 1;
  73. *lock = CRYPTO_THREAD_lock_new();
  74. if (*lock == NULL) {
  75. ERR_raise(ERR_LIB_ASN1, ERR_R_CRYPTO_LIB);
  76. return -1;
  77. }
  78. break;
  79. case 1:
  80. if (!CRYPTO_UP_REF(lck, &ret, *lock))
  81. return -1;
  82. break;
  83. case -1:
  84. if (!CRYPTO_DOWN_REF(lck, &ret, *lock))
  85. return -1; /* failed */
  86. REF_PRINT_EX(it->sname, ret, (void *)it);
  87. REF_ASSERT_ISNT(ret < 0);
  88. if (ret == 0) {
  89. CRYPTO_THREAD_lock_free(*lock);
  90. *lock = NULL;
  91. }
  92. break;
  93. }
  94. return ret;
  95. }
  96. static ASN1_ENCODING *asn1_get_enc_ptr(ASN1_VALUE **pval, const ASN1_ITEM *it)
  97. {
  98. const ASN1_AUX *aux;
  99. if (pval == NULL || *pval == NULL)
  100. return NULL;
  101. aux = it->funcs;
  102. if (aux == NULL || (aux->flags & ASN1_AFLG_ENCODING) == 0)
  103. return NULL;
  104. return offset2ptr(*pval, aux->enc_offset);
  105. }
  106. static const ASN1_ENCODING *asn1_get_const_enc_ptr(const ASN1_VALUE **pval,
  107. const ASN1_ITEM *it)
  108. {
  109. const ASN1_AUX *aux;
  110. if (pval == NULL || *pval == NULL)
  111. return NULL;
  112. aux = it->funcs;
  113. if (aux == NULL || (aux->flags & ASN1_AFLG_ENCODING) == 0)
  114. return NULL;
  115. return offset2ptr(*pval, aux->enc_offset);
  116. }
  117. void ossl_asn1_enc_init(ASN1_VALUE **pval, const ASN1_ITEM *it)
  118. {
  119. ASN1_ENCODING *enc = asn1_get_enc_ptr(pval, it);
  120. if (enc != NULL) {
  121. enc->enc = NULL;
  122. enc->len = 0;
  123. enc->modified = 1;
  124. }
  125. }
  126. void ossl_asn1_enc_free(ASN1_VALUE **pval, const ASN1_ITEM *it)
  127. {
  128. ASN1_ENCODING *enc = asn1_get_enc_ptr(pval, it);
  129. if (enc != NULL) {
  130. OPENSSL_free(enc->enc);
  131. enc->enc = NULL;
  132. enc->len = 0;
  133. enc->modified = 1;
  134. }
  135. }
  136. int ossl_asn1_enc_save(ASN1_VALUE **pval, const unsigned char *in, int inlen,
  137. const ASN1_ITEM *it)
  138. {
  139. ASN1_ENCODING *enc = asn1_get_enc_ptr(pval, it);
  140. if (enc == NULL)
  141. return 1;
  142. OPENSSL_free(enc->enc);
  143. if (inlen <= 0)
  144. return 0;
  145. if ((enc->enc = OPENSSL_malloc(inlen)) == NULL)
  146. return 0;
  147. memcpy(enc->enc, in, inlen);
  148. enc->len = inlen;
  149. enc->modified = 0;
  150. return 1;
  151. }
  152. int ossl_asn1_enc_restore(int *len, unsigned char **out, const ASN1_VALUE **pval,
  153. const ASN1_ITEM *it)
  154. {
  155. const ASN1_ENCODING *enc = asn1_get_const_enc_ptr(pval, it);
  156. if (enc == NULL || enc->modified)
  157. return 0;
  158. if (out) {
  159. memcpy(*out, enc->enc, enc->len);
  160. *out += enc->len;
  161. }
  162. if (len != NULL)
  163. *len = enc->len;
  164. return 1;
  165. }
  166. /* Given an ASN1_TEMPLATE get a pointer to a field */
  167. ASN1_VALUE **ossl_asn1_get_field_ptr(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt)
  168. {
  169. ASN1_VALUE **pvaltmp = offset2ptr(*pval, tt->offset);
  170. /*
  171. * NOTE for BOOLEAN types the field is just a plain int so we can't
  172. * return int **, so settle for (int *).
  173. */
  174. return pvaltmp;
  175. }
  176. /* Given an ASN1_TEMPLATE get a const pointer to a field */
  177. const ASN1_VALUE **ossl_asn1_get_const_field_ptr(const ASN1_VALUE **pval,
  178. const ASN1_TEMPLATE *tt)
  179. {
  180. return offset2ptr(*pval, tt->offset);
  181. }
  182. /*
  183. * Handle ANY DEFINED BY template, find the selector, look up the relevant
  184. * ASN1_TEMPLATE in the table and return it.
  185. */
  186. const ASN1_TEMPLATE *ossl_asn1_do_adb(const ASN1_VALUE *val,
  187. const ASN1_TEMPLATE *tt,
  188. int nullerr)
  189. {
  190. const ASN1_ADB *adb;
  191. const ASN1_ADB_TABLE *atbl;
  192. long selector;
  193. const ASN1_VALUE **sfld;
  194. int i;
  195. if ((tt->flags & ASN1_TFLG_ADB_MASK) == 0)
  196. return tt;
  197. /* Else ANY DEFINED BY ... get the table */
  198. adb = ASN1_ADB_ptr(tt->item);
  199. /* Get the selector field */
  200. sfld = offset2ptr(val, adb->offset);
  201. /* Check if NULL */
  202. if (*sfld == NULL) {
  203. if (adb->null_tt == NULL)
  204. goto err;
  205. return adb->null_tt;
  206. }
  207. /*
  208. * Convert type to a long: NB: don't check for NID_undef here because it
  209. * might be a legitimate value in the table
  210. */
  211. if ((tt->flags & ASN1_TFLG_ADB_OID) != 0)
  212. selector = OBJ_obj2nid((ASN1_OBJECT *)*sfld);
  213. else
  214. selector = ASN1_INTEGER_get((ASN1_INTEGER *)*sfld);
  215. /* Let application callback translate value */
  216. if (adb->adb_cb != NULL && adb->adb_cb(&selector) == 0) {
  217. ERR_raise(ERR_LIB_ASN1, ASN1_R_UNSUPPORTED_ANY_DEFINED_BY_TYPE);
  218. return NULL;
  219. }
  220. /*
  221. * Try to find matching entry in table Maybe should check application
  222. * types first to allow application override? Might also be useful to
  223. * have a flag which indicates table is sorted and we can do a binary
  224. * search. For now stick to a linear search.
  225. */
  226. for (atbl = adb->tbl, i = 0; i < adb->tblcount; i++, atbl++)
  227. if (atbl->value == selector)
  228. return &atbl->tt;
  229. /* FIXME: need to search application table too */
  230. /* No match, return default type */
  231. if (!adb->default_tt)
  232. goto err;
  233. return adb->default_tt;
  234. err:
  235. /* FIXME: should log the value or OID of unsupported type */
  236. if (nullerr)
  237. ERR_raise(ERR_LIB_ASN1, ASN1_R_UNSUPPORTED_ANY_DEFINED_BY_TYPE);
  238. return NULL;
  239. }