doujiang1001 2017-11-09 06:58
浏览 101
已采纳

在Go gRPC处理程序中从客户端证书获取主题DN

I'm using Golang gRPC with mutual tls. Is it possible to get client's certificate subject DN from rpc method?

// ...
func main() {
    // ...
    creds := credentials.NewTLS(&tls.Config{
        ClientAuth:   tls.RequireAndVerifyClientCert,
        Certificates: []tls.Certificate{certificate},
        ClientCAs:    certPool,
        MinVersion:   tsl.VersionTLS12,
    })
    s := NewMyService()
    gs := grpc.NewServer(grpc.Creds(creds))
    RegisterGRPCZmqProxyServer(gs, s)
    er := gs.Serve(lis)
    // ...
}

// ...
func (s *myService) Foo(ctx context.Context, req *FooRequest) (*FooResonse, error) {
    $dn := // What should be here?
    // ...
}

Is it possible?

  • 写回答

1条回答 默认 最新

  • duanmanpi9358 2017-11-15 12:25
    关注

    You can use peer.Peer from ctx context.Context for access to the OID registry in x509.Certificate.

    func (s *myService) Foo(ctx context.Context, req *FooRequest) (*FooResonse, error) {
        p, ok := peer.FromContext(ctx)
            if ok {
                tlsInfo := p.AuthInfo.(credentials.TLSInfo)
                subject := tlsInfo.State.VerifiedChains[0][0].Subject
                // do something ...
            }
    }
    

    Subject it is pkix.Name and in docs write:

    Name represents an X.509 distinguished name

    And I used code from this answer and it well work.

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

报告相同问题?

悬赏问题

  • ¥20 matlab yalmip kkt 双层优化问题
  • ¥15 如何在3D高斯飞溅的渲染的场景中获得一个可控的旋转物体
  • ¥88 实在没有想法,需要个思路
  • ¥15 MATLAB报错输入参数太多
  • ¥15 python中合并修改日期相同的CSV文件并按照修改日期的名字命名文件
  • ¥15 有赏,i卡绘世画不出
  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入
  • ¥40 使用MATLAB解答线性代数问题
  • ¥15 COCOS的问题COCOS的问题