You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

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