Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for loading WebSocket certificate from PEM string #1177

Merged
merged 1 commit into from
May 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 22 additions & 16 deletions src/impl/websocket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,28 @@ using namespace std::placeholders;
using namespace std::chrono_literals;
using std::chrono::milliseconds;

const string PemBeginCertificateTag = "-----BEGIN CERTIFICATE-----";

WebSocket::WebSocket(optional<Configuration> optConfig, certificate_ptr certificate)
: config(optConfig ? std::move(*optConfig) : Configuration()),
mCertificate(certificate ? std::move(certificate) : std::move(loadCertificate(config))),
mIsSecure(mCertificate != nullptr), mRecvQueue(RECV_QUEUE_LIMIT, message_size_func) {
mRecvQueue(RECV_QUEUE_LIMIT, message_size_func) {
PLOG_VERBOSE << "Creating WebSocket";

if (certificate) {
mCertificate = std::move(certificate);
} else if (config.certificatePemFile && config.keyPemFile) {
mCertificate = std::make_shared<Certificate>(
config.certificatePemFile->find(PemBeginCertificateTag) != string::npos
? Certificate::FromString(*config.certificatePemFile, *config.keyPemFile)
: Certificate::FromFile(*config.certificatePemFile, *config.keyPemFile,
config.keyPemPass.value_or("")));
} else if (config.certificatePemFile || config.keyPemFile) {
throw std::invalid_argument(
"Either none or both certificate and key PEM files must be specified");
}

mIsSecure = mCertificate != nullptr;

if (config.proxyServer) {
if (config.proxyServer->type == ProxyServer::Type::Socks5)
throw std::invalid_argument(
Expand All @@ -49,19 +66,6 @@ WebSocket::WebSocket(optional<Configuration> optConfig, certificate_ptr certific
}
}

certificate_ptr WebSocket::loadCertificate(const Configuration& config) {
if (!config.certificatePemFile)
return nullptr;

if (config.keyPemFile)
return std::make_shared<Certificate>(
Certificate::FromFile(*config.certificatePemFile, *config.keyPemFile,
config.keyPemPass.value_or("")));

throw std::invalid_argument(
"Either none or both certificate and key PEM files must be specified");
}

WebSocket::~WebSocket() { PLOG_VERBOSE << "Destroying WebSocket"; }

void WebSocket::open(const string &url) {
Expand Down Expand Up @@ -156,7 +160,9 @@ bool WebSocket::isOpen() const { return state == State::Open; }

bool WebSocket::isClosed() const { return state == State::Closed; }

size_t WebSocket::maxMessageSize() const { return config.maxMessageSize.value_or(DEFAULT_WS_MAX_MESSAGE_SIZE); }
size_t WebSocket::maxMessageSize() const {
return config.maxMessageSize.value_or(DEFAULT_WS_MAX_MESSAGE_SIZE);
}

optional<message_variant> WebSocket::receive() {
auto next = mRecvQueue.pop();
Expand Down
2 changes: 1 addition & 1 deletion src/impl/websocket.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ struct WebSocket final : public Channel, public std::enable_shared_from_this<Web

const init_token mInitToken = Init::Instance().token();

const certificate_ptr mCertificate;
certificate_ptr mCertificate;
bool mIsSecure;

optional<string> mHostname; // for TLS SNI and Proxy
Expand Down
Loading