在 PHP 中如何获取 CSR 的公钥?
我有一个网页,它接受 CSR(证书签名请求)并对其进行签名。问题是:我不知道如何从 CSR 中提取公钥。我尝试了 openssl_csr_get_public_key($request)
,其中 request 是保存 pem 编码请求的字符串,但这似乎不起作用。关于如何做到这一点有什么想法吗?
谢谢!
I have a web page which takes a CSR (certificate signing request) and signs them. Problem is: I dont know how to extract the public key from the CSR. I tried openssl_csr_get_public_key($request)
where request is the string holding the pem encoded request, but that doesn't seem to work. Any ideas on how to do this?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我发现 phpseclib 的纯 PHP CSR 实现 通常更加“容错”。例如。
有了它,您应该能够从 CSR 访问您想要的任何内容!
I find that phpseclib's pure PHP CSR implementation is generally much more "fault tolerant". eg.
With that you should be able to access anything from the CSR that you want!
您不想签署公钥本身,而是签署 CSR,即签名包含 CSR 中的元数据,该元数据也与公钥一起签署。
PHP命令是
openssl_csr_sign
。为了使其工作,您需要一个 CSR(由客户端提供)和一个 CAcert 文件(从服务器上的文件加载并作为字符串传递给您的命令)。要生成后者,请查看此处: http://www.openssl.org/docs/apps /ca.html (官方)和这里(如何)http://www.freebsdmadeeasy.com/tutorials/freebsd/create-a-ca-with-openssl.php
显然如果你是一个“真正的”CA——例如你有一个由其他人信任的 CA 签名的证书 - 那么您就已经拥有一个证书(由他们签名)。
一般来说,签署其他人提供的公钥并不是一个好习惯。您想要做的是要求您的客户提供 CSR 元数据,然后为他们生成密钥,返回私钥和证书 - 这可以以 PKCS 12 格式完成(请参阅 openssl_pkcs12_export< /code>--整个交换应该通过 SSL 进行。
You don't want to sign a public key per se, but the CSR, i.e. the signature includes metadata in the CSR that is also signed together with the public key.
The PHP command is
openssl_csr_sign
.In order for this to work, you need a CSR (supplied by the client) and a CAcert file (loaded from a file on your server and passed as a string to your command). For generating the latter, look here: http://www.openssl.org/docs/apps/ca.html (official) and here (how to) http://www.freebsdmadeeasy.com/tutorials/freebsd/create-a-ca-with-openssl.php
obviously if you are a "real" CA -- e.g. you have a certificate signed by a CA that others trust -- then you would already have a cert (signed by them).
Generally speaking, it is not good practice to sign public keys supplied by others. What you want to do is ask your client to supply the CSR metadata, and then you generate their key for them, returning both the private key and the certificate -- this can be done in a PKCS 12 format (see
openssl_pkcs12_export
-- the entire exchange should occur over SSL.确保 csr 的格式正确。检查 PHP 是否没有将加号更改为空格或类似的内容。
Make sure that the csr is correctly formatted. Check that PHP isnt changing plusses into spaces or something of the sort.