| 1 |
/* |
| 2 |
* |
| 3 |
* Copyright 2022 Google LLC |
| 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 |
* https://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 |
|
| 19 |
package tlsconfigstore |
| 20 |
|
| 21 |
import ( |
| 22 |
"bytes" |
| 23 |
"context" |
| 24 |
"crypto/tls" |
| 25 |
"errors" |
| 26 |
"fmt" |
| 27 |
"log" |
| 28 |
"net" |
| 29 |
"sync" |
| 30 |
"testing" |
| 31 |
"time" |
| 32 |
|
| 33 |
"github.com/google/go-cmp/cmp" |
| 34 |
"github.com/google/go-cmp/cmp/cmpopts" |
| 35 |
"github.com/google/s2a-go/internal/tokenmanager" |
| 36 |
"github.com/google/s2a-go/internal/v2/fakes2av2" |
| 37 |
"google.golang.org/grpc" |
| 38 |
"google.golang.org/grpc/credentials/insecure" |
| 39 |
"google.golang.org/protobuf/testing/protocmp" |
| 40 |
|
| 41 |
_ "embed" |
| 42 |
|
| 43 |
commonpbv1 "github.com/google/s2a-go/internal/proto/common_go_proto" |
| 44 |
commonpb "github.com/google/s2a-go/internal/proto/v2/common_go_proto" |
| 45 |
s2av2pb "github.com/google/s2a-go/internal/proto/v2/s2a_go_proto" |
| 46 |
) |
| 47 |
|
| 48 |
const ( |
| 49 |
defaultTimeout = 10.0 * time.Second |
| 50 |
) |
| 51 |
|
| 52 |
var ( |
| 53 |
//go:embed testdata/client_cert.pem |
| 54 |
clientCertpem []byte |
| 55 |
//go:embed testdata/server_cert.pem |
| 56 |
serverCertpem []byte |
| 57 |
//go:embed testdata/client_key.pem |
| 58 |
clientKeypem []byte |
| 59 |
//go:embed testdata/server_key.pem |
| 60 |
serverKeypem []byte |
| 61 |
) |
| 62 |
|
| 63 |
// fakeAccessTokenManager implements the AccessTokenManager interface. |
| 64 |
type fakeAccessTokenManager struct { |
| 65 |
acceptedIdentity *commonpb.Identity |
| 66 |
accessToken string |
| 67 |
allowEmptyIdentity bool |
| 68 |
} |
| 69 |
|
| 70 |
// DefaultToken returns the token managed by the fakeAccessTokenManager. |
| 71 |
func (m *fakeAccessTokenManager) DefaultToken() (string, error) { |
| 72 |
if !m.allowEmptyIdentity { |
| 73 |
return "", fmt.Errorf("not allowed to get token for empty identity") |
| 74 |
} |
| 75 |
return m.accessToken, nil |
| 76 |
} |
| 77 |
|
| 78 |
// Token returns the token managed by the fakeAccessTokenManager. |
| 79 |
func (m *fakeAccessTokenManager) Token(identity interface{}) (string, error) { |
| 80 |
switch v := identity.(type) { |
| 81 |
case *commonpbv1.Identity: |
| 82 |
// valid type. |
| 83 |
case *commonpb.Identity: |
| 84 |
// valid type. |
| 85 |
default: |
| 86 |
return "", fmt.Errorf("Incorrect identity type: %v", v) |
| 87 |
} |
| 88 |
if identity == nil || cmp.Equal(identity, &commonpb.Identity{}, protocmp.Transform()) { |
| 89 |
if !m.allowEmptyIdentity { |
| 90 |
return "", fmt.Errorf("not allowed to get token for empty identity") |
| 91 |
} |
| 92 |
return m.accessToken, nil |
| 93 |
} |
| 94 |
if cmp.Equal(identity, m.acceptedIdentity, protocmp.Transform()) { |
| 95 |
return m.accessToken, nil |
| 96 |
} |
| 97 |
return "", fmt.Errorf("unable to get token") |
| 98 |
} |
| 99 |
|
| 100 |
func startFakeS2Av2Server(wg *sync.WaitGroup, expToken string, shouldNotReturnClientCreds bool) (stop func(), address string, err error) { |
| 101 |
listener, err := net.Listen("tcp", ":0") |
| 102 |
if err != nil { |
| 103 |
log.Fatalf("Failed to listen on address %s: %v", address, err) |
| 104 |
} |
| 105 |
address = listener.Addr().String() |
| 106 |
s := grpc.NewServer() |
| 107 |
log.Printf("Server: started gRPC fake S2Av2 Server on address: %s", address) |
| 108 |
s2av2pb.RegisterS2AServiceServer(s, &fakes2av2.Server{ |
| 109 |
ExpectedToken: expToken, |
| 110 |
ShouldNotReturnClientCredentials: shouldNotReturnClientCreds, |
| 111 |
}) |
| 112 |
go func() { |
| 113 |
wg.Done() |
| 114 |
if err := s.Serve(listener); err != nil { |
| 115 |
log.Printf("Failed to serve: %v", err) |
| 116 |
} |
| 117 |
}() |
| 118 |
return func() { s.Stop() }, address, nil |
| 119 |
} |
| 120 |
|
| 121 |
// TestTLSConfigStoreClient runs unit tests for GetTLSConfigurationForClient. |
| 122 |
func TestTLSConfigStoreClient(t *testing.T) { |
| 123 |
// Setup for static client test. |
| 124 |
cert, err := tls.X509KeyPair(clientCertpem, clientKeypem) |
| 125 |
if err != nil { |
| 126 |
t.Fatalf("tls.X509KeyPair failed: %v", err) |
| 127 |
} |
| 128 |
|
| 129 |
// Start up fake S2Av2 server. |
| 130 |
var wg sync.WaitGroup |
| 131 |
wg.Add(1) |
| 132 |
stop, address, err := startFakeS2Av2Server(&wg, "TestTlsConfigStoreClient_token", false) |
| 133 |
wg.Wait() |
| 134 |
if err != nil { |
| 135 |
t.Fatalf("Error starting fake S2Av2 Server: %v", err) |
| 136 |
} |
| 137 |
|
| 138 |
accessTokenManager := &fakeAccessTokenManager{ |
| 139 |
allowEmptyIdentity: true, |
| 140 |
accessToken: "TestTlsConfigStoreClient_token", |
| 141 |
} |
| 142 |
for _, tc := range []struct { |
| 143 |
description string |
| 144 |
tokenManager tokenmanager.AccessTokenManager |
| 145 |
Certificates []tls.Certificate |
| 146 |
ServerName string |
| 147 |
InsecureSkipVerify bool |
| 148 |
SessionTicketsDisabled bool |
| 149 |
ClientSessionCache tls.ClientSessionCache |
| 150 |
MinVersion uint16 |
| 151 |
MaxVersion uint16 |
| 152 |
NextProtos []string |
| 153 |
}{ |
| 154 |
{ |
| 155 |
description: "static - nil tokenManager", |
| 156 |
tokenManager: nil, |
| 157 |
Certificates: []tls.Certificate{cert}, |
| 158 |
ServerName: "host", |
| 159 |
InsecureSkipVerify: true, // NOLINT |
| 160 |
SessionTicketsDisabled: true, |
| 161 |
ClientSessionCache: nil, |
| 162 |
MinVersion: tls.VersionTLS13, |
| 163 |
MaxVersion: tls.VersionTLS13, |
| 164 |
NextProtos: []string{"h2"}, |
| 165 |
}, |
| 166 |
{ |
| 167 |
description: "static - non-nil tokenManager", |
| 168 |
tokenManager: accessTokenManager, |
| 169 |
Certificates: []tls.Certificate{cert}, |
| 170 |
ServerName: "host", |
| 171 |
InsecureSkipVerify: true, // NOLINT |
| 172 |
SessionTicketsDisabled: true, |
| 173 |
ClientSessionCache: nil, |
| 174 |
MinVersion: tls.VersionTLS13, |
| 175 |
MaxVersion: tls.VersionTLS13, |
| 176 |
NextProtos: []string{"h2"}, |
| 177 |
}, |
| 178 |
} { |
| 179 |
t.Run(tc.description, func(t *testing.T) { |
| 180 |
// Create stream to S2Av2. |
| 181 |
opts := []grpc.DialOption{ |
| 182 |
grpc.WithTransportCredentials(insecure.NewCredentials()), |
| 183 |
grpc.WithReturnConnectionError(), |
| 184 |
grpc.WithBlock(), |
| 185 |
} |
| 186 |
conn, err := grpc.Dial(address, opts...) |
| 187 |
if err != nil { |
| 188 |
t.Fatalf("Client: failed to connect: %v", err) |
| 189 |
} |
| 190 |
defer conn.Close() |
| 191 |
c := s2av2pb.NewS2AServiceClient(conn) |
| 192 |
log.Printf("Client: connected to: %s", address) |
| 193 |
ctx, cancel := context.WithTimeout(context.Background(), defaultTimeout) |
| 194 |
defer cancel() |
| 195 |
|
| 196 |
// Setup bidrectional streaming session. |
| 197 |
callOpts := []grpc.CallOption{} |
| 198 |
cstream, err := c.SetUpSession(ctx, callOpts...) |
| 199 |
if err != nil { |
| 200 |
t.Fatalf("Client: failed to setup bidirectional streaming RPC session: %v", err) |
| 201 |
} |
| 202 |
log.Printf("Client: set up bidirectional streaming RPC session.") |
| 203 |
config, err := GetTLSConfigurationForClient(tc.ServerName, cstream, tc.tokenManager, nil, s2av2pb.ValidatePeerCertificateChainReq_CONNECT_TO_GOOGLE, nil) |
| 204 |
if err != nil { |
| 205 |
t.Errorf("GetTLSConfigurationForClient failed: %v", err) |
| 206 |
} |
| 207 |
if got, want := config.Certificates[0].Certificate[0], tc.Certificates[0].Certificate[0]; !bytes.Equal(got, want) { |
| 208 |
t.Errorf("config.Certificates[0].Certificate[0] = %v, want %v", got, want) |
| 209 |
} |
| 210 |
if got, want := config.InsecureSkipVerify, tc.InsecureSkipVerify; got != want { |
| 211 |
t.Errorf("config.InsecureSkipVerify = %v, want %v", got, want) |
| 212 |
} |
| 213 |
if got, want := config.SessionTicketsDisabled, tc.SessionTicketsDisabled; got != want { |
| 214 |
t.Errorf("config.SessionTicketsDisabled = %v, want %v", got, want) |
| 215 |
} |
| 216 |
if got, want := config.ClientSessionCache, tc.ClientSessionCache; got != want { |
| 217 |
t.Errorf("config.ClientSessionCache = %v, want %v", got, want) |
| 218 |
} |
| 219 |
if got, want := config.MinVersion, tc.MinVersion; got != want { |
| 220 |
t.Errorf("config.MinVersion = %v, want %v", got, want) |
| 221 |
} |
| 222 |
if got, want := config.MaxVersion, tc.MaxVersion; got != want { |
| 223 |
t.Errorf("config.MaxVersion = %v, want %v", got, want) |
| 224 |
} |
| 225 |
if !compareNextProtos(config.NextProtos, tc.NextProtos) { |
| 226 |
t.Errorf("config.NextProtos = %v, want %v", config.NextProtos, tc.NextProtos) |
| 227 |
} |
| 228 |
}) |
| 229 |
} |
| 230 |
stop() |
| 231 |
} |
| 232 |
|
| 233 |
// TestTLSConfigStoreClientWithoutCredentials runs unit tests for |
| 234 |
// GetTLSConfigurationForClient, but does not expect the TLS configuration |
| 235 |
// to contain any client credentials. |
| 236 |
func TestTLSConfigStoreClientWithoutCredentials(t *testing.T) { |
| 237 |
// Start up fake S2Av2 server. |
| 238 |
var wg sync.WaitGroup |
| 239 |
wg.Add(1) |
| 240 |
stop, address, err := startFakeS2Av2Server(&wg, "", true) |
| 241 |
wg.Wait() |
| 242 |
if err != nil { |
| 243 |
t.Fatalf("Error starting fake S2Av2 Server: %v", err) |
| 244 |
} |
| 245 |
|
| 246 |
// Create stream to S2Av2. |
| 247 |
opts := []grpc.DialOption{ |
| 248 |
grpc.WithTransportCredentials(insecure.NewCredentials()), |
| 249 |
grpc.WithReturnConnectionError(), |
| 250 |
grpc.WithBlock(), |
| 251 |
} |
| 252 |
conn, err := grpc.Dial(address, opts...) |
| 253 |
if err != nil { |
| 254 |
t.Fatalf("Client: failed to connect: %v", err) |
| 255 |
} |
| 256 |
defer conn.Close() |
| 257 |
c := s2av2pb.NewS2AServiceClient(conn) |
| 258 |
log.Printf("Client: connected to: %s", address) |
| 259 |
ctx, cancel := context.WithTimeout(context.Background(), defaultTimeout) |
| 260 |
defer cancel() |
| 261 |
|
| 262 |
// Setup bidrectional streaming session. |
| 263 |
callOpts := []grpc.CallOption{} |
| 264 |
cstream, err := c.SetUpSession(ctx, callOpts...) |
| 265 |
if err != nil { |
| 266 |
t.Fatalf("Client: failed to setup bidirectional streaming RPC session: %v", err) |
| 267 |
} |
| 268 |
log.Printf("Client: set up bidirectional streaming RPC session.") |
| 269 |
config, err := GetTLSConfigurationForClient("hostname", cstream, nil, nil, s2av2pb.ValidatePeerCertificateChainReq_CONNECT_TO_GOOGLE, nil) |
| 270 |
if err != nil { |
| 271 |
t.Errorf("GetTLSConfigurationForClient failed: %v", err) |
| 272 |
} |
| 273 |
if len(config.Certificates) > 0 { |
| 274 |
t.Errorf("config had unexpected number of certificates") |
| 275 |
} |
| 276 |
if got, want := config.InsecureSkipVerify, true; got != want { |
| 277 |
t.Errorf("config.InsecureSkipVerify = %v, want %v", got, want) |
| 278 |
} |
| 279 |
if got, want := config.SessionTicketsDisabled, true; got != want { |
| 280 |
t.Errorf("config.SessionTicketsDisabled = %v, want %v", got, want) |
| 281 |
} |
| 282 |
if config.ClientSessionCache != nil { |
| 283 |
t.Errorf("config.ClientSessionCache expected to be nil") |
| 284 |
} |
| 285 |
if got, want := int(config.MinVersion), tls.VersionTLS13; got != want { |
| 286 |
t.Errorf("config.MinVersion = %v, want %v", got, want) |
| 287 |
} |
| 288 |
if got, want := int(config.MaxVersion), tls.VersionTLS13; got != want { |
| 289 |
t.Errorf("config.MaxVersion = %v, want %v", got, want) |
| 290 |
} |
| 291 |
if !compareNextProtos(config.NextProtos, []string{"h2"}) { |
| 292 |
t.Errorf("config.NextProtos = %v, want %v", config.NextProtos, []string{"h2"}) |
| 293 |
} |
| 294 |
stop() |
| 295 |
} |
| 296 |
|
| 297 |
// TestTLSConfigStoreServer runs unit tests for GetTLSConfigurationForServer. |
| 298 |
func TestTLSConfigStoreServer(t *testing.T) { |
| 299 |
// Setup for static server test. |
| 300 |
cert, err := tls.X509KeyPair(serverCertpem, serverKeypem) |
| 301 |
if err != nil { |
| 302 |
t.Fatalf("tls.X509KeyPair failed: %v", err) |
| 303 |
} |
| 304 |
// Start up fake S2Av2 server. |
| 305 |
var wg sync.WaitGroup |
| 306 |
wg.Add(1) |
| 307 |
stop, address, err := startFakeS2Av2Server(&wg, "TestTlsConfigStoreServer_token", false) |
| 308 |
wg.Wait() |
| 309 |
if err != nil { |
| 310 |
t.Fatalf("Error starting fake S2Av2 Server: %v", err) |
| 311 |
} |
| 312 |
|
| 313 |
accessTokenManager := &fakeAccessTokenManager{ |
| 314 |
allowEmptyIdentity: true, |
| 315 |
accessToken: "TestTlsConfigStoreServer_token", |
| 316 |
} |
| 317 |
var identities []*commonpb.Identity |
| 318 |
identities = append(identities, nil) |
| 319 |
for _, tc := range []struct { |
| 320 |
description string |
| 321 |
tokenManager tokenmanager.AccessTokenManager |
| 322 |
Certificates []tls.Certificate |
| 323 |
SessionTicketsDisabled bool |
| 324 |
ClientAuth tls.ClientAuthType |
| 325 |
MinVersion uint16 |
| 326 |
MaxVersion uint16 |
| 327 |
NextProtos []string |
| 328 |
}{ |
| 329 |
{ |
| 330 |
description: "static - nil tokenManager", |
| 331 |
tokenManager: nil, |
| 332 |
Certificates: []tls.Certificate{cert}, |
| 333 |
SessionTicketsDisabled: true, |
| 334 |
ClientAuth: tls.RequireAnyClientCert, |
| 335 |
MinVersion: tls.VersionTLS13, |
| 336 |
MaxVersion: tls.VersionTLS13, |
| 337 |
NextProtos: []string{"h2"}, |
| 338 |
}, |
| 339 |
{ |
| 340 |
description: "static - non-nil tokenManager", |
| 341 |
tokenManager: accessTokenManager, |
| 342 |
Certificates: []tls.Certificate{cert}, |
| 343 |
SessionTicketsDisabled: true, |
| 344 |
ClientAuth: tls.RequireAnyClientCert, |
| 345 |
MinVersion: tls.VersionTLS13, |
| 346 |
MaxVersion: tls.VersionTLS13, |
| 347 |
NextProtos: []string{"h2"}, |
| 348 |
}, |
| 349 |
} { |
| 350 |
t.Run(tc.description, func(t *testing.T) { |
| 351 |
// Create stream to S2Av2. |
| 352 |
opts := []grpc.DialOption{ |
| 353 |
grpc.WithTransportCredentials(insecure.NewCredentials()), |
| 354 |
grpc.WithReturnConnectionError(), |
| 355 |
grpc.WithBlock(), |
| 356 |
} |
| 357 |
conn, err := grpc.Dial(address, opts...) |
| 358 |
if err != nil { |
| 359 |
t.Fatalf("Client: failed to connect: %v", err) |
| 360 |
} |
| 361 |
defer conn.Close() |
| 362 |
c := s2av2pb.NewS2AServiceClient(conn) |
| 363 |
log.Printf("Client: connected to: %s", address) |
| 364 |
ctx, cancel := context.WithTimeout(context.Background(), defaultTimeout) |
| 365 |
defer cancel() |
| 366 |
|
| 367 |
// Setup bidrectional streaming session. |
| 368 |
callOpts := []grpc.CallOption{} |
| 369 |
cstream, err := c.SetUpSession(ctx, callOpts...) |
| 370 |
if err != nil { |
| 371 |
t.Fatalf("Client: failed to setup bidirectional streaming RPC session: %v", err) |
| 372 |
} |
| 373 |
log.Printf("Client: set up bidirectional streaming RPC session.") |
| 374 |
config, err := GetTLSConfigurationForServer(cstream, tc.tokenManager, identities, s2av2pb.ValidatePeerCertificateChainReq_CONNECT_TO_GOOGLE) |
| 375 |
if err != nil { |
| 376 |
t.Errorf("GetTLSConfigurationForClient failed: %v", err) |
| 377 |
} |
| 378 |
clientConfigFunc := config.GetConfigForClient |
| 379 |
config, err = clientConfigFunc(&tls.ClientHelloInfo{ |
| 380 |
ServerName: "host_1", |
| 381 |
}) |
| 382 |
if err != nil { |
| 383 |
t.Errorf("ClientConfig failed: %v", err) |
| 384 |
} |
| 385 |
if got, want := config.Certificates[0].Certificate[0], tc.Certificates[0].Certificate[0]; !bytes.Equal(got, want) { |
| 386 |
t.Errorf("config.Certificates[0].Certificate[0] = %v, want %v", got, want) |
| 387 |
} |
| 388 |
if got, want := config.SessionTicketsDisabled, tc.SessionTicketsDisabled; got != want { |
| 389 |
t.Errorf("config.SessionTicketsDisabled = %v, want %v", got, want) |
| 390 |
} |
| 391 |
if got, want := config.ClientAuth, tc.ClientAuth; got != want { |
| 392 |
t.Errorf("config.ClientAuth = %v, want %v", got, want) |
| 393 |
} |
| 394 |
if got, want := config.MinVersion, tc.MinVersion; got != want { |
| 395 |
t.Errorf("config.MinVersion = %v, want %v", got, want) |
| 396 |
} |
| 397 |
if got, want := config.MaxVersion, tc.MaxVersion; got != want { |
| 398 |
t.Errorf("config.MaxVersion = %v, want %v", got, want) |
| 399 |
} |
| 400 |
if !compareNextProtos(config.NextProtos, tc.NextProtos) { |
| 401 |
t.Errorf("config.NextProtos = %v, want %v", config.NextProtos, tc.NextProtos) |
| 402 |
} |
| 403 |
}) |
| 404 |
} |
| 405 |
stop() |
| 406 |
} |
| 407 |
|
| 408 |
func TestGetTLSMinMaxVersionsClient(t *testing.T) { |
| 409 |
m := makeMapOfTLSVersions() |
| 410 |
for min := commonpb.TLSVersion_TLS_VERSION_1_0; min <= commonpb.TLSVersion_TLS_VERSION_1_3; min++ { |
| 411 |
for max := commonpb.TLSVersion_TLS_VERSION_1_0; max <= commonpb.TLSVersion_TLS_VERSION_1_3; max++ { |
| 412 |
tlsConfig := &s2av2pb.GetTlsConfigurationResp_ServerTlsConfiguration{ |
| 413 |
MinTlsVersion: min, |
| 414 |
MaxTlsVersion: max, |
| 415 |
} |
| 416 |
tlsMin, tlsMax, err := getTLSMinMaxVersionsServer(tlsConfig) |
| 417 |
if err != nil { |
| 418 |
if min <= max { |
| 419 |
t.Errorf("err = %v, expected err = nil", err) |
| 420 |
} else { |
| 421 |
if m[min] != tlsMin { |
| 422 |
t.Errorf("tlsMin = %v, expected %v", tlsMin, m[min]) |
| 423 |
} |
| 424 |
if m[max] != tlsMax { |
| 425 |
t.Errorf("tlsMax = %v, expected %v", tlsMax, m[max]) |
| 426 |
} |
| 427 |
} |
| 428 |
} else { |
| 429 |
if min > max { |
| 430 |
t.Errorf("err = nil, expected err = S2Av2 provided minVersion > maxVersion.") |
| 431 |
} else { |
| 432 |
if m[min] != tlsMin { |
| 433 |
t.Errorf("tlsMin = %v, expected %v", tlsMin, m[min]) |
| 434 |
} |
| 435 |
if m[max] != tlsMax { |
| 436 |
t.Errorf("tlsMax = %v, expected %v", tlsMax, m[max]) |
| 437 |
} |
| 438 |
} |
| 439 |
} |
| 440 |
} |
| 441 |
} |
| 442 |
// Test invalid input. |
| 443 |
tlsConfig := &s2av2pb.GetTlsConfigurationResp_ClientTlsConfiguration{ |
| 444 |
MinTlsVersion: commonpb.TLSVersion_TLS_VERSION_1_0 - 1, |
| 445 |
MaxTlsVersion: commonpb.TLSVersion_TLS_VERSION_1_3, |
| 446 |
} |
| 447 |
expErr := fmt.Errorf("S2Av2 provided invalid MinTlsVersion: %v", tlsConfig.MinTlsVersion) |
| 448 |
_, _, err := getTLSMinMaxVersionsClient(tlsConfig) |
| 449 |
if (err == nil) || (err.Error() != expErr.Error()) { |
| 450 |
t.Errorf("err = %v, expErr = %v", err, expErr) |
| 451 |
} |
| 452 |
|
| 453 |
tlsConfig = &s2av2pb.GetTlsConfigurationResp_ClientTlsConfiguration{ |
| 454 |
MinTlsVersion: commonpb.TLSVersion_TLS_VERSION_1_0, |
| 455 |
MaxTlsVersion: commonpb.TLSVersion_TLS_VERSION_1_3 + 1, |
| 456 |
} |
| 457 |
expErr = fmt.Errorf("S2Av2 provided invalid MaxTlsVersion: %v", tlsConfig.MaxTlsVersion) |
| 458 |
_, _, err = getTLSMinMaxVersionsClient(tlsConfig) |
| 459 |
if (err == nil) || (err.Error() != expErr.Error()) { |
| 460 |
t.Errorf("err = %v, expErr = %v", err, expErr) |
| 461 |
} |
| 462 |
} |
| 463 |
|
| 464 |
func TestGetTLSMinMaxVersionsServer(t *testing.T) { |
| 465 |
m := makeMapOfTLSVersions() |
| 466 |
for min := commonpb.TLSVersion_TLS_VERSION_1_0; min <= commonpb.TLSVersion_TLS_VERSION_1_3; min++ { |
| 467 |
for max := commonpb.TLSVersion_TLS_VERSION_1_0; max <= commonpb.TLSVersion_TLS_VERSION_1_3; max++ { |
| 468 |
tlsConfig := &s2av2pb.GetTlsConfigurationResp_ServerTlsConfiguration{ |
| 469 |
MinTlsVersion: min, |
| 470 |
MaxTlsVersion: max, |
| 471 |
} |
| 472 |
tlsMin, tlsMax, err := getTLSMinMaxVersionsServer(tlsConfig) |
| 473 |
if err != nil { |
| 474 |
if min <= max { |
| 475 |
t.Errorf("err = %v, expected err = nil", err) |
| 476 |
} else { |
| 477 |
if m[min] != tlsMin { |
| 478 |
t.Errorf("tlsMin = %v, expected %v", tlsMin, m[min]) |
| 479 |
} |
| 480 |
if m[max] != tlsMax { |
| 481 |
t.Errorf("tlsMax = %v, expected %v", tlsMax, m[max]) |
| 482 |
} |
| 483 |
} |
| 484 |
} else { |
| 485 |
if min > max { |
| 486 |
t.Errorf("err = nil, expected err = S2Av2 provided minVersion > maxVersion.") |
| 487 |
} else { |
| 488 |
if m[min] != tlsMin { |
| 489 |
t.Errorf("tlsMin = %v, expected %v", tlsMin, m[min]) |
| 490 |
} |
| 491 |
if m[max] != tlsMax { |
| 492 |
t.Errorf("tlsMax = %v, expected %v", tlsMax, m[max]) |
| 493 |
} |
| 494 |
} |
| 495 |
} |
| 496 |
} |
| 497 |
} |
| 498 |
|
| 499 |
// Test invalid input. |
| 500 |
tlsConfig := &s2av2pb.GetTlsConfigurationResp_ServerTlsConfiguration{ |
| 501 |
MinTlsVersion: commonpb.TLSVersion_TLS_VERSION_1_0 - 1, |
| 502 |
MaxTlsVersion: commonpb.TLSVersion_TLS_VERSION_1_3, |
| 503 |
} |
| 504 |
expErr := fmt.Errorf("S2Av2 provided invalid MinTlsVersion: %v", tlsConfig.MinTlsVersion) |
| 505 |
_, _, err := getTLSMinMaxVersionsServer(tlsConfig) |
| 506 |
if (err == nil) || (err.Error() != expErr.Error()) { |
| 507 |
t.Errorf("err = %v, expErr = %v", err, expErr) |
| 508 |
} |
| 509 |
|
| 510 |
tlsConfig = &s2av2pb.GetTlsConfigurationResp_ServerTlsConfiguration{ |
| 511 |
MinTlsVersion: commonpb.TLSVersion_TLS_VERSION_1_0, |
| 512 |
MaxTlsVersion: commonpb.TLSVersion_TLS_VERSION_1_3 + 1, |
| 513 |
} |
| 514 |
expErr = fmt.Errorf("S2Av2 provided invalid MaxTlsVersion: %v", tlsConfig.MaxTlsVersion) |
| 515 |
_, _, err = getTLSMinMaxVersionsServer(tlsConfig) |
| 516 |
if (err == nil) || (err.Error() != expErr.Error()) { |
| 517 |
t.Errorf("err = %v, expErr = %v", err, expErr) |
| 518 |
} |
| 519 |
} |
| 520 |
|
| 521 |
func TestGetAuthMechanisms(t *testing.T) { |
| 522 |
// Setup data for test. |
| 523 |
sortProtos := cmpopts.SortSlices(func(m1, m2 *s2av2pb.AuthenticationMechanism) bool { return m1.String() < m2.String() }) |
| 524 |
|
| 525 |
// TODO(rmehta19): Add additional tests. |
| 526 |
for _, tc := range []struct { |
| 527 |
description string |
| 528 |
tokenManager tokenmanager.AccessTokenManager |
| 529 |
localIdentities []*commonpb.Identity |
| 530 |
expectedAuthMechanisms []*s2av2pb.AuthenticationMechanism |
| 531 |
}{ |
| 532 |
{ |
| 533 |
description: "fake token manager is nil", |
| 534 |
tokenManager: nil, |
| 535 |
expectedAuthMechanisms: nil, |
| 536 |
}, |
| 537 |
{ |
| 538 |
description: "fake token manager allows empty identity", |
| 539 |
tokenManager: &fakeAccessTokenManager{ |
| 540 |
accessToken: "TestGetAuthMechanisms_s2a_access_token", |
| 541 |
allowEmptyIdentity: true, |
| 542 |
}, |
| 543 |
expectedAuthMechanisms: []*s2av2pb.AuthenticationMechanism{ |
| 544 |
{ |
| 545 |
MechanismOneof: &s2av2pb.AuthenticationMechanism_Token{ |
| 546 |
Token: "TestGetAuthMechanisms_s2a_access_token", |
| 547 |
}, |
| 548 |
}, |
| 549 |
}, |
| 550 |
}, |
| 551 |
{ |
| 552 |
description: "fake token manager does not allow empty identity", |
| 553 |
tokenManager: &fakeAccessTokenManager{ |
| 554 |
allowEmptyIdentity: false, |
| 555 |
}, |
| 556 |
expectedAuthMechanisms: nil, |
| 557 |
}, |
| 558 |
} { |
| 559 |
t.Run(tc.description, func(t *testing.T) { |
| 560 |
authMechanisms := getAuthMechanisms(tc.tokenManager, tc.localIdentities) |
| 561 |
if got, want := (authMechanisms == nil), (tc.expectedAuthMechanisms == nil); got != want { |
| 562 |
t.Errorf("authMechanisms == nil: %t, tc.expectedAuthMechanisms == nil: %t", got, want) |
| 563 |
} |
| 564 |
if authMechanisms != nil && tc.expectedAuthMechanisms != nil { |
| 565 |
if diff := cmp.Diff(authMechanisms, tc.expectedAuthMechanisms, protocmp.Transform(), sortProtos); diff != "" { |
| 566 |
t.Errorf("getAuthMechanisms(%v, %v) returned incorrect slice, (-want +got):\n%s", tc.tokenManager, tc.localIdentities, diff) |
| 567 |
} |
| 568 |
} |
| 569 |
}) |
| 570 |
} |
| 571 |
} |
| 572 |
func TestGetServerConfigFromS2Av2(t *testing.T) { |
| 573 |
// Start up fake S2Av2 server. |
| 574 |
var wg sync.WaitGroup |
| 575 |
wg.Add(1) |
| 576 |
stop, address, err := startFakeS2Av2Server(&wg, "TestGetServerConfigFromS2Av2_token", false) |
| 577 |
wg.Wait() |
| 578 |
if err != nil { |
| 579 |
t.Fatalf("Error starting fake S2Av2 Server: %v", err) |
| 580 |
} |
| 581 |
for _, tc := range []struct { |
| 582 |
description string |
| 583 |
tokenManager tokenmanager.AccessTokenManager |
| 584 |
localIdentities []*commonpb.Identity |
| 585 |
expTLSConfig *s2av2pb.GetTlsConfigurationResp_ServerTlsConfiguration |
| 586 |
expErr error |
| 587 |
}{ |
| 588 |
{ |
| 589 |
description: "empty accessToken and empty localIdentities", |
| 590 |
tokenManager: &fakeAccessTokenManager{ |
| 591 |
allowEmptyIdentity: true, |
| 592 |
accessToken: "", |
| 593 |
}, |
| 594 |
localIdentities: nil, |
| 595 |
expErr: errors.New("rpc error: code = Unknown desc = SessionReq has no AuthenticationMechanism with a valid token"), |
| 596 |
}, |
| 597 |
{ |
| 598 |
description: "invalid accessToken", |
| 599 |
tokenManager: &fakeAccessTokenManager{ |
| 600 |
acceptedIdentity: &commonpb.Identity{ |
| 601 |
IdentityOneof: &commonpb.Identity_Hostname{ |
| 602 |
Hostname: "server_hostname", |
| 603 |
}, |
| 604 |
}, |
| 605 |
allowEmptyIdentity: true, |
| 606 |
accessToken: "invalid_access_token", |
| 607 |
}, |
| 608 |
localIdentities: []*commonpb.Identity{ |
| 609 |
{ |
| 610 |
IdentityOneof: &commonpb.Identity_Hostname{ |
| 611 |
Hostname: "server_hostname", |
| 612 |
}, |
| 613 |
}, |
| 614 |
}, |
| 615 |
expErr: errors.New("rpc error: code = Unknown desc = SessionReq has no AuthenticationMechanism with a valid token"), |
| 616 |
}, |
| 617 |
{ |
| 618 |
description: "correct accessToken and non - empty localIdentities", |
| 619 |
tokenManager: &fakeAccessTokenManager{ |
| 620 |
acceptedIdentity: &commonpb.Identity{ |
| 621 |
IdentityOneof: &commonpb.Identity_Hostname{ |
| 622 |
Hostname: "server_hostname", |
| 623 |
}, |
| 624 |
}, |
| 625 |
allowEmptyIdentity: true, |
| 626 |
accessToken: "TestGetServerConfigFromS2Av2_token", |
| 627 |
}, |
| 628 |
localIdentities: []*commonpb.Identity{ |
| 629 |
{ |
| 630 |
IdentityOneof: &commonpb.Identity_Hostname{ |
| 631 |
Hostname: "server_hostname", |
| 632 |
}, |
| 633 |
}, |
| 634 |
}, |
| 635 |
expTLSConfig: &s2av2pb.GetTlsConfigurationResp_ServerTlsConfiguration{ |
| 636 |
CertificateChain: []string{ |
| 637 |
string(serverCertpem), |
| 638 |
}, |
| 639 |
MinTlsVersion: commonpb.TLSVersion_TLS_VERSION_1_3, |
| 640 |
MaxTlsVersion: commonpb.TLSVersion_TLS_VERSION_1_3, |
| 641 |
TlsResumptionEnabled: false, |
| 642 |
RequestClientCertificate: s2av2pb.GetTlsConfigurationResp_ServerTlsConfiguration_REQUEST_AND_REQUIRE_CLIENT_CERTIFICATE_AND_VERIFY, |
| 643 |
MaxOverheadOfTicketAead: 0, |
| 644 |
}, |
| 645 |
expErr: nil, |
| 646 |
}, |
| 647 |
} { |
| 648 |
t.Run(tc.description, func(t *testing.T) { |
| 649 |
// Create stream to S2Av2. |
| 650 |
opts := []grpc.DialOption{ |
| 651 |
grpc.WithTransportCredentials(insecure.NewCredentials()), |
| 652 |
grpc.WithReturnConnectionError(), |
| 653 |
grpc.WithBlock(), |
| 654 |
} |
| 655 |
conn, err := grpc.Dial(address, opts...) |
| 656 |
if err != nil { |
| 657 |
t.Fatalf("Client: failed to connect: %v", err) |
| 658 |
} |
| 659 |
defer conn.Close() |
| 660 |
c := s2av2pb.NewS2AServiceClient(conn) |
| 661 |
log.Printf("Client: connected to: %s", address) |
| 662 |
ctx, cancel := context.WithTimeout(context.Background(), defaultTimeout) |
| 663 |
defer cancel() |
| 664 |
|
| 665 |
// Setup bidrectional streaming session. |
| 666 |
callOpts := []grpc.CallOption{} |
| 667 |
cstream, err := c.SetUpSession(ctx, callOpts...) |
| 668 |
if err != nil { |
| 669 |
t.Fatalf("Client: failed to setup bidirectional streaming RPC session: %v", err) |
| 670 |
} |
| 671 |
log.Printf("Client: set up bidirectional streaming RPC session.") |
| 672 |
gotTLSConfig, gotErr := getServerConfigFromS2Av2(tc.tokenManager, tc.localIdentities, "test_server_name", cstream) |
| 673 |
if gotErr != tc.expErr { |
| 674 |
if (gotErr == nil) || (tc.expErr == nil) { |
| 675 |
t.Errorf("gotErr = %v, tc.expErr = %v", gotErr, tc.expErr) |
| 676 |
} else if gotErr.Error() != tc.expErr.Error() { |
| 677 |
t.Errorf("gotErr = %v, tc.expErr = %v", gotErr, tc.expErr) |
| 678 |
} |
| 679 |
} |
| 680 |
if (gotErr == nil) && (tc.expErr == nil) { |
| 681 |
if diff := cmp.Diff(gotTLSConfig, tc.expTLSConfig, protocmp.Transform()); diff != "" { |
| 682 |
t.Errorf("getServerConfigFromS2Av2 returned incorrect GetTlsConfigurationResp_ServerTlsConfiguration, (-want +got):\n%s", diff) |
| 683 |
} |
| 684 |
} |
| 685 |
}) |
| 686 |
} |
| 687 |
stop() |
| 688 |
} |
| 689 |
|
| 690 |
func TestGetClientConfig(t *testing.T) { |
| 691 |
// Setup test. |
| 692 |
cert, err := tls.X509KeyPair(serverCertpem, serverKeypem) |
| 693 |
if err != nil { |
| 694 |
t.Fatalf("tls.X509KeyPair failed: %v", err) |
| 695 |
} |
| 696 |
// Start up fake S2Av2 server. |
| 697 |
var wg sync.WaitGroup |
| 698 |
wg.Add(1) |
| 699 |
stop, address, err := startFakeS2Av2Server(&wg, "TestGetClientConfig_token", false) |
| 700 |
wg.Wait() |
| 701 |
if err != nil { |
| 702 |
t.Fatalf("Error starting fake S2Av2 Server: %v", err) |
| 703 |
} |
| 704 |
|
| 705 |
accessTokenManager := &fakeAccessTokenManager{ |
| 706 |
accessToken: "TestGetClientConfig_token", |
| 707 |
allowEmptyIdentity: true, |
| 708 |
} |
| 709 |
var identities []*commonpb.Identity |
| 710 |
identities = append(identities, nil) |
| 711 |
for _, tc := range []struct { |
| 712 |
description string |
| 713 |
Certificates []tls.Certificate |
| 714 |
SessionTicketsDisabled bool |
| 715 |
ClientAuth tls.ClientAuthType |
| 716 |
MinVersion uint16 |
| 717 |
MaxVersion uint16 |
| 718 |
NextProtos []string |
| 719 |
}{ |
| 720 |
{ |
| 721 |
description: "static", |
| 722 |
Certificates: []tls.Certificate{cert}, |
| 723 |
SessionTicketsDisabled: true, |
| 724 |
ClientAuth: tls.RequireAnyClientCert, |
| 725 |
MinVersion: tls.VersionTLS13, |
| 726 |
MaxVersion: tls.VersionTLS13, |
| 727 |
NextProtos: []string{"h2"}, |
| 728 |
}, |
| 729 |
} { |
| 730 |
t.Run(tc.description, func(t *testing.T) { |
| 731 |
// Create stream to S2Av2. |
| 732 |
opts := []grpc.DialOption{ |
| 733 |
grpc.WithTransportCredentials(insecure.NewCredentials()), |
| 734 |
grpc.WithReturnConnectionError(), |
| 735 |
grpc.WithBlock(), |
| 736 |
} |
| 737 |
conn, err := grpc.Dial(address, opts...) |
| 738 |
if err != nil { |
| 739 |
t.Fatalf("Client: failed to connect: %v", err) |
| 740 |
} |
| 741 |
defer conn.Close() |
| 742 |
c := s2av2pb.NewS2AServiceClient(conn) |
| 743 |
log.Printf("Client: connected to: %s", address) |
| 744 |
ctx, cancel := context.WithTimeout(context.Background(), defaultTimeout) |
| 745 |
defer cancel() |
| 746 |
|
| 747 |
// Setup bidrectional streaming session. |
| 748 |
callOpts := []grpc.CallOption{} |
| 749 |
cstream, err := c.SetUpSession(ctx, callOpts...) |
| 750 |
if err != nil { |
| 751 |
t.Fatalf("Client: failed to setup bidirectional streaming RPC session: %v", err) |
| 752 |
} |
| 753 |
log.Printf("Client: set up bidirectional streaming RPC session.") |
| 754 |
clientConfigFunc := ClientConfig(accessTokenManager, identities, s2av2pb.ValidatePeerCertificateChainReq_CONNECT_TO_GOOGLE, cstream) |
| 755 |
config, err := clientConfigFunc(&tls.ClientHelloInfo{ |
| 756 |
ServerName: "host_1", |
| 757 |
}) |
| 758 |
if err != nil { |
| 759 |
t.Errorf("ClientConfig failed: %v", err) |
| 760 |
} |
| 761 |
if got, want := config.Certificates[0].Certificate[0], tc.Certificates[0].Certificate[0]; !bytes.Equal(got, want) { |
| 762 |
t.Errorf("config.Certificates[0].Certificate[0] = %v, want %v", got, want) |
| 763 |
} |
| 764 |
if got, want := config.SessionTicketsDisabled, tc.SessionTicketsDisabled; got != want { |
| 765 |
t.Errorf("config.SessionTicketsDisabled = %v, want %v", got, want) |
| 766 |
} |
| 767 |
if got, want := config.ClientAuth, tc.ClientAuth; got != want { |
| 768 |
t.Errorf("config.ClientAuth = %v, want %v", got, want) |
| 769 |
} |
| 770 |
if got, want := config.MinVersion, tc.MinVersion; got != want { |
| 771 |
t.Errorf("config.MinVersion = %v, want %v", got, want) |
| 772 |
} |
| 773 |
if got, want := config.MaxVersion, tc.MaxVersion; got != want { |
| 774 |
t.Errorf("config.MaxVersion = %v, want %v", got, want) |
| 775 |
} |
| 776 |
if !compareNextProtos(config.NextProtos, tc.NextProtos) { |
| 777 |
t.Errorf("config.NextProtos = %v, want %v", config.NextProtos, tc.NextProtos) |
| 778 |
} |
| 779 |
}) |
| 780 |
} |
| 781 |
stop() |
| 782 |
} |
| 783 |
|
| 784 |
func TestGetTLSClientAuthType(t *testing.T) { |
| 785 |
for _, tc := range []struct { |
| 786 |
description string |
| 787 |
tlsConfig *s2av2pb.GetTlsConfigurationResp_ServerTlsConfiguration |
| 788 |
expClientAuthType tls.ClientAuthType |
| 789 |
}{ |
| 790 |
{ |
| 791 |
description: "Don't request client cert", |
| 792 |
tlsConfig: &s2av2pb.GetTlsConfigurationResp_ServerTlsConfiguration{ |
| 793 |
RequestClientCertificate: s2av2pb.GetTlsConfigurationResp_ServerTlsConfiguration_DONT_REQUEST_CLIENT_CERTIFICATE, |
| 794 |
}, |
| 795 |
expClientAuthType: tls.NoClientCert, |
| 796 |
}, |
| 797 |
{ |
| 798 |
description: "Request client cert, but don't verify", |
| 799 |
tlsConfig: &s2av2pb.GetTlsConfigurationResp_ServerTlsConfiguration{ |
| 800 |
RequestClientCertificate: s2av2pb.GetTlsConfigurationResp_ServerTlsConfiguration_REQUEST_CLIENT_CERTIFICATE_BUT_DONT_VERIFY, |
| 801 |
}, |
| 802 |
expClientAuthType: tls.RequestClientCert, |
| 803 |
}, |
| 804 |
{ |
| 805 |
description: "Request client cert and verify", |
| 806 |
tlsConfig: &s2av2pb.GetTlsConfigurationResp_ServerTlsConfiguration{ |
| 807 |
RequestClientCertificate: s2av2pb.GetTlsConfigurationResp_ServerTlsConfiguration_REQUEST_CLIENT_CERTIFICATE_AND_VERIFY, |
| 808 |
}, |
| 809 |
expClientAuthType: tls.RequireAnyClientCert, |
| 810 |
}, |
| 811 |
{ |
| 812 |
description: "Request and Require client cert, but don't verify", |
| 813 |
tlsConfig: &s2av2pb.GetTlsConfigurationResp_ServerTlsConfiguration{ |
| 814 |
RequestClientCertificate: s2av2pb.GetTlsConfigurationResp_ServerTlsConfiguration_REQUEST_AND_REQUIRE_CLIENT_CERTIFICATE_BUT_DONT_VERIFY, |
| 815 |
}, |
| 816 |
expClientAuthType: tls.RequireAnyClientCert, |
| 817 |
}, |
| 818 |
{ |
| 819 |
description: "Request and Require client cert and verify", |
| 820 |
tlsConfig: &s2av2pb.GetTlsConfigurationResp_ServerTlsConfiguration{ |
| 821 |
RequestClientCertificate: s2av2pb.GetTlsConfigurationResp_ServerTlsConfiguration_REQUEST_AND_REQUIRE_CLIENT_CERTIFICATE_AND_VERIFY, |
| 822 |
}, |
| 823 |
expClientAuthType: tls.RequireAnyClientCert, |
| 824 |
}, |
| 825 |
{ |
| 826 |
description: "default case", |
| 827 |
tlsConfig: &s2av2pb.GetTlsConfigurationResp_ServerTlsConfiguration{ |
| 828 |
RequestClientCertificate: s2av2pb.GetTlsConfigurationResp_ServerTlsConfiguration_UNSPECIFIED, |
| 829 |
}, |
| 830 |
expClientAuthType: tls.RequireAnyClientCert, |
| 831 |
}, |
| 832 |
} { |
| 833 |
t.Run(tc.description, func(t *testing.T) { |
| 834 |
if got, want := getTLSClientAuthType(tc.tlsConfig), tc.expClientAuthType; got != want { |
| 835 |
t.Errorf("getClientAuthType(%v) returned %v, want = %v", tc.tlsConfig, got, want) |
| 836 |
} |
| 837 |
}) |
| 838 |
} |
| 839 |
} |
| 840 |
|
| 841 |
func TestGetTLSCipherSuite(t *testing.T) { |
| 842 |
for _, tc := range []struct { |
| 843 |
description string |
| 844 |
inp commonpb.Ciphersuite |
| 845 |
expOut uint16 |
| 846 |
}{ |
| 847 |
{ |
| 848 |
description: "ECDSA with AES 128 GCM SHA256", |
| 849 |
inp: commonpb.Ciphersuite_CIPHERSUITE_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256, |
| 850 |
expOut: tls.TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256, |
| 851 |
}, |
| 852 |
{ |
| 853 |
description: "ECDSA with AES 256 GCM SHA384", |
| 854 |
inp: commonpb.Ciphersuite_CIPHERSUITE_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384, |
| 855 |
expOut: tls.TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384, |
| 856 |
}, |
| 857 |
{ |
| 858 |
description: "ECDSA with CHACHA20 Poly 1305 SHA256", |
| 859 |
inp: commonpb.Ciphersuite_CIPHERSUITE_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256, |
| 860 |
expOut: tls.TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256, |
| 861 |
}, |
| 862 |
{ |
| 863 |
description: "RSA with AES 128 GCM SHA256", |
| 864 |
inp: commonpb.Ciphersuite_CIPHERSUITE_ECDHE_RSA_WITH_AES_128_GCM_SHA256, |
| 865 |
expOut: tls.TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256, |
| 866 |
}, |
| 867 |
{ |
| 868 |
description: "RSA with AES 256 GCM SHA384", |
| 869 |
inp: commonpb.Ciphersuite_CIPHERSUITE_ECDHE_RSA_WITH_AES_256_GCM_SHA384, |
| 870 |
expOut: tls.TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384, |
| 871 |
}, |
| 872 |
{ |
| 873 |
description: "RSA with CHACHA20 Poly1305 SHA256", |
| 874 |
inp: commonpb.Ciphersuite_CIPHERSUITE_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256, |
| 875 |
expOut: tls.TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256, |
| 876 |
}, |
| 877 |
{ |
| 878 |
description: "unspecified", |
| 879 |
inp: commonpb.Ciphersuite_CIPHERSUITE_UNSPECIFIED, |
| 880 |
expOut: 0xffff, |
| 881 |
}, |
| 882 |
} { |
| 883 |
t.Run(tc.description, func(t *testing.T) { |
| 884 |
if got, want := getTLSCipherSuite(tc.inp), tc.expOut; got != want { |
| 885 |
t.Errorf("getTLSCipherSuite(%v) returned %v, want %v", tc.inp, got, want) |
| 886 |
} |
| 887 |
}) |
| 888 |
} |
| 889 |
} |
| 890 |
|
| 891 |
func TestGetCipherSuites(t *testing.T) { |
| 892 |
for _, tc := range []struct { |
| 893 |
description string |
| 894 |
inp []commonpb.Ciphersuite |
| 895 |
expOut []uint16 |
| 896 |
}{ |
| 897 |
{ |
| 898 |
description: "empty input", |
| 899 |
inp: []commonpb.Ciphersuite{}, |
| 900 |
expOut: []uint16{}, |
| 901 |
}, |
| 902 |
{ |
| 903 |
description: "non - empty input array of size 1", |
| 904 |
inp: []commonpb.Ciphersuite{commonpb.Ciphersuite_CIPHERSUITE_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256}, |
| 905 |
expOut: []uint16{tls.TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256}, |
| 906 |
}, |
| 907 |
{ |
| 908 |
description: "non - empty input array of size 2", |
| 909 |
inp: []commonpb.Ciphersuite{commonpb.Ciphersuite_CIPHERSUITE_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256, commonpb.Ciphersuite_CIPHERSUITE_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384}, |
| 910 |
expOut: []uint16{tls.TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256, tls.TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384}, |
| 911 |
}, |
| 912 |
{ |
| 913 |
description: "unspecified array of size 1", |
| 914 |
inp: []commonpb.Ciphersuite{commonpb.Ciphersuite_CIPHERSUITE_UNSPECIFIED}, |
| 915 |
expOut: []uint16{}, |
| 916 |
}, |
| 917 |
{ |
| 918 |
description: "unspecified array of size 2", |
| 919 |
inp: []commonpb.Ciphersuite{commonpb.Ciphersuite_CIPHERSUITE_UNSPECIFIED, commonpb.Ciphersuite_CIPHERSUITE_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256}, |
| 920 |
expOut: []uint16{tls.TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256}, |
| 921 |
}, |
| 922 |
} { |
| 923 |
t.Run(tc.description, func(t *testing.T) { |
| 924 |
if got, want := getCipherSuites(tc.inp), tc.expOut; !compareCipherSuites(got, want) { |
| 925 |
t.Errorf("getCipherSuites(%+v) returned %+v, want %+v", tc.inp, got, want) |
| 926 |
} |
| 927 |
}) |
| 928 |
} |
| 929 |
} |
| 930 |
|
| 931 |
func makeMapOfTLSVersions() map[commonpb.TLSVersion]uint16 { |
| 932 |
m := make(map[commonpb.TLSVersion]uint16) |
| 933 |
m[commonpb.TLSVersion_TLS_VERSION_1_0] = tls.VersionTLS10 |
| 934 |
m[commonpb.TLSVersion_TLS_VERSION_1_1] = tls.VersionTLS11 |
| 935 |
m[commonpb.TLSVersion_TLS_VERSION_1_2] = tls.VersionTLS12 |
| 936 |
m[commonpb.TLSVersion_TLS_VERSION_1_3] = tls.VersionTLS13 |
| 937 |
return m |
| 938 |
} |
| 939 |
|
| 940 |
func compareNextProtos(a, b []string) bool { |
| 941 |
if len(a) != len(b) { |
| 942 |
return false |
| 943 |
} |
| 944 |
for i, v := range a { |
| 945 |
if v != b[i] { |
| 946 |
return false |
| 947 |
} |
| 948 |
} |
| 949 |
return true |
| 950 |
} |
| 951 |
|
| 952 |
func compareCipherSuites(a, b []uint16) bool { |
| 953 |
if len(a) != len(b) { |
| 954 |
return false |
| 955 |
} |
| 956 |
for i, v := range a { |
| 957 |
if v != b[i] { |
| 958 |
return false |
| 959 |
} |
| 960 |
} |
| 961 |
return true |
| 962 |
} |