Skip to content

Commit

Permalink
Fix two bugs in the pivkey code related to cleanup and certs. (#912)
Browse files Browse the repository at this point in the history
The card management was refactored a bit so we can't just defer a close on
the key itself, we need to move this up to the Signer.

There was also an errant break statement causing a fallback in our switch.

Signed-off-by: Dan Lorenc <lorenc.d@gmail.com>
  • Loading branch information
dlorenc committed Oct 18, 2021
1 parent 699fab4 commit 2594f7a
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 6 deletions.
1 change: 1 addition & 0 deletions cmd/cosign/cli/attest/attest.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ func AttestCmd(ctx context.Context, ko sign.KeyOpts, regOpts options.RegistryOpt
if err != nil {
return errors.Wrap(err, "getting signer")
}
defer sv.Close()
wrapped := dsse.WrapSigner(sv, predicateURI)
dd := cremote.NewDupeDetector(sv)

Expand Down
2 changes: 2 additions & 0 deletions cmd/cosign/cli/policy_init.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,8 @@ func signPolicy() *cobra.Command {
if err != nil {
return err
}
defer sv.Close()

certs, err := cryptoutils.LoadCertificatesFromPEM(bytes.NewReader(sv.Cert))
if err != nil {
return err
Expand Down
22 changes: 16 additions & 6 deletions cmd/cosign/cli/sign/sign.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ func SignCmd(ctx context.Context, ko KeyOpts, regOpts options.RegistryOptions, a
if err != nil {
return errors.Wrap(err, "getting signer")
}
defer sv.Close()
dd := cremote.NewDupeDetector(sv)

var staticPayload []byte
Expand Down Expand Up @@ -295,7 +296,6 @@ func SignerFromKeyOpts(ctx context.Context, certPath string, ko KeyOpts) (*CertS
switch {
case ko.Sk:
sk, err := pivkey.GetKeyWithSlot(ko.Slot)
defer sk.Close()
if err != nil {
return nil, err
}
Expand All @@ -309,17 +309,20 @@ func SignerFromKeyOpts(ctx context.Context, certPath string, ko KeyOpts) (*CertS
// token as the private key. If it's not there, show a warning to the
// user.
certFromPIV, err := sk.Certificate()
var pemBytes []byte
if err != nil {
fmt.Fprintln(os.Stderr, "warning: no x509 certificate retrieved from the PIV token")
break
}
pemBytes, err := cryptoutils.MarshalCertificateToPEM(certFromPIV)
if err != nil {
return nil, err
} else {
pemBytes, err = cryptoutils.MarshalCertificateToPEM(certFromPIV)
if err != nil {
return nil, err
}
}

return &CertSignVerifier{
Cert: pemBytes,
SignerVerifier: sv,
close: sk.Close,
}, nil

case ko.KeyRef != "":
Expand Down Expand Up @@ -404,4 +407,11 @@ type CertSignVerifier struct {
Cert []byte
Chain []byte
signature.SignerVerifier
close func()
}

func (c *CertSignVerifier) Close() {
if c.close != nil {
c.close()
}
}
1 change: 1 addition & 0 deletions cmd/cosign/cli/sign/sign_blob.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ func SignBlobCmd(ctx context.Context, ko KeyOpts, regOpts options.RegistryOption
if err != nil {
return nil, err
}
defer sv.Close()

sig, err := sv.SignMessage(bytes.NewReader(payload), signatureoptions.WithContext(ctx))
if err != nil {
Expand Down

0 comments on commit 2594f7a

Please sign in to comment.