A local copy of OpenSSL from GitHub
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.

365 lines
8.0 KiB

  1. /*
  2. * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL license (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include <stdio.h>
  10. #include <limits.h>
  11. #include "internal/cryptlib.h"
  12. #include <openssl/asn1.h>
  13. static int asn1_get_length(const unsigned char **pp, int *inf, long *rl,
  14. long max);
  15. static void asn1_put_length(unsigned char **pp, int length);
  16. static int _asn1_check_infinite_end(const unsigned char **p, long len)
  17. {
  18. /*
  19. * If there is 0 or 1 byte left, the length check should pick things up
  20. */
  21. if (len <= 0)
  22. return (1);
  23. else if ((len >= 2) && ((*p)[0] == 0) && ((*p)[1] == 0)) {
  24. (*p) += 2;
  25. return (1);
  26. }
  27. return (0);
  28. }
  29. int ASN1_check_infinite_end(unsigned char **p, long len)
  30. {
  31. return _asn1_check_infinite_end((const unsigned char **)p, len);
  32. }
  33. int ASN1_const_check_infinite_end(const unsigned char **p, long len)
  34. {
  35. return _asn1_check_infinite_end(p, len);
  36. }
  37. int ASN1_get_object(const unsigned char **pp, long *plength, int *ptag,
  38. int *pclass, long omax)
  39. {
  40. int i, ret;
  41. long l;
  42. const unsigned char *p = *pp;
  43. int tag, xclass, inf;
  44. long max = omax;
  45. if (!max)
  46. goto err;
  47. ret = (*p & V_ASN1_CONSTRUCTED);
  48. xclass = (*p & V_ASN1_PRIVATE);
  49. i = *p & V_ASN1_PRIMITIVE_TAG;
  50. if (i == V_ASN1_PRIMITIVE_TAG) { /* high-tag */
  51. p++;
  52. if (--max == 0)
  53. goto err;
  54. l = 0;
  55. while (*p & 0x80) {
  56. l <<= 7L;
  57. l |= *(p++) & 0x7f;
  58. if (--max == 0)
  59. goto err;
  60. if (l > (INT_MAX >> 7L))
  61. goto err;
  62. }
  63. l <<= 7L;
  64. l |= *(p++) & 0x7f;
  65. tag = (int)l;
  66. if (--max == 0)
  67. goto err;
  68. } else {
  69. tag = i;
  70. p++;
  71. if (--max == 0)
  72. goto err;
  73. }
  74. *ptag = tag;
  75. *pclass = xclass;
  76. if (!asn1_get_length(&p, &inf, plength, max))
  77. goto err;
  78. if (inf && !(ret & V_ASN1_CONSTRUCTED))
  79. goto err;
  80. if (*plength > (omax - (p - *pp))) {
  81. ASN1err(ASN1_F_ASN1_GET_OBJECT, ASN1_R_TOO_LONG);
  82. /*
  83. * Set this so that even if things are not long enough the values are
  84. * set correctly
  85. */
  86. ret |= 0x80;
  87. }
  88. *pp = p;
  89. return (ret | inf);
  90. err:
  91. ASN1err(ASN1_F_ASN1_GET_OBJECT, ASN1_R_HEADER_TOO_LONG);
  92. return (0x80);
  93. }
  94. static int asn1_get_length(const unsigned char **pp, int *inf, long *rl,
  95. long max)
  96. {
  97. const unsigned char *p = *pp;
  98. unsigned long ret = 0;
  99. unsigned long i;
  100. if (max-- < 1)
  101. return 0;
  102. if (*p == 0x80) {
  103. *inf = 1;
  104. ret = 0;
  105. p++;
  106. } else {
  107. *inf = 0;
  108. i = *p & 0x7f;
  109. if (*(p++) & 0x80) {
  110. if (max < (long)i + 1)
  111. return 0;
  112. /* Skip leading zeroes */
  113. while (i && *p == 0) {
  114. p++;
  115. i--;
  116. }
  117. if (i > sizeof(long))
  118. return 0;
  119. while (i-- > 0) {
  120. ret <<= 8L;
  121. ret |= *(p++);
  122. }
  123. } else
  124. ret = i;
  125. }
  126. if (ret > LONG_MAX)
  127. return 0;
  128. *pp = p;
  129. *rl = (long)ret;
  130. return 1;
  131. }
  132. /*
  133. * class 0 is constructed constructed == 2 for indefinite length constructed
  134. */
  135. void ASN1_put_object(unsigned char **pp, int constructed, int length, int tag,
  136. int xclass)
  137. {
  138. unsigned char *p = *pp;
  139. int i, ttag;
  140. i = (constructed) ? V_ASN1_CONSTRUCTED : 0;
  141. i |= (xclass & V_ASN1_PRIVATE);
  142. if (tag < 31)
  143. *(p++) = i | (tag & V_ASN1_PRIMITIVE_TAG);
  144. else {
  145. *(p++) = i | V_ASN1_PRIMITIVE_TAG;
  146. for (i = 0, ttag = tag; ttag > 0; i++)
  147. ttag >>= 7;
  148. ttag = i;
  149. while (i-- > 0) {
  150. p[i] = tag & 0x7f;
  151. if (i != (ttag - 1))
  152. p[i] |= 0x80;
  153. tag >>= 7;
  154. }
  155. p += ttag;
  156. }
  157. if (constructed == 2)
  158. *(p++) = 0x80;
  159. else
  160. asn1_put_length(&p, length);
  161. *pp = p;
  162. }
  163. int ASN1_put_eoc(unsigned char **pp)
  164. {
  165. unsigned char *p = *pp;
  166. *p++ = 0;
  167. *p++ = 0;
  168. *pp = p;
  169. return 2;
  170. }
  171. static void asn1_put_length(unsigned char **pp, int length)
  172. {
  173. unsigned char *p = *pp;
  174. int i, l;
  175. if (length <= 127)
  176. *(p++) = (unsigned char)length;
  177. else {
  178. l = length;
  179. for (i = 0; l > 0; i++)
  180. l >>= 8;
  181. *(p++) = i | 0x80;
  182. l = i;
  183. while (i-- > 0) {
  184. p[i] = length & 0xff;
  185. length >>= 8;
  186. }
  187. p += l;
  188. }
  189. *pp = p;
  190. }
  191. int ASN1_object_size(int constructed, int length, int tag)
  192. {
  193. int ret;
  194. ret = length;
  195. ret++;
  196. if (tag >= 31) {
  197. while (tag > 0) {
  198. tag >>= 7;
  199. ret++;
  200. }
  201. }
  202. if (constructed == 2)
  203. return ret + 3;
  204. ret++;
  205. if (length > 127) {
  206. while (length > 0) {
  207. length >>= 8;
  208. ret++;
  209. }
  210. }
  211. return (ret);
  212. }
  213. int ASN1_STRING_copy(ASN1_STRING *dst, const ASN1_STRING *str)
  214. {
  215. if (str == NULL)
  216. return 0;
  217. dst->type = str->type;
  218. if (!ASN1_STRING_set(dst, str->data, str->length))
  219. return 0;
  220. /* Copy flags but preserve embed value */
  221. dst->flags &= ASN1_STRING_FLAG_EMBED;
  222. dst->flags |= str->flags & ~ASN1_STRING_FLAG_EMBED;
  223. return 1;
  224. }
  225. ASN1_STRING *ASN1_STRING_dup(const ASN1_STRING *str)
  226. {
  227. ASN1_STRING *ret;
  228. if (!str)
  229. return NULL;
  230. ret = ASN1_STRING_new();
  231. if (ret == NULL)
  232. return NULL;
  233. if (!ASN1_STRING_copy(ret, str)) {
  234. ASN1_STRING_free(ret);
  235. return NULL;
  236. }
  237. return ret;
  238. }
  239. int ASN1_STRING_set(ASN1_STRING *str, const void *_data, int len)
  240. {
  241. unsigned char *c;
  242. const char *data = _data;
  243. if (len < 0) {
  244. if (data == NULL)
  245. return (0);
  246. else
  247. len = strlen(data);
  248. }
  249. if ((str->length < len) || (str->data == NULL)) {
  250. c = str->data;
  251. str->data = OPENSSL_realloc(c, len + 1);
  252. if (str->data == NULL) {
  253. ASN1err(ASN1_F_ASN1_STRING_SET, ERR_R_MALLOC_FAILURE);
  254. str->data = c;
  255. return (0);
  256. }
  257. }
  258. str->length = len;
  259. if (data != NULL) {
  260. memcpy(str->data, data, len);
  261. /* an allowance for strings :-) */
  262. str->data[len] = '\0';
  263. }
  264. return (1);
  265. }
  266. void ASN1_STRING_set0(ASN1_STRING *str, void *data, int len)
  267. {
  268. OPENSSL_free(str->data);
  269. str->data = data;
  270. str->length = len;
  271. }
  272. ASN1_STRING *ASN1_STRING_new(void)
  273. {
  274. return (ASN1_STRING_type_new(V_ASN1_OCTET_STRING));
  275. }
  276. ASN1_STRING *ASN1_STRING_type_new(int type)
  277. {
  278. ASN1_STRING *ret;
  279. ret = OPENSSL_zalloc(sizeof(*ret));
  280. if (ret == NULL) {
  281. ASN1err(ASN1_F_ASN1_STRING_TYPE_NEW, ERR_R_MALLOC_FAILURE);
  282. return (NULL);
  283. }
  284. ret->type = type;
  285. return (ret);
  286. }
  287. void ASN1_STRING_free(ASN1_STRING *a)
  288. {
  289. if (a == NULL)
  290. return;
  291. if (!(a->flags & ASN1_STRING_FLAG_NDEF))
  292. OPENSSL_free(a->data);
  293. if (!(a->flags & ASN1_STRING_FLAG_EMBED))
  294. OPENSSL_free(a);
  295. }
  296. void ASN1_STRING_clear_free(ASN1_STRING *a)
  297. {
  298. if (a == NULL)
  299. return;
  300. if (a->data && !(a->flags & ASN1_STRING_FLAG_NDEF))
  301. OPENSSL_cleanse(a->data, a->length);
  302. ASN1_STRING_free(a);
  303. }
  304. int ASN1_STRING_cmp(const ASN1_STRING *a, const ASN1_STRING *b)
  305. {
  306. int i;
  307. i = (a->length - b->length);
  308. if (i == 0) {
  309. i = memcmp(a->data, b->data, a->length);
  310. if (i == 0)
  311. return (a->type - b->type);
  312. else
  313. return (i);
  314. } else
  315. return (i);
  316. }
  317. int ASN1_STRING_length(const ASN1_STRING *x)
  318. {
  319. return x->length;
  320. }
  321. void ASN1_STRING_length_set(ASN1_STRING *x, int len)
  322. {
  323. x->length = len;
  324. }
  325. int ASN1_STRING_type(ASN1_STRING *x)
  326. {
  327. return x->type;
  328. }
  329. unsigned char *ASN1_STRING_data(ASN1_STRING *x)
  330. {
  331. return x->data;
  332. }