tls.go 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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. "net"
  25. "net/url"
  26. "os"
  27. credinternal "google.golang.org/grpc/internal/credentials"
  28. )
  29. // TLSInfo contains the auth information for a TLS authenticated connection.
  30. // It implements the AuthInfo interface.
  31. type TLSInfo struct {
  32. State tls.ConnectionState
  33. CommonAuthInfo
  34. // This API is experimental.
  35. SPIFFEID *url.URL
  36. }
  37. // AuthType returns the type of TLSInfo as a string.
  38. func (t TLSInfo) AuthType() string {
  39. return "tls"
  40. }
  41. // cipherSuiteLookup returns the string version of a TLS cipher suite ID.
  42. func cipherSuiteLookup(cipherSuiteID uint16) string {
  43. for _, s := range tls.CipherSuites() {
  44. if s.ID == cipherSuiteID {
  45. return s.Name
  46. }
  47. }
  48. for _, s := range tls.InsecureCipherSuites() {
  49. if s.ID == cipherSuiteID {
  50. return s.Name
  51. }
  52. }
  53. return fmt.Sprintf("unknown ID: %v", cipherSuiteID)
  54. }
  55. // GetSecurityValue returns security info requested by channelz.
  56. func (t TLSInfo) GetSecurityValue() ChannelzSecurityValue {
  57. v := &TLSChannelzSecurityValue{
  58. StandardName: cipherSuiteLookup(t.State.CipherSuite),
  59. }
  60. // Currently there's no way to get LocalCertificate info from tls package.
  61. if len(t.State.PeerCertificates) > 0 {
  62. v.RemoteCertificate = t.State.PeerCertificates[0].Raw
  63. }
  64. return v
  65. }
  66. // tlsCreds is the credentials required for authenticating a connection using TLS.
  67. type tlsCreds struct {
  68. // TLS configuration
  69. config *tls.Config
  70. }
  71. func (c tlsCreds) Info() ProtocolInfo {
  72. return ProtocolInfo{
  73. SecurityProtocol: "tls",
  74. SecurityVersion: "1.2",
  75. ServerName: c.config.ServerName,
  76. }
  77. }
  78. func (c *tlsCreds) ClientHandshake(ctx context.Context, authority string, rawConn net.Conn) (_ net.Conn, _ AuthInfo, err error) {
  79. // use local cfg to avoid clobbering ServerName if using multiple endpoints
  80. cfg := credinternal.CloneTLSConfig(c.config)
  81. if cfg.ServerName == "" {
  82. serverName, _, err := net.SplitHostPort(authority)
  83. if err != nil {
  84. // If the authority had no host port or if the authority cannot be parsed, use it as-is.
  85. serverName = authority
  86. }
  87. cfg.ServerName = serverName
  88. }
  89. conn := tls.Client(rawConn, cfg)
  90. errChannel := make(chan error, 1)
  91. go func() {
  92. errChannel <- conn.Handshake()
  93. close(errChannel)
  94. }()
  95. select {
  96. case err := <-errChannel:
  97. if err != nil {
  98. conn.Close()
  99. return nil, nil, err
  100. }
  101. case <-ctx.Done():
  102. conn.Close()
  103. return nil, nil, ctx.Err()
  104. }
  105. tlsInfo := TLSInfo{
  106. State: conn.ConnectionState(),
  107. CommonAuthInfo: CommonAuthInfo{
  108. SecurityLevel: PrivacyAndIntegrity,
  109. },
  110. }
  111. id := credinternal.SPIFFEIDFromState(conn.ConnectionState())
  112. if id != nil {
  113. tlsInfo.SPIFFEID = id
  114. }
  115. return credinternal.WrapSyscallConn(rawConn, conn), tlsInfo, nil
  116. }
  117. func (c *tlsCreds) ServerHandshake(rawConn net.Conn) (net.Conn, AuthInfo, error) {
  118. conn := tls.Server(rawConn, c.config)
  119. if err := conn.Handshake(); err != nil {
  120. conn.Close()
  121. return nil, nil, err
  122. }
  123. tlsInfo := TLSInfo{
  124. State: conn.ConnectionState(),
  125. CommonAuthInfo: CommonAuthInfo{
  126. SecurityLevel: PrivacyAndIntegrity,
  127. },
  128. }
  129. id := credinternal.SPIFFEIDFromState(conn.ConnectionState())
  130. if id != nil {
  131. tlsInfo.SPIFFEID = id
  132. }
  133. return credinternal.WrapSyscallConn(rawConn, conn), tlsInfo, nil
  134. }
  135. func (c *tlsCreds) Clone() TransportCredentials {
  136. return NewTLS(c.config)
  137. }
  138. func (c *tlsCreds) OverrideServerName(serverNameOverride string) error {
  139. c.config.ServerName = serverNameOverride
  140. return nil
  141. }
  142. // The following cipher suites are forbidden for use with HTTP/2 by
  143. // https://datatracker.ietf.org/doc/html/rfc7540#appendix-A
  144. var tls12ForbiddenCipherSuites = map[uint16]struct{}{
  145. tls.TLS_RSA_WITH_AES_128_CBC_SHA: {},
  146. tls.TLS_RSA_WITH_AES_256_CBC_SHA: {},
  147. tls.TLS_RSA_WITH_AES_128_GCM_SHA256: {},
  148. tls.TLS_RSA_WITH_AES_256_GCM_SHA384: {},
  149. tls.TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA: {},
  150. tls.TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA: {},
  151. tls.TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA: {},
  152. tls.TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA: {},
  153. }
  154. // NewTLS uses c to construct a TransportCredentials based on TLS.
  155. func NewTLS(c *tls.Config) TransportCredentials {
  156. tc := &tlsCreds{credinternal.CloneTLSConfig(c)}
  157. tc.config.NextProtos = credinternal.AppendH2ToNextProtos(tc.config.NextProtos)
  158. // If the user did not configure a MinVersion and did not configure a
  159. // MaxVersion < 1.2, use MinVersion=1.2, which is required by
  160. // https://datatracker.ietf.org/doc/html/rfc7540#section-9.2
  161. if tc.config.MinVersion == 0 && (tc.config.MaxVersion == 0 || tc.config.MaxVersion >= tls.VersionTLS12) {
  162. tc.config.MinVersion = tls.VersionTLS12
  163. }
  164. // If the user did not configure CipherSuites, use all "secure" cipher
  165. // suites reported by the TLS package, but remove some explicitly forbidden
  166. // by https://datatracker.ietf.org/doc/html/rfc7540#appendix-A
  167. if tc.config.CipherSuites == nil {
  168. for _, cs := range tls.CipherSuites() {
  169. if _, ok := tls12ForbiddenCipherSuites[cs.ID]; !ok {
  170. tc.config.CipherSuites = append(tc.config.CipherSuites, cs.ID)
  171. }
  172. }
  173. }
  174. return tc
  175. }
  176. // NewClientTLSFromCert constructs TLS credentials from the provided root
  177. // certificate authority certificate(s) to validate server connections. If
  178. // certificates to establish the identity of the client need to be included in
  179. // the credentials (eg: for mTLS), use NewTLS instead, where a complete
  180. // tls.Config can be specified.
  181. // serverNameOverride is for testing only. If set to a non empty string,
  182. // it will override the virtual host name of authority (e.g. :authority header
  183. // field) in requests.
  184. func NewClientTLSFromCert(cp *x509.CertPool, serverNameOverride string) TransportCredentials {
  185. return NewTLS(&tls.Config{ServerName: serverNameOverride, RootCAs: cp})
  186. }
  187. // NewClientTLSFromFile constructs TLS credentials from the provided root
  188. // certificate authority certificate file(s) to validate server connections. If
  189. // certificates to establish the identity of the client need to be included in
  190. // the credentials (eg: for mTLS), use NewTLS instead, where a complete
  191. // tls.Config can be specified.
  192. // serverNameOverride is for testing only. If set to a non empty string,
  193. // it will override the virtual host name of authority (e.g. :authority header
  194. // field) in requests.
  195. func NewClientTLSFromFile(certFile, serverNameOverride string) (TransportCredentials, error) {
  196. b, err := os.ReadFile(certFile)
  197. if err != nil {
  198. return nil, err
  199. }
  200. cp := x509.NewCertPool()
  201. if !cp.AppendCertsFromPEM(b) {
  202. return nil, fmt.Errorf("credentials: failed to append certificates")
  203. }
  204. return NewTLS(&tls.Config{ServerName: serverNameOverride, RootCAs: cp}), nil
  205. }
  206. // NewServerTLSFromCert constructs TLS credentials from the input certificate for server.
  207. func NewServerTLSFromCert(cert *tls.Certificate) TransportCredentials {
  208. return NewTLS(&tls.Config{Certificates: []tls.Certificate{*cert}})
  209. }
  210. // NewServerTLSFromFile constructs TLS credentials from the input certificate file and key
  211. // file for server.
  212. func NewServerTLSFromFile(certFile, keyFile string) (TransportCredentials, error) {
  213. cert, err := tls.LoadX509KeyPair(certFile, keyFile)
  214. if err != nil {
  215. return nil, err
  216. }
  217. return NewTLS(&tls.Config{Certificates: []tls.Certificate{cert}}), nil
  218. }
  219. // TLSChannelzSecurityValue defines the struct that TLS protocol should return
  220. // from GetSecurityValue(), containing security info like cipher and certificate used.
  221. //
  222. // # Experimental
  223. //
  224. // Notice: This type is EXPERIMENTAL and may be changed or removed in a
  225. // later release.
  226. type TLSChannelzSecurityValue struct {
  227. ChannelzSecurityValue
  228. StandardName string
  229. LocalCertificate []byte
  230. RemoteCertificate []byte
  231. }