Golang:验证证书是否使用私钥签名

huangapple go评论74阅读模式
英文:

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).

huangapple
  • 本文由 发表于 2017年6月28日 04:11:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/44789059.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定