Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 
 
 
 

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