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.

Switch OPENSSL_VERSION_NUMBER to 1.1.0. Although we are derived from 1.0.2, we mimic 1.1.0 in some ways around our FOO_up_ref functions and opaque libssl types. This causes some difficulties when porting third-party code as any OPENSSL_VERSION_NUMBER checks for 1.1.0 APIs we have will be wrong. Moreover, adding accessors without changing OPENSSL_VERSION_NUMBER can break external projects. It is common to implement a compatibility version of an accessor under #ifdef as a static function. This then conflicts with our headers if we, unlike OpenSSL 1.0.2, have this function. This change switches OPENSSL_VERSION_NUMBER to 1.1.0 and atomically adds enough accessors for software with 1.1.0 support already. The hope is this will unblock hiding SSL_CTX and SSL_SESSION, which will be especially useful with C++-ficiation. The cost is we will hit some growing pains as more 1.1.0 consumers enter the ecosystem and we converge on the right set of APIs to import from upstream. It does not remove any 1.0.2 APIs, so we will not require that all projects support 1.1.0. The exception is APIs which changed in 1.1.0 but did not change the function signature. Those are breaking changes. Specifically: - SSL_CTX_sess_set_get_cb is now const-correct. - X509_get0_signature is now const-correct. For C++ consumers only, this change temporarily includes an overload hack for SSL_CTX_sess_set_get_cb that keeps the old callback working. This is a workaround for Node not yet supporting OpenSSL 1.1.0. The version number is set at (the as yet unreleased) 1.1.0g to denote that this change includes https://github.com/openssl/openssl/pull/4384. Bug: 91 Change-Id: I5eeb27448a6db4c25c244afac37f9604d9608a76 Reviewed-on: https://boringssl-review.googlesource.com/10340 Commit-Queue: David Benjamin <davidben@google.com> CQ-Verified: CQ bot account: commit-bot@chromium.org <commit-bot@chromium.org> Reviewed-by: Adam Langley <agl@google.com>
преди 8 години
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  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. #include "../internal.h"
  63. /* Cross-module errors from crypto/x509/i2d_pr.c. */
  64. OPENSSL_DECLARE_ERROR_REASON(ASN1, UNSUPPORTED_PUBLIC_KEY_TYPE)
  65. /* Cross-module errors from crypto/x509/algorithm.c. */
  66. OPENSSL_DECLARE_ERROR_REASON(ASN1, CONTEXT_NOT_INITIALISED)
  67. OPENSSL_DECLARE_ERROR_REASON(ASN1, DIGEST_AND_KEY_TYPE_NOT_SUPPORTED)
  68. OPENSSL_DECLARE_ERROR_REASON(ASN1, UNKNOWN_MESSAGE_DIGEST_ALGORITHM)
  69. OPENSSL_DECLARE_ERROR_REASON(ASN1, UNKNOWN_SIGNATURE_ALGORITHM)
  70. OPENSSL_DECLARE_ERROR_REASON(ASN1, WRONG_PUBLIC_KEY_TYPE)
  71. /*
  72. * Cross-module errors from crypto/x509/asn1_gen.c. TODO(davidben): Remove
  73. * these once asn1_gen.c is gone.
  74. */
  75. OPENSSL_DECLARE_ERROR_REASON(ASN1, DEPTH_EXCEEDED)
  76. OPENSSL_DECLARE_ERROR_REASON(ASN1, ILLEGAL_BITSTRING_FORMAT)
  77. OPENSSL_DECLARE_ERROR_REASON(ASN1, ILLEGAL_BOOLEAN)
  78. OPENSSL_DECLARE_ERROR_REASON(ASN1, ILLEGAL_FORMAT)
  79. OPENSSL_DECLARE_ERROR_REASON(ASN1, ILLEGAL_HEX)
  80. OPENSSL_DECLARE_ERROR_REASON(ASN1, ILLEGAL_IMPLICIT_TAG)
  81. OPENSSL_DECLARE_ERROR_REASON(ASN1, ILLEGAL_INTEGER)
  82. OPENSSL_DECLARE_ERROR_REASON(ASN1, ILLEGAL_NESTED_TAGGING)
  83. OPENSSL_DECLARE_ERROR_REASON(ASN1, ILLEGAL_NULL_VALUE)
  84. OPENSSL_DECLARE_ERROR_REASON(ASN1, ILLEGAL_OBJECT)
  85. OPENSSL_DECLARE_ERROR_REASON(ASN1, ILLEGAL_TIME_VALUE)
  86. OPENSSL_DECLARE_ERROR_REASON(ASN1, INTEGER_NOT_ASCII_FORMAT)
  87. OPENSSL_DECLARE_ERROR_REASON(ASN1, INVALID_MODIFIER)
  88. OPENSSL_DECLARE_ERROR_REASON(ASN1, INVALID_NUMBER)
  89. OPENSSL_DECLARE_ERROR_REASON(ASN1, LIST_ERROR)
  90. OPENSSL_DECLARE_ERROR_REASON(ASN1, MISSING_VALUE)
  91. OPENSSL_DECLARE_ERROR_REASON(ASN1, NOT_ASCII_FORMAT)
  92. OPENSSL_DECLARE_ERROR_REASON(ASN1, OBJECT_NOT_ASCII_FORMAT)
  93. OPENSSL_DECLARE_ERROR_REASON(ASN1, SEQUENCE_OR_SET_NEEDS_CONFIG)
  94. OPENSSL_DECLARE_ERROR_REASON(ASN1, TIME_NOT_ASCII_FORMAT)
  95. OPENSSL_DECLARE_ERROR_REASON(ASN1, UNKNOWN_FORMAT)
  96. OPENSSL_DECLARE_ERROR_REASON(ASN1, UNKNOWN_TAG)
  97. OPENSSL_DECLARE_ERROR_REASON(ASN1, UNSUPPORTED_TYPE)
  98. static int asn1_get_length(const unsigned char **pp, int *inf, long *rl,
  99. long max);
  100. static void asn1_put_length(unsigned char **pp, int length);
  101. int ASN1_get_object(const unsigned char **pp, long *plength, int *ptag,
  102. int *pclass, long omax)
  103. {
  104. int i, ret;
  105. long l;
  106. const unsigned char *p = *pp;
  107. int tag, xclass, inf;
  108. long max = omax;
  109. if (!max)
  110. goto err;
  111. ret = (*p & V_ASN1_CONSTRUCTED);
  112. xclass = (*p & V_ASN1_PRIVATE);
  113. i = *p & V_ASN1_PRIMITIVE_TAG;
  114. if (i == V_ASN1_PRIMITIVE_TAG) { /* high-tag */
  115. p++;
  116. if (--max == 0)
  117. goto err;
  118. l = 0;
  119. while (*p & 0x80) {
  120. l <<= 7L;
  121. l |= *(p++) & 0x7f;
  122. if (--max == 0)
  123. goto err;
  124. if (l > (INT_MAX >> 7L))
  125. goto err;
  126. }
  127. l <<= 7L;
  128. l |= *(p++) & 0x7f;
  129. tag = (int)l;
  130. if (--max == 0)
  131. goto err;
  132. } else {
  133. tag = i;
  134. p++;
  135. if (--max == 0)
  136. goto err;
  137. }
  138. /* To avoid ambiguity with V_ASN1_NEG, impose a limit on universal tags. */
  139. if (xclass == V_ASN1_UNIVERSAL && tag > V_ASN1_MAX_UNIVERSAL)
  140. goto err;
  141. *ptag = tag;
  142. *pclass = xclass;
  143. if (!asn1_get_length(&p, &inf, plength, max))
  144. goto err;
  145. if (inf && !(ret & V_ASN1_CONSTRUCTED))
  146. goto err;
  147. #if 0
  148. fprintf(stderr, "p=%d + *plength=%ld > omax=%ld + *pp=%d (%d > %d)\n",
  149. (int)p, *plength, omax, (int)*pp, (int)(p + *plength),
  150. (int)(omax + *pp));
  151. #endif
  152. if (*plength > (omax - (p - *pp))) {
  153. OPENSSL_PUT_ERROR(ASN1, ASN1_R_TOO_LONG);
  154. /*
  155. * Set this so that even if things are not long enough the values are
  156. * set correctly
  157. */
  158. ret |= 0x80;
  159. }
  160. *pp = p;
  161. return (ret | inf);
  162. err:
  163. OPENSSL_PUT_ERROR(ASN1, ASN1_R_HEADER_TOO_LONG);
  164. return (0x80);
  165. }
  166. static int asn1_get_length(const unsigned char **pp, int *inf, long *rl,
  167. long max)
  168. {
  169. const unsigned char *p = *pp;
  170. unsigned long ret = 0;
  171. unsigned long i;
  172. if (max-- < 1)
  173. return 0;
  174. if (*p == 0x80) {
  175. *inf = 1;
  176. ret = 0;
  177. p++;
  178. } else {
  179. *inf = 0;
  180. i = *p & 0x7f;
  181. if (*(p++) & 0x80) {
  182. if (i > sizeof(ret) || max < (long)i)
  183. return 0;
  184. while (i-- > 0) {
  185. ret <<= 8L;
  186. ret |= *(p++);
  187. }
  188. } else
  189. ret = i;
  190. }
  191. if (ret > LONG_MAX)
  192. return 0;
  193. *pp = p;
  194. *rl = (long)ret;
  195. return 1;
  196. }
  197. /*
  198. * class 0 is constructed constructed == 2 for indefinite length constructed
  199. */
  200. void ASN1_put_object(unsigned char **pp, int constructed, int length, int tag,
  201. int xclass)
  202. {
  203. unsigned char *p = *pp;
  204. int i, ttag;
  205. i = (constructed) ? V_ASN1_CONSTRUCTED : 0;
  206. i |= (xclass & V_ASN1_PRIVATE);
  207. if (tag < 31)
  208. *(p++) = i | (tag & V_ASN1_PRIMITIVE_TAG);
  209. else {
  210. *(p++) = i | V_ASN1_PRIMITIVE_TAG;
  211. for (i = 0, ttag = tag; ttag > 0; i++)
  212. ttag >>= 7;
  213. ttag = i;
  214. while (i-- > 0) {
  215. p[i] = tag & 0x7f;
  216. if (i != (ttag - 1))
  217. p[i] |= 0x80;
  218. tag >>= 7;
  219. }
  220. p += ttag;
  221. }
  222. if (constructed == 2)
  223. *(p++) = 0x80;
  224. else
  225. asn1_put_length(&p, length);
  226. *pp = p;
  227. }
  228. int ASN1_put_eoc(unsigned char **pp)
  229. {
  230. unsigned char *p = *pp;
  231. *p++ = 0;
  232. *p++ = 0;
  233. *pp = p;
  234. return 2;
  235. }
  236. static void asn1_put_length(unsigned char **pp, int length)
  237. {
  238. unsigned char *p = *pp;
  239. int i, l;
  240. if (length <= 127)
  241. *(p++) = (unsigned char)length;
  242. else {
  243. l = length;
  244. for (i = 0; l > 0; i++)
  245. l >>= 8;
  246. *(p++) = i | 0x80;
  247. l = i;
  248. while (i-- > 0) {
  249. p[i] = length & 0xff;
  250. length >>= 8;
  251. }
  252. p += l;
  253. }
  254. *pp = p;
  255. }
  256. int ASN1_object_size(int constructed, int length, int tag)
  257. {
  258. int ret = 1;
  259. if (length < 0)
  260. return -1;
  261. if (tag >= 31) {
  262. while (tag > 0) {
  263. tag >>= 7;
  264. ret++;
  265. }
  266. }
  267. if (constructed == 2) {
  268. ret += 3;
  269. } else {
  270. ret++;
  271. if (length > 127) {
  272. int tmplen = length;
  273. while (tmplen > 0) {
  274. tmplen >>= 8;
  275. ret++;
  276. }
  277. }
  278. }
  279. if (ret >= INT_MAX - length)
  280. return -1;
  281. return ret + length;
  282. }
  283. int ASN1_STRING_copy(ASN1_STRING *dst, const ASN1_STRING *str)
  284. {
  285. if (str == NULL)
  286. return 0;
  287. dst->type = str->type;
  288. if (!ASN1_STRING_set(dst, str->data, str->length))
  289. return 0;
  290. dst->flags = str->flags;
  291. return 1;
  292. }
  293. ASN1_STRING *ASN1_STRING_dup(const ASN1_STRING *str)
  294. {
  295. ASN1_STRING *ret;
  296. if (!str)
  297. return NULL;
  298. ret = ASN1_STRING_new();
  299. if (!ret)
  300. return NULL;
  301. if (!ASN1_STRING_copy(ret, str)) {
  302. ASN1_STRING_free(ret);
  303. return NULL;
  304. }
  305. return ret;
  306. }
  307. int ASN1_STRING_set(ASN1_STRING *str, const void *_data, int len)
  308. {
  309. unsigned char *c;
  310. const char *data = _data;
  311. if (len < 0) {
  312. if (data == NULL)
  313. return (0);
  314. else
  315. len = strlen(data);
  316. }
  317. if ((str->length <= len) || (str->data == NULL)) {
  318. c = str->data;
  319. if (c == NULL)
  320. str->data = OPENSSL_malloc(len + 1);
  321. else
  322. str->data = OPENSSL_realloc(c, len + 1);
  323. if (str->data == NULL) {
  324. OPENSSL_PUT_ERROR(ASN1, ERR_R_MALLOC_FAILURE);
  325. str->data = c;
  326. return (0);
  327. }
  328. }
  329. str->length = len;
  330. if (data != NULL) {
  331. OPENSSL_memcpy(str->data, data, len);
  332. /* an allowance for strings :-) */
  333. str->data[len] = '\0';
  334. }
  335. return (1);
  336. }
  337. void ASN1_STRING_set0(ASN1_STRING *str, void *data, int len)
  338. {
  339. if (str->data)
  340. OPENSSL_free(str->data);
  341. str->data = data;
  342. str->length = len;
  343. }
  344. ASN1_STRING *ASN1_STRING_new(void)
  345. {
  346. return (ASN1_STRING_type_new(V_ASN1_OCTET_STRING));
  347. }
  348. ASN1_STRING *ASN1_STRING_type_new(int type)
  349. {
  350. ASN1_STRING *ret;
  351. ret = (ASN1_STRING *)OPENSSL_malloc(sizeof(ASN1_STRING));
  352. if (ret == NULL) {
  353. OPENSSL_PUT_ERROR(ASN1, ERR_R_MALLOC_FAILURE);
  354. return (NULL);
  355. }
  356. ret->length = 0;
  357. ret->type = type;
  358. ret->data = NULL;
  359. ret->flags = 0;
  360. return (ret);
  361. }
  362. void ASN1_STRING_free(ASN1_STRING *a)
  363. {
  364. if (a == NULL)
  365. return;
  366. if (a->data && !(a->flags & ASN1_STRING_FLAG_NDEF))
  367. OPENSSL_free(a->data);
  368. OPENSSL_free(a);
  369. }
  370. int ASN1_STRING_cmp(const ASN1_STRING *a, const ASN1_STRING *b)
  371. {
  372. int i;
  373. i = (a->length - b->length);
  374. if (i == 0) {
  375. i = OPENSSL_memcmp(a->data, b->data, a->length);
  376. if (i == 0)
  377. return (a->type - b->type);
  378. else
  379. return (i);
  380. } else
  381. return (i);
  382. }
  383. int ASN1_STRING_length(const ASN1_STRING *x)
  384. {
  385. return M_ASN1_STRING_length(x);
  386. }
  387. void ASN1_STRING_length_set(ASN1_STRING *x, int len)
  388. {
  389. M_ASN1_STRING_length_set(x, len);
  390. return;
  391. }
  392. int ASN1_STRING_type(ASN1_STRING *x)
  393. {
  394. return M_ASN1_STRING_type(x);
  395. }
  396. unsigned char *ASN1_STRING_data(ASN1_STRING *x)
  397. {
  398. return M_ASN1_STRING_data(x);
  399. }
  400. const unsigned char *ASN1_STRING_get0_data(const ASN1_STRING *x)
  401. {
  402. return x->data;
  403. }