mirror of
https://github.com/yhirose/cpp-httplib.git
synced 2025-05-15 01:08:27 +00:00
Load in-memory CA certificates (#1579)
* Load in-memory CA certs * Add test cases for in-memory cert loading * Don't use the IIFE style
This commit is contained in:
parent
3e287b3a26
commit
eab5ea01d7
2 changed files with 84 additions and 0 deletions
41
httplib.h
41
httplib.h
|
@ -1124,6 +1124,7 @@ public:
|
|||
void set_ca_cert_path(const std::string &ca_cert_file_path,
|
||||
const std::string &ca_cert_dir_path = std::string());
|
||||
void set_ca_cert_store(X509_STORE *ca_cert_store);
|
||||
X509_STORE *create_ca_cert_store(const char *ca_cert, std::size_t size);
|
||||
#endif
|
||||
|
||||
#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
|
||||
|
@ -1504,6 +1505,7 @@ public:
|
|||
const std::string &ca_cert_dir_path = std::string());
|
||||
|
||||
void set_ca_cert_store(X509_STORE *ca_cert_store);
|
||||
void load_ca_cert_store(const char *ca_cert, std::size_t size);
|
||||
|
||||
long get_openssl_verify_result() const;
|
||||
|
||||
|
@ -1563,6 +1565,7 @@ public:
|
|||
bool is_valid() const override;
|
||||
|
||||
void set_ca_cert_store(X509_STORE *ca_cert_store);
|
||||
void load_ca_cert_store(const char *ca_cert, std::size_t size);
|
||||
|
||||
long get_openssl_verify_result() const;
|
||||
|
||||
|
@ -7590,6 +7593,35 @@ inline void ClientImpl::set_ca_cert_store(X509_STORE *ca_cert_store) {
|
|||
ca_cert_store_ = ca_cert_store;
|
||||
}
|
||||
}
|
||||
|
||||
inline X509_STORE *ClientImpl::create_ca_cert_store(const char *ca_cert,
|
||||
std::size_t size) {
|
||||
auto mem = BIO_new_mem_buf(ca_cert, size);
|
||||
if (!mem) return nullptr;
|
||||
|
||||
auto inf = PEM_X509_INFO_read_bio(mem, nullptr, nullptr, nullptr);
|
||||
if (!inf) {
|
||||
BIO_free_all(mem);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
auto cts = X509_STORE_new();
|
||||
if (cts) {
|
||||
for (int first = 0, last = sk_X509_INFO_num(inf); first < last; ++first) {
|
||||
auto itmp = sk_X509_INFO_value(inf, first);
|
||||
if (!itmp) continue;
|
||||
|
||||
if (itmp->x509) X509_STORE_add_cert(cts, itmp->x509);
|
||||
|
||||
if (itmp->crl) X509_STORE_add_crl(cts, itmp->crl);
|
||||
}
|
||||
}
|
||||
|
||||
sk_X509_INFO_pop_free(inf, X509_INFO_free);
|
||||
BIO_free_all(mem);
|
||||
|
||||
return cts;
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
|
||||
|
@ -7990,6 +8022,11 @@ inline void SSLClient::set_ca_cert_store(X509_STORE *ca_cert_store) {
|
|||
}
|
||||
}
|
||||
|
||||
inline void SSLClient::load_ca_cert_store(const char *ca_cert,
|
||||
std::size_t size) {
|
||||
set_ca_cert_store(ClientImpl::create_ca_cert_store(ca_cert, size));
|
||||
}
|
||||
|
||||
inline long SSLClient::get_openssl_verify_result() const {
|
||||
return verify_result_;
|
||||
}
|
||||
|
@ -8773,6 +8810,10 @@ inline void Client::set_ca_cert_store(X509_STORE *ca_cert_store) {
|
|||
}
|
||||
}
|
||||
|
||||
inline void Client::load_ca_cert_store(const char *ca_cert, std::size_t size) {
|
||||
set_ca_cert_store(cli_->create_ca_cert_store(ca_cert, size));
|
||||
}
|
||||
|
||||
inline long Client::get_openssl_verify_result() const {
|
||||
if (is_ssl_) {
|
||||
return static_cast<SSLClient &>(*cli_).get_openssl_verify_result();
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue