-
Notifications
You must be signed in to change notification settings - Fork 0
/
entities.go
400 lines (335 loc) · 10.5 KB
/
entities.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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
/*
Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package entities
import (
"encoding/pem"
"reflect"
"github.com/hyperledger/fabric/bccsp"
"github.com/pkg/errors"
)
/**********************/
/* Struct definitions */
/**********************/
// BCCSPEntity is an implementation of the Entity interface
// holding a BCCSP instance
type BCCSPEntity struct {
IDstr string
BCCSP bccsp.BCCSP
}
// BCCSPSignerEntity is an implementation of the SignerEntity interface
type BCCSPSignerEntity struct {
BCCSPEntity
SKey bccsp.Key
SOpts bccsp.SignerOpts
HOpts bccsp.HashOpts
}
// BCCSPEncrypterEntity is an implementation of the EncrypterEntity interface
type BCCSPEncrypterEntity struct {
BCCSPEntity
EKey bccsp.Key
EOpts bccsp.EncrypterOpts
DOpts bccsp.DecrypterOpts
}
// BCCSPEncrypterSignerEntity is an implementation of the EncrypterSignerEntity interface
type BCCSPEncrypterSignerEntity struct {
BCCSPEncrypterEntity
BCCSPSignerEntity
}
/****************/
/* Constructors */
/****************/
// NewAES256EncrypterEntity returns an encrypter entity that is
// capable of performing AES 256 bit encryption using PKCS#7 padding.
// Optionally, the IV can be provided in which case it is used during
// the encryption; othjerwise, a random one is generated.
func NewAES256EncrypterEntity(ID string, b bccsp.BCCSP, key, IV []byte) (*BCCSPEncrypterEntity, error) {
if b == nil {
return nil, errors.New("nil BCCSP")
}
k, err := b.KeyImport(key, &bccsp.AES256ImportKeyOpts{Temporary: true})
if err != nil {
return nil, errors.WithMessage(err, "bccspInst.KeyImport failed")
}
return NewEncrypterEntity(ID, b, k, &bccsp.AESCBCPKCS7ModeOpts{IV: IV}, &bccsp.AESCBCPKCS7ModeOpts{})
}
// NewEncrypterEntity returns an EncrypterEntity that is capable
// of performing encryption using i) the supplied BCCSP instance;
// ii) the supplied encryption key and iii) the supplied encryption
// and decryption options. The identifier of the entity is supplied
// as an argument as well - it's the caller's responsibility to
// choose it in a way that it is meaningful
func NewEncrypterEntity(ID string, bccsp bccsp.BCCSP, eKey bccsp.Key, eOpts bccsp.EncrypterOpts, dOpts bccsp.DecrypterOpts) (*BCCSPEncrypterEntity, error) {
if ID == "" {
return nil, errors.New("NewEntity error: empty ID")
}
if bccsp == nil {
return nil, errors.New("NewEntity error: nil bccsp")
}
if eKey == nil {
return nil, errors.New("NewEntity error: nil keys")
}
return &BCCSPEncrypterEntity{
BCCSPEntity: BCCSPEntity{
IDstr: ID,
BCCSP: bccsp,
},
EKey: eKey,
EOpts: eOpts,
DOpts: dOpts,
}, nil
}
// NewECDSASignerEntity returns a signer entity that is capable of signing using ECDSA
func NewECDSASignerEntity(ID string, b bccsp.BCCSP, signKeyBytes []byte) (*BCCSPSignerEntity, error) {
if b == nil {
return nil, errors.New("nil BCCSP")
}
bl, _ := pem.Decode(signKeyBytes)
if bl == nil {
return nil, errors.New("pem.Decode returns nil")
}
signKey, err := b.KeyImport(bl.Bytes, &bccsp.ECDSAPrivateKeyImportOpts{Temporary: true})
if err != nil {
return nil, errors.WithMessage(err, "bccspInst.KeyImport failed")
}
return NewSignerEntity(ID, b, signKey, nil, &bccsp.SHA256Opts{})
}
// NewECDSAVerifierEntity returns a verifier entity that is capable of verifying using ECDSA
func NewECDSAVerifierEntity(ID string, b bccsp.BCCSP, signKeyBytes []byte) (*BCCSPSignerEntity, error) {
if b == nil {
return nil, errors.New("nil BCCSP")
}
bl, _ := pem.Decode(signKeyBytes)
if bl == nil {
return nil, errors.New("pem.Decode returns nil")
}
signKey, err := b.KeyImport(bl.Bytes, &bccsp.ECDSAPKIXPublicKeyImportOpts{Temporary: true})
if err != nil {
return nil, errors.WithMessage(err, "bccspInst.KeyImport failed")
}
return NewSignerEntity(ID, b, signKey, nil, &bccsp.SHA256Opts{})
}
// NewSignerEntity returns a SignerEntity
func NewSignerEntity(ID string, bccsp bccsp.BCCSP, sKey bccsp.Key, sOpts bccsp.SignerOpts, hOpts bccsp.HashOpts) (*BCCSPSignerEntity, error) {
if ID == "" {
return nil, errors.New("NewSignerEntity error: empty ID")
}
if bccsp == nil {
return nil, errors.New("NewSignerEntity error: nil bccsp")
}
if sKey == nil {
return nil, errors.New("NewSignerEntity error: nil key")
}
return &BCCSPSignerEntity{
BCCSPEntity: BCCSPEntity{
IDstr: ID,
BCCSP: bccsp,
},
SKey: sKey,
SOpts: sOpts,
HOpts: hOpts,
}, nil
}
// NewAES256EncrypterECDSASignerEntity returns an encrypter entity that is
// capable of performing AES 256 bit encryption using PKCS#7 padding and
// signing using ECDSA
func NewAES256EncrypterECDSASignerEntity(ID string, b bccsp.BCCSP, encKeyBytes, signKeyBytes []byte) (*BCCSPEncrypterSignerEntity, error) {
if b == nil {
return nil, errors.New("nil BCCSP")
}
encKey, err := b.KeyImport(encKeyBytes, &bccsp.AES256ImportKeyOpts{Temporary: true})
if err != nil {
return nil, errors.WithMessage(err, "bccspInst.KeyImport failed")
}
bl, _ := pem.Decode(signKeyBytes)
if bl == nil {
return nil, errors.New("pem.Decode returns nil")
}
signKey, err := b.KeyImport(bl.Bytes, &bccsp.ECDSAPrivateKeyImportOpts{Temporary: true})
if err != nil {
return nil, errors.WithMessage(err, "bccspInst.KeyImport failed")
}
return NewEncrypterSignerEntity(ID, b, encKey, signKey, &bccsp.AESCBCPKCS7ModeOpts{}, &bccsp.AESCBCPKCS7ModeOpts{}, nil, &bccsp.SHA256Opts{})
}
// NewEncrypterSignerEntity returns an EncrypterSignerEntity
// (which is also an EncrypterEntity) that is capable of
// performing encryption AND of generating signatures using
// i) the supplied BCCSP instance; ii) the supplied encryption
// and signing keys and iii) the supplied encryption, decryption,
// signing and hashing options. The identifier of the entity is
// supplied as an argument as well - it's the caller's responsibility
// to choose it in a way that it is meaningful
func NewEncrypterSignerEntity(ID string, bccsp bccsp.BCCSP, eKey, sKey bccsp.Key, eOpts bccsp.EncrypterOpts, dOpts bccsp.DecrypterOpts, sOpts bccsp.SignerOpts, hOpts bccsp.HashOpts) (*BCCSPEncrypterSignerEntity, error) {
if ID == "" {
return nil, errors.New("NewEntity error: empty ID")
}
if bccsp == nil {
return nil, errors.New("NewEntity error: nil bccsp")
}
if eKey == nil || sKey == nil {
return nil, errors.New("NewEntity error: nil keys")
}
return &BCCSPEncrypterSignerEntity{
BCCSPEncrypterEntity: BCCSPEncrypterEntity{
BCCSPEntity: BCCSPEntity{
IDstr: ID,
BCCSP: bccsp,
},
EKey: eKey,
EOpts: eOpts,
DOpts: dOpts,
},
BCCSPSignerEntity: BCCSPSignerEntity{
BCCSPEntity: BCCSPEntity{
IDstr: ID,
BCCSP: bccsp,
},
SKey: sKey,
SOpts: sOpts,
HOpts: hOpts,
},
}, nil
}
/***********/
/* Methods */
/***********/
func (e *BCCSPEntity) ID() string {
return e.IDstr
}
func (e *BCCSPEncrypterEntity) Encrypt(plaintext []byte) ([]byte, error) {
return e.BCCSP.Encrypt(e.EKey, plaintext, e.EOpts)
}
func (e *BCCSPEncrypterEntity) Decrypt(ciphertext []byte) ([]byte, error) {
return e.BCCSP.Decrypt(e.EKey, ciphertext, e.DOpts)
}
func (this *BCCSPEncrypterEntity) Equals(e Entity) bool {
if that, rightType := e.(*BCCSPEncrypterEntity); rightType {
return compare(this.EKey, that.EKey)
}
return false
}
func (pe *BCCSPEncrypterEntity) Public() (Entity, error) {
var err error
eKeyPub := pe.EKey
if !pe.EKey.Symmetric() {
if eKeyPub, err = pe.EKey.PublicKey(); err != nil {
return nil, errors.WithMessage(err, "public error, eKey.PublicKey returned")
}
}
return &BCCSPEncrypterEntity{
BCCSPEntity: BCCSPEntity{
IDstr: pe.IDstr,
BCCSP: pe.BCCSP,
},
DOpts: pe.DOpts,
EOpts: pe.EOpts,
EKey: eKeyPub,
}, nil
}
func (this *BCCSPSignerEntity) Equals(e Entity) bool {
if that, rightType := e.(*BCCSPSignerEntity); rightType {
return compare(this.SKey, that.SKey)
}
return false
}
func (e *BCCSPSignerEntity) Public() (Entity, error) {
var err error
sKeyPub := e.SKey
if !e.SKey.Symmetric() {
if sKeyPub, err = e.SKey.PublicKey(); err != nil {
return nil, errors.WithMessage(err, "public error, sKey.PublicKey returned")
}
}
return &BCCSPSignerEntity{
BCCSPEntity: BCCSPEntity{
IDstr: e.IDstr,
BCCSP: e.BCCSP,
},
HOpts: e.HOpts,
SOpts: e.SOpts,
SKey: sKeyPub,
}, nil
}
func (e *BCCSPSignerEntity) Sign(msg []byte) ([]byte, error) {
h, err := e.BCCSP.Hash(msg, e.HOpts)
if err != nil {
return nil, errors.WithMessage(err, "sign error: bccsp.Hash returned")
}
return e.BCCSP.Sign(e.SKey, h, e.SOpts)
}
func (e *BCCSPSignerEntity) Verify(signature, msg []byte) (bool, error) {
h, err := e.BCCSP.Hash(msg, e.HOpts)
if err != nil {
return false, errors.WithMessage(err, "sign error: bccsp.Hash returned")
}
return e.BCCSP.Verify(e.SKey, signature, h, e.SOpts)
}
func (pe *BCCSPEncrypterSignerEntity) Public() (Entity, error) {
var err error
eKeyPub := pe.EKey
if !pe.EKey.Symmetric() {
if eKeyPub, err = pe.EKey.PublicKey(); err != nil {
return nil, errors.WithMessage(err, "public error, eKey.PublicKey returned")
}
}
sKeyPub, err := pe.SKey.PublicKey()
if err != nil {
return nil, errors.WithMessage(err, "public error, sKey.PublicKey returned")
}
return &BCCSPEncrypterSignerEntity{
BCCSPEncrypterEntity: BCCSPEncrypterEntity{
BCCSPEntity: BCCSPEntity{
IDstr: pe.BCCSPEncrypterEntity.IDstr,
BCCSP: pe.BCCSPEncrypterEntity.BCCSP,
},
EKey: eKeyPub,
EOpts: pe.EOpts,
DOpts: pe.DOpts,
},
BCCSPSignerEntity: BCCSPSignerEntity{
BCCSPEntity: BCCSPEntity{
IDstr: pe.BCCSPEncrypterEntity.IDstr,
BCCSP: pe.BCCSPEncrypterEntity.BCCSP,
},
SKey: sKeyPub,
HOpts: pe.HOpts,
SOpts: pe.SOpts,
},
}, nil
}
func (this *BCCSPEncrypterSignerEntity) Equals(e Entity) bool {
if that, rightType := e.(*BCCSPEncrypterSignerEntity); rightType {
return compare(this.SKey, that.SKey) && compare(this.EKey, that.EKey)
} else {
return false
}
}
func (e *BCCSPEncrypterSignerEntity) ID() string {
return e.BCCSPEncrypterEntity.ID()
}
/********************/
/* Helper functions */
/********************/
// compare returns true if the two supplied keys are equivalent.
// If the supplied keys are symmetric keys, we compare their
// public versions. This is required because when we compare
// two entities, we might compare the public and the private
// version of the same entity and expect to be told that the
// entities are equivalent
func compare(this, that bccsp.Key) bool {
var err error
if this.Private() {
this, err = this.PublicKey()
if err != nil {
return false
}
}
if that.Private() {
that, err = that.PublicKey()
if err != nil {
return false
}
}
return reflect.DeepEqual(this, that)
}