IXWebSocket/ixwebsocket/IXSocketOpenSSL.h

54 lines
1.6 KiB
C
Raw Normal View History

2018-09-27 23:56:48 +02:00
/*
* IXSocketOpenSSL.h
* Author: Benjamin Sergeant
* Copyright (c) 2017-2018 Machine Zone, Inc. All rights reserved.
*/
#pragma once
2018-12-15 01:28:11 +01:00
#include "IXCancellationRequest.h"
2019-05-30 17:46:50 +02:00
#include "IXSocket.h"
#include <mutex>
2018-09-27 23:56:48 +02:00
#include <openssl/bio.h>
#include <openssl/conf.h>
#include <openssl/err.h>
2019-05-30 17:46:50 +02:00
#include <openssl/hmac.h>
2018-09-27 23:56:48 +02:00
#include <openssl/ssl.h>
namespace ix
2018-09-27 23:56:48 +02:00
{
2019-05-12 20:43:21 +02:00
class SocketOpenSSL final : public Socket
2018-09-27 23:56:48 +02:00
{
public:
2018-12-30 06:53:33 +01:00
SocketOpenSSL(int fd = -1);
2018-09-27 23:56:48 +02:00
~SocketOpenSSL();
virtual bool connect(const std::string& host,
2018-09-27 23:56:48 +02:00
int port,
std::string& errMsg,
2018-12-15 01:28:11 +01:00
const CancellationRequest& isCancellationRequested) final;
2018-09-27 23:56:48 +02:00
virtual void close() final;
virtual ssize_t send(char* buffer, size_t length) final;
virtual ssize_t send(const std::string& buffer) final;
virtual ssize_t recv(void* buffer, size_t length) final;
2018-09-27 23:56:48 +02:00
private:
2019-01-05 20:42:25 +01:00
void openSSLInitialize();
2018-09-27 23:56:48 +02:00
std::string getSSLError(int ret);
SSL_CTX* openSSLCreateContext(std::string& errMsg);
bool openSSLHandshake(const std::string& hostname, std::string& errMsg);
2019-05-30 17:46:50 +02:00
bool openSSLCheckServerCert(SSL* ssl, const std::string& hostname, std::string& errMsg);
bool checkHost(const std::string& host, const char* pattern);
2018-09-27 23:56:48 +02:00
SSL* _ssl_connection;
2019-01-05 02:28:13 +01:00
SSL_CTX* _ssl_context;
2018-09-27 23:56:48 +02:00
const SSL_METHOD* _ssl_method;
2019-05-30 17:46:50 +02:00
mutable std::mutex _mutex; // OpenSSL routines are not thread-safe
2019-01-05 20:42:25 +01:00
static std::once_flag _openSSLInitFlag;
2019-01-05 20:42:25 +01:00
static std::atomic<bool> _openSSLInitializationSuccessful;
2018-09-27 23:56:48 +02:00
};
2019-05-30 17:46:50 +02:00
} // namespace ix