2018-08-15 14:41:42 +02:00
|
|
|
//
|
2022-12-31 22:28:08 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2023
|
2018-08-15 14:41:42 +02:00
|
|
|
//
|
|
|
|
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
|
|
|
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
|
|
|
//
|
|
|
|
#include "td/net/SslStream.h"
|
|
|
|
|
2019-06-28 13:03:06 +02:00
|
|
|
#if !TD_EMSCRIPTEN
|
2019-02-12 22:26:36 +01:00
|
|
|
#include "td/utils/common.h"
|
2020-06-06 20:31:47 +02:00
|
|
|
#include "td/utils/crypto.h"
|
2018-08-15 14:41:42 +02:00
|
|
|
#include "td/utils/logging.h"
|
2018-08-16 00:06:53 +02:00
|
|
|
#include "td/utils/misc.h"
|
2020-05-16 16:50:58 +02:00
|
|
|
#include "td/utils/port/IPAddress.h"
|
2018-08-15 15:29:46 +02:00
|
|
|
#include "td/utils/Status.h"
|
2018-08-15 14:41:42 +02:00
|
|
|
#include "td/utils/Time.h"
|
|
|
|
|
2022-11-11 12:23:43 +01:00
|
|
|
#include <openssl/bio.h>
|
2018-08-15 14:41:42 +02:00
|
|
|
#include <openssl/err.h>
|
|
|
|
#include <openssl/ssl.h>
|
2018-08-15 22:05:06 +02:00
|
|
|
#include <openssl/x509.h>
|
2018-08-15 14:41:42 +02:00
|
|
|
|
2018-08-16 00:06:53 +02:00
|
|
|
#include <cstring>
|
2022-11-23 14:32:09 +01:00
|
|
|
#include <memory>
|
2018-08-15 22:05:06 +02:00
|
|
|
|
2018-08-15 14:41:42 +02:00
|
|
|
namespace td {
|
|
|
|
|
|
|
|
namespace detail {
|
|
|
|
namespace {
|
|
|
|
#if OPENSSL_VERSION_NUMBER < 0x10100000L
|
2018-08-15 15:29:46 +02:00
|
|
|
void *BIO_get_data(BIO *b) {
|
2018-08-15 14:41:42 +02:00
|
|
|
return b->ptr;
|
|
|
|
}
|
2018-08-15 15:29:46 +02:00
|
|
|
void BIO_set_data(BIO *b, void *ptr) {
|
2018-08-15 14:41:42 +02:00
|
|
|
b->ptr = ptr;
|
|
|
|
}
|
2018-08-15 15:29:46 +02:00
|
|
|
void BIO_set_init(BIO *b, int init) {
|
2018-08-15 14:41:42 +02:00
|
|
|
b->init = init;
|
|
|
|
}
|
|
|
|
|
|
|
|
int BIO_get_new_index() {
|
|
|
|
return 0;
|
|
|
|
}
|
2018-08-15 15:29:46 +02:00
|
|
|
BIO_METHOD *BIO_meth_new(int type, const char *name) {
|
2018-08-15 14:41:42 +02:00
|
|
|
auto res = new BIO_METHOD();
|
2018-08-16 00:06:53 +02:00
|
|
|
std::memset(res, 0, sizeof(*res));
|
2018-08-15 14:41:42 +02:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2018-08-15 15:29:46 +02:00
|
|
|
int BIO_meth_set_write(BIO_METHOD *biom, int (*bwrite)(BIO *, const char *, int)) {
|
2018-08-15 14:41:42 +02:00
|
|
|
biom->bwrite = bwrite;
|
|
|
|
return 1;
|
|
|
|
}
|
2018-08-15 15:29:46 +02:00
|
|
|
int BIO_meth_set_read(BIO_METHOD *biom, int (*bread)(BIO *, char *, int)) {
|
2018-08-15 14:41:42 +02:00
|
|
|
biom->bread = bread;
|
|
|
|
return 1;
|
|
|
|
}
|
2018-08-15 15:29:46 +02:00
|
|
|
int BIO_meth_set_ctrl(BIO_METHOD *biom, long (*ctrl)(BIO *, int, long, void *)) {
|
2018-08-15 14:41:42 +02:00
|
|
|
biom->ctrl = ctrl;
|
|
|
|
return 1;
|
|
|
|
}
|
2018-08-15 15:29:46 +02:00
|
|
|
int BIO_meth_set_create(BIO_METHOD *biom, int (*create)(BIO *)) {
|
2018-08-15 14:41:42 +02:00
|
|
|
biom->create = create;
|
|
|
|
return 1;
|
|
|
|
}
|
2018-08-15 15:29:46 +02:00
|
|
|
int BIO_meth_set_destroy(BIO_METHOD *biom, int (*destroy)(BIO *)) {
|
2018-08-15 14:41:42 +02:00
|
|
|
biom->destroy = destroy;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2018-08-15 15:29:46 +02:00
|
|
|
int strm_create(BIO *b) {
|
2018-08-15 14:41:42 +02:00
|
|
|
BIO_set_init(b, 1);
|
|
|
|
return 1;
|
|
|
|
}
|
2018-08-15 22:05:06 +02:00
|
|
|
|
2018-08-15 15:29:46 +02:00
|
|
|
int strm_destroy(BIO *b) {
|
2018-08-15 14:41:42 +02:00
|
|
|
return 1;
|
|
|
|
}
|
2018-08-15 22:05:06 +02:00
|
|
|
|
2018-08-15 15:29:46 +02:00
|
|
|
int strm_read(BIO *b, char *buf, int len);
|
2018-08-15 22:05:06 +02:00
|
|
|
|
2018-08-15 15:29:46 +02:00
|
|
|
int strm_write(BIO *b, const char *buf, int len);
|
2018-08-15 14:41:42 +02:00
|
|
|
|
2018-08-15 15:29:46 +02:00
|
|
|
long strm_ctrl(BIO *b, int cmd, long num, void *ptr) {
|
2018-08-15 14:41:42 +02:00
|
|
|
switch (cmd) {
|
|
|
|
case BIO_CTRL_FLUSH:
|
|
|
|
return 1;
|
|
|
|
case BIO_CTRL_PUSH:
|
|
|
|
case BIO_CTRL_POP:
|
|
|
|
return 0;
|
2023-02-04 09:00:04 +01:00
|
|
|
#if OPENSSL_VERSION_NUMBER >= 0x30000000L && !defined(LIBRESSL_VERSION_NUMBER)
|
2022-07-13 11:01:41 +02:00
|
|
|
case BIO_CTRL_GET_KTLS_SEND:
|
|
|
|
case BIO_CTRL_GET_KTLS_RECV:
|
|
|
|
return 0;
|
|
|
|
#endif
|
2018-08-15 14:41:42 +02:00
|
|
|
default:
|
|
|
|
LOG(FATAL) << b << " " << cmd << " " << num << " " << ptr;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2018-08-15 15:29:46 +02:00
|
|
|
BIO_METHOD *BIO_s_sslstream() {
|
|
|
|
static BIO_METHOD *result = [] {
|
|
|
|
BIO_METHOD *res = BIO_meth_new(BIO_get_new_index(), "td::SslStream helper bio");
|
2018-08-15 14:41:42 +02:00
|
|
|
BIO_meth_set_write(res, strm_write);
|
|
|
|
BIO_meth_set_read(res, strm_read);
|
|
|
|
BIO_meth_set_create(res, strm_create);
|
|
|
|
BIO_meth_set_destroy(res, strm_destroy);
|
|
|
|
BIO_meth_set_ctrl(res, strm_ctrl);
|
|
|
|
return res;
|
|
|
|
}();
|
2018-08-15 15:29:46 +02:00
|
|
|
return result;
|
2018-08-15 14:41:42 +02:00
|
|
|
}
|
2018-08-15 22:05:06 +02:00
|
|
|
|
2020-07-02 17:01:23 +02:00
|
|
|
struct SslHandleDeleter {
|
|
|
|
void operator()(SSL *ssl_handle) {
|
2022-09-15 14:03:19 +02:00
|
|
|
auto start_time = Time::now();
|
2020-07-05 01:57:36 +02:00
|
|
|
if (SSL_is_init_finished(ssl_handle)) {
|
|
|
|
clear_openssl_errors("Before SSL_shutdown");
|
|
|
|
SSL_set_quiet_shutdown(ssl_handle, 1);
|
|
|
|
SSL_shutdown(ssl_handle);
|
|
|
|
clear_openssl_errors("After SSL_shutdown");
|
2018-08-15 14:41:42 +02:00
|
|
|
}
|
2020-07-02 17:01:23 +02:00
|
|
|
SSL_free(ssl_handle);
|
2022-09-15 14:03:19 +02:00
|
|
|
auto elapsed_time = Time::now() - start_time;
|
2022-09-23 21:29:18 +02:00
|
|
|
if (elapsed_time >= 0.1) {
|
2023-07-22 13:06:57 +02:00
|
|
|
LOG(WARNING) << "SSL_free took " << elapsed_time << " seconds";
|
2022-09-15 14:03:19 +02:00
|
|
|
}
|
2018-08-15 14:41:42 +02:00
|
|
|
}
|
2020-07-02 17:01:23 +02:00
|
|
|
};
|
2018-08-15 14:41:42 +02:00
|
|
|
|
2020-07-02 17:01:23 +02:00
|
|
|
using SslHandle = std::unique_ptr<SSL, SslHandleDeleter>;
|
|
|
|
|
|
|
|
} // namespace
|
2018-08-15 14:41:42 +02:00
|
|
|
|
2020-07-02 17:01:23 +02:00
|
|
|
class SslStreamImpl {
|
|
|
|
public:
|
2022-11-11 12:51:22 +01:00
|
|
|
Status init(CSlice host, SslCtx ssl_ctx, bool check_ip_address_as_host) {
|
|
|
|
if (!ssl_ctx) {
|
|
|
|
return Status::Error("Invalid SSL context provided");
|
|
|
|
}
|
2020-07-02 17:01:23 +02:00
|
|
|
|
|
|
|
clear_openssl_errors("Before SslFd::init");
|
|
|
|
|
2022-11-11 12:23:43 +01:00
|
|
|
auto ssl_handle = SslHandle(SSL_new(static_cast<SSL_CTX *>(ssl_ctx.get_openssl_ctx())));
|
2020-07-02 17:01:23 +02:00
|
|
|
if (!ssl_handle) {
|
2018-08-15 14:41:42 +02:00
|
|
|
return create_openssl_error(-13, "Failed to create an SSL handle");
|
|
|
|
}
|
|
|
|
|
2020-05-16 16:50:58 +02:00
|
|
|
auto r_ip_address = IPAddress::get_ip_address(host);
|
|
|
|
|
2018-08-15 14:41:42 +02:00
|
|
|
#if OPENSSL_VERSION_NUMBER >= 0x10002000L
|
2020-07-02 17:01:23 +02:00
|
|
|
X509_VERIFY_PARAM *param = SSL_get0_param(ssl_handle.get());
|
2018-08-15 14:41:42 +02:00
|
|
|
X509_VERIFY_PARAM_set_hostflags(param, 0);
|
2020-09-14 02:58:57 +02:00
|
|
|
if (r_ip_address.is_ok() && !check_ip_address_as_host) {
|
2020-05-16 21:53:19 +02:00
|
|
|
LOG(DEBUG) << "Set verification IP address to " << r_ip_address.ok().get_ip_str();
|
2020-05-16 17:43:49 +02:00
|
|
|
X509_VERIFY_PARAM_set1_ip_asc(param, r_ip_address.ok().get_ip_str().c_str());
|
|
|
|
} else {
|
2020-05-16 21:53:19 +02:00
|
|
|
LOG(DEBUG) << "Set verification host to " << host;
|
2020-05-16 17:43:49 +02:00
|
|
|
X509_VERIFY_PARAM_set1_host(param, host.c_str(), 0);
|
|
|
|
}
|
2018-08-15 14:41:42 +02:00
|
|
|
#else
|
|
|
|
#warning DANGEROUS! HTTPS HOST WILL NOT BE CHECKED. INSTALL OPENSSL >= 1.0.2 OR IMPLEMENT HTTPS HOST CHECK MANUALLY
|
|
|
|
#endif
|
|
|
|
|
2018-08-15 15:29:46 +02:00
|
|
|
auto *bio = BIO_new(BIO_s_sslstream());
|
2018-08-16 00:06:53 +02:00
|
|
|
BIO_set_data(bio, static_cast<void *>(this));
|
2020-07-02 17:01:23 +02:00
|
|
|
SSL_set_bio(ssl_handle.get(), bio, bio);
|
2018-08-15 14:41:42 +02:00
|
|
|
|
|
|
|
#if OPENSSL_VERSION_NUMBER >= 0x0090806fL && !defined(OPENSSL_NO_TLSEXT)
|
2020-05-16 16:50:58 +02:00
|
|
|
if (r_ip_address.is_error()) { // IP address must not be send as SNI
|
2020-05-16 21:53:19 +02:00
|
|
|
LOG(DEBUG) << "Set SNI host name to " << host;
|
2020-05-16 16:50:58 +02:00
|
|
|
auto host_str = host.str();
|
2020-07-02 17:01:23 +02:00
|
|
|
SSL_set_tlsext_host_name(ssl_handle.get(), MutableCSlice(host_str).begin());
|
2020-05-16 16:50:58 +02:00
|
|
|
}
|
2018-08-15 14:41:42 +02:00
|
|
|
#endif
|
2020-07-02 17:01:23 +02:00
|
|
|
SSL_set_connect_state(ssl_handle.get());
|
2018-08-15 14:41:42 +02:00
|
|
|
|
2020-07-02 17:01:23 +02:00
|
|
|
ssl_handle_ = std::move(ssl_handle);
|
2018-08-15 14:41:42 +02:00
|
|
|
|
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
|
2018-08-15 15:29:46 +02:00
|
|
|
ByteFlowInterface &read_byte_flow() {
|
2018-08-15 14:41:42 +02:00
|
|
|
return read_flow_;
|
|
|
|
}
|
2018-08-15 15:29:46 +02:00
|
|
|
ByteFlowInterface &write_byte_flow() {
|
2018-08-15 14:41:42 +02:00
|
|
|
return write_flow_;
|
|
|
|
}
|
|
|
|
size_t flow_read(MutableSlice slice) {
|
|
|
|
return read_flow_.read(slice);
|
|
|
|
}
|
|
|
|
size_t flow_write(Slice slice) {
|
|
|
|
return write_flow_.write(slice);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2020-07-02 17:01:23 +02:00
|
|
|
SslHandle ssl_handle_;
|
2018-08-15 14:41:42 +02:00
|
|
|
|
|
|
|
friend class SslReadByteFlow;
|
|
|
|
friend class SslWriteByteFlow;
|
|
|
|
|
|
|
|
Result<size_t> write(Slice slice) {
|
2020-06-06 20:31:47 +02:00
|
|
|
clear_openssl_errors("Before SslFd::write");
|
2022-09-20 16:11:15 +02:00
|
|
|
auto start_time = Time::now();
|
2020-07-02 17:01:23 +02:00
|
|
|
auto size = SSL_write(ssl_handle_.get(), slice.data(), static_cast<int>(slice.size()));
|
2022-09-20 16:11:15 +02:00
|
|
|
auto elapsed_time = Time::now() - start_time;
|
2022-09-23 21:29:18 +02:00
|
|
|
if (elapsed_time >= 0.1) {
|
2023-07-22 13:06:57 +02:00
|
|
|
LOG(WARNING) << "SSL_write of size " << slice.size() << " took " << elapsed_time << " seconds and returned "
|
|
|
|
<< size << ' ' << SSL_get_error(ssl_handle_.get(), size);
|
2022-09-20 16:11:15 +02:00
|
|
|
}
|
2018-08-15 14:41:42 +02:00
|
|
|
if (size <= 0) {
|
|
|
|
return process_ssl_error(size);
|
|
|
|
}
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
|
|
|
Result<size_t> read(MutableSlice slice) {
|
2020-06-06 20:31:47 +02:00
|
|
|
clear_openssl_errors("Before SslFd::read");
|
2022-09-15 14:03:19 +02:00
|
|
|
auto start_time = Time::now();
|
2020-07-02 17:01:23 +02:00
|
|
|
auto size = SSL_read(ssl_handle_.get(), slice.data(), static_cast<int>(slice.size()));
|
2022-09-15 14:03:19 +02:00
|
|
|
auto elapsed_time = Time::now() - start_time;
|
2022-09-23 21:29:18 +02:00
|
|
|
if (elapsed_time >= 0.1) {
|
2023-07-22 13:06:57 +02:00
|
|
|
LOG(WARNING) << "SSL_read took " << elapsed_time << " seconds and returned " << size << ' '
|
|
|
|
<< SSL_get_error(ssl_handle_.get(), size);
|
2022-09-15 14:03:19 +02:00
|
|
|
}
|
2018-08-15 14:41:42 +02:00
|
|
|
if (size <= 0) {
|
|
|
|
return process_ssl_error(size);
|
|
|
|
}
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
2021-07-04 04:58:54 +02:00
|
|
|
class SslReadByteFlow final : public ByteFlowBase {
|
2018-08-15 14:41:42 +02:00
|
|
|
public:
|
2018-08-16 00:06:53 +02:00
|
|
|
explicit SslReadByteFlow(SslStreamImpl *stream) : stream_(stream) {
|
2018-08-15 14:41:42 +02:00
|
|
|
}
|
2021-07-03 22:51:36 +02:00
|
|
|
bool loop() final {
|
2020-07-22 20:52:00 +02:00
|
|
|
auto to_read = output_.prepare_append();
|
|
|
|
auto r_size = stream_->read(to_read);
|
|
|
|
if (r_size.is_error()) {
|
|
|
|
finish(r_size.move_as_error());
|
|
|
|
return false;
|
2018-08-15 14:41:42 +02:00
|
|
|
}
|
2020-07-22 20:52:00 +02:00
|
|
|
auto size = r_size.move_as_ok();
|
|
|
|
if (size == 0) {
|
|
|
|
return false;
|
2018-08-15 14:41:42 +02:00
|
|
|
}
|
2020-07-22 20:52:00 +02:00
|
|
|
output_.confirm_append(size);
|
|
|
|
return true;
|
2018-08-15 14:41:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t read(MutableSlice data) {
|
2018-08-16 00:06:53 +02:00
|
|
|
return input_->advance(min(data.size(), input_->size()), data);
|
2018-08-15 14:41:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2018-08-15 15:29:46 +02:00
|
|
|
SslStreamImpl *stream_;
|
2018-08-15 14:41:42 +02:00
|
|
|
};
|
|
|
|
|
2021-07-04 04:58:54 +02:00
|
|
|
class SslWriteByteFlow final : public ByteFlowBase {
|
2018-08-15 14:41:42 +02:00
|
|
|
public:
|
2018-08-16 00:06:53 +02:00
|
|
|
explicit SslWriteByteFlow(SslStreamImpl *stream) : stream_(stream) {
|
2018-08-15 14:41:42 +02:00
|
|
|
}
|
2021-07-03 22:51:36 +02:00
|
|
|
bool loop() final {
|
2020-07-22 20:52:00 +02:00
|
|
|
auto to_write = input_->prepare_read();
|
|
|
|
auto r_size = stream_->write(to_write);
|
|
|
|
if (r_size.is_error()) {
|
|
|
|
finish(r_size.move_as_error());
|
|
|
|
return false;
|
2018-08-15 14:41:42 +02:00
|
|
|
}
|
2020-07-22 20:52:00 +02:00
|
|
|
auto size = r_size.move_as_ok();
|
|
|
|
if (size == 0) {
|
|
|
|
return false;
|
2018-08-15 14:41:42 +02:00
|
|
|
}
|
2020-07-22 20:52:00 +02:00
|
|
|
input_->confirm_read(size);
|
|
|
|
return true;
|
2018-08-15 14:41:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t write(Slice data) {
|
|
|
|
output_.append(data);
|
|
|
|
return data.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2018-08-15 15:29:46 +02:00
|
|
|
SslStreamImpl *stream_;
|
2018-08-15 14:41:42 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
SslReadByteFlow read_flow_{this};
|
|
|
|
SslWriteByteFlow write_flow_{this};
|
|
|
|
|
|
|
|
Result<size_t> process_ssl_error(int ret) {
|
2018-08-15 15:29:46 +02:00
|
|
|
auto os_error = OS_ERROR("SSL_ERROR_SYSCALL");
|
2020-07-02 17:01:23 +02:00
|
|
|
int error = SSL_get_error(ssl_handle_.get(), ret);
|
2018-08-15 14:41:42 +02:00
|
|
|
switch (error) {
|
|
|
|
case SSL_ERROR_NONE:
|
|
|
|
LOG(ERROR) << "SSL_get_error returned no error";
|
|
|
|
return 0;
|
|
|
|
case SSL_ERROR_ZERO_RETURN:
|
2020-05-16 19:26:21 +02:00
|
|
|
LOG(DEBUG) << "SSL_ZERO_RETURN";
|
2018-08-15 14:41:42 +02:00
|
|
|
return 0;
|
|
|
|
case SSL_ERROR_WANT_READ:
|
2020-05-16 19:26:21 +02:00
|
|
|
LOG(DEBUG) << "SSL_WANT_READ";
|
2018-08-15 14:41:42 +02:00
|
|
|
return 0;
|
|
|
|
case SSL_ERROR_WANT_WRITE:
|
2020-05-16 19:26:21 +02:00
|
|
|
LOG(DEBUG) << "SSL_WANT_WRITE";
|
2018-08-15 14:41:42 +02:00
|
|
|
return 0;
|
|
|
|
case SSL_ERROR_WANT_CONNECT:
|
|
|
|
case SSL_ERROR_WANT_ACCEPT:
|
|
|
|
case SSL_ERROR_WANT_X509_LOOKUP:
|
2020-05-16 19:26:21 +02:00
|
|
|
LOG(DEBUG) << "SSL: CONNECT ACCEPT LOOKUP";
|
2018-08-15 14:41:42 +02:00
|
|
|
return 0;
|
|
|
|
case SSL_ERROR_SYSCALL:
|
|
|
|
if (ERR_peek_error() == 0) {
|
2018-08-15 15:29:46 +02:00
|
|
|
if (os_error.code() != 0) {
|
2020-05-16 19:26:21 +02:00
|
|
|
LOG(DEBUG) << "SSL_ERROR_SYSCALL";
|
2018-08-15 15:29:46 +02:00
|
|
|
return std::move(os_error);
|
2018-08-15 14:41:42 +02:00
|
|
|
} else {
|
2020-05-16 19:26:21 +02:00
|
|
|
LOG(DEBUG) << "SSL_SYSCALL";
|
2018-08-15 14:41:42 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
2020-12-16 14:04:27 +01:00
|
|
|
/* fallthrough */
|
2018-08-15 14:41:42 +02:00
|
|
|
default:
|
|
|
|
LOG(DEBUG) << "SSL_ERROR Default";
|
|
|
|
return create_openssl_error(1, "SSL error ");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
namespace {
|
2018-08-15 15:29:46 +02:00
|
|
|
int strm_read(BIO *b, char *buf, int len) {
|
2018-08-16 00:06:53 +02:00
|
|
|
auto *stream = static_cast<SslStreamImpl *>(BIO_get_data(b));
|
|
|
|
CHECK(stream != nullptr);
|
2018-08-15 14:41:42 +02:00
|
|
|
BIO_clear_retry_flags(b);
|
2020-05-19 14:11:21 +02:00
|
|
|
CHECK(buf != nullptr);
|
2021-10-20 00:27:02 +02:00
|
|
|
auto res = narrow_cast<int>(stream->flow_read(MutableSlice(buf, len)));
|
2018-08-15 14:41:42 +02:00
|
|
|
if (res == 0) {
|
|
|
|
BIO_set_retry_read(b);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
2018-08-15 15:29:46 +02:00
|
|
|
int strm_write(BIO *b, const char *buf, int len) {
|
2018-08-16 00:06:53 +02:00
|
|
|
auto *stream = static_cast<SslStreamImpl *>(BIO_get_data(b));
|
|
|
|
CHECK(stream != nullptr);
|
2018-08-15 14:41:42 +02:00
|
|
|
BIO_clear_retry_flags(b);
|
2020-05-19 14:11:21 +02:00
|
|
|
CHECK(buf != nullptr);
|
2018-08-15 14:41:42 +02:00
|
|
|
return narrow_cast<int>(stream->flow_write(Slice(buf, len)));
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
} // namespace detail
|
|
|
|
|
|
|
|
SslStream::SslStream() = default;
|
2021-10-18 13:36:15 +02:00
|
|
|
SslStream::SslStream(SslStream &&) noexcept = default;
|
|
|
|
SslStream &SslStream::operator=(SslStream &&) noexcept = default;
|
2018-08-15 14:41:42 +02:00
|
|
|
SslStream::~SslStream() = default;
|
|
|
|
|
2022-11-11 12:51:22 +01:00
|
|
|
Result<SslStream> SslStream::create(CSlice host, SslCtx ssl_ctx, bool use_ip_address_as_host) {
|
2018-09-27 03:19:03 +02:00
|
|
|
auto impl = make_unique<detail::SslStreamImpl>();
|
2022-11-11 12:51:22 +01:00
|
|
|
TRY_STATUS(impl->init(host, ssl_ctx, use_ip_address_as_host));
|
2018-08-15 14:41:42 +02:00
|
|
|
return SslStream(std::move(impl));
|
|
|
|
}
|
2018-09-27 03:19:03 +02:00
|
|
|
SslStream::SslStream(unique_ptr<detail::SslStreamImpl> impl) : impl_(std::move(impl)) {
|
2018-08-15 14:41:42 +02:00
|
|
|
}
|
2018-08-15 15:29:46 +02:00
|
|
|
ByteFlowInterface &SslStream::read_byte_flow() {
|
2018-08-15 14:41:42 +02:00
|
|
|
return impl_->read_byte_flow();
|
|
|
|
}
|
2018-08-15 15:29:46 +02:00
|
|
|
ByteFlowInterface &SslStream::write_byte_flow() {
|
2018-08-15 14:41:42 +02:00
|
|
|
return impl_->write_byte_flow();
|
|
|
|
}
|
|
|
|
size_t SslStream::flow_read(MutableSlice slice) {
|
|
|
|
return impl_->flow_read(slice);
|
|
|
|
}
|
|
|
|
size_t SslStream::flow_write(Slice slice) {
|
|
|
|
return impl_->flow_write(slice);
|
|
|
|
}
|
2018-08-15 22:05:06 +02:00
|
|
|
|
2018-08-15 14:41:42 +02:00
|
|
|
} // namespace td
|
2019-07-22 06:01:51 +02:00
|
|
|
|
|
|
|
#else
|
|
|
|
|
2019-06-28 13:03:06 +02:00
|
|
|
namespace td {
|
2019-07-22 06:01:51 +02:00
|
|
|
|
2019-06-28 13:03:06 +02:00
|
|
|
namespace detail {
|
2019-07-22 06:01:51 +02:00
|
|
|
class SslStreamImpl {};
|
|
|
|
} // namespace detail
|
|
|
|
|
2019-06-28 13:03:06 +02:00
|
|
|
SslStream::SslStream() = default;
|
2022-11-11 12:23:43 +01:00
|
|
|
SslStream::SslStream(SslStream &&) noexcept = default;
|
|
|
|
SslStream &SslStream::operator=(SslStream &&) noexcept = default;
|
2019-06-28 13:03:06 +02:00
|
|
|
SslStream::~SslStream() = default;
|
|
|
|
|
2022-11-11 12:51:22 +01:00
|
|
|
Result<SslStream> SslStream::create(CSlice host, SslCtx ssl_ctx, bool check_ip_address_as_host) {
|
2022-11-11 12:23:43 +01:00
|
|
|
return Status::Error("Not supported in Emscripten");
|
2019-06-28 13:03:06 +02:00
|
|
|
}
|
2019-07-22 06:01:51 +02:00
|
|
|
|
2019-06-28 13:03:06 +02:00
|
|
|
SslStream::SslStream(unique_ptr<detail::SslStreamImpl> impl) : impl_(std::move(impl)) {
|
|
|
|
}
|
2019-07-22 06:01:51 +02:00
|
|
|
|
2019-06-28 13:03:06 +02:00
|
|
|
ByteFlowInterface &SslStream::read_byte_flow() {
|
|
|
|
UNREACHABLE();
|
|
|
|
}
|
2019-07-22 06:01:51 +02:00
|
|
|
|
2019-06-28 13:03:06 +02:00
|
|
|
ByteFlowInterface &SslStream::write_byte_flow() {
|
|
|
|
UNREACHABLE();
|
|
|
|
}
|
2019-07-22 06:01:51 +02:00
|
|
|
|
2019-06-28 13:03:06 +02:00
|
|
|
size_t SslStream::flow_read(MutableSlice slice) {
|
|
|
|
UNREACHABLE();
|
|
|
|
}
|
2019-07-22 06:01:51 +02:00
|
|
|
|
2019-06-28 13:03:06 +02:00
|
|
|
size_t SslStream::flow_write(Slice slice) {
|
|
|
|
UNREACHABLE();
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace td
|
2019-07-22 06:01:51 +02:00
|
|
|
|
2019-06-28 13:03:06 +02:00
|
|
|
#endif
|