forked from hyperledger/fabric-sdk-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
getsigid.go
245 lines (226 loc) · 7 KB
/
getsigid.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
/*
Copyright SecureKey Technologies Inc. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package msp
import (
"fmt"
"strings"
fabricCaUtil "github.com/hyperledger/fabric-sdk-go/internal/github.com/hyperledger/fabric-ca/util"
"github.com/hyperledger/fabric-sdk-go/pkg/common/providers/core"
"github.com/hyperledger/fabric-sdk-go/pkg/common/providers/msp"
"github.com/hyperledger/fabric-sdk-go/pkg/core/config/cryptoutil"
"github.com/hyperledger/fabric-sdk-go/pkg/fab/comm"
"github.com/pkg/errors"
)
func newUser(userData *msp.UserData, cryptoSuite core.CryptoSuite) (*User, error) {
pubKey, err := cryptoutil.GetPublicKeyFromCert(userData.EnrollmentCertificate, cryptoSuite)
if err != nil {
return nil, errors.WithMessage(err, "fetching public key from cert failed")
}
pk, err := cryptoSuite.GetKey(pubKey.SKI())
if err != nil {
return nil, errors.WithMessage(err, "cryptoSuite GetKey failed")
}
u := &User{
id: userData.ID,
mspID: userData.MSPID,
enrollmentCertificate: userData.EnrollmentCertificate,
privateKey: pk,
}
return u, nil
}
// NewUser creates a User instance
func (mgr *IdentityManager) NewUser(userData *msp.UserData) (*User, error) {
return newUser(userData, mgr.cryptoSuite)
}
func (mgr *IdentityManager) loadUserFromStore(username string) (*User, error) {
if mgr.userStore == nil {
return nil, msp.ErrUserNotFound
}
var user *User
userData, err := mgr.userStore.Load(msp.IdentityIdentifier{MSPID: mgr.orgMSPID, ID: username})
if err != nil {
return nil, err
}
user, err = mgr.NewUser(userData)
if err != nil {
return nil, err
}
return user, nil
}
// GetSigningIdentity returns a signing identity for the given id
func (mgr *IdentityManager) GetSigningIdentity(id string) (msp.SigningIdentity, error) {
user, err := mgr.GetUser(id)
if err != nil {
return nil, err
}
return user, nil
}
// CreateSigningIdentity creates a signing identity with the given options
func (mgr *IdentityManager) CreateSigningIdentity(opts ...msp.SigningIdentityOption) (msp.SigningIdentity, error) {
opt := msp.IdentityOption{}
for _, param := range opts {
err := param(&opt)
if err != nil {
return nil, errors.WithMessage(err, "failed to create identity")
}
}
if opt.Cert == nil {
return nil, errors.New("missing certificate")
}
var privateKey core.Key
if opt.PrivateKey == nil {
pubKey, err := cryptoutil.GetPublicKeyFromCert(opt.Cert, mgr.cryptoSuite)
if err != nil {
return nil, errors.WithMessage(err, "fetching public key from cert failed")
}
privateKey, err = mgr.cryptoSuite.GetKey(pubKey.SKI())
if err != nil {
return nil, errors.WithMessage(err, "could not find matching key for SKI")
}
} else {
var err error
privateKey, err = fabricCaUtil.ImportBCCSPKeyFromPEMBytes(opt.PrivateKey, mgr.cryptoSuite, true)
if err != nil {
return nil, errors.WithMessage(err, "failed to import key")
}
}
return &User{
mspID: mgr.orgMSPID,
enrollmentCertificate: opt.Cert,
privateKey: privateKey,
}, nil
}
// GetUser returns a user for the given user name
func (mgr *IdentityManager) GetUser(username string) (*User, error) { //nolint
u, err := mgr.loadUserFromStore(username)
if err != nil {
if err != msp.ErrUserNotFound {
return nil, errors.WithMessage(err, "loading user from store failed")
}
// Not found, continue
}
if u == nil {
certBytes := mgr.getEmbeddedCertBytes(username)
if certBytes == nil {
certBytes, err = mgr.getCertBytesFromCertStore(username)
if err != nil && err != msp.ErrUserNotFound {
return nil, errors.WithMessage(err, "fetching cert from store failed")
}
}
if certBytes == nil {
return nil, msp.ErrUserNotFound
}
privateKey, err := mgr.getEmbeddedPrivateKey(username)
if err != nil {
return nil, errors.WithMessage(err, "fetching embedded private key failed")
}
if privateKey == nil {
privateKey, err = mgr.getPrivateKeyFromCert(username, certBytes)
if err != nil {
return nil, errors.WithMessage(err, "getting private key from cert failed")
}
}
if privateKey == nil {
return nil, fmt.Errorf("unable to find private key for user [%s]", username)
}
mspID, ok := comm.MSPID(mgr.config, mgr.orgName)
if !ok {
return nil, errors.New("MSP ID config read failed")
}
u = &User{
id: username,
mspID: mspID,
enrollmentCertificate: certBytes,
privateKey: privateKey,
}
}
return u, nil
}
func (mgr *IdentityManager) getEmbeddedCertBytes(username string) []byte {
return mgr.embeddedUsers[strings.ToLower(username)].Cert
}
func (mgr *IdentityManager) getEmbeddedPrivateKey(username string) (core.Key, error) {
var privateKey core.Key
var err error
pemBytes := mgr.embeddedUsers[strings.ToLower(username)].Key
if pemBytes != nil {
// Try the crypto provider as a SKI
privateKey, err = mgr.cryptoSuite.GetKey(pemBytes)
if err != nil || privateKey == nil {
// Try as a pem
privateKey, err = fabricCaUtil.ImportBCCSPKeyFromPEMBytes(pemBytes, mgr.cryptoSuite, true)
if err != nil {
return nil, errors.Wrap(err, "import private key failed")
}
}
}
return privateKey, nil
}
func (mgr *IdentityManager) getPrivateKeyPemFromKeyStore(username string, ski []byte) ([]byte, error) {
if mgr.mspPrivKeyStore == nil {
return nil, nil
}
key, err := mgr.mspPrivKeyStore.Load(
&msp.PrivKeyKey{
ID: username,
MSPID: mgr.orgMSPID,
SKI: ski,
})
if err != nil {
return nil, err
}
keyBytes, ok := key.([]byte)
if !ok {
return nil, errors.New("key from store is not []byte")
}
return keyBytes, nil
}
func (mgr *IdentityManager) getCertBytesFromCertStore(username string) ([]byte, error) {
if mgr.mspCertStore == nil {
return nil, msp.ErrUserNotFound
}
cert, err := mgr.mspCertStore.Load(&msp.IdentityIdentifier{
ID: username,
MSPID: mgr.orgMSPID,
})
if err != nil {
if err == core.ErrKeyValueNotFound {
return nil, msp.ErrUserNotFound
}
return nil, err
}
certBytes, ok := cert.([]byte)
if !ok {
return nil, errors.New("cert from store is not []byte")
}
return certBytes, nil
}
func (mgr *IdentityManager) getPrivateKeyFromCert(username string, cert []byte) (core.Key, error) {
if cert == nil {
return nil, errors.New("cert is nil")
}
pubKey, err := cryptoutil.GetPublicKeyFromCert(cert, mgr.cryptoSuite)
if err != nil {
return nil, errors.WithMessage(err, "fetching public key from cert failed")
}
privKey, err := mgr.getPrivateKeyFromKeyStore(username, pubKey.SKI())
if err == nil {
return privKey, nil
}
if err != core.ErrKeyValueNotFound {
return nil, errors.WithMessage(err, "fetching private key from key store failed")
}
return mgr.cryptoSuite.GetKey(pubKey.SKI())
}
func (mgr *IdentityManager) getPrivateKeyFromKeyStore(username string, ski []byte) (core.Key, error) {
pemBytes, err := mgr.getPrivateKeyPemFromKeyStore(username, ski)
if err != nil {
return nil, err
}
if pemBytes != nil {
return fabricCaUtil.ImportBCCSPKeyFromPEMBytes(pemBytes, mgr.cryptoSuite, true)
}
return nil, core.ErrKeyValueNotFound
}