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.
 
 
 
 
 
 

484 lines
13 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. /* Cross-module errors from crypto/x509/i2d_pr.c. */
  63. OPENSSL_DECLARE_ERROR_REASON(ASN1, UNSUPPORTED_PUBLIC_KEY_TYPE)
  64. /* Cross-module errors from crypto/x509/algorithm.c. */
  65. OPENSSL_DECLARE_ERROR_REASON(ASN1, CONTEXT_NOT_INITIALISED)
  66. OPENSSL_DECLARE_ERROR_REASON(ASN1, DIGEST_AND_KEY_TYPE_NOT_SUPPORTED)
  67. OPENSSL_DECLARE_ERROR_REASON(ASN1, UNKNOWN_MESSAGE_DIGEST_ALGORITHM)
  68. OPENSSL_DECLARE_ERROR_REASON(ASN1, UNKNOWN_SIGNATURE_ALGORITHM)
  69. OPENSSL_DECLARE_ERROR_REASON(ASN1, WRONG_PUBLIC_KEY_TYPE)
  70. /*
  71. * Cross-module errors from crypto/x509/asn1_gen.c. TODO(davidben): Remove
  72. * these once asn1_gen.c is gone.
  73. */
  74. OPENSSL_DECLARE_ERROR_REASON(ASN1, DEPTH_EXCEEDED)
  75. OPENSSL_DECLARE_ERROR_REASON(ASN1, ILLEGAL_BITSTRING_FORMAT)
  76. OPENSSL_DECLARE_ERROR_REASON(ASN1, ILLEGAL_BOOLEAN)
  77. OPENSSL_DECLARE_ERROR_REASON(ASN1, ILLEGAL_FORMAT)
  78. OPENSSL_DECLARE_ERROR_REASON(ASN1, ILLEGAL_HEX)
  79. OPENSSL_DECLARE_ERROR_REASON(ASN1, ILLEGAL_IMPLICIT_TAG)
  80. OPENSSL_DECLARE_ERROR_REASON(ASN1, ILLEGAL_INTEGER)
  81. OPENSSL_DECLARE_ERROR_REASON(ASN1, ILLEGAL_NESTED_TAGGING)
  82. OPENSSL_DECLARE_ERROR_REASON(ASN1, ILLEGAL_NULL_VALUE)
  83. OPENSSL_DECLARE_ERROR_REASON(ASN1, ILLEGAL_OBJECT)
  84. OPENSSL_DECLARE_ERROR_REASON(ASN1, ILLEGAL_TIME_VALUE)
  85. OPENSSL_DECLARE_ERROR_REASON(ASN1, INTEGER_NOT_ASCII_FORMAT)
  86. OPENSSL_DECLARE_ERROR_REASON(ASN1, INVALID_MODIFIER)
  87. OPENSSL_DECLARE_ERROR_REASON(ASN1, INVALID_NUMBER)
  88. OPENSSL_DECLARE_ERROR_REASON(ASN1, LIST_ERROR)
  89. OPENSSL_DECLARE_ERROR_REASON(ASN1, MISSING_VALUE)
  90. OPENSSL_DECLARE_ERROR_REASON(ASN1, NOT_ASCII_FORMAT)
  91. OPENSSL_DECLARE_ERROR_REASON(ASN1, OBJECT_NOT_ASCII_FORMAT)
  92. OPENSSL_DECLARE_ERROR_REASON(ASN1, SEQUENCE_OR_SET_NEEDS_CONFIG)
  93. OPENSSL_DECLARE_ERROR_REASON(ASN1, TIME_NOT_ASCII_FORMAT)
  94. OPENSSL_DECLARE_ERROR_REASON(ASN1, UNKNOWN_FORMAT)
  95. OPENSSL_DECLARE_ERROR_REASON(ASN1, UNKNOWN_TAG)
  96. OPENSSL_DECLARE_ERROR_REASON(ASN1, UNSUPPORTED_TYPE)
  97. static int asn1_get_length(const unsigned char **pp, int *inf, long *rl,
  98. long max);
  99. static void asn1_put_length(unsigned char **pp, int length);
  100. static int _asn1_check_infinite_end(const unsigned char **p, long len)
  101. {
  102. /*
  103. * If there is 0 or 1 byte left, the length check should pick things up
  104. */
  105. if (len <= 0)
  106. return (1);
  107. else if ((len >= 2) && ((*p)[0] == 0) && ((*p)[1] == 0)) {
  108. (*p) += 2;
  109. return (1);
  110. }
  111. return (0);
  112. }
  113. int ASN1_check_infinite_end(unsigned char **p, long len)
  114. {
  115. return _asn1_check_infinite_end((const unsigned char **)p, len);
  116. }
  117. int ASN1_const_check_infinite_end(const unsigned char **p, long len)
  118. {
  119. return _asn1_check_infinite_end(p, len);
  120. }
  121. int ASN1_get_object(const unsigned char **pp, long *plength, int *ptag,
  122. int *pclass, long omax)
  123. {
  124. int i, ret;
  125. long l;
  126. const unsigned char *p = *pp;
  127. int tag, xclass, inf;
  128. long max = omax;
  129. if (!max)
  130. 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) { /* high-tag */
  135. p++;
  136. if (--max == 0)
  137. goto err;
  138. l = 0;
  139. while (*p & 0x80) {
  140. l <<= 7L;
  141. l |= *(p++) & 0x7f;
  142. if (--max == 0)
  143. goto err;
  144. if (l > (INT_MAX >> 7L))
  145. goto err;
  146. }
  147. l <<= 7L;
  148. l |= *(p++) & 0x7f;
  149. tag = (int)l;
  150. if (--max == 0)
  151. goto err;
  152. } else {
  153. tag = i;
  154. p++;
  155. if (--max == 0)
  156. goto err;
  157. }
  158. /* To avoid ambiguity with V_ASN1_NEG, impose a limit on universal tags. */
  159. if (xclass == V_ASN1_UNIVERSAL && tag > V_ASN1_MAX_UNIVERSAL)
  160. goto err;
  161. *ptag = tag;
  162. *pclass = xclass;
  163. if (!asn1_get_length(&p, &inf, plength, max))
  164. goto err;
  165. if (inf && !(ret & V_ASN1_CONSTRUCTED))
  166. goto err;
  167. #if 0
  168. fprintf(stderr, "p=%d + *plength=%ld > omax=%ld + *pp=%d (%d > %d)\n",
  169. (int)p, *plength, omax, (int)*pp, (int)(p + *plength),
  170. (int)(omax + *pp));
  171. #endif
  172. if (*plength > (omax - (p - *pp))) {
  173. OPENSSL_PUT_ERROR(ASN1, ASN1_R_TOO_LONG);
  174. /*
  175. * Set this so that even if things are not long enough the values are
  176. * set correctly
  177. */
  178. ret |= 0x80;
  179. }
  180. *pp = p;
  181. return (ret | inf);
  182. err:
  183. OPENSSL_PUT_ERROR(ASN1, ASN1_R_HEADER_TOO_LONG);
  184. return (0x80);
  185. }
  186. static int asn1_get_length(const unsigned char **pp, int *inf, long *rl,
  187. long max)
  188. {
  189. const unsigned char *p = *pp;
  190. unsigned long ret = 0;
  191. unsigned long i;
  192. if (max-- < 1)
  193. return 0;
  194. if (*p == 0x80) {
  195. *inf = 1;
  196. ret = 0;
  197. p++;
  198. } else {
  199. *inf = 0;
  200. i = *p & 0x7f;
  201. if (*(p++) & 0x80) {
  202. if (i > sizeof(ret) || max < (long)i)
  203. return 0;
  204. while (i-- > 0) {
  205. ret <<= 8L;
  206. ret |= *(p++);
  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. /*
  218. * class 0 is constructed constructed == 2 for indefinite length constructed
  219. */
  220. void ASN1_put_object(unsigned char **pp, int constructed, int length, int tag,
  221. int xclass)
  222. {
  223. unsigned char *p = *pp;
  224. int i, ttag;
  225. i = (constructed) ? V_ASN1_CONSTRUCTED : 0;
  226. i |= (xclass & V_ASN1_PRIVATE);
  227. if (tag < 31)
  228. *(p++) = i | (tag & V_ASN1_PRIMITIVE_TAG);
  229. else {
  230. *(p++) = i | V_ASN1_PRIMITIVE_TAG;
  231. for (i = 0, ttag = tag; ttag > 0; i++)
  232. ttag >>= 7;
  233. ttag = i;
  234. while (i-- > 0) {
  235. p[i] = tag & 0x7f;
  236. if (i != (ttag - 1))
  237. p[i] |= 0x80;
  238. tag >>= 7;
  239. }
  240. p += ttag;
  241. }
  242. if (constructed == 2)
  243. *(p++) = 0x80;
  244. else
  245. asn1_put_length(&p, length);
  246. *pp = p;
  247. }
  248. int ASN1_put_eoc(unsigned char **pp)
  249. {
  250. unsigned char *p = *pp;
  251. *p++ = 0;
  252. *p++ = 0;
  253. *pp = p;
  254. return 2;
  255. }
  256. static void asn1_put_length(unsigned char **pp, int length)
  257. {
  258. unsigned char *p = *pp;
  259. int i, l;
  260. if (length <= 127)
  261. *(p++) = (unsigned char)length;
  262. else {
  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. p[i] = length & 0xff;
  270. length >>= 8;
  271. }
  272. p += l;
  273. }
  274. *pp = p;
  275. }
  276. int ASN1_object_size(int constructed, int length, int tag)
  277. {
  278. int ret = 1;
  279. if (length < 0)
  280. return -1;
  281. if (tag >= 31) {
  282. while (tag > 0) {
  283. tag >>= 7;
  284. ret++;
  285. }
  286. }
  287. if (constructed == 2) {
  288. ret += 3;
  289. } else {
  290. ret++;
  291. if (length > 127) {
  292. int tmplen = length;
  293. while (tmplen > 0) {
  294. tmplen >>= 8;
  295. ret++;
  296. }
  297. }
  298. }
  299. if (ret >= INT_MAX - length)
  300. return -1;
  301. return ret + length;
  302. }
  303. static int _asn1_Finish(ASN1_const_CTX *c)
  304. {
  305. if ((c->inf == (1 | V_ASN1_CONSTRUCTED)) && (!c->eos)) {
  306. if (!ASN1_const_check_infinite_end(&c->p, c->slen)) {
  307. c->error = ASN1_R_MISSING_ASN1_EOS;
  308. return (0);
  309. }
  310. }
  311. if (((c->slen != 0) && !(c->inf & 1)) || ((c->slen < 0) && (c->inf & 1))) {
  312. c->error = ASN1_R_ASN1_LENGTH_MISMATCH;
  313. return (0);
  314. }
  315. return (1);
  316. }
  317. int asn1_Finish(ASN1_CTX *c)
  318. {
  319. return _asn1_Finish((ASN1_const_CTX *)c);
  320. }
  321. int asn1_const_Finish(ASN1_const_CTX *c)
  322. {
  323. return _asn1_Finish(c);
  324. }
  325. int ASN1_STRING_copy(ASN1_STRING *dst, const ASN1_STRING *str)
  326. {
  327. if (str == NULL)
  328. return 0;
  329. dst->type = str->type;
  330. if (!ASN1_STRING_set(dst, str->data, str->length))
  331. return 0;
  332. dst->flags = str->flags;
  333. return 1;
  334. }
  335. ASN1_STRING *ASN1_STRING_dup(const ASN1_STRING *str)
  336. {
  337. ASN1_STRING *ret;
  338. if (!str)
  339. return NULL;
  340. ret = ASN1_STRING_new();
  341. if (!ret)
  342. return NULL;
  343. if (!ASN1_STRING_copy(ret, str)) {
  344. ASN1_STRING_free(ret);
  345. return NULL;
  346. }
  347. return ret;
  348. }
  349. int ASN1_STRING_set(ASN1_STRING *str, const void *_data, int len)
  350. {
  351. unsigned char *c;
  352. const char *data = _data;
  353. if (len < 0) {
  354. if (data == NULL)
  355. return (0);
  356. else
  357. len = strlen(data);
  358. }
  359. if ((str->length <= len) || (str->data == NULL)) {
  360. c = str->data;
  361. if (c == NULL)
  362. str->data = OPENSSL_malloc(len + 1);
  363. else
  364. str->data = OPENSSL_realloc(c, len + 1);
  365. if (str->data == NULL) {
  366. OPENSSL_PUT_ERROR(ASN1, ERR_R_MALLOC_FAILURE);
  367. str->data = c;
  368. return (0);
  369. }
  370. }
  371. str->length = len;
  372. if (data != NULL) {
  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. OPENSSL_PUT_ERROR(ASN1, 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)
  407. 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. i = memcmp(a->data, b->data, a->length);
  418. if (i == 0)
  419. return (a->type - b->type);
  420. else
  421. return (i);
  422. } else
  423. return (i);
  424. }
  425. int ASN1_STRING_length(const ASN1_STRING *x)
  426. {
  427. return M_ASN1_STRING_length(x);
  428. }
  429. void ASN1_STRING_length_set(ASN1_STRING *x, int len)
  430. {
  431. M_ASN1_STRING_length_set(x, len);
  432. return;
  433. }
  434. int ASN1_STRING_type(ASN1_STRING *x)
  435. {
  436. return M_ASN1_STRING_type(x);
  437. }
  438. unsigned char *ASN1_STRING_data(ASN1_STRING *x)
  439. {
  440. return M_ASN1_STRING_data(x);
  441. }