-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move certificate generation for tests into testutil package
Signed-off-by: Derek McGowan <[email protected]> (github: dmcgowan)
- Loading branch information
Showing
2 changed files
with
125 additions
and
104 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
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,94 @@ | ||
package testutil | ||
|
||
import ( | ||
"crypto" | ||
"crypto/rand" | ||
"crypto/x509" | ||
"crypto/x509/pkix" | ||
"math/big" | ||
"time" | ||
) | ||
|
||
// GenerateTrustCA generates a new certificate authority for testing. | ||
func GenerateTrustCA(pub crypto.PublicKey, priv crypto.PrivateKey) (*x509.Certificate, error) { | ||
cert := &x509.Certificate{ | ||
SerialNumber: big.NewInt(0), | ||
Subject: pkix.Name{ | ||
CommonName: "CA Root", | ||
}, | ||
NotBefore: time.Now().Add(-time.Second), | ||
NotAfter: time.Now().Add(time.Hour), | ||
IsCA: true, | ||
KeyUsage: x509.KeyUsageCertSign | x509.KeyUsageCRLSign, | ||
BasicConstraintsValid: true, | ||
} | ||
|
||
certDER, err := x509.CreateCertificate(rand.Reader, cert, cert, pub, priv) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
cert, err = x509.ParseCertificate(certDER) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return cert, nil | ||
} | ||
|
||
// GenerateIntermediate generates an intermediate certificate for testing using | ||
// the parent certificate (likely a CA) and the provided keys. | ||
func GenerateIntermediate(key crypto.PublicKey, parentKey crypto.PrivateKey, parent *x509.Certificate) (*x509.Certificate, error) { | ||
cert := &x509.Certificate{ | ||
SerialNumber: big.NewInt(0), | ||
Subject: pkix.Name{ | ||
CommonName: "Intermediate", | ||
}, | ||
NotBefore: time.Now().Add(-time.Second), | ||
NotAfter: time.Now().Add(time.Hour), | ||
IsCA: true, | ||
KeyUsage: x509.KeyUsageCertSign | x509.KeyUsageCRLSign, | ||
BasicConstraintsValid: true, | ||
} | ||
|
||
certDER, err := x509.CreateCertificate(rand.Reader, cert, parent, key, parentKey) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
cert, err = x509.ParseCertificate(certDER) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return cert, nil | ||
} | ||
|
||
// GenerateTrustCert generates a new trust certificate for testing. Unlike the | ||
// intermediate certificates, this certificate should be used for signature | ||
// only, not creating certificates. | ||
func GenerateTrustCert(key crypto.PublicKey, parentKey crypto.PrivateKey, parent *x509.Certificate) (*x509.Certificate, error) { | ||
cert := &x509.Certificate{ | ||
SerialNumber: big.NewInt(0), | ||
Subject: pkix.Name{ | ||
CommonName: "Trust Cert", | ||
}, | ||
NotBefore: time.Now().Add(-time.Second), | ||
NotAfter: time.Now().Add(time.Hour), | ||
IsCA: true, | ||
KeyUsage: x509.KeyUsageDigitalSignature, | ||
BasicConstraintsValid: true, | ||
} | ||
|
||
certDER, err := x509.CreateCertificate(rand.Reader, cert, parent, key, parentKey) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
cert, err = x509.ParseCertificate(certDER) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return cert, nil | ||
} |