Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 
 
 
 
 

600 rader
22 KiB

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