tasn_utl.c 7.5 KB

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