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

refector & test: update to support new signature.Signer interface #52

Merged
merged 2 commits into from
Aug 26, 2022
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
12 changes: 12 additions & 0 deletions signature/algorithm.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,18 @@ const (
KeyTypeEC // KeyType EC
)

// String is the stringer function for KeyType
func (keyType KeyType) String() string {
switch keyType {
case KeyTypeRSA:
return "RSA"
case KeyTypeEC:
return "ECDSA"
default:
return fmt.Sprintf("unknown key type: %d", keyType)
}
}

// KeySpec defines a key type and size.
type KeySpec struct {
Type KeyType
Expand Down
25 changes: 25 additions & 0 deletions signature/algorithm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,3 +229,28 @@ func TestSignatureAlgorithm(t *testing.T) {
})
}
}

func TestKeyTypeStringer(t *testing.T) {
testCase := []struct {
keyType KeyType
str string
}{
{
keyType: KeyTypeEC,
str: "ECDSA",
},
{
keyType: KeyTypeRSA,
str: "RSA",
},
{
keyType: 10,
str: "unknown key type: 10",
},
}
for _, tt := range testCase {
if tt.keyType.String() != tt.str {
t.Fatalf("keyType: %s stringer test failed", tt.keyType)
}
}
}
78 changes: 9 additions & 69 deletions signature/cose/envelope_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ package cose

import (
"crypto"
"crypto/elliptic"
"crypto/x509"
"errors"
"fmt"
"testing"
"time"

"github.com/notaryproject/notation-core-go/signature"
"github.com/notaryproject/notation-core-go/signature/signaturetest"
"github.com/notaryproject/notation-core-go/testhelper"
"github.com/veraison/go-cose"
)
Expand All @@ -20,7 +20,6 @@ const (

var (
signingSchemeString = []string{"notary.x509", "notary.x509.signingAuthority"}
keyType = []signature.KeyType{signature.KeyTypeRSA, signature.KeyTypeEC}
)

func TestParseEnvelopeError(t *testing.T) {
Expand All @@ -39,14 +38,8 @@ func TestParseEnvelopeError(t *testing.T) {
func TestSign(t *testing.T) {
env := envelope{}
for _, signingScheme := range signingSchemeString {
for _, keyType := range keyType {
var size []int
if keyType == signature.KeyTypeRSA {
size = []int{2048, 3072, 4096}
} else {
size = []int{256, 384, 521}
}
for _, size := range size {
for _, keyType := range signaturetest.KeyTypes {
for _, size := range signaturetest.GetKeySizes(keyType) {
t.Run(fmt.Sprintf("with %s scheme, %v keyType, %v keySize when all arguments are present", signingScheme, keyType, size), func(t *testing.T) {
signRequest, err := newSignRequest(signingScheme, keyType, size)
if err != nil {
Expand All @@ -59,7 +52,7 @@ func TestSign(t *testing.T) {
})

t.Run(fmt.Sprintf("with %s scheme, %v keyType, %v keySize when minimal arguments are present", signingScheme, keyType, size), func(t *testing.T) {
signer, err := getTestSigner(keyType, size)
signer, err := signaturetest.GetTestLocalSigner(keyType, size)
if err != nil {
t.Fatalf("Sign() failed. Error = %s", err)
}
Expand Down Expand Up @@ -654,14 +647,8 @@ func TestSignerInfoErrors(t *testing.T) {
func TestSignAndVerify(t *testing.T) {
env := envelope{}
for _, signingScheme := range signingSchemeString {
for _, keyType := range keyType {
var size []int
if keyType == signature.KeyTypeRSA {
size = []int{2048, 3072, 4096}
} else {
size = []int{256, 384, 521}
}
for _, size := range size {
for _, keyType := range signaturetest.KeyTypes {
for _, size := range signaturetest.GetKeySizes(keyType) {
t.Run(fmt.Sprintf("with %s scheme, %v keyType, %v keySize", signingScheme, keyType, size), func(t *testing.T) {
// Sign
signRequest, err := newSignRequest(signingScheme, keyType, size)
Expand All @@ -686,14 +673,8 @@ func TestSignAndVerify(t *testing.T) {

func TestSignAndParseVerify(t *testing.T) {
for _, signingScheme := range signingSchemeString {
for _, keyType := range keyType {
var size []int
if keyType == signature.KeyTypeRSA {
size = []int{2048, 3072, 4096}
} else {
size = []int{256, 384, 521}
}
for _, size := range size {
for _, keyType := range signaturetest.KeyTypes {
for _, size := range signaturetest.GetKeySizes(keyType) {
t.Run(fmt.Sprintf("with %s scheme, %v keyType, %v keySize", signingScheme, keyType, size), func(t *testing.T) {
//Verify after UnmarshalCBOR
env, err := getVerifyCOSE(signingScheme, keyType, size)
Expand All @@ -712,7 +693,7 @@ func TestSignAndParseVerify(t *testing.T) {
}

func newSignRequest(signingScheme string, keyType signature.KeyType, size int) (*signature.SignRequest, error) {
signer, err := getTestSigner(keyType, size)
signer, err := signaturetest.GetTestLocalSigner(keyType, size)
if err != nil {
return nil, err
}
Expand All @@ -733,47 +714,6 @@ func newSignRequest(signingScheme string, keyType signature.KeyType, size int) (
}, nil
}

func getTestSigner(keyType signature.KeyType, size int) (signature.Signer, error) {
switch keyType {
case signature.KeyTypeEC:
switch size {
case 256:
leafCertTuple := testhelper.GetECCertTuple(elliptic.P256())
certs := []*x509.Certificate{leafCertTuple.Cert, testhelper.GetECRootCertificate().Cert}
return signature.NewLocalSigner(certs, leafCertTuple.PrivateKey)
case 384:
leafCertTuple := testhelper.GetECCertTuple(elliptic.P384())
certs := []*x509.Certificate{leafCertTuple.Cert, testhelper.GetECRootCertificate().Cert}
return signature.NewLocalSigner(certs, leafCertTuple.PrivateKey)
case 521:
leafCertTuple := testhelper.GetECCertTuple(elliptic.P521())
certs := []*x509.Certificate{leafCertTuple.Cert, testhelper.GetECRootCertificate().Cert}
return signature.NewLocalSigner(certs, leafCertTuple.PrivateKey)
default:
return nil, fmt.Errorf("key size not supported")
}
case signature.KeyTypeRSA:
switch size {
case 2048:
leafCertTuple := testhelper.GetRSACertTuple(2048)
certs := []*x509.Certificate{leafCertTuple.Cert, testhelper.GetRSARootCertificate().Cert}
return signature.NewLocalSigner(certs, leafCertTuple.PrivateKey)
case 3072:
leafCertTuple := testhelper.GetRSACertTuple(3072)
certs := []*x509.Certificate{leafCertTuple.Cert, testhelper.GetRSARootCertificate().Cert}
return signature.NewLocalSigner(certs, leafCertTuple.PrivateKey)
case 4096:
leafCertTuple := testhelper.GetRSACertTuple(4096)
certs := []*x509.Certificate{leafCertTuple.Cert, testhelper.GetRSARootCertificate().Cert}
return signature.NewLocalSigner(certs, leafCertTuple.PrivateKey)
default:
return nil, fmt.Errorf("key size not supported")
}
default:
return nil, fmt.Errorf("keyType not supported")
}
}

func getSignRequest() (*signature.SignRequest, error) {
return newSignRequest("notary.x509", signature.KeyTypeRSA, 3072)
}
Expand Down
12 changes: 11 additions & 1 deletion signature/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,5 +122,15 @@ type EnvelopeKeyRepeatedError struct {

// Error returns the formatted error message.
func (e *EnvelopeKeyRepeatedError) Error() string {
return fmt.Sprintf("repeated key: %q exists in the envelope.", e.Key)
return fmt.Sprintf(`repeated key: "%s" exists in the both protected header and extended signed attributes.`, e.Key)
}

// RemoteSigningError is used when remote signer causes the error.
type RemoteSigningError struct {
Msg string
}

// Error returns formated remote signing error
func (e *RemoteSigningError) Error() string {
return fmt.Sprintf("remote signing error. Error: %s", e.Msg)
}
13 changes: 11 additions & 2 deletions signature/errors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,9 +194,18 @@ func TestUnsupportedSignatureFormatError(t *testing.T) {

func TestEnvelopeKeyRepeatedError(t *testing.T) {
err := &EnvelopeKeyRepeatedError{Key: errMsg}
expectMsg := fmt.Sprintf("repeated key: %q exists in the envelope.", errMsg)
expectMsg := fmt.Sprintf(`repeated key: "%s" exists in the both protected header and extended signed attributes.`, errMsg)

if err.Error() != expectMsg {
t.Errorf("Expected %v but got %v", expectMsg, err.Error())
}
}
}

func TestRemoteSigningError(t *testing.T) {
err := &RemoteSigningError{Msg: errMsg}
expectMsg := fmt.Sprintf("remote signing error. Error: %s", errMsg)

if err.Error() != expectMsg {
t.Errorf("Expected %v but got %v", expectMsg, err.Error())
}
}
2 changes: 1 addition & 1 deletion signature/internal/base/envelope.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ func validateSignerInfo(info *signature.SignerInfo) error {
)
}

// validateSigningTime checks that sigining time is within the valid range of
// validateSigningTime checks that signing time is within the valid range of
// time duration.
func validateSigningTime(signingTime, expireTime time.Time) error {
if signingTime.IsZero() {
Expand Down
Loading