dongtu7205 2018-01-23 19:41
浏览 311
已采纳

使用golang相互TLS身份验证信任特定客户端

I've had a successful TLS mutual authentication client/server setup in Go for a while, but now looking to make some small tweaks.

Specifically, I'm wondering if there is a way to require only a specific client certificate for mutual auth.

I'm currently using something like this:

    // Load cert and build pool
    caCert, _ := ioutil.ReadFile(caPath)        
    caCertPool := x509.NewCertPool()
    caCertPool.AppendCertsFromPEM(caCert)

    // Require client authentication
    tlsConfig := &tls.Config{
        ClientAuth: tls.RequireAndVerifyClientCert,
        ClientCAs: caCertPool,
    }

Which works fine, however if the PEM file I'm reading in is actually a certificate chain (A issued by B, and B is a root CA), this will actually end up trusting any certificate issued by B, which I don't want.

Is there any way I can tweak this code to ONLY trust the specific A certificate?

It seems that if I only include A in the loaded PEM file, the server handshake code tells the client "send me all your certs signed by A", which of course is not what I want, as cert A is not signed by A.

Ideally I'd want to say "you require specifically certificate A" to connect successfully. Is there such a mechanism?

  • 写回答

1条回答 默认 最新

  • duanji1924 2018-01-23 19:51
    关注

    There is no mechanism to do this for you, but starting with go 1.8 you can specify your own callback using the VerifyPeerCertificate field in the tls.Config object (this works both on the server and client side).

    This takes a method with the following signature:

    func(rawCerts [][]byte, verifiedChains [][]*x509.Certificate) error
    

    Per the docs:

    It receives the raw ASN.1 certificates provided by the peer and also any verified chains that normal processing found.

    The certificate validation has already run, so you only need to apply your specific logic: examine the leaf certificates (first in each chain) in verifiedChains and check that the certificate is in the list of allowed certificates (just A in your case). If it is not: return an error and the handshake will fail.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Jenkins+k8s部署slave节点offline
  • ¥15 微信小游戏反编译后,出现找不到分包的情况
  • ¥15 如何实现从tello无人机上获取实时传输的视频流,然后将获取的视频通过yolov5进行检测
  • ¥15 WPF使用Canvas绘制矢量图问题
  • ¥15 用三极管设计一个单管共射放大电路
  • ¥15 孟德尔随机化r语言运行问题
  • ¥15 pyinstaller编译的时候出现No module named 'imp'
  • ¥15 nirs_kit中打码怎么看(打码文件是csv格式)
  • ¥15 怎么把多于硬盘空间放到根目录下
  • ¥15 Matlab问题解答有两个问题