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.
 
 
 
 
 
 

489 lines
11 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 <limits.h>
  58. #include <string.h>
  59. #include <openssl/asn1_mac.h>
  60. #include <openssl/err.h>
  61. #include <openssl/mem.h>
  62. OPENSSL_DECLARE_ERROR_REASON(ASN1, MALLOC_FAILURE);
  63. static int asn1_get_length(const unsigned char **pp,int *inf,long *rl,int max);
  64. static void asn1_put_length(unsigned char **pp, int length);
  65. static int _asn1_check_infinite_end(const unsigned char **p, long len)
  66. {
  67. /* If there is 0 or 1 byte left, the length check should pick
  68. * things up */
  69. if (len <= 0)
  70. return(1);
  71. else if ((len >= 2) && ((*p)[0] == 0) && ((*p)[1] == 0))
  72. {
  73. (*p)+=2;
  74. return(1);
  75. }
  76. return(0);
  77. }
  78. int ASN1_check_infinite_end(unsigned char **p, long len)
  79. {
  80. return _asn1_check_infinite_end((const unsigned char **)p, len);
  81. }
  82. int ASN1_const_check_infinite_end(const unsigned char **p, long len)
  83. {
  84. return _asn1_check_infinite_end(p, len);
  85. }
  86. int ASN1_get_object(const unsigned char **pp, long *plength, int *ptag,
  87. int *pclass, long omax)
  88. {
  89. int i,ret;
  90. long l;
  91. const unsigned char *p= *pp;
  92. int tag,xclass,inf;
  93. long max=omax;
  94. if (!max) goto err;
  95. ret=(*p&V_ASN1_CONSTRUCTED);
  96. xclass=(*p&V_ASN1_PRIVATE);
  97. i= *p&V_ASN1_PRIMITIVE_TAG;
  98. if (i == V_ASN1_PRIMITIVE_TAG)
  99. { /* high-tag */
  100. p++;
  101. if (--max == 0) goto err;
  102. l=0;
  103. while (*p&0x80)
  104. {
  105. l<<=7L;
  106. l|= *(p++)&0x7f;
  107. if (--max == 0) goto err;
  108. if (l > (INT_MAX >> 7L)) goto err;
  109. }
  110. l<<=7L;
  111. l|= *(p++)&0x7f;
  112. tag=(int)l;
  113. if (--max == 0) goto err;
  114. }
  115. else
  116. {
  117. tag=i;
  118. p++;
  119. if (--max == 0) goto err;
  120. }
  121. *ptag=tag;
  122. *pclass=xclass;
  123. if (!asn1_get_length(&p,&inf,plength,(int)max)) goto err;
  124. if (inf && !(ret & V_ASN1_CONSTRUCTED))
  125. goto err;
  126. #if 0
  127. fprintf(stderr,"p=%d + *plength=%ld > omax=%ld + *pp=%d (%d > %d)\n",
  128. (int)p,*plength,omax,(int)*pp,(int)(p+ *plength),
  129. (int)(omax+ *pp));
  130. #endif
  131. if (*plength > (omax - (p - *pp)))
  132. {
  133. OPENSSL_PUT_ERROR(ASN1, ASN1_get_object, ASN1_R_TOO_LONG);
  134. /* Set this so that even if things are not long enough
  135. * the values are set correctly */
  136. ret|=0x80;
  137. }
  138. *pp=p;
  139. return(ret|inf);
  140. err:
  141. OPENSSL_PUT_ERROR(ASN1, ASN1_get_object, ASN1_R_HEADER_TOO_LONG);
  142. return(0x80);
  143. }
  144. static int asn1_get_length(const unsigned char **pp, int *inf, long *rl, int max)
  145. {
  146. const unsigned char *p= *pp;
  147. unsigned long ret=0;
  148. unsigned int i;
  149. if (max-- < 1) return(0);
  150. if (*p == 0x80)
  151. {
  152. *inf=1;
  153. ret=0;
  154. p++;
  155. }
  156. else
  157. {
  158. *inf=0;
  159. i= *p&0x7f;
  160. if (*(p++) & 0x80)
  161. {
  162. if (i > sizeof(long))
  163. return 0;
  164. if (max-- == 0) return(0);
  165. while (i-- > 0)
  166. {
  167. ret<<=8L;
  168. ret|= *(p++);
  169. if (max-- == 0) return(0);
  170. }
  171. }
  172. else
  173. ret=i;
  174. }
  175. if (ret > LONG_MAX)
  176. return 0;
  177. *pp=p;
  178. *rl=(long)ret;
  179. return(1);
  180. }
  181. /* class 0 is constructed
  182. * constructed == 2 for indefinite length constructed */
  183. void ASN1_put_object(unsigned char **pp, int constructed, int length, int tag,
  184. int xclass)
  185. {
  186. unsigned char *p= *pp;
  187. int i, ttag;
  188. i=(constructed)?V_ASN1_CONSTRUCTED:0;
  189. i|=(xclass&V_ASN1_PRIVATE);
  190. if (tag < 31)
  191. *(p++)=i|(tag&V_ASN1_PRIMITIVE_TAG);
  192. else
  193. {
  194. *(p++)=i|V_ASN1_PRIMITIVE_TAG;
  195. for(i = 0, ttag = tag; ttag > 0; i++) ttag >>=7;
  196. ttag = i;
  197. while(i-- > 0)
  198. {
  199. p[i] = tag & 0x7f;
  200. if(i != (ttag - 1)) p[i] |= 0x80;
  201. tag >>= 7;
  202. }
  203. p += ttag;
  204. }
  205. if (constructed == 2)
  206. *(p++)=0x80;
  207. else
  208. asn1_put_length(&p,length);
  209. *pp=p;
  210. }
  211. int ASN1_put_eoc(unsigned char **pp)
  212. {
  213. unsigned char *p = *pp;
  214. *p++ = 0;
  215. *p++ = 0;
  216. *pp = p;
  217. return 2;
  218. }
  219. static void asn1_put_length(unsigned char **pp, int length)
  220. {
  221. unsigned char *p= *pp;
  222. int i,l;
  223. if (length <= 127)
  224. *(p++)=(unsigned char)length;
  225. else
  226. {
  227. l=length;
  228. for (i=0; l > 0; i++)
  229. l>>=8;
  230. *(p++)=i|0x80;
  231. l=i;
  232. while (i-- > 0)
  233. {
  234. p[i]=length&0xff;
  235. length>>=8;
  236. }
  237. p+=l;
  238. }
  239. *pp=p;
  240. }
  241. int ASN1_object_size(int constructed, int length, int tag)
  242. {
  243. int ret;
  244. ret=length;
  245. ret++;
  246. if (tag >= 31)
  247. {
  248. while (tag > 0)
  249. {
  250. tag>>=7;
  251. ret++;
  252. }
  253. }
  254. if (constructed == 2)
  255. return ret + 3;
  256. ret++;
  257. if (length > 127)
  258. {
  259. while (length > 0)
  260. {
  261. length>>=8;
  262. ret++;
  263. }
  264. }
  265. return(ret);
  266. }
  267. static int _asn1_Finish(ASN1_const_CTX *c)
  268. {
  269. if ((c->inf == (1|V_ASN1_CONSTRUCTED)) && (!c->eos))
  270. {
  271. if (!ASN1_const_check_infinite_end(&c->p,c->slen))
  272. {
  273. c->error=ASN1_R_MISSING_ASN1_EOS;
  274. return(0);
  275. }
  276. }
  277. if ( ((c->slen != 0) && !(c->inf & 1)) ||
  278. ((c->slen < 0) && (c->inf & 1)))
  279. {
  280. c->error=ASN1_R_ASN1_LENGTH_MISMATCH;
  281. return(0);
  282. }
  283. return(1);
  284. }
  285. int asn1_Finish(ASN1_CTX *c)
  286. {
  287. return _asn1_Finish((ASN1_const_CTX *)c);
  288. }
  289. int asn1_const_Finish(ASN1_const_CTX *c)
  290. {
  291. return _asn1_Finish(c);
  292. }
  293. int asn1_GetSequence(ASN1_const_CTX *c, long *length)
  294. {
  295. const unsigned char *q;
  296. q=c->p;
  297. c->inf=ASN1_get_object(&(c->p),&(c->slen),&(c->tag),&(c->xclass),
  298. *length);
  299. if (c->inf & 0x80)
  300. {
  301. c->error=ASN1_R_BAD_GET_ASN1_OBJECT_CALL;
  302. return(0);
  303. }
  304. if (c->tag != V_ASN1_SEQUENCE)
  305. {
  306. c->error=ASN1_R_EXPECTING_AN_ASN1_SEQUENCE;
  307. return(0);
  308. }
  309. (*length)-=(c->p-q);
  310. if (c->max && (*length < 0))
  311. {
  312. c->error=ASN1_R_ASN1_LENGTH_MISMATCH;
  313. return(0);
  314. }
  315. if (c->inf == (1|V_ASN1_CONSTRUCTED))
  316. c->slen= *length+ *(c->pp)-c->p;
  317. c->eos=0;
  318. return(1);
  319. }
  320. int ASN1_STRING_copy(ASN1_STRING *dst, const ASN1_STRING *str)
  321. {
  322. if (str == NULL)
  323. return 0;
  324. dst->type = str->type;
  325. if (!ASN1_STRING_set(dst,str->data,str->length))
  326. return 0;
  327. dst->flags = str->flags;
  328. return 1;
  329. }
  330. ASN1_STRING *ASN1_STRING_dup(const ASN1_STRING *str)
  331. {
  332. ASN1_STRING *ret;
  333. if (!str)
  334. return NULL;
  335. ret=ASN1_STRING_new();
  336. if (!ret)
  337. return NULL;
  338. if (!ASN1_STRING_copy(ret,str))
  339. {
  340. ASN1_STRING_free(ret);
  341. return NULL;
  342. }
  343. return ret;
  344. }
  345. int ASN1_STRING_set(ASN1_STRING *str, const void *_data, int len)
  346. {
  347. unsigned char *c;
  348. const char *data=_data;
  349. if (len < 0)
  350. {
  351. if (data == NULL)
  352. return(0);
  353. else
  354. len=strlen(data);
  355. }
  356. if ((str->length < len) || (str->data == NULL))
  357. {
  358. c=str->data;
  359. if (c == NULL)
  360. str->data=OPENSSL_malloc(len+1);
  361. else
  362. str->data=OPENSSL_realloc(c,len+1);
  363. if (str->data == NULL)
  364. {
  365. OPENSSL_PUT_ERROR(ASN1, ASN1_STRING_set, ERR_R_MALLOC_FAILURE);
  366. str->data=c;
  367. return(0);
  368. }
  369. }
  370. str->length=len;
  371. if (data != NULL)
  372. {
  373. memcpy(str->data,data,len);
  374. /* an allowance for strings :-) */
  375. str->data[len]='\0';
  376. }
  377. return(1);
  378. }
  379. void ASN1_STRING_set0(ASN1_STRING *str, void *data, int len)
  380. {
  381. if (str->data)
  382. OPENSSL_free(str->data);
  383. str->data = data;
  384. str->length = len;
  385. }
  386. ASN1_STRING *ASN1_STRING_new(void)
  387. {
  388. return(ASN1_STRING_type_new(V_ASN1_OCTET_STRING));
  389. }
  390. ASN1_STRING *ASN1_STRING_type_new(int type)
  391. {
  392. ASN1_STRING *ret;
  393. ret=(ASN1_STRING *)OPENSSL_malloc(sizeof(ASN1_STRING));
  394. if (ret == NULL)
  395. {
  396. OPENSSL_PUT_ERROR(ASN1, ASN1_STRING_type_new, ERR_R_MALLOC_FAILURE);
  397. return(NULL);
  398. }
  399. ret->length=0;
  400. ret->type=type;
  401. ret->data=NULL;
  402. ret->flags=0;
  403. return(ret);
  404. }
  405. void ASN1_STRING_free(ASN1_STRING *a)
  406. {
  407. if (a == NULL) return;
  408. if (a->data && !(a->flags & ASN1_STRING_FLAG_NDEF))
  409. OPENSSL_free(a->data);
  410. OPENSSL_free(a);
  411. }
  412. int ASN1_STRING_cmp(const ASN1_STRING *a, const ASN1_STRING *b)
  413. {
  414. int i;
  415. i=(a->length-b->length);
  416. if (i == 0)
  417. {
  418. i=memcmp(a->data,b->data,a->length);
  419. if (i == 0)
  420. return(a->type-b->type);
  421. else
  422. return(i);
  423. }
  424. else
  425. return(i);
  426. }
  427. void asn1_add_error(const unsigned char *address, int offset)
  428. {
  429. char buf1[DECIMAL_SIZE(address)+1],buf2[DECIMAL_SIZE(offset)+1];
  430. BIO_snprintf(buf1,sizeof buf1,"%lu",(unsigned long)address);
  431. BIO_snprintf(buf2,sizeof buf2,"%d",offset);
  432. ERR_add_error_data(4,"address=",buf1," offset=",buf2);
  433. }
  434. int ASN1_STRING_length(const ASN1_STRING *x)
  435. { return M_ASN1_STRING_length(x); }
  436. void ASN1_STRING_length_set(ASN1_STRING *x, int len)
  437. { M_ASN1_STRING_length_set(x, len); return; }
  438. int ASN1_STRING_type(ASN1_STRING *x)
  439. { return M_ASN1_STRING_type(x); }
  440. unsigned char * ASN1_STRING_data(ASN1_STRING *x)
  441. { return M_ASN1_STRING_data(x); }