Skip to content

Commit

Permalink
Add chain in response for all CAs, fix newlines in response (#341)
Browse files Browse the repository at this point in the history
The certificate's chain was not being included when issuing
certificates for non-GCP CAs.

We were also adding too many newlines between PEM-encoded
certificates. pem.Encode automatically adds newlines. Just
in case the source that's providing the certificates is
not including trailing newlines, I've added a check to
optionally append newlines.

Signed-off-by: Hayden Blauzvern <hblauzvern@google.com>
  • Loading branch information
haydentherapper authored Jan 22, 2022
1 parent 50b605d commit 10262db
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 10 deletions.
4 changes: 2 additions & 2 deletions pkg/api/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,8 +242,8 @@ func fakeCTLogServer(t *testing.T) *httptest.Server {
}
var chain certChain
json.Unmarshal(body, &chain)
if len(chain.Chain) != 1 {
t.Fatalf("Did not get expected chain for input, wanted 1 entry, got %d", len(chain.Chain))
if len(chain.Chain) != 2 {
t.Fatalf("Did not get expected chain for input, wanted 2 entries, got %d", len(chain.Chain))
}
// Just make sure we can decode it.
for _, chainEntry := range chain.Chain {
Expand Down
11 changes: 9 additions & 2 deletions pkg/api/ca.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,14 +196,21 @@ func signingCert(w http.ResponseWriter, req *http.Request) {
handleFulcioAPIError(w, req, http.StatusInternalServerError, err, failedToMarshalCert)
return
}
fmt.Fprintf(&ret, "%s\n", finalPEM)
fmt.Fprintf(&ret, "%s", finalPEM)
if finalPEM[len(finalPEM)-1] != '\n' {
fmt.Fprintf(&ret, "\n")
}

finalChainPEM, err := csc.ChainPEM()
if err != nil {
handleFulcioAPIError(w, req, http.StatusInternalServerError, err, failedToMarshalCert)
return
}
if len(finalChainPEM) > 0 {
fmt.Fprintf(&ret, "%s\n", finalChainPEM)
fmt.Fprintf(&ret, "%s", finalChainPEM)
if finalPEM[len(finalChainPEM)-1] != '\n' {
fmt.Fprintf(&ret, "\n")
}
}

// Set the SCT and Content-Type headers, and then respond with a 201 Created.
Expand Down
2 changes: 1 addition & 1 deletion pkg/ca/fileca/fileca.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ func (fca *fileCA) CreateCertificate(_ context.Context, subject *challenges.Chal
return nil, err
}

return ca.CreateCSCFromDER(subject, finalCertBytes, nil)
return ca.CreateCSCFromDER(subject, finalCertBytes, fca.certs)
}

func (fca *fileCA) Root(ctx context.Context) ([]byte, error) {
Expand Down
8 changes: 4 additions & 4 deletions pkg/ca/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func CreateCSCFromPEM(subject *challenges.ChallengeResult, cert string, chain []
return c, nil
}

func CreateCSCFromDER(subject *challenges.ChallengeResult, cert, chain []byte) (c *CodeSigningCertificate, err error) {
func CreateCSCFromDER(subject *challenges.ChallengeResult, cert []byte, chain []*x509.Certificate) (c *CodeSigningCertificate, err error) {
c = &CodeSigningCertificate{
Subject: subject,
}
Expand All @@ -76,14 +76,14 @@ func CreateCSCFromDER(subject *challenges.ChallengeResult, cert, chain []byte) (
}

// convert to X509 and store both formats
c.FinalChain, err = x509.ParseCertificates(chain)
c.FinalChain = chain
if err != nil {
return nil, err
}
buf := bytes.Buffer{}
for i, chainCert := range c.FinalChain {
for _, chainCert := range c.FinalChain {
buf.Write(cryptoutils.PEMEncode(cryptoutils.CertificatePEMType, chainCert.Raw))
if i != len(c.FinalChain) {
if chainCert.Raw[len(chainCert.Raw)-1] != '\n' {
buf.WriteRune('\n')
}
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/ca/x509ca/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ func (x *X509CA) CreateCertificate(_ context.Context, subject *challenges.Challe
return nil, err
}

return ca.CreateCSCFromDER(subject, finalCertBytes, nil)
return ca.CreateCSCFromDER(subject, finalCertBytes, []*x509.Certificate{x.RootCA})
}

func AdditionalExtensions(subject *challenges.ChallengeResult) []pkix.Extension {
Expand Down

0 comments on commit 10262db

Please sign in to comment.