Source File
handshaker.go
Belonging Package
google.golang.org/grpc/credentials/alts/internal/handshaker
frameLimit = 64 * 1024 // 64 KB
maxPendingHandshakes = 100
)
var (
hsProtocol = altspb.HandshakeProtocol_ALTS
appProtocols = []string{"grpc"}
recordProtocols = []string{rekeyRecordProtocolName}
keyLength = map[string]int{
rekeyRecordProtocolName: 44,
}
rekeyRecordProtocolName: func( core.Side, []byte) (conn.ALTSRecordCrypto, error) {
return conn.NewAES128GCMRekey(, )
},
errOutOfBound = errors.New("handshaker service consumed bytes value is out-of-bound")
)
func () {
for , := range altsRecordFuncs {
if := conn.RegisterProtocol(, ); != nil {
panic()
}
}
}
func () bool {
:= int64(1)
:= maxPendingHandshakes-concurrentHandshakes >=
if {
concurrentHandshakes +=
}
mu.Unlock()
return
}
func () {
:= int64(1)
concurrentHandshakes -=
if concurrentHandshakes < 0 {
mu.Unlock()
panic("bad release")
}
mu.Unlock()
}
func () *ClientHandshakerOptions {
return &ClientHandshakerOptions{}
}
func () *ServerHandshakerOptions {
return &ServerHandshakerOptions{}
}
func ( context.Context, *grpc.ClientConn, net.Conn, *ClientHandshakerOptions) (core.Handshaker, error) {
, := altsgrpc.NewHandshakerServiceClient().DoHandshake(, grpc.WaitForReady(true))
if != nil {
return nil,
}
return &altsHandshaker{
stream: ,
conn: ,
clientOpts: ,
side: core.ClientSide,
}, nil
}
func ( context.Context, *grpc.ClientConn, net.Conn, *ServerHandshakerOptions) (core.Handshaker, error) {
, := altsgrpc.NewHandshakerServiceClient().DoHandshake(, grpc.WaitForReady(true))
if != nil {
return nil,
}
return &altsHandshaker{
stream: ,
conn: ,
serverOpts: ,
side: core.ServerSide,
}, nil
}
func ( *altsHandshaker) ( context.Context) (net.Conn, credentials.AuthInfo, error) {
if !acquire() {
return nil, nil, errDropped
}
defer release()
if .side != core.ClientSide {
return nil, nil, errors.New("only handshakers created using NewClientHandshaker can perform a client handshaker")
}
:= make([]*altspb.Identity, 0, len(.clientOpts.TargetServiceAccounts))
for , := range .clientOpts.TargetServiceAccounts {
= append(, &altspb.Identity{
IdentityOneof: &altspb.Identity_ServiceAccount{
ServiceAccount: ,
},
})
}
:= &altspb.HandshakerReq{
ReqOneof: &altspb.HandshakerReq_ClientStart{
ClientStart: &altspb.StartClientHandshakeReq{
HandshakeSecurityProtocol: hsProtocol,
ApplicationProtocols: appProtocols,
RecordProtocols: recordProtocols,
TargetIdentities: ,
LocalIdentity: .clientOpts.ClientIdentity,
TargetName: .clientOpts.TargetName,
RpcVersions: .clientOpts.RPCVersions,
},
},
}
, , := .doHandshake()
if != nil {
return nil, nil,
}
:= authinfo.New()
return , , nil
}
func ( *altsHandshaker) ( context.Context) (net.Conn, credentials.AuthInfo, error) {
if !acquire() {
return nil, nil, errDropped
}
defer release()
if .side != core.ServerSide {
return nil, nil, errors.New("only handshakers created using NewServerHandshaker can perform a server handshaker")
}
:= make([]byte, frameLimit)
, := .conn.Read()
if != nil {
return nil, nil,
}
:= make(map[int32]*altspb.ServerHandshakeParameters)
[int32(altspb.HandshakeProtocol_ALTS)] = &altspb.ServerHandshakeParameters{
RecordProtocols: recordProtocols,
}
:= &altspb.HandshakerReq{
ReqOneof: &altspb.HandshakerReq_ServerStart{
ServerStart: &altspb.StartServerHandshakeReq{
ApplicationProtocols: appProtocols,
HandshakeParameters: ,
InBytes: [:],
RpcVersions: .serverOpts.RPCVersions,
},
},
}
, , := .doHandshake()
if != nil {
return nil, nil,
}
:= authinfo.New()
return , , nil
}
func ( *altsHandshaker) ( *altspb.HandshakerReq) (net.Conn, *altspb.HandshakerResult, error) {
, := .accessHandshakerService()
if != nil {
return nil, nil,
if .GetStatus() != nil {
if , := .GetStatus().Code, uint32(codes.OK); != {
return nil, nil, fmt.Errorf("%v", .GetStatus().Details)
}
}
var []byte
if .GetServerStart() != nil {
if .GetBytesConsumed() > uint32(len(.GetServerStart().GetInBytes())) {
return nil, nil, errOutOfBound
}
= .GetServerStart().GetInBytes()[.GetBytesConsumed():]
}
, , := .processUntilDone(, )
if != nil {
return nil, nil,
, := keyLength[.RecordProtocol]
if ! {
return nil, nil, fmt.Errorf("unknown resulted record protocol %v", .RecordProtocol)
}
, := conn.NewConn(.conn, .side, .GetRecordProtocol(), .KeyData[:], )
if != nil {
return nil, nil,
}
return , , nil
}
func ( *altsHandshaker) ( *altspb.HandshakerReq) (*altspb.HandshakerResp, error) {
if := .stream.Send(); != nil {
return nil,
}
, := .stream.Recv()
if != nil {
return nil,
}
return , nil
}
func ( *altsHandshaker) ( *altspb.HandshakerResp, []byte) (*altspb.HandshakerResult, []byte, error) {
for {
if len(.OutFrames) > 0 {
if , := .conn.Write(.OutFrames); != nil {
return nil, nil,
}
}
if .Result != nil {
return .Result, , nil
}
:= make([]byte, frameLimit)
, := .conn.Read()
if != nil && != io.EOF {
return nil, nil,
, = .accessHandshakerService(&altspb.HandshakerReq{
ReqOneof: &altspb.HandshakerReq_Next{
Next: &altspb.NextHandshakeMessageReq{
InBytes: ,
},
},
})
if != nil {
return nil, nil,
if .GetBytesConsumed() > uint32(len()) {
return nil, nil, errOutOfBound
}
= [.GetBytesConsumed():]
}
}
func ( *altsHandshaker) () {
.stream.CloseSend()
![]() |
The pages are generated with Golds v0.3.2-preview. (GOOS=darwin GOARCH=amd64) Golds is a Go 101 project developed by Tapir Liu. PR and bug reports are welcome and can be submitted to the issue list. Please follow @Go100and1 (reachable from the left QR code) to get the latest news of Golds. |