handoff.cc 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  1. /* Copyright (c) 2018, Google Inc.
  2. *
  3. * Permission to use, copy, modify, and/or distribute this software for any
  4. * purpose with or without fee is hereby granted, provided that the above
  5. * copyright notice and this permission notice appear in all copies.
  6. *
  7. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  8. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  9. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  10. * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  11. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
  12. * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  13. * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
  14. #include <openssl/ssl.h>
  15. #include <openssl/bytestring.h>
  16. #include "internal.h"
  17. BSSL_NAMESPACE_BEGIN
  18. constexpr int kHandoffVersion = 0;
  19. constexpr int kHandbackVersion = 0;
  20. // serialize_features adds a description of features supported by this binary to
  21. // |out|. Returns true on success and false on error.
  22. static bool serialize_features(CBB *out) {
  23. CBB ciphers;
  24. if (!CBB_add_asn1(out, &ciphers, CBS_ASN1_OCTETSTRING)) {
  25. return false;
  26. }
  27. Span<const SSL_CIPHER> all_ciphers = AllCiphers();
  28. for (const SSL_CIPHER& cipher : all_ciphers) {
  29. if (!CBB_add_u16(&ciphers, static_cast<uint16_t>(cipher.id))) {
  30. return false;
  31. }
  32. }
  33. CBB curves;
  34. if (!CBB_add_asn1(out, &curves, CBS_ASN1_OCTETSTRING)) {
  35. return false;
  36. }
  37. for (const NamedGroup& g : NamedGroups()) {
  38. if (!CBB_add_u16(&curves, g.group_id)) {
  39. return false;
  40. }
  41. }
  42. return CBB_flush(out);
  43. }
  44. bool SSL_serialize_handoff(const SSL *ssl, CBB *out,
  45. SSL_CLIENT_HELLO *out_hello) {
  46. const SSL3_STATE *const s3 = ssl->s3;
  47. if (!ssl->server ||
  48. s3->hs == nullptr ||
  49. s3->rwstate != SSL_ERROR_HANDOFF) {
  50. return false;
  51. }
  52. CBB seq;
  53. SSLMessage msg;
  54. Span<const uint8_t> transcript = s3->hs->transcript.buffer();
  55. if (!CBB_add_asn1(out, &seq, CBS_ASN1_SEQUENCE) ||
  56. !CBB_add_asn1_uint64(&seq, kHandoffVersion) ||
  57. !CBB_add_asn1_octet_string(&seq, transcript.data(), transcript.size()) ||
  58. !CBB_add_asn1_octet_string(&seq,
  59. reinterpret_cast<uint8_t *>(s3->hs_buf->data),
  60. s3->hs_buf->length) ||
  61. !serialize_features(&seq) ||
  62. !CBB_flush(out) ||
  63. !ssl->method->get_message(ssl, &msg) ||
  64. !ssl_client_hello_init(ssl, out_hello, msg)) {
  65. return false;
  66. }
  67. return true;
  68. }
  69. bool SSL_decline_handoff(SSL *ssl) {
  70. const SSL3_STATE *const s3 = ssl->s3;
  71. if (!ssl->server ||
  72. s3->hs == nullptr ||
  73. s3->rwstate != SSL_ERROR_HANDOFF) {
  74. return false;
  75. }
  76. s3->hs->config->handoff = false;
  77. return true;
  78. }
  79. // apply_remote_features reads a list of supported features from |in| and
  80. // (possibly) reconfigures |ssl| to disallow the negotation of features whose
  81. // support has not been indicated. (This prevents the the handshake from
  82. // committing to features that are not supported on the handoff/handback side.)
  83. static bool apply_remote_features(SSL *ssl, CBS *in) {
  84. CBS ciphers;
  85. if (!CBS_get_asn1(in, &ciphers, CBS_ASN1_OCTETSTRING)) {
  86. return false;
  87. }
  88. bssl::UniquePtr<STACK_OF(SSL_CIPHER)> supported(sk_SSL_CIPHER_new_null());
  89. while (CBS_len(&ciphers)) {
  90. uint16_t id;
  91. if (!CBS_get_u16(&ciphers, &id)) {
  92. return false;
  93. }
  94. const SSL_CIPHER *cipher = SSL_get_cipher_by_value(id);
  95. if (!cipher) {
  96. continue;
  97. }
  98. if (!sk_SSL_CIPHER_push(supported.get(), cipher)) {
  99. return false;
  100. }
  101. }
  102. STACK_OF(SSL_CIPHER) *configured =
  103. ssl->config->cipher_list ? ssl->config->cipher_list->ciphers.get()
  104. : ssl->ctx->cipher_list->ciphers.get();
  105. bssl::UniquePtr<STACK_OF(SSL_CIPHER)> unsupported(sk_SSL_CIPHER_new_null());
  106. for (const SSL_CIPHER *configured_cipher : configured) {
  107. if (sk_SSL_CIPHER_find(supported.get(), nullptr, configured_cipher)) {
  108. continue;
  109. }
  110. if (!sk_SSL_CIPHER_push(unsupported.get(), configured_cipher)) {
  111. return false;
  112. }
  113. }
  114. if (sk_SSL_CIPHER_num(unsupported.get()) && !ssl->config->cipher_list) {
  115. ssl->config->cipher_list = bssl::MakeUnique<SSLCipherPreferenceList>();
  116. if (!ssl->config->cipher_list->Init(*ssl->ctx->cipher_list)) {
  117. return false;
  118. }
  119. }
  120. for (const SSL_CIPHER *unsupported_cipher : unsupported.get()) {
  121. ssl->config->cipher_list->Remove(unsupported_cipher);
  122. }
  123. if (sk_SSL_CIPHER_num(SSL_get_ciphers(ssl)) == 0) {
  124. return false;
  125. }
  126. CBS curves;
  127. if (!CBS_get_asn1(in, &curves, CBS_ASN1_OCTETSTRING)) {
  128. return false;
  129. }
  130. Array<uint16_t> supported_curves;
  131. if (!supported_curves.Init(CBS_len(&curves) / 2)) {
  132. return false;
  133. }
  134. size_t idx = 0;
  135. while (CBS_len(&curves)) {
  136. uint16_t curve;
  137. if (!CBS_get_u16(&curves, &curve)) {
  138. return false;
  139. }
  140. supported_curves[idx++] = curve;
  141. }
  142. Span<const uint16_t> configured_curves =
  143. tls1_get_grouplist(ssl->s3->hs.get());
  144. Array<uint16_t> new_configured_curves;
  145. if (!new_configured_curves.Init(configured_curves.size())) {
  146. return false;
  147. }
  148. idx = 0;
  149. for (uint16_t configured_curve : configured_curves) {
  150. bool ok = false;
  151. for (uint16_t supported_curve : supported_curves) {
  152. if (supported_curve == configured_curve) {
  153. ok = true;
  154. break;
  155. }
  156. }
  157. if (ok) {
  158. new_configured_curves[idx++] = configured_curve;
  159. }
  160. }
  161. if (idx == 0) {
  162. return false;
  163. }
  164. new_configured_curves.Shrink(idx);
  165. ssl->config->supported_group_list = std::move(new_configured_curves);
  166. return true;
  167. }
  168. bool SSL_apply_handoff(SSL *ssl, Span<const uint8_t> handoff) {
  169. if (ssl->method->is_dtls) {
  170. return false;
  171. }
  172. CBS seq, handoff_cbs(handoff);
  173. uint64_t handoff_version;
  174. if (!CBS_get_asn1(&handoff_cbs, &seq, CBS_ASN1_SEQUENCE) ||
  175. !CBS_get_asn1_uint64(&seq, &handoff_version) ||
  176. handoff_version != kHandoffVersion) {
  177. return false;
  178. }
  179. CBS transcript, hs_buf;
  180. if (!CBS_get_asn1(&seq, &transcript, CBS_ASN1_OCTETSTRING) ||
  181. !CBS_get_asn1(&seq, &hs_buf, CBS_ASN1_OCTETSTRING) ||
  182. !apply_remote_features(ssl, &seq)) {
  183. return false;
  184. }
  185. SSL_set_accept_state(ssl);
  186. SSL3_STATE *const s3 = ssl->s3;
  187. s3->v2_hello_done = true;
  188. s3->has_message = true;
  189. s3->hs_buf.reset(BUF_MEM_new());
  190. if (!s3->hs_buf ||
  191. !BUF_MEM_append(s3->hs_buf.get(), CBS_data(&hs_buf), CBS_len(&hs_buf))) {
  192. return false;
  193. }
  194. if (CBS_len(&transcript) != 0) {
  195. s3->hs->transcript.Update(transcript);
  196. s3->is_v2_hello = true;
  197. }
  198. s3->hs->handback = true;
  199. return true;
  200. }
  201. bool SSL_serialize_handback(const SSL *ssl, CBB *out) {
  202. if (!ssl->server || ssl->method->is_dtls) {
  203. return false;
  204. }
  205. handback_t type;
  206. switch (ssl->s3->hs->state) {
  207. case state12_read_change_cipher_spec:
  208. type = handback_after_session_resumption;
  209. break;
  210. case state12_read_client_certificate:
  211. type = handback_after_ecdhe;
  212. break;
  213. case state12_finish_server_handshake:
  214. type = handback_after_handshake;
  215. break;
  216. default:
  217. return false;
  218. }
  219. const SSL3_STATE *const s3 = ssl->s3;
  220. size_t hostname_len = 0;
  221. if (s3->hostname) {
  222. hostname_len = strlen(s3->hostname.get());
  223. }
  224. Span<const uint8_t> transcript;
  225. if (type == handback_after_ecdhe ||
  226. type == handback_after_session_resumption) {
  227. transcript = s3->hs->transcript.buffer();
  228. }
  229. size_t write_iv_len = 0;
  230. const uint8_t *write_iv = nullptr;
  231. if ((type == handback_after_session_resumption ||
  232. type == handback_after_handshake) &&
  233. ssl->version == TLS1_VERSION &&
  234. SSL_CIPHER_is_block_cipher(s3->aead_write_ctx->cipher()) &&
  235. !s3->aead_write_ctx->GetIV(&write_iv, &write_iv_len)) {
  236. return false;
  237. }
  238. size_t read_iv_len = 0;
  239. const uint8_t *read_iv = nullptr;
  240. if (type == handback_after_handshake &&
  241. ssl->version == TLS1_VERSION &&
  242. SSL_CIPHER_is_block_cipher(s3->aead_read_ctx->cipher()) &&
  243. !s3->aead_read_ctx->GetIV(&read_iv, &read_iv_len)) {
  244. return false;
  245. }
  246. // TODO(mab): make sure everything is serialized.
  247. CBB seq, key_share;
  248. const SSL_SESSION *session =
  249. s3->session_reused ? ssl->session.get() : s3->hs->new_session.get();
  250. if (!CBB_add_asn1(out, &seq, CBS_ASN1_SEQUENCE) ||
  251. !CBB_add_asn1_uint64(&seq, kHandbackVersion) ||
  252. !CBB_add_asn1_uint64(&seq, type) ||
  253. !CBB_add_asn1_octet_string(&seq, s3->read_sequence,
  254. sizeof(s3->read_sequence)) ||
  255. !CBB_add_asn1_octet_string(&seq, s3->write_sequence,
  256. sizeof(s3->write_sequence)) ||
  257. !CBB_add_asn1_octet_string(&seq, s3->server_random,
  258. sizeof(s3->server_random)) ||
  259. !CBB_add_asn1_octet_string(&seq, s3->client_random,
  260. sizeof(s3->client_random)) ||
  261. !CBB_add_asn1_octet_string(&seq, read_iv, read_iv_len) ||
  262. !CBB_add_asn1_octet_string(&seq, write_iv, write_iv_len) ||
  263. !CBB_add_asn1_bool(&seq, s3->session_reused) ||
  264. !CBB_add_asn1_bool(&seq, s3->channel_id_valid) ||
  265. !ssl_session_serialize(session, &seq) ||
  266. !CBB_add_asn1_octet_string(&seq, s3->next_proto_negotiated.data(),
  267. s3->next_proto_negotiated.size()) ||
  268. !CBB_add_asn1_octet_string(&seq, s3->alpn_selected.data(),
  269. s3->alpn_selected.size()) ||
  270. !CBB_add_asn1_octet_string(
  271. &seq, reinterpret_cast<uint8_t *>(s3->hostname.get()),
  272. hostname_len) ||
  273. !CBB_add_asn1_octet_string(&seq, s3->channel_id,
  274. sizeof(s3->channel_id)) ||
  275. !CBB_add_asn1_bool(&seq, ssl->s3->token_binding_negotiated) ||
  276. !CBB_add_asn1_uint64(&seq, ssl->s3->negotiated_token_binding_param) ||
  277. !CBB_add_asn1_bool(&seq, s3->hs->next_proto_neg_seen) ||
  278. !CBB_add_asn1_bool(&seq, s3->hs->cert_request) ||
  279. !CBB_add_asn1_bool(&seq, s3->hs->extended_master_secret) ||
  280. !CBB_add_asn1_bool(&seq, s3->hs->ticket_expected) ||
  281. !CBB_add_asn1_uint64(&seq, SSL_CIPHER_get_id(s3->hs->new_cipher)) ||
  282. !CBB_add_asn1_octet_string(&seq, transcript.data(), transcript.size()) ||
  283. !CBB_add_asn1(&seq, &key_share, CBS_ASN1_SEQUENCE)) {
  284. return false;
  285. }
  286. if (type == handback_after_ecdhe &&
  287. !s3->hs->key_shares[0]->Serialize(&key_share)) {
  288. return false;
  289. }
  290. return CBB_flush(out);
  291. }
  292. bool SSL_apply_handback(SSL *ssl, Span<const uint8_t> handback) {
  293. if (ssl->do_handshake != nullptr ||
  294. ssl->method->is_dtls) {
  295. return false;
  296. }
  297. SSL3_STATE *const s3 = ssl->s3;
  298. uint64_t handback_version, negotiated_token_binding_param, cipher, type;
  299. CBS seq, read_seq, write_seq, server_rand, client_rand, read_iv, write_iv,
  300. next_proto, alpn, hostname, channel_id, transcript, key_share;
  301. int session_reused, channel_id_valid, cert_request, extended_master_secret,
  302. ticket_expected, token_binding_negotiated, next_proto_neg_seen;
  303. SSL_SESSION *session = nullptr;
  304. CBS handback_cbs(handback);
  305. if (!CBS_get_asn1(&handback_cbs, &seq, CBS_ASN1_SEQUENCE) ||
  306. !CBS_get_asn1_uint64(&seq, &handback_version) ||
  307. handback_version != kHandbackVersion ||
  308. !CBS_get_asn1_uint64(&seq, &type)) {
  309. return false;
  310. }
  311. if (!CBS_get_asn1(&seq, &read_seq, CBS_ASN1_OCTETSTRING) ||
  312. CBS_len(&read_seq) != sizeof(s3->read_sequence) ||
  313. !CBS_get_asn1(&seq, &write_seq, CBS_ASN1_OCTETSTRING) ||
  314. CBS_len(&write_seq) != sizeof(s3->write_sequence) ||
  315. !CBS_get_asn1(&seq, &server_rand, CBS_ASN1_OCTETSTRING) ||
  316. CBS_len(&server_rand) != sizeof(s3->server_random) ||
  317. !CBS_copy_bytes(&server_rand, s3->server_random,
  318. sizeof(s3->server_random)) ||
  319. !CBS_get_asn1(&seq, &client_rand, CBS_ASN1_OCTETSTRING) ||
  320. CBS_len(&client_rand) != sizeof(s3->client_random) ||
  321. !CBS_copy_bytes(&client_rand, s3->client_random,
  322. sizeof(s3->client_random)) ||
  323. !CBS_get_asn1(&seq, &read_iv, CBS_ASN1_OCTETSTRING) ||
  324. !CBS_get_asn1(&seq, &write_iv, CBS_ASN1_OCTETSTRING) ||
  325. !CBS_get_asn1_bool(&seq, &session_reused) ||
  326. !CBS_get_asn1_bool(&seq, &channel_id_valid)) {
  327. return false;
  328. }
  329. s3->hs = ssl_handshake_new(ssl);
  330. if (session_reused) {
  331. ssl->session =
  332. SSL_SESSION_parse(&seq, ssl->ctx->x509_method, ssl->ctx->pool);
  333. session = ssl->session.get();
  334. } else {
  335. s3->hs->new_session =
  336. SSL_SESSION_parse(&seq, ssl->ctx->x509_method, ssl->ctx->pool);
  337. session = s3->hs->new_session.get();
  338. }
  339. if (!session || !CBS_get_asn1(&seq, &next_proto, CBS_ASN1_OCTETSTRING) ||
  340. !CBS_get_asn1(&seq, &alpn, CBS_ASN1_OCTETSTRING) ||
  341. !CBS_get_asn1(&seq, &hostname, CBS_ASN1_OCTETSTRING) ||
  342. !CBS_get_asn1(&seq, &channel_id, CBS_ASN1_OCTETSTRING) ||
  343. CBS_len(&channel_id) != sizeof(s3->channel_id) ||
  344. !CBS_copy_bytes(&channel_id, s3->channel_id,
  345. sizeof(s3->channel_id)) ||
  346. !CBS_get_asn1_bool(&seq, &token_binding_negotiated) ||
  347. !CBS_get_asn1_uint64(&seq, &negotiated_token_binding_param) ||
  348. !CBS_get_asn1_bool(&seq, &next_proto_neg_seen) ||
  349. !CBS_get_asn1_bool(&seq, &cert_request) ||
  350. !CBS_get_asn1_bool(&seq, &extended_master_secret) ||
  351. !CBS_get_asn1_bool(&seq, &ticket_expected) ||
  352. !CBS_get_asn1_uint64(&seq, &cipher)) {
  353. return false;
  354. }
  355. if ((s3->hs->new_cipher =
  356. SSL_get_cipher_by_value(static_cast<uint16_t>(cipher))) == nullptr) {
  357. return false;
  358. }
  359. if (!CBS_get_asn1(&seq, &transcript, CBS_ASN1_OCTETSTRING) ||
  360. !CBS_get_asn1(&seq, &key_share, CBS_ASN1_SEQUENCE)) {
  361. return false;
  362. }
  363. ssl->version = session->ssl_version;
  364. s3->have_version = true;
  365. if (!ssl_method_supports_version(ssl->method, ssl->version) ||
  366. session->cipher != s3->hs->new_cipher ||
  367. ssl_protocol_version(ssl) < SSL_CIPHER_get_min_version(session->cipher) ||
  368. SSL_CIPHER_get_max_version(session->cipher) < ssl_protocol_version(ssl)) {
  369. return false;
  370. }
  371. ssl->do_handshake = ssl_server_handshake;
  372. ssl->server = true;
  373. switch (type) {
  374. case handback_after_session_resumption:
  375. ssl->s3->hs->state = state12_read_change_cipher_spec;
  376. if (!session_reused) {
  377. return false;
  378. }
  379. break;
  380. case handback_after_ecdhe:
  381. ssl->s3->hs->state = state12_read_client_certificate;
  382. if (session_reused) {
  383. return false;
  384. }
  385. break;
  386. case handback_after_handshake:
  387. ssl->s3->hs->state = state12_finish_server_handshake;
  388. break;
  389. default:
  390. return false;
  391. }
  392. s3->session_reused = session_reused;
  393. s3->channel_id_valid = channel_id_valid;
  394. s3->next_proto_negotiated.CopyFrom(next_proto);
  395. s3->alpn_selected.CopyFrom(alpn);
  396. const size_t hostname_len = CBS_len(&hostname);
  397. if (hostname_len == 0) {
  398. s3->hostname.reset();
  399. } else {
  400. char *hostname_str = nullptr;
  401. if (!CBS_strdup(&hostname, &hostname_str)) {
  402. return false;
  403. }
  404. s3->hostname.reset(hostname_str);
  405. }
  406. s3->token_binding_negotiated = token_binding_negotiated;
  407. s3->negotiated_token_binding_param =
  408. static_cast<uint8_t>(negotiated_token_binding_param);
  409. s3->hs->next_proto_neg_seen = next_proto_neg_seen;
  410. s3->hs->wait = ssl_hs_flush;
  411. s3->hs->extended_master_secret = extended_master_secret;
  412. s3->hs->ticket_expected = ticket_expected;
  413. s3->aead_write_ctx->SetVersionIfNullCipher(ssl->version);
  414. s3->hs->cert_request = cert_request;
  415. // TODO(davidben): When handoff for TLS 1.3 is added, serialize
  416. // |early_data_reason| and stabilize the constants.
  417. s3->early_data_reason = ssl_early_data_protocol_version;
  418. Array<uint8_t> key_block;
  419. if ((type == handback_after_session_resumption ||
  420. type == handback_after_handshake) &&
  421. (!tls1_configure_aead(ssl, evp_aead_seal, &key_block, session->cipher,
  422. write_iv) ||
  423. !CBS_copy_bytes(&write_seq, s3->write_sequence,
  424. sizeof(s3->write_sequence)))) {
  425. return false;
  426. }
  427. if (type == handback_after_handshake &&
  428. (!tls1_configure_aead(ssl, evp_aead_open, &key_block, session->cipher,
  429. read_iv) ||
  430. !CBS_copy_bytes(&read_seq, s3->read_sequence,
  431. sizeof(s3->read_sequence)))) {
  432. return false;
  433. }
  434. if ((type == handback_after_ecdhe ||
  435. type == handback_after_session_resumption) &&
  436. (!s3->hs->transcript.Init() ||
  437. !s3->hs->transcript.InitHash(ssl_protocol_version(ssl),
  438. s3->hs->new_cipher) ||
  439. !s3->hs->transcript.Update(transcript))) {
  440. return false;
  441. }
  442. if (type == handback_after_ecdhe &&
  443. (s3->hs->key_shares[0] = SSLKeyShare::Create(&key_share)) == nullptr) {
  444. return false;
  445. }
  446. return CBS_len(&seq) == 0;
  447. }
  448. BSSL_NAMESPACE_END