【发布时间】:2023-03-25 01:53:01
【问题描述】:
我有一个使用 C++ 编程语言的 OpenSSL 的 SSL/TLS 客户端程序。我正在寻找方法来验证 SSL_get_peer_certificate 函数调用返回的服务器证书 (X509)。此外,我使用SSL_CTX_load_verify_locations 函数加载了我自己的 CA 证书。 CA 认证了服务器证书。
我能够与我的服务器建立 SSL 会话。现在,我想使用我自己的 CA 验证在 SSL 握手期间收到的服务器证书。我找不到使用 C 或 C++ 的方法。
#include <iostream>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <resolv.h>
#include <netdb.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <openssl/bio.h>
#include <openssl/ssl.h>
#include <openssl/err.h>
#include <openssl/pem.h>
#include <openssl/x509.h>
#include <openssl/x509_vfy.h>
#define DEFAULT_PORT_NUMBER 443
int create_socket(char *, uint16_t port_num);
int openSSL_client_init()
{
OpenSSL_add_all_algorithms();
ERR_load_BIO_strings();
ERR_load_crypto_strings();
SSL_load_error_strings();
if (SSL_library_init() < 0)
return -1;
return 0;
}
int openSSL_create_client_ctx(SSL_CTX **ctx)
{
const SSL_METHOD *method = SSLv23_client_method();
if ((*ctx = SSL_CTX_new(method)) == NULL)
return -1;
//SSL_CTX_set_options(*ctx, SSL_OP_NO_SSLv2);
return 0;
}
int main(int argc, char *argv[])
{
BIO *outbio = NULL;
X509 *cert;
X509_NAME *certname = NULL;
SSL_CTX *ctx;
SSL *ssl;
int server = 0;
int ret, i;
if (openSSL_client_init()) {
std :: cerr << "Could not initialize the OpenSSL library !" << std :: endl;
return -1;
}
outbio = BIO_new_fp(stdout, BIO_NOCLOSE);
if (openSSL_create_client_ctx(&ctx)) {
std :: cerr << "Unable to create a new SSL context structure." << std :: endl;
return -1;
}
std :: cout << "Adding Certifcate" << std :: endl;
if (SSL_CTX_load_verify_locations(ctx, "ca-cert.pem", NULL) <= 0) {
std :: cerr << "Unable to Load certificate" << std :: endl;
return -1;
}
ssl = SSL_new(ctx);
server = create_socket(argv[1], atoi(argv[2]));
if (server < 0) {
std :: cerr << "Error: Can't create TCP session" << std :: endl;
return -1;
}
std :: cout << "Successfully made the TCP connection to: " << argv[1] << " port: " << atoi(argv[2]) << std :: endl;
SSL_set_fd(ssl, server);
if (SSL_connect(ssl) != 1) {
std :: cerr << "Error: Could not build a SSL session to: " << argv[1] << std :: endl;
return -1;
}
std :: cout << "Successfully enabled SSL/TLS session to: " << argv[1] << std :: endl;
//SSL_SESSION *ss = SSL_get_session(ssl);
cert = SSL_get_peer_certificate(ssl);
if (cert == NULL) {
std :: cerr << "Error: Could not get a certificate from: " << argv[1] << std :: endl;
return -1;
}
certname = X509_NAME_new();
certname = X509_get_subject_name(cert);
std :: cout << "Displaying the certificate subject data:" << std :: endl;
X509_NAME_print_ex(outbio, certname, 0, 0);
std :: cout << std :: endl;
char msg[100000] = "GET / HTTP/1.1\r\nHOST: www.siliconbolt.com\r\n\r\n";
SSL_write(ssl, msg, strlen(msg));
SSL_read(ssl, msg, 100000);
std :: cout << "Message is " << msg << std :: endl;
SSL_free(ssl);
close(server);
X509_free(cert);
SSL_CTX_free(ctx);
std :: cout << "Finished SSL/TLS connection with server" << std :: endl;
return 0;
}
int create_socket(char *ip_cstr, uint16_t port_num)
{
int fd;
struct sockaddr_in dest_addr;
fd = socket(AF_INET, SOCK_STREAM, 0);
dest_addr.sin_family = AF_INET;
dest_addr.sin_port = htons(port_num);
dest_addr.sin_addr.s_addr = inet_addr(ip_cstr);
memset(&(dest_addr.sin_zero), '\0', 8);
if (connect(fd, (struct sockaddr *)&dest_addr, sizeof(struct sockaddr)) == -1)
return -1;
return fd;
}
【问题讨论】:
-
另请参阅 OpenSSL wiki 上的 SSL/TLS Client。它会带您完成验证服务器证书所需的 2 或 3 个步骤。如果您使用的是 OpenSSL 1.1.0 或更高版本,那么它的 2 个步骤。如果它的 OpenSSL 1.0.2 或更低版本,那么它的 3 个步骤。我们尚未更新 OpenSSL 1.1.0 的示例。
-
可能是因为没有 C/C++ 语言。您的代码是 C++,而不是 C。
-
@Oalf - 所以我很清楚......你是说在 C 和/或 C++ 中这个问题没有答案。对吗?
-
@Olaf 。我编辑了我的答案。