简体   繁体   English

PHP stream_context_set_option SSL 证书作为字符串

[英]PHP stream_context_set_option SSL certificate as string

I've got a weird issue.我有一个奇怪的问题。 Basically, I need to do this:基本上,我需要这样做:

 $handle = stream_context_create();
 stream_context_set_option($handle , 'ssl', 'local_cert', '/tmp/cert');

However.然而。 The certificate is not held as a file within the server.证书不作为服务器中的文件保存。 Rather it's an encrypted string held in a clustered database environment.相反,它是一个保存在集群数据库环境中的加密字符串。 So instead of the certificate being a file name pointer, its the physical content of the certificate.因此,证书不是文件名指针,而是证书的物理内容。 So instead of using the file name, I need to specify the content of the certificate instead.因此,我不需要使用文件名,而是需要指定证书的内容。

For example:例如:

 $cert = '-----BEGIN CERTIFICATE-----....
 upWbwmdMd61SjNCdtOpZcNW3YmzuT96Fr7GUPiDQ
 -----END CERTIFICATE-----';

Does anyone have any idea whatsoever how on earth I can do this?有谁知道我到底如何做到这一点? I'm scratching my head over this problem, but my gut instinct says it is doable.我正在为这个问题挠头,但我的直觉告诉我这是可行的。

Thanks in advance everyone!提前谢谢大家!

As Maerlyn said, it appears the only way to do this will be to write the certificate from memory to a temporary file, call the function, make the request, and then remove the temp file.正如 Maerlyn 所说,似乎唯一的方法是将证书从内存写入临时文件,调用该函数,发出请求,然后删除临时文件。

I looked at the PHP source code (relevant code here ) and when you make a request that will use SSL, it checks to see if local_cert context option is set, and if so, ultimately calls the OpenSSL function SSL_CTX_use_PrivateKey_file which reads the certificate from a disk file.我查看了 PHP 源代码(此处为相关代码),当您发出将使用 SSL 的请求时,它会检查是否设置了local_cert上下文选项,如果是,则最终调用 OpenSSL 函数SSL_CTX_use_PrivateKey_file ,该函数从磁盘文件。

Take note that the file doesn't get read until the request is performed, so you can't delete the temp file until after your request, as opposed to after calling stream_context_set_option .请注意,在执行请求之前不会读取该文件,因此您在请求之后才能删除临时文件,而不是在调用stream_context_set_option

I tried to use stream_wrapper (php://memory & custom stream wrapper) with no success :-(. At least, you can create a random temp file, register it to be deleted on shutdown, then run you code我尝试使用 stream_wrapper (php://memory & custom stream wrapper) 没有成功:-(。至少,你可以创建一个随机临时文件,注册它在关机时删除,然后运行你的代码

$tmp_file = tempnam(sys_get_temp_dir(), "key");
register_shutdown_function("unlink",  $tmp_file);

//rest of your code go there

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM