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.
 
 
 
 
 
 

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