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.
 
 
 
 
 
 

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