asn1_lib.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  1. /* crypto/asn1/asn1_lib.c */
  2. /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  3. * All rights reserved.
  4. *
  5. * This package is an SSL implementation written
  6. * by Eric Young (eay@cryptsoft.com).
  7. * The implementation was written so as to conform with Netscapes SSL.
  8. *
  9. * This library is free for commercial and non-commercial use as long as
  10. * the following conditions are aheared to. The following conditions
  11. * apply to all code found in this distribution, be it the RC4, RSA,
  12. * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  13. * included with this distribution is covered by the same copyright terms
  14. * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  15. *
  16. * Copyright remains Eric Young's, and as such any Copyright notices in
  17. * the code are not to be removed.
  18. * If this package is used in a product, Eric Young should be given attribution
  19. * as the author of the parts of the library used.
  20. * This can be in the form of a textual message at program startup or
  21. * in documentation (online or textual) provided with the package.
  22. *
  23. * Redistribution and use in source and binary forms, with or without
  24. * modification, are permitted provided that the following conditions
  25. * are met:
  26. * 1. Redistributions of source code must retain the copyright
  27. * notice, this list of conditions and the following disclaimer.
  28. * 2. Redistributions in binary form must reproduce the above copyright
  29. * notice, this list of conditions and the following disclaimer in the
  30. * documentation and/or other materials provided with the distribution.
  31. * 3. All advertising materials mentioning features or use of this software
  32. * must display the following acknowledgement:
  33. * "This product includes cryptographic software written by
  34. * Eric Young (eay@cryptsoft.com)"
  35. * The word 'cryptographic' can be left out if the rouines from the library
  36. * being used are not cryptographic related :-).
  37. * 4. If you include any Windows specific code (or a derivative thereof) from
  38. * the apps directory (application code) you must include an acknowledgement:
  39. * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  40. *
  41. * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  42. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  43. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  44. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  45. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  46. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  47. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  48. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  49. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  50. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  51. * SUCH DAMAGE.
  52. *
  53. * The licence and distribution terms for any publically available version or
  54. * derivative of this code cannot be changed. i.e. this code cannot simply be
  55. * copied and put under another distribution licence
  56. * [including the GNU Public Licence.]
  57. */
  58. #include <stdio.h>
  59. #include <limits.h>
  60. #include "internal/cryptlib.h"
  61. #include <openssl/asn1.h>
  62. static int asn1_get_length(const unsigned char **pp, int *inf, long *rl,
  63. int max);
  64. static void asn1_put_length(unsigned char **pp, int length);
  65. const char ASN1_version[] = "ASN.1" OPENSSL_VERSION_PTEXT;
  66. static int _asn1_check_infinite_end(const unsigned char **p, long len)
  67. {
  68. /*
  69. * If there is 0 or 1 byte left, the length check should pick things up
  70. */
  71. if (len <= 0)
  72. return (1);
  73. else if ((len >= 2) && ((*p)[0] == 0) && ((*p)[1] == 0)) {
  74. (*p) += 2;
  75. return (1);
  76. }
  77. return (0);
  78. }
  79. int ASN1_check_infinite_end(unsigned char **p, long len)
  80. {
  81. return _asn1_check_infinite_end((const unsigned char **)p, len);
  82. }
  83. int ASN1_const_check_infinite_end(const unsigned char **p, long len)
  84. {
  85. return _asn1_check_infinite_end(p, len);
  86. }
  87. int ASN1_get_object(const unsigned char **pp, long *plength, int *ptag,
  88. int *pclass, long omax)
  89. {
  90. int i, ret;
  91. long l;
  92. const unsigned char *p = *pp;
  93. int tag, xclass, inf;
  94. long max = omax;
  95. if (!max)
  96. goto err;
  97. ret = (*p & V_ASN1_CONSTRUCTED);
  98. xclass = (*p & V_ASN1_PRIVATE);
  99. i = *p & V_ASN1_PRIMITIVE_TAG;
  100. if (i == V_ASN1_PRIMITIVE_TAG) { /* high-tag */
  101. p++;
  102. if (--max == 0)
  103. goto err;
  104. l = 0;
  105. while (*p & 0x80) {
  106. l <<= 7L;
  107. l |= *(p++) & 0x7f;
  108. if (--max == 0)
  109. goto err;
  110. if (l > (INT_MAX >> 7L))
  111. goto err;
  112. }
  113. l <<= 7L;
  114. l |= *(p++) & 0x7f;
  115. tag = (int)l;
  116. if (--max == 0)
  117. goto err;
  118. } else {
  119. tag = i;
  120. p++;
  121. if (--max == 0)
  122. goto err;
  123. }
  124. *ptag = tag;
  125. *pclass = xclass;
  126. if (!asn1_get_length(&p, &inf, plength, (int)max))
  127. goto err;
  128. if (inf && !(ret & V_ASN1_CONSTRUCTED))
  129. goto err;
  130. if (*plength > (omax - (p - *pp))) {
  131. ASN1err(ASN1_F_ASN1_GET_OBJECT, ASN1_R_TOO_LONG);
  132. /*
  133. * Set this so that even if things are not long enough the values are
  134. * set correctly
  135. */
  136. ret |= 0x80;
  137. }
  138. *pp = p;
  139. return (ret | inf);
  140. err:
  141. ASN1err(ASN1_F_ASN1_GET_OBJECT, ASN1_R_HEADER_TOO_LONG);
  142. return (0x80);
  143. }
  144. static int asn1_get_length(const unsigned char **pp, int *inf, long *rl,
  145. int max)
  146. {
  147. const unsigned char *p = *pp;
  148. unsigned long ret = 0;
  149. unsigned int i;
  150. if (max-- < 1)
  151. return (0);
  152. if (*p == 0x80) {
  153. *inf = 1;
  154. ret = 0;
  155. p++;
  156. } else {
  157. *inf = 0;
  158. i = *p & 0x7f;
  159. if (*(p++) & 0x80) {
  160. if (max < (int)i)
  161. return 0;
  162. /* Skip leading zeroes */
  163. while (i && *p == 0) {
  164. p++;
  165. i--;
  166. }
  167. if (i > sizeof(long))
  168. return 0;
  169. while (i-- > 0) {
  170. ret <<= 8L;
  171. ret |= *(p++);
  172. }
  173. } else
  174. ret = i;
  175. }
  176. if (ret > LONG_MAX)
  177. return 0;
  178. *pp = p;
  179. *rl = (long)ret;
  180. return (1);
  181. }
  182. /*
  183. * class 0 is constructed constructed == 2 for indefinite length constructed
  184. */
  185. void ASN1_put_object(unsigned char **pp, int constructed, int length, int tag,
  186. int xclass)
  187. {
  188. unsigned char *p = *pp;
  189. int i, ttag;
  190. i = (constructed) ? V_ASN1_CONSTRUCTED : 0;
  191. i |= (xclass & V_ASN1_PRIVATE);
  192. if (tag < 31)
  193. *(p++) = i | (tag & V_ASN1_PRIMITIVE_TAG);
  194. else {
  195. *(p++) = i | V_ASN1_PRIMITIVE_TAG;
  196. for (i = 0, ttag = tag; ttag > 0; i++)
  197. ttag >>= 7;
  198. ttag = i;
  199. while (i-- > 0) {
  200. p[i] = tag & 0x7f;
  201. if (i != (ttag - 1))
  202. p[i] |= 0x80;
  203. tag >>= 7;
  204. }
  205. p += ttag;
  206. }
  207. if (constructed == 2)
  208. *(p++) = 0x80;
  209. else
  210. asn1_put_length(&p, length);
  211. *pp = p;
  212. }
  213. int ASN1_put_eoc(unsigned char **pp)
  214. {
  215. unsigned char *p = *pp;
  216. *p++ = 0;
  217. *p++ = 0;
  218. *pp = p;
  219. return 2;
  220. }
  221. static void asn1_put_length(unsigned char **pp, int length)
  222. {
  223. unsigned char *p = *pp;
  224. int i, l;
  225. if (length <= 127)
  226. *(p++) = (unsigned char)length;
  227. else {
  228. l = length;
  229. for (i = 0; l > 0; i++)
  230. l >>= 8;
  231. *(p++) = i | 0x80;
  232. l = i;
  233. while (i-- > 0) {
  234. p[i] = length & 0xff;
  235. length >>= 8;
  236. }
  237. p += l;
  238. }
  239. *pp = p;
  240. }
  241. int ASN1_object_size(int constructed, int length, int tag)
  242. {
  243. int ret;
  244. ret = length;
  245. ret++;
  246. if (tag >= 31) {
  247. while (tag > 0) {
  248. tag >>= 7;
  249. ret++;
  250. }
  251. }
  252. if (constructed == 2)
  253. return ret + 3;
  254. ret++;
  255. if (length > 127) {
  256. while (length > 0) {
  257. length >>= 8;
  258. ret++;
  259. }
  260. }
  261. return (ret);
  262. }
  263. int ASN1_STRING_copy(ASN1_STRING *dst, const ASN1_STRING *str)
  264. {
  265. if (str == NULL)
  266. return 0;
  267. dst->type = str->type;
  268. if (!ASN1_STRING_set(dst, str->data, str->length))
  269. return 0;
  270. dst->flags = str->flags;
  271. return 1;
  272. }
  273. ASN1_STRING *ASN1_STRING_dup(const ASN1_STRING *str)
  274. {
  275. ASN1_STRING *ret;
  276. if (!str)
  277. return NULL;
  278. ret = ASN1_STRING_new();
  279. if (!ret)
  280. return NULL;
  281. if (!ASN1_STRING_copy(ret, str)) {
  282. ASN1_STRING_free(ret);
  283. return NULL;
  284. }
  285. return ret;
  286. }
  287. int ASN1_STRING_set(ASN1_STRING *str, const void *_data, int len)
  288. {
  289. unsigned char *c;
  290. const char *data = _data;
  291. if (len < 0) {
  292. if (data == NULL)
  293. return (0);
  294. else
  295. len = strlen(data);
  296. }
  297. if ((str->length < len) || (str->data == NULL)) {
  298. c = str->data;
  299. str->data = OPENSSL_realloc(c, len + 1);
  300. if (str->data == NULL) {
  301. ASN1err(ASN1_F_ASN1_STRING_SET, ERR_R_MALLOC_FAILURE);
  302. str->data = c;
  303. return (0);
  304. }
  305. }
  306. str->length = len;
  307. if (data != NULL) {
  308. memcpy(str->data, data, len);
  309. /* an allowance for strings :-) */
  310. str->data[len] = '\0';
  311. }
  312. return (1);
  313. }
  314. void ASN1_STRING_set0(ASN1_STRING *str, void *data, int len)
  315. {
  316. OPENSSL_free(str->data);
  317. str->data = data;
  318. str->length = len;
  319. }
  320. ASN1_STRING *ASN1_STRING_new(void)
  321. {
  322. return (ASN1_STRING_type_new(V_ASN1_OCTET_STRING));
  323. }
  324. ASN1_STRING *ASN1_STRING_type_new(int type)
  325. {
  326. ASN1_STRING *ret;
  327. ret = OPENSSL_malloc(sizeof(*ret));
  328. if (ret == NULL) {
  329. ASN1err(ASN1_F_ASN1_STRING_TYPE_NEW, ERR_R_MALLOC_FAILURE);
  330. return (NULL);
  331. }
  332. ret->length = 0;
  333. ret->type = type;
  334. ret->data = NULL;
  335. ret->flags = 0;
  336. return (ret);
  337. }
  338. void ASN1_STRING_free(ASN1_STRING *a)
  339. {
  340. if (a == NULL)
  341. return;
  342. if (!(a->flags & ASN1_STRING_FLAG_NDEF))
  343. OPENSSL_free(a->data);
  344. OPENSSL_free(a);
  345. }
  346. void ASN1_STRING_clear_free(ASN1_STRING *a)
  347. {
  348. if (a == NULL)
  349. return;
  350. if (a->data && !(a->flags & ASN1_STRING_FLAG_NDEF))
  351. OPENSSL_cleanse(a->data, a->length);
  352. ASN1_STRING_free(a);
  353. }
  354. int ASN1_STRING_cmp(const ASN1_STRING *a, const ASN1_STRING *b)
  355. {
  356. int i;
  357. i = (a->length - b->length);
  358. if (i == 0) {
  359. i = memcmp(a->data, b->data, a->length);
  360. if (i == 0)
  361. return (a->type - b->type);
  362. else
  363. return (i);
  364. } else
  365. return (i);
  366. }
  367. int ASN1_STRING_length(const ASN1_STRING *x)
  368. {
  369. return x->length;
  370. }
  371. void ASN1_STRING_length_set(ASN1_STRING *x, int len)
  372. {
  373. x->length = len;
  374. }
  375. int ASN1_STRING_type(ASN1_STRING *x)
  376. {
  377. return x->type;
  378. }
  379. unsigned char *ASN1_STRING_data(ASN1_STRING *x)
  380. {
  381. return x->data;
  382. }