Skip to content

Commit

Permalink
Add support for loading WebSocket certificate from PEM string
Browse files Browse the repository at this point in the history
  • Loading branch information
paullouisageneau committed May 11, 2024
1 parent 3a11fec commit 601a3b1
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 16 deletions.
34 changes: 19 additions & 15 deletions src/impl/websocket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,26 @@ 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) {
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");
}

if (config.proxyServer) {
if (config.proxyServer->type == ProxyServer::Type::Socks5)
throw std::invalid_argument(
Expand All @@ -49,19 +64,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 +158,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

0 comments on commit 601a3b1

Please sign in to comment.