-
Notifications
You must be signed in to change notification settings - Fork 0
/
client_impl.go
executable file
·276 lines (223 loc) · 7.67 KB
/
client_impl.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
/*
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
*/
package crypto
import (
"crypto/aes"
"crypto/cipher"
"github.com/openblockchain/obc-peer/openchain/crypto/utils"
obc "github.com/openblockchain/obc-peer/protos"
)
type clientImpl struct {
node *nodeImpl
isInitialized bool
// TCA KDFKey
tCertOwnerKDFKey []byte
}
func (client *clientImpl) GetName() string {
return client.node.GetName()
}
// NewChaincodeDeployTransaction is used to deploy chaincode.
func (client *clientImpl) NewChaincodeDeployTransaction(chaincodeDeploymentSpec *obc.ChaincodeDeploymentSpec, uuid string) (*obc.Transaction, error) {
// Verify that the client is initialized
if !client.isInitialized {
return nil, utils.ErrNotInitialized
}
// Get next available (not yet used) transaction certificate
rawTCert, err := client.getNextTCert()
if err != nil {
client.node.log.Error("Failed getting next transaction certificate [%s].", err.Error())
return nil, err
}
// Create Transaction
return client.newChaincodeDeployUsingTCert(chaincodeDeploymentSpec, uuid, rawTCert, nil)
}
// NewChaincodeInvokeTransaction is used to invoke chaincode's functions.
func (client *clientImpl) NewChaincodeExecute(chaincodeInvocation *obc.ChaincodeInvocationSpec, uuid string) (*obc.Transaction, error) {
// Verify that the client is initialized
if !client.isInitialized {
return nil, utils.ErrNotInitialized
}
// Get next available (not yet used) transaction certificate
rawTCert, err := client.getNextTCert()
if err != nil {
client.node.log.Error("Failed getting next transaction certificate [%s].", err.Error())
return nil, err
}
// Create Transaction
return client.newChaincodeExecuteUsingTCert(chaincodeInvocation, uuid, rawTCert, nil)
}
// NewChaincodeQuery is used to query chaincode's functions.
func (client *clientImpl) NewChaincodeQuery(chaincodeInvocation *obc.ChaincodeInvocationSpec, uuid string) (*obc.Transaction, error) {
// Verify that the client is initialized
if !client.isInitialized {
return nil, utils.ErrNotInitialized
}
// Get next available (not yet used) transaction certificate
rawTCert, err := client.getNextTCert()
if err != nil {
client.node.log.Error("Failed getting next transaction certificate [%s].", err.Error())
return nil, err
}
// Create Transaction
return client.newChaincodeQueryUsingTCert(chaincodeInvocation, uuid, rawTCert, nil)
}
// DecryptQueryResult is used to decrypt the result of a query transaction
func (client *clientImpl) DecryptQueryResult(queryTx *obc.Transaction, ct []byte) ([]byte, error) {
// Verify that the client is initialized
if !client.isInitialized {
return nil, utils.ErrNotInitialized
}
queryKey := utils.HMACTruncated(client.node.enrollChainKey, append([]byte{6}, queryTx.Nonce...), utils.AESKeyLength)
// client.node.log.Info("QUERY Decrypting with key: ", utils.EncodeBase64(queryKey))
if len(ct) <= utils.NonceSize {
return nil, utils.ErrDecrypt
}
c, err := aes.NewCipher(queryKey)
if err != nil {
return nil, err
}
gcm, err := cipher.NewGCM(c)
if err != nil {
return nil, err
}
nonce := make([]byte, gcm.NonceSize())
copy(nonce, ct)
out, err := gcm.Open(nil, nonce, ct[gcm.NonceSize():], nil)
if err != nil {
client.node.log.Error("Failed decrypting query result [%s].", err.Error())
return nil, utils.ErrDecrypt
}
return out, nil
}
// GetEnrollmentCertHandler returns a CertificateHandler whose certificate is the enrollment certificate
func (client *clientImpl) GetEnrollmentCertificateHandler() (CertificateHandler, error) {
// Verify that the client is initialized
if !client.isInitialized {
return nil, utils.ErrNotInitialized
}
// Return the handler
handler := &eCertHandlerImpl{}
err := handler.init(client)
if err != nil {
client.node.log.Error("Failed getting handler [%s].", err.Error())
return nil, err
}
return handler, nil
}
// GetTCertHandlerNext returns a CertificateHandler whose certificate is the next available TCert
func (client *clientImpl) GetTCertificateHandlerNext() (CertificateHandler, error) {
// Verify that the client is initialized
if !client.isInitialized {
return nil, utils.ErrNotInitialized
}
client.node.log.Info("Getting a CertificateHandler for the next available TCert...")
// Get next TCert
tCertDER, err := client.getNextTCert()
if err != nil {
client.node.log.Error("Failed getting next transaction certificate [%s].", err.Error())
return nil, err
}
// Return the handler
handler := &tCertHandlerImpl{}
err = handler.initDER(client, tCertDER)
if err != nil {
client.node.log.Error("Failed getting handler [%s].", err.Error())
return nil, err
}
return handler, nil
}
// GetTCertHandlerFromDER returns a CertificateHandler whose certificate is the one passed
func (client *clientImpl) GetTCertificateHandlerFromDER(tCertDER []byte) (CertificateHandler, error) {
// Verify that the client is initialized
if !client.isInitialized {
return nil, utils.ErrNotInitialized
}
client.node.log.Info("Getting a CertificateHandler for TCert [%s]", utils.EncodeBase64(tCertDER))
// Validate the transaction certificate
tCert, err := client.validateTCert(tCertDER)
if err != nil {
client.node.log.Warning("Failed validating transaction certificate [%s].", err)
return nil, err
}
// Return the handler
handler := &tCertHandlerImpl{}
err = handler.initX509(client, tCert)
if err != nil {
client.node.log.Error("Failed getting handler [%s].", err.Error())
return nil, err
}
return handler, nil
}
func (client *clientImpl) register(name string, pwd []byte, enrollID, enrollPWD string) error {
if client.isInitialized {
client.node.log.Error("Registering [%s]...done! Initialization already performed", name)
return nil
}
// Register node
node := new(nodeImpl)
if err := node.register("client", name, pwd, enrollID, enrollPWD); err != nil {
log.Error("Failed registering [%s] [%s].", enrollID, err.Error())
return err
}
client.node = node
return nil
}
func (client *clientImpl) init(id string, pwd []byte) error {
if client.isInitialized {
client.node.log.Info("Already initializaed.")
return nil
}
// Register node
var node *nodeImpl
if client.node != nil {
node = client.node
} else {
node = new(nodeImpl)
}
if err := node.init("client", id, pwd); err != nil {
return err
}
client.node = node
// Initialize keystore
client.node.log.Info("Init keystore...")
err := client.initKeyStore()
if err != nil {
if err != utils.ErrKeyStoreAlreadyInitialized {
client.node.log.Error("Keystore already initialized.")
} else {
client.node.log.Error("Failed initiliazing keystore [%s].", err.Error())
return err
}
}
client.node.log.Info("Init keystore...done.")
// Init crypto engine
err = client.initCryptoEngine()
if err != nil {
client.node.log.Error("Failed initiliazing crypto engine [%s].", err.Error())
return err
}
// initialized
client.isInitialized = true
client.node.log.Info("Initialization...done.")
return nil
}
func (client *clientImpl) close() error {
if client.node != nil {
return client.node.close()
}
return nil
}