github.com/Hnampk/fabric@v2.1.1+incompatible/cmd/common/signer/signer_test.go (about) 1 /* 2 Copyright IBM Corp. All Rights Reserved. 3 4 SPDX-License-Identifier: Apache-2.0 5 */ 6 7 package signer 8 9 import ( 10 "crypto/ecdsa" 11 "crypto/x509" 12 "encoding/pem" 13 "io/ioutil" 14 "os" 15 "path/filepath" 16 "testing" 17 18 "github.com/hyperledger/fabric/bccsp/utils" 19 "github.com/hyperledger/fabric/common/util" 20 "github.com/stretchr/testify/assert" 21 ) 22 23 func TestSigner(t *testing.T) { 24 conf := Config{ 25 MSPID: "SampleOrg", 26 IdentityPath: filepath.Join("testdata", "signer", "cert.pem"), 27 KeyPath: filepath.Join("testdata", "signer", "8150cb2d09628ccc89727611ebb736189f6482747eff9b8aaaa27e9a382d2e93_sk"), 28 } 29 30 signer, err := NewSigner(conf) 31 assert.NoError(t, err) 32 33 msg := []byte("foo") 34 sig, err := signer.Sign(msg) 35 assert.NoError(t, err) 36 37 r, s, err := utils.UnmarshalECDSASignature(sig) 38 ecdsa.Verify(&signer.key.PublicKey, util.ComputeSHA256(msg), r, s) 39 } 40 41 func TestSignerDifferentFormats(t *testing.T) { 42 key := `-----BEGIN EC PRIVATE KEY----- 43 MHcCAQEEIOwCtOQIkowasuWoDQpXHgC547VHq+aBFaSyPOoV8mnGoAoGCCqGSM49 44 AwEHoUQDQgAEEsrroAkPez9reWvJukufUqyfouJjakrKuhNBYuclkldqsLZ/TO+w 45 ZsQXrlIqlmNalfYPX+NDDELqlpXQBeEqnA== 46 -----END EC PRIVATE KEY-----` 47 48 pemBlock, _ := pem.Decode([]byte(key)) 49 assert.NotNil(t, pemBlock) 50 51 ecPK, err := x509.ParseECPrivateKey(pemBlock.Bytes) 52 assert.NoError(t, err) 53 54 ec1, err := x509.MarshalECPrivateKey(ecPK) 55 assert.NoError(t, err) 56 57 pkcs8, err := x509.MarshalPKCS8PrivateKey(ecPK) 58 assert.NoError(t, err) 59 60 for _, testCase := range []struct { 61 description string 62 keyBytes []byte 63 }{ 64 { 65 description: "EC1", 66 keyBytes: pem.EncodeToMemory(&pem.Block{Type: "EC Private Key", Bytes: ec1}), 67 }, 68 { 69 description: "PKCS8", 70 keyBytes: pem.EncodeToMemory(&pem.Block{Type: "Private Key", Bytes: pkcs8}), 71 }, 72 } { 73 t.Run(testCase.description, func(t *testing.T) { 74 tmpFile, err := ioutil.TempFile("", "key") 75 assert.NoError(t, err) 76 77 defer os.Remove(tmpFile.Name()) 78 79 err = ioutil.WriteFile(tmpFile.Name(), []byte(testCase.keyBytes), 0600) 80 assert.NoError(t, err) 81 82 signer, err := NewSigner(Config{ 83 MSPID: "MSPID", 84 IdentityPath: filepath.Join("testdata", "signer", "cert.pem"), 85 KeyPath: tmpFile.Name(), 86 }) 87 88 assert.NoError(t, err) 89 assert.NotNil(t, signer) 90 }) 91 } 92 } 93 94 func TestSignerBadConfig(t *testing.T) { 95 conf := Config{ 96 MSPID: "SampleOrg", 97 IdentityPath: filepath.Join("testdata", "signer", "non_existent_cert"), 98 } 99 100 signer, err := NewSigner(conf) 101 assert.EqualError(t, err, "open testdata/signer/non_existent_cert: no such file or directory") 102 assert.Nil(t, signer) 103 104 conf = Config{ 105 MSPID: "SampleOrg", 106 IdentityPath: filepath.Join("testdata", "signer", "cert.pem"), 107 KeyPath: filepath.Join("testdata", "signer", "non_existent_cert"), 108 } 109 110 signer, err = NewSigner(conf) 111 assert.EqualError(t, err, "open testdata/signer/non_existent_cert: no such file or directory") 112 assert.Nil(t, signer) 113 114 conf = Config{ 115 MSPID: "SampleOrg", 116 IdentityPath: filepath.Join("testdata", "signer", "cert.pem"), 117 KeyPath: filepath.Join("testdata", "signer", "broken_private_key"), 118 } 119 120 signer, err = NewSigner(conf) 121 assert.EqualError(t, err, "failed to decode PEM block from testdata/signer/broken_private_key") 122 assert.Nil(t, signer) 123 124 conf = Config{ 125 MSPID: "SampleOrg", 126 IdentityPath: filepath.Join("testdata", "signer", "cert.pem"), 127 KeyPath: filepath.Join("testdata", "signer", "empty_private_key"), 128 } 129 130 signer, err = NewSigner(conf) 131 assert.EqualError(t, err, "failed to parse private key: x509: failed to parse EC private key: asn1: syntax error: sequence truncated") 132 assert.Nil(t, signer) 133 }