Skip to content

Commit 8b6a9c0

Browse files
authored
feat: support more ssl verify mode (#3141)
* feat: support more ssl verify mode * 1
1 parent f58ec39 commit 8b6a9c0

File tree

3 files changed

+29
-3
lines changed

3 files changed

+29
-3
lines changed

src/brpc/details/ssl_helper.cpp

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818

1919

20+
#include "brpc/ssl_options.h"
2021
#include <openssl/bio.h>
2122
#ifndef USE_MESALINK
2223

@@ -412,8 +413,18 @@ static int SetSSLOptions(SSL_CTX* ctx, const std::string& ciphers,
412413

413414
// TODO: Verify the CNAME in certificate matches the requesting host
414415
if (verify.verify_depth > 0) {
415-
SSL_CTX_set_verify(ctx, (SSL_VERIFY_PEER
416-
| SSL_VERIFY_FAIL_IF_NO_PEER_CERT), NULL);
416+
if (verify.verify_mode == VerifyMode::VERIFY_FAIL_IF_NO_PEER_CERT) {
417+
SSL_CTX_set_verify(ctx, (SSL_VERIFY_PEER
418+
| SSL_VERIFY_FAIL_IF_NO_PEER_CERT), NULL);
419+
} else if (verify.verify_mode == VerifyMode::VERIFY_PEER) {
420+
SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, NULL);
421+
} else if (verify.verify_mode == VerifyMode::VERIFY_NONE) {
422+
SSL_CTX_set_verify(ctx, SSL_VERIFY_NONE, NULL);
423+
} else {
424+
// for forward compatibility
425+
SSL_CTX_set_verify(ctx, (SSL_VERIFY_PEER
426+
| SSL_VERIFY_FAIL_IF_NO_PEER_CERT), NULL);
427+
}
417428
SSL_CTX_set_verify_depth(ctx, verify.verify_depth);
418429
std::string cafile = verify.ca_file_path;
419430
if (cafile.empty()) {

src/brpc/ssl_options.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,10 @@
2020

2121
namespace brpc {
2222

23-
VerifyOptions::VerifyOptions() : verify_depth(0) {}
23+
VerifyOptions::VerifyOptions()
24+
: verify_depth(0)
25+
, verify_mode(VerifyMode::NOT_SET)
26+
{}
2427

2528
ChannelSSLOptions::ChannelSSLOptions()
2629
: ciphers("DEFAULT")

src/brpc/ssl_options.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,13 @@ struct CertInfo {
4141
std::vector<std::string> sni_filters;
4242
};
4343

44+
enum class VerifyMode {
45+
NOT_SET,
46+
VERIFY_NONE,
47+
VERIFY_PEER,
48+
VERIFY_FAIL_IF_NO_PEER_CERT,
49+
};
50+
4451
struct VerifyOptions {
4552
// Constructed with default options
4653
VerifyOptions();
@@ -50,6 +57,11 @@ struct VerifyOptions {
5057
// Default: 0
5158
int verify_depth;
5259

60+
// Set ssl verify mode for openssl
61+
// If VERIFY_FAIL_IF_NO_PEER_CERT, it will set `SSL_VERIFY_FAIL_IF_NO_PEER_CERT | SSL_VERIFY_PEER`
62+
// Default: NOT_SET
63+
VerifyMode verify_mode;
64+
5365
// Set the trusted CA file to verify the peer's certificate
5466
// If empty, use the system default CA files
5567
// Default: ""

0 commit comments

Comments
 (0)