You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

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