From 59472de6a9489a3c8f2fe0572361c2f5fea2f36d Mon Sep 17 00:00:00 2001 From: Shelikhoo Date: Thu, 15 Apr 2021 18:16:19 +0100 Subject: [PATCH] verify peer cert function for better man in the middle prevention --- transport/internet/tls/config.go | 26 ++++++++++++++++++++++++++ transport/internet/tls/config.pb.go | 23 ++++++++++++++++++++--- transport/internet/tls/config.proto | 7 +++++++ 3 files changed, 53 insertions(+), 3 deletions(-) diff --git a/transport/internet/tls/config.go b/transport/internet/tls/config.go index ae310d533..31d306ed3 100644 --- a/transport/internet/tls/config.go +++ b/transport/internet/tls/config.go @@ -3,8 +3,11 @@ package tls import ( + "crypto/hmac" + "crypto/sha256" "crypto/tls" "crypto/x509" + "encoding/hex" "strings" "sync" "time" @@ -170,6 +173,28 @@ func (c *Config) parseServerName() string { return c.ServerName } +func (c *Config) verifyPeerCert(rawCerts [][]byte, verifiedChains [][]*x509.Certificate) error { + if c.PinnedPeerCertificateChainSha256 != nil { + var hashValue []byte + for _, certValue := range rawCerts { + out := sha256.Sum256(certValue) + if hashValue == nil { + hashValue = out[:] + } else { + newHashValue := sha256.Sum256(append(hashValue, out[:]...)) + hashValue = newHashValue[:] + } + } + for _, v := range c.PinnedPeerCertificateChainSha256 { + if hmac.Equal(hashValue, v) { + return nil + } + } + return newError("peer cert is unrecognized: ", hex.EncodeToString(hashValue)) + } + return nil +} + // GetTLSConfig converts this Config into tls.Config. func (c *Config) GetTLSConfig(opts ...Option) *tls.Config { root, err := c.getCertPool() @@ -193,6 +218,7 @@ func (c *Config) GetTLSConfig(opts ...Option) *tls.Config { InsecureSkipVerify: c.AllowInsecure, NextProtos: c.NextProtocol, SessionTicketsDisabled: !c.EnableSessionResumption, + VerifyPeerCertificate: c.verifyPeerCert, } for _, opt := range opts { diff --git a/transport/internet/tls/config.pb.go b/transport/internet/tls/config.pb.go index 04658f41a..3a9e5abcd 100644 --- a/transport/internet/tls/config.pb.go +++ b/transport/internet/tls/config.pb.go @@ -152,6 +152,11 @@ type Config struct { // If true, root certificates on the system will not be loaded for // verification. DisableSystemRoot bool `protobuf:"varint,6,opt,name=disable_system_root,json=disableSystemRoot,proto3" json:"disable_system_root,omitempty"` + // @Document A pinned certificate chain sha256 hash. + //@Document If the server's hash does not match this value, the connection will be aborted. + //@Document This value replace allow_insecure. + //@Critical + PinnedPeerCertificateChainSha256 [][]byte `protobuf:"bytes,7,rep,name=pinned_peer_certificate_chain_sha256,json=pinnedPeerCertificateChainSha256,proto3" json:"pinned_peer_certificate_chain_sha256,omitempty"` } func (x *Config) Reset() { @@ -228,6 +233,13 @@ func (x *Config) GetDisableSystemRoot() bool { return false } +func (x *Config) GetPinnedPeerCertificateChainSha256() [][]byte { + if x != nil { + return x.PinnedPeerCertificateChainSha256 + } + return nil +} + var File_transport_internet_tls_config_proto protoreflect.FileDescriptor var file_transport_internet_tls_config_proto_rawDesc = []byte{ @@ -248,8 +260,8 @@ var file_transport_internet_tls_config_proto_rawDesc = []byte{ 0x65, 0x12, 0x10, 0x0a, 0x0c, 0x45, 0x4e, 0x43, 0x49, 0x50, 0x48, 0x45, 0x52, 0x4d, 0x45, 0x4e, 0x54, 0x10, 0x00, 0x12, 0x14, 0x0a, 0x10, 0x41, 0x55, 0x54, 0x48, 0x4f, 0x52, 0x49, 0x54, 0x59, 0x5f, 0x56, 0x45, 0x52, 0x49, 0x46, 0x59, 0x10, 0x01, 0x12, 0x13, 0x0a, 0x0f, 0x41, 0x55, 0x54, - 0x48, 0x4f, 0x52, 0x49, 0x54, 0x59, 0x5f, 0x49, 0x53, 0x53, 0x55, 0x45, 0x10, 0x02, 0x22, 0xb3, - 0x02, 0x0a, 0x06, 0x43, 0x6f, 0x6e, 0x66, 0x69, 0x67, 0x12, 0x25, 0x0a, 0x0e, 0x61, 0x6c, 0x6c, + 0x48, 0x4f, 0x52, 0x49, 0x54, 0x59, 0x5f, 0x49, 0x53, 0x53, 0x55, 0x45, 0x10, 0x02, 0x22, 0x83, + 0x03, 0x0a, 0x06, 0x43, 0x6f, 0x6e, 0x66, 0x69, 0x67, 0x12, 0x25, 0x0a, 0x0e, 0x61, 0x6c, 0x6c, 0x6f, 0x77, 0x5f, 0x69, 0x6e, 0x73, 0x65, 0x63, 0x75, 0x72, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x08, 0x52, 0x0d, 0x61, 0x6c, 0x6c, 0x6f, 0x77, 0x49, 0x6e, 0x73, 0x65, 0x63, 0x75, 0x72, 0x65, 0x12, 0x50, 0x0a, 0x0b, 0x63, 0x65, 0x72, 0x74, 0x69, 0x66, 0x69, 0x63, 0x61, 0x74, 0x65, 0x18, @@ -268,7 +280,12 @@ var file_transport_internet_tls_config_proto_rawDesc = []byte{ 0x74, 0x69, 0x6f, 0x6e, 0x12, 0x2e, 0x0a, 0x13, 0x64, 0x69, 0x73, 0x61, 0x62, 0x6c, 0x65, 0x5f, 0x73, 0x79, 0x73, 0x74, 0x65, 0x6d, 0x5f, 0x72, 0x6f, 0x6f, 0x74, 0x18, 0x06, 0x20, 0x01, 0x28, 0x08, 0x52, 0x11, 0x64, 0x69, 0x73, 0x61, 0x62, 0x6c, 0x65, 0x53, 0x79, 0x73, 0x74, 0x65, 0x6d, - 0x52, 0x6f, 0x6f, 0x74, 0x42, 0x84, 0x01, 0x0a, 0x25, 0x63, 0x6f, 0x6d, 0x2e, 0x76, 0x32, 0x72, + 0x52, 0x6f, 0x6f, 0x74, 0x12, 0x4e, 0x0a, 0x24, 0x70, 0x69, 0x6e, 0x6e, 0x65, 0x64, 0x5f, 0x70, + 0x65, 0x65, 0x72, 0x5f, 0x63, 0x65, 0x72, 0x74, 0x69, 0x66, 0x69, 0x63, 0x61, 0x74, 0x65, 0x5f, + 0x63, 0x68, 0x61, 0x69, 0x6e, 0x5f, 0x73, 0x68, 0x61, 0x32, 0x35, 0x36, 0x18, 0x07, 0x20, 0x03, + 0x28, 0x0c, 0x52, 0x20, 0x70, 0x69, 0x6e, 0x6e, 0x65, 0x64, 0x50, 0x65, 0x65, 0x72, 0x43, 0x65, + 0x72, 0x74, 0x69, 0x66, 0x69, 0x63, 0x61, 0x74, 0x65, 0x43, 0x68, 0x61, 0x69, 0x6e, 0x53, 0x68, + 0x61, 0x32, 0x35, 0x36, 0x42, 0x84, 0x01, 0x0a, 0x25, 0x63, 0x6f, 0x6d, 0x2e, 0x76, 0x32, 0x72, 0x61, 0x79, 0x2e, 0x63, 0x6f, 0x72, 0x65, 0x2e, 0x74, 0x72, 0x61, 0x6e, 0x73, 0x70, 0x6f, 0x72, 0x74, 0x2e, 0x69, 0x6e, 0x74, 0x65, 0x72, 0x6e, 0x65, 0x74, 0x2e, 0x74, 0x6c, 0x73, 0x50, 0x01, 0x5a, 0x35, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x76, 0x32, 0x66, diff --git a/transport/internet/tls/config.proto b/transport/internet/tls/config.proto index ed5137b27..6d8e77435 100644 --- a/transport/internet/tls/config.proto +++ b/transport/internet/tls/config.proto @@ -41,4 +41,11 @@ message Config { // If true, root certificates on the system will not be loaded for // verification. bool disable_system_root = 6; + + /* @Document A pinned certificate chain sha256 hash. + @Document If the server's hash does not match this value, the connection will be aborted. + @Document This value replace allow_insecure. + @Critical + */ + repeated bytes pinned_peer_certificate_chain_sha256 = 7; }