tasn_utl.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. /* tasn_utl.c */
  2. /*
  3. * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL project
  4. * 2000.
  5. */
  6. /* ====================================================================
  7. * Copyright (c) 2000-2004 The OpenSSL Project. All rights reserved.
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions
  11. * are met:
  12. *
  13. * 1. Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions and the following disclaimer.
  15. *
  16. * 2. Redistributions in binary form must reproduce the above copyright
  17. * notice, this list of conditions and the following disclaimer in
  18. * the documentation and/or other materials provided with the
  19. * distribution.
  20. *
  21. * 3. All advertising materials mentioning features or use of this
  22. * software must display the following acknowledgment:
  23. * "This product includes software developed by the OpenSSL Project
  24. * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
  25. *
  26. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  27. * endorse or promote products derived from this software without
  28. * prior written permission. For written permission, please contact
  29. * licensing@OpenSSL.org.
  30. *
  31. * 5. Products derived from this software may not be called "OpenSSL"
  32. * nor may "OpenSSL" appear in their names without prior written
  33. * permission of the OpenSSL Project.
  34. *
  35. * 6. Redistributions of any form whatsoever must retain the following
  36. * acknowledgment:
  37. * "This product includes software developed by the OpenSSL Project
  38. * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
  39. *
  40. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  41. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  42. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  43. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  44. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  45. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  46. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  47. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  48. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  49. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  50. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  51. * OF THE POSSIBILITY OF SUCH DAMAGE.
  52. * ====================================================================
  53. *
  54. * This product includes cryptographic software written by Eric Young
  55. * (eay@cryptsoft.com). This product includes software written by Tim
  56. * Hudson (tjh@cryptsoft.com).
  57. *
  58. */
  59. #include <stddef.h>
  60. #include <string.h>
  61. #include <openssl/asn1.h>
  62. #include <openssl/asn1t.h>
  63. #include <openssl/objects.h>
  64. #include <openssl/err.h>
  65. #include "asn1_locl.h"
  66. /* Utility functions for manipulating fields and offsets */
  67. /* Add 'offset' to 'addr' */
  68. #define offset2ptr(addr, offset) (void *)(((char *) addr) + offset)
  69. /*
  70. * Given an ASN1_ITEM CHOICE type return the selector value
  71. */
  72. int asn1_get_choice_selector(ASN1_VALUE **pval, const ASN1_ITEM *it)
  73. {
  74. int *sel = offset2ptr(*pval, it->utype);
  75. return *sel;
  76. }
  77. /*
  78. * Given an ASN1_ITEM CHOICE type set the selector value, return old value.
  79. */
  80. int asn1_set_choice_selector(ASN1_VALUE **pval, int value,
  81. const ASN1_ITEM *it)
  82. {
  83. int *sel, ret;
  84. sel = offset2ptr(*pval, it->utype);
  85. ret = *sel;
  86. *sel = value;
  87. return ret;
  88. }
  89. /*
  90. * Do reference counting. The value 'op' decides what to do. if it is +1
  91. * then the count is incremented. If op is 0 count is set to 1. If op is -1
  92. * count is decremented and the return value is the current reference count
  93. * or 0 if no reference count exists.
  94. */
  95. int asn1_do_lock(ASN1_VALUE **pval, int op, const ASN1_ITEM *it)
  96. {
  97. const ASN1_AUX *aux;
  98. int *lck, ret;
  99. if ((it->itype != ASN1_ITYPE_SEQUENCE)
  100. && (it->itype != ASN1_ITYPE_NDEF_SEQUENCE))
  101. return 0;
  102. aux = it->funcs;
  103. if (!aux || !(aux->flags & ASN1_AFLG_REFCOUNT))
  104. return 0;
  105. lck = offset2ptr(*pval, aux->ref_offset);
  106. if (op == 0) {
  107. *lck = 1;
  108. return 1;
  109. }
  110. ret = CRYPTO_add(lck, op, aux->ref_lock);
  111. #ifdef REF_PRINT
  112. fprintf(stderr, "%s: Reference Count: %d\n", it->sname, *lck);
  113. #endif
  114. #ifdef REF_CHECK
  115. if (ret < 0)
  116. fprintf(stderr, "%s, bad reference count\n", it->sname);
  117. #endif
  118. return ret;
  119. }
  120. static ASN1_ENCODING *asn1_get_enc_ptr(ASN1_VALUE **pval, const ASN1_ITEM *it)
  121. {
  122. const ASN1_AUX *aux;
  123. if (!pval || !*pval)
  124. return NULL;
  125. aux = it->funcs;
  126. if (!aux || !(aux->flags & ASN1_AFLG_ENCODING))
  127. return NULL;
  128. return offset2ptr(*pval, aux->enc_offset);
  129. }
  130. void asn1_enc_init(ASN1_VALUE **pval, const ASN1_ITEM *it)
  131. {
  132. ASN1_ENCODING *enc;
  133. enc = asn1_get_enc_ptr(pval, it);
  134. if (enc) {
  135. enc->enc = NULL;
  136. enc->len = 0;
  137. enc->modified = 1;
  138. }
  139. }
  140. void asn1_enc_free(ASN1_VALUE **pval, const ASN1_ITEM *it)
  141. {
  142. ASN1_ENCODING *enc;
  143. enc = asn1_get_enc_ptr(pval, it);
  144. if (enc) {
  145. OPENSSL_free(enc->enc);
  146. enc->enc = NULL;
  147. enc->len = 0;
  148. enc->modified = 1;
  149. }
  150. }
  151. int asn1_enc_save(ASN1_VALUE **pval, const unsigned char *in, int inlen,
  152. const ASN1_ITEM *it)
  153. {
  154. ASN1_ENCODING *enc;
  155. enc = asn1_get_enc_ptr(pval, it);
  156. if (!enc)
  157. return 1;
  158. OPENSSL_free(enc->enc);
  159. enc->enc = OPENSSL_malloc(inlen);
  160. if (!enc->enc)
  161. return 0;
  162. memcpy(enc->enc, in, inlen);
  163. enc->len = inlen;
  164. enc->modified = 0;
  165. return 1;
  166. }
  167. int asn1_enc_restore(int *len, unsigned char **out, ASN1_VALUE **pval,
  168. const ASN1_ITEM *it)
  169. {
  170. ASN1_ENCODING *enc;
  171. enc = asn1_get_enc_ptr(pval, it);
  172. if (!enc || enc->modified)
  173. return 0;
  174. if (out) {
  175. memcpy(*out, enc->enc, enc->len);
  176. *out += enc->len;
  177. }
  178. if (len)
  179. *len = enc->len;
  180. return 1;
  181. }
  182. /* Given an ASN1_TEMPLATE get a pointer to a field */
  183. ASN1_VALUE **asn1_get_field_ptr(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt)
  184. {
  185. ASN1_VALUE **pvaltmp;
  186. pvaltmp = offset2ptr(*pval, tt->offset);
  187. /*
  188. * NOTE for BOOLEAN types the field is just a plain int so we can't
  189. * return int **, so settle for (int *).
  190. */
  191. return pvaltmp;
  192. }
  193. /*
  194. * Handle ANY DEFINED BY template, find the selector, look up the relevant
  195. * ASN1_TEMPLATE in the table and return it.
  196. */
  197. const ASN1_TEMPLATE *asn1_do_adb(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt,
  198. int nullerr)
  199. {
  200. const ASN1_ADB *adb;
  201. const ASN1_ADB_TABLE *atbl;
  202. long selector;
  203. ASN1_VALUE **sfld;
  204. int i;
  205. if (!(tt->flags & ASN1_TFLG_ADB_MASK))
  206. return tt;
  207. /* Else ANY DEFINED BY ... get the table */
  208. adb = ASN1_ADB_ptr(tt->item);
  209. /* Get the selector field */
  210. sfld = offset2ptr(*pval, adb->offset);
  211. /* Check if NULL */
  212. if (!sfld) {
  213. if (!adb->null_tt)
  214. goto err;
  215. return adb->null_tt;
  216. }
  217. /*
  218. * Convert type to a long: NB: don't check for NID_undef here because it
  219. * might be a legitimate value in the table
  220. */
  221. if (tt->flags & ASN1_TFLG_ADB_OID)
  222. selector = OBJ_obj2nid((ASN1_OBJECT *)*sfld);
  223. else
  224. selector = ASN1_INTEGER_get((ASN1_INTEGER *)*sfld);
  225. /*
  226. * Try to find matching entry in table Maybe should check application
  227. * types first to allow application override? Might also be useful to
  228. * have a flag which indicates table is sorted and we can do a binary
  229. * search. For now stick to a linear search.
  230. */
  231. for (atbl = adb->tbl, i = 0; i < adb->tblcount; i++, atbl++)
  232. if (atbl->value == selector)
  233. return &atbl->tt;
  234. /* FIXME: need to search application table too */
  235. /* No match, return default type */
  236. if (!adb->default_tt)
  237. goto err;
  238. return adb->default_tt;
  239. err:
  240. /* FIXME: should log the value or OID of unsupported type */
  241. if (nullerr)
  242. ASN1err(ASN1_F_ASN1_DO_ADB, ASN1_R_UNSUPPORTED_ANY_DEFINED_BY_TYPE);
  243. return NULL;
  244. }