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

259 строки
8.1 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 <openssl/asn1t.h>
  58. #include <openssl/mem.h>
  59. #include <openssl/obj.h>
  60. #include <openssl/err.h>
  61. /* Utility functions for manipulating fields and offsets */
  62. /* Add 'offset' to 'addr' */
  63. #define offset2ptr(addr, offset) (void *)(((char *) addr) + offset)
  64. /* Given an ASN1_ITEM CHOICE type return the selector value */
  65. int asn1_get_choice_selector(ASN1_VALUE **pval, const ASN1_ITEM *it) {
  66. int *sel = offset2ptr(*pval, it->utype);
  67. return *sel;
  68. }
  69. /* Given an ASN1_ITEM CHOICE type set the selector value, return old value. */
  70. int asn1_set_choice_selector(ASN1_VALUE **pval, int value,
  71. const ASN1_ITEM *it) {
  72. int *sel, ret;
  73. sel = offset2ptr(*pval, it->utype);
  74. ret = *sel;
  75. *sel = value;
  76. return ret;
  77. }
  78. /* Do reference counting. The value 'op' decides what to do. if it is +1 then
  79. * the count is incremented. If op is 0 count is set to 1. If op is -1 count is
  80. * decremented and the return value is the current refrence count or 0 if no
  81. * reference count exists. */
  82. int asn1_do_lock(ASN1_VALUE **pval, int op, const ASN1_ITEM *it) {
  83. const ASN1_AUX *aux;
  84. int *lck, ret;
  85. if (it->itype != ASN1_ITYPE_SEQUENCE &&
  86. it->itype != ASN1_ITYPE_NDEF_SEQUENCE) {
  87. return 0;
  88. }
  89. aux = it->funcs;
  90. if (!aux || !(aux->flags & ASN1_AFLG_REFCOUNT)) {
  91. return 0;
  92. }
  93. lck = offset2ptr(*pval, aux->ref_offset);
  94. if (op == 0) {
  95. *lck = 1;
  96. return 1;
  97. }
  98. ret = CRYPTO_add(lck, op, aux->ref_lock);
  99. return ret;
  100. }
  101. static ASN1_ENCODING *asn1_get_enc_ptr(ASN1_VALUE **pval, const ASN1_ITEM *it) {
  102. const ASN1_AUX *aux;
  103. if (!pval || !*pval) {
  104. return NULL;
  105. }
  106. aux = it->funcs;
  107. if (!aux || !(aux->flags & ASN1_AFLG_ENCODING)) {
  108. return NULL;
  109. }
  110. return offset2ptr(*pval, aux->enc_offset);
  111. }
  112. void asn1_enc_init(ASN1_VALUE **pval, const ASN1_ITEM *it) {
  113. ASN1_ENCODING *enc;
  114. enc = asn1_get_enc_ptr(pval, it);
  115. if (enc) {
  116. enc->enc = NULL;
  117. enc->len = 0;
  118. enc->modified = 1;
  119. }
  120. }
  121. void asn1_enc_free(ASN1_VALUE **pval, const ASN1_ITEM *it) {
  122. ASN1_ENCODING *enc;
  123. enc = asn1_get_enc_ptr(pval, it);
  124. if (enc) {
  125. if (enc->enc) {
  126. OPENSSL_free(enc->enc);
  127. }
  128. enc->enc = NULL;
  129. enc->len = 0;
  130. enc->modified = 1;
  131. }
  132. }
  133. int asn1_enc_save(ASN1_VALUE **pval, const unsigned char *in, int inlen,
  134. const ASN1_ITEM *it) {
  135. ASN1_ENCODING *enc;
  136. enc = asn1_get_enc_ptr(pval, it);
  137. if (!enc) {
  138. return 1;
  139. }
  140. if (enc->enc) {
  141. OPENSSL_free(enc->enc);
  142. }
  143. enc->enc = OPENSSL_malloc(inlen);
  144. if (!enc->enc) {
  145. return 0;
  146. }
  147. memcpy(enc->enc, in, inlen);
  148. enc->len = inlen;
  149. enc->modified = 0;
  150. return 1;
  151. }
  152. int asn1_enc_restore(int *len, unsigned char **out, ASN1_VALUE **pval,
  153. const ASN1_ITEM *it) {
  154. ASN1_ENCODING *enc;
  155. enc = asn1_get_enc_ptr(pval, it);
  156. if (!enc || enc->modified) {
  157. return 0;
  158. }
  159. if (out) {
  160. memcpy(*out, enc->enc, enc->len);
  161. *out += enc->len;
  162. }
  163. if (len) {
  164. *len = enc->len;
  165. }
  166. return 1;
  167. }
  168. /* Given an ASN1_TEMPLATE get a pointer to a field */
  169. ASN1_VALUE **asn1_get_field_ptr(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt) {
  170. ASN1_VALUE **pvaltmp;
  171. if (tt->flags & ASN1_TFLG_COMBINE) {
  172. return pval;
  173. }
  174. pvaltmp = offset2ptr(*pval, tt->offset);
  175. /* NOTE for BOOLEAN types the field is just a plain int so we can't return
  176. * int **, so settle for (int *). */
  177. return pvaltmp;
  178. }
  179. /* Handle ANY DEFINED BY template, find the selector, look up the relevant
  180. * ASN1_TEMPLATE in the table and return it. */
  181. const ASN1_TEMPLATE *asn1_do_adb(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt,
  182. int nullerr) {
  183. const ASN1_ADB *adb;
  184. const ASN1_ADB_TABLE *atbl;
  185. long selector;
  186. ASN1_VALUE **sfld;
  187. int i;
  188. if (!(tt->flags & ASN1_TFLG_ADB_MASK)) {
  189. return tt;
  190. }
  191. /* Else ANY DEFINED BY ... get the table */
  192. adb = ASN1_ADB_ptr(tt->item);
  193. /* Get the selector field */
  194. sfld = offset2ptr(*pval, adb->offset);
  195. /* Check if NULL */
  196. if (!sfld) {
  197. if (!adb->null_tt) {
  198. goto err;
  199. }
  200. return adb->null_tt;
  201. }
  202. /* Convert type to a long:
  203. * NB: don't check for NID_undef here because it
  204. * might be a legitimate value in the table */
  205. if (tt->flags & ASN1_TFLG_ADB_OID) {
  206. selector = OBJ_obj2nid((ASN1_OBJECT *)*sfld);
  207. } else {
  208. selector = ASN1_INTEGER_get((ASN1_INTEGER *)*sfld);
  209. }
  210. /* Try to find matching entry in table Maybe should check application types
  211. * first to allow application override? Might also be useful to have a flag
  212. * which indicates table is sorted and we can do a binary search. For now
  213. * stick to a linear search. */
  214. for (atbl = adb->tbl, i = 0; i < adb->tblcount; i++, atbl++) {
  215. if (atbl->value == selector) {
  216. return &atbl->tt;
  217. }
  218. }
  219. /* FIXME: need to search application table too */
  220. /* No match, return default type */
  221. if (!adb->default_tt) {
  222. goto err;
  223. }
  224. return adb->default_tt;
  225. err:
  226. /* FIXME: should log the value or OID of unsupported type */
  227. if (nullerr) {
  228. OPENSSL_PUT_ERROR(ASN1, asn1_do_adb,
  229. ASN1_R_UNSUPPORTED_ANY_DEFINED_BY_TYPE);
  230. }
  231. return NULL;
  232. }