2
0

tasn_utl.c 6.5 KB

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