Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 
 
 
 
 

624 Zeilen
23 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 && !for_ticket) {
  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. /* SSL_SESSION_parse_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 SSL_SESSION_parse_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, SSL_SESSION_parse_string, SSL_R_INVALID_SSL_SESSION);
  340. return 0;
  341. }
  342. if (present) {
  343. if (CBS_contains_zero_byte(&value)) {
  344. OPENSSL_PUT_ERROR(SSL, SSL_SESSION_parse_string,
  345. SSL_R_INVALID_SSL_SESSION);
  346. return 0;
  347. }
  348. if (!CBS_strdup(&value, out)) {
  349. OPENSSL_PUT_ERROR(SSL, SSL_SESSION_parse_string, ERR_R_MALLOC_FAILURE);
  350. return 0;
  351. }
  352. } else {
  353. OPENSSL_free(*out);
  354. *out = NULL;
  355. }
  356. return 1;
  357. }
  358. /* SSL_SESSION_parse_string gets an optional ASN.1 OCTET STRING
  359. * explicitly tagged with |tag| from |cbs| and stows it in |*out_ptr|
  360. * and |*out_len|. If |*out_ptr| is not NULL, it frees the existing
  361. * contents. On entry, if the element was not found, it sets
  362. * |*out_ptr| to NULL. It returns one on success, whether or not the
  363. * element was found, and zero on decode error. */
  364. static int SSL_SESSION_parse_octet_string(CBS *cbs, uint8_t **out_ptr,
  365. size_t *out_len, unsigned tag) {
  366. CBS value;
  367. if (!CBS_get_optional_asn1_octet_string(cbs, &value, NULL, tag)) {
  368. OPENSSL_PUT_ERROR(SSL, SSL_SESSION_parse_octet_string,
  369. SSL_R_INVALID_SSL_SESSION);
  370. return 0;
  371. }
  372. if (!CBS_stow(&value, out_ptr, out_len)) {
  373. OPENSSL_PUT_ERROR(SSL, SSL_SESSION_parse_octet_string,
  374. ERR_R_MALLOC_FAILURE);
  375. return 0;
  376. }
  377. return 1;
  378. }
  379. static SSL_SESSION *SSL_SESSION_parse(CBS *cbs) {
  380. SSL_SESSION *ret = NULL;
  381. CBS session, cipher, session_id, master_key;
  382. CBS peer, sid_ctx, peer_sha256, original_handshake_hash;
  383. int has_peer, has_peer_sha256, extended_master_secret;
  384. uint64_t version, ssl_version;
  385. uint64_t session_time, timeout, verify_result, ticket_lifetime_hint;
  386. ret = SSL_SESSION_new();
  387. if (ret == NULL) {
  388. goto err;
  389. }
  390. if (!CBS_get_asn1(cbs, &session, CBS_ASN1_SEQUENCE) ||
  391. !CBS_get_asn1_uint64(&session, &version) ||
  392. !CBS_get_asn1_uint64(&session, &ssl_version) ||
  393. !CBS_get_asn1(&session, &cipher, CBS_ASN1_OCTETSTRING) ||
  394. !CBS_get_asn1(&session, &session_id, CBS_ASN1_OCTETSTRING) ||
  395. !CBS_get_asn1(&session, &master_key, CBS_ASN1_OCTETSTRING) ||
  396. !CBS_get_optional_asn1_uint64(&session, &session_time, kTimeTag,
  397. time(NULL)) ||
  398. !CBS_get_optional_asn1_uint64(&session, &timeout, kTimeoutTag, 3) ||
  399. !CBS_get_optional_asn1(&session, &peer, &has_peer, kPeerTag) ||
  400. !CBS_get_optional_asn1_octet_string(&session, &sid_ctx, NULL,
  401. kSessionIDContextTag) ||
  402. !CBS_get_optional_asn1_uint64(&session, &verify_result, kVerifyResultTag,
  403. X509_V_OK)) {
  404. OPENSSL_PUT_ERROR(SSL, SSL_SESSION_parse, SSL_R_INVALID_SSL_SESSION);
  405. goto err;
  406. }
  407. if (!SSL_SESSION_parse_string(&session, &ret->tlsext_hostname,
  408. kHostNameTag) ||
  409. !SSL_SESSION_parse_string(&session, &ret->psk_identity,
  410. kPSKIdentityTag)) {
  411. goto err;
  412. }
  413. if (!CBS_get_optional_asn1_uint64(&session, &ticket_lifetime_hint,
  414. kTicketLifetimeHintTag, 0)) {
  415. OPENSSL_PUT_ERROR(SSL, SSL_SESSION_parse, SSL_R_INVALID_SSL_SESSION);
  416. goto err;
  417. }
  418. if (!SSL_SESSION_parse_octet_string(&session, &ret->tlsext_tick,
  419. &ret->tlsext_ticklen, kTicketTag)) {
  420. goto err;
  421. }
  422. if (!CBS_get_optional_asn1_octet_string(&session, &peer_sha256,
  423. &has_peer_sha256, kPeerSHA256Tag) ||
  424. !CBS_get_optional_asn1_octet_string(&session, &original_handshake_hash,
  425. NULL, kOriginalHandshakeHashTag)) {
  426. OPENSSL_PUT_ERROR(SSL, SSL_SESSION_parse, SSL_R_INVALID_SSL_SESSION);
  427. goto err;
  428. }
  429. if (!SSL_SESSION_parse_octet_string(
  430. &session, &ret->tlsext_signed_cert_timestamp_list,
  431. &ret->tlsext_signed_cert_timestamp_list_length,
  432. kSignedCertTimestampListTag) ||
  433. !SSL_SESSION_parse_octet_string(
  434. &session, &ret->ocsp_response, &ret->ocsp_response_length,
  435. kOCSPResponseTag)) {
  436. goto err;
  437. }
  438. if (!CBS_get_optional_asn1_bool(&session, &extended_master_secret,
  439. kExtendedMasterSecretTag,
  440. 0 /* default to false */) ||
  441. CBS_len(&session) != 0) {
  442. OPENSSL_PUT_ERROR(SSL, SSL_SESSION_parse, SSL_R_INVALID_SSL_SESSION);
  443. goto err;
  444. }
  445. ret->extended_master_secret = extended_master_secret;
  446. if (version != SSL_SESSION_ASN1_VERSION) {
  447. OPENSSL_PUT_ERROR(SSL, SSL_SESSION_parse, SSL_R_INVALID_SSL_SESSION);
  448. goto err;
  449. }
  450. /* Only support SSLv3/TLS and DTLS. */
  451. if ((ssl_version >> 8) != SSL3_VERSION_MAJOR &&
  452. (ssl_version >> 8) != (DTLS1_VERSION >> 8)) {
  453. OPENSSL_PUT_ERROR(SSL, SSL_SESSION_parse, SSL_R_UNKNOWN_SSL_VERSION);
  454. goto err;
  455. }
  456. ret->ssl_version = ssl_version;
  457. uint16_t cipher_value;
  458. if (!CBS_get_u16(&cipher, &cipher_value) || CBS_len(&cipher) != 0) {
  459. OPENSSL_PUT_ERROR(SSL, SSL_SESSION_parse, SSL_R_CIPHER_CODE_WRONG_LENGTH);
  460. goto err;
  461. }
  462. ret->cipher = SSL_get_cipher_by_value(cipher_value);
  463. if (ret->cipher == NULL) {
  464. OPENSSL_PUT_ERROR(SSL, SSL_SESSION_parse, SSL_R_UNSUPPORTED_CIPHER);
  465. goto err;
  466. }
  467. if (CBS_len(&session_id) > SSL3_MAX_SSL_SESSION_ID_LENGTH) {
  468. OPENSSL_PUT_ERROR(SSL, SSL_SESSION_parse, SSL_R_INVALID_SSL_SESSION);
  469. goto err;
  470. }
  471. memcpy(ret->session_id, CBS_data(&session_id), CBS_len(&session_id));
  472. ret->session_id_length = CBS_len(&session_id);
  473. if (CBS_len(&master_key) > SSL_MAX_MASTER_KEY_LENGTH) {
  474. OPENSSL_PUT_ERROR(SSL, SSL_SESSION_parse, SSL_R_INVALID_SSL_SESSION);
  475. goto err;
  476. }
  477. memcpy(ret->master_key, CBS_data(&master_key), CBS_len(&master_key));
  478. ret->master_key_length = CBS_len(&master_key);
  479. if (session_time > LONG_MAX ||
  480. timeout > LONG_MAX) {
  481. OPENSSL_PUT_ERROR(SSL, SSL_SESSION_parse, SSL_R_INVALID_SSL_SESSION);
  482. goto err;
  483. }
  484. ret->time = session_time;
  485. ret->timeout = timeout;
  486. X509_free(ret->peer);
  487. ret->peer = NULL;
  488. if (has_peer) {
  489. const uint8_t *ptr;
  490. ptr = CBS_data(&peer);
  491. ret->peer = d2i_X509(NULL, &ptr, CBS_len(&peer));
  492. if (ret->peer == NULL) {
  493. goto err;
  494. }
  495. if (ptr != CBS_data(&peer) + CBS_len(&peer)) {
  496. OPENSSL_PUT_ERROR(SSL, SSL_SESSION_parse, SSL_R_INVALID_SSL_SESSION);
  497. goto err;
  498. }
  499. }
  500. if (CBS_len(&sid_ctx) > sizeof(ret->sid_ctx)) {
  501. OPENSSL_PUT_ERROR(SSL, SSL_SESSION_parse, SSL_R_INVALID_SSL_SESSION);
  502. goto err;
  503. }
  504. memcpy(ret->sid_ctx, CBS_data(&sid_ctx), CBS_len(&sid_ctx));
  505. ret->sid_ctx_length = CBS_len(&sid_ctx);
  506. if (verify_result > LONG_MAX ||
  507. ticket_lifetime_hint > 0xffffffff) {
  508. OPENSSL_PUT_ERROR(SSL, SSL_SESSION_parse, SSL_R_INVALID_SSL_SESSION);
  509. goto err;
  510. }
  511. ret->verify_result = verify_result;
  512. ret->tlsext_tick_lifetime_hint = ticket_lifetime_hint;
  513. if (has_peer_sha256) {
  514. if (CBS_len(&peer_sha256) != sizeof(ret->peer_sha256)) {
  515. OPENSSL_PUT_ERROR(SSL, SSL_SESSION_parse, SSL_R_INVALID_SSL_SESSION);
  516. goto err;
  517. }
  518. memcpy(ret->peer_sha256, CBS_data(&peer_sha256), sizeof(ret->peer_sha256));
  519. ret->peer_sha256_valid = 1;
  520. } else {
  521. ret->peer_sha256_valid = 0;
  522. }
  523. if (CBS_len(&original_handshake_hash) >
  524. sizeof(ret->original_handshake_hash)) {
  525. OPENSSL_PUT_ERROR(SSL, SSL_SESSION_parse, SSL_R_INVALID_SSL_SESSION);
  526. goto err;
  527. }
  528. memcpy(ret->original_handshake_hash, CBS_data(&original_handshake_hash),
  529. CBS_len(&original_handshake_hash));
  530. ret->original_handshake_hash_len = CBS_len(&original_handshake_hash);
  531. return ret;
  532. err:
  533. SSL_SESSION_free(ret);
  534. return NULL;
  535. }
  536. SSL_SESSION *SSL_SESSION_from_bytes(const uint8_t *in, size_t in_len) {
  537. CBS cbs;
  538. CBS_init(&cbs, in, in_len);
  539. SSL_SESSION *ret = SSL_SESSION_parse(&cbs);
  540. if (ret == NULL) {
  541. return NULL;
  542. }
  543. if (CBS_len(&cbs) != 0) {
  544. OPENSSL_PUT_ERROR(SSL, SSL_SESSION_from_bytes, SSL_R_INVALID_SSL_SESSION);
  545. SSL_SESSION_free(ret);
  546. return NULL;
  547. }
  548. return ret;
  549. }
  550. SSL_SESSION *d2i_SSL_SESSION(SSL_SESSION **a, const uint8_t **pp, long length) {
  551. if (length < 0) {
  552. OPENSSL_PUT_ERROR(SSL, d2i_SSL_SESSION, ERR_R_INTERNAL_ERROR);
  553. return NULL;
  554. }
  555. CBS cbs;
  556. CBS_init(&cbs, *pp, length);
  557. SSL_SESSION *ret = SSL_SESSION_parse(&cbs);
  558. if (ret == NULL) {
  559. return NULL;
  560. }
  561. if (a) {
  562. SSL_SESSION_free(*a);
  563. *a = ret;
  564. }
  565. *pp = CBS_data(&cbs);
  566. return ret;
  567. }