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 1 commit
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
36 changes: 25 additions & 11 deletions httplib.h
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,12 @@ struct scope_exit {

} // namespace detail

enum SSLVerifierResponse {
Verified, // connection certificate is verified and accepted
CheckAgain, // use the built-in certificate checker again
Declined // connection certificate was process but is declined
};
Copy link
Contributor

Choose a reason for hiding this comment

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

Might I suggest different names:

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

Also, order NoDecisionMade to the top. It makes more sense to be the default enum value and is certainly less dangerous than CertificateAccepted.

Just two cents from a random contributor...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Proposed change I think is not bad, will update PR.

But since I first posted #2061 and asked if I should publish PR and got an "OK" from @yhirose I think it should be OK to be proposing public API change, although not a major one IMHO; final decision is still his.


enum StatusCode {
// Information responses
Continue_100 = 100,
Expand Down Expand Up @@ -1483,7 +1489,7 @@ 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 +1606,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 +1919,7 @@ 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 +9015,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 +9629,20 @@ inline bool SSLClient::initialize_ssl(Socket &socket, Error &error) {
}

if (server_certificate_verification_) {
if (server_certificate_verifier_) {
if (!server_certificate_verifier_(ssl2)) {
error = Error::SSLServerVerification;
return false;
}
} else {
// set default status to CheckAgain
SSLVerifierResponse verificationStatus = SSLVerifierResponse::CheckAgain;

if (server_certificate_verifier_)
verificationStatus = server_certificate_verifier_(ssl2);
Copy link
Contributor

Choose a reason for hiding this comment

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

This code base uses curlies even around single statements. Also, please run clang-format. This PR would fail #2062.


if (verificationStatus == SSLVerifierResponse::Declined)
{
error = Error::SSLServerVerification;
return false;
}

if (verificationStatus == SSLVerifierResponse::CheckAgain)
{
verify_result_ = SSL_get_verify_result(ssl2);

if (verify_result_ != X509_V_OK) {
Expand Down Expand Up @@ -10389,7 +10403,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