Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 
 
 
 
 

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