-
Notifications
You must be signed in to change notification settings - Fork 12
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[fftls] Allow for In-Memory or Inlined TLS Certificates #132
Changes from 4 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
// Copyright © 2023 Kaleido, Inc. | ||
// Copyright © 2024 Kaleido, Inc. | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
|
@@ -24,9 +24,11 @@ import ( | |
"crypto/x509" | ||
"crypto/x509/pkix" | ||
"encoding/pem" | ||
"github.com/stretchr/testify/require" | ||
"math/big" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there a specific reason to pull in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So It's just syntactic sugar for: if err != nil {
t.Fatal(err)
} I believe |
||
"net" | ||
"os" | ||
"strings" | ||
"testing" | ||
"time" | ||
|
||
|
@@ -35,6 +37,34 @@ import ( | |
) | ||
|
||
func buildSelfSignedTLSKeyPair(t *testing.T, subject pkix.Name) (string, string) { | ||
// Create an X509 certificate pair | ||
privatekey, _ := rsa.GenerateKey(rand.Reader, 2048) | ||
publickey := &privatekey.PublicKey | ||
var privateKeyBytes []byte = x509.MarshalPKCS1PrivateKey(privatekey) | ||
privateKeyBlock := &pem.Block{Type: "RSA PRIVATE KEY", Bytes: privateKeyBytes} | ||
privateKeyPEM := &strings.Builder{} | ||
err := pem.Encode(privateKeyPEM, privateKeyBlock) | ||
require.NoError(t, err) | ||
serialNumber, err := rand.Int(rand.Reader, new(big.Int).Lsh(big.NewInt(1), 128)) | ||
x509Template := &x509.Certificate{ | ||
SerialNumber: serialNumber, | ||
Subject: subject, | ||
NotBefore: time.Now(), | ||
NotAfter: time.Now().Add(100 * time.Second), | ||
KeyUsage: x509.KeyUsageDigitalSignature, | ||
BasicConstraintsValid: true, | ||
IPAddresses: []net.IP{net.IPv4(127, 0, 0, 1)}, | ||
} | ||
require.NoError(t, err) | ||
derBytes, err := x509.CreateCertificate(rand.Reader, x509Template, x509Template, publickey, privatekey) | ||
require.NoError(t, err) | ||
publicKeyPEM := &strings.Builder{} | ||
err = pem.Encode(publicKeyPEM, &pem.Block{Type: "CERTIFICATE", Bytes: derBytes}) | ||
require.NoError(t, err) | ||
return publicKeyPEM.String(), privateKeyPEM.String() | ||
} | ||
|
||
func buildSelfSignedTLSKeyPairFiles(t *testing.T, subject pkix.Name) (string, string) { | ||
// Create an X509 certificate pair | ||
privatekey, _ := rsa.GenerateKey(rand.Reader, 2048) | ||
publickey := &privatekey.PublicKey | ||
|
@@ -129,7 +159,7 @@ func TestTLSDefault(t *testing.T) { | |
func TestErrInvalidCAFile(t *testing.T) { | ||
|
||
config.RootConfigReset() | ||
_, notTheCAFileTheKey := buildSelfSignedTLSKeyPair(t, pkix.Name{ | ||
_, notTheCAFileTheKey := buildSelfSignedTLSKeyPairFiles(t, pkix.Name{ | ||
CommonName: "server.example.com", | ||
}) | ||
|
||
|
@@ -140,13 +170,28 @@ func TestErrInvalidCAFile(t *testing.T) { | |
|
||
_, err := ConstructTLSConfig(context.Background(), conf, ClientType) | ||
assert.Regexp(t, "FF00152", err) | ||
} | ||
|
||
func TestErrInvalidCA(t *testing.T) { | ||
|
||
config.RootConfigReset() | ||
_, notTheCATheKey := buildSelfSignedTLSKeyPair(t, pkix.Name{ | ||
CommonName: "server.example.com", | ||
}) | ||
|
||
conf := config.RootSection("fftls_server") | ||
InitTLSConfig(conf) | ||
conf.Set(HTTPConfTLSEnabled, true) | ||
conf.Set(HTTPConfTLSCA, notTheCATheKey) | ||
|
||
_, err := ConstructTLSConfig(context.Background(), conf, ClientType) | ||
assert.Regexp(t, "FF00152", err) | ||
} | ||
|
||
func TestErrInvalidKeyPairFile(t *testing.T) { | ||
|
||
config.RootConfigReset() | ||
notTheKeyFile, notTheCertFile := buildSelfSignedTLSKeyPair(t, pkix.Name{ | ||
notTheKeyFile, notTheCertFile := buildSelfSignedTLSKeyPairFiles(t, pkix.Name{ | ||
CommonName: "server.example.com", | ||
}) | ||
|
||
|
@@ -161,12 +206,29 @@ func TestErrInvalidKeyPairFile(t *testing.T) { | |
|
||
} | ||
|
||
func TestMTLSOk(t *testing.T) { | ||
func TestErrInvalidKeyPair(t *testing.T) { | ||
|
||
serverPublicKeyFile, serverKeyFile := buildSelfSignedTLSKeyPair(t, pkix.Name{ | ||
config.RootConfigReset() | ||
notTheKey, notTheCert := buildSelfSignedTLSKeyPair(t, pkix.Name{ | ||
CommonName: "server.example.com", | ||
}) | ||
clientPublicKeyFile, clientKeyFile := buildSelfSignedTLSKeyPair(t, pkix.Name{ | ||
|
||
conf := config.RootSection("fftls_server") | ||
InitTLSConfig(conf) | ||
conf.Set(HTTPConfTLSEnabled, true) | ||
conf.Set(HTTPConfTLSKey, notTheKey) | ||
conf.Set(HTTPConfTLSCert, notTheCert) | ||
|
||
_, err := ConstructTLSConfig(context.Background(), conf, ClientType) | ||
assert.Regexp(t, "FF00206", err) | ||
|
||
} | ||
|
||
func TestMTLSOk(t *testing.T) { | ||
serverPublicKey, serverKey := buildSelfSignedTLSKeyPair(t, pkix.Name{ | ||
CommonName: "server.example.com", | ||
}) | ||
clientPublicKey, clientKey := buildSelfSignedTLSKeyPair(t, pkix.Name{ | ||
CommonName: "client.example.com", | ||
}) | ||
|
||
|
@@ -175,9 +237,9 @@ func TestMTLSOk(t *testing.T) { | |
serverConf := config.RootSection("fftls_server") | ||
InitTLSConfig(serverConf) | ||
serverConf.Set(HTTPConfTLSEnabled, true) | ||
serverConf.Set(HTTPConfTLSCAFile, clientPublicKeyFile) | ||
serverConf.Set(HTTPConfTLSCertFile, serverPublicKeyFile) | ||
serverConf.Set(HTTPConfTLSKeyFile, serverKeyFile) | ||
serverConf.Set(HTTPConfTLSCA, clientPublicKey) | ||
serverConf.Set(HTTPConfTLSCert, serverPublicKey) | ||
serverConf.Set(HTTPConfTLSKey, serverKey) | ||
serverConf.Set(HTTPConfTLSClientAuth, true) | ||
|
||
addr, done := buildTLSListener(t, serverConf, ServerType) | ||
|
@@ -186,9 +248,9 @@ func TestMTLSOk(t *testing.T) { | |
clientConf := config.RootSection("fftls_client") | ||
InitTLSConfig(clientConf) | ||
clientConf.Set(HTTPConfTLSEnabled, true) | ||
clientConf.Set(HTTPConfTLSCAFile, serverPublicKeyFile) | ||
clientConf.Set(HTTPConfTLSCertFile, clientPublicKeyFile) | ||
clientConf.Set(HTTPConfTLSKeyFile, clientKeyFile) | ||
clientConf.Set(HTTPConfTLSCA, serverPublicKey) | ||
clientConf.Set(HTTPConfTLSCert, clientPublicKey) | ||
clientConf.Set(HTTPConfTLSKey, clientKey) | ||
|
||
tlsConfig, err := ConstructTLSConfig(context.Background(), clientConf, ClientType) | ||
assert.NoError(t, err) | ||
|
@@ -208,7 +270,7 @@ func TestMTLSOk(t *testing.T) { | |
|
||
func TestMTLSMissingClientCert(t *testing.T) { | ||
|
||
serverPublicKeyFile, serverKeyFile := buildSelfSignedTLSKeyPair(t, pkix.Name{ | ||
serverPublicKeyFile, serverKeyFile := buildSelfSignedTLSKeyPairFiles(t, pkix.Name{ | ||
CommonName: "server.example.com", | ||
}) | ||
|
||
|
@@ -243,10 +305,10 @@ func TestMTLSMissingClientCert(t *testing.T) { | |
|
||
func TestMTLSMatchFullSubject(t *testing.T) { | ||
|
||
serverPublicKeyFile, serverKeyFile := buildSelfSignedTLSKeyPair(t, pkix.Name{ | ||
serverPublicKeyFile, serverKeyFile := buildSelfSignedTLSKeyPairFiles(t, pkix.Name{ | ||
CommonName: "server.example.com", | ||
}) | ||
clientPublicKeyFile, clientKeyFile := buildSelfSignedTLSKeyPair(t, pkix.Name{ | ||
clientPublicKeyFile, clientKeyFile := buildSelfSignedTLSKeyPairFiles(t, pkix.Name{ | ||
CommonName: "client.example.com", | ||
Country: []string{"GB"}, | ||
Organization: []string{"hyperledger"}, | ||
|
@@ -306,10 +368,10 @@ func TestMTLSMatchFullSubject(t *testing.T) { | |
|
||
func TestMTLSMismatchSubject(t *testing.T) { | ||
|
||
serverPublicKeyFile, serverKeyFile := buildSelfSignedTLSKeyPair(t, pkix.Name{ | ||
serverPublicKeyFile, serverKeyFile := buildSelfSignedTLSKeyPairFiles(t, pkix.Name{ | ||
CommonName: "server.example.com", | ||
}) | ||
clientPublicKeyFile, clientKeyFile := buildSelfSignedTLSKeyPair(t, pkix.Name{ | ||
clientPublicKeyFile, clientKeyFile := buildSelfSignedTLSKeyPairFiles(t, pkix.Name{ | ||
CommonName: "wrong.example.com", | ||
}) | ||
|
||
|
@@ -429,7 +491,7 @@ func TestMTLSDNValidatorEmptyChain(t *testing.T) { | |
|
||
func TestConnectSkipVerification(t *testing.T) { | ||
|
||
serverPublicKeyFile, serverKeyFile := buildSelfSignedTLSKeyPair(t, pkix.Name{ | ||
serverPublicKeyFile, serverKeyFile := buildSelfSignedTLSKeyPairFiles(t, pkix.Name{ | ||
CommonName: "server.example.com", | ||
}) | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should document (probably in this comment so it's visible to the developer) that if these are set they will take precedence over the "file" fields.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well its the opposite in that the file fields take precedence over these fields for backwards compatibility's sake.
That could be the wrong approach, but I'm updating the comments now to reflect the current behavior.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Comments updated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You're right sorry. Had the switch statement backwards in my head.