Skip to content

Commit

Permalink
certutil: restore previous GenerateChildCert signature
Browse files Browse the repository at this point in the history
It restores the previous GenerateChildCert to undo a breaking change and creates GenerateGenericChildCert with the new signature
  • Loading branch information
AndersonQ committed Oct 9, 2024
1 parent 196dace commit 2e3c774
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 8 deletions.
43 changes: 37 additions & 6 deletions testing/certutil/certutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,45 @@ func NewRSARootCA() (crypto.PrivateKey, *x509.Certificate, Pair, error) {
return rootKey, cert, pair, err
}

// GenerateChildCert generates a x509 Certificate as a child of caCert and
// returns the following:
// - the certificate in PEM format as a byte slice
// - the private key in PEM format as a byte slice
// GenerateChildCert generates a ECDSA (P-384) x509 Certificate as a child of
// caCert and returns the following:
// - the certificate and private key as a tls.Certificate
// - a Pair with the certificate and its key im PEM format
//
// If any error occurs during the generation process, a non-nil error is returned.
func GenerateChildCert(
func GenerateChildCert(name string, ips []net.IP, caPrivKey crypto.PrivateKey, caCert *x509.Certificate) (*tls.Certificate, Pair, error) {
priv, err := rsa.GenerateKey(rand.Reader, 2048)
if err != nil {
return nil, Pair{}, fmt.Errorf("could not create RSA private key: %w", err)
}

cert, childPair, err :=
GenerateGenericChildCert(
name,
ips,
priv,
&priv.PublicKey,
caPrivKey,
caCert)
if err != nil {
return nil, Pair{}, fmt.Errorf(
"could not generate child TLS certificate CA: %w", err)
}

return cert, childPair, nil
}

// GenerateGenericChildCert generates a x509 Certificate using priv and pub
// as the certificate's private and public keys and as a child of caCert.
// Use this function if oyu need fine control over keys or ips and certificate name,
// otherwise prefer GenerateChildCert or NewRootAndChildCerts/NewRSARootAndChildCerts
//
// It returns the following:
// - the certificate and private key as a tls.Certificate
// - a Pair with the certificate and its key im PEM format
//
// If any error occurs during the generation process, a non-nil error is returned.
func GenerateGenericChildCert(
name string,
ips []net.IP,
priv crypto.PrivateKey,
Expand Down Expand Up @@ -263,7 +294,7 @@ func defaultChildCert(
pub crypto.PublicKey,
rootCACert *x509.Certificate) (Pair, error) {
_, childPair, err :=
GenerateChildCert(
GenerateGenericChildCert(
"localhost",
[]net.IP{net.ParseIP("127.0.0.1")},
priv,
Expand Down
2 changes: 1 addition & 1 deletion testing/certutil/certutil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (
"github.com/stretchr/testify/require"
)

func TestECCertificates(t *testing.T) {
func TestCertificates(t *testing.T) {
ecRootPair, ecChildPair, err := NewRootAndChildCerts()
require.NoError(t, err, "could not create EC certificates")

Expand Down
2 changes: 1 addition & 1 deletion testing/certutil/cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func main() {
rootCert, rootKey := getCA(rsa, caPath, caKeyPath, dest, filePrefix)
priv, pub := generateKey(rsa)

childCert, childPair, err := certutil.GenerateChildCert(
childCert, childPair, err := certutil.GenerateGenericChildCert(
name,
netIPs,
priv,
Expand Down

0 comments on commit 2e3c774

Please sign in to comment.