Skip to content

Commit 6904fdc

Browse files
authored
certutil: restore previous GenerateChildCert signature (#237)
It restores the previous GenerateChildCert to undo a breaking change and creates GenerateGenericChildCert with the new signature
1 parent 196dace commit 6904fdc

File tree

3 files changed

+40
-9
lines changed

3 files changed

+40
-9
lines changed

testing/certutil/certutil.go

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -71,14 +71,45 @@ func NewRSARootCA() (crypto.PrivateKey, *x509.Certificate, Pair, error) {
7171
return rootKey, cert, pair, err
7272
}
7373

74-
// GenerateChildCert generates a x509 Certificate as a child of caCert and
75-
// returns the following:
76-
// - the certificate in PEM format as a byte slice
77-
// - the private key in PEM format as a byte slice
74+
// GenerateChildCert generates a ECDSA (P-384) x509 Certificate as a child of
75+
// caCert and returns the following:
7876
// - the certificate and private key as a tls.Certificate
77+
// - a Pair with the certificate and its key im PEM format
7978
//
8079
// If any error occurs during the generation process, a non-nil error is returned.
81-
func GenerateChildCert(
80+
func GenerateChildCert(name string, ips []net.IP, caPrivKey crypto.PrivateKey, caCert *x509.Certificate) (*tls.Certificate, Pair, error) {
81+
priv, err := rsa.GenerateKey(rand.Reader, 2048)
82+
if err != nil {
83+
return nil, Pair{}, fmt.Errorf("could not create RSA private key: %w", err)
84+
}
85+
86+
cert, childPair, err :=
87+
GenerateGenericChildCert(
88+
name,
89+
ips,
90+
priv,
91+
&priv.PublicKey,
92+
caPrivKey,
93+
caCert)
94+
if err != nil {
95+
return nil, Pair{}, fmt.Errorf(
96+
"could not generate child TLS certificate CA: %w", err)
97+
}
98+
99+
return cert, childPair, nil
100+
}
101+
102+
// GenerateGenericChildCert generates a x509 Certificate using priv and pub
103+
// as the certificate's private and public keys and as a child of caCert.
104+
// Use this function if you need fine control over keys or ips and certificate name,
105+
// otherwise prefer GenerateChildCert or NewRootAndChildCerts/NewRSARootAndChildCerts
106+
//
107+
// It returns the following:
108+
// - the certificate and private key as a tls.Certificate
109+
// - a Pair with the certificate and its key im PEM format
110+
//
111+
// If any error occurs during the generation process, a non-nil error is returned.
112+
func GenerateGenericChildCert(
82113
name string,
83114
ips []net.IP,
84115
priv crypto.PrivateKey,
@@ -263,7 +294,7 @@ func defaultChildCert(
263294
pub crypto.PublicKey,
264295
rootCACert *x509.Certificate) (Pair, error) {
265296
_, childPair, err :=
266-
GenerateChildCert(
297+
GenerateGenericChildCert(
267298
"localhost",
268299
[]net.IP{net.ParseIP("127.0.0.1")},
269300
priv,

testing/certutil/certutil_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import (
2626
"github.com/stretchr/testify/require"
2727
)
2828

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

testing/certutil/cmd/main.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
// specific language governing permissions and limitations
1616
// under the License.
1717

18-
// nolint:errorlint,forbidigo // it's a cli application
18+
//nolint:errorlint,forbidigo // it's a cli application
1919
package main
2020

2121
import (
@@ -84,7 +84,7 @@ func main() {
8484
rootCert, rootKey := getCA(rsa, caPath, caKeyPath, dest, filePrefix)
8585
priv, pub := generateKey(rsa)
8686

87-
childCert, childPair, err := certutil.GenerateChildCert(
87+
childCert, childPair, err := certutil.GenerateGenericChildCert(
8888
name,
8989
netIPs,
9090
priv,

0 commit comments

Comments
 (0)