Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add signerverifiers for RSAPSS, ECDSA, ED25519 #43

Merged
merged 8 commits into from
May 2, 2023
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
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ jobs:
test:
strategy:
matrix:
go-version: [1.17.x, 1.18.x, 1.19.x, 1.20.x]
go-version: [1.18.x, 1.19.x, 1.20.x]
os: [ubuntu-latest, macos-latest, windows-latest]
runs-on: ${{ matrix.os }}
steps:
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/secure-systems-lab/go-securesystemslib

go 1.17
go 1.20

require (
github.com/codahale/rfc6979 v0.0.0-20141003034818-6a90f24967eb
Expand Down
111 changes: 111 additions & 0 deletions signerverifier/ecdsa.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
package signerverifier

import (
"context"
"crypto"
"crypto/ecdsa"
"crypto/rand"
"crypto/sha256"
"crypto/sha512"
"fmt"
"os"
)

const ECDSAKeyType = "ecdsa"

// ECDSASignerVerifier is a dsse.SignerVerifier compliant interface to sign and
// verify signatures using ECDSA keys.
type ECDSASignerVerifier struct {
keyID string
curveSize int
private *ecdsa.PrivateKey
public *ecdsa.PublicKey
}

// NewECDSASignerVerifierFromSSLibKey creates an ECDSASignerVerifier from an
// SSLibKey.
func NewECDSASignerVerifierFromSSLibKey(key *SSLibKey) (*ECDSASignerVerifier, error) {
if len(key.KeyVal.Public) == 0 {
return nil, ErrInvalidKey
}

_, publicParsedKey, err := decodeAndParsePEM([]byte(key.KeyVal.Public))
if err != nil {
return nil, fmt.Errorf("unable to create ECDSA signerverifier: %w", err)
}

sv := &ECDSASignerVerifier{
keyID: key.KeyID,
curveSize: publicParsedKey.(*ecdsa.PublicKey).Params().BitSize,
public: publicParsedKey.(*ecdsa.PublicKey),
private: nil,
}

if len(key.KeyVal.Private) > 0 {
_, privateParsedKey, err := decodeAndParsePEM([]byte(key.KeyVal.Private))
if err != nil {
return nil, fmt.Errorf("unable to create ECDSA signerverifier: %w", err)
}

sv.private = privateParsedKey.(*ecdsa.PrivateKey)
}

return sv, nil
}

// Sign creates a signature for `data`.
func (sv *ECDSASignerVerifier) Sign(ctx context.Context, data []byte) ([]byte, error) {
if sv.private == nil {
return nil, ErrNotPrivateKey
}

hashedData := getECDSAHashedData(data, sv.curveSize)

return ecdsa.SignASN1(rand.Reader, sv.private, hashedData)
}

// Verify verifies the `sig` value passed in against `data`.
func (sv *ECDSASignerVerifier) Verify(ctx context.Context, data []byte, sig []byte) error {
hashedData := getECDSAHashedData(data, sv.curveSize)

if ok := ecdsa.VerifyASN1(sv.public, hashedData, sig); !ok {
return ErrSignatureVerificationFailed
}

return nil
}

// KeyID returns the identifier of the key used to create the
// ECDSASignerVerifier instance.
func (sv *ECDSASignerVerifier) KeyID() (string, error) {
return sv.keyID, nil
}

// Public returns the public portion of the key used to create the
// ECDSASignerVerifier instance.
func (sv *ECDSASignerVerifier) Public() crypto.PublicKey {
return sv.public
}

// LoadECDSAKeyFromFile returns an SSLibKey instance for an ECDSA key stored in
// a file in the custom securesystemslib format.
func LoadECDSAKeyFromFile(path string) (*SSLibKey, error) {
contents, err := os.ReadFile(path)
if err != nil {
return nil, fmt.Errorf("unable to load ECDSA key from file: %w", err)
}

return loadKeyFromSSLibBytes(contents)
}

func getECDSAHashedData(data []byte, curveSize int) []byte {
switch {
case curveSize <= 256:
return hashBeforeSigning(data, sha256.New())
case 256 < curveSize && curveSize <= 384:
return hashBeforeSigning(data, sha512.New384())
case curveSize > 384:
return hashBeforeSigning(data, sha512.New())
}
return []byte{}
}
191 changes: 191 additions & 0 deletions signerverifier/ecdsa_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,191 @@
package signerverifier

import (
"context"
"encoding/json"
"os"
"path/filepath"
"testing"

"github.com/secure-systems-lab/go-securesystemslib/cjson"
"github.com/secure-systems-lab/go-securesystemslib/dsse"
"github.com/stretchr/testify/assert"
)

func TestNewECDSASignerVerifierFromSSLibKey(t *testing.T) {
key, err := LoadECDSAKeyFromFile(filepath.Join("test-data", "ecdsa-test-key.pub"))
if err != nil {
t.Fatal(err)
}

sv, err := NewECDSASignerVerifierFromSSLibKey(key)
if err != nil {
t.Fatal(err)
}

expectedPublicString := "-----BEGIN PUBLIC KEY-----\nMFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEu+HEqqpXLa48lXH9rkRygsfsCKq1\nXM36oXymJ9wxpM68nCqkrZCVnZ9lkEeCwD8qWYTNxD5yfWXwJjFh+K7qLQ==\n-----END PUBLIC KEY-----"
_, expectedPublicKey, err := decodeAndParsePEM([]byte(expectedPublicString))
assert.Nil(t, err)

assert.Equal(t, "98adf38602c48c5479e9a991ee3f8cbf541ee4f985e00f7a5fc4148d9a45b704", sv.keyID)
assert.Equal(t, expectedPublicKey, sv.public)
assert.Nil(t, sv.private)
}

func TestLoadECDSAKeyFromFile(t *testing.T) {
t.Run("ecdsa public key", func(t *testing.T) {
key, err := LoadECDSAKeyFromFile(filepath.Join("test-data", "ecdsa-test-key.pub"))
assert.Nil(t, err)

assert.Equal(t, "98adf38602c48c5479e9a991ee3f8cbf541ee4f985e00f7a5fc4148d9a45b704", key.KeyID)
assert.Equal(t, "-----BEGIN PUBLIC KEY-----\nMFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEu+HEqqpXLa48lXH9rkRygsfsCKq1\nXM36oXymJ9wxpM68nCqkrZCVnZ9lkEeCwD8qWYTNxD5yfWXwJjFh+K7qLQ==\n-----END PUBLIC KEY-----", key.KeyVal.Public)
assert.Equal(t, "ecdsa-sha2-nistp256", key.Scheme)
assert.Equal(t, ECDSAKeyType, key.KeyType)
})

t.Run("ecdsa private key", func(t *testing.T) {
key, err := LoadECDSAKeyFromFile(filepath.Join("test-data", "ecdsa-test-key"))
assert.Nil(t, err)

assert.Equal(t, "98adf38602c48c5479e9a991ee3f8cbf541ee4f985e00f7a5fc4148d9a45b704", key.KeyID)
assert.Equal(t, "-----BEGIN PUBLIC KEY-----\nMFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEu+HEqqpXLa48lXH9rkRygsfsCKq1\nXM36oXymJ9wxpM68nCqkrZCVnZ9lkEeCwD8qWYTNxD5yfWXwJjFh+K7qLQ==\n-----END PUBLIC KEY-----", key.KeyVal.Public)
assert.Equal(t, "-----BEGIN EC PRIVATE KEY-----\nMHcCAQEEIAo6DxXlgqYy+TkvocIOyWlqA3KVtp6dlSY7lS3kkeEMoAoGCCqGSM49\nAwEHoUQDQgAEu+HEqqpXLa48lXH9rkRygsfsCKq1XM36oXymJ9wxpM68nCqkrZCV\nnZ9lkEeCwD8qWYTNxD5yfWXwJjFh+K7qLQ==\n-----END EC PRIVATE KEY-----", key.KeyVal.Private)
assert.Equal(t, "ecdsa-sha2-nistp256", key.Scheme)
assert.Equal(t, ECDSAKeyType, key.KeyType)
})

t.Run("invalid path", func(t *testing.T) {
_, err := LoadECDSAKeyFromFile(filepath.Join("test-data", "invalid"))
assert.ErrorContains(t, err, "unable to load ECDSA key from file")
})
}

func TestECDSASignerVerifierSign(t *testing.T) {
t.Run("using valid key", func(t *testing.T) {
key, err := LoadECDSAKeyFromFile(filepath.Join("test-data", "ecdsa-test-key"))
if err != nil {
t.Fatal(err)
}

sv, err := NewECDSASignerVerifierFromSSLibKey(key)
if err != nil {
t.Fatal(err)
}

message := []byte("test message")

signature, err := sv.Sign(context.Background(), message)
assert.Nil(t, err)

err = sv.Verify(context.Background(), message, signature)
assert.Nil(t, err)
})

t.Run("using invalid key", func(t *testing.T) {
key, err := LoadECDSAKeyFromFile(filepath.Join("test-data", "ecdsa-test-key.pub"))
if err != nil {
t.Fatal(err)
}

sv, err := NewECDSASignerVerifierFromSSLibKey(key)
if err != nil {
t.Fatal(err)
}

message := []byte("test message")

_, err = sv.Sign(context.Background(), message)
assert.ErrorIs(t, err, ErrNotPrivateKey)
})
}

func TestECDSASignerVerifierWithDSSEEnvelope(t *testing.T) {
key, err := LoadECDSAKeyFromFile(filepath.Join("test-data", "ecdsa-test-key"))
if err != nil {
t.Fatal(err)
}

sv, err := NewECDSASignerVerifierFromSSLibKey(key)
if err != nil {
t.Fatal(err)
}

payloadType := "application/vnd.dsse+json"
payload := []byte("test message")

es, err := dsse.NewEnvelopeSigner(sv)
if err != nil {
t.Error(err)
}

env, err := es.SignPayload(context.Background(), payloadType, payload)
if err != nil {
t.Error(err)
}

assert.Equal(t, "98adf38602c48c5479e9a991ee3f8cbf541ee4f985e00f7a5fc4148d9a45b704", env.Signatures[0].KeyID)
envPayload, err := env.DecodeB64Payload()
assert.Equal(t, payload, envPayload)
assert.Nil(t, err)

key, err = LoadECDSAKeyFromFile(filepath.Join("test-data", "ecdsa-test-key.pub"))
if err != nil {
t.Fatal(err)
}

sv, err = NewECDSASignerVerifierFromSSLibKey(key)
if err != nil {
t.Fatal(err)
}

ev, err := dsse.NewEnvelopeVerifier(sv)
if err != nil {
t.Error(err)
}

acceptedKeys, err := ev.Verify(context.Background(), env)
assert.Nil(t, err)
assert.Equal(t, "98adf38602c48c5479e9a991ee3f8cbf541ee4f985e00f7a5fc4148d9a45b704", acceptedKeys[0].KeyID)
}

func TestECDSASignerVerifierWithMetablockFile(t *testing.T) {
key, err := LoadECDSAKeyFromFile(filepath.Join("test-data", "ecdsa-test-key.pub"))
if err != nil {
t.Fatal(err)
}

sv, err := NewECDSASignerVerifierFromSSLibKey(key)
if err != nil {
t.Fatal(err)
}

metadataBytes, err := os.ReadFile(filepath.Join("test-data", "test-ecdsa.98adf386.link"))
if err != nil {
t.Fatal(err)
}

mb := struct {
Signatures []struct {
KeyID string `json:"keyid"`
Sig string `json:"sig"`
} `json:"signatures"`
Signed any `json:"signed"`
}{}

if err := json.Unmarshal(metadataBytes, &mb); err != nil {
t.Fatal(err)
}

assert.Equal(t, "304502201fbb03c0937504182a48c66f9218bdcb2e99a07ada273e92e5e543867f98c8d7022100dbfa7bbf74fd76d76c1d08676419cba85bbd81dfb000f3ac6a786693ddc508f5", mb.Signatures[0].Sig)
assert.Equal(t, sv.keyID, mb.Signatures[0].KeyID)

encodedBytes, err := cjson.EncodeCanonical(mb.Signed)
if err != nil {
t.Fatal(err)
}

decodedSig := hexDecode(t, mb.Signatures[0].Sig)

err = sv.Verify(context.Background(), encodedBytes, decodedSig)
assert.Nil(t, err)
}
Loading