|
| 1 | +/* |
| 2 | +Copyright IBM Corp. 2017 All Rights Reserved. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package pkcs11 |
| 18 | + |
| 19 | +import ( |
| 20 | + "crypto/rand" |
| 21 | + "crypto/rsa" |
| 22 | + "crypto/x509" |
| 23 | + "testing" |
| 24 | + |
| 25 | + "github.com/hyperledger/fabric/bccsp" |
| 26 | + "github.com/hyperledger/fabric/bccsp/utils" |
| 27 | + "github.com/stretchr/testify/assert" |
| 28 | +) |
| 29 | + |
| 30 | +func TestECDSAPKIXPublicKeyImportOptsKeyImporter(t *testing.T) { |
| 31 | + ki := currentBCCSP |
| 32 | + |
| 33 | + _, err := ki.KeyImport("Hello World", &bccsp.ECDSAPKIXPublicKeyImportOpts{}) |
| 34 | + assert.Error(t, err) |
| 35 | + assert.Contains(t, err.Error(), "[ECDSAPKIXPublicKeyImportOpts] Invalid raw material. Expected byte array.") |
| 36 | + |
| 37 | + _, err = ki.KeyImport("Hello World", nil) |
| 38 | + assert.Error(t, err) |
| 39 | + assert.Contains(t, err.Error(), "Invalid Opts parameter. It must not be nil.") |
| 40 | + |
| 41 | + _, err = ki.KeyImport(nil, &bccsp.ECDSAPKIXPublicKeyImportOpts{}) |
| 42 | + assert.Error(t, err) |
| 43 | + assert.Contains(t, err.Error(), "Invalid raw. Cannot be nil.") |
| 44 | + |
| 45 | + _, err = ki.KeyImport([]byte(nil), &bccsp.ECDSAPKIXPublicKeyImportOpts{}) |
| 46 | + assert.Error(t, err) |
| 47 | + assert.Contains(t, err.Error(), "[ECDSAPKIXPublicKeyImportOpts] Invalid raw. It must not be nil.") |
| 48 | + |
| 49 | + _, err = ki.KeyImport([]byte{0}, &bccsp.ECDSAPKIXPublicKeyImportOpts{}) |
| 50 | + assert.Error(t, err) |
| 51 | + assert.Contains(t, err.Error(), "Failed converting PKIX to ECDSA public key") |
| 52 | + |
| 53 | + k, err := rsa.GenerateKey(rand.Reader, 512) |
| 54 | + assert.NoError(t, err) |
| 55 | + raw, err := utils.PublicKeyToDER(&k.PublicKey) |
| 56 | + assert.NoError(t, err) |
| 57 | + _, err = ki.KeyImport(raw, &bccsp.ECDSAPKIXPublicKeyImportOpts{}) |
| 58 | + assert.Error(t, err) |
| 59 | + assert.Contains(t, err.Error(), "Failed casting to ECDSA public key. Invalid raw material.") |
| 60 | +} |
| 61 | + |
| 62 | +func TestECDSAPrivateKeyImportOptsKeyImporter(t *testing.T) { |
| 63 | + ki := currentBCCSP |
| 64 | + ki.(*impl).noPrivImport = false |
| 65 | + |
| 66 | + _, err := ki.KeyImport("Hello World", &bccsp.ECDSAPrivateKeyImportOpts{}) |
| 67 | + assert.Error(t, err) |
| 68 | + assert.Contains(t, err.Error(), "[ECDSADERPrivateKeyImportOpts] Invalid raw material. Expected byte array.") |
| 69 | + |
| 70 | + _, err = ki.KeyImport(nil, &bccsp.ECDSAPrivateKeyImportOpts{}) |
| 71 | + assert.Error(t, err) |
| 72 | + assert.Contains(t, err.Error(), "Invalid raw. Cannot be nil.") |
| 73 | + |
| 74 | + _, err = ki.KeyImport([]byte(nil), &bccsp.ECDSAPrivateKeyImportOpts{}) |
| 75 | + assert.Error(t, err) |
| 76 | + assert.Contains(t, err.Error(), "[ECDSADERPrivateKeyImportOpts] Invalid raw. It must not be nil.") |
| 77 | + |
| 78 | + _, err = ki.KeyImport([]byte{0}, &bccsp.ECDSAPrivateKeyImportOpts{}) |
| 79 | + assert.Error(t, err) |
| 80 | + assert.Contains(t, err.Error(), "Failed converting PKIX to ECDSA public key") |
| 81 | + |
| 82 | + k, err := rsa.GenerateKey(rand.Reader, 512) |
| 83 | + assert.NoError(t, err) |
| 84 | + raw := x509.MarshalPKCS1PrivateKey(k) |
| 85 | + _, err = ki.KeyImport(raw, &bccsp.ECDSAPrivateKeyImportOpts{}) |
| 86 | + assert.Error(t, err) |
| 87 | + assert.Contains(t, err.Error(), "Failed casting to ECDSA public key. Invalid raw material.") |
| 88 | +} |
| 89 | + |
| 90 | +func TestECDSAGoPublicKeyImportOptsKeyImporter(t *testing.T) { |
| 91 | + ki := currentBCCSP |
| 92 | + |
| 93 | + _, err := ki.KeyImport("Hello World", &bccsp.ECDSAGoPublicKeyImportOpts{}) |
| 94 | + assert.Error(t, err) |
| 95 | + assert.Contains(t, err.Error(), "[ECDSAGoPublicKeyImportOpts] Invalid raw material. Expected *ecdsa.PublicKey.") |
| 96 | + |
| 97 | + _, err = ki.KeyImport(nil, &bccsp.ECDSAGoPublicKeyImportOpts{}) |
| 98 | + assert.Error(t, err) |
| 99 | + assert.Contains(t, err.Error(), "Invalid raw. Cannot be nil.") |
| 100 | +} |
| 101 | + |
| 102 | +func TestX509PublicKeyImportOptsKeyImporter(t *testing.T) { |
| 103 | + ki := currentBCCSP |
| 104 | + |
| 105 | + _, err := ki.KeyImport("Hello World", &bccsp.X509PublicKeyImportOpts{}) |
| 106 | + assert.Error(t, err) |
| 107 | + assert.Contains(t, err.Error(), "[X509PublicKeyImportOpts] Invalid raw material. Expected *x509.Certificate.") |
| 108 | + |
| 109 | + _, err = ki.KeyImport(nil, &bccsp.X509PublicKeyImportOpts{}) |
| 110 | + assert.Error(t, err) |
| 111 | + assert.Contains(t, err.Error(), "Invalid raw. Cannot be nil.") |
| 112 | + |
| 113 | + cert := &x509.Certificate{} |
| 114 | + cert.PublicKey = "Hello world" |
| 115 | + _, err = ki.KeyImport(cert, &bccsp.X509PublicKeyImportOpts{}) |
| 116 | + assert.Error(t, err) |
| 117 | + assert.Contains(t, err.Error(), "Certificate's public key type not recognized. Supported keys: [ECDSA, RSA]") |
| 118 | +} |
0 commit comments