Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 
 
 

267 строки
8.3 KiB

  1. /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  2. * All rights reserved.
  3. *
  4. * This package is an SSL implementation written
  5. * by Eric Young (eay@cryptsoft.com).
  6. * The implementation was written so as to conform with Netscapes SSL.
  7. *
  8. * This library is free for commercial and non-commercial use as long as
  9. * the following conditions are aheared to. The following conditions
  10. * apply to all code found in this distribution, be it the RC4, RSA,
  11. * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  12. * included with this distribution is covered by the same copyright terms
  13. * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  14. *
  15. * Copyright remains Eric Young's, and as such any Copyright notices in
  16. * the code are not to be removed.
  17. * If this package is used in a product, Eric Young should be given attribution
  18. * as the author of the parts of the library used.
  19. * This can be in the form of a textual message at program startup or
  20. * in documentation (online or textual) provided with the package.
  21. *
  22. * Redistribution and use in source and binary forms, with or without
  23. * modification, are permitted provided that the following conditions
  24. * are met:
  25. * 1. Redistributions of source code must retain the copyright
  26. * notice, this list of conditions and the following disclaimer.
  27. * 2. Redistributions in binary form must reproduce the above copyright
  28. * notice, this list of conditions and the following disclaimer in the
  29. * documentation and/or other materials provided with the distribution.
  30. * 3. All advertising materials mentioning features or use of this software
  31. * must display the following acknowledgement:
  32. * "This product includes cryptographic software written by
  33. * Eric Young (eay@cryptsoft.com)"
  34. * The word 'cryptographic' can be left out if the rouines from the library
  35. * being used are not cryptographic related :-).
  36. * 4. If you include any Windows specific code (or a derivative thereof) from
  37. * the apps directory (application code) you must include an acknowledgement:
  38. * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  39. *
  40. * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  41. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  42. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  43. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  44. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  45. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  46. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  47. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  48. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  49. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  50. * SUCH DAMAGE.
  51. *
  52. * The licence and distribution terms for any publically available version or
  53. * derivative of this code cannot be changed. i.e. this code cannot simply be
  54. * copied and put under another distribution licence
  55. * [including the GNU Public Licence.] */
  56. #include <openssl/asn1.h>
  57. #include <string.h>
  58. #include <openssl/asn1t.h>
  59. #include <openssl/mem.h>
  60. #include <openssl/obj.h>
  61. #include <openssl/err.h>
  62. #include <openssl/thread.h>
  63. #include "../internal.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 the selector value */
  68. int asn1_get_choice_selector(ASN1_VALUE **pval, const ASN1_ITEM *it) {
  69. int *sel = offset2ptr(*pval, it->utype);
  70. return *sel;
  71. }
  72. /* Given an ASN1_ITEM CHOICE type set the selector value, return old value. */
  73. int asn1_set_choice_selector(ASN1_VALUE **pval, int value,
  74. const ASN1_ITEM *it) {
  75. int *sel, ret;
  76. sel = offset2ptr(*pval, it->utype);
  77. ret = *sel;
  78. *sel = value;
  79. return ret;
  80. }
  81. static CRYPTO_refcount_t *asn1_get_references(ASN1_VALUE **pval,
  82. const ASN1_ITEM *it) {
  83. if (it->itype != ASN1_ITYPE_SEQUENCE &&
  84. it->itype != ASN1_ITYPE_NDEF_SEQUENCE) {
  85. return NULL;
  86. }
  87. const ASN1_AUX *aux = it->funcs;
  88. if (!aux || !(aux->flags & ASN1_AFLG_REFCOUNT)) {
  89. return NULL;
  90. }
  91. return offset2ptr(*pval, aux->ref_offset);
  92. }
  93. void asn1_refcount_set_one(ASN1_VALUE **pval, const ASN1_ITEM *it) {
  94. CRYPTO_refcount_t *references = asn1_get_references(pval, it);
  95. if (references != NULL) {
  96. *references = 1;
  97. }
  98. }
  99. int asn1_refcount_dec_and_test_zero(ASN1_VALUE **pval, const ASN1_ITEM *it) {
  100. CRYPTO_refcount_t *references = asn1_get_references(pval, it);
  101. if (references != NULL) {
  102. return CRYPTO_refcount_dec_and_test_zero(references);
  103. }
  104. return 1;
  105. }
  106. static ASN1_ENCODING *asn1_get_enc_ptr(ASN1_VALUE **pval, const ASN1_ITEM *it) {
  107. const ASN1_AUX *aux;
  108. if (!pval || !*pval) {
  109. return NULL;
  110. }
  111. aux = it->funcs;
  112. if (!aux || !(aux->flags & ASN1_AFLG_ENCODING)) {
  113. return NULL;
  114. }
  115. return offset2ptr(*pval, aux->enc_offset);
  116. }
  117. void asn1_enc_init(ASN1_VALUE **pval, const ASN1_ITEM *it) {
  118. ASN1_ENCODING *enc;
  119. enc = asn1_get_enc_ptr(pval, it);
  120. if (enc) {
  121. enc->enc = NULL;
  122. enc->len = 0;
  123. enc->modified = 1;
  124. }
  125. }
  126. void asn1_enc_free(ASN1_VALUE **pval, const ASN1_ITEM *it) {
  127. ASN1_ENCODING *enc;
  128. enc = asn1_get_enc_ptr(pval, it);
  129. if (enc) {
  130. if (enc->enc) {
  131. OPENSSL_free(enc->enc);
  132. }
  133. enc->enc = NULL;
  134. enc->len = 0;
  135. enc->modified = 1;
  136. }
  137. }
  138. int asn1_enc_save(ASN1_VALUE **pval, const unsigned char *in, int inlen,
  139. const ASN1_ITEM *it) {
  140. ASN1_ENCODING *enc;
  141. enc = asn1_get_enc_ptr(pval, it);
  142. if (!enc) {
  143. return 1;
  144. }
  145. if (enc->enc) {
  146. OPENSSL_free(enc->enc);
  147. }
  148. enc->enc = OPENSSL_malloc(inlen);
  149. if (!enc->enc) {
  150. return 0;
  151. }
  152. memcpy(enc->enc, in, inlen);
  153. enc->len = inlen;
  154. enc->modified = 0;
  155. return 1;
  156. }
  157. int asn1_enc_restore(int *len, unsigned char **out, ASN1_VALUE **pval,
  158. const ASN1_ITEM *it) {
  159. ASN1_ENCODING *enc;
  160. enc = asn1_get_enc_ptr(pval, it);
  161. if (!enc || enc->modified) {
  162. return 0;
  163. }
  164. if (out) {
  165. memcpy(*out, enc->enc, enc->len);
  166. *out += enc->len;
  167. }
  168. if (len) {
  169. *len = enc->len;
  170. }
  171. return 1;
  172. }
  173. /* Given an ASN1_TEMPLATE get a pointer to a field */
  174. ASN1_VALUE **asn1_get_field_ptr(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt) {
  175. ASN1_VALUE **pvaltmp;
  176. if (tt->flags & ASN1_TFLG_COMBINE) {
  177. return pval;
  178. }
  179. pvaltmp = offset2ptr(*pval, tt->offset);
  180. /* NOTE for BOOLEAN types the field is just a plain int so we can't return
  181. * int **, so settle for (int *). */
  182. return pvaltmp;
  183. }
  184. /* Handle ANY DEFINED BY template, find the selector, look up the relevant
  185. * ASN1_TEMPLATE in the table and return it. */
  186. const ASN1_TEMPLATE *asn1_do_adb(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt,
  187. int nullerr) {
  188. const ASN1_ADB *adb;
  189. const ASN1_ADB_TABLE *atbl;
  190. long selector;
  191. ASN1_VALUE **sfld;
  192. int i;
  193. if (!(tt->flags & ASN1_TFLG_ADB_MASK)) {
  194. return tt;
  195. }
  196. /* Else ANY DEFINED BY ... get the table */
  197. adb = ASN1_ADB_ptr(tt->item);
  198. /* Get the selector field */
  199. sfld = offset2ptr(*pval, adb->offset);
  200. /* Check if NULL */
  201. if (*sfld == NULL) {
  202. if (!adb->null_tt) {
  203. goto err;
  204. }
  205. return adb->null_tt;
  206. }
  207. /* Convert type to a long:
  208. * NB: don't check for NID_undef here because it
  209. * might be a legitimate value in the table */
  210. if (tt->flags & ASN1_TFLG_ADB_OID) {
  211. selector = OBJ_obj2nid((ASN1_OBJECT *)*sfld);
  212. } else {
  213. selector = ASN1_INTEGER_get((ASN1_INTEGER *)*sfld);
  214. }
  215. /* Try to find matching entry in table Maybe should check application types
  216. * first to allow application override? Might also be useful to have a flag
  217. * which indicates table is sorted and we can do a binary search. For now
  218. * stick to a linear search. */
  219. for (atbl = adb->tbl, i = 0; i < adb->tblcount; i++, atbl++) {
  220. if (atbl->value == selector) {
  221. return &atbl->tt;
  222. }
  223. }
  224. /* FIXME: need to search application table too */
  225. /* No match, return default type */
  226. if (!adb->default_tt) {
  227. goto err;
  228. }
  229. return adb->default_tt;
  230. err:
  231. /* FIXME: should log the value or OID of unsupported type */
  232. if (nullerr) {
  233. OPENSSL_PUT_ERROR(ASN1, ASN1_R_UNSUPPORTED_ANY_DEFINED_BY_TYPE);
  234. }
  235. return NULL;
  236. }