asn1_lib.c 8.7 KB

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