25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.

ssl_asn1.c 22 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618
  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. */
  57. /* ====================================================================
  58. * Copyright 2005 Nokia. All rights reserved.
  59. *
  60. * The portions of the attached software ("Contribution") is developed by
  61. * Nokia Corporation and is licensed pursuant to the OpenSSL open source
  62. * license.
  63. *
  64. * The Contribution, originally written by Mika Kousa and Pasi Eronen of
  65. * Nokia Corporation, consists of the "PSK" (Pre-Shared Key) ciphersuites
  66. * support (see RFC 4279) to OpenSSL.
  67. *
  68. * No patent licenses or other rights except those expressly stated in
  69. * the OpenSSL open source license shall be deemed granted or received
  70. * expressly, by implication, estoppel, or otherwise.
  71. *
  72. * No assurances are provided by Nokia that the Contribution does not
  73. * infringe the patent or other intellectual property rights of any third
  74. * party or that the license provides you with all the necessary rights
  75. * to make use of the Contribution.
  76. *
  77. * THE SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. IN
  78. * ADDITION TO THE DISCLAIMERS INCLUDED IN THE LICENSE, NOKIA
  79. * SPECIFICALLY DISCLAIMS ANY LIABILITY FOR CLAIMS BROUGHT BY YOU OR ANY
  80. * OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS OR
  81. * OTHERWISE. */
  82. #include <limits.h>
  83. #include <string.h>
  84. #include <openssl/bytestring.h>
  85. #include <openssl/err.h>
  86. #include <openssl/mem.h>
  87. #include <openssl/x509.h>
  88. #include "internal.h"
  89. /* An SSL_SESSION is serialized as the following ASN.1 structure:
  90. *
  91. * SSLSession ::= SEQUENCE {
  92. * version INTEGER (1), -- ignored
  93. * sslVersion INTEGER, -- protocol version number
  94. * cipher OCTET STRING, -- two bytes long
  95. * sessionID OCTET STRING,
  96. * masterKey OCTET STRING,
  97. * time [1] INTEGER OPTIONAL, -- seconds since UNIX epoch
  98. * timeout [2] INTEGER OPTIONAL, -- in seconds
  99. * peer [3] Certificate OPTIONAL,
  100. * sessionIDContext [4] OCTET STRING OPTIONAL,
  101. * verifyResult [5] INTEGER OPTIONAL, -- one of X509_V_* codes
  102. * hostName [6] OCTET STRING OPTIONAL,
  103. * -- from server_name extension
  104. * pskIdentity [8] OCTET STRING OPTIONAL,
  105. * ticketLifetimeHint [9] INTEGER OPTIONAL, -- client-only
  106. * ticket [10] OCTET STRING OPTIONAL, -- client-only
  107. * peerSHA256 [13] OCTET STRING OPTIONAL,
  108. * originalHandshakeHash [14] OCTET STRING OPTIONAL,
  109. * signedCertTimestampList [15] OCTET STRING OPTIONAL,
  110. * -- contents of SCT extension
  111. * ocspResponse [16] OCTET STRING OPTIONAL,
  112. * -- stapled OCSP response from the server
  113. * extendedMasterSecret [17] BOOLEAN OPTIONAL,
  114. * }
  115. *
  116. * Note: historically this serialization has included other optional
  117. * fields. Their presense is currently treated as a parse error:
  118. *
  119. * keyArg [0] IMPLICIT OCTET STRING OPTIONAL,
  120. * pskIdentityHint [7] OCTET STRING OPTIONAL,
  121. * compressionMethod [11] OCTET STRING OPTIONAL,
  122. * srpUsername [12] OCTET STRING OPTIONAL, */
  123. static const int kTimeTag =
  124. CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 1;
  125. static const int kTimeoutTag =
  126. CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 2;
  127. static const int kPeerTag =
  128. CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 3;
  129. static const int kSessionIDContextTag =
  130. CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 4;
  131. static const int kVerifyResultTag =
  132. CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 5;
  133. static const int kHostNameTag =
  134. CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 6;
  135. static const int kPSKIdentityTag =
  136. CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 8;
  137. static const int kTicketLifetimeHintTag =
  138. CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 9;
  139. static const int kTicketTag =
  140. CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 10;
  141. static const int kPeerSHA256Tag =
  142. CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 13;
  143. static const int kOriginalHandshakeHashTag =
  144. CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 14;
  145. static const int kSignedCertTimestampListTag =
  146. CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 15;
  147. static const int kOCSPResponseTag =
  148. CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 16;
  149. static const int kExtendedMasterSecretTag =
  150. CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 17;
  151. static int SSL_SESSION_to_bytes_full(SSL_SESSION *in, uint8_t **out_data,
  152. size_t *out_len, int for_ticket) {
  153. CBB cbb, session, child, child2;
  154. if (in == NULL || in->cipher == NULL) {
  155. return 0;
  156. }
  157. CBB_zero(&cbb);
  158. if (!CBB_init(&cbb, 0) ||
  159. !CBB_add_asn1(&cbb, &session, CBS_ASN1_SEQUENCE) ||
  160. !CBB_add_asn1_uint64(&session, SSL_SESSION_ASN1_VERSION) ||
  161. !CBB_add_asn1_uint64(&session, in->ssl_version) ||
  162. !CBB_add_asn1(&session, &child, CBS_ASN1_OCTETSTRING) ||
  163. !CBB_add_u16(&child, (uint16_t)(in->cipher->id & 0xffff)) ||
  164. !CBB_add_asn1(&session, &child, CBS_ASN1_OCTETSTRING) ||
  165. /* The session ID is irrelevant for a session ticket. */
  166. !CBB_add_bytes(&child, in->session_id,
  167. for_ticket ? 0 : in->session_id_length) ||
  168. !CBB_add_asn1(&session, &child, CBS_ASN1_OCTETSTRING) ||
  169. !CBB_add_bytes(&child, in->master_key, in->master_key_length)) {
  170. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  171. goto err;
  172. }
  173. if (in->time != 0) {
  174. if (!CBB_add_asn1(&session, &child, kTimeTag) ||
  175. !CBB_add_asn1_uint64(&child, in->time)) {
  176. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  177. goto err;
  178. }
  179. }
  180. if (in->timeout != 0) {
  181. if (!CBB_add_asn1(&session, &child, kTimeoutTag) ||
  182. !CBB_add_asn1_uint64(&child, in->timeout)) {
  183. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  184. goto err;
  185. }
  186. }
  187. /* The peer certificate is only serialized if the SHA-256 isn't
  188. * serialized instead. */
  189. if (in->peer && !in->peer_sha256_valid) {
  190. uint8_t *buf;
  191. int len = i2d_X509(in->peer, NULL);
  192. if (len < 0) {
  193. goto err;
  194. }
  195. if (!CBB_add_asn1(&session, &child, kPeerTag) ||
  196. !CBB_add_space(&child, &buf, len)) {
  197. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  198. goto err;
  199. }
  200. if (buf != NULL && i2d_X509(in->peer, &buf) < 0) {
  201. goto err;
  202. }
  203. }
  204. /* Although it is OPTIONAL and usually empty, OpenSSL has
  205. * historically always encoded the sid_ctx. */
  206. if (!CBB_add_asn1(&session, &child, kSessionIDContextTag) ||
  207. !CBB_add_asn1(&child, &child2, CBS_ASN1_OCTETSTRING) ||
  208. !CBB_add_bytes(&child2, in->sid_ctx, in->sid_ctx_length)) {
  209. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  210. goto err;
  211. }
  212. if (in->verify_result != X509_V_OK) {
  213. if (!CBB_add_asn1(&session, &child, kVerifyResultTag) ||
  214. !CBB_add_asn1_uint64(&child, in->verify_result)) {
  215. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  216. goto err;
  217. }
  218. }
  219. if (in->tlsext_hostname) {
  220. if (!CBB_add_asn1(&session, &child, kHostNameTag) ||
  221. !CBB_add_asn1(&child, &child2, CBS_ASN1_OCTETSTRING) ||
  222. !CBB_add_bytes(&child2, (const uint8_t *)in->tlsext_hostname,
  223. strlen(in->tlsext_hostname))) {
  224. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  225. goto err;
  226. }
  227. }
  228. if (in->psk_identity) {
  229. if (!CBB_add_asn1(&session, &child, kPSKIdentityTag) ||
  230. !CBB_add_asn1(&child, &child2, CBS_ASN1_OCTETSTRING) ||
  231. !CBB_add_bytes(&child2, (const uint8_t *)in->psk_identity,
  232. strlen(in->psk_identity))) {
  233. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  234. goto err;
  235. }
  236. }
  237. if (in->tlsext_tick_lifetime_hint > 0) {
  238. if (!CBB_add_asn1(&session, &child, kTicketLifetimeHintTag) ||
  239. !CBB_add_asn1_uint64(&child, in->tlsext_tick_lifetime_hint)) {
  240. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  241. goto err;
  242. }
  243. }
  244. if (in->tlsext_tick && !for_ticket) {
  245. if (!CBB_add_asn1(&session, &child, kTicketTag) ||
  246. !CBB_add_asn1(&child, &child2, CBS_ASN1_OCTETSTRING) ||
  247. !CBB_add_bytes(&child2, in->tlsext_tick, in->tlsext_ticklen)) {
  248. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  249. goto err;
  250. }
  251. }
  252. if (in->peer_sha256_valid) {
  253. if (!CBB_add_asn1(&session, &child, kPeerSHA256Tag) ||
  254. !CBB_add_asn1(&child, &child2, CBS_ASN1_OCTETSTRING) ||
  255. !CBB_add_bytes(&child2, in->peer_sha256, sizeof(in->peer_sha256))) {
  256. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  257. goto err;
  258. }
  259. }
  260. if (in->original_handshake_hash_len > 0) {
  261. if (!CBB_add_asn1(&session, &child, kOriginalHandshakeHashTag) ||
  262. !CBB_add_asn1(&child, &child2, CBS_ASN1_OCTETSTRING) ||
  263. !CBB_add_bytes(&child2, in->original_handshake_hash,
  264. in->original_handshake_hash_len)) {
  265. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  266. goto err;
  267. }
  268. }
  269. if (in->tlsext_signed_cert_timestamp_list_length > 0) {
  270. if (!CBB_add_asn1(&session, &child, kSignedCertTimestampListTag) ||
  271. !CBB_add_asn1(&child, &child2, CBS_ASN1_OCTETSTRING) ||
  272. !CBB_add_bytes(&child2, in->tlsext_signed_cert_timestamp_list,
  273. in->tlsext_signed_cert_timestamp_list_length)) {
  274. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  275. goto err;
  276. }
  277. }
  278. if (in->ocsp_response_length > 0) {
  279. if (!CBB_add_asn1(&session, &child, kOCSPResponseTag) ||
  280. !CBB_add_asn1(&child, &child2, CBS_ASN1_OCTETSTRING) ||
  281. !CBB_add_bytes(&child2, in->ocsp_response, in->ocsp_response_length)) {
  282. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  283. goto err;
  284. }
  285. }
  286. if (in->extended_master_secret) {
  287. if (!CBB_add_asn1(&session, &child, kExtendedMasterSecretTag) ||
  288. !CBB_add_asn1(&child, &child2, CBS_ASN1_BOOLEAN) ||
  289. !CBB_add_u8(&child2, 0xff)) {
  290. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  291. goto err;
  292. }
  293. }
  294. if (!CBB_finish(&cbb, out_data, out_len)) {
  295. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  296. goto err;
  297. }
  298. return 1;
  299. err:
  300. CBB_cleanup(&cbb);
  301. return 0;
  302. }
  303. int SSL_SESSION_to_bytes(SSL_SESSION *in, uint8_t **out_data, size_t *out_len) {
  304. return SSL_SESSION_to_bytes_full(in, out_data, out_len, 0);
  305. }
  306. int SSL_SESSION_to_bytes_for_ticket(SSL_SESSION *in, uint8_t **out_data,
  307. size_t *out_len) {
  308. return SSL_SESSION_to_bytes_full(in, out_data, out_len, 1);
  309. }
  310. int i2d_SSL_SESSION(SSL_SESSION *in, uint8_t **pp) {
  311. uint8_t *out;
  312. size_t len;
  313. if (!SSL_SESSION_to_bytes(in, &out, &len)) {
  314. return -1;
  315. }
  316. if (len > INT_MAX) {
  317. OPENSSL_free(out);
  318. OPENSSL_PUT_ERROR(SSL, ERR_R_OVERFLOW);
  319. return -1;
  320. }
  321. if (pp) {
  322. memcpy(*pp, out, len);
  323. *pp += len;
  324. }
  325. OPENSSL_free(out);
  326. return len;
  327. }
  328. /* SSL_SESSION_parse_string gets an optional ASN.1 OCTET STRING
  329. * explicitly tagged with |tag| from |cbs| and saves it in |*out|. On
  330. * entry, if |*out| is not NULL, it frees the existing contents. If
  331. * the element was not found, it sets |*out| to NULL. It returns one
  332. * on success, whether or not the element was found, and zero on
  333. * decode error. */
  334. static int SSL_SESSION_parse_string(CBS *cbs, char **out, unsigned tag) {
  335. CBS value;
  336. int present;
  337. if (!CBS_get_optional_asn1_octet_string(cbs, &value, &present, tag)) {
  338. OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
  339. return 0;
  340. }
  341. if (present) {
  342. if (CBS_contains_zero_byte(&value)) {
  343. OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
  344. return 0;
  345. }
  346. if (!CBS_strdup(&value, out)) {
  347. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  348. return 0;
  349. }
  350. } else {
  351. OPENSSL_free(*out);
  352. *out = NULL;
  353. }
  354. return 1;
  355. }
  356. /* SSL_SESSION_parse_string gets an optional ASN.1 OCTET STRING
  357. * explicitly tagged with |tag| from |cbs| and stows it in |*out_ptr|
  358. * and |*out_len|. If |*out_ptr| is not NULL, it frees the existing
  359. * contents. On entry, if the element was not found, it sets
  360. * |*out_ptr| to NULL. It returns one on success, whether or not the
  361. * element was found, and zero on decode error. */
  362. static int SSL_SESSION_parse_octet_string(CBS *cbs, uint8_t **out_ptr,
  363. size_t *out_len, unsigned tag) {
  364. CBS value;
  365. if (!CBS_get_optional_asn1_octet_string(cbs, &value, NULL, tag)) {
  366. OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
  367. return 0;
  368. }
  369. if (!CBS_stow(&value, out_ptr, out_len)) {
  370. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  371. return 0;
  372. }
  373. return 1;
  374. }
  375. static SSL_SESSION *SSL_SESSION_parse(CBS *cbs) {
  376. SSL_SESSION *ret = NULL;
  377. CBS session, cipher, session_id, master_key;
  378. CBS peer, sid_ctx, peer_sha256, original_handshake_hash;
  379. int has_peer, has_peer_sha256, extended_master_secret;
  380. uint64_t version, ssl_version;
  381. uint64_t session_time, timeout, verify_result, ticket_lifetime_hint;
  382. ret = SSL_SESSION_new();
  383. if (ret == NULL) {
  384. goto err;
  385. }
  386. if (!CBS_get_asn1(cbs, &session, CBS_ASN1_SEQUENCE) ||
  387. !CBS_get_asn1_uint64(&session, &version) ||
  388. !CBS_get_asn1_uint64(&session, &ssl_version) ||
  389. !CBS_get_asn1(&session, &cipher, CBS_ASN1_OCTETSTRING) ||
  390. !CBS_get_asn1(&session, &session_id, CBS_ASN1_OCTETSTRING) ||
  391. !CBS_get_asn1(&session, &master_key, CBS_ASN1_OCTETSTRING) ||
  392. !CBS_get_optional_asn1_uint64(&session, &session_time, kTimeTag,
  393. time(NULL)) ||
  394. !CBS_get_optional_asn1_uint64(&session, &timeout, kTimeoutTag, 3) ||
  395. !CBS_get_optional_asn1(&session, &peer, &has_peer, kPeerTag) ||
  396. !CBS_get_optional_asn1_octet_string(&session, &sid_ctx, NULL,
  397. kSessionIDContextTag) ||
  398. !CBS_get_optional_asn1_uint64(&session, &verify_result, kVerifyResultTag,
  399. X509_V_OK)) {
  400. OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
  401. goto err;
  402. }
  403. if (!SSL_SESSION_parse_string(&session, &ret->tlsext_hostname,
  404. kHostNameTag) ||
  405. !SSL_SESSION_parse_string(&session, &ret->psk_identity,
  406. kPSKIdentityTag)) {
  407. goto err;
  408. }
  409. if (!CBS_get_optional_asn1_uint64(&session, &ticket_lifetime_hint,
  410. kTicketLifetimeHintTag, 0)) {
  411. OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
  412. goto err;
  413. }
  414. if (!SSL_SESSION_parse_octet_string(&session, &ret->tlsext_tick,
  415. &ret->tlsext_ticklen, kTicketTag)) {
  416. goto err;
  417. }
  418. if (!CBS_get_optional_asn1_octet_string(&session, &peer_sha256,
  419. &has_peer_sha256, kPeerSHA256Tag) ||
  420. !CBS_get_optional_asn1_octet_string(&session, &original_handshake_hash,
  421. NULL, kOriginalHandshakeHashTag)) {
  422. OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
  423. goto err;
  424. }
  425. if (!SSL_SESSION_parse_octet_string(
  426. &session, &ret->tlsext_signed_cert_timestamp_list,
  427. &ret->tlsext_signed_cert_timestamp_list_length,
  428. kSignedCertTimestampListTag) ||
  429. !SSL_SESSION_parse_octet_string(
  430. &session, &ret->ocsp_response, &ret->ocsp_response_length,
  431. kOCSPResponseTag)) {
  432. goto err;
  433. }
  434. if (!CBS_get_optional_asn1_bool(&session, &extended_master_secret,
  435. kExtendedMasterSecretTag,
  436. 0 /* default to false */) ||
  437. CBS_len(&session) != 0) {
  438. OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
  439. goto err;
  440. }
  441. ret->extended_master_secret = extended_master_secret;
  442. if (version != SSL_SESSION_ASN1_VERSION) {
  443. OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
  444. goto err;
  445. }
  446. /* Only support SSLv3/TLS and DTLS. */
  447. if ((ssl_version >> 8) != SSL3_VERSION_MAJOR &&
  448. (ssl_version >> 8) != (DTLS1_VERSION >> 8)) {
  449. OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_SSL_VERSION);
  450. goto err;
  451. }
  452. ret->ssl_version = ssl_version;
  453. uint16_t cipher_value;
  454. if (!CBS_get_u16(&cipher, &cipher_value) || CBS_len(&cipher) != 0) {
  455. OPENSSL_PUT_ERROR(SSL, SSL_R_CIPHER_CODE_WRONG_LENGTH);
  456. goto err;
  457. }
  458. ret->cipher = SSL_get_cipher_by_value(cipher_value);
  459. if (ret->cipher == NULL) {
  460. OPENSSL_PUT_ERROR(SSL, SSL_R_UNSUPPORTED_CIPHER);
  461. goto err;
  462. }
  463. if (CBS_len(&session_id) > SSL3_MAX_SSL_SESSION_ID_LENGTH) {
  464. OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
  465. goto err;
  466. }
  467. memcpy(ret->session_id, CBS_data(&session_id), CBS_len(&session_id));
  468. ret->session_id_length = CBS_len(&session_id);
  469. if (CBS_len(&master_key) > SSL_MAX_MASTER_KEY_LENGTH) {
  470. OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
  471. goto err;
  472. }
  473. memcpy(ret->master_key, CBS_data(&master_key), CBS_len(&master_key));
  474. ret->master_key_length = CBS_len(&master_key);
  475. if (session_time > LONG_MAX ||
  476. timeout > LONG_MAX) {
  477. OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
  478. goto err;
  479. }
  480. ret->time = session_time;
  481. ret->timeout = timeout;
  482. X509_free(ret->peer);
  483. ret->peer = NULL;
  484. if (has_peer) {
  485. const uint8_t *ptr;
  486. ptr = CBS_data(&peer);
  487. ret->peer = d2i_X509(NULL, &ptr, CBS_len(&peer));
  488. if (ret->peer == NULL) {
  489. goto err;
  490. }
  491. if (ptr != CBS_data(&peer) + CBS_len(&peer)) {
  492. OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
  493. goto err;
  494. }
  495. }
  496. if (CBS_len(&sid_ctx) > sizeof(ret->sid_ctx)) {
  497. OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
  498. goto err;
  499. }
  500. memcpy(ret->sid_ctx, CBS_data(&sid_ctx), CBS_len(&sid_ctx));
  501. ret->sid_ctx_length = CBS_len(&sid_ctx);
  502. if (verify_result > LONG_MAX ||
  503. ticket_lifetime_hint > 0xffffffff) {
  504. OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
  505. goto err;
  506. }
  507. ret->verify_result = verify_result;
  508. ret->tlsext_tick_lifetime_hint = ticket_lifetime_hint;
  509. if (has_peer_sha256) {
  510. if (CBS_len(&peer_sha256) != sizeof(ret->peer_sha256)) {
  511. OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
  512. goto err;
  513. }
  514. memcpy(ret->peer_sha256, CBS_data(&peer_sha256), sizeof(ret->peer_sha256));
  515. ret->peer_sha256_valid = 1;
  516. } else {
  517. ret->peer_sha256_valid = 0;
  518. }
  519. if (CBS_len(&original_handshake_hash) >
  520. sizeof(ret->original_handshake_hash)) {
  521. OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
  522. goto err;
  523. }
  524. memcpy(ret->original_handshake_hash, CBS_data(&original_handshake_hash),
  525. CBS_len(&original_handshake_hash));
  526. ret->original_handshake_hash_len = CBS_len(&original_handshake_hash);
  527. return ret;
  528. err:
  529. SSL_SESSION_free(ret);
  530. return NULL;
  531. }
  532. SSL_SESSION *SSL_SESSION_from_bytes(const uint8_t *in, size_t in_len) {
  533. CBS cbs;
  534. CBS_init(&cbs, in, in_len);
  535. SSL_SESSION *ret = SSL_SESSION_parse(&cbs);
  536. if (ret == NULL) {
  537. return NULL;
  538. }
  539. if (CBS_len(&cbs) != 0) {
  540. OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
  541. SSL_SESSION_free(ret);
  542. return NULL;
  543. }
  544. return ret;
  545. }
  546. SSL_SESSION *d2i_SSL_SESSION(SSL_SESSION **a, const uint8_t **pp, long length) {
  547. if (length < 0) {
  548. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  549. return NULL;
  550. }
  551. CBS cbs;
  552. CBS_init(&cbs, *pp, length);
  553. SSL_SESSION *ret = SSL_SESSION_parse(&cbs);
  554. if (ret == NULL) {
  555. return NULL;
  556. }
  557. if (a) {
  558. SSL_SESSION_free(*a);
  559. *a = ret;
  560. }
  561. *pp = CBS_data(&cbs);
  562. return ret;
  563. }