tls.go 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. /*
  2. *
  3. * Copyright 2014 gRPC authors.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. */
  18. package credentials
  19. import (
  20. "context"
  21. "crypto/tls"
  22. "crypto/x509"
  23. "fmt"
  24. "io/ioutil"
  25. "net"
  26. "net/url"
  27. "google.golang.org/grpc/credentials/internal"
  28. credinternal "google.golang.org/grpc/internal/credentials"
  29. )
  30. // TLSInfo contains the auth information for a TLS authenticated connection.
  31. // It implements the AuthInfo interface.
  32. type TLSInfo struct {
  33. State tls.ConnectionState
  34. CommonAuthInfo
  35. // This API is experimental.
  36. SPIFFEID *url.URL
  37. }
  38. // AuthType returns the type of TLSInfo as a string.
  39. func (t TLSInfo) AuthType() string {
  40. return "tls"
  41. }
  42. // GetSecurityValue returns security info requested by channelz.
  43. func (t TLSInfo) GetSecurityValue() ChannelzSecurityValue {
  44. v := &TLSChannelzSecurityValue{
  45. StandardName: cipherSuiteLookup[t.State.CipherSuite],
  46. }
  47. // Currently there's no way to get LocalCertificate info from tls package.
  48. if len(t.State.PeerCertificates) > 0 {
  49. v.RemoteCertificate = t.State.PeerCertificates[0].Raw
  50. }
  51. return v
  52. }
  53. // tlsCreds is the credentials required for authenticating a connection using TLS.
  54. type tlsCreds struct {
  55. // TLS configuration
  56. config *tls.Config
  57. }
  58. func (c tlsCreds) Info() ProtocolInfo {
  59. return ProtocolInfo{
  60. SecurityProtocol: "tls",
  61. SecurityVersion: "1.2",
  62. ServerName: c.config.ServerName,
  63. }
  64. }
  65. func (c *tlsCreds) ClientHandshake(ctx context.Context, authority string, rawConn net.Conn) (_ net.Conn, _ AuthInfo, err error) {
  66. // use local cfg to avoid clobbering ServerName if using multiple endpoints
  67. cfg := cloneTLSConfig(c.config)
  68. if cfg.ServerName == "" {
  69. serverName, _, err := net.SplitHostPort(authority)
  70. if err != nil {
  71. // If the authority had no host port or if the authority cannot be parsed, use it as-is.
  72. serverName = authority
  73. }
  74. cfg.ServerName = serverName
  75. }
  76. conn := tls.Client(rawConn, cfg)
  77. errChannel := make(chan error, 1)
  78. go func() {
  79. errChannel <- conn.Handshake()
  80. close(errChannel)
  81. }()
  82. select {
  83. case err := <-errChannel:
  84. if err != nil {
  85. conn.Close()
  86. return nil, nil, err
  87. }
  88. case <-ctx.Done():
  89. conn.Close()
  90. return nil, nil, ctx.Err()
  91. }
  92. tlsInfo := TLSInfo{
  93. State: conn.ConnectionState(),
  94. CommonAuthInfo: CommonAuthInfo{
  95. SecurityLevel: PrivacyAndIntegrity,
  96. },
  97. }
  98. id := credinternal.SPIFFEIDFromState(conn.ConnectionState())
  99. if id != nil {
  100. tlsInfo.SPIFFEID = id
  101. }
  102. return internal.WrapSyscallConn(rawConn, conn), tlsInfo, nil
  103. }
  104. func (c *tlsCreds) ServerHandshake(rawConn net.Conn) (net.Conn, AuthInfo, error) {
  105. conn := tls.Server(rawConn, c.config)
  106. if err := conn.Handshake(); err != nil {
  107. conn.Close()
  108. return nil, nil, err
  109. }
  110. tlsInfo := TLSInfo{
  111. State: conn.ConnectionState(),
  112. CommonAuthInfo: CommonAuthInfo{
  113. SecurityLevel: PrivacyAndIntegrity,
  114. },
  115. }
  116. id := credinternal.SPIFFEIDFromState(conn.ConnectionState())
  117. if id != nil {
  118. tlsInfo.SPIFFEID = id
  119. }
  120. return internal.WrapSyscallConn(rawConn, conn), tlsInfo, nil
  121. }
  122. func (c *tlsCreds) Clone() TransportCredentials {
  123. return NewTLS(c.config)
  124. }
  125. func (c *tlsCreds) OverrideServerName(serverNameOverride string) error {
  126. c.config.ServerName = serverNameOverride
  127. return nil
  128. }
  129. const alpnProtoStrH2 = "h2"
  130. func appendH2ToNextProtos(ps []string) []string {
  131. for _, p := range ps {
  132. if p == alpnProtoStrH2 {
  133. return ps
  134. }
  135. }
  136. ret := make([]string, 0, len(ps)+1)
  137. ret = append(ret, ps...)
  138. return append(ret, alpnProtoStrH2)
  139. }
  140. // NewTLS uses c to construct a TransportCredentials based on TLS.
  141. func NewTLS(c *tls.Config) TransportCredentials {
  142. tc := &tlsCreds{cloneTLSConfig(c)}
  143. tc.config.NextProtos = appendH2ToNextProtos(tc.config.NextProtos)
  144. return tc
  145. }
  146. // NewClientTLSFromCert constructs TLS credentials from the provided root
  147. // certificate authority certificate(s) to validate server connections. If
  148. // certificates to establish the identity of the client need to be included in
  149. // the credentials (eg: for mTLS), use NewTLS instead, where a complete
  150. // tls.Config can be specified.
  151. // serverNameOverride is for testing only. If set to a non empty string,
  152. // it will override the virtual host name of authority (e.g. :authority header
  153. // field) in requests.
  154. func NewClientTLSFromCert(cp *x509.CertPool, serverNameOverride string) TransportCredentials {
  155. return NewTLS(&tls.Config{ServerName: serverNameOverride, RootCAs: cp})
  156. }
  157. // NewClientTLSFromFile constructs TLS credentials from the provided root
  158. // certificate authority certificate file(s) to validate server connections. If
  159. // certificates to establish the identity of the client need to be included in
  160. // the credentials (eg: for mTLS), use NewTLS instead, where a complete
  161. // tls.Config can be specified.
  162. // serverNameOverride is for testing only. If set to a non empty string,
  163. // it will override the virtual host name of authority (e.g. :authority header
  164. // field) in requests.
  165. func NewClientTLSFromFile(certFile, serverNameOverride string) (TransportCredentials, error) {
  166. b, err := ioutil.ReadFile(certFile)
  167. if err != nil {
  168. return nil, err
  169. }
  170. cp := x509.NewCertPool()
  171. if !cp.AppendCertsFromPEM(b) {
  172. return nil, fmt.Errorf("credentials: failed to append certificates")
  173. }
  174. return NewTLS(&tls.Config{ServerName: serverNameOverride, RootCAs: cp}), nil
  175. }
  176. // NewServerTLSFromCert constructs TLS credentials from the input certificate for server.
  177. func NewServerTLSFromCert(cert *tls.Certificate) TransportCredentials {
  178. return NewTLS(&tls.Config{Certificates: []tls.Certificate{*cert}})
  179. }
  180. // NewServerTLSFromFile constructs TLS credentials from the input certificate file and key
  181. // file for server.
  182. func NewServerTLSFromFile(certFile, keyFile string) (TransportCredentials, error) {
  183. cert, err := tls.LoadX509KeyPair(certFile, keyFile)
  184. if err != nil {
  185. return nil, err
  186. }
  187. return NewTLS(&tls.Config{Certificates: []tls.Certificate{cert}}), nil
  188. }
  189. // TLSChannelzSecurityValue defines the struct that TLS protocol should return
  190. // from GetSecurityValue(), containing security info like cipher and certificate used.
  191. //
  192. // This API is EXPERIMENTAL.
  193. type TLSChannelzSecurityValue struct {
  194. ChannelzSecurityValue
  195. StandardName string
  196. LocalCertificate []byte
  197. RemoteCertificate []byte
  198. }
  199. var cipherSuiteLookup = map[uint16]string{
  200. tls.TLS_RSA_WITH_RC4_128_SHA: "TLS_RSA_WITH_RC4_128_SHA",
  201. tls.TLS_RSA_WITH_3DES_EDE_CBC_SHA: "TLS_RSA_WITH_3DES_EDE_CBC_SHA",
  202. tls.TLS_RSA_WITH_AES_128_CBC_SHA: "TLS_RSA_WITH_AES_128_CBC_SHA",
  203. tls.TLS_RSA_WITH_AES_256_CBC_SHA: "TLS_RSA_WITH_AES_256_CBC_SHA",
  204. tls.TLS_RSA_WITH_AES_128_GCM_SHA256: "TLS_RSA_WITH_AES_128_GCM_SHA256",
  205. tls.TLS_RSA_WITH_AES_256_GCM_SHA384: "TLS_RSA_WITH_AES_256_GCM_SHA384",
  206. tls.TLS_ECDHE_ECDSA_WITH_RC4_128_SHA: "TLS_ECDHE_ECDSA_WITH_RC4_128_SHA",
  207. tls.TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA: "TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA",
  208. tls.TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA: "TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA",
  209. tls.TLS_ECDHE_RSA_WITH_RC4_128_SHA: "TLS_ECDHE_RSA_WITH_RC4_128_SHA",
  210. tls.TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA: "TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA",
  211. tls.TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA: "TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA",
  212. tls.TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA: "TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA",
  213. tls.TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256: "TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256",
  214. tls.TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256: "TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256",
  215. tls.TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384: "TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384",
  216. tls.TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384: "TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384",
  217. tls.TLS_FALLBACK_SCSV: "TLS_FALLBACK_SCSV",
  218. tls.TLS_RSA_WITH_AES_128_CBC_SHA256: "TLS_RSA_WITH_AES_128_CBC_SHA256",
  219. tls.TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256: "TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256",
  220. tls.TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256: "TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256",
  221. tls.TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305: "TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305",
  222. tls.TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305: "TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305",
  223. }
  224. // cloneTLSConfig returns a shallow clone of the exported
  225. // fields of cfg, ignoring the unexported sync.Once, which
  226. // contains a mutex and must not be copied.
  227. //
  228. // If cfg is nil, a new zero tls.Config is returned.
  229. //
  230. // TODO: inline this function if possible.
  231. func cloneTLSConfig(cfg *tls.Config) *tls.Config {
  232. if cfg == nil {
  233. return &tls.Config{}
  234. }
  235. return cfg.Clone()
  236. }