handshaker.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. /*
  2. *
  3. * Copyright 2018 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 handshaker provides ALTS handshaking functionality for GCP.
  19. package handshaker
  20. import (
  21. "context"
  22. "errors"
  23. "fmt"
  24. "io"
  25. "net"
  26. "sync"
  27. grpc "google.golang.org/grpc"
  28. "google.golang.org/grpc/codes"
  29. "google.golang.org/grpc/credentials"
  30. core "google.golang.org/grpc/credentials/alts/internal"
  31. "google.golang.org/grpc/credentials/alts/internal/authinfo"
  32. "google.golang.org/grpc/credentials/alts/internal/conn"
  33. altsgrpc "google.golang.org/grpc/credentials/alts/internal/proto/grpc_gcp"
  34. altspb "google.golang.org/grpc/credentials/alts/internal/proto/grpc_gcp"
  35. )
  36. const (
  37. // The maximum byte size of receive frames.
  38. frameLimit = 64 * 1024 // 64 KB
  39. rekeyRecordProtocolName = "ALTSRP_GCM_AES128_REKEY"
  40. // maxPendingHandshakes represents the maximum number of concurrent
  41. // handshakes.
  42. maxPendingHandshakes = 100
  43. )
  44. var (
  45. hsProtocol = altspb.HandshakeProtocol_ALTS
  46. appProtocols = []string{"grpc"}
  47. recordProtocols = []string{rekeyRecordProtocolName}
  48. keyLength = map[string]int{
  49. rekeyRecordProtocolName: 44,
  50. }
  51. altsRecordFuncs = map[string]conn.ALTSRecordFunc{
  52. // ALTS handshaker protocols.
  53. rekeyRecordProtocolName: func(s core.Side, keyData []byte) (conn.ALTSRecordCrypto, error) {
  54. return conn.NewAES128GCMRekey(s, keyData)
  55. },
  56. }
  57. // control number of concurrent created (but not closed) handshakers.
  58. mu sync.Mutex
  59. concurrentHandshakes = int64(0)
  60. // errDropped occurs when maxPendingHandshakes is reached.
  61. errDropped = errors.New("maximum number of concurrent ALTS handshakes is reached")
  62. // errOutOfBound occurs when the handshake service returns a consumed
  63. // bytes value larger than the buffer that was passed to it originally.
  64. errOutOfBound = errors.New("handshaker service consumed bytes value is out-of-bound")
  65. )
  66. func init() {
  67. for protocol, f := range altsRecordFuncs {
  68. if err := conn.RegisterProtocol(protocol, f); err != nil {
  69. panic(err)
  70. }
  71. }
  72. }
  73. func acquire() bool {
  74. mu.Lock()
  75. // If we need n to be configurable, we can pass it as an argument.
  76. n := int64(1)
  77. success := maxPendingHandshakes-concurrentHandshakes >= n
  78. if success {
  79. concurrentHandshakes += n
  80. }
  81. mu.Unlock()
  82. return success
  83. }
  84. func release() {
  85. mu.Lock()
  86. // If we need n to be configurable, we can pass it as an argument.
  87. n := int64(1)
  88. concurrentHandshakes -= n
  89. if concurrentHandshakes < 0 {
  90. mu.Unlock()
  91. panic("bad release")
  92. }
  93. mu.Unlock()
  94. }
  95. // ClientHandshakerOptions contains the client handshaker options that can
  96. // provided by the caller.
  97. type ClientHandshakerOptions struct {
  98. // ClientIdentity is the handshaker client local identity.
  99. ClientIdentity *altspb.Identity
  100. // TargetName is the server service account name for secure name
  101. // checking.
  102. TargetName string
  103. // TargetServiceAccounts contains a list of expected target service
  104. // accounts. One of these accounts should match one of the accounts in
  105. // the handshaker results. Otherwise, the handshake fails.
  106. TargetServiceAccounts []string
  107. // RPCVersions specifies the gRPC versions accepted by the client.
  108. RPCVersions *altspb.RpcProtocolVersions
  109. }
  110. // ServerHandshakerOptions contains the server handshaker options that can
  111. // provided by the caller.
  112. type ServerHandshakerOptions struct {
  113. // RPCVersions specifies the gRPC versions accepted by the server.
  114. RPCVersions *altspb.RpcProtocolVersions
  115. }
  116. // DefaultClientHandshakerOptions returns the default client handshaker options.
  117. func DefaultClientHandshakerOptions() *ClientHandshakerOptions {
  118. return &ClientHandshakerOptions{}
  119. }
  120. // DefaultServerHandshakerOptions returns the default client handshaker options.
  121. func DefaultServerHandshakerOptions() *ServerHandshakerOptions {
  122. return &ServerHandshakerOptions{}
  123. }
  124. // TODO: add support for future local and remote endpoint in both client options
  125. // and server options (server options struct does not exist now. When
  126. // caller can provide endpoints, it should be created.
  127. // altsHandshaker is used to complete an ALTS handshake between client and
  128. // server. This handshaker talks to the ALTS handshaker service in the metadata
  129. // server.
  130. type altsHandshaker struct {
  131. // RPC stream used to access the ALTS Handshaker service.
  132. stream altsgrpc.HandshakerService_DoHandshakeClient
  133. // the connection to the peer.
  134. conn net.Conn
  135. // a virtual connection to the ALTS handshaker service.
  136. clientConn *grpc.ClientConn
  137. // client handshake options.
  138. clientOpts *ClientHandshakerOptions
  139. // server handshake options.
  140. serverOpts *ServerHandshakerOptions
  141. // defines the side doing the handshake, client or server.
  142. side core.Side
  143. }
  144. // NewClientHandshaker creates a core.Handshaker that performs a client-side
  145. // ALTS handshake by acting as a proxy between the peer and the ALTS handshaker
  146. // service in the metadata server.
  147. func NewClientHandshaker(ctx context.Context, conn *grpc.ClientConn, c net.Conn, opts *ClientHandshakerOptions) (core.Handshaker, error) {
  148. return &altsHandshaker{
  149. stream: nil,
  150. conn: c,
  151. clientConn: conn,
  152. clientOpts: opts,
  153. side: core.ClientSide,
  154. }, nil
  155. }
  156. // NewServerHandshaker creates a core.Handshaker that performs a server-side
  157. // ALTS handshake by acting as a proxy between the peer and the ALTS handshaker
  158. // service in the metadata server.
  159. func NewServerHandshaker(ctx context.Context, conn *grpc.ClientConn, c net.Conn, opts *ServerHandshakerOptions) (core.Handshaker, error) {
  160. return &altsHandshaker{
  161. stream: nil,
  162. conn: c,
  163. clientConn: conn,
  164. serverOpts: opts,
  165. side: core.ServerSide,
  166. }, nil
  167. }
  168. // ClientHandshake starts and completes a client ALTS handshake for GCP. Once
  169. // done, ClientHandshake returns a secure connection.
  170. func (h *altsHandshaker) ClientHandshake(ctx context.Context) (net.Conn, credentials.AuthInfo, error) {
  171. if !acquire() {
  172. return nil, nil, errDropped
  173. }
  174. defer release()
  175. if h.side != core.ClientSide {
  176. return nil, nil, errors.New("only handshakers created using NewClientHandshaker can perform a client handshaker")
  177. }
  178. // TODO(matthewstevenson88): Change unit tests to use public APIs so
  179. // that h.stream can unconditionally be set based on h.clientConn.
  180. if h.stream == nil {
  181. stream, err := altsgrpc.NewHandshakerServiceClient(h.clientConn).DoHandshake(ctx)
  182. if err != nil {
  183. return nil, nil, fmt.Errorf("failed to establish stream to ALTS handshaker service: %v", err)
  184. }
  185. h.stream = stream
  186. }
  187. // Create target identities from service account list.
  188. targetIdentities := make([]*altspb.Identity, 0, len(h.clientOpts.TargetServiceAccounts))
  189. for _, account := range h.clientOpts.TargetServiceAccounts {
  190. targetIdentities = append(targetIdentities, &altspb.Identity{
  191. IdentityOneof: &altspb.Identity_ServiceAccount{
  192. ServiceAccount: account,
  193. },
  194. })
  195. }
  196. req := &altspb.HandshakerReq{
  197. ReqOneof: &altspb.HandshakerReq_ClientStart{
  198. ClientStart: &altspb.StartClientHandshakeReq{
  199. HandshakeSecurityProtocol: hsProtocol,
  200. ApplicationProtocols: appProtocols,
  201. RecordProtocols: recordProtocols,
  202. TargetIdentities: targetIdentities,
  203. LocalIdentity: h.clientOpts.ClientIdentity,
  204. TargetName: h.clientOpts.TargetName,
  205. RpcVersions: h.clientOpts.RPCVersions,
  206. },
  207. },
  208. }
  209. conn, result, err := h.doHandshake(req)
  210. if err != nil {
  211. return nil, nil, err
  212. }
  213. authInfo := authinfo.New(result)
  214. return conn, authInfo, nil
  215. }
  216. // ServerHandshake starts and completes a server ALTS handshake for GCP. Once
  217. // done, ServerHandshake returns a secure connection.
  218. func (h *altsHandshaker) ServerHandshake(ctx context.Context) (net.Conn, credentials.AuthInfo, error) {
  219. if !acquire() {
  220. return nil, nil, errDropped
  221. }
  222. defer release()
  223. if h.side != core.ServerSide {
  224. return nil, nil, errors.New("only handshakers created using NewServerHandshaker can perform a server handshaker")
  225. }
  226. // TODO(matthewstevenson88): Change unit tests to use public APIs so
  227. // that h.stream can unconditionally be set based on h.clientConn.
  228. if h.stream == nil {
  229. stream, err := altsgrpc.NewHandshakerServiceClient(h.clientConn).DoHandshake(ctx)
  230. if err != nil {
  231. return nil, nil, fmt.Errorf("failed to establish stream to ALTS handshaker service: %v", err)
  232. }
  233. h.stream = stream
  234. }
  235. p := make([]byte, frameLimit)
  236. n, err := h.conn.Read(p)
  237. if err != nil {
  238. return nil, nil, err
  239. }
  240. // Prepare server parameters.
  241. // TODO: currently only ALTS parameters are provided. Might need to use
  242. // more options in the future.
  243. params := make(map[int32]*altspb.ServerHandshakeParameters)
  244. params[int32(altspb.HandshakeProtocol_ALTS)] = &altspb.ServerHandshakeParameters{
  245. RecordProtocols: recordProtocols,
  246. }
  247. req := &altspb.HandshakerReq{
  248. ReqOneof: &altspb.HandshakerReq_ServerStart{
  249. ServerStart: &altspb.StartServerHandshakeReq{
  250. ApplicationProtocols: appProtocols,
  251. HandshakeParameters: params,
  252. InBytes: p[:n],
  253. RpcVersions: h.serverOpts.RPCVersions,
  254. },
  255. },
  256. }
  257. conn, result, err := h.doHandshake(req)
  258. if err != nil {
  259. return nil, nil, err
  260. }
  261. authInfo := authinfo.New(result)
  262. return conn, authInfo, nil
  263. }
  264. func (h *altsHandshaker) doHandshake(req *altspb.HandshakerReq) (net.Conn, *altspb.HandshakerResult, error) {
  265. resp, err := h.accessHandshakerService(req)
  266. if err != nil {
  267. return nil, nil, err
  268. }
  269. // Check of the returned status is an error.
  270. if resp.GetStatus() != nil {
  271. if got, want := resp.GetStatus().Code, uint32(codes.OK); got != want {
  272. return nil, nil, fmt.Errorf("%v", resp.GetStatus().Details)
  273. }
  274. }
  275. var extra []byte
  276. if req.GetServerStart() != nil {
  277. if resp.GetBytesConsumed() > uint32(len(req.GetServerStart().GetInBytes())) {
  278. return nil, nil, errOutOfBound
  279. }
  280. extra = req.GetServerStart().GetInBytes()[resp.GetBytesConsumed():]
  281. }
  282. result, extra, err := h.processUntilDone(resp, extra)
  283. if err != nil {
  284. return nil, nil, err
  285. }
  286. // The handshaker returns a 128 bytes key. It should be truncated based
  287. // on the returned record protocol.
  288. keyLen, ok := keyLength[result.RecordProtocol]
  289. if !ok {
  290. return nil, nil, fmt.Errorf("unknown resulted record protocol %v", result.RecordProtocol)
  291. }
  292. sc, err := conn.NewConn(h.conn, h.side, result.GetRecordProtocol(), result.KeyData[:keyLen], extra)
  293. if err != nil {
  294. return nil, nil, err
  295. }
  296. return sc, result, nil
  297. }
  298. func (h *altsHandshaker) accessHandshakerService(req *altspb.HandshakerReq) (*altspb.HandshakerResp, error) {
  299. if err := h.stream.Send(req); err != nil {
  300. return nil, err
  301. }
  302. resp, err := h.stream.Recv()
  303. if err != nil {
  304. return nil, err
  305. }
  306. return resp, nil
  307. }
  308. // processUntilDone processes the handshake until the handshaker service returns
  309. // the results. Handshaker service takes care of frame parsing, so we read
  310. // whatever received from the network and send it to the handshaker service.
  311. func (h *altsHandshaker) processUntilDone(resp *altspb.HandshakerResp, extra []byte) (*altspb.HandshakerResult, []byte, error) {
  312. for {
  313. if len(resp.OutFrames) > 0 {
  314. if _, err := h.conn.Write(resp.OutFrames); err != nil {
  315. return nil, nil, err
  316. }
  317. }
  318. if resp.Result != nil {
  319. return resp.Result, extra, nil
  320. }
  321. buf := make([]byte, frameLimit)
  322. n, err := h.conn.Read(buf)
  323. if err != nil && err != io.EOF {
  324. return nil, nil, err
  325. }
  326. // If there is nothing to send to the handshaker service, and
  327. // nothing is received from the peer, then we are stuck.
  328. // This covers the case when the peer is not responding. Note
  329. // that handshaker service connection issues are caught in
  330. // accessHandshakerService before we even get here.
  331. if len(resp.OutFrames) == 0 && n == 0 {
  332. return nil, nil, core.PeerNotRespondingError
  333. }
  334. // Append extra bytes from the previous interaction with the
  335. // handshaker service with the current buffer read from conn.
  336. p := append(extra, buf[:n]...)
  337. // From here on, p and extra point to the same slice.
  338. resp, err = h.accessHandshakerService(&altspb.HandshakerReq{
  339. ReqOneof: &altspb.HandshakerReq_Next{
  340. Next: &altspb.NextHandshakeMessageReq{
  341. InBytes: p,
  342. },
  343. },
  344. })
  345. if err != nil {
  346. return nil, nil, err
  347. }
  348. // Set extra based on handshaker service response.
  349. if resp.GetBytesConsumed() > uint32(len(p)) {
  350. return nil, nil, errOutOfBound
  351. }
  352. extra = p[resp.GetBytesConsumed():]
  353. }
  354. }
  355. // Close terminates the Handshaker. It should be called when the caller obtains
  356. // the secure connection.
  357. func (h *altsHandshaker) Close() {
  358. if h.stream != nil {
  359. h.stream.CloseSend()
  360. }
  361. }