grpc 1.38.0 → 1.39.0.pre1

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of grpc might be problematic. Click here for more details.

Files changed (199) hide show
  1. checksums.yaml +4 -4
  2. data/Makefile +50 -19
  3. data/include/grpc/event_engine/endpoint_config.h +48 -0
  4. data/include/grpc/event_engine/event_engine.h +13 -15
  5. data/include/grpc/event_engine/port.h +2 -0
  6. data/include/grpc/event_engine/slice_allocator.h +17 -7
  7. data/include/grpc/grpc.h +9 -2
  8. data/include/grpc/grpc_security.h +32 -0
  9. data/include/grpc/grpc_security_constants.h +1 -0
  10. data/include/grpc/impl/codegen/grpc_types.h +17 -13
  11. data/include/grpc/impl/codegen/port_platform.h +17 -0
  12. data/src/core/ext/filters/client_channel/client_channel.cc +2 -2
  13. data/src/core/ext/filters/client_channel/health/health_check_client.cc +2 -0
  14. data/src/core/ext/filters/client_channel/health/health_check_client.h +3 -3
  15. data/src/core/ext/filters/client_channel/http_proxy.cc +16 -1
  16. data/src/core/ext/filters/client_channel/lb_policy/ring_hash/ring_hash.cc +755 -0
  17. data/src/core/ext/filters/client_channel/lb_policy/ring_hash/ring_hash.h +10 -0
  18. data/src/core/ext/filters/client_channel/lb_policy/xds/cds.cc +10 -24
  19. data/src/core/ext/filters/client_channel/lb_policy/xds/xds_cluster_resolver.cc +63 -95
  20. data/src/core/ext/filters/client_channel/resolver/dns/c_ares/dns_resolver_ares.cc +1 -3
  21. data/src/core/ext/filters/client_channel/resolver/dns/c_ares/grpc_ares_ev_driver_event_engine.cc +31 -0
  22. data/src/core/ext/filters/client_channel/resolver/dns/c_ares/grpc_ares_wrapper_event_engine.cc +28 -0
  23. data/src/core/ext/filters/client_channel/resolver/dns/native/dns_resolver.cc +1 -3
  24. data/src/core/ext/filters/client_channel/resolver/google_c2p/google_c2p_resolver.cc +7 -2
  25. data/src/core/ext/filters/client_channel/resolver/xds/xds_resolver.cc +15 -3
  26. data/src/core/ext/filters/client_channel/retry_filter.cc +665 -404
  27. data/src/core/ext/filters/client_channel/retry_service_config.cc +43 -24
  28. data/src/core/ext/filters/client_channel/retry_service_config.h +8 -2
  29. data/src/core/ext/filters/client_idle/client_idle_filter.cc +1 -1
  30. data/src/core/ext/filters/fault_injection/fault_injection_filter.cc +6 -0
  31. data/src/core/ext/transport/chttp2/client/insecure/channel_create_posix.cc +2 -1
  32. data/src/core/ext/transport/chttp2/server/insecure/server_chttp2_posix.cc +3 -2
  33. data/src/core/ext/transport/chttp2/transport/chttp2_transport.cc +10 -4
  34. data/src/core/ext/transport/chttp2/transport/internal.h +1 -0
  35. data/src/core/ext/transport/chttp2/transport/parsing.cc +2 -2
  36. data/src/core/ext/transport/inproc/inproc_transport.cc +42 -31
  37. data/src/core/ext/xds/xds_api.cc +247 -106
  38. data/src/core/ext/xds/xds_api.h +15 -6
  39. data/src/core/lib/address_utils/sockaddr_utils.cc +13 -0
  40. data/src/core/lib/address_utils/sockaddr_utils.h +10 -0
  41. data/src/core/lib/channel/channelz.h +3 -0
  42. data/src/core/lib/event_engine/endpoint_config.cc +46 -0
  43. data/src/core/lib/event_engine/endpoint_config_internal.h +42 -0
  44. data/src/core/lib/event_engine/event_engine.cc +50 -0
  45. data/src/core/lib/event_engine/slice_allocator.cc +33 -3
  46. data/src/core/lib/event_engine/sockaddr.cc +14 -12
  47. data/src/core/lib/event_engine/sockaddr.h +44 -0
  48. data/src/core/lib/gpr/wrap_memcpy.cc +2 -1
  49. data/src/core/lib/gprpp/status_helper.h +3 -0
  50. data/src/core/lib/iomgr/endpoint_pair_event_engine.cc +33 -0
  51. data/src/core/lib/iomgr/error.cc +5 -4
  52. data/src/core/lib/iomgr/error.h +1 -1
  53. data/src/core/lib/iomgr/event_engine/closure.cc +54 -0
  54. data/src/core/lib/iomgr/event_engine/closure.h +33 -0
  55. data/src/core/lib/iomgr/event_engine/endpoint.cc +194 -0
  56. data/src/core/lib/iomgr/event_engine/endpoint.h +53 -0
  57. data/src/core/lib/iomgr/event_engine/iomgr.cc +105 -0
  58. data/src/core/lib/iomgr/event_engine/iomgr.h +24 -0
  59. data/src/core/lib/iomgr/event_engine/pollset.cc +87 -0
  60. data/{include/grpc/event_engine/channel_args.h → src/core/lib/iomgr/event_engine/pollset.h} +7 -10
  61. data/src/core/lib/iomgr/event_engine/promise.h +51 -0
  62. data/src/core/lib/iomgr/event_engine/resolved_address_internal.cc +41 -0
  63. data/src/core/lib/iomgr/event_engine/resolved_address_internal.h +35 -0
  64. data/src/core/lib/iomgr/event_engine/resolver.cc +110 -0
  65. data/src/core/lib/iomgr/event_engine/tcp.cc +243 -0
  66. data/src/core/lib/iomgr/event_engine/timer.cc +57 -0
  67. data/src/core/lib/iomgr/exec_ctx.cc +8 -0
  68. data/src/core/lib/iomgr/exec_ctx.h +3 -4
  69. data/src/core/lib/iomgr/executor/threadpool.cc +2 -3
  70. data/src/core/lib/iomgr/executor/threadpool.h +2 -2
  71. data/src/core/lib/iomgr/iomgr.cc +1 -1
  72. data/src/core/lib/iomgr/iomgr_posix.cc +2 -0
  73. data/src/core/lib/iomgr/iomgr_posix_cfstream.cc +40 -10
  74. data/src/core/lib/iomgr/pollset_custom.cc +2 -2
  75. data/src/core/lib/iomgr/pollset_custom.h +3 -1
  76. data/src/core/lib/iomgr/pollset_uv.cc +3 -1
  77. data/src/core/lib/iomgr/pollset_uv.h +5 -1
  78. data/src/core/lib/iomgr/port.h +7 -5
  79. data/src/core/lib/iomgr/resolve_address.cc +5 -1
  80. data/src/core/lib/iomgr/resolve_address.h +6 -0
  81. data/src/core/lib/iomgr/sockaddr.h +1 -0
  82. data/src/core/lib/iomgr/socket_mutator.cc +15 -2
  83. data/src/core/lib/iomgr/socket_mutator.h +26 -2
  84. data/src/core/lib/iomgr/socket_utils_common_posix.cc +4 -4
  85. data/src/core/lib/iomgr/socket_utils_posix.h +2 -2
  86. data/src/core/lib/iomgr/tcp_client_posix.cc +7 -2
  87. data/src/core/lib/iomgr/tcp_posix.cc +42 -39
  88. data/src/core/lib/iomgr/tcp_posix.h +8 -0
  89. data/src/core/lib/iomgr/tcp_server_custom.cc +3 -4
  90. data/src/core/lib/iomgr/tcp_server_posix.cc +6 -0
  91. data/src/core/lib/iomgr/tcp_server_utils_posix_common.cc +2 -1
  92. data/src/core/lib/iomgr/timer.h +6 -1
  93. data/src/core/lib/security/authorization/authorization_engine.h +44 -0
  94. data/src/core/lib/security/authorization/authorization_policy_provider.h +32 -0
  95. data/src/core/lib/security/authorization/authorization_policy_provider_vtable.cc +46 -0
  96. data/src/core/lib/security/authorization/evaluate_args.cc +209 -0
  97. data/src/core/lib/security/authorization/evaluate_args.h +91 -0
  98. data/src/core/lib/security/credentials/google_default/google_default_credentials.cc +3 -1
  99. data/src/core/lib/security/credentials/tls/tls_utils.cc +32 -0
  100. data/src/core/lib/security/credentials/tls/tls_utils.h +13 -0
  101. data/src/core/lib/security/security_connector/local/local_security_connector.cc +9 -6
  102. data/src/core/lib/security/security_connector/ssl_utils.cc +5 -0
  103. data/src/core/lib/surface/call.cc +21 -1
  104. data/src/core/lib/surface/call.h +11 -0
  105. data/src/core/lib/surface/completion_queue.cc +22 -22
  106. data/src/core/lib/surface/completion_queue.h +1 -1
  107. data/src/core/lib/surface/completion_queue_factory.cc +1 -2
  108. data/src/core/lib/surface/init.cc +1 -3
  109. data/src/core/lib/surface/init.h +10 -1
  110. data/src/core/lib/surface/version.cc +1 -1
  111. data/src/core/lib/transport/error_utils.cc +2 -2
  112. data/src/core/lib/transport/transport.h +2 -0
  113. data/src/core/lib/transport/transport_op_string.cc +1 -1
  114. data/src/core/plugin_registry/grpc_plugin_registry.cc +4 -0
  115. data/src/core/tsi/alts/crypt/gsec.h +2 -0
  116. data/src/ruby/ext/grpc/extconf.rb +2 -0
  117. data/src/ruby/ext/grpc/rb_grpc_imports.generated.c +6 -0
  118. data/src/ruby/ext/grpc/rb_grpc_imports.generated.h +10 -1
  119. data/src/ruby/lib/grpc/version.rb +1 -1
  120. data/third_party/boringssl-with-bazel/err_data.c +269 -263
  121. data/third_party/boringssl-with-bazel/src/crypto/asn1/a_object.c +8 -6
  122. data/third_party/boringssl-with-bazel/src/crypto/cipher_extra/cipher_extra.c +4 -0
  123. data/third_party/boringssl-with-bazel/src/crypto/curve25519/curve25519.c +1 -1
  124. data/third_party/boringssl-with-bazel/src/crypto/curve25519/internal.h +1 -1
  125. data/third_party/boringssl-with-bazel/src/crypto/evp/evp.c +9 -0
  126. data/third_party/boringssl-with-bazel/src/crypto/fipsmodule/bn/prime.c +0 -4
  127. data/third_party/boringssl-with-bazel/src/crypto/fipsmodule/digest/digest.c +7 -0
  128. data/third_party/boringssl-with-bazel/src/crypto/fipsmodule/digest/md32_common.h +87 -121
  129. data/third_party/boringssl-with-bazel/src/crypto/fipsmodule/md4/md4.c +20 -30
  130. data/third_party/boringssl-with-bazel/src/crypto/fipsmodule/md5/md5.c +19 -30
  131. data/third_party/boringssl-with-bazel/src/crypto/fipsmodule/rand/internal.h +1 -4
  132. data/third_party/boringssl-with-bazel/src/crypto/fipsmodule/rand/rand.c +0 -13
  133. data/third_party/boringssl-with-bazel/src/crypto/fipsmodule/rsa/rsa.c +26 -24
  134. data/third_party/boringssl-with-bazel/src/crypto/fipsmodule/rsa/rsa_impl.c +10 -7
  135. data/third_party/boringssl-with-bazel/src/crypto/fipsmodule/sha/sha1.c +28 -39
  136. data/third_party/boringssl-with-bazel/src/crypto/fipsmodule/sha/sha256.c +48 -66
  137. data/third_party/boringssl-with-bazel/src/crypto/fipsmodule/sha/sha512.c +4 -5
  138. data/third_party/boringssl-with-bazel/src/crypto/hpke/hpke.c +362 -371
  139. data/third_party/boringssl-with-bazel/src/crypto/pkcs7/pkcs7_x509.c +4 -2
  140. data/third_party/boringssl-with-bazel/src/crypto/rand_extra/passive.c +2 -2
  141. data/third_party/boringssl-with-bazel/src/crypto/rsa_extra/rsa_asn1.c +1 -2
  142. data/third_party/boringssl-with-bazel/src/crypto/x509/internal.h +101 -11
  143. data/third_party/boringssl-with-bazel/src/crypto/x509/t_x509a.c +3 -0
  144. data/third_party/boringssl-with-bazel/src/crypto/x509/x509_cmp.c +2 -2
  145. data/third_party/boringssl-with-bazel/src/crypto/x509/x509_req.c +3 -0
  146. data/third_party/boringssl-with-bazel/src/crypto/x509/x509_set.c +1 -1
  147. data/third_party/boringssl-with-bazel/src/crypto/x509/x509_trs.c +2 -0
  148. data/third_party/boringssl-with-bazel/src/crypto/x509/x509_vfy.c +14 -15
  149. data/third_party/boringssl-with-bazel/src/crypto/x509/x509_vpm.c +53 -73
  150. data/third_party/boringssl-with-bazel/src/crypto/x509/x509cset.c +31 -0
  151. data/third_party/boringssl-with-bazel/src/crypto/x509/x509rset.c +3 -0
  152. data/third_party/boringssl-with-bazel/src/crypto/x509/x_all.c +3 -0
  153. data/third_party/boringssl-with-bazel/src/crypto/x509/x_req.c +5 -8
  154. data/third_party/boringssl-with-bazel/src/crypto/x509/x_sig.c +5 -0
  155. data/third_party/boringssl-with-bazel/src/crypto/x509/x_x509a.c +3 -0
  156. data/third_party/boringssl-with-bazel/src/crypto/x509v3/internal.h +7 -0
  157. data/third_party/boringssl-with-bazel/src/crypto/x509v3/v3_purp.c +1 -1
  158. data/third_party/boringssl-with-bazel/src/crypto/x509v3/v3_utl.c +5 -8
  159. data/third_party/boringssl-with-bazel/src/include/openssl/aead.h +1 -1
  160. data/third_party/boringssl-with-bazel/src/include/openssl/arm_arch.h +66 -1
  161. data/third_party/boringssl-with-bazel/src/include/openssl/base.h +40 -9
  162. data/third_party/boringssl-with-bazel/src/include/openssl/bytestring.h +1 -0
  163. data/third_party/boringssl-with-bazel/src/include/openssl/chacha.h +1 -1
  164. data/third_party/boringssl-with-bazel/src/include/openssl/digest.h +6 -2
  165. data/third_party/boringssl-with-bazel/src/include/openssl/ecdsa.h +14 -0
  166. data/third_party/boringssl-with-bazel/src/include/openssl/evp.h +19 -11
  167. data/third_party/boringssl-with-bazel/src/include/openssl/hpke.h +325 -0
  168. data/third_party/boringssl-with-bazel/src/include/openssl/pkcs7.h +23 -7
  169. data/third_party/boringssl-with-bazel/src/include/openssl/rsa.h +99 -63
  170. data/third_party/boringssl-with-bazel/src/include/openssl/ssl.h +139 -109
  171. data/third_party/boringssl-with-bazel/src/include/openssl/tls1.h +12 -19
  172. data/third_party/boringssl-with-bazel/src/include/openssl/x509.h +48 -50
  173. data/third_party/boringssl-with-bazel/src/include/openssl/x509_vfy.h +451 -435
  174. data/third_party/boringssl-with-bazel/src/include/openssl/x509v3.h +0 -1
  175. data/third_party/boringssl-with-bazel/src/ssl/d1_both.cc +2 -2
  176. data/third_party/boringssl-with-bazel/src/ssl/d1_srtp.cc +1 -1
  177. data/third_party/boringssl-with-bazel/src/ssl/encrypted_client_hello.cc +773 -84
  178. data/third_party/boringssl-with-bazel/src/ssl/handoff.cc +80 -47
  179. data/third_party/boringssl-with-bazel/src/ssl/handshake.cc +24 -19
  180. data/third_party/boringssl-with-bazel/src/ssl/handshake_client.cc +189 -86
  181. data/third_party/boringssl-with-bazel/src/ssl/handshake_server.cc +45 -56
  182. data/third_party/boringssl-with-bazel/src/ssl/internal.h +272 -167
  183. data/third_party/boringssl-with-bazel/src/ssl/s3_both.cc +2 -2
  184. data/third_party/boringssl-with-bazel/src/ssl/s3_lib.cc +2 -2
  185. data/third_party/boringssl-with-bazel/src/ssl/s3_pkt.cc +14 -19
  186. data/third_party/boringssl-with-bazel/src/ssl/ssl_lib.cc +34 -102
  187. data/third_party/boringssl-with-bazel/src/ssl/ssl_privkey.cc +2 -0
  188. data/third_party/boringssl-with-bazel/src/ssl/ssl_session.cc +8 -31
  189. data/third_party/boringssl-with-bazel/src/ssl/ssl_stat.cc +3 -0
  190. data/third_party/boringssl-with-bazel/src/ssl/ssl_transcript.cc +4 -3
  191. data/third_party/boringssl-with-bazel/src/ssl/ssl_versions.cc +7 -3
  192. data/third_party/boringssl-with-bazel/src/ssl/t1_lib.cc +576 -648
  193. data/third_party/boringssl-with-bazel/src/ssl/tls13_both.cc +31 -3
  194. data/third_party/boringssl-with-bazel/src/ssl/tls13_client.cc +98 -39
  195. data/third_party/boringssl-with-bazel/src/ssl/tls13_enc.cc +141 -94
  196. data/third_party/boringssl-with-bazel/src/ssl/tls13_server.cc +58 -68
  197. metadata +65 -40
  198. data/third_party/boringssl-with-bazel/src/crypto/hpke/internal.h +0 -267
  199. data/third_party/boringssl-with-bazel/src/crypto/x509/vpm_int.h +0 -71
@@ -884,7 +884,6 @@ OPENSSL_EXPORT int X509_check_ip_asc(X509 *x, const char *ipasc,
884
884
 
885
885
  OPENSSL_EXPORT ASN1_OCTET_STRING *a2i_IPADDRESS(const char *ipasc);
886
886
  OPENSSL_EXPORT ASN1_OCTET_STRING *a2i_IPADDRESS_NC(const char *ipasc);
887
- OPENSSL_EXPORT int a2i_ipadd(unsigned char *ipout, const char *ipasc);
888
887
  OPENSSL_EXPORT int X509V3_NAME_from_section(X509_NAME *nm,
889
888
  STACK_OF(CONF_VALUE) *dn_sk,
890
889
  unsigned long chtype);
@@ -503,7 +503,7 @@ void dtls_clear_outgoing_messages(SSL *ssl) {
503
503
  ssl->d1->flight_has_reply = false;
504
504
  }
505
505
 
506
- bool dtls1_init_message(SSL *ssl, CBB *cbb, CBB *body, uint8_t type) {
506
+ bool dtls1_init_message(const SSL *ssl, CBB *cbb, CBB *body, uint8_t type) {
507
507
  // Pick a modest size hint to save most of the |realloc| calls.
508
508
  if (!CBB_init(cbb, 64) ||
509
509
  !CBB_add_u8(cbb, type) ||
@@ -517,7 +517,7 @@ bool dtls1_init_message(SSL *ssl, CBB *cbb, CBB *body, uint8_t type) {
517
517
  return true;
518
518
  }
519
519
 
520
- bool dtls1_finish_message(SSL *ssl, CBB *cbb, Array<uint8_t> *out_msg) {
520
+ bool dtls1_finish_message(const SSL *ssl, CBB *cbb, Array<uint8_t> *out_msg) {
521
521
  if (!CBBFinishArray(cbb, out_msg) ||
522
522
  out_msg->size() < DTLS1_HM_HEADER_LENGTH) {
523
523
  OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
@@ -202,7 +202,7 @@ int SSL_set_srtp_profiles(SSL *ssl, const char *profiles) {
202
202
  ssl_ctx_make_profiles(profiles, &ssl->config->srtp_profiles);
203
203
  }
204
204
 
205
- STACK_OF(SRTP_PROTECTION_PROFILE) *SSL_get_srtp_profiles(SSL *ssl) {
205
+ const STACK_OF(SRTP_PROTECTION_PROFILE) *SSL_get_srtp_profiles(const SSL *ssl) {
206
206
  if (ssl == nullptr) {
207
207
  return nullptr;
208
208
  }
@@ -12,11 +12,21 @@
12
12
  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
13
13
  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
14
14
 
15
+ #include <openssl/ssl.h>
16
+
17
+ #include <assert.h>
18
+ #include <string.h>
19
+
20
+ #include <algorithm>
21
+ #include <utility>
22
+
23
+ #include <openssl/aead.h>
15
24
  #include <openssl/bytestring.h>
16
25
  #include <openssl/curve25519.h>
17
26
  #include <openssl/err.h>
18
27
  #include <openssl/hkdf.h>
19
- #include <openssl/ssl.h>
28
+ #include <openssl/hpke.h>
29
+ #include <openssl/rand.h>
20
30
 
21
31
  #include "internal.h"
22
32
 
@@ -29,6 +39,25 @@
29
39
 
30
40
  BSSL_NAMESPACE_BEGIN
31
41
 
42
+ // ECH reuses the extension code point for the version number.
43
+ static const uint16_t kECHConfigVersion = TLSEXT_TYPE_encrypted_client_hello;
44
+
45
+ static const decltype(&EVP_hpke_aes_128_gcm) kSupportedAEADs[] = {
46
+ &EVP_hpke_aes_128_gcm,
47
+ &EVP_hpke_aes_256_gcm,
48
+ &EVP_hpke_chacha20_poly1305,
49
+ };
50
+
51
+ static const EVP_HPKE_AEAD *get_ech_aead(uint16_t aead_id) {
52
+ for (const auto aead_func : kSupportedAEADs) {
53
+ const EVP_HPKE_AEAD *aead = aead_func();
54
+ if (aead_id == EVP_HPKE_AEAD_id(aead)) {
55
+ return aead;
56
+ }
57
+ }
58
+ return nullptr;
59
+ }
60
+
32
61
  // ssl_client_hello_write_without_extensions serializes |client_hello| into
33
62
  // |out|, omitting the length-prefixed extensions. It serializes individual
34
63
  // fields, starting with |client_hello->version|, and ignores the
@@ -248,22 +277,21 @@ bool ssl_decode_client_hello_inner(
248
277
  bool ssl_client_hello_decrypt(
249
278
  EVP_HPKE_CTX *hpke_ctx, Array<uint8_t> *out_encoded_client_hello_inner,
250
279
  bool *out_is_decrypt_error, const SSL_CLIENT_HELLO *client_hello_outer,
251
- uint16_t kdf_id, uint16_t aead_id, Span<const uint8_t> config_id,
280
+ uint16_t kdf_id, uint16_t aead_id, const uint8_t config_id,
252
281
  Span<const uint8_t> enc, Span<const uint8_t> payload) {
253
282
  *out_is_decrypt_error = false;
254
283
 
255
284
  // Compute the ClientHello portion of the ClientHelloOuterAAD value. See
256
- // draft-ietf-tls-esni-09, section 5.2.
257
- ScopedCBB ch_outer_aad_cbb;
258
- CBB config_id_cbb, enc_cbb, outer_hello_cbb, extensions_cbb;
259
- if (!CBB_init(ch_outer_aad_cbb.get(), 0) ||
260
- !CBB_add_u16(ch_outer_aad_cbb.get(), kdf_id) ||
261
- !CBB_add_u16(ch_outer_aad_cbb.get(), aead_id) ||
262
- !CBB_add_u8_length_prefixed(ch_outer_aad_cbb.get(), &config_id_cbb) ||
263
- !CBB_add_bytes(&config_id_cbb, config_id.data(), config_id.size()) ||
264
- !CBB_add_u16_length_prefixed(ch_outer_aad_cbb.get(), &enc_cbb) ||
285
+ // draft-ietf-tls-esni-10, section 5.2.
286
+ ScopedCBB aad;
287
+ CBB enc_cbb, outer_hello_cbb, extensions_cbb;
288
+ if (!CBB_init(aad.get(), 256) ||
289
+ !CBB_add_u16(aad.get(), kdf_id) ||
290
+ !CBB_add_u16(aad.get(), aead_id) ||
291
+ !CBB_add_u8(aad.get(), config_id) ||
292
+ !CBB_add_u16_length_prefixed(aad.get(), &enc_cbb) ||
265
293
  !CBB_add_bytes(&enc_cbb, enc.data(), enc.size()) ||
266
- !CBB_add_u24_length_prefixed(ch_outer_aad_cbb.get(), &outer_hello_cbb) ||
294
+ !CBB_add_u24_length_prefixed(aad.get(), &outer_hello_cbb) ||
267
295
  !ssl_client_hello_write_without_extensions(client_hello_outer,
268
296
  &outer_hello_cbb) ||
269
297
  !CBB_add_u16_length_prefixed(&outer_hello_cbb, &extensions_cbb)) {
@@ -292,11 +320,25 @@ bool ssl_client_hello_decrypt(
292
320
  return false;
293
321
  }
294
322
  }
295
- if (!CBB_flush(ch_outer_aad_cbb.get())) {
323
+ if (!CBB_flush(aad.get())) {
296
324
  OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
297
325
  return false;
298
326
  }
299
327
 
328
+ #if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
329
+ // In fuzzer mode, disable encryption to improve coverage. We reserve a short
330
+ // input to signal decryption failure, so the fuzzer can explore fallback to
331
+ // ClientHelloOuter.
332
+ const uint8_t kBadPayload[] = {0xff};
333
+ if (payload == kBadPayload) {
334
+ *out_is_decrypt_error = true;
335
+ OPENSSL_PUT_ERROR(SSL, SSL_R_DECRYPTION_FAILED);
336
+ return false;
337
+ }
338
+ if (!out_encoded_client_hello_inner->CopyFrom(payload)) {
339
+ return false;
340
+ }
341
+ #else
300
342
  // Attempt to decrypt into |out_encoded_client_hello_inner|.
301
343
  if (!out_encoded_client_hello_inner->Init(payload.size())) {
302
344
  OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
@@ -306,77 +348,235 @@ bool ssl_client_hello_decrypt(
306
348
  if (!EVP_HPKE_CTX_open(hpke_ctx, out_encoded_client_hello_inner->data(),
307
349
  &encoded_client_hello_inner_len,
308
350
  out_encoded_client_hello_inner->size(), payload.data(),
309
- payload.size(), CBB_data(ch_outer_aad_cbb.get()),
310
- CBB_len(ch_outer_aad_cbb.get()))) {
351
+ payload.size(), CBB_data(aad.get()),
352
+ CBB_len(aad.get()))) {
311
353
  *out_is_decrypt_error = true;
312
354
  OPENSSL_PUT_ERROR(SSL, SSL_R_DECRYPTION_FAILED);
313
355
  return false;
314
356
  }
315
357
  out_encoded_client_hello_inner->Shrink(encoded_client_hello_inner_len);
358
+ #endif
316
359
  return true;
317
360
  }
318
361
 
362
+ static bool parse_ipv4_number(Span<const uint8_t> in, uint32_t *out) {
363
+ // See https://url.spec.whatwg.org/#ipv4-number-parser.
364
+ uint32_t base = 10;
365
+ if (in.size() >= 2 && in[0] == '0' && (in[1] == 'x' || in[1] == 'X')) {
366
+ in = in.subspan(2);
367
+ base = 16;
368
+ } else if (in.size() >= 1 && in[0] == '0') {
369
+ in = in.subspan(1);
370
+ base = 8;
371
+ }
372
+ *out = 0;
373
+ for (uint8_t c : in) {
374
+ uint32_t d;
375
+ if ('0' <= c && c <= '9') {
376
+ d = c - '0';
377
+ } else if ('a' <= c && c <= 'f') {
378
+ d = c - 'a' + 10;
379
+ } else if ('A' <= c && c <= 'F') {
380
+ d = c - 'A' + 10;
381
+ } else {
382
+ return false;
383
+ }
384
+ if (d >= base ||
385
+ *out > UINT32_MAX / base) {
386
+ return false;
387
+ }
388
+ *out *= base;
389
+ if (*out > UINT32_MAX - d) {
390
+ return false;
391
+ }
392
+ *out += d;
393
+ }
394
+ return true;
395
+ }
319
396
 
320
- bool ECHServerConfig::Init(Span<const uint8_t> raw,
321
- Span<const uint8_t> private_key,
322
- bool is_retry_config) {
323
- assert(!initialized_);
324
- is_retry_config_ = is_retry_config;
397
+ static bool is_ipv4_address(Span<const uint8_t> in) {
398
+ // See https://url.spec.whatwg.org/#concept-ipv4-parser
399
+ uint32_t numbers[4];
400
+ size_t num_numbers = 0;
401
+ while (!in.empty()) {
402
+ if (num_numbers == 4) {
403
+ // Too many components.
404
+ return false;
405
+ }
406
+ // Find the next dot-separated component.
407
+ auto dot = std::find(in.begin(), in.end(), '.');
408
+ if (dot == in.begin()) {
409
+ // Empty components are not allowed.
410
+ return false;
411
+ }
412
+ Span<const uint8_t> component;
413
+ if (dot == in.end()) {
414
+ component = in;
415
+ in = Span<const uint8_t>();
416
+ } else {
417
+ component = in.subspan(0, dot - in.begin());
418
+ in = in.subspan(dot - in.begin() + 1); // Skip the dot.
419
+ }
420
+ if (!parse_ipv4_number(component, &numbers[num_numbers])) {
421
+ return false;
422
+ }
423
+ num_numbers++;
424
+ }
425
+ if (num_numbers == 0) {
426
+ return false;
427
+ }
428
+ for (size_t i = 0; i < num_numbers - 1; i++) {
429
+ if (numbers[i] > 255) {
430
+ return false;
431
+ }
432
+ }
433
+ return num_numbers == 1 ||
434
+ numbers[num_numbers - 1] < 1u << (8 * (5 - num_numbers));
435
+ }
325
436
 
326
- if (!raw_.CopyFrom(raw)) {
327
- OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
437
+ bool ssl_is_valid_ech_public_name(Span<const uint8_t> public_name) {
438
+ // See draft-ietf-tls-esni-11, Section 4 and RFC5890, Section 2.3.1. The
439
+ // public name must be a dot-separated sequence of LDH labels and not begin or
440
+ // end with a dot.
441
+ auto copy = public_name;
442
+ if (copy.empty()) {
328
443
  return false;
329
444
  }
330
- // Read from |raw_| so we can save Spans with the same lifetime as |this|.
331
- CBS reader(raw_);
445
+ while (!copy.empty()) {
446
+ // Find the next dot-separated component.
447
+ auto dot = std::find(copy.begin(), copy.end(), '.');
448
+ Span<const uint8_t> component;
449
+ if (dot == copy.end()) {
450
+ component = copy;
451
+ copy = Span<const uint8_t>();
452
+ } else {
453
+ component = copy.subspan(0, dot - copy.begin());
454
+ copy = copy.subspan(dot - copy.begin() + 1); // Skip the dot.
455
+ if (copy.empty()) {
456
+ // Trailing dots are not allowed.
457
+ return false;
458
+ }
459
+ }
460
+ // |component| must be a valid LDH label. Checking for empty components also
461
+ // rejects leading dots.
462
+ if (component.empty() || component.size() > 63 ||
463
+ component.front() == '-' || component.back() == '-') {
464
+ return false;
465
+ }
466
+ for (uint8_t c : component) {
467
+ if (!('a' <= c && c <= 'z') && !('A' <= c && c <= 'Z') &&
468
+ !('0' <= c && c <= '9') && c != '-') {
469
+ return false;
470
+ }
471
+ }
472
+ }
473
+
474
+ return !is_ipv4_address(public_name);
475
+ }
332
476
 
477
+ static bool parse_ech_config(CBS *cbs, ECHConfig *out, bool *out_supported,
478
+ bool all_extensions_mandatory) {
333
479
  uint16_t version;
334
- if (!CBS_get_u16(&reader, &version)) {
480
+ CBS orig = *cbs;
481
+ CBS contents;
482
+ if (!CBS_get_u16(cbs, &version) ||
483
+ !CBS_get_u16_length_prefixed(cbs, &contents)) {
335
484
  OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
336
485
  return false;
337
486
  }
487
+
488
+ if (version != kECHConfigVersion) {
489
+ *out_supported = false;
490
+ return true;
491
+ }
492
+
493
+ // Make a copy of the ECHConfig and parse from it, so the results alias into
494
+ // the saved copy.
495
+ if (!out->raw.CopyFrom(
496
+ MakeConstSpan(CBS_data(&orig), CBS_len(&orig) - CBS_len(cbs)))) {
497
+ return false;
498
+ }
499
+
500
+ CBS ech_config(out->raw);
501
+ CBS public_name, public_key, cipher_suites, extensions;
502
+ if (!CBS_skip(&ech_config, 2) || // version
503
+ !CBS_get_u16_length_prefixed(&ech_config, &contents) ||
504
+ !CBS_get_u8(&contents, &out->config_id) ||
505
+ !CBS_get_u16(&contents, &out->kem_id) ||
506
+ !CBS_get_u16_length_prefixed(&contents, &public_key) ||
507
+ CBS_len(&public_key) == 0 ||
508
+ !CBS_get_u16_length_prefixed(&contents, &cipher_suites) ||
509
+ CBS_len(&cipher_suites) == 0 || CBS_len(&cipher_suites) % 4 != 0 ||
510
+ !CBS_get_u16(&contents, &out->maximum_name_length) ||
511
+ !CBS_get_u16_length_prefixed(&contents, &public_name) ||
512
+ CBS_len(&public_name) == 0 ||
513
+ !CBS_get_u16_length_prefixed(&contents, &extensions) ||
514
+ CBS_len(&contents) != 0) {
515
+ OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
516
+ return false;
517
+ }
518
+
519
+ if (!ssl_is_valid_ech_public_name(public_name)) {
520
+ // TODO(https://crbug.com/boringssl/275): The draft says ECHConfigs with
521
+ // invalid public names should be ignored, but LDH syntax failures are
522
+ // unambiguously invalid.
523
+ *out_supported = false;
524
+ return true;
525
+ }
526
+
527
+ out->public_key = public_key;
528
+ out->public_name = public_name;
529
+ // This function does not ensure |out->kem_id| and |out->cipher_suites| use
530
+ // supported algorithms. The caller must do this.
531
+ out->cipher_suites = cipher_suites;
532
+
533
+ bool has_unknown_mandatory_extension = false;
534
+ while (CBS_len(&extensions) != 0) {
535
+ uint16_t type;
536
+ CBS body;
537
+ if (!CBS_get_u16(&extensions, &type) ||
538
+ !CBS_get_u16_length_prefixed(&extensions, &body)) {
539
+ OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
540
+ return false;
541
+ }
542
+ // We currently do not support any extensions.
543
+ if (type & 0x8000 || all_extensions_mandatory) {
544
+ // Extension numbers with the high bit set are mandatory. Continue parsing
545
+ // to enforce syntax, but we will ultimately ignore this ECHConfig as a
546
+ // client and reject it as a server.
547
+ has_unknown_mandatory_extension = true;
548
+ }
549
+ }
550
+
551
+ *out_supported = !has_unknown_mandatory_extension;
552
+ return true;
553
+ }
554
+
555
+ bool ECHServerConfig::Init(Span<const uint8_t> ech_config,
556
+ const EVP_HPKE_KEY *key, bool is_retry_config) {
557
+ is_retry_config_ = is_retry_config;
558
+
338
559
  // Parse the ECHConfig, rejecting all unsupported parameters and extensions.
339
560
  // Unlike most server options, ECH's server configuration is serialized and
340
561
  // configured in both the server and DNS. If the caller configures an
341
562
  // unsupported parameter, this is a deployment error. To catch these errors,
342
563
  // we fail early.
343
- if (version != TLSEXT_TYPE_encrypted_client_hello) {
344
- OPENSSL_PUT_ERROR(SSL, SSL_R_UNSUPPORTED_ECH_SERVER_CONFIG);
564
+ CBS cbs = ech_config;
565
+ bool supported;
566
+ if (!parse_ech_config(&cbs, &ech_config_, &supported,
567
+ /*all_extensions_mandatory=*/true)) {
345
568
  return false;
346
569
  }
347
-
348
- CBS ech_config_contents, public_name, public_key, cipher_suites, extensions;
349
- uint16_t kem_id, max_name_len;
350
- if (!CBS_get_u16_length_prefixed(&reader, &ech_config_contents) ||
351
- !CBS_get_u16_length_prefixed(&ech_config_contents, &public_name) ||
352
- CBS_len(&public_name) == 0 ||
353
- !CBS_get_u16_length_prefixed(&ech_config_contents, &public_key) ||
354
- CBS_len(&public_key) == 0 ||
355
- !CBS_get_u16(&ech_config_contents, &kem_id) ||
356
- !CBS_get_u16_length_prefixed(&ech_config_contents, &cipher_suites) ||
357
- CBS_len(&cipher_suites) == 0 ||
358
- !CBS_get_u16(&ech_config_contents, &max_name_len) ||
359
- !CBS_get_u16_length_prefixed(&ech_config_contents, &extensions) ||
360
- CBS_len(&ech_config_contents) != 0 || //
361
- CBS_len(&reader) != 0) {
570
+ if (CBS_len(&cbs) != 0) {
362
571
  OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
363
572
  return false;
364
573
  }
365
- // We only support one KEM, and the KEM decides the length of |public_key|.
366
- if (CBS_len(&public_key) != X25519_PUBLIC_VALUE_LEN ||
367
- kem_id != EVP_HPKE_DHKEM_X25519_HKDF_SHA256) {
574
+ if (!supported) {
368
575
  OPENSSL_PUT_ERROR(SSL, SSL_R_UNSUPPORTED_ECH_SERVER_CONFIG);
369
576
  return false;
370
577
  }
371
- public_key_ = public_key;
372
-
373
- // We do not support any ECHConfig extensions, so |extensions| must be empty.
374
- if (CBS_len(&extensions) != 0) {
375
- OPENSSL_PUT_ERROR(SSL, SSL_R_ECH_SERVER_CONFIG_UNSUPPORTED_EXTENSION);
376
- return false;
377
- }
378
578
 
379
- cipher_suites_ = cipher_suites;
579
+ CBS cipher_suites = ech_config_.cipher_suites;
380
580
  while (CBS_len(&cipher_suites) > 0) {
381
581
  uint16_t kdf_id, aead_id;
382
582
  if (!CBS_get_u16(&cipher_suites, &kdf_id) ||
@@ -384,50 +584,42 @@ bool ECHServerConfig::Init(Span<const uint8_t> raw,
384
584
  OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
385
585
  return false;
386
586
  }
387
- // This parser fails when it encounters any bytes it does not understand. If
388
- // the config lists any unsupported cipher suites, that is a parse error.
389
- if (kdf_id != EVP_HPKE_HKDF_SHA256 ||
390
- (aead_id != EVP_HPKE_AEAD_AES_128_GCM &&
391
- aead_id != EVP_HPKE_AEAD_AES_256_GCM &&
392
- aead_id != EVP_HPKE_AEAD_CHACHA20POLY1305)) {
587
+ // The server promises to support every option in the ECHConfig, so reject
588
+ // any unsupported cipher suites.
589
+ if (kdf_id != EVP_HPKE_HKDF_SHA256 || get_ech_aead(aead_id) == nullptr) {
393
590
  OPENSSL_PUT_ERROR(SSL, SSL_R_UNSUPPORTED_ECH_SERVER_CONFIG);
394
591
  return false;
395
592
  }
396
593
  }
397
594
 
398
- // Precompute the config_id.
399
- uint8_t key[EVP_MAX_KEY_LENGTH];
400
- size_t key_len;
401
- static const uint8_t kInfo[] = "tls ech config id";
402
- if (!HKDF_extract(key, &key_len, EVP_sha256(), raw_.data(), raw_.size(),
403
- nullptr, 0) ||
404
- !HKDF_expand(config_id_sha256_, sizeof(config_id_sha256_), EVP_sha256(),
405
- key, key_len, kInfo, OPENSSL_ARRAY_SIZE(kInfo) - 1)) {
406
- OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
595
+ // Check the public key in the ECHConfig matches |key|.
596
+ uint8_t expected_public_key[EVP_HPKE_MAX_PUBLIC_KEY_LENGTH];
597
+ size_t expected_public_key_len;
598
+ if (!EVP_HPKE_KEY_public_key(key, expected_public_key,
599
+ &expected_public_key_len,
600
+ sizeof(expected_public_key))) {
407
601
  return false;
408
602
  }
409
-
410
- if (private_key.size() != X25519_PRIVATE_KEY_LEN) {
411
- OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
603
+ if (ech_config_.kem_id != EVP_HPKE_KEM_id(EVP_HPKE_KEY_kem(key)) ||
604
+ MakeConstSpan(expected_public_key, expected_public_key_len) !=
605
+ ech_config_.public_key) {
606
+ OPENSSL_PUT_ERROR(SSL, SSL_R_ECH_SERVER_CONFIG_AND_PRIVATE_KEY_MISMATCH);
412
607
  return false;
413
608
  }
414
- uint8_t expected_public_key[X25519_PUBLIC_VALUE_LEN];
415
- X25519_public_from_private(expected_public_key, private_key.data());
416
- if (public_key_ != expected_public_key) {
417
- OPENSSL_PUT_ERROR(SSL, SSL_R_ECH_SERVER_CONFIG_AND_PRIVATE_KEY_MISMATCH);
609
+
610
+ if (!EVP_HPKE_KEY_copy(key_.get(), key)) {
418
611
  return false;
419
612
  }
420
- assert(sizeof(private_key_) == private_key.size());
421
- OPENSSL_memcpy(private_key_, private_key.data(), private_key.size());
422
613
 
423
- initialized_ = true;
424
614
  return true;
425
615
  }
426
616
 
427
- bool ECHServerConfig::SupportsCipherSuite(uint16_t kdf_id,
428
- uint16_t aead_id) const {
429
- assert(initialized_);
430
- CBS cbs(cipher_suites_);
617
+ bool ECHServerConfig::SetupContext(EVP_HPKE_CTX *ctx, uint16_t kdf_id,
618
+ uint16_t aead_id,
619
+ Span<const uint8_t> enc) const {
620
+ // Check the cipher suite is supported by this ECHServerConfig.
621
+ CBS cbs(ech_config_.cipher_suites);
622
+ bool cipher_ok = false;
431
623
  while (CBS_len(&cbs) != 0) {
432
624
  uint16_t supported_kdf_id, supported_aead_id;
433
625
  if (!CBS_get_u16(&cbs, &supported_kdf_id) ||
@@ -435,10 +627,507 @@ bool ECHServerConfig::SupportsCipherSuite(uint16_t kdf_id,
435
627
  return false;
436
628
  }
437
629
  if (kdf_id == supported_kdf_id && aead_id == supported_aead_id) {
438
- return true;
630
+ cipher_ok = true;
631
+ break;
632
+ }
633
+ }
634
+ if (!cipher_ok) {
635
+ return false;
636
+ }
637
+
638
+ static const uint8_t kInfoLabel[] = "tls ech";
639
+ ScopedCBB info_cbb;
640
+ if (!CBB_init(info_cbb.get(), sizeof(kInfoLabel) + ech_config_.raw.size()) ||
641
+ !CBB_add_bytes(info_cbb.get(), kInfoLabel,
642
+ sizeof(kInfoLabel) /* includes trailing NUL */) ||
643
+ !CBB_add_bytes(info_cbb.get(), ech_config_.raw.data(),
644
+ ech_config_.raw.size())) {
645
+ OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
646
+ return false;
647
+ }
648
+
649
+ assert(kdf_id == EVP_HPKE_HKDF_SHA256);
650
+ assert(get_ech_aead(aead_id) != NULL);
651
+ return EVP_HPKE_CTX_setup_recipient(
652
+ ctx, key_.get(), EVP_hpke_hkdf_sha256(), get_ech_aead(aead_id), enc.data(),
653
+ enc.size(), CBB_data(info_cbb.get()), CBB_len(info_cbb.get()));
654
+ }
655
+
656
+ bool ssl_is_valid_ech_config_list(Span<const uint8_t> ech_config_list) {
657
+ CBS cbs = ech_config_list, child;
658
+ if (!CBS_get_u16_length_prefixed(&cbs, &child) || //
659
+ CBS_len(&child) == 0 || //
660
+ CBS_len(&cbs) > 0) {
661
+ return false;
662
+ }
663
+ while (CBS_len(&child) > 0) {
664
+ ECHConfig ech_config;
665
+ bool supported;
666
+ if (!parse_ech_config(&child, &ech_config, &supported,
667
+ /*all_extensions_mandatory=*/false)) {
668
+ return false;
669
+ }
670
+ }
671
+ return true;
672
+ }
673
+
674
+ static bool select_ech_cipher_suite(const EVP_HPKE_KDF **out_kdf,
675
+ const EVP_HPKE_AEAD **out_aead,
676
+ Span<const uint8_t> cipher_suites) {
677
+ const bool has_aes_hardware = EVP_has_aes_hardware();
678
+ const EVP_HPKE_AEAD *aead = nullptr;
679
+ CBS cbs = cipher_suites;
680
+ while (CBS_len(&cbs) != 0) {
681
+ uint16_t kdf_id, aead_id;
682
+ if (!CBS_get_u16(&cbs, &kdf_id) || //
683
+ !CBS_get_u16(&cbs, &aead_id)) {
684
+ return false;
685
+ }
686
+ // Pick the first common cipher suite, but prefer ChaCha20-Poly1305 if we
687
+ // don't have AES hardware.
688
+ const EVP_HPKE_AEAD *candidate = get_ech_aead(aead_id);
689
+ if (kdf_id != EVP_HPKE_HKDF_SHA256 || candidate == nullptr) {
690
+ continue;
691
+ }
692
+ if (aead == nullptr ||
693
+ (!has_aes_hardware && aead_id == EVP_HPKE_CHACHA20_POLY1305)) {
694
+ aead = candidate;
695
+ }
696
+ }
697
+ if (aead == nullptr) {
698
+ return false;
699
+ }
700
+
701
+ *out_kdf = EVP_hpke_hkdf_sha256();
702
+ *out_aead = aead;
703
+ return true;
704
+ }
705
+
706
+ bool ssl_select_ech_config(SSL_HANDSHAKE *hs, Span<uint8_t> out_enc,
707
+ size_t *out_enc_len) {
708
+ *out_enc_len = 0;
709
+ if (hs->max_version < TLS1_3_VERSION) {
710
+ // ECH requires TLS 1.3.
711
+ return true;
712
+ }
713
+
714
+ if (!hs->config->client_ech_config_list.empty()) {
715
+ CBS cbs = MakeConstSpan(hs->config->client_ech_config_list);
716
+ CBS child;
717
+ if (!CBS_get_u16_length_prefixed(&cbs, &child) || //
718
+ CBS_len(&child) == 0 || //
719
+ CBS_len(&cbs) > 0) {
720
+ return false;
721
+ }
722
+ // Look for the first ECHConfig with supported parameters.
723
+ while (CBS_len(&child) > 0) {
724
+ ECHConfig ech_config;
725
+ bool supported;
726
+ if (!parse_ech_config(&child, &ech_config, &supported,
727
+ /*all_extensions_mandatory=*/false)) {
728
+ return false;
729
+ }
730
+ const EVP_HPKE_KEM *kem = EVP_hpke_x25519_hkdf_sha256();
731
+ const EVP_HPKE_KDF *kdf;
732
+ const EVP_HPKE_AEAD *aead;
733
+ if (supported && //
734
+ ech_config.kem_id == EVP_HPKE_DHKEM_X25519_HKDF_SHA256 &&
735
+ select_ech_cipher_suite(&kdf, &aead, ech_config.cipher_suites)) {
736
+ ScopedCBB info;
737
+ static const uint8_t kInfoLabel[] = "tls ech"; // includes trailing NUL
738
+ if (!CBB_init(info.get(), sizeof(kInfoLabel) + ech_config.raw.size()) ||
739
+ !CBB_add_bytes(info.get(), kInfoLabel, sizeof(kInfoLabel)) ||
740
+ !CBB_add_bytes(info.get(), ech_config.raw.data(),
741
+ ech_config.raw.size())) {
742
+ OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
743
+ return false;
744
+ }
745
+
746
+ if (!EVP_HPKE_CTX_setup_sender(
747
+ hs->ech_hpke_ctx.get(), out_enc.data(), out_enc_len,
748
+ out_enc.size(), kem, kdf, aead, ech_config.public_key.data(),
749
+ ech_config.public_key.size(), CBB_data(info.get()),
750
+ CBB_len(info.get())) ||
751
+ !hs->inner_transcript.Init()) {
752
+ return false;
753
+ }
754
+
755
+ hs->selected_ech_config = MakeUnique<ECHConfig>(std::move(ech_config));
756
+ return hs->selected_ech_config != nullptr;
757
+ }
439
758
  }
440
759
  }
441
- return false;
760
+
761
+ return true;
762
+ }
763
+
764
+ static size_t aead_overhead(const EVP_HPKE_AEAD *aead) {
765
+ #if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
766
+ // TODO(https://crbug.com/boringssl/275): Having to adjust the overhead
767
+ // everywhere is tedious. Change fuzzer mode to append a fake tag but still
768
+ // otherwise be cleartext, refresh corpora, and then inline this function.
769
+ return 0;
770
+ #else
771
+ return EVP_AEAD_max_overhead(EVP_HPKE_AEAD_aead(aead));
772
+ #endif
773
+ }
774
+
775
+ static size_t compute_extension_length(const EVP_HPKE_AEAD *aead,
776
+ size_t enc_len, size_t in_len) {
777
+ size_t ret = 4; // HpkeSymmetricCipherSuite cipher_suite
778
+ ret++; // uint8 config_id
779
+ ret += 2 + enc_len; // opaque enc<1..2^16-1>
780
+ ret += 2 + in_len + aead_overhead(aead); // opaque payload<1..2^16-1>
781
+ return ret;
782
+ }
783
+
784
+ // random_size returns a random value between |min| and |max|, inclusive.
785
+ static size_t random_size(size_t min, size_t max) {
786
+ assert(min < max);
787
+ size_t value;
788
+ RAND_bytes(reinterpret_cast<uint8_t *>(&value), sizeof(value));
789
+ return value % (max - min + 1) + min;
790
+ }
791
+
792
+ static bool setup_ech_grease(SSL_HANDSHAKE *hs) {
793
+ assert(!hs->selected_ech_config);
794
+ if (hs->max_version < TLS1_3_VERSION || !hs->config->ech_grease_enabled) {
795
+ return true;
796
+ }
797
+
798
+ const uint16_t kdf_id = EVP_HPKE_HKDF_SHA256;
799
+ const EVP_HPKE_AEAD *aead = EVP_has_aes_hardware()
800
+ ? EVP_hpke_aes_128_gcm()
801
+ : EVP_hpke_chacha20_poly1305();
802
+ static_assert(ssl_grease_ech_config_id < sizeof(hs->grease_seed),
803
+ "hs->grease_seed is too small");
804
+ uint8_t config_id = hs->grease_seed[ssl_grease_ech_config_id];
805
+
806
+ uint8_t enc[X25519_PUBLIC_VALUE_LEN];
807
+ uint8_t private_key_unused[X25519_PRIVATE_KEY_LEN];
808
+ X25519_keypair(enc, private_key_unused);
809
+
810
+ // To determine a plausible length for the payload, we estimate the size of a
811
+ // typical EncodedClientHelloInner without resumption:
812
+ //
813
+ // 2+32+1+2 version, random, legacy_session_id, legacy_compression_methods
814
+ // 2+4*2 cipher_suites (three TLS 1.3 ciphers, GREASE)
815
+ // 2 extensions prefix
816
+ // 4 ech_is_inner
817
+ // 4+1+2*2 supported_versions (TLS 1.3, GREASE)
818
+ // 4+1+10*2 outer_extensions (key_share, sigalgs, sct, alpn,
819
+ // supported_groups, status_request, psk_key_exchange_modes,
820
+ // compress_certificate, GREASE x2)
821
+ //
822
+ // The server_name extension has an overhead of 9 bytes. For now, arbitrarily
823
+ // estimate maximum_name_length to be between 32 and 100 bytes.
824
+ //
825
+ // TODO(https://crbug.com/boringssl/275): If the padding scheme changes to
826
+ // also round the entire payload, adjust this to match. See
827
+ // https://github.com/tlswg/draft-ietf-tls-esni/issues/433
828
+ const size_t overhead = aead_overhead(aead);
829
+ const size_t in_len = random_size(128, 196);
830
+ const size_t extension_len =
831
+ compute_extension_length(aead, sizeof(enc), in_len);
832
+ bssl::ScopedCBB cbb;
833
+ CBB enc_cbb, payload_cbb;
834
+ uint8_t *payload;
835
+ if (!CBB_init(cbb.get(), extension_len) ||
836
+ !CBB_add_u16(cbb.get(), kdf_id) ||
837
+ !CBB_add_u16(cbb.get(), EVP_HPKE_AEAD_id(aead)) ||
838
+ !CBB_add_u8(cbb.get(), config_id) ||
839
+ !CBB_add_u16_length_prefixed(cbb.get(), &enc_cbb) ||
840
+ !CBB_add_bytes(&enc_cbb, enc, sizeof(enc)) ||
841
+ !CBB_add_u16_length_prefixed(cbb.get(), &payload_cbb) ||
842
+ !CBB_add_space(&payload_cbb, &payload, in_len + overhead) ||
843
+ !RAND_bytes(payload, in_len + overhead) ||
844
+ !CBBFinishArray(cbb.get(), &hs->ech_client_bytes)) {
845
+ return false;
846
+ }
847
+ assert(hs->ech_client_bytes.size() == extension_len);
848
+ return true;
849
+ }
850
+
851
+ bool ssl_encrypt_client_hello(SSL_HANDSHAKE *hs, Span<const uint8_t> enc) {
852
+ SSL *const ssl = hs->ssl;
853
+ if (!hs->selected_ech_config) {
854
+ return setup_ech_grease(hs);
855
+ }
856
+
857
+ // Construct ClientHelloInner and EncodedClientHelloInner. See
858
+ // draft-ietf-tls-esni-10, sections 5.1 and 6.1.
859
+ bssl::ScopedCBB cbb, encoded;
860
+ CBB body;
861
+ bool needs_psk_binder;
862
+ bssl::Array<uint8_t> hello_inner;
863
+ if (!ssl->method->init_message(ssl, cbb.get(), &body, SSL3_MT_CLIENT_HELLO) ||
864
+ !CBB_init(encoded.get(), 256) ||
865
+ !ssl_write_client_hello_without_extensions(hs, &body,
866
+ ssl_client_hello_inner,
867
+ /*empty_session_id=*/false) ||
868
+ !ssl_write_client_hello_without_extensions(hs, encoded.get(),
869
+ ssl_client_hello_inner,
870
+ /*empty_session_id=*/true) ||
871
+ !ssl_add_clienthello_tlsext(hs, &body, encoded.get(), &needs_psk_binder,
872
+ ssl_client_hello_inner, CBB_len(&body),
873
+ /*omit_ech_len=*/0) ||
874
+ !ssl->method->finish_message(ssl, cbb.get(), &hello_inner)) {
875
+ OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
876
+ return false;
877
+ }
878
+
879
+ if (needs_psk_binder) {
880
+ size_t binder_len;
881
+ if (!tls13_write_psk_binder(hs, hs->inner_transcript, MakeSpan(hello_inner),
882
+ &binder_len)) {
883
+ return false;
884
+ }
885
+ // Also update the EncodedClientHelloInner.
886
+ if (CBB_len(encoded.get()) < binder_len) {
887
+ OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
888
+ return false;
889
+ }
890
+ OPENSSL_memcpy(const_cast<uint8_t *>(CBB_data(encoded.get())) +
891
+ CBB_len(encoded.get()) - binder_len,
892
+ hello_inner.data() + hello_inner.size() - binder_len,
893
+ binder_len);
894
+ }
895
+
896
+ if (!hs->inner_transcript.Update(hello_inner)) {
897
+ return false;
898
+ }
899
+
900
+ // Construct ClientHelloOuterAAD. See draft-ietf-tls-esni-10, section 5.2.
901
+ // TODO(https://crbug.com/boringssl/275): This ends up constructing the
902
+ // ClientHelloOuter twice. Revisit this in the next draft, which uses a more
903
+ // forgiving construction.
904
+ const EVP_HPKE_KDF *kdf = EVP_HPKE_CTX_kdf(hs->ech_hpke_ctx.get());
905
+ const EVP_HPKE_AEAD *aead = EVP_HPKE_CTX_aead(hs->ech_hpke_ctx.get());
906
+ const size_t extension_len =
907
+ compute_extension_length(aead, enc.size(), CBB_len(encoded.get()));
908
+ bssl::ScopedCBB aad;
909
+ CBB outer_hello;
910
+ CBB enc_cbb;
911
+ if (!CBB_init(aad.get(), 256) ||
912
+ !CBB_add_u16(aad.get(), EVP_HPKE_KDF_id(kdf)) ||
913
+ !CBB_add_u16(aad.get(), EVP_HPKE_AEAD_id(aead)) ||
914
+ !CBB_add_u8(aad.get(), hs->selected_ech_config->config_id) ||
915
+ !CBB_add_u16_length_prefixed(aad.get(), &enc_cbb) ||
916
+ !CBB_add_bytes(&enc_cbb, enc.data(), enc.size()) ||
917
+ !CBB_add_u24_length_prefixed(aad.get(), &outer_hello) ||
918
+ !ssl_write_client_hello_without_extensions(hs, &outer_hello,
919
+ ssl_client_hello_outer,
920
+ /*empty_session_id=*/false) ||
921
+ !ssl_add_clienthello_tlsext(hs, &outer_hello, /*out_encoded=*/nullptr,
922
+ &needs_psk_binder, ssl_client_hello_outer,
923
+ CBB_len(&outer_hello),
924
+ /*omit_ech_len=*/4 + extension_len) ||
925
+ !CBB_flush(aad.get())) {
926
+ OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
927
+ return false;
928
+ }
929
+ // ClientHelloOuter may not require a PSK binder. Otherwise, we have a
930
+ // circular dependency.
931
+ assert(!needs_psk_binder);
932
+
933
+ CBB payload_cbb;
934
+ if (!CBB_init(cbb.get(), extension_len) ||
935
+ !CBB_add_u16(cbb.get(), EVP_HPKE_KDF_id(kdf)) ||
936
+ !CBB_add_u16(cbb.get(), EVP_HPKE_AEAD_id(aead)) ||
937
+ !CBB_add_u8(cbb.get(), hs->selected_ech_config->config_id) ||
938
+ !CBB_add_u16_length_prefixed(cbb.get(), &enc_cbb) ||
939
+ !CBB_add_bytes(&enc_cbb, enc.data(), enc.size()) ||
940
+ !CBB_add_u16_length_prefixed(cbb.get(), &payload_cbb)) {
941
+ return false;
942
+ }
943
+ #if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
944
+ // In fuzzer mode, the server expects a cleartext payload.
945
+ if (!CBB_add_bytes(&payload_cbb, CBB_data(encoded.get()),
946
+ CBB_len(encoded.get()))) {
947
+ return false;
948
+ }
949
+ #else
950
+ uint8_t *payload;
951
+ size_t payload_len =
952
+ CBB_len(encoded.get()) + EVP_AEAD_max_overhead(EVP_HPKE_AEAD_aead(aead));
953
+ if (!CBB_reserve(&payload_cbb, &payload, payload_len) ||
954
+ !EVP_HPKE_CTX_seal(hs->ech_hpke_ctx.get(), payload, &payload_len,
955
+ payload_len, CBB_data(encoded.get()),
956
+ CBB_len(encoded.get()), CBB_data(aad.get()),
957
+ CBB_len(aad.get())) ||
958
+ !CBB_did_write(&payload_cbb, payload_len)) {
959
+ return false;
960
+ }
961
+ #endif // BORINGSSL_UNSAFE_FUZZER_MODE
962
+ if (!CBBFinishArray(cbb.get(), &hs->ech_client_bytes)) {
963
+ return false;
964
+ }
965
+
966
+ // The |aad| calculation relies on |extension_length| being correct.
967
+ assert(hs->ech_client_bytes.size() == extension_len);
968
+ return true;
442
969
  }
443
970
 
444
971
  BSSL_NAMESPACE_END
972
+
973
+ using namespace bssl;
974
+
975
+ void SSL_set_enable_ech_grease(SSL *ssl, int enable) {
976
+ if (!ssl->config) {
977
+ return;
978
+ }
979
+ ssl->config->ech_grease_enabled = !!enable;
980
+ }
981
+
982
+ int SSL_set1_ech_config_list(SSL *ssl, const uint8_t *ech_config_list,
983
+ size_t ech_config_list_len) {
984
+ if (!ssl->config) {
985
+ return 0;
986
+ }
987
+
988
+ auto span = MakeConstSpan(ech_config_list, ech_config_list_len);
989
+ if (!ssl_is_valid_ech_config_list(span)) {
990
+ OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_ECH_CONFIG_LIST);
991
+ return 0;
992
+ }
993
+ return ssl->config->client_ech_config_list.CopyFrom(span);
994
+ }
995
+
996
+ int SSL_marshal_ech_config(uint8_t **out, size_t *out_len, uint8_t config_id,
997
+ const EVP_HPKE_KEY *key, const char *public_name,
998
+ size_t max_name_len) {
999
+ Span<const uint8_t> public_name_u8 = MakeConstSpan(
1000
+ reinterpret_cast<const uint8_t *>(public_name), strlen(public_name));
1001
+ if (!ssl_is_valid_ech_public_name(public_name_u8)) {
1002
+ OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_ECH_PUBLIC_NAME);
1003
+ return 0;
1004
+ }
1005
+
1006
+ // See draft-ietf-tls-esni-10, section 4.
1007
+ ScopedCBB cbb;
1008
+ CBB contents, child;
1009
+ uint8_t *public_key;
1010
+ size_t public_key_len;
1011
+ if (!CBB_init(cbb.get(), 128) || //
1012
+ !CBB_add_u16(cbb.get(), kECHConfigVersion) ||
1013
+ !CBB_add_u16_length_prefixed(cbb.get(), &contents) ||
1014
+ !CBB_add_u8(&contents, config_id) ||
1015
+ !CBB_add_u16(&contents, EVP_HPKE_KEM_id(EVP_HPKE_KEY_kem(key))) ||
1016
+ !CBB_add_u16_length_prefixed(&contents, &child) ||
1017
+ !CBB_reserve(&child, &public_key, EVP_HPKE_MAX_PUBLIC_KEY_LENGTH) ||
1018
+ !EVP_HPKE_KEY_public_key(key, public_key, &public_key_len,
1019
+ EVP_HPKE_MAX_PUBLIC_KEY_LENGTH) ||
1020
+ !CBB_did_write(&child, public_key_len) ||
1021
+ !CBB_add_u16_length_prefixed(&contents, &child) ||
1022
+ // Write a default cipher suite configuration.
1023
+ !CBB_add_u16(&child, EVP_HPKE_HKDF_SHA256) ||
1024
+ !CBB_add_u16(&child, EVP_HPKE_AES_128_GCM) ||
1025
+ !CBB_add_u16(&child, EVP_HPKE_HKDF_SHA256) ||
1026
+ !CBB_add_u16(&child, EVP_HPKE_CHACHA20_POLY1305) ||
1027
+ !CBB_add_u16(&contents, max_name_len) ||
1028
+ !CBB_add_u16_length_prefixed(&contents, &child) ||
1029
+ !CBB_add_bytes(&child, public_name_u8.data(), public_name_u8.size()) ||
1030
+ // TODO(https://crbug.com/boringssl/275): Reserve some GREASE extensions
1031
+ // and include some.
1032
+ !CBB_add_u16(&contents, 0 /* no extensions */) ||
1033
+ !CBB_finish(cbb.get(), out, out_len)) {
1034
+ OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
1035
+ return 0;
1036
+ }
1037
+ return 1;
1038
+ }
1039
+
1040
+ SSL_ECH_KEYS *SSL_ECH_KEYS_new() { return New<SSL_ECH_KEYS>(); }
1041
+
1042
+ void SSL_ECH_KEYS_up_ref(SSL_ECH_KEYS *keys) {
1043
+ CRYPTO_refcount_inc(&keys->references);
1044
+ }
1045
+
1046
+ void SSL_ECH_KEYS_free(SSL_ECH_KEYS *keys) {
1047
+ if (keys == nullptr ||
1048
+ !CRYPTO_refcount_dec_and_test_zero(&keys->references)) {
1049
+ return;
1050
+ }
1051
+
1052
+ keys->~ssl_ech_keys_st();
1053
+ OPENSSL_free(keys);
1054
+ }
1055
+
1056
+ int SSL_ECH_KEYS_add(SSL_ECH_KEYS *configs, int is_retry_config,
1057
+ const uint8_t *ech_config, size_t ech_config_len,
1058
+ const EVP_HPKE_KEY *key) {
1059
+ UniquePtr<ECHServerConfig> parsed_config = MakeUnique<ECHServerConfig>();
1060
+ if (!parsed_config) {
1061
+ return 0;
1062
+ }
1063
+ if (!parsed_config->Init(MakeConstSpan(ech_config, ech_config_len), key,
1064
+ !!is_retry_config)) {
1065
+ OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
1066
+ return 0;
1067
+ }
1068
+ if (!configs->configs.Push(std::move(parsed_config))) {
1069
+ OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
1070
+ return 0;
1071
+ }
1072
+ return 1;
1073
+ }
1074
+
1075
+ int SSL_ECH_KEYS_has_duplicate_config_id(const SSL_ECH_KEYS *keys) {
1076
+ bool seen[256] = {false};
1077
+ for (const auto &config : keys->configs) {
1078
+ if (seen[config->ech_config().config_id]) {
1079
+ return 1;
1080
+ }
1081
+ seen[config->ech_config().config_id] = true;
1082
+ }
1083
+ return 0;
1084
+ }
1085
+
1086
+ int SSL_ECH_KEYS_marshal_retry_configs(const SSL_ECH_KEYS *keys, uint8_t **out,
1087
+ size_t *out_len) {
1088
+ ScopedCBB cbb;
1089
+ CBB child;
1090
+ if (!CBB_init(cbb.get(), 128) ||
1091
+ !CBB_add_u16_length_prefixed(cbb.get(), &child)) {
1092
+ OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
1093
+ return false;
1094
+ }
1095
+ for (const auto &config : keys->configs) {
1096
+ if (config->is_retry_config() &&
1097
+ !CBB_add_bytes(&child, config->ech_config().raw.data(),
1098
+ config->ech_config().raw.size())) {
1099
+ OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
1100
+ return false;
1101
+ }
1102
+ }
1103
+ return CBB_finish(cbb.get(), out, out_len);
1104
+ }
1105
+
1106
+ int SSL_CTX_set1_ech_keys(SSL_CTX *ctx, SSL_ECH_KEYS *keys) {
1107
+ bool has_retry_config = false;
1108
+ for (const auto &config : keys->configs) {
1109
+ if (config->is_retry_config()) {
1110
+ has_retry_config = true;
1111
+ break;
1112
+ }
1113
+ }
1114
+ if (!has_retry_config) {
1115
+ OPENSSL_PUT_ERROR(SSL, SSL_R_ECH_SERVER_WOULD_HAVE_NO_RETRY_CONFIGS);
1116
+ return 0;
1117
+ }
1118
+ UniquePtr<SSL_ECH_KEYS> owned_keys = UpRef(keys);
1119
+ MutexWriteLock lock(&ctx->lock);
1120
+ ctx->ech_keys.swap(owned_keys);
1121
+ return 1;
1122
+ }
1123
+
1124
+ int SSL_ech_accepted(const SSL *ssl) {
1125
+ if (SSL_in_early_data(ssl) && !ssl->server) {
1126
+ // In the client early data state, we report properties as if the server
1127
+ // accepted early data. The server can only accept early data with
1128
+ // ClientHelloInner.
1129
+ return ssl->s3->hs->selected_ech_config != nullptr;
1130
+ }
1131
+
1132
+ return ssl->s3->ech_accept;
1133
+ }