Skip to content

server_certificate_verifier extended to reuse built-in verifier #2064

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Feb 17, 2025
Merged
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 29 additions & 10 deletions httplib.h
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,15 @@ struct scope_exit {

} // namespace detail

enum SSLVerifierResponse {
// no decision has been made, use the built-in certificate verifier
NoDecisionMade,
// connection certificate is verified and accepted
CertificateAccepted,
// connection certificate was processed but is rejected
CertificateRejected
};

enum StatusCode {
// Information responses
Continue_100 = 100,
Expand Down Expand Up @@ -1483,7 +1492,8 @@ class ClientImpl {
#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
void enable_server_certificate_verification(bool enabled);
void enable_server_hostname_verification(bool enabled);
void set_server_certificate_verifier(std::function<bool(SSL *ssl)> verifier);
void set_server_certificate_verifier(
std::function<SSLVerifierResponse(SSL *ssl)> verifier);
#endif

void set_logger(Logger logger);
Expand Down Expand Up @@ -1600,7 +1610,7 @@ class ClientImpl {
#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
bool server_certificate_verification_ = true;
bool server_hostname_verification_ = true;
std::function<bool(SSL *ssl)> server_certificate_verifier_;
std::function<SSLVerifierResponse(SSL *ssl)> server_certificate_verifier_;
#endif

Logger logger_;
Expand Down Expand Up @@ -1913,7 +1923,8 @@ class Client {
#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
void enable_server_certificate_verification(bool enabled);
void enable_server_hostname_verification(bool enabled);
void set_server_certificate_verifier(std::function<bool(SSL *ssl)> verifier);
void set_server_certificate_verifier(
std::function<SSLVerifierResponse(SSL *ssl)> verifier);
#endif

void set_logger(Logger logger);
Expand Down Expand Up @@ -9009,7 +9020,7 @@ inline void ClientImpl::enable_server_hostname_verification(bool enabled) {
}

inline void ClientImpl::set_server_certificate_verifier(
std::function<bool(SSL *ssl)> verifier) {
std::function<SSLVerifierResponse(SSL *ssl)> verifier) {
server_certificate_verifier_ = verifier;
}
#endif
Expand Down Expand Up @@ -9623,12 +9634,20 @@ inline bool SSLClient::initialize_ssl(Socket &socket, Error &error) {
}

if (server_certificate_verification_) {
SSLVerifierResponse verification_status_ =
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you change it to auto verification_status?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I meant two things, not only auto, but also verification_status (without the trailing _).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorry, missed that; fixed

SSLVerifierResponse::NoDecisionMade;

if (server_certificate_verifier_) {
if (!server_certificate_verifier_(ssl2)) {
error = Error::SSLServerVerification;
return false;
}
} else {
verification_status_ = server_certificate_verifier_(ssl2);
}

if (verification_status_ ==
SSLVerifierResponse::CertificateRejected) {
error = Error::SSLServerVerification;
return false;
}

if (verification_status_ == SSLVerifierResponse::NoDecisionMade) {
verify_result_ = SSL_get_verify_result(ssl2);

if (verify_result_ != X509_V_OK) {
Expand Down Expand Up @@ -10389,7 +10408,7 @@ inline void Client::enable_server_hostname_verification(bool enabled) {
}

inline void Client::set_server_certificate_verifier(
std::function<bool(SSL *ssl)> verifier) {
std::function<SSLVerifierResponse(SSL *ssl)> verifier) {
cli_->set_server_certificate_verifier(verifier);
}
#endif
Expand Down