asn1_lib.c 14 KB

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