使用 OpenSSL 从内存中读取证书文件而不是文件
我有一个使用 OpenSSL 侦听 HTTPS 的服务器。为此,我必须提供要使用的证书。但是,当前的实现使用提供给 OpenSSL API 的文件名。
我希望从内存中读取证书信息,这样我就不必发送打开的证书文件。我尝试用谷歌搜索,但没有找到任何选项。
有可能吗?如果是这样,如何从内存中读取证书文件而不是使用 OpenSSL 的文件?
编辑:以下内容已从评论移至问题。
// CURRENT
void start_server()
{
const char *fileName = "cert_and_key.pem";
set_server_ssl_file(fileName);
}
set_server_ssl_file(const char *fileName)
{
//initialize context
SSL_CTX_use_certificate_file(CTX, pem, SSL_FILETYPE_PEM);
SSL_CTX_use_PrivateKey_file(CTX, pem, SSL_FILETYPE_PEM);
}
//REQUIRED
void start_server()
{
const char *cert = "--BEGIN CERTIFICATE--............";
const char *key = "--BEGIN RSA PRIVATE KEY--.......";
set_server_ssl_options(cert, key);
}
set_server_ssl_options(const char *cert, const char *key)
{
//IMPLEMENTATION REQUIRED
}
I have a server which would listen on HTTPS using OpenSSL. For this, I have to provide the certificate to use. However, the current implementation uses a filename to be provided to the OpenSSL API.
I want the certificate information to be read from memory, so that I don't have to ship the certificate file opening. I tried to google, but I didn't come up with any options.
Is is possible? If so, how do I read certificate files from memory instead of a file using OpenSSL?
EDIT: The following was moved from the comments to the question.
// CURRENT
void start_server()
{
const char *fileName = "cert_and_key.pem";
set_server_ssl_file(fileName);
}
set_server_ssl_file(const char *fileName)
{
//initialize context
SSL_CTX_use_certificate_file(CTX, pem, SSL_FILETYPE_PEM);
SSL_CTX_use_PrivateKey_file(CTX, pem, SSL_FILETYPE_PEM);
}
//REQUIRED
void start_server()
{
const char *cert = "--BEGIN CERTIFICATE--............";
const char *key = "--BEGIN RSA PRIVATE KEY--.......";
set_server_ssl_options(cert, key);
}
set_server_ssl_options(const char *cert, const char *key)
{
//IMPLEMENTATION REQUIRED
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
其他片段将仅加载一个证书。 http://curl.haxx.se/ca/cacert.pem 包含许多不同的证书需要一种新方法。这是改编自openssl 1.0.1p(主要是openssl-1.0.1p\crypto\x509\by_file.c,char* buf包含*.pem文件的内容,ctx是boost::asio::ssl::context ),自行添加错误处理:
The other snippets will only load one certificate. The content of files like http://curl.haxx.se/ca/cacert.pem that contain a lot of different certificates need a new approach. This is adapted from openssl 1.0.1p (mostly openssl-1.0.1p\crypto\x509\by_file.c, char* buf contains the content of a *.pem file, ctx is a boost::asio::ssl::context), add error handling on your own:
不要忘记
cert_data
和pkey_data
之前的&
- 请注意 OpenSSL 会修改这些指针。Don't forget
&
beforecert_data
andpkey_data
- and note that OpenSSL modifies these pointers.还有另一个使用
X509_STORE_add_cert
的响应,该响应已被投票但不正确。该答案是在内存中执行SSL_CTX_load_verify_locations
的方法,但不会加载服务器证书链。对该评论的回复也表明它不起作用。以下代码是基于 OpenSSL 中该函数实现的
SSL_CTX_use_certificate_chain_file
从内存加载实现:There is another response that uses
X509_STORE_add_cert
, which is up-voted but incorrect. That answer is a way to doSSL_CTX_load_verify_locations
in memory, but does not load the server certificate chain. Replies to that comment also indicate that it does not work.The following code is a load-from-memory implementation of
SSL_CTX_use_certificate_chain_file
based on the implementation of that function in OpenSSL:以下代码为我完成了这项工作:
The following code did the job for me: