-
Notifications
You must be signed in to change notification settings - Fork 2.4k
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
Changes from 1 commit
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
c27d1fd
server_certificate_verifier extended to reuse built-in verifier
1be86c8
code cleanup and SSLVerifierResponse enum clarification as per @falbr…
59888ae
cleanup
d62d1cb
clang-format
c45f344
change local var verification_status_ declaration to auto
ce7dc7a
change local var verification_status_ to verification_status
a46b287
clang-format
5c2bce6
clang-format
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
}; | ||
|
||
enum StatusCode { | ||
// Information responses | ||
Continue_100 = 100, | ||
|
@@ -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); | ||
|
@@ -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_; | ||
|
@@ -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); | ||
|
@@ -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 | ||
|
@@ -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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This code base uses curlies even around single statements. Also, please run |
||
|
||
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) { | ||
|
@@ -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 | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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:
Also, order
NoDecisionMade
to the top. It makes more sense to be the default enum value and is certainly less dangerous thanCertificateAccepted
.Just two cents from a random contributor...
There was a problem hiding this comment.
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.