2
0

tasn_utl.c 7.6 KB

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