tls_method.cc
Go to the documentation of this file.
1 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
2  * All rights reserved.
3  *
4  * This package is an SSL implementation written
5  * by Eric Young (eay@cryptsoft.com).
6  * The implementation was written so as to conform with Netscapes SSL.
7  *
8  * This library is free for commercial and non-commercial use as long as
9  * the following conditions are aheared to. The following conditions
10  * apply to all code found in this distribution, be it the RC4, RSA,
11  * lhash, DES, etc., code; not just the SSL code. The SSL documentation
12  * included with this distribution is covered by the same copyright terms
13  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
14  *
15  * Copyright remains Eric Young's, and as such any Copyright notices in
16  * the code are not to be removed.
17  * If this package is used in a product, Eric Young should be given attribution
18  * as the author of the parts of the library used.
19  * This can be in the form of a textual message at program startup or
20  * in documentation (online or textual) provided with the package.
21  *
22  * Redistribution and use in source and binary forms, with or without
23  * modification, are permitted provided that the following conditions
24  * are met:
25  * 1. Redistributions of source code must retain the copyright
26  * notice, this list of conditions and the following disclaimer.
27  * 2. Redistributions in binary form must reproduce the above copyright
28  * notice, this list of conditions and the following disclaimer in the
29  * documentation and/or other materials provided with the distribution.
30  * 3. All advertising materials mentioning features or use of this software
31  * must display the following acknowledgement:
32  * "This product includes cryptographic software written by
33  * Eric Young (eay@cryptsoft.com)"
34  * The word 'cryptographic' can be left out if the rouines from the library
35  * being used are not cryptographic related :-).
36  * 4. If you include any Windows specific code (or a derivative thereof) from
37  * the apps directory (application code) you must include an acknowledgement:
38  * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
39  *
40  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
41  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
44  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50  * SUCH DAMAGE.
51  *
52  * The licence and distribution terms for any publically available version or
53  * derivative of this code cannot be changed. i.e. this code cannot simply be
54  * copied and put under another distribution licence
55  * [including the GNU Public Licence.] */
56 
57 #include <openssl/ssl.h>
58 
59 #include <assert.h>
60 #include <string.h>
61 
62 #include <openssl/err.h>
63 
64 #include "../crypto/internal.h"
65 #include "internal.h"
66 
67 
69 
70 static void tls_on_handshake_complete(SSL *ssl) {
71  // The handshake should have released its final message.
72  assert(!ssl->s3->has_message);
73 
74  // During the handshake, |hs_buf| is retained. Release if it there is no
75  // excess in it. There should not be any excess because the handshake logic
76  // rejects unprocessed data after each Finished message. Note this means we do
77  // not allow a TLS 1.2 HelloRequest to be packed into the same record as
78  // Finished. (Schannel also rejects this.)
79  assert(!ssl->s3->hs_buf || ssl->s3->hs_buf->length == 0);
80  if (ssl->s3->hs_buf && ssl->s3->hs_buf->length == 0) {
81  ssl->s3->hs_buf.reset();
82  }
83 }
84 
85 static bool tls_set_read_state(SSL *ssl, ssl_encryption_level_t level,
86  UniquePtr<SSLAEADContext> aead_ctx,
87  Span<const uint8_t> secret_for_quic) {
88  // Cipher changes are forbidden if the current epoch has leftover data.
92  return false;
93  }
94 
95  if (ssl->quic_method != nullptr) {
96  if ((ssl->s3->hs == nullptr || !ssl->s3->hs->hints_requested) &&
97  !ssl->quic_method->set_read_secret(ssl, level, aead_ctx->cipher(),
98  secret_for_quic.data(),
99  secret_for_quic.size())) {
100  return false;
101  }
102 
103  // QUIC only uses |ssl| for handshake messages, which never use early data
104  // keys, so we return without installing anything. This avoids needing to
105  // have two secrets active at once in 0-RTT.
107  return true;
108  }
109  }
110 
111  OPENSSL_memset(ssl->s3->read_sequence, 0, sizeof(ssl->s3->read_sequence));
112  ssl->s3->aead_read_ctx = std::move(aead_ctx);
113  ssl->s3->read_level = level;
114  return true;
115 }
116 
117 static bool tls_set_write_state(SSL *ssl, ssl_encryption_level_t level,
118  UniquePtr<SSLAEADContext> aead_ctx,
119  Span<const uint8_t> secret_for_quic) {
120  if (!tls_flush_pending_hs_data(ssl)) {
121  return false;
122  }
123 
124  if (ssl->quic_method != nullptr) {
125  if ((ssl->s3->hs == nullptr || !ssl->s3->hs->hints_requested) &&
126  !ssl->quic_method->set_write_secret(ssl, level, aead_ctx->cipher(),
127  secret_for_quic.data(),
128  secret_for_quic.size())) {
129  return false;
130  }
131 
132  // QUIC only uses |ssl| for handshake messages, which never use early data
133  // keys, so we return without installing anything. This avoids needing to
134  // have two secrets active at once in 0-RTT.
136  return true;
137  }
138  }
139 
140  OPENSSL_memset(ssl->s3->write_sequence, 0, sizeof(ssl->s3->write_sequence));
141  ssl->s3->aead_write_ctx = std::move(aead_ctx);
142  ssl->s3->write_level = level;
143  return true;
144 }
145 
147  false /* is_dtls */,
148  tls_new,
149  tls_free,
166 };
167 
170  return true;
171 }
172 
173 static void ssl_noop_x509_clear(CERT *cert) {}
174 static void ssl_noop_x509_free(CERT *cert) {}
175 static void ssl_noop_x509_dup(CERT *new_cert, const CERT *cert) {}
179  return true;
180 }
181 static bool ssl_noop_x509_session_dup(SSL_SESSION *new_session,
182  const SSL_SESSION *session) {
183  return true;
184 }
185 static void ssl_noop_x509_session_clear(SSL_SESSION *session) {}
187  SSL_HANDSHAKE *hs,
188  uint8_t *out_alert) {
189  return false;
190 }
191 
193 static bool ssl_noop_x509_ssl_new(SSL_HANDSHAKE *hs) { return true; }
197  return true;
198 }
199 static bool ssl_noop_x509_ssl_ctx_new(SSL_CTX *ctx) { return true; }
202 
222 };
223 
225 
226 using namespace bssl;
227 
228 const SSL_METHOD *TLS_method(void) {
229  static const SSL_METHOD kMethod = {
230  0,
233  };
234  return &kMethod;
235 }
236 
237 const SSL_METHOD *SSLv23_method(void) {
238  return TLS_method();
239 }
240 
242  static const SSL_METHOD kMethod = {
243  0,
246  };
247  return &kMethod;
248 }
249 
250 // Legacy version-locked methods.
251 
253  static const SSL_METHOD kMethod = {
257  };
258  return &kMethod;
259 }
260 
262  static const SSL_METHOD kMethod = {
266  };
267  return &kMethod;
268 }
269 
270 const SSL_METHOD *TLSv1_method(void) {
271  static const SSL_METHOD kMethod = {
272  TLS1_VERSION,
275  };
276  return &kMethod;
277 }
278 
279 // Legacy side-specific methods.
280 
282  return TLSv1_2_method();
283 }
284 
286  return TLSv1_1_method();
287 }
288 
290  return TLSv1_method();
291 }
292 
294  return TLSv1_2_method();
295 }
296 
298  return TLSv1_1_method();
299 }
300 
302  return TLSv1_method();
303 }
304 
306  return SSLv23_method();
307 }
308 
310  return SSLv23_method();
311 }
312 
314  return TLS_method();
315 }
316 
318  return TLS_method();
319 }
ssl_send_alert
void ssl_send_alert(SSL *ssl, int level, int desc)
Definition: s3_pkt.cc:379
tls_write_app_data
int tls_write_app_data(SSL *ssl, bool *out_needs_handshake, const uint8_t *buf, int len)
Definition: s3_pkt.cc:131
ssl_noop_x509_ssl_ctx_free
static void ssl_noop_x509_ssl_ctx_free(SSL_CTX *ctx)
Definition: tls_method.cc:200
SSL_AD_UNEXPECTED_MESSAGE
#define SSL_AD_UNEXPECTED_MESSAGE
Definition: ssl.h:3795
tls_set_read_state
static bool tls_set_read_state(SSL *ssl, ssl_encryption_level_t level, UniquePtr< SSLAEADContext > aead_ctx, Span< const uint8_t > secret_for_quic)
Definition: tls_method.cc:85
Span::size
size_t size() const
Definition: boringssl-with-bazel/src/include/openssl/span.h:133
TLS_with_buffers_method
const SSL_METHOD * TLS_with_buffers_method(void)
Definition: tls_method.cc:241
ctx
Definition: benchmark-async.c:30
tls_free
void tls_free(SSL *ssl)
Definition: s3_lib.cc:210
ssl_noop_x509_flush_cached_leaf
static void ssl_noop_x509_flush_cached_leaf(CERT *cert)
Definition: tls_method.cc:176
ssl_noop_x509_method
const SSL_X509_METHOD ssl_noop_x509_method
Definition: tls_method.cc:203
SSLv23_method
const SSL_METHOD * SSLv23_method(void)
Definition: tls_method.cc:237
OPENSSL_PUT_ERROR
#define OPENSSL_PUT_ERROR(library, reason)
Definition: err.h:423
internal.h
tls_set_write_state
static bool tls_set_write_state(SSL *ssl, ssl_encryption_level_t level, UniquePtr< SSLAEADContext > aead_ctx, Span< const uint8_t > secret_for_quic)
Definition: tls_method.cc:117
string.h
tls_flush_flight
int tls_flush_flight(SSL *ssl)
Definition: s3_both.cc:284
SSL_X509_METHOD
Definition: third_party/boringssl-with-bazel/src/ssl/internal.h:2510
names
sub_type names
Definition: cxa_demangle.cpp:4905
SSL_R_EXCESS_HANDSHAKE_DATA
#define SSL_R_EXCESS_HANDSHAKE_DATA
Definition: ssl.h:5522
ssl_noop_x509_ssl_new
static bool ssl_noop_x509_ssl_new(SSL_HANDSHAKE *hs)
Definition: tls_method.cc:193
TLS_client_method
const SSL_METHOD * TLS_client_method(void)
Definition: tls_method.cc:317
ssl_noop_x509_ssl_flush_cached_client_CA
static void ssl_noop_x509_ssl_flush_cached_client_CA(SSL_CONFIG *cfg)
Definition: tls_method.cc:195
TLS_method
const SSL_METHOD * TLS_method(void)
Definition: tls_method.cc:228
bssl
Definition: hpke_test.cc:37
crypto_buffer_st
Definition: third_party/boringssl-with-bazel/src/crypto/pool/internal.h:31
TLSv1_1_method
const SSL_METHOD * TLSv1_1_method(void)
Definition: tls_method.cc:261
ssl_noop_x509_session_dup
static bool ssl_noop_x509_session_dup(SSL_SESSION *new_session, const SSL_SESSION *session)
Definition: tls_method.cc:181
SSLv23_server_method
const SSL_METHOD * SSLv23_server_method(void)
Definition: tls_method.cc:305
uint8_t
unsigned char uint8_t
Definition: stdint-msvc2008.h:78
ssl_ctx_st
Definition: third_party/boringssl-with-bazel/src/ssl/internal.h:3404
OPENSSL_memset
static void * OPENSSL_memset(void *dst, int c, size_t n)
Definition: third_party/boringssl-with-bazel/src/crypto/internal.h:835
ssl_st::quic_method
const SSL_QUIC_METHOD * quic_method
Definition: third_party/boringssl-with-bazel/src/ssl/internal.h:3769
tls_get_message
bool tls_get_message(const SSL *ssl, SSLMessage *out)
Definition: s3_both.cc:505
TLS_server_method
const SSL_METHOD * TLS_server_method(void)
Definition: tls_method.cc:313
ssl_quic_method_st::set_read_secret
int(* set_read_secret)(SSL *ssl, enum ssl_encryption_level_t level, const SSL_CIPHER *cipher, const uint8_t *secret, size_t secret_len)
Definition: ssl.h:3312
CERT
Definition: third_party/boringssl-with-bazel/src/ssl/internal.h:2345
TLSv1_1_server_method
const SSL_METHOD * TLSv1_1_server_method(void)
Definition: tls_method.cc:285
tls_open_handshake
ssl_open_record_t tls_open_handshake(SSL *ssl, size_t *out_consumed, uint8_t *out_alert, Span< uint8_t > in)
Definition: s3_both.cc:561
TLS1_1_VERSION
#define TLS1_1_VERSION
Definition: ssl.h:651
tls_add_message
bool tls_add_message(SSL *ssl, Array< uint8_t > msg)
Definition: s3_both.cc:188
SSL_HANDSHAKE
Definition: third_party/boringssl-with-bazel/src/ssl/internal.h:1720
absl::move
constexpr absl::remove_reference_t< T > && move(T &&t) noexcept
Definition: abseil-cpp/absl/utility/utility.h:221
STACK_OF
#define STACK_OF(type)
Definition: stack.h:125
ssl_noop_x509_session_cache_objects
static bool ssl_noop_x509_session_cache_objects(SSL_SESSION *sess)
Definition: tls_method.cc:178
ssl_noop_x509_ssl_ctx_flush_cached_client_CA
static void ssl_noop_x509_ssl_ctx_flush_cached_client_CA(SSL_CTX *ctx)
Definition: tls_method.cc:201
ssl_st
Definition: third_party/boringssl-with-bazel/src/ssl/internal.h:3698
SSLv23_client_method
const SSL_METHOD * SSLv23_client_method(void)
Definition: tls_method.cc:309
BSSL_NAMESPACE_END
#define BSSL_NAMESPACE_END
Definition: base.h:480
ssl_noop_x509_check_client_CA_names
static bool ssl_noop_x509_check_client_CA_names(STACK_OF(CRYPTO_BUFFER) *names)
Definition: tls_method.cc:168
ssl_noop_x509_session_clear
static void ssl_noop_x509_session_clear(SSL_SESSION *session)
Definition: tls_method.cc:185
err.h
ssl_noop_x509_ssl_auto_chain_if_needed
static bool ssl_noop_x509_ssl_auto_chain_if_needed(SSL_HANDSHAKE *hs)
Definition: tls_method.cc:196
SSL_PROTOCOL_METHOD
Definition: third_party/boringssl-with-bazel/src/ssl/internal.h:2427
tls_init_message
bool tls_init_message(const SSL *ssl, CBB *cbb, CBB *body, uint8_t type)
Definition: s3_both.cc:171
TLSv1_2_client_method
const SSL_METHOD * TLSv1_2_client_method(void)
Definition: tls_method.cc:293
Span< const uint8_t >
SSL3_AL_FATAL
#define SSL3_AL_FATAL
Definition: ssl3.h:280
ssl.h
TLSv1_server_method
const SSL_METHOD * TLSv1_server_method(void)
Definition: tls_method.cc:289
TLSv1_2_method
const SSL_METHOD * TLSv1_2_method(void)
Definition: tls_method.cc:252
tls_on_handshake_complete
static BSSL_NAMESPACE_BEGIN void tls_on_handshake_complete(SSL *ssl)
Definition: tls_method.cc:70
ssl_noop_x509_ssl_ctx_new
static bool ssl_noop_x509_ssl_ctx_new(SSL_CTX *ctx)
Definition: tls_method.cc:199
ssl_session_st
Definition: third_party/boringssl-with-bazel/src/ssl/internal.h:3787
ssl_quic_method_st::set_write_secret
int(* set_write_secret)(SSL *ssl, enum ssl_encryption_level_t level, const SSL_CIPHER *cipher, const uint8_t *secret, size_t secret_len)
Definition: ssl.h:3332
TLS1_2_VERSION
#define TLS1_2_VERSION
Definition: ssl.h:652
BSSL_NAMESPACE_BEGIN
Definition: trust_token_test.cc:45
ssl_noop_x509_flush_cached_chain
static void ssl_noop_x509_flush_cached_chain(CERT *cert)
Definition: tls_method.cc:177
ssl_st::s3
bssl::SSL3_STATE * s3
Definition: third_party/boringssl-with-bazel/src/ssl/internal.h:3730
ssl_noop_x509_hs_flush_cached_ca_names
static void ssl_noop_x509_hs_flush_cached_ca_names(SSL_HANDSHAKE *hs)
Definition: tls_method.cc:192
ssl_noop_x509_clear
static void ssl_noop_x509_clear(CERT *cert)
Definition: tls_method.cc:173
TLS1_VERSION
#define TLS1_VERSION
Definition: ssl.h:650
tls_dispatch_alert
int tls_dispatch_alert(SSL *ssl)
Definition: s3_pkt.cc:424
TLSv1_client_method
const SSL_METHOD * TLSv1_client_method(void)
Definition: tls_method.cc:301
tls_finish_message
bool tls_finish_message(const SSL *ssl, CBB *cbb, Array< uint8_t > *out_msg)
Definition: s3_both.cc:184
TLSv1_2_server_method
const SSL_METHOD * TLSv1_2_server_method(void)
Definition: tls_method.cc:281
ssl_noop_x509_free
static void ssl_noop_x509_free(CERT *cert)
Definition: tls_method.cc:174
ssl_method_st
Definition: third_party/boringssl-with-bazel/src/ssl/internal.h:3392
TLSv1_method
const SSL_METHOD * TLSv1_method(void)
Definition: tls_method.cc:270
kTLSProtocolMethod
static const SSL_PROTOCOL_METHOD kTLSProtocolMethod
Definition: tls_method.cc:146
tls_has_unprocessed_handshake_data
bool tls_has_unprocessed_handshake_data(const SSL *ssl)
Definition: s3_both.cc:539
ssl_noop_x509_session_verify_cert_chain
static bool ssl_noop_x509_session_verify_cert_chain(SSL_SESSION *session, SSL_HANDSHAKE *hs, uint8_t *out_alert)
Definition: tls_method.cc:186
TLSv1_1_client_method
const SSL_METHOD * TLSv1_1_client_method(void)
Definition: tls_method.cc:297
tls_add_change_cipher_spec
bool tls_add_change_cipher_spec(SSL *ssl)
Definition: s3_both.cc:266
SSL_CONFIG
Definition: third_party/boringssl-with-bazel/src/ssl/internal.h:2960
ssl_encryption_early_data
ssl_encryption_early_data
Definition: ssl.h:3283
tls_new
bool tls_new(SSL *ssl)
Definition: s3_lib.cc:186
ssl_noop_x509_ssl_config_free
static void ssl_noop_x509_ssl_config_free(SSL_CONFIG *cfg)
Definition: tls_method.cc:194
Span::data
T * data() const
Definition: boringssl-with-bazel/src/include/openssl/span.h:132
tls_open_change_cipher_spec
ssl_open_record_t tls_open_change_cipher_spec(SSL *ssl, size_t *out_consumed, uint8_t *out_alert, Span< uint8_t > in)
Definition: s3_pkt.cc:353
ssl_crypto_x509_method
const SSL_X509_METHOD ssl_crypto_x509_method
Definition: ssl_x509.cc:511
client.level
level
Definition: examples/python/async_streaming/client.py:118
tls_open_app_data
ssl_open_record_t tls_open_app_data(SSL *ssl, Span< uint8_t > *out, size_t *out_consumed, uint8_t *out_alert, Span< uint8_t > in)
Definition: s3_pkt.cc:297
tls_flush_pending_hs_data
bool tls_flush_pending_hs_data(SSL *ssl)
Definition: s3_both.cc:244
ssl_noop_x509_dup
static void ssl_noop_x509_dup(CERT *new_cert, const CERT *cert)
Definition: tls_method.cc:175
tls_next_message
void tls_next_message(SSL *ssl)
Definition: s3_both.cc:640


grpc
Author(s):
autogenerated on Thu Mar 13 2025 03:01:40