Move signAndWriteCert from main.go into cert.go
diff --git a/cmd/ceremony/cert.go b/cmd/ceremony/cert.go index dfa8c93..9ee2449 100644 --- a/cmd/ceremony/cert.go +++ b/cmd/ceremony/cert.go
@@ -6,9 +6,11 @@ "crypto/x509" "crypto/x509/pkix" "encoding/asn1" + "encoding/pem" "errors" "fmt" "io" + "log" "math/big" "slices" "strconv" @@ -368,3 +370,41 @@ } return csrDER, nil } + +func signAndWriteCert(tbs, issuer *x509.Certificate, lintCert lintCert, subjectPubKey crypto.PublicKey, signer crypto.Signer, certPath string) (*x509.Certificate, error) { + if lintCert == nil { + return nil, fmt.Errorf("linting was not performed prior to issuance") + } + // x509.CreateCertificate uses a io.Reader here for signing methods that require + // a source of randomness. Since PKCS#11 based signing generates needed randomness + // at the HSM we don't need to pass a real reader. Instead of passing a nil reader + // we use one that always returns errors in case the internal usage of this reader + // changes. + certBytes, err := x509.CreateCertificate(&failReader{}, tbs, issuer, subjectPubKey, signer) + if err != nil { + return nil, fmt.Errorf("failed to create certificate: %s", err) + } + pemBytes := pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: certBytes}) + log.Printf("Signed certificate PEM:\n%s", pemBytes) + cert, err := x509.ParseCertificate(certBytes) + if err != nil { + return nil, fmt.Errorf("failed to parse signed certificate: %s", err) + } + if tbs == issuer { + // If cert is self-signed we need to populate the issuer subject key to + // verify the signature + issuer.PublicKey = cert.PublicKey + issuer.PublicKeyAlgorithm = cert.PublicKeyAlgorithm + } + err = cert.CheckSignatureFrom(issuer) + if err != nil { + return nil, fmt.Errorf("failed to verify certificate signature: %s", err) + } + err = writeFile(certPath, pemBytes) + if err != nil { + return nil, fmt.Errorf("failed to write certificate to %q: %s", certPath, err) + } + log.Printf("Certificate written to %q\n", certPath) + + return cert, nil +}
diff --git a/cmd/ceremony/cert_test.go b/cmd/ceremony/cert_test.go index 4f161fc..a63155e 100644 --- a/cmd/ceremony/cert_test.go +++ b/cmd/ceremony/cert_test.go
@@ -562,3 +562,9 @@ test.AssertEquals(t, len(sha256skid), 20) test.AssertEquals(t, cap(sha256skid), 20) } + +func TestSignAndWriteNoLintCert(t *testing.T) { + _, err := signAndWriteCert(nil, nil, nil, nil, nil, "") + test.AssertError(t, err, "should have failed because no lintCert was provided") + test.AssertDeepEquals(t, err, fmt.Errorf("linting was not performed prior to issuance")) +}
diff --git a/cmd/ceremony/main.go b/cmd/ceremony/main.go index 604a06a..f3cce2a 100644 --- a/cmd/ceremony/main.go +++ b/cmd/ceremony/main.go
@@ -496,44 +496,6 @@ return signer, newRandReader(session), nil } -func signAndWriteCert(tbs, issuer *x509.Certificate, lintCert lintCert, subjectPubKey crypto.PublicKey, signer crypto.Signer, certPath string) (*x509.Certificate, error) { - if lintCert == nil { - return nil, fmt.Errorf("linting was not performed prior to issuance") - } - // x509.CreateCertificate uses a io.Reader here for signing methods that require - // a source of randomness. Since PKCS#11 based signing generates needed randomness - // at the HSM we don't need to pass a real reader. Instead of passing a nil reader - // we use one that always returns errors in case the internal usage of this reader - // changes. - certBytes, err := x509.CreateCertificate(&failReader{}, tbs, issuer, subjectPubKey, signer) - if err != nil { - return nil, fmt.Errorf("failed to create certificate: %s", err) - } - pemBytes := pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: certBytes}) - log.Printf("Signed certificate PEM:\n%s", pemBytes) - cert, err := x509.ParseCertificate(certBytes) - if err != nil { - return nil, fmt.Errorf("failed to parse signed certificate: %s", err) - } - if tbs == issuer { - // If cert is self-signed we need to populate the issuer subject key to - // verify the signature - issuer.PublicKey = cert.PublicKey - issuer.PublicKeyAlgorithm = cert.PublicKeyAlgorithm - } - err = cert.CheckSignatureFrom(issuer) - if err != nil { - return nil, fmt.Errorf("failed to verify certificate signature: %s", err) - } - err = writeFile(certPath, pemBytes) - if err != nil { - return nil, fmt.Errorf("failed to write certificate to %q: %s", certPath, err) - } - log.Printf("Certificate written to %q\n", certPath) - - return cert, nil -} - // loadPubKey loads a PEM public key specified by filename. It returns a // crypto.PublicKey, the PEM bytes of the public key, and an error. If an error // exists, no public key or bytes are returned. The public key is checked by the
diff --git a/cmd/ceremony/main_test.go b/cmd/ceremony/main_test.go index a275f0c..af61433 100644 --- a/cmd/ceremony/main_test.go +++ b/cmd/ceremony/main_test.go
@@ -6,7 +6,6 @@ "crypto/rand" "crypto/x509" "encoding/pem" - "fmt" "io/fs" "math/big" "os" @@ -1206,12 +1205,6 @@ } } -func TestSignAndWriteNoLintCert(t *testing.T) { - _, err := signAndWriteCert(nil, nil, nil, nil, nil, "") - test.AssertError(t, err, "should have failed because no lintCert was provided") - test.AssertDeepEquals(t, err, fmt.Errorf("linting was not performed prior to issuance")) -} - func TestPostIssuanceLinting(t *testing.T) { clk := clock.New() err := postIssuanceLinting(nil, nil)