Older/ToolKit/Util/SSLBox.h

207 lines
5.8 KiB
C
Raw Normal View History

2024-09-28 23:55:00 +08:00
/*
* Copyright (c) 2016 The ZLToolKit project authors. All Rights Reserved.
*
* This file is part of ZLToolKit(https://github.com/ZLMediaKit/ZLToolKit).
*
* Use of this source code is governed by MIT license that can be found in the
* LICENSE file in the root of the source tree. All contributing project authors
* may be found in the AUTHORS file in the root of the source tree.
*/
#ifndef CRYPTO_SSLBOX_H_
#define CRYPTO_SSLBOX_H_
#include <mutex>
#include <string>
#include <functional>
#include "logger.h"
#include "List.h"
#include "util.h"
#include "Network/Buffer.h"
#include "ResourcePool.h"
typedef struct x509_st X509;
typedef struct evp_pkey_st EVP_PKEY;
typedef struct ssl_ctx_st SSL_CTX;
typedef struct ssl_st SSL;
typedef struct bio_st BIO;
namespace toolkit {
class SSL_Initor {
public:
friend class SSL_Box;
static SSL_Initor &Instance();
/**
*
* (cer格式的证书只包括公钥使)
* ()
* @param pem_or_p12 pem或p12文件路径或者文件内容字符串
* @param server_mode
* @param password
* @param is_file pem_or_p12是否为文件路径
* @param is_default
*/
bool loadCertificate(const std::string &pem_or_p12, bool server_mode = true, const std::string &password = "",
bool is_file = true, bool is_default = true);
/**
*
*
* @param ignore
*/
void ignoreInvalidCertificate(bool ignore = true);
/**
* ,CA签署的证书使用
*
* @param pem_p12_cer pem文件或p12文件或cer文件路径或内容
* @param server_mode
* @param password pem或p12证书的密码
* @param is_file
* @return
*/
bool trustCertificate(const std::string &pem_p12_cer, bool server_mode = false, const std::string &password = "",
bool is_file = true);
/**
*
* @param cer
* @param server_mode
* @return
*/
bool trustCertificate(X509 *cer, bool server_mode = false);
/**
* SSL_CTX对象
* @param vhost
* @param server_mode
* @return SSL_CTX对象
*/
std::shared_ptr<SSL_CTX> getSSLCtx(const std::string &vhost, bool server_mode);
private:
SSL_Initor();
~SSL_Initor();
/**
* SSL对象
*/
std::shared_ptr<SSL> makeSSL(bool server_mode);
/**
* ssl context
* @param vhost
* @param ctx ssl context
* @param server_mode ssl context
* @param is_default
*/
bool setContext(const std::string &vhost, const std::shared_ptr<SSL_CTX> &ctx, bool server_mode, bool is_default = true);
/**
* SSL_CTX的默认配置
* @param ctx
*/
void setupCtx(SSL_CTX *ctx);
std::shared_ptr<SSL_CTX> getSSLCtx_l(const std::string &vhost, bool server_mode);
std::shared_ptr<SSL_CTX> getSSLCtxWildcards(const std::string &vhost, bool server_mode);
/**
*
*/
std::string defaultVhost(bool server_mode);
/**
* vhost name
*/
static int findCertificate(SSL *ssl, int *ad, void *arg);
private:
struct less_nocase {
bool operator()(const std::string &x, const std::string &y) const {
return strcasecmp(x.data(), y.data()) < 0;
}
};
private:
std::string _default_vhost[2];
std::shared_ptr<SSL_CTX> _ctx_empty[2];
std::map<std::string, std::shared_ptr<SSL_CTX>, less_nocase> _ctxs[2];
std::map<std::string, std::shared_ptr<SSL_CTX>, less_nocase> _ctxs_wildcards[2];
};
////////////////////////////////////////////////////////////////////////////////////
class SSL_Box {
public:
SSL_Box(bool server_mode = true, bool enable = true, int buff_size = 32 * 1024);
~SSL_Box();
/**
*
* @param buffer
*/
void onRecv(const Buffer::Ptr &buffer);
/**
*
* @param buffer
*/
void onSend(Buffer::Ptr buffer);
/**
*
* @param cb
*/
void setOnDecData(const std::function<void(const Buffer::Ptr &)> &cb);
/**
*
* @param cb
*/
void setOnEncData(const std::function<void(const Buffer::Ptr &)> &cb);
/**
* ssl
*/
void shutdown();
/**
*
*/
void flush();
/**
*
* @param host
* @return
*/
bool setHost(const char *host);
private:
void flushWriteBio();
void flushReadBio();
private:
bool _server_mode;
bool _send_handshake;
bool _is_flush = false;
int _buff_size;
BIO *_read_bio;
BIO *_write_bio;
std::shared_ptr<SSL> _ssl;
List <Buffer::Ptr> _buffer_send;
ResourcePool <BufferRaw> _buffer_pool;
std::function<void(const Buffer::Ptr &)> _on_dec;
std::function<void(const Buffer::Ptr &)> _on_enc;
};
} /* namespace toolkit */
#endif /* CRYPTO_SSLBOX_H_ */