Skip to content

Commit

Permalink
update PR
Browse files Browse the repository at this point in the history
Signed-off-by: Junjie Gao <junjiegao@microsoft.com>
  • Loading branch information
JeyJeyGao committed Aug 26, 2022
1 parent f199296 commit ac60e82
Show file tree
Hide file tree
Showing 8 changed files with 220 additions and 128 deletions.
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
16 changes: 16 additions & 0 deletions signature/algorithm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,3 +229,19 @@ func TestSignatureAlgorithm(t *testing.T) {
})
}
}

func TestKeyTypeStringer(t *testing.T) {
if KeyTypeEC.String() != "ECDSA" {
t.Fatal("KeyTypeEC stringer test failed")
}

if KeyTypeRSA.String() != "RSA" {
t.Fatal("KeyTypeRSA stringer test failed")
}

var keyType KeyType
keyType = 10
if keyType.String() != "unknown key type: 10" {
t.Fatal("KeyType stringer unknown key type test failed")
}
}
75 changes: 9 additions & 66 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,13 +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 _, keyType := range signaturetest.KeyTypes {
var size = signaturetest.GetKeySizes(keyType)
for _, size := range size {
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)
Expand All @@ -59,7 +53,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.GetLocalSigner(keyType, size)
if err != nil {
t.Fatalf("Sign() failed. Error = %s", err)
}
Expand Down Expand Up @@ -654,13 +648,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 _, keyType := range signaturetest.KeyTypes {
var size = signaturetest.GetKeySizes(keyType)
for _, size := range size {
t.Run(fmt.Sprintf("with %s scheme, %v keyType, %v keySize", signingScheme, keyType, size), func(t *testing.T) {
// Sign
Expand All @@ -686,13 +675,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 _, keyType := range signaturetest.KeyTypes {
var size = signaturetest.GetKeySizes(keyType)
for _, size := range size {
t.Run(fmt.Sprintf("with %s scheme, %v keyType, %v keySize", signingScheme, keyType, size), func(t *testing.T) {
//Verify after UnmarshalCBOR
Expand All @@ -712,7 +696,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.GetLocalSigner(keyType, size)
if err != nil {
return nil, err
}
Expand All @@ -733,47 +717,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
Loading

0 comments on commit ac60e82

Please sign in to comment.