-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Static TLS Configuration Store Library
- Loading branch information
Showing
8 changed files
with
401 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
This directory has implementation for S2Av2 go client. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test") | ||
|
||
package( | ||
default_visibility = ["//internal/v2:__subpackages__"], | ||
) | ||
|
||
go_library( | ||
name = "tls_config_store", | ||
srcs = ["tls_config_store.go"], | ||
importpath = "github.com/google/s2a-go/internal/v2/tls_config_store", | ||
embedsrcs = [ | ||
"example_cert_key/client_cert.pem", | ||
"example_cert_key/client_key.pem", | ||
"example_cert_key/server_cert.pem", | ||
"example_cert_key/server_key.pem", | ||
], | ||
) | ||
|
||
go_test( | ||
name = "tls_config_store_test", | ||
srcs = ["tls_config_store_test.go"], | ||
embed = [":tls_config_store"], | ||
embedsrcs = [ | ||
"example_cert_key/client_cert.pem", | ||
"example_cert_key/client_key.pem", | ||
"example_cert_key/server_cert.pem", | ||
"example_cert_key/server_key.pem", | ||
], | ||
) |
25 changes: 25 additions & 0 deletions
25
internal/v2/tls_config_store/example_cert_key/client_cert.pem
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
-----BEGIN CERTIFICATE----- | ||
MIID8TCCAtmgAwIBAgIUMAQ1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fAU+rbzwk3pmQUkKiTkq7WFmIo+14+ra4RGA/JsfJVkLejZ8gVqkyJu1 | ||
lLdQDcGxiP3WjidUwzU7KhUu8Rw0nYXyzgfmQE+aixy9fRHEBsB1Vggofbi0pq+Y | ||
3cmesQ1zpRNL6RNwfa+R51jfatfNFhOjKl7xLj9LcWdYkTwki+233XTqXXH3TEgs | ||
fHjWhSt4/lczlDxZEYZ+/tOdCIPXX0V8YQ74e0vB4NCWW1wZYUAiwhzBJ7GPuVdJ | ||
TByGbU2PavPBvbLTi4zVm8dLoU+1ObLv8PzsbJhA27tIlkOs82im2ul+XLTkHvbB | ||
uIpxoWA= | ||
-----END CERTIFICATE----- | ||
|
28 changes: 28 additions & 0 deletions
28
internal/v2/tls_config_store/example_cert_key/client_key.pem
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
-----BEGIN RSA PRIVATE KEY----- | ||
MIIEpAIBAAKCAQEA73LaT+MFIDhjOS53/rqfFAw+SQ9Jj9ay1U1qmIxitsrkFP1S | ||
4+PcbEId5brOxhXelrdwrIKSiFQVIHeX879wQzHOLRbC05lIShDq2GQt4MbXdRQ/ | ||
Fi3kqo7vKbB6Io40UQtq+vT6QhAzowjFeyMtpUoE00LfaKfVU15qWxKDR143hpIO | ||
YoKz/QjjYgGWgSSrsoeokvjuDlUke5OJcP7R2Mz5WYAgs9hLYGDh1mEQA+4uuTIq | ||
95m+HLez9H5AN8BXKy5GKwJlKmL1Gb2NvHi22sEqeDawtaKW3kGSRqM0ZZFutoyf | ||
DOSB+Ob2SjPbaq6yQZ7juDf9bp7fEkRANYcBWwIDAQABAoIBADJ3vaW6zpjE6bzi | ||
m233/ZFnJzWU4EdN1DF6+K2gYSnvx3TZE8BuhUXYBZ8m6W/8qgaQMVJazvGm7zEB | ||
o+g/ADVZaQA93OBmXUM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Ou/T8s0v6 | ||
JwHaZY3MBJ9Zt+nBTWntUgdeNRz5XP8hUy65D7W8k1FaNsBGX9MdocYpyleVGkZ1 | ||
DaMRMgUu4p47jPSYjNqsqh37FmdRscqRe2F5eLlxCyJh3z5k5ND1YA== | ||
-----END RSA PRIVATE KEY----- | ||
|
25 changes: 25 additions & 0 deletions
25
internal/v2/tls_config_store/example_cert_key/server_cert.pem
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
-----BEGIN CERTIFICATE----- | ||
MIID8TCCAtmgAwIBAgIUISXQBSlrMDJp9mRdibxw/RV9X6wwDQYJKoZIhvcNAQEL | ||
BQAwgYcxCzAJBgNVBAYTAlVTMQswCQYDVQQIDAJDQTESMBAGA1UEBwwJU3Vubnl2 | ||
YWxlMRAwDgYDVQQKDAdDb21wYW55MREwDwYDVQQLDAhEaXZpc2lvbjEWMBQGA1UE | ||
AwwNczJhX3Rlc3RfY2VydDEaMBgGCSqGSIb3DQEJARYLeHl6QHh5ei5jb20wHhcN | ||
MjIwNTIzMTgwMTE1WhcNMjMwNTIzMTgwMTE1WjCBhzELMAkGA1UEBhMCVVMxCzAJ | ||
BgNVBAgMAkNBMRIwEAYDVQQHDAlTdW5ueXZhbGUxEDAOBgNVBAoMB0NvbXBhbnkx | ||
ETAPBgNVBAsMCERpdmlzaW9uMRYwFAYDVQQDDA1zMmFfdGVzdF9jZXJ0MRowGAYJ | ||
KoZIhvcNAQkBFgt4eXpAeHl6LmNvbTCCASIwDQYJKoZIhvcNAQEBBQADggEPADCC | ||
AQoCggEBAL23L/zvCQd6HlGNXcEn0IG6LTTP2unritO64vBdV3B5rCNfZEZ5kkku | ||
JtCTmJNUOivPkRJ4iYACSlcjepK+fEUdG7ihhYxurrkw3tLCRx3YjexlynZdmKxM | ||
6tcgMToFm4WTeG1E543B0mzM4be5CQyql5zpVOkf664TqYo0WoDlnPw8GsVaN0ek | ||
sAibnVi63Darlko7QBa+tteyBip+FcPpozJocy+GM/skWlZb+2x1lwIJqM1MZOXQ | ||
Ytc1u5ubzPZcinO1kkiGcoH0OlnKLQhjxDr+i4UZ3oQI5wft7Au4Z7K2H+s191+R | ||
x3DOBPvfvmJF1YHPhrj7MsK3KA7vaZMCAwEAAaNTMFEwHQYDVR0OBBYEFIiliZPw | ||
l2xoosx02Is18dytHQnZMB8GA1UdIwQYMBaAFIiliZPwl2xoosx02Is18dytHQnZ | ||
MA8GA1UdEwEB/wQFMAMBAf8wDQYJKoZIhvcNAQELBQADggEBACp2Gkk5rrwMBud1 | ||
MyAARaykIZKfbOzk0VXpOmOunjwM8Us2XLc3XUuOtvd3V4b+664+K0Iwlx7QVVO1 | ||
ytpeP9+afSIZtvx8kg2EYMHeBo2RHut8paoe3MT40A6vfnLtpOjZCjmuxjEa6LYM | ||
B4SqNcr9Oo80FXsb7i6iIqxXlXwrJBtlcXuHoyWWZW6EpnSNvkrwfGZcgnjXeiiW | ||
i3pujHeZaB6i/4UcS0dp7qpmMoLEpFRjtzXYQnUb0I5qH/O/SmiYKHEJWnfjmj6Y | ||
hW8HK+746OyhGVnEDNjLK91rZPgUvmNlEmUU0vYFZqJPfZmVgIKVAG2Pqs0c9p2y | ||
AhxtER4= | ||
-----END CERTIFICATE----- | ||
|
28 changes: 28 additions & 0 deletions
28
internal/v2/tls_config_store/example_cert_key/server_key.pem
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
-----BEGIN RSA PRIVATE KEY----- | ||
MIIEpQIBAAKCAQEAvbcv/O8JB3oeUY1dwSfQgbotNM/a6euK07ri8F1XcHmsI19k | ||
RnmSSS4m0JOYk1Q6K8+REniJgAJKVyN6kr58RR0buKGFjG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1qSMk0fgZ0kOcb0j3xRV62Qrn5/lZoKtKlMVFooaM1IQ5r0lc | ||
zXsrVC9Da2K8/Awyj+h1YUunVdgVzvnpKkyiL59tp1CD93WUuMqm2K2DTWfWsWJ2 | ||
b+YSKQ15CZJKQiM0zTzKsEPBAoGBAJfCPnbTHvDitrj2QmdCd4gAlsAPXKVi/7Eu | ||
bAqi1nImZKw1FBJLApHtl42yhnWkzIH50vPwe6veKF7BFoDUW0/vnSt58sUJvw1Q | ||
ZGxmrrTL/4c9MHcvlGTOl+Bd2kJaLfVdX++7kbbx6ArH6rb67ysZ7XsaWqNLPFPy | ||
ORl8CoupAoGAUyhKzR0dnfbayffEDJFSyiumcacc222cXoTFFHQN74wgTbrPyINp | ||
G2moRYYh4exqboiPxUXCMqFQ7zsYlLIfBJV2cEzSmPYcvoi6+9hiV2Es6HTNJ+05 | ||
XYTuBDwwVBV+1x7xZQ3vpoohrYcLD9Yd3lUE3LQJcu4zYvaQ/E5H+3I= | ||
-----END RSA PRIVATE KEY----- | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
// Static implementation of TLS Configuration Store (no calls to S2Av2, Remote Signer Library, Certificate Verifier) | ||
package tls_config_store | ||
|
||
import ( | ||
"log" | ||
"crypto/tls" | ||
"crypto/x509" | ||
"fmt" | ||
"time" | ||
|
||
_ "embed" | ||
) | ||
|
||
var ( | ||
//go:embed example_cert_key/client_cert.pem | ||
clientCert []byte | ||
//go:embed example_cert_key/server_cert.pem | ||
serverCert []byte | ||
//go:embed example_cert_key/client_key.pem | ||
clientKey []byte | ||
//go:embed example_cert_key/server_key.pem | ||
serverKey []byte | ||
) | ||
|
||
func VerifyPeerCertificateFunc(instanceName string, pool *x509.CertPool) func(rawCerts [][]byte, verifiedChains [][]*x509.Certificate) error { | ||
return func(rawCerts [][]byte, verifiedChains [][]*x509.Certificate) error { | ||
if len(rawCerts) == 0 { | ||
return fmt.Errorf("no certificate to verify") | ||
} | ||
cert, err := x509.ParseCertificate(rawCerts[0]) | ||
if err != nil { | ||
return fmt.Errorf("x509.ParseCertificate(rawCerts[0]) returned error: %v", err) | ||
} | ||
|
||
opts := x509.VerifyOptions{ | ||
CurrentTime: time.Now(), | ||
Roots: pool, | ||
} | ||
|
||
if _, err = cert.Verify(opts); err != nil { | ||
return err | ||
} | ||
|
||
if cert.Subject.CommonName != instanceName { | ||
return fmt.Errorf("certificate had Common Name %q, expected %q", cert.Subject.CommonName, instanceName) | ||
} | ||
return nil | ||
} | ||
} | ||
|
||
|
||
func GetTlsConfigurationForClient(id string) tls.Config { | ||
// Static implementation. TODO : Call S2Av2 for these values. | ||
min_version := uint16(tls.VersionTLS13) | ||
max_version := uint16(tls.VersionTLS13) | ||
var cipher_suites []uint16 | ||
var curve_preferences []tls.CurveID | ||
|
||
// Static implementation. TODO : Call remote signer library for Private Key. | ||
cert, err := tls.X509KeyPair(clientCert, clientKey) | ||
if err != nil { | ||
log.Fatalf("Failed to get client cert") | ||
} | ||
|
||
rootCertPool := x509.NewCertPool() | ||
rootCertPool.AppendCertsFromPEM(serverCert) | ||
|
||
// Create mTLS credentials for client. | ||
config := tls.Config { | ||
Certificates: []tls.Certificate{cert}, | ||
VerifyPeerCertificate: VerifyPeerCertificateFunc("s2a_test_cert", rootCertPool), // Static implementation. TODO : Call cert verifier library. | ||
RootCAs: rootCertPool, | ||
InsecureSkipVerify: true, | ||
CipherSuites: cipher_suites, | ||
ClientSessionCache: nil, | ||
MinVersion: min_version, | ||
MaxVersion: max_version, | ||
CurvePreferences: curve_preferences, | ||
} | ||
return config | ||
} | ||
|
||
|
||
func GetTlsConfigurationForServer(id string, server_name string) tls.Config { | ||
// Static implementation. TODO : Call S2Av2 for these values. | ||
min_version := uint16(tls.VersionTLS13) | ||
max_version := uint16(tls.VersionTLS13) | ||
var cipher_suites []uint16 | ||
var curve_preferences []tls.CurveID | ||
client_auth := tls.RequireAndVerifyClientCert | ||
|
||
// Static implementation. TODO : Call remote signer library for Private Key. | ||
cert, err := tls.X509KeyPair(serverCert, serverKey) | ||
if err != nil { | ||
log.Fatalf("Failed to get server cert") | ||
} | ||
|
||
certPool := x509.NewCertPool() | ||
certPool.AppendCertsFromPEM(clientCert) | ||
|
||
// Create mTLS credentials for server. | ||
config := tls.Config { | ||
Certificates: []tls.Certificate{cert}, | ||
VerifyPeerCertificate: VerifyPeerCertificateFunc("s2a_test_cert", certPool), // Static implementation. TODO : Call cert verifier library. | ||
ClientAuth: client_auth, | ||
ClientCAs: certPool, | ||
InsecureSkipVerify: true, | ||
CipherSuites: cipher_suites, | ||
MinVersion: min_version, | ||
MaxVersion: max_version, | ||
CurvePreferences: curve_preferences, | ||
} | ||
return config | ||
} |
Oops, something went wrong.