Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 
 
 

309 linhas
8.9 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/buf.h>
  59. #include <openssl/err.h>
  60. #include <openssl/mem.h>
  61. static int asn1_collate_primitive(ASN1_STRING *a, ASN1_const_CTX *c);
  62. /*
  63. * type is a 'bitmap' of acceptable string types.
  64. */
  65. ASN1_STRING *d2i_ASN1_type_bytes(ASN1_STRING **a, const unsigned char **pp,
  66. long length, int type)
  67. {
  68. ASN1_STRING *ret = NULL;
  69. const unsigned char *p;
  70. unsigned char *s;
  71. long len;
  72. int inf, tag, xclass;
  73. int i = 0;
  74. p = *pp;
  75. inf = ASN1_get_object(&p, &len, &tag, &xclass, length);
  76. if (inf & 0x80)
  77. goto err;
  78. if (tag >= 32) {
  79. i = ASN1_R_TAG_VALUE_TOO_HIGH;
  80. goto err;
  81. }
  82. if (!(ASN1_tag2bit(tag) & type)) {
  83. i = ASN1_R_WRONG_TYPE;
  84. goto err;
  85. }
  86. /* If a bit-string, exit early */
  87. if (tag == V_ASN1_BIT_STRING)
  88. return (d2i_ASN1_BIT_STRING(a, pp, length));
  89. if ((a == NULL) || ((*a) == NULL)) {
  90. if ((ret = ASN1_STRING_new()) == NULL)
  91. return (NULL);
  92. } else
  93. ret = (*a);
  94. if (len != 0) {
  95. s = (unsigned char *)OPENSSL_malloc((int)len + 1);
  96. if (s == NULL) {
  97. i = ERR_R_MALLOC_FAILURE;
  98. goto err;
  99. }
  100. memcpy(s, p, (int)len);
  101. s[len] = '\0';
  102. p += len;
  103. } else
  104. s = NULL;
  105. if (ret->data != NULL)
  106. OPENSSL_free(ret->data);
  107. ret->length = (int)len;
  108. ret->data = s;
  109. ret->type = tag;
  110. if (a != NULL)
  111. (*a) = ret;
  112. *pp = p;
  113. return (ret);
  114. err:
  115. OPENSSL_PUT_ERROR(ASN1, i);
  116. if ((ret != NULL) && ((a == NULL) || (*a != ret)))
  117. ASN1_STRING_free(ret);
  118. return (NULL);
  119. }
  120. int i2d_ASN1_bytes(ASN1_STRING *a, unsigned char **pp, int tag, int xclass)
  121. {
  122. int ret, r, constructed;
  123. unsigned char *p;
  124. if (a == NULL)
  125. return (0);
  126. if (tag == V_ASN1_BIT_STRING)
  127. return (i2d_ASN1_BIT_STRING(a, pp));
  128. ret = a->length;
  129. r = ASN1_object_size(0, ret, tag);
  130. if (pp == NULL)
  131. return (r);
  132. p = *pp;
  133. if ((tag == V_ASN1_SEQUENCE) || (tag == V_ASN1_SET))
  134. constructed = 1;
  135. else
  136. constructed = 0;
  137. ASN1_put_object(&p, constructed, ret, tag, xclass);
  138. memcpy(p, a->data, a->length);
  139. p += a->length;
  140. *pp = p;
  141. return (r);
  142. }
  143. ASN1_STRING *d2i_ASN1_bytes(ASN1_STRING **a, const unsigned char **pp,
  144. long length, int Ptag, int Pclass)
  145. {
  146. ASN1_STRING *ret = NULL;
  147. const unsigned char *p;
  148. unsigned char *s;
  149. long len;
  150. int inf, tag, xclass;
  151. int i = 0;
  152. if ((a == NULL) || ((*a) == NULL)) {
  153. if ((ret = ASN1_STRING_new()) == NULL)
  154. return (NULL);
  155. } else
  156. ret = (*a);
  157. p = *pp;
  158. inf = ASN1_get_object(&p, &len, &tag, &xclass, length);
  159. if (inf & 0x80) {
  160. i = ASN1_R_BAD_OBJECT_HEADER;
  161. goto err;
  162. }
  163. if (tag != Ptag) {
  164. i = ASN1_R_WRONG_TAG;
  165. goto err;
  166. }
  167. if (inf & V_ASN1_CONSTRUCTED) {
  168. ASN1_const_CTX c;
  169. c.pp = pp;
  170. c.p = p;
  171. c.inf = inf;
  172. c.slen = len;
  173. c.tag = Ptag;
  174. c.xclass = Pclass;
  175. c.max = (length == 0) ? 0 : (p + length);
  176. if (!asn1_collate_primitive(ret, &c))
  177. goto err;
  178. else {
  179. p = c.p;
  180. }
  181. } else {
  182. if (len != 0) {
  183. if ((ret->length < len) || (ret->data == NULL)) {
  184. s = (unsigned char *)OPENSSL_malloc((int)len + 1);
  185. if (s == NULL) {
  186. i = ERR_R_MALLOC_FAILURE;
  187. goto err;
  188. }
  189. if (ret->data != NULL)
  190. OPENSSL_free(ret->data);
  191. } else
  192. s = ret->data;
  193. memcpy(s, p, (int)len);
  194. s[len] = '\0';
  195. p += len;
  196. } else {
  197. s = NULL;
  198. if (ret->data != NULL)
  199. OPENSSL_free(ret->data);
  200. }
  201. ret->length = (int)len;
  202. ret->data = s;
  203. ret->type = Ptag;
  204. }
  205. if (a != NULL)
  206. (*a) = ret;
  207. *pp = p;
  208. return (ret);
  209. err:
  210. if ((ret != NULL) && ((a == NULL) || (*a != ret)))
  211. ASN1_STRING_free(ret);
  212. OPENSSL_PUT_ERROR(ASN1, i);
  213. return (NULL);
  214. }
  215. /*
  216. * We are about to parse 0..n d2i_ASN1_bytes objects, we are to collapse them
  217. * into the one structure that is then returned
  218. */
  219. /*
  220. * There have been a few bug fixes for this function from Paul Keogh
  221. * <paul.keogh@sse.ie>, many thanks to him
  222. */
  223. static int asn1_collate_primitive(ASN1_STRING *a, ASN1_const_CTX *c)
  224. {
  225. ASN1_STRING *os = NULL;
  226. BUF_MEM b;
  227. int num;
  228. b.length = 0;
  229. b.max = 0;
  230. b.data = NULL;
  231. if (a == NULL) {
  232. c->error = ERR_R_PASSED_NULL_PARAMETER;
  233. goto err;
  234. }
  235. num = 0;
  236. for (;;) {
  237. if (c->inf & 1) {
  238. c->eos = ASN1_const_check_infinite_end(&c->p,
  239. (long)(c->max - c->p));
  240. if (c->eos)
  241. break;
  242. } else {
  243. if (c->slen <= 0)
  244. break;
  245. }
  246. c->q = c->p;
  247. if (d2i_ASN1_bytes(&os, &c->p, c->max - c->p, c->tag, c->xclass)
  248. == NULL) {
  249. c->error = ERR_R_ASN1_LIB;
  250. goto err;
  251. }
  252. if (!BUF_MEM_grow_clean(&b, num + os->length)) {
  253. c->error = ERR_R_BUF_LIB;
  254. goto err;
  255. }
  256. memcpy(&(b.data[num]), os->data, os->length);
  257. if (!(c->inf & 1))
  258. c->slen -= (c->p - c->q);
  259. num += os->length;
  260. }
  261. if (!asn1_const_Finish(c))
  262. goto err;
  263. a->length = num;
  264. if (a->data != NULL)
  265. OPENSSL_free(a->data);
  266. a->data = (unsigned char *)b.data;
  267. if (os != NULL)
  268. ASN1_STRING_free(os);
  269. return (1);
  270. err:
  271. OPENSSL_PUT_ERROR(ASN1, c->error);
  272. if (os != NULL)
  273. ASN1_STRING_free(os);
  274. if (b.data != NULL)
  275. OPENSSL_free(b.data);
  276. return (0);
  277. }