Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 
 
 

589 linhas
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/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. if (!CBB_init(&cbb, 0)) {
  158. return 0;
  159. }
  160. if (!CBB_add_asn1(&cbb, &session, CBS_ASN1_SEQUENCE) ||
  161. !CBB_add_asn1_uint64(&session, SSL_SESSION_ASN1_VERSION) ||
  162. !CBB_add_asn1_uint64(&session, in->ssl_version) ||
  163. !CBB_add_asn1(&session, &child, CBS_ASN1_OCTETSTRING) ||
  164. !CBB_add_u16(&child, (uint16_t)(in->cipher->id & 0xffff)) ||
  165. !CBB_add_asn1(&session, &child, CBS_ASN1_OCTETSTRING) ||
  166. /* The session ID is irrelevant for a session ticket. */
  167. !CBB_add_bytes(&child, in->session_id,
  168. for_ticket ? 0 : in->session_id_length) ||
  169. !CBB_add_asn1(&session, &child, CBS_ASN1_OCTETSTRING) ||
  170. !CBB_add_bytes(&child, in->master_key, in->master_key_length)) {
  171. OPENSSL_PUT_ERROR(SSL, SSL_SESSION_to_bytes_full, ERR_R_MALLOC_FAILURE);
  172. goto err;
  173. }
  174. if (in->time != 0) {
  175. if (!CBB_add_asn1(&session, &child, kTimeTag) ||
  176. !CBB_add_asn1_uint64(&child, in->time)) {
  177. OPENSSL_PUT_ERROR(SSL, SSL_SESSION_to_bytes_full, ERR_R_MALLOC_FAILURE);
  178. goto err;
  179. }
  180. }
  181. if (in->timeout != 0) {
  182. if (!CBB_add_asn1(&session, &child, kTimeoutTag) ||
  183. !CBB_add_asn1_uint64(&child, in->timeout)) {
  184. OPENSSL_PUT_ERROR(SSL, SSL_SESSION_to_bytes_full, ERR_R_MALLOC_FAILURE);
  185. goto err;
  186. }
  187. }
  188. /* The peer certificate is only serialized if the SHA-256 isn't
  189. * serialized instead. */
  190. if (in->peer && !in->peer_sha256_valid) {
  191. uint8_t *buf;
  192. int len = i2d_X509(in->peer, NULL);
  193. if (len < 0) {
  194. goto err;
  195. }
  196. if (!CBB_add_asn1(&session, &child, kPeerTag) ||
  197. !CBB_add_space(&child, &buf, len)) {
  198. OPENSSL_PUT_ERROR(SSL, SSL_SESSION_to_bytes_full, ERR_R_MALLOC_FAILURE);
  199. goto err;
  200. }
  201. if (buf != NULL && i2d_X509(in->peer, &buf) < 0) {
  202. goto err;
  203. }
  204. }
  205. /* Although it is OPTIONAL and usually empty, OpenSSL has
  206. * historically always encoded the sid_ctx. */
  207. if (!CBB_add_asn1(&session, &child, kSessionIDContextTag) ||
  208. !CBB_add_asn1(&child, &child2, CBS_ASN1_OCTETSTRING) ||
  209. !CBB_add_bytes(&child2, in->sid_ctx, in->sid_ctx_length)) {
  210. OPENSSL_PUT_ERROR(SSL, SSL_SESSION_to_bytes_full, ERR_R_MALLOC_FAILURE);
  211. goto err;
  212. }
  213. if (in->verify_result != X509_V_OK) {
  214. if (!CBB_add_asn1(&session, &child, kVerifyResultTag) ||
  215. !CBB_add_asn1_uint64(&child, in->verify_result)) {
  216. OPENSSL_PUT_ERROR(SSL, SSL_SESSION_to_bytes_full, ERR_R_MALLOC_FAILURE);
  217. goto err;
  218. }
  219. }
  220. if (in->tlsext_hostname) {
  221. if (!CBB_add_asn1(&session, &child, kHostNameTag) ||
  222. !CBB_add_asn1(&child, &child2, CBS_ASN1_OCTETSTRING) ||
  223. !CBB_add_bytes(&child2, (const uint8_t *)in->tlsext_hostname,
  224. strlen(in->tlsext_hostname))) {
  225. OPENSSL_PUT_ERROR(SSL, SSL_SESSION_to_bytes_full, ERR_R_MALLOC_FAILURE);
  226. goto err;
  227. }
  228. }
  229. if (in->psk_identity) {
  230. if (!CBB_add_asn1(&session, &child, kPSKIdentityTag) ||
  231. !CBB_add_asn1(&child, &child2, CBS_ASN1_OCTETSTRING) ||
  232. !CBB_add_bytes(&child2, (const uint8_t *)in->psk_identity,
  233. strlen(in->psk_identity))) {
  234. OPENSSL_PUT_ERROR(SSL, SSL_SESSION_to_bytes_full, ERR_R_MALLOC_FAILURE);
  235. goto err;
  236. }
  237. }
  238. if (in->tlsext_tick_lifetime_hint > 0) {
  239. if (!CBB_add_asn1(&session, &child, kTicketLifetimeHintTag) ||
  240. !CBB_add_asn1_uint64(&child, in->tlsext_tick_lifetime_hint)) {
  241. OPENSSL_PUT_ERROR(SSL, SSL_SESSION_to_bytes_full, ERR_R_MALLOC_FAILURE);
  242. goto err;
  243. }
  244. }
  245. if (in->tlsext_tick) {
  246. if (!CBB_add_asn1(&session, &child, kTicketTag) ||
  247. !CBB_add_asn1(&child, &child2, CBS_ASN1_OCTETSTRING) ||
  248. !CBB_add_bytes(&child2, in->tlsext_tick, in->tlsext_ticklen)) {
  249. OPENSSL_PUT_ERROR(SSL, SSL_SESSION_to_bytes_full, ERR_R_MALLOC_FAILURE);
  250. goto err;
  251. }
  252. }
  253. if (in->peer_sha256_valid) {
  254. if (!CBB_add_asn1(&session, &child, kPeerSHA256Tag) ||
  255. !CBB_add_asn1(&child, &child2, CBS_ASN1_OCTETSTRING) ||
  256. !CBB_add_bytes(&child2, in->peer_sha256, sizeof(in->peer_sha256))) {
  257. OPENSSL_PUT_ERROR(SSL, SSL_SESSION_to_bytes_full, ERR_R_MALLOC_FAILURE);
  258. goto err;
  259. }
  260. }
  261. if (in->original_handshake_hash_len > 0) {
  262. if (!CBB_add_asn1(&session, &child, kOriginalHandshakeHashTag) ||
  263. !CBB_add_asn1(&child, &child2, CBS_ASN1_OCTETSTRING) ||
  264. !CBB_add_bytes(&child2, in->original_handshake_hash,
  265. in->original_handshake_hash_len)) {
  266. OPENSSL_PUT_ERROR(SSL, SSL_SESSION_to_bytes_full, ERR_R_MALLOC_FAILURE);
  267. goto err;
  268. }
  269. }
  270. if (in->tlsext_signed_cert_timestamp_list_length > 0) {
  271. if (!CBB_add_asn1(&session, &child, kSignedCertTimestampListTag) ||
  272. !CBB_add_asn1(&child, &child2, CBS_ASN1_OCTETSTRING) ||
  273. !CBB_add_bytes(&child2, in->tlsext_signed_cert_timestamp_list,
  274. in->tlsext_signed_cert_timestamp_list_length)) {
  275. OPENSSL_PUT_ERROR(SSL, SSL_SESSION_to_bytes_full, ERR_R_MALLOC_FAILURE);
  276. goto err;
  277. }
  278. }
  279. if (in->ocsp_response_length > 0) {
  280. if (!CBB_add_asn1(&session, &child, kOCSPResponseTag) ||
  281. !CBB_add_asn1(&child, &child2, CBS_ASN1_OCTETSTRING) ||
  282. !CBB_add_bytes(&child2, in->ocsp_response, in->ocsp_response_length)) {
  283. OPENSSL_PUT_ERROR(SSL, SSL_SESSION_to_bytes_full, ERR_R_MALLOC_FAILURE);
  284. goto err;
  285. }
  286. }
  287. if (in->extended_master_secret) {
  288. if (!CBB_add_asn1(&session, &child, kExtendedMasterSecretTag) ||
  289. !CBB_add_asn1(&child, &child2, CBS_ASN1_BOOLEAN) ||
  290. !CBB_add_u8(&child2, 0xff)) {
  291. OPENSSL_PUT_ERROR(SSL, SSL_SESSION_to_bytes_full, ERR_R_MALLOC_FAILURE);
  292. goto err;
  293. }
  294. }
  295. if (!CBB_finish(&cbb, out_data, out_len)) {
  296. OPENSSL_PUT_ERROR(SSL, SSL_SESSION_to_bytes_full, ERR_R_MALLOC_FAILURE);
  297. goto err;
  298. }
  299. return 1;
  300. err:
  301. CBB_cleanup(&cbb);
  302. return 0;
  303. }
  304. int SSL_SESSION_to_bytes(SSL_SESSION *in, uint8_t **out_data, size_t *out_len) {
  305. return SSL_SESSION_to_bytes_full(in, out_data, out_len, 0);
  306. }
  307. int SSL_SESSION_to_bytes_for_ticket(SSL_SESSION *in, uint8_t **out_data,
  308. size_t *out_len) {
  309. return SSL_SESSION_to_bytes_full(in, out_data, out_len, 1);
  310. }
  311. int i2d_SSL_SESSION(SSL_SESSION *in, uint8_t **pp) {
  312. uint8_t *out;
  313. size_t len;
  314. if (!SSL_SESSION_to_bytes(in, &out, &len)) {
  315. return -1;
  316. }
  317. if (len > INT_MAX) {
  318. OPENSSL_free(out);
  319. OPENSSL_PUT_ERROR(SSL, i2d_SSL_SESSION, ERR_R_OVERFLOW);
  320. return -1;
  321. }
  322. if (pp) {
  323. memcpy(*pp, out, len);
  324. *pp += len;
  325. }
  326. OPENSSL_free(out);
  327. return len;
  328. }
  329. /* d2i_SSL_SESSION_get_string gets an optional ASN.1 OCTET STRING
  330. * explicitly tagged with |tag| from |cbs| and saves it in |*out|. On
  331. * entry, if |*out| is not NULL, it frees the existing contents. If
  332. * the element was not found, it sets |*out| to NULL. It returns one
  333. * on success, whether or not the element was found, and zero on
  334. * decode error. */
  335. static int d2i_SSL_SESSION_get_string(CBS *cbs, char **out, unsigned tag) {
  336. CBS value;
  337. int present;
  338. if (!CBS_get_optional_asn1_octet_string(cbs, &value, &present, tag)) {
  339. OPENSSL_PUT_ERROR(SSL, d2i_SSL_SESSION, SSL_R_INVALID_SSL_SESSION);
  340. return 0;
  341. }
  342. if (present) {
  343. if (CBS_contains_zero_byte(&value)) {
  344. OPENSSL_PUT_ERROR(SSL, d2i_SSL_SESSION, SSL_R_INVALID_SSL_SESSION);
  345. return 0;
  346. }
  347. if (!CBS_strdup(&value, out)) {
  348. OPENSSL_PUT_ERROR(SSL, d2i_SSL_SESSION, ERR_R_MALLOC_FAILURE);
  349. return 0;
  350. }
  351. } else {
  352. OPENSSL_free(*out);
  353. *out = NULL;
  354. }
  355. return 1;
  356. }
  357. /* d2i_SSL_SESSION_get_string gets an optional ASN.1 OCTET STRING
  358. * explicitly tagged with |tag| from |cbs| and stows it in |*out_ptr|
  359. * and |*out_len|. If |*out_ptr| is not NULL, it frees the existing
  360. * contents. On entry, if the element was not found, it sets
  361. * |*out_ptr| to NULL. It returns one on success, whether or not the
  362. * element was found, and zero on decode error. */
  363. static int d2i_SSL_SESSION_get_octet_string(CBS *cbs, uint8_t **out_ptr,
  364. size_t *out_len, unsigned tag) {
  365. CBS value;
  366. if (!CBS_get_optional_asn1_octet_string(cbs, &value, NULL, tag)) {
  367. OPENSSL_PUT_ERROR(SSL, d2i_SSL_SESSION, SSL_R_INVALID_SSL_SESSION);
  368. return 0;
  369. }
  370. if (!CBS_stow(&value, out_ptr, out_len)) {
  371. OPENSSL_PUT_ERROR(SSL, d2i_SSL_SESSION, ERR_R_MALLOC_FAILURE);
  372. return 0;
  373. }
  374. return 1;
  375. }
  376. SSL_SESSION *d2i_SSL_SESSION(SSL_SESSION **a, const uint8_t **pp, long length) {
  377. SSL_SESSION *ret, *allocated = NULL;
  378. CBS cbs, session, cipher, session_id, master_key;
  379. CBS peer, sid_ctx, peer_sha256, original_handshake_hash;
  380. int has_peer, has_peer_sha256, extended_master_secret;
  381. uint64_t version, ssl_version;
  382. uint64_t session_time, timeout, verify_result, ticket_lifetime_hint;
  383. if (a && *a) {
  384. ret = *a;
  385. } else {
  386. ret = allocated = SSL_SESSION_new();
  387. if (allocated == NULL) {
  388. goto err;
  389. }
  390. }
  391. CBS_init(&cbs, *pp, length);
  392. if (!CBS_get_asn1(&cbs, &session, CBS_ASN1_SEQUENCE) ||
  393. !CBS_get_asn1_uint64(&session, &version) ||
  394. !CBS_get_asn1_uint64(&session, &ssl_version) ||
  395. !CBS_get_asn1(&session, &cipher, CBS_ASN1_OCTETSTRING) ||
  396. !CBS_get_asn1(&session, &session_id, CBS_ASN1_OCTETSTRING) ||
  397. !CBS_get_asn1(&session, &master_key, CBS_ASN1_OCTETSTRING) ||
  398. !CBS_get_optional_asn1_uint64(&session, &session_time, kTimeTag,
  399. time(NULL)) ||
  400. !CBS_get_optional_asn1_uint64(&session, &timeout, kTimeoutTag, 3) ||
  401. !CBS_get_optional_asn1(&session, &peer, &has_peer, kPeerTag) ||
  402. !CBS_get_optional_asn1_octet_string(&session, &sid_ctx, NULL,
  403. kSessionIDContextTag) ||
  404. !CBS_get_optional_asn1_uint64(&session, &verify_result, kVerifyResultTag,
  405. X509_V_OK)) {
  406. OPENSSL_PUT_ERROR(SSL, d2i_SSL_SESSION, SSL_R_INVALID_SSL_SESSION);
  407. goto err;
  408. }
  409. if (!d2i_SSL_SESSION_get_string(&session, &ret->tlsext_hostname,
  410. kHostNameTag) ||
  411. !d2i_SSL_SESSION_get_string(&session, &ret->psk_identity,
  412. kPSKIdentityTag)) {
  413. goto err;
  414. }
  415. if (!CBS_get_optional_asn1_uint64(&session, &ticket_lifetime_hint,
  416. kTicketLifetimeHintTag, 0)) {
  417. OPENSSL_PUT_ERROR(SSL, d2i_SSL_SESSION, SSL_R_INVALID_SSL_SESSION);
  418. goto err;
  419. }
  420. if (!d2i_SSL_SESSION_get_octet_string(&session, &ret->tlsext_tick,
  421. &ret->tlsext_ticklen, kTicketTag)) {
  422. goto err;
  423. }
  424. if (!CBS_get_optional_asn1_octet_string(&session, &peer_sha256,
  425. &has_peer_sha256, kPeerSHA256Tag) ||
  426. !CBS_get_optional_asn1_octet_string(&session, &original_handshake_hash,
  427. NULL, kOriginalHandshakeHashTag)) {
  428. OPENSSL_PUT_ERROR(SSL, d2i_SSL_SESSION, SSL_R_INVALID_SSL_SESSION);
  429. goto err;
  430. }
  431. if (!d2i_SSL_SESSION_get_octet_string(
  432. &session, &ret->tlsext_signed_cert_timestamp_list,
  433. &ret->tlsext_signed_cert_timestamp_list_length,
  434. kSignedCertTimestampListTag) ||
  435. !d2i_SSL_SESSION_get_octet_string(
  436. &session, &ret->ocsp_response, &ret->ocsp_response_length,
  437. kOCSPResponseTag)) {
  438. goto err;
  439. }
  440. if (!CBS_get_optional_asn1_bool(&session, &extended_master_secret,
  441. kExtendedMasterSecretTag,
  442. 0 /* default to false */)) {
  443. OPENSSL_PUT_ERROR(SSL, d2i_SSL_SESSION, SSL_R_INVALID_SSL_SESSION);
  444. goto err;
  445. }
  446. ret->extended_master_secret = extended_master_secret;
  447. /* Ignore |version|. The structure version number is ignored. */
  448. /* Only support SSLv3/TLS and DTLS. */
  449. if ((ssl_version >> 8) != SSL3_VERSION_MAJOR &&
  450. (ssl_version >> 8) != (DTLS1_VERSION >> 8)) {
  451. OPENSSL_PUT_ERROR(SSL, d2i_SSL_SESSION, SSL_R_UNKNOWN_SSL_VERSION);
  452. goto err;
  453. }
  454. ret->ssl_version = ssl_version;
  455. uint16_t cipher_value;
  456. if (!CBS_get_u16(&cipher, &cipher_value) || CBS_len(&cipher) != 0) {
  457. OPENSSL_PUT_ERROR(SSL, d2i_SSL_SESSION, SSL_R_CIPHER_CODE_WRONG_LENGTH);
  458. goto err;
  459. }
  460. ret->cipher = ssl3_get_cipher_by_value(cipher_value);
  461. if (ret->cipher == NULL) {
  462. OPENSSL_PUT_ERROR(SSL, d2i_SSL_SESSION, SSL_R_UNSUPPORTED_CIPHER);
  463. goto err;
  464. }
  465. if (CBS_len(&session_id) > SSL3_MAX_SSL_SESSION_ID_LENGTH) {
  466. OPENSSL_PUT_ERROR(SSL, d2i_SSL_SESSION, SSL_R_INVALID_SSL_SESSION);
  467. goto err;
  468. }
  469. memcpy(ret->session_id, CBS_data(&session_id), CBS_len(&session_id));
  470. ret->session_id_length = CBS_len(&session_id);
  471. if (CBS_len(&master_key) > SSL_MAX_MASTER_KEY_LENGTH) {
  472. OPENSSL_PUT_ERROR(SSL, d2i_SSL_SESSION, SSL_R_INVALID_SSL_SESSION);
  473. goto err;
  474. }
  475. memcpy(ret->master_key, CBS_data(&master_key), CBS_len(&master_key));
  476. ret->master_key_length = CBS_len(&master_key);
  477. if (session_time > LONG_MAX ||
  478. timeout > LONG_MAX) {
  479. OPENSSL_PUT_ERROR(SSL, d2i_SSL_SESSION, SSL_R_INVALID_SSL_SESSION);
  480. goto err;
  481. }
  482. ret->time = session_time;
  483. ret->timeout = timeout;
  484. X509_free(ret->peer);
  485. ret->peer = NULL;
  486. if (has_peer) {
  487. const uint8_t *ptr;
  488. ptr = CBS_data(&peer);
  489. ret->peer = d2i_X509(NULL, &ptr, CBS_len(&peer));
  490. if (ret->peer == NULL) {
  491. goto err;
  492. }
  493. if (ptr != CBS_data(&peer) + CBS_len(&peer)) {
  494. OPENSSL_PUT_ERROR(SSL, d2i_SSL_SESSION, SSL_R_INVALID_SSL_SESSION);
  495. goto err;
  496. }
  497. }
  498. if (CBS_len(&sid_ctx) > sizeof(ret->sid_ctx)) {
  499. OPENSSL_PUT_ERROR(SSL, d2i_SSL_SESSION, SSL_R_INVALID_SSL_SESSION);
  500. goto err;
  501. }
  502. memcpy(ret->sid_ctx, CBS_data(&sid_ctx), CBS_len(&sid_ctx));
  503. ret->sid_ctx_length = CBS_len(&sid_ctx);
  504. if (verify_result > LONG_MAX ||
  505. ticket_lifetime_hint > 0xffffffff) {
  506. OPENSSL_PUT_ERROR(SSL, d2i_SSL_SESSION, SSL_R_INVALID_SSL_SESSION);
  507. goto err;
  508. }
  509. ret->verify_result = verify_result;
  510. ret->tlsext_tick_lifetime_hint = ticket_lifetime_hint;
  511. if (has_peer_sha256) {
  512. if (CBS_len(&peer_sha256) != sizeof(ret->peer_sha256)) {
  513. OPENSSL_PUT_ERROR(SSL, d2i_SSL_SESSION, SSL_R_INVALID_SSL_SESSION);
  514. goto err;
  515. }
  516. memcpy(ret->peer_sha256, CBS_data(&peer_sha256), sizeof(ret->peer_sha256));
  517. ret->peer_sha256_valid = 1;
  518. } else {
  519. ret->peer_sha256_valid = 0;
  520. }
  521. if (CBS_len(&original_handshake_hash) >
  522. sizeof(ret->original_handshake_hash)) {
  523. OPENSSL_PUT_ERROR(SSL, d2i_SSL_SESSION, SSL_R_INVALID_SSL_SESSION);
  524. goto err;
  525. }
  526. memcpy(ret->original_handshake_hash, CBS_data(&original_handshake_hash),
  527. CBS_len(&original_handshake_hash));
  528. ret->original_handshake_hash_len = CBS_len(&original_handshake_hash);
  529. if (a) {
  530. *a = ret;
  531. }
  532. *pp = CBS_data(&cbs);
  533. return ret;
  534. err:
  535. SSL_SESSION_free(allocated);
  536. return NULL;
  537. }