credentials.go 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. /*
  2. *
  3. * Copyright 2014, Google Inc.
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are
  8. * met:
  9. *
  10. * * Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * * Redistributions in binary form must reproduce the above
  13. * copyright notice, this list of conditions and the following disclaimer
  14. * in the documentation and/or other materials provided with the
  15. * distribution.
  16. * * Neither the name of Google Inc. nor the names of its
  17. * contributors may be used to endorse or promote products derived from
  18. * this software without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. *
  32. */
  33. // Package credentials implements various credentials supported by gRPC library,
  34. // which encapsulate all the state needed by a client to authenticate with a
  35. // server and make various assertions, e.g., about the client's identity, role,
  36. // or whether it is authorized to make a particular call.
  37. package credentials // import "google.golang.org/grpc/credentials"
  38. import (
  39. "crypto/tls"
  40. "crypto/x509"
  41. "errors"
  42. "fmt"
  43. "io/ioutil"
  44. "net"
  45. "strings"
  46. "golang.org/x/net/context"
  47. )
  48. var (
  49. // alpnProtoStr are the specified application level protocols for gRPC.
  50. alpnProtoStr = []string{"h2"}
  51. )
  52. // PerRPCCredentials defines the common interface for the credentials which need to
  53. // attach security information to every RPC (e.g., oauth2).
  54. type PerRPCCredentials interface {
  55. // GetRequestMetadata gets the current request metadata, refreshing
  56. // tokens if required. This should be called by the transport layer on
  57. // each request, and the data should be populated in headers or other
  58. // context. uri is the URI of the entry point for the request. When
  59. // supported by the underlying implementation, ctx can be used for
  60. // timeout and cancellation.
  61. // TODO(zhaoq): Define the set of the qualified keys instead of leaving
  62. // it as an arbitrary string.
  63. GetRequestMetadata(ctx context.Context, uri ...string) (map[string]string, error)
  64. // RequireTransportSecurity indicates whether the credentials requires
  65. // transport security.
  66. RequireTransportSecurity() bool
  67. }
  68. // ProtocolInfo provides information regarding the gRPC wire protocol version,
  69. // security protocol, security protocol version in use, server name, etc.
  70. type ProtocolInfo struct {
  71. // ProtocolVersion is the gRPC wire protocol version.
  72. ProtocolVersion string
  73. // SecurityProtocol is the security protocol in use.
  74. SecurityProtocol string
  75. // SecurityVersion is the security protocol version.
  76. SecurityVersion string
  77. // ServerName is the user-configured server name.
  78. ServerName string
  79. }
  80. // AuthInfo defines the common interface for the auth information the users are interested in.
  81. type AuthInfo interface {
  82. AuthType() string
  83. }
  84. var (
  85. // ErrConnDispatched indicates that rawConn has been dispatched out of gRPC
  86. // and the caller should not close rawConn.
  87. ErrConnDispatched = errors.New("credentials: rawConn is dispatched out of gRPC")
  88. )
  89. // TransportCredentials defines the common interface for all the live gRPC wire
  90. // protocols and supported transport security protocols (e.g., TLS, SSL).
  91. type TransportCredentials interface {
  92. // ClientHandshake does the authentication handshake specified by the corresponding
  93. // authentication protocol on rawConn for clients. It returns the authenticated
  94. // connection and the corresponding auth information about the connection.
  95. // Implementations must use the provided context to implement timely cancellation.
  96. ClientHandshake(context.Context, string, net.Conn) (net.Conn, AuthInfo, error)
  97. // ServerHandshake does the authentication handshake for servers. It returns
  98. // the authenticated connection and the corresponding auth information about
  99. // the connection.
  100. ServerHandshake(net.Conn) (net.Conn, AuthInfo, error)
  101. // Info provides the ProtocolInfo of this TransportCredentials.
  102. Info() ProtocolInfo
  103. // Clone makes a copy of this TransportCredentials.
  104. Clone() TransportCredentials
  105. // OverrideServerName overrides the server name used to verify the hostname on the returned certificates from the server.
  106. // gRPC internals also use it to override the virtual hosting name if it is set.
  107. // It must be called before dialing. Currently, this is only used by grpclb.
  108. OverrideServerName(string) error
  109. }
  110. // TLSInfo contains the auth information for a TLS authenticated connection.
  111. // It implements the AuthInfo interface.
  112. type TLSInfo struct {
  113. State tls.ConnectionState
  114. }
  115. // AuthType returns the type of TLSInfo as a string.
  116. func (t TLSInfo) AuthType() string {
  117. return "tls"
  118. }
  119. // tlsCreds is the credentials required for authenticating a connection using TLS.
  120. type tlsCreds struct {
  121. // TLS configuration
  122. config *tls.Config
  123. }
  124. func (c tlsCreds) Info() ProtocolInfo {
  125. return ProtocolInfo{
  126. SecurityProtocol: "tls",
  127. SecurityVersion: "1.2",
  128. ServerName: c.config.ServerName,
  129. }
  130. }
  131. func (c *tlsCreds) ClientHandshake(ctx context.Context, addr string, rawConn net.Conn) (_ net.Conn, _ AuthInfo, err error) {
  132. // use local cfg to avoid clobbering ServerName if using multiple endpoints
  133. cfg := cloneTLSConfig(c.config)
  134. if cfg.ServerName == "" {
  135. colonPos := strings.LastIndex(addr, ":")
  136. if colonPos == -1 {
  137. colonPos = len(addr)
  138. }
  139. cfg.ServerName = addr[:colonPos]
  140. }
  141. conn := tls.Client(rawConn, cfg)
  142. errChannel := make(chan error, 1)
  143. go func() {
  144. errChannel <- conn.Handshake()
  145. }()
  146. select {
  147. case err := <-errChannel:
  148. if err != nil {
  149. return nil, nil, err
  150. }
  151. case <-ctx.Done():
  152. return nil, nil, ctx.Err()
  153. }
  154. // TODO(zhaoq): Omit the auth info for client now. It is more for
  155. // information than anything else.
  156. return conn, nil, nil
  157. }
  158. func (c *tlsCreds) ServerHandshake(rawConn net.Conn) (net.Conn, AuthInfo, error) {
  159. conn := tls.Server(rawConn, c.config)
  160. if err := conn.Handshake(); err != nil {
  161. return nil, nil, err
  162. }
  163. return conn, TLSInfo{conn.ConnectionState()}, nil
  164. }
  165. func (c *tlsCreds) Clone() TransportCredentials {
  166. return NewTLS(c.config)
  167. }
  168. func (c *tlsCreds) OverrideServerName(serverNameOverride string) error {
  169. c.config.ServerName = serverNameOverride
  170. return nil
  171. }
  172. // NewTLS uses c to construct a TransportCredentials based on TLS.
  173. func NewTLS(c *tls.Config) TransportCredentials {
  174. tc := &tlsCreds{cloneTLSConfig(c)}
  175. tc.config.NextProtos = alpnProtoStr
  176. return tc
  177. }
  178. // NewClientTLSFromCert constructs a TLS from the input certificate for client.
  179. // serverNameOverride is for testing only. If set to a non empty string,
  180. // it will override the virtual host name of authority (e.g. :authority header field) in requests.
  181. func NewClientTLSFromCert(cp *x509.CertPool, serverNameOverride string) TransportCredentials {
  182. return NewTLS(&tls.Config{ServerName: serverNameOverride, RootCAs: cp})
  183. }
  184. // NewClientTLSFromFile constructs a TLS from the input certificate file for client.
  185. // serverNameOverride is for testing only. If set to a non empty string,
  186. // it will override the virtual host name of authority (e.g. :authority header field) in requests.
  187. func NewClientTLSFromFile(certFile, serverNameOverride string) (TransportCredentials, error) {
  188. b, err := ioutil.ReadFile(certFile)
  189. if err != nil {
  190. return nil, err
  191. }
  192. cp := x509.NewCertPool()
  193. if !cp.AppendCertsFromPEM(b) {
  194. return nil, fmt.Errorf("credentials: failed to append certificates")
  195. }
  196. return NewTLS(&tls.Config{ServerName: serverNameOverride, RootCAs: cp}), nil
  197. }
  198. // NewServerTLSFromCert constructs a TLS from the input certificate for server.
  199. func NewServerTLSFromCert(cert *tls.Certificate) TransportCredentials {
  200. return NewTLS(&tls.Config{Certificates: []tls.Certificate{*cert}})
  201. }
  202. // NewServerTLSFromFile constructs a TLS from the input certificate file and key
  203. // file for server.
  204. func NewServerTLSFromFile(certFile, keyFile string) (TransportCredentials, error) {
  205. cert, err := tls.LoadX509KeyPair(certFile, keyFile)
  206. if err != nil {
  207. return nil, err
  208. }
  209. return NewTLS(&tls.Config{Certificates: []tls.Certificate{cert}}), nil
  210. }