PageRenderTime 40ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/Source/ThirdParty/openssl/crypto/asn1/asn1_lib.c

https://gitlab.com/Teo-Mirror/AtomicGameEngine
C | 483 lines | 373 code | 42 blank | 68 comment | 103 complexity | 2a54ae58761428ccac7f86cc0bd4c635 MD5 | raw file
  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 "cryptlib.h"
  61. #include <openssl/asn1.h>
  62. #include <openssl/asn1_mac.h>
  63. static int asn1_get_length(const unsigned char **pp, int *inf, long *rl,
  64. int max);
  65. static void asn1_put_length(unsigned char **pp, int length);
  66. const char ASN1_version[] = "ASN.1" OPENSSL_VERSION_PTEXT;
  67. static int _asn1_check_infinite_end(const unsigned char **p, long len)
  68. {
  69. /*
  70. * If there is 0 or 1 byte left, the length check should pick things up
  71. */
  72. if (len <= 0)
  73. return (1);
  74. else if ((len >= 2) && ((*p)[0] == 0) && ((*p)[1] == 0)) {
  75. (*p) += 2;
  76. return (1);
  77. }
  78. return (0);
  79. }
  80. int ASN1_check_infinite_end(unsigned char **p, long len)
  81. {
  82. return _asn1_check_infinite_end((const unsigned char **)p, len);
  83. }
  84. int ASN1_const_check_infinite_end(const unsigned char **p, long len)
  85. {
  86. return _asn1_check_infinite_end(p, len);
  87. }
  88. int ASN1_get_object(const unsigned char **pp, long *plength, int *ptag,
  89. int *pclass, long omax)
  90. {
  91. int i, ret;
  92. long l;
  93. const unsigned char *p = *pp;
  94. int tag, xclass, inf;
  95. long max = omax;
  96. if (!max)
  97. goto err;
  98. ret = (*p & V_ASN1_CONSTRUCTED);
  99. xclass = (*p & V_ASN1_PRIVATE);
  100. i = *p & V_ASN1_PRIMITIVE_TAG;
  101. if (i == V_ASN1_PRIMITIVE_TAG) { /* high-tag */
  102. p++;
  103. if (--max == 0)
  104. goto err;
  105. l = 0;
  106. while (*p & 0x80) {
  107. l <<= 7L;
  108. l |= *(p++) & 0x7f;
  109. if (--max == 0)
  110. goto err;
  111. if (l > (INT_MAX >> 7L))
  112. goto err;
  113. }
  114. l <<= 7L;
  115. l |= *(p++) & 0x7f;
  116. tag = (int)l;
  117. if (--max == 0)
  118. goto err;
  119. } else {
  120. tag = i;
  121. p++;
  122. if (--max == 0)
  123. goto err;
  124. }
  125. *ptag = tag;
  126. *pclass = xclass;
  127. if (!asn1_get_length(&p, &inf, plength, (int)max))
  128. goto err;
  129. if (inf && !(ret & V_ASN1_CONSTRUCTED))
  130. goto err;
  131. #if 0
  132. fprintf(stderr, "p=%d + *plength=%ld > omax=%ld + *pp=%d (%d > %d)\n",
  133. (int)p, *plength, omax, (int)*pp, (int)(p + *plength),
  134. (int)(omax + *pp));
  135. #endif
  136. if (*plength > (omax - (p - *pp))) {
  137. ASN1err(ASN1_F_ASN1_GET_OBJECT, ASN1_R_TOO_LONG);
  138. /*
  139. * Set this so that even if things are not long enough the values are
  140. * set correctly
  141. */
  142. ret |= 0x80;
  143. }
  144. *pp = p;
  145. return (ret | inf);
  146. err:
  147. ASN1err(ASN1_F_ASN1_GET_OBJECT, ASN1_R_HEADER_TOO_LONG);
  148. return (0x80);
  149. }
  150. static int asn1_get_length(const unsigned char **pp, int *inf, long *rl,
  151. int max)
  152. {
  153. const unsigned char *p = *pp;
  154. unsigned long ret = 0;
  155. unsigned int i;
  156. if (max-- < 1)
  157. return (0);
  158. if (*p == 0x80) {
  159. *inf = 1;
  160. ret = 0;
  161. p++;
  162. } else {
  163. *inf = 0;
  164. i = *p & 0x7f;
  165. if (*(p++) & 0x80) {
  166. if (i > sizeof(long))
  167. return 0;
  168. if (max-- == 0)
  169. return (0);
  170. while (i-- > 0) {
  171. ret <<= 8L;
  172. ret |= *(p++);
  173. if (max-- == 0)
  174. return (0);
  175. }
  176. } else
  177. ret = i;
  178. }
  179. if (ret > LONG_MAX)
  180. return 0;
  181. *pp = p;
  182. *rl = (long)ret;
  183. return (1);
  184. }
  185. /*
  186. * class 0 is constructed constructed == 2 for indefinite length constructed
  187. */
  188. void ASN1_put_object(unsigned char **pp, int constructed, int length, int tag,
  189. int xclass)
  190. {
  191. unsigned char *p = *pp;
  192. int i, ttag;
  193. i = (constructed) ? V_ASN1_CONSTRUCTED : 0;
  194. i |= (xclass & V_ASN1_PRIVATE);
  195. if (tag < 31)
  196. *(p++) = i | (tag & V_ASN1_PRIMITIVE_TAG);
  197. else {
  198. *(p++) = i | V_ASN1_PRIMITIVE_TAG;
  199. for (i = 0, ttag = tag; ttag > 0; i++)
  200. ttag >>= 7;
  201. ttag = i;
  202. while (i-- > 0) {
  203. p[i] = tag & 0x7f;
  204. if (i != (ttag - 1))
  205. p[i] |= 0x80;
  206. tag >>= 7;
  207. }
  208. p += ttag;
  209. }
  210. if (constructed == 2)
  211. *(p++) = 0x80;
  212. else
  213. asn1_put_length(&p, length);
  214. *pp = p;
  215. }
  216. int ASN1_put_eoc(unsigned char **pp)
  217. {
  218. unsigned char *p = *pp;
  219. *p++ = 0;
  220. *p++ = 0;
  221. *pp = p;
  222. return 2;
  223. }
  224. static void asn1_put_length(unsigned char **pp, int length)
  225. {
  226. unsigned char *p = *pp;
  227. int i, l;
  228. if (length <= 127)
  229. *(p++) = (unsigned char)length;
  230. else {
  231. l = length;
  232. for (i = 0; l > 0; i++)
  233. l >>= 8;
  234. *(p++) = i | 0x80;
  235. l = i;
  236. while (i-- > 0) {
  237. p[i] = length & 0xff;
  238. length >>= 8;
  239. }
  240. p += l;
  241. }
  242. *pp = p;
  243. }
  244. int ASN1_object_size(int constructed, int length, int tag)
  245. {
  246. int ret;
  247. ret = length;
  248. ret++;
  249. if (tag >= 31) {
  250. while (tag > 0) {
  251. tag >>= 7;
  252. ret++;
  253. }
  254. }
  255. if (constructed == 2)
  256. return ret + 3;
  257. ret++;
  258. if (length > 127) {
  259. while (length > 0) {
  260. length >>= 8;
  261. ret++;
  262. }
  263. }
  264. return (ret);
  265. }
  266. static int _asn1_Finish(ASN1_const_CTX *c)
  267. {
  268. if ((c->inf == (1 | V_ASN1_CONSTRUCTED)) && (!c->eos)) {
  269. if (!ASN1_const_check_infinite_end(&c->p, c->slen)) {
  270. c->error = ERR_R_MISSING_ASN1_EOS;
  271. return (0);
  272. }
  273. }
  274. if (((c->slen != 0) && !(c->inf & 1)) || ((c->slen < 0) && (c->inf & 1))) {
  275. c->error = ERR_R_ASN1_LENGTH_MISMATCH;
  276. return (0);
  277. }
  278. return (1);
  279. }
  280. int asn1_Finish(ASN1_CTX *c)
  281. {
  282. return _asn1_Finish((ASN1_const_CTX *)c);
  283. }
  284. int asn1_const_Finish(ASN1_const_CTX *c)
  285. {
  286. return _asn1_Finish(c);
  287. }
  288. int asn1_GetSequence(ASN1_const_CTX *c, long *length)
  289. {
  290. const unsigned char *q;
  291. q = c->p;
  292. c->inf = ASN1_get_object(&(c->p), &(c->slen), &(c->tag), &(c->xclass),
  293. *length);
  294. if (c->inf & 0x80) {
  295. c->error = ERR_R_BAD_GET_ASN1_OBJECT_CALL;
  296. return (0);
  297. }
  298. if (c->tag != V_ASN1_SEQUENCE) {
  299. c->error = ERR_R_EXPECTING_AN_ASN1_SEQUENCE;
  300. return (0);
  301. }
  302. (*length) -= (c->p - q);
  303. if (c->max && (*length < 0)) {
  304. c->error = ERR_R_ASN1_LENGTH_MISMATCH;
  305. return (0);
  306. }
  307. if (c->inf == (1 | V_ASN1_CONSTRUCTED))
  308. c->slen = *length + *(c->pp) - c->p;
  309. c->eos = 0;
  310. return (1);
  311. }
  312. int ASN1_STRING_copy(ASN1_STRING *dst, const ASN1_STRING *str)
  313. {
  314. if (str == NULL)
  315. return 0;
  316. dst->type = str->type;
  317. if (!ASN1_STRING_set(dst, str->data, str->length))
  318. return 0;
  319. dst->flags = str->flags;
  320. return 1;
  321. }
  322. ASN1_STRING *ASN1_STRING_dup(const ASN1_STRING *str)
  323. {
  324. ASN1_STRING *ret;
  325. if (!str)
  326. return NULL;
  327. ret = ASN1_STRING_new();
  328. if (!ret)
  329. return NULL;
  330. if (!ASN1_STRING_copy(ret, str)) {
  331. ASN1_STRING_free(ret);
  332. return NULL;
  333. }
  334. return ret;
  335. }
  336. int ASN1_STRING_set(ASN1_STRING *str, const void *_data, int len)
  337. {
  338. unsigned char *c;
  339. const char *data = _data;
  340. if (len < 0) {
  341. if (data == NULL)
  342. return (0);
  343. else
  344. len = strlen(data);
  345. }
  346. if ((str->length < len) || (str->data == NULL)) {
  347. c = str->data;
  348. if (c == NULL)
  349. str->data = OPENSSL_malloc(len + 1);
  350. else
  351. str->data = OPENSSL_realloc(c, len + 1);
  352. if (str->data == NULL) {
  353. ASN1err(ASN1_F_ASN1_STRING_SET, ERR_R_MALLOC_FAILURE);
  354. str->data = c;
  355. return (0);
  356. }
  357. }
  358. str->length = len;
  359. if (data != NULL) {
  360. memcpy(str->data, data, len);
  361. /* an allowance for strings :-) */
  362. str->data[len] = '\0';
  363. }
  364. return (1);
  365. }
  366. void ASN1_STRING_set0(ASN1_STRING *str, void *data, int len)
  367. {
  368. if (str->data)
  369. OPENSSL_free(str->data);
  370. str->data = data;
  371. str->length = len;
  372. }
  373. ASN1_STRING *ASN1_STRING_new(void)
  374. {
  375. return (ASN1_STRING_type_new(V_ASN1_OCTET_STRING));
  376. }
  377. ASN1_STRING *ASN1_STRING_type_new(int type)
  378. {
  379. ASN1_STRING *ret;
  380. ret = (ASN1_STRING *)OPENSSL_malloc(sizeof(ASN1_STRING));
  381. if (ret == NULL) {
  382. ASN1err(ASN1_F_ASN1_STRING_TYPE_NEW, ERR_R_MALLOC_FAILURE);
  383. return (NULL);
  384. }
  385. ret->length = 0;
  386. ret->type = type;
  387. ret->data = NULL;
  388. ret->flags = 0;
  389. return (ret);
  390. }
  391. void ASN1_STRING_free(ASN1_STRING *a)
  392. {
  393. if (a == NULL)
  394. return;
  395. if (a->data && !(a->flags & ASN1_STRING_FLAG_NDEF))
  396. OPENSSL_free(a->data);
  397. OPENSSL_free(a);
  398. }
  399. void ASN1_STRING_clear_free(ASN1_STRING *a)
  400. {
  401. if (a && a->data && !(a->flags & ASN1_STRING_FLAG_NDEF))
  402. OPENSSL_cleanse(a->data, a->length);
  403. ASN1_STRING_free(a);
  404. }
  405. int ASN1_STRING_cmp(const ASN1_STRING *a, const ASN1_STRING *b)
  406. {
  407. int i;
  408. i = (a->length - b->length);
  409. if (i == 0) {
  410. i = memcmp(a->data, b->data, a->length);
  411. if (i == 0)
  412. return (a->type - b->type);
  413. else
  414. return (i);
  415. } else
  416. return (i);
  417. }
  418. void asn1_add_error(const unsigned char *address, int offset)
  419. {
  420. char buf1[DECIMAL_SIZE(address) + 1], buf2[DECIMAL_SIZE(offset) + 1];
  421. BIO_snprintf(buf1, sizeof buf1, "%lu", (unsigned long)address);
  422. BIO_snprintf(buf2, sizeof buf2, "%d", offset);
  423. ERR_add_error_data(4, "address=", buf1, " offset=", buf2);
  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. }