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.
 
 
 
 
 
 

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