1、简介
HTTPS协议简单的说就是经过ssl加密的HTTP协议,本文不介绍https server中http信令的实现,只介绍ssl的实现。
2、需要的库
使用openssl实现ssl,需要编译openssl生成libssl.a和libcrypto.a
3、API的使用
需要包含的头文件
#include <openssl/ssl.h>
初始化,cert_path是证书文件的路径,private_key_path是私钥文件路径
SSL_CTX* ssl_socket_init(const char* cert_path, const char* private_key_path)
{
SSL_library_init();
OpenSSL_add_all_algorithms();
SSL_load_error_strings();
SSL_CTX* ctx = SSL_CTX_new(SSLv23_server_method());
if (ctx == NULL) {
return ctx;
}
if ((SSL_CTX_use_certificate_file(ctx, cert_path, SSL_FILETYPE_PEM) < 1) ||
(SSL_CTX_use_PrivateKey_file(ctx, private_key_path, SSL_FILETYPE_PEM) < 1) ||
(!SSL_CTX_check_private_key(ctx)) )
{
SSL_CTX_free(ctx);
ctx = NULL;
return ctx;
}
return ctx;