Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 
 
 
 

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