英文:
Golang: verify cert was signed using private key
问题
我正在将一个应用程序从 PHP 迁移到 Go,但无法弄清楚如何验证 SSL 证书是否由我们的私钥签名,以供给定的服务器使用。
在 PHP 中,可以简单地使用以下代码进行验证:
openssl_x509_check_private_key($cert, $pkey)
我可以验证主机的起始/结束日期和有效性,但无法找到验证 SSL 证书是否由私钥签名的方法。
英文:
I'm porting an app from php to go and can't figure out how to verify a SSL cert was signed by our private key for a given server.
In php its simply:
openssl_x509_check_private_key($cert, $pkey)
I can verify start/end dates and validity for a host, but can't see a way to do this.
答案1
得分: 1
openssl_x509_check_private_key的文档实际上没有说明参数是什么。我假设它们要么是PEM编码数据的文件名,要么是直接的PEM编码数据(字符串)。
在这种情况下,你可以简单地调用tls.LoadX509KeyPair或tls.X509KeyPair,它们会检查证书和密钥是否匹配。
如果你对执行实际检查的低级API感兴趣,可以查看tls.X509KeyPair的源代码(搜索x509.ParseCertificate)。
英文:
The docs for openssl_x509_check_private_key don't actually say what the arguments are. I'm going to assume that they're either filenames for PEM encoded data, or PEM encoded data (strings) directly.
In that case you can simply call tls.LoadX509KeyPair or tls.X509KeyPair, which will check that the certificate and key match.
Check the source for tls.X509KeyPair if you are interested in the low-level API that performs the actual check (search for x509.ParseCertificate).
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论