Skip to content

Commit

Permalink
Support x509 keypairs.
Browse files Browse the repository at this point in the history
  • Loading branch information
mdwhatcott committed Oct 9, 2024
1 parent 6c23e7a commit 4b8baa5
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion config.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,27 @@ func New(options ...option) (*tls.Config, error) {
return nil, fmt.Errorf("unable to parse trusted CA PEM: %w", ErrMalformedPEM)
}

for len(config.X509KeyPairs) > 0 {
var (
certPEMBlock = []byte(config.X509KeyPairs[0])
keyPEMBlock = []byte(config.X509KeyPairs[1])
)
cert, err := tls.X509KeyPair(certPEMBlock, keyPEMBlock)
if err != nil {
return nil, fmt.Errorf("unable to parse x509 keypair from PEM: %w", err)
}
target.Certificates = append(target.Certificates, cert)
config.X509KeyPairs = config.X509KeyPairs[2:]
}

if len(config.ServerName) > 0 {
target.ServerName = config.ServerName
}

target.MinVersion = config.MinTLSVersion
target.MaxVersion = config.MaxTLSVersion

// FUTURE: support server certificate(s), RSA/EC private key, and password-protected RSA/EC private key
// FUTURE: support server certificate(s), and password-protected RSA/EC private key
return target, nil
}
func resolvePEM(source, filename string) ([]byte, error) {
Expand Down Expand Up @@ -98,6 +111,7 @@ type configuration struct {
ServerName string
TrustedCAsPEM string
TrustedCAsPEMFile string
X509KeyPairs []string
MinTLSVersion uint16
MaxTLSVersion uint16
}
Expand Down Expand Up @@ -128,6 +142,9 @@ func (singleton) TrustedCAsPEMFile(value string) option {
}
}
}
func (singleton) X509KeyPair(certPEMBlock, keyPEMBlock string) option {
return func(this *configuration) { this.X509KeyPairs = append(this.X509KeyPairs, certPEMBlock, keyPEMBlock) }
}
func (singleton) MinTLSVersion(value uint16) option {
return func(this *configuration) { this.MinTLSVersion = value }
}
Expand Down

0 comments on commit 4b8baa5

Please sign in to comment.