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.
 
 
 
 
 
 

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