optimize ssl module

This commit is contained in:
zhengshuxin 2022-08-20 21:32:08 +08:00
parent 5dcdc47999
commit a1b09b743e
21 changed files with 172 additions and 228 deletions

View File

@ -161,23 +161,17 @@ void master_service::proc_on_init(void)
// 允许服务端的 SSL 会话缓存功能
conf_->enable_cache(var_cfg_ssl_session_cache);
// 添加本地服务的证书
if (!conf_->add_cert(var_cfg_crt_file)) {
// 添加本地服务的证书及服务密钥
if (!conf_->add_cert(var_cfg_crt_file, var_cfg_key_file)) {
logger_error("add cert failed, crt: %s, key: %s",
var_cfg_crt_file, var_cfg_key_file);
delete conf_;
conf_ = NULL;
return;
}
logger("load cert ok, crt: %s, key: %s",
var_cfg_crt_file, var_cfg_key_file);
// 添加本地服务密钥
if (!conf_->set_key(var_cfg_key_file)) {
logger_error("set private key error");
delete conf_;
conf_ = NULL;
}
}
void master_service::proc_on_exit(void)

View File

@ -216,8 +216,8 @@ void master_service::proc_on_init(void)
// 允许服务端的 SSL 会话缓存功能
conf_->enable_cache(var_cfg_ssl_session_cache);
// 添加本地服务的证书
if (!conf_->add_cert(var_cfg_crt_file)) {
// 添加本地服务的证书及服务密钥
if (!conf_->add_cert(var_cfg_crt_file, var_cfg_key_file)) {
logger_error("add cert failed, crt: %s, key: %s",
var_cfg_crt_file, var_cfg_key_file);
delete conf_;
@ -226,13 +226,6 @@ void master_service::proc_on_init(void)
}
logger("load cert ok, crt: %s, key: %s",
var_cfg_crt_file, var_cfg_key_file);
// 添加本地服务密钥
if (!conf_->set_key(var_cfg_key_file)) {
logger_error("set private key error");
delete conf_;
conf_ = NULL;
}
}
void master_service::proc_on_exit(void)

View File

@ -1,6 +1,7 @@
#pragma once
#include "../acl_cpp_define.hpp"
#include "../stdlib/thread_mutex.hpp"
#include "../stdlib/string.hpp"
#include "sslbase_conf.hpp"
#include <vector>
@ -42,18 +43,26 @@ public:
/**
* @override
*/
bool append_key_cert(const char* crt_file, const char* key_file,
bool add_cert(const char* crt_file, const char* key_file,
const char* key_pass = NULL);
/**
* @override
* : mbedtls_conf , 使
*/
bool add_cert(const char* crt_file);
bool add_cert(const char* /* crt_file */)
{
return false;
}
/**
* @override
* : mbedtls_conf , 使
*/
bool set_key(const char* key_file, const char* key_pass = NULL);
bool set_key(const char* /*key_file*/, const char* /* key_pass */)
{
return false;
}
/**
* @override
@ -116,8 +125,9 @@ private:
void* entropy_;
void* rnd_;
void* cacert_;
void* pkey_;
void* cert_chain_;
string key_file_;
string key_pass_;
string crt_file_;
void* cache_;
mbedtls_verify_t verify_mode_;
std::vector<std::pair<void*, void*> > cert_keys_;

View File

@ -20,19 +20,9 @@ public:
/**
* @override
*/
bool append_key_cert(const char* crt_file, const char* key_file,
bool add_cert(const char* crt_file, const char* key_file,
const char* key_pass = NULL);
/**
* @override
*/
//bool add_cert(const char* crt_file);
/**
* @override
*/
//bool set_key(const char* key_file, const char* key_pass = NULL);
/**
* @override
*/

View File

@ -39,6 +39,12 @@ public:
*/
bool load_ca(const char* ca_file, const char* ca_path);
/**
* @override
*/
bool add_cert(const char* crt_file, const char* key_file,
const char* key_pass = NULL);
/**
* @override
*/

View File

@ -40,7 +40,7 @@ public:
* @param key_pass {const char*} NULL
* @return {bool}
*/
virtual bool append_key_cert(const char* crt_file, const char* key_file,
virtual bool add_cert(const char* crt_file, const char* key_file,
const char* key_pass = NULL)
{
(void) crt_file;

View File

@ -240,7 +240,7 @@ static void usage(const char* procname)
int main(int argc, char* argv[])
{
// 事件引擎是否采用内核中的高效模式
bool use_kernel = false;
bool use_kernel = false, use_polarssl = true;
acl::string key_file, cert_file, libpath("../libpolarssl.so");
acl::string addr("127.0.0.1:9800");
int ch, delay_ms = 100, check_fds_inter = 10;
@ -303,6 +303,7 @@ int main(int argc, char* argv[])
cert_file.clear();
printf("load %s error\r\n", libpath.c_str());
}
use_polarssl = false;
} else if (libpath.find("polarssl") != NULL) {
acl::polarssl_conf::set_libpath(libpath);
@ -322,22 +323,30 @@ int main(int argc, char* argv[])
// 允许服务端的 SSL 会话缓存功能
__ssl_conf->enable_cache(true);
// 添加本地服务的证书
if (!__ssl_conf->add_cert(cert_file.c_str())) {
delete __ssl_conf;
__ssl_conf = NULL;
std::cout << "add_cert error: " << cert_file.c_str()
<< std::endl;
}
if (use_polarssl) {
// 添加本地服务的证书
if (!__ssl_conf->add_cert(cert_file.c_str())) {
delete __ssl_conf;
__ssl_conf = NULL;
std::cout << "add_cert error: " << cert_file.c_str() << std::endl;
}
// 添加本地服务密钥
else if (!__ssl_conf->set_key(key_file.c_str())) {
delete __ssl_conf;
__ssl_conf = NULL;
std::cout << "set_key error: " << key_file.c_str()
<< std::endl;
// 添加本地服务密钥
else if (!__ssl_conf->set_key(key_file.c_str())) {
delete __ssl_conf;
__ssl_conf = NULL;
std::cout << "set_key error: " << key_file.c_str() << std::endl;
std::cout << "Load cert&key OK!" << std::endl;
}
} else {
std::cout << "Load cert&key OK!" << std::endl;
// 添加本地服务的证书
if (!__ssl_conf->add_cert(cert_file.c_str(), key_file.c_str())) {
delete __ssl_conf;
__ssl_conf = NULL;
std::cout << "add_cert error: " << cert_file.c_str() << std::endl;
} else {
std::cout << "Load cert&key OK!" << std::endl;
}
}
}

View File

@ -250,24 +250,33 @@ void master_service::proc_on_init()
// 允许服务端的 SSL 会话缓存功能
server_ssl_conf_->enable_cache(var_cfg_session_cache ? true : false);
// 添加本地服务的证书
if (!server_ssl_conf_->add_cert(var_cfg_crt_file)) {
logger_error("add cert failed, crt: %s, key: %s",
var_cfg_crt_file, var_cfg_key_file);
delete server_ssl_conf_;
server_ssl_conf_ = NULL;
return;
if (use_mbedtls) {
if (!server_ssl_conf_->add_cert(var_cfg_crt_file, var_cfg_key_file)) {
logger_error("add cert failed, crt: %s, key: %s",
var_cfg_crt_file, var_cfg_key_file);
delete server_ssl_conf_;
server_ssl_conf_ = NULL;
return;
}
} else {
// 添加本地服务的证书
if (!server_ssl_conf_->add_cert(var_cfg_crt_file)) {
logger_error("add cert failed, crt: %s, key: %s",
var_cfg_crt_file, var_cfg_key_file);
delete server_ssl_conf_;
server_ssl_conf_ = NULL;
return;
}
// 添加本地服务密钥
if (!server_ssl_conf_->set_key(var_cfg_key_file)) {
logger_error("add key failed, crt: %s, key: %s",
var_cfg_crt_file, var_cfg_key_file);
delete server_ssl_conf_;
server_ssl_conf_ = NULL;
}
}
logger("load cert ok, crt: %s, key: %s",
var_cfg_crt_file, var_cfg_key_file);
// 添加本地服务密钥
if (!server_ssl_conf_->set_key(var_cfg_key_file)) {
logger_error("add key failed, crt: %s, key: %s",
var_cfg_crt_file, var_cfg_key_file);
delete server_ssl_conf_;
server_ssl_conf_ = NULL;
}
logger("load cert ok, crt: %s, key: %s", var_cfg_crt_file, var_cfg_key_file);
}
void master_service::proc_on_exit()

View File

@ -12,4 +12,4 @@ ifeq ($(findstring Darwin, $(UNIXNAME)), Darwin)
endif
PROG = https_server
#EXTLIBS += -lpolarssl -lz
EXTLIBS += -lz -ldl
EXTLIBS += -L/usr/local/lib -lssl -lcrypto -lz -ldl

View File

@ -79,6 +79,6 @@ bool http_servlet::doPost(acl::HttpServletRequest& req,
// 发送 http 响应体,因为设置了 chunk 传输模式,所以需要多调用一次
// res.write 且两个参数均为 0 以表示 chunk 传输数据结束
bool ret = res.write(buf) && res.write(NULL, 0) && keep_alive;
printf(">>>ret: %s\r\n", ret ? "ok":"err");
printf(">>>write ret: %s\r\n", ret ? "ok":"err");
return ret;
}

View File

@ -114,13 +114,14 @@ service http_server {
# key_file = {install_path}/conf/ssl/mm263com.key
# crt_file = ./4233173_xys.lonpeak.com.pem
# key_file = ./4233173_xys.lonpeak.com.pem
crt_file = ./ssl_crt.pem
key_file = ./ssl_key.pem
crt_file = ../ssl_crt.pem
key_file = ../ssl_key.pem
libcrypto_path = ../libmbedcrypto.so
libx509_path = ../libmbedx509.so
libssl_path = ../libmbedtls.so
# libssl_path = ../libpolarssl.so
# libssl_path = ../libopenssl.so
# libcrypto_path = ../libmbedcrypto.dylib
# libx509_path = ../libmbedx509.dylib

View File

@ -19,7 +19,7 @@ int main(int argc, char* argv[])
if (argc >= 2 && strcmp(argv[1], "alone") == 0)
{
acl::log::stdout_open(true); // 日志输出至标准输出
const char* addr = ":1443";
const char* addr = "";
printf("listen on: %s\r\n", addr);
if (argc >= 3)
ms.run_alone(addr, argv[2], 0); // 单独运行方式

View File

@ -18,7 +18,7 @@ acl::master_str_tbl var_conf_str_tab[] = {
#else
{ "libcrypto_path", "./libmbedcrypto.so", &var_cfg_libcrypto_path },
{ "libx509_path", "./libmbedx509.so", &var_cfg_libx509_path },
{ "libssl_path", "./lib.so", &var_cfg_libssl_path },
{ "libssl_path", "./libssl.so", &var_cfg_libssl_path },
#endif
{ "crt_file", "./ssl_crt.pem", &var_cfg_crt_file },
{ "key_file", "./ssl_key.pem", &var_cfg_key_file },
@ -157,6 +157,8 @@ void master_service::proc_on_init()
return;
}
bool use_polarssl = false;
if (strstr(var_cfg_libssl_path, "mbedtls")) {
acl::mbedtls_conf::set_libpath(var_cfg_libcrypto_path,
var_cfg_libx509_path, var_cfg_libssl_path);
@ -174,28 +176,42 @@ void master_service::proc_on_init()
}
conf_ = new acl::polarssl_conf();
use_polarssl = true;
} else if (strstr(var_cfg_libssl_path, "openssl")) {
conf_ = new acl::openssl_conf(true);
} else {
logger_error("not support this ssl lib=%s!", var_cfg_libssl_path);
exit (1);
}
// 允许服务端的 SSL 会话缓存功能
conf_->enable_cache(var_cfg_session_cache);
// 添加本地服务的证书
if (!conf_->add_cert(var_cfg_crt_file)) {
if (use_polarssl) {
// 添加本地服务的证书
if (!conf_->add_cert(var_cfg_crt_file)) {
logger_error("add cert failed, crt: %s, key: %s",
var_cfg_crt_file, var_cfg_key_file);
delete conf_;
conf_ = NULL;
return;
}
// 添加本地服务密钥
if (!conf_->set_key(var_cfg_key_file)) {
logger_error("set private key error");
delete conf_;
conf_ = NULL;
}
} else if (!conf_->add_cert(var_cfg_crt_file, var_cfg_key_file)) {
logger_error("add cert failed, crt: %s, key: %s",
var_cfg_crt_file, var_cfg_key_file);
delete conf_;
conf_ = NULL;
return;
}
logger("load cert ok, crt: %s, key: %s",
var_cfg_crt_file, var_cfg_key_file);
// 添加本地服务密钥
if (!conf_->set_key(var_cfg_key_file)) {
logger_error("set private key error");
delete conf_;
conf_ = NULL;
}
logger("load cert ok, crt: %s, key: %s", var_cfg_crt_file, var_cfg_key_file);
}
void master_service::proc_on_exit()

View File

@ -10,8 +10,9 @@
// TODO: 在此处引用程序要求的附加头文件
#include "acl_cpp/lib_acl.hpp"
#include "lib_acl.h"
#include "acl_cpp/lib_acl.hpp"
#include "acl_cpp/stream/openssl_conf.hpp"
#ifdef WIN32
#define snprintf _snprintf

View File

@ -33,7 +33,7 @@ private:
bool setup_ssl(void) {
bool non_block = false;
acl::sslbase_io* ssl = ssl_conf_.open(non_block);
acl::sslbase_io* ssl = ssl_conf_.create(non_block);
// 对于使用 SSL 方式的流对象,需要将 SSL IO 流对象注册至网络
// 连接流对象中,即用 ssl io 替换 stream 中默认的底层 IO 过程
@ -85,18 +85,12 @@ static bool ssl_init(const acl::string& ssl_crt, const acl::string& ssl_key,
ssl_conf.enable_cache(true);
// ¼ÓÔØ SSL Ö¤Êé
if (!ssl_conf.add_cert(ssl_crt)) {
// źÓÔŘ SSL Ö¤Ęéź°Ö¤Ęé˽Կ
if (!ssl_conf.add_cert(ssl_crt, ssl_key)) {
printf("add ssl crt=%s error\r\n", ssl_crt.c_str());
return false;
}
// ÉèÖà SSL Ö¤Êé˽Կ
if (!ssl_conf.set_key(ssl_key)) {
printf("set ssl key=%s error\r\n", ssl_key.c_str());
return false;
}
return true;
}

View File

@ -363,6 +363,17 @@ protected:
// 允许服务端的 SSL 会话缓存功能
conf_->enable_cache(true);
#ifdef USE_MBEDTLS
// 添加本地服务的证书
if (!conf_->add_cert(crt_file_.c_str(), key_file_.c_str())) {
logger_error("add cert failed, crt: %s, key: %s",
crt_file_.c_str(), key_file_.c_str());
delete conf_;
conf_ = NULL;
return;
}
logger("load cert ok, crt: %s", crt_file_.c_str());
#else
// 添加本地服务的证书
if (conf_->add_cert(crt_file_.c_str()) == false) {
logger_error("add cert failed, crt: %s, key: %s",
@ -381,6 +392,7 @@ protected:
return;
}
logger("set key ok, key: %s", key_file_.c_str());
#endif
#ifndef USE_MBEDTLS
conf_->set_authmode(verify_mode_);

View File

@ -202,24 +202,33 @@ void master_service::proc_on_init()
// 允许服务端的 SSL 会话缓存功能
conf_->enable_cache(var_cfg_session_cache);
// 添加本地服务的证书
if (!conf_->add_cert(var_cfg_crt_file)) {
logger_error("add cert failed, crt: %s, key: %s",
var_cfg_crt_file, var_cfg_key_file);
delete conf_;
conf_ = NULL;
return;
if (var_cfg_use_mbedtls) {
if (!conf_->add_cert(var_cfg_crt_file, var_cfg_key_file)) {
logger_error("add cert failed, crt: %s, key: %s",
var_cfg_crt_file, var_cfg_key_file);
delete conf_;
conf_ = NULL;
return;
}
} else {
// 添加本地服务的证书
if (!conf_->add_cert(var_cfg_crt_file)) {
logger_error("add cert failed, crt: %s, key: %s",
var_cfg_crt_file, var_cfg_key_file);
delete conf_;
conf_ = NULL;
return;
}
// 添加本地服务密钥
if (!conf_->set_key(var_cfg_key_file)) {
logger_error("set private key error");
delete conf_;
conf_ = NULL;
}
}
logger("load cert ok, crt: %s, key: %s",
var_cfg_crt_file, var_cfg_key_file);
// 添加本地服务密钥
if (!conf_->set_key(var_cfg_key_file)) {
logger_error("set private key error");
delete conf_;
conf_ = NULL;
}
logger("load cert ok, crt: %s, key: %s", var_cfg_crt_file, var_cfg_key_file);
}
void master_service::proc_on_exit()

View File

@ -680,10 +680,7 @@ mbedtls_conf::mbedtls_conf(bool server_side, mbedtls_verify_t verify_mode)
entropy_ = acl_mycalloc(1, sizeof(mbedtls_entropy_context));
rnd_ = acl_mycalloc(1, sizeof(mbedtls_ctr_drbg_context));
cacert_ = NULL;
cert_chain_ = NULL;
cache_ = NULL;
pkey_ = NULL;
verify_mode_ = verify_mode;
#else
(void) server_side;
@ -695,9 +692,7 @@ mbedtls_conf::mbedtls_conf(bool server_side, mbedtls_verify_t verify_mode)
(void) entropy_;
(void) rnd_;
(void) cacert_;
(void) cert_chain_;
(void) cache_;
(void) pkey_;
(void) verify_mode_;
#endif
}
@ -707,16 +702,6 @@ mbedtls_conf::~mbedtls_conf(void)
#ifdef HAS_MBEDTLS
free_ca();
if (cert_chain_) {
__x509_crt_free((X509_CRT*) cert_chain_);
acl_myfree(cert_chain_);
}
if (pkey_) {
__pk_free((PKEY*) pkey_);
acl_myfree(pkey_);
}
if (init_status_ != CONF_INIT_NIL) {
__entropy_free((mbedtls_entropy_context*) entropy_);
}
@ -813,7 +798,7 @@ bool mbedtls_conf::load_ca(const char* ca_file, const char* ca_path)
#endif
}
bool mbedtls_conf::append_key_cert(const char* crt_file, const char* key_file,
bool mbedtls_conf::add_cert(const char* crt_file, const char* key_file,
const char* key_pass)
{
if (crt_file == NULL || crt_file[0] == '\0' ||
@ -855,8 +840,7 @@ bool mbedtls_conf::append_key_cert(const char* crt_file, const char* key_file,
cert_status_ = CONF_OWN_CERT_OK;
return true;
ERR:
logger_error("append_key_cert(%s:%s) error: -0x%04x",
crt_file, key_file, -ret);
logger_error("add cert (%s:%s) error: -0x%04x", crt_file, key_file, -ret);
if (cert) {
__x509_crt_free(cert);
acl_myfree(cert);
@ -877,83 +861,6 @@ ERR:
#endif
}
bool mbedtls_conf::add_cert(const char* crt_file)
{
if (crt_file == NULL || *crt_file == 0) {
logger_error("crt_file null");
return false;
}
#ifdef HAS_MBEDTLS
if (!init_once()) {
logger_error("init_once error");
return false;
}
if (cert_chain_ == NULL) {
cert_chain_ = acl_mycalloc(1, sizeof(X509_CRT));
__x509_crt_init((X509_CRT*) cert_chain_);
}
int ret = __x509_crt_parse_file((X509_CRT*) cert_chain_, crt_file);
if (ret != 0) {
logger_error("x509_crt_parse_file(%s) error: -0x%04x",
crt_file, -ret);
__x509_crt_free((X509_CRT*) cert_chain_);
acl_myfree(cert_chain_);
cert_chain_ = NULL;
return false;
}
return true;
#else
(void) crt_file;
logger_error("HAS_MBEDTLS not defined!");
return false;
#endif
}
bool mbedtls_conf::set_key(const char* key_file,
const char* key_pass /* = NULL */)
{
#ifdef HAS_MBEDTLS
if (!init_once()) {
logger_error("init_once error");
return false;
}
if (pkey_ != NULL) {
__pk_free((PKEY*) pkey_);
acl_myfree(pkey_);
}
pkey_ = acl_mycalloc(1, sizeof(PKEY));
__pk_init((PKEY*) pkey_);
int ret = __pk_parse_keyfile((PKEY*) pkey_, key_file,
key_pass ? key_pass : "");
if (ret != 0) {
logger_error("pk_parse_keyfile(%s) error: -0x%04x",
key_file, -ret);
__pk_free((PKEY*) pkey_);
acl_myfree(pkey_);
pkey_ = NULL;
return false;
}
return true;
#else
(void) key_file;
(void) key_pass;
logger_error("HAS_MBEDTLS not defined!");
return false;
#endif
}
void mbedtls_conf::enable_cache(bool on)
{
#ifdef HAS_MBEDTLS
@ -998,26 +905,6 @@ bool mbedtls_conf::setup_certs(void* ssl)
return false;
}
if (cert_chain_ == NULL || pkey_ == NULL) {
return true;
}
thread_mutex_guard guard(lock_);
if (cert_status_ == CONF_OWN_CERT_OK) {
return true;
} else if (cert_status_ == CONF_OWN_CERT_ERR) {
return false;
}
// Setup own's cert chain and private key
ret = __ssl_conf_own_cert((mbedtls_ssl_config*) conf_,
(X509_CRT*) cert_chain_, (PKEY*) pkey_);
if (ret != 0) {
cert_status_ = CONF_OWN_CERT_ERR;
logger_error("ssl_conf_own_cert error: -0x%04x", -ret);
return false;
}
cert_status_ = CONF_OWN_CERT_OK;
return true;
#else
(void) ssl;

View File

@ -86,7 +86,7 @@ bool openssl_conf::load_ca(const char* ca_file, const char* /* ca_path */)
return true;
}
bool openssl_conf::append_key_cert(const char* crt_file, const char* key_file,
bool openssl_conf::add_cert(const char* crt_file, const char* key_file,
const char* key_pass /* NULL */)
{
if (crt_file == NULL || key_file == NULL) {

View File

@ -80,7 +80,6 @@ bool openssl_io::handshake(void)
}
int ret = SSL_do_handshake((SSL*) ssl_);
printf(">>>>>>>>>>handshake ret=%d\r\n", ret);
if (ret == 1) {
handshake_ok_ = true;
return true;
@ -107,6 +106,7 @@ bool openssl_io::on_close(bool alive)
// OpenSSL 1.0.2f complains if SSL_shutdown() is called during
// an SSL handshake, while previous versions always return 0.
// Avoid calling SSL_shutdown() if handshake wasn't completed.
// -- nginx
return true;
}
@ -135,13 +135,17 @@ bool openssl_io::on_close(bool alive)
int openssl_io::read(void* buf, size_t len)
{
size_t total_bytes = 0;
char* ptr = (char*) buf;
char* ptr = (char*) buf;
while (total_bytes < len) {
int ret = SSL_read((SSL*) ssl_, ptr, len);
if (ret > 0) {
total_bytes += ret;
ptr += ret;
if (!nblock_) {
break;
}
continue;
}
@ -176,6 +180,10 @@ int openssl_io::send(const void* buf, size_t len)
bytes_written = SSL_write((SSL*) ssl_, buf, len);
if (bytes_written > 0) {
total_bytes += bytes_written;
if (!nblock_) {
break;
}
continue;
}

View File

@ -403,6 +403,12 @@ bool polarssl_conf::load_ca(const char* ca_file, const char* ca_path)
#endif
}
bool polarssl_conf::add_cert(const char* crt_file, const char* key_file,
const char* key_pass /* NULL */)
{
return add_cert(crt_file) && set_key(key_file, key_pass);
}
bool polarssl_conf::add_cert(const char* crt_file)
{
if (crt_file == NULL || *crt_file == 0) {
@ -437,8 +443,7 @@ bool polarssl_conf::add_cert(const char* crt_file)
#endif
}
bool polarssl_conf::set_key(const char* key_file,
const char* key_pass /* = NULL */)
bool polarssl_conf::set_key(const char* key_file, const char* key_pass /* NULL */)
{
#ifdef HAS_POLARSSL
init_once();