| 1 |
// Copyright (C) 1995-1998 Eric Young ([email protected]) |
| 2 |
// Copyright (c) 1998-2007 The OpenSSL Project. All rights reserved. |
| 3 |
// Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED. |
| 4 |
// Copyright 2005 Nokia. All rights reserved. |
| 5 |
// |
| 6 |
// ECC cipher suite support in OpenSSL originally developed by |
| 7 |
// SUN MICROSYSTEMS, INC., and contributed to the OpenSSL project. |
| 8 |
// |
| 9 |
// The Contribution, originally written by Mika Kousa and Pasi Eronen of |
| 10 |
// Nokia Corporation, consists of the "PSK" (Pre-Shared Key) ciphersuites |
| 11 |
// support (see RFC 4279) to OpenSSL. |
| 12 |
// |
| 13 |
// SPDX-License-Identifier: Apache-2.0 |
| 14 |
|
| 15 |
#ifndef OPENSSL_HEADER_SSL_INTERNAL_H |
| 16 |
#define OPENSSL_HEADER_SSL_INTERNAL_H |
| 17 |
|
| 18 |
#include <openssl/base.h> |
| 19 |
|
| 20 |
#include <stdlib.h> |
| 21 |
|
| 22 |
#include <algorithm> |
| 23 |
#include <bitset> |
| 24 |
#include <initializer_list> |
| 25 |
#include <limits> |
| 26 |
#include <new> |
| 27 |
#include <type_traits> |
| 28 |
#include <utility> |
| 29 |
|
| 30 |
#include <openssl/aead.h> |
| 31 |
#include <openssl/curve25519.h> |
| 32 |
#include <openssl/err.h> |
| 33 |
#include <openssl/hpke.h> |
| 34 |
#include <openssl/lhash.h> |
| 35 |
#include <openssl/mem.h> |
| 36 |
#include <openssl/span.h> |
| 37 |
#include <openssl/ssl.h> |
| 38 |
#include <openssl/stack.h> |
| 39 |
|
| 40 |
#include "../crypto/err/internal.h" |
| 41 |
#include "../crypto/internal.h" |
| 42 |
#include "../crypto/lhash/internal.h" |
| 43 |
|
| 44 |
|
| 45 |
#if defined(OPENSSL_WINDOWS) |
| 46 |
// Windows defines struct timeval in winsock2.h. |
| 47 |
OPENSSL_MSVC_PRAGMA(warning(push, 3)) |
| 48 |
#include <winsock2.h> |
| 49 |
OPENSSL_MSVC_PRAGMA(warning(pop)) |
| 50 |
#else |
| 51 |
#include <sys/time.h> |
| 52 |
#endif |
| 53 |
|
| 54 |
|
| 55 |
#include <atomic> |
| 56 |
|
| 57 |
typedef int SSL_stats_t; |
| 58 |
|
| 59 |
// Using ATOMIC_INT_LOCK_FREE as SSL_stats_t is an int |
| 60 |
#if !defined(OPENSSL_STATS_C11_ATOMIC) && defined(OPENSSL_THREADS) && ATOMIC_INT_LOCK_FREE == 2 |
| 61 |
#define OPENSSL_STATS_C11_ATOMIC |
| 62 |
#endif |
| 63 |
|
| 64 |
// Define the actual storage type for statistics counters |
| 65 |
#if defined(OPENSSL_STATS_C11_ATOMIC) |
| 66 |
#define SSL_STATS_COUNTER_TYPE std::atomic<SSL_stats_t> |
| 67 |
#else |
| 68 |
#define SSL_STATS_COUNTER_TYPE SSL_stats_t |
| 69 |
#endif |
| 70 |
|
| 71 |
BSSL_NAMESPACE_BEGIN |
| 72 |
|
| 73 |
struct SSL_CONFIG; |
| 74 |
struct SSL_HANDSHAKE; |
| 75 |
struct SSL_PROTOCOL_METHOD; |
| 76 |
struct SSL_X509_METHOD; |
| 77 |
|
| 78 |
// C++ utilities. |
| 79 |
|
| 80 |
// New behaves like |new| but uses |OPENSSL_malloc| for memory allocation. It |
| 81 |
// returns nullptr on allocation error. It only implements single-object |
| 82 |
// allocation and not new T[n]. |
| 83 |
// |
| 84 |
// Note: unlike |new|, this does not support non-public constructors. |
| 85 |
template <typename T, typename... Args> |
| 86 |
T *New(Args &&...args) { |
| 87 |
void *t = OPENSSL_malloc(sizeof(T)); |
| 88 |
if (t == nullptr) { |
| 89 |
return nullptr; |
| 90 |
} |
| 91 |
return new (t) T(std::forward<Args>(args)...); |
| 92 |
} |
| 93 |
|
| 94 |
// Delete behaves like |delete| but uses |OPENSSL_free| to release memory. |
| 95 |
// |
| 96 |
// Note: unlike |delete| this does not support non-public destructors. |
| 97 |
template <typename T> |
| 98 |
void Delete(T *t) { |
| 99 |
if (t != nullptr) { |
| 100 |
t->~T(); |
| 101 |
OPENSSL_free(t); |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
// All types with kAllowUniquePtr set may be used with UniquePtr. Other types |
| 106 |
// may be C structs which require a |BORINGSSL_MAKE_DELETER| registration. |
| 107 |
namespace internal { |
| 108 |
template <typename T> |
| 109 |
struct DeleterImpl<T, typename std::enable_if<T::kAllowUniquePtr>::type> { |
| 110 |
static void Free(T *t) { Delete(t); } |
| 111 |
}; |
| 112 |
} // namespace internal |
| 113 |
|
| 114 |
// MakeUnique behaves like |std::make_unique| but returns nullptr on allocation |
| 115 |
// error. |
| 116 |
template <typename T, typename... Args> |
| 117 |
UniquePtr<T> MakeUnique(Args &&...args) { |
| 118 |
return UniquePtr<T>(New<T>(std::forward<Args>(args)...)); |
| 119 |
} |
| 120 |
|
| 121 |
#if defined(BORINGSSL_ALLOW_CXX_RUNTIME) |
| 122 |
#define HAS_VIRTUAL_DESTRUCTOR |
| 123 |
#define PURE_VIRTUAL = 0 |
| 124 |
#else |
| 125 |
// HAS_VIRTUAL_DESTRUCTOR should be declared in any base class which defines a |
| 126 |
// virtual destructor. This avoids a dependency on |_ZdlPv| and prevents the |
| 127 |
// class from being used with |delete|. |
| 128 |
#define HAS_VIRTUAL_DESTRUCTOR \ |
| 129 |
void operator delete(void *) { abort(); } |
| 130 |
|
| 131 |
// PURE_VIRTUAL should be used instead of = 0 when defining pure-virtual |
| 132 |
// functions. This avoids a dependency on |__cxa_pure_virtual| but loses |
| 133 |
// compile-time checking. |
| 134 |
#define PURE_VIRTUAL \ |
| 135 |
{ abort(); } |
| 136 |
#endif |
| 137 |
|
| 138 |
// CONSTEXPR_ARRAY works around a VS 2015 bug where ranged for loops don't work |
| 139 |
// on constexpr arrays. |
| 140 |
#if defined(_MSC_VER) && !defined(__clang__) && _MSC_VER < 1910 |
| 141 |
#define CONSTEXPR_ARRAY const |
| 142 |
#else |
| 143 |
#define CONSTEXPR_ARRAY constexpr |
| 144 |
#endif |
| 145 |
|
| 146 |
// Array<T> is an owning array of elements of |T|. |
| 147 |
template <typename T> |
| 148 |
class Array { |
| 149 |
public: |
| 150 |
// Array's default constructor creates an empty array. |
| 151 |
Array() {} |
| 152 |
Array(const Array &) = delete; |
| 153 |
Array(Array &&other) { *this = std::move(other); } |
| 154 |
|
| 155 |
~Array() { Reset(); } |
| 156 |
|
| 157 |
Array &operator=(const Array &) = delete; |
| 158 |
Array &operator=(Array &&other) { |
| 159 |
Reset(); |
| 160 |
other.Release(&data_, &size_); |
| 161 |
return *this; |
| 162 |
} |
| 163 |
|
| 164 |
const T *data() const { return data_; } |
| 165 |
T *data() { return data_; } |
| 166 |
size_t size() const { return size_; } |
| 167 |
bool empty() const { return size_ == 0; } |
| 168 |
|
| 169 |
const T &operator[](size_t i) const { return data_[i]; } |
| 170 |
T &operator[](size_t i) { return data_[i]; } |
| 171 |
|
| 172 |
T *begin() { return data_; } |
| 173 |
const T *begin() const { return data_; } |
| 174 |
T *end() { return data_ + size_; } |
| 175 |
const T *end() const { return data_ + size_; } |
| 176 |
|
| 177 |
void Reset() { Reset(nullptr, 0); } |
| 178 |
|
| 179 |
// Reset releases the current contents of the array and takes ownership of the |
| 180 |
// raw pointer supplied by the caller. |
| 181 |
void Reset(T *new_data, size_t new_size) { |
| 182 |
for (size_t i = 0; i < size_; i++) { |
| 183 |
data_[i].~T(); |
| 184 |
} |
| 185 |
OPENSSL_free(data_); |
| 186 |
data_ = new_data; |
| 187 |
size_ = new_size; |
| 188 |
} |
| 189 |
|
| 190 |
// Release releases ownership of the array to a raw pointer supplied by the |
| 191 |
// caller. |
| 192 |
void Release(T **out, size_t *out_size) { |
| 193 |
*out = data_; |
| 194 |
*out_size = size_; |
| 195 |
data_ = nullptr; |
| 196 |
size_ = 0; |
| 197 |
} |
| 198 |
|
| 199 |
// Init replaces the array with a newly-allocated array of |new_size| |
| 200 |
// default-constructed copies of |T|. It returns true on success and false on |
| 201 |
// error. |
| 202 |
// |
| 203 |
// Note that if |T| is a primitive type like |uint8_t|, it is uninitialized. |
| 204 |
bool Init(size_t new_size) { |
| 205 |
Reset(); |
| 206 |
if (new_size == 0) { |
| 207 |
return true; |
| 208 |
} |
| 209 |
|
| 210 |
if (new_size > std::numeric_limits<size_t>::max() / sizeof(T)) { |
| 211 |
OPENSSL_PUT_ERROR(SSL, ERR_R_OVERFLOW); |
| 212 |
return false; |
| 213 |
} |
| 214 |
data_ = reinterpret_cast<T *>(OPENSSL_malloc(new_size * sizeof(T))); |
| 215 |
if (data_ == nullptr) { |
| 216 |
return false; |
| 217 |
} |
| 218 |
size_ = new_size; |
| 219 |
for (size_t i = 0; i < size_; i++) { |
| 220 |
new (&data_[i]) T; |
| 221 |
} |
| 222 |
return true; |
| 223 |
} |
| 224 |
|
| 225 |
// CopyFrom replaces the array with a newly-allocated copy of |in|. It returns |
| 226 |
// true on success and false on error. |
| 227 |
bool CopyFrom(Span<const T> in) { |
| 228 |
if (!Init(in.size())) { |
| 229 |
return false; |
| 230 |
} |
| 231 |
std::copy(in.begin(), in.end(), data_); |
| 232 |
return true; |
| 233 |
} |
| 234 |
|
| 235 |
// Shrink shrinks the stored size of the array to |new_size|. It crashes if |
| 236 |
// the new size is larger. Note this does not shrink the allocation itself. |
| 237 |
void Shrink(size_t new_size) { |
| 238 |
if (new_size > size_) { |
| 239 |
abort(); |
| 240 |
} |
| 241 |
for (size_t i = new_size; i < size_; i++) { |
| 242 |
data_[i].~T(); |
| 243 |
} |
| 244 |
size_ = new_size; |
| 245 |
} |
| 246 |
|
| 247 |
private: |
| 248 |
T *data_ = nullptr; |
| 249 |
size_t size_ = 0; |
| 250 |
}; |
| 251 |
|
| 252 |
// GrowableArray<T> is an array that owns elements of |T|, backed by an |
| 253 |
// Array<T>. When necessary, pushing will automatically trigger a resize. |
| 254 |
// |
| 255 |
// Note, for simplicity, this class currently differs from |std::vector| in that |
| 256 |
// |T| must be efficiently default-constructible. Allocated elements beyond the |
| 257 |
// end of the array are constructed and destructed. |
| 258 |
template <typename T> |
| 259 |
class GrowableArray { |
| 260 |
public: |
| 261 |
GrowableArray() = default; |
| 262 |
GrowableArray(const GrowableArray &) = delete; |
| 263 |
GrowableArray(GrowableArray &&other) { *this = std::move(other); } |
| 264 |
~GrowableArray() {} |
| 265 |
|
| 266 |
GrowableArray &operator=(const GrowableArray &) = delete; |
| 267 |
GrowableArray &operator=(GrowableArray &&other) { |
| 268 |
size_ = other.size_; |
| 269 |
other.size_ = 0; |
| 270 |
array_ = std::move(other.array_); |
| 271 |
return *this; |
| 272 |
} |
| 273 |
|
| 274 |
const T *data() const { return array_.data(); } |
| 275 |
T *data() { return array_.data(); } |
| 276 |
size_t size() const { return size_; } |
| 277 |
bool empty() const { return size_ == 0; } |
| 278 |
|
| 279 |
const T &operator[](size_t i) const { return array_[i]; } |
| 280 |
T &operator[](size_t i) { return array_[i]; } |
| 281 |
|
| 282 |
T *begin() { return array_.data(); } |
| 283 |
const T *begin() const { return array_.data(); } |
| 284 |
T *end() { return array_.data() + size_; } |
| 285 |
const T *end() const { return array_.data() + size_; } |
| 286 |
|
| 287 |
void clear() { |
| 288 |
size_ = 0; |
| 289 |
array_.Reset(); |
| 290 |
} |
| 291 |
|
| 292 |
// Push adds |elem| at the end of the internal array, growing if necessary. It |
| 293 |
// returns false when allocation fails. |
| 294 |
bool Push(T elem) { |
| 295 |
if (!MaybeGrow()) { |
| 296 |
return false; |
| 297 |
} |
| 298 |
array_[size_] = std::move(elem); |
| 299 |
size_++; |
| 300 |
return true; |
| 301 |
} |
| 302 |
|
| 303 |
// CopyFrom replaces the contents of the array with a copy of |in|. It returns |
| 304 |
// true on success and false on allocation error. |
| 305 |
bool CopyFrom(Span<const T> in) { |
| 306 |
if (!array_.CopyFrom(in)) { |
| 307 |
return false; |
| 308 |
} |
| 309 |
size_ = in.size(); |
| 310 |
return true; |
| 311 |
} |
| 312 |
|
| 313 |
private: |
| 314 |
// If there is no room for one more element, creates a new backing array with |
| 315 |
// double the size of the old one and copies elements over. |
| 316 |
bool MaybeGrow() { |
| 317 |
if (array_.size() == 0) { |
| 318 |
return array_.Init(kDefaultSize); |
| 319 |
} |
| 320 |
// No need to grow if we have room for one more T. |
| 321 |
if (size_ < array_.size()) { |
| 322 |
return true; |
| 323 |
} |
| 324 |
// Double the array's size if it's safe to do so. |
| 325 |
if (array_.size() > std::numeric_limits<size_t>::max() / 2) { |
| 326 |
OPENSSL_PUT_ERROR(SSL, ERR_R_OVERFLOW); |
| 327 |
return false; |
| 328 |
} |
| 329 |
Array<T> new_array; |
| 330 |
if (!new_array.Init(array_.size() * 2)) { |
| 331 |
return false; |
| 332 |
} |
| 333 |
for (size_t i = 0; i < array_.size(); i++) { |
| 334 |
new_array[i] = std::move(array_[i]); |
| 335 |
} |
| 336 |
array_ = std::move(new_array); |
| 337 |
|
| 338 |
return true; |
| 339 |
} |
| 340 |
|
| 341 |
// |size_| is the number of elements stored in this GrowableArray. |
| 342 |
size_t size_ = 0; |
| 343 |
// |array_| is the backing array. Note that |array_.size()| is this |
| 344 |
// GrowableArray's current capacity and that |size_ <= array_.size()|. |
| 345 |
Array<T> array_; |
| 346 |
// |kDefaultSize| is the default initial size of the backing array. |
| 347 |
static constexpr size_t kDefaultSize = 16; |
| 348 |
}; |
| 349 |
|
| 350 |
// CBBFinishArray behaves like |CBB_finish| but stores the result in an Array. |
| 351 |
OPENSSL_EXPORT bool CBBFinishArray(CBB *cbb, Array<uint8_t> *out); |
| 352 |
|
| 353 |
OPENSSL_EXPORT UniquePtr<CRYPTO_BUFFER> x509_to_buffer(X509 *x509); |
| 354 |
|
| 355 |
// GetAllNames helps to implement |*_get_all_*_names| style functions. It |
| 356 |
// writes at most |max_out| string pointers to |out| and returns the number that |
| 357 |
// it would have liked to have written. The strings written consist of |
| 358 |
// |fixed_names_len| strings from |fixed_names| followed by |objects_len| |
| 359 |
// strings taken by projecting |objects| through |name|. |
| 360 |
template <typename T, typename Name> |
| 361 |
inline size_t GetAllNames(const char **out, size_t max_out, |
| 362 |
Span<const char *const> fixed_names, Name(T::*name), |
| 363 |
Span<const T> objects) { |
| 364 |
auto span = bssl::MakeSpan(out, max_out); |
| 365 |
for (size_t i = 0; !span.empty() && i < fixed_names.size(); i++) { |
| 366 |
span[0] = fixed_names[i]; |
| 367 |
span = span.subspan(1); |
| 368 |
} |
| 369 |
span = span.subspan(0, objects.size()); |
| 370 |
for (size_t i = 0; i < span.size(); i++) { |
| 371 |
span[i] = objects[i].*name; |
| 372 |
} |
| 373 |
return fixed_names.size() + objects.size(); |
| 374 |
} |
| 375 |
|
| 376 |
// RefCounted is a common base for ref-counted types. This is an instance of the |
| 377 |
// C++ curiously-recurring template pattern, so a type Foo must subclass |
| 378 |
// RefCounted<Foo>. It additionally must friend RefCounted<Foo> to allow calling |
| 379 |
// the destructor. |
| 380 |
template <typename Derived> |
| 381 |
class RefCounted { |
| 382 |
public: |
| 383 |
RefCounted(const RefCounted &) = delete; |
| 384 |
RefCounted &operator=(const RefCounted &) = delete; |
| 385 |
|
| 386 |
// These methods are intentionally named differently from `bssl::UpRef` to |
| 387 |
// avoid a collision. Only the implementations of `FOO_up_ref` and `FOO_free` |
| 388 |
// should call these. |
| 389 |
void UpRefInternal() { CRYPTO_refcount_inc(&references_); } |
| 390 |
void DecRefInternal() { |
| 391 |
if (CRYPTO_refcount_dec_and_test_zero(&references_)) { |
| 392 |
Derived *d = static_cast<Derived *>(this); |
| 393 |
d->~Derived(); |
| 394 |
OPENSSL_free(d); |
| 395 |
} |
| 396 |
} |
| 397 |
|
| 398 |
protected: |
| 399 |
// Ensure that only `Derived`, which must inherit from `RefCounted<Derived>`, |
| 400 |
// can call the constructor. This catches bugs where someone inherited from |
| 401 |
// the wrong base. |
| 402 |
class CheckSubClass { |
| 403 |
private: |
| 404 |
friend Derived; |
| 405 |
CheckSubClass() = default; |
| 406 |
}; |
| 407 |
RefCounted(CheckSubClass) { |
| 408 |
static_assert(std::is_base_of<RefCounted, Derived>::value, |
| 409 |
"Derived must subclass RefCounted<Derived>"); |
| 410 |
} |
| 411 |
|
| 412 |
~RefCounted() = default; |
| 413 |
|
| 414 |
private: |
| 415 |
CRYPTO_refcount_t references_ = 1; |
| 416 |
}; |
| 417 |
|
| 418 |
|
| 419 |
// Protocol versions. |
| 420 |
// |
| 421 |
// Due to DTLS's historical wire version differences, we maintain two notions of |
| 422 |
// version. |
| 423 |
// |
| 424 |
// The "version" or "wire version" is the actual 16-bit value that appears on |
| 425 |
// the wire. It uniquely identifies a version and is also used at API |
| 426 |
// boundaries. The set of supported versions differs between TLS and DTLS. Wire |
| 427 |
// versions are opaque values and may not be compared numerically. |
| 428 |
// |
| 429 |
// The "protocol version" identifies the high-level handshake variant being |
| 430 |
// used. DTLS versions map to the corresponding TLS versions. Protocol versions |
| 431 |
// are sequential and may be compared numerically. |
| 432 |
|
| 433 |
// ssl_protocol_version_from_wire sets |*out| to the protocol version |
| 434 |
// corresponding to wire version |version| and returns true. If |version| is not |
| 435 |
// a valid TLS or DTLS version, it returns false. |
| 436 |
// |
| 437 |
// Note this simultaneously handles both DTLS and TLS. Use one of the |
| 438 |
// higher-level functions below for most operations. |
| 439 |
bool ssl_protocol_version_from_wire(uint16_t *out, uint16_t version); |
| 440 |
|
| 441 |
// ssl_get_version_range sets |*out_min_version| and |*out_max_version| to the |
| 442 |
// minimum and maximum enabled protocol versions, respectively. |
| 443 |
bool ssl_get_version_range(const SSL_HANDSHAKE *hs, uint16_t *out_min_version, |
| 444 |
uint16_t *out_max_version); |
| 445 |
|
| 446 |
// ssl_supports_version returns whether |hs| supports |version|. |
| 447 |
bool ssl_supports_version(const SSL_HANDSHAKE *hs, uint16_t version); |
| 448 |
|
| 449 |
// ssl_method_supports_version returns whether |method| supports |version|. |
| 450 |
bool ssl_method_supports_version(const SSL_PROTOCOL_METHOD *method, |
| 451 |
uint16_t version); |
| 452 |
|
| 453 |
// ssl_add_supported_versions writes the supported versions of |hs| to |cbb|, in |
| 454 |
// decreasing preference order. The version list is filtered to those whose |
| 455 |
// protocol version is at least |extra_min_version|. |
| 456 |
bool ssl_add_supported_versions(const SSL_HANDSHAKE *hs, CBB *cbb, |
| 457 |
uint16_t extra_min_version); |
| 458 |
|
| 459 |
// ssl_negotiate_version negotiates a common version based on |hs|'s preferences |
| 460 |
// and the peer preference list in |peer_versions|. On success, it returns true |
| 461 |
// and sets |*out_version| to the selected version. Otherwise, it returns false |
| 462 |
// and sets |*out_alert| to an alert to send. |
| 463 |
bool ssl_negotiate_version(SSL_HANDSHAKE *hs, uint8_t *out_alert, |
| 464 |
uint16_t *out_version, const CBS *peer_versions); |
| 465 |
|
| 466 |
// ssl_protocol_version returns |ssl|'s protocol version. It is an error to |
| 467 |
// call this function before the version is determined. |
| 468 |
uint16_t ssl_protocol_version(const SSL *ssl); |
| 469 |
|
| 470 |
// Cipher suites. |
| 471 |
|
| 472 |
BSSL_NAMESPACE_END |
| 473 |
|
| 474 |
struct ssl_cipher_st { |
| 475 |
// name is the OpenSSL name for the cipher. |
| 476 |
const char *name; |
| 477 |
// standard_name is the IETF name for the cipher. |
| 478 |
const char *standard_name; |
| 479 |
// id is the cipher suite value bitwise OR-d with 0x03000000. |
| 480 |
uint32_t id; |
| 481 |
|
| 482 |
// algorithm_* determine the cipher suite. See constants below for the values. |
| 483 |
uint32_t algorithm_mkey; |
| 484 |
uint32_t algorithm_auth; |
| 485 |
uint32_t algorithm_enc; |
| 486 |
uint32_t algorithm_mac; |
| 487 |
uint32_t algorithm_prf; |
| 488 |
}; |
| 489 |
|
| 490 |
// tls_print_all_supported_cipher_suites prints all supported cipher suites for |
| 491 |
// all TLS versions to stdout. |
| 492 |
OPENSSL_EXPORT bool tls_print_all_supported_cipher_suites( |
| 493 |
bool use_openssl_name); |
| 494 |
|
| 495 |
BSSL_NAMESPACE_BEGIN |
| 496 |
|
| 497 |
// Bits for |algorithm_mkey| (key exchange algorithm). |
| 498 |
#define SSL_kRSA 0x00000001u |
| 499 |
#define SSL_kECDHE 0x00000002u |
| 500 |
// SSL_kPSK is only set for plain PSK, not ECDHE_PSK. |
| 501 |
#define SSL_kPSK 0x00000004u |
| 502 |
#define SSL_kGENERIC 0x00000008u |
| 503 |
|
| 504 |
// Bits for |algorithm_auth| (server authentication). |
| 505 |
#define SSL_aRSA 0x00000001u |
| 506 |
#define SSL_aECDSA 0x00000002u |
| 507 |
// SSL_aPSK is set for both PSK and ECDHE_PSK. |
| 508 |
#define SSL_aPSK 0x00000004u |
| 509 |
#define SSL_aGENERIC 0x00000008u |
| 510 |
|
| 511 |
#define SSL_aCERT (SSL_aRSA | SSL_aECDSA) |
| 512 |
|
| 513 |
// Bits for |algorithm_enc| (symmetric encryption). |
| 514 |
#define SSL_3DES 0x00000001u |
| 515 |
#define SSL_AES128 0x00000002u |
| 516 |
#define SSL_AES256 0x00000004u |
| 517 |
#define SSL_AES128GCM 0x00000008u |
| 518 |
#define SSL_AES256GCM 0x00000010u |
| 519 |
#define SSL_eNULL 0x00000020u |
| 520 |
#define SSL_CHACHA20POLY1305 0x00000040u |
| 521 |
|
| 522 |
#define SSL_AES (SSL_AES128 | SSL_AES256 | SSL_AES128GCM | SSL_AES256GCM) |
| 523 |
|
| 524 |
// Bits for |algorithm_mac| (symmetric authentication). |
| 525 |
#define SSL_SHA1 0x00000001u |
| 526 |
// SSL_AEAD is set for all AEADs. |
| 527 |
#define SSL_AEAD 0x00000002u |
| 528 |
#define SSL_SHA256 0x00000003u |
| 529 |
#define SSL_SHA384 0x00000004u |
| 530 |
|
| 531 |
// Bits for |algorithm_prf| (handshake digest). |
| 532 |
#define SSL_HANDSHAKE_MAC_DEFAULT 0x1 |
| 533 |
#define SSL_HANDSHAKE_MAC_SHA256 0x2 |
| 534 |
#define SSL_HANDSHAKE_MAC_SHA384 0x4 |
| 535 |
|
| 536 |
// SSL_MAX_MD_SIZE is size of the largest hash function used in TLS, SHA-384. |
| 537 |
#define SSL_MAX_MD_SIZE 48 |
| 538 |
|
| 539 |
// An SSLCipherPreferenceList contains a list of SSL_CIPHERs with equal- |
| 540 |
// preference groups. For TLS clients, the groups are moot because the server |
| 541 |
// picks the cipher and groups cannot be expressed on the wire. However, for |
| 542 |
// servers, the equal-preference groups allow the client's preferences to be |
| 543 |
// partially respected. (This only has an effect with |
| 544 |
// SSL_OP_CIPHER_SERVER_PREFERENCE). Note that the method used to compute the |
| 545 |
// negotiated cipher suite in TLS 1.3 does not use the equal-preference |
| 546 |
// data structure described below. |
| 547 |
// |
| 548 |
// The equal-preference groups are expressed by grouping SSL_CIPHERs together. |
| 549 |
// All elements of a group have the same priority: no ordering is expressed |
| 550 |
// within a group. |
| 551 |
// |
| 552 |
// The values in |ciphers| are in one-to-one correspondence with |
| 553 |
// |in_group_flags|. (That is, sk_SSL_CIPHER_num(ciphers) is the number of |
| 554 |
// bytes in |in_group_flags|.) The bytes in |in_group_flags| are either 1, to |
| 555 |
// indicate that the corresponding SSL_CIPHER is not the last element of a |
| 556 |
// group, or 0 to indicate that it is. |
| 557 |
// |
| 558 |
// For example, if |in_group_flags| contains all zeros then that indicates a |
| 559 |
// traditional, fully-ordered preference. Every SSL_CIPHER is the last element |
| 560 |
// of the group (i.e. they are all in a one-element group). |
| 561 |
// |
| 562 |
// For a more complex example, consider: |
| 563 |
// ciphers: A B C D E F |
| 564 |
// in_group_flags: 1 1 0 0 1 0 |
| 565 |
// |
| 566 |
// That would express the following, order: |
| 567 |
// |
| 568 |
// A E |
| 569 |
// B -> D -> F |
| 570 |
// C |
| 571 |
struct SSLCipherPreferenceList { |
| 572 |
static constexpr bool kAllowUniquePtr = true; |
| 573 |
|
| 574 |
SSLCipherPreferenceList() = default; |
| 575 |
~SSLCipherPreferenceList(); |
| 576 |
|
| 577 |
bool Init(UniquePtr<STACK_OF(SSL_CIPHER)> ciphers, |
| 578 |
Span<const bool> in_group_flags); |
| 579 |
bool Init(const SSLCipherPreferenceList &); |
| 580 |
|
| 581 |
void Remove(const SSL_CIPHER *cipher); |
| 582 |
|
| 583 |
UniquePtr<STACK_OF(SSL_CIPHER)> ciphers; |
| 584 |
bool *in_group_flags = nullptr; |
| 585 |
}; |
| 586 |
|
| 587 |
// AllCiphers returns an array of all supported ciphers, sorted by id. |
| 588 |
Span<const SSL_CIPHER> AllCiphers(); |
| 589 |
|
| 590 |
// ssl_cipher_get_evp_aead sets |*out_aead| to point to the correct EVP_AEAD |
| 591 |
// object for |cipher| protocol version |version|. It sets |*out_mac_secret_len| |
| 592 |
// and |*out_fixed_iv_len| to the MAC key length and fixed IV length, |
| 593 |
// respectively. The MAC key length is zero except for legacy block and stream |
| 594 |
// ciphers. It returns true on success and false on error. |
| 595 |
bool ssl_cipher_get_evp_aead(const EVP_AEAD **out_aead, |
| 596 |
size_t *out_mac_secret_len, |
| 597 |
size_t *out_fixed_iv_len, const SSL_CIPHER *cipher, |
| 598 |
uint16_t version, bool is_dtls); |
| 599 |
|
| 600 |
// ssl_get_handshake_digest returns the |EVP_MD| corresponding to |version| and |
| 601 |
// |cipher|. |
| 602 |
const EVP_MD *ssl_get_handshake_digest(uint16_t version, |
| 603 |
const SSL_CIPHER *cipher); |
| 604 |
|
| 605 |
// ssl_create_cipher_list evaluates |rule_str|. It sets |*out_cipher_list| to a |
| 606 |
// newly-allocated |SSLCipherPreferenceList| containing the result. It returns |
| 607 |
// true on success and false on failure. If |strict| is true, nonsense will be |
| 608 |
// rejected. If false, nonsense will be silently ignored. If |config_tls13| is |
| 609 |
// true, only TLS 1.3 ciphers are considered in |ssl_cipher_collect_ciphers|. If |
| 610 |
// false, TLS 1.2 and below ciphers participate in |ssl_cipher_collect_ciphers|. |
| 611 |
// An empty result is considered an error regardless of |strict| or |
| 612 |
// |config_tls13|. |has_aes_hw| indicates if the list should be ordered based on |
| 613 |
// having support for AES in hardware or not. |
| 614 |
bool ssl_create_cipher_list(UniquePtr<SSLCipherPreferenceList> *out_cipher_list, |
| 615 |
const bool has_aes_hw, const char *rule_str, |
| 616 |
bool strict, bool config_tls13); |
| 617 |
|
| 618 |
// update_cipher_list creates a new |SSLCipherPreferenceList| containing ciphers |
| 619 |
// from both |ciphers| and |tls13_ciphers| and assigns it to |dst|. The function: |
| 620 |
// |
| 621 |
// 1. Creates a copy of |ciphers| |
| 622 |
// 2. Removes any stale TLS 1.3 ciphersuites from the copy |
| 623 |
// 3. Adds any configured TLS 1.3 ciphersuites from |tls13_ciphers| to the |
| 624 |
// front of the list. |
| 625 |
// 4. Combines |in_group_flags| from both input lists into |dst->in_group_flags| |
| 626 |
// |
| 627 |
// Returns one on success, zero on error. |
| 628 |
int update_cipher_list(UniquePtr<SSLCipherPreferenceList> &dst, |
| 629 |
UniquePtr<SSLCipherPreferenceList> &ciphers, |
| 630 |
UniquePtr<SSLCipherPreferenceList> &tls13_ciphers); |
| 631 |
|
| 632 |
// ssl_get_certificate_slot_index returns the |SSL_PKEY_*| certificate slot |
| 633 |
// index corresponding to the private key type of |pkey|. It returns -1 if not |
| 634 |
// supported. This was |ssl_cert_type| in OpenSSL 1.0.2. |
| 635 |
int ssl_get_certificate_slot_index(const EVP_PKEY *pkey); |
| 636 |
|
| 637 |
// ssl_cipher_auth_mask_for_key returns the mask of cipher |algorithm_auth| |
| 638 |
// values suitable for use with |key| in TLS 1.2 and below. |
| 639 |
uint32_t ssl_cipher_auth_mask_for_key(const EVP_PKEY *key); |
| 640 |
|
| 641 |
// ssl_cipher_uses_certificate_auth returns whether |cipher| authenticates the |
| 642 |
// server and, optionally, the client with a certificate. |
| 643 |
bool ssl_cipher_uses_certificate_auth(const SSL_CIPHER *cipher); |
| 644 |
|
| 645 |
// ssl_cipher_requires_server_key_exchange returns whether |cipher| requires a |
| 646 |
// ServerKeyExchange message. |
| 647 |
// |
| 648 |
// This function may return false while still allowing |cipher| an optional |
| 649 |
// ServerKeyExchange. This is the case for plain PSK ciphers. |
| 650 |
bool ssl_cipher_requires_server_key_exchange(const SSL_CIPHER *cipher); |
| 651 |
|
| 652 |
// ssl_cipher_get_record_split_len, for TLS 1.0 CBC mode ciphers, returns the |
| 653 |
// length of an encrypted 1-byte record, for use in record-splitting. Otherwise |
| 654 |
// it returns zero. |
| 655 |
size_t ssl_cipher_get_record_split_len(const SSL_CIPHER *cipher); |
| 656 |
|
| 657 |
// ssl_choose_tls13_cipher returns an |SSL_CIPHER| corresponding with the best |
| 658 |
// available from |client_cipher_suites| compatible with |version| and |
| 659 |
// configured |tls13_ciphers|. It returns NULL if there isn't a compatible |
| 660 |
// cipher. |has_aes_hw| indicates if the choice should be made as if support for |
| 661 |
// AES in hardware is available. |
| 662 |
const SSL_CIPHER *ssl_choose_tls13_cipher( |
| 663 |
const STACK_OF(SSL_CIPHER) *client_cipher_suites, bool has_aes_hw, uint16_t version, |
| 664 |
const STACK_OF(SSL_CIPHER) *tls13_ciphers); |
| 665 |
|
| 666 |
|
| 667 |
// Transcript layer. |
| 668 |
|
| 669 |
// SSLTranscript maintains the handshake transcript as a combination of a |
| 670 |
// buffer and running hash. |
| 671 |
class SSLTranscript { |
| 672 |
public: |
| 673 |
SSLTranscript(); |
| 674 |
~SSLTranscript(); |
| 675 |
|
| 676 |
SSLTranscript(SSLTranscript &&other) = default; |
| 677 |
SSLTranscript &operator=(SSLTranscript &&other) = default; |
| 678 |
|
| 679 |
// Init initializes the handshake transcript. If called on an existing |
| 680 |
// transcript, it resets the transcript and hash. It returns true on success |
| 681 |
// and false on failure. |
| 682 |
bool Init(); |
| 683 |
|
| 684 |
// InitHash initializes the handshake hash based on the PRF and contents of |
| 685 |
// the handshake transcript. Subsequent calls to |Update| will update the |
| 686 |
// rolling hash. It returns one on success and zero on failure. It is an error |
| 687 |
// to call this function after the handshake buffer is released. This may be |
| 688 |
// called multiple times to change the hash function. |
| 689 |
bool InitHash(uint16_t version, const SSL_CIPHER *cipher); |
| 690 |
|
| 691 |
// UpdateForHelloRetryRequest resets the rolling hash with the |
| 692 |
// HelloRetryRequest construction. It returns true on success and false on |
| 693 |
// failure. It is an error to call this function before the handshake buffer |
| 694 |
// is released. |
| 695 |
bool UpdateForHelloRetryRequest(); |
| 696 |
|
| 697 |
// CopyToHashContext initializes |ctx| with |digest| and the data thus far in |
| 698 |
// the transcript. It returns true on success and false on failure. If the |
| 699 |
// handshake buffer is still present, |digest| may be any supported digest. |
| 700 |
// Otherwise, |digest| must match the transcript hash. |
| 701 |
bool CopyToHashContext(EVP_MD_CTX *ctx, const EVP_MD *digest) const; |
| 702 |
|
| 703 |
Span<const uint8_t> buffer() const { |
| 704 |
return MakeConstSpan(reinterpret_cast<const uint8_t *>(buffer_->data), |
| 705 |
buffer_->length); |
| 706 |
} |
| 707 |
|
| 708 |
// FreeBuffer releases the handshake buffer. Subsequent calls to |
| 709 |
// |Update| will not update the handshake buffer. |
| 710 |
void FreeBuffer(); |
| 711 |
|
| 712 |
// DigestLen returns the length of the PRF hash. |
| 713 |
size_t DigestLen() const; |
| 714 |
|
| 715 |
// Digest returns the PRF hash. For TLS 1.1 and below, this is |
| 716 |
// |EVP_md5_sha1|. |
| 717 |
const EVP_MD *Digest() const; |
| 718 |
|
| 719 |
// Update adds |in| to the handshake buffer and handshake hash, whichever is |
| 720 |
// enabled. It returns true on success and false on failure. |
| 721 |
bool Update(Span<const uint8_t> in); |
| 722 |
|
| 723 |
// GetHash writes the handshake hash to |out| which must have room for at |
| 724 |
// least |DigestLen| bytes. On success, it returns true and sets |*out_len| to |
| 725 |
// the number of bytes written. Otherwise, it returns false. |
| 726 |
bool GetHash(uint8_t *out, size_t *out_len) const; |
| 727 |
|
| 728 |
// GetFinishedMAC computes the MAC for the Finished message into the bytes |
| 729 |
// pointed by |out| and writes the number of bytes to |*out_len|. |out| must |
| 730 |
// have room for |EVP_MAX_MD_SIZE| bytes. It returns true on success and false |
| 731 |
// on failure. |
| 732 |
bool GetFinishedMAC(uint8_t *out, size_t *out_len, const SSL_SESSION *session, |
| 733 |
bool from_server) const; |
| 734 |
|
| 735 |
private: |
| 736 |
// buffer_, if non-null, contains the handshake transcript. |
| 737 |
UniquePtr<BUF_MEM> buffer_; |
| 738 |
// hash, if initialized with an |EVP_MD|, maintains the handshake hash. |
| 739 |
ScopedEVP_MD_CTX hash_; |
| 740 |
}; |
| 741 |
|
| 742 |
// tls1_prf computes the PRF function for |ssl|. It fills |out|, using |secret| |
| 743 |
// as the secret and |label| as the label. |seed1| and |seed2| are concatenated |
| 744 |
// to form the seed parameter. It returns true on success and false on failure. |
| 745 |
bool tls1_prf(const EVP_MD *digest, Span<uint8_t> out, |
| 746 |
Span<const uint8_t> secret, Span<const char> label, |
| 747 |
Span<const uint8_t> seed1, Span<const uint8_t> seed2); |
| 748 |
|
| 749 |
|
| 750 |
// Encryption layer. |
| 751 |
|
| 752 |
// SSLAEADContext contains information about an AEAD that is being used to |
| 753 |
// encrypt an SSL connection. |
| 754 |
class SSLAEADContext { |
| 755 |
public: |
| 756 |
SSLAEADContext(uint16_t version, bool is_dtls, const SSL_CIPHER *cipher); |
| 757 |
~SSLAEADContext(); |
| 758 |
static constexpr bool kAllowUniquePtr = true; |
| 759 |
|
| 760 |
SSLAEADContext(const SSLAEADContext &&) = delete; |
| 761 |
SSLAEADContext &operator=(const SSLAEADContext &&) = delete; |
| 762 |
|
| 763 |
// CreateNullCipher creates an |SSLAEADContext| for the null cipher. |
| 764 |
static UniquePtr<SSLAEADContext> CreateNullCipher(bool is_dtls); |
| 765 |
|
| 766 |
// Create creates an |SSLAEADContext| using the supplied key material. It |
| 767 |
// returns nullptr on error. Only one of |Open| or |Seal| may be used with the |
| 768 |
// resulting object, depending on |direction|. |version| is the normalized |
| 769 |
// protocol version, so DTLS 1.0 is represented as 0x0301, not 0xffef. |
| 770 |
static UniquePtr<SSLAEADContext> Create(enum evp_aead_direction_t direction, |
| 771 |
uint16_t version, bool is_dtls, |
| 772 |
const SSL_CIPHER *cipher, |
| 773 |
Span<const uint8_t> enc_key, |
| 774 |
Span<const uint8_t> mac_key, |
| 775 |
Span<const uint8_t> fixed_iv); |
| 776 |
|
| 777 |
// CreatePlaceholderForQUIC creates a placeholder |SSLAEADContext| for the |
| 778 |
// given cipher and version. The resulting object can be queried for various |
| 779 |
// properties but cannot encrypt or decrypt data. |
| 780 |
static UniquePtr<SSLAEADContext> CreatePlaceholderForQUIC( |
| 781 |
uint16_t version, const SSL_CIPHER *cipher); |
| 782 |
|
| 783 |
// SetVersionIfNullCipher sets the version the SSLAEADContext for the null |
| 784 |
// cipher, to make version-specific determinations in the record layer prior |
| 785 |
// to a cipher being selected. |
| 786 |
void SetVersionIfNullCipher(uint16_t version); |
| 787 |
|
| 788 |
// ProtocolVersion returns the protocol version associated with this |
| 789 |
// SSLAEADContext. It can only be called once |version_| has been set to a |
| 790 |
// valid value. |
| 791 |
uint16_t ProtocolVersion() const; |
| 792 |
|
| 793 |
// RecordVersion returns the record version that should be used with this |
| 794 |
// SSLAEADContext for record construction and crypto. |
| 795 |
uint16_t RecordVersion() const; |
| 796 |
|
| 797 |
const SSL_CIPHER *cipher() const { return cipher_; } |
| 798 |
|
| 799 |
// is_null_cipher returns true if this is the null cipher. |
| 800 |
bool is_null_cipher() const { return !cipher_; } |
| 801 |
|
| 802 |
// ExplicitNonceLen returns the length of the explicit nonce. |
| 803 |
size_t ExplicitNonceLen() const; |
| 804 |
|
| 805 |
// MaxOverhead returns the maximum overhead of calling |Seal|. |
| 806 |
size_t MaxOverhead() const; |
| 807 |
|
| 808 |
// SuffixLen calculates the suffix length written by |SealScatter| and writes |
| 809 |
// it to |*out_suffix_len|. It returns true on success and false on error. |
| 810 |
// |in_len| and |extra_in_len| should equal the argument of the same names |
| 811 |
// passed to |SealScatter|. |
| 812 |
bool SuffixLen(size_t *out_suffix_len, size_t in_len, |
| 813 |
size_t extra_in_len) const; |
| 814 |
|
| 815 |
// CiphertextLen calculates the total ciphertext length written by |
| 816 |
// |SealScatter| and writes it to |*out_len|. It returns true on success and |
| 817 |
// false on error. |in_len| and |extra_in_len| should equal the argument of |
| 818 |
// the same names passed to |SealScatter|. |
| 819 |
bool CiphertextLen(size_t *out_len, size_t in_len, size_t extra_in_len) const; |
| 820 |
|
| 821 |
// Open authenticates and decrypts |in| in-place. On success, it sets |*out| |
| 822 |
// to the plaintext in |in| and returns true. Otherwise, it returns |
| 823 |
// false. The output will always be |ExplicitNonceLen| bytes ahead of |in|. |
| 824 |
bool Open(Span<uint8_t> *out, uint8_t type, uint16_t record_version, |
| 825 |
const uint8_t seqnum[8], Span<const uint8_t> header, |
| 826 |
Span<uint8_t> in); |
| 827 |
|
| 828 |
// Seal encrypts and authenticates |in_len| bytes from |in| and writes the |
| 829 |
// result to |out|. It returns true on success and false on error. |
| 830 |
// |
| 831 |
// If |in| and |out| alias then |out| + |ExplicitNonceLen| must be == |in|. |
| 832 |
bool Seal(uint8_t *out, size_t *out_len, size_t max_out, uint8_t type, |
| 833 |
uint16_t record_version, const uint8_t seqnum[8], |
| 834 |
Span<const uint8_t> header, const uint8_t *in, size_t in_len); |
| 835 |
|
| 836 |
// SealScatter encrypts and authenticates |in_len| bytes from |in| and splits |
| 837 |
// the result between |out_prefix|, |out| and |out_suffix|. It returns one on |
| 838 |
// success and zero on error. |
| 839 |
// |
| 840 |
// On successful return, exactly |ExplicitNonceLen| bytes are written to |
| 841 |
// |out_prefix|, |in_len| bytes to |out|, and |SuffixLen| bytes to |
| 842 |
// |out_suffix|. |
| 843 |
// |
| 844 |
// |extra_in| may point to an additional plaintext buffer. If present, |
| 845 |
// |extra_in_len| additional bytes are encrypted and authenticated, and the |
| 846 |
// ciphertext is written to the beginning of |out_suffix|. |SuffixLen| should |
| 847 |
// be used to size |out_suffix| accordingly. |
| 848 |
// |
| 849 |
// If |in| and |out| alias then |out| must be == |in|. Other arguments may not |
| 850 |
// alias anything. |
| 851 |
bool SealScatter(uint8_t *out_prefix, uint8_t *out, uint8_t *out_suffix, |
| 852 |
uint8_t type, uint16_t record_version, |
| 853 |
const uint8_t seqnum[8], Span<const uint8_t> header, |
| 854 |
const uint8_t *in, size_t in_len, const uint8_t *extra_in, |
| 855 |
size_t extra_in_len); |
| 856 |
|
| 857 |
bool GetIV(const uint8_t **out_iv, size_t *out_iv_len) const; |
| 858 |
|
| 859 |
int SerializeState(CBB *cbb) const; |
| 860 |
|
| 861 |
int DeserializeState(CBS *cbs) const; |
| 862 |
|
| 863 |
private: |
| 864 |
// GetAdditionalData returns the additional data, writing into |storage| if |
| 865 |
// necessary. |
| 866 |
Span<const uint8_t> GetAdditionalData(uint8_t storage[13], uint8_t type, |
| 867 |
uint16_t record_version, |
| 868 |
const uint8_t seqnum[8], |
| 869 |
size_t plaintext_len, |
| 870 |
Span<const uint8_t> header); |
| 871 |
|
| 872 |
const SSL_CIPHER *cipher_; |
| 873 |
ScopedEVP_AEAD_CTX ctx_; |
| 874 |
// fixed_nonce_ contains any bytes of the nonce that are fixed for all |
| 875 |
// records. |
| 876 |
uint8_t fixed_nonce_[12]; |
| 877 |
uint8_t fixed_nonce_len_ = 0, variable_nonce_len_ = 0; |
| 878 |
// version_ is the wire version that should be used with this AEAD. |
| 879 |
uint16_t version_; |
| 880 |
// is_dtls_ is whether DTLS is being used with this AEAD. |
| 881 |
bool is_dtls_; |
| 882 |
// variable_nonce_included_in_record_ is true if the variable nonce |
| 883 |
// for a record is included as a prefix before the ciphertext. |
| 884 |
bool variable_nonce_included_in_record_ : 1; |
| 885 |
// random_variable_nonce_ is true if the variable nonce is |
| 886 |
// randomly generated, rather than derived from the sequence |
| 887 |
// number. |
| 888 |
bool random_variable_nonce_ : 1; |
| 889 |
// xor_fixed_nonce_ is true if the fixed nonce should be XOR'd into the |
| 890 |
// variable nonce rather than prepended. |
| 891 |
bool xor_fixed_nonce_ : 1; |
| 892 |
// omit_length_in_ad_ is true if the length should be omitted in the |
| 893 |
// AEAD's ad parameter. |
| 894 |
bool omit_length_in_ad_ : 1; |
| 895 |
// ad_is_header_ is true if the AEAD's ad parameter is the record header. |
| 896 |
bool ad_is_header_ : 1; |
| 897 |
}; |
| 898 |
|
| 899 |
|
| 900 |
// DTLS replay bitmap. |
| 901 |
|
| 902 |
// DTLS1_BITMAP maintains a sliding window of 64 sequence numbers to detect |
| 903 |
// replayed packets. It should be initialized by zeroing every field. |
| 904 |
struct DTLS1_BITMAP { |
| 905 |
// map is a bitset of sequence numbers that have been seen. Bit i corresponds |
| 906 |
// to |max_seq_num - i|. |
| 907 |
std::bitset<256> map; |
| 908 |
// max_seq_num is the largest sequence number seen so far as a 64-bit |
| 909 |
// integer. |
| 910 |
uint64_t max_seq_num = 0; |
| 911 |
}; |
| 912 |
|
| 913 |
|
| 914 |
// Record layer. |
| 915 |
|
| 916 |
// ssl_record_sequence_update increments the sequence number in |seq|. It |
| 917 |
// returns true on success and false on wraparound. |
| 918 |
bool ssl_record_sequence_update(uint8_t *seq, size_t seq_len); |
| 919 |
|
| 920 |
// ssl_record_prefix_len returns the length of the prefix before the ciphertext |
| 921 |
// of a record for |ssl|. |
| 922 |
// |
| 923 |
// TODO(davidben): Expose this as part of public API once the high-level |
| 924 |
// buffer-free APIs are available. |
| 925 |
size_t ssl_record_prefix_len(const SSL *ssl); |
| 926 |
|
| 927 |
enum ssl_open_record_t { |
| 928 |
ssl_open_record_success, |
| 929 |
ssl_open_record_discard, |
| 930 |
ssl_open_record_partial, |
| 931 |
ssl_open_record_close_notify, |
| 932 |
ssl_open_record_error, |
| 933 |
}; |
| 934 |
|
| 935 |
// tls_open_record decrypts a record from |in| in-place. |
| 936 |
// |
| 937 |
// If the input did not contain a complete record, it returns |
| 938 |
// |ssl_open_record_partial|. It sets |*out_consumed| to the total number of |
| 939 |
// bytes necessary. It is guaranteed that a successful call to |tls_open_record| |
| 940 |
// will consume at least that many bytes. |
| 941 |
// |
| 942 |
// Otherwise, it sets |*out_consumed| to the number of bytes of input |
| 943 |
// consumed. Note that input may be consumed on all return codes if a record was |
| 944 |
// decrypted. |
| 945 |
// |
| 946 |
// On success, it returns |ssl_open_record_success|. It sets |*out_type| to the |
| 947 |
// record type and |*out| to the record body in |in|. Note that |*out| may be |
| 948 |
// empty. |
| 949 |
// |
| 950 |
// If a record was successfully processed but should be discarded, it returns |
| 951 |
// |ssl_open_record_discard|. |
| 952 |
// |
| 953 |
// If a record was successfully processed but is a close_notify, it returns |
| 954 |
// |ssl_open_record_close_notify|. |
| 955 |
// |
| 956 |
// On failure or fatal alert, it returns |ssl_open_record_error| and sets |
| 957 |
// |*out_alert| to an alert to emit, or zero if no alert should be emitted. |
| 958 |
enum ssl_open_record_t tls_open_record(SSL *ssl, uint8_t *out_type, |
| 959 |
Span<uint8_t> *out, size_t *out_consumed, |
| 960 |
uint8_t *out_alert, Span<uint8_t> in); |
| 961 |
|
| 962 |
// dtls_open_record implements |tls_open_record| for DTLS. It only returns |
| 963 |
// |ssl_open_record_partial| if |in| was empty and sets |*out_consumed| to |
| 964 |
// zero. The caller should read one packet and try again. |
| 965 |
enum ssl_open_record_t dtls_open_record(SSL *ssl, uint8_t *out_type, |
| 966 |
Span<uint8_t> *out, |
| 967 |
size_t *out_consumed, |
| 968 |
uint8_t *out_alert, Span<uint8_t> in); |
| 969 |
|
| 970 |
// ssl_seal_align_prefix_len returns the length of the prefix before the start |
| 971 |
// of the bulk of the ciphertext when sealing a record with |ssl|. Callers may |
| 972 |
// use this to align buffers. |
| 973 |
// |
| 974 |
// Note when TLS 1.0 CBC record-splitting is enabled, this includes the one byte |
| 975 |
// record and is the offset into second record's ciphertext. Thus sealing a |
| 976 |
// small record may result in a smaller output than this value. |
| 977 |
// |
| 978 |
// TODO(davidben): Is this alignment valuable? Record-splitting makes this a |
| 979 |
// mess. |
| 980 |
size_t ssl_seal_align_prefix_len(const SSL *ssl); |
| 981 |
|
| 982 |
// tls_seal_record seals a new record of type |type| and body |in| and writes it |
| 983 |
// to |out|. At most |max_out| bytes will be written. It returns true on success |
| 984 |
// and false on error. If enabled, |tls_seal_record| implements TLS 1.0 CBC |
| 985 |
// 1/n-1 record splitting and may write two records concatenated. |
| 986 |
// |
| 987 |
// For a large record, the bulk of the ciphertext will begin |
| 988 |
// |ssl_seal_align_prefix_len| bytes into out. Aligning |out| appropriately may |
| 989 |
// improve performance. It writes at most |in_len| + |SSL_max_seal_overhead| |
| 990 |
// bytes to |out|. |
| 991 |
// |
| 992 |
// |in| and |out| may not alias. |
| 993 |
bool tls_seal_record(SSL *ssl, uint8_t *out, size_t *out_len, size_t max_out, |
| 994 |
uint8_t type, const uint8_t *in, size_t in_len); |
| 995 |
|
| 996 |
enum dtls1_use_epoch_t { |
| 997 |
dtls1_use_previous_epoch, |
| 998 |
dtls1_use_current_epoch, |
| 999 |
}; |
| 1000 |
|
| 1001 |
// dtls_max_seal_overhead returns the maximum overhead, in bytes, of sealing a |
| 1002 |
// record. |
| 1003 |
size_t dtls_max_seal_overhead(const SSL *ssl, enum dtls1_use_epoch_t use_epoch); |
| 1004 |
|
| 1005 |
// dtls_seal_prefix_len returns the number of bytes of prefix to reserve in |
| 1006 |
// front of the plaintext when sealing a record in-place. |
| 1007 |
size_t dtls_seal_prefix_len(const SSL *ssl, enum dtls1_use_epoch_t use_epoch); |
| 1008 |
|
| 1009 |
// dtls_seal_record implements |tls_seal_record| for DTLS. |use_epoch| selects |
| 1010 |
// which epoch's cipher state to use. Unlike |tls_seal_record|, |in| and |out| |
| 1011 |
// may alias but, if they do, |in| must be exactly |dtls_seal_prefix_len| bytes |
| 1012 |
// ahead of |out|. |
| 1013 |
bool dtls_seal_record(SSL *ssl, uint8_t *out, size_t *out_len, size_t max_out, |
| 1014 |
uint8_t type, const uint8_t *in, size_t in_len, |
| 1015 |
enum dtls1_use_epoch_t use_epoch); |
| 1016 |
|
| 1017 |
// ssl_process_alert processes |in| as an alert and updates |ssl|'s shutdown |
| 1018 |
// state. It returns one of |ssl_open_record_discard|, |ssl_open_record_error|, |
| 1019 |
// |ssl_open_record_close_notify|, or |ssl_open_record_fatal_alert| as |
| 1020 |
// appropriate. |
| 1021 |
enum ssl_open_record_t ssl_process_alert(SSL *ssl, uint8_t *out_alert, |
| 1022 |
Span<const uint8_t> in); |
| 1023 |
|
| 1024 |
|
| 1025 |
// Private key operations. |
| 1026 |
|
| 1027 |
// ssl_has_private_key returns whether |hs| has a private key configured. |
| 1028 |
bool ssl_has_private_key(const SSL_HANDSHAKE *hs); |
| 1029 |
|
| 1030 |
// ssl_private_key_* perform the corresponding operation on |
| 1031 |
// |SSL_PRIVATE_KEY_METHOD|. If there is a custom private key configured, they |
| 1032 |
// call the corresponding function or |complete| depending on whether there is a |
| 1033 |
// pending operation. Otherwise, they implement the operation with |
| 1034 |
// |EVP_PKEY|. |
| 1035 |
|
| 1036 |
enum ssl_private_key_result_t ssl_private_key_sign( |
| 1037 |
SSL_HANDSHAKE *hs, uint8_t *out, size_t *out_len, size_t max_out, |
| 1038 |
uint16_t sigalg, Span<const uint8_t> in); |
| 1039 |
|
| 1040 |
enum ssl_private_key_result_t ssl_private_key_decrypt(SSL_HANDSHAKE *hs, |
| 1041 |
uint8_t *out, |
| 1042 |
size_t *out_len, |
| 1043 |
size_t max_out, |
| 1044 |
Span<const uint8_t> in); |
| 1045 |
|
| 1046 |
// ssl_public_key_supports_signature_algorithm returns whether |hs|'s extracted |
| 1047 |
// public key supports |sigalg|. |
| 1048 |
bool ssl_public_key_supports_signature_algorithm(SSL_HANDSHAKE *hs, |
| 1049 |
uint16_t sigalg); |
| 1050 |
|
| 1051 |
// ssl_public_key_supports_legacy_signature_algorithm is the tls1.0/1.1 |
| 1052 |
// version of |ssl_public_key_supports_signature_algorithm|. |
| 1053 |
bool ssl_public_key_supports_legacy_signature_algorithm(uint16_t *out, |
| 1054 |
SSL_HANDSHAKE *hs); |
| 1055 |
|
| 1056 |
// ssl_cert_private_keys_supports_signature_algorithm returns whether any of |
| 1057 |
// |hs|'s available private keys supports |sigalg|. If one does, we switch to |
| 1058 |
// using that private key and the corresponding certificate for the rest of the |
| 1059 |
// connection. |hs->local_pubkey| is also updated correspondingly. |
| 1060 |
// |
| 1061 |
// NOTE: Multiple certificate slots is only supported on the server side, when |
| 1062 |
// not using designated credentials. |
| 1063 |
bool ssl_cert_private_keys_supports_signature_algorithm(SSL_HANDSHAKE *hs, |
| 1064 |
uint16_t sigalg); |
| 1065 |
|
| 1066 |
// ssl_cert_private_keys_supports_legacy_signature_algorithm is the tls1.0/1.1 |
| 1067 |
// version of |ssl_cert_private_keys_supports_signature_algorithm|. |
| 1068 |
bool ssl_cert_private_keys_supports_legacy_signature_algorithm( |
| 1069 |
uint16_t *out, SSL_HANDSHAKE *hs); |
| 1070 |
|
| 1071 |
// ssl_public_key_verify verifies that the |signature| is valid for the public |
| 1072 |
// key |pkey| and input |in|, using the signature algorithm |sigalg|. |
| 1073 |
bool ssl_public_key_verify(SSL *ssl, Span<const uint8_t> signature, |
| 1074 |
uint16_t sigalg, EVP_PKEY *pkey, |
| 1075 |
Span<const uint8_t> in); |
| 1076 |
|
| 1077 |
|
| 1078 |
// Custom extensions |
| 1079 |
|
| 1080 |
BSSL_NAMESPACE_END |
| 1081 |
|
| 1082 |
// |SSL_CUSTOM_EXTENSION| is a structure that contains information about |
| 1083 |
// custom-extension callbacks. It is defined unnamespaced for compatibility with |
| 1084 |
// |STACK_OF(SSL_CUSTOM_EXTENSION)|. |
| 1085 |
typedef struct ssl_custom_extension { |
| 1086 |
SSL_custom_ext_add_cb add_callback; |
| 1087 |
void *add_arg; |
| 1088 |
SSL_custom_ext_free_cb free_callback; |
| 1089 |
SSL_custom_ext_parse_cb parse_callback; |
| 1090 |
void *parse_arg; |
| 1091 |
uint16_t value; |
| 1092 |
} SSL_CUSTOM_EXTENSION; |
| 1093 |
|
| 1094 |
DEFINE_STACK_OF(SSL_CUSTOM_EXTENSION) |
| 1095 |
|
| 1096 |
BSSL_NAMESPACE_BEGIN |
| 1097 |
|
| 1098 |
void SSL_CUSTOM_EXTENSION_free(SSL_CUSTOM_EXTENSION *custom_extension); |
| 1099 |
|
| 1100 |
int custom_ext_add_clienthello(SSL_HANDSHAKE *hs, CBB *extensions); |
| 1101 |
int custom_ext_parse_serverhello(SSL_HANDSHAKE *hs, int *out_alert, |
| 1102 |
uint16_t value, const CBS *extension); |
| 1103 |
int custom_ext_parse_clienthello(SSL_HANDSHAKE *hs, int *out_alert, |
| 1104 |
uint16_t value, const CBS *extension); |
| 1105 |
int custom_ext_add_serverhello(SSL_HANDSHAKE *hs, CBB *extensions); |
| 1106 |
|
| 1107 |
|
| 1108 |
// Key shares. |
| 1109 |
|
| 1110 |
// SSLKeyShare abstracts over Diffie-Hellman-like key exchanges. |
| 1111 |
class SSLKeyShare { |
| 1112 |
public: |
| 1113 |
virtual ~SSLKeyShare() {} |
| 1114 |
static constexpr bool kAllowUniquePtr = true; |
| 1115 |
HAS_VIRTUAL_DESTRUCTOR |
| 1116 |
|
| 1117 |
// Create returns a SSLKeyShare instance for use with group |group_id| or |
| 1118 |
// nullptr on error. Marked with OPENSSL_EXPORT to make it available for |
| 1119 |
// unit tests. |
| 1120 |
OPENSSL_EXPORT static UniquePtr<SSLKeyShare> Create(uint16_t group_id); |
| 1121 |
|
| 1122 |
// GroupID returns the group ID. |
| 1123 |
virtual uint16_t GroupID() const PURE_VIRTUAL; |
| 1124 |
|
| 1125 |
// Offer generates a keypair and writes the public value to |
| 1126 |
// |out_public_key|. It returns true on success and false on error. |
| 1127 |
virtual bool Offer(CBB *out_public_key) PURE_VIRTUAL; |
| 1128 |
|
| 1129 |
// Accept performs a key exchange against the |peer_key| generated by |Offer|. |
| 1130 |
// On success, it returns true, writes the public value to |out_public_key|, |
| 1131 |
// and sets |*out_secret| to the shared secret. On failure, it returns false |
| 1132 |
// and sets |*out_alert| to an alert to send to the peer. |
| 1133 |
// |
| 1134 |
// The default implementation calls |Offer| and then |Finish|, assuming a key |
| 1135 |
// exchange protocol where the peers are symmetric. |
| 1136 |
virtual bool Accept(CBB *out_public_key, Array<uint8_t> *out_secret, |
| 1137 |
uint8_t *out_alert, Span<const uint8_t> peer_key); |
| 1138 |
|
| 1139 |
// Finish performs a key exchange against the |peer_key| generated by |
| 1140 |
// |Accept|. On success, it returns true and sets |*out_secret| to the shared |
| 1141 |
// secret. On failure, it returns false and sets |*out_alert| to an alert to |
| 1142 |
// send to the peer. |
| 1143 |
virtual bool Finish(Array<uint8_t> *out_secret, uint8_t *out_alert, |
| 1144 |
Span<const uint8_t> peer_key) PURE_VIRTUAL; |
| 1145 |
|
| 1146 |
// SerializePrivateKey writes the private key to |out|, returning true if |
| 1147 |
// successful and false otherwise. It should be called after |Offer|. |
| 1148 |
virtual bool SerializePrivateKey(CBB *out) { return false; } |
| 1149 |
|
| 1150 |
// DeserializePrivateKey initializes the state of the key exchange from |in|, |
| 1151 |
// returning true if successful and false otherwise. |
| 1152 |
virtual bool DeserializePrivateKey(CBS *in) { return false; } |
| 1153 |
}; |
| 1154 |
|
| 1155 |
struct NamedGroup { |
| 1156 |
int nid; |
| 1157 |
uint16_t group_id; |
| 1158 |
const char name[32], alias[32]; |
| 1159 |
}; |
| 1160 |
|
| 1161 |
// NamedGroups returns all supported groups. |
| 1162 |
Span<const NamedGroup> NamedGroups(); |
| 1163 |
|
| 1164 |
// Hybrid groups adhere to the draft specification: |
| 1165 |
// https://datatracker.ietf.org/doc/html/draft-ietf-tls-hybrid-design |
| 1166 |
#define NUM_HYBRID_COMPONENTS 2 |
| 1167 |
struct HybridGroup { |
| 1168 |
// Just like regular groups, each hybrid group has a group ID that |
| 1169 |
// identifies the group as a whole unit... |
| 1170 |
uint16_t group_id; |
| 1171 |
|
| 1172 |
// ...and each component of the hybrid group has its own group ID |
| 1173 |
uint16_t component_group_ids[NUM_HYBRID_COMPONENTS]; |
| 1174 |
}; |
| 1175 |
|
| 1176 |
// HybridGroups returns all supported hybrid groups. A hybrid group will likely |
| 1177 |
// (but not necessarily) contain at least one PQ group. Marked with |
| 1178 |
// OPENSSL_EXPORT to make it available for unit tests. |
| 1179 |
OPENSSL_EXPORT Span<const HybridGroup> HybridGroups(); |
| 1180 |
|
| 1181 |
// PQGroups returns all supported post-quantum groups. A post-quantum |
| 1182 |
// group may be a hybrid group containing at least one PQ |
| 1183 |
// component or a standalone PQ group. |
| 1184 |
Span<const uint16_t> PQGroups(); |
| 1185 |
|
| 1186 |
// ssl_nid_to_group_id looks up the group corresponding to |nid|. On success, it |
| 1187 |
// sets |*out_group_id| to the group ID and returns true. Otherwise, it returns |
| 1188 |
// false. |
| 1189 |
bool ssl_nid_to_group_id(uint16_t *out_group_id, int nid); |
| 1190 |
|
| 1191 |
// ssl_nid_to_group_id looks up the group corresponding to |group_id|. On |
| 1192 |
// success, it sets |*out_nid| to the group's nid and returns true. Otherwise, |
| 1193 |
// it returns false. |
| 1194 |
bool ssl_group_id_to_nid(uint16_t *out_nid, int group_id); |
| 1195 |
|
| 1196 |
// ssl_name_to_group_id looks up the group corresponding to the |name| string of |
| 1197 |
// length |len|. On success, it sets |*out_group_id| to the group ID and returns |
| 1198 |
// true. Otherwise, it returns false. |
| 1199 |
bool ssl_name_to_group_id(uint16_t *out_group_id, const char *name, size_t len); |
| 1200 |
|
| 1201 |
// ssl_group_id_to_nid returns the NID corresponding to |group_id| or |
| 1202 |
// |NID_undef| if unknown. |
| 1203 |
int ssl_group_id_to_nid(uint16_t group_id); |
| 1204 |
|
| 1205 |
|
| 1206 |
// Handshake messages. |
| 1207 |
|
| 1208 |
struct SSLMessage { |
| 1209 |
bool is_v2_hello; |
| 1210 |
uint8_t type; |
| 1211 |
CBS body; |
| 1212 |
// raw is the entire serialized handshake message, including the TLS or DTLS |
| 1213 |
// message header. |
| 1214 |
CBS raw; |
| 1215 |
}; |
| 1216 |
|
| 1217 |
// SSL_MAX_HANDSHAKE_FLIGHT is the number of messages, including |
| 1218 |
// ChangeCipherSpec, in the longest handshake flight. Currently this is the |
| 1219 |
// client's second leg in a full handshake when client certificates, NPN, and |
| 1220 |
// Channel ID, are all enabled. |
| 1221 |
#define SSL_MAX_HANDSHAKE_FLIGHT 7 |
| 1222 |
|
| 1223 |
extern const uint8_t kHelloRetryRequest[SSL3_RANDOM_SIZE]; |
| 1224 |
extern const uint8_t kTLS12DowngradeRandom[8]; |
| 1225 |
extern const uint8_t kTLS13DowngradeRandom[8]; |
| 1226 |
extern const uint8_t kJDK11DowngradeRandom[8]; |
| 1227 |
|
| 1228 |
// ssl_max_handshake_message_len returns the maximum number of bytes permitted |
| 1229 |
// in a handshake message for |ssl|. |
| 1230 |
size_t ssl_max_handshake_message_len(const SSL *ssl); |
| 1231 |
|
| 1232 |
// tls_can_accept_handshake_data returns whether |ssl| is able to accept more |
| 1233 |
// data into handshake buffer. |
| 1234 |
bool tls_can_accept_handshake_data(const SSL *ssl, uint8_t *out_alert); |
| 1235 |
|
| 1236 |
// tls_has_unprocessed_handshake_data returns whether there is buffered |
| 1237 |
// handshake data that has not been consumed by |get_message|. |
| 1238 |
bool tls_has_unprocessed_handshake_data(const SSL *ssl); |
| 1239 |
|
| 1240 |
// tls_append_handshake_data appends |data| to the handshake buffer. It returns |
| 1241 |
// true on success and false on allocation failure. |
| 1242 |
bool tls_append_handshake_data(SSL *ssl, Span<const uint8_t> data); |
| 1243 |
|
| 1244 |
// dtls_has_unprocessed_handshake_data behaves like |
| 1245 |
// |tls_has_unprocessed_handshake_data| for DTLS. |
| 1246 |
bool dtls_has_unprocessed_handshake_data(const SSL *ssl); |
| 1247 |
|
| 1248 |
// tls_flush_pending_hs_data flushes any handshake plaintext data. |
| 1249 |
bool tls_flush_pending_hs_data(SSL *ssl); |
| 1250 |
|
| 1251 |
struct DTLS_OUTGOING_MESSAGE { |
| 1252 |
DTLS_OUTGOING_MESSAGE() {} |
| 1253 |
DTLS_OUTGOING_MESSAGE(const DTLS_OUTGOING_MESSAGE &) = delete; |
| 1254 |
DTLS_OUTGOING_MESSAGE &operator=(const DTLS_OUTGOING_MESSAGE &) = delete; |
| 1255 |
|
| 1256 |
void Clear(); |
| 1257 |
|
| 1258 |
Array<uint8_t> data; |
| 1259 |
uint16_t epoch = 0; |
| 1260 |
bool is_ccs = false; |
| 1261 |
}; |
| 1262 |
|
| 1263 |
// dtls_clear_outgoing_messages releases all buffered outgoing messages. |
| 1264 |
void dtls_clear_outgoing_messages(SSL *ssl); |
| 1265 |
|
| 1266 |
|
| 1267 |
// Callbacks. |
| 1268 |
|
| 1269 |
// ssl_do_info_callback calls |ssl|'s info callback, if set. |
| 1270 |
void ssl_do_info_callback(const SSL *ssl, int type, int value); |
| 1271 |
|
| 1272 |
// ssl_do_msg_callback calls |ssl|'s message callback, if set. |
| 1273 |
void ssl_do_msg_callback(const SSL *ssl, int is_write, int content_type, |
| 1274 |
Span<const uint8_t> in); |
| 1275 |
|
| 1276 |
|
| 1277 |
// Transport buffers. |
| 1278 |
|
| 1279 |
enum SSL_BUFFER_SERDE_VERSION { |
| 1280 |
SSL_BUFFER_SERDE_VERSION_ONE = 1, |
| 1281 |
SSL_BUFFER_SERDE_VERSION_TWO = 2 |
| 1282 |
}; |
| 1283 |
|
| 1284 |
const unsigned kSSLBufferMaxSerDeVersion = SSL_BUFFER_SERDE_VERSION_TWO; |
| 1285 |
|
| 1286 |
#define SSLBUFFER_READ_AHEAD_MIN_CAPACITY 512 |
| 1287 |
#define SSLBUFFER_MAX_CAPACITY INT_MAX |
| 1288 |
class OPENSSL_EXPORT SSLBuffer { |
| 1289 |
public: |
| 1290 |
SSLBuffer() {} |
| 1291 |
~SSLBuffer() { Clear(); } |
| 1292 |
|
| 1293 |
SSLBuffer(const SSLBuffer &) = delete; |
| 1294 |
SSLBuffer &operator=(const SSLBuffer &) = delete; |
| 1295 |
|
| 1296 |
uint8_t *buf_ptr() { return buf_; } |
| 1297 |
uint8_t *data() { return buf_ + offset_; } |
| 1298 |
size_t size() const { return size_; } |
| 1299 |
bool empty() const { return size_ == 0; } |
| 1300 |
size_t cap() const { return cap_; } |
| 1301 |
size_t buf_size() const { return buf_size_; } |
| 1302 |
|
| 1303 |
Span<uint8_t> span() { return MakeSpan(data(), size()); } |
| 1304 |
|
| 1305 |
Span<uint8_t> remaining() { |
| 1306 |
return MakeSpan(data() + size(), cap() - size()); |
| 1307 |
} |
| 1308 |
|
| 1309 |
// Clear releases the buffer. |
| 1310 |
void Clear(); |
| 1311 |
|
| 1312 |
// EnsureCap ensures the buffer has capacity at least |new_cap|, aligned such |
| 1313 |
// that data written after |header_len| is aligned to a |
| 1314 |
// |SSL3_ALIGN_PAYLOAD|-byte boundary. It returns true on success and false |
| 1315 |
// on error. |
| 1316 |
bool EnsureCap(size_t header_len, size_t new_cap); |
| 1317 |
|
| 1318 |
// DidWrite extends the buffer by |len|. The caller must have filled in to |
| 1319 |
// this point. |
| 1320 |
void DidWrite(size_t len); |
| 1321 |
|
| 1322 |
// Consume consumes |len| bytes from the front of the buffer. The memory |
| 1323 |
// consumed will remain valid until the next call to |DiscardConsumed| or |
| 1324 |
// |Clear|. |
| 1325 |
void Consume(size_t len); |
| 1326 |
|
| 1327 |
// DiscardConsumed discards the consumed bytes from the buffer. If the buffer |
| 1328 |
// is now empty, it releases memory used by it. |
| 1329 |
void DiscardConsumed(); |
| 1330 |
|
| 1331 |
// DoSerialization writes all fields into |cbb|. |
| 1332 |
bool DoSerialization(CBB &cbb); |
| 1333 |
|
| 1334 |
// DoDeserialization recovers the states encoded via |DoSerialization|. |
| 1335 |
bool DoDeserialization(CBS &in); |
| 1336 |
|
| 1337 |
bool SerializeBufferView(CBB &cbb, Span<uint8_t> &view); |
| 1338 |
bool DeserializeBufferView(CBS &cbb, Span<uint8_t> &view); |
| 1339 |
|
| 1340 |
private: |
| 1341 |
// buf_ is the memory allocated for this buffer. |
| 1342 |
uint8_t *buf_ = nullptr; |
| 1343 |
// buf_allocated_ is true if |buf_| points to allocated data and must be freed |
| 1344 |
// or false if it points into |inline_buf_|. |
| 1345 |
bool buf_allocated_ = false; |
| 1346 |
// The total capacity requested for this buffer by |EnsureCap|. |
| 1347 |
size_t buf_cap_ = 0; |
| 1348 |
// buf_size_ is how much memory allocated for |buf_|. This is needed by |
| 1349 |
// |DoSerializationV1|. This is the total size of the buffer with the requested capacity + padding. |
| 1350 |
size_t buf_size_ = 0; |
| 1351 |
// header length used to calculate initial offset |
| 1352 |
size_t header_len_ = 0; |
| 1353 |
// offset_ is the offset into |buf_| which the buffer contents start at, and is moved as contents are consumed |
| 1354 |
int offset_ = 0; |
| 1355 |
// size_ is the size of the buffer contents from |buf_| + |offset_|. |
| 1356 |
int size_ = 0; |
| 1357 |
// cap_ is how much memory beyond |buf_| + |offset_| is available. |
| 1358 |
int cap_ = 0; |
| 1359 |
// inline_buf_ is a static buffer for short reads. |
| 1360 |
uint8_t inline_buf_[SSL3_RT_HEADER_LENGTH]; |
| 1361 |
|
| 1362 |
// The V1 version has some intricacies were solved in later serialization versions. |
| 1363 |
// This is mainly to capture if a V1 version was restored and whether it needs to be |
| 1364 |
// re-serialized as that version. |
| 1365 |
uint32_t max_serialization_version_ = SSL_BUFFER_SERDE_VERSION_TWO; |
| 1366 |
|
| 1367 |
bool DoSerializationV1(CBB &cbb); |
| 1368 |
bool DoSerializationV2(CBB &cbb); |
| 1369 |
|
| 1370 |
bool DoDeserializationV1(CBS &in); |
| 1371 |
bool DoDeserializationV2(CBS &in); |
| 1372 |
|
| 1373 |
bool ValidateBuffersState(); |
| 1374 |
}; |
| 1375 |
|
| 1376 |
// ssl_read_buffer_extend_to extends the read buffer to the desired length. For |
| 1377 |
// TLS, it reads to the end of the buffer until the buffer is |len| bytes |
| 1378 |
// long. For DTLS, it reads a new packet and ignores |len|. It returns one on |
| 1379 |
// success, zero on EOF, and a negative number on error. |
| 1380 |
// |
| 1381 |
// It is an error to call |ssl_read_buffer_extend_to| in DTLS when the buffer is |
| 1382 |
// non-empty. |
| 1383 |
int ssl_read_buffer_extend_to(SSL *ssl, size_t len); |
| 1384 |
|
| 1385 |
// ssl_handle_open_record handles the result of passing |ssl->s3->read_buffer| |
| 1386 |
// to a record-processing function. If |ret| is a success or if the caller |
| 1387 |
// should retry, it returns one and sets |*out_retry|. Otherwise, it returns <= |
| 1388 |
// 0. |
| 1389 |
int ssl_handle_open_record(SSL *ssl, bool *out_retry, ssl_open_record_t ret, |
| 1390 |
size_t consumed, uint8_t alert); |
| 1391 |
|
| 1392 |
// ssl_write_buffer_flush flushes the write buffer to the transport. It returns |
| 1393 |
// one on success and <= 0 on error. For DTLS, whether or not the write |
| 1394 |
// succeeds, the write buffer will be cleared. |
| 1395 |
int ssl_write_buffer_flush(SSL *ssl); |
| 1396 |
|
| 1397 |
|
| 1398 |
// Certificate functions. |
| 1399 |
|
| 1400 |
// ssl_has_certificate returns whether a certificate and private key are |
| 1401 |
// configured. |
| 1402 |
bool ssl_has_certificate(const SSL_HANDSHAKE *hs); |
| 1403 |
|
| 1404 |
// ssl_parse_cert_chain parses a certificate list from |cbs| in the format used |
| 1405 |
// by a TLS Certificate message. On success, it advances |cbs| and returns |
| 1406 |
// true. Otherwise, it returns false and sets |*out_alert| to an alert to send |
| 1407 |
// to the peer. |
| 1408 |
// |
| 1409 |
// If the list is non-empty then |*out_chain| and |*out_pubkey| will be set to |
| 1410 |
// the certificate chain and the leaf certificate's public key |
| 1411 |
// respectively. Otherwise, both will be set to nullptr. |
| 1412 |
// |
| 1413 |
// If the list is non-empty and |out_leaf_sha256| is non-NULL, it writes the |
| 1414 |
// SHA-256 hash of the leaf to |out_leaf_sha256|. |
| 1415 |
bool ssl_parse_cert_chain(uint8_t *out_alert, |
| 1416 |
UniquePtr<STACK_OF(CRYPTO_BUFFER)> *out_chain, |
| 1417 |
UniquePtr<EVP_PKEY> *out_pubkey, |
| 1418 |
uint8_t *out_leaf_sha256, CBS *cbs, |
| 1419 |
CRYPTO_BUFFER_POOL *pool); |
| 1420 |
|
| 1421 |
// ssl_add_cert_chain adds |hs->ssl|'s certificate chain to |cbb| in the format |
| 1422 |
// used by a TLS Certificate message. If there is no certificate chain, it emits |
| 1423 |
// an empty certificate list. It returns true on success and false on error. |
| 1424 |
bool ssl_add_cert_chain(SSL_HANDSHAKE *hs, CBB *cbb); |
| 1425 |
|
| 1426 |
enum ssl_key_usage_t { |
| 1427 |
key_usage_digital_signature = 0, |
| 1428 |
key_usage_encipherment = 2, |
| 1429 |
}; |
| 1430 |
|
| 1431 |
// ssl_cert_check_key_usage parses the DER-encoded, X.509 certificate in |in| |
| 1432 |
// and returns true if doesn't specify a key usage or, if it does, if it |
| 1433 |
// includes |bit|. Otherwise it pushes to the error queue and returns false. |
| 1434 |
OPENSSL_EXPORT bool ssl_cert_check_key_usage(const CBS *in, |
| 1435 |
enum ssl_key_usage_t bit); |
| 1436 |
|
| 1437 |
// ssl_cert_parse_pubkey extracts the public key from the DER-encoded, X.509 |
| 1438 |
// certificate in |in|. It returns an allocated |EVP_PKEY| or else returns |
| 1439 |
// nullptr and pushes to the error queue. |
| 1440 |
UniquePtr<EVP_PKEY> ssl_cert_parse_pubkey(const CBS *in); |
| 1441 |
|
| 1442 |
// ssl_cert_parse_leaf_pubkey calls |ssl_cert_parse_pubkey| and extracts the |
| 1443 |
// public key from the first element of |chain|. It's expected that the first |
| 1444 |
// element of |chain| is the leaf certificate. |
| 1445 |
UniquePtr<EVP_PKEY> ssl_cert_parse_leaf_pubkey(STACK_OF(CRYPTO_BUFFER) *chain); |
| 1446 |
|
| 1447 |
// ssl_parse_client_CA_list parses a CA list from |cbs| in the format used by a |
| 1448 |
// TLS CertificateRequest message. On success, it returns a newly-allocated |
| 1449 |
// |CRYPTO_BUFFER| list and advances |cbs|. Otherwise, it returns nullptr and |
| 1450 |
// sets |*out_alert| to an alert to send to the peer. |
| 1451 |
UniquePtr<STACK_OF(CRYPTO_BUFFER)> ssl_parse_client_CA_list(SSL *ssl, |
| 1452 |
uint8_t *out_alert, |
| 1453 |
CBS *cbs); |
| 1454 |
|
| 1455 |
// ssl_has_client_CAs returns there are configured CAs. |
| 1456 |
bool ssl_has_client_CAs(const SSL_CONFIG *cfg); |
| 1457 |
|
| 1458 |
// ssl_add_client_CA_list adds the configured CA list to |cbb| in the format |
| 1459 |
// used by a TLS CertificateRequest message. It returns true on success and |
| 1460 |
// false on error. |
| 1461 |
bool ssl_add_client_CA_list(SSL_HANDSHAKE *hs, CBB *cbb); |
| 1462 |
|
| 1463 |
// ssl_check_leaf_certificate returns one if |pkey| and |leaf| are suitable as |
| 1464 |
// a server's leaf certificate for |hs|. Otherwise, it returns zero and pushes |
| 1465 |
// an error on the error queue. |
| 1466 |
bool ssl_check_leaf_certificate(SSL_HANDSHAKE *hs, EVP_PKEY *pkey, |
| 1467 |
const CRYPTO_BUFFER *leaf); |
| 1468 |
|
| 1469 |
// ssl_on_certificate_selected is called once the certificate has been selected. |
| 1470 |
// It finalizes the certificate and initializes |hs->local_pubkey|. It returns |
| 1471 |
// true on success and false on error. |
| 1472 |
bool ssl_on_certificate_selected(SSL_HANDSHAKE *hs); |
| 1473 |
|
| 1474 |
// ssl_handshake_load_local_pubkey loads |local_pubkey| in |hs| based on the |
| 1475 |
// current designated certificate. |
| 1476 |
bool ssl_handshake_load_local_pubkey(SSL_HANDSHAKE *hs); |
| 1477 |
|
| 1478 |
|
| 1479 |
// TLS 1.3 key derivation. |
| 1480 |
|
| 1481 |
// tls13_init_key_schedule initializes the handshake hash and key derivation |
| 1482 |
// state, and incorporates the PSK. The cipher suite and PRF hash must have been |
| 1483 |
// selected at this point. It returns true on success and false on error. |
| 1484 |
bool tls13_init_key_schedule(SSL_HANDSHAKE *hs, Span<const uint8_t> psk); |
| 1485 |
|
| 1486 |
// tls13_init_early_key_schedule initializes the handshake hash and key |
| 1487 |
// derivation state from |session| for use with 0-RTT. It returns one on success |
| 1488 |
// and zero on error. |
| 1489 |
bool tls13_init_early_key_schedule(SSL_HANDSHAKE *hs, |
| 1490 |
const SSL_SESSION *session); |
| 1491 |
|
| 1492 |
// tls13_advance_key_schedule incorporates |in| into the key schedule with |
| 1493 |
// HKDF-Extract. It returns true on success and false on error. |
| 1494 |
bool tls13_advance_key_schedule(SSL_HANDSHAKE *hs, Span<const uint8_t> in); |
| 1495 |
|
| 1496 |
// tls13_set_traffic_key sets the read or write traffic keys to |
| 1497 |
// |traffic_secret|. The version and cipher suite are determined from |session|. |
| 1498 |
// It returns true on success and false on error. |
| 1499 |
bool tls13_set_traffic_key(SSL *ssl, enum ssl_encryption_level_t level, |
| 1500 |
enum evp_aead_direction_t direction, |
| 1501 |
const SSL_SESSION *session, |
| 1502 |
Span<const uint8_t> traffic_secret); |
| 1503 |
|
| 1504 |
// tls13_derive_early_secret derives the early traffic secret. It returns true |
| 1505 |
// on success and false on error. |
| 1506 |
bool tls13_derive_early_secret(SSL_HANDSHAKE *hs); |
| 1507 |
|
| 1508 |
// tls13_derive_handshake_secrets derives the handshake traffic secret. It |
| 1509 |
// returns true on success and false on error. |
| 1510 |
bool tls13_derive_handshake_secrets(SSL_HANDSHAKE *hs); |
| 1511 |
|
| 1512 |
// tls13_rotate_traffic_key derives the next read or write traffic secret. It |
| 1513 |
// returns true on success and false on error. |
| 1514 |
bool tls13_rotate_traffic_key(SSL *ssl, enum evp_aead_direction_t direction); |
| 1515 |
|
| 1516 |
// tls13_derive_application_secrets derives the initial application data traffic |
| 1517 |
// and exporter secrets based on the handshake transcripts and |master_secret|. |
| 1518 |
// It returns true on success and false on error. |
| 1519 |
bool tls13_derive_application_secrets(SSL_HANDSHAKE *hs); |
| 1520 |
|
| 1521 |
// tls13_derive_resumption_secret derives the |resumption_secret|. |
| 1522 |
bool tls13_derive_resumption_secret(SSL_HANDSHAKE *hs); |
| 1523 |
|
| 1524 |
// tls13_export_keying_material provides an exporter interface to use the |
| 1525 |
// |exporter_secret|. |
| 1526 |
bool tls13_export_keying_material(SSL *ssl, Span<uint8_t> out, |
| 1527 |
Span<const uint8_t> secret, |
| 1528 |
Span<const char> label, |
| 1529 |
Span<const uint8_t> context); |
| 1530 |
|
| 1531 |
// tls13_finished_mac calculates the MAC of the handshake transcript to verify |
| 1532 |
// the integrity of the Finished message, and stores the result in |out| and |
| 1533 |
// length in |out_len|. |is_server| is true if this is for the Server Finished |
| 1534 |
// and false for the Client Finished. |
| 1535 |
bool tls13_finished_mac(SSL_HANDSHAKE *hs, uint8_t *out, size_t *out_len, |
| 1536 |
bool is_server); |
| 1537 |
|
| 1538 |
// tls13_derive_session_psk calculates the PSK for this session based on the |
| 1539 |
// resumption master secret and |nonce|. It returns true on success, and false |
| 1540 |
// on failure. |
| 1541 |
bool tls13_derive_session_psk(SSL_SESSION *session, Span<const uint8_t> nonce); |
| 1542 |
|
| 1543 |
// tls13_write_psk_binder calculates the PSK binder value over |transcript| and |
| 1544 |
// |msg|, and replaces the last bytes of |msg| with the resulting value. It |
| 1545 |
// returns true on success, and false on failure. If |out_binder_len| is |
| 1546 |
// non-NULL, it sets |*out_binder_len| to the length of the value computed. |
| 1547 |
bool tls13_write_psk_binder(const SSL_HANDSHAKE *hs, |
| 1548 |
const SSLTranscript &transcript, Span<uint8_t> msg, |
| 1549 |
size_t *out_binder_len); |
| 1550 |
|
| 1551 |
// tls13_verify_psk_binder verifies that the handshake transcript, truncated up |
| 1552 |
// to the binders has a valid signature using the value of |session|'s |
| 1553 |
// resumption secret. It returns true on success, and false on failure. |
| 1554 |
bool tls13_verify_psk_binder(const SSL_HANDSHAKE *hs, |
| 1555 |
const SSL_SESSION *session, const SSLMessage &msg, |
| 1556 |
CBS *binders); |
| 1557 |
|
| 1558 |
|
| 1559 |
// Encrypted ClientHello. |
| 1560 |
|
| 1561 |
struct ECHConfig { |
| 1562 |
static constexpr bool kAllowUniquePtr = true; |
| 1563 |
// raw contains the serialized ECHConfig. |
| 1564 |
Array<uint8_t> raw; |
| 1565 |
// The following fields alias into |raw|. |
| 1566 |
Span<const uint8_t> public_key; |
| 1567 |
Span<const uint8_t> public_name; |
| 1568 |
Span<const uint8_t> cipher_suites; |
| 1569 |
uint16_t kem_id = 0; |
| 1570 |
uint8_t maximum_name_length = 0; |
| 1571 |
uint8_t config_id = 0; |
| 1572 |
}; |
| 1573 |
|
| 1574 |
class ECHServerConfig { |
| 1575 |
public: |
| 1576 |
static constexpr bool kAllowUniquePtr = true; |
| 1577 |
ECHServerConfig() = default; |
| 1578 |
ECHServerConfig(const ECHServerConfig &other) = delete; |
| 1579 |
ECHServerConfig &operator=(ECHServerConfig &&) = delete; |
| 1580 |
|
| 1581 |
// Init parses |ech_config| as an ECHConfig and saves a copy of |key|. |
| 1582 |
// It returns true on success and false on error. |
| 1583 |
bool Init(Span<const uint8_t> ech_config, const EVP_HPKE_KEY *key, |
| 1584 |
bool is_retry_config); |
| 1585 |
|
| 1586 |
// SetupContext sets up |ctx| for a new connection, given the specified |
| 1587 |
// HPKE ciphersuite and encapsulated KEM key. It returns true on success and |
| 1588 |
// false on error. This function may only be called on an initialized object. |
| 1589 |
bool SetupContext(EVP_HPKE_CTX *ctx, uint16_t kdf_id, uint16_t aead_id, |
| 1590 |
Span<const uint8_t> enc) const; |
| 1591 |
|
| 1592 |
const ECHConfig &ech_config() const { return ech_config_; } |
| 1593 |
bool is_retry_config() const { return is_retry_config_; } |
| 1594 |
|
| 1595 |
private: |
| 1596 |
ECHConfig ech_config_; |
| 1597 |
ScopedEVP_HPKE_KEY key_; |
| 1598 |
bool is_retry_config_ = false; |
| 1599 |
}; |
| 1600 |
|
| 1601 |
enum ssl_client_hello_type_t { |
| 1602 |
ssl_client_hello_unencrypted, |
| 1603 |
ssl_client_hello_inner, |
| 1604 |
ssl_client_hello_outer, |
| 1605 |
}; |
| 1606 |
|
| 1607 |
// ECH_CLIENT_* are types for the ClientHello encrypted_client_hello extension. |
| 1608 |
#define ECH_CLIENT_OUTER 0 |
| 1609 |
#define ECH_CLIENT_INNER 1 |
| 1610 |
|
| 1611 |
// ssl_decode_client_hello_inner recovers the full ClientHelloInner from the |
| 1612 |
// EncodedClientHelloInner |encoded_client_hello_inner| by replacing its |
| 1613 |
// outer_extensions extension with the referenced extensions from the |
| 1614 |
// ClientHelloOuter |client_hello_outer|. If successful, it writes the recovered |
| 1615 |
// ClientHelloInner to |out_client_hello_inner|. It returns true on success and |
| 1616 |
// false on failure. |
| 1617 |
// |
| 1618 |
// This function is exported for fuzzing. |
| 1619 |
OPENSSL_EXPORT bool ssl_decode_client_hello_inner( |
| 1620 |
SSL *ssl, uint8_t *out_alert, Array<uint8_t> *out_client_hello_inner, |
| 1621 |
Span<const uint8_t> encoded_client_hello_inner, |
| 1622 |
const SSL_CLIENT_HELLO *client_hello_outer); |
| 1623 |
|
| 1624 |
// ssl_client_hello_decrypt attempts to decrypt and decode the |payload|. It |
| 1625 |
// writes the result to |*out|. |payload| must point into |client_hello_outer|. |
| 1626 |
// It returns true on success and false on error. On error, it sets |
| 1627 |
// |*out_is_decrypt_error| to whether the failure was due to a bad ciphertext. |
| 1628 |
bool ssl_client_hello_decrypt(SSL_HANDSHAKE *hs, uint8_t *out_alert, |
| 1629 |
bool *out_is_decrypt_error, Array<uint8_t> *out, |
| 1630 |
const SSL_CLIENT_HELLO *client_hello_outer, |
| 1631 |
Span<const uint8_t> payload); |
| 1632 |
|
| 1633 |
#define ECH_CONFIRMATION_SIGNAL_LEN 8 |
| 1634 |
|
| 1635 |
// ssl_ech_confirmation_signal_hello_offset returns the offset of the ECH |
| 1636 |
// confirmation signal in a ServerHello message, including the handshake header. |
| 1637 |
size_t ssl_ech_confirmation_signal_hello_offset(const SSL *ssl); |
| 1638 |
|
| 1639 |
// ssl_ech_accept_confirmation computes the server's ECH acceptance signal, |
| 1640 |
// writing it to |out|. The transcript portion is the concatenation of |
| 1641 |
// |transcript| with |msg|. The |ECH_CONFIRMATION_SIGNAL_LEN| bytes from |
| 1642 |
// |offset| in |msg| are replaced with zeros before hashing. This function |
| 1643 |
// returns true on success, and false on failure. |
| 1644 |
bool ssl_ech_accept_confirmation(const SSL_HANDSHAKE *hs, Span<uint8_t> out, |
| 1645 |
Span<const uint8_t> client_random, |
| 1646 |
const SSLTranscript &transcript, bool is_hrr, |
| 1647 |
Span<const uint8_t> msg, size_t offset); |
| 1648 |
|
| 1649 |
// ssl_is_valid_ech_public_name returns true if |public_name| is a valid ECH |
| 1650 |
// public name and false otherwise. It is exported for testing. |
| 1651 |
OPENSSL_EXPORT bool ssl_is_valid_ech_public_name( |
| 1652 |
Span<const uint8_t> public_name); |
| 1653 |
|
| 1654 |
// ssl_is_valid_ech_config_list returns true if |ech_config_list| is a valid |
| 1655 |
// ECHConfigList structure and false otherwise. |
| 1656 |
bool ssl_is_valid_ech_config_list(Span<const uint8_t> ech_config_list); |
| 1657 |
|
| 1658 |
// ssl_select_ech_config selects an ECHConfig and associated parameters to offer |
| 1659 |
// on the client and updates |hs|. It returns true on success, whether an |
| 1660 |
// ECHConfig was found or not, and false on internal error. On success, the |
| 1661 |
// encapsulated key is written to |out_enc| and |*out_enc_len| is set to the |
| 1662 |
// number of bytes written. If the function did not select an ECHConfig, the |
| 1663 |
// encapsulated key is the empty string. |
| 1664 |
bool ssl_select_ech_config(SSL_HANDSHAKE *hs, Span<uint8_t> out_enc, |
| 1665 |
size_t *out_enc_len); |
| 1666 |
|
| 1667 |
// ssl_ech_extension_body_length returns the length of the body of a ClientHello |
| 1668 |
// ECH extension that encrypts |in_len| bytes with |aead| and an 'enc' value of |
| 1669 |
// length |enc_len|. The result does not include the four-byte extension header. |
| 1670 |
size_t ssl_ech_extension_body_length(const EVP_HPKE_AEAD *aead, size_t enc_len, |
| 1671 |
size_t in_len); |
| 1672 |
|
| 1673 |
// ssl_encrypt_client_hello constructs a new ClientHelloInner, adds it to the |
| 1674 |
// inner transcript, and encrypts for inclusion in the ClientHelloOuter. |enc| |
| 1675 |
// is the encapsulated key to include in the extension. It returns true on |
| 1676 |
// success and false on error. If not offering ECH, |enc| is ignored and the |
| 1677 |
// function will compute a GREASE ECH extension if necessary, and otherwise |
| 1678 |
// return success while doing nothing. |
| 1679 |
// |
| 1680 |
// Encrypting the ClientHelloInner incorporates all extensions in the |
| 1681 |
// ClientHelloOuter, so all other state necessary for |ssl_add_client_hello| |
| 1682 |
// must already be computed. |
| 1683 |
bool ssl_encrypt_client_hello(SSL_HANDSHAKE *hs, Span<const uint8_t> enc); |
| 1684 |
|
| 1685 |
|
| 1686 |
// Delegated credentials. |
| 1687 |
|
| 1688 |
// This structure stores a delegated credential (DC) as defined by RFC 9345. |
| 1689 |
struct DC { |
| 1690 |
static constexpr bool kAllowUniquePtr = true; |
| 1691 |
~DC(); |
| 1692 |
|
| 1693 |
// Dup returns a copy of this DC and takes references to |raw| and |pkey|. |
| 1694 |
UniquePtr<DC> Dup(); |
| 1695 |
|
| 1696 |
// Parse parses the delegated credential stored in |in|. If successful it |
| 1697 |
// returns the parsed structure, otherwise it returns |nullptr| and sets |
| 1698 |
// |*out_alert|. |
| 1699 |
static UniquePtr<DC> Parse(CRYPTO_BUFFER *in, uint8_t *out_alert); |
| 1700 |
|
| 1701 |
// raw is the delegated credential encoded as specified in RFC 9345. |
| 1702 |
UniquePtr<CRYPTO_BUFFER> raw; |
| 1703 |
|
| 1704 |
// dc_cert_verify_algorithm is the signature scheme of the DC public key. |
| 1705 |
uint16_t dc_cert_verify_algorithm = 0; |
| 1706 |
|
| 1707 |
// pkey is the public key parsed from |public_key|. |
| 1708 |
UniquePtr<EVP_PKEY> pkey; |
| 1709 |
|
| 1710 |
private: |
| 1711 |
friend DC *New<DC>(); |
| 1712 |
DC(); |
| 1713 |
}; |
| 1714 |
|
| 1715 |
// ssl_signing_with_dc returns true if the peer has indicated support for |
| 1716 |
// delegated credentials and this host has sent a delegated credential in |
| 1717 |
// response. If this is true then we've committed to using the DC in the |
| 1718 |
// handshake. |
| 1719 |
bool ssl_signing_with_dc(const SSL_HANDSHAKE *hs); |
| 1720 |
|
| 1721 |
|
| 1722 |
// Handshake functions. |
| 1723 |
|
| 1724 |
enum ssl_hs_wait_t { |
| 1725 |
ssl_hs_error, |
| 1726 |
ssl_hs_ok, |
| 1727 |
ssl_hs_read_server_hello, |
| 1728 |
ssl_hs_read_message, |
| 1729 |
ssl_hs_flush, |
| 1730 |
ssl_hs_certificate_selection_pending, |
| 1731 |
ssl_hs_handoff, |
| 1732 |
ssl_hs_handback, |
| 1733 |
ssl_hs_x509_lookup, |
| 1734 |
ssl_hs_private_key_operation, |
| 1735 |
ssl_hs_pending_session, |
| 1736 |
ssl_hs_pending_ticket, |
| 1737 |
ssl_hs_early_return, |
| 1738 |
ssl_hs_early_data_rejected, |
| 1739 |
ssl_hs_read_end_of_early_data, |
| 1740 |
ssl_hs_read_change_cipher_spec, |
| 1741 |
ssl_hs_certificate_verify, |
| 1742 |
ssl_hs_hints_ready, |
| 1743 |
}; |
| 1744 |
|
| 1745 |
enum ssl_grease_index_t { |
| 1746 |
ssl_grease_cipher = 0, |
| 1747 |
ssl_grease_group, |
| 1748 |
ssl_grease_extension1, |
| 1749 |
ssl_grease_extension2, |
| 1750 |
ssl_grease_version, |
| 1751 |
ssl_grease_ticket_extension, |
| 1752 |
ssl_grease_ech_config_id, |
| 1753 |
ssl_grease_last_index = ssl_grease_ech_config_id, |
| 1754 |
}; |
| 1755 |
|
| 1756 |
enum tls12_server_hs_state_t { |
| 1757 |
state12_start_accept = 0, |
| 1758 |
state12_read_client_hello, |
| 1759 |
state12_read_client_hello_after_ech, |
| 1760 |
state12_select_certificate, |
| 1761 |
state12_tls13, |
| 1762 |
state12_select_parameters, |
| 1763 |
state12_send_server_hello, |
| 1764 |
state12_send_server_certificate, |
| 1765 |
state12_send_server_key_exchange, |
| 1766 |
state12_send_server_hello_done, |
| 1767 |
state12_read_client_certificate, |
| 1768 |
state12_verify_client_certificate, |
| 1769 |
state12_read_client_key_exchange, |
| 1770 |
state12_read_client_certificate_verify, |
| 1771 |
state12_read_change_cipher_spec, |
| 1772 |
state12_process_change_cipher_spec, |
| 1773 |
state12_read_next_proto, |
| 1774 |
state12_read_channel_id, |
| 1775 |
state12_read_client_finished, |
| 1776 |
state12_send_server_finished, |
| 1777 |
state12_finish_server_handshake, |
| 1778 |
state12_done, |
| 1779 |
}; |
| 1780 |
|
| 1781 |
enum tls13_server_hs_state_t { |
| 1782 |
state13_select_parameters = 0, |
| 1783 |
state13_select_session, |
| 1784 |
state13_send_hello_retry_request, |
| 1785 |
state13_read_second_client_hello, |
| 1786 |
state13_send_server_hello, |
| 1787 |
state13_send_server_certificate_verify, |
| 1788 |
state13_send_server_finished, |
| 1789 |
state13_send_half_rtt_ticket, |
| 1790 |
state13_read_second_client_flight, |
| 1791 |
state13_process_end_of_early_data, |
| 1792 |
state13_read_client_encrypted_extensions, |
| 1793 |
state13_read_client_certificate, |
| 1794 |
state13_read_client_certificate_verify, |
| 1795 |
state13_read_channel_id, |
| 1796 |
state13_read_client_finished, |
| 1797 |
state13_send_new_session_ticket, |
| 1798 |
state13_done, |
| 1799 |
}; |
| 1800 |
|
| 1801 |
// handback_t lists the points in the state machine where a handback can occur. |
| 1802 |
// These are the different points at which key material is no longer needed. |
| 1803 |
enum handback_t { |
| 1804 |
handback_after_session_resumption = 0, |
| 1805 |
handback_after_ecdhe = 1, |
| 1806 |
handback_after_handshake = 2, |
| 1807 |
handback_tls13 = 3, |
| 1808 |
handback_max_value = handback_tls13, |
| 1809 |
}; |
| 1810 |
|
| 1811 |
// SSL_HANDSHAKE_HINTS contains handshake hints for a connection. See |
| 1812 |
// |SSL_request_handshake_hints| and related functions. |
| 1813 |
struct SSL_HANDSHAKE_HINTS { |
| 1814 |
static constexpr bool kAllowUniquePtr = true; |
| 1815 |
|
| 1816 |
Array<uint8_t> server_random_tls12; |
| 1817 |
Array<uint8_t> server_random_tls13; |
| 1818 |
|
| 1819 |
uint16_t key_share_group_id = 0; |
| 1820 |
Array<uint8_t> key_share_public_key; |
| 1821 |
Array<uint8_t> key_share_secret; |
| 1822 |
|
| 1823 |
uint16_t signature_algorithm = 0; |
| 1824 |
Array<uint8_t> signature_input; |
| 1825 |
Array<uint8_t> signature_spki; |
| 1826 |
Array<uint8_t> signature; |
| 1827 |
|
| 1828 |
Array<uint8_t> decrypted_psk; |
| 1829 |
bool ignore_psk = false; |
| 1830 |
|
| 1831 |
uint16_t cert_compression_alg_id = 0; |
| 1832 |
Array<uint8_t> cert_compression_input; |
| 1833 |
Array<uint8_t> cert_compression_output; |
| 1834 |
|
| 1835 |
uint16_t ecdhe_group_id = 0; |
| 1836 |
Array<uint8_t> ecdhe_public_key; |
| 1837 |
Array<uint8_t> ecdhe_private_key; |
| 1838 |
|
| 1839 |
Array<uint8_t> decrypted_ticket; |
| 1840 |
bool renew_ticket = false; |
| 1841 |
bool ignore_ticket = false; |
| 1842 |
}; |
| 1843 |
|
| 1844 |
struct SSL_HANDSHAKE { |
| 1845 |
explicit SSL_HANDSHAKE(SSL *ssl); |
| 1846 |
~SSL_HANDSHAKE(); |
| 1847 |
static constexpr bool kAllowUniquePtr = true; |
| 1848 |
|
| 1849 |
// ssl is a non-owning pointer to the parent |SSL| object. |
| 1850 |
SSL *ssl; |
| 1851 |
|
| 1852 |
// config is a non-owning pointer to the handshake configuration. |
| 1853 |
SSL_CONFIG *config; |
| 1854 |
|
| 1855 |
// wait contains the operation the handshake is currently blocking on or |
| 1856 |
// |ssl_hs_ok| if none. |
| 1857 |
enum ssl_hs_wait_t wait = ssl_hs_ok; |
| 1858 |
|
| 1859 |
// state is the internal state for the TLS 1.2 and below handshake. Its |
| 1860 |
// values depend on |do_handshake| but the starting state is always zero. |
| 1861 |
int state = 0; |
| 1862 |
|
| 1863 |
// tls13_state is the internal state for the TLS 1.3 handshake. Its values |
| 1864 |
// depend on |do_handshake| but the starting state is always zero. |
| 1865 |
int tls13_state = 0; |
| 1866 |
|
| 1867 |
// min_version is the minimum accepted protocol version, taking account both |
| 1868 |
// |SSL_OP_NO_*| and |SSL_CTX_set_min_proto_version| APIs. |
| 1869 |
uint16_t min_version = 0; |
| 1870 |
|
| 1871 |
// signature_algorithm is the signature algorithm negotiated for this |
| 1872 |
// handshake. |
| 1873 |
uint16_t signature_algorithm = 0; |
| 1874 |
|
| 1875 |
// max_version is the maximum accepted protocol version, taking account both |
| 1876 |
// |SSL_OP_NO_*| and |SSL_CTX_set_max_proto_version| APIs. |
| 1877 |
uint16_t max_version = 0; |
| 1878 |
|
| 1879 |
private: |
| 1880 |
size_t hash_len_ = 0; |
| 1881 |
uint8_t secret_[SSL_MAX_MD_SIZE] = {0}; |
| 1882 |
uint8_t early_traffic_secret_[SSL_MAX_MD_SIZE] = {0}; |
| 1883 |
uint8_t client_handshake_secret_[SSL_MAX_MD_SIZE] = {0}; |
| 1884 |
uint8_t server_handshake_secret_[SSL_MAX_MD_SIZE] = {0}; |
| 1885 |
uint8_t client_traffic_secret_0_[SSL_MAX_MD_SIZE] = {0}; |
| 1886 |
uint8_t server_traffic_secret_0_[SSL_MAX_MD_SIZE] = {0}; |
| 1887 |
uint8_t expected_client_finished_[SSL_MAX_MD_SIZE] = {0}; |
| 1888 |
|
| 1889 |
public: |
| 1890 |
void ResizeSecrets(size_t hash_len); |
| 1891 |
|
| 1892 |
// GetClientHello, on the server, returns either the normal ClientHello |
| 1893 |
// message or the ClientHelloInner if it has been serialized to |
| 1894 |
// |ech_client_hello_buf|. This function should only be called when the |
| 1895 |
// current message is a ClientHello. It returns true on success and false on |
| 1896 |
// error. |
| 1897 |
// |
| 1898 |
// Note that fields of the returned |out_msg| and |out_client_hello| point |
| 1899 |
// into a handshake-owned buffer, so their lifetimes should not exceed this |
| 1900 |
// SSL_HANDSHAKE. |
| 1901 |
bool GetClientHello(SSLMessage *out_msg, SSL_CLIENT_HELLO *out_client_hello); |
| 1902 |
|
| 1903 |
Span<uint8_t> secret() { return MakeSpan(secret_, hash_len_); } |
| 1904 |
Span<const uint8_t> secret() const { |
| 1905 |
return MakeConstSpan(secret_, hash_len_); |
| 1906 |
} |
| 1907 |
Span<uint8_t> early_traffic_secret() { |
| 1908 |
return MakeSpan(early_traffic_secret_, hash_len_); |
| 1909 |
} |
| 1910 |
Span<uint8_t> client_handshake_secret() { |
| 1911 |
return MakeSpan(client_handshake_secret_, hash_len_); |
| 1912 |
} |
| 1913 |
Span<uint8_t> server_handshake_secret() { |
| 1914 |
return MakeSpan(server_handshake_secret_, hash_len_); |
| 1915 |
} |
| 1916 |
Span<uint8_t> client_traffic_secret_0() { |
| 1917 |
return MakeSpan(client_traffic_secret_0_, hash_len_); |
| 1918 |
} |
| 1919 |
Span<uint8_t> server_traffic_secret_0() { |
| 1920 |
return MakeSpan(server_traffic_secret_0_, hash_len_); |
| 1921 |
} |
| 1922 |
Span<uint8_t> expected_client_finished() { |
| 1923 |
return MakeSpan(expected_client_finished_, hash_len_); |
| 1924 |
} |
| 1925 |
|
| 1926 |
union { |
| 1927 |
// sent is a bitset where the bits correspond to elements of kExtensions |
| 1928 |
// in extensions.cc. Each bit is set if that extension was sent in a |
| 1929 |
// ClientHello. It's not used by servers. |
| 1930 |
uint32_t sent = 0; |
| 1931 |
// received is a bitset, like |sent|, but is used by servers to record |
| 1932 |
// which extensions were received from a client. |
| 1933 |
uint32_t received; |
| 1934 |
} extensions; |
| 1935 |
|
| 1936 |
// inner_extensions_sent, on clients that offer ECH, is |extensions.sent| for |
| 1937 |
// the ClientHelloInner. |
| 1938 |
uint32_t inner_extensions_sent = 0; |
| 1939 |
|
| 1940 |
union { |
| 1941 |
// sent is a bitset where the bits correspond to elements of |
| 1942 |
// |client_custom_extensions| in the |SSL_CTX|. Each bit is set if that |
| 1943 |
// extension was sent in a ClientHello. It's not used by servers. |
| 1944 |
uint16_t sent = 0; |
| 1945 |
// received is a bitset, like |sent|, but is used by servers to record |
| 1946 |
// which custom extensions were received from a client. The bits here |
| 1947 |
// correspond to |server_custom_extensions|. |
| 1948 |
uint16_t received; |
| 1949 |
} custom_extensions; |
| 1950 |
|
| 1951 |
// error, if |wait| is |ssl_hs_error|, is the error the handshake failed on. |
| 1952 |
UniquePtr<ERR_SAVE_STATE> error; |
| 1953 |
|
| 1954 |
// key_shares are the current key exchange instances. The second is only used |
| 1955 |
// as a client if we believe that we should offer two key shares in a |
| 1956 |
// ClientHello. |
| 1957 |
UniquePtr<SSLKeyShare> key_shares[2]; |
| 1958 |
|
| 1959 |
// transcript is the current handshake transcript. |
| 1960 |
SSLTranscript transcript; |
| 1961 |
|
| 1962 |
// inner_transcript, on the client, is the handshake transcript for the |
| 1963 |
// ClientHelloInner handshake. It is moved to |transcript| if the server |
| 1964 |
// accepts ECH. |
| 1965 |
SSLTranscript inner_transcript; |
| 1966 |
|
| 1967 |
// inner_client_random is the ClientHello random value used with |
| 1968 |
// ClientHelloInner. |
| 1969 |
uint8_t inner_client_random[SSL3_RANDOM_SIZE] = {0}; |
| 1970 |
|
| 1971 |
// cookie is the value of the cookie in HelloRetryRequest, or empty if none |
| 1972 |
// was received. |
| 1973 |
Array<uint8_t> cookie; |
| 1974 |
|
| 1975 |
// dtls_cookie is the value of the cookie in DTLS HelloVerifyRequest. If |
| 1976 |
// empty, either none was received or HelloVerifyRequest contained an empty |
| 1977 |
// cookie. |
| 1978 |
Array<uint8_t> dtls_cookie; |
| 1979 |
|
| 1980 |
// ech_client_outer contains the outer ECH extension to send in the |
| 1981 |
// ClientHello, excluding the header and type byte. |
| 1982 |
Array<uint8_t> ech_client_outer; |
| 1983 |
|
| 1984 |
// ech_retry_configs, on the client, contains the retry configs from the |
| 1985 |
// server as a serialized ECHConfigList. |
| 1986 |
Array<uint8_t> ech_retry_configs; |
| 1987 |
|
| 1988 |
// ech_client_hello_buf, on the server, contains the bytes of the |
| 1989 |
// reconstructed ClientHelloInner message. |
| 1990 |
Array<uint8_t> ech_client_hello_buf; |
| 1991 |
|
| 1992 |
// key_share_bytes is the key_share extension that the client should send. |
| 1993 |
Array<uint8_t> key_share_bytes; |
| 1994 |
|
| 1995 |
// ecdh_public_key, for servers, is the key share to be sent to the client in |
| 1996 |
// TLS 1.3. |
| 1997 |
Array<uint8_t> ecdh_public_key; |
| 1998 |
|
| 1999 |
// peer_sigalgs are the signature algorithms that the peer supports. These are |
| 2000 |
// taken from the contents of the signature algorithms extension for a server |
| 2001 |
// or from the CertificateRequest for a client. |
| 2002 |
Array<uint16_t> peer_sigalgs; |
| 2003 |
|
| 2004 |
// peer_supported_group_list contains the supported group IDs advertised by |
| 2005 |
// the peer. This is only set on the server's end. The server does not |
| 2006 |
// advertise this extension to the client. |
| 2007 |
Array<uint16_t> peer_supported_group_list; |
| 2008 |
|
| 2009 |
// peer_delegated_credential_sigalgs are the signature algorithms the peer |
| 2010 |
// supports with delegated credentials, or empty if the peer does not support |
| 2011 |
// delegated credentials. |
| 2012 |
Array<uint16_t> peer_delegated_credential_sigalgs; |
| 2013 |
|
| 2014 |
// extension_permutation is the permutation to apply to ClientHello |
| 2015 |
// extensions. It maps indices into the |kExtensions| table into other |
| 2016 |
// indices. |
| 2017 |
Array<uint8_t> extension_permutation; |
| 2018 |
|
| 2019 |
// cert_compression_alg_id, for a server, contains the negotiated certificate |
| 2020 |
// compression algorithm for this client. It is only valid if |
| 2021 |
// |cert_compression_negotiated| is true. |
| 2022 |
uint16_t cert_compression_alg_id; |
| 2023 |
|
| 2024 |
// ech_hpke_ctx is the HPKE context used in ECH. On the server, it is |
| 2025 |
// initialized if |ech_status| is |ssl_ech_accepted|. On the client, it is |
| 2026 |
// initialized if |selected_ech_config| is not nullptr. |
| 2027 |
ScopedEVP_HPKE_CTX ech_hpke_ctx; |
| 2028 |
|
| 2029 |
// server_params, in a TLS 1.2 server, stores the ServerKeyExchange |
| 2030 |
// parameters. It has client and server randoms prepended for signing |
| 2031 |
// convenience. |
| 2032 |
Array<uint8_t> server_params; |
| 2033 |
|
| 2034 |
// peer_psk_identity_hint, on the client, is the psk_identity_hint sent by the |
| 2035 |
// server when using a TLS 1.2 PSK key exchange. |
| 2036 |
UniquePtr<char> peer_psk_identity_hint; |
| 2037 |
|
| 2038 |
// ca_names, on the client, contains the list of CAs received in a |
| 2039 |
// CertificateRequest message. |
| 2040 |
UniquePtr<STACK_OF(CRYPTO_BUFFER)> ca_names; |
| 2041 |
|
| 2042 |
// certificate_types, on the client, contains the set of certificate types |
| 2043 |
// received in a CertificateRequest message. |
| 2044 |
Array<uint8_t> certificate_types; |
| 2045 |
|
| 2046 |
// local_pubkey is the public key we are authenticating as. |
| 2047 |
UniquePtr<EVP_PKEY> local_pubkey; |
| 2048 |
|
| 2049 |
// peer_pubkey is the public key parsed from the peer's leaf certificate. |
| 2050 |
UniquePtr<EVP_PKEY> peer_pubkey; |
| 2051 |
|
| 2052 |
// new_session is the new mutable session being established by the current |
| 2053 |
// handshake. It should not be cached. |
| 2054 |
UniquePtr<SSL_SESSION> new_session; |
| 2055 |
|
| 2056 |
// early_session is the session corresponding to the current 0-RTT state on |
| 2057 |
// the client if |in_early_data| is true. |
| 2058 |
UniquePtr<SSL_SESSION> early_session; |
| 2059 |
|
| 2060 |
// ssl_ech_keys, for servers, is the set of ECH keys to use with this |
| 2061 |
// handshake. This is copied from |SSL_CTX| to ensure consistent behavior as |
| 2062 |
// |SSL_CTX| rotates keys. |
| 2063 |
UniquePtr<SSL_ECH_KEYS> ech_keys; |
| 2064 |
|
| 2065 |
// selected_ech_config, for clients, is the ECHConfig the client uses to offer |
| 2066 |
// ECH, or nullptr if ECH is not being offered. If non-NULL, |ech_hpke_ctx| |
| 2067 |
// will be initialized. |
| 2068 |
UniquePtr<ECHConfig> selected_ech_config; |
| 2069 |
|
| 2070 |
// new_cipher is the cipher being negotiated in this handshake. |
| 2071 |
const SSL_CIPHER *new_cipher = nullptr; |
| 2072 |
|
| 2073 |
// key_block is the record-layer key block for TLS 1.2 and earlier. |
| 2074 |
Array<uint8_t> key_block; |
| 2075 |
|
| 2076 |
// hints contains the handshake hints for this connection. If |
| 2077 |
// |hints_requested| is true, this field is non-null and contains the pending |
| 2078 |
// hints to filled as the predicted handshake progresses. Otherwise, this |
| 2079 |
// field, if non-null, contains hints configured by the caller and will |
| 2080 |
// influence the handshake on match. |
| 2081 |
UniquePtr<SSL_HANDSHAKE_HINTS> hints; |
| 2082 |
|
| 2083 |
// ech_is_inner, on the server, indicates whether the ClientHello contained an |
| 2084 |
// inner ECH extension. |
| 2085 |
bool ech_is_inner : 1; |
| 2086 |
|
| 2087 |
// ech_authenticated_reject, on the client, indicates whether an ECH rejection |
| 2088 |
// handshake has been authenticated. |
| 2089 |
bool ech_authenticated_reject : 1; |
| 2090 |
|
| 2091 |
// scts_requested is true if the SCT extension is in the ClientHello. |
| 2092 |
bool scts_requested : 1; |
| 2093 |
|
| 2094 |
bool received_custom_extension : 1; |
| 2095 |
|
| 2096 |
// handshake_finalized is true once the handshake has completed, at which |
| 2097 |
// point accessors should use the established state. |
| 2098 |
bool handshake_finalized : 1; |
| 2099 |
|
| 2100 |
// accept_psk_mode stores whether the client's PSK mode is compatible with our |
| 2101 |
// preferences. |
| 2102 |
bool accept_psk_mode : 1; |
| 2103 |
|
| 2104 |
// cert_request is true if a client certificate was requested. |
| 2105 |
bool cert_request : 1; |
| 2106 |
|
| 2107 |
// certificate_status_expected is true if OCSP stapling was negotiated and the |
| 2108 |
// server is expected to send a CertificateStatus message. (This is used on |
| 2109 |
// both the client and server sides.) |
| 2110 |
bool certificate_status_expected : 1; |
| 2111 |
|
| 2112 |
// ocsp_stapling_requested is true if a client requested OCSP stapling. |
| 2113 |
bool ocsp_stapling_requested : 1; |
| 2114 |
|
| 2115 |
// should_ack_sni is used by a server and indicates that the SNI extension |
| 2116 |
// should be echoed in the ServerHello. |
| 2117 |
bool should_ack_sni : 1; |
| 2118 |
|
| 2119 |
// in_false_start is true if there is a pending client handshake in False |
| 2120 |
// Start. The client may write data at this point. |
| 2121 |
bool in_false_start : 1; |
| 2122 |
|
| 2123 |
// in_early_data is true if there is a pending handshake that has progressed |
| 2124 |
// enough to send and receive early data. |
| 2125 |
bool in_early_data : 1; |
| 2126 |
|
| 2127 |
// early_data_offered is true if the client sent the early_data extension. |
| 2128 |
bool early_data_offered : 1; |
| 2129 |
|
| 2130 |
// can_early_read is true if application data may be read at this point in the |
| 2131 |
// handshake. |
| 2132 |
bool can_early_read : 1; |
| 2133 |
|
| 2134 |
// can_early_write is true if application data may be written at this point in |
| 2135 |
// the handshake. |
| 2136 |
bool can_early_write : 1; |
| 2137 |
|
| 2138 |
// next_proto_neg_seen is one of NPN was negotiated. |
| 2139 |
bool next_proto_neg_seen : 1; |
| 2140 |
|
| 2141 |
// ticket_expected is true if a TLS 1.2 NewSessionTicket message is to be sent |
| 2142 |
// or received. |
| 2143 |
bool ticket_expected : 1; |
| 2144 |
|
| 2145 |
// extended_master_secret is true if the extended master secret extension is |
| 2146 |
// negotiated in this handshake. |
| 2147 |
bool extended_master_secret : 1; |
| 2148 |
|
| 2149 |
// pending_private_key_op is true if there is a pending private key operation |
| 2150 |
// in progress. |
| 2151 |
bool pending_private_key_op : 1; |
| 2152 |
|
| 2153 |
// handback indicates that a server should pause the handshake after |
| 2154 |
// finishing operations that require private key material, in such a way that |
| 2155 |
// |SSL_get_error| returns |SSL_ERROR_HANDBACK|. It is set by |
| 2156 |
// |SSL_apply_handoff|. |
| 2157 |
bool handback : 1; |
| 2158 |
|
| 2159 |
// hints_requested indicates the caller has requested handshake hints. Only |
| 2160 |
// the first round-trip of the handshake will complete, after which the |
| 2161 |
// |hints| structure can be serialized. |
| 2162 |
bool hints_requested : 1; |
| 2163 |
|
| 2164 |
// cert_compression_negotiated is true iff |cert_compression_alg_id| is valid. |
| 2165 |
bool cert_compression_negotiated : 1; |
| 2166 |
|
| 2167 |
// apply_jdk11_workaround is true if the peer is probably a JDK 11 client |
| 2168 |
// which implemented TLS 1.3 incorrectly. |
| 2169 |
bool apply_jdk11_workaround : 1; |
| 2170 |
|
| 2171 |
// can_release_private_key is true if the private key will no longer be used |
| 2172 |
// in this handshake. |
| 2173 |
bool can_release_private_key : 1; |
| 2174 |
|
| 2175 |
// channel_id_negotiated is true if Channel ID should be used in this |
| 2176 |
// handshake. |
| 2177 |
bool channel_id_negotiated : 1; |
| 2178 |
|
| 2179 |
// client_version is the value sent or received in the ClientHello version. |
| 2180 |
uint16_t client_version = 0; |
| 2181 |
|
| 2182 |
// early_data_read is the amount of early data that has been read by the |
| 2183 |
// record layer. |
| 2184 |
uint16_t early_data_read = 0; |
| 2185 |
|
| 2186 |
// early_data_written is the amount of early data that has been written by the |
| 2187 |
// record layer. |
| 2188 |
uint16_t early_data_written = 0; |
| 2189 |
|
| 2190 |
// ech_config_id is the ECH config sent by the client. |
| 2191 |
uint8_t ech_config_id = 0; |
| 2192 |
|
| 2193 |
// session_id is the session ID in the ClientHello. |
| 2194 |
uint8_t session_id[SSL_MAX_SSL_SESSION_ID_LENGTH] = {0}; |
| 2195 |
uint8_t session_id_len = 0; |
| 2196 |
|
| 2197 |
// grease_seed is the entropy for GREASE values. |
| 2198 |
uint8_t grease_seed[ssl_grease_last_index + 1] = {0}; |
| 2199 |
}; |
| 2200 |
|
| 2201 |
// kMaxTickets is the maximum number of tickets to send immediately after the |
| 2202 |
// handshake. We use a one-byte ticket nonce, and there is no point in sending |
| 2203 |
// so many tickets. |
| 2204 |
constexpr size_t kMaxTickets = 16; |
| 2205 |
|
| 2206 |
UniquePtr<SSL_HANDSHAKE> ssl_handshake_new(SSL *ssl); |
| 2207 |
|
| 2208 |
// ssl_check_message_type checks if |msg| has type |type|. If so it returns |
| 2209 |
// one. Otherwise, it sends an alert and returns zero. |
| 2210 |
bool ssl_check_message_type(SSL *ssl, const SSLMessage &msg, int type); |
| 2211 |
|
| 2212 |
// ssl_run_handshake runs the TLS handshake. It returns one on success and <= 0 |
| 2213 |
// on error. It sets |out_early_return| to one if we've completed the handshake |
| 2214 |
// early. |
| 2215 |
int ssl_run_handshake(SSL_HANDSHAKE *hs, bool *out_early_return); |
| 2216 |
|
| 2217 |
// The following are implementations of |do_handshake| for the client and |
| 2218 |
// server. |
| 2219 |
enum ssl_hs_wait_t ssl_client_handshake(SSL_HANDSHAKE *hs); |
| 2220 |
enum ssl_hs_wait_t ssl_server_handshake(SSL_HANDSHAKE *hs); |
| 2221 |
enum ssl_hs_wait_t tls13_client_handshake(SSL_HANDSHAKE *hs); |
| 2222 |
enum ssl_hs_wait_t tls13_server_handshake(SSL_HANDSHAKE *hs); |
| 2223 |
|
| 2224 |
// The following functions return human-readable representations of the TLS |
| 2225 |
// handshake states for debugging. |
| 2226 |
const char *ssl_client_handshake_state(SSL_HANDSHAKE *hs); |
| 2227 |
const char *ssl_server_handshake_state(SSL_HANDSHAKE *hs); |
| 2228 |
const char *tls13_client_handshake_state(SSL_HANDSHAKE *hs); |
| 2229 |
const char *tls13_server_handshake_state(SSL_HANDSHAKE *hs); |
| 2230 |
|
| 2231 |
// tls13_add_key_update queues a KeyUpdate message on |ssl|. The |
| 2232 |
// |update_requested| argument must be one of |SSL_KEY_UPDATE_REQUESTED| or |
| 2233 |
// |SSL_KEY_UPDATE_NOT_REQUESTED|. |
| 2234 |
bool tls13_add_key_update(SSL *ssl, int update_requested); |
| 2235 |
|
| 2236 |
// tls13_post_handshake processes a post-handshake message. It returns true on |
| 2237 |
// success and false on failure. |
| 2238 |
bool tls13_post_handshake(SSL *ssl, const SSLMessage &msg); |
| 2239 |
|
| 2240 |
bool tls13_process_certificate(SSL_HANDSHAKE *hs, const SSLMessage &msg, |
| 2241 |
bool allow_anonymous); |
| 2242 |
bool tls13_process_certificate_verify(SSL_HANDSHAKE *hs, const SSLMessage &msg); |
| 2243 |
|
| 2244 |
// tls13_process_finished processes |msg| as a Finished message from the |
| 2245 |
// peer. If |use_saved_value| is true, the verify_data is compared against |
| 2246 |
// |hs->expected_client_finished| rather than computed fresh. |
| 2247 |
bool tls13_process_finished(SSL_HANDSHAKE *hs, const SSLMessage &msg, |
| 2248 |
bool use_saved_value); |
| 2249 |
|
| 2250 |
bool tls13_add_certificate(SSL_HANDSHAKE *hs); |
| 2251 |
|
| 2252 |
// tls13_add_certificate_verify adds a TLS 1.3 CertificateVerify message to the |
| 2253 |
// handshake. If it returns |ssl_private_key_retry|, it should be called again |
| 2254 |
// to retry when the signing operation is completed. |
| 2255 |
// |
| 2256 |
// NOTE: |signature_algorithm| in |hs| should be initialized already before |
| 2257 |
// this is called. |
| 2258 |
enum ssl_private_key_result_t tls13_add_certificate_verify(SSL_HANDSHAKE *hs); |
| 2259 |
|
| 2260 |
bool tls13_add_finished(SSL_HANDSHAKE *hs); |
| 2261 |
bool tls13_process_new_session_ticket(SSL *ssl, const SSLMessage &msg); |
| 2262 |
bssl::UniquePtr<SSL_SESSION> tls13_create_session_with_ticket(SSL *ssl, |
| 2263 |
CBS *body); |
| 2264 |
|
| 2265 |
// ssl_setup_extension_permutation computes a ClientHello extension permutation |
| 2266 |
// for |hs|, if applicable. It returns true on success and false on error. |
| 2267 |
bool ssl_setup_extension_permutation(SSL_HANDSHAKE *hs); |
| 2268 |
|
| 2269 |
// ssl_setup_key_shares computes client key shares and saves them in |hs|. It |
| 2270 |
// returns true on success and false on failure. If |override_group_id| is zero, |
| 2271 |
// it offers the default groups, including GREASE. If it is non-zero, it offers |
| 2272 |
// a single key share of the specified group. |
| 2273 |
bool ssl_setup_key_shares(SSL_HANDSHAKE *hs, uint16_t override_group_id); |
| 2274 |
|
| 2275 |
bool ssl_ext_key_share_parse_serverhello(SSL_HANDSHAKE *hs, |
| 2276 |
Array<uint8_t> *out_secret, |
| 2277 |
Array<uint8_t> *out_peer_key, |
| 2278 |
uint8_t *out_alert, CBS *contents); |
| 2279 |
bool ssl_ext_key_share_parse_clienthello(SSL_HANDSHAKE *hs, bool *out_found, |
| 2280 |
Span<const uint8_t> *out_peer_key, |
| 2281 |
uint8_t *out_alert, |
| 2282 |
const SSL_CLIENT_HELLO *client_hello); |
| 2283 |
bool ssl_ext_key_share_add_serverhello(SSL_HANDSHAKE *hs, CBB *out); |
| 2284 |
|
| 2285 |
bool ssl_ext_pre_shared_key_parse_serverhello(SSL_HANDSHAKE *hs, |
| 2286 |
uint8_t *out_alert, |
| 2287 |
CBS *contents); |
| 2288 |
bool ssl_ext_pre_shared_key_parse_clienthello( |
| 2289 |
SSL_HANDSHAKE *hs, CBS *out_ticket, CBS *out_binders, |
| 2290 |
uint32_t *out_obfuscated_ticket_age, uint8_t *out_alert, |
| 2291 |
const SSL_CLIENT_HELLO *client_hello, CBS *contents); |
| 2292 |
bool ssl_ext_pre_shared_key_add_serverhello(SSL_HANDSHAKE *hs, CBB *out); |
| 2293 |
|
| 2294 |
// ssl_is_sct_list_valid does a shallow parse of the SCT list in |contents| and |
| 2295 |
// returns whether it's valid. |
| 2296 |
bool ssl_is_sct_list_valid(const CBS *contents); |
| 2297 |
|
| 2298 |
// ssl_write_client_hello_without_extensions writes a ClientHello to |out|, |
| 2299 |
// up to the extensions field. |type| determines the type of ClientHello to |
| 2300 |
// write. If |omit_session_id| is true, the session ID is empty. |
| 2301 |
bool ssl_write_client_hello_without_extensions(const SSL_HANDSHAKE *hs, |
| 2302 |
CBB *cbb, |
| 2303 |
ssl_client_hello_type_t type, |
| 2304 |
bool empty_session_id); |
| 2305 |
|
| 2306 |
// ssl_add_client_hello constructs a ClientHello and adds it to the outgoing |
| 2307 |
// flight. It returns true on success and false on error. |
| 2308 |
bool ssl_add_client_hello(SSL_HANDSHAKE *hs); |
| 2309 |
|
| 2310 |
struct ParsedServerHello { |
| 2311 |
CBS raw; |
| 2312 |
uint16_t legacy_version = 0; |
| 2313 |
CBS random; |
| 2314 |
CBS session_id; |
| 2315 |
uint16_t cipher_suite = 0; |
| 2316 |
uint8_t compression_method = 0; |
| 2317 |
CBS extensions; |
| 2318 |
}; |
| 2319 |
|
| 2320 |
// ssl_parse_server_hello parses |msg| as a ServerHello. On success, it writes |
| 2321 |
// the result to |*out| and returns true. Otherwise, it returns false and sets |
| 2322 |
// |*out_alert| to an alert to send to the peer. |
| 2323 |
bool ssl_parse_server_hello(ParsedServerHello *out, uint8_t *out_alert, |
| 2324 |
const SSLMessage &msg); |
| 2325 |
|
| 2326 |
enum ssl_cert_verify_context_t { |
| 2327 |
ssl_cert_verify_server, |
| 2328 |
ssl_cert_verify_client, |
| 2329 |
ssl_cert_verify_channel_id, |
| 2330 |
}; |
| 2331 |
|
| 2332 |
// tls13_get_cert_verify_signature_input generates the message to be signed for |
| 2333 |
// TLS 1.3's CertificateVerify message. |cert_verify_context| determines the |
| 2334 |
// type of signature. It sets |*out| to a newly allocated buffer containing the |
| 2335 |
// result. This function returns true on success and false on failure. |
| 2336 |
bool tls13_get_cert_verify_signature_input( |
| 2337 |
SSL_HANDSHAKE *hs, Array<uint8_t> *out, |
| 2338 |
enum ssl_cert_verify_context_t cert_verify_context); |
| 2339 |
|
| 2340 |
// ssl_is_valid_alpn_list returns whether |in| is a valid ALPN protocol list. |
| 2341 |
bool ssl_is_valid_alpn_list(Span<const uint8_t> in); |
| 2342 |
|
| 2343 |
// ssl_is_alpn_protocol_allowed returns whether |protocol| is a valid server |
| 2344 |
// selection for |hs->ssl|'s client preferences. |
| 2345 |
bool ssl_is_alpn_protocol_allowed(const SSL_HANDSHAKE *hs, |
| 2346 |
Span<const uint8_t> protocol); |
| 2347 |
|
| 2348 |
// ssl_alpn_list_contains_protocol returns whether |list|, a serialized ALPN |
| 2349 |
// protocol list, contains |protocol|. |
| 2350 |
bool ssl_alpn_list_contains_protocol(Span<const uint8_t> list, |
| 2351 |
Span<const uint8_t> protocol); |
| 2352 |
|
| 2353 |
// ssl_negotiate_alpn negotiates the ALPN extension, if applicable. It returns |
| 2354 |
// true on successful negotiation or if nothing was negotiated. It returns false |
| 2355 |
// and sets |*out_alert| to an alert on error. |
| 2356 |
bool ssl_negotiate_alpn(SSL_HANDSHAKE *hs, uint8_t *out_alert, |
| 2357 |
const SSL_CLIENT_HELLO *client_hello); |
| 2358 |
|
| 2359 |
// ssl_get_local_application_settings looks up the configured ALPS value for |
| 2360 |
// |protocol|. If found, it sets |*out_settings| to the value and returns true. |
| 2361 |
// Otherwise, it returns false. |
| 2362 |
bool ssl_get_local_application_settings(const SSL_HANDSHAKE *hs, |
| 2363 |
Span<const uint8_t> *out_settings, |
| 2364 |
Span<const uint8_t> protocol); |
| 2365 |
|
| 2366 |
// ssl_negotiate_alps negotiates the ALPS extension, if applicable. It returns |
| 2367 |
// true on successful negotiation or if nothing was negotiated. It returns false |
| 2368 |
// and sets |*out_alert| to an alert on error. |
| 2369 |
bool ssl_negotiate_alps(SSL_HANDSHAKE *hs, uint8_t *out_alert, |
| 2370 |
const SSL_CLIENT_HELLO *client_hello); |
| 2371 |
|
| 2372 |
struct SSLExtension { |
| 2373 |
SSLExtension(uint16_t type_arg, bool allowed_arg = true) |
| 2374 |
: type(type_arg), allowed(allowed_arg), present(false) { |
| 2375 |
CBS_init(&data, nullptr, 0); |
| 2376 |
} |
| 2377 |
|
| 2378 |
uint16_t type; |
| 2379 |
bool allowed; |
| 2380 |
bool present; |
| 2381 |
CBS data; |
| 2382 |
}; |
| 2383 |
|
| 2384 |
// ssl_parse_extensions parses a TLS extensions block out of |cbs| and advances |
| 2385 |
// it. It writes the parsed extensions to pointers in |extensions|. On success, |
| 2386 |
// it fills in the |present| and |data| fields and returns true. Otherwise, it |
| 2387 |
// sets |*out_alert| to an alert to send and returns false. Unknown extensions |
| 2388 |
// are rejected unless |ignore_unknown| is true. |
| 2389 |
bool ssl_parse_extensions(const CBS *cbs, uint8_t *out_alert, |
| 2390 |
std::initializer_list<SSLExtension *> extensions, |
| 2391 |
bool ignore_unknown); |
| 2392 |
|
| 2393 |
// ssl_verify_peer_cert verifies the peer certificate for |hs|. |
| 2394 |
enum ssl_verify_result_t ssl_verify_peer_cert(SSL_HANDSHAKE *hs); |
| 2395 |
// ssl_reverify_peer_cert verifies the peer certificate for |hs| when resuming a |
| 2396 |
// session. |
| 2397 |
enum ssl_verify_result_t ssl_reverify_peer_cert(SSL_HANDSHAKE *hs, |
| 2398 |
bool send_alert); |
| 2399 |
|
| 2400 |
enum ssl_hs_wait_t ssl_get_finished(SSL_HANDSHAKE *hs); |
| 2401 |
bool ssl_send_finished(SSL_HANDSHAKE *hs); |
| 2402 |
bool ssl_output_cert_chain(SSL_HANDSHAKE *hs); |
| 2403 |
|
| 2404 |
// ssl_handshake_session returns the |SSL_SESSION| corresponding to the current |
| 2405 |
// handshake. Note, in TLS 1.2 resumptions, this session is immutable. |
| 2406 |
const SSL_SESSION *ssl_handshake_session(const SSL_HANDSHAKE *hs); |
| 2407 |
|
| 2408 |
// ssl_done_writing_client_hello is called after the last ClientHello is written |
| 2409 |
// by |hs|. It releases some memory that is no longer needed. |
| 2410 |
void ssl_done_writing_client_hello(SSL_HANDSHAKE *hs); |
| 2411 |
|
| 2412 |
|
| 2413 |
// SSLKEYLOGFILE functions. |
| 2414 |
|
| 2415 |
// ssl_log_secret logs |secret| with label |label|, if logging is enabled for |
| 2416 |
// |ssl|. It returns true on success and false on failure. |
| 2417 |
bool ssl_log_secret(const SSL *ssl, const char *label, |
| 2418 |
Span<const uint8_t> secret); |
| 2419 |
|
| 2420 |
|
| 2421 |
// ClientHello functions. |
| 2422 |
|
| 2423 |
// ssl_client_hello_init parses |body| as a ClientHello message, excluding the |
| 2424 |
// message header, and writes the result to |*out|. It returns true on success |
| 2425 |
// and false on error. This function is exported for testing. |
| 2426 |
OPENSSL_EXPORT bool ssl_client_hello_init(const SSL *ssl, SSL_CLIENT_HELLO *out, |
| 2427 |
Span<const uint8_t> body); |
| 2428 |
|
| 2429 |
bool ssl_parse_client_hello_with_trailing_data(const SSL *ssl, CBS *cbs, |
| 2430 |
SSL_CLIENT_HELLO *out); |
| 2431 |
|
| 2432 |
bool ssl_client_hello_get_extension(const SSL_CLIENT_HELLO *client_hello, |
| 2433 |
CBS *out, uint16_t extension_type); |
| 2434 |
|
| 2435 |
bool ssl_client_cipher_list_contains_cipher( |
| 2436 |
const SSL_CLIENT_HELLO *client_hello, uint16_t id); |
| 2437 |
|
| 2438 |
// ssl_parse_client_cipher_list returns the ciphers offered by the client |
| 2439 |
// during handshake that are supported by this library, or null if the handshake hasn't |
| 2440 |
// occurred or there was an error. It also stores the unparsed raw bytes of cipher suites offered in |
| 2441 |
// the client hello into |ssl->all_client_cipher_suites|. |
| 2442 |
bool ssl_parse_client_cipher_list(SSL *ssl, const SSL_CLIENT_HELLO *client_hello, |
| 2443 |
UniquePtr<STACK_OF(SSL_CIPHER)> *ciphers_out); |
| 2444 |
|
| 2445 |
|
| 2446 |
// GREASE. |
| 2447 |
|
| 2448 |
// ssl_get_grease_value returns a GREASE value for |hs|. For a given |
| 2449 |
// connection, the values for each index will be deterministic. This allows the |
| 2450 |
// same ClientHello be sent twice for a HelloRetryRequest or the same group be |
| 2451 |
// advertised in both supported_groups and key_shares. |
| 2452 |
uint16_t ssl_get_grease_value(const SSL_HANDSHAKE *hs, |
| 2453 |
enum ssl_grease_index_t index); |
| 2454 |
|
| 2455 |
|
| 2456 |
// Signature algorithms. |
| 2457 |
|
| 2458 |
// tls1_parse_peer_sigalgs parses |sigalgs| as the list of peer signature |
| 2459 |
// algorithms and saves them on |hs|. It returns true on success and false on |
| 2460 |
// error. |
| 2461 |
bool tls1_parse_peer_sigalgs(SSL_HANDSHAKE *hs, const CBS *sigalgs); |
| 2462 |
|
| 2463 |
// tls1_get_legacy_signature_algorithm sets |*out| to the signature algorithm |
| 2464 |
// that should be used with |pkey| in TLS 1.1 and earlier. It returns true on |
| 2465 |
// success and false if |pkey| may not be used at those versions. |
| 2466 |
bool tls1_get_legacy_signature_algorithm(uint16_t *out, const EVP_PKEY *pkey); |
| 2467 |
|
| 2468 |
// tls1_choose_signature_algorithm sets |*out| to a signature algorithm for use |
| 2469 |
// with |hs|'s private key based on the peer's preferences and the algorithms |
| 2470 |
// supported. It returns true on success and false on error. |
| 2471 |
bool tls1_choose_signature_algorithm(SSL_HANDSHAKE *hs, uint16_t *out); |
| 2472 |
|
| 2473 |
// tls1_get_peer_verify_algorithms returns the signature schemes for which the |
| 2474 |
// peer indicated support. |
| 2475 |
// |
| 2476 |
// NOTE: The related function |SSL_get0_peer_verify_algorithms| only has |
| 2477 |
// well-defined behavior during the callbacks set by |SSL_CTX_set_cert_cb| and |
| 2478 |
// |SSL_CTX_set_client_cert_cb|, or when the handshake is paused because of |
| 2479 |
// them. |
| 2480 |
Span<const uint16_t> tls1_get_peer_verify_algorithms(const SSL_HANDSHAKE *hs); |
| 2481 |
|
| 2482 |
// tls12_add_verify_sigalgs adds the signature algorithms acceptable for the |
| 2483 |
// peer signature to |out|. It returns true on success and false on error. |
| 2484 |
bool tls12_add_verify_sigalgs(const SSL_HANDSHAKE *hs, CBB *out); |
| 2485 |
|
| 2486 |
// tls12_check_peer_sigalg checks if |sigalg| is acceptable for the peer |
| 2487 |
// signature. It returns true on success and false on error, setting |
| 2488 |
// |*out_alert| to an alert to send. |
| 2489 |
bool tls12_check_peer_sigalg(const SSL_HANDSHAKE *hs, uint8_t *out_alert, |
| 2490 |
uint16_t sigalg); |
| 2491 |
|
| 2492 |
|
| 2493 |
// Underdocumented functions. |
| 2494 |
// |
| 2495 |
// Functions below here haven't been touched up and may be underdocumented. |
| 2496 |
|
| 2497 |
#define TLSEXT_CHANNEL_ID_SIZE 128 |
| 2498 |
|
| 2499 |
// From RFC 4492, used in encoding the curve type in ECParameters |
| 2500 |
#define NAMED_CURVE_TYPE 3 |
| 2501 |
|
| 2502 |
// SSL_PKEY_* denote certificate types. These represent an index within |
| 2503 |
// |CERT->cert_privatekeys|. |
| 2504 |
#define SSL_PKEY_RSA 0 |
| 2505 |
#define SSL_PKEY_ECC 1 |
| 2506 |
#define SSL_PKEY_ED25519 2 |
| 2507 |
#define SSL_PKEY_SIZE 3 |
| 2508 |
|
| 2509 |
struct CERT_PKEY { |
| 2510 |
UniquePtr<EVP_PKEY> privatekey; |
| 2511 |
|
| 2512 |
// chain contains the certificate chain, with the leaf at the beginning. The |
| 2513 |
// first element of |chain| may be NULL to indicate that the leaf certificate |
| 2514 |
// has not yet been set. |
| 2515 |
// If |chain| != NULL -> len(chain) >= 1 |
| 2516 |
// If |chain[0]| == NULL -> len(chain) >= 2. |
| 2517 |
// |chain[1..]| != NULL |
| 2518 |
UniquePtr<STACK_OF(CRYPTO_BUFFER)> chain; |
| 2519 |
|
| 2520 |
// x509_chain may contain a parsed copy of |chain[1..]|. This is only used as |
| 2521 |
// a cache in order to implement “get0” functions that return a non-owning |
| 2522 |
// pointer to the certificate chain. |
| 2523 |
STACK_OF(X509) *x509_chain = nullptr; |
| 2524 |
|
| 2525 |
// x509_leaf retains the |X509| structure of the first element of |chain|. |
| 2526 |
// However, if certs are set with |SSL_CTX_use_certificate_ASN1| or |
| 2527 |
// |SSL_use_certificate_ASN1|, this is only used as a cache in order to |
| 2528 |
// implement “get0” functions that return a non-owning pointer to the |
| 2529 |
// certificate chain. |
| 2530 |
X509 *x509_leaf = nullptr; |
| 2531 |
}; |
| 2532 |
|
| 2533 |
struct CERT { |
| 2534 |
static constexpr bool kAllowUniquePtr = true; |
| 2535 |
|
| 2536 |
explicit CERT(const SSL_X509_METHOD *x509_method); |
| 2537 |
~CERT(); |
| 2538 |
|
| 2539 |
// SetKeyMethod sets |key_method| for private key callback operations and |
| 2540 |
// clears any software private key in slot |slot_idx| to maintain mutual |
| 2541 |
// exclusivity between |key_method| and per-slot |privatekey|. It returns |
| 2542 |
// true on success. If |slot_idx| is out of range and |method| is non-NULL, |
| 2543 |
// it returns false because the mutual exclusivity invariant cannot be |
| 2544 |
// enforced. |
| 2545 |
bool SetKeyMethod(const SSL_PRIVATE_KEY_METHOD *method, int slot_idx); |
| 2546 |
|
| 2547 |
// SetSlotPrivateKey sets the software private key for |slot_idx| and clears |
| 2548 |
// |key_method| to maintain mutual exclusivity between per-slot |privatekey| |
| 2549 |
// and |key_method|. It returns false if |slot_idx| is out of range. |
| 2550 |
bool SetSlotPrivateKey(int slot_idx, EVP_PKEY *pkey); |
| 2551 |
|
| 2552 |
// cert_privatekey_idx ALWAYS points to an element of the |cert_pkeys| |
| 2553 |
// array. OpenSSL implements this as a pointer, but an index is more |
| 2554 |
// efficient. |
| 2555 |
int cert_private_key_idx = -1; |
| 2556 |
|
| 2557 |
Array<CERT_PKEY> cert_private_keys; |
| 2558 |
|
| 2559 |
/// We'lll see what we want to do about the |x509_stash| below later. |
| 2560 |
|
| 2561 |
// x509_stash contains the last |X509| object append to the chain. This is a |
| 2562 |
// workaround for some third-party code that continue to use an |X509| object |
| 2563 |
// even after passing ownership with an “add0” function. |
| 2564 |
X509 *x509_stash = nullptr; |
| 2565 |
|
| 2566 |
// key_method, if non-NULL, is a set of callbacks to call for private key |
| 2567 |
// operations. |
| 2568 |
const SSL_PRIVATE_KEY_METHOD *key_method = nullptr; |
| 2569 |
|
| 2570 |
// x509_method contains pointers to functions that might deal with |X509| |
| 2571 |
// compatibility, or might be a no-op, depending on the application. |
| 2572 |
const SSL_X509_METHOD *x509_method = nullptr; |
| 2573 |
|
| 2574 |
// sigalgs, if non-empty, is the set of signature algorithms supported by |
| 2575 |
// |privatekey| in decreasing order of preference. |
| 2576 |
Array<uint16_t> sigalgs; |
| 2577 |
|
| 2578 |
// Certificate setup callback: if set is called whenever a |
| 2579 |
// certificate may be required (client or server). the callback |
| 2580 |
// can then examine any appropriate parameters and setup any |
| 2581 |
// certificates required. This allows advanced applications |
| 2582 |
// to select certificates on the fly: for example based on |
| 2583 |
// supported signature algorithms or curves. |
| 2584 |
int (*cert_cb)(SSL *ssl, void *arg) = nullptr; |
| 2585 |
void *cert_cb_arg = nullptr; |
| 2586 |
|
| 2587 |
// Optional X509_STORE for certificate validation. If NULL the parent SSL_CTX |
| 2588 |
// store is used instead. |
| 2589 |
X509_STORE *verify_store = nullptr; |
| 2590 |
|
| 2591 |
// Signed certificate timestamp list to be sent to the client, if requested |
| 2592 |
UniquePtr<CRYPTO_BUFFER> signed_cert_timestamp_list; |
| 2593 |
|
| 2594 |
// OCSP response to be sent to the client, if requested. |
| 2595 |
UniquePtr<CRYPTO_BUFFER> ocsp_response; |
| 2596 |
|
| 2597 |
// sid_ctx partitions the session space within a shared session cache or |
| 2598 |
// ticket key. Only sessions with a matching value will be accepted. |
| 2599 |
uint8_t sid_ctx_length = 0; |
| 2600 |
uint8_t sid_ctx[SSL_MAX_SID_CTX_LENGTH] = {0}; |
| 2601 |
|
| 2602 |
// Delegated credentials. |
| 2603 |
|
| 2604 |
// dc is the delegated credential to send to the peer (if requested). |
| 2605 |
UniquePtr<DC> dc = nullptr; |
| 2606 |
|
| 2607 |
// dc_privatekey is used instead of |privatekey| or |key_method| to |
| 2608 |
// authenticate the host if a delegated credential is used in the handshake. |
| 2609 |
UniquePtr<EVP_PKEY> dc_privatekey = nullptr; |
| 2610 |
|
| 2611 |
// dc_key_method, if not NULL, is used instead of |dc_privatekey| to |
| 2612 |
// authenticate the host. |
| 2613 |
const SSL_PRIVATE_KEY_METHOD *dc_key_method = nullptr; |
| 2614 |
}; |
| 2615 |
|
| 2616 |
// |SSL_PROTOCOL_METHOD| abstracts between TLS and DTLS. |
| 2617 |
struct SSL_PROTOCOL_METHOD { |
| 2618 |
bool is_dtls; |
| 2619 |
bool (*ssl_new)(SSL *ssl); |
| 2620 |
void (*ssl_free)(SSL *ssl); |
| 2621 |
// get_message sets |*out| to the current handshake message and returns true |
| 2622 |
// if one has been received. It returns false if more input is needed. |
| 2623 |
bool (*get_message)(const SSL *ssl, SSLMessage *out); |
| 2624 |
// next_message is called to release the current handshake message. |
| 2625 |
void (*next_message)(SSL *ssl); |
| 2626 |
// has_unprocessed_handshake_data returns whether there is buffered |
| 2627 |
// handshake data that has not been consumed by |get_message|. |
| 2628 |
bool (*has_unprocessed_handshake_data)(const SSL *ssl); |
| 2629 |
// Use the |ssl_open_handshake| wrapper. |
| 2630 |
ssl_open_record_t (*open_handshake)(SSL *ssl, size_t *out_consumed, |
| 2631 |
uint8_t *out_alert, Span<uint8_t> in); |
| 2632 |
// Use the |ssl_open_change_cipher_spec| wrapper. |
| 2633 |
ssl_open_record_t (*open_change_cipher_spec)(SSL *ssl, size_t *out_consumed, |
| 2634 |
uint8_t *out_alert, |
| 2635 |
Span<uint8_t> in); |
| 2636 |
// Use the |ssl_open_app_data| wrapper. |
| 2637 |
ssl_open_record_t (*open_app_data)(SSL *ssl, Span<uint8_t> *out, |
| 2638 |
size_t *out_consumed, uint8_t *out_alert, |
| 2639 |
Span<uint8_t> in); |
| 2640 |
// write_app_data encrypts and writes |in| as application data. On success, it |
| 2641 |
// returns one and sets |*out_bytes_written| to the number of bytes of |in| |
| 2642 |
// written. Otherwise, it returns <= 0 and sets |*out_needs_handshake| to |
| 2643 |
// whether the operation failed because the caller needs to drive the |
| 2644 |
// handshake. |
| 2645 |
int (*write_app_data)(SSL *ssl, bool *out_needs_handshake, |
| 2646 |
size_t *out_bytes_written, Span<const uint8_t> in); |
| 2647 |
int (*dispatch_alert)(SSL *ssl); |
| 2648 |
// init_message begins a new handshake message of type |type|. |cbb| is the |
| 2649 |
// root CBB to be passed into |finish_message|. |*body| is set to a child CBB |
| 2650 |
// the caller should write to. It returns true on success and false on error. |
| 2651 |
bool (*init_message)(const SSL *ssl, CBB *cbb, CBB *body, uint8_t type); |
| 2652 |
// finish_message finishes a handshake message. It sets |*out_msg| to the |
| 2653 |
// serialized message. It returns true on success and false on error. |
| 2654 |
bool (*finish_message)(const SSL *ssl, CBB *cbb, |
| 2655 |
bssl::Array<uint8_t> *out_msg); |
| 2656 |
// add_message adds a handshake message to the pending flight. It returns |
| 2657 |
// true on success and false on error. |
| 2658 |
bool (*add_message)(SSL *ssl, bssl::Array<uint8_t> msg); |
| 2659 |
// add_change_cipher_spec adds a ChangeCipherSpec record to the pending |
| 2660 |
// flight. It returns true on success and false on error. |
| 2661 |
bool (*add_change_cipher_spec)(SSL *ssl); |
| 2662 |
// flush_flight flushes the pending flight to the transport. It returns one on |
| 2663 |
// success and <= 0 on error. |
| 2664 |
int (*flush_flight)(SSL *ssl); |
| 2665 |
// on_handshake_complete is called when the handshake is complete. |
| 2666 |
void (*on_handshake_complete)(SSL *ssl); |
| 2667 |
// set_read_state sets |ssl|'s read cipher state and level to |aead_ctx| and |
| 2668 |
// |level|. In QUIC, |aead_ctx| is a placeholder object and |secret_for_quic| |
| 2669 |
// is the original secret. This function returns true on success and false on |
| 2670 |
// error. |
| 2671 |
bool (*set_read_state)(SSL *ssl, ssl_encryption_level_t level, |
| 2672 |
UniquePtr<SSLAEADContext> aead_ctx, |
| 2673 |
Span<const uint8_t> secret_for_quic); |
| 2674 |
// set_write_state sets |ssl|'s write cipher state and level to |aead_ctx| and |
| 2675 |
// |level|. In QUIC, |aead_ctx| is a placeholder object and |secret_for_quic| |
| 2676 |
// is the original secret. This function returns true on success and false on |
| 2677 |
// error. |
| 2678 |
bool (*set_write_state)(SSL *ssl, ssl_encryption_level_t level, |
| 2679 |
UniquePtr<SSLAEADContext> aead_ctx, |
| 2680 |
Span<const uint8_t> secret_for_quic); |
| 2681 |
}; |
| 2682 |
|
| 2683 |
// The following wrappers call |open_*| but handle |read_shutdown| correctly. |
| 2684 |
|
| 2685 |
// ssl_open_handshake processes a record from |in| for reading a handshake |
| 2686 |
// message. |
| 2687 |
ssl_open_record_t ssl_open_handshake(SSL *ssl, size_t *out_consumed, |
| 2688 |
uint8_t *out_alert, Span<uint8_t> in); |
| 2689 |
|
| 2690 |
// ssl_open_change_cipher_spec processes a record from |in| for reading a |
| 2691 |
// ChangeCipherSpec. |
| 2692 |
ssl_open_record_t ssl_open_change_cipher_spec(SSL *ssl, size_t *out_consumed, |
| 2693 |
uint8_t *out_alert, |
| 2694 |
Span<uint8_t> in); |
| 2695 |
|
| 2696 |
// ssl_open_app_data processes a record from |in| for reading application data. |
| 2697 |
// On success, it returns |ssl_open_record_success| and sets |*out| to the |
| 2698 |
// input. If it encounters a post-handshake message, it returns |
| 2699 |
// |ssl_open_record_discard|. The caller should then retry, after processing any |
| 2700 |
// messages received with |get_message|. |
| 2701 |
ssl_open_record_t ssl_open_app_data(SSL *ssl, Span<uint8_t> *out, |
| 2702 |
size_t *out_consumed, uint8_t *out_alert, |
| 2703 |
Span<uint8_t> in); |
| 2704 |
|
| 2705 |
struct SSL_X509_METHOD { |
| 2706 |
// check_client_CA_list returns one if |names| is a good list of X.509 |
| 2707 |
// distinguished names and zero otherwise. This is used to ensure that we can |
| 2708 |
// reject unparsable values at handshake time when using crypto/x509. |
| 2709 |
bool (*check_client_CA_list)(STACK_OF(CRYPTO_BUFFER) *names); |
| 2710 |
|
| 2711 |
// cert_clear frees and NULLs all X509 certificate-related state. |
| 2712 |
void (*cert_clear)(CERT *cert); |
| 2713 |
// cert_free frees all X509-related state. |
| 2714 |
void (*cert_free)(CERT *cert); |
| 2715 |
// cert_dup duplicates any needed fields from |cert| to |new_cert|. |
| 2716 |
void (*cert_dup)(CERT *new_cert, const CERT *cert); |
| 2717 |
// cert_flush_cached_chain drops any cached |X509|-based certificate chain |
| 2718 |
// from |cert|. |
| 2719 |
void (*cert_flush_cached_chain)(CERT *cert); |
| 2720 |
// cert_flush_leaf drops the |X509|-based leaf certificate from |cert|. |
| 2721 |
void (*cert_flush_leaf)(CERT *cert); |
| 2722 |
|
| 2723 |
// session_cache_objects fills out |sess->x509_peer| and |sess->x509_chain| |
| 2724 |
// from |sess->certs| and erases |sess->x509_chain_without_leaf|. It returns |
| 2725 |
// true on success or false on error. |
| 2726 |
bool (*session_cache_objects)(SSL_SESSION *session); |
| 2727 |
// session_dup duplicates any needed fields from |session| to |new_session|. |
| 2728 |
// It returns true on success or false on error. |
| 2729 |
bool (*session_dup)(SSL_SESSION *new_session, const SSL_SESSION *session); |
| 2730 |
// session_clear frees any X509-related state from |session|. |
| 2731 |
void (*session_clear)(SSL_SESSION *session); |
| 2732 |
// session_verify_cert_chain verifies the certificate chain in |session|, |
| 2733 |
// and sets |session->verify_result| and |session->x509_verified_chain|. It |
| 2734 |
// returns true on success or false on error. |
| 2735 |
bool (*session_verify_cert_chain)(SSL_SESSION *session, SSL_HANDSHAKE *ssl, |
| 2736 |
uint8_t *out_alert); |
| 2737 |
|
| 2738 |
// hs_flush_cached_ca_names drops any cached peer CA |X509_NAME|s from |
| 2739 |
// |hs->ssl->s3|. Called when |hs->ca_names| is reparsed. |
| 2740 |
void (*hs_flush_cached_ca_names)(SSL_HANDSHAKE *hs); |
| 2741 |
// ssl_new does any necessary initialisation of |hs|. It returns true on |
| 2742 |
// success or false on error. |
| 2743 |
bool (*ssl_new)(SSL_HANDSHAKE *hs); |
| 2744 |
// ssl_free frees anything created by |ssl_new|. |
| 2745 |
void (*ssl_config_free)(SSL_CONFIG *cfg); |
| 2746 |
// ssl_flush_cached_client_CA drops any cached |X509_NAME|s from |ssl|. |
| 2747 |
void (*ssl_flush_cached_client_CA)(SSL_CONFIG *cfg); |
| 2748 |
// ssl_auto_chain_if_needed runs the deprecated auto-chaining logic if |
| 2749 |
// necessary. On success, it updates |ssl|'s certificate configuration as |
| 2750 |
// needed and returns true. Otherwise, it returns false. |
| 2751 |
bool (*ssl_auto_chain_if_needed)(SSL_HANDSHAKE *hs); |
| 2752 |
// ssl_ctx_new does any necessary initialisation of |ctx|. It returns true on |
| 2753 |
// success or false on error. |
| 2754 |
bool (*ssl_ctx_new)(SSL_CTX *ctx); |
| 2755 |
// ssl_ctx_free frees anything created by |ssl_ctx_new|. |
| 2756 |
void (*ssl_ctx_free)(SSL_CTX *ctx); |
| 2757 |
// ssl_ctx_flush_cached_client_CA drops any cached |X509_NAME|s from |ctx|. |
| 2758 |
void (*ssl_ctx_flush_cached_client_CA)(SSL_CTX *ssl); |
| 2759 |
}; |
| 2760 |
|
| 2761 |
// ssl_crypto_x509_method provides the |SSL_X509_METHOD| functions using |
| 2762 |
// crypto/x509. |
| 2763 |
extern const SSL_X509_METHOD ssl_crypto_x509_method; |
| 2764 |
|
| 2765 |
// ssl_noop_x509_method provides the |SSL_X509_METHOD| functions that avoid |
| 2766 |
// crypto/x509. |
| 2767 |
extern const SSL_X509_METHOD ssl_noop_x509_method; |
| 2768 |
|
| 2769 |
struct TicketKey { |
| 2770 |
static constexpr bool kAllowUniquePtr = true; |
| 2771 |
|
| 2772 |
uint8_t name[SSL_TICKET_KEY_NAME_LEN] = {0}; |
| 2773 |
uint8_t hmac_key[16] = {0}; |
| 2774 |
uint8_t aes_key[16] = {0}; |
| 2775 |
// next_rotation_tv_sec is the time (in seconds from the epoch) when the |
| 2776 |
// current key should be superseded by a new key, or the time when a previous |
| 2777 |
// key should be dropped. If zero, then the key should not be automatically |
| 2778 |
// rotated. |
| 2779 |
uint64_t next_rotation_tv_sec = 0; |
| 2780 |
}; |
| 2781 |
|
| 2782 |
struct CertCompressionAlg { |
| 2783 |
static constexpr bool kAllowUniquePtr = true; |
| 2784 |
|
| 2785 |
ssl_cert_compression_func_t compress = nullptr; |
| 2786 |
ssl_cert_decompression_func_t decompress = nullptr; |
| 2787 |
uint16_t alg_id = 0; |
| 2788 |
}; |
| 2789 |
|
| 2790 |
BSSL_NAMESPACE_END |
| 2791 |
|
| 2792 |
DEFINE_LHASH_OF(SSL_SESSION) |
| 2793 |
|
| 2794 |
BSSL_NAMESPACE_BEGIN |
| 2795 |
|
| 2796 |
// An ssl_shutdown_t describes the shutdown state of one end of the connection, |
| 2797 |
// whether it is alive or has been shutdown via close_notify or fatal alert. |
| 2798 |
enum ssl_shutdown_t { |
| 2799 |
ssl_shutdown_none = 0, |
| 2800 |
ssl_shutdown_close_notify = 1, |
| 2801 |
ssl_shutdown_error = 2, |
| 2802 |
}; |
| 2803 |
|
| 2804 |
enum ssl_ech_status_t { |
| 2805 |
// ssl_ech_none indicates ECH was not offered, or we have not gotten far |
| 2806 |
// enough in the handshake to determine the status. |
| 2807 |
ssl_ech_none, |
| 2808 |
// ssl_ech_accepted indicates the server accepted ECH. |
| 2809 |
ssl_ech_accepted, |
| 2810 |
// ssl_ech_rejected indicates the server was offered ECH but rejected it. |
| 2811 |
ssl_ech_rejected, |
| 2812 |
}; |
| 2813 |
|
| 2814 |
#define SSL3_SEND_ALERT_SIZE 2 |
| 2815 |
#define TLS_SEQ_NUM_SIZE 8 |
| 2816 |
#define SSL3_CHANNEL_ID_SIZE 64 |
| 2817 |
#define PREV_FINISHED_MAX_SIZE EVP_MAX_MD_SIZE |
| 2818 |
|
| 2819 |
struct SSL3_STATE { |
| 2820 |
static constexpr bool kAllowUniquePtr = true; |
| 2821 |
|
| 2822 |
SSL3_STATE(); |
| 2823 |
~SSL3_STATE(); |
| 2824 |
|
| 2825 |
uint8_t read_sequence[TLS_SEQ_NUM_SIZE] = {0}; |
| 2826 |
uint8_t write_sequence[TLS_SEQ_NUM_SIZE] = {0}; |
| 2827 |
|
| 2828 |
uint8_t server_random[SSL3_RANDOM_SIZE] = {0}; |
| 2829 |
uint8_t client_random[SSL3_RANDOM_SIZE] = {0}; |
| 2830 |
|
| 2831 |
// read_buffer holds data from the transport to be processed. |
| 2832 |
SSLBuffer read_buffer; |
| 2833 |
// write_buffer holds data to be written to the transport. |
| 2834 |
SSLBuffer write_buffer; |
| 2835 |
|
| 2836 |
// pending_app_data is the unconsumed application data. It points into |
| 2837 |
// |read_buffer|. |
| 2838 |
Span<uint8_t> pending_app_data; |
| 2839 |
|
| 2840 |
// unreported_bytes_written is the number of bytes successfully written to the |
| 2841 |
// transport, but not yet reported to the caller. The next |SSL_write| will |
| 2842 |
// skip this many bytes from the input. This is used if |
| 2843 |
// |SSL_MODE_ENABLE_PARTIAL_WRITE| is disabled, in which case |SSL_write| only |
| 2844 |
// reports bytes written when the full caller input is written. |
| 2845 |
size_t unreported_bytes_written = 0; |
| 2846 |
|
| 2847 |
// pending_write, if |has_pending_write| is true, is the caller-supplied data |
| 2848 |
// corresponding to the current pending write. This is used to check the |
| 2849 |
// caller retried with a compatible buffer. |
| 2850 |
Span<const uint8_t> pending_write; |
| 2851 |
|
| 2852 |
// pending_write_type, if |has_pending_write| is true, is the record type |
| 2853 |
// for the current pending write. |
| 2854 |
// |
| 2855 |
// TODO(davidben): Remove this when alerts are moved out of this write path. |
| 2856 |
uint8_t pending_write_type = 0; |
| 2857 |
|
| 2858 |
// read_shutdown is the shutdown state for the read half of the connection. |
| 2859 |
enum ssl_shutdown_t read_shutdown = ssl_shutdown_none; |
| 2860 |
|
| 2861 |
// write_shutdown is the shutdown state for the write half of the connection. |
| 2862 |
enum ssl_shutdown_t write_shutdown = ssl_shutdown_none; |
| 2863 |
|
| 2864 |
// read_error, if |read_shutdown| is |ssl_shutdown_error|, is the error for |
| 2865 |
// the receive half of the connection. |
| 2866 |
UniquePtr<ERR_SAVE_STATE> read_error; |
| 2867 |
|
| 2868 |
int total_renegotiations = 0; |
| 2869 |
|
| 2870 |
// This holds a variable that indicates what we were doing when a 0 or -1 is |
| 2871 |
// returned. This is needed for non-blocking IO so we know what request |
| 2872 |
// needs re-doing when in SSL_accept or SSL_connect |
| 2873 |
int rwstate = SSL_ERROR_NONE; |
| 2874 |
|
| 2875 |
// key_update_pending will be either |SSL_KEY_UPDATE_REQUESTED| or |
| 2876 |
// |SSL_KEY_UPDATE_NOT_REQUESTED| if we have a KeyUpdate acknowledgment |
| 2877 |
// outstanding. |SSL_KEY_UPDATE_NONE| indicates that no KeyUpdates |
| 2878 |
// acknowledgements are pending. |
| 2879 |
int key_update_pending = SSL_KEY_UPDATE_NONE; |
| 2880 |
|
| 2881 |
enum ssl_encryption_level_t read_level = ssl_encryption_initial; |
| 2882 |
enum ssl_encryption_level_t write_level = ssl_encryption_initial; |
| 2883 |
|
| 2884 |
// early_data_skipped is the amount of early data that has been skipped by the |
| 2885 |
// record layer. |
| 2886 |
uint16_t early_data_skipped = 0; |
| 2887 |
|
| 2888 |
// empty_record_count is the number of consecutive empty records received. |
| 2889 |
uint8_t empty_record_count = 0; |
| 2890 |
|
| 2891 |
// warning_alert_count is the number of consecutive warning alerts |
| 2892 |
// received. |
| 2893 |
uint8_t warning_alert_count = 0; |
| 2894 |
|
| 2895 |
// ech_status indicates whether ECH was accepted by the server. |
| 2896 |
ssl_ech_status_t ech_status = ssl_ech_none; |
| 2897 |
|
| 2898 |
// skip_early_data instructs the record layer to skip unexpected early data |
| 2899 |
// messages when 0RTT is rejected. |
| 2900 |
bool skip_early_data : 1; |
| 2901 |
|
| 2902 |
// have_version is true if the connection's final version is known. Otherwise |
| 2903 |
// the version has not been negotiated yet. |
| 2904 |
bool have_version : 1; |
| 2905 |
|
| 2906 |
// v2_hello_done is true if the peer's V2ClientHello, if any, has been handled |
| 2907 |
// and future messages should use the record layer. |
| 2908 |
bool v2_hello_done : 1; |
| 2909 |
|
| 2910 |
// is_v2_hello is true if the current handshake message was derived from a |
| 2911 |
// V2ClientHello rather than received from the peer directly. |
| 2912 |
bool is_v2_hello : 1; |
| 2913 |
|
| 2914 |
// has_message is true if the current handshake message has been returned |
| 2915 |
// at least once by |get_message| and false otherwise. |
| 2916 |
bool has_message : 1; |
| 2917 |
|
| 2918 |
// initial_handshake_complete is true if the initial handshake has |
| 2919 |
// completed. |
| 2920 |
bool initial_handshake_complete : 1; |
| 2921 |
|
| 2922 |
// session_reused indicates whether a session was resumed. |
| 2923 |
bool session_reused : 1; |
| 2924 |
|
| 2925 |
// delegated_credential_used is whether we presented a delegated credential to |
| 2926 |
// the peer. |
| 2927 |
bool delegated_credential_used : 1; |
| 2928 |
|
| 2929 |
bool send_connection_binding : 1; |
| 2930 |
|
| 2931 |
// channel_id_valid is true if, on the server, the client has negotiated a |
| 2932 |
// Channel ID and the |channel_id| field is filled in. |
| 2933 |
bool channel_id_valid : 1; |
| 2934 |
|
| 2935 |
// early_data_accepted is true if early data was accepted by the server. |
| 2936 |
bool early_data_accepted : 1; |
| 2937 |
|
| 2938 |
// alert_dispatch is true there is an alert in |send_alert| to be sent. |
| 2939 |
bool alert_dispatch : 1; |
| 2940 |
|
| 2941 |
// renegotiate_pending is whether the read half of the channel is blocked on a |
| 2942 |
// HelloRequest. |
| 2943 |
bool renegotiate_pending : 1; |
| 2944 |
|
| 2945 |
// used_hello_retry_request is whether the handshake used a TLS 1.3 |
| 2946 |
// HelloRetryRequest message. |
| 2947 |
bool used_hello_retry_request : 1; |
| 2948 |
|
| 2949 |
// was_key_usage_invalid is whether the handshake succeeded despite using a |
| 2950 |
// TLS mode which was incompatible with the leaf certificate's keyUsage |
| 2951 |
// extension. |
| 2952 |
bool was_key_usage_invalid : 1; |
| 2953 |
|
| 2954 |
// hs_buf is the buffer of handshake data to process. |
| 2955 |
UniquePtr<BUF_MEM> hs_buf; |
| 2956 |
|
| 2957 |
// pending_hs_data contains the pending handshake data that has not yet |
| 2958 |
// been encrypted to |pending_flight|. This allows packing the handshake into |
| 2959 |
// fewer records. |
| 2960 |
UniquePtr<BUF_MEM> pending_hs_data; |
| 2961 |
|
| 2962 |
// pending_flight is the pending outgoing flight. This is used to flush each |
| 2963 |
// handshake flight in a single write. |write_buffer| must be written out |
| 2964 |
// before this data. |
| 2965 |
UniquePtr<BUF_MEM> pending_flight; |
| 2966 |
|
| 2967 |
// pending_flight_offset is the number of bytes of |pending_flight| which have |
| 2968 |
// been successfully written. |
| 2969 |
uint32_t pending_flight_offset = 0; |
| 2970 |
|
| 2971 |
// ticket_age_skew is the difference, in seconds, between the client-sent |
| 2972 |
// ticket age and the server-computed value in TLS 1.3 server connections |
| 2973 |
// which resumed a session. |
| 2974 |
int32_t ticket_age_skew = 0; |
| 2975 |
|
| 2976 |
// ssl_early_data_reason stores details on why 0-RTT was accepted or rejected. |
| 2977 |
enum ssl_early_data_reason_t early_data_reason = ssl_early_data_unknown; |
| 2978 |
|
| 2979 |
// aead_read_ctx is the current read cipher state. |
| 2980 |
UniquePtr<SSLAEADContext> aead_read_ctx; |
| 2981 |
|
| 2982 |
// aead_write_ctx is the current write cipher state. |
| 2983 |
UniquePtr<SSLAEADContext> aead_write_ctx; |
| 2984 |
|
| 2985 |
// hs is the handshake state for the current handshake or NULL if there isn't |
| 2986 |
// one. |
| 2987 |
UniquePtr<SSL_HANDSHAKE> hs; |
| 2988 |
|
| 2989 |
// cached_x509_peer_ca_names, on the client, contains the list of CAs |
| 2990 |
// received in a CertificateRequest message, as X509_NAMEs. This is eagerly |
| 2991 |
// populated from |hs->ca_names| before the handshake object is destroyed, so |
| 2992 |
// that |SSL_get_client_CA_list| can return it after the handshake. |
| 2993 |
STACK_OF(X509_NAME) *cached_x509_peer_ca_names = nullptr; |
| 2994 |
|
| 2995 |
// peer_key is the peer's ECDH key for both TLS 1.2/1.3. This is only used |
| 2996 |
// for observing with |SSL_get_peer_tmp_key| and is not serialized as part of |
| 2997 |
// the SSL Transfer feature. |
| 2998 |
Array<uint8_t> peer_key; |
| 2999 |
|
| 3000 |
uint8_t write_traffic_secret[SSL_MAX_MD_SIZE] = {0}; |
| 3001 |
uint8_t read_traffic_secret[SSL_MAX_MD_SIZE] = {0}; |
| 3002 |
uint8_t exporter_secret[SSL_MAX_MD_SIZE] = {0}; |
| 3003 |
uint8_t write_traffic_secret_len = 0; |
| 3004 |
uint8_t read_traffic_secret_len = 0; |
| 3005 |
uint8_t exporter_secret_len = 0; |
| 3006 |
|
| 3007 |
// Connection binding to prevent renegotiation attacks |
| 3008 |
uint8_t previous_client_finished[PREV_FINISHED_MAX_SIZE] = {0}; |
| 3009 |
uint8_t previous_client_finished_len = 0; |
| 3010 |
uint8_t previous_server_finished_len = 0; |
| 3011 |
uint8_t previous_server_finished[PREV_FINISHED_MAX_SIZE] = {0}; |
| 3012 |
|
| 3013 |
uint8_t send_alert[SSL3_SEND_ALERT_SIZE] = {0}; |
| 3014 |
|
| 3015 |
// established_session is the session established by the connection. This |
| 3016 |
// session is only filled upon the completion of the handshake and is |
| 3017 |
// immutable. |
| 3018 |
UniquePtr<SSL_SESSION> established_session; |
| 3019 |
|
| 3020 |
// Next protocol negotiation. For the client, this is the protocol that we |
| 3021 |
// sent in NextProtocol and is set when handling ServerHello extensions. |
| 3022 |
// |
| 3023 |
// For a server, this is the client's selected_protocol from NextProtocol and |
| 3024 |
// is set when handling the NextProtocol message, before the Finished |
| 3025 |
// message. |
| 3026 |
Array<uint8_t> next_proto_negotiated; |
| 3027 |
|
| 3028 |
// ALPN information |
| 3029 |
// (we are in the process of transitioning from NPN to ALPN.) |
| 3030 |
|
| 3031 |
// In a server these point to the selected ALPN protocol after the |
| 3032 |
// ClientHello has been processed. In a client these contain the protocol |
| 3033 |
// that the server selected once the ServerHello has been processed. |
| 3034 |
Array<uint8_t> alpn_selected; |
| 3035 |
|
| 3036 |
// hostname, on the server, is the value of the SNI extension. |
| 3037 |
UniquePtr<char> hostname; |
| 3038 |
|
| 3039 |
// For a server: |
| 3040 |
// If |channel_id_valid| is true, then this contains the |
| 3041 |
// verified Channel ID from the client: a P256 point, (x,y), where |
| 3042 |
// each are big-endian values. |
| 3043 |
uint8_t channel_id[SSL3_CHANNEL_ID_SIZE] = {0}; |
| 3044 |
|
| 3045 |
// Contains the QUIC transport params received by the peer. |
| 3046 |
Array<uint8_t> peer_quic_transport_params; |
| 3047 |
|
| 3048 |
// srtp_profile is the selected SRTP protection profile for |
| 3049 |
// DTLS-SRTP. |
| 3050 |
const SRTP_PROTECTION_PROFILE *srtp_profile = nullptr; |
| 3051 |
}; |
| 3052 |
|
| 3053 |
// lengths of messages |
| 3054 |
#define DTLS1_RT_HEADER_LENGTH 13 |
| 3055 |
|
| 3056 |
#define DTLS1_HM_HEADER_LENGTH 12 |
| 3057 |
|
| 3058 |
#define DTLS1_CCS_HEADER_LENGTH 1 |
| 3059 |
|
| 3060 |
#define DTLS1_AL_HEADER_LENGTH 2 |
| 3061 |
|
| 3062 |
struct hm_header_st { |
| 3063 |
uint8_t type; |
| 3064 |
uint32_t msg_len; |
| 3065 |
uint16_t seq; |
| 3066 |
uint32_t frag_off; |
| 3067 |
uint32_t frag_len; |
| 3068 |
}; |
| 3069 |
|
| 3070 |
// An hm_fragment is an incoming DTLS message, possibly not yet assembled. |
| 3071 |
struct hm_fragment { |
| 3072 |
static constexpr bool kAllowUniquePtr = true; |
| 3073 |
|
| 3074 |
hm_fragment() {} |
| 3075 |
hm_fragment(const hm_fragment &) = delete; |
| 3076 |
hm_fragment &operator=(const hm_fragment &) = delete; |
| 3077 |
|
| 3078 |
~hm_fragment(); |
| 3079 |
|
| 3080 |
// type is the type of the message. |
| 3081 |
uint8_t type = 0; |
| 3082 |
// seq is the sequence number of this message. |
| 3083 |
uint16_t seq = 0; |
| 3084 |
// msg_len is the length of the message body. |
| 3085 |
uint32_t msg_len = 0; |
| 3086 |
// data is a pointer to the message, including message header. It has length |
| 3087 |
// |DTLS1_HM_HEADER_LENGTH| + |msg_len|. |
| 3088 |
uint8_t *data = nullptr; |
| 3089 |
// reassembly is a bitmask of |msg_len| bits corresponding to which parts of |
| 3090 |
// the message have been received. It is NULL if the message is complete. |
| 3091 |
uint8_t *reassembly = nullptr; |
| 3092 |
}; |
| 3093 |
|
| 3094 |
struct OPENSSL_timeval { |
| 3095 |
uint64_t tv_sec; |
| 3096 |
uint32_t tv_usec; |
| 3097 |
}; |
| 3098 |
|
| 3099 |
struct DTLS1_STATE { |
| 3100 |
static constexpr bool kAllowUniquePtr = true; |
| 3101 |
|
| 3102 |
DTLS1_STATE(); |
| 3103 |
~DTLS1_STATE(); |
| 3104 |
|
| 3105 |
// has_change_cipher_spec is true if we have received a ChangeCipherSpec from |
| 3106 |
// the peer in this epoch. |
| 3107 |
bool has_change_cipher_spec : 1; |
| 3108 |
|
| 3109 |
// outgoing_messages_complete is true if |outgoing_messages| has been |
| 3110 |
// completed by an attempt to flush it. Future calls to |add_message| and |
| 3111 |
// |add_change_cipher_spec| will start a new flight. |
| 3112 |
bool outgoing_messages_complete : 1; |
| 3113 |
|
| 3114 |
// flight_has_reply is true if the current outgoing flight is complete and has |
| 3115 |
// processed at least one message. This is used to detect whether we or the |
| 3116 |
// peer sent the final flight. |
| 3117 |
bool flight_has_reply : 1; |
| 3118 |
|
| 3119 |
// The current data and handshake epoch. This is initially undefined, and |
| 3120 |
// starts at zero once the initial handshake is completed. |
| 3121 |
uint16_t r_epoch = 0; |
| 3122 |
uint16_t w_epoch = 0; |
| 3123 |
|
| 3124 |
// records being received in the current epoch |
| 3125 |
DTLS1_BITMAP bitmap; |
| 3126 |
|
| 3127 |
uint16_t handshake_write_seq = 0; |
| 3128 |
uint16_t handshake_read_seq = 0; |
| 3129 |
|
| 3130 |
// save last sequence number for retransmissions |
| 3131 |
uint8_t last_write_sequence[8] = {0}; |
| 3132 |
UniquePtr<SSLAEADContext> last_aead_write_ctx; |
| 3133 |
|
| 3134 |
// incoming_messages is a ring buffer of incoming handshake messages that have |
| 3135 |
// yet to be processed. The front of the ring buffer is message number |
| 3136 |
// |handshake_read_seq|, at position |handshake_read_seq| % |
| 3137 |
// |SSL_MAX_HANDSHAKE_FLIGHT|. |
| 3138 |
UniquePtr<hm_fragment> incoming_messages[SSL_MAX_HANDSHAKE_FLIGHT]; |
| 3139 |
|
| 3140 |
// outgoing_messages is the queue of outgoing messages from the last handshake |
| 3141 |
// flight. |
| 3142 |
DTLS_OUTGOING_MESSAGE outgoing_messages[SSL_MAX_HANDSHAKE_FLIGHT]; |
| 3143 |
uint8_t outgoing_messages_len = 0; |
| 3144 |
|
| 3145 |
// outgoing_written is the number of outgoing messages that have been |
| 3146 |
// written. |
| 3147 |
uint8_t outgoing_written = 0; |
| 3148 |
// outgoing_offset is the number of bytes of the next outgoing message have |
| 3149 |
// been written. |
| 3150 |
uint32_t outgoing_offset = 0; |
| 3151 |
|
| 3152 |
unsigned mtu = 0; // max DTLS packet size |
| 3153 |
|
| 3154 |
// num_timeouts is the number of times the retransmit timer has fired since |
| 3155 |
// the last time it was reset. |
| 3156 |
unsigned num_timeouts = 0; |
| 3157 |
|
| 3158 |
// Indicates when the last handshake msg or heartbeat sent will |
| 3159 |
// timeout. |
| 3160 |
struct OPENSSL_timeval next_timeout = {0, 0}; |
| 3161 |
|
| 3162 |
// timeout_duration_ms is the timeout duration in milliseconds. |
| 3163 |
unsigned timeout_duration_ms = 0; |
| 3164 |
}; |
| 3165 |
|
| 3166 |
// An ALPSConfig is a pair of ALPN protocol and settings value to use with ALPS. |
| 3167 |
struct ALPSConfig { |
| 3168 |
Array<uint8_t> protocol; |
| 3169 |
Array<uint8_t> settings; |
| 3170 |
}; |
| 3171 |
|
| 3172 |
// SSL_CONFIG contains configuration bits that can be shed after the handshake |
| 3173 |
// completes. Objects of this type are not shared; they are unique to a |
| 3174 |
// particular |SSL|. |
| 3175 |
// |
| 3176 |
// See SSL_shed_handshake_config() for more about the conditions under which |
| 3177 |
// configuration can be shed. |
| 3178 |
struct SSL_CONFIG { |
| 3179 |
static constexpr bool kAllowUniquePtr = true; |
| 3180 |
|
| 3181 |
explicit SSL_CONFIG(SSL *ssl_arg); |
| 3182 |
~SSL_CONFIG(); |
| 3183 |
|
| 3184 |
// ssl is a non-owning pointer to the parent |SSL| object. |
| 3185 |
SSL *const ssl = nullptr; |
| 3186 |
|
| 3187 |
// conf_max_version is the maximum acceptable version configured by |
| 3188 |
// |SSL_set_max_proto_version|. Note this version is not normalized in DTLS |
| 3189 |
// and is further constrained by |SSL_OP_NO_*|. |
| 3190 |
uint16_t conf_max_version = 0; |
| 3191 |
|
| 3192 |
// conf_min_version is the minimum acceptable version configured by |
| 3193 |
// |SSL_set_min_proto_version|. Note this version is not normalized in DTLS |
| 3194 |
// and is further constrained by |SSL_OP_NO_*|. |
| 3195 |
uint16_t conf_min_version = 0; |
| 3196 |
|
| 3197 |
X509_VERIFY_PARAM *param = nullptr; |
| 3198 |
|
| 3199 |
// cipher_list holds all available cipher suites for tls 1.3, |
| 3200 |
// and 1.2 and below. Any configured ciphersuites here take precedence |
| 3201 |
// over the parent |SSL_CTX| object. |
| 3202 |
UniquePtr<SSLCipherPreferenceList> cipher_list; |
| 3203 |
|
| 3204 |
// tls13_cipher_list holds the default or configured tls1.3 and above |
| 3205 |
// cipher suites. Any configured ciphersuites here take precedence |
| 3206 |
// over the parent |SSL_CTX| object. |
| 3207 |
UniquePtr<SSLCipherPreferenceList> tls13_cipher_list; |
| 3208 |
|
| 3209 |
// This is used to hold the local certificate used (i.e. the server |
| 3210 |
// certificate for a server or the client certificate for a client). |
| 3211 |
UniquePtr<CERT> cert; |
| 3212 |
|
| 3213 |
int (*verify_callback)(int ok, |
| 3214 |
X509_STORE_CTX *ctx) = |
| 3215 |
nullptr; // fail if callback returns 0 |
| 3216 |
|
| 3217 |
enum ssl_verify_result_t (*custom_verify_callback)( |
| 3218 |
SSL *ssl, uint8_t *out_alert) = nullptr; |
| 3219 |
// Server-only: psk_identity_hint is the identity hint to send in |
| 3220 |
// PSK-based key exchanges. |
| 3221 |
UniquePtr<char> psk_identity_hint; |
| 3222 |
|
| 3223 |
unsigned (*psk_client_callback)(SSL *ssl, const char *hint, char *identity, |
| 3224 |
unsigned max_identity_len, uint8_t *psk, |
| 3225 |
unsigned max_psk_len) = nullptr; |
| 3226 |
unsigned (*psk_server_callback)(SSL *ssl, const char *identity, uint8_t *psk, |
| 3227 |
unsigned max_psk_len) = nullptr; |
| 3228 |
|
| 3229 |
// for server side, keep the list of CA_dn we can use |
| 3230 |
UniquePtr<STACK_OF(CRYPTO_BUFFER)> client_CA; |
| 3231 |
|
| 3232 |
// cached_x509_client_CA is a cache of parsed versions of the elements of |
| 3233 |
// |client_CA|. |
| 3234 |
STACK_OF(X509_NAME) *cached_x509_client_CA = nullptr; |
| 3235 |
|
| 3236 |
Array<uint16_t> supported_group_list; // our list |
| 3237 |
|
| 3238 |
// channel_id_private is the client's Channel ID private key, or null if |
| 3239 |
// Channel ID should not be offered on this connection. |
| 3240 |
UniquePtr<EVP_PKEY> channel_id_private; |
| 3241 |
|
| 3242 |
// For a client, this contains the list of supported protocols in wire |
| 3243 |
// format. |
| 3244 |
Array<uint8_t> alpn_client_proto_list; |
| 3245 |
|
| 3246 |
// alps_configs contains the list of supported protocols to use with ALPS, |
| 3247 |
// along with their corresponding ALPS values. |
| 3248 |
GrowableArray<ALPSConfig> alps_configs; |
| 3249 |
|
| 3250 |
// Contains the QUIC transport params that this endpoint will send. |
| 3251 |
Array<uint8_t> quic_transport_params; |
| 3252 |
|
| 3253 |
// Contains the context used to decide whether to accept early data in QUIC. |
| 3254 |
Array<uint8_t> quic_early_data_context; |
| 3255 |
|
| 3256 |
// verify_sigalgs, if not empty, is the set of signature algorithms |
| 3257 |
// accepted from the peer in decreasing order of preference. |
| 3258 |
Array<uint16_t> verify_sigalgs; |
| 3259 |
|
| 3260 |
// srtp_profiles is the list of configured SRTP protection profiles for |
| 3261 |
// DTLS-SRTP. |
| 3262 |
UniquePtr<STACK_OF(SRTP_PROTECTION_PROFILE)> srtp_profiles; |
| 3263 |
|
| 3264 |
// client_ech_config_list, if not empty, is a serialized ECHConfigList |
| 3265 |
// structure for the client to use when negotiating ECH. |
| 3266 |
Array<uint8_t> client_ech_config_list; |
| 3267 |
|
| 3268 |
// verify_mode is a bitmask of |SSL_VERIFY_*| values. |
| 3269 |
uint8_t verify_mode = SSL_VERIFY_NONE; |
| 3270 |
|
| 3271 |
// ech_grease_enabled controls whether ECH GREASE may be sent in the |
| 3272 |
// ClientHello. |
| 3273 |
bool ech_grease_enabled : 1; |
| 3274 |
|
| 3275 |
// Enable signed certificate time stamps. Currently client only. |
| 3276 |
bool signed_cert_timestamps_enabled : 1; |
| 3277 |
|
| 3278 |
// ocsp_stapling_enabled is only used by client connections and indicates |
| 3279 |
// whether OCSP stapling will be requested. |
| 3280 |
bool ocsp_stapling_enabled : 1; |
| 3281 |
|
| 3282 |
// channel_id_enabled is copied from the |SSL_CTX|. For a server, it means |
| 3283 |
// that we'll accept Channel IDs from clients. It is ignored on the client. |
| 3284 |
bool channel_id_enabled : 1; |
| 3285 |
|
| 3286 |
// If enforce_rsa_key_usage is true, the handshake will fail if the |
| 3287 |
// keyUsage extension is present and incompatible with the TLS usage. |
| 3288 |
// This field is not read until after certificate verification. |
| 3289 |
bool enforce_rsa_key_usage : 1; |
| 3290 |
|
| 3291 |
// retain_only_sha256_of_client_certs is true if we should compute the SHA256 |
| 3292 |
// hash of the peer's certificate and then discard it to save memory and |
| 3293 |
// session space. Only effective on the server side. |
| 3294 |
bool retain_only_sha256_of_client_certs : 1; |
| 3295 |
|
| 3296 |
// handoff indicates that a server should stop after receiving the |
| 3297 |
// ClientHello and pause the handshake in such a way that |SSL_get_error| |
| 3298 |
// returns |SSL_ERROR_HANDOFF|. This is copied in |SSL_new| from the |SSL_CTX| |
| 3299 |
// element of the same name and may be cleared if the handoff is declined. |
| 3300 |
bool handoff : 1; |
| 3301 |
|
| 3302 |
// shed_handshake_config indicates that the handshake config (this object!) |
| 3303 |
// should be freed after the handshake completes. |
| 3304 |
bool shed_handshake_config : 1; |
| 3305 |
|
| 3306 |
// jdk11_workaround is whether to disable TLS 1.3 for JDK 11 clients, as a |
| 3307 |
// workaround for https://bugs.openjdk.java.net/browse/JDK-8211806. |
| 3308 |
bool jdk11_workaround : 1; |
| 3309 |
|
| 3310 |
// QUIC drafts up to and including 32 used a different TLS extension |
| 3311 |
// codepoint to convey QUIC's transport parameters. |
| 3312 |
bool quic_use_legacy_codepoint : 1; |
| 3313 |
|
| 3314 |
// permute_extensions is whether to permute extensions when sending messages. |
| 3315 |
bool permute_extensions : 1; |
| 3316 |
|
| 3317 |
// aes_hw_override if set indicates we should override checking for aes |
| 3318 |
// hardware support, and use the value in aes_hw_override_value instead. |
| 3319 |
bool aes_hw_override : 1; |
| 3320 |
|
| 3321 |
// aes_hw_override_value is used for testing to indicate the support or lack |
| 3322 |
// of support for AES hw. The value is only considered if |aes_hw_override| is |
| 3323 |
// true. |
| 3324 |
bool aes_hw_override_value : 1; |
| 3325 |
|
| 3326 |
// conf_max_version_use_default indicates whether the |SSL_CONFIG| is configured |
| 3327 |
// to use the default maximum protocol version for the relevant protocol |
| 3328 |
// method. By default, |SSL_new| will set this to true and connections will use |
| 3329 |
// the default max version. callers can change the max version used by calling |
| 3330 |
// |SSL_set_max_proto_version| with a non-zero value. |
| 3331 |
bool conf_max_version_use_default : 1; |
| 3332 |
|
| 3333 |
// conf_min_version_use_default indicates whether the |SSL_CONFIG| is configured |
| 3334 |
// to use the default minimum protocol version for the relevant protocol |
| 3335 |
// method. By default, |SSL_new| will set this to true and connections will use |
| 3336 |
// the default min version. callers can change the min version used by calling |
| 3337 |
// |SSL_set_min_proto_version| with a non-zero value. |
| 3338 |
bool conf_min_version_use_default : 1; |
| 3339 |
|
| 3340 |
// alps_use_new_codepoint if set indicates we use new ALPS extension codepoint |
| 3341 |
// to negotiate and convey application settings. |
| 3342 |
bool alps_use_new_codepoint : 1; |
| 3343 |
|
| 3344 |
// check_client_certificate_type indicates whether the client, in TLS 1.2 and |
| 3345 |
// below, will check its certificate against the server's requested |
| 3346 |
// certificate types. |
| 3347 |
bool check_client_certificate_type : 1; |
| 3348 |
}; |
| 3349 |
|
| 3350 |
// From RFC 8446, used in determining PSK modes. |
| 3351 |
#define SSL_PSK_DHE_KE 0x1 |
| 3352 |
|
| 3353 |
// kMaxEarlyDataAccepted is the advertised number of plaintext bytes of early |
| 3354 |
// data that will be accepted. This value should be slightly below |
| 3355 |
// kMaxEarlyDataSkipped in tls_record.c, which is measured in ciphertext. |
| 3356 |
static const size_t kMaxEarlyDataAccepted = 14336; |
| 3357 |
|
| 3358 |
UniquePtr<CERT> ssl_cert_dup(CERT *cert); |
| 3359 |
bool ssl_set_cert(CERT *cert, UniquePtr<CRYPTO_BUFFER> buffer); |
| 3360 |
bool ssl_is_key_type_supported(int key_type); |
| 3361 |
// ssl_compare_public_and_private_key returns true if |pubkey| is the public |
| 3362 |
// counterpart to |privkey|. Otherwise it returns false and pushes a helpful |
| 3363 |
// message on the error queue. |
| 3364 |
bool ssl_compare_public_and_private_key(const EVP_PKEY *pubkey, |
| 3365 |
const EVP_PKEY *privkey); |
| 3366 |
bool ssl_cert_check_private_key(const CERT *cert, const EVP_PKEY *privkey); |
| 3367 |
|
| 3368 |
CRYPTO_BUFFER *buffer_up_ref(const CRYPTO_BUFFER *buffer); |
| 3369 |
|
| 3370 |
// ssl_cert_check_cert_private_keys_usage returns true if |cert_private_keys| |
| 3371 |
// in |cert| has a valid index and a sufficient amount of slots. |
| 3372 |
bool ssl_cert_check_cert_private_keys_usage(const CERT *cert); |
| 3373 |
|
| 3374 |
bool ssl_get_new_session(SSL_HANDSHAKE *hs); |
| 3375 |
bool ssl_encrypt_ticket(SSL_HANDSHAKE *hs, CBB *out, |
| 3376 |
const SSL_SESSION *session); |
| 3377 |
bool ssl_ctx_rotate_ticket_encryption_key(SSL_CTX *ctx); |
| 3378 |
|
| 3379 |
// ssl_session_new returns a newly-allocated blank |SSL_SESSION| or nullptr on |
| 3380 |
// error. |
| 3381 |
UniquePtr<SSL_SESSION> ssl_session_new(const SSL_X509_METHOD *x509_method); |
| 3382 |
|
| 3383 |
// ssl_hash_session_id returns a hash of |session_id|, suitable for a hash table |
| 3384 |
// keyed on session IDs. |
| 3385 |
uint32_t ssl_hash_session_id(Span<const uint8_t> session_id); |
| 3386 |
|
| 3387 |
// SSL_SESSION_parse parses an |SSL_SESSION| from |cbs| and advances |cbs| over |
| 3388 |
// the parsed data. |
| 3389 |
OPENSSL_EXPORT UniquePtr<SSL_SESSION> SSL_SESSION_parse( |
| 3390 |
CBS *cbs, const SSL_X509_METHOD *x509_method, CRYPTO_BUFFER_POOL *pool); |
| 3391 |
|
| 3392 |
// ssl_session_serialize writes |in| to |cbb| as if it were serialising a |
| 3393 |
// session for Session-ID resumption. It returns true on success and false on |
| 3394 |
// error. |
| 3395 |
OPENSSL_EXPORT bool ssl_session_serialize(const SSL_SESSION *in, CBB *cbb); |
| 3396 |
|
| 3397 |
// ssl_session_is_context_valid returns whether |session|'s session ID context |
| 3398 |
// matches the one set on |hs|. |
| 3399 |
bool ssl_session_is_context_valid(const SSL_HANDSHAKE *hs, |
| 3400 |
const SSL_SESSION *session); |
| 3401 |
|
| 3402 |
// ssl_session_is_time_valid returns true if |session| is still valid and false |
| 3403 |
// if it has expired. |
| 3404 |
bool ssl_session_is_time_valid(const SSL *ssl, const SSL_SESSION *session); |
| 3405 |
|
| 3406 |
// ssl_session_is_resumable returns whether |session| is resumable for |hs|. |
| 3407 |
bool ssl_session_is_resumable(const SSL_HANDSHAKE *hs, |
| 3408 |
const SSL_SESSION *session); |
| 3409 |
|
| 3410 |
// ssl_session_protocol_version returns the protocol version associated with |
| 3411 |
// |session|. Note that despite the name, this is not the same as |
| 3412 |
// |SSL_SESSION_get_protocol_version|. The latter is based on upstream's name. |
| 3413 |
uint16_t ssl_session_protocol_version(const SSL_SESSION *session); |
| 3414 |
|
| 3415 |
// ssl_session_get_digest returns the digest used in |session|. |
| 3416 |
const EVP_MD *ssl_session_get_digest(const SSL_SESSION *session); |
| 3417 |
|
| 3418 |
void ssl_set_session(SSL *ssl, SSL_SESSION *session); |
| 3419 |
|
| 3420 |
// ssl_get_prev_session looks up the previous session based on |client_hello|. |
| 3421 |
// On success, it sets |*out_session| to the session or nullptr if none was |
| 3422 |
// found. If the session could not be looked up synchronously, it returns |
| 3423 |
// |ssl_hs_pending_session| and should be called again. If a ticket could not be |
| 3424 |
// decrypted immediately it returns |ssl_hs_pending_ticket| and should also |
| 3425 |
// be called again. Otherwise, it returns |ssl_hs_error|. |
| 3426 |
enum ssl_hs_wait_t ssl_get_prev_session(SSL_HANDSHAKE *hs, |
| 3427 |
UniquePtr<SSL_SESSION> *out_session, |
| 3428 |
bool *out_tickets_supported, |
| 3429 |
bool *out_renew_ticket, |
| 3430 |
const SSL_CLIENT_HELLO *client_hello); |
| 3431 |
|
| 3432 |
// The following flags determine which parts of the session are duplicated. |
| 3433 |
#define SSL_SESSION_DUP_AUTH_ONLY 0x0 |
| 3434 |
#define SSL_SESSION_INCLUDE_TICKET 0x1 |
| 3435 |
#define SSL_SESSION_INCLUDE_NONAUTH 0x2 |
| 3436 |
#define SSL_SESSION_DUP_ALL \ |
| 3437 |
(SSL_SESSION_INCLUDE_TICKET | SSL_SESSION_INCLUDE_NONAUTH) |
| 3438 |
|
| 3439 |
// SSL_SESSION_dup returns a newly-allocated |SSL_SESSION| with a copy of the |
| 3440 |
// fields in |session| or nullptr on error. The new session is non-resumable and |
| 3441 |
// must be explicitly marked resumable once it has been filled in. |
| 3442 |
OPENSSL_EXPORT UniquePtr<SSL_SESSION> SSL_SESSION_dup(SSL_SESSION *session, |
| 3443 |
int dup_flags); |
| 3444 |
|
| 3445 |
// ssl_session_rebase_time updates |session|'s start time to the current time, |
| 3446 |
// adjusting the timeout so the expiration time is unchanged. |
| 3447 |
void ssl_session_rebase_time(SSL *ssl, SSL_SESSION *session); |
| 3448 |
|
| 3449 |
// ssl_session_renew_timeout calls |ssl_session_rebase_time| and renews |
| 3450 |
// |session|'s timeout to |timeout| (measured from the current time). The |
| 3451 |
// renewal is clamped to the session's auth_timeout. |
| 3452 |
void ssl_session_renew_timeout(SSL *ssl, SSL_SESSION *session, |
| 3453 |
uint32_t timeout); |
| 3454 |
|
| 3455 |
void ssl_update_cache(SSL *ssl); |
| 3456 |
|
| 3457 |
void ssl_send_alert(SSL *ssl, int level, int desc); |
| 3458 |
int ssl_send_alert_impl(SSL *ssl, int level, int desc); |
| 3459 |
bool tls_get_message(const SSL *ssl, SSLMessage *out); |
| 3460 |
ssl_open_record_t tls_open_handshake(SSL *ssl, size_t *out_consumed, |
| 3461 |
uint8_t *out_alert, Span<uint8_t> in); |
| 3462 |
void tls_next_message(SSL *ssl); |
| 3463 |
|
| 3464 |
int tls_dispatch_alert(SSL *ssl); |
| 3465 |
ssl_open_record_t tls_open_app_data(SSL *ssl, Span<uint8_t> *out, |
| 3466 |
size_t *out_consumed, uint8_t *out_alert, |
| 3467 |
Span<uint8_t> in); |
| 3468 |
ssl_open_record_t tls_open_change_cipher_spec(SSL *ssl, size_t *out_consumed, |
| 3469 |
uint8_t *out_alert, |
| 3470 |
Span<uint8_t> in); |
| 3471 |
int tls_write_app_data(SSL *ssl, bool *out_needs_handshake, |
| 3472 |
size_t *out_bytes_written, Span<const uint8_t> in); |
| 3473 |
|
| 3474 |
bool tls_new(SSL *ssl); |
| 3475 |
void tls_free(SSL *ssl); |
| 3476 |
|
| 3477 |
bool tls_init_message(const SSL *ssl, CBB *cbb, CBB *body, uint8_t type); |
| 3478 |
bool tls_finish_message(const SSL *ssl, CBB *cbb, Array<uint8_t> *out_msg); |
| 3479 |
bool tls_add_message(SSL *ssl, Array<uint8_t> msg); |
| 3480 |
bool tls_add_change_cipher_spec(SSL *ssl); |
| 3481 |
int tls_flush_flight(SSL *ssl); |
| 3482 |
|
| 3483 |
bool dtls1_init_message(const SSL *ssl, CBB *cbb, CBB *body, uint8_t type); |
| 3484 |
bool dtls1_finish_message(const SSL *ssl, CBB *cbb, Array<uint8_t> *out_msg); |
| 3485 |
bool dtls1_add_message(SSL *ssl, Array<uint8_t> msg); |
| 3486 |
bool dtls1_add_change_cipher_spec(SSL *ssl); |
| 3487 |
int dtls1_flush_flight(SSL *ssl); |
| 3488 |
|
| 3489 |
// ssl_add_message_cbb finishes the handshake message in |cbb| and adds it to |
| 3490 |
// the pending flight. It returns true on success and false on error. |
| 3491 |
bool ssl_add_message_cbb(SSL *ssl, CBB *cbb); |
| 3492 |
|
| 3493 |
// ssl_hash_message incorporates |msg| into the handshake hash. It returns true |
| 3494 |
// on success and false on allocation failure. |
| 3495 |
bool ssl_hash_message(SSL_HANDSHAKE *hs, const SSLMessage &msg); |
| 3496 |
|
| 3497 |
ssl_open_record_t dtls1_open_app_data(SSL *ssl, Span<uint8_t> *out, |
| 3498 |
size_t *out_consumed, uint8_t *out_alert, |
| 3499 |
Span<uint8_t> in); |
| 3500 |
ssl_open_record_t dtls1_open_change_cipher_spec(SSL *ssl, size_t *out_consumed, |
| 3501 |
uint8_t *out_alert, |
| 3502 |
Span<uint8_t> in); |
| 3503 |
|
| 3504 |
int dtls1_write_app_data(SSL *ssl, bool *out_needs_handshake, |
| 3505 |
size_t *out_bytes_written, Span<const uint8_t> in); |
| 3506 |
|
| 3507 |
// dtls1_write_record sends a record. It returns one on success and <= 0 on |
| 3508 |
// error. |
| 3509 |
int dtls1_write_record(SSL *ssl, int type, Span<const uint8_t> in, |
| 3510 |
enum dtls1_use_epoch_t use_epoch); |
| 3511 |
|
| 3512 |
int dtls1_retransmit_outgoing_messages(SSL *ssl); |
| 3513 |
bool dtls1_parse_fragment(CBS *cbs, struct hm_header_st *out_hdr, |
| 3514 |
CBS *out_body); |
| 3515 |
bool dtls1_check_timeout_num(SSL *ssl); |
| 3516 |
|
| 3517 |
void dtls1_start_timer(SSL *ssl); |
| 3518 |
void dtls1_stop_timer(SSL *ssl); |
| 3519 |
bool dtls1_is_timer_expired(SSL *ssl); |
| 3520 |
unsigned int dtls1_min_mtu(void); |
| 3521 |
|
| 3522 |
bool dtls1_new(SSL *ssl); |
| 3523 |
void dtls1_free(SSL *ssl); |
| 3524 |
|
| 3525 |
bool dtls1_get_message(const SSL *ssl, SSLMessage *out); |
| 3526 |
ssl_open_record_t dtls1_open_handshake(SSL *ssl, size_t *out_consumed, |
| 3527 |
uint8_t *out_alert, Span<uint8_t> in); |
| 3528 |
void dtls1_next_message(SSL *ssl); |
| 3529 |
int dtls1_dispatch_alert(SSL *ssl); |
| 3530 |
|
| 3531 |
// tls1_configure_aead configures either the read or write direction AEAD (as |
| 3532 |
// determined by |direction|) using the keys generated by the TLS KDF. The |
| 3533 |
// |key_block_cache| argument is used to store the generated key block, if |
| 3534 |
// empty. Otherwise it's assumed that the key block is already contained within |
| 3535 |
// it. It returns true on success or false on error. |
| 3536 |
bool tls1_configure_aead(SSL *ssl, evp_aead_direction_t direction, |
| 3537 |
Array<uint8_t> *key_block_cache, |
| 3538 |
const SSL_SESSION *session, |
| 3539 |
Span<const uint8_t> iv_override); |
| 3540 |
|
| 3541 |
bool tls1_change_cipher_state(SSL_HANDSHAKE *hs, |
| 3542 |
evp_aead_direction_t direction); |
| 3543 |
int tls1_generate_master_secret(SSL_HANDSHAKE *hs, uint8_t *out, |
| 3544 |
Span<const uint8_t> premaster); |
| 3545 |
|
| 3546 |
// tls1_get_default_grouplist returns the default group list |
| 3547 |
OPENSSL_EXPORT Span<const uint16_t> tls1_get_default_grouplist(void); |
| 3548 |
|
| 3549 |
// tls1_get_grouplist returns the locally-configured group preference list. |
| 3550 |
Span<const uint16_t> tls1_get_grouplist(const SSL_HANDSHAKE *ssl); |
| 3551 |
|
| 3552 |
// tls1_check_group_id returns whether |group_id| is consistent with locally- |
| 3553 |
// configured group preferences. |
| 3554 |
bool tls1_check_group_id(const SSL_HANDSHAKE *ssl, uint16_t group_id); |
| 3555 |
|
| 3556 |
// tls1_get_shared_group sets |*out_group_id| to the first preferred shared |
| 3557 |
// group between client and server preferences and returns true. If none may be |
| 3558 |
// found, it returns false. |
| 3559 |
bool tls1_get_shared_group(SSL_HANDSHAKE *hs, uint16_t *out_group_id); |
| 3560 |
|
| 3561 |
// tls1_set_curves converts the array of NIDs in |curves| into a newly allocated |
| 3562 |
// array of TLS group IDs. On success, the function returns true and writes the |
| 3563 |
// array to |*out_group_ids|. Otherwise, it returns false. |
| 3564 |
bool tls1_set_curves(Array<uint16_t> *out_group_ids, Span<const int> curves); |
| 3565 |
|
| 3566 |
// tls1_set_curves_list converts the string of curves pointed to by |curves| |
| 3567 |
// into a newly allocated array of TLS group IDs. On success, the function |
| 3568 |
// returns true and writes the array to |*out_group_ids|. Otherwise, it returns |
| 3569 |
// false. |
| 3570 |
bool tls1_set_curves_list(Array<uint16_t> *out_group_ids, const char *curves); |
| 3571 |
|
| 3572 |
// tls1_call_ocsp_stapling_callback calls the legacy OCSP logic for TLS |
| 3573 |
// handshakes. This should be called right after the server certificate has been |
| 3574 |
// finalized. |
| 3575 |
bool tls1_call_ocsp_stapling_callback(SSL_HANDSHAKE *hs); |
| 3576 |
|
| 3577 |
// ssl_add_clienthello_tlsext writes ClientHello extensions to |out| for |type|. |
| 3578 |
// It returns true on success and false on failure. The |header_len| argument is |
| 3579 |
// the length of the ClientHello written so far and is used to compute the |
| 3580 |
// padding length. (It does not include the record header or handshake headers.) |
| 3581 |
// |
| 3582 |
// If |type| is |ssl_client_hello_inner|, this function also writes the |
| 3583 |
// compressed extensions to |out_encoded|. Otherwise, |out_encoded| should be |
| 3584 |
// nullptr. |
| 3585 |
// |
| 3586 |
// On success, the function sets |*out_needs_psk_binder| to whether the last |
| 3587 |
// ClientHello extension was the pre_shared_key extension and needs a PSK binder |
| 3588 |
// filled in. The caller should then update |out| and, if applicable, |
| 3589 |
// |out_encoded| with the binder after completing the whole message. |
| 3590 |
bool ssl_add_clienthello_tlsext(SSL_HANDSHAKE *hs, CBB *out, CBB *out_encoded, |
| 3591 |
bool *out_needs_psk_binder, |
| 3592 |
ssl_client_hello_type_t type, |
| 3593 |
size_t header_len); |
| 3594 |
|
| 3595 |
bool ssl_add_serverhello_tlsext(SSL_HANDSHAKE *hs, CBB *out); |
| 3596 |
bool ssl_parse_clienthello_tlsext(SSL_HANDSHAKE *hs, |
| 3597 |
const SSL_CLIENT_HELLO *client_hello); |
| 3598 |
bool ssl_parse_serverhello_tlsext(SSL_HANDSHAKE *hs, const CBS *extensions); |
| 3599 |
|
| 3600 |
|
| 3601 |
// SSL serialization. |
| 3602 |
|
| 3603 |
// ssl_transfer_supported returns true when the |in| is supported by |
| 3604 |
// current SSL transfer scope. Specific support scope is included inside |
| 3605 |
// the function. |
| 3606 |
// Otherwise, it returns false. |
| 3607 |
OPENSSL_EXPORT bool ssl_transfer_supported(const SSL *in); |
| 3608 |
|
| 3609 |
#define tlsext_tick_md EVP_sha256 |
| 3610 |
|
| 3611 |
// ssl_process_ticket processes a session ticket from the client. It returns |
| 3612 |
// one of: |
| 3613 |
// |ssl_ticket_aead_success|: |*out_session| is set to the parsed session and |
| 3614 |
// |*out_renew_ticket| is set to whether the ticket should be renewed. |
| 3615 |
// |ssl_ticket_aead_ignore_ticket|: |*out_renew_ticket| is set to whether a |
| 3616 |
// fresh ticket should be sent, but the given ticket cannot be used. |
| 3617 |
// |ssl_ticket_aead_retry|: the ticket could not be immediately decrypted. |
| 3618 |
// Retry later. |
| 3619 |
// |ssl_ticket_aead_error|: an error occured that is fatal to the connection. |
| 3620 |
enum ssl_ticket_aead_result_t ssl_process_ticket( |
| 3621 |
SSL_HANDSHAKE *hs, UniquePtr<SSL_SESSION> *out_session, |
| 3622 |
bool *out_renew_ticket, Span<const uint8_t> ticket, |
| 3623 |
Span<const uint8_t> session_id); |
| 3624 |
|
| 3625 |
// tls1_verify_channel_id processes |msg| as a Channel ID message, and verifies |
| 3626 |
// the signature. If the key is valid, it saves the Channel ID and returns true. |
| 3627 |
// Otherwise, it returns false. |
| 3628 |
bool tls1_verify_channel_id(SSL_HANDSHAKE *hs, const SSLMessage &msg); |
| 3629 |
|
| 3630 |
// tls1_write_channel_id generates a Channel ID message and puts the output in |
| 3631 |
// |cbb|. |ssl->channel_id_private| must already be set before calling. This |
| 3632 |
// function returns true on success and false on error. |
| 3633 |
bool tls1_write_channel_id(SSL_HANDSHAKE *hs, CBB *cbb); |
| 3634 |
|
| 3635 |
// tls1_channel_id_hash computes the hash to be signed by Channel ID and writes |
| 3636 |
// it to |out|, which must contain at least |EVP_MAX_MD_SIZE| bytes. It returns |
| 3637 |
// true on success and false on failure. |
| 3638 |
bool tls1_channel_id_hash(SSL_HANDSHAKE *hs, uint8_t *out, size_t *out_len); |
| 3639 |
|
| 3640 |
// tls1_record_handshake_hashes_for_channel_id records the current handshake |
| 3641 |
// hashes in |hs->new_session| so that Channel ID resumptions can sign that |
| 3642 |
// data. |
| 3643 |
bool tls1_record_handshake_hashes_for_channel_id(SSL_HANDSHAKE *hs); |
| 3644 |
|
| 3645 |
// ssl_can_write returns whether |ssl| is allowed to write. |
| 3646 |
bool ssl_can_write(const SSL *ssl); |
| 3647 |
|
| 3648 |
// ssl_can_read returns wheter |ssl| is allowed to read. |
| 3649 |
bool ssl_can_read(const SSL *ssl); |
| 3650 |
|
| 3651 |
void ssl_get_current_time(const SSL *ssl, struct OPENSSL_timeval *out_clock); |
| 3652 |
void ssl_ctx_get_current_time(const SSL_CTX *ctx, |
| 3653 |
struct OPENSSL_timeval *out_clock); |
| 3654 |
|
| 3655 |
// ssl_reset_error_state resets state for |SSL_get_error|. |
| 3656 |
void ssl_reset_error_state(SSL *ssl); |
| 3657 |
|
| 3658 |
// ssl_set_read_error sets |ssl|'s read half into an error state, saving the |
| 3659 |
// current state of the error queue. |
| 3660 |
void ssl_set_read_error(SSL *ssl); |
| 3661 |
|
| 3662 |
// ssl_update_counter updates the stat counters in |SSL_CTX|. lock should be |
| 3663 |
// set to false when the mutex in |SSL_CTX| has already been locked. |
| 3664 |
void ssl_update_counter(SSL_CTX *ctx, SSL_STATS_COUNTER_TYPE &counter, bool lock); |
| 3665 |
|
| 3666 |
BSSL_NAMESPACE_END |
| 3667 |
|
| 3668 |
// ssl_x509_persist_peer_ca_names eagerly converts the peer CA names from |
| 3669 |
// |hs->ca_names| to X509_NAMEs and stores them in |
| 3670 |
// |ssl->s3->cached_x509_peer_ca_names|, so they remain available via |
| 3671 |
// |SSL_get_client_CA_list| after the handshake object is destroyed. It returns |
| 3672 |
// true on success and false on allocation failure. |
| 3673 |
bool ssl_x509_persist_peer_ca_names(SSL *ssl); |
| 3674 |
|
| 3675 |
|
| 3676 |
// Opaque C types. |
| 3677 |
// |
| 3678 |
// The following types are exported to C code as public typedefs, so they must |
| 3679 |
// be defined outside of the namespace. |
| 3680 |
|
| 3681 |
// ssl_method_st backs the public |SSL_METHOD| type. It is a compatibility |
| 3682 |
// structure to support the legacy version-locked methods. |
| 3683 |
struct ssl_method_st { |
| 3684 |
// version, if non-zero, is the only protocol version acceptable to an |
| 3685 |
// SSL_CTX initialized from this method. |
| 3686 |
uint16_t version; |
| 3687 |
// method is the underlying SSL_PROTOCOL_METHOD that initializes the |
| 3688 |
// SSL_CTX. |
| 3689 |
const bssl::SSL_PROTOCOL_METHOD *method; |
| 3690 |
// x509_method contains pointers to functions that might deal with |X509| |
| 3691 |
// compatibility, or might be a no-op, depending on the application. |
| 3692 |
const bssl::SSL_X509_METHOD *x509_method; |
| 3693 |
}; |
| 3694 |
|
| 3695 |
// TLS13_DEFAULT_CIPHER_LIST_AES_HW is the default TLS 1.3 cipher suite |
| 3696 |
// configuration when AES hardware acceleration is enabled. |
| 3697 |
#define TLS13_DEFAULT_CIPHER_LIST_AES_HW "TLS_AES_128_GCM_SHA256:" \ |
| 3698 |
"TLS_AES_256_GCM_SHA384:" \ |
| 3699 |
"TLS_CHACHA20_POLY1305_SHA256" |
| 3700 |
|
| 3701 |
// TLS13_DEFAULT_CIPHER_LIST_NO_AES_HW is the default TLS 1.3 cipher suite |
| 3702 |
// configuration when no AES hardware acceleration is enabled. |
| 3703 |
#define TLS13_DEFAULT_CIPHER_LIST_NO_AES_HW "TLS_CHACHA20_POLY1305_SHA256:" \ |
| 3704 |
"TLS_AES_128_GCM_SHA256:" \ |
| 3705 |
"TLS_AES_256_GCM_SHA384" |
| 3706 |
|
| 3707 |
#define MIN_SAFE_FRAGMENT_SIZE 512 |
| 3708 |
struct ssl_ctx_st : public bssl::RefCounted<ssl_ctx_st> { |
| 3709 |
explicit ssl_ctx_st(const SSL_METHOD *ssl_method); |
| 3710 |
ssl_ctx_st(const ssl_ctx_st &) = delete; |
| 3711 |
ssl_ctx_st &operator=(const ssl_ctx_st &) = delete; |
| 3712 |
|
| 3713 |
const bssl::SSL_PROTOCOL_METHOD *method = nullptr; |
| 3714 |
const bssl::SSL_X509_METHOD *x509_method = nullptr; |
| 3715 |
|
| 3716 |
// lock is used to protect various operations on this object. |
| 3717 |
CRYPTO_MUTEX lock; |
| 3718 |
|
| 3719 |
// conf_max_version is the maximum acceptable protocol version configured by |
| 3720 |
// |SSL_CTX_set_max_proto_version|. Note this version is normalized in DTLS |
| 3721 |
// and is further constrainted by |SSL_OP_NO_*|. |
| 3722 |
uint16_t conf_max_version = 0; |
| 3723 |
|
| 3724 |
// conf_min_version is the minimum acceptable protocol version configured by |
| 3725 |
// |SSL_CTX_set_min_proto_version|. Note this version is normalized in DTLS |
| 3726 |
// and is further constrainted by |SSL_OP_NO_*|. |
| 3727 |
uint16_t conf_min_version = 0; |
| 3728 |
|
| 3729 |
// num_tickets is the number of tickets to send immediately after the TLS 1.3 |
| 3730 |
// handshake. TLS 1.3 recommends single-use tickets so, by default, issue two |
| 3731 |
/// in case the client makes several connections before getting a renewal. |
| 3732 |
uint8_t num_tickets = 2; |
| 3733 |
|
| 3734 |
// read_ahead_buffer_size is the amount of data to read if |enable_read_ahead| is true |
| 3735 |
size_t read_ahead_buffer_size = SSL3_RT_MAX_PLAIN_LENGTH + SSL3_RT_MAX_ENCRYPTED_OVERHEAD; |
| 3736 |
|
| 3737 |
// quic_method is the method table corresponding to the QUIC hooks. |
| 3738 |
const SSL_QUIC_METHOD *quic_method = nullptr; |
| 3739 |
|
| 3740 |
// cipher_list holds all available cipher suites for tls 1.3, |
| 3741 |
// and 1.2 and below |
| 3742 |
bssl::UniquePtr<bssl::SSLCipherPreferenceList> cipher_list; |
| 3743 |
|
| 3744 |
// tls13_cipher_list holds the default or configured tls1.3 and above |
| 3745 |
// cipher suites. |
| 3746 |
bssl::UniquePtr<bssl::SSLCipherPreferenceList> tls13_cipher_list; |
| 3747 |
|
| 3748 |
X509_STORE *cert_store = nullptr; |
| 3749 |
LHASH_OF(SSL_SESSION) *sessions = nullptr; |
| 3750 |
// Most session-ids that will be cached, default is |
| 3751 |
// SSL_SESSION_CACHE_MAX_SIZE_DEFAULT. 0 is unlimited. |
| 3752 |
unsigned long session_cache_size = SSL_SESSION_CACHE_MAX_SIZE_DEFAULT; |
| 3753 |
SSL_SESSION *session_cache_head = nullptr; |
| 3754 |
SSL_SESSION *session_cache_tail = nullptr; |
| 3755 |
|
| 3756 |
// handshakes_since_cache_flush is the number of successful handshakes since |
| 3757 |
// the last cache flush. |
| 3758 |
int handshakes_since_cache_flush = 0; |
| 3759 |
|
| 3760 |
// This can have one of 2 values, ored together, |
| 3761 |
// SSL_SESS_CACHE_CLIENT, |
| 3762 |
// SSL_SESS_CACHE_SERVER, |
| 3763 |
// Default is SSL_SESSION_CACHE_SERVER, which means only |
| 3764 |
// SSL_accept which cache SSL_SESSIONS. |
| 3765 |
int session_cache_mode = SSL_SESS_CACHE_SERVER; |
| 3766 |
|
| 3767 |
// session_timeout is the default lifetime for new sessions in TLS 1.2 and |
| 3768 |
// earlier, in seconds. |
| 3769 |
uint32_t session_timeout = SSL_DEFAULT_SESSION_TIMEOUT; |
| 3770 |
|
| 3771 |
// session_psk_dhe_timeout is the default lifetime for new sessions in TLS |
| 3772 |
// 1.3, in seconds. |
| 3773 |
uint32_t session_psk_dhe_timeout = SSL_DEFAULT_SESSION_PSK_DHE_TIMEOUT; |
| 3774 |
|
| 3775 |
// If this callback is not null, it will be called each time a session id is |
| 3776 |
// added to the cache. If this function returns 1, it means that the |
| 3777 |
// callback will do a SSL_SESSION_free() when it has finished using it. |
| 3778 |
// Otherwise, on 0, it means the callback has finished with it. If |
| 3779 |
// remove_session_cb is not null, it will be called when a session-id is |
| 3780 |
// removed from the cache. After the call, OpenSSL will SSL_SESSION_free() |
| 3781 |
// it. |
| 3782 |
int (*new_session_cb)(SSL *ssl, SSL_SESSION *sess) = nullptr; |
| 3783 |
void (*remove_session_cb)(SSL_CTX *ctx, SSL_SESSION *sess) = nullptr; |
| 3784 |
SSL_SESSION *(*get_session_cb)(SSL *ssl, const uint8_t *data, int len, |
| 3785 |
int *copy) = nullptr; |
| 3786 |
|
| 3787 |
struct { |
| 3788 |
SSL_STATS_COUNTER_TYPE sess_connect{}; // SSL new conn - started |
| 3789 |
SSL_STATS_COUNTER_TYPE sess_connect_renegotiate{}; // SSL reneg - requested |
| 3790 |
SSL_STATS_COUNTER_TYPE sess_connect_good{}; // SSL new conne/reneg - finished |
| 3791 |
SSL_STATS_COUNTER_TYPE sess_accept{}; // SSL new accept - started |
| 3792 |
SSL_STATS_COUNTER_TYPE sess_accept_good{}; // SSL accept/reneg - finished |
| 3793 |
SSL_STATS_COUNTER_TYPE sess_miss{}; // session lookup misses |
| 3794 |
SSL_STATS_COUNTER_TYPE sess_timeout{}; // reuse attempt on timeouted session |
| 3795 |
SSL_STATS_COUNTER_TYPE sess_cache_full{}; // session removed due to full cache |
| 3796 |
SSL_STATS_COUNTER_TYPE sess_hit{}; // session reuse actually done |
| 3797 |
SSL_STATS_COUNTER_TYPE sess_cb_hit{}; // session-id that was not |
| 3798 |
// in the cache was |
| 3799 |
// passed back via the callback. This |
| 3800 |
// indicates that the application is |
| 3801 |
// supplying session-id's from other |
| 3802 |
// processes - spooky :-) |
| 3803 |
} stats; |
| 3804 |
|
| 3805 |
// if defined, these override the X509_verify_cert() calls |
| 3806 |
int (*app_verify_callback)(X509_STORE_CTX *store_ctx, void *arg) = nullptr; |
| 3807 |
void *app_verify_arg = nullptr; |
| 3808 |
|
| 3809 |
ssl_verify_result_t (*custom_verify_callback)(SSL *ssl, |
| 3810 |
uint8_t *out_alert) = nullptr; |
| 3811 |
|
| 3812 |
// Default password callback. |
| 3813 |
pem_password_cb *default_passwd_callback = nullptr; |
| 3814 |
|
| 3815 |
// Default password callback user data. |
| 3816 |
void *default_passwd_callback_userdata = nullptr; |
| 3817 |
|
| 3818 |
// get client cert callback |
| 3819 |
int (*client_cert_cb)(SSL *ssl, X509 **out_x509, |
| 3820 |
EVP_PKEY **out_pkey) = nullptr; |
| 3821 |
|
| 3822 |
CRYPTO_EX_DATA ex_data; |
| 3823 |
|
| 3824 |
// custom_*_extensions stores any callback sets for custom extensions. Note |
| 3825 |
// that these pointers will be NULL if the stack would otherwise be empty. |
| 3826 |
STACK_OF(SSL_CUSTOM_EXTENSION) *client_custom_extensions = nullptr; |
| 3827 |
STACK_OF(SSL_CUSTOM_EXTENSION) *server_custom_extensions = nullptr; |
| 3828 |
|
| 3829 |
// Default values used when no per-SSL value is defined follow |
| 3830 |
|
| 3831 |
void (*info_callback)(const SSL *ssl, int type, int value) = nullptr; |
| 3832 |
|
| 3833 |
// what we put in client cert requests |
| 3834 |
bssl::UniquePtr<STACK_OF(CRYPTO_BUFFER)> client_CA; |
| 3835 |
|
| 3836 |
// cached_x509_client_CA is a cache of parsed versions of the elements of |
| 3837 |
// |client_CA|. |
| 3838 |
STACK_OF(X509_NAME) *cached_x509_client_CA = nullptr; |
| 3839 |
|
| 3840 |
|
| 3841 |
// Default values to use in SSL structures follow (these are copied by |
| 3842 |
// SSL_new) |
| 3843 |
|
| 3844 |
uint32_t options = 0; |
| 3845 |
// Disable the auto-chaining feature by default. wpa_supplicant relies on this |
| 3846 |
// feature, but require callers opt into it. |
| 3847 |
uint32_t mode = SSL_MODE_NO_AUTO_CHAIN; |
| 3848 |
uint32_t max_cert_list = SSL_MAX_CERT_LIST_DEFAULT; |
| 3849 |
|
| 3850 |
bssl::UniquePtr<bssl::CERT> cert; |
| 3851 |
|
| 3852 |
// callback that allows applications to peek at protocol messages |
| 3853 |
void (*msg_callback)(int is_write, int version, int content_type, |
| 3854 |
const void *buf, size_t len, SSL *ssl, |
| 3855 |
void *arg) = nullptr; |
| 3856 |
void *msg_callback_arg = nullptr; |
| 3857 |
|
| 3858 |
int verify_mode = SSL_VERIFY_NONE; |
| 3859 |
int (*default_verify_callback)(int ok, X509_STORE_CTX *ctx) = |
| 3860 |
nullptr; // called 'verify_callback' in the SSL |
| 3861 |
|
| 3862 |
X509_VERIFY_PARAM *param = nullptr; |
| 3863 |
|
| 3864 |
// select_certificate_cb is called before most ClientHello processing and |
| 3865 |
// before the decision whether to resume a session is made. See |
| 3866 |
// |ssl_select_cert_result_t| for details of the return values. |
| 3867 |
ssl_select_cert_result_t (*select_certificate_cb)(const SSL_CLIENT_HELLO *) = |
| 3868 |
nullptr; |
| 3869 |
|
| 3870 |
// dos_protection_cb is called once the resumption decision for a ClientHello |
| 3871 |
// has been made. It returns one to continue the handshake or zero to |
| 3872 |
// abort. |
| 3873 |
int (*dos_protection_cb)(const SSL_CLIENT_HELLO *) = nullptr; |
| 3874 |
|
| 3875 |
// Controls whether to verify certificates when resuming connections. They |
| 3876 |
// were already verified when the connection was first made, so the default is |
| 3877 |
// false. For now, this is only respected on clients, not servers. |
| 3878 |
bool reverify_on_resume = false; |
| 3879 |
|
| 3880 |
// Maximum amount of data to send in one fragment. actual record size can be |
| 3881 |
// more than this due to padding and MAC overheads. |
| 3882 |
uint16_t max_send_fragment = SSL3_RT_MAX_PLAIN_LENGTH; |
| 3883 |
|
| 3884 |
/* ClientHello callback. Mostly for extensions, but not entirely. */ |
| 3885 |
SSL_client_hello_cb_fn client_hello_cb = NULL; |
| 3886 |
void *client_hello_cb_arg = NULL; |
| 3887 |
|
| 3888 |
// TLS extensions servername callback |
| 3889 |
int (*servername_callback)(SSL *, int *, void *) = nullptr; |
| 3890 |
void *servername_arg = nullptr; |
| 3891 |
|
| 3892 |
// RFC 4507 session ticket keys. |ticket_key_current| may be NULL before the |
| 3893 |
// first handshake and |ticket_key_prev| may be NULL at any time. |
| 3894 |
// Automatically generated ticket keys are rotated as needed at handshake |
| 3895 |
// time. Hence, all access must be synchronized through |lock|. |
| 3896 |
bssl::UniquePtr<bssl::TicketKey> ticket_key_current; |
| 3897 |
bssl::UniquePtr<bssl::TicketKey> ticket_key_prev; |
| 3898 |
|
| 3899 |
// Callback to support customisation of ticket key setting |
| 3900 |
int (*ticket_key_cb)(SSL *ssl, uint8_t *name, uint8_t *iv, |
| 3901 |
EVP_CIPHER_CTX *ectx, HMAC_CTX *hctx, int enc) = nullptr; |
| 3902 |
|
| 3903 |
// Server-only: psk_identity_hint is the default identity hint to send in |
| 3904 |
// PSK-based key exchanges. |
| 3905 |
bssl::UniquePtr<char> psk_identity_hint; |
| 3906 |
|
| 3907 |
unsigned (*psk_client_callback)(SSL *ssl, const char *hint, char *identity, |
| 3908 |
unsigned max_identity_len, uint8_t *psk, |
| 3909 |
unsigned max_psk_len) = nullptr; |
| 3910 |
unsigned (*psk_server_callback)(SSL *ssl, const char *identity, uint8_t *psk, |
| 3911 |
unsigned max_psk_len) = nullptr; |
| 3912 |
|
| 3913 |
|
| 3914 |
// Next protocol negotiation information |
| 3915 |
// (for experimental NPN extension). |
| 3916 |
|
| 3917 |
// For a server, this contains a callback function by which the set of |
| 3918 |
// advertised protocols can be provided. |
| 3919 |
int (*next_protos_advertised_cb)(SSL *ssl, const uint8_t **out, |
| 3920 |
unsigned *out_len, void *arg) = nullptr; |
| 3921 |
void *next_protos_advertised_cb_arg = nullptr; |
| 3922 |
// For a client, this contains a callback function that selects the |
| 3923 |
// next protocol from the list provided by the server. |
| 3924 |
int (*next_proto_select_cb)(SSL *ssl, uint8_t **out, uint8_t *out_len, |
| 3925 |
const uint8_t *in, unsigned in_len, |
| 3926 |
void *arg) = nullptr; |
| 3927 |
void *next_proto_select_cb_arg = nullptr; |
| 3928 |
|
| 3929 |
// ALPN information |
| 3930 |
// (we are in the process of transitioning from NPN to ALPN.) |
| 3931 |
|
| 3932 |
// For a server, this contains a callback function that allows the |
| 3933 |
// server to select the protocol for the connection. |
| 3934 |
// out: on successful return, this must point to the raw protocol |
| 3935 |
// name (without the length prefix). |
| 3936 |
// outlen: on successful return, this contains the length of |*out|. |
| 3937 |
// in: points to the client's list of supported protocols in |
| 3938 |
// wire-format. |
| 3939 |
// inlen: the length of |in|. |
| 3940 |
int (*alpn_select_cb)(SSL *ssl, const uint8_t **out, uint8_t *out_len, |
| 3941 |
const uint8_t *in, unsigned in_len, |
| 3942 |
void *arg) = nullptr; |
| 3943 |
void *alpn_select_cb_arg = nullptr; |
| 3944 |
|
| 3945 |
// For a client, this contains the list of supported protocols in wire |
| 3946 |
// format. |
| 3947 |
bssl::Array<uint8_t> alpn_client_proto_list; |
| 3948 |
|
| 3949 |
// SRTP profiles we are willing to do from RFC 5764 |
| 3950 |
bssl::UniquePtr<STACK_OF(SRTP_PROTECTION_PROFILE)> srtp_profiles; |
| 3951 |
|
| 3952 |
// Defined compression algorithms for certificates. |
| 3953 |
bssl::GrowableArray<bssl::CertCompressionAlg> cert_compression_algs; |
| 3954 |
|
| 3955 |
// Supported group values inherited by SSL structure |
| 3956 |
bssl::Array<uint16_t> supported_group_list; |
| 3957 |
|
| 3958 |
// channel_id_private is the client's Channel ID private key, or null if |
| 3959 |
// Channel ID should not be offered on this connection. |
| 3960 |
bssl::UniquePtr<EVP_PKEY> channel_id_private; |
| 3961 |
|
| 3962 |
// ech_keys contains the server's list of ECHConfig values and associated |
| 3963 |
// private keys. This list may be swapped out at any time, so all access must |
| 3964 |
// be synchronized through |lock|. |
| 3965 |
bssl::UniquePtr<SSL_ECH_KEYS> ech_keys; |
| 3966 |
|
| 3967 |
// keylog_callback, if not NULL, is the key logging callback. See |
| 3968 |
// |SSL_CTX_set_keylog_callback|. |
| 3969 |
void (*keylog_callback)(const SSL *ssl, const char *line) = nullptr; |
| 3970 |
|
| 3971 |
// current_time_cb, if not NULL, is the function to use to get the current |
| 3972 |
// time. It sets |*out_clock| to the current time. The |ssl| argument is |
| 3973 |
// always NULL. See |SSL_CTX_set_current_time_cb|. |
| 3974 |
void (*current_time_cb)(const SSL *ssl, struct timeval *out_clock) = nullptr; |
| 3975 |
|
| 3976 |
// pool is used for all |CRYPTO_BUFFER|s in case we wish to share certificate |
| 3977 |
// memory. |
| 3978 |
CRYPTO_BUFFER_POOL *pool = nullptr; |
| 3979 |
|
| 3980 |
// ticket_aead_method contains function pointers for opening and sealing |
| 3981 |
// session tickets. |
| 3982 |
const SSL_TICKET_AEAD_METHOD *ticket_aead_method = nullptr; |
| 3983 |
|
| 3984 |
// legacy_ocsp_callback implements an OCSP-related callback for OpenSSL |
| 3985 |
// compatibility. |
| 3986 |
int (*legacy_ocsp_callback)(SSL *ssl, void *arg) = nullptr; |
| 3987 |
void *legacy_ocsp_callback_arg = nullptr; |
| 3988 |
|
| 3989 |
// verify_sigalgs, if not empty, is the set of signature algorithms |
| 3990 |
// accepted from the peer in decreasing order of preference. |
| 3991 |
bssl::Array<uint16_t> verify_sigalgs; |
| 3992 |
|
| 3993 |
// retain_only_sha256_of_client_certs is true if we should compute the SHA256 |
| 3994 |
// hash of the peer's certificate and then discard it to save memory and |
| 3995 |
// session space. Only effective on the server side. |
| 3996 |
bool retain_only_sha256_of_client_certs : 1; |
| 3997 |
|
| 3998 |
// quiet_shutdown is true if the connection should not send a close_notify on |
| 3999 |
// shutdown. |
| 4000 |
bool quiet_shutdown : 1; |
| 4001 |
|
| 4002 |
// ocsp_stapling_enabled is only used by client connections and indicates |
| 4003 |
// whether OCSP stapling will be requested. |
| 4004 |
bool ocsp_stapling_enabled : 1; |
| 4005 |
|
| 4006 |
// If true, a client will request certificate timestamps. |
| 4007 |
bool signed_cert_timestamps_enabled : 1; |
| 4008 |
|
| 4009 |
// channel_id_enabled is whether Channel ID is enabled. For a server, means |
| 4010 |
// that we'll accept Channel IDs from clients. For a client, means that we'll |
| 4011 |
// advertise support. |
| 4012 |
bool channel_id_enabled : 1; |
| 4013 |
|
| 4014 |
// grease_enabled is whether GREASE (RFC 8701) is enabled. |
| 4015 |
bool grease_enabled : 1; |
| 4016 |
|
| 4017 |
// permute_extensions is whether to permute extensions when sending messages. |
| 4018 |
bool permute_extensions : 1; |
| 4019 |
|
| 4020 |
// allow_unknown_alpn_protos is whether the client allows unsolicited ALPN |
| 4021 |
// protocols from the peer. |
| 4022 |
bool allow_unknown_alpn_protos : 1; |
| 4023 |
|
| 4024 |
// false_start_allowed_without_alpn is whether False Start (if |
| 4025 |
// |SSL_MODE_ENABLE_FALSE_START| is enabled) is allowed without ALPN. |
| 4026 |
bool false_start_allowed_without_alpn : 1; |
| 4027 |
|
| 4028 |
// handoff indicates that a server should stop after receiving the |
| 4029 |
// ClientHello and pause the handshake in such a way that |SSL_get_error| |
| 4030 |
// returns |SSL_ERROR_HANDOFF|. |
| 4031 |
bool handoff : 1; |
| 4032 |
|
| 4033 |
// If enable_early_data is true, early data can be sent and accepted. |
| 4034 |
bool enable_early_data : 1; |
| 4035 |
|
| 4036 |
// enable_read_ahead indicates whether the |SSL_CTX| is configured to read as much |
| 4037 |
// as will fit in the SSLBuffer from the BIO, or just enough to read the record |
| 4038 |
// header and then the length of the body |
| 4039 |
bool enable_read_ahead : 1; |
| 4040 |
|
| 4041 |
// aes_hw_override if set indicates we should override checking for AES |
| 4042 |
// hardware support, and use the value in aes_hw_override_value instead. |
| 4043 |
bool aes_hw_override : 1; |
| 4044 |
|
| 4045 |
// aes_hw_override_value is used for testing to indicate the support or lack |
| 4046 |
// of support for AES hardware. The value is only considered if |
| 4047 |
// |aes_hw_override| is true. |
| 4048 |
bool aes_hw_override_value : 1; |
| 4049 |
|
| 4050 |
// conf_max_version_use_default indicates whether the |SSL_CTX| is configured |
| 4051 |
// to use the default maximum protocol version for the relevant protocol |
| 4052 |
// method. By default, |SSL_CTX_new| will set this to true and connections will |
| 4053 |
// use the default max version. callers can change the max version used by calling |
| 4054 |
// |SSL_CTX_set_max_proto_version| with a non-zero value. |
| 4055 |
bool conf_max_version_use_default : 1; |
| 4056 |
|
| 4057 |
// conf_min_version_use_default indicates whether the |SSL_CTX| is configured |
| 4058 |
// to use the default minimum protocol version for the relevant protocol |
| 4059 |
// method. By default, |SSL_CTX_new| will set this to true and connections will |
| 4060 |
// use the default min version. callers can change the min version used by calling |
| 4061 |
// |SSL_CTX_set_min_proto_version| with a non-zero value. |
| 4062 |
bool conf_min_version_use_default : 1; |
| 4063 |
|
| 4064 |
private: |
| 4065 |
friend RefCounted; |
| 4066 |
~ssl_ctx_st(); |
| 4067 |
}; |
| 4068 |
|
| 4069 |
struct ssl_st { |
| 4070 |
explicit ssl_st(SSL_CTX *ctx_arg); |
| 4071 |
ssl_st(const ssl_st &) = delete; |
| 4072 |
ssl_st &operator=(const ssl_st &) = delete; |
| 4073 |
~ssl_st(); |
| 4074 |
|
| 4075 |
// method is the method table corresponding to the current protocol (DTLS or |
| 4076 |
// TLS). |
| 4077 |
const bssl::SSL_PROTOCOL_METHOD *method = nullptr; |
| 4078 |
|
| 4079 |
// config is a container for handshake configuration. Accesses to this field |
| 4080 |
// should check for nullptr, since configuration may be shed after the |
| 4081 |
// handshake completes. (If you have the |SSL_HANDSHAKE| object at hand, use |
| 4082 |
// that instead, and skip the null check.) |
| 4083 |
bssl::UniquePtr<bssl::SSL_CONFIG> config; |
| 4084 |
|
| 4085 |
// version is the protocol version. |
| 4086 |
uint16_t version = 0; |
| 4087 |
|
| 4088 |
uint16_t max_send_fragment = 0; |
| 4089 |
|
| 4090 |
size_t read_ahead_buffer_size = SSL3_RT_MAX_PLAIN_LENGTH + SSL3_RT_MAX_ENCRYPTED_OVERHEAD; |
| 4091 |
|
| 4092 |
// There are 2 BIO's even though they are normally both the same. This is so |
| 4093 |
// data can be read and written to different handlers |
| 4094 |
|
| 4095 |
bssl::UniquePtr<BIO> rbio; // used by SSL_read |
| 4096 |
bssl::UniquePtr<BIO> wbio; // used by SSL_write |
| 4097 |
|
| 4098 |
// do_handshake runs the handshake. On completion, it returns |ssl_hs_ok|. |
| 4099 |
// Otherwise, it returns a value corresponding to what operation is needed to |
| 4100 |
// progress. |
| 4101 |
bssl::ssl_hs_wait_t (*do_handshake)(bssl::SSL_HANDSHAKE *hs) = nullptr; |
| 4102 |
|
| 4103 |
bssl::SSL3_STATE *s3 = nullptr; // TLS variables |
| 4104 |
bssl::DTLS1_STATE *d1 = nullptr; // DTLS variables |
| 4105 |
|
| 4106 |
// callback that allows applications to peek at protocol messages |
| 4107 |
void (*msg_callback)(int write_p, int version, int content_type, |
| 4108 |
const void *buf, size_t len, SSL *ssl, |
| 4109 |
void *arg) = nullptr; |
| 4110 |
void *msg_callback_arg = nullptr; |
| 4111 |
|
| 4112 |
// session info |
| 4113 |
|
| 4114 |
// initial_timeout_duration_ms is the default DTLS timeout duration in |
| 4115 |
// milliseconds. It's used to initialize the timer any time it's restarted. |
| 4116 |
// |
| 4117 |
// RFC 6347 states that implementations SHOULD use an initial timer value of 1 |
| 4118 |
// second. |
| 4119 |
unsigned initial_timeout_duration_ms = 1000; |
| 4120 |
|
| 4121 |
// session is the configured session to be offered by the client. This session |
| 4122 |
// is immutable. |
| 4123 |
bssl::UniquePtr<SSL_SESSION> session; |
| 4124 |
|
| 4125 |
// client_cipher_suites contains cipher suites offered by the client during |
| 4126 |
// the handshake and that are supported by this library, with preference order |
| 4127 |
// maintained. This field is NOT serialized and is only populated if used in |
| 4128 |
// a server context. |
| 4129 |
bssl::UniquePtr<STACK_OF(SSL_CIPHER)> client_cipher_suites; |
| 4130 |
|
| 4131 |
// all_client_cipher_suites contains the raw bytes for cipher suites offered |
| 4132 |
// by the client during the handshake (including those not supported by this |
| 4133 |
// library), with preference order maintained. This field may contain |
| 4134 |
// cipher IDs that are invalid. |
| 4135 |
bssl::UniquePtr<char> all_client_cipher_suites; |
| 4136 |
|
| 4137 |
// Field length of ciphers in client hello. Maximum allowed size is 2^16 |
| 4138 |
uint16_t all_client_cipher_suites_len = 0; |
| 4139 |
|
| 4140 |
void (*info_callback)(const SSL *ssl, int type, int value) = nullptr; |
| 4141 |
|
| 4142 |
bssl::UniquePtr<SSL_CTX> ctx; |
| 4143 |
|
| 4144 |
// session_ctx is the |SSL_CTX| used for the session cache and related |
| 4145 |
// settings. |
| 4146 |
bssl::UniquePtr<SSL_CTX> session_ctx; |
| 4147 |
|
| 4148 |
// extra application data |
| 4149 |
CRYPTO_EX_DATA ex_data; |
| 4150 |
|
| 4151 |
// verify_result is the result of certificate verification in the case of |
| 4152 |
// non-fatal certificate errors. |
| 4153 |
long verify_result = X509_V_ERR_INVALID_CALL; |
| 4154 |
|
| 4155 |
uint32_t options = 0; // protocol behaviour |
| 4156 |
uint32_t mode = 0; // API behaviour |
| 4157 |
uint32_t max_cert_list = 0; |
| 4158 |
bssl::UniquePtr<char> hostname; |
| 4159 |
|
| 4160 |
// quic_method is the method table corresponding to the QUIC hooks. |
| 4161 |
const SSL_QUIC_METHOD *quic_method = nullptr; |
| 4162 |
|
| 4163 |
// renegotiate_mode controls how peer renegotiation attempts are handled. |
| 4164 |
ssl_renegotiate_mode_t renegotiate_mode = ssl_renegotiate_never; |
| 4165 |
|
| 4166 |
// server is true iff the this SSL* is the server half. Note: before the SSL* |
| 4167 |
// is initialized by either SSL_set_accept_state or SSL_set_connect_state, |
| 4168 |
// the side is not determined. In this state, server is always false. |
| 4169 |
bool server : 1; |
| 4170 |
|
| 4171 |
// quiet_shutdown is true if the connection should not send a close_notify on |
| 4172 |
// shutdown. |
| 4173 |
bool quiet_shutdown : 1; |
| 4174 |
|
| 4175 |
// If enable_early_data is true, early data can be sent and accepted. |
| 4176 |
bool enable_early_data : 1; |
| 4177 |
|
| 4178 |
// enable_read_ahead indicates whether the |SSL| is configured to read as much |
| 4179 |
// as will fit in the SSLBuffer from the BIO, or just enough to read the record |
| 4180 |
// header and then the length of the body |
| 4181 |
bool enable_read_ahead : 1; |
| 4182 |
|
| 4183 |
// is_suspended_state indicates that the |SSL| object has been serialized and |
| 4184 |
// operations should not be performed on the connection. |
| 4185 |
bool is_suspended_state : 1; |
| 4186 |
}; |
| 4187 |
|
| 4188 |
struct ssl_session_st : public bssl::RefCounted<ssl_session_st> { |
| 4189 |
explicit ssl_session_st(const bssl::SSL_X509_METHOD *method); |
| 4190 |
ssl_session_st(const ssl_session_st &) = delete; |
| 4191 |
ssl_session_st &operator=(const ssl_session_st &) = delete; |
| 4192 |
|
| 4193 |
// ssl_version is the (D)TLS version that established the session. |
| 4194 |
uint16_t ssl_version = 0; |
| 4195 |
|
| 4196 |
// group_id is the ID of the ECDH group used to establish this session or zero |
| 4197 |
// if not applicable or unknown. |
| 4198 |
uint16_t group_id = 0; |
| 4199 |
|
| 4200 |
// peer_signature_algorithm is the signature algorithm used to authenticate |
| 4201 |
// the peer, or zero if not applicable or unknown. |
| 4202 |
uint16_t peer_signature_algorithm = 0; |
| 4203 |
|
| 4204 |
// secret, in TLS 1.2 and below, is the master secret associated with the |
| 4205 |
// session. In TLS 1.3 and up, it is the resumption PSK for sessions handed to |
| 4206 |
// the caller, but it stores the resumption secret when stored on |SSL| |
| 4207 |
// objects. |
| 4208 |
uint8_t secret_length = 0; |
| 4209 |
uint8_t secret[SSL_MAX_MASTER_KEY_LENGTH] = {0}; |
| 4210 |
|
| 4211 |
// session_id - valid? |
| 4212 |
uint8_t session_id_length = 0; |
| 4213 |
uint8_t session_id[SSL_MAX_SSL_SESSION_ID_LENGTH] = {0}; |
| 4214 |
// this is used to determine whether the session is being reused in |
| 4215 |
// the appropriate context. It is up to the application to set this, |
| 4216 |
// via SSL_new |
| 4217 |
uint8_t sid_ctx_length = 0; |
| 4218 |
uint8_t sid_ctx[SSL_MAX_SID_CTX_LENGTH] = {0}; |
| 4219 |
|
| 4220 |
bssl::UniquePtr<char> psk_identity; |
| 4221 |
|
| 4222 |
// certs contains the certificate chain from the peer, starting with the leaf |
| 4223 |
// certificate. |
| 4224 |
bssl::UniquePtr<STACK_OF(CRYPTO_BUFFER)> certs; |
| 4225 |
|
| 4226 |
const bssl::SSL_X509_METHOD *x509_method = nullptr; |
| 4227 |
|
| 4228 |
// x509_peer is the peer's certificate. |
| 4229 |
X509 *x509_peer = nullptr; |
| 4230 |
|
| 4231 |
// x509_chain is the certificate chain sent by the peer. NOTE: for historical |
| 4232 |
// reasons, when a client (so the peer is a server), the chain includes |
| 4233 |
// |peer|, but when a server it does not. |
| 4234 |
STACK_OF(X509) *x509_chain = nullptr; |
| 4235 |
|
| 4236 |
// x509_chain_without_leaf is a lazily constructed copy of |x509_chain| that |
| 4237 |
// omits the leaf certificate. This exists because OpenSSL, historically, |
| 4238 |
// didn't include the leaf certificate in the chain for a server, but did for |
| 4239 |
// a client. The |x509_chain| always includes it and, if an API call requires |
| 4240 |
// a chain without, it is stored here. |
| 4241 |
STACK_OF(X509) *x509_chain_without_leaf = nullptr; |
| 4242 |
|
| 4243 |
// x509_verified_chain is a lazily constructed copy of the chain that has |
| 4244 |
// been verified. This is not necessarily the same as |x509_chain| which |
| 4245 |
// may include additional certificates that were not used. |
| 4246 |
STACK_OF(X509) *x509_verified_chain = nullptr; |
| 4247 |
|
| 4248 |
|
| 4249 |
// verify_result is the result of certificate verification in the case of |
| 4250 |
// non-fatal certificate errors. |
| 4251 |
long verify_result = X509_V_ERR_INVALID_CALL; |
| 4252 |
|
| 4253 |
// timeout is the lifetime of the session in seconds, measured from |time|. |
| 4254 |
// This is renewable up to |auth_timeout|. |
| 4255 |
uint32_t timeout = SSL_DEFAULT_SESSION_TIMEOUT; |
| 4256 |
|
| 4257 |
// auth_timeout is the non-renewable lifetime of the session in seconds, |
| 4258 |
// measured from |time|. |
| 4259 |
uint32_t auth_timeout = SSL_DEFAULT_SESSION_TIMEOUT; |
| 4260 |
|
| 4261 |
// time is the time the session was issued, measured in seconds from the UNIX |
| 4262 |
// epoch. |
| 4263 |
uint64_t time = 0; |
| 4264 |
|
| 4265 |
const SSL_CIPHER *cipher = nullptr; |
| 4266 |
|
| 4267 |
CRYPTO_EX_DATA ex_data; // application specific data |
| 4268 |
|
| 4269 |
// These are used to make removal of session-ids more efficient and to |
| 4270 |
// implement a maximum cache size. |
| 4271 |
SSL_SESSION *prev = nullptr, *next = nullptr; |
| 4272 |
|
| 4273 |
bssl::Array<uint8_t> ticket; |
| 4274 |
|
| 4275 |
bssl::UniquePtr<CRYPTO_BUFFER> signed_cert_timestamp_list; |
| 4276 |
|
| 4277 |
// The OCSP response that came with the session. |
| 4278 |
bssl::UniquePtr<CRYPTO_BUFFER> ocsp_response; |
| 4279 |
|
| 4280 |
// peer_sha256 contains the SHA-256 hash of the peer's certificate if |
| 4281 |
// |peer_sha256_valid| is true. |
| 4282 |
uint8_t peer_sha256[SHA256_DIGEST_LENGTH] = {0}; |
| 4283 |
|
| 4284 |
// original_handshake_hash contains the handshake hash (either SHA-1+MD5 or |
| 4285 |
// SHA-2, depending on TLS version) for the original, full handshake that |
| 4286 |
// created a session. This is used by Channel IDs during resumption. |
| 4287 |
uint8_t original_handshake_hash[EVP_MAX_MD_SIZE] = {0}; |
| 4288 |
uint8_t original_handshake_hash_len = 0; |
| 4289 |
|
| 4290 |
uint32_t ticket_lifetime_hint = 0; // Session lifetime hint in seconds |
| 4291 |
|
| 4292 |
uint32_t ticket_age_add = 0; |
| 4293 |
|
| 4294 |
// ticket_max_early_data is the maximum amount of data allowed to be sent as |
| 4295 |
// early data. If zero, 0-RTT is disallowed. |
| 4296 |
uint32_t ticket_max_early_data = 0; |
| 4297 |
|
| 4298 |
// early_alpn is the ALPN protocol from the initial handshake. This is only |
| 4299 |
// stored for TLS 1.3 and above in order to enforce ALPN matching for 0-RTT |
| 4300 |
// resumptions. For the current connection's ALPN protocol, see |
| 4301 |
// |alpn_selected| on |SSL3_STATE|. |
| 4302 |
bssl::Array<uint8_t> early_alpn; |
| 4303 |
|
| 4304 |
// local_application_settings, if |has_application_settings| is true, is the |
| 4305 |
// local ALPS value for this connection. |
| 4306 |
bssl::Array<uint8_t> local_application_settings; |
| 4307 |
|
| 4308 |
// peer_application_settings, if |has_application_settings| is true, is the |
| 4309 |
// peer ALPS value for this connection. |
| 4310 |
bssl::Array<uint8_t> peer_application_settings; |
| 4311 |
|
| 4312 |
// extended_master_secret is whether the master secret in this session was |
| 4313 |
// generated using EMS and thus isn't vulnerable to the Triple Handshake |
| 4314 |
// attack. |
| 4315 |
bool extended_master_secret : 1; |
| 4316 |
|
| 4317 |
// peer_sha256_valid is whether |peer_sha256| is valid. |
| 4318 |
bool peer_sha256_valid : 1; // Non-zero if peer_sha256 is valid |
| 4319 |
|
| 4320 |
// not_resumable is used to indicate that session resumption is disallowed. |
| 4321 |
bool not_resumable : 1; |
| 4322 |
|
| 4323 |
// ticket_age_add_valid is whether |ticket_age_add| is valid. |
| 4324 |
bool ticket_age_add_valid : 1; |
| 4325 |
|
| 4326 |
// is_server is whether this session was created by a server. |
| 4327 |
bool is_server : 1; |
| 4328 |
|
| 4329 |
// is_quic indicates whether this session was created using QUIC. |
| 4330 |
bool is_quic : 1; |
| 4331 |
|
| 4332 |
// has_application_settings indicates whether ALPS was negotiated in this |
| 4333 |
// session. |
| 4334 |
bool has_application_settings : 1; |
| 4335 |
|
| 4336 |
// quic_early_data_context is used to determine whether early data must be |
| 4337 |
// rejected when performing a QUIC handshake. |
| 4338 |
bssl::Array<uint8_t> quic_early_data_context; |
| 4339 |
|
| 4340 |
private: |
| 4341 |
friend RefCounted; |
| 4342 |
~ssl_session_st(); |
| 4343 |
}; |
| 4344 |
|
| 4345 |
struct ssl_ech_keys_st : public bssl::RefCounted<ssl_ech_keys_st> { |
| 4346 |
ssl_ech_keys_st() : RefCounted(CheckSubClass()) {} |
| 4347 |
|
| 4348 |
bssl::GrowableArray<bssl::UniquePtr<bssl::ECHServerConfig>> configs; |
| 4349 |
|
| 4350 |
private: |
| 4351 |
friend RefCounted; |
| 4352 |
~ssl_ech_keys_st() = default; |
| 4353 |
}; |
| 4354 |
|
| 4355 |
#endif // OPENSSL_HEADER_SSL_INTERNAL_H |