Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 
 
 
 

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