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.
 
 
 
 
 
 

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