我正在编写TLS监听器并接受新连接。在继续之前,必须根据SHA1列表验证新连接。问题是tls.Listen()
返回接口net.Listener
的侦听器。这意味着Accept()
将产生简体net.Conn
,而不是提供tls.Conn
的{{1}}。
接受的ConnectionState().PeerCertificates
太基本但是当我转换为更具体的net.Conn
类型时,我收到运行时异常。
*tls.Conn
这样的错误:
package main
import (
"crypto/tls"
"fmt"
"net"
)
type Service struct {
listener net.Listener
}
func (serv *Service)Listen(port int64) (err error) {
cert, err := tls.LoadX509KeyPair( "/etc/SCAMP/services/helloworld.crt","/etc/SCAMP/services/helloworld.key" )
if err != nil {
return
}
config := &tls.Config{
Certificates: []tls.Certificate{ cert },
}
serv.listener,err = tls.Listen("tcp", "127.0.0.1:30101", config)
if err != nil {
return err
}
return
}
func (serv *Service)AcceptRequests() {
for {
netConn,_ := serv.listener.Accept()
var tlsConn (*tls.Conn) = netConn.(*tls.Conn)
certs := tlsConn.ConnectionState().PeerCertificates
fmt.Printf("got certs: `%s`\n", certs)
}
}
func main() {
serv := new(Service)
serv.Listen(30100)
tlsConn, _ := tls.Dial("tcp","127.0.0.1:30101", &tls.Config{
InsecureSkipVerify: true,
})
tlsConn.Write([]byte("hey"))
}
答案 0 :(得分:1)
问题在于加载证书。这是一个工作示例,以防其他人偶然发现这个例子。您确实可以在Accept()
之外检查证书(但是从我正在学习的内容来看,这不是TLS中的常见情况,您通常会检查客户端的证书,因为各个客户端不需要证书)。 / p>
package main
import (
"crypto/tls"
"fmt"
"net"
"time"
)
type Service struct {
listener net.Listener
}
func (serv *Service)Listen(port int64) (err error) {
rsaCertPEM := `-----BEGIN CERTIFICATE-----
MIIGKjCCBBKgAwIBAgIJAOaBs5/Gdk7RMA0GCSqGSIb3DQEBBQUAMGsxHzAdBgNV
BAMTFlhhdmllcnMtTUJQIGhlbGxvd29ybGQxEjAQBgNVBAoTCVNDQU1QIEluYzEL
MAkGA1UEBhMCVVMxEzARBgNVBAgTCkNhbGlmb3JuaWExEjAQBgNVBAcTCVNhbiB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KWxB5HoxLQecDpZUQAKoPwaMxa54/GcgE/wN0XTht4OnyvLZ343X5TX8Mv
l27bSTNxS7ZeGAyHfZh6Sqbv/o2LGJSc1n6Nfri1JRns/f+2XckwQ4M5fbvMLJif
P0X+1xGNhlLmPj7+MUD7k4RMh5t24RW9O0VvxoQ85w2kyNjsh8/xCOD2OfoqBj8T
5yRmLQKCAQBZA6Wg5xuEIz1GI87cyEURS1jwDbXuBn6AjIbKCZ5AR/pcoYg8HdFz
Gjj0h6RrlsoK4z6AdBlJV5rpdCnzSxz65X11kFNVPfZRk7Cj/QyNSJ4WbfGykEK2
ZprMn2tRxZXORVHcypIfPxxYGwWuhgzk/m4uPt7cCbqHipuDAluXh9iMInRTZySr
4ZMaix9ipCHHBp2auH2HH9dXYIDBurR695FrVG1eGRbz9AiovmRSMudmfpYGiSFO
ASlLt4gEQic3xq2nDGOD29qR/EAo9Qumgws4IlDbKC9nszL+JYAQy28zecNjSQGv
8aeaEgHTw14vYs5I5Xvrl7rT1AMzSWVxAoIBAQCaDhdF/OyysP5WuQYd3v0J1yBM
bYc3y+RxdzCHtdxor5fQvTAN0gtp1KhrCmfoYdW4yUnOQZ6YoRzllERcQyoD2tGm
nEWm5YXQU8WZBGvCNfcL2qAVKZ3g498NJ3NqFMW9u2O1OlCSTRzLYp5Hdsx/7kl/
rzuo3vqtgYQqUkWDps6gjGkr55ztZHMoV6wGH9nf8tD0kqk+U6zIzaxiDj0U8+nr
xtEY4RYeEg1LV1I+kKvhEXx2mlyw7EbHTBXsfr7NdW3i7jFGN+mYywEhAqTlxJds
/IIDg+agTUeg+vYYbPA9nAwlTU1nzkhE3x3Aix/YRrtM70xFEF1v/sg4Oot/
-----END RSA PRIVATE KEY-----`
cert, err := tls.X509KeyPair([]byte(rsaCertPEM), []byte(rsaKeyPEM))
config := &tls.Config{
Certificates: []tls.Certificate{ cert },
}
serv.listener,err = tls.Listen("tcp", "127.0.0.1:30101", config)
if err != nil {
return err
}
return
}
func (serv *Service)AcceptRequests() {
for {
netConn,_ := serv.listener.Accept()
var tlsConn (*tls.Conn) = netConn.(*tls.Conn)
certs := tlsConn.ConnectionState().PeerCertificates
fmt.Printf("got certs: `%s`\n", certs)
}
}
func main() {
serv := new(Service)
err := serv.Listen(30100)
if err != nil {
fmt.Printf("go err: `%s`", err)
return
}
go serv.AcceptRequests()
time.Sleep(1)
tlsConn, _ := tls.Dial("tcp","127.0.0.1:30101", &tls.Config{
InsecureSkipVerify: true,
})
tlsConn.Write([]byte("hey"))
}