Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 43 additions & 15 deletions crypto/spiffe/signer/signer.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,22 +51,9 @@ func New(svidSource x509svid.Source, trustAnchors trustanchors.Interface) *Signe
// Returns the signature bytes and the DER-encoded certificate chain (leaf +
// intermediates concatenated).
func (s *Signer) Sign(digest []byte) ([]byte, []byte, error) {
if s.svidSource == nil {
return nil, nil, errors.New("signing not available: no SVID source configured")
}

svid, err := s.svidSource.GetX509SVID()
svid, certChainDER, err := s.currentSVIDChain("signing")
if err != nil {
return nil, nil, fmt.Errorf("failed to get X.509 SVID: %w", err)
}

if len(svid.Certificates) == 0 {
return nil, nil, errors.New("SVID has no certificates")
}

var certChainDER []byte
for _, cert := range svid.Certificates {
certChainDER = append(certChainDER, cert.Raw...)
return nil, nil, err
}

sig, err := signWithKey(svid.PrivateKey, digest)
Expand All @@ -77,6 +64,20 @@ func (s *Signer) Sign(digest []byte) ([]byte, []byte, error) {
return sig, certChainDER, nil
}

// CertChainDER returns the DER-encoded certificate chain (leaf +
// intermediates concatenated) of the current SVID, without performing a
// signing operation. Callers that need to reference their own certificate
// before signing (e.g. to commit a cert digest into a signed payload)
// should use this rather than signing a throwaway digest.
func (s *Signer) CertChainDER() ([]byte, error) {
_, certChainDER, err := s.currentSVIDChain("certificate chain")
if err != nil {
return nil, err
}

return certChainDER, nil
}
Comment thread
JoshVanL marked this conversation as resolved.
Resolved

// VerifySignature verifies a cryptographic signature against the given digest
// using the public key from the provided DER-encoded certificate chain. This
// only checks the cryptographic signature; use VerifyCertChainOfTrust to
Expand Down Expand Up @@ -149,6 +150,33 @@ func (s *Signer) VerifyCertChainOfTrust(certChainDER []byte, signingTime time.Ti
return nil
}

// currentSVIDChain fetches the current SVID and returns it alongside the
// concatenated DER-encoded certificate chain (leaf first, intermediates
// concatenated). The op label is interpolated into the "<op> not
// available" error so callers see a context-appropriate message when the
// signer was configured without an SVID source.
func (s *Signer) currentSVIDChain(op string) (*x509svid.SVID, []byte, error) {
if s.svidSource == nil {
return nil, nil, fmt.Errorf("%s not available: no SVID source configured", op)
}

svid, err := s.svidSource.GetX509SVID()
if err != nil {
return nil, nil, fmt.Errorf("failed to get X.509 SVID: %w", err)
}

if len(svid.Certificates) == 0 {
return nil, nil, errors.New("SVID has no certificates")
}

var certChainDER []byte
for _, cert := range svid.Certificates {
certChainDER = append(certChainDER, cert.Raw...)
}

return svid, certChainDER, nil
}

// signWithKey signs the given digest with the private key.
func signWithKey(key crypto.Signer, digest []byte) ([]byte, error) {
switch k := key.(type) {
Expand Down
53 changes: 53 additions & 0 deletions crypto/spiffe/signer/signer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,59 @@ func TestSign_ReturnsCertChainDER(t *testing.T) {
assert.Equal(t, certDER, certChain)
}

func TestCertChainDER_NilSVIDSource(t *testing.T) {
t.Parallel()

s := New(nil, fake.New())
_, err := s.CertChainDER()
require.Error(t, err)
assert.Contains(t, err.Error(), "certificate chain not available")
assert.Contains(t, err.Error(), "no SVID source configured")
}

func TestCertChainDER_SVIDSourceError(t *testing.T) {
t.Parallel()

source := &staticSVIDSource{err: errors.New("svid unavailable")}
s := New(source, nil)
_, err := s.CertChainDER()
require.Error(t, err)
assert.Contains(t, err.Error(), "svid unavailable")
}

func TestCertChainDER_NoCertificates(t *testing.T) {
t.Parallel()

source := &staticSVIDSource{svid: &x509svid.SVID{
Certificates: nil,
PrivateKey: ed25519.NewKeyFromSeed(make([]byte, 32)),
}}
s := New(source, nil)
_, err := s.CertChainDER()
require.Error(t, err)
assert.Contains(t, err.Error(), "no certificates")
}

func TestCertChainDER_MatchesSignChain(t *testing.T) {
t.Parallel()
_, cert, priv := generateEd25519Cert(t)
s := New(newSVIDSource(cert, priv), nil)

signChain := func(t *testing.T) []byte {
t.Helper()

_, chain, err := s.Sign(testDigest("any"))
require.NoError(t, err)

return chain
}

got, err := s.CertChainDER()
require.NoError(t, err)
assert.Equal(t, signChain(t), got,
"CertChainDER must produce the same chain bytes Sign returns")
}

func TestVerifyCertChainOfTrust_SelfSigned(t *testing.T) {
t.Parallel()
certDER, cert, _ := generateEd25519Cert(t)
Expand Down
Loading