github.com/hellobchain/third_party@v0.0.0-20230331131523-deb0478a2e52/hyperledger/fabric/bccsp/sw/keyimport.go (about) 1 /* 2 Copyright IBM Corp. All Rights Reserved. 3 4 SPDX-License-Identifier: Apache-2.0 5 */ 6 7 package sw 8 9 import ( 10 "crypto/rsa" 11 "errors" 12 "fmt" 13 "github.com/hellobchain/newcryptosm/ecdsa" 14 "github.com/hellobchain/newcryptosm/x509" 15 "reflect" 16 17 "github.com/hellobchain/third_party/hyperledger/fabric/bccsp" 18 ) 19 20 type aes256ImportKeyOptsKeyImporter struct{} 21 22 func (*aes256ImportKeyOptsKeyImporter) KeyImport(raw interface{}, opts bccsp.KeyImportOpts) (bccsp.Key, error) { 23 aesRaw, ok := raw.([]byte) 24 if !ok { 25 return nil, errors.New("Invalid raw material. Expected byte array.") 26 } 27 28 if aesRaw == nil { 29 return nil, errors.New("Invalid raw material. It must not be nil.") 30 } 31 32 if len(aesRaw) != 32 { 33 return nil, fmt.Errorf("Invalid Key Length [%d]. Must be 32 bytes", len(aesRaw)) 34 } 35 36 return &symmetryPrivateKey{aesRaw, false}, nil 37 } 38 39 type sm4ImportKeyOptsKeyImporter struct{} 40 41 func (*sm4ImportKeyOptsKeyImporter) KeyImport(raw interface{}, opts bccsp.KeyImportOpts) (k bccsp.Key, err error) { 42 sm4Raw, ok := raw.([]byte) 43 if !ok { 44 return nil, errors.New("Invalid raw material. Expected byte array.") 45 } 46 47 if sm4Raw == nil { 48 return nil, errors.New("Invalid raw material. It must not be nil.") 49 } 50 51 if len(sm4Raw) != 16 { 52 return nil, fmt.Errorf("Invalid Key Length [%d]. Must be 16 bytes", len(sm4Raw)) 53 } 54 55 return &symmetryPrivateKey{sm4Raw, false}, nil 56 } 57 58 type hmacImportKeyOptsKeyImporter struct{} 59 60 func (*hmacImportKeyOptsKeyImporter) KeyImport(raw interface{}, opts bccsp.KeyImportOpts) (bccsp.Key, error) { 61 symmetryRaw, ok := raw.([]byte) 62 if !ok { 63 return nil, errors.New("Invalid raw material. Expected byte array.") 64 } 65 66 if len(symmetryRaw) == 0 { 67 return nil, errors.New("Invalid raw material. It must not be nil.") 68 } 69 70 return &symmetryPrivateKey{symmetryRaw, false}, nil 71 } 72 73 type ecdsaPKIXPublicKeyImportOptsKeyImporter struct{} 74 75 func (*ecdsaPKIXPublicKeyImportOptsKeyImporter) KeyImport(raw interface{}, opts bccsp.KeyImportOpts) (bccsp.Key, error) { 76 der, ok := raw.([]byte) 77 if !ok { 78 return nil, errors.New("Invalid raw material. Expected byte array.") 79 } 80 81 if len(der) == 0 { 82 return nil, errors.New("Invalid raw. It must not be nil.") 83 } 84 85 lowLevelKey, err := derToPublicKey(der) 86 if err != nil { 87 return nil, fmt.Errorf("Failed converting PKIX to ECDSA public key [%s]", err) 88 } 89 90 ecdsaPK, ok := lowLevelKey.(*ecdsa.PublicKey) 91 if !ok { 92 return nil, errors.New("Failed casting to ECDSA public key. Invalid raw material.") 93 } 94 95 return &ecdsaPublicKey{ecdsaPK}, nil 96 } 97 98 type ecdsaPrivateKeyImportOptsKeyImporter struct{} 99 100 func (*ecdsaPrivateKeyImportOptsKeyImporter) KeyImport(raw interface{}, opts bccsp.KeyImportOpts) (bccsp.Key, error) { 101 der, ok := raw.([]byte) 102 if !ok { 103 return nil, errors.New("[ECDSADERPrivateKeyImportOpts] Invalid raw material. Expected byte array.") 104 } 105 106 if len(der) == 0 { 107 return nil, errors.New("[ECDSADERPrivateKeyImportOpts] Invalid raw. It must not be nil.") 108 } 109 110 lowLevelKey, err := derToPrivateKey(der) 111 if err != nil { 112 return nil, fmt.Errorf("Failed converting PKIX to ECDSA public key [%s]", err) 113 } 114 115 ecdsaSK, ok := lowLevelKey.(*ecdsa.PrivateKey) 116 if !ok { 117 return nil, errors.New("Failed casting to ECDSA private key. Invalid raw material.") 118 } 119 120 return &ecdsaPrivateKey{ecdsaSK}, nil 121 } 122 123 type ecdsaGoPublicKeyImportOptsKeyImporter struct{} 124 125 func (*ecdsaGoPublicKeyImportOptsKeyImporter) KeyImport(raw interface{}, opts bccsp.KeyImportOpts) (bccsp.Key, error) { 126 lowLevelKey, ok := raw.(*ecdsa.PublicKey) 127 if !ok { 128 return nil, errors.New("Invalid raw material. Expected *ecdsa.PublicKey.") 129 } 130 131 return &ecdsaPublicKey{lowLevelKey}, nil 132 } 133 134 type x509PublicKeyImportOptsKeyImporter struct { 135 bccsp *CSP 136 } 137 138 func (ki *x509PublicKeyImportOptsKeyImporter) KeyImport(raw interface{}, opts bccsp.KeyImportOpts) (bccsp.Key, error) { 139 x509Cert, ok := raw.(*x509.Certificate) 140 if !ok { 141 return nil, errors.New("Invalid raw material. Expected *x509.Certificate.") 142 } 143 144 pk := x509Cert.PublicKey 145 146 switch pk := pk.(type) { 147 case *ecdsa.PublicKey: 148 return ki.bccsp.KeyImporters[reflect.TypeOf(&bccsp.ECDSAGoPublicKeyImportOpts{})].KeyImport( 149 pk, 150 &bccsp.ECDSAGoPublicKeyImportOpts{Temporary: opts.Ephemeral()}) 151 case *rsa.PublicKey: 152 // This path only exists to support environments that use RSA certificate 153 // authorities to issue ECDSA certificates. 154 return &rsaPublicKey{pubKey: pk}, nil 155 default: 156 return nil, errors.New("Certificate's public key type not recognized. Supported keys: [ECDSA, RSA]") 157 } 158 }