No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 
 
 
 

194 líneas
6.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 <openssl/err.h>
  58. #include <openssl/mem.h>
  59. #if 0
  60. #ifndef NO_ASN1_OLD
  61. /* ASN1 packing and unpacking functions */
  62. /* Turn an ASN1 encoded SEQUENCE OF into a STACK of structures */
  63. STACK_OF(OPENSSL_BLOCK) *ASN1_seq_unpack(const unsigned char *buf, int len,
  64. d2i_of_void *d2i, void (*free_func)(OPENSSL_BLOCK))
  65. {
  66. STACK_OF(OPENSSL_BLOCK) *sk;
  67. const unsigned char *pbuf;
  68. pbuf = buf;
  69. if (!(sk = d2i_ASN1_SET(NULL, &pbuf, len, d2i, free_func,
  70. V_ASN1_SEQUENCE, V_ASN1_UNIVERSAL)))
  71. OPENSSL_PUT_ERROR(ASN1, XXX, ASN1_R_DECODE_ERROR);
  72. return sk;
  73. }
  74. /* Turn a STACK structures into an ASN1 encoded SEQUENCE OF structure in a
  75. * OPENSSL_malloc'ed buffer
  76. */
  77. unsigned char *ASN1_seq_pack(STACK_OF(OPENSSL_BLOCK) *safes, i2d_of_void *i2d,
  78. unsigned char **buf, int *len)
  79. {
  80. int safelen;
  81. unsigned char *safe, *p;
  82. if (!(safelen = i2d_ASN1_SET(safes, NULL, i2d, V_ASN1_SEQUENCE,
  83. V_ASN1_UNIVERSAL, IS_SEQUENCE))) {
  84. OPENSSL_PUT_ERROR(ASN1, XXX, ASN1_R_ENCODE_ERROR);
  85. return NULL;
  86. }
  87. if (!(safe = OPENSSL_malloc (safelen))) {
  88. OPENSSL_PUT_ERROR(ASN1, XXX, ERR_R_MALLOC_FAILURE);
  89. return NULL;
  90. }
  91. p = safe;
  92. i2d_ASN1_SET(safes, &p, i2d, V_ASN1_SEQUENCE, V_ASN1_UNIVERSAL,
  93. IS_SEQUENCE);
  94. if (len) *len = safelen;
  95. if (buf) *buf = safe;
  96. return safe;
  97. }
  98. /* Extract an ASN1 object from an ASN1_STRING */
  99. void *ASN1_unpack_string(ASN1_STRING *oct, d2i_of_void *d2i)
  100. {
  101. const unsigned char *p;
  102. char *ret;
  103. p = oct->data;
  104. if(!(ret = d2i(NULL, &p, oct->length)))
  105. OPENSSL_PUT_ERROR(ASN1, XXX, ASN1_R_DECODE_ERROR);
  106. return ret;
  107. }
  108. /* Pack an ASN1 object into an ASN1_STRING */
  109. ASN1_STRING *ASN1_pack_string(void *obj, i2d_of_void *i2d, ASN1_STRING **oct)
  110. {
  111. unsigned char *p;
  112. ASN1_STRING *octmp;
  113. if (!oct || !*oct) {
  114. if (!(octmp = ASN1_STRING_new ())) {
  115. OPENSSL_PUT_ERROR(ASN1, XXX, ERR_R_MALLOC_FAILURE);
  116. return NULL;
  117. }
  118. if (oct) *oct = octmp;
  119. } else octmp = *oct;
  120. if (!(octmp->length = i2d(obj, NULL))) {
  121. OPENSSL_PUT_ERROR(ASN1, XXX, ASN1_R_ENCODE_ERROR);
  122. return NULL;
  123. }
  124. if (!(p = OPENSSL_malloc (octmp->length))) {
  125. OPENSSL_PUT_ERROR(ASN1, XXX, ERR_R_MALLOC_FAILURE);
  126. return NULL;
  127. }
  128. octmp->data = p;
  129. i2d (obj, &p);
  130. return octmp;
  131. }
  132. #endif
  133. #endif
  134. /* ASN1_ITEM versions of the above */
  135. ASN1_STRING *ASN1_item_pack(void *obj, const ASN1_ITEM *it, ASN1_STRING **oct)
  136. {
  137. ASN1_STRING *octmp;
  138. if (!oct || !*oct) {
  139. if (!(octmp = ASN1_STRING_new ())) {
  140. OPENSSL_PUT_ERROR(ASN1, ASN1_item_pack, ERR_R_MALLOC_FAILURE);
  141. return NULL;
  142. }
  143. if (oct) *oct = octmp;
  144. } else octmp = *oct;
  145. if(octmp->data) {
  146. OPENSSL_free(octmp->data);
  147. octmp->data = NULL;
  148. }
  149. if (!(octmp->length = ASN1_item_i2d(obj, &octmp->data, it))) {
  150. OPENSSL_PUT_ERROR(ASN1, ASN1_item_pack, ASN1_R_ENCODE_ERROR);
  151. return NULL;
  152. }
  153. if (!octmp->data) {
  154. OPENSSL_PUT_ERROR(ASN1, ASN1_item_pack, ERR_R_MALLOC_FAILURE);
  155. return NULL;
  156. }
  157. return octmp;
  158. }
  159. /* Extract an ASN1 object from an ASN1_STRING */
  160. void *ASN1_item_unpack(ASN1_STRING *oct, const ASN1_ITEM *it)
  161. {
  162. const unsigned char *p;
  163. void *ret;
  164. p = oct->data;
  165. if(!(ret = ASN1_item_d2i(NULL, &p, oct->length, it)))
  166. OPENSSL_PUT_ERROR(ASN1, ASN1_item_unpack, ASN1_R_DECODE_ERROR);
  167. return ret;
  168. }