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.
 
 
 
 
 
 

482 line
16 KiB

  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. const SSL3_STATE *const s3 = ssl->s3;
  46. if (!ssl->server ||
  47. s3->hs == nullptr ||
  48. s3->rwstate != SSL_HANDOFF) {
  49. return false;
  50. }
  51. CBB seq;
  52. Span<const uint8_t> transcript = s3->hs->transcript.buffer();
  53. if (!CBB_add_asn1(out, &seq, CBS_ASN1_SEQUENCE) ||
  54. !CBB_add_asn1_uint64(&seq, kHandoffVersion) ||
  55. !CBB_add_asn1_octet_string(&seq, transcript.data(), transcript.size()) ||
  56. !CBB_add_asn1_octet_string(&seq,
  57. reinterpret_cast<uint8_t *>(s3->hs_buf->data),
  58. s3->hs_buf->length) ||
  59. !serialize_features(&seq) ||
  60. !CBB_flush(out)) {
  61. return false;
  62. }
  63. return true;
  64. }
  65. bool SSL_decline_handoff(SSL *ssl) {
  66. const SSL3_STATE *const s3 = ssl->s3;
  67. if (!ssl->server ||
  68. s3->hs == nullptr ||
  69. s3->rwstate != SSL_HANDOFF) {
  70. return false;
  71. }
  72. s3->hs->config->handoff = false;
  73. return true;
  74. }
  75. // apply_remote_features reads a list of supported features from |in| and
  76. // (possibly) reconfigures |ssl| to disallow the negotation of features whose
  77. // support has not been indicated. (This prevents the the handshake from
  78. // committing to features that are not supported on the handoff/handback side.)
  79. static bool apply_remote_features(SSL *ssl, CBS *in) {
  80. CBS ciphers;
  81. if (!CBS_get_asn1(in, &ciphers, CBS_ASN1_OCTETSTRING)) {
  82. return false;
  83. }
  84. bssl::UniquePtr<STACK_OF(SSL_CIPHER)> supported(sk_SSL_CIPHER_new_null());
  85. while (CBS_len(&ciphers)) {
  86. uint16_t id;
  87. if (!CBS_get_u16(&ciphers, &id)) {
  88. return false;
  89. }
  90. const SSL_CIPHER *cipher = SSL_get_cipher_by_value(id);
  91. if (!cipher) {
  92. continue;
  93. }
  94. if (!sk_SSL_CIPHER_push(supported.get(), cipher)) {
  95. return false;
  96. }
  97. }
  98. STACK_OF(SSL_CIPHER) *configured =
  99. ssl->config->cipher_list ? ssl->config->cipher_list->ciphers.get()
  100. : ssl->ctx->cipher_list->ciphers.get();
  101. bssl::UniquePtr<STACK_OF(SSL_CIPHER)> unsupported(sk_SSL_CIPHER_new_null());
  102. for (const SSL_CIPHER *configured_cipher : configured) {
  103. if (sk_SSL_CIPHER_find(supported.get(), nullptr, configured_cipher)) {
  104. continue;
  105. }
  106. if (!sk_SSL_CIPHER_push(unsupported.get(), configured_cipher)) {
  107. return false;
  108. }
  109. }
  110. if (sk_SSL_CIPHER_num(unsupported.get()) && !ssl->config->cipher_list) {
  111. ssl->config->cipher_list = bssl::MakeUnique<SSLCipherPreferenceList>();
  112. if (!ssl->config->cipher_list->Init(*ssl->ctx->cipher_list)) {
  113. return false;
  114. }
  115. }
  116. for (const SSL_CIPHER *unsupported_cipher : unsupported.get()) {
  117. ssl->config->cipher_list->Remove(unsupported_cipher);
  118. }
  119. if (sk_SSL_CIPHER_num(SSL_get_ciphers(ssl)) == 0) {
  120. return false;
  121. }
  122. CBS curves;
  123. if (!CBS_get_asn1(in, &curves, CBS_ASN1_OCTETSTRING)) {
  124. return false;
  125. }
  126. Array<uint16_t> supported_curves;
  127. if (!supported_curves.Init(CBS_len(&curves) / 2)) {
  128. return false;
  129. }
  130. size_t idx = 0;
  131. while (CBS_len(&curves)) {
  132. uint16_t curve;
  133. if (!CBS_get_u16(&curves, &curve)) {
  134. return false;
  135. }
  136. supported_curves[idx++] = curve;
  137. }
  138. Span<const uint16_t> configured_curves =
  139. tls1_get_grouplist(ssl->s3->hs.get());
  140. Array<uint16_t> new_configured_curves;
  141. if (!new_configured_curves.Init(configured_curves.size())) {
  142. return false;
  143. }
  144. idx = 0;
  145. for (uint16_t configured_curve : configured_curves) {
  146. bool ok = false;
  147. for (uint16_t supported_curve : supported_curves) {
  148. if (supported_curve == configured_curve) {
  149. ok = true;
  150. break;
  151. }
  152. }
  153. if (ok) {
  154. new_configured_curves[idx++] = configured_curve;
  155. }
  156. }
  157. if (idx == 0) {
  158. return false;
  159. }
  160. new_configured_curves.Shrink(idx);
  161. ssl->config->supported_group_list = std::move(new_configured_curves);
  162. return true;
  163. }
  164. bool SSL_apply_handoff(SSL *ssl, Span<const uint8_t> handoff) {
  165. if (ssl->method->is_dtls) {
  166. return false;
  167. }
  168. CBS seq, handoff_cbs(handoff);
  169. uint64_t handoff_version;
  170. if (!CBS_get_asn1(&handoff_cbs, &seq, CBS_ASN1_SEQUENCE) ||
  171. !CBS_get_asn1_uint64(&seq, &handoff_version) ||
  172. handoff_version != kHandoffVersion) {
  173. return false;
  174. }
  175. CBS transcript, hs_buf;
  176. if (!CBS_get_asn1(&seq, &transcript, CBS_ASN1_OCTETSTRING) ||
  177. !CBS_get_asn1(&seq, &hs_buf, CBS_ASN1_OCTETSTRING) ||
  178. !apply_remote_features(ssl, &seq)) {
  179. return false;
  180. }
  181. SSL_set_accept_state(ssl);
  182. SSL3_STATE *const s3 = ssl->s3;
  183. s3->v2_hello_done = true;
  184. s3->has_message = true;
  185. s3->hs_buf.reset(BUF_MEM_new());
  186. if (!s3->hs_buf ||
  187. !BUF_MEM_append(s3->hs_buf.get(), CBS_data(&hs_buf), CBS_len(&hs_buf))) {
  188. return false;
  189. }
  190. if (CBS_len(&transcript) != 0) {
  191. s3->hs->transcript.Update(transcript);
  192. s3->is_v2_hello = true;
  193. }
  194. s3->hs->handback = true;
  195. return true;
  196. }
  197. bool SSL_serialize_handback(const SSL *ssl, CBB *out) {
  198. if (!ssl->server || ssl->method->is_dtls) {
  199. return false;
  200. }
  201. handback_t type;
  202. switch (ssl->s3->hs->state) {
  203. case state12_read_change_cipher_spec:
  204. type = handback_after_session_resumption;
  205. break;
  206. case state12_read_client_certificate:
  207. type = handback_after_ecdhe;
  208. break;
  209. case state12_finish_server_handshake:
  210. type = handback_after_handshake;
  211. break;
  212. default:
  213. return false;
  214. }
  215. const SSL3_STATE *const s3 = ssl->s3;
  216. size_t hostname_len = 0;
  217. if (s3->hostname) {
  218. hostname_len = strlen(s3->hostname.get());
  219. }
  220. Span<const uint8_t> transcript;
  221. if (type == handback_after_ecdhe ||
  222. type == handback_after_session_resumption) {
  223. transcript = s3->hs->transcript.buffer();
  224. }
  225. size_t write_iv_len = 0;
  226. const uint8_t *write_iv = nullptr;
  227. if ((type == handback_after_session_resumption ||
  228. type == handback_after_handshake) &&
  229. ssl->version == TLS1_VERSION &&
  230. SSL_CIPHER_is_block_cipher(s3->aead_write_ctx->cipher()) &&
  231. !s3->aead_write_ctx->GetIV(&write_iv, &write_iv_len)) {
  232. return false;
  233. }
  234. size_t read_iv_len = 0;
  235. const uint8_t *read_iv = nullptr;
  236. if (type == handback_after_handshake &&
  237. ssl->version == TLS1_VERSION &&
  238. SSL_CIPHER_is_block_cipher(s3->aead_read_ctx->cipher()) &&
  239. !s3->aead_read_ctx->GetIV(&read_iv, &read_iv_len)) {
  240. return false;
  241. }
  242. // TODO(mab): make sure everything is serialized.
  243. CBB seq, key_share;
  244. const SSL_SESSION *session =
  245. s3->session_reused ? ssl->session.get() : s3->hs->new_session.get();
  246. if (!CBB_add_asn1(out, &seq, CBS_ASN1_SEQUENCE) ||
  247. !CBB_add_asn1_uint64(&seq, kHandbackVersion) ||
  248. !CBB_add_asn1_uint64(&seq, type) ||
  249. !CBB_add_asn1_octet_string(&seq, s3->read_sequence,
  250. sizeof(s3->read_sequence)) ||
  251. !CBB_add_asn1_octet_string(&seq, s3->write_sequence,
  252. sizeof(s3->write_sequence)) ||
  253. !CBB_add_asn1_octet_string(&seq, s3->server_random,
  254. sizeof(s3->server_random)) ||
  255. !CBB_add_asn1_octet_string(&seq, s3->client_random,
  256. sizeof(s3->client_random)) ||
  257. !CBB_add_asn1_octet_string(&seq, read_iv, read_iv_len) ||
  258. !CBB_add_asn1_octet_string(&seq, write_iv, write_iv_len) ||
  259. !CBB_add_asn1_bool(&seq, s3->session_reused) ||
  260. !CBB_add_asn1_bool(&seq, s3->channel_id_valid) ||
  261. !ssl_session_serialize(session, &seq) ||
  262. !CBB_add_asn1_octet_string(&seq, s3->next_proto_negotiated.data(),
  263. s3->next_proto_negotiated.size()) ||
  264. !CBB_add_asn1_octet_string(&seq, s3->alpn_selected.data(),
  265. s3->alpn_selected.size()) ||
  266. !CBB_add_asn1_octet_string(
  267. &seq, reinterpret_cast<uint8_t *>(s3->hostname.get()),
  268. hostname_len) ||
  269. !CBB_add_asn1_octet_string(&seq, s3->channel_id,
  270. sizeof(s3->channel_id)) ||
  271. !CBB_add_asn1_bool(&seq, ssl->s3->token_binding_negotiated) ||
  272. !CBB_add_asn1_uint64(&seq, ssl->s3->negotiated_token_binding_param) ||
  273. !CBB_add_asn1_bool(&seq, s3->hs->next_proto_neg_seen) ||
  274. !CBB_add_asn1_bool(&seq, s3->hs->cert_request) ||
  275. !CBB_add_asn1_bool(&seq, s3->hs->extended_master_secret) ||
  276. !CBB_add_asn1_bool(&seq, s3->hs->ticket_expected) ||
  277. !CBB_add_asn1_uint64(&seq, SSL_CIPHER_get_id(s3->hs->new_cipher)) ||
  278. !CBB_add_asn1_octet_string(&seq, transcript.data(), transcript.size()) ||
  279. !CBB_add_asn1(&seq, &key_share, CBS_ASN1_SEQUENCE)) {
  280. return false;
  281. }
  282. if (type == handback_after_ecdhe &&
  283. !s3->hs->key_shares[0]->Serialize(&key_share)) {
  284. return false;
  285. }
  286. return CBB_flush(out);
  287. }
  288. bool SSL_apply_handback(SSL *ssl, Span<const uint8_t> handback) {
  289. if (ssl->do_handshake != nullptr ||
  290. ssl->method->is_dtls) {
  291. return false;
  292. }
  293. SSL3_STATE *const s3 = ssl->s3;
  294. uint64_t handback_version, negotiated_token_binding_param, cipher, type;
  295. CBS seq, read_seq, write_seq, server_rand, client_rand, read_iv, write_iv,
  296. next_proto, alpn, hostname, channel_id, transcript, key_share;
  297. int session_reused, channel_id_valid, cert_request, extended_master_secret,
  298. ticket_expected, token_binding_negotiated, next_proto_neg_seen;
  299. SSL_SESSION *session = nullptr;
  300. CBS handback_cbs(handback);
  301. if (!CBS_get_asn1(&handback_cbs, &seq, CBS_ASN1_SEQUENCE) ||
  302. !CBS_get_asn1_uint64(&seq, &handback_version) ||
  303. handback_version != kHandbackVersion ||
  304. !CBS_get_asn1_uint64(&seq, &type)) {
  305. return false;
  306. }
  307. if (!CBS_get_asn1(&seq, &read_seq, CBS_ASN1_OCTETSTRING) ||
  308. CBS_len(&read_seq) != sizeof(s3->read_sequence) ||
  309. !CBS_get_asn1(&seq, &write_seq, CBS_ASN1_OCTETSTRING) ||
  310. CBS_len(&write_seq) != sizeof(s3->write_sequence) ||
  311. !CBS_get_asn1(&seq, &server_rand, CBS_ASN1_OCTETSTRING) ||
  312. CBS_len(&server_rand) != sizeof(s3->server_random) ||
  313. !CBS_copy_bytes(&server_rand, s3->server_random,
  314. sizeof(s3->server_random)) ||
  315. !CBS_get_asn1(&seq, &client_rand, CBS_ASN1_OCTETSTRING) ||
  316. CBS_len(&client_rand) != sizeof(s3->client_random) ||
  317. !CBS_copy_bytes(&client_rand, s3->client_random,
  318. sizeof(s3->client_random)) ||
  319. !CBS_get_asn1(&seq, &read_iv, CBS_ASN1_OCTETSTRING) ||
  320. !CBS_get_asn1(&seq, &write_iv, CBS_ASN1_OCTETSTRING) ||
  321. !CBS_get_asn1_bool(&seq, &session_reused) ||
  322. !CBS_get_asn1_bool(&seq, &channel_id_valid)) {
  323. return false;
  324. }
  325. s3->hs = ssl_handshake_new(ssl);
  326. if (session_reused) {
  327. ssl->session =
  328. SSL_SESSION_parse(&seq, ssl->ctx->x509_method, ssl->ctx->pool);
  329. session = ssl->session.get();
  330. } else {
  331. s3->hs->new_session =
  332. SSL_SESSION_parse(&seq, ssl->ctx->x509_method, ssl->ctx->pool);
  333. session = s3->hs->new_session.get();
  334. }
  335. if (!session || !CBS_get_asn1(&seq, &next_proto, CBS_ASN1_OCTETSTRING) ||
  336. !CBS_get_asn1(&seq, &alpn, CBS_ASN1_OCTETSTRING) ||
  337. !CBS_get_asn1(&seq, &hostname, CBS_ASN1_OCTETSTRING) ||
  338. !CBS_get_asn1(&seq, &channel_id, CBS_ASN1_OCTETSTRING) ||
  339. CBS_len(&channel_id) != sizeof(s3->channel_id) ||
  340. !CBS_copy_bytes(&channel_id, s3->channel_id,
  341. sizeof(s3->channel_id)) ||
  342. !CBS_get_asn1_bool(&seq, &token_binding_negotiated) ||
  343. !CBS_get_asn1_uint64(&seq, &negotiated_token_binding_param) ||
  344. !CBS_get_asn1_bool(&seq, &next_proto_neg_seen) ||
  345. !CBS_get_asn1_bool(&seq, &cert_request) ||
  346. !CBS_get_asn1_bool(&seq, &extended_master_secret) ||
  347. !CBS_get_asn1_bool(&seq, &ticket_expected) ||
  348. !CBS_get_asn1_uint64(&seq, &cipher)) {
  349. return false;
  350. }
  351. if ((s3->hs->new_cipher =
  352. SSL_get_cipher_by_value(static_cast<uint16_t>(cipher))) == nullptr) {
  353. return false;
  354. }
  355. if (!CBS_get_asn1(&seq, &transcript, CBS_ASN1_OCTETSTRING) ||
  356. !CBS_get_asn1(&seq, &key_share, CBS_ASN1_SEQUENCE)) {
  357. return false;
  358. }
  359. ssl->version = session->ssl_version;
  360. s3->have_version = true;
  361. if (!ssl_method_supports_version(ssl->method, ssl->version) ||
  362. session->cipher != s3->hs->new_cipher ||
  363. ssl_protocol_version(ssl) < SSL_CIPHER_get_min_version(session->cipher) ||
  364. SSL_CIPHER_get_max_version(session->cipher) < ssl_protocol_version(ssl)) {
  365. return false;
  366. }
  367. ssl->do_handshake = ssl_server_handshake;
  368. ssl->server = true;
  369. switch (type) {
  370. case handback_after_session_resumption:
  371. ssl->s3->hs->state = state12_read_change_cipher_spec;
  372. if (!session_reused) {
  373. return false;
  374. }
  375. break;
  376. case handback_after_ecdhe:
  377. ssl->s3->hs->state = state12_read_client_certificate;
  378. if (session_reused) {
  379. return false;
  380. }
  381. break;
  382. case handback_after_handshake:
  383. ssl->s3->hs->state = state12_finish_server_handshake;
  384. break;
  385. default:
  386. return false;
  387. }
  388. s3->session_reused = session_reused;
  389. s3->channel_id_valid = channel_id_valid;
  390. s3->next_proto_negotiated.CopyFrom(next_proto);
  391. s3->alpn_selected.CopyFrom(alpn);
  392. const size_t hostname_len = CBS_len(&hostname);
  393. if (hostname_len == 0) {
  394. s3->hostname.reset();
  395. } else {
  396. char *hostname_str = nullptr;
  397. if (!CBS_strdup(&hostname, &hostname_str)) {
  398. return false;
  399. }
  400. s3->hostname.reset(hostname_str);
  401. }
  402. s3->token_binding_negotiated = token_binding_negotiated;
  403. s3->negotiated_token_binding_param =
  404. static_cast<uint8_t>(negotiated_token_binding_param);
  405. s3->hs->next_proto_neg_seen = next_proto_neg_seen;
  406. s3->hs->wait = ssl_hs_flush;
  407. s3->hs->extended_master_secret = extended_master_secret;
  408. s3->hs->ticket_expected = ticket_expected;
  409. s3->aead_write_ctx->SetVersionIfNullCipher(ssl->version);
  410. s3->hs->cert_request = cert_request;
  411. Array<uint8_t> key_block;
  412. if ((type == handback_after_session_resumption ||
  413. type == handback_after_handshake) &&
  414. (!tls1_configure_aead(ssl, evp_aead_seal, &key_block, session->cipher,
  415. write_iv) ||
  416. !CBS_copy_bytes(&write_seq, s3->write_sequence,
  417. sizeof(s3->write_sequence)))) {
  418. return false;
  419. }
  420. if (type == handback_after_handshake &&
  421. (!tls1_configure_aead(ssl, evp_aead_open, &key_block, session->cipher,
  422. read_iv) ||
  423. !CBS_copy_bytes(&read_seq, s3->read_sequence,
  424. sizeof(s3->read_sequence)))) {
  425. return false;
  426. }
  427. if ((type == handback_after_ecdhe ||
  428. type == handback_after_session_resumption) &&
  429. (!s3->hs->transcript.Init() ||
  430. !s3->hs->transcript.InitHash(ssl_protocol_version(ssl),
  431. s3->hs->new_cipher) ||
  432. !s3->hs->transcript.Update(transcript))) {
  433. return false;
  434. }
  435. if (type == handback_after_ecdhe &&
  436. (s3->hs->key_shares[0] = SSLKeyShare::Create(&key_share)) == nullptr) {
  437. return false;
  438. }
  439. return CBS_len(&seq) == 0;
  440. }
  441. BSSL_NAMESPACE_END