grpc 1.65.0 → 1.65.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (66) hide show
  1. checksums.yaml +4 -4
  2. data/Makefile +1 -1
  3. data/src/core/client_channel/subchannel.cc +10 -7
  4. data/src/core/ext/transport/chttp2/client/chttp2_connector.cc +3 -1
  5. data/src/core/ext/transport/chttp2/transport/chttp2_transport.cc +6 -6
  6. data/src/core/ext/transport/inproc/inproc_transport.cc +2 -2
  7. data/src/core/handshaker/http_connect/http_connect_handshaker.cc +2 -2
  8. data/src/core/handshaker/http_connect/http_proxy_mapper.cc +7 -10
  9. data/src/core/lib/compression/message_compress.cc +3 -3
  10. data/src/core/lib/event_engine/posix_engine/ev_epoll1_linux.cc +2 -1
  11. data/src/core/lib/event_engine/posix_engine/posix_endpoint.cc +3 -3
  12. data/src/core/lib/event_engine/posix_engine/posix_endpoint.h +1 -1
  13. data/src/core/lib/event_engine/posix_engine/posix_engine_listener_utils.cc +6 -6
  14. data/src/core/lib/event_engine/posix_engine/tcp_socket_utils.cc +12 -6
  15. data/src/core/lib/event_engine/thread_pool/work_stealing_thread_pool.cc +3 -2
  16. data/src/core/lib/event_engine/windows/win_socket.cc +4 -2
  17. data/src/core/lib/event_engine/windows/windows_endpoint.cc +5 -4
  18. data/src/core/lib/experiments/config.cc +11 -9
  19. data/src/core/lib/experiments/experiments.cc +1 -1
  20. data/src/core/lib/experiments/experiments.h +1 -2
  21. data/src/core/lib/gprpp/dual_ref_counted.h +30 -30
  22. data/src/core/lib/iomgr/endpoint_pair_windows.cc +4 -4
  23. data/src/core/lib/iomgr/ev_epoll1_linux.cc +1 -1
  24. data/src/core/lib/iomgr/socket_utils_common_posix.cc +16 -17
  25. data/src/core/lib/iomgr/socket_windows.cc +3 -3
  26. data/src/core/lib/iomgr/tcp_posix.cc +2 -2
  27. data/src/core/lib/iomgr/tcp_server_posix.cc +9 -12
  28. data/src/core/lib/iomgr/tcp_server_windows.cc +2 -2
  29. data/src/core/lib/promise/party.cc +4 -4
  30. data/src/core/lib/security/security_connector/tls/tls_security_connector.cc +2 -2
  31. data/src/core/load_balancing/grpclb/grpclb.cc +14 -15
  32. data/src/core/server/server.cc +1 -1
  33. data/src/core/util/log.cc +10 -8
  34. data/src/ruby/lib/grpc/version.rb +1 -1
  35. data/third_party/boringssl-with-bazel/src/crypto/bytestring/cbs.c +3 -5
  36. data/third_party/boringssl-with-bazel/src/crypto/cipher_extra/e_chacha20poly1305.c +7 -0
  37. data/third_party/boringssl-with-bazel/src/crypto/cpu_arm_linux.c +4 -1
  38. data/third_party/boringssl-with-bazel/src/crypto/cpu_intel.c +0 -15
  39. data/third_party/boringssl-with-bazel/src/crypto/crypto.c +7 -61
  40. data/third_party/boringssl-with-bazel/src/crypto/dilithium/dilithium.c +43 -1
  41. data/third_party/boringssl-with-bazel/src/crypto/fipsmodule/aes/aes.c +21 -0
  42. data/third_party/boringssl-with-bazel/src/crypto/fipsmodule/aes/internal.h +31 -7
  43. data/third_party/boringssl-with-bazel/src/crypto/fipsmodule/bcm.c +0 -2
  44. data/third_party/boringssl-with-bazel/src/crypto/fipsmodule/ec/p256-nistz.c +5 -0
  45. data/third_party/boringssl-with-bazel/src/crypto/fipsmodule/self_check/fips.c +6 -4
  46. data/third_party/boringssl-with-bazel/src/crypto/internal.h +22 -10
  47. data/third_party/boringssl-with-bazel/src/crypto/kyber/kyber.c +1 -0
  48. data/third_party/boringssl-with-bazel/src/include/openssl/crypto.h +6 -11
  49. data/third_party/boringssl-with-bazel/src/include/openssl/experimental/dilithium.h +6 -0
  50. data/third_party/boringssl-with-bazel/src/include/openssl/experimental/kyber.h +10 -0
  51. data/third_party/boringssl-with-bazel/src/include/openssl/span.h +26 -12
  52. data/third_party/boringssl-with-bazel/src/include/openssl/ssl.h +83 -33
  53. data/third_party/boringssl-with-bazel/src/ssl/d1_both.cc +6 -8
  54. data/third_party/boringssl-with-bazel/src/ssl/d1_pkt.cc +4 -4
  55. data/third_party/boringssl-with-bazel/src/ssl/dtls_record.cc +14 -13
  56. data/third_party/boringssl-with-bazel/src/ssl/extensions.cc +22 -16
  57. data/third_party/boringssl-with-bazel/src/ssl/handshake_client.cc +2 -1
  58. data/third_party/boringssl-with-bazel/src/ssl/handshake_server.cc +2 -1
  59. data/third_party/boringssl-with-bazel/src/ssl/internal.h +15 -15
  60. data/third_party/boringssl-with-bazel/src/ssl/ssl_key_share.cc +1 -0
  61. data/third_party/boringssl-with-bazel/src/ssl/ssl_lib.cc +38 -27
  62. data/third_party/boringssl-with-bazel/src/ssl/ssl_privkey.cc +59 -20
  63. data/third_party/boringssl-with-bazel/src/ssl/ssl_versions.cc +1 -1
  64. data/third_party/boringssl-with-bazel/src/ssl/tls13_both.cc +2 -1
  65. data/third_party/boringssl-with-bazel/src/ssl/tls_record.cc +1 -1
  66. metadata +3 -3
@@ -612,6 +612,11 @@ static int ecp_nistz256_cmp_x_coordinate(const EC_GROUP *group,
612
612
  }
613
613
 
614
614
  DEFINE_METHOD_FUNCTION(EC_METHOD, EC_GFp_nistz256_method) {
615
+ // TODO(crbug.com/42290548): The x86_64 assembly depends on initializing
616
+ // |OPENSSL_ia32cap_P|. Move the dispatch to C. For now, explicitly initialize
617
+ // things.
618
+ OPENSSL_init_cpuid();
619
+
615
620
  out->point_get_affine_coordinates = ecp_nistz256_get_affine;
616
621
  out->add = ecp_nistz256_add;
617
622
  out->dbl = ecp_nistz256_dbl;
@@ -72,7 +72,8 @@ int FIPS_query_algorithm_status(const char *algorithm) {
72
72
  #if defined(BORINGSSL_FIPS_COUNTERS)
73
73
 
74
74
  size_t FIPS_read_counter(enum fips_counter_t counter) {
75
- if (counter < 0 || counter > fips_counter_max) {
75
+ size_t index = (size_t)counter;
76
+ if (index > fips_counter_max) {
76
77
  abort();
77
78
  }
78
79
 
@@ -82,11 +83,12 @@ size_t FIPS_read_counter(enum fips_counter_t counter) {
82
83
  return 0;
83
84
  }
84
85
 
85
- return array[counter];
86
+ return array[index];
86
87
  }
87
88
 
88
89
  void boringssl_fips_inc_counter(enum fips_counter_t counter) {
89
- if (counter < 0 || counter > fips_counter_max) {
90
+ size_t index = (size_t)counter;
91
+ if (index > fips_counter_max) {
90
92
  abort();
91
93
  }
92
94
 
@@ -106,7 +108,7 @@ void boringssl_fips_inc_counter(enum fips_counter_t counter) {
106
108
  }
107
109
  }
108
110
 
109
- array[counter]++;
111
+ array[index]++;
110
112
  }
111
113
 
112
114
  #else
@@ -180,17 +180,29 @@ extern "C" {
180
180
  #endif
181
181
 
182
182
 
183
- #if defined(OPENSSL_X86) || defined(OPENSSL_X86_64) || defined(OPENSSL_ARM) || \
184
- defined(OPENSSL_AARCH64)
185
- // OPENSSL_cpuid_setup initializes the platform-specific feature cache.
183
+ #if !defined(OPENSSL_NO_ASM) && !defined(OPENSSL_STATIC_ARMCAP) && \
184
+ (defined(OPENSSL_X86) || defined(OPENSSL_X86_64) || \
185
+ defined(OPENSSL_ARM) || defined(OPENSSL_AARCH64))
186
+ // x86, x86_64, and the ARMs need to record the result of a cpuid/getauxval call
187
+ // for the asm to work correctly, unless compiled without asm code.
188
+ #define NEED_CPUID
189
+
190
+ // OPENSSL_cpuid_setup initializes the platform-specific feature cache. This
191
+ // function should not be called directly. Call |OPENSSL_init_cpuid| instead.
186
192
  void OPENSSL_cpuid_setup(void);
193
+
194
+ // OPENSSL_init_cpuid initializes the platform-specific feature cache, if
195
+ // needed. This function is idempotent and may be called concurrently.
196
+ void OPENSSL_init_cpuid(void);
197
+ #else
198
+ OPENSSL_INLINE void OPENSSL_init_cpuid(void) {}
187
199
  #endif
188
200
 
189
201
  #if (defined(OPENSSL_ARM) || defined(OPENSSL_AARCH64)) && \
190
202
  !defined(OPENSSL_STATIC_ARMCAP)
191
203
  // OPENSSL_get_armcap_pointer_for_test returns a pointer to |OPENSSL_armcap_P|
192
- // for unit tests. Any modifications to the value must be made after
193
- // |CRYPTO_library_init| but before any other function call in BoringSSL.
204
+ // for unit tests. Any modifications to the value must be made before any other
205
+ // function call in BoringSSL.
194
206
  OPENSSL_EXPORT uint32_t *OPENSSL_get_armcap_pointer_for_test(void);
195
207
  #endif
196
208
 
@@ -1521,7 +1533,6 @@ OPENSSL_INLINE int CRYPTO_is_x86_SHA_capable(void) {
1521
1533
  // otherwise select. See chacha-x86_64.pl.
1522
1534
  //
1523
1535
  // Bonnell, Silvermont's predecessor in the Atom lineup, will also be matched by
1524
- // this. |OPENSSL_cpuid_setup| forces Knights Landing to also be matched by
1525
1536
  // this. Goldmont (Silvermont's successor in the Atom lineup) added XSAVE so it
1526
1537
  // isn't matched by this. Various sources indicate AMD first implemented MOVBE
1527
1538
  // and XSAVE at the same time in Jaguar, so it seems like AMD chips will not be
@@ -1530,11 +1541,12 @@ OPENSSL_INLINE int CRYPTO_cpu_perf_is_like_silvermont(void) {
1530
1541
  // WARNING: This MUST NOT be used to guard the execution of the XSAVE
1531
1542
  // instruction. This is the "hardware supports XSAVE" bit, not the OSXSAVE bit
1532
1543
  // that indicates whether we can safely execute XSAVE. This bit may be set
1533
- // even when XSAVE is disabled (by the operating system). See the comment in
1534
- // cpu_intel.c and check how the users of this bit use it.
1544
+ // even when XSAVE is disabled (by the operating system). See how the users of
1545
+ // this bit use it.
1535
1546
  //
1536
- // We do not use |__XSAVE__| for static detection because the hack in
1537
- // |OPENSSL_cpuid_setup| for Knights Landing CPUs needs to override it.
1547
+ // Historically, the XSAVE bit was artificially cleared on Knights Landing
1548
+ // and Knights Mill chips, but as Intel has removed all support from GCC,
1549
+ // LLVM, and SDE, we assume they are no longer worth special-casing.
1538
1550
  int hardware_supports_xsave = (OPENSSL_get_ia32cap(1) & (1u << 26)) != 0;
1539
1551
  return !hardware_supports_xsave && CRYPTO_is_MOVBE_capable();
1540
1552
  }
@@ -12,6 +12,7 @@
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
+ #define OPENSSL_UNSTABLE_EXPERIMENTAL_KYBER
15
16
  #include <openssl/experimental/kyber.h>
16
17
 
17
18
  #include <assert.h>
@@ -32,18 +32,9 @@ extern "C" {
32
32
  #endif
33
33
 
34
34
 
35
- // crypto.h contains functions for initializing the crypto library.
35
+ // crypto.h contains functions for library-wide initialization and properties.
36
36
 
37
37
 
38
- // CRYPTO_library_init initializes the crypto library. It must be called if the
39
- // library is built with BORINGSSL_NO_STATIC_INITIALIZER. Otherwise, it does
40
- // nothing and a static initializer is used instead. It is safe to call this
41
- // function multiple times and concurrently from multiple threads.
42
- //
43
- // On some ARM configurations, this function may require filesystem access and
44
- // should be called before entering a sandbox.
45
- OPENSSL_EXPORT void CRYPTO_library_init(void);
46
-
47
38
  // CRYPTO_is_confidential_build returns one if the linked version of BoringSSL
48
39
  // has been built with the BORINGSSL_CONFIDENTIAL define and zero otherwise.
49
40
  //
@@ -164,7 +155,7 @@ OPENSSL_EXPORT void OPENSSL_load_builtin_modules(void);
164
155
  #define OPENSSL_INIT_NO_LOAD_CONFIG 0
165
156
  #define OPENSSL_INIT_NO_ATEXIT 0
166
157
 
167
- // OPENSSL_init_crypto calls |CRYPTO_library_init| and returns one.
158
+ // OPENSSL_init_crypto returns one.
168
159
  OPENSSL_EXPORT int OPENSSL_init_crypto(uint64_t opts,
169
160
  const OPENSSL_INIT_SETTINGS *settings);
170
161
 
@@ -199,6 +190,10 @@ OPENSSL_EXPORT int FIPS_query_algorithm_status(const char *algorithm);
199
190
  OPENSSL_EXPORT int CRYPTO_has_broken_NEON(void);
200
191
  #endif
201
192
 
193
+ // CRYPTO_library_init does nothing. Historically, it was needed in some build
194
+ // configurations to initialization the library. This is no longer necessary.
195
+ OPENSSL_EXPORT void CRYPTO_library_init(void);
196
+
202
197
 
203
198
  #if defined(__cplusplus)
204
199
  } // extern C
@@ -70,6 +70,12 @@ OPENSSL_EXPORT int DILITHIUM_generate_key(
70
70
  uint8_t out_encoded_public_key[DILITHIUM_PUBLIC_KEY_BYTES],
71
71
  struct DILITHIUM_private_key *out_private_key);
72
72
 
73
+ // DILITHIUM_public_from_private sets |*out_public_key| to the public key that
74
+ // corresponds to |private_key|. Returns 1 on success and 0 on failure.
75
+ OPENSSL_EXPORT int DILITHIUM_public_from_private(
76
+ struct DILITHIUM_public_key *out_public_key,
77
+ const struct DILITHIUM_private_key *private_key);
78
+
73
79
  // DILITHIUM_sign generates a signature for the message |msg| of length
74
80
  // |msg_len| using |private_key| following the randomized algorithm, and writes
75
81
  // the encoded signature to |out_encoded_signature|. Returns 1 on success and 0
@@ -22,6 +22,14 @@ extern "C" {
22
22
  #endif
23
23
 
24
24
 
25
+ #if defined(OPENSSL_UNSTABLE_EXPERIMENTAL_KYBER)
26
+ // This header implements experimental, draft versions of not-yet-standardized
27
+ // primitives. When the standard is complete, these functions will be removed
28
+ // and replaced with the final, incompatible standard version. They are
29
+ // available now for short-lived experiments, but must not be deployed anywhere
30
+ // durable, such as a long-lived key store. To use these functions define
31
+ // OPENSSL_UNSTABLE_EXPERIMENTAL_KYBER
32
+
25
33
  // Kyber768.
26
34
  //
27
35
  // This implements the round-3 specification of Kyber, defined at
@@ -128,6 +136,8 @@ OPENSSL_EXPORT int KYBER_marshal_private_key(
128
136
  OPENSSL_EXPORT int KYBER_parse_private_key(
129
137
  struct KYBER_private_key *out_private_key, CBS *in);
130
138
 
139
+ #endif // OPENSSL_UNSTABLE_EXPERIMENTAL_KYBER
140
+
131
141
 
132
142
  #if defined(__cplusplus)
133
143
  } // extern C
@@ -49,6 +49,16 @@ class SpanBase {
49
49
 
50
50
  friend bool operator!=(Span<T> lhs, Span<T> rhs) { return !(lhs == rhs); }
51
51
  };
52
+
53
+ // Heuristically test whether C is a container type that can be converted into
54
+ // a Span<T> by checking for data() and size() member functions.
55
+ //
56
+ // TODO(davidben): Require C++17 support for std::is_convertible_v, etc.
57
+ template <typename C, typename T>
58
+ using EnableIfContainer = std::enable_if_t<
59
+ std::is_convertible<decltype(std::declval<C>().data()), T *>::value &&
60
+ std::is_integral<decltype(std::declval<C>().size())>::value>;
61
+
52
62
  } // namespace internal
53
63
 
54
64
  // A Span<T> is a non-owning reference to a contiguous array of objects of type
@@ -84,16 +94,6 @@ class SpanBase {
84
94
  // a reference or pointer to a container or array.
85
95
  template <typename T>
86
96
  class Span : private internal::SpanBase<const T> {
87
- private:
88
- // Heuristically test whether C is a container type that can be converted into
89
- // a Span by checking for data() and size() member functions.
90
- //
91
- // TODO(davidben): Require C++17 support for std::is_convertible_v, etc.
92
- template <typename C>
93
- using EnableIfContainer = std::enable_if_t<
94
- std::is_convertible<decltype(std::declval<C>().data()), T *>::value &&
95
- std::is_integral<decltype(std::declval<C>().size())>::value>;
96
-
97
97
  public:
98
98
  static const size_t npos = static_cast<size_t>(-1);
99
99
 
@@ -114,12 +114,12 @@ class Span : private internal::SpanBase<const T> {
114
114
  template <size_t N>
115
115
  constexpr Span(T (&array)[N]) : Span(array, N) {}
116
116
 
117
- template <typename C, typename = EnableIfContainer<C>,
117
+ template <typename C, typename = internal::EnableIfContainer<C, T>,
118
118
  typename = std::enable_if_t<std::is_const<T>::value, C>>
119
119
  constexpr Span(const C &container)
120
120
  : data_(container.data()), size_(container.size()) {}
121
121
 
122
- template <typename C, typename = EnableIfContainer<C>,
122
+ template <typename C, typename = internal::EnableIfContainer<C, T>,
123
123
  typename = std::enable_if_t<!std::is_const<T>::value, C>>
124
124
  constexpr explicit Span(C &container)
125
125
  : data_(container.data()), size_(container.size()) {}
@@ -188,6 +188,20 @@ class Span : private internal::SpanBase<const T> {
188
188
  template <typename T>
189
189
  const size_t Span<T>::npos;
190
190
 
191
+ #if __cplusplus >= 201703L
192
+ template <typename T>
193
+ Span(T *, size_t) -> Span<T>;
194
+ template <typename T, size_t size>
195
+ Span(T (&array)[size]) -> Span<T>;
196
+ template <
197
+ typename C,
198
+ typename T = std::remove_pointer_t<decltype(std::declval<C>().data())>,
199
+ typename = internal::EnableIfContainer<C, T>>
200
+ Span(C &) -> Span<T>;
201
+ #endif
202
+
203
+ // C++17 callers can instead rely on CTAD and the deduction guides defined
204
+ // above.
191
205
  template <typename T>
192
206
  constexpr Span<T> MakeSpan(T *ptr, size_t size) {
193
207
  return Span<T>(ptr, size);
@@ -1205,6 +1205,11 @@ OPENSSL_EXPORT int SSL_set_ocsp_response(SSL *ssl,
1205
1205
  #define SSL_SIGN_RSA_PSS_RSAE_SHA512 0x0806
1206
1206
  #define SSL_SIGN_ED25519 0x0807
1207
1207
 
1208
+ // SSL_SIGN_RSA_PKCS1_SHA256_LEGACY is a backport of RSASSA-PKCS1-v1_5 with
1209
+ // SHA-256 to TLS 1.3. It is disabled by default and only defined for client
1210
+ // certificates.
1211
+ #define SSL_SIGN_RSA_PKCS1_SHA256_LEGACY 0x0420
1212
+
1208
1213
  // SSL_SIGN_RSA_PKCS1_MD5_SHA1 is an internal signature algorithm used to
1209
1214
  // specify raw RSASSA-PKCS1-v1_5 with an MD5/SHA-1 concatenation, as used in TLS
1210
1215
  // before TLS 1.2.
@@ -3115,7 +3120,8 @@ OPENSSL_EXPORT int SSL_set_alpn_protos(SSL *ssl, const uint8_t *protos,
3115
3120
 
3116
3121
  // SSL_CTX_set_alpn_select_cb sets a callback function on |ctx| that is called
3117
3122
  // during ClientHello processing in order to select an ALPN protocol from the
3118
- // client's list of offered protocols.
3123
+ // client's list of offered protocols. |SSL_select_next_proto| is an optional
3124
+ // utility function which may be useful in implementing this callback.
3119
3125
  //
3120
3126
  // The callback is passed a wire-format (i.e. a series of non-empty, 8-bit
3121
3127
  // length-prefixed strings) ALPN protocol list in |in|. To select a protocol,
@@ -3265,30 +3271,50 @@ OPENSSL_EXPORT int SSL_CTX_add_cert_compression_alg(
3265
3271
  // and deprecated in favor of it.
3266
3272
 
3267
3273
  // SSL_CTX_set_next_protos_advertised_cb sets a callback that is called when a
3268
- // TLS server needs a list of supported protocols for Next Protocol
3269
- // Negotiation. The returned list must be in wire format. The list is returned
3270
- // by setting |*out| to point to it and |*out_len| to its length. This memory
3271
- // will not be modified, but one should assume that |ssl| keeps a reference to
3272
- // it.
3273
- //
3274
- // The callback should return |SSL_TLSEXT_ERR_OK| if it wishes to advertise.
3275
- // Otherwise, no such extension will be included in the ServerHello.
3274
+ // TLS server needs a list of supported protocols for Next Protocol Negotiation.
3275
+ //
3276
+ // If the callback wishes to advertise NPN to the client, it should return
3277
+ // |SSL_TLSEXT_ERR_OK| and then set |*out| and |*out_len| to describe to a
3278
+ // buffer containing a (possibly empty) list of supported protocols in wire
3279
+ // format. That is, each protocol is prefixed with a 1-byte length, then
3280
+ // concatenated. From there, the client will select a protocol, possibly one not
3281
+ // on the server's list. The caller can use |SSL_get0_next_proto_negotiated|
3282
+ // after the handshake completes to query the final protocol.
3283
+ //
3284
+ // The returned buffer must remain valid and unmodified for at least the
3285
+ // duration of the |SSL| operation (e.g. |SSL_do_handshake|) that triggered the
3286
+ // callback.
3287
+ //
3288
+ // If the caller wishes not to advertise NPN, it should return
3289
+ // |SSL_TLSEXT_ERR_NOACK|. No NPN extension will be included in the ServerHello,
3290
+ // and the TLS server will behave as if it does not implement NPN.
3276
3291
  OPENSSL_EXPORT void SSL_CTX_set_next_protos_advertised_cb(
3277
3292
  SSL_CTX *ctx,
3278
3293
  int (*cb)(SSL *ssl, const uint8_t **out, unsigned *out_len, void *arg),
3279
3294
  void *arg);
3280
3295
 
3281
3296
  // SSL_CTX_set_next_proto_select_cb sets a callback that is called when a client
3282
- // needs to select a protocol from the server's provided list. |*out| must be
3283
- // set to point to the selected protocol (which may be within |in|). The length
3284
- // of the protocol name must be written into |*out_len|. The server's advertised
3285
- // protocols are provided in |in| and |in_len|. The callback can assume that
3286
- // |in| is syntactically valid.
3287
- //
3288
- // The client must select a protocol. It is fatal to the connection if this
3289
- // callback returns a value other than |SSL_TLSEXT_ERR_OK|.
3290
- //
3291
- // Configuring this callback enables NPN on a client.
3297
+ // needs to select a protocol from the server's provided list, passed in wire
3298
+ // format in |in_len| bytes from |in|. The callback can assume that |in| is
3299
+ // syntactically valid. |SSL_select_next_proto| is an optional utility function
3300
+ // which may be useful in implementing this callback.
3301
+ //
3302
+ // On success, the callback should return |SSL_TLSEXT_ERR_OK| and set |*out| and
3303
+ // |*out_len| to describe a buffer containing the selected protocol, or an
3304
+ // empty buffer to select no protocol. The returned buffer may point within
3305
+ // |in|, or it may point to some other buffer that remains valid and unmodified
3306
+ // for at least the duration of the |SSL| operation (e.g. |SSL_do_handshake|)
3307
+ // that triggered the callback.
3308
+ //
3309
+ // Returning any other value indicates a fatal error and will terminate the TLS
3310
+ // connection. To proceed without selecting a protocol, the callback must return
3311
+ // |SSL_TLSEXT_ERR_OK| and set |*out| and |*out_len| to an empty buffer. (E.g.
3312
+ // NULL and zero, respectively.)
3313
+ //
3314
+ // Configuring this callback enables NPN on a client. Although the callback can
3315
+ // then decline to negotiate a protocol, merely configuring the callback causes
3316
+ // the client to offer NPN in the ClientHello. Callers thus should not configure
3317
+ // this callback in TLS client contexts that are not intended to use NPN.
3292
3318
  OPENSSL_EXPORT void SSL_CTX_set_next_proto_select_cb(
3293
3319
  SSL_CTX *ctx, int (*cb)(SSL *ssl, uint8_t **out, uint8_t *out_len,
3294
3320
  const uint8_t *in, unsigned in_len, void *arg),
@@ -3296,7 +3322,7 @@ OPENSSL_EXPORT void SSL_CTX_set_next_proto_select_cb(
3296
3322
 
3297
3323
  // SSL_get0_next_proto_negotiated sets |*out_data| and |*out_len| to point to
3298
3324
  // the client's requested protocol for this connection. If the client didn't
3299
- // request any protocol, then |*out_data| is set to NULL.
3325
+ // request any protocol, then |*out_len| is set to zero.
3300
3326
  //
3301
3327
  // Note that the client can request any protocol it chooses. The value returned
3302
3328
  // from this function need not be a member of the list of supported protocols
@@ -3305,21 +3331,45 @@ OPENSSL_EXPORT void SSL_get0_next_proto_negotiated(const SSL *ssl,
3305
3331
  const uint8_t **out_data,
3306
3332
  unsigned *out_len);
3307
3333
 
3308
- // SSL_select_next_proto implements the standard protocol selection. It is
3309
- // expected that this function is called from the callback set by
3334
+ // SSL_select_next_proto implements the standard protocol selection for either
3335
+ // ALPN servers or NPN clients. It is expected that this function is called from
3336
+ // the callback set by |SSL_CTX_set_alpn_select_cb| or
3310
3337
  // |SSL_CTX_set_next_proto_select_cb|.
3311
3338
  //
3312
- // |peer| and |supported| must be vectors of 8-bit, length-prefixed byte strings
3313
- // containing the peer and locally-configured protocols, respectively. The
3314
- // length byte itself is not included in the length. A byte string of length 0
3315
- // is invalid. No byte string may be truncated. |supported| is assumed to be
3316
- // non-empty.
3317
- //
3318
- // This function finds the first protocol in |peer| which is also in
3319
- // |supported|. If one was found, it sets |*out| and |*out_len| to point to it
3320
- // and returns |OPENSSL_NPN_NEGOTIATED|. Otherwise, it returns
3339
+ // |peer| and |supported| contain the peer and locally-configured protocols,
3340
+ // respectively. This function finds the first protocol in |peer| which is also
3341
+ // in |supported|. If one was found, it sets |*out| and |*out_len| to point to
3342
+ // it and returns |OPENSSL_NPN_NEGOTIATED|. Otherwise, it returns
3321
3343
  // |OPENSSL_NPN_NO_OVERLAP| and sets |*out| and |*out_len| to the first
3322
3344
  // supported protocol.
3345
+ //
3346
+ // In ALPN, the server should only select protocols among those that the client
3347
+ // offered. Thus, if this function returns |OPENSSL_NPN_NO_OVERLAP|, the caller
3348
+ // should ignore |*out| and return |SSL_TLSEXT_ERR_ALERT_FATAL| from
3349
+ // |SSL_CTX_set_alpn_select_cb|'s callback to indicate there was no match.
3350
+ //
3351
+ // In NPN, the client may either select one of the server's protocols, or an
3352
+ // "opportunistic" protocol as described in Section 6 of
3353
+ // draft-agl-tls-nextprotoneg-03. When this function returns
3354
+ // |OPENSSL_NPN_NO_OVERLAP|, |*out| implicitly selects the first supported
3355
+ // protocol for use as the opportunistic protocol. The caller may use it,
3356
+ // ignore it and select a different opportunistic protocol, or ignore it and
3357
+ // select no protocol (empty string).
3358
+ //
3359
+ // |peer| and |supported| must be vectors of 8-bit, length-prefixed byte
3360
+ // strings. The length byte itself is not included in the length. A byte string
3361
+ // of length 0 is invalid. No byte string may be truncated. |supported| must be
3362
+ // non-empty; a caller that supports no ALPN/NPN protocols should skip
3363
+ // negotiating the extension, rather than calling this function. If any of these
3364
+ // preconditions do not hold, this function will return |OPENSSL_NPN_NO_OVERLAP|
3365
+ // and set |*out| and |*out_len| to an empty buffer for robustness, but callers
3366
+ // are not recommended to rely on this. An empty buffer is not a valid output
3367
+ // for |SSL_CTX_set_alpn_select_cb|'s callback.
3368
+ //
3369
+ // WARNING: |*out| and |*out_len| may alias either |peer| or |supported| and may
3370
+ // not be used after one of those buffers is modified or released. Additionally,
3371
+ // this function is not const-correct for compatibility reasons. Although |*out|
3372
+ // is a non-const pointer, callers may not modify the buffer though |*out|.
3323
3373
  OPENSSL_EXPORT int SSL_select_next_proto(uint8_t **out, uint8_t *out_len,
3324
3374
  const uint8_t *peer, unsigned peer_len,
3325
3375
  const uint8_t *supported,
@@ -4814,7 +4864,7 @@ OPENSSL_EXPORT void SSL_set_check_ecdsa_curve(SSL *ssl, int enable);
4814
4864
 
4815
4865
  // Deprecated functions.
4816
4866
 
4817
- // SSL_library_init calls |CRYPTO_library_init| and returns one.
4867
+ // SSL_library_init returns one.
4818
4868
  OPENSSL_EXPORT int SSL_library_init(void);
4819
4869
 
4820
4870
  // SSL_CIPHER_description writes a description of |cipher| into |buf| and
@@ -5377,7 +5427,7 @@ OPENSSL_EXPORT SSL_SESSION *SSL_get1_session(SSL *ssl);
5377
5427
  #define OPENSSL_INIT_LOAD_SSL_STRINGS 0
5378
5428
  #define OPENSSL_INIT_SSL_DEFAULT 0
5379
5429
 
5380
- // OPENSSL_init_ssl calls |CRYPTO_library_init| and returns one.
5430
+ // OPENSSL_init_ssl returns one.
5381
5431
  OPENSSL_EXPORT int OPENSSL_init_ssl(uint64_t opts,
5382
5432
  const OPENSSL_INIT_SETTINGS *settings);
5383
5433
 
@@ -624,16 +624,14 @@ static enum seal_result_t seal_next_message(SSL *ssl, uint8_t *out,
624
624
  assert(ssl->d1->outgoing_written < ssl->d1->outgoing_messages_len);
625
625
  assert(msg == &ssl->d1->outgoing_messages[ssl->d1->outgoing_written]);
626
626
 
627
- enum dtls1_use_epoch_t use_epoch = dtls1_use_current_epoch;
628
- if (ssl->d1->w_epoch >= 1 && msg->epoch == ssl->d1->w_epoch - 1) {
629
- use_epoch = dtls1_use_previous_epoch;
630
- } else if (msg->epoch != ssl->d1->w_epoch) {
627
+ if (msg->epoch != ssl->d1->w_epoch &&
628
+ (ssl->d1->w_epoch == 0 || msg->epoch != ssl->d1->w_epoch - 1)) {
631
629
  OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
632
630
  return seal_error;
633
631
  }
634
632
 
635
- size_t overhead = dtls_max_seal_overhead(ssl, use_epoch);
636
- size_t prefix = dtls_seal_prefix_len(ssl, use_epoch);
633
+ size_t overhead = dtls_max_seal_overhead(ssl, msg->epoch);
634
+ size_t prefix = dtls_seal_prefix_len(ssl, msg->epoch);
637
635
 
638
636
  if (msg->is_ccs) {
639
637
  // Check there is room for the ChangeCipherSpec.
@@ -644,7 +642,7 @@ static enum seal_result_t seal_next_message(SSL *ssl, uint8_t *out,
644
642
 
645
643
  if (!dtls_seal_record(ssl, out, out_len, max_out,
646
644
  SSL3_RT_CHANGE_CIPHER_SPEC, kChangeCipherSpec,
647
- sizeof(kChangeCipherSpec), use_epoch)) {
645
+ sizeof(kChangeCipherSpec), msg->epoch)) {
648
646
  return seal_error;
649
647
  }
650
648
 
@@ -697,7 +695,7 @@ static enum seal_result_t seal_next_message(SSL *ssl, uint8_t *out,
697
695
  MakeSpan(frag, frag_len));
698
696
 
699
697
  if (!dtls_seal_record(ssl, out, out_len, max_out, SSL3_RT_HANDSHAKE,
700
- out + prefix, frag_len, use_epoch)) {
698
+ out + prefix, frag_len, msg->epoch)) {
701
699
  return seal_error;
702
700
  }
703
701
 
@@ -208,7 +208,7 @@ int dtls1_write_app_data(SSL *ssl, bool *out_needs_handshake,
208
208
  }
209
209
 
210
210
  int ret = dtls1_write_record(ssl, SSL3_RT_APPLICATION_DATA, in,
211
- dtls1_use_current_epoch);
211
+ ssl->d1->w_epoch);
212
212
  if (ret <= 0) {
213
213
  return ret;
214
214
  }
@@ -217,7 +217,7 @@ int dtls1_write_app_data(SSL *ssl, bool *out_needs_handshake,
217
217
  }
218
218
 
219
219
  int dtls1_write_record(SSL *ssl, int type, Span<const uint8_t> in,
220
- enum dtls1_use_epoch_t use_epoch) {
220
+ uint16_t epoch) {
221
221
  SSLBuffer *buf = &ssl->s3->write_buffer;
222
222
  assert(in.size() <= SSL3_RT_MAX_PLAIN_LENGTH);
223
223
  // There should never be a pending write buffer in DTLS. One can't write half
@@ -235,7 +235,7 @@ int dtls1_write_record(SSL *ssl, int type, Span<const uint8_t> in,
235
235
  in.size() + SSL_max_seal_overhead(ssl)) ||
236
236
  !dtls_seal_record(ssl, buf->remaining().data(), &ciphertext_len,
237
237
  buf->remaining().size(), type, in.data(), in.size(),
238
- use_epoch)) {
238
+ epoch)) {
239
239
  buf->Clear();
240
240
  return -1;
241
241
  }
@@ -250,7 +250,7 @@ int dtls1_write_record(SSL *ssl, int type, Span<const uint8_t> in,
250
250
 
251
251
  int dtls1_dispatch_alert(SSL *ssl) {
252
252
  int ret = dtls1_write_record(ssl, SSL3_RT_ALERT, ssl->s3->send_alert,
253
- dtls1_use_current_epoch);
253
+ ssl->d1->w_epoch);
254
254
  if (ret <= 0) {
255
255
  return ret;
256
256
  }
@@ -258,29 +258,30 @@ enum ssl_open_record_t dtls_open_record(SSL *ssl, uint8_t *out_type,
258
258
  }
259
259
 
260
260
  static const SSLAEADContext *get_write_aead(const SSL *ssl,
261
- enum dtls1_use_epoch_t use_epoch) {
262
- if (use_epoch == dtls1_use_previous_epoch) {
263
- assert(ssl->d1->w_epoch >= 1);
261
+ uint16_t epoch) {
262
+ if (epoch < ssl->d1->w_epoch) {
263
+ assert(epoch + 1 == ssl->d1->w_epoch);
264
264
  return ssl->d1->last_aead_write_ctx.get();
265
265
  }
266
266
 
267
+ assert(epoch == ssl->d1->w_epoch);
267
268
  return ssl->s3->aead_write_ctx.get();
268
269
  }
269
270
 
270
271
  size_t dtls_max_seal_overhead(const SSL *ssl,
271
- enum dtls1_use_epoch_t use_epoch) {
272
- return DTLS1_RT_HEADER_LENGTH + get_write_aead(ssl, use_epoch)->MaxOverhead();
272
+ uint16_t epoch) {
273
+ return DTLS1_RT_HEADER_LENGTH + get_write_aead(ssl, epoch)->MaxOverhead();
273
274
  }
274
275
 
275
- size_t dtls_seal_prefix_len(const SSL *ssl, enum dtls1_use_epoch_t use_epoch) {
276
+ size_t dtls_seal_prefix_len(const SSL *ssl, uint16_t epoch) {
276
277
  return DTLS1_RT_HEADER_LENGTH +
277
- get_write_aead(ssl, use_epoch)->ExplicitNonceLen();
278
+ get_write_aead(ssl, epoch)->ExplicitNonceLen();
278
279
  }
279
280
 
280
281
  bool dtls_seal_record(SSL *ssl, uint8_t *out, size_t *out_len, size_t max_out,
281
282
  uint8_t type, const uint8_t *in, size_t in_len,
282
- enum dtls1_use_epoch_t use_epoch) {
283
- const size_t prefix = dtls_seal_prefix_len(ssl, use_epoch);
283
+ uint16_t epoch) {
284
+ const size_t prefix = dtls_seal_prefix_len(ssl, epoch);
284
285
  if (buffers_alias(in, in_len, out, max_out) &&
285
286
  (max_out < prefix || out + prefix != in)) {
286
287
  OPENSSL_PUT_ERROR(SSL, SSL_R_OUTPUT_ALIASES_INPUT);
@@ -288,14 +289,14 @@ bool dtls_seal_record(SSL *ssl, uint8_t *out, size_t *out_len, size_t max_out,
288
289
  }
289
290
 
290
291
  // Determine the parameters for the current epoch.
291
- uint16_t epoch = ssl->d1->w_epoch;
292
292
  SSLAEADContext *aead = ssl->s3->aead_write_ctx.get();
293
293
  uint64_t *seq = &ssl->s3->write_sequence;
294
- if (use_epoch == dtls1_use_previous_epoch) {
295
- assert(ssl->d1->w_epoch >= 1);
296
- epoch = ssl->d1->w_epoch - 1;
294
+ if (epoch < ssl->d1->w_epoch) {
295
+ assert(epoch + 1 == ssl->d1->w_epoch);
297
296
  aead = ssl->d1->last_aead_write_ctx.get();
298
297
  seq = &ssl->d1->last_write_sequence;
298
+ } else {
299
+ assert(epoch == ssl->d1->w_epoch);
299
300
  }
300
301
 
301
302
  if (max_out < DTLS1_RT_HEADER_LENGTH) {
@@ -441,16 +441,18 @@ bool tls12_add_verify_sigalgs(const SSL_HANDSHAKE *hs, CBB *out) {
441
441
  }
442
442
 
443
443
  bool tls12_check_peer_sigalg(const SSL_HANDSHAKE *hs, uint8_t *out_alert,
444
- uint16_t sigalg) {
445
- for (uint16_t verify_sigalg : tls12_get_verify_sigalgs(hs)) {
446
- if (verify_sigalg == sigalg) {
447
- return true;
448
- }
444
+ uint16_t sigalg, EVP_PKEY *pkey) {
445
+ // The peer must have selected an algorithm that is consistent with its public
446
+ // key, the TLS version, and what we advertised.
447
+ Span<const uint16_t> sigalgs = tls12_get_verify_sigalgs(hs);
448
+ if (std::find(sigalgs.begin(), sigalgs.end(), sigalg) == sigalgs.end() ||
449
+ !ssl_pkey_supports_algorithm(hs->ssl, pkey, sigalg, /*is_verify=*/true)) {
450
+ OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_SIGNATURE_TYPE);
451
+ *out_alert = SSL_AD_ILLEGAL_PARAMETER;
452
+ return false;
449
453
  }
450
454
 
451
- OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_SIGNATURE_TYPE);
452
- *out_alert = SSL_AD_ILLEGAL_PARAMETER;
453
- return false;
455
+ return true;
454
456
  }
455
457
 
456
458
  // tls_extension represents a TLS extension that is handled internally.
@@ -1474,16 +1476,19 @@ bool ssl_is_alpn_protocol_allowed(const SSL_HANDSHAKE *hs,
1474
1476
  }
1475
1477
 
1476
1478
  // Check that the protocol name is one of the ones we advertised.
1477
- CBS client_protocol_name_list =
1478
- MakeConstSpan(hs->config->alpn_client_proto_list),
1479
- client_protocol_name;
1480
- while (CBS_len(&client_protocol_name_list) > 0) {
1481
- if (!CBS_get_u8_length_prefixed(&client_protocol_name_list,
1482
- &client_protocol_name)) {
1479
+ return ssl_alpn_list_contains_protocol(hs->config->alpn_client_proto_list,
1480
+ protocol);
1481
+ }
1482
+
1483
+ bool ssl_alpn_list_contains_protocol(Span<const uint8_t> list,
1484
+ Span<const uint8_t> protocol) {
1485
+ CBS cbs = list, candidate;
1486
+ while (CBS_len(&cbs) > 0) {
1487
+ if (!CBS_get_u8_length_prefixed(&cbs, &candidate)) {
1483
1488
  return false;
1484
1489
  }
1485
1490
 
1486
- if (client_protocol_name == protocol) {
1491
+ if (candidate == protocol) {
1487
1492
  return true;
1488
1493
  }
1489
1494
  }
@@ -4144,7 +4149,8 @@ bool tls1_choose_signature_algorithm(SSL_HANDSHAKE *hs,
4144
4149
  ? MakeConstSpan(kSignSignatureAlgorithms)
4145
4150
  : cred->sigalgs;
4146
4151
  for (uint16_t sigalg : sigalgs) {
4147
- if (!ssl_pkey_supports_algorithm(ssl, cred->pubkey.get(), sigalg)) {
4152
+ if (!ssl_pkey_supports_algorithm(ssl, cred->pubkey.get(), sigalg,
4153
+ /*is_verify=*/false)) {
4148
4154
  continue;
4149
4155
  }
4150
4156