Coverage Report

Created: 2025-12-07 06:13

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/boringssl/ssl/ssl_lib.cc
Line
Count
Source
1
// Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
2
// Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved.
3
// Copyright 2005 Nokia. All rights reserved.
4
//
5
// Licensed under the Apache License, Version 2.0 (the "License");
6
// you may not use this file except in compliance with the License.
7
// You may obtain a copy of the License at
8
//
9
//     https://www.apache.org/licenses/LICENSE-2.0
10
//
11
// Unless required by applicable law or agreed to in writing, software
12
// distributed under the License is distributed on an "AS IS" BASIS,
13
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
// See the License for the specific language governing permissions and
15
// limitations under the License.
16
17
#include <openssl/ssl.h>
18
19
#include <algorithm>
20
#include <iterator>
21
22
#include <assert.h>
23
#include <limits.h>
24
#include <stdlib.h>
25
#include <string.h>
26
27
#include <openssl/bytestring.h>
28
#include <openssl/crypto.h>
29
#include <openssl/err.h>
30
#include <openssl/evp.h>
31
#include <openssl/mem.h>
32
#include <openssl/nid.h>
33
#include <openssl/rand.h>
34
35
#include "../crypto/internal.h"
36
#include "internal.h"
37
38
#if defined(OPENSSL_WINDOWS)
39
#include <sys/timeb.h>
40
#else
41
#include <sys/socket.h>
42
#include <sys/time.h>
43
#endif
44
45
46
BSSL_NAMESPACE_BEGIN
47
48
static_assert(SSL3_RT_MAX_ENCRYPTED_OVERHEAD >=
49
                  SSL3_RT_SEND_MAX_ENCRYPTED_OVERHEAD,
50
              "max overheads are inconsistent");
51
52
// |SSL_R_UNKNOWN_PROTOCOL| is no longer emitted, but continue to define it
53
// to avoid downstream churn.
54
OPENSSL_DECLARE_ERROR_REASON(SSL, UNKNOWN_PROTOCOL)
55
56
// The following errors are no longer emitted, but are used in nginx without
57
// #ifdefs.
58
OPENSSL_DECLARE_ERROR_REASON(SSL, BLOCK_CIPHER_PAD_IS_WRONG)
59
OPENSSL_DECLARE_ERROR_REASON(SSL, NO_CIPHERS_SPECIFIED)
60
61
// Some error codes are special. Ensure the make_errors.go script never
62
// regresses this.
63
static_assert(SSL_R_TLSV1_ALERT_NO_RENEGOTIATION ==
64
                  SSL_AD_NO_RENEGOTIATION + SSL_AD_REASON_OFFSET,
65
              "alert reason code mismatch");
66
67
// kMaxHandshakeSize is the maximum size, in bytes, of a handshake message.
68
static const size_t kMaxHandshakeSize = (1u << 24) - 1;
69
70
static CRYPTO_EX_DATA_CLASS g_ex_data_class_ssl =
71
    CRYPTO_EX_DATA_CLASS_INIT_WITH_APP_DATA;
72
static CRYPTO_EX_DATA_CLASS g_ex_data_class_ssl_ctx =
73
    CRYPTO_EX_DATA_CLASS_INIT_WITH_APP_DATA;
74
75
400k
bool CBBFinishArray(CBB *cbb, Array<uint8_t> *out) {
76
400k
  uint8_t *ptr;
77
400k
  size_t len;
78
400k
  if (!CBB_finish(cbb, &ptr, &len)) {
79
0
    OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
80
0
    return false;
81
0
  }
82
400k
  out->Reset(ptr, len);
83
400k
  return true;
84
400k
}
85
86
120k
void ssl_reset_error_state(SSL *ssl) {
87
  // Functions which use |SSL_get_error| must reset I/O and error state on
88
  // entry.
89
120k
  ssl->s3->rwstate = SSL_ERROR_NONE;
90
120k
  ERR_clear_error();
91
120k
  ERR_clear_system_error();
92
120k
}
93
94
4.49k
void ssl_set_read_error(SSL *ssl) {
95
4.49k
  ssl->s3->read_shutdown = ssl_shutdown_error;
96
4.49k
  ssl->s3->read_error.reset(ERR_save_state());
97
4.49k
}
98
99
2.19M
static bool check_read_error(const SSL *ssl) {
100
2.19M
  if (ssl->s3->read_shutdown == ssl_shutdown_error) {
101
0
    ERR_restore_state(ssl->s3->read_error.get());
102
0
    return false;
103
0
  }
104
2.19M
  return true;
105
2.19M
}
106
107
0
bool ssl_can_write(const SSL *ssl) {
108
0
  return !SSL_in_init(ssl) || ssl->s3->hs->can_early_write;
109
0
}
110
111
695k
bool ssl_can_read(const SSL *ssl) {
112
695k
  return !SSL_in_init(ssl) || ssl->s3->hs->can_early_read;
113
695k
}
114
115
ssl_open_record_t ssl_open_handshake(SSL *ssl, size_t *out_consumed,
116
1.64M
                                     uint8_t *out_alert, Span<uint8_t> in) {
117
1.64M
  *out_consumed = 0;
118
1.64M
  if (!check_read_error(ssl)) {
119
0
    *out_alert = 0;
120
0
    return ssl_open_record_error;
121
0
  }
122
1.64M
  auto ret = ssl->method->open_handshake(ssl, out_consumed, out_alert, in);
123
1.64M
  if (ret == ssl_open_record_error) {
124
3.52k
    ssl_set_read_error(ssl);
125
3.52k
  }
126
1.64M
  return ret;
127
1.64M
}
128
129
ssl_open_record_t ssl_open_change_cipher_spec(SSL *ssl, size_t *out_consumed,
130
                                              uint8_t *out_alert,
131
152k
                                              Span<uint8_t> in) {
132
152k
  *out_consumed = 0;
133
152k
  if (!check_read_error(ssl)) {
134
0
    *out_alert = 0;
135
0
    return ssl_open_record_error;
136
0
  }
137
152k
  auto ret =
138
152k
      ssl->method->open_change_cipher_spec(ssl, out_consumed, out_alert, in);
139
152k
  if (ret == ssl_open_record_error) {
140
210
    ssl_set_read_error(ssl);
141
210
  }
142
152k
  return ret;
143
152k
}
144
145
ssl_open_record_t ssl_open_app_data(SSL *ssl, Span<uint8_t> *out,
146
                                    size_t *out_consumed, uint8_t *out_alert,
147
355k
                                    Span<uint8_t> in) {
148
355k
  *out_consumed = 0;
149
355k
  if (!check_read_error(ssl)) {
150
0
    *out_alert = 0;
151
0
    return ssl_open_record_error;
152
0
  }
153
355k
  auto ret = ssl->method->open_app_data(ssl, out, out_consumed, out_alert, in);
154
355k
  if (ret == ssl_open_record_error) {
155
633
    ssl_set_read_error(ssl);
156
633
  }
157
355k
  return ret;
158
355k
}
159
160
0
static uint8_t hex_char_consttime(uint8_t b) {
161
0
  declassify_assert(b < 16);
162
0
  return constant_time_select_8(constant_time_lt_8(b, 10), b + '0',
163
0
                                b - 10 + 'a');
164
0
}
165
166
0
static bool cbb_add_hex_consttime(CBB *cbb, Span<const uint8_t> in) {
167
0
  uint8_t *out;
168
0
  if (!CBB_add_space(cbb, &out, in.size() * 2)) {
169
0
    return false;
170
0
  }
171
172
0
  for (uint8_t b : in) {
173
0
    *(out++) = hex_char_consttime(b >> 4);
174
0
    *(out++) = hex_char_consttime(b & 0xf);
175
0
  }
176
177
0
  return true;
178
0
}
179
180
bool ssl_log_secret(const SSL *ssl, const char *label,
181
75.0k
                    Span<const uint8_t> secret) {
182
75.0k
  if (ssl->ctx->keylog_callback == nullptr) {
183
75.0k
    return true;
184
75.0k
  }
185
186
0
  ScopedCBB cbb;
187
0
  Array<uint8_t> line;
188
0
  auto label_bytes = bssl::StringAsBytes(label);
189
0
  if (!CBB_init(cbb.get(), strlen(label) + 1 + SSL3_RANDOM_SIZE * 2 + 1 +
190
0
                               secret.size() * 2 + 1) ||
191
0
      !CBB_add_bytes(cbb.get(), label_bytes.data(), label_bytes.size()) ||
192
0
      !CBB_add_u8(cbb.get(), ' ') ||
193
0
      !cbb_add_hex_consttime(cbb.get(), ssl->s3->client_random) ||
194
0
      !CBB_add_u8(cbb.get(), ' ') ||
195
      // Convert to hex in constant time to avoid leaking |secret|. If the
196
      // callback discards the data, we should not introduce side channels.
197
0
      !cbb_add_hex_consttime(cbb.get(), secret) ||
198
0
      !CBB_add_u8(cbb.get(), 0 /* NUL */) ||
199
0
      !CBBFinishArray(cbb.get(), &line)) {
200
0
    return false;
201
0
  }
202
203
0
  ssl->ctx->keylog_callback(ssl, reinterpret_cast<const char *>(line.data()));
204
0
  return true;
205
0
}
206
207
1.35M
void ssl_do_info_callback(const SSL *ssl, int type, int value) {
208
1.35M
  void (*cb)(const SSL *ssl, int type, int value) = nullptr;
209
1.35M
  if (ssl->info_callback != nullptr) {
210
0
    cb = ssl->info_callback;
211
1.35M
  } else if (ssl->ctx->info_callback != nullptr) {
212
0
    cb = ssl->ctx->info_callback;
213
0
  }
214
215
1.35M
  if (cb != nullptr) {
216
0
    cb(ssl, type, value);
217
0
  }
218
1.35M
}
219
220
void ssl_do_msg_callback(const SSL *ssl, int is_write, int content_type,
221
1.92M
                         Span<const uint8_t> in) {
222
1.92M
  if (ssl->msg_callback == nullptr) {
223
1.92M
    return;
224
1.92M
  }
225
226
  // |version| is zero when calling for |SSL3_RT_HEADER| and |SSL2_VERSION| for
227
  // a V2ClientHello.
228
0
  int version;
229
0
  switch (content_type) {
230
0
    case 0:
231
      // V2ClientHello
232
0
      version = SSL2_VERSION;
233
0
      break;
234
0
    case SSL3_RT_HEADER:
235
0
      version = 0;
236
0
      break;
237
0
    default:
238
0
      version = SSL_version(ssl);
239
0
  }
240
241
0
  ssl->msg_callback(is_write, version, content_type, in.data(), in.size(),
242
0
                    const_cast<SSL *>(ssl), ssl->msg_callback_arg);
243
0
}
244
245
284k
OPENSSL_timeval ssl_ctx_get_current_time(const SSL_CTX *ctx) {
246
284k
  if (ctx->current_time_cb != nullptr) {
247
    // TODO(davidben): Update current_time_cb to use OPENSSL_timeval. See
248
    // https://crbug.com/boringssl/155.
249
284k
    struct timeval clock;
250
284k
    ctx->current_time_cb(nullptr /* ssl */, &clock);
251
284k
    if (clock.tv_sec < 0) {
252
0
      assert(0);
253
0
      return {0, 0};
254
284k
    } else {
255
284k
      return {static_cast<uint64_t>(clock.tv_sec),
256
284k
              static_cast<uint32_t>(clock.tv_usec)};
257
284k
    }
258
284k
  }
259
260
#if defined(OPENSSL_WINDOWS)
261
  struct _timeb time;
262
  _ftime(&time);
263
  if (time.time < 0) {
264
    assert(0);
265
    return {0, 0};
266
  } else {
267
    return {static_cast<uint64_t>(time.time),
268
            static_cast<uint32_t>(time.millitm * 1000)};
269
  }
270
#else
271
0
  struct timeval clock;
272
0
  gettimeofday(&clock, nullptr);
273
0
  if (clock.tv_sec < 0) {
274
0
    assert(0);
275
0
    return {0, 0};
276
0
  } else {
277
0
    return {static_cast<uint64_t>(clock.tv_sec),
278
0
            static_cast<uint32_t>(clock.tv_usec)};
279
0
  }
280
0
#endif
281
0
}
282
283
0
void SSL_CTX_set_handoff_mode(SSL_CTX *ctx, bool on) { ctx->handoff = on; }
284
285
96.0k
static bool ssl_can_renegotiate(const SSL *ssl) {
286
96.0k
  if (ssl->server || SSL_is_dtls(ssl)) {
287
0
    return false;
288
0
  }
289
290
96.0k
  if (ssl->s3->version != 0  //
291
96.0k
      && ssl_protocol_version(ssl) >= TLS1_3_VERSION) {
292
0
    return false;
293
0
  }
294
295
  // The config has already been shed.
296
96.0k
  if (!ssl->config) {
297
0
    return false;
298
0
  }
299
300
96.0k
  switch (ssl->renegotiate_mode) {
301
0
    case ssl_renegotiate_ignore:
302
0
    case ssl_renegotiate_never:
303
0
      return false;
304
305
96.0k
    case ssl_renegotiate_freely:
306
96.0k
    case ssl_renegotiate_explicit:
307
96.0k
      return true;
308
0
    case ssl_renegotiate_once:
309
0
      return ssl->s3->total_renegotiations == 0;
310
96.0k
  }
311
312
96.0k
  assert(0);
313
0
  return false;
314
0
}
315
316
66.3k
static void ssl_maybe_shed_handshake_config(SSL *ssl) {
317
66.3k
  if (ssl->s3->hs != nullptr ||               //
318
52.1k
      ssl->config == nullptr ||               //
319
52.1k
      !ssl->config->shed_handshake_config ||  //
320
66.3k
      ssl_can_renegotiate(ssl)) {
321
66.3k
    return;
322
66.3k
  }
323
324
0
  ssl->config.reset();
325
0
}
326
327
14.9k
void SSL_set_handoff_mode(SSL *ssl, bool on) {
328
14.9k
  if (!ssl->config) {
329
0
    return;
330
0
  }
331
14.9k
  ssl->config->handoff = on;
332
14.9k
}
333
334
bool SSL_get_traffic_secrets(const SSL *ssl,
335
                             Span<const uint8_t> *out_read_traffic_secret,
336
0
                             Span<const uint8_t> *out_write_traffic_secret) {
337
  // This API is not well-defined for DTLS, where multiple epochs may be alive
338
  // at once. Callers should use |SSL_get_dtls_*_traffic_secret| instead. In
339
  // QUIC, the application is already handed the traffic secret.
340
0
  if (SSL_is_dtls(ssl) || SSL_is_quic(ssl)) {
341
0
    OPENSSL_PUT_ERROR(SSL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
342
0
    return false;
343
0
  }
344
345
0
  if (!ssl->s3->initial_handshake_complete) {
346
0
    OPENSSL_PUT_ERROR(SSL, SSL_R_HANDSHAKE_NOT_COMPLETE);
347
0
    return false;
348
0
  }
349
350
0
  if (SSL_version(ssl) < TLS1_3_VERSION) {
351
0
    OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_SSL_VERSION);
352
0
    return false;
353
0
  }
354
355
0
  *out_read_traffic_secret = ssl->s3->read_traffic_secret;
356
0
  *out_write_traffic_secret = ssl->s3->write_traffic_secret;
357
0
  return true;
358
0
}
359
360
void SSL_CTX_set_aes_hw_override_for_testing(SSL_CTX *ctx,
361
0
                                             bool override_value) {
362
0
  ctx->aes_hw_override = true;
363
0
  ctx->aes_hw_override_value = override_value;
364
0
}
365
366
0
void SSL_set_aes_hw_override_for_testing(SSL *ssl, bool override_value) {
367
0
  ssl->config->aes_hw_override = true;
368
0
  ssl->config->aes_hw_override_value = override_value;
369
0
}
370
371
BSSL_NAMESPACE_END
372
373
using namespace bssl;
374
375
0
int SSL_library_init(void) { return 1; }
376
377
0
int OPENSSL_init_ssl(uint64_t opts, const OPENSSL_INIT_SETTINGS *settings) {
378
0
  return 1;
379
0
}
380
381
72.2k
static uint32_t ssl_session_hash(const SSL_SESSION *sess) {
382
72.2k
  return ssl_hash_session_id(sess->session_id);
383
72.2k
}
384
385
186k
static int ssl_session_cmp(const SSL_SESSION *a, const SSL_SESSION *b) {
386
186k
  return Span(a->session_id) == b->session_id ? 0 : 1;
387
186k
}
388
389
ssl_ctx_st::ssl_ctx_st(const SSL_METHOD *ssl_method)
390
4.91k
    : RefCounted(CheckSubClass()),
391
4.91k
      method(ssl_method->method),
392
4.91k
      x509_method(ssl_method->x509_method),
393
4.91k
      retain_only_sha256_of_client_certs(false),
394
4.91k
      quiet_shutdown(false),
395
4.91k
      ocsp_stapling_enabled(false),
396
4.91k
      signed_cert_timestamps_enabled(false),
397
4.91k
      channel_id_enabled(false),
398
4.91k
      grease_enabled(false),
399
4.91k
      permute_extensions(false),
400
4.91k
      allow_unknown_alpn_protos(false),
401
4.91k
      false_start_allowed_without_alpn(false),
402
4.91k
      handoff(false),
403
4.91k
      enable_early_data(false),
404
4.91k
      aes_hw_override(false),
405
4.91k
      aes_hw_override_value(false),
406
4.91k
      resumption_across_names_enabled(false) {
407
4.91k
  CRYPTO_MUTEX_init(&lock);
408
4.91k
  CRYPTO_new_ex_data(&ex_data);
409
4.91k
}
410
411
4.89k
ssl_ctx_st::~ssl_ctx_st() {
412
  // Free the internal session cache. Note that this calls the caller-supplied
413
  // remove callback, so we must do it before clearing ex_data. (See ticket
414
  // [openssl.org #212].)
415
4.89k
  SSL_CTX_flush_sessions(this, 0);
416
417
4.89k
  CRYPTO_free_ex_data(&g_ex_data_class_ssl_ctx, &ex_data);
418
419
4.89k
  CRYPTO_MUTEX_cleanup(&lock);
420
4.89k
  lh_SSL_SESSION_free(sessions);
421
4.89k
  x509_method->ssl_ctx_free(this);
422
4.89k
}
423
424
4.91k
SSL_CTX *SSL_CTX_new(const SSL_METHOD *method) {
425
4.91k
  if (method == nullptr) {
426
0
    OPENSSL_PUT_ERROR(SSL, SSL_R_NULL_SSL_METHOD_PASSED);
427
0
    return nullptr;
428
0
  }
429
430
4.91k
  UniquePtr<SSL_CTX> ret = MakeUnique<SSL_CTX>(method);
431
4.91k
  if (!ret) {
432
0
    return nullptr;
433
0
  }
434
435
4.91k
  ret->cert = MakeUnique<CERT>(method->x509_method);
436
4.91k
  ret->sessions = lh_SSL_SESSION_new(ssl_session_hash, ssl_session_cmp);
437
4.91k
  ret->client_CA.reset(sk_CRYPTO_BUFFER_new_null());
438
4.91k
  ret->CA_names.reset(sk_CRYPTO_BUFFER_new_null());
439
4.91k
  if (ret->cert == nullptr ||       //
440
4.91k
      !ret->cert->is_valid() ||     //
441
4.91k
      ret->sessions == nullptr ||   //
442
4.91k
      ret->client_CA == nullptr ||  //
443
4.91k
      ret->CA_names == nullptr ||   //
444
4.91k
      !ret->x509_method->ssl_ctx_new(ret.get())) {
445
0
    return nullptr;
446
0
  }
447
448
4.91k
  if (!SSL_CTX_set_strict_cipher_list(ret.get(), SSL_DEFAULT_CIPHER_LIST) ||
449
      // Lock the SSL_CTX to the specified version, for compatibility with
450
      // legacy uses of SSL_METHOD.
451
4.91k
      !SSL_CTX_set_max_proto_version(ret.get(), method->version) ||
452
4.91k
      !SSL_CTX_set_min_proto_version(ret.get(), method->version)) {
453
0
    OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
454
0
    return nullptr;
455
0
  }
456
457
4.91k
  if (!ret->supported_group_list.CopyFrom(DefaultSupportedGroupIds())) {
458
0
    return nullptr;
459
0
  }
460
461
4.91k
  return ret.release();
462
4.91k
}
463
464
241k
int SSL_CTX_up_ref(SSL_CTX *ctx) {
465
241k
  ctx->UpRefInternal();
466
241k
  return 1;
467
241k
}
468
469
246k
void SSL_CTX_free(SSL_CTX *ctx) {
470
246k
  if (ctx != nullptr) {
471
246k
    ctx->DecRefInternal();
472
246k
  }
473
246k
}
474
475
ssl_st::ssl_st(SSL_CTX *ctx_arg)
476
120k
    : method(ctx_arg->method),
477
120k
      max_send_fragment(ctx_arg->max_send_fragment),
478
120k
      msg_callback(ctx_arg->msg_callback),
479
120k
      msg_callback_arg(ctx_arg->msg_callback_arg),
480
120k
      ctx(UpRef(ctx_arg)),
481
120k
      session_ctx(UpRef(ctx_arg)),
482
120k
      options(ctx->options),
483
120k
      mode(ctx->mode),
484
120k
      max_cert_list(ctx->max_cert_list),
485
120k
      server(false),
486
120k
      quiet_shutdown(ctx->quiet_shutdown),
487
120k
      enable_early_data(ctx->enable_early_data),
488
120k
      resumption_across_names_enabled(ctx->resumption_across_names_enabled) {
489
120k
  CRYPTO_new_ex_data(&ex_data);
490
120k
}
491
492
120k
ssl_st::~ssl_st() {
493
120k
  CRYPTO_free_ex_data(&g_ex_data_class_ssl, &ex_data);
494
  // |config| refers to |this|, so we must release it earlier.
495
120k
  config.reset();
496
120k
  if (method != nullptr) {
497
120k
    method->ssl_free(this);
498
120k
  }
499
120k
}
500
501
120k
SSL *SSL_new(SSL_CTX *ctx) {
502
120k
  if (ctx == nullptr) {
503
0
    OPENSSL_PUT_ERROR(SSL, SSL_R_NULL_SSL_CTX);
504
0
    return nullptr;
505
0
  }
506
507
120k
  UniquePtr<SSL> ssl = MakeUnique<SSL>(ctx);
508
120k
  if (ssl == nullptr) {
509
0
    return nullptr;
510
0
  }
511
512
120k
  ssl->config = MakeUnique<SSL_CONFIG>(ssl.get());
513
120k
  if (ssl->config == nullptr) {
514
0
    return nullptr;
515
0
  }
516
120k
  ssl->config->conf_min_version = ctx->conf_min_version;
517
120k
  ssl->config->conf_max_version = ctx->conf_max_version;
518
519
120k
  ssl->config->cert = ssl_cert_dup(ctx->cert.get());
520
120k
  if (ssl->config->cert == nullptr) {
521
0
    return nullptr;
522
0
  }
523
524
120k
  ssl->config->verify_mode = ctx->verify_mode;
525
120k
  ssl->config->verify_callback = ctx->default_verify_callback;
526
120k
  ssl->config->custom_verify_callback = ctx->custom_verify_callback;
527
120k
  ssl->config->retain_only_sha256_of_client_certs =
528
120k
      ctx->retain_only_sha256_of_client_certs;
529
120k
  ssl->config->permute_extensions = ctx->permute_extensions;
530
120k
  ssl->config->aes_hw_override = ctx->aes_hw_override;
531
120k
  ssl->config->aes_hw_override_value = ctx->aes_hw_override_value;
532
120k
  ssl->config->compliance_policy = ctx->compliance_policy;
533
534
120k
  if (!ssl->config->supported_group_list.CopyFrom(ctx->supported_group_list) ||
535
120k
      !ssl->config->alpn_client_proto_list.CopyFrom(
536
120k
          ctx->alpn_client_proto_list) ||
537
120k
      !ssl->config->verify_sigalgs.CopyFrom(ctx->verify_sigalgs)) {
538
0
    return nullptr;
539
0
  }
540
541
120k
  if (ctx->requested_trust_anchors) {
542
0
    ssl->config->requested_trust_anchors.emplace();
543
0
    if (!ssl->config->requested_trust_anchors->CopyFrom(
544
0
            *ctx->requested_trust_anchors)) {
545
0
      return nullptr;
546
0
    }
547
0
  }
548
549
120k
  if (ctx->psk_identity_hint) {
550
0
    ssl->config->psk_identity_hint.reset(
551
0
        OPENSSL_strdup(ctx->psk_identity_hint.get()));
552
0
    if (ssl->config->psk_identity_hint == nullptr) {
553
0
      return nullptr;
554
0
    }
555
0
  }
556
120k
  ssl->config->psk_client_callback = ctx->psk_client_callback;
557
120k
  ssl->config->psk_server_callback = ctx->psk_server_callback;
558
559
120k
  ssl->config->channel_id_enabled = ctx->channel_id_enabled;
560
120k
  ssl->config->channel_id_private = UpRef(ctx->channel_id_private);
561
562
120k
  ssl->config->signed_cert_timestamps_enabled =
563
120k
      ctx->signed_cert_timestamps_enabled;
564
120k
  ssl->config->ocsp_stapling_enabled = ctx->ocsp_stapling_enabled;
565
120k
  ssl->config->handoff = ctx->handoff;
566
120k
  ssl->quic_method = ctx->quic_method;
567
568
120k
  if (!ssl->method->ssl_new(ssl.get()) ||
569
120k
      !ssl->ctx->x509_method->ssl_new(ssl->s3->hs.get())) {
570
0
    return nullptr;
571
0
  }
572
573
120k
  return ssl.release();
574
120k
}
575
576
SSL_CONFIG::SSL_CONFIG(SSL *ssl_arg)
577
120k
    : ssl(ssl_arg),
578
120k
      ech_grease_enabled(false),
579
120k
      signed_cert_timestamps_enabled(false),
580
120k
      ocsp_stapling_enabled(false),
581
120k
      channel_id_enabled(false),
582
120k
      enforce_rsa_key_usage(true),
583
120k
      retain_only_sha256_of_client_certs(false),
584
120k
      handoff(false),
585
120k
      shed_handshake_config(false),
586
120k
      jdk11_workaround(false),
587
120k
      quic_use_legacy_codepoint(false),
588
120k
      permute_extensions(false),
589
120k
      alps_use_new_codepoint(true) {
590
120k
  assert(ssl);
591
120k
}
592
593
120k
SSL_CONFIG::~SSL_CONFIG() {
594
120k
  if (ssl->ctx != nullptr) {
595
120k
    ssl->ctx->x509_method->ssl_config_free(this);
596
120k
  }
597
120k
}
598
599
120k
void SSL_free(SSL *ssl) { Delete(ssl); }
600
601
25.6k
void SSL_set_connect_state(SSL *ssl) {
602
25.6k
  ssl->server = false;
603
25.6k
  ssl->do_handshake = ssl_client_handshake;
604
25.6k
}
605
606
59.1k
void SSL_set_accept_state(SSL *ssl) {
607
59.1k
  ssl->server = true;
608
59.1k
  ssl->do_handshake = ssl_server_handshake;
609
59.1k
}
610
611
30.9k
void SSL_set0_rbio(SSL *ssl, BIO *rbio) { ssl->rbio.reset(rbio); }
612
613
30.9k
void SSL_set0_wbio(SSL *ssl, BIO *wbio) { ssl->wbio.reset(wbio); }
614
615
0
void SSL_set_bio(SSL *ssl, BIO *rbio, BIO *wbio) {
616
  // For historical reasons, this function has many different cases in ownership
617
  // handling.
618
619
  // If nothing has changed, do nothing
620
0
  if (rbio == SSL_get_rbio(ssl) && wbio == SSL_get_wbio(ssl)) {
621
0
    return;
622
0
  }
623
624
  // If the two arguments are equal, one fewer reference is granted than
625
  // taken.
626
0
  if (rbio != nullptr && rbio == wbio) {
627
0
    BIO_up_ref(rbio);
628
0
  }
629
630
  // If only the wbio is changed, adopt only one reference.
631
0
  if (rbio == SSL_get_rbio(ssl)) {
632
0
    SSL_set0_wbio(ssl, wbio);
633
0
    return;
634
0
  }
635
636
  // There is an asymmetry here for historical reasons. If only the rbio is
637
  // changed AND the rbio and wbio were originally different, then we only adopt
638
  // one reference.
639
0
  if (wbio == SSL_get_wbio(ssl) && SSL_get_rbio(ssl) != SSL_get_wbio(ssl)) {
640
0
    SSL_set0_rbio(ssl, rbio);
641
0
    return;
642
0
  }
643
644
  // Otherwise, adopt both references.
645
0
  SSL_set0_rbio(ssl, rbio);
646
0
  SSL_set0_wbio(ssl, wbio);
647
0
}
648
649
8.02k
BIO *SSL_get_rbio(const SSL *ssl) { return ssl->rbio.get(); }
650
651
6
BIO *SSL_get_wbio(const SSL *ssl) { return ssl->wbio.get(); }
652
653
size_t SSL_quic_max_handshake_flight_len(const SSL *ssl,
654
0
                                         enum ssl_encryption_level_t level) {
655
  // Limits flights to 16K by default when there are no large
656
  // (certificate-carrying) messages.
657
0
  static const size_t kDefaultLimit = 16384;
658
659
0
  switch (level) {
660
0
    case ssl_encryption_initial:
661
0
      return kDefaultLimit;
662
0
    case ssl_encryption_early_data:
663
      // QUIC does not send EndOfEarlyData.
664
0
      return 0;
665
0
    case ssl_encryption_handshake:
666
0
      if (ssl->server) {
667
        // Servers may receive Certificate message if configured to request
668
        // client certificates.
669
0
        if (!!(ssl->config->verify_mode & SSL_VERIFY_PEER) &&
670
0
            ssl->max_cert_list > kDefaultLimit) {
671
0
          return ssl->max_cert_list;
672
0
        }
673
0
      } else {
674
        // Clients may receive both Certificate message and a CertificateRequest
675
        // message.
676
0
        if (2 * ssl->max_cert_list > kDefaultLimit) {
677
0
          return 2 * ssl->max_cert_list;
678
0
        }
679
0
      }
680
0
      return kDefaultLimit;
681
0
    case ssl_encryption_application:
682
      // Note there is not actually a bound on the number of NewSessionTickets
683
      // one may send in a row. This level may need more involved flow
684
      // control. See https://github.com/quicwg/base-drafts/issues/1834.
685
0
      return kDefaultLimit;
686
0
  }
687
688
0
  return 0;
689
0
}
690
691
0
enum ssl_encryption_level_t SSL_quic_read_level(const SSL *ssl) {
692
0
  assert(SSL_is_quic(ssl));
693
0
  return ssl->s3->quic_read_level;
694
0
}
695
696
0
enum ssl_encryption_level_t SSL_quic_write_level(const SSL *ssl) {
697
0
  assert(SSL_is_quic(ssl));
698
0
  return ssl->s3->quic_write_level;
699
0
}
700
701
int SSL_provide_quic_data(SSL *ssl, enum ssl_encryption_level_t level,
702
0
                          const uint8_t *data, size_t len) {
703
0
  if (!SSL_is_quic(ssl)) {
704
0
    OPENSSL_PUT_ERROR(SSL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
705
0
    return 0;
706
0
  }
707
708
0
  if (level != ssl->s3->quic_read_level) {
709
0
    OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_ENCRYPTION_LEVEL_RECEIVED);
710
0
    return 0;
711
0
  }
712
713
0
  size_t new_len = (ssl->s3->hs_buf ? ssl->s3->hs_buf->length : 0) + len;
714
0
  if (new_len < len ||
715
0
      new_len > SSL_quic_max_handshake_flight_len(ssl, level)) {
716
0
    OPENSSL_PUT_ERROR(SSL, SSL_R_EXCESSIVE_MESSAGE_SIZE);
717
0
    return 0;
718
0
  }
719
720
0
  return tls_append_handshake_data(ssl, Span(data, len));
721
0
}
722
723
79.0k
int SSL_do_handshake(SSL *ssl) {
724
79.0k
  ssl_reset_error_state(ssl);
725
726
79.0k
  if (ssl->do_handshake == nullptr) {
727
0
    OPENSSL_PUT_ERROR(SSL, SSL_R_CONNECTION_TYPE_NOT_SET);
728
0
    return -1;
729
0
  }
730
731
79.0k
  if (!SSL_in_init(ssl)) {
732
0
    return 1;
733
0
  }
734
735
  // Run the handshake.
736
79.0k
  SSL_HANDSHAKE *hs = ssl->s3->hs.get();
737
738
79.0k
  bool early_return = false;
739
79.0k
  int ret = ssl_run_handshake(hs, &early_return);
740
79.0k
  ssl_do_info_callback(
741
79.0k
      ssl, ssl->server ? SSL_CB_ACCEPT_EXIT : SSL_CB_CONNECT_EXIT, ret);
742
79.0k
  if (ret <= 0) {
743
26.8k
    return ret;
744
26.8k
  }
745
746
  // Destroy the handshake object if the handshake has completely finished.
747
52.2k
  if (!early_return) {
748
52.1k
    ssl->s3->hs.reset();
749
52.1k
    ssl_maybe_shed_handshake_config(ssl);
750
52.1k
  }
751
752
52.2k
  return 1;
753
79.0k
}
754
755
0
int SSL_connect(SSL *ssl) {
756
0
  if (ssl->do_handshake == nullptr) {
757
    // Not properly initialized yet
758
0
    SSL_set_connect_state(ssl);
759
0
  }
760
761
0
  return SSL_do_handshake(ssl);
762
0
}
763
764
0
int SSL_accept(SSL *ssl) {
765
0
  if (ssl->do_handshake == nullptr) {
766
    // Not properly initialized yet
767
0
    SSL_set_accept_state(ssl);
768
0
  }
769
770
0
  return SSL_do_handshake(ssl);
771
0
}
772
773
90.0k
static int ssl_do_post_handshake(SSL *ssl, const SSLMessage &msg) {
774
90.0k
  if (ssl_protocol_version(ssl) >= TLS1_3_VERSION) {
775
42.0k
    return tls13_post_handshake(ssl, msg);
776
42.0k
  }
777
778
  // Check for renegotiation on the server before parsing to use the correct
779
  // error. Renegotiation is triggered by a different message for servers.
780
48.0k
  if (ssl->server) {
781
0
    OPENSSL_PUT_ERROR(SSL, SSL_R_NO_RENEGOTIATION);
782
0
    ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_NO_RENEGOTIATION);
783
0
    return 0;
784
0
  }
785
786
48.0k
  if (msg.type != SSL3_MT_HELLO_REQUEST || CBS_len(&msg.body) != 0) {
787
5
    ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
788
5
    OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_HELLO_REQUEST);
789
5
    return 0;
790
5
  }
791
792
48.0k
  if (ssl->renegotiate_mode == ssl_renegotiate_ignore) {
793
0
    return 1;  // Ignore the HelloRequest.
794
0
  }
795
796
48.0k
  ssl->s3->renegotiate_pending = true;
797
48.0k
  if (ssl->renegotiate_mode == ssl_renegotiate_explicit) {
798
0
    return 1;  // Handle it later.
799
0
  }
800
801
48.0k
  if (!SSL_renegotiate(ssl)) {
802
0
    ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_NO_RENEGOTIATION);
803
0
    return 0;
804
0
  }
805
806
48.0k
  return 1;
807
48.0k
}
808
809
0
int SSL_process_quic_post_handshake(SSL *ssl) {
810
0
  ssl_reset_error_state(ssl);
811
812
0
  if (!SSL_is_quic(ssl) || SSL_in_init(ssl)) {
813
0
    OPENSSL_PUT_ERROR(SSL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
814
0
    return 0;
815
0
  }
816
817
  // Replay post-handshake message errors.
818
0
  if (!check_read_error(ssl)) {
819
0
    return 0;
820
0
  }
821
822
  // Process any buffered post-handshake messages.
823
0
  SSLMessage msg;
824
0
  while (ssl->method->get_message(ssl, &msg)) {
825
    // Handle the post-handshake message and try again.
826
0
    if (!ssl_do_post_handshake(ssl, msg)) {
827
0
      ssl_set_read_error(ssl);
828
0
      return 0;
829
0
    }
830
0
    ssl->method->next_message(ssl);
831
0
  }
832
833
0
  return 1;
834
0
}
835
836
41.8k
static int ssl_read_impl(SSL *ssl) {
837
41.8k
  ssl_reset_error_state(ssl);
838
839
41.8k
  if (ssl->do_handshake == nullptr) {
840
0
    OPENSSL_PUT_ERROR(SSL, SSL_R_UNINITIALIZED);
841
0
    return -1;
842
0
  }
843
844
  // Replay post-handshake message errors.
845
41.8k
  if (!check_read_error(ssl)) {
846
0
    return -1;
847
0
  }
848
849
482k
  while (ssl->s3->pending_app_data.empty()) {
850
446k
    if (ssl->s3->renegotiate_pending) {
851
0
      ssl->s3->rwstate = SSL_ERROR_WANT_RENEGOTIATE;
852
0
      return -1;
853
0
    }
854
855
    // If a read triggered a DTLS ACK or retransmit, resolve that before reading
856
    // more.
857
446k
    if (SSL_is_dtls(ssl)) {
858
184k
      int ret = ssl->method->flush(ssl);
859
184k
      if (ret <= 0) {
860
2
        return ret;
861
2
      }
862
184k
    }
863
864
    // Complete the current handshake, if any. False Start will cause
865
    // |SSL_do_handshake| to return mid-handshake, so this may require multiple
866
    // iterations.
867
493k
    while (!ssl_can_read(ssl)) {
868
48.0k
      int ret = SSL_do_handshake(ssl);
869
48.0k
      if (ret < 0) {
870
699
        return ret;
871
699
      }
872
47.3k
      if (ret == 0) {
873
543
        OPENSSL_PUT_ERROR(SSL, SSL_R_SSL_HANDSHAKE_FAILURE);
874
543
        return -1;
875
543
      }
876
47.3k
    }
877
878
    // Process any buffered post-handshake messages.
879
445k
    SSLMessage msg;
880
445k
    if (ssl->method->get_message(ssl, &msg)) {
881
      // If we received an interrupt in early read (EndOfEarlyData), loop again
882
      // for the handshake to process it.
883
90.0k
      if (SSL_in_init(ssl)) {
884
16
        ssl->s3->hs->can_early_read = false;
885
16
        continue;
886
16
      }
887
888
      // Handle the post-handshake message and try again.
889
90.0k
      if (!ssl_do_post_handshake(ssl, msg)) {
890
129
        ssl_set_read_error(ssl);
891
129
        return -1;
892
129
      }
893
89.9k
      ssl->method->next_message(ssl);
894
89.9k
      continue;  // Loop again. We may have begun a new handshake.
895
90.0k
    }
896
897
355k
    uint8_t alert = SSL_AD_DECODE_ERROR;
898
355k
    size_t consumed = 0;
899
355k
    auto ret = ssl_open_app_data(ssl, &ssl->s3->pending_app_data, &consumed,
900
355k
                                 &alert, ssl->s3->read_buffer.span());
901
355k
    bool retry;
902
355k
    int bio_ret = ssl_handle_open_record(ssl, &retry, ret, consumed, alert);
903
355k
    if (bio_ret <= 0) {
904
4.01k
      return bio_ret;
905
4.01k
    }
906
351k
    if (!retry) {
907
36.1k
      assert(!ssl->s3->pending_app_data.empty());
908
36.1k
      ssl->s3->key_update_count = 0;
909
36.1k
    }
910
351k
  }
911
912
36.4k
  return 1;
913
41.8k
}
914
915
41.8k
int SSL_read(SSL *ssl, void *buf, int num) {
916
41.8k
  int ret = SSL_peek(ssl, buf, num);
917
41.8k
  if (ret <= 0) {
918
5.38k
    return ret;
919
5.38k
  }
920
  // TODO(davidben): In DTLS, should the rest of the record be discarded?  DTLS
921
  // is not a stream. See https://crbug.com/boringssl/65.
922
36.4k
  ssl->s3->pending_app_data =
923
36.4k
      ssl->s3->pending_app_data.subspan(static_cast<size_t>(ret));
924
36.4k
  if (ssl->s3->pending_app_data.empty()) {
925
36.1k
    ssl->s3->read_buffer.DiscardConsumed();
926
36.1k
  }
927
36.4k
  return ret;
928
41.8k
}
929
930
41.8k
int SSL_peek(SSL *ssl, void *buf, int num) {
931
41.8k
  if (SSL_is_quic(ssl)) {
932
0
    OPENSSL_PUT_ERROR(SSL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
933
0
    return -1;
934
0
  }
935
936
41.8k
  int ret = ssl_read_impl(ssl);
937
41.8k
  if (ret <= 0) {
938
5.38k
    return ret;
939
5.38k
  }
940
36.4k
  if (num <= 0) {
941
0
    return num;
942
0
  }
943
36.4k
  size_t todo =
944
36.4k
      std::min(ssl->s3->pending_app_data.size(), static_cast<size_t>(num));
945
36.4k
  OPENSSL_memcpy(buf, ssl->s3->pending_app_data.data(), todo);
946
36.4k
  return static_cast<int>(todo);
947
36.4k
}
948
949
0
int SSL_write(SSL *ssl, const void *buf, int num) {
950
0
  ssl_reset_error_state(ssl);
951
952
0
  if (SSL_is_quic(ssl)) {
953
0
    OPENSSL_PUT_ERROR(SSL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
954
0
    return -1;
955
0
  }
956
957
0
  if (ssl->do_handshake == nullptr) {
958
0
    OPENSSL_PUT_ERROR(SSL, SSL_R_UNINITIALIZED);
959
0
    return -1;
960
0
  }
961
962
0
  int ret = 0;
963
0
  size_t bytes_written = 0;
964
0
  bool needs_handshake = false;
965
0
  do {
966
    // If necessary, complete the handshake implicitly.
967
0
    if (!ssl_can_write(ssl)) {
968
0
      ret = SSL_do_handshake(ssl);
969
0
      if (ret < 0) {
970
0
        return ret;
971
0
      }
972
0
      if (ret == 0) {
973
0
        OPENSSL_PUT_ERROR(SSL, SSL_R_SSL_HANDSHAKE_FAILURE);
974
0
        return -1;
975
0
      }
976
0
    }
977
978
0
    if (num < 0) {
979
0
      OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_LENGTH);
980
0
      return -1;
981
0
    }
982
0
    ret = ssl->method->write_app_data(
983
0
        ssl, &needs_handshake, &bytes_written,
984
0
        Span(static_cast<const uint8_t *>(buf), static_cast<size_t>(num)));
985
0
  } while (needs_handshake);
986
0
  return ret <= 0 ? ret : static_cast<int>(bytes_written);
987
0
}
988
989
0
int SSL_key_update(SSL *ssl, int request_type) {
990
0
  ssl_reset_error_state(ssl);
991
992
0
  if (ssl->do_handshake == nullptr) {
993
0
    OPENSSL_PUT_ERROR(SSL, SSL_R_UNINITIALIZED);
994
0
    return 0;
995
0
  }
996
997
0
  if (SSL_is_quic(ssl)) {
998
0
    OPENSSL_PUT_ERROR(SSL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
999
0
    return 0;
1000
0
  }
1001
1002
0
  if (!ssl->s3->initial_handshake_complete) {
1003
0
    OPENSSL_PUT_ERROR(SSL, SSL_R_HANDSHAKE_NOT_COMPLETE);
1004
0
    return 0;
1005
0
  }
1006
1007
0
  if (ssl_protocol_version(ssl) < TLS1_3_VERSION) {
1008
0
    OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_SSL_VERSION);
1009
0
    return 0;
1010
0
  }
1011
1012
0
  return tls13_add_key_update(ssl, request_type);
1013
0
}
1014
1015
0
int SSL_shutdown(SSL *ssl) {
1016
0
  ssl_reset_error_state(ssl);
1017
1018
0
  if (ssl->do_handshake == nullptr) {
1019
0
    OPENSSL_PUT_ERROR(SSL, SSL_R_UNINITIALIZED);
1020
0
    return -1;
1021
0
  }
1022
1023
  // If we are in the middle of a handshake, silently succeed. Consumers often
1024
  // call this function before |SSL_free|, whether the handshake succeeded or
1025
  // not. We assume the caller has already handled failed handshakes.
1026
0
  if (SSL_in_init(ssl)) {
1027
0
    return 1;
1028
0
  }
1029
1030
0
  if (ssl->quiet_shutdown) {
1031
    // Do nothing if configured not to send a close_notify.
1032
0
    ssl->s3->write_shutdown = ssl_shutdown_close_notify;
1033
0
    ssl->s3->read_shutdown = ssl_shutdown_close_notify;
1034
0
    return 1;
1035
0
  }
1036
1037
  // This function completes in two stages. It sends a close_notify and then it
1038
  // waits for a close_notify to come in. Perform exactly one action and return
1039
  // whether or not it succeeds.
1040
1041
0
  if (ssl->s3->write_shutdown != ssl_shutdown_close_notify) {
1042
    // Send a close_notify.
1043
0
    if (ssl_send_alert_impl(ssl, SSL3_AL_WARNING, SSL_AD_CLOSE_NOTIFY) <= 0) {
1044
0
      return -1;
1045
0
    }
1046
0
  } else if (ssl->s3->alert_dispatch) {
1047
    // Finish sending the close_notify.
1048
0
    if (ssl->method->dispatch_alert(ssl) <= 0) {
1049
0
      return -1;
1050
0
    }
1051
0
  } else if (ssl->s3->read_shutdown != ssl_shutdown_close_notify) {
1052
0
    if (SSL_is_dtls(ssl)) {
1053
      // Bidirectional shutdown doesn't make sense for an unordered
1054
      // transport. DTLS alerts also aren't delivered reliably, so we may even
1055
      // time out because the peer never received our close_notify. Report to
1056
      // the caller that the channel has fully shut down.
1057
0
      if (ssl->s3->read_shutdown == ssl_shutdown_error) {
1058
0
        ERR_restore_state(ssl->s3->read_error.get());
1059
0
        return -1;
1060
0
      }
1061
0
      ssl->s3->read_shutdown = ssl_shutdown_close_notify;
1062
0
    } else {
1063
      // Process records until an error, close_notify, or application data.
1064
0
      if (ssl_read_impl(ssl) > 0) {
1065
        // We received some unexpected application data.
1066
0
        OPENSSL_PUT_ERROR(SSL, SSL_R_APPLICATION_DATA_ON_SHUTDOWN);
1067
0
        return -1;
1068
0
      }
1069
0
      if (ssl->s3->read_shutdown != ssl_shutdown_close_notify) {
1070
0
        return -1;
1071
0
      }
1072
0
    }
1073
0
  }
1074
1075
  // Return 0 for unidirectional shutdown and 1 for bidirectional shutdown.
1076
0
  return ssl->s3->read_shutdown == ssl_shutdown_close_notify;
1077
0
}
1078
1079
0
int SSL_send_fatal_alert(SSL *ssl, uint8_t alert) {
1080
0
  if (ssl->s3->alert_dispatch) {
1081
0
    if (ssl->s3->send_alert[0] != SSL3_AL_FATAL ||
1082
0
        ssl->s3->send_alert[1] != alert) {
1083
      // We are already attempting to write a different alert.
1084
0
      OPENSSL_PUT_ERROR(SSL, SSL_R_PROTOCOL_IS_SHUTDOWN);
1085
0
      return -1;
1086
0
    }
1087
0
    return ssl->method->dispatch_alert(ssl);
1088
0
  }
1089
1090
0
  return ssl_send_alert_impl(ssl, SSL3_AL_FATAL, alert);
1091
0
}
1092
1093
int SSL_set_quic_transport_params(SSL *ssl, const uint8_t *params,
1094
0
                                  size_t params_len) {
1095
0
  return ssl->config &&
1096
0
         ssl->config->quic_transport_params.CopyFrom(Span(params, params_len));
1097
0
}
1098
1099
void SSL_get_peer_quic_transport_params(const SSL *ssl,
1100
                                        const uint8_t **out_params,
1101
0
                                        size_t *out_params_len) {
1102
0
  *out_params = ssl->s3->peer_quic_transport_params.data();
1103
0
  *out_params_len = ssl->s3->peer_quic_transport_params.size();
1104
0
}
1105
1106
int SSL_set_quic_early_data_context(SSL *ssl, const uint8_t *context,
1107
0
                                    size_t context_len) {
1108
0
  return ssl->config && ssl->config->quic_early_data_context.CopyFrom(
1109
0
                            Span(context, context_len));
1110
0
}
1111
1112
12
void SSL_CTX_set_early_data_enabled(SSL_CTX *ctx, int enabled) {
1113
12
  ctx->enable_early_data = !!enabled;
1114
12
}
1115
1116
0
void SSL_set_early_data_enabled(SSL *ssl, int enabled) {
1117
0
  ssl->enable_early_data = !!enabled;
1118
0
}
1119
1120
11.4k
int SSL_in_early_data(const SSL *ssl) {
1121
11.4k
  if (ssl->s3->hs == nullptr) {
1122
11.1k
    return 0;
1123
11.1k
  }
1124
306
  return ssl->s3->hs->in_early_data;
1125
11.4k
}
1126
1127
0
int SSL_early_data_accepted(const SSL *ssl) {
1128
0
  return ssl->s3->early_data_accepted;
1129
0
}
1130
1131
0
void SSL_reset_early_data_reject(SSL *ssl) {
1132
0
  SSL_HANDSHAKE *hs = ssl->s3->hs.get();
1133
0
  if (hs == nullptr ||  //
1134
0
      hs->wait != ssl_hs_early_data_rejected) {
1135
0
    abort();
1136
0
  }
1137
1138
0
  hs->wait = ssl_hs_ok;
1139
0
  hs->in_early_data = false;
1140
0
  hs->early_session.reset();
1141
1142
  // Discard any unfinished writes from the perspective of |SSL_write|'s
1143
  // retry. The handshake will transparently flush out the pending record
1144
  // (discarded by the server) to keep the framing correct.
1145
0
  ssl->s3->pending_write = {};
1146
0
}
1147
1148
0
enum ssl_early_data_reason_t SSL_get_early_data_reason(const SSL *ssl) {
1149
0
  return ssl->s3->early_data_reason;
1150
0
}
1151
1152
0
const char *SSL_early_data_reason_string(enum ssl_early_data_reason_t reason) {
1153
0
  switch (reason) {
1154
0
    case ssl_early_data_unknown:
1155
0
      return "unknown";
1156
0
    case ssl_early_data_disabled:
1157
0
      return "disabled";
1158
0
    case ssl_early_data_accepted:
1159
0
      return "accepted";
1160
0
    case ssl_early_data_protocol_version:
1161
0
      return "protocol_version";
1162
0
    case ssl_early_data_peer_declined:
1163
0
      return "peer_declined";
1164
0
    case ssl_early_data_no_session_offered:
1165
0
      return "no_session_offered";
1166
0
    case ssl_early_data_session_not_resumed:
1167
0
      return "session_not_resumed";
1168
0
    case ssl_early_data_unsupported_for_session:
1169
0
      return "unsupported_for_session";
1170
0
    case ssl_early_data_hello_retry_request:
1171
0
      return "hello_retry_request";
1172
0
    case ssl_early_data_alpn_mismatch:
1173
0
      return "alpn_mismatch";
1174
0
    case ssl_early_data_channel_id:
1175
0
      return "channel_id";
1176
0
    case ssl_early_data_ticket_age_skew:
1177
0
      return "ticket_age_skew";
1178
0
    case ssl_early_data_quic_parameter_mismatch:
1179
0
      return "quic_parameter_mismatch";
1180
0
    case ssl_early_data_alps_mismatch:
1181
0
      return "alps_mismatch";
1182
0
  }
1183
1184
0
  return nullptr;
1185
0
}
1186
1187
0
static int bio_retry_reason_to_error(int reason) {
1188
0
  switch (reason) {
1189
0
    case BIO_RR_CONNECT:
1190
0
      return SSL_ERROR_WANT_CONNECT;
1191
0
    case BIO_RR_ACCEPT:
1192
0
      return SSL_ERROR_WANT_ACCEPT;
1193
0
    default:
1194
0
      return SSL_ERROR_SYSCALL;
1195
0
  }
1196
0
}
1197
1198
38.9k
int SSL_get_error(const SSL *ssl, int ret_code) {
1199
38.9k
  if (ret_code > 0) {
1200
0
    return SSL_ERROR_NONE;
1201
0
  }
1202
1203
  // Make things return SSL_ERROR_SYSCALL when doing SSL_do_handshake etc,
1204
  // where we do encode the error
1205
38.9k
  uint32_t err = ERR_peek_error();
1206
38.9k
  if (err != 0) {
1207
30.8k
    if (ERR_GET_LIB(err) == ERR_LIB_SYS) {
1208
0
      return SSL_ERROR_SYSCALL;
1209
0
    }
1210
30.8k
    return SSL_ERROR_SSL;
1211
30.8k
  }
1212
1213
8.16k
  if (ret_code == 0) {
1214
0
    if (ssl->s3->rwstate == SSL_ERROR_ZERO_RETURN) {
1215
0
      return SSL_ERROR_ZERO_RETURN;
1216
0
    }
1217
    // An EOF was observed which violates the protocol, and the underlying
1218
    // transport does not participate in the error queue. Bubble up to the
1219
    // caller.
1220
0
    return SSL_ERROR_SYSCALL;
1221
0
  }
1222
1223
8.16k
  switch (ssl->s3->rwstate) {
1224
0
    case SSL_ERROR_PENDING_SESSION:
1225
0
    case SSL_ERROR_PENDING_CERTIFICATE:
1226
6
    case SSL_ERROR_HANDOFF:
1227
6
    case SSL_ERROR_HANDBACK:
1228
6
    case SSL_ERROR_WANT_X509_LOOKUP:
1229
6
    case SSL_ERROR_WANT_PRIVATE_KEY_OPERATION:
1230
6
    case SSL_ERROR_PENDING_TICKET:
1231
6
    case SSL_ERROR_EARLY_DATA_REJECTED:
1232
6
    case SSL_ERROR_WANT_CERTIFICATE_VERIFY:
1233
6
    case SSL_ERROR_WANT_RENEGOTIATE:
1234
6
    case SSL_ERROR_HANDSHAKE_HINTS_READY:
1235
6
      return ssl->s3->rwstate;
1236
1237
8.02k
    case SSL_ERROR_WANT_READ: {
1238
8.02k
      if (SSL_is_quic(ssl)) {
1239
0
        return SSL_ERROR_WANT_READ;
1240
0
      }
1241
8.02k
      BIO *bio = SSL_get_rbio(ssl);
1242
8.02k
      if (BIO_should_read(bio)) {
1243
0
        return SSL_ERROR_WANT_READ;
1244
0
      }
1245
1246
8.02k
      if (BIO_should_write(bio)) {
1247
        // TODO(davidben): OpenSSL historically checked for writes on the read
1248
        // BIO. Can this be removed?
1249
0
        return SSL_ERROR_WANT_WRITE;
1250
0
      }
1251
1252
8.02k
      if (BIO_should_io_special(bio)) {
1253
0
        return bio_retry_reason_to_error(BIO_get_retry_reason(bio));
1254
0
      }
1255
1256
8.02k
      break;
1257
8.02k
    }
1258
1259
8.02k
    case SSL_ERROR_WANT_WRITE: {
1260
0
      BIO *bio = SSL_get_wbio(ssl);
1261
0
      if (BIO_should_write(bio)) {
1262
0
        return SSL_ERROR_WANT_WRITE;
1263
0
      }
1264
1265
0
      if (BIO_should_read(bio)) {
1266
        // TODO(davidben): OpenSSL historically checked for reads on the write
1267
        // BIO. Can this be removed?
1268
0
        return SSL_ERROR_WANT_READ;
1269
0
      }
1270
1271
0
      if (BIO_should_io_special(bio)) {
1272
0
        return bio_retry_reason_to_error(BIO_get_retry_reason(bio));
1273
0
      }
1274
1275
0
      break;
1276
0
    }
1277
8.16k
  }
1278
1279
8.15k
  return SSL_ERROR_SYSCALL;
1280
8.16k
}
1281
1282
0
const char *SSL_error_description(int err) {
1283
0
  switch (err) {
1284
0
    case SSL_ERROR_NONE:
1285
0
      return "NONE";
1286
0
    case SSL_ERROR_SSL:
1287
0
      return "SSL";
1288
0
    case SSL_ERROR_WANT_READ:
1289
0
      return "WANT_READ";
1290
0
    case SSL_ERROR_WANT_WRITE:
1291
0
      return "WANT_WRITE";
1292
0
    case SSL_ERROR_WANT_X509_LOOKUP:
1293
0
      return "WANT_X509_LOOKUP";
1294
0
    case SSL_ERROR_SYSCALL:
1295
0
      return "SYSCALL";
1296
0
    case SSL_ERROR_ZERO_RETURN:
1297
0
      return "ZERO_RETURN";
1298
0
    case SSL_ERROR_WANT_CONNECT:
1299
0
      return "WANT_CONNECT";
1300
0
    case SSL_ERROR_WANT_ACCEPT:
1301
0
      return "WANT_ACCEPT";
1302
0
    case SSL_ERROR_PENDING_SESSION:
1303
0
      return "PENDING_SESSION";
1304
0
    case SSL_ERROR_PENDING_CERTIFICATE:
1305
0
      return "PENDING_CERTIFICATE";
1306
0
    case SSL_ERROR_WANT_PRIVATE_KEY_OPERATION:
1307
0
      return "WANT_PRIVATE_KEY_OPERATION";
1308
0
    case SSL_ERROR_PENDING_TICKET:
1309
0
      return "PENDING_TICKET";
1310
0
    case SSL_ERROR_EARLY_DATA_REJECTED:
1311
0
      return "EARLY_DATA_REJECTED";
1312
0
    case SSL_ERROR_WANT_CERTIFICATE_VERIFY:
1313
0
      return "WANT_CERTIFICATE_VERIFY";
1314
0
    case SSL_ERROR_HANDOFF:
1315
0
      return "HANDOFF";
1316
0
    case SSL_ERROR_HANDBACK:
1317
0
      return "HANDBACK";
1318
0
    case SSL_ERROR_WANT_RENEGOTIATE:
1319
0
      return "WANT_RENEGOTIATE";
1320
0
    case SSL_ERROR_HANDSHAKE_HINTS_READY:
1321
0
      return "HANDSHAKE_HINTS_READY";
1322
0
    default:
1323
0
      return nullptr;
1324
0
  }
1325
0
}
1326
1327
3.79k
uint32_t SSL_CTX_set_options(SSL_CTX *ctx, uint32_t options) {
1328
3.79k
  ctx->options |= options;
1329
3.79k
  return ctx->options;
1330
3.79k
}
1331
1332
1.93k
uint32_t SSL_CTX_clear_options(SSL_CTX *ctx, uint32_t options) {
1333
1.93k
  ctx->options &= ~options;
1334
1.93k
  return ctx->options;
1335
1.93k
}
1336
1337
3.30k
uint32_t SSL_CTX_get_options(const SSL_CTX *ctx) { return ctx->options; }
1338
1339
0
uint32_t SSL_set_options(SSL *ssl, uint32_t options) {
1340
0
  ssl->options |= options;
1341
0
  return ssl->options;
1342
0
}
1343
1344
0
uint32_t SSL_clear_options(SSL *ssl, uint32_t options) {
1345
0
  ssl->options &= ~options;
1346
0
  return ssl->options;
1347
0
}
1348
1349
142k
uint32_t SSL_get_options(const SSL *ssl) { return ssl->options; }
1350
1351
3.09k
uint32_t SSL_CTX_set_mode(SSL_CTX *ctx, uint32_t mode) {
1352
3.09k
  ctx->mode |= mode;
1353
3.09k
  return ctx->mode;
1354
3.09k
}
1355
1356
3.36k
uint32_t SSL_CTX_clear_mode(SSL_CTX *ctx, uint32_t mode) {
1357
3.36k
  ctx->mode &= ~mode;
1358
3.36k
  return ctx->mode;
1359
3.36k
}
1360
1361
2.19k
uint32_t SSL_CTX_get_mode(const SSL_CTX *ctx) { return ctx->mode; }
1362
1363
0
uint32_t SSL_set_mode(SSL *ssl, uint32_t mode) {
1364
0
  ssl->mode |= mode;
1365
0
  return ssl->mode;
1366
0
}
1367
1368
0
uint32_t SSL_clear_mode(SSL *ssl, uint32_t mode) {
1369
0
  ssl->mode &= ~mode;
1370
0
  return ssl->mode;
1371
0
}
1372
1373
49.8k
uint32_t SSL_get_mode(const SSL *ssl) { return ssl->mode; }
1374
1375
0
void SSL_CTX_set0_buffer_pool(SSL_CTX *ctx, CRYPTO_BUFFER_POOL *pool) {
1376
0
  ctx->pool = pool;
1377
0
}
1378
1379
int SSL_get_tls_unique(const SSL *ssl, uint8_t *out, size_t *out_len,
1380
0
                       size_t max_out) {
1381
0
  *out_len = 0;
1382
0
  OPENSSL_memset(out, 0, max_out);
1383
1384
  // tls-unique is not defined for TLS 1.3.
1385
0
  if (!ssl->s3->initial_handshake_complete ||
1386
0
      ssl_protocol_version(ssl) >= TLS1_3_VERSION) {
1387
0
    return 0;
1388
0
  }
1389
1390
  // The tls-unique value is the first Finished message in the handshake, which
1391
  // is the client's in a full handshake and the server's for a resumption. See
1392
  // https://tools.ietf.org/html/rfc5929#section-3.1.
1393
0
  Span<const uint8_t> finished = ssl->s3->previous_client_finished;
1394
0
  if (ssl->session != nullptr) {
1395
    // tls-unique is broken for resumed sessions unless EMS is used.
1396
0
    if (!ssl->session->extended_master_secret) {
1397
0
      return 0;
1398
0
    }
1399
0
    finished = ssl->s3->previous_server_finished;
1400
0
  }
1401
1402
0
  *out_len = finished.size();
1403
0
  if (finished.size() > max_out) {
1404
0
    *out_len = max_out;
1405
0
  }
1406
1407
0
  OPENSSL_memcpy(out, finished.data(), *out_len);
1408
0
  return 1;
1409
0
}
1410
1411
static int set_session_id_context(CERT *cert, const uint8_t *sid_ctx,
1412
1.79k
                                  size_t sid_ctx_len) {
1413
1.79k
  if (!cert->sid_ctx.TryCopyFrom(Span(sid_ctx, sid_ctx_len))) {
1414
1.00k
    OPENSSL_PUT_ERROR(SSL, SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG);
1415
1.00k
    return 0;
1416
1.00k
  }
1417
1418
793
  return 1;
1419
1.79k
}
1420
1421
int SSL_CTX_set_session_id_context(SSL_CTX *ctx, const uint8_t *sid_ctx,
1422
1.79k
                                   size_t sid_ctx_len) {
1423
1.79k
  return set_session_id_context(ctx->cert.get(), sid_ctx, sid_ctx_len);
1424
1.79k
}
1425
1426
int SSL_set_session_id_context(SSL *ssl, const uint8_t *sid_ctx,
1427
0
                               size_t sid_ctx_len) {
1428
0
  if (!ssl->config) {
1429
0
    return 0;
1430
0
  }
1431
0
  return set_session_id_context(ssl->config->cert.get(), sid_ctx, sid_ctx_len);
1432
0
}
1433
1434
0
const uint8_t *SSL_get0_session_id_context(const SSL *ssl, size_t *out_len) {
1435
0
  if (!ssl->config) {
1436
0
    assert(ssl->config);
1437
0
    *out_len = 0;
1438
0
    return nullptr;
1439
0
  }
1440
0
  *out_len = ssl->config->cert->sid_ctx.size();
1441
0
  return ssl->config->cert->sid_ctx.data();
1442
0
}
1443
1444
0
int SSL_get_fd(const SSL *ssl) { return SSL_get_rfd(ssl); }
1445
1446
0
int SSL_get_rfd(const SSL *ssl) {
1447
0
  int ret = -1;
1448
0
  BIO *b = BIO_find_type(SSL_get_rbio(ssl), BIO_TYPE_DESCRIPTOR);
1449
0
  if (b != nullptr) {
1450
0
    BIO_get_fd(b, &ret);
1451
0
  }
1452
0
  return ret;
1453
0
}
1454
1455
0
int SSL_get_wfd(const SSL *ssl) {
1456
0
  int ret = -1;
1457
0
  BIO *b = BIO_find_type(SSL_get_wbio(ssl), BIO_TYPE_DESCRIPTOR);
1458
0
  if (b != nullptr) {
1459
0
    BIO_get_fd(b, &ret);
1460
0
  }
1461
0
  return ret;
1462
0
}
1463
1464
#if !defined(OPENSSL_NO_SOCK)
1465
0
int SSL_set_fd(SSL *ssl, int fd) {
1466
0
  BIO *bio = BIO_new(BIO_s_socket());
1467
0
  if (bio == nullptr) {
1468
0
    OPENSSL_PUT_ERROR(SSL, ERR_R_BUF_LIB);
1469
0
    return 0;
1470
0
  }
1471
0
  BIO_set_fd(bio, fd, BIO_NOCLOSE);
1472
0
  SSL_set_bio(ssl, bio, bio);
1473
0
  return 1;
1474
0
}
1475
1476
0
int SSL_set_wfd(SSL *ssl, int fd) {
1477
0
  BIO *rbio = SSL_get_rbio(ssl);
1478
0
  if (rbio == nullptr || BIO_method_type(rbio) != BIO_TYPE_SOCKET ||
1479
0
      BIO_get_fd(rbio, nullptr) != fd) {
1480
0
    BIO *bio = BIO_new(BIO_s_socket());
1481
0
    if (bio == nullptr) {
1482
0
      OPENSSL_PUT_ERROR(SSL, ERR_R_BUF_LIB);
1483
0
      return 0;
1484
0
    }
1485
0
    BIO_set_fd(bio, fd, BIO_NOCLOSE);
1486
0
    SSL_set0_wbio(ssl, bio);
1487
0
  } else {
1488
    // Copy the rbio over to the wbio.
1489
0
    BIO_up_ref(rbio);
1490
0
    SSL_set0_wbio(ssl, rbio);
1491
0
  }
1492
1493
0
  return 1;
1494
0
}
1495
1496
0
int SSL_set_rfd(SSL *ssl, int fd) {
1497
0
  BIO *wbio = SSL_get_wbio(ssl);
1498
0
  if (wbio == nullptr || BIO_method_type(wbio) != BIO_TYPE_SOCKET ||
1499
0
      BIO_get_fd(wbio, nullptr) != fd) {
1500
0
    BIO *bio = BIO_new(BIO_s_socket());
1501
0
    if (bio == nullptr) {
1502
0
      OPENSSL_PUT_ERROR(SSL, ERR_R_BUF_LIB);
1503
0
      return 0;
1504
0
    }
1505
0
    BIO_set_fd(bio, fd, BIO_NOCLOSE);
1506
0
    SSL_set0_rbio(ssl, bio);
1507
0
  } else {
1508
    // Copy the wbio over to the rbio.
1509
0
    BIO_up_ref(wbio);
1510
0
    SSL_set0_rbio(ssl, wbio);
1511
0
  }
1512
0
  return 1;
1513
0
}
1514
#endif  // !OPENSSL_NO_SOCK
1515
1516
0
static size_t copy_finished(void *out, size_t out_len, Span<const uint8_t> in) {
1517
0
  if (out_len > in.size()) {
1518
0
    out_len = in.size();
1519
0
  }
1520
0
  OPENSSL_memcpy(out, in.data(), out_len);
1521
0
  return in.size();
1522
0
}
1523
1524
0
size_t SSL_get_finished(const SSL *ssl, void *buf, size_t count) {
1525
0
  if (!ssl->s3->initial_handshake_complete ||
1526
0
      ssl_protocol_version(ssl) >= TLS1_3_VERSION) {
1527
0
    return 0;
1528
0
  }
1529
1530
0
  if (ssl->server) {
1531
0
    return copy_finished(buf, count, ssl->s3->previous_server_finished);
1532
0
  }
1533
1534
0
  return copy_finished(buf, count, ssl->s3->previous_client_finished);
1535
0
}
1536
1537
0
size_t SSL_get_peer_finished(const SSL *ssl, void *buf, size_t count) {
1538
0
  if (!ssl->s3->initial_handshake_complete ||
1539
0
      ssl_protocol_version(ssl) >= TLS1_3_VERSION) {
1540
0
    return 0;
1541
0
  }
1542
1543
0
  if (ssl->server) {
1544
0
    return copy_finished(buf, count, ssl->s3->previous_client_finished);
1545
0
  }
1546
1547
0
  return copy_finished(buf, count, ssl->s3->previous_server_finished);
1548
0
}
1549
1550
0
int SSL_get_verify_mode(const SSL *ssl) {
1551
0
  if (!ssl->config) {
1552
0
    assert(ssl->config);
1553
0
    return -1;
1554
0
  }
1555
0
  return ssl->config->verify_mode;
1556
0
}
1557
1558
0
int SSL_get_extms_support(const SSL *ssl) {
1559
  // TLS 1.3 does not require extended master secret and always reports as
1560
  // supporting it.
1561
0
  if (ssl->s3->version == 0) {
1562
0
    return 0;
1563
0
  }
1564
0
  if (ssl_protocol_version(ssl) >= TLS1_3_VERSION) {
1565
0
    return 1;
1566
0
  }
1567
1568
  // If the initial handshake completed, query the established session.
1569
0
  if (ssl->s3->established_session != nullptr) {
1570
0
    return ssl->s3->established_session->extended_master_secret;
1571
0
  }
1572
1573
  // Otherwise, query the in-progress handshake.
1574
0
  if (ssl->s3->hs != nullptr) {
1575
0
    return ssl->s3->hs->extended_master_secret;
1576
0
  }
1577
0
  assert(0);
1578
0
  return 0;
1579
0
}
1580
1581
0
int SSL_CTX_get_read_ahead(const SSL_CTX *ctx) { return 0; }
1582
1583
0
int SSL_get_read_ahead(const SSL *ssl) { return 0; }
1584
1585
0
int SSL_CTX_set_read_ahead(SSL_CTX *ctx, int yes) { return 1; }
1586
1587
0
int SSL_set_read_ahead(SSL *ssl, int yes) { return 1; }
1588
1589
0
int SSL_pending(const SSL *ssl) {
1590
0
  return static_cast<int>(ssl->s3->pending_app_data.size());
1591
0
}
1592
1593
0
int SSL_has_pending(const SSL *ssl) {
1594
0
  return SSL_pending(ssl) != 0 || !ssl->s3->read_buffer.empty();
1595
0
}
1596
1597
4.29k
static bool has_cert_and_key(const SSL_CREDENTIAL *cred) {
1598
  // TODO(davidben): If |cred->key_method| is set, that should be fine too.
1599
4.29k
  if (cred->privkey == nullptr) {
1600
1.18k
    OPENSSL_PUT_ERROR(SSL, SSL_R_NO_PRIVATE_KEY_ASSIGNED);
1601
1.18k
    return false;
1602
1.18k
  }
1603
1604
3.10k
  if (cred->chain == nullptr ||
1605
2.90k
      sk_CRYPTO_BUFFER_value(cred->chain.get(), 0) == nullptr) {
1606
404
    OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CERTIFICATE_ASSIGNED);
1607
404
    return false;
1608
404
  }
1609
1610
2.70k
  return true;
1611
3.10k
}
1612
1613
4.29k
int SSL_CTX_check_private_key(const SSL_CTX *ctx) {
1614
  // There is no need to actually check consistency because inconsistent values
1615
  // can never be configured.
1616
4.29k
  return has_cert_and_key(ctx->cert->legacy_credential.get());
1617
4.29k
}
1618
1619
0
int SSL_check_private_key(const SSL *ssl) {
1620
0
  if (!ssl->config) {
1621
0
    return 0;
1622
0
  }
1623
1624
  // There is no need to actually check consistency because inconsistent values
1625
  // can never be configured.
1626
0
  return has_cert_and_key(ssl->config->cert->legacy_credential.get());
1627
0
}
1628
1629
0
long SSL_get_default_timeout(const SSL *ssl) {
1630
0
  return SSL_DEFAULT_SESSION_TIMEOUT;
1631
0
}
1632
1633
48.0k
int SSL_renegotiate(SSL *ssl) {
1634
  // Caller-initiated renegotiation is not supported.
1635
48.0k
  if (!ssl->s3->renegotiate_pending) {
1636
0
    OPENSSL_PUT_ERROR(SSL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
1637
0
    return 0;
1638
0
  }
1639
1640
48.0k
  if (!ssl_can_renegotiate(ssl)) {
1641
0
    OPENSSL_PUT_ERROR(SSL, SSL_R_NO_RENEGOTIATION);
1642
0
    return 0;
1643
0
  }
1644
1645
  // We should not have told the caller to release the private key.
1646
48.0k
  assert(!SSL_can_release_private_key(ssl));
1647
1648
  // Renegotiation is only supported at quiescent points in the application
1649
  // protocol, namely in HTTPS, just before reading the HTTP response.
1650
  // Require the record-layer be idle and avoid complexities of sending a
1651
  // handshake record while an application_data record is being written.
1652
48.0k
  if (!ssl->s3->write_buffer.empty() ||
1653
48.0k
      ssl->s3->write_shutdown != ssl_shutdown_none) {
1654
0
    OPENSSL_PUT_ERROR(SSL, SSL_R_NO_RENEGOTIATION);
1655
0
    return 0;
1656
0
  }
1657
1658
  // Begin a new handshake.
1659
48.0k
  if (ssl->s3->hs != nullptr) {
1660
0
    OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
1661
0
    return 0;
1662
0
  }
1663
48.0k
  ssl->s3->hs = ssl_handshake_new(ssl);
1664
48.0k
  if (ssl->s3->hs == nullptr) {
1665
0
    return 0;
1666
0
  }
1667
1668
48.0k
  ssl->s3->renegotiate_pending = false;
1669
48.0k
  ssl->s3->total_renegotiations++;
1670
48.0k
  return 1;
1671
48.0k
}
1672
1673
0
int SSL_renegotiate_pending(SSL *ssl) {
1674
0
  return SSL_in_init(ssl) && ssl->s3->initial_handshake_complete;
1675
0
}
1676
1677
0
int SSL_total_renegotiations(const SSL *ssl) {
1678
0
  return ssl->s3->total_renegotiations;
1679
0
}
1680
1681
2.14k
size_t SSL_CTX_get_max_cert_list(const SSL_CTX *ctx) {
1682
2.14k
  return ctx->max_cert_list;
1683
2.14k
}
1684
1685
1.79k
void SSL_CTX_set_max_cert_list(SSL_CTX *ctx, size_t max_cert_list) {
1686
1.79k
  if (max_cert_list > kMaxHandshakeSize) {
1687
1.37k
    max_cert_list = kMaxHandshakeSize;
1688
1.37k
  }
1689
1.79k
  ctx->max_cert_list = (uint32_t)max_cert_list;
1690
1.79k
}
1691
1692
0
size_t SSL_get_max_cert_list(const SSL *ssl) { return ssl->max_cert_list; }
1693
1694
0
void SSL_set_max_cert_list(SSL *ssl, size_t max_cert_list) {
1695
0
  if (max_cert_list > kMaxHandshakeSize) {
1696
0
    max_cert_list = kMaxHandshakeSize;
1697
0
  }
1698
0
  ssl->max_cert_list = (uint32_t)max_cert_list;
1699
0
}
1700
1701
2.80k
int SSL_CTX_set_max_send_fragment(SSL_CTX *ctx, size_t max_send_fragment) {
1702
2.80k
  if (max_send_fragment < 512) {
1703
283
    max_send_fragment = 512;
1704
283
  }
1705
2.80k
  if (max_send_fragment > SSL3_RT_MAX_PLAIN_LENGTH) {
1706
2.50k
    max_send_fragment = SSL3_RT_MAX_PLAIN_LENGTH;
1707
2.50k
  }
1708
2.80k
  ctx->max_send_fragment = (uint16_t)max_send_fragment;
1709
1710
2.80k
  return 1;
1711
2.80k
}
1712
1713
0
int SSL_set_max_send_fragment(SSL *ssl, size_t max_send_fragment) {
1714
0
  if (max_send_fragment < 512) {
1715
0
    max_send_fragment = 512;
1716
0
  }
1717
0
  if (max_send_fragment > SSL3_RT_MAX_PLAIN_LENGTH) {
1718
0
    max_send_fragment = SSL3_RT_MAX_PLAIN_LENGTH;
1719
0
  }
1720
0
  ssl->max_send_fragment = (uint16_t)max_send_fragment;
1721
1722
0
  return 1;
1723
0
}
1724
1725
0
int SSL_set_mtu(SSL *ssl, unsigned mtu) {
1726
0
  if (!SSL_is_dtls(ssl) || mtu < dtls1_min_mtu()) {
1727
0
    return 0;
1728
0
  }
1729
0
  ssl->d1->mtu = mtu;
1730
0
  return 1;
1731
0
}
1732
1733
0
int SSL_get_secure_renegotiation_support(const SSL *ssl) {
1734
0
  if (ssl->s3->version == 0) {
1735
0
    return 0;
1736
0
  }
1737
0
  return ssl_protocol_version(ssl) >= TLS1_3_VERSION ||
1738
0
         ssl->s3->send_connection_binding;
1739
0
}
1740
1741
1.78k
size_t SSL_CTX_sess_number(const SSL_CTX *ctx) {
1742
1.78k
  MutexReadLock lock(const_cast<CRYPTO_MUTEX *>(&ctx->lock));
1743
1.78k
  return lh_SSL_SESSION_num_items(ctx->sessions);
1744
1.78k
}
1745
1746
1.87k
unsigned long SSL_CTX_sess_set_cache_size(SSL_CTX *ctx, unsigned long size) {
1747
1.87k
  unsigned long ret = ctx->session_cache_size;
1748
1.87k
  ctx->session_cache_size = size;
1749
1.87k
  return ret;
1750
1.87k
}
1751
1752
129k
unsigned long SSL_CTX_sess_get_cache_size(const SSL_CTX *ctx) {
1753
129k
  return ctx->session_cache_size;
1754
129k
}
1755
1756
0
int SSL_CTX_set_session_cache_mode(SSL_CTX *ctx, int mode) {
1757
0
  int ret = ctx->session_cache_mode;
1758
0
  ctx->session_cache_mode = mode;
1759
0
  return ret;
1760
0
}
1761
1762
0
int SSL_CTX_get_session_cache_mode(const SSL_CTX *ctx) {
1763
0
  return ctx->session_cache_mode;
1764
0
}
1765
1766
1767
0
int SSL_CTX_get_tlsext_ticket_keys(SSL_CTX *ctx, void *out, size_t len) {
1768
0
  if (out == nullptr) {
1769
0
    return 48;
1770
0
  }
1771
0
  if (len != 48) {
1772
0
    OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_TICKET_KEYS_LENGTH);
1773
0
    return 0;
1774
0
  }
1775
1776
  // The default ticket keys are initialized lazily. Trigger a key
1777
  // rotation to initialize them.
1778
0
  if (!ssl_ctx_rotate_ticket_encryption_key(ctx)) {
1779
0
    return 0;
1780
0
  }
1781
1782
0
  uint8_t *out_bytes = reinterpret_cast<uint8_t *>(out);
1783
0
  MutexReadLock lock(&ctx->lock);
1784
0
  OPENSSL_memcpy(out_bytes, ctx->ticket_key_current->name, 16);
1785
0
  OPENSSL_memcpy(out_bytes + 16, ctx->ticket_key_current->hmac_key, 16);
1786
0
  OPENSSL_memcpy(out_bytes + 32, ctx->ticket_key_current->aes_key, 16);
1787
0
  return 1;
1788
0
}
1789
1790
1.28k
int SSL_CTX_set_tlsext_ticket_keys(SSL_CTX *ctx, const void *in, size_t len) {
1791
1.28k
  if (in == nullptr) {
1792
253
    return 48;
1793
253
  }
1794
1.03k
  if (len != 48) {
1795
643
    OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_TICKET_KEYS_LENGTH);
1796
643
    return 0;
1797
643
  }
1798
387
  auto key = MakeUnique<TicketKey>();
1799
387
  if (!key) {
1800
0
    return 0;
1801
0
  }
1802
387
  const uint8_t *in_bytes = reinterpret_cast<const uint8_t *>(in);
1803
387
  OPENSSL_memcpy(key->name, in_bytes, 16);
1804
387
  OPENSSL_memcpy(key->hmac_key, in_bytes + 16, 16);
1805
387
  OPENSSL_memcpy(key->aes_key, in_bytes + 32, 16);
1806
  // Disable automatic key rotation for manually-configured keys. This is now
1807
  // the caller's responsibility.
1808
387
  key->next_rotation_tv_sec = 0;
1809
387
  ctx->ticket_key_current = std::move(key);
1810
387
  ctx->ticket_key_prev.reset();
1811
387
  return 1;
1812
387
}
1813
1814
int SSL_CTX_set_tlsext_ticket_key_cb(
1815
    SSL_CTX *ctx,
1816
    int (*callback)(SSL *ssl, uint8_t *key_name, uint8_t *iv,
1817
0
                    EVP_CIPHER_CTX *ctx, HMAC_CTX *hmac_ctx, int encrypt)) {
1818
0
  ctx->ticket_key_cb = callback;
1819
0
  return 1;
1820
0
}
1821
1822
3.99k
static bool check_no_duplicates(Span<const uint16_t> list) {
1823
3.99k
  if (list.size() < 2) {
1824
810
    return true;
1825
810
  }
1826
6.89k
  for (size_t i = 0; i < list.size() - 1; ++i) {
1827
11.5k
    for (size_t j = i + 1; j < list.size(); ++j) {
1828
7.88k
      if (list[i] == list[j]) {
1829
1.29k
        OPENSSL_PUT_ERROR(SSL, SSL_R_DUPLICATE_GROUP);
1830
1.29k
        return false;
1831
1.29k
      }
1832
7.88k
    }
1833
5.00k
  }
1834
1.89k
  return true;
1835
3.18k
}
1836
1837
3.70k
static bool check_group_ids(Span<const uint16_t> group_ids) {
1838
7.84k
  for (uint16_t group_id : group_ids) {
1839
7.84k
    if (ssl_group_id_to_nid(group_id) == NID_undef) {
1840
1.72k
      OPENSSL_PUT_ERROR(SSL, SSL_R_UNSUPPORTED_ELLIPTIC_CURVE);
1841
1.72k
      return false;
1842
1.72k
    }
1843
7.84k
  }
1844
1.97k
  return check_no_duplicates(group_ids);
1845
3.70k
}
1846
1847
// validate_key_shares returns whether the `requested_key_shares` are free of
1848
// duplicates and are a (correctly ordered) subsequence of the supported
1849
// `groups`.
1850
static bool validate_key_shares(Span<const uint16_t> requested_key_shares,
1851
0
                                Span<const uint16_t> groups) {
1852
0
  if (!check_no_duplicates(requested_key_shares)) {
1853
0
    return false;
1854
0
  }
1855
0
  if (requested_key_shares.size() > groups.size()) {
1856
0
    return false;
1857
0
  }
1858
0
  size_t key_shares_idx = 0u, groups_idx = 0u;
1859
0
  while (key_shares_idx < requested_key_shares.size() &&
1860
0
         groups_idx < groups.size()) {
1861
0
    if (requested_key_shares[key_shares_idx] == groups[groups_idx++]) {
1862
0
      ++key_shares_idx;
1863
0
    }
1864
0
  }
1865
0
  return key_shares_idx == requested_key_shares.size();
1866
0
}
1867
1868
0
static void clear_key_shares_if_invalid(SSL_CONFIG *config) {
1869
0
  if (!config->client_key_share_selections) {
1870
0
    return;
1871
0
  }
1872
0
  if (!validate_key_shares(*(config->client_key_share_selections),
1873
0
                           config->supported_group_list)) {
1874
0
    config->client_key_share_selections.reset();
1875
0
  }
1876
0
}
1877
1878
int SSL_CTX_set1_group_ids(SSL_CTX *ctx, const uint16_t *group_ids,
1879
3.70k
                           size_t num_group_ids) {
1880
3.70k
  auto span = Span(group_ids, num_group_ids);
1881
3.70k
  if (span.empty()) {
1882
1.35k
    span = DefaultSupportedGroupIds();
1883
1.35k
  }
1884
3.70k
  return check_group_ids(span) && ctx->supported_group_list.CopyFrom(span);
1885
3.70k
}
1886
1887
int SSL_set1_group_ids(SSL *ssl, const uint16_t *group_ids,
1888
0
                       size_t num_group_ids) {
1889
0
  if (!ssl->config) {
1890
0
    return 0;
1891
0
  }
1892
0
  auto span = Span(group_ids, num_group_ids);
1893
0
  if (span.empty()) {
1894
0
    span = DefaultSupportedGroupIds();
1895
0
  }
1896
0
  if (check_group_ids(span) &&
1897
0
      ssl->config->supported_group_list.CopyFrom(span)) {
1898
0
    clear_key_shares_if_invalid(ssl->config.get());
1899
0
    return 1;
1900
0
  }
1901
0
  return 0;
1902
0
}
1903
1904
static bool ssl_nids_to_group_ids(Array<uint16_t> *out_group_ids,
1905
5.89k
                                  Span<const int> nids) {
1906
5.89k
  if (nids.empty()) {
1907
1.40k
    return out_group_ids->CopyFrom(DefaultSupportedGroupIds());
1908
1.40k
  }
1909
4.48k
  Array<uint16_t> group_ids;
1910
4.48k
  if (!group_ids.InitForOverwrite(nids.size())) {
1911
0
    return false;
1912
0
  }
1913
1914
6.85k
  for (size_t i = 0; i < nids.size(); i++) {
1915
5.92k
    if (!ssl_nid_to_group_id(&group_ids[i], nids[i])) {
1916
3.56k
      OPENSSL_PUT_ERROR(SSL, SSL_R_UNSUPPORTED_ELLIPTIC_CURVE);
1917
3.56k
      return false;
1918
3.56k
    }
1919
5.92k
  }
1920
927
  if (!check_no_duplicates(group_ids)) {
1921
446
    return false;
1922
446
  }
1923
1924
481
  *out_group_ids = std::move(group_ids);
1925
481
  return true;
1926
927
}
1927
1928
5.89k
int SSL_CTX_set1_groups(SSL_CTX *ctx, const int *groups, size_t num_groups) {
1929
5.89k
  return ssl_nids_to_group_ids(&ctx->supported_group_list,
1930
5.89k
                               Span(groups, num_groups));
1931
5.89k
}
1932
1933
0
int SSL_set1_groups(SSL *ssl, const int *groups, size_t num_groups) {
1934
0
  if (!ssl->config) {
1935
0
    return 0;
1936
0
  }
1937
0
  if (ssl_nids_to_group_ids(&ssl->config->supported_group_list,
1938
0
                            Span(groups, num_groups))) {
1939
0
    clear_key_shares_if_invalid(ssl->config.get());
1940
0
    return 1;
1941
0
  }
1942
0
  return 0;
1943
0
}
1944
1945
static bool ssl_str_to_group_ids(Array<uint16_t> *out_group_ids,
1946
4.92k
                                 const char *str) {
1947
  // Count the number of groups in the list.
1948
4.92k
  size_t count = 0;
1949
4.92k
  const char *ptr = str, *col;
1950
14.9k
  do {
1951
14.9k
    col = strchr(ptr, ':');
1952
14.9k
    count++;
1953
14.9k
    if (col) {
1954
10.0k
      ptr = col + 1;
1955
10.0k
    }
1956
14.9k
  } while (col);
1957
1958
4.92k
  Array<uint16_t> group_ids;
1959
4.92k
  if (!group_ids.InitForOverwrite(count)) {
1960
0
    return false;
1961
0
  }
1962
1963
4.92k
  size_t i = 0;
1964
4.92k
  ptr = str;
1965
6.31k
  do {
1966
6.31k
    col = strchr(ptr, ':');
1967
6.31k
    if (!ssl_name_to_group_id(&group_ids[i++], ptr,
1968
6.31k
                              col ? (size_t)(col - ptr) : strlen(ptr))) {
1969
3.82k
      OPENSSL_PUT_ERROR(SSL, SSL_R_UNSUPPORTED_ELLIPTIC_CURVE);
1970
3.82k
      return false;
1971
3.82k
    }
1972
2.49k
    if (col) {
1973
1.39k
      ptr = col + 1;
1974
1.39k
    }
1975
2.49k
  } while (col);
1976
1977
4.92k
  assert(i == count);
1978
1.09k
  if (!check_no_duplicates(group_ids)) {
1979
445
    return false;
1980
445
  }
1981
653
  *out_group_ids = std::move(group_ids);
1982
653
  return true;
1983
1.09k
}
1984
1985
4.92k
int SSL_CTX_set1_groups_list(SSL_CTX *ctx, const char *groups) {
1986
4.92k
  return ssl_str_to_group_ids(&ctx->supported_group_list, groups);
1987
4.92k
}
1988
1989
0
int SSL_set1_groups_list(SSL *ssl, const char *groups) {
1990
0
  if (!ssl->config) {
1991
0
    return 0;
1992
0
  }
1993
0
  if (ssl_str_to_group_ids(&ssl->config->supported_group_list, groups)) {
1994
0
    clear_key_shares_if_invalid(ssl->config.get());
1995
0
    return 1;
1996
0
  }
1997
0
  return 0;
1998
0
}
1999
2000
0
uint16_t SSL_get_group_id(const SSL *ssl) {
2001
0
  SSL_SESSION *session = SSL_get_session(ssl);
2002
0
  if (session == nullptr) {
2003
0
    return 0;
2004
0
  }
2005
2006
0
  return session->group_id;
2007
0
}
2008
2009
0
int SSL_get_negotiated_group(const SSL *ssl) {
2010
0
  uint16_t group_id = SSL_get_group_id(ssl);
2011
0
  if (group_id == 0) {
2012
0
    return NID_undef;
2013
0
  }
2014
0
  return ssl_group_id_to_nid(group_id);
2015
0
}
2016
2017
int SSL_set1_client_key_shares(SSL *ssl, const uint16_t *group_ids,
2018
0
                               size_t num_group_ids) {
2019
0
  if (!ssl->config) {
2020
0
    return 0;
2021
0
  }
2022
0
  auto requested_key_shares = Span(group_ids, num_group_ids);
2023
0
  if (!validate_key_shares(requested_key_shares,
2024
0
                           ssl->config->supported_group_list)) {
2025
0
    return 0;
2026
0
  }
2027
2028
0
  assert(requested_key_shares.size() <= kNumNamedGroups);
2029
0
  ssl->config->client_key_share_selections.emplace();
2030
0
  ssl->config->client_key_share_selections->CopyFrom(requested_key_shares);
2031
0
  return 1;
2032
0
}
2033
2034
int SSL_set1_server_supported_groups_hint(SSL *ssl,
2035
                                          const uint16_t *server_groups,
2036
0
                                          size_t num_server_groups) {
2037
0
  if (!ssl->config) {
2038
0
    return 0;
2039
0
  }
2040
0
  auto span = Span(server_groups, num_server_groups);
2041
0
  return ssl->config->server_supported_groups_hint.CopyFrom(span);
2042
0
}
2043
2044
0
int SSL_CTX_set_tmp_dh(SSL_CTX *ctx, const DH *dh) { return 1; }
2045
2046
0
int SSL_set_tmp_dh(SSL *ssl, const DH *dh) { return 1; }
2047
2048
0
STACK_OF(SSL_CIPHER) *SSL_CTX_get_ciphers(const SSL_CTX *ctx) {
2049
0
  return ctx->cipher_list->ciphers.get();
2050
0
}
2051
2052
0
int SSL_CTX_cipher_in_group(const SSL_CTX *ctx, size_t i) {
2053
0
  if (i >= sk_SSL_CIPHER_num(ctx->cipher_list->ciphers.get())) {
2054
0
    return 0;
2055
0
  }
2056
0
  return ctx->cipher_list->in_group_flags[i];
2057
0
}
2058
2059
120k
STACK_OF(SSL_CIPHER) *SSL_get_ciphers(const SSL *ssl) {
2060
120k
  if (ssl == nullptr) {
2061
0
    return nullptr;
2062
0
  }
2063
120k
  if (ssl->config == nullptr) {
2064
0
    assert(ssl->config);
2065
0
    return nullptr;
2066
0
  }
2067
2068
120k
  return ssl->config->cipher_list ? ssl->config->cipher_list->ciphers.get()
2069
120k
                                  : ssl->ctx->cipher_list->ciphers.get();
2070
120k
}
2071
2072
0
const char *SSL_get_cipher_list(const SSL *ssl, int n) {
2073
0
  if (ssl == nullptr) {
2074
0
    return nullptr;
2075
0
  }
2076
2077
0
  STACK_OF(SSL_CIPHER) *sk = SSL_get_ciphers(ssl);
2078
0
  if (sk == nullptr || n < 0 || (size_t)n >= sk_SSL_CIPHER_num(sk)) {
2079
0
    return nullptr;
2080
0
  }
2081
2082
0
  const SSL_CIPHER *c = sk_SSL_CIPHER_value(sk, n);
2083
0
  if (c == nullptr) {
2084
0
    return nullptr;
2085
0
  }
2086
2087
0
  return c->name;
2088
0
}
2089
2090
13.9k
int SSL_CTX_set_cipher_list(SSL_CTX *ctx, const char *str) {
2091
13.9k
  const bool has_aes_hw = ctx->aes_hw_override ? ctx->aes_hw_override_value
2092
13.9k
                                               : EVP_has_aes_hardware();
2093
13.9k
  return ssl_create_cipher_list(&ctx->cipher_list, has_aes_hw, str,
2094
13.9k
                                false /* not strict */);
2095
13.9k
}
2096
2097
12.5k
int SSL_CTX_set_strict_cipher_list(SSL_CTX *ctx, const char *str) {
2098
12.5k
  const bool has_aes_hw = ctx->aes_hw_override ? ctx->aes_hw_override_value
2099
12.5k
                                               : EVP_has_aes_hardware();
2100
12.5k
  return ssl_create_cipher_list(&ctx->cipher_list, has_aes_hw, str,
2101
12.5k
                                true /* strict */);
2102
12.5k
}
2103
2104
0
int SSL_set_cipher_list(SSL *ssl, const char *str) {
2105
0
  if (!ssl->config) {
2106
0
    return 0;
2107
0
  }
2108
0
  const bool has_aes_hw = ssl->config->aes_hw_override
2109
0
                              ? ssl->config->aes_hw_override_value
2110
0
                              : EVP_has_aes_hardware();
2111
0
  return ssl_create_cipher_list(&ssl->config->cipher_list, has_aes_hw, str,
2112
0
                                false /* not strict */);
2113
0
}
2114
2115
0
int SSL_set_strict_cipher_list(SSL *ssl, const char *str) {
2116
0
  if (!ssl->config) {
2117
0
    return 0;
2118
0
  }
2119
0
  const bool has_aes_hw = ssl->config->aes_hw_override
2120
0
                              ? ssl->config->aes_hw_override_value
2121
0
                              : EVP_has_aes_hardware();
2122
0
  return ssl_create_cipher_list(&ssl->config->cipher_list, has_aes_hw, str,
2123
0
                                true /* strict */);
2124
0
}
2125
2126
0
const char *SSL_get_servername(const SSL *ssl, const int type) {
2127
0
  if (type != TLSEXT_NAMETYPE_host_name) {
2128
0
    return nullptr;
2129
0
  }
2130
2131
  // Historically, |SSL_get_servername| was also the configuration getter
2132
  // corresponding to |SSL_set_tlsext_host_name|.
2133
0
  if (ssl->hostname != nullptr) {
2134
0
    return ssl->hostname.get();
2135
0
  }
2136
2137
0
  return ssl->s3->hostname.get();
2138
0
}
2139
2140
0
int SSL_get_servername_type(const SSL *ssl) {
2141
0
  if (SSL_get_servername(ssl, TLSEXT_NAMETYPE_host_name) == nullptr) {
2142
0
    return -1;
2143
0
  }
2144
0
  return TLSEXT_NAMETYPE_host_name;
2145
0
}
2146
2147
void SSL_CTX_set_custom_verify(
2148
    SSL_CTX *ctx, int mode,
2149
0
    enum ssl_verify_result_t (*callback)(SSL *ssl, uint8_t *out_alert)) {
2150
0
  ctx->verify_mode = mode;
2151
0
  ctx->custom_verify_callback = callback;
2152
0
}
2153
2154
void SSL_set_custom_verify(
2155
    SSL *ssl, int mode,
2156
0
    enum ssl_verify_result_t (*callback)(SSL *ssl, uint8_t *out_alert)) {
2157
0
  if (!ssl->config) {
2158
0
    return;
2159
0
  }
2160
0
  ssl->config->verify_mode = mode;
2161
0
  ssl->config->custom_verify_callback = callback;
2162
0
}
2163
2164
4.89k
void SSL_CTX_enable_signed_cert_timestamps(SSL_CTX *ctx) {
2165
4.89k
  ctx->signed_cert_timestamps_enabled = true;
2166
4.89k
}
2167
2168
0
void SSL_enable_signed_cert_timestamps(SSL *ssl) {
2169
0
  if (!ssl->config) {
2170
0
    return;
2171
0
  }
2172
0
  ssl->config->signed_cert_timestamps_enabled = true;
2173
0
}
2174
2175
3.01k
void SSL_CTX_enable_ocsp_stapling(SSL_CTX *ctx) {
2176
3.01k
  ctx->ocsp_stapling_enabled = true;
2177
3.01k
}
2178
2179
0
void SSL_enable_ocsp_stapling(SSL *ssl) {
2180
0
  if (!ssl->config) {
2181
0
    return;
2182
0
  }
2183
0
  ssl->config->ocsp_stapling_enabled = true;
2184
0
}
2185
2186
void SSL_get0_signed_cert_timestamp_list(const SSL *ssl, const uint8_t **out,
2187
0
                                         size_t *out_len) {
2188
0
  SSL_SESSION *session = SSL_get_session(ssl);
2189
0
  if (ssl->server || !session || !session->signed_cert_timestamp_list) {
2190
0
    *out_len = 0;
2191
0
    *out = nullptr;
2192
0
    return;
2193
0
  }
2194
2195
0
  *out = CRYPTO_BUFFER_data(session->signed_cert_timestamp_list.get());
2196
0
  *out_len = CRYPTO_BUFFER_len(session->signed_cert_timestamp_list.get());
2197
0
}
2198
2199
void SSL_get0_ocsp_response(const SSL *ssl, const uint8_t **out,
2200
0
                            size_t *out_len) {
2201
0
  SSL_SESSION *session = SSL_get_session(ssl);
2202
0
  if (ssl->server || !session || !session->ocsp_response) {
2203
0
    *out_len = 0;
2204
0
    *out = nullptr;
2205
0
    return;
2206
0
  }
2207
2208
0
  *out = CRYPTO_BUFFER_data(session->ocsp_response.get());
2209
0
  *out_len = CRYPTO_BUFFER_len(session->ocsp_response.get());
2210
0
}
2211
2212
14.2k
int SSL_set_tlsext_host_name(SSL *ssl, const char *name) {
2213
14.2k
  ssl->hostname.reset();
2214
14.2k
  if (name == nullptr) {
2215
0
    return 1;
2216
0
  }
2217
2218
14.2k
  size_t len = strlen(name);
2219
14.2k
  if (len == 0 || len > TLSEXT_MAXLEN_host_name) {
2220
0
    OPENSSL_PUT_ERROR(SSL, SSL_R_SSL3_EXT_INVALID_SERVERNAME);
2221
0
    return 0;
2222
0
  }
2223
14.2k
  ssl->hostname.reset(OPENSSL_strdup(name));
2224
14.2k
  if (ssl->hostname == nullptr) {
2225
0
    return 0;
2226
0
  }
2227
14.2k
  return 1;
2228
14.2k
}
2229
2230
int SSL_CTX_set_tlsext_servername_callback(
2231
0
    SSL_CTX *ctx, int (*callback)(SSL *ssl, int *out_alert, void *arg)) {
2232
0
  ctx->servername_callback = callback;
2233
0
  return 1;
2234
0
}
2235
2236
0
int SSL_CTX_set_tlsext_servername_arg(SSL_CTX *ctx, void *arg) {
2237
0
  ctx->servername_arg = arg;
2238
0
  return 1;
2239
0
}
2240
2241
int SSL_select_next_proto(uint8_t **out, uint8_t *out_len, const uint8_t *peer,
2242
                          unsigned peer_len, const uint8_t *supported,
2243
0
                          unsigned supported_len) {
2244
0
  *out = nullptr;
2245
0
  *out_len = 0;
2246
2247
  // Both |peer| and |supported| must be valid protocol lists, but |peer| may be
2248
  // empty in NPN.
2249
0
  auto peer_span = Span(peer, peer_len);
2250
0
  auto supported_span = Span(supported, supported_len);
2251
0
  if ((!peer_span.empty() && !ssl_is_valid_alpn_list(peer_span)) ||
2252
0
      !ssl_is_valid_alpn_list(supported_span)) {
2253
0
    return OPENSSL_NPN_NO_OVERLAP;
2254
0
  }
2255
2256
  // For each protocol in peer preference order, see if we support it.
2257
0
  CBS cbs = peer_span, proto;
2258
0
  while (CBS_len(&cbs) != 0) {
2259
0
    if (!CBS_get_u8_length_prefixed(&cbs, &proto) || CBS_len(&proto) == 0) {
2260
0
      return OPENSSL_NPN_NO_OVERLAP;
2261
0
    }
2262
2263
0
    if (ssl_alpn_list_contains_protocol(Span(supported, supported_len),
2264
0
                                        proto)) {
2265
      // This function is not const-correct for compatibility with existing
2266
      // callers.
2267
0
      *out = const_cast<uint8_t *>(CBS_data(&proto));
2268
      // A u8 length prefix will fit in |uint8_t|.
2269
0
      *out_len = static_cast<uint8_t>(CBS_len(&proto));
2270
0
      return OPENSSL_NPN_NEGOTIATED;
2271
0
    }
2272
0
  }
2273
2274
  // There's no overlap between our protocols and the peer's list. In ALPN, the
2275
  // caller is expected to fail the connection with no_application_protocol. In
2276
  // NPN, the caller is expected to opportunistically select the first protocol.
2277
  // See draft-agl-tls-nextprotoneg-04, section 6.
2278
0
  cbs = supported_span;
2279
0
  if (!CBS_get_u8_length_prefixed(&cbs, &proto) || CBS_len(&proto) == 0) {
2280
0
    return OPENSSL_NPN_NO_OVERLAP;
2281
0
  }
2282
2283
  // See above.
2284
0
  *out = const_cast<uint8_t *>(CBS_data(&proto));
2285
0
  *out_len = static_cast<uint8_t>(CBS_len(&proto));
2286
0
  return OPENSSL_NPN_NO_OVERLAP;
2287
0
}
2288
2289
void SSL_get0_next_proto_negotiated(const SSL *ssl, const uint8_t **out_data,
2290
0
                                    unsigned *out_len) {
2291
  // NPN protocols have one-byte lengths, so they must fit in |unsigned|.
2292
0
  assert(ssl->s3->next_proto_negotiated.size() <= UINT_MAX);
2293
0
  *out_data = ssl->s3->next_proto_negotiated.data();
2294
0
  *out_len = static_cast<unsigned>(ssl->s3->next_proto_negotiated.size());
2295
0
}
2296
2297
void SSL_CTX_set_next_protos_advertised_cb(
2298
    SSL_CTX *ctx,
2299
    int (*cb)(SSL *ssl, const uint8_t **out, unsigned *out_len, void *arg),
2300
12
    void *arg) {
2301
12
  ctx->next_protos_advertised_cb = cb;
2302
12
  ctx->next_protos_advertised_cb_arg = arg;
2303
12
}
2304
2305
void SSL_CTX_set_next_proto_select_cb(SSL_CTX *ctx,
2306
                                      int (*cb)(SSL *ssl, uint8_t **out,
2307
                                                uint8_t *out_len,
2308
                                                const uint8_t *in,
2309
                                                unsigned in_len, void *arg),
2310
12
                                      void *arg) {
2311
12
  ctx->next_proto_select_cb = cb;
2312
12
  ctx->next_proto_select_cb_arg = arg;
2313
12
}
2314
2315
int SSL_CTX_set_alpn_protos(SSL_CTX *ctx, const uint8_t *protos,
2316
1.47k
                            size_t protos_len) {
2317
  // Note this function's return value is backwards.
2318
1.47k
  auto span = Span(protos, protos_len);
2319
1.47k
  if (!span.empty() && !ssl_is_valid_alpn_list(span)) {
2320
1.02k
    OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_ALPN_PROTOCOL_LIST);
2321
1.02k
    return 1;
2322
1.02k
  }
2323
451
  return ctx->alpn_client_proto_list.CopyFrom(span) ? 0 : 1;
2324
1.47k
}
2325
2326
0
int SSL_set_alpn_protos(SSL *ssl, const uint8_t *protos, size_t protos_len) {
2327
  // Note this function's return value is backwards.
2328
0
  if (!ssl->config) {
2329
0
    return 1;
2330
0
  }
2331
0
  auto span = Span(protos, protos_len);
2332
0
  if (!span.empty() && !ssl_is_valid_alpn_list(span)) {
2333
0
    OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_ALPN_PROTOCOL_LIST);
2334
0
    return 1;
2335
0
  }
2336
0
  return ssl->config->alpn_client_proto_list.CopyFrom(span) ? 0 : 1;
2337
0
}
2338
2339
void SSL_CTX_set_alpn_select_cb(SSL_CTX *ctx,
2340
                                int (*cb)(SSL *ssl, const uint8_t **out,
2341
                                          uint8_t *out_len, const uint8_t *in,
2342
                                          unsigned in_len, void *arg),
2343
12
                                void *arg) {
2344
12
  ctx->alpn_select_cb = cb;
2345
12
  ctx->alpn_select_cb_arg = arg;
2346
12
}
2347
2348
void SSL_get0_alpn_selected(const SSL *ssl, const uint8_t **out_data,
2349
0
                            unsigned *out_len) {
2350
0
  Span<const uint8_t> protocol;
2351
0
  if (SSL_in_early_data(ssl) && !ssl->server) {
2352
0
    protocol = ssl->s3->hs->early_session->early_alpn;
2353
0
  } else {
2354
0
    protocol = ssl->s3->alpn_selected;
2355
0
  }
2356
  // ALPN protocols have one-byte lengths, so they must fit in |unsigned|.
2357
0
  assert(protocol.size() < UINT_MAX);
2358
0
  *out_data = protocol.data();
2359
0
  *out_len = static_cast<unsigned>(protocol.size());
2360
0
}
2361
2362
0
void SSL_CTX_set_allow_unknown_alpn_protos(SSL_CTX *ctx, int enabled) {
2363
0
  ctx->allow_unknown_alpn_protos = !!enabled;
2364
0
}
2365
2366
int SSL_add_application_settings(SSL *ssl, const uint8_t *proto,
2367
                                 size_t proto_len, const uint8_t *settings,
2368
0
                                 size_t settings_len) {
2369
0
  if (!ssl->config) {
2370
0
    return 0;
2371
0
  }
2372
0
  ALPSConfig config;
2373
0
  if (!config.protocol.CopyFrom(Span(proto, proto_len)) ||
2374
0
      !config.settings.CopyFrom(Span(settings, settings_len)) ||
2375
0
      !ssl->config->alps_configs.Push(std::move(config))) {
2376
0
    return 0;
2377
0
  }
2378
0
  return 1;
2379
0
}
2380
2381
void SSL_get0_peer_application_settings(const SSL *ssl,
2382
                                        const uint8_t **out_data,
2383
0
                                        size_t *out_len) {
2384
0
  const SSL_SESSION *session = SSL_get_session(ssl);
2385
0
  Span<const uint8_t> settings =
2386
0
      session ? session->peer_application_settings : Span<const uint8_t>();
2387
0
  *out_data = settings.data();
2388
0
  *out_len = settings.size();
2389
0
}
2390
2391
0
int SSL_has_application_settings(const SSL *ssl) {
2392
0
  const SSL_SESSION *session = SSL_get_session(ssl);
2393
0
  return session && session->has_application_settings;
2394
0
}
2395
2396
0
void SSL_set_alps_use_new_codepoint(SSL *ssl, int use_new) {
2397
0
  if (!ssl->config) {
2398
0
    return;
2399
0
  }
2400
0
  ssl->config->alps_use_new_codepoint = !!use_new;
2401
0
}
2402
2403
int SSL_CTX_add_cert_compression_alg(SSL_CTX *ctx, uint16_t alg_id,
2404
                                     ssl_cert_compression_func_t compress,
2405
0
                                     ssl_cert_decompression_func_t decompress) {
2406
0
  assert(compress != nullptr || decompress != nullptr);
2407
2408
0
  for (const auto &alg : ctx->cert_compression_algs) {
2409
0
    if (alg.alg_id == alg_id) {
2410
0
      return 0;
2411
0
    }
2412
0
  }
2413
2414
0
  CertCompressionAlg alg;
2415
0
  alg.alg_id = alg_id;
2416
0
  alg.compress = compress;
2417
0
  alg.decompress = decompress;
2418
0
  return ctx->cert_compression_algs.Push(alg);
2419
0
}
2420
2421
12
void SSL_CTX_set_tls_channel_id_enabled(SSL_CTX *ctx, int enabled) {
2422
12
  ctx->channel_id_enabled = !!enabled;
2423
12
}
2424
2425
0
int SSL_CTX_enable_tls_channel_id(SSL_CTX *ctx) {
2426
0
  SSL_CTX_set_tls_channel_id_enabled(ctx, 1);
2427
0
  return 1;
2428
0
}
2429
2430
0
void SSL_set_tls_channel_id_enabled(SSL *ssl, int enabled) {
2431
0
  if (!ssl->config) {
2432
0
    return;
2433
0
  }
2434
0
  ssl->config->channel_id_enabled = !!enabled;
2435
0
}
2436
2437
0
int SSL_enable_tls_channel_id(SSL *ssl) {
2438
0
  SSL_set_tls_channel_id_enabled(ssl, 1);
2439
0
  return 1;
2440
0
}
2441
2442
12
int SSL_CTX_set1_tls_channel_id(SSL_CTX *ctx, EVP_PKEY *private_key) {
2443
12
  if (EVP_PKEY_get_ec_curve_nid(private_key) != NID_X9_62_prime256v1) {
2444
0
    OPENSSL_PUT_ERROR(SSL, SSL_R_CHANNEL_ID_NOT_P256);
2445
0
    return 0;
2446
0
  }
2447
2448
12
  ctx->channel_id_private = UpRef(private_key);
2449
12
  return 1;
2450
12
}
2451
2452
0
int SSL_set1_tls_channel_id(SSL *ssl, EVP_PKEY *private_key) {
2453
0
  if (!ssl->config) {
2454
0
    return 0;
2455
0
  }
2456
0
  if (EVP_PKEY_get_ec_curve_nid(private_key) != NID_X9_62_prime256v1) {
2457
0
    OPENSSL_PUT_ERROR(SSL, SSL_R_CHANNEL_ID_NOT_P256);
2458
0
    return 0;
2459
0
  }
2460
2461
0
  ssl->config->channel_id_private = UpRef(private_key);
2462
0
  return 1;
2463
0
}
2464
2465
0
size_t SSL_get_tls_channel_id(SSL *ssl, uint8_t *out, size_t max_out) {
2466
0
  if (!ssl->s3->channel_id_valid) {
2467
0
    return 0;
2468
0
  }
2469
0
  OPENSSL_memcpy(out, ssl->s3->channel_id, (max_out < 64) ? max_out : 64);
2470
0
  return 64;
2471
0
}
2472
2473
0
size_t SSL_get0_certificate_types(const SSL *ssl, const uint8_t **out_types) {
2474
0
  Span<const uint8_t> types;
2475
0
  if (!ssl->server && ssl->s3->hs != nullptr) {
2476
0
    types = ssl->s3->hs->certificate_types;
2477
0
  }
2478
0
  *out_types = types.data();
2479
0
  return types.size();
2480
0
}
2481
2482
size_t SSL_get0_peer_verify_algorithms(const SSL *ssl,
2483
0
                                       const uint16_t **out_sigalgs) {
2484
0
  Span<const uint16_t> sigalgs;
2485
0
  if (ssl->s3->hs != nullptr) {
2486
0
    sigalgs = ssl->s3->hs->peer_sigalgs;
2487
0
  }
2488
0
  *out_sigalgs = sigalgs.data();
2489
0
  return sigalgs.size();
2490
0
}
2491
2492
size_t SSL_get0_peer_delegation_algorithms(const SSL *ssl,
2493
0
                                           const uint16_t **out_sigalgs) {
2494
0
  Span<const uint16_t> sigalgs;
2495
0
  if (ssl->s3->hs != nullptr) {
2496
0
    sigalgs = ssl->s3->hs->peer_delegated_credential_sigalgs;
2497
0
  }
2498
0
  *out_sigalgs = sigalgs.data();
2499
0
  return sigalgs.size();
2500
0
}
2501
2502
0
EVP_PKEY *SSL_get_privatekey(const SSL *ssl) {
2503
0
  if (!ssl->config) {
2504
0
    assert(ssl->config);
2505
0
    return nullptr;
2506
0
  }
2507
0
  return ssl->config->cert->legacy_credential->privkey.get();
2508
0
}
2509
2510
1.45k
EVP_PKEY *SSL_CTX_get0_privatekey(const SSL_CTX *ctx) {
2511
1.45k
  return ctx->cert->legacy_credential->privkey.get();
2512
1.45k
}
2513
2514
0
const SSL_CIPHER *SSL_get_current_cipher(const SSL *ssl) {
2515
0
  const SSL_SESSION *session = SSL_get_session(ssl);
2516
0
  return session == nullptr ? nullptr : session->cipher;
2517
0
}
2518
2519
0
int SSL_session_reused(const SSL *ssl) {
2520
0
  return ssl->s3->session_reused || SSL_in_early_data(ssl);
2521
0
}
2522
2523
0
const COMP_METHOD *SSL_get_current_compression(SSL *ssl) { return nullptr; }
2524
2525
0
const COMP_METHOD *SSL_get_current_expansion(SSL *ssl) { return nullptr; }
2526
2527
0
int SSL_get_server_tmp_key(SSL *ssl, EVP_PKEY **out_key) { return 0; }
2528
2529
84.0k
void SSL_CTX_set_quiet_shutdown(SSL_CTX *ctx, int mode) {
2530
84.0k
  ctx->quiet_shutdown = (mode != 0);
2531
84.0k
}
2532
2533
5.07k
int SSL_CTX_get_quiet_shutdown(const SSL_CTX *ctx) {
2534
5.07k
  return ctx->quiet_shutdown;
2535
5.07k
}
2536
2537
0
void SSL_set_quiet_shutdown(SSL *ssl, int mode) {
2538
0
  ssl->quiet_shutdown = (mode != 0);
2539
0
}
2540
2541
0
int SSL_get_quiet_shutdown(const SSL *ssl) { return ssl->quiet_shutdown; }
2542
2543
0
void SSL_set_shutdown(SSL *ssl, int mode) {
2544
  // It is an error to clear any bits that have already been set. (We can't try
2545
  // to get a second close_notify or send two.)
2546
0
  assert((SSL_get_shutdown(ssl) & mode) == SSL_get_shutdown(ssl));
2547
2548
0
  if (mode & SSL_RECEIVED_SHUTDOWN &&
2549
0
      ssl->s3->read_shutdown == ssl_shutdown_none) {
2550
0
    ssl->s3->read_shutdown = ssl_shutdown_close_notify;
2551
0
  }
2552
2553
0
  if (mode & SSL_SENT_SHUTDOWN &&
2554
0
      ssl->s3->write_shutdown == ssl_shutdown_none) {
2555
0
    ssl->s3->write_shutdown = ssl_shutdown_close_notify;
2556
0
  }
2557
0
}
2558
2559
0
int SSL_get_shutdown(const SSL *ssl) {
2560
0
  int ret = 0;
2561
0
  if (ssl->s3->read_shutdown != ssl_shutdown_none) {
2562
    // Historically, OpenSSL set |SSL_RECEIVED_SHUTDOWN| on both close_notify
2563
    // and fatal alert.
2564
0
    ret |= SSL_RECEIVED_SHUTDOWN;
2565
0
  }
2566
0
  if (ssl->s3->write_shutdown == ssl_shutdown_close_notify) {
2567
    // Historically, OpenSSL set |SSL_SENT_SHUTDOWN| on only close_notify.
2568
0
    ret |= SSL_SENT_SHUTDOWN;
2569
0
  }
2570
0
  return ret;
2571
0
}
2572
2573
0
SSL_CTX *SSL_get_SSL_CTX(const SSL *ssl) { return ssl->ctx.get(); }
2574
2575
0
SSL_CTX *SSL_set_SSL_CTX(SSL *ssl, SSL_CTX *ctx) {
2576
0
  if (!ssl->config) {
2577
0
    return nullptr;
2578
0
  }
2579
0
  if (ssl->ctx.get() == ctx) {
2580
0
    return ssl->ctx.get();
2581
0
  }
2582
2583
  // One cannot change the X.509 callbacks during a connection.
2584
0
  if (ssl->ctx->x509_method != ctx->x509_method) {
2585
0
    assert(0);
2586
0
    return nullptr;
2587
0
  }
2588
2589
0
  UniquePtr<CERT> new_cert = ssl_cert_dup(ctx->cert.get());
2590
0
  if (!new_cert) {
2591
0
    return nullptr;
2592
0
  }
2593
2594
0
  ssl->config->cert = std::move(new_cert);
2595
0
  ssl->ctx = UpRef(ctx);
2596
0
  ssl->enable_early_data = ssl->ctx->enable_early_data;
2597
2598
0
  return ssl->ctx.get();
2599
0
}
2600
2601
void SSL_set_info_callback(SSL *ssl,
2602
0
                           void (*cb)(const SSL *ssl, int type, int value)) {
2603
0
  ssl->info_callback = cb;
2604
0
}
2605
2606
void (*SSL_get_info_callback(const SSL *ssl))(const SSL *ssl, int type,
2607
0
                                              int value) {
2608
0
  return ssl->info_callback;
2609
0
}
2610
2611
0
int SSL_state(const SSL *ssl) {
2612
0
  return SSL_in_init(ssl) ? SSL_ST_INIT : SSL_ST_OK;
2613
0
}
2614
2615
0
void SSL_set_state(SSL *ssl, int state) {}
2616
2617
0
char *SSL_get_shared_ciphers(const SSL *ssl, char *buf, int len) {
2618
0
  if (len <= 0) {
2619
0
    return nullptr;
2620
0
  }
2621
0
  buf[0] = '\0';
2622
0
  return buf;
2623
0
}
2624
2625
int SSL_get_shared_sigalgs(SSL *ssl, int idx, int *psign, int *phash,
2626
0
                           int *psignandhash, uint8_t *rsig, uint8_t *rhash) {
2627
0
  return 0;
2628
0
}
2629
2630
0
int SSL_CTX_set_quic_method(SSL_CTX *ctx, const SSL_QUIC_METHOD *quic_method) {
2631
0
  if (ctx->method->is_dtls) {
2632
0
    return 0;
2633
0
  }
2634
0
  ctx->quic_method = quic_method;
2635
0
  return 1;
2636
0
}
2637
2638
0
int SSL_set_quic_method(SSL *ssl, const SSL_QUIC_METHOD *quic_method) {
2639
0
  if (ssl->method->is_dtls) {
2640
0
    return 0;
2641
0
  }
2642
0
  ssl->quic_method = quic_method;
2643
0
  return 1;
2644
0
}
2645
2646
int SSL_get_ex_new_index(long argl, void *argp, CRYPTO_EX_unused *unused,
2647
0
                         CRYPTO_EX_dup *dup_unused, CRYPTO_EX_free *free_func) {
2648
0
  return CRYPTO_get_ex_new_index_ex(&g_ex_data_class_ssl, argl, argp,
2649
0
                                    free_func);
2650
0
}
2651
2652
0
int SSL_set_ex_data(SSL *ssl, int idx, void *data) {
2653
0
  return CRYPTO_set_ex_data(&ssl->ex_data, idx, data);
2654
0
}
2655
2656
0
void *SSL_get_ex_data(const SSL *ssl, int idx) {
2657
0
  return CRYPTO_get_ex_data(&ssl->ex_data, idx);
2658
0
}
2659
2660
int SSL_CTX_get_ex_new_index(long argl, void *argp, CRYPTO_EX_unused *unused,
2661
                             CRYPTO_EX_dup *dup_unused,
2662
0
                             CRYPTO_EX_free *free_func) {
2663
0
  return CRYPTO_get_ex_new_index_ex(&g_ex_data_class_ssl_ctx, argl, argp,
2664
0
                                    free_func);
2665
0
}
2666
2667
0
int SSL_CTX_set_ex_data(SSL_CTX *ctx, int idx, void *data) {
2668
0
  return CRYPTO_set_ex_data(&ctx->ex_data, idx, data);
2669
0
}
2670
2671
0
void *SSL_CTX_get_ex_data(const SSL_CTX *ctx, int idx) {
2672
0
  return CRYPTO_get_ex_data(&ctx->ex_data, idx);
2673
0
}
2674
2675
0
int SSL_want(const SSL *ssl) {
2676
  // Historically, OpenSSL did not track |SSL_ERROR_ZERO_RETURN| as an |rwstate|
2677
  // value. We do, but map it back to |SSL_ERROR_NONE| to preserve the original
2678
  // behavior.
2679
0
  return ssl->s3->rwstate == SSL_ERROR_ZERO_RETURN ? SSL_ERROR_NONE
2680
0
                                                   : ssl->s3->rwstate;
2681
0
}
2682
2683
void SSL_CTX_set_tmp_rsa_callback(SSL_CTX *ctx,
2684
                                  RSA *(*cb)(SSL *ssl, int is_export,
2685
0
                                             int keylength)) {}
2686
2687
void SSL_set_tmp_rsa_callback(SSL *ssl, RSA *(*cb)(SSL *ssl, int is_export,
2688
0
                                                   int keylength)) {}
2689
2690
void SSL_CTX_set_tmp_dh_callback(SSL_CTX *ctx,
2691
                                 DH *(*cb)(SSL *ssl, int is_export,
2692
0
                                           int keylength)) {}
2693
2694
void SSL_set_tmp_dh_callback(SSL *ssl, DH *(*cb)(SSL *ssl, int is_export,
2695
0
                                                 int keylength)) {}
2696
2697
static int use_psk_identity_hint(UniquePtr<char> *out,
2698
0
                                 const char *identity_hint) {
2699
0
  if (identity_hint != nullptr &&
2700
0
      strlen(identity_hint) > PSK_MAX_IDENTITY_LEN) {
2701
0
    OPENSSL_PUT_ERROR(SSL, SSL_R_DATA_LENGTH_TOO_LONG);
2702
0
    return 0;
2703
0
  }
2704
2705
  // Clear currently configured hint, if any.
2706
0
  out->reset();
2707
2708
  // Treat the empty hint as not supplying one. Plain PSK makes it possible to
2709
  // send either no hint (omit ServerKeyExchange) or an empty hint, while
2710
  // ECDHE_PSK can only spell empty hint. Having different capabilities is odd,
2711
  // so we interpret empty and missing as identical.
2712
0
  if (identity_hint != nullptr && identity_hint[0] != '\0') {
2713
0
    out->reset(OPENSSL_strdup(identity_hint));
2714
0
    if (*out == nullptr) {
2715
0
      return 0;
2716
0
    }
2717
0
  }
2718
2719
0
  return 1;
2720
0
}
2721
2722
0
int SSL_CTX_use_psk_identity_hint(SSL_CTX *ctx, const char *identity_hint) {
2723
0
  return use_psk_identity_hint(&ctx->psk_identity_hint, identity_hint);
2724
0
}
2725
2726
0
int SSL_use_psk_identity_hint(SSL *ssl, const char *identity_hint) {
2727
0
  if (!ssl->config) {
2728
0
    return 0;
2729
0
  }
2730
0
  return use_psk_identity_hint(&ssl->config->psk_identity_hint, identity_hint);
2731
0
}
2732
2733
0
const char *SSL_get_psk_identity_hint(const SSL *ssl) {
2734
0
  if (ssl == nullptr) {
2735
0
    return nullptr;
2736
0
  }
2737
0
  if (ssl->config == nullptr) {
2738
0
    assert(ssl->config);
2739
0
    return nullptr;
2740
0
  }
2741
0
  return ssl->config->psk_identity_hint.get();
2742
0
}
2743
2744
0
const char *SSL_get_psk_identity(const SSL *ssl) {
2745
0
  if (ssl == nullptr) {
2746
0
    return nullptr;
2747
0
  }
2748
0
  SSL_SESSION *session = SSL_get_session(ssl);
2749
0
  if (session == nullptr) {
2750
0
    return nullptr;
2751
0
  }
2752
0
  return session->psk_identity.get();
2753
0
}
2754
2755
void SSL_set_psk_client_callback(
2756
    SSL *ssl, unsigned (*cb)(SSL *ssl, const char *hint, char *identity,
2757
                             unsigned max_identity_len, uint8_t *psk,
2758
0
                             unsigned max_psk_len)) {
2759
0
  if (!ssl->config) {
2760
0
    return;
2761
0
  }
2762
0
  ssl->config->psk_client_callback = cb;
2763
0
}
2764
2765
void SSL_CTX_set_psk_client_callback(
2766
    SSL_CTX *ctx, unsigned (*cb)(SSL *ssl, const char *hint, char *identity,
2767
                                 unsigned max_identity_len, uint8_t *psk,
2768
0
                                 unsigned max_psk_len)) {
2769
0
  ctx->psk_client_callback = cb;
2770
0
}
2771
2772
void SSL_set_psk_server_callback(SSL *ssl,
2773
                                 unsigned (*cb)(SSL *ssl, const char *identity,
2774
                                                uint8_t *psk,
2775
0
                                                unsigned max_psk_len)) {
2776
0
  if (!ssl->config) {
2777
0
    return;
2778
0
  }
2779
0
  ssl->config->psk_server_callback = cb;
2780
0
}
2781
2782
void SSL_CTX_set_psk_server_callback(
2783
    SSL_CTX *ctx, unsigned (*cb)(SSL *ssl, const char *identity, uint8_t *psk,
2784
0
                                 unsigned max_psk_len)) {
2785
0
  ctx->psk_server_callback = cb;
2786
0
}
2787
2788
void SSL_CTX_set_msg_callback(SSL_CTX *ctx,
2789
                              void (*cb)(int write_p, int version,
2790
                                         int content_type, const void *buf,
2791
0
                                         size_t len, SSL *ssl, void *arg)) {
2792
0
  ctx->msg_callback = cb;
2793
0
}
2794
2795
0
void SSL_CTX_set_msg_callback_arg(SSL_CTX *ctx, void *arg) {
2796
0
  ctx->msg_callback_arg = arg;
2797
0
}
2798
2799
void SSL_set_msg_callback(SSL *ssl,
2800
                          void (*cb)(int write_p, int version, int content_type,
2801
                                     const void *buf, size_t len, SSL *ssl,
2802
0
                                     void *arg)) {
2803
0
  ssl->msg_callback = cb;
2804
0
}
2805
2806
0
void SSL_set_msg_callback_arg(SSL *ssl, void *arg) {
2807
0
  ssl->msg_callback_arg = arg;
2808
0
}
2809
2810
void SSL_CTX_set_keylog_callback(SSL_CTX *ctx,
2811
0
                                 void (*cb)(const SSL *ssl, const char *line)) {
2812
0
  ctx->keylog_callback = cb;
2813
0
}
2814
2815
void (*SSL_CTX_get_keylog_callback(const SSL_CTX *ctx))(const SSL *ssl,
2816
0
                                                        const char *line) {
2817
0
  return ctx->keylog_callback;
2818
0
}
2819
2820
void SSL_CTX_set_current_time_cb(SSL_CTX *ctx,
2821
                                 void (*cb)(const SSL *ssl,
2822
12
                                            struct timeval *out_clock)) {
2823
12
  ctx->current_time_cb = cb;
2824
12
}
2825
2826
48.0k
int SSL_can_release_private_key(const SSL *ssl) {
2827
48.0k
  if (ssl_can_renegotiate(ssl)) {
2828
    // If the connection can renegotiate (client only), the private key may be
2829
    // used in a future handshake.
2830
48.0k
    return 0;
2831
48.0k
  }
2832
2833
  // Otherwise, this is determined by the current handshake.
2834
0
  return !ssl->s3->hs || ssl->s3->hs->can_release_private_key;
2835
48.0k
}
2836
2837
0
int SSL_is_init_finished(const SSL *ssl) { return !SSL_in_init(ssl); }
2838
2839
3.39M
int SSL_in_init(const SSL *ssl) {
2840
  // This returns false once all the handshake state has been finalized, to
2841
  // allow callbacks and getters based on SSL_in_init to return the correct
2842
  // values.
2843
3.39M
  SSL_HANDSHAKE *hs = ssl->s3->hs.get();
2844
3.39M
  return hs != nullptr && !hs->handshake_finalized;
2845
3.39M
}
2846
2847
0
int SSL_in_false_start(const SSL *ssl) {
2848
0
  if (ssl->s3->hs == nullptr) {
2849
0
    return 0;
2850
0
  }
2851
0
  return ssl->s3->hs->in_false_start;
2852
0
}
2853
2854
0
int SSL_cutthrough_complete(const SSL *ssl) { return SSL_in_false_start(ssl); }
2855
2856
51.8k
int SSL_is_server(const SSL *ssl) { return ssl->server; }
2857
2858
7.22M
int SSL_is_dtls(const SSL *ssl) { return ssl->method->is_dtls; }
2859
2860
3.19M
int SSL_is_quic(const SSL *ssl) { return ssl->quic_method != nullptr; }
2861
2862
void SSL_CTX_set_select_certificate_cb(
2863
    SSL_CTX *ctx,
2864
0
    enum ssl_select_cert_result_t (*cb)(const SSL_CLIENT_HELLO *)) {
2865
0
  ctx->select_certificate_cb = cb;
2866
0
}
2867
2868
void SSL_CTX_set_dos_protection_cb(SSL_CTX *ctx,
2869
0
                                   int (*cb)(const SSL_CLIENT_HELLO *)) {
2870
0
  ctx->dos_protection_cb = cb;
2871
0
}
2872
2873
0
void SSL_CTX_set_reverify_on_resume(SSL_CTX *ctx, int enabled) {
2874
0
  ctx->reverify_on_resume = !!enabled;
2875
0
}
2876
2877
0
void SSL_set_enforce_rsa_key_usage(SSL *ssl, int enabled) {
2878
0
  if (!ssl->config) {
2879
0
    return;
2880
0
  }
2881
0
  ssl->config->enforce_rsa_key_usage = !!enabled;
2882
0
}
2883
2884
0
int SSL_was_key_usage_invalid(const SSL *ssl) {
2885
0
  return ssl->s3->was_key_usage_invalid;
2886
0
}
2887
2888
14.2k
void SSL_set_renegotiate_mode(SSL *ssl, enum ssl_renegotiate_mode_t mode) {
2889
14.2k
  ssl->renegotiate_mode = mode;
2890
2891
  // Check if |ssl_can_renegotiate| has changed and the configuration may now be
2892
  // shed. HTTP clients may initially allow renegotiation for HTTP/1.1, and then
2893
  // disable after the handshake once the ALPN protocol is known to be HTTP/2.
2894
14.2k
  ssl_maybe_shed_handshake_config(ssl);
2895
14.2k
}
2896
2897
int SSL_get_ivs(const SSL *ssl, const uint8_t **out_read_iv,
2898
0
                const uint8_t **out_write_iv, size_t *out_iv_len) {
2899
  // No cipher suites maintain stateful internal IVs in DTLS. It would not be
2900
  // compatible with reordering.
2901
0
  if (SSL_is_dtls(ssl)) {
2902
0
    OPENSSL_PUT_ERROR(SSL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
2903
0
    return 0;
2904
0
  }
2905
2906
0
  size_t write_iv_len;
2907
0
  if (!ssl->s3->aead_read_ctx->GetIV(out_read_iv, out_iv_len) ||
2908
0
      !ssl->s3->aead_write_ctx->GetIV(out_write_iv, &write_iv_len) ||
2909
0
      *out_iv_len != write_iv_len) {
2910
0
    return 0;
2911
0
  }
2912
2913
0
  return 1;
2914
0
}
2915
2916
0
uint64_t SSL_get_read_sequence(const SSL *ssl) {
2917
0
  if (SSL_is_dtls(ssl)) {
2918
    // TODO(crbug.com/42290608): This API should not be implemented for DTLS or
2919
    // QUIC. In QUIC we do not maintain a sequence number.
2920
0
    const DTLSReadEpoch *read_epoch = &ssl->d1->read_epoch;
2921
0
    return DTLSRecordNumber(read_epoch->epoch, read_epoch->bitmap.max_seq_num())
2922
0
        .combined();
2923
0
  }
2924
0
  return ssl->s3->read_sequence;
2925
0
}
2926
2927
0
uint64_t SSL_get_write_sequence(const SSL *ssl) {
2928
  // TODO(crbug.com/42290608): This API should not be implemented for DTLS or
2929
  // QUIC. In QUIC we do not maintain a sequence number. In DTLS, this API isn't
2930
  // harmful per se, but the caller already needs to use a DTLS-specific API on
2931
  // the read side.
2932
0
  if (SSL_is_dtls(ssl)) {
2933
0
    return ssl->d1->write_epoch.next_record.combined();
2934
0
  }
2935
2936
0
  return ssl->s3->write_sequence;
2937
0
}
2938
2939
0
int SSL_is_dtls_handshake_idle(const SSL *ssl) {
2940
0
  BSSL_CHECK(SSL_is_dtls(ssl));
2941
2942
0
  return !SSL_in_init(ssl) &&
2943
         // No unacknowledged messages in DTLS 1.3. In DTLS 1.2, there no ACKs
2944
         // and we currently never clear |outgoing_messages| on the side that
2945
         // speaks last.
2946
0
         (ssl_protocol_version(ssl) < TLS1_3_VERSION ||
2947
0
          ssl->d1->outgoing_messages.empty()) &&
2948
         // No partial or out-of-order messages.
2949
0
         std::all_of(std::begin(ssl->d1->incoming_messages),
2950
0
                     std::end(ssl->d1->incoming_messages),
2951
0
                     [](const auto &msg) { return msg == nullptr; }) &&
2952
         // Not trying to send a KeyUpdate.
2953
0
         !ssl->s3->key_update_pending &&
2954
0
         ssl->d1->queued_key_update == bssl::QueuedKeyUpdate::kNone;
2955
0
}
2956
2957
0
uint32_t SSL_get_dtls_handshake_read_seq(const SSL *ssl) {
2958
0
  BSSL_CHECK(SSL_is_dtls(ssl));
2959
0
  return ssl->d1->handshake_read_overflow
2960
0
             ? uint32_t{0x10000}
2961
0
             : uint32_t{ssl->d1->handshake_read_seq};
2962
0
}
2963
2964
0
uint32_t SSL_get_dtls_handshake_write_seq(const SSL *ssl) {
2965
0
  BSSL_CHECK(SSL_is_dtls(ssl));
2966
0
  return ssl->d1->handshake_write_overflow
2967
0
             ? uint32_t{0x10000}
2968
0
             : uint32_t{ssl->d1->handshake_write_seq};
2969
0
}
2970
2971
0
uint16_t SSL_get_dtls_read_epoch(const SSL *ssl) {
2972
0
  BSSL_CHECK(SSL_is_dtls(ssl));
2973
  // Return the highest available epoch.
2974
0
  return ssl->d1->next_read_epoch ? ssl->d1->next_read_epoch->epoch
2975
0
                                  : ssl->d1->read_epoch.epoch;
2976
0
}
2977
2978
0
uint16_t SSL_get_dtls_write_epoch(const SSL *ssl) {
2979
0
  BSSL_CHECK(SSL_is_dtls(ssl));
2980
0
  return ssl->d1->write_epoch.epoch();
2981
0
}
2982
2983
0
uint64_t SSL_get_dtls_read_sequence(const SSL *ssl, uint16_t epoch) {
2984
0
  BSSL_CHECK(SSL_is_dtls(ssl));
2985
0
  const DTLSReadEpoch *read_epoch = dtls_get_read_epoch(ssl, epoch);
2986
0
  if (read_epoch == nullptr) {
2987
0
    return UINT64_MAX;
2988
0
  }
2989
0
  uint64_t max_seq_num = read_epoch->bitmap.max_seq_num();
2990
0
  assert(max_seq_num <= DTLSRecordNumber::kMaxSequence);
2991
0
  if (read_epoch->bitmap.ShouldDiscard(max_seq_num)) {
2992
    // Increment to get to an available sequence number.
2993
0
    max_seq_num++;
2994
0
  } else {
2995
    // If |max_seq_num| was available, the bitmap must have been empty.
2996
0
    assert(max_seq_num == 0);
2997
0
  }
2998
0
  return max_seq_num;
2999
0
}
3000
3001
0
uint64_t SSL_get_dtls_write_sequence(const SSL *ssl, uint16_t epoch) {
3002
0
  BSSL_CHECK(SSL_is_dtls(ssl));
3003
0
  const DTLSWriteEpoch *write_epoch = dtls_get_write_epoch(ssl, epoch);
3004
0
  if (write_epoch == nullptr) {
3005
0
    return UINT64_MAX;
3006
0
  }
3007
0
  return write_epoch->next_record.sequence();
3008
0
}
3009
3010
template <typename EpochState>
3011
static int get_dtls_traffic_secret(
3012
    const SSL *ssl, EpochState *(*get_epoch)(const SSL *, uint16_t),
3013
0
    const uint8_t **out_data, size_t *out_len, uint16_t epoch) {
3014
0
  BSSL_CHECK(SSL_is_dtls(ssl));
3015
  // This function only applies to encrypted DTLS 1.3 epochs.
3016
0
  if (epoch == 0 || ssl->s3->version == 0 ||
3017
0
      ssl_protocol_version(ssl) < TLS1_3_VERSION) {
3018
0
    return 0;
3019
0
  }
3020
0
  const EpochState *epoch_state = get_epoch(ssl, epoch);
3021
0
  if (epoch_state == nullptr) {
3022
0
    return 0;
3023
0
  }
3024
0
  assert(!epoch_state->traffic_secret.empty());
3025
0
  *out_data = epoch_state->traffic_secret.data();
3026
0
  *out_len = epoch_state->traffic_secret.size();
3027
0
  return 1;
3028
0
}
Unexecuted instantiation: ssl_lib.cc:int get_dtls_traffic_secret<bssl::DTLSReadEpoch>(ssl_st const*, bssl::DTLSReadEpoch* (*)(ssl_st const*, unsigned short), unsigned char const**, unsigned long*, unsigned short)
Unexecuted instantiation: ssl_lib.cc:int get_dtls_traffic_secret<bssl::DTLSWriteEpoch>(ssl_st const*, bssl::DTLSWriteEpoch* (*)(ssl_st const*, unsigned short), unsigned char const**, unsigned long*, unsigned short)
3029
3030
int SSL_get_dtls_read_traffic_secret(const SSL *ssl, const uint8_t **out_data,
3031
0
                                     size_t *out_len, uint16_t epoch) {
3032
0
  return get_dtls_traffic_secret(ssl, dtls_get_read_epoch, out_data, out_len,
3033
0
                                 epoch);
3034
0
}
3035
3036
int SSL_get_dtls_write_traffic_secret(const SSL *ssl, const uint8_t **out_data,
3037
0
                                      size_t *out_len, uint16_t epoch) {
3038
0
  return get_dtls_traffic_secret(ssl, dtls_get_write_epoch, out_data, out_len,
3039
0
                                 epoch);
3040
0
}
3041
3042
0
uint16_t SSL_get_peer_signature_algorithm(const SSL *ssl) {
3043
0
  SSL_SESSION *session = SSL_get_session(ssl);
3044
0
  if (session == nullptr) {
3045
0
    return 0;
3046
0
  }
3047
3048
0
  return session->peer_signature_algorithm;
3049
0
}
3050
3051
0
size_t SSL_get_client_random(const SSL *ssl, uint8_t *out, size_t max_out) {
3052
0
  if (max_out == 0) {
3053
0
    return sizeof(ssl->s3->client_random);
3054
0
  }
3055
0
  if (max_out > sizeof(ssl->s3->client_random)) {
3056
0
    max_out = sizeof(ssl->s3->client_random);
3057
0
  }
3058
0
  OPENSSL_memcpy(out, ssl->s3->client_random, max_out);
3059
0
  return max_out;
3060
0
}
3061
3062
0
size_t SSL_get_server_random(const SSL *ssl, uint8_t *out, size_t max_out) {
3063
0
  if (max_out == 0) {
3064
0
    return sizeof(ssl->s3->server_random);
3065
0
  }
3066
0
  if (max_out > sizeof(ssl->s3->server_random)) {
3067
0
    max_out = sizeof(ssl->s3->server_random);
3068
0
  }
3069
0
  OPENSSL_memcpy(out, ssl->s3->server_random, max_out);
3070
0
  return max_out;
3071
0
}
3072
3073
0
const SSL_CIPHER *SSL_get_pending_cipher(const SSL *ssl) {
3074
0
  SSL_HANDSHAKE *hs = ssl->s3->hs.get();
3075
0
  if (hs == nullptr) {
3076
0
    return nullptr;
3077
0
  }
3078
0
  return hs->new_cipher;
3079
0
}
3080
3081
0
void SSL_set_retain_only_sha256_of_client_certs(SSL *ssl, int enabled) {
3082
0
  if (!ssl->config) {
3083
0
    return;
3084
0
  }
3085
0
  ssl->config->retain_only_sha256_of_client_certs = !!enabled;
3086
0
}
3087
3088
3.10k
void SSL_CTX_set_retain_only_sha256_of_client_certs(SSL_CTX *ctx, int enabled) {
3089
3.10k
  ctx->retain_only_sha256_of_client_certs = !!enabled;
3090
3.10k
}
3091
3092
991
void SSL_CTX_set_grease_enabled(SSL_CTX *ctx, int enabled) {
3093
991
  ctx->grease_enabled = !!enabled;
3094
991
}
3095
3096
0
void SSL_CTX_set_permute_extensions(SSL_CTX *ctx, int enabled) {
3097
0
  ctx->permute_extensions = !!enabled;
3098
0
}
3099
3100
0
void SSL_set_permute_extensions(SSL *ssl, int enabled) {
3101
0
  if (!ssl->config) {
3102
0
    return;
3103
0
  }
3104
0
  ssl->config->permute_extensions = !!enabled;
3105
0
}
3106
3107
0
int32_t SSL_get_ticket_age_skew(const SSL *ssl) {
3108
0
  return ssl->s3->ticket_age_skew;
3109
0
}
3110
3111
0
void SSL_CTX_set_false_start_allowed_without_alpn(SSL_CTX *ctx, int allowed) {
3112
0
  ctx->false_start_allowed_without_alpn = !!allowed;
3113
0
}
3114
3115
0
int SSL_used_hello_retry_request(const SSL *ssl) {
3116
0
  return ssl->s3->used_hello_retry_request;
3117
0
}
3118
3119
0
void SSL_set_shed_handshake_config(SSL *ssl, int enable) {
3120
0
  if (!ssl->config) {
3121
0
    return;
3122
0
  }
3123
0
  ssl->config->shed_handshake_config = !!enable;
3124
0
}
3125
3126
0
void SSL_set_jdk11_workaround(SSL *ssl, int enable) {
3127
0
  if (!ssl->config) {
3128
0
    return;
3129
0
  }
3130
0
  ssl->config->jdk11_workaround = !!enable;
3131
0
}
3132
3133
0
void SSL_set_quic_use_legacy_codepoint(SSL *ssl, int use_legacy) {
3134
0
  if (!ssl->config) {
3135
0
    return;
3136
0
  }
3137
0
  ssl->config->quic_use_legacy_codepoint = !!use_legacy;
3138
0
}
3139
3140
0
int SSL_clear(SSL *ssl) {
3141
0
  if (!ssl->config) {
3142
0
    return 0;  // SSL_clear may not be used after shedding config.
3143
0
  }
3144
3145
  // In OpenSSL, reusing a client |SSL| with |SSL_clear| causes the previously
3146
  // established session to be offered the next time around. wpa_supplicant
3147
  // depends on this behavior, so emulate it.
3148
0
  UniquePtr<SSL_SESSION> session;
3149
0
  if (!ssl->server && ssl->s3->established_session != nullptr) {
3150
0
    session = UpRef(ssl->s3->established_session);
3151
0
  }
3152
3153
  // The ssl->d1->mtu is simultaneously configuration (preserved across
3154
  // clear) and connection-specific state (gets reset).
3155
  //
3156
  // TODO(davidben): Avoid this.
3157
0
  unsigned mtu = 0;
3158
0
  if (ssl->d1 != nullptr) {
3159
0
    mtu = ssl->d1->mtu;
3160
0
  }
3161
3162
0
  ssl->method->ssl_free(ssl);
3163
0
  if (!ssl->method->ssl_new(ssl)) {
3164
0
    return 0;
3165
0
  }
3166
3167
0
  if (SSL_is_dtls(ssl) && (SSL_get_options(ssl) & SSL_OP_NO_QUERY_MTU)) {
3168
0
    ssl->d1->mtu = mtu;
3169
0
  }
3170
3171
0
  if (session != nullptr) {
3172
0
    SSL_set_session(ssl, session.get());
3173
0
  }
3174
3175
0
  return 1;
3176
0
}
3177
3178
0
int SSL_CTX_sess_connect(const SSL_CTX *ctx) { return 0; }
3179
0
int SSL_CTX_sess_connect_good(const SSL_CTX *ctx) { return 0; }
3180
0
int SSL_CTX_sess_connect_renegotiate(const SSL_CTX *ctx) { return 0; }
3181
0
int SSL_CTX_sess_accept(const SSL_CTX *ctx) { return 0; }
3182
0
int SSL_CTX_sess_accept_renegotiate(const SSL_CTX *ctx) { return 0; }
3183
0
int SSL_CTX_sess_accept_good(const SSL_CTX *ctx) { return 0; }
3184
0
int SSL_CTX_sess_hits(const SSL_CTX *ctx) { return 0; }
3185
0
int SSL_CTX_sess_cb_hits(const SSL_CTX *ctx) { return 0; }
3186
0
int SSL_CTX_sess_misses(const SSL_CTX *ctx) { return 0; }
3187
0
int SSL_CTX_sess_timeouts(const SSL_CTX *ctx) { return 0; }
3188
0
int SSL_CTX_sess_cache_full(const SSL_CTX *ctx) { return 0; }
3189
3190
0
int SSL_num_renegotiations(const SSL *ssl) {
3191
0
  return SSL_total_renegotiations(ssl);
3192
0
}
3193
3194
0
int SSL_CTX_need_tmp_RSA(const SSL_CTX *ctx) { return 0; }
3195
0
int SSL_need_tmp_RSA(const SSL *ssl) { return 0; }
3196
0
int SSL_CTX_set_tmp_rsa(SSL_CTX *ctx, const RSA *rsa) { return 1; }
3197
0
int SSL_set_tmp_rsa(SSL *ssl, const RSA *rsa) { return 1; }
3198
0
void ERR_load_SSL_strings(void) {}
3199
0
void SSL_load_error_strings(void) {}
3200
0
int SSL_cache_hit(SSL *ssl) { return SSL_session_reused(ssl); }
3201
3202
0
int SSL_CTX_set_tmp_ecdh(SSL_CTX *ctx, const EC_KEY *ec_key) {
3203
0
  if (ec_key == nullptr || EC_KEY_get0_group(ec_key) == nullptr) {
3204
0
    OPENSSL_PUT_ERROR(SSL, ERR_R_PASSED_NULL_PARAMETER);
3205
0
    return 0;
3206
0
  }
3207
0
  int nid = EC_GROUP_get_curve_name(EC_KEY_get0_group(ec_key));
3208
0
  return SSL_CTX_set1_groups(ctx, &nid, 1);
3209
0
}
3210
3211
0
int SSL_set_tmp_ecdh(SSL *ssl, const EC_KEY *ec_key) {
3212
0
  if (ec_key == nullptr || EC_KEY_get0_group(ec_key) == nullptr) {
3213
0
    OPENSSL_PUT_ERROR(SSL, ERR_R_PASSED_NULL_PARAMETER);
3214
0
    return 0;
3215
0
  }
3216
0
  int nid = EC_GROUP_get_curve_name(EC_KEY_get0_group(ec_key));
3217
0
  return SSL_set1_groups(ssl, &nid, 1);
3218
0
}
3219
3220
void SSL_CTX_set_ticket_aead_method(SSL_CTX *ctx,
3221
0
                                    const SSL_TICKET_AEAD_METHOD *aead_method) {
3222
0
  ctx->ticket_aead_method = aead_method;
3223
0
}
3224
3225
SSL_SESSION *SSL_process_tls13_new_session_ticket(SSL *ssl, const uint8_t *buf,
3226
0
                                                  size_t buf_len) {
3227
0
  if (SSL_in_init(ssl) ||                             //
3228
0
      ssl_protocol_version(ssl) != TLS1_3_VERSION ||  //
3229
0
      ssl->server) {
3230
    // Only TLS 1.3 clients are supported.
3231
0
    OPENSSL_PUT_ERROR(SSL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
3232
0
    return nullptr;
3233
0
  }
3234
3235
0
  CBS cbs, body;
3236
0
  CBS_init(&cbs, buf, buf_len);
3237
0
  uint8_t type;
3238
0
  if (!CBS_get_u8(&cbs, &type) ||                   //
3239
0
      !CBS_get_u24_length_prefixed(&cbs, &body) ||  //
3240
0
      CBS_len(&cbs) != 0) {
3241
0
    OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
3242
0
    return nullptr;
3243
0
  }
3244
3245
0
  UniquePtr<SSL_SESSION> session = tls13_create_session_with_ticket(ssl, &body);
3246
0
  if (!session) {
3247
    // |tls13_create_session_with_ticket| puts the correct error.
3248
0
    return nullptr;
3249
0
  }
3250
0
  return session.release();
3251
0
}
3252
3253
0
int SSL_CTX_set_num_tickets(SSL_CTX *ctx, size_t num_tickets) {
3254
0
  num_tickets = std::min(num_tickets, kMaxTickets);
3255
0
  static_assert(kMaxTickets <= 0xff, "Too many tickets.");
3256
0
  ctx->num_tickets = static_cast<uint8_t>(num_tickets);
3257
0
  return 1;
3258
0
}
3259
3260
0
size_t SSL_CTX_get_num_tickets(const SSL_CTX *ctx) { return ctx->num_tickets; }
3261
3262
0
int SSL_set_tlsext_status_type(SSL *ssl, int type) {
3263
0
  if (!ssl->config) {
3264
0
    return 0;
3265
0
  }
3266
0
  ssl->config->ocsp_stapling_enabled = type == TLSEXT_STATUSTYPE_ocsp;
3267
0
  return 1;
3268
0
}
3269
3270
0
int SSL_get_tlsext_status_type(const SSL *ssl) {
3271
0
  if (ssl->server) {
3272
0
    SSL_HANDSHAKE *hs = ssl->s3->hs.get();
3273
0
    return hs != nullptr && hs->ocsp_stapling_requested
3274
0
               ? TLSEXT_STATUSTYPE_ocsp
3275
0
               : TLSEXT_STATUSTYPE_nothing;
3276
0
  }
3277
3278
0
  return ssl->config != nullptr && ssl->config->ocsp_stapling_enabled
3279
0
             ? TLSEXT_STATUSTYPE_ocsp
3280
0
             : TLSEXT_STATUSTYPE_nothing;
3281
0
}
3282
3283
0
int SSL_set_tlsext_status_ocsp_resp(SSL *ssl, uint8_t *resp, size_t resp_len) {
3284
0
  if (SSL_set_ocsp_response(ssl, resp, resp_len)) {
3285
0
    OPENSSL_free(resp);
3286
0
    return 1;
3287
0
  }
3288
0
  return 0;
3289
0
}
3290
3291
0
size_t SSL_get_tlsext_status_ocsp_resp(const SSL *ssl, const uint8_t **out) {
3292
0
  size_t ret;
3293
0
  SSL_get0_ocsp_response(ssl, out, &ret);
3294
0
  return ret;
3295
0
}
3296
3297
int SSL_CTX_set_tlsext_status_cb(SSL_CTX *ctx,
3298
0
                                 int (*callback)(SSL *ssl, void *arg)) {
3299
0
  ctx->legacy_ocsp_callback = callback;
3300
0
  return 1;
3301
0
}
3302
3303
0
int SSL_CTX_set_tlsext_status_arg(SSL_CTX *ctx, void *arg) {
3304
0
  ctx->legacy_ocsp_callback_arg = arg;
3305
0
  return 1;
3306
0
}
3307
3308
0
uint16_t SSL_get_curve_id(const SSL *ssl) { return SSL_get_group_id(ssl); }
3309
3310
0
const char *SSL_get_curve_name(uint16_t curve_id) {
3311
0
  return SSL_get_group_name(curve_id);
3312
0
}
3313
3314
0
size_t SSL_get_all_curve_names(const char **out, size_t max_out) {
3315
0
  return SSL_get_all_group_names(out, max_out);
3316
0
}
3317
3318
0
int SSL_CTX_set1_curves(SSL_CTX *ctx, const int *curves, size_t num_curves) {
3319
0
  return SSL_CTX_set1_groups(ctx, curves, num_curves);
3320
0
}
3321
3322
0
int SSL_set1_curves(SSL *ssl, const int *curves, size_t num_curves) {
3323
0
  return SSL_set1_groups(ssl, curves, num_curves);
3324
0
}
3325
3326
0
int SSL_CTX_set1_curves_list(SSL_CTX *ctx, const char *curves) {
3327
0
  return SSL_CTX_set1_groups_list(ctx, curves);
3328
0
}
3329
3330
0
int SSL_set1_curves_list(SSL *ssl, const char *curves) {
3331
0
  return SSL_set1_groups_list(ssl, curves);
3332
0
}
3333
3334
namespace fips202205 {
3335
3336
// (References are to SP 800-52r2):
3337
3338
// Section 3.4.2.2
3339
// "at least one of the NIST-approved curves, P-256 (secp256r1) and P384
3340
// (secp384r1), shall be supported as described in RFC 8422."
3341
//
3342
// Section 3.3.1
3343
// "The server shall be configured to only use cipher suites that are
3344
// composed entirely of NIST approved algorithms"
3345
static const uint16_t kGroups[] = {SSL_GROUP_SECP256R1, SSL_GROUP_SECP384R1};
3346
3347
static const uint16_t kSigAlgs[] = {
3348
    SSL_SIGN_RSA_PKCS1_SHA256,
3349
    SSL_SIGN_RSA_PKCS1_SHA384,
3350
    SSL_SIGN_RSA_PKCS1_SHA512,
3351
    // Table 4.1:
3352
    // "The curve should be P-256 or P-384"
3353
    SSL_SIGN_ECDSA_SECP256R1_SHA256,
3354
    SSL_SIGN_ECDSA_SECP384R1_SHA384,
3355
    SSL_SIGN_RSA_PSS_RSAE_SHA256,
3356
    SSL_SIGN_RSA_PSS_RSAE_SHA384,
3357
    SSL_SIGN_RSA_PSS_RSAE_SHA512,
3358
};
3359
3360
static const char kTLS12Ciphers[] =
3361
    "TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256:"
3362
    "TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256:"
3363
    "TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384:"
3364
    "TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384";
3365
3366
0
static int Configure(SSL_CTX *ctx) {
3367
0
  ctx->compliance_policy = ssl_compliance_policy_fips_202205;
3368
3369
0
  return
3370
      // Section 3.1:
3371
      // "Servers that support government-only applications shall be
3372
      // configured to use TLS 1.2 and should be configured to use TLS 1.3
3373
      // as well. These servers should not be configured to use TLS 1.1 and
3374
      // shall not use TLS 1.0, SSL 3.0, or SSL 2.0.
3375
0
      SSL_CTX_set_min_proto_version(ctx, TLS1_2_VERSION) &&
3376
0
      SSL_CTX_set_max_proto_version(ctx, TLS1_3_VERSION) &&
3377
      // Sections 3.3.1.1.1 and 3.3.1.1.2 are ambiguous about whether
3378
      // HMAC-SHA-1 cipher suites are permitted with TLS 1.2. However, later the
3379
      // Encrypt-then-MAC extension is required for all CBC cipher suites and so
3380
      // it's easier to drop them.
3381
0
      SSL_CTX_set_strict_cipher_list(ctx, kTLS12Ciphers) &&
3382
0
      SSL_CTX_set1_group_ids(ctx, kGroups, std::size(kGroups)) &&
3383
0
      SSL_CTX_set_signing_algorithm_prefs(ctx, kSigAlgs, std::size(kSigAlgs)) &&
3384
0
      SSL_CTX_set_verify_algorithm_prefs(ctx, kSigAlgs, std::size(kSigAlgs));
3385
0
}
3386
3387
0
static int Configure(SSL *ssl) {
3388
0
  ssl->config->compliance_policy = ssl_compliance_policy_fips_202205;
3389
3390
  // See |Configure(SSL_CTX)|, above, for reasoning.
3391
0
  return SSL_set_min_proto_version(ssl, TLS1_2_VERSION) &&
3392
0
         SSL_set_max_proto_version(ssl, TLS1_3_VERSION) &&
3393
0
         SSL_set_strict_cipher_list(ssl, kTLS12Ciphers) &&
3394
0
         SSL_set1_group_ids(ssl, kGroups, std::size(kGroups)) &&
3395
0
         SSL_set_signing_algorithm_prefs(ssl, kSigAlgs, std::size(kSigAlgs)) &&
3396
0
         SSL_set_verify_algorithm_prefs(ssl, kSigAlgs, std::size(kSigAlgs));
3397
0
}
3398
3399
}  // namespace fips202205
3400
3401
namespace wpa202304 {
3402
3403
// See WPA version 3.1, section 3.5.
3404
3405
static const uint16_t kGroups[] = {SSL_GROUP_SECP384R1};
3406
3407
static const uint16_t kSigAlgs[] = {
3408
    SSL_SIGN_RSA_PKCS1_SHA384,        //
3409
    SSL_SIGN_RSA_PKCS1_SHA512,        //
3410
    SSL_SIGN_ECDSA_SECP384R1_SHA384,  //
3411
    SSL_SIGN_RSA_PSS_RSAE_SHA384,     //
3412
    SSL_SIGN_RSA_PSS_RSAE_SHA512,     //
3413
};
3414
3415
static const char kTLS12Ciphers[] =
3416
    "TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384:"
3417
    "TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384";
3418
3419
0
static int Configure(SSL_CTX *ctx) {
3420
0
  ctx->compliance_policy = ssl_compliance_policy_wpa3_192_202304;
3421
3422
0
  return SSL_CTX_set_min_proto_version(ctx, TLS1_2_VERSION) &&
3423
0
         SSL_CTX_set_max_proto_version(ctx, TLS1_3_VERSION) &&
3424
0
         SSL_CTX_set_strict_cipher_list(ctx, kTLS12Ciphers) &&
3425
0
         SSL_CTX_set1_group_ids(ctx, kGroups, std::size(kGroups)) &&
3426
0
         SSL_CTX_set_signing_algorithm_prefs(ctx, kSigAlgs,
3427
0
                                             std::size(kSigAlgs)) &&
3428
0
         SSL_CTX_set_verify_algorithm_prefs(ctx, kSigAlgs, std::size(kSigAlgs));
3429
0
}
3430
3431
0
static int Configure(SSL *ssl) {
3432
0
  ssl->config->compliance_policy = ssl_compliance_policy_wpa3_192_202304;
3433
3434
0
  return SSL_set_min_proto_version(ssl, TLS1_2_VERSION) &&
3435
0
         SSL_set_max_proto_version(ssl, TLS1_3_VERSION) &&
3436
0
         SSL_set_strict_cipher_list(ssl, kTLS12Ciphers) &&
3437
0
         SSL_set1_group_ids(ssl, kGroups, std::size(kGroups)) &&
3438
0
         SSL_set_signing_algorithm_prefs(ssl, kSigAlgs, std::size(kSigAlgs)) &&
3439
0
         SSL_set_verify_algorithm_prefs(ssl, kSigAlgs, std::size(kSigAlgs));
3440
0
}
3441
3442
}  // namespace wpa202304
3443
3444
namespace cnsa202407 {
3445
3446
0
static int Configure(SSL_CTX *ctx) {
3447
0
  ctx->compliance_policy = ssl_compliance_policy_cnsa_202407;
3448
0
  return 1;
3449
0
}
3450
3451
0
static int Configure(SSL *ssl) {
3452
0
  ssl->config->compliance_policy = ssl_compliance_policy_cnsa_202407;
3453
0
  return 1;
3454
0
}
3455
3456
}  // namespace cnsa202407
3457
3458
int SSL_CTX_set_compliance_policy(SSL_CTX *ctx,
3459
0
                                  enum ssl_compliance_policy_t policy) {
3460
0
  switch (policy) {
3461
0
    case ssl_compliance_policy_fips_202205:
3462
0
      return fips202205::Configure(ctx);
3463
0
    case ssl_compliance_policy_wpa3_192_202304:
3464
0
      return wpa202304::Configure(ctx);
3465
0
    case ssl_compliance_policy_cnsa_202407:
3466
0
      return cnsa202407::Configure(ctx);
3467
0
    default:
3468
0
      return 0;
3469
0
  }
3470
0
}
3471
3472
0
enum ssl_compliance_policy_t SSL_CTX_get_compliance_policy(const SSL_CTX *ctx) {
3473
0
  return ctx->compliance_policy;
3474
0
}
3475
3476
0
int SSL_set_compliance_policy(SSL *ssl, enum ssl_compliance_policy_t policy) {
3477
0
  switch (policy) {
3478
0
    case ssl_compliance_policy_fips_202205:
3479
0
      return fips202205::Configure(ssl);
3480
0
    case ssl_compliance_policy_wpa3_192_202304:
3481
0
      return wpa202304::Configure(ssl);
3482
0
    case ssl_compliance_policy_cnsa_202407:
3483
0
      return cnsa202407::Configure(ssl);
3484
0
    default:
3485
0
      return 0;
3486
0
  }
3487
0
}
3488
3489
0
enum ssl_compliance_policy_t SSL_get_compliance_policy(const SSL *ssl) {
3490
0
  return ssl->config->compliance_policy;
3491
0
}
3492
3493
0
int SSL_peer_matched_trust_anchor(const SSL *ssl) {
3494
0
  return ssl->s3->hs != nullptr && ssl->s3->hs->peer_matched_trust_anchor;
3495
0
}
3496
3497
void SSL_get0_peer_available_trust_anchors(const SSL *ssl, const uint8_t **out,
3498
0
                                           size_t *out_len) {
3499
0
  Span<const uint8_t> ret;
3500
0
  if (SSL_HANDSHAKE *hs = ssl->s3->hs.get(); hs != nullptr) {
3501
0
    ret = hs->peer_available_trust_anchors;
3502
0
  }
3503
0
  *out = ret.data();
3504
0
  *out_len = ret.size();
3505
0
}
3506
3507
int SSL_CTX_set1_requested_trust_anchors(SSL_CTX *ctx, const uint8_t *ids,
3508
0
                                         size_t ids_len) {
3509
0
  auto span = Span(ids, ids_len);
3510
0
  if (!ssl_is_valid_trust_anchor_list(span)) {
3511
0
    OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_TRUST_ANCHOR_LIST);
3512
0
    return 0;
3513
0
  }
3514
0
  Array<uint8_t> copy;
3515
0
  if (!copy.CopyFrom(span)) {
3516
0
    return 0;
3517
0
  }
3518
0
  ctx->requested_trust_anchors = std::move(copy);
3519
0
  return 1;
3520
0
}
3521
3522
int SSL_set1_requested_trust_anchors(SSL *ssl, const uint8_t *ids,
3523
0
                                     size_t ids_len) {
3524
0
  if (!ssl->config) {
3525
0
    return 0;
3526
0
  }
3527
0
  auto span = Span(ids, ids_len);
3528
0
  if (!ssl_is_valid_trust_anchor_list(span)) {
3529
0
    OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_TRUST_ANCHOR_LIST);
3530
0
    return 0;
3531
0
  }
3532
0
  Array<uint8_t> copy;
3533
0
  if (!copy.CopyFrom(span)) {
3534
0
    return 0;
3535
0
  }
3536
0
  ssl->config->requested_trust_anchors = std::move(copy);
3537
0
  return 1;
3538
0
}
3539
3540
0
int SSL_CTX_get_security_level(const SSL_CTX *ctx) { return 0; }