| 1 |
// Copyright (c) 2016 Sandstorm Development Group, Inc. and contributorscxx . . $ `<file>/src/kj/compat/tls.c++`/ |
| 2 |
// Licensed under the MIT License: |
| 3 |
// |
| 4 |
// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 5 |
// of this software and associated documentation files (the "Software"), to deal |
| 6 |
// in the Software without restriction, including without limitation the rights |
| 7 |
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 8 |
// copies of the Software, and to permit persons to whom the Software is |
| 9 |
// furnished to do so, subject to the following conditions: |
| 10 |
// |
| 11 |
// The above copyright notice and this permission notice shall be included in |
| 12 |
// all copies or substantial portions of the Software. |
| 13 |
// |
| 14 |
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 |
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 |
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 17 |
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 18 |
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 19 |
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 20 |
// THE SOFTWARE. |
| 21 |
|
| 22 |
#if KJ_HAS_OPENSSLcxx . . $ `<built-in>:485:9`! |
| 23 |
|
| 24 |
#include "tls.h"cxx . . $ `<file>/src/kj/compat/tls.h`/ |
| 25 |
|
| 26 |
#include "readiness-io.h"cxx . . $ `<file>/src/kj/compat/readiness-io.h`/ |
| 27 |
|
| 28 |
#include <openssl/bio.h> |
| 29 |
#include <openssl/conf.h> |
| 30 |
#include <openssl/err.h> |
| 31 |
#include <openssl/evp.h> |
| 32 |
#include <openssl/ssl.h> |
| 33 |
#include <openssl/tls1.h> |
| 34 |
#include <openssl/x509.h> |
| 35 |
#include <openssl/x509v3.h> |
| 36 |
|
| 37 |
#include <kj/async-queue.h>cxx . . $ `<file>/src/kj/async-queue.h`/ |
| 38 |
#include <kj/debug.h>cxx . . $ `<file>/src/kj/debug.h`/ |
| 39 |
#include <kj/vector.h>cxx . . $ `<file>/src/kj/vector.h`/ |
| 40 |
|
| 41 |
#if OPENSSL_VERSION_NUMBERcxx . . $ `<external>/4166512202731279593/opensslv.h:115:10`! < 0x10100000L |
| 42 |
#define BIO_set_init(x,v) (x->init=v) |
| 43 |
#define BIO_get_data(x) (x->ptr) |
| 44 |
#define BIO_set_data(x,v) (x->ptr=v) |
| 45 |
#endif |
| 46 |
|
| 47 |
namespace kjcxx . . $ kj/ { |
| 48 |
|
| 49 |
// ======================================================================================= |
| 50 |
// misc helpers |
| 51 |
|
| 52 |
namespacecxx . . $ kj/`$anonymous_namespace_src/kj/compat/tls.c++`/ { |
| 53 |
|
| 54 |
kjcxx . . $ kj/::Exceptioncxx . . $ kj/Exception# getOpensslErrorcxx . . $ kj/`$anonymous_namespace_src/kj/compat/tls.c++`/getOpensslError(331c1aa6ceacfe87).() { |
| 55 |
// Call when an OpenSSL function returns an error code to convert that into an exception. |
| 56 |
|
| 57 |
kjcxx . . $ kj/::Vectorcxx . . $ kj/Vector#<kjcxx . . $ kj/::Stringcxx . . $ kj/String#> linescxx . . $ kj/Vector#Vector(49f6e7a06ebc5aa8).; |
| 58 |
while (unsigned long long error = ERR_get_error()) { |
| 59 |
#ifdef SSL_R_UNEXPECTED_EOF_WHILE_READINGcxx . . $ `<external>/1684504134986157426/sslerr.h:337:9`! |
| 60 |
// OpenSSL 3.0+ reports unexpected disconnects this way. |
| 61 |
if (ERR_GET_REASON(error) == SSL_R_UNEXPECTED_EOF_WHILE_READINGcxx . . $ `<external>/1684504134986157426/sslerr.h:337:9`!) { |
| 62 |
return KJ_EXCEPTIONcxx . . $ `src/kj/debug.h:311:9`!(DISCONNECTED, |
| 63 |
"peer disconnected without gracefully ending TLS session"); |
| 64 |
} |
| 65 |
#endif |
| 66 |
|
| 67 |
char message[1024]; |
| 68 |
ERR_error_string_n(error, message, sizeof(message)); |
| 69 |
lines.addcxx . . $ kj/Vector#add(7f9966f43a57f391).(kjcxx . . $ kj/::heapStringcxx . . $ kj/heapString(99c5d3b04c1c0a87).(message)); |
| 70 |
} |
| 71 |
kjcxx . . $ kj/::Stringcxx . . $ kj/String# message = kjcxx . . $ kj/::strArraycxx . . $ kj/strArray(53035638ff13f71b).(lines, "\n"); |
| 72 |
return KJ_EXCEPTIONcxx . . $ `src/kj/debug.h:311:9`!(FAILED, "OpenSSL error", message); |
| 73 |
} |
| 74 |
|
| 75 |
KJ_NORETURNcxx . . $ `src/kj/common.h:248:9`!(void throwOpensslError()); |
| 76 |
void throwOpensslErrorcxx . . $ kj/`$anonymous_namespace_src/kj/compat/tls.c++`/throwOpensslError(8df32a62017fbbf6).() { |
| 77 |
// Call when an OpenSSL function returns an error code to convert that into an exception and |
| 78 |
// throw it. |
| 79 |
|
| 80 |
kjcxx . . $ kj/::throwFatalExceptioncxx . . $ kj/throwFatalException(7665b3bc7566d459).(getOpensslErrorcxx . . $ kj/`$anonymous_namespace_src/kj/compat/tls.c++`/getOpensslError(331c1aa6ceacfe87).()); |
| 81 |
} |
| 82 |
|
| 83 |
#if OPENSSL_VERSION_NUMBERcxx . . $ `<external>/4166512202731279593/opensslv.h:115:10`! < 0x10100000L && !defined(OPENSSL_IS_BORINGSSL) |
| 84 |
// Older versions of OpenSSL don't define _up_ref() functions. |
| 85 |
|
| 86 |
void EVP_PKEY_up_ref(EVP_PKEY* pkey) { |
| 87 |
CRYPTO_add(&pkey->references, 1, CRYPTO_LOCK_EVP_PKEY); |
| 88 |
} |
| 89 |
|
| 90 |
void X509_up_ref(X509* x509) { |
| 91 |
CRYPTO_add(&x509->references, 1, CRYPTO_LOCK_X509); |
| 92 |
} |
| 93 |
|
| 94 |
#endif |
| 95 |
|
| 96 |
#if OPENSSL_VERSION_NUMBERcxx . . $ `<external>/4166512202731279593/opensslv.h:115:10`! < 0x10100000L |
| 97 |
class OpenSslInit { |
| 98 |
// Initializes the OpenSSL library. |
| 99 |
public: |
| 100 |
OpenSslInit() { |
| 101 |
SSL_library_init(); |
| 102 |
SSL_load_error_strings(); |
| 103 |
OPENSSL_config(nullptr); |
| 104 |
} |
| 105 |
}; |
| 106 |
|
| 107 |
void ensureOpenSslInitialized() { |
| 108 |
// Initializes the OpenSSL library the first time it is called. |
| 109 |
static OpenSslInit init; |
| 110 |
} |
| 111 |
#else |
| 112 |
inline void ensureOpenSslInitializedcxx . . $ kj/`$anonymous_namespace_src/kj/compat/tls.c++`/ensureOpenSslInitialized(49f6e7a06ebc5aa8).() { |
| 113 |
// As of 1.1.0, no initialization is needed. |
| 114 |
} |
| 115 |
#endif |
| 116 |
|
| 117 |
bool isIpAddresscxx . . $ kj/`$anonymous_namespace_src/kj/compat/tls.c++`/isIpAddress(566dc73899da6988).(kjcxx . . $ kj/::StringPtrcxx . . $ kj/StringPtr# addr) { |
| 118 |
bool isPossiblyIp6 = true; |
| 119 |
bool isPossiblyIp4 = true; |
| 120 |
uintcxx . . $ kj/uint# colonCount = 0; |
| 121 |
uintcxx . . $ kj/uint# dotCount = 0; |
| 122 |
for (auto c: addr) { |
| 123 |
if (c == ':') { |
| 124 |
isPossiblyIp4 = false; |
| 125 |
++colonCount; |
| 126 |
} else if (c == '.') { |
| 127 |
isPossiblyIp6 = false; |
| 128 |
++dotCount; |
| 129 |
} else if ('0' <= c && c <= '9') { |
| 130 |
// Digit is valid for ipv4 or ipv6. |
| 131 |
} else if (('a' <= c && c <= 'f') || ('A' <= c && c <= 'F')) { |
| 132 |
// Hex digit could be ipv6 but not ipv4. |
| 133 |
isPossiblyIp4 = false; |
| 134 |
} else { |
| 135 |
// Nope. |
| 136 |
return false; |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
// An IPv4 address has 3 dots. (Yes, I'm aware that technically IPv4 addresses can be formatted |
| 141 |
// with fewer dots, but it's not clear that we actually want to support TLS authentication of |
| 142 |
// non-canonical address formats, so for now I'm not. File a bug if you care.) An IPv6 address |
| 143 |
// has at least 2 and as many as 7 colons. |
| 144 |
return (isPossiblyIp4 && dotCount == 3) |
| 145 |
|| (isPossiblyIp6 && colonCount >= 2 && colonCount <= 7); |
| 146 |
} |
| 147 |
|
| 148 |
} // namespace |
| 149 |
|
| 150 |
// ======================================================================================= |
| 151 |
// Implementation of kj::AsyncIoStream that applies TLS on top of some other AsyncIoStream. |
| 152 |
// |
| 153 |
// TODO(perf): OpenSSL's I/O abstraction layer, "BIO", is readiness-based, but AsyncIoStream is |
| 154 |
// completion-based. This forces us to use an intermediate buffer which wastes memory and incurs |
| 155 |
// redundant copies. We could improve the situation by creating a way to detect if the underlying |
| 156 |
// AsyncIoStream is simply wrapping a file descriptor (or other readiness-based stream?) and use |
| 157 |
// that directly if so. |
| 158 |
|
| 159 |
class TlsConnectioncxx . . $ kj/TlsConnection# final: public kjcxx . . $ kj/::AsyncIoStreamcxx . . $ kj/AsyncIoStream# { |
| 160 |
public: |
| 161 |
TlsConnectioncxx . . $ kj/TlsConnection#TlsConnection(c2d97c65184cca11).(kjcxx . . $ kj/::Owncxx . . $ kj/Own#<kjcxx . . $ kj/::AsyncIoStreamcxx . . $ kj/AsyncIoStream#> stream, SSL_CTX* ctx) |
| 162 |
: TlsConnectioncxx . . $ kj/TlsConnection#TlsConnection(cd7f85144eff025b).(*cxx . . $ kj/Own#`operator*`(ed53f71ddbc8dcc3).stream, ctx) { |
| 163 |
ownInnercxx . . $ kj/TlsConnection#ownInner. =cxx . . $ kj/Own#`operator=`(e96ca0ce8edb0ed). kjcxx . . $ kj/::mvcxx . . $ kj/mv(9768e8ef939d477).(stream); |
| 164 |
} |
| 165 |
|
| 166 |
TlsConnectioncxx . . $ kj/TlsConnection#TlsConnection(cd7f85144eff025b).(kjcxx . . $ kj/::AsyncIoStreamcxx . . $ kj/AsyncIoStream#& stream, SSL_CTX* ctx) |
| 167 |
: innercxx . . $ kj/TlsConnection#inner.(stream), readBuffercxx . . $ kj/TlsConnection#readBuffer.(stream), writeBuffercxx . . $ kj/TlsConnection#writeBuffer.(stream) { |
| 168 |
sslcxx . . $ kj/TlsConnection#ssl. = SSL_new(ctx); |
| 169 |
if (sslcxx . . $ kj/TlsConnection#ssl. == nullptr) { |
| 170 |
throwOpensslErrorcxx . . $ kj/`$anonymous_namespace_src/kj/compat/tls.c++`/throwOpensslError(8df32a62017fbbf6).(); |
| 171 |
} |
| 172 |
|
| 173 |
BIO* bio = BIO_new(const_cast<BIO_METHOD*>(getBioVtablecxx . . $ kj/TlsConnection#getBioVtable(cbc56c67b39b23ea).())); |
| 174 |
if (bio == nullptr) { |
| 175 |
SSL_free(sslcxx . . $ kj/TlsConnection#ssl.); |
| 176 |
throwOpensslErrorcxx . . $ kj/`$anonymous_namespace_src/kj/compat/tls.c++`/throwOpensslError(8df32a62017fbbf6).(); |
| 177 |
} |
| 178 |
|
| 179 |
BIO_set_data(bio, this); |
| 180 |
BIO_set_init(bio, 1); |
| 181 |
SSL_set_bio(sslcxx . . $ kj/TlsConnection#ssl., bio, bio); |
| 182 |
} |
| 183 |
|
| 184 |
kjcxx . . $ kj/::Promisecxx . . $ kj/Promise#<void> connectcxx . . $ kj/TlsConnection#connect(1d2c1f1d64173a3a).(kjcxx . . $ kj/::StringPtrcxx . . $ kj/StringPtr# expectedServerHostname) { |
| 185 |
if (!SSL_set_tlsext_host_namecxx . . $ `<external>/8732523339078948436/tls1.h:292:9`!(ssl, expectedServerHostname.cStr())) { |
| 186 |
return getOpensslErrorcxx . . $ kj/Promise#Promise(c33f37f983fdaa7c).(); |
| 187 |
} |
| 188 |
|
| 189 |
X509_VERIFY_PARAM* verify = SSL_get0_param(sslcxx . . $ kj/TlsConnection#ssl.); |
| 190 |
if (verify == nullptr) { |
| 191 |
return getOpensslErrorcxx . . $ kj/Promise#Promise(c33f37f983fdaa7c).(); |
| 192 |
} |
| 193 |
|
| 194 |
if (isIpAddresscxx . . $ kj/`$anonymous_namespace_src/kj/compat/tls.c++`/isIpAddress(566dc73899da6988).(expectedServerHostname)) { |
| 195 |
if (X509_VERIFY_PARAM_set1_ip_asc(verify, expectedServerHostname.cStrcxx . . $ kj/StringPtr#cStr(a9f41ea0e82d88cf).()) <= 0) { |
| 196 |
return getOpensslErrorcxx . . $ kj/Promise#Promise(c33f37f983fdaa7c).(); |
| 197 |
} |
| 198 |
} else { |
| 199 |
if (X509_VERIFY_PARAM_set1_host( |
| 200 |
verify, expectedServerHostname.cStrcxx . . $ kj/StringPtr#cStr(a9f41ea0e82d88cf).(), expectedServerHostname.sizecxx . . $ kj/StringPtr#size(fe4d2d262d4164a8).()) <= 0) { |
| 201 |
return getOpensslErrorcxx . . $ kj/Promise#Promise(c33f37f983fdaa7c).(); |
| 202 |
} |
| 203 |
} |
| 204 |
|
| 205 |
// As of OpenSSL 1.1.0, X509_V_FLAG_TRUSTED_FIRST is on by default. Turning it on for older |
| 206 |
// versions -- as well as certain OpenSSL-compatible libraries -- fixes the problem described |
| 207 |
// here: https://community.letsencrypt.org/t/openssl-client-compatibility-changes-for-let-s-encrypt-certificates/143816 |
| 208 |
// |
| 209 |
// Otherwise, certificates issued by Let's Encrypt won't work as of September 30, 2021: |
| 210 |
// https://letsencrypt.org/docs/dst-root-ca-x3-expiration-september-2021/ |
| 211 |
X509_VERIFY_PARAM_set_flags(verify, X509_V_FLAG_TRUSTED_FIRSTcxx . . $ `<external>/18019146896230236826/x509_vfy.h:466:9`!); |
| 212 |
|
| 213 |
return sslCallcxx . . $ kj/TlsConnection#sslCall(f569766beb1301f5).([this]() { return SSL_connect(sslcxx . . $ kj/TlsConnection#ssl.); }).thencxx . . $ kj/Promise#then(fd6a6beaa0df29f).([this](size_t) { |
| 214 |
X509* cert = SSL_get_peer_certificatecxx . . $ `<external>/17793290034335270529/ssl.h:1831:9`!(sslcxx . . $ kj/TlsConnection#ssl.); |
| 215 |
KJ_REQUIREcxx . . $ `src/kj/debug.h:227:9`!(cert != nullptr, "TLS peer provided no certificate") { return; } |
| 216 |
X509_free(cert); |
| 217 |
|
| 218 |
auto result = SSL_get_verify_result(sslcxx . . $ kj/TlsConnection#ssl.); |
| 219 |
if (result != X509_V_OKcxx . . $ `<external>/18019146896230236826/x509_vfy.h:312:9`!) { |
| 220 |
const char* reason = X509_verify_cert_error_string(result); |
| 221 |
KJ_FAIL_REQUIREcxx . . $ `src/kj/debug.h:232:9`!("TLS peer's certificate is not trusted", reason) { break; } |
| 222 |
} |
| 223 |
}); |
| 224 |
} |
| 225 |
|
| 226 |
kjcxx . . $ kj/::Promisecxx . . $ kj/Promise#<void> acceptcxx . . $ kj/TlsConnection#accept(542ddddf9229d39a).() { |
| 227 |
// We are the server. Set SSL options to prefer server's cipher choice. |
| 228 |
SSL_set_options(sslcxx . . $ kj/TlsConnection#ssl., SSL_OP_CIPHER_SERVER_PREFERENCEcxx . . $ `<external>/17793290034335270529/ssl.h:415:9`!); |
| 229 |
|
| 230 |
auto acceptPromise = sslCallcxx . . $ kj/TlsConnection#sslCall(f569766beb1301f5).([this]() { |
| 231 |
return SSL_accept(sslcxx . . $ kj/TlsConnection#ssl.); |
| 232 |
}); |
| 233 |
return acceptPromise.thencxx . . $ kj/Promise#then(fd6a6beaa0df29f).([](size_t ret) { |
| 234 |
if (ret == 0) { |
| 235 |
kjcxx . . $ kj/::throwRecoverableExceptioncxx . . $ kj/throwRecoverableException(6529c6cadc6e9b51).( |
| 236 |
KJ_EXCEPTIONcxx . . $ `src/kj/debug.h:311:9`!(DISCONNECTED, "Client disconnected during SSL_accept()")); |
| 237 |
} |
| 238 |
}); |
| 239 |
} |
| 240 |
|
| 241 |
kjcxx . . $ kj/::Owncxx . . $ kj/Own#<TlsPeerIdentitycxx . . $ kj/TlsPeerIdentity#> getIdentitycxx . . $ kj/TlsConnection#getIdentity(dd9645e44839626a).(kjcxx . . $ kj/::Owncxx . . $ kj/Own#<kjcxx . . $ kj/::PeerIdentitycxx . . $ kj/PeerIdentity#> inner) { |
| 242 |
return kjcxx . . $ kj/::heapcxx . . $ kj/heap(bae2fec6325dc41a).<TlsPeerIdentitycxx . . $ kj/TlsPeerIdentity#>(SSL_get_peer_certificatecxx . . $ `<external>/17793290034335270529/ssl.h:1831:9`!(sslcxx . . $ kj/TlsConnection#ssl.), kjcxx . . $ kj/::mvcxx . . $ kj/mv(9768e8ef939d477).(inner), |
| 243 |
kjcxx . . $ kj/Badge#Badge(49f6e7a06ebc5aa8).::Badgecxx . . $ kj/Badge#<TlsConnectioncxx . . $ kj/TlsConnection#>()); |
| 244 |
} |
| 245 |
|
| 246 |
~cxx . . $ kj/TlsConnection#`~TlsConnection`(49f6e7a06ebc5aa8).TlsConnectioncxx . . $ kj/TlsConnection#() noexcept(false) { |
| 247 |
SSL_free(sslcxx . . $ kj/TlsConnection#ssl.); |
| 248 |
} |
| 249 |
|
| 250 |
kjcxx . . $ kj/::Promisecxx . . $ kj/Promise#<size_t> tryReadcxx . . $ kj/TlsConnection#tryRead(1d893319628a2258).(void* buffer, size_t minBytes, size_t maxBytes) override { |
| 251 |
return tryReadInternalcxx . . $ kj/TlsConnection#tryReadInternal(66ebff3b72408c0a).(buffer, minBytes, maxBytes, 0); |
| 252 |
} |
| 253 |
|
| 254 |
Promisecxx . . $ kj/Promise#<void> writecxx . . $ kj/TlsConnection#write(7ee344f2bd176b7a).(const void* buffer, size_t size) override { |
| 255 |
return writeInternalcxx . . $ kj/TlsConnection#writeInternal(942d41152b1920cb).(kjcxx . . $ kj/::arrayPtrcxx . . $ kj/arrayPtr(e55e65d9587a059a).(reinterpret_cast<const bytecxx . . $ kj/byte#*>(buffer), size), nullptrcxx . . $ kj/ArrayPtr#ArrayPtr(49b4c06ce5d54a60).); |
| 256 |
} |
| 257 |
|
| 258 |
Promisecxx . . $ kj/Promise#<void> writecxx . . $ kj/TlsConnection#write(251ec43b44baa62).(ArrayPtrcxx . . $ kj/ArrayPtr#<const ArrayPtrcxx . . $ kj/ArrayPtr#<const bytecxx . . $ kj/byte#>> pieces) override { |
| 259 |
auto cork = writeBuffercxx . . $ kj/TlsConnection#writeBuffer..corkcxx . . $ kj/ReadyOutputStreamWrapper#cork(d097bbdb19a1666b).(); |
| 260 |
return writeInternalcxx . . $ kj/TlsConnection#writeInternal(942d41152b1920cb).(pieces[cxx . . $ kj/ArrayPtr#`operator[]`(ec43b8547ac5c60).0], pieces.slicecxx . . $ kj/ArrayPtr#slice(2c7e4e1cf603d8fe).(1, pieces.sizecxx . . $ kj/ArrayPtr#size(fe4d2d262d4164a8).())).attachcxx . . $ kj/Promise#attach(27d17a82377fab45).(kjcxx . . $ kj/::mvcxx . . $ kj/mv(9768e8ef939d477).(cork)); |
| 261 |
} |
| 262 |
|
| 263 |
Promisecxx . . $ kj/Promise#<void> whenWriteDisconnectedcxx . . $ kj/TlsConnection#whenWriteDisconnected(542ddddf9229d39a).() override { |
| 264 |
return innercxx . . $ kj/TlsConnection#inner..whenWriteDisconnectedcxx . . $ kj/AsyncOutputStream#whenWriteDisconnected(542ddddf9229d39a).(); |
| 265 |
} |
| 266 |
|
| 267 |
void shutdownWritecxx . . $ kj/TlsConnection#shutdownWrite(49f6e7a06ebc5aa8).() override { |
| 268 |
KJ_REQUIREcxx . . $ `src/kj/debug.h:227:9`!(shutdownTask == nullptr, "already called shutdownWrite()"); |
| 269 |
|
| 270 |
// TODO(2.0): shutdownWrite() is problematic because it doesn't return a promise. It was |
| 271 |
// designed to assume that it would only be called after all writes are finished and that |
| 272 |
// there was no reason to block at that point, but SSL sessions don't fit this since they |
| 273 |
// actually have to send a shutdown message. |
| 274 |
shutdownTaskcxx . . $ kj/TlsConnection#shutdownTask. =cxx . . $ kj/Maybe#`operator=`(3b798851889fcb13). sslCallcxx . . $ kj/TlsConnection#sslCall(f569766beb1301f5).([this]() { |
| 275 |
// The first SSL_shutdown() call is expected to return 0 and may flag a misleading error. |
| 276 |
int result = SSL_shutdown(sslcxx . . $ kj/TlsConnection#ssl.); |
| 277 |
return result == 0 ? 1 : result; |
| 278 |
}).ignoreResultcxx . . $ kj/Promise#ignoreResult(542ddddf9229d39a).().eagerlyEvaluatecxx . . $ kj/Promise#eagerlyEvaluate(2be579741f6bb3d).([](kjcxx . . $ kj/::Exceptioncxx . . $ kj/Exception#&& e) { |
| 279 |
KJ_LOGcxx . . $ `src/kj/debug.h:219:9`!(ERROR, e); |
| 280 |
}); |
| 281 |
} |
| 282 |
|
| 283 |
void abortReadcxx . . $ kj/TlsConnection#abortRead(49f6e7a06ebc5aa8).() override { |
| 284 |
innercxx . . $ kj/TlsConnection#inner..abortReadcxx . . $ kj/AsyncIoStream#abortRead(49f6e7a06ebc5aa8).(); |
| 285 |
} |
| 286 |
|
| 287 |
void getsockoptcxx . . $ kj/TlsConnection#getsockopt(8f49a46285e37e42).(int level, int option, void* value, uintcxx . . $ kj/uint#* length) override { |
| 288 |
innercxx . . $ kj/TlsConnection#inner..getsockoptcxx . . $ kj/AsyncIoStream#getsockopt(8f49a46285e37e42).(level, option, value, length); |
| 289 |
} |
| 290 |
void setsockoptcxx . . $ kj/TlsConnection#setsockopt(beb4e39792953a71).(int level, int option, const void* value, uintcxx . . $ kj/uint# length) override { |
| 291 |
innercxx . . $ kj/TlsConnection#inner..setsockoptcxx . . $ kj/AsyncIoStream#setsockopt(beb4e39792953a71).(level, option, value, length); |
| 292 |
} |
| 293 |
|
| 294 |
void getsocknamecxx . . $ kj/TlsConnection#getsockname(553581679dd6ad96).(struct sockaddrcxx . . $ sockaddr#* addr, uintcxx . . $ kj/uint#* length) override { |
| 295 |
innercxx . . $ kj/TlsConnection#inner..getsocknamecxx . . $ kj/AsyncIoStream#getsockname(553581679dd6ad96).(addr, length); |
| 296 |
} |
| 297 |
void getpeernamecxx . . $ kj/TlsConnection#getpeername(553581679dd6ad96).(struct sockaddrcxx . . $ sockaddr#* addr, uintcxx . . $ kj/uint#* length) override { |
| 298 |
innercxx . . $ kj/TlsConnection#inner..getpeernamecxx . . $ kj/AsyncIoStream#getpeername(553581679dd6ad96).(addr, length); |
| 299 |
} |
| 300 |
|
| 301 |
kjcxx . . $ kj/::Maybecxx . . $ kj/Maybe#<int> getFdcxx . . $ kj/TlsConnection#getFd(4e8e5944c880cc1f).() const override { |
| 302 |
return innercxx . . $ kj/TlsConnection#inner..getFdcxx . . $ kj/AsyncIoStream#getFd(4e8e5944c880cc1f).(); |
| 303 |
} |
| 304 |
|
| 305 |
private: |
| 306 |
SSL* sslcxx . . $ kj/TlsConnection#ssl.; |
| 307 |
kjcxx . . $ kj/::AsyncIoStreamcxx . . $ kj/AsyncIoStream#& innercxx . . $ kj/TlsConnection#inner.; |
| 308 |
kjcxx . . $ kj/::Owncxx . . $ kj/Own#<kjcxx . . $ kj/::AsyncIoStreamcxx . . $ kj/AsyncIoStream#> ownInnercxx . . $ kj/TlsConnection#ownInner.; |
| 309 |
|
| 310 |
kjcxx . . $ kj/::Maybecxx . . $ kj/Maybe#<kjcxx . . $ kj/::Promisecxx . . $ kj/Promise#<void>> shutdownTaskcxx . . $ kj/TlsConnection#shutdownTask.; |
| 311 |
|
| 312 |
ReadyInputStreamWrappercxx . . $ kj/ReadyInputStreamWrapper# readBuffercxx . . $ kj/TlsConnection#readBuffer.; |
| 313 |
ReadyOutputStreamWrappercxx . . $ kj/ReadyOutputStreamWrapper# writeBuffercxx . . $ kj/TlsConnection#writeBuffer.; |
| 314 |
|
| 315 |
kjcxx . . $ kj/::Promisecxx . . $ kj/Promise#<size_t> tryReadInternalcxx . . $ kj/TlsConnection#tryReadInternal(66ebff3b72408c0a).( |
| 316 |
void* buffer, size_t minBytes, size_t maxBytes, size_t alreadyDone) { |
| 317 |
return sslCallcxx . . $ kj/TlsConnection#sslCall(f569766beb1301f5).([this,buffer,maxBytes]() { return SSL_read(sslcxx . . $ kj/TlsConnection#ssl., buffer, maxBytes); }) |
| 318 |
.thencxx . . $ kj/Promise#then(fd6a6beaa0df29f).([this,buffer,minBytes,maxBytes,alreadyDone](size_t n) -> kjcxx . . $ kj/::Promisecxx . . $ kj/Promise#<size_t> { |
| 319 |
if (n >= minBytes || n == 0) { |
| 320 |
return alreadyDonecxx . . $ kj/Promise#Promise(d2a2e67ecf6e02d9). + n; |
| 321 |
} else { |
| 322 |
return tryReadInternalcxx . . $ kj/TlsConnection#tryReadInternal(66ebff3b72408c0a).(reinterpret_cast<bytecxx . . $ kj/byte#*>(buffer) + n, |
| 323 |
minBytes - n, maxBytes - n, alreadyDone + n); |
| 324 |
} |
| 325 |
}); |
| 326 |
} |
| 327 |
|
| 328 |
Promisecxx . . $ kj/Promise#<void> writeInternalcxx . . $ kj/TlsConnection#writeInternal(942d41152b1920cb).(kjcxx . . $ kj/::ArrayPtrcxx . . $ kj/ArrayPtr#<const bytecxx . . $ kj/byte#> first, |
| 329 |
kjcxx . . $ kj/::ArrayPtrcxx . . $ kj/ArrayPtr#<const kjcxx . . $ kj/::ArrayPtrcxx . . $ kj/ArrayPtr#<const bytecxx . . $ kj/byte#>> rest) { |
| 330 |
KJ_REQUIREcxx . . $ `src/kj/debug.h:227:9`!(shutdownTask == nullptr, "already called shutdownWrite()"); |
| 331 |
|
| 332 |
// SSL_write() with a zero-sized input returns 0, but a 0 return is documented as indicating |
| 333 |
// an error. So, we need to avoid zero-sized writes entirely. |
| 334 |
while (first.sizecxx . . $ kj/ArrayPtr#size(fe4d2d262d4164a8).() == 0) { |
| 335 |
if (rest.sizecxx . . $ kj/ArrayPtr#size(fe4d2d262d4164a8).() == 0) { |
| 336 |
return kjcxx . . $ kj/::READY_NOWcxx . . $ kj/READY_NOW.; |
| 337 |
} |
| 338 |
first =cxx . . $ kj/ArrayPtr#`operator=`(c3a6a213a904915e). rest.frontcxx . . $ kj/ArrayPtr#front(19b2b6f17af048b).(); |
| 339 |
rest =cxx . . $ kj/ArrayPtr#`operator=`(1db1f54bc7b38063). rest.slicecxx . . $ kj/ArrayPtr#slice(2c7e4e1cf603d8fe).(1, rest.sizecxx . . $ kj/ArrayPtr#size(fe4d2d262d4164a8).()); |
| 340 |
} |
| 341 |
|
| 342 |
return sslCallcxx . . $ kj/TlsConnection#sslCall(f569766beb1301f5).([this,first]() { return SSL_write(sslcxx . . $ kj/TlsConnection#ssl., first.begincxx . . $ kj/ArrayPtr#begin(1016ee5406d91ab9).(), first.sizecxx . . $ kj/ArrayPtr#size(fe4d2d262d4164a8).()); }) |
| 343 |
.thencxx . . $ kj/Promise#then(fd6a6beaa0df29f).([this,first,rest](size_t n) -> kjcxx . . $ kj/::Promisecxx . . $ kj/Promise#<void> { |
| 344 |
if (n == 0) { |
| 345 |
return KJ_EXCEPTIONcxx . . $ `src/kj/debug.h:311:9`!(DISCONNECTED, "ssl connection ended during write"); |
| 346 |
} else if (n < first.sizecxx . . $ kj/ArrayPtr#size(fe4d2d262d4164a8).()) { |
| 347 |
return writeInternalcxx . . $ kj/TlsConnection#writeInternal(942d41152b1920cb).(first.slicecxx . . $ kj/ArrayPtr#slice(f0c0f5e40ca648d8).(n, first.sizecxx . . $ kj/ArrayPtr#size(fe4d2d262d4164a8).()), rest); |
| 348 |
} else if (rest.sizecxx . . $ kj/ArrayPtr#size(fe4d2d262d4164a8).() > 0) { |
| 349 |
return writeInternalcxx . . $ kj/TlsConnection#writeInternal(942d41152b1920cb).(rest[cxx . . $ kj/ArrayPtr#`operator[]`(b741b238679282eb).0], rest.slicecxx . . $ kj/ArrayPtr#slice(f0c0f5e40ca648d8).(1, rest.sizecxx . . $ kj/ArrayPtr#size(fe4d2d262d4164a8).())); |
| 350 |
} else { |
| 351 |
return kjcxx . . $ kj/::READY_NOWcxx . . $ kj/READY_NOW.; |
| 352 |
} |
| 353 |
}); |
| 354 |
} |
| 355 |
|
| 356 |
template <typename Func> |
| 357 |
kjcxx . . $ kj/::Promisecxx . . $ kj/Promise#<size_t> sslCallcxx . . $ kj/TlsConnection#sslCall(f569766beb1301f5).(Func&& func) { |
| 358 |
auto result = func(); |
| 359 |
|
| 360 |
if (result >cxx . . $ kj/`operator>`(2f718606bd243212). 0) { |
| 361 |
return result; |
| 362 |
} else { |
| 363 |
int error = SSL_get_error(sslcxx . . $ kj/TlsConnection#ssl., result); |
| 364 |
switch (error) { |
| 365 |
case SSL_ERROR_ZERO_RETURNcxx . . $ `<external>/17793290034335270529/ssl.h:1265:9`!: |
| 366 |
return constPromisecxx . . $ kj/constPromise(5668e015fc35486c).<size_t, 0>(); |
| 367 |
case SSL_ERROR_WANT_READcxx . . $ `<external>/17793290034335270529/ssl.h:1260:9`!: |
| 368 |
return readBuffercxx . . $ kj/TlsConnection#readBuffer..whenReadycxx . . $ kj/ReadyInputStreamWrapper#whenReady(542ddddf9229d39a).().thencxx . . $ kj/Promise#then(c7845d38097d22bb).( |
| 369 |
[this,func=kjcxx . . $ kj/::mvcxx . . $ kj/mv(9768e8ef939d477).(func)]() mutable { return sslCallcxx . . $ kj/TlsConnection#sslCall(f569766beb1301f5).(kjcxx . . $ kj/::fwdcxx . . $ kj/fwd(68f4e95e387cd780).<Func>(func)); }); |
| 370 |
case SSL_ERROR_WANT_WRITEcxx . . $ `<external>/17793290034335270529/ssl.h:1261:9`!: |
| 371 |
return writeBuffercxx . . $ kj/TlsConnection#writeBuffer..whenReadycxx . . $ kj/ReadyOutputStreamWrapper#whenReady(542ddddf9229d39a).().thencxx . . $ kj/Promise#then(c7845d38097d22bb).( |
| 372 |
[this,func=kjcxx . . $ kj/::mvcxx . . $ kj/mv(9768e8ef939d477).(func)]() mutable { return sslCallcxx . . $ kj/TlsConnection#sslCall(f569766beb1301f5).(kjcxx . . $ kj/::fwdcxx . . $ kj/fwd(68f4e95e387cd780).<Func>(func)); }); |
| 373 |
case SSL_ERROR_SSLcxx . . $ `<external>/17793290034335270529/ssl.h:1259:9`!: |
| 374 |
return getOpensslErrorcxx . . $ kj/Promise#Promise(c33f37f983fdaa7c).(); |
| 375 |
case SSL_ERROR_SYSCALLcxx . . $ `<external>/17793290034335270529/ssl.h:1263:9`!: |
| 376 |
if (result ==cxx . . $ kj/`operator==`(d65311d0a28cd285). 0) { |
| 377 |
// OpenSSL pre-3.0 reports unexpected disconnects this way. Note that 3.0+ report it |
| 378 |
// as SSL_ERROR_SSL with the reason SSL_R_UNEXPECTED_EOF_WHILE_READING, which is |
| 379 |
// handled in throwOpensslError(). |
| 380 |
return KJ_EXCEPTIONcxx . . $ `src/kj/debug.h:311:9`!(DISCONNECTED, |
| 381 |
"peer disconnected without gracefully ending TLS session"); |
| 382 |
} else { |
| 383 |
// According to documentation we shouldn't get here, because our BIO never returns an |
| 384 |
// "error". But in practice we do get here sometimes when the peer disconnects |
| 385 |
// prematurely. |
| 386 |
return KJ_EXCEPTIONcxx . . $ `src/kj/debug.h:311:9`!(DISCONNECTED, "SSL unable to continue I/O"); |
| 387 |
} |
| 388 |
default: |
| 389 |
KJ_FAIL_ASSERTcxx . . $ `src/kj/debug.h:360:9`!("unexpected SSL error code", error); |
| 390 |
} |
| 391 |
} |
| 392 |
} |
| 393 |
|
| 394 |
static int bioReadcxx . . $ kj/TlsConnection#bioRead(51f43b5bf7d18f7d).(BIO* b, char* out, int outl) { |
| 395 |
BIO_clear_retry_flagscxx . . $ `<external>/10464486504159979787/bio.h:262:9`!(b); |
| 396 |
KJ_IF_MAYBEcxx . . $ `src/kj/common.h:1325:9`!(n, reinterpret_cast<TlsConnection*>(BIO_get_data(b))->readBuffer |
| 397 |
.read(kj::arrayPtr(out, outl).asBytes())) { |
| 398 |
return *cxx . . $ kj/_/NullableValue#`operator*`(db8caaf2f996d67e).n; |
| 399 |
} else { |
| 400 |
BIO_set_retry_readcxx . . $ `<external>/10464486504159979787/bio.h:256:9`!(b); |
| 401 |
return -1; |
| 402 |
} |
| 403 |
} |
| 404 |
|
| 405 |
static int bioWritecxx . . $ kj/TlsConnection#bioWrite(f9215a6f23e4542).(BIO* b, const char* in, int inl) { |
| 406 |
BIO_clear_retry_flagscxx . . $ `<external>/10464486504159979787/bio.h:262:9`!(b); |
| 407 |
KJ_IF_MAYBEcxx . . $ `src/kj/common.h:1325:9`!(n, reinterpret_cast<TlsConnection*>(BIO_get_data(b))->writeBuffer |
| 408 |
.write(kj::arrayPtr(in, inl).asBytes())) { |
| 409 |
return *cxx . . $ kj/_/NullableValue#`operator*`(db8caaf2f996d67e).n; |
| 410 |
} else { |
| 411 |
BIO_set_retry_writecxx . . $ `<external>/10464486504159979787/bio.h:258:9`!(b); |
| 412 |
return -1; |
| 413 |
} |
| 414 |
} |
| 415 |
|
| 416 |
static long bioCtrlcxx . . $ kj/TlsConnection#bioCtrl(c3d6b80742a51d22).(BIO* b, int cmd, long num, void* ptr) { |
| 417 |
switch (cmd) { |
| 418 |
case BIO_CTRL_EOFcxx . . $ `<external>/10464486504159979787/bio.h:91:9`!: |
| 419 |
return reinterpret_cast<TlsConnectioncxx . . $ kj/TlsConnection#*>(BIO_get_data(b))->readBuffercxx . . $ kj/TlsConnection#readBuffer..isAtEndcxx . . $ kj/ReadyInputStreamWrapper#isAtEnd(c9857a73d68e59ba).(); |
| 420 |
case BIO_CTRL_FLUSHcxx . . $ `<external>/10464486504159979787/bio.h:100:9`!: |
| 421 |
return 1; |
| •422 |
case BIO_CTRL_INFOcxx . . $ `<external>/10464486504159979787/bio.h:92:9`!: |
| 423 |
// Queries for info |
| 424 |
return 0; |
| 425 |
case BIO_CTRL_PUSHcxx . . $ `<external>/10464486504159979787/bio.h:95:9`!: |
| 426 |
case BIO_CTRL_POPcxx . . $ `<external>/10464486504159979787/bio.h:96:9`!: |
| 427 |
// Informational? |
| 428 |
return 0; |
| 429 |
#ifdef BIO_CTRL_GET_KTLS_SENDcxx . . $ `<external>/10464486504159979787/bio.h:170:9`! |
| 430 |
case BIO_CTRL_GET_KTLS_SENDcxx . . $ `<external>/10464486504159979787/bio.h:170:9`!: |
| 431 |
case BIO_CTRL_GET_KTLS_RECVcxx . . $ `<external>/10464486504159979787/bio.h:171:9`!: |
| 432 |
// TODO(someday): Support kTLS if the underlying stream is a raw socket. |
| 433 |
return 0; |
| 434 |
#endif |
| 435 |
default: |
| 436 |
KJ_LOGcxx . . $ `src/kj/debug.h:219:9`!(WARNING, "unimplemented bio_ctrl", cmd); |
| 437 |
return 0; |
| 438 |
} |
| 439 |
} |
| 440 |
|
| 441 |
static int bioCreatecxx . . $ kj/TlsConnection#bioCreate(835332762aa40168).(BIO* b) { |
| 442 |
BIO_set_data(b, nullptr); |
| 443 |
return 1; |
| 444 |
} |
| 445 |
|
| 446 |
static int bioDestroycxx . . $ kj/TlsConnection#bioDestroy(835332762aa40168).(BIO* b) { |
| 447 |
// The BIO does NOT own the TlsConnection. |
| 448 |
return 1; |
| 449 |
} |
| 450 |
|
| 451 |
#if OPENSSL_VERSION_NUMBERcxx . . $ `<external>/4166512202731279593/opensslv.h:115:10`! < 0x10100000L |
| 452 |
static const BIO_METHOD* getBioVtable() { |
| 453 |
static const BIO_METHOD VTABLE { |
| 454 |
BIO_TYPE_SOURCE_SINK, |
| 455 |
"KJ stream", |
| 456 |
TlsConnection::bioWrite, |
| 457 |
TlsConnection::bioRead, |
| 458 |
nullptr, // puts |
| 459 |
nullptr, // gets |
| 460 |
TlsConnection::bioCtrl, |
| 461 |
TlsConnection::bioCreate, |
| 462 |
TlsConnection::bioDestroy, |
| 463 |
nullptr |
| 464 |
}; |
| 465 |
return &VTABLE; |
| 466 |
} |
| 467 |
#else |
| 468 |
static const BIO_METHOD* getBioVtablecxx . . $ kj/TlsConnection#getBioVtable(cbc56c67b39b23ea).() { |
| 469 |
static const BIO_METHOD* const vtable = makeBioVtablecxx . . $ kj/TlsConnection#makeBioVtable(cbc56c67b39b23ea).(); |
| 470 |
return vtable; |
| 471 |
} |
| 472 |
static const BIO_METHOD* makeBioVtablecxx . . $ kj/TlsConnection#makeBioVtable(cbc56c67b39b23ea).() { |
| 473 |
BIO_METHOD* vtable = BIO_meth_new(BIO_TYPE_SOURCE_SINKcxx . . $ `<external>/10464486504159979787/bio.h:43:9`!, "KJ stream"); |
| 474 |
BIO_meth_set_write(vtable, TlsConnectioncxx . . $ kj/TlsConnection#::bioWritecxx . . $ kj/TlsConnection#bioWrite(f9215a6f23e4542).); |
| 475 |
BIO_meth_set_read(vtable, TlsConnectioncxx . . $ kj/TlsConnection#::bioReadcxx . . $ kj/TlsConnection#bioRead(51f43b5bf7d18f7d).); |
| 476 |
BIO_meth_set_ctrl(vtable, TlsConnectioncxx . . $ kj/TlsConnection#::bioCtrlcxx . . $ kj/TlsConnection#bioCtrl(c3d6b80742a51d22).); |
| 477 |
BIO_meth_set_create(vtable, TlsConnectioncxx . . $ kj/TlsConnection#::bioCreatecxx . . $ kj/TlsConnection#bioCreate(835332762aa40168).); |
| 478 |
BIO_meth_set_destroy(vtable, TlsConnectioncxx . . $ kj/TlsConnection#::bioDestroycxx . . $ kj/TlsConnection#bioDestroy(835332762aa40168).); |
| 479 |
return vtable; |
| 480 |
} |
| 481 |
#endif |
| 482 |
}; |
| 483 |
|
| 484 |
// ======================================================================================= |
| 485 |
// Implementations of ConnectionReceiver, NetworkAddress, and Network as wrappers adding TLS. |
| 486 |
|
| 487 |
class TlsConnectionReceivercxx . . $ kj/TlsConnectionReceiver# final: public ConnectionReceivercxx . . $ kj/ConnectionReceiver#, public TaskSetcxx . . $ kj/TaskSet#::ErrorHandlercxx . . $ kj/TaskSet#ErrorHandler# { |
| 488 |
public: |
| 489 |
TlsConnectionReceivercxx . . $ kj/TlsConnectionReceiver#TlsConnectionReceiver(ee9c3583b36bca9b).( |
| 490 |
TlsContextcxx . . $ kj/TlsContext# &tls, Owncxx . . $ kj/Own#<ConnectionReceivercxx . . $ kj/ConnectionReceiver#> inner, |
| 491 |
kjcxx . . $ kj/::Maybecxx . . $ kj/Maybe#<TlsErrorHandlercxx . . $ kj/TlsErrorHandler#> acceptErrorHandler) |
| 492 |
: tlscxx . . $ kj/TlsConnectionReceiver#tls.(tls), innercxx . . $ kj/TlsConnectionReceiver#inner.(kjcxx . . $ kj/::mvcxx . . $ kj/mv(9768e8ef939d477).(inner)), |
| 493 |
acceptLoopTaskcxx . . $ kj/TlsConnectionReceiver#acceptLoopTask.(acceptLoopcxx . . $ kj/TlsConnectionReceiver#acceptLoop(542ddddf9229d39a).().eagerlyEvaluatecxx . . $ kj/Promise#eagerlyEvaluate(2be579741f6bb3d).([this](Exceptioncxx . . $ kj/Exception# &&e) { |
| 494 |
onAcceptFailurecxx . . $ kj/TlsConnectionReceiver#onAcceptFailure(c33f37f983fdaa7c).(kjcxx . . $ kj/::mvcxx . . $ kj/mv(9768e8ef939d477).(e)); |
| 495 |
})), |
| 496 |
acceptErrorHandlercxx . . $ kj/TlsConnectionReceiver#acceptErrorHandler.(kjcxx . . $ kj/::mvcxx . . $ kj/mv(9768e8ef939d477).(acceptErrorHandler)), |
| 497 |
taskscxx . . $ kj/TlsConnectionReceiver#tasks.(*this) {} |
| 498 |
|
| 499 |
void taskFailedcxx . . $ kj/TlsConnectionReceiver#taskFailed(c33f37f983fdaa7c).(Exceptioncxx . . $ kj/Exception#&& e) override { |
| 500 |
KJ_IF_MAYBEcxx . . $ `src/kj/common.h:1325:9`!(handler, acceptErrorHandler){ |
| 501 |
handler->operator()(kjcxx . . $ kj/::mvcxx . . $ kj/mv(9768e8ef939d477).(e)); |
| 502 |
} else if (e.getTypecxx . . $ kj/Exception#getType(981cd29bd1003f36).() != Exceptioncxx . . $ kj/Exception#::Typecxx . . $ kj/Exception#Type#::DISCONNECTEDcxx . . $ kj/Exception#Type#DISCONNECTED.) { |
| 503 |
KJ_LOGcxx . . $ `src/kj/debug.h:219:9`!(ERROR, "error accepting tls connection", kj::mv(e)); |
| 504 |
} |
| 505 |
}; |
| 506 |
|
| 507 |
Promisecxx . . $ kj/Promise#<Owncxx . . $ kj/Own#<AsyncIoStreamcxx . . $ kj/AsyncIoStream#>> acceptcxx . . $ kj/TlsConnectionReceiver#accept(61a533e69b34abe2).() override { |
| 508 |
return acceptAuthenticatedcxx . . $ kj/TlsConnectionReceiver#acceptAuthenticated(f5454c7a382d7752).().thencxx . . $ kj/Promise#then(fd6a6beaa0df29f).([](AuthenticatedStreamcxx . . $ kj/AuthenticatedStream#&& stream) { |
| 509 |
return kjcxx . . $ kj/Own#Own(2757233a295510d2).::mvcxx . . $ kj/mv(9768e8ef939d477).(stream.streamcxx . . $ kj/AuthenticatedStream#stream.); |
| 510 |
}); |
| 511 |
} |
| 512 |
|
| 513 |
Promisecxx . . $ kj/Promise#<AuthenticatedStreamcxx . . $ kj/AuthenticatedStream#> acceptAuthenticatedcxx . . $ kj/TlsConnectionReceiver#acceptAuthenticated(f5454c7a382d7752).() override { |
| 514 |
KJ_IF_MAYBEcxx . . $ `src/kj/common.h:1325:9`!(e, maybeInnerException) { |
| 515 |
// We've experienced an exception from the inner receiver, we consider this unrecoverable. |
| 516 |
return Exceptioncxx . . $ kj/Promise#Promise(c33f37f983fdaa7c).(*e); |
| 517 |
} |
| 518 |
|
| 519 |
return queuecxx . . $ kj/TlsConnectionReceiver#queue..popcxx . . $ kj/ProducerConsumerQueue#pop(5668e015fc35486c).(); |
| 520 |
} |
| 521 |
|
| 522 |
uintcxx . . $ kj/uint# getPortcxx . . $ kj/TlsConnectionReceiver#getPort(db52760e79bf9054).() override { |
| 523 |
return innercxx . . $ kj/TlsConnectionReceiver#inner.->cxx . . $ kj/Own#`operator->`(155dc7f103cdb9db).getPortcxx . . $ kj/ConnectionReceiver#getPort(db52760e79bf9054).(); |
| 524 |
} |
| 525 |
|
| 526 |
void getsockoptcxx . . $ kj/TlsConnectionReceiver#getsockopt(8f49a46285e37e42).(int level, int option, void* value, uintcxx . . $ kj/uint#* length) override { |
| 527 |
return innercxx . . $ kj/TlsConnectionReceiver#inner.->cxx . . $ kj/Own#`operator->`(155dc7f103cdb9db).getsockoptcxx . . $ kj/ConnectionReceiver#getsockopt(8f49a46285e37e42).(level, option, value, length); |
| 528 |
} |
| 529 |
|
| 530 |
void setsockoptcxx . . $ kj/TlsConnectionReceiver#setsockopt(beb4e39792953a71).(int level, int option, const void* value, uintcxx . . $ kj/uint# length) override { |
| 531 |
return innercxx . . $ kj/TlsConnectionReceiver#inner.->cxx . . $ kj/Own#`operator->`(155dc7f103cdb9db).setsockoptcxx . . $ kj/ConnectionReceiver#setsockopt(beb4e39792953a71).(level, option, value, length); |
| 532 |
} |
| 533 |
|
| 534 |
private: |
| 535 |
void onAcceptSuccesscxx . . $ kj/TlsConnectionReceiver#onAcceptSuccess(e748fd2252ff1477).(AuthenticatedStreamcxx . . $ kj/AuthenticatedStream#&& stream) { |
| 536 |
// Queue this stream to go through SSL_accept. |
| 537 |
|
| 538 |
auto acceptPromise = kjcxx . . $ kj/::evalNowcxx . . $ kj/evalNow(2a9d78fa55ded97b).([&] { |
| 539 |
// Do the SSL acceptance procedure. |
| 540 |
return tlscxx . . $ kj/TlsConnectionReceiver#tls..wrapServercxx . . $ kj/TlsContext#wrapServer(23480717d04fe30b).(kjcxx . . $ kj/::mvcxx . . $ kj/mv(9768e8ef939d477).(stream)); |
| 541 |
}); |
| 542 |
|
| 543 |
auto sslPromise = acceptPromise.thencxx . . $ kj/Promise#then(fd6a6beaa0df29f).([this](auto&& stream) -> Promisecxx . . $ kj/Promise#<void> { |
| 544 |
// This is only attached to the success path, thus the error handler will catch if our |
| 545 |
// promise fails. |
| 546 |
queuecxx . . $ kj/TlsConnectionReceiver#queue..pushcxx . . $ kj/ProducerConsumerQueue#push(9b289cee16747614).(kjcxx . . $ kj/::mvcxx . . $ kj/mv(9768e8ef939d477).(stream)); |
| 547 |
return kjcxx . . $ kj/::READY_NOWcxx . . $ kj/READY_NOW.; |
| 548 |
}); |
| 549 |
taskscxx . . $ kj/TlsConnectionReceiver#tasks..addcxx . . $ kj/TaskSet#add(4b4073c8e3e9a341).(kjcxx . . $ kj/::mvcxx . . $ kj/mv(9768e8ef939d477).(sslPromise)); |
| 550 |
} |
| 551 |
|
| 552 |
void onAcceptFailurecxx . . $ kj/TlsConnectionReceiver#onAcceptFailure(c33f37f983fdaa7c).(Exceptioncxx . . $ kj/Exception#&& e) { |
| 553 |
// Store this exception to reject all future calls to accept() and reject any unfulfilled |
| 554 |
// promises from the queue. |
| 555 |
maybeInnerExceptioncxx . . $ kj/TlsConnectionReceiver#maybeInnerException. =cxx . . $ kj/Maybe#`operator=`(3b798851889fcb13). kjcxx . . $ kj/::mvcxx . . $ kj/mv(9768e8ef939d477).(e); |
| 556 |
queuecxx . . $ kj/TlsConnectionReceiver#queue..rejectAllcxx . . $ kj/ProducerConsumerQueue#rejectAll(d34461beea93531e).(Exceptioncxx . . $ kj/Exception#(KJ_REQUIRE_NONNULLcxx . . $ `src/kj/debug.h:299:9`!(maybeInnerException))); |
| 557 |
} |
| 558 |
|
| 559 |
Promisecxx . . $ kj/Promise#<void> acceptLoopcxx . . $ kj/TlsConnectionReceiver#acceptLoop(542ddddf9229d39a).() { |
| 560 |
// Accept one connection and queue up the next accept on our TaskSet. |
| 561 |
|
| 562 |
return innercxx . . $ kj/TlsConnectionReceiver#inner.->cxx . . $ kj/Own#`operator->`(155dc7f103cdb9db).acceptAuthenticatedcxx . . $ kj/ConnectionReceiver#acceptAuthenticated(f5454c7a382d7752).().thencxx . . $ kj/Promise#then(fd6a6beaa0df29f).( |
| 563 |
[this](AuthenticatedStreamcxx . . $ kj/AuthenticatedStream#&& stream) { |
| 564 |
onAcceptSuccesscxx . . $ kj/TlsConnectionReceiver#onAcceptSuccess(e748fd2252ff1477).(kjcxx . . $ kj/::mvcxx . . $ kj/mv(9768e8ef939d477).(stream)); |
| 565 |
|
| 566 |
// Queue up the next accept loop immediately without waiting for SSL_accept()/wrapServer(). |
| 567 |
return acceptLoopcxx . . $ kj/TlsConnectionReceiver#acceptLoop(542ddddf9229d39a).(); |
| 568 |
}); |
| 569 |
} |
| 570 |
|
| 571 |
TlsContextcxx . . $ kj/TlsContext#& tlscxx . . $ kj/TlsConnectionReceiver#tls.; |
| 572 |
Owncxx . . $ kj/Own#<ConnectionReceivercxx . . $ kj/ConnectionReceiver#> innercxx . . $ kj/TlsConnectionReceiver#inner.; |
| 573 |
|
| 574 |
Promisecxx . . $ kj/Promise#<void> acceptLoopTaskcxx . . $ kj/TlsConnectionReceiver#acceptLoopTask.; |
| 575 |
ProducerConsumerQueuecxx . . $ kj/ProducerConsumerQueue#<AuthenticatedStreamcxx . . $ kj/AuthenticatedStream#> queuecxx . . $ kj/TlsConnectionReceiver#queue.; |
| 576 |
kjcxx . . $ kj/::Maybecxx . . $ kj/Maybe#<TlsErrorHandlercxx . . $ kj/TlsErrorHandler#> acceptErrorHandlercxx . . $ kj/TlsConnectionReceiver#acceptErrorHandler.; |
| 577 |
TaskSetcxx . . $ kj/TaskSet# taskscxx . . $ kj/TlsConnectionReceiver#tasks.; |
| 578 |
|
| 579 |
Maybecxx . . $ kj/Maybe#<Exceptioncxx . . $ kj/Exception#> maybeInnerExceptioncxx . . $ kj/TlsConnectionReceiver#maybeInnerException.; |
| 580 |
}; |
| 581 |
|
| 582 |
class TlsNetworkAddresscxx . . $ kj/TlsNetworkAddress# final: public kjcxx . . $ kj/::NetworkAddresscxx . . $ kj/NetworkAddress# { |
| 583 |
public: |
| 584 |
TlsNetworkAddresscxx . . $ kj/TlsNetworkAddress#TlsNetworkAddress(5cddf18c275bfdc0).(TlsContextcxx . . $ kj/TlsContext#& tls, kjcxx . . $ kj/::Stringcxx . . $ kj/String# hostname, kjcxx . . $ kj/::Owncxx . . $ kj/Own#<kjcxx . . $ kj/::NetworkAddresscxx . . $ kj/NetworkAddress#>&& inner) |
| 585 |
: tlscxx . . $ kj/TlsNetworkAddress#tls.(tls), hostnamecxx . . $ kj/TlsNetworkAddress#hostname.(kjcxx . . $ kj/::mvcxx . . $ kj/mv(9768e8ef939d477).(hostname)), innercxx . . $ kj/TlsNetworkAddress#inner.(kjcxx . . $ kj/::mvcxx . . $ kj/mv(9768e8ef939d477).(inner)) {} |
| 586 |
|
| 587 |
Promisecxx . . $ kj/Promise#<Owncxx . . $ kj/Own#<AsyncIoStreamcxx . . $ kj/AsyncIoStream#>> connectcxx . . $ kj/TlsNetworkAddress#connect(61a533e69b34abe2).() override { |
| 588 |
// Note: It's unfortunately pretty common for people to assume they can drop the NetworkAddress |
| 589 |
// as soon as connect() returns, and this works with the native network implementation. |
| 590 |
// So, we make some copies here. |
| 591 |
auto& tlsRef = tlscxx . . $ kj/TlsNetworkAddress#tls.; |
| 592 |
auto hostnameCopy = kjcxx . . $ kj/::strcxx . . $ kj/str(d4f75be60228bbc5).(hostnamecxx . . $ kj/TlsNetworkAddress#hostname.); |
| 593 |
return innercxx . . $ kj/TlsNetworkAddress#inner.->cxx . . $ kj/Own#`operator->`(155dc7f103cdb9db).connectcxx . . $ kj/NetworkAddress#connect(61a533e69b34abe2).().thencxx . . $ kj/Promise#then(fd6a6beaa0df29f).( |
| 594 |
[&tlsRef,hostname=kjcxx . . $ kj/::mvcxx . . $ kj/mv(9768e8ef939d477).(hostnameCopy)](Owncxx . . $ kj/Own#<AsyncIoStreamcxx . . $ kj/AsyncIoStream#>&& stream) { |
| 595 |
return tlsRef.wrapClientcxx . . $ kj/TlsContext#wrapClient(3836d9b2584f67da).(kjcxx . . $ kj/Own#Own(2757233a295510d2).::mvcxx . . $ kj/mv(9768e8ef939d477).(stream), hostnamecxx . . $ kj/StringPtr#StringPtr(8e4c6fa50a77d3e6).); |
| 596 |
}); |
| 597 |
} |
| 598 |
|
| 599 |
Promisecxx . . $ kj/Promise#<kjcxx . . $ kj/::AuthenticatedStreamcxx . . $ kj/AuthenticatedStream#> connectAuthenticatedcxx . . $ kj/TlsNetworkAddress#connectAuthenticated(f5454c7a382d7752).() override { |
| 600 |
// Note: It's unfortunately pretty common for people to assume they can drop the NetworkAddress |
| 601 |
// as soon as connect() returns, and this works with the native network implementation. |
| 602 |
// So, we make some copies here. |
| 603 |
auto& tlsRef = tlscxx . . $ kj/TlsNetworkAddress#tls.; |
| 604 |
auto hostnameCopy = kjcxx . . $ kj/::strcxx . . $ kj/str(d4f75be60228bbc5).(hostnamecxx . . $ kj/TlsNetworkAddress#hostname.); |
| 605 |
return innercxx . . $ kj/TlsNetworkAddress#inner.->cxx . . $ kj/Own#`operator->`(155dc7f103cdb9db).connectAuthenticatedcxx . . $ kj/NetworkAddress#connectAuthenticated(f5454c7a382d7752).().thencxx . . $ kj/Promise#then(fd6a6beaa0df29f).( |
| 606 |
[&tlsRef, hostname = kjcxx . . $ kj/::mvcxx . . $ kj/mv(9768e8ef939d477).(hostnameCopy)](kjcxx . . $ kj/::AuthenticatedStreamcxx . . $ kj/AuthenticatedStream# stream) { |
| 607 |
return tlsRef.wrapClientcxx . . $ kj/TlsContext#wrapClient(4ecc64a3eecd33b5).(kjcxx . . $ kj/::mvcxx . . $ kj/mv(9768e8ef939d477).(stream), hostnamecxx . . $ kj/StringPtr#StringPtr(8e4c6fa50a77d3e6).); |
| 608 |
}); |
| 609 |
} |
| 610 |
|
| 611 |
Owncxx . . $ kj/Own#<ConnectionReceivercxx . . $ kj/ConnectionReceiver#> listencxx . . $ kj/TlsNetworkAddress#listen(dbacf5411e921979).() override { |
| 612 |
return tlscxx . . $ kj/TlsNetworkAddress#tls..wrapPortcxx . . $ kj/TlsContext#wrapPort(d212f26a61cd2b85).(innercxx . . $ kj/TlsNetworkAddress#inner.->cxx . . $ kj/Own#`operator->`(155dc7f103cdb9db).listencxx . . $ kj/NetworkAddress#listen(dbacf5411e921979).()); |
| 613 |
} |
| 614 |
|
| 615 |
Owncxx . . $ kj/Own#<NetworkAddresscxx . . $ kj/NetworkAddress#> clonecxx . . $ kj/TlsNetworkAddress#clone(b81ed4165adc1f30).() override { |
| 616 |
return kjcxx . . $ kj/Own#Own(97d08cb47632509f).::heapcxx . . $ kj/heap(bae2fec6325dc41a).<TlsNetworkAddresscxx . . $ kj/TlsNetworkAddress#>(tlscxx . . $ kj/TlsNetworkAddress#tls., kjcxx . . $ kj/::strcxx . . $ kj/str(d4f75be60228bbc5).(hostnamecxx . . $ kj/TlsNetworkAddress#hostname.), innercxx . . $ kj/TlsNetworkAddress#inner.->cxx . . $ kj/Own#`operator->`(155dc7f103cdb9db).clonecxx . . $ kj/NetworkAddress#clone(b81ed4165adc1f30).()); |
| 617 |
} |
| 618 |
|
| 619 |
Stringcxx . . $ kj/String# toStringcxx . . $ kj/TlsNetworkAddress#toString(af22d04b00c82e5d).() override { |
| 620 |
return kjcxx . . $ kj/::strcxx . . $ kj/str(d4f75be60228bbc5).("tls:", innercxx . . $ kj/TlsNetworkAddress#inner.->cxx . . $ kj/Own#`operator->`(155dc7f103cdb9db).toStringcxx . . $ kj/NetworkAddress#toString(af22d04b00c82e5d).()); |
| 621 |
} |
| 622 |
|
| 623 |
private: |
| 624 |
TlsContextcxx . . $ kj/TlsContext#& tlscxx . . $ kj/TlsNetworkAddress#tls.; |
| 625 |
kjcxx . . $ kj/::Stringcxx . . $ kj/String# hostnamecxx . . $ kj/TlsNetworkAddress#hostname.; |
| 626 |
kjcxx . . $ kj/::Owncxx . . $ kj/Own#<kjcxx . . $ kj/::NetworkAddresscxx . . $ kj/NetworkAddress#> innercxx . . $ kj/TlsNetworkAddress#inner.; |
| 627 |
}; |
| 628 |
|
| 629 |
class TlsNetworkcxx . . $ kj/TlsNetwork# final: public kjcxx . . $ kj/::Networkcxx . . $ kj/Network# { |
| 630 |
public: |
| 631 |
TlsNetworkcxx . . $ kj/TlsNetwork#TlsNetwork(ff3e5917d8e2f62b).(TlsContextcxx . . $ kj/TlsContext#& tls, kjcxx . . $ kj/::Networkcxx . . $ kj/Network#& inner): tlscxx . . $ kj/TlsNetwork#tls.(tls), innercxx . . $ kj/TlsNetwork#inner.(inner) {} |
| 632 |
TlsNetworkcxx . . $ kj/TlsNetwork#TlsNetwork(9745b997506e9eb).(TlsContextcxx . . $ kj/TlsContext#& tls, kjcxx . . $ kj/::Owncxx . . $ kj/Own#<kjcxx . . $ kj/::Networkcxx . . $ kj/Network#> inner) |
| 633 |
: tlscxx . . $ kj/TlsNetwork#tls.(tls), innercxx . . $ kj/TlsNetwork#inner.(*cxx . . $ kj/Own#`operator*`(ed53f71ddbc8dcc3).inner), ownInnercxx . . $ kj/TlsNetwork#ownInner.(kjcxx . . $ kj/::mvcxx . . $ kj/mv(9768e8ef939d477).(inner)) {} |
| 634 |
|
| 635 |
Promisecxx . . $ kj/Promise#<Owncxx . . $ kj/Own#<NetworkAddresscxx . . $ kj/NetworkAddress#>> parseAddresscxx . . $ kj/TlsNetwork#parseAddress(68498fd2efe36710).(StringPtrcxx . . $ kj/StringPtr# addr, uintcxx . . $ kj/uint# portHint) override { |
| 636 |
// We want to parse the hostname or IP address out of `addr`. This is a bit complicated as |
| 637 |
// KJ's default network implementation has a fairly featureful grammar for these things. |
| 638 |
// In particular, we cannot just split on ':' because the address might be IPv6. |
| 639 |
|
| 640 |
kjcxx . . $ kj/::Stringcxx . . $ kj/String# hostnamecxx . . $ kj/String#String(49f6e7a06ebc5aa8).; |
| 641 |
|
| 642 |
if (addr.startsWithcxx . . $ kj/StringPtr#startsWith(92cba9538efd7e94).("["cxx . . $ kj/StringPtr#StringPtr(85c52e162fed56f9).)) { |
| 643 |
// IPv6, like "[1234:5678::abcd]:123". Take the part between the brackets. |
| 644 |
KJ_IF_MAYBEcxx . . $ `src/kj/common.h:1325:9`!(pos, addr.findFirst(']')) { |
| 645 |
hostname =cxx . . $ kj/String#`operator=`(d2fb16dfceba2c4e). kjcxx . . $ kj/::strcxx . . $ kj/str(d4f75be60228bbc5).(addr.slicecxx . . $ kj/StringPtr#slice(b094122d636adbe9).(1, *cxx . . $ kj/_/NullableValue#`operator*`(db8caaf2f996d67e).pos)); |
| 646 |
} else { |
| 647 |
// Uhh??? Just take the whole thing, cert will fail later. |
| 648 |
hostname =cxx . . $ kj/String#`operator=`(d2fb16dfceba2c4e). kjcxx . . $ kj/::heapStringcxx . . $ kj/heapString(c0c66c566dc4c708).(addr); |
| 649 |
} |
| 650 |
} else if (addr.startsWithcxx . . $ kj/StringPtr#startsWith(92cba9538efd7e94).("unix:"cxx . . $ kj/StringPtr#StringPtr(85c52e162fed56f9).) || addr.startsWithcxx . . $ kj/StringPtr#startsWith(92cba9538efd7e94).("unix-abstract:"cxx . . $ kj/StringPtr#StringPtr(85c52e162fed56f9).)) { |
| 651 |
// Unfortunately, `unix:123` is ambiguous (maybe there is a host named "unix"?), but the |
| 652 |
// default KJ network implementation will interpret it as a Unix domain socket address. |
| 653 |
// We don't want TLS to then try to authenticate that as a host named "unix". |
| 654 |
KJ_FAIL_REQUIREcxx . . $ `src/kj/debug.h:232:9`!("can't authenticate Unix domain socket with TLS", addr); |
| 655 |
} else { |
| 656 |
uintcxx . . $ kj/uint# colons = 0; |
| 657 |
for (auto c: addr) { |
| 658 |
if (c == ':') { |
| 659 |
++colons; |
| 660 |
} |
| 661 |
} |
| 662 |
|
| 663 |
if (colons >= 2) { |
| 664 |
// Must be an IPv6 address. If it had a port, it would have been wrapped in []. So don't |
| 665 |
// strip the port. |
| 666 |
hostname =cxx . . $ kj/String#`operator=`(d2fb16dfceba2c4e). kjcxx . . $ kj/::heapStringcxx . . $ kj/heapString(c0c66c566dc4c708).(addr); |
| 667 |
} else { |
| 668 |
// Assume host:port or ipv4:port. This is a shaky assumption, as the above hacks |
| 669 |
// demonstrate. |
| 670 |
// |
| 671 |
// In theory it might make sense to extend the NetworkAddress interface so that it can tell |
| 672 |
// us what the actual parser decided the hostname is. However, when I tried this it proved |
| 673 |
// rather cumbersome and actually broke code in the Workers Runtime that does complicated |
| 674 |
// stacking of kj::Network implementations. |
| 675 |
KJ_IF_MAYBEcxx . . $ `src/kj/common.h:1325:9`!(pos, addr.findFirst(':')) { |
| 676 |
hostname =cxx . . $ kj/String#`operator=`(d2fb16dfceba2c4e). kjcxx . . $ kj/::heapStringcxx . . $ kj/heapString(a5a6cc00795c9446).(addr.slicecxx . . $ kj/StringPtr#slice(b094122d636adbe9).(0, *cxx . . $ kj/_/NullableValue#`operator*`(db8caaf2f996d67e).pos)); |
| 677 |
} else { |
| 678 |
hostname =cxx . . $ kj/String#`operator=`(d2fb16dfceba2c4e). kjcxx . . $ kj/::heapStringcxx . . $ kj/heapString(c0c66c566dc4c708).(addr); |
| 679 |
} |
| 680 |
} |
| 681 |
} |
| 682 |
|
| 683 |
return innercxx . . $ kj/TlsNetwork#inner..parseAddresscxx . . $ kj/Network#parseAddress(68498fd2efe36710).(addr, portHint) |
| 684 |
.thencxx . . $ kj/Promise#then(fd6a6beaa0df29f).([this, hostname=kjcxx . . $ kj/::mvcxx . . $ kj/mv(9768e8ef939d477).(hostname)](kjcxx . . $ kj/::Owncxx . . $ kj/Own#<NetworkAddresscxx . . $ kj/NetworkAddress#>&& addr) mutable |
| 685 |
-> kjcxx . . $ kj/::Owncxx . . $ kj/Own#<kjcxx . . $ kj/::NetworkAddresscxx . . $ kj/NetworkAddress#> { |
| 686 |
return kjcxx . . $ kj/Own#Own(97d08cb47632509f).::heapcxx . . $ kj/heap(bae2fec6325dc41a).<TlsNetworkAddresscxx . . $ kj/TlsNetworkAddress#>(tlscxx . . $ kj/TlsNetwork#tls., kjcxx . . $ kj/::mvcxx . . $ kj/mv(9768e8ef939d477).(hostname), kjcxx . . $ kj/::mvcxx . . $ kj/mv(9768e8ef939d477).(addr)); |
| 687 |
}); |
| 688 |
} |
| 689 |
|
| 690 |
Owncxx . . $ kj/Own#<NetworkAddresscxx . . $ kj/NetworkAddress#> getSockaddrcxx . . $ kj/TlsNetwork#getSockaddr(f85705051c06e7a).(const void* sockaddr, uintcxx . . $ kj/uint# len) override { |
| 691 |
KJ_UNIMPLEMENTEDcxx . . $ `src/kj/debug.h:273:9`!("TLS does not implement getSockaddr() because it needs to know hostnames"); |
| 692 |
} |
| 693 |
|
| 694 |
Owncxx . . $ kj/Own#<Networkcxx . . $ kj/Network#> restrictPeerscxx . . $ kj/TlsNetwork#restrictPeers(90dcbb10fa1ad74).( |
| 695 |
kjcxx . . $ kj/::ArrayPtrcxx . . $ kj/ArrayPtr#<const kjcxx . . $ kj/::StringPtrcxx . . $ kj/StringPtr#> allow, |
| 696 |
kjcxx . . $ kj/::ArrayPtrcxx . . $ kj/ArrayPtr#<const kjcxx . . $ kj/::StringPtrcxx . . $ kj/StringPtr#> deny = nullptrcxx . . $ kj/ArrayPtr#ArrayPtr(49b4c06ce5d54a60).) override { |
| 697 |
// TODO(someday): Maybe we could implement the ability to specify CA or hostname restrictions? |
| 698 |
// Or is it better to let people do that via the TlsContext? A neat thing about |
| 699 |
// restrictPeers() is that it's easy to make user-configurable. |
| 700 |
return kjcxx . . $ kj/Own#Own(97d08cb47632509f).::heapcxx . . $ kj/heap(bae2fec6325dc41a).<TlsNetworkcxx . . $ kj/TlsNetwork#>(tlscxx . . $ kj/TlsNetwork#tls., innercxx . . $ kj/TlsNetwork#inner..restrictPeerscxx . . $ kj/Network#restrictPeers(90dcbb10fa1ad74).(allow, deny)); |
| 701 |
} |
| 702 |
|
| 703 |
private: |
| 704 |
TlsContextcxx . . $ kj/TlsContext#& tlscxx . . $ kj/TlsNetwork#tls.; |
| 705 |
kjcxx . . $ kj/::Networkcxx . . $ kj/Network#& innercxx . . $ kj/TlsNetwork#inner.; |
| 706 |
kjcxx . . $ kj/::Owncxx . . $ kj/Own#<kjcxx . . $ kj/::Networkcxx . . $ kj/Network#> ownInnercxx . . $ kj/TlsNetwork#ownInner.; |
| 707 |
}; |
| 708 |
|
| 709 |
// ======================================================================================= |
| 710 |
// class TlsContext |
| 711 |
|
| 712 |
TlsContextcxx . . $ kj/TlsContext#::Optionscxx . . $ kj/TlsContext#Options#::Optionscxx . . $ kj/TlsContext#Options#Options(49f6e7a06ebc5aa8).() |
| 713 |
: useSystemTrustStorecxx . . $ kj/TlsContext#Options#useSystemTrustStore.(true), |
| 714 |
verifyClientscxx . . $ kj/TlsContext#Options#verifyClients.(false), |
| 715 |
minVersioncxx . . $ kj/TlsContext#Options#minVersion.(TlsVersioncxx . . $ kj/TlsVersion#::TLS_1_2cxx . . $ kj/TlsVersion#TLS_1_2.), |
| 716 |
cipherListcxx . . $ kj/TlsContext#Options#cipherList.("ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305") {} |
| 717 |
// Cipher list is Mozilla's "intermediate" list, except with classic DH removed since we don't |
| 718 |
// currently support setting dhparams. See: |
| 719 |
// https://mozilla.github.io/server-side-tls/ssl-config-generator/ |
| 720 |
// |
| 721 |
// Classic DH is arguably obsolete and will only become more so as time passes, so perhaps we'll |
| 722 |
// never bother. |
| 723 |
|
| 724 |
struct TlsContextcxx . . $ kj/TlsContext#::SniCallbackcxx . . $ kj/TlsContext#SniCallback# { |
| 725 |
// struct SniCallback exists only so that callback() can be declared in the .c++ file, since it |
| 726 |
// references OpenSSL types. |
| 727 |
|
| 728 |
static int callbackcxx . . $ kj/TlsContext#SniCallback#callback(319e886ac3c8f843).(SSL* ssl, int* ad, void* arg); |
| 729 |
}; |
| 730 |
|
| 731 |
TlsContextcxx . . $ kj/TlsContext#::TlsContextcxx . . $ kj/TlsContext#TlsContext(30e6f473a31b2773).(Optionscxx . . $ kj/TlsContext#Options# options) { |
| 732 |
ensureOpenSslInitializedcxx . . $ kj/`$anonymous_namespace_src/kj/compat/tls.c++`/ensureOpenSslInitialized(49f6e7a06ebc5aa8).(); |
| 733 |
|
| 734 |
#if OPENSSL_VERSION_NUMBERcxx . . $ `<external>/4166512202731279593/opensslv.h:115:10`! >= 0x10100000L || defined(OPENSSL_IS_BORINGSSL) |
| 735 |
SSL_CTX* ctx = SSL_CTX_new(TLS_method()); |
| 736 |
#else |
| 737 |
SSL_CTX* ctx = SSL_CTX_new(SSLv23_method()); |
| 738 |
#endif |
| 739 |
|
| 740 |
if (ctx == nullptr) { |
| 741 |
throwOpensslErrorcxx . . $ kj/`$anonymous_namespace_src/kj/compat/tls.c++`/throwOpensslError(8df32a62017fbbf6).(); |
| 742 |
} |
| 743 |
KJ_ON_SCOPE_FAILUREcxx . . $ `src/kj/exception.h:402:9`!(SSL_CTX_free(ctx)); |
| 744 |
|
| 745 |
// honor options.useSystemTrustStore |
| 746 |
if (options.useSystemTrustStorecxx . . $ kj/TlsContext#Options#useSystemTrustStore.) { |
| 747 |
if (!SSL_CTX_set_default_verify_paths(ctx)) { |
| 748 |
throwOpensslErrorcxx . . $ kj/`$anonymous_namespace_src/kj/compat/tls.c++`/throwOpensslError(8df32a62017fbbf6).(); |
| 749 |
} |
| 750 |
} |
| 751 |
|
| 752 |
// honor options.trustedCertificates |
| 753 |
if (options.trustedCertificatescxx . . $ kj/TlsContext#Options#trustedCertificates..sizecxx . . $ kj/ArrayPtr#size(fe4d2d262d4164a8).() > 0) { |
| 754 |
X509_STORE* store = SSL_CTX_get_cert_store(ctx); |
| 755 |
if (store == nullptr) { |
| 756 |
throwOpensslErrorcxx . . $ kj/`$anonymous_namespace_src/kj/compat/tls.c++`/throwOpensslError(8df32a62017fbbf6).(); |
| 757 |
} |
| 758 |
for (auto& cert: options.trustedCertificatescxx . . $ kj/TlsContext#Options#trustedCertificates.) { |
| 759 |
if (!X509_STORE_add_cert(store, reinterpret_cast<X509*>(cert.chaincxx . . $ kj/TlsCertificate#chain.[0]))) { |
| 760 |
throwOpensslErrorcxx . . $ kj/`$anonymous_namespace_src/kj/compat/tls.c++`/throwOpensslError(8df32a62017fbbf6).(); |
| 761 |
} |
| 762 |
} |
| 763 |
} |
| 764 |
|
| 765 |
if (options.verifyClientscxx . . $ kj/TlsContext#Options#verifyClients.) { |
| 766 |
SSL_CTX_set_verify(ctx, SSL_VERIFY_PEERcxx . . $ `<external>/17793290034335270529/ssl.h:1178:9`! | SSL_VERIFY_FAIL_IF_NO_PEER_CERTcxx . . $ `<external>/17793290034335270529/ssl.h:1179:9`!, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!); |
| 767 |
} |
| 768 |
|
| 769 |
// honor options.minVersion |
| 770 |
long optionFlags = 0; |
| 771 |
if (options.minVersioncxx . . $ kj/TlsContext#Options#minVersion. > TlsVersioncxx . . $ kj/TlsVersion#::SSL_3cxx . . $ kj/TlsVersion#SSL_3.) { |
| 772 |
optionFlags |= SSL_OP_NO_SSLv3cxx . . $ `<external>/17793290034335270529/ssl.h:428:9`!; |
| 773 |
} |
| 774 |
if (options.minVersioncxx . . $ kj/TlsContext#Options#minVersion. > TlsVersioncxx . . $ kj/TlsVersion#::TLS_1_0cxx . . $ kj/TlsVersion#TLS_1_0.) { |
| 775 |
optionFlags |= SSL_OP_NO_TLSv1cxx . . $ `<external>/17793290034335270529/ssl.h:429:9`!; |
| 776 |
} |
| 777 |
if (options.minVersioncxx . . $ kj/TlsContext#Options#minVersion. > TlsVersioncxx . . $ kj/TlsVersion#::TLS_1_1cxx . . $ kj/TlsVersion#TLS_1_1.) { |
| 778 |
optionFlags |= SSL_OP_NO_TLSv1_1cxx . . $ `<external>/17793290034335270529/ssl.h:431:9`!; |
| 779 |
} |
| 780 |
if (options.minVersioncxx . . $ kj/TlsContext#Options#minVersion. > TlsVersioncxx . . $ kj/TlsVersion#::TLS_1_2cxx . . $ kj/TlsVersion#TLS_1_2.) { |
| 781 |
optionFlags |= SSL_OP_NO_TLSv1_2cxx . . $ `<external>/17793290034335270529/ssl.h:430:9`!; |
| 782 |
} |
| 783 |
if (options.minVersioncxx . . $ kj/TlsContext#Options#minVersion. > TlsVersioncxx . . $ kj/TlsVersion#::TLS_1_3cxx . . $ kj/TlsVersion#TLS_1_3.) { |
| 784 |
#ifdef SSL_OP_NO_TLSv1_3cxx . . $ `<external>/17793290034335270529/ssl.h:432:9`! |
| 785 |
optionFlags |= SSL_OP_NO_TLSv1_3cxx . . $ `<external>/17793290034335270529/ssl.h:432:9`!; |
| 786 |
#else |
| 787 |
KJ_FAIL_REQUIRE("OpenSSL headers don't support TLS 1.3"); |
| 788 |
#endif |
| 789 |
} |
| 790 |
SSL_CTX_set_options(ctx, optionFlags); // note: never fails; returns new options bitmask |
| 791 |
|
| 792 |
// honor options.cipherList |
| 793 |
if (!SSL_CTX_set_cipher_list(ctx, options.cipherListcxx . . $ kj/TlsContext#Options#cipherList..cStrcxx . . $ kj/StringPtr#cStr(a9f41ea0e82d88cf).())) { |
| 794 |
throwOpensslErrorcxx . . $ kj/`$anonymous_namespace_src/kj/compat/tls.c++`/throwOpensslError(8df32a62017fbbf6).(); |
| 795 |
} |
| 796 |
|
| 797 |
// honor options.defaultKeypair |
| 798 |
KJ_IF_MAYBEcxx . . $ `src/kj/common.h:1325:9`!(kp, options.defaultKeypair) { |
| 799 |
if (!SSL_CTX_use_PrivateKey(ctx, reinterpret_cast<EVP_PKEY*>(kp->privateKeycxx . . $ kj/TlsKeypair#privateKey..pkeycxx . . $ kj/TlsPrivateKey#pkey.))) { |
| 800 |
throwOpensslErrorcxx . . $ kj/`$anonymous_namespace_src/kj/compat/tls.c++`/throwOpensslError(8df32a62017fbbf6).(); |
| 801 |
} |
| 802 |
|
| 803 |
if (!SSL_CTX_use_certificate(ctx, reinterpret_cast<X509*>(kp->certificatecxx . . $ kj/TlsKeypair#certificate..chaincxx . . $ kj/TlsCertificate#chain.[0]))) { |
| 804 |
throwOpensslErrorcxx . . $ kj/`$anonymous_namespace_src/kj/compat/tls.c++`/throwOpensslError(8df32a62017fbbf6).(); |
| 805 |
} |
| 806 |
|
| 807 |
for (size_t i = 1; i < kjcxx . . $ kj/::sizecxx . . $ kj/size(2ca297d61627c561).(kp->certificatecxx . . $ kj/TlsKeypair#certificate..chaincxx . . $ kj/TlsCertificate#chain.); i++) { |
| 808 |
X509* x509 = reinterpret_cast<X509*>(kp->certificatecxx . . $ kj/TlsKeypair#certificate..chaincxx . . $ kj/TlsCertificate#chain.[i]); |
| 809 |
if (x509 == nullptr) break; // end of chain |
| 810 |
|
| 811 |
if (!SSL_CTX_add_extra_chain_certcxx . . $ `<external>/17793290034335270529/ssl.h:1429:9`!(ctx, x509)) { |
| 812 |
throwOpensslErrorcxx . . $ kj/`$anonymous_namespace_src/kj/compat/tls.c++`/throwOpensslError(8df32a62017fbbf6).(); |
| 813 |
} |
| 814 |
|
| 815 |
// SSL_CTX_add_extra_chain_cert() does NOT up the refcount itself. |
| 816 |
X509_up_ref(x509); |
| 817 |
} |
| 818 |
} |
| 819 |
|
| 820 |
// honor options.sniCallback |
| 821 |
KJ_IF_MAYBEcxx . . $ `src/kj/common.h:1325:9`!(sni, options.sniCallback) { |
| 822 |
SSL_CTX_set_tlsext_servername_callbackcxx . . $ `<external>/8732523339078948436/tls1.h:333:9`!(ctx, &SniCallback::callback); |
| 823 |
SSL_CTX_set_tlsext_servername_argcxx . . $ `<external>/8732523339078948436/tls1.h:342:9`!(ctx, sni); |
| 824 |
} |
| 825 |
|
| 826 |
KJ_IF_MAYBEcxx . . $ `src/kj/common.h:1325:9`!(timeout, options.acceptTimeout) { |
| 827 |
this->timercxx . . $ kj/TlsContext#timer. =cxx . . $ kj/Maybe#`operator=`(58a27d4210775e5a). KJ_REQUIRE_NONNULLcxx . . $ `src/kj/debug.h:299:9`!(options.timer, |
| 828 |
"acceptTimeout option requires that a timer is also provided"); |
| 829 |
this->acceptTimeoutcxx . . $ kj/TlsContext#acceptTimeout. =cxx . . $ kj/Maybe#`operator=`(b5115863f3180186). *timeout; |
| 830 |
} |
| 831 |
|
| 832 |
this->acceptErrorHandlercxx . . $ kj/TlsContext#acceptErrorHandler. =cxx . . $ kj/Maybe#`operator=`(97cdbe1ef4a882e7). kjcxx . . $ kj/::mvcxx . . $ kj/mv(9768e8ef939d477).(options.acceptErrorHandlercxx . . $ kj/TlsContext#Options#acceptErrorHandler.); |
| 833 |
|
| 834 |
this->ctxcxx . . $ kj/TlsContext#ctx. = ctx; |
| 835 |
} |
| 836 |
|
| 837 |
int TlsContextcxx . . $ kj/TlsContext#::SniCallbackcxx . . $ kj/TlsContext#SniCallback#::callbackcxx . . $ kj/TlsContext#SniCallback#callback(319e886ac3c8f843).(SSL* ssl, int* ad, void* arg) { |
| 838 |
// The third parameter is actually type TlsSniCallback*. |
| 839 |
|
| 840 |
KJ_IF_MAYBEcxx . . $ `src/kj/common.h:1325:9`!(exception, kj::runCatchingExceptions([&]() { |
| 841 |
TlsSniCallback& sni = *reinterpret_cast<TlsSniCallback*>(arg); |
| 842 |
|
| 843 |
const char* name = SSL_get_servername(ssl, TLSEXT_NAMETYPE_host_namecxx . . $ `<external>/8732523339078948436/tls1.h:171:9`!); |
| 844 |
if (name != nullptr) { |
| 845 |
KJ_IF_MAYBEcxx . . $ `src/kj/common.h:1325:9`!(kp, sni.getKey(name)) { |
| 846 |
if (!SSL_use_PrivateKey(ssl, reinterpret_cast<EVP_PKEY*>(kp->privateKey.pkey))) { |
| 847 |
throwOpensslError(); |
| 848 |
} |
| 849 |
|
| 850 |
if (!SSL_use_certificate(ssl, reinterpret_cast<X509*>(kp->certificate.chain[0]))) { |
| 851 |
throwOpensslError(); |
| 852 |
} |
| 853 |
|
| 854 |
if (!SSL_clear_chain_certscxx . . $ `<external>/17793290034335270529/ssl.h:1477:9`!(ssl)) { |
| 855 |
throwOpensslError(); |
| 856 |
} |
| 857 |
|
| 858 |
for (size_t i = 1; i < kj::size(kp->certificate.chain); i++) { |
| 859 |
X509* x509 = reinterpret_cast<X509*>(kp->certificate.chain[i]); |
| 860 |
if (x509 == nullptr) break; // end of chain |
| 861 |
|
| 862 |
if (!SSL_add0_chain_certcxx . . $ `<external>/17793290034335270529/ssl.h:1471:9`!(ssl, x509)) { |
| 863 |
throwOpensslError(); |
| 864 |
} |
| 865 |
|
| 866 |
// SSL_add0_chain_cert() does NOT up the refcount itself. |
| 867 |
X509_up_ref(x509); |
| 868 |
} |
| 869 |
} |
| 870 |
} |
| 871 |
})) { |
| 872 |
KJ_LOGcxx . . $ `src/kj/debug.h:219:9`!(ERROR, "exception when invoking SNI callback", *exception); |
| 873 |
*ad = SSL_AD_INTERNAL_ERRORcxx . . $ `<external>/17793290034335270529/ssl.h:1243:9`!; |
| 874 |
return SSL_TLSEXT_ERR_ALERT_FATALcxx . . $ `<external>/8732523339078948436/tls1.h:339:9`!; |
| 875 |
} |
| 876 |
|
| 877 |
return SSL_TLSEXT_ERR_OKcxx . . $ `<external>/8732523339078948436/tls1.h:337:9`!; |
| 878 |
} |
| 879 |
|
| 880 |
TlsContextcxx . . $ kj/TlsContext#::~cxx . . $ kj/TlsContext#`~TlsContext`(49f6e7a06ebc5aa8).TlsContextcxx . . $ kj/TlsContext#() noexcept(false) { |
| 881 |
SSL_CTX_free(reinterpret_cast<SSL_CTX*>(ctxcxx . . $ kj/TlsContext#ctx.)); |
| 882 |
} |
| 883 |
|
| 884 |
kjcxx . . $ kj/::Promisecxx . . $ kj/Promise#<kjcxx . . $ kj/::Owncxx . . $ kj/Own#<kjcxx . . $ kj/::AsyncIoStreamcxx . . $ kj/AsyncIoStream#>> TlsContextcxx . . $ kj/TlsContext#::wrapClientcxx . . $ kj/TlsContext#wrapClient(3836d9b2584f67da).( |
| 885 |
kjcxx . . $ kj/::Owncxx . . $ kj/Own#<kjcxx . . $ kj/::AsyncIoStreamcxx . . $ kj/AsyncIoStream#> stream, kjcxx . . $ kj/::StringPtrcxx . . $ kj/StringPtr# expectedServerHostname) { |
| 886 |
auto conn = kjcxx . . $ kj/::heapcxx . . $ kj/heap(bae2fec6325dc41a).<TlsConnectioncxx . . $ kj/TlsConnection#>(kjcxx . . $ kj/::mvcxx . . $ kj/mv(9768e8ef939d477).(stream), reinterpret_cast<SSL_CTX*>(ctxcxx . . $ kj/TlsContext#ctx.)); |
| 887 |
auto promise = conn->cxx . . $ kj/Own#`operator->`(155dc7f103cdb9db).connectcxx . . $ kj/TlsConnection#connect(1d2c1f1d64173a3a).(expectedServerHostname); |
| 888 |
return promise.thencxx . . $ kj/Promise#then(fd6a6beaa0df29f).([conn=kjcxx . . $ kj/Own#Own(2757233a295510d2).::mvcxx . . $ kj/mv(9768e8ef939d477).(conn)]() mutable |
| 889 |
-> kjcxx . . $ kj/::Owncxx . . $ kj/Own#<kjcxx . . $ kj/::AsyncIoStreamcxx . . $ kj/AsyncIoStream#> { |
| 890 |
return kjcxx . . $ kj/Own#Own(97d08cb47632509f).::mvcxx . . $ kj/mv(9768e8ef939d477).(conn); |
| 891 |
}); |
| 892 |
} |
| 893 |
|
| 894 |
kjcxx . . $ kj/::Promisecxx . . $ kj/Promise#<kjcxx . . $ kj/::Owncxx . . $ kj/Own#<kjcxx . . $ kj/::AsyncIoStreamcxx . . $ kj/AsyncIoStream#>> TlsContextcxx . . $ kj/TlsContext#::wrapServercxx . . $ kj/TlsContext#wrapServer(d60cfa3a0f35930f).(kjcxx . . $ kj/::Owncxx . . $ kj/Own#<kjcxx . . $ kj/::AsyncIoStreamcxx . . $ kj/AsyncIoStream#> stream) { |
| 895 |
auto conn = kjcxx . . $ kj/::heapcxx . . $ kj/heap(bae2fec6325dc41a).<TlsConnectioncxx . . $ kj/TlsConnection#>(kjcxx . . $ kj/::mvcxx . . $ kj/mv(9768e8ef939d477).(stream), reinterpret_cast<SSL_CTX*>(ctxcxx . . $ kj/TlsContext#ctx.)); |
| 896 |
auto promise = conn->cxx . . $ kj/Own#`operator->`(155dc7f103cdb9db).acceptcxx . . $ kj/TlsConnection#accept(542ddddf9229d39a).(); |
| 897 |
KJ_IF_MAYBEcxx . . $ `src/kj/common.h:1325:9`!(timeout, acceptTimeout) { |
| 898 |
promise =cxx . . $ kj/Promise#`operator=`(83311e5543e7d716). KJ_REQUIRE_NONNULLcxx . . $ `src/kj/debug.h:299:9`!(timer).afterDelaycxx . . $ kj/Timer#afterDelay(db2a3f25f8456e84).(*timeout).thencxx . . $ kj/Promise#then(fd6a6beaa0df29f).([]() -> kjcxx . . $ kj/::Promisecxx . . $ kj/Promise#<void> { |
| 899 |
return KJ_EXCEPTIONcxx . . $ `src/kj/debug.h:311:9`!(DISCONNECTED, "timed out waiting for client during TLS handshake"); |
| 900 |
}).exclusiveJoincxx . . $ kj/Promise#exclusiveJoin(22099adea898022b).(kjcxx . . $ kj/::mvcxx . . $ kj/mv(9768e8ef939d477).(promise)); |
| 901 |
} |
| 902 |
return promise.thencxx . . $ kj/Promise#then(fd6a6beaa0df29f).([conn=kjcxx . . $ kj/Own#Own(2757233a295510d2).::mvcxx . . $ kj/mv(9768e8ef939d477).(conn)]() mutable |
| 903 |
-> kjcxx . . $ kj/::Owncxx . . $ kj/Own#<kjcxx . . $ kj/::AsyncIoStreamcxx . . $ kj/AsyncIoStream#> { |
| 904 |
return kjcxx . . $ kj/Own#Own(97d08cb47632509f).::mvcxx . . $ kj/mv(9768e8ef939d477).(conn); |
| 905 |
}); |
| 906 |
} |
| 907 |
|
| 908 |
kjcxx . . $ kj/::Promisecxx . . $ kj/Promise#<kjcxx . . $ kj/::AuthenticatedStreamcxx . . $ kj/AuthenticatedStream#> TlsContextcxx . . $ kj/TlsContext#::wrapClientcxx . . $ kj/TlsContext#wrapClient(4ecc64a3eecd33b5).( |
| 909 |
kjcxx . . $ kj/::AuthenticatedStreamcxx . . $ kj/AuthenticatedStream# stream, kjcxx . . $ kj/::StringPtrcxx . . $ kj/StringPtr# expectedServerHostname) { |
| 910 |
auto conn = kjcxx . . $ kj/::heapcxx . . $ kj/heap(bae2fec6325dc41a).<TlsConnectioncxx . . $ kj/TlsConnection#>(kjcxx . . $ kj/::mvcxx . . $ kj/mv(9768e8ef939d477).(stream.streamcxx . . $ kj/AuthenticatedStream#stream.), reinterpret_cast<SSL_CTX*>(ctxcxx . . $ kj/TlsContext#ctx.)); |
| 911 |
auto promise = conn->cxx . . $ kj/Own#`operator->`(155dc7f103cdb9db).connectcxx . . $ kj/TlsConnection#connect(1d2c1f1d64173a3a).(expectedServerHostname); |
| 912 |
return promise.thencxx . . $ kj/Promise#then(fd6a6beaa0df29f).([conn=kjcxx . . $ kj/Own#Own(2757233a295510d2).::mvcxx . . $ kj/mv(9768e8ef939d477).(conn),innerId=kjcxx . . $ kj/Own#Own(2757233a295510d2).::mvcxx . . $ kj/mv(9768e8ef939d477).(stream.peerIdentitycxx . . $ kj/AuthenticatedStream#peerIdentity.)]() mutable { |
| 913 |
auto id = conn->cxx . . $ kj/Own#`operator->`(155dc7f103cdb9db).getIdentitycxx . . $ kj/TlsConnection#getIdentity(dd9645e44839626a).(kjcxx . . $ kj/Own#Own(2757233a295510d2).::mvcxx . . $ kj/mv(9768e8ef939d477).(innerId)); |
| 914 |
return kjcxx . . $ kj/::AuthenticatedStreamcxx . . $ kj/AuthenticatedStream# { kjcxx . . $ kj/::mvcxx . . $ kj/mv(9768e8ef939d477).(conn), kjcxx . . $ kj/::mvcxx . . $ kj/mv(9768e8ef939d477).(id) }; |
| 915 |
}); |
| 916 |
} |
| 917 |
|
| 918 |
kjcxx . . $ kj/::Promisecxx . . $ kj/Promise#<kjcxx . . $ kj/::AuthenticatedStreamcxx . . $ kj/AuthenticatedStream#> TlsContextcxx . . $ kj/TlsContext#::wrapServercxx . . $ kj/TlsContext#wrapServer(23480717d04fe30b).(kjcxx . . $ kj/::AuthenticatedStreamcxx . . $ kj/AuthenticatedStream# stream) { |
| 919 |
auto conn = kjcxx . . $ kj/::heapcxx . . $ kj/heap(bae2fec6325dc41a).<TlsConnectioncxx . . $ kj/TlsConnection#>(kjcxx . . $ kj/::mvcxx . . $ kj/mv(9768e8ef939d477).(stream.streamcxx . . $ kj/AuthenticatedStream#stream.), reinterpret_cast<SSL_CTX*>(ctxcxx . . $ kj/TlsContext#ctx.)); |
| 920 |
auto promise = conn->cxx . . $ kj/Own#`operator->`(155dc7f103cdb9db).acceptcxx . . $ kj/TlsConnection#accept(542ddddf9229d39a).(); |
| 921 |
KJ_IF_MAYBEcxx . . $ `src/kj/common.h:1325:9`!(timeout, acceptTimeout) { |
| 922 |
promise =cxx . . $ kj/Promise#`operator=`(83311e5543e7d716). KJ_REQUIRE_NONNULLcxx . . $ `src/kj/debug.h:299:9`!(timer).afterDelaycxx . . $ kj/Timer#afterDelay(db2a3f25f8456e84).(*timeout).thencxx . . $ kj/Promise#then(fd6a6beaa0df29f).([]() -> kjcxx . . $ kj/::Promisecxx . . $ kj/Promise#<void> { |
| 923 |
return KJ_EXCEPTIONcxx . . $ `src/kj/debug.h:311:9`!(DISCONNECTED, "timed out waiting for client during TLS handshake"); |
| 924 |
}).exclusiveJoincxx . . $ kj/Promise#exclusiveJoin(22099adea898022b).(kjcxx . . $ kj/::mvcxx . . $ kj/mv(9768e8ef939d477).(promise)); |
| 925 |
} |
| 926 |
return promise.thencxx . . $ kj/Promise#then(fd6a6beaa0df29f).([conn=kjcxx . . $ kj/Own#Own(2757233a295510d2).::mvcxx . . $ kj/mv(9768e8ef939d477).(conn),innerId=kjcxx . . $ kj/Own#Own(2757233a295510d2).::mvcxx . . $ kj/mv(9768e8ef939d477).(stream.peerIdentitycxx . . $ kj/AuthenticatedStream#peerIdentity.)]() mutable { |
| 927 |
auto id = conn->cxx . . $ kj/Own#`operator->`(155dc7f103cdb9db).getIdentitycxx . . $ kj/TlsConnection#getIdentity(dd9645e44839626a).(kjcxx . . $ kj/Own#Own(2757233a295510d2).::mvcxx . . $ kj/mv(9768e8ef939d477).(innerId)); |
| 928 |
return kjcxx . . $ kj/::AuthenticatedStreamcxx . . $ kj/AuthenticatedStream# { kjcxx . . $ kj/::mvcxx . . $ kj/mv(9768e8ef939d477).(conn), kjcxx . . $ kj/::mvcxx . . $ kj/mv(9768e8ef939d477).(id) }; |
| 929 |
}); |
| 930 |
} |
| 931 |
|
| 932 |
kjcxx . . $ kj/::Owncxx . . $ kj/Own#<kjcxx . . $ kj/::ConnectionReceivercxx . . $ kj/ConnectionReceiver#> TlsContextcxx . . $ kj/TlsContext#::wrapPortcxx . . $ kj/TlsContext#wrapPort(d212f26a61cd2b85).(kjcxx . . $ kj/::Owncxx . . $ kj/Own#<kjcxx . . $ kj/::ConnectionReceivercxx . . $ kj/ConnectionReceiver#> port) { |
| 933 |
auto handler = acceptErrorHandlercxx . . $ kj/TlsContext#acceptErrorHandler..mapcxx . . $ kj/Maybe#map(15cd17b8f10d1038).([](TlsErrorHandlercxx . . $ kj/TlsErrorHandler#& handler) { |
| 934 |
return handler.referencecxx . . $ kj/Function#reference(b7b67ebd8991e950).(); |
| 935 |
}); |
| 936 |
return kjcxx . . $ kj/Own#Own(97d08cb47632509f).::heapcxx . . $ kj/heap(bae2fec6325dc41a).<TlsConnectionReceivercxx . . $ kj/TlsConnectionReceiver#>(*this, kjcxx . . $ kj/::mvcxx . . $ kj/mv(9768e8ef939d477).(port), kjcxx . . $ kj/::mvcxx . . $ kj/mv(9768e8ef939d477).(handler)); |
| 937 |
} |
| 938 |
|
| 939 |
kjcxx . . $ kj/::Owncxx . . $ kj/Own#<kjcxx . . $ kj/::NetworkAddresscxx . . $ kj/NetworkAddress#> TlsContextcxx . . $ kj/TlsContext#::wrapAddresscxx . . $ kj/TlsContext#wrapAddress(b64d8b40e89a0eed).( |
| 940 |
kjcxx . . $ kj/::Owncxx . . $ kj/Own#<kjcxx . . $ kj/::NetworkAddresscxx . . $ kj/NetworkAddress#> address, kjcxx . . $ kj/::StringPtrcxx . . $ kj/StringPtr# expectedServerHostname) { |
| 941 |
return kjcxx . . $ kj/Own#Own(97d08cb47632509f).::heapcxx . . $ kj/heap(bae2fec6325dc41a).<TlsNetworkAddresscxx . . $ kj/TlsNetworkAddress#>(*this, kjcxx . . $ kj/::strcxx . . $ kj/str(d4f75be60228bbc5).(expectedServerHostname), kjcxx . . $ kj/::mvcxx . . $ kj/mv(9768e8ef939d477).(address)); |
| 942 |
} |
| 943 |
|
| 944 |
kjcxx . . $ kj/::Owncxx . . $ kj/Own#<kjcxx . . $ kj/::Networkcxx . . $ kj/Network#> TlsContextcxx . . $ kj/TlsContext#::wrapNetworkcxx . . $ kj/TlsContext#wrapNetwork(45c92659aa3eeff5).(kjcxx . . $ kj/::Networkcxx . . $ kj/Network#& network) { |
| 945 |
return kjcxx . . $ kj/Own#Own(97d08cb47632509f).::heapcxx . . $ kj/heap(bae2fec6325dc41a).<TlsNetworkcxx . . $ kj/TlsNetwork#>(*this, network); |
| 946 |
} |
| 947 |
|
| 948 |
// ======================================================================================= |
| 949 |
// class TlsPrivateKey |
| 950 |
|
| 951 |
TlsPrivateKeycxx . . $ kj/TlsPrivateKey#::TlsPrivateKeycxx . . $ kj/TlsPrivateKey#TlsPrivateKey(30ede09ecd811094).(kjcxx . . $ kj/::ArrayPtrcxx . . $ kj/ArrayPtr#<const bytecxx . . $ kj/byte#> asn1) { |
| 952 |
ensureOpenSslInitializedcxx . . $ kj/`$anonymous_namespace_src/kj/compat/tls.c++`/ensureOpenSslInitialized(49f6e7a06ebc5aa8).(); |
| 953 |
|
| 954 |
const bytecxx . . $ kj/byte#* ptr = asn1.begincxx . . $ kj/ArrayPtr#begin(155dc7f103cdb9db).(); |
| 955 |
pkeycxx . . $ kj/TlsPrivateKey#pkey. = d2i_AutoPrivateKey(nullptr, &ptr, asn1.sizecxx . . $ kj/ArrayPtr#size(fe4d2d262d4164a8).()); |
| 956 |
if (pkeycxx . . $ kj/TlsPrivateKey#pkey. == nullptr) { |
| 957 |
throwOpensslErrorcxx . . $ kj/`$anonymous_namespace_src/kj/compat/tls.c++`/throwOpensslError(8df32a62017fbbf6).(); |
| 958 |
} |
| 959 |
} |
| 960 |
|
| 961 |
TlsPrivateKeycxx . . $ kj/TlsPrivateKey#::TlsPrivateKeycxx . . $ kj/TlsPrivateKey#TlsPrivateKey(f8eed0f0a0b75b80).(kjcxx . . $ kj/::StringPtrcxx . . $ kj/StringPtr# pem, kjcxx . . $ kj/::Maybecxx . . $ kj/Maybe#<kjcxx . . $ kj/::StringPtrcxx . . $ kj/StringPtr#> password) { |
| 962 |
ensureOpenSslInitializedcxx . . $ kj/`$anonymous_namespace_src/kj/compat/tls.c++`/ensureOpenSslInitialized(49f6e7a06ebc5aa8).(); |
| 963 |
|
| 964 |
// const_cast apparently needed for older versions of OpenSSL. |
| 965 |
BIO* bio = BIO_new_mem_buf(const_cast<char*>(pem.begincxx . . $ kj/StringPtr#begin(a9f41ea0e82d88cf).()), pem.sizecxx . . $ kj/StringPtr#size(fe4d2d262d4164a8).()); |
| 966 |
KJ_DEFERcxx . . $ `src/kj/common.h:2043:9`!(BIO_free(bio)); |
| 967 |
|
| 968 |
pkeycxx . . $ kj/TlsPrivateKey#pkey. = PEM_read_bio_PrivateKey(bio, nullptr, &passwordCallbackcxx . . $ kj/TlsPrivateKey#passwordCallback(6f3ee5008532c85d)., &password); |
| 969 |
if (pkeycxx . . $ kj/TlsPrivateKey#pkey. == nullptr) { |
| 970 |
throwOpensslErrorcxx . . $ kj/`$anonymous_namespace_src/kj/compat/tls.c++`/throwOpensslError(8df32a62017fbbf6).(); |
| 971 |
} |
| 972 |
} |
| 973 |
|
| 974 |
TlsPrivateKeycxx . . $ kj/TlsPrivateKey#::TlsPrivateKeycxx . . $ kj/TlsPrivateKey#TlsPrivateKey(a31f6600159b1a80).(const TlsPrivateKeycxx . . $ kj/TlsPrivateKey#& other) |
| 975 |
: pkeycxx . . $ kj/TlsPrivateKey#pkey.(other.pkeycxx . . $ kj/TlsPrivateKey#pkey.) { |
| 976 |
if (pkeycxx . . $ kj/TlsPrivateKey#pkey. != nullptr) EVP_PKEY_up_ref(reinterpret_cast<EVP_PKEY*>(pkeycxx . . $ kj/TlsPrivateKey#pkey.)); |
| 977 |
} |
| 978 |
|
| 979 |
TlsPrivateKeycxx . . $ kj/TlsPrivateKey#& TlsPrivateKeycxx . . $ kj/TlsPrivateKey#::operatorcxx . . $ kj/TlsPrivateKey#`operator=`(84c7605521ffef82).=(const TlsPrivateKeycxx . . $ kj/TlsPrivateKey#& other) { |
| 980 |
if (pkeycxx . . $ kj/TlsPrivateKey#pkey. != other.pkeycxx . . $ kj/TlsPrivateKey#pkey.) { |
| 981 |
EVP_PKEY_free(reinterpret_cast<EVP_PKEY*>(pkeycxx . . $ kj/TlsPrivateKey#pkey.)); |
| 982 |
pkeycxx . . $ kj/TlsPrivateKey#pkey. = other.pkeycxx . . $ kj/TlsPrivateKey#pkey.; |
| 983 |
if (pkeycxx . . $ kj/TlsPrivateKey#pkey. != nullptr) EVP_PKEY_up_ref(reinterpret_cast<EVP_PKEY*>(pkeycxx . . $ kj/TlsPrivateKey#pkey.)); |
| 984 |
} |
| 985 |
return *this; |
| 986 |
} |
| 987 |
|
| 988 |
TlsPrivateKeycxx . . $ kj/TlsPrivateKey#::~cxx . . $ kj/TlsPrivateKey#`~TlsPrivateKey`(49f6e7a06ebc5aa8).TlsPrivateKeycxx . . $ kj/TlsPrivateKey#() noexcept(false) { |
| 989 |
EVP_PKEY_free(reinterpret_cast<EVP_PKEY*>(pkeycxx . . $ kj/TlsPrivateKey#pkey.)); |
| 990 |
} |
| 991 |
|
| 992 |
int TlsPrivateKeycxx . . $ kj/TlsPrivateKey#::passwordCallbackcxx . . $ kj/TlsPrivateKey#passwordCallback(6f3ee5008532c85d).(char* buf, int size, int rwflag, void* u) { |
| 993 |
auto& password = *reinterpret_cast<kjcxx . . $ kj/::Maybecxx . . $ kj/Maybe#<kjcxx . . $ kj/::StringPtrcxx . . $ kj/StringPtr#>*>(u); |
| 994 |
|
| 995 |
KJ_IF_MAYBEcxx . . $ `src/kj/common.h:1325:9`!(p, password) { |
| 996 |
int result = kjcxx . . $ kj/::mincxx . . $ kj/min(4af833f8d939a8d2).(p->sizecxx . . $ kj/StringPtr#size(fe4d2d262d4164a8).(), size); |
| 997 |
memcpy(buf, p->begincxx . . $ kj/StringPtr#begin(a9f41ea0e82d88cf).(), result); |
| 998 |
return result; |
| 999 |
} else { |
| 1000 |
return 0; |
| 1001 |
} |
| 1002 |
} |
| 1003 |
|
| 1004 |
// ======================================================================================= |
| 1005 |
// class TlsCertificate |
| 1006 |
|
| 1007 |
TlsCertificatecxx . . $ kj/TlsCertificate#::TlsCertificatecxx . . $ kj/TlsCertificate#TlsCertificate(9a8c2cc5c087affd).(kjcxx . . $ kj/::ArrayPtrcxx . . $ kj/ArrayPtr#<const kjcxx . . $ kj/::ArrayPtrcxx . . $ kj/ArrayPtr#<const bytecxx . . $ kj/byte#>> asn1) { |
| 1008 |
ensureOpenSslInitializedcxx . . $ kj/`$anonymous_namespace_src/kj/compat/tls.c++`/ensureOpenSslInitialized(49f6e7a06ebc5aa8).(); |
| 1009 |
|
| 1010 |
KJ_REQUIREcxx . . $ `src/kj/debug.h:227:9`!(asn1.size() > 0, "must provide at least one certificate in chain"); |
| 1011 |
KJ_REQUIREcxx . . $ `src/kj/debug.h:227:9`!(asn1.size() <= kj::size(chain), |
| 1012 |
"exceeded maximum certificate chain length of 10"); |
| 1013 |
|
| 1014 |
memset(chaincxx . . $ kj/TlsCertificate#chain., 0, sizeof(chaincxx . . $ kj/TlsCertificate#chain.)); |
| 1015 |
|
| 1016 |
for (auto i: kjcxx . . $ kj/::indicescxx . . $ kj/indices(5511956b41a96918).(asn1)) { |
| 1017 |
auto p = asn1[cxx . . $ kj/ArrayPtr#`operator[]`(ec43b8547ac5c60).i].begincxx . . $ kj/ArrayPtr#begin(1016ee5406d91ab9).(); |
| 1018 |
|
| 1019 |
// "_AUX" apparently refers to some auxiliary information that can be appended to the |
| 1020 |
// certificate, but should only be trusted for your own certificate, not the whole chain?? |
| 1021 |
// I don't really know, I'm just cargo-culting. |
| 1022 |
chaincxx . . $ kj/TlsCertificate#chain.[i] = i == 0 ? d2i_X509_AUX(nullptr, &p, asn1[cxx . . $ kj/ArrayPtr#`operator[]`(ec43b8547ac5c60).i].sizecxx . . $ kj/ArrayPtr#size(fe4d2d262d4164a8).()) |
| 1023 |
: d2i_X509(nullptr, &p, asn1[cxx . . $ kj/ArrayPtr#`operator[]`(ec43b8547ac5c60).i].sizecxx . . $ kj/ArrayPtr#size(fe4d2d262d4164a8).()); |
| 1024 |
|
| 1025 |
if (chaincxx . . $ kj/TlsCertificate#chain.[i] == nullptr) { |
| 1026 |
for (size_t j = 0; j < i; j++) { |
| 1027 |
X509_free(reinterpret_cast<X509*>(chaincxx . . $ kj/TlsCertificate#chain.[j])); |
| 1028 |
} |
| 1029 |
throwOpensslErrorcxx . . $ kj/`$anonymous_namespace_src/kj/compat/tls.c++`/throwOpensslError(8df32a62017fbbf6).(); |
| 1030 |
} |
| 1031 |
} |
| 1032 |
} |
| 1033 |
|
| 1034 |
TlsCertificatecxx . . $ kj/TlsCertificate#::TlsCertificatecxx . . $ kj/TlsCertificate#TlsCertificate(30ede09ecd811094).(kjcxx . . $ kj/::ArrayPtrcxx . . $ kj/ArrayPtr#<const bytecxx . . $ kj/byte#> asn1) |
| 1035 |
: TlsCertificatecxx . . $ kj/TlsCertificate#TlsCertificate(9a8c2cc5c087affd).(kjcxx . . $ kj/::arrayPtrcxx . . $ kj/arrayPtr(e55e65d9587a059a).(&asn1, 1)) {} |
| 1036 |
|
| 1037 |
TlsCertificatecxx . . $ kj/TlsCertificate#::TlsCertificatecxx . . $ kj/TlsCertificate#TlsCertificate(9daf31930a91c4f1).(kjcxx . . $ kj/::StringPtrcxx . . $ kj/StringPtr# pem) { |
| 1038 |
ensureOpenSslInitializedcxx . . $ kj/`$anonymous_namespace_src/kj/compat/tls.c++`/ensureOpenSslInitialized(49f6e7a06ebc5aa8).(); |
| 1039 |
|
| 1040 |
memset(chaincxx . . $ kj/TlsCertificate#chain., 0, sizeof(chaincxx . . $ kj/TlsCertificate#chain.)); |
| 1041 |
|
| 1042 |
// const_cast apparently needed for older versions of OpenSSL. |
| 1043 |
BIO* bio = BIO_new_mem_buf(const_cast<char*>(pem.begincxx . . $ kj/StringPtr#begin(a9f41ea0e82d88cf).()), pem.sizecxx . . $ kj/StringPtr#size(fe4d2d262d4164a8).()); |
| 1044 |
KJ_DEFERcxx . . $ `src/kj/common.h:2043:9`!(BIO_free(bio)); |
| 1045 |
|
| 1046 |
for (auto i: kjcxx . . $ kj/::indicescxx . . $ kj/indices(5511956b41a96918).(chaincxx . . $ kj/TlsCertificate#chain.)) { |
| 1047 |
// "_AUX" apparently refers to some auxiliary information that can be appended to the |
| 1048 |
// certificate, but should only be trusted for your own certificate, not the whole chain?? |
| 1049 |
// I don't really know, I'm just cargo-culting. |
| 1050 |
chaincxx . . $ kj/TlsCertificate#chain.[i] = i == 0 ? PEM_read_bio_X509_AUX(bio, nullptr, nullptr, nullptr) |
| 1051 |
: PEM_read_bio_X509(bio, nullptr, nullptr, nullptr); |
| 1052 |
|
| 1053 |
if (chaincxx . . $ kj/TlsCertificate#chain.[i] == nullptr) { |
| 1054 |
auto error = ERR_peek_last_error(); |
| 1055 |
if (i > 0 && ERR_GET_LIB(error) == ERR_LIB_PEMcxx . . $ `<external>/10274939680011219511/err.h:81:9`! && |
| 1056 |
ERR_GET_REASON(error) == PEM_R_NO_START_LINEcxx . . $ `<external>/4786226268131619264/pemerr.h:44:9`!) { |
| 1057 |
// EOF; we're done. |
| 1058 |
ERR_clear_error(); |
| 1059 |
return; |
| 1060 |
} else { |
| 1061 |
for (size_t j = 0; j < i; j++) { |
| 1062 |
X509_free(reinterpret_cast<X509*>(chaincxx . . $ kj/TlsCertificate#chain.[j])); |
| 1063 |
} |
| 1064 |
throwOpensslErrorcxx . . $ kj/`$anonymous_namespace_src/kj/compat/tls.c++`/throwOpensslError(8df32a62017fbbf6).(); |
| 1065 |
} |
| 1066 |
} |
| 1067 |
} |
| 1068 |
|
| 1069 |
// We reached the chain length limit. Try to read one more to verify that the chain ends here. |
| 1070 |
X509* dummy = PEM_read_bio_X509(bio, nullptr, nullptr, nullptr); |
| 1071 |
if (dummy != nullptr) { |
| 1072 |
X509_free(dummy); |
| 1073 |
for (auto i: kjcxx . . $ kj/::indicescxx . . $ kj/indices(5511956b41a96918).(chaincxx . . $ kj/TlsCertificate#chain.)) { |
| 1074 |
X509_free(reinterpret_cast<X509*>(chaincxx . . $ kj/TlsCertificate#chain.[i])); |
| 1075 |
} |
| 1076 |
KJ_FAIL_REQUIREcxx . . $ `src/kj/debug.h:232:9`!("exceeded maximum certificate chain length of 10"); |
| 1077 |
} |
| 1078 |
} |
| 1079 |
|
| 1080 |
TlsCertificatecxx . . $ kj/TlsCertificate#::TlsCertificatecxx . . $ kj/TlsCertificate#TlsCertificate(6a9479aa16c5ebdc).(const TlsCertificatecxx . . $ kj/TlsCertificate#& other) { |
| 1081 |
memcpy(chaincxx . . $ kj/TlsCertificate#chain., other.chaincxx . . $ kj/TlsCertificate#chain., sizeof(chaincxx . . $ kj/TlsCertificate#chain.)); |
| 1082 |
for (void* p: chaincxx . . $ kj/TlsCertificate#chain.) { |
| 1083 |
if (p == nullptr) break; // end of chain; quit early |
| 1084 |
X509_up_ref(reinterpret_cast<X509*>(p)); |
| 1085 |
} |
| 1086 |
} |
| 1087 |
|
| 1088 |
TlsCertificatecxx . . $ kj/TlsCertificate#& TlsCertificatecxx . . $ kj/TlsCertificate#::operatorcxx . . $ kj/TlsCertificate#`operator=`(542484d95a8e984).=(const TlsCertificatecxx . . $ kj/TlsCertificate#& other) { |
| 1089 |
for (auto i: kjcxx . . $ kj/::indicescxx . . $ kj/indices(5511956b41a96918).(chaincxx . . $ kj/TlsCertificate#chain.)) { |
| 1090 |
if (chaincxx . . $ kj/TlsCertificate#chain.[i] != other.chaincxx . . $ kj/TlsCertificate#chain.[i]) { |
| 1091 |
EVP_PKEY_free(reinterpret_cast<EVP_PKEY*>(chaincxx . . $ kj/TlsCertificate#chain.[i])); |
| 1092 |
chaincxx . . $ kj/TlsCertificate#chain.[i] = other.chaincxx . . $ kj/TlsCertificate#chain.[i]; |
| 1093 |
if (chaincxx . . $ kj/TlsCertificate#chain.[i] != nullptr) X509_up_ref(reinterpret_cast<X509*>(chaincxx . . $ kj/TlsCertificate#chain.[i])); |
| 1094 |
} else if (chaincxx . . $ kj/TlsCertificate#chain.[i] == nullptr) { |
| 1095 |
// end of both chains; quit early |
| 1096 |
break; |
| 1097 |
} |
| 1098 |
} |
| 1099 |
return *this; |
| 1100 |
} |
| 1101 |
|
| 1102 |
TlsCertificatecxx . . $ kj/TlsCertificate#::~cxx . . $ kj/TlsCertificate#`~TlsCertificate`(49f6e7a06ebc5aa8).TlsCertificatecxx . . $ kj/TlsCertificate#() noexcept(false) { |
| 1103 |
for (void* p: chaincxx . . $ kj/TlsCertificate#chain.) { |
| 1104 |
if (p == nullptr) break; // end of chain; quit early |
| 1105 |
X509_free(reinterpret_cast<X509*>(p)); |
| 1106 |
} |
| 1107 |
} |
| 1108 |
|
| 1109 |
// ======================================================================================= |
| 1110 |
// class TlsPeerIdentity |
| 1111 |
|
| 1112 |
TlsPeerIdentitycxx . . $ kj/TlsPeerIdentity#::~cxx . . $ kj/TlsPeerIdentity#`~TlsPeerIdentity`(49f6e7a06ebc5aa8).TlsPeerIdentitycxx . . $ kj/TlsPeerIdentity#() noexcept(false) { |
| 1113 |
if (certcxx . . $ kj/TlsPeerIdentity#cert. != nullptr) { |
| 1114 |
X509_free(reinterpret_cast<X509*>(certcxx . . $ kj/TlsPeerIdentity#cert.)); |
| 1115 |
} |
| 1116 |
} |
| 1117 |
|
| 1118 |
kjcxx . . $ kj/::Stringcxx . . $ kj/String# TlsPeerIdentitycxx . . $ kj/TlsPeerIdentity#::toStringcxx . . $ kj/TlsPeerIdentity#toString(af22d04b00c82e5d).() { |
| 1119 |
if (hasCertificatecxx . . $ kj/TlsPeerIdentity#hasCertificate(c9857a73d68e59ba).()) { |
| 1120 |
return getCommonNamecxx . . $ kj/TlsPeerIdentity#getCommonName(af22d04b00c82e5d).(); |
| 1121 |
} else { |
| 1122 |
return kjcxx . . $ kj/::strcxx . . $ kj/str(d4f75be60228bbc5).("(anonymous client)"); |
| 1123 |
} |
| 1124 |
} |
| 1125 |
|
| 1126 |
kjcxx . . $ kj/::Stringcxx . . $ kj/String# TlsPeerIdentitycxx . . $ kj/TlsPeerIdentity#::getCommonNamecxx . . $ kj/TlsPeerIdentity#getCommonName(af22d04b00c82e5d).() { |
| 1127 |
if (certcxx . . $ kj/TlsPeerIdentity#cert. == nullptr) { |
| 1128 |
KJ_FAIL_REQUIREcxx . . $ `src/kj/debug.h:232:9`!("client did not provide a certificate") { return nullptrcxx . . $ kj/String#String(49b4c06ce5d54a60).; } |
| 1129 |
} |
| 1130 |
|
| •1131 |
const X509_NAME* subj = X509_get_subject_name(reinterpret_cast<X509*>(certcxx . . $ kj/TlsPeerIdentity#cert.)); |
| 1132 |
|
| 1133 |
int index = X509_NAME_get_index_by_NID(subj, NID_commonNamecxx . . $ `<external>/18030087176251566933/obj_mac.h:2403:9`!, -1); |
| 1134 |
KJ_ASSERTcxx . . $ `src/kj/debug.h:359:9`!(index != -1, "certificate has no common name?"); |
| •1135 |
const X509_NAME_ENTRY* entry = X509_NAME_get_entry(subj, index); |
| 1136 |
KJ_ASSERTcxx . . $ `src/kj/debug.h:359:9`!(entry != nullptr); |
| •1137 |
const ASN1_STRING* data = X509_NAME_ENTRY_get_data(entry); |
| 1138 |
KJ_ASSERTcxx . . $ `src/kj/debug.h:359:9`!(data != nullptr); |
| 1139 |
|
| 1140 |
unsigned char* out = nullptr; |
| 1141 |
int len = ASN1_STRING_to_UTF8(&out, data); |
| 1142 |
KJ_ASSERTcxx . . $ `src/kj/debug.h:359:9`!(len >= 0); |
| 1143 |
KJ_DEFERcxx . . $ `src/kj/common.h:2043:9`!(OPENSSL_freecxx . . $ `<external>/8711174516434588327/crypto.h:130:9`!(out)); |
| 1144 |
|
| 1145 |
return kjcxx . . $ kj/::heapStringcxx . . $ kj/heapString(adf6f9c85251b1e5).(reinterpret_cast<char*>(out), len); |
| 1146 |
} |
| 1147 |
|
| 1148 |
} // namespace kj |
| 1149 |
|
| 1150 |
#endif // KJ_HAS_OPENSSL |