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.
 
 
 
 
 
 

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