如何使用boost::asio SSL验证客户端证书
how to verify client certificates using boost::asio SSL?
下面是SSL上下文初始化和验证注册回调的代码片段。如果我用正确的证书连接SSL客户端,它会验证证书并按预期工作。
但是如果我连接客户端没有任何证书,那么它允许连接(实际上它不应该允许没有证书的连接)。如果SSL客户端不发送证书,则不调用验证回调。
boost::asio::ssl::context_base::method SSL_version =
static_cast<boost::asio::ssl::context_base::method>(param_values[ID_PROTOCOL_VERSION].int32_value);
// load certificate files
boost::shared_ptr<boost::asio::ssl::context> context_ = boost::shared_ptr<boost::asio::ssl::context>(
new boost::asio::ssl::context(SSL_version)); // parasoft-suppress BD-RES-LEAKS "The memory is allocated via boost::shared_ptr, hence it'll be deallocated automatically"
p_ctx = boost::static_pointer_cast<void>(context_);
context_->set_options(boost::asio::ssl::context::default_workarounds);
context_->use_certificate_chain_file(cert_chain_file);
context_->use_certificate_file(cert_file, boost::asio::ssl::context::pem);
context_->use_private_key_file(cert_file, boost::asio::ssl::context::pem);
context_->set_verify_mode(boost::asio::ssl::verify_peer);
context_->set_verify_callback(boost::bind(&verify_certificate_cb, _1, _2));
verify_certificate_cb
回调函数
bool verify_certificate_cb(bool preverified, boost::asio::ssl::verify_context& ctx)
{
std::cout << "Function : " << __func__ << " ----------------- Line : " << __LINE__ << std::endl;
int8_t subject_name[256];
X509_STORE_CTX *cts = ctx.native_handle();
int32_t length = 0;
X509* cert = X509_STORE_CTX_get_current_cert(ctx.native_handle());
std::cout << "CTX ERROR : " << cts->error << std::endl;
int32_t depth = X509_STORE_CTX_get_error_depth(cts);
std::cout << "CTX DEPTH : " << depth << std::endl;
switch (cts->error)
{
case X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT:
Debug(PRIORITY_ERROR, "X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT");
break;
case X509_V_ERR_CERT_NOT_YET_VALID:
case X509_V_ERR_ERROR_IN_CERT_NOT_BEFORE_FIELD:
Debug(PRIORITY_ERROR, "Certificate not yet valid!!");
break;
case X509_V_ERR_CERT_HAS_EXPIRED:
case X509_V_ERR_ERROR_IN_CERT_NOT_AFTER_FIELD:
Debug(PRIORITY_ERROR, "Certificate expired..");
break;
case X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN:
Debug(PRIORITY_WARN, "Self signed certificate in chain!!!n");
preverified = true;
break;
default:
break;
}
const int32_t name_length = 256;
X509_NAME_oneline(X509_get_subject_name(cert), reinterpret_cast<char*>(subject_name), name_length);
Debug(PRIORITY_INFO, "Verifying %s", subject_name);
Debug(PRIORITY_INFO, "Verification status : %d", preverified);
std::cout << "Function : " << __func__ << " ----------------- Line : " << __LINE__ << std::endl;
return preverified;
}
如果没有适当的证书文件,如何修改不允许连接的代码?提前谢谢你。
终于得到了答案。我的一个队友建议将boost::asio::ssl::verify_fail_if_no_peer_cert
与boost::asio::ssl::verify_peer
结合使用,并且成功了。
更新的代码行:
context_->set_verify_mode(boost::asio::ssl::verify_peer | boost::asio::ssl::verify_fail_if_no_peer_cert);
相关文章:
- "unknown ca"自生成的 CA、证书和客户端/服务器
- 使用 WinInet 的客户端身份验证(证书 + 私钥)
- QT REST客户端为Microsoft认知服务翻译器API获得身份验证令牌
- 证书验证在客户端服务器通信中使用boost :: asio和openssl失败
- 客户端验证的算法
- 使用套接字的C++中的客户端到服务器身份验证
- 客户端身份验证或与Mongoose Web服务器的相互身份验证
- 由于SSL3_GET_CLIENT_CERTIFICATE:未返回证书,验证 SSL 客户端真实性失败
- 验证进程外的COM服务器客户端是否受信任
- 是否使用LDAP+kerberos作为身份验证程序启用SASL(带有GSSAPI)的客户端和服务器?(单点登录SSO系统
- 客户端和身份验证服务器
- OpenSSL客户端验证-需要客户端验证要求
- 增强asio客户端身份验证
- OpenSSL:服务器无法验证客户端证书
- 如何使用boost::asio SSL验证客户端证书
- 如何向外部客户端提供私有验证服务
- 验证SSL_get_peer_certificate返回的服务器证书的客户端程序
- 客户端/服务器应用程序的身份验证策略
- 对服务器/客户端应用程序使用Kerberos身份验证
- C++ 通过游戏客户端进行 Steam ID 身份验证