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.
 
 
 
 
 
 

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