首页 > 解决方案 > 完成 TLS 握手之前的 OCSP 吊销检查

问题描述

在完成 TLS 握手之前,我需要使用 Go 作为客户端对服务器证书进行 OCSP 吊销检查,即 [启动握手 -> 获取服务器证书 -> 检查吊销状态 -> 如果撤销中止],而不是 [启动握手-> 完成握手 -> 检查吊销状态]

使用 Go 的标准 TLS 库这似乎是不可能的,因为 tls.Dial 似乎没有进行任何 OCSP 检查。另一种可能的解决方法是在不执行握手的情况下获取服务器证书,然后检查吊销状态,如果状态正常,则使用 tls.Dial 重做握手,但我找不到在 Go 中执行此操作的方法。

关于如何解决这个特定问题的任何建议?

标签: sslgoocsp

解决方案


您可以VerifyPeerCertificatetls.Config对象中设置,如果撤销检查失败并且您希望中止握手,则让指向函数返回非零错误。

文档

// VerifyPeerCertificate, if not nil, is called after normal
// certificate verification by either a TLS client or server. It
// receives the raw ASN.1 certificates provided by the peer and also
// any verified chains that normal processing found. If it returns a
// non-nil error, the handshake is aborted and that error results.
//
// If normal verification fails then the handshake will abort before
// considering this callback. If normal verification is disabled by
// setting InsecureSkipVerify, or (for a server) when ClientAuth is
// RequestClientCert or RequireAnyClientCert, then this callback will
// be considered but the verifiedChains argument will always be nil.
VerifyPeerCertificate func(rawCerts [][]byte, verifiedChains [][]*x509.Certificate) error // Go 1.8

推荐阅读