tasn_utl.c 6.6 KB

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