无法使用提升验证我的自签名证书
Can't validate my self-signed certificate with boost
我生成了自签名证书(用我自己的CA),现在我试图让一个boost ASIO客户端验证服务器的身份。我用openssl验证了这些,验证似乎有效。
服务器端和客户端代码分别在这里和这里。
我只修改了以下部分:
class server
{
public:
server(boost::asio::io_service& io_service, unsigned short port)
: io_service_(io_service),
acceptor_(io_service,
boost::asio::ip::tcp::endpoint(boost::asio::ip::tcp::v4(), port)),
context_(boost::asio::ssl::context::tlsv12_server)
{
context_.set_options(
boost::asio::ssl::context::default_workarounds
| boost::asio::ssl::context::no_sslv2
| boost::asio::ssl::context::single_dh_use);
context_.set_password_callback(boost::bind(&server::get_password, this));
// Use the certificate for my website that I had generated context_.use_certificate_file("/home/paul/ca/intermediate/certs/mywebsite.net.cert.pem", boost::asio::ssl::context::pem);
// Not sure if I need this, probably not. I do have an intermediate CA though
//context_.use_certificate_chain_file("/home/paul/ca/intermediate/certs/ca-chain.cert.pem");
// Use website private key context_.use_private_key_file("/home/paul/ca/intermediate/private/mywebsite.net.key.pem", boost::asio::ssl::context::pem);
context_.use_tmp_dh_file("/home/paul/SSLTest/dh512.pem");
start_accept();
}
和在客户端:
bool verify_certificate(bool preverified,
boost::asio::ssl::verify_context& ctx)
{
// The verify callback can be used to check whether the certificate that is
// being presented is valid for the peer. For example, RFC 2818 describes
// the steps involved in doing this for HTTPS. Consult the OpenSSL
// documentation for more details. Note that the callback is called once
// for each certificate in the certificate chain, starting from the root
// certificate authority.
// In this example we will simply print the certificate's subject name.
char subject_name[256];
X509* cert = X509_STORE_CTX_get_current_cert(ctx.native_handle());
X509_NAME_oneline(X509_get_subject_name(cert), subject_name, 256);
std::cout << "Verifying " << subject_name << "n";
std::cout << "preverified: " << std::boolalpha << preverified << "n";
return preverified;
}
...
int main(int argc, char* argv[])
{
try
{
boost::asio::io_service io_service;
boost::asio::ip::tcp::resolver resolver(io_service);
boost::asio::ip::tcp::resolver::query query("localhost", "3232");
boost::asio::ip::tcp::resolver::iterator iterator = resolver.resolve(query);
boost::asio::ssl::context ctx(boost::asio::ssl::context::tlsv12_client);
std::ifstream ca_file("/home/paul/ca/certs/ca.cert.pem", std::ios::binary | std::ios::ate);
std::vector<char> data;
auto size = ca_file.tellg();
data.resize(size);
ca_file.seekg(0, std::ios::beg);
ca_file.read(data.data(), size);
ca_file.close();
// Have my own CA added to the list of known CAs
ctx.add_certificate_authority(boost::asio::buffer(data, data.size()));
// Not sure if I need something here, the CA should be enough to
// validate the server's certificate prompted (even if signed by the
// intermediate CA)
//ctx.load_verify_file("/home/paul/ca/private/ca.key.pem");
//ctx.load_verify_file("/home/paul/ca/intermediate/private/intermediate.key.pem");
client c(io_service, ctx, iterator);
但是这不起作用,客户端返回
Verifying /C=IT/ST=Italy/L=Milan/O=MyCompanyLtd/OU=MyCompanyLtd Auth/CN=mywebsite.net/emailAddress=info@mywebsite.net
preverified: false
Handshake failed: certificate verify failed
我知道在回调中没有执行验证,但我认为set_verification_callback中的回调集将在预验证后调用(因此是preverified
参数)。
我错在哪里?
仅使用您提供的数据是不行的。没有足够的信息来验证证书。通常应该调用这两个。
ctx.use_certificate_chain_file("path");
ctx.use_private_key_file("path", boost::asio::ssl::context::pem);
相关文章:
- 我的神经网络不起作用 [XOR 问题]
- C++我的数学有什么问题,为什么我的代码不能正确循环
- 我的字符计数代码计算错误.为什么
- 为什么我的C#代码在调用回C++COM直到Task时会暂停.等待/线程.加入
- cmake在我的项目中所需的所有静态库都不成功
- 为什么我的代码在输出中增加了93天
- 我的简单if-else语句是如何无法访问的代码
- 为什么我的for循环不能正确获取argv
- 我的项目不会像"undefined reference to `grpc::g_core_codegen_interface'"那样使用未定义的引用错误进行编译
- 0-1背包代码中的错误.我的代码中有什么错误
- 当我的阵列太大时出现分段错误
- 如何确认我的constexpr表达式实际上已经在编译时执行
- 为什么二进制搜索在我的测试中不起作用
- 如何指定我希望我的LIB链接到的DLL文件?-Visual Studio 2019
- 我的代码中有错误吗?使用BGI图形的C++代码对我不起作用
- 当我在main中声明了我的2d数组时,为什么我的程序会退出
- OpenGL在启用深度测试时不会丢弃我的碎片
- 我如何创建一个Gnutls-Client发送它的证书到服务器
- 无法使用提升验证我的自签名证书
- 为什么我不能获得用户正在使用的证书的正确信息?