Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

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