Você não pode selecionar mais de 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.
 
 
 
 
 
 

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