【问题标题】:How to access the client's certificate in Go?如何在 Go 中访问客户端的证书?
【发布时间】:2015-01-14 10:03:54
【问题描述】:

以下是我在 Go 中用于 HTTPS 服务器的代码。服务器要求客户端提供 SSL 证书,但不验证证书(按设计)。

如果您想尝试一下,可以生成如下的私钥/公钥对:

$ openssl genrsa -out private.pem 2048
$ openssl req -new -x509 -key private.pem -out public.pem -days 365

有谁知道我如何在处理程序中访问客户端证书?假设我想报告有关客户端向客户端提供的证书的一些属性。

谢谢, 克里斯。

package main

import (
    "fmt"
    "crypto/tls"
    "net/http"
)

const (
    PORT       = ":8443"
    PRIV_KEY   = "./private.pem"
    PUBLIC_KEY = "./public.pem"
)

func rootHandler(w http.ResponseWriter, r *http.Request) {
    fmt.Fprint(w, "Nobody should read this.")
}

func main() {

    server := &http.Server{
        TLSConfig: &tls.Config{
            ClientAuth: tls.RequireAnyClientCert,
            MinVersion: tls.VersionTLS12,
        },
        Addr: "127.0.0.1:8443",
    }

    http.HandleFunc("/", rootHandler)
    err := server.ListenAndServeTLS(PUBLIC_KEY, PRIV_KEY)
    if err != nil {
        fmt.Printf("main(): %s\n", err)
    }
}

【问题讨论】:

    标签: ssl go


    【解决方案1】:

    似乎r *http.RequestTLS *tls.ConnectionState 字段,而PeerCertificates []*x509.Certificate 字段,所以

    fmt.Fprint(w, r.TLS.PeerCertificates)
    

    应该可以

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-02-28
      • 1970-01-01
      • 2016-09-18
      • 2017-06-26
      • 2016-11-29
      • 1970-01-01
      • 1970-01-01
      • 2017-10-07
      相关资源
      最近更新 更多