forked from hyperledger/fabric-private-chaincode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmock_enclave.go
More file actions
210 lines (171 loc) · 5.27 KB
/
Copy pathmock_enclave.go
File metadata and controls
210 lines (171 loc) · 5.27 KB
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
// +build mock_ecc
/*
Copyright IBM Corp. All Rights Reserved.
Copyright 2020 Intel Corporation
SPDX-License-Identifier: Apache-2.0
*/
package enclave
import (
"crypto/sha256"
"encoding/base64"
"encoding/hex"
"fmt"
"strings"
"github.com/golang/protobuf/proto"
"github.com/golang/protobuf/ptypes/any"
"github.com/hyperledger-labs/fabric-private-chaincode/internal/crypto"
"github.com/hyperledger-labs/fabric-private-chaincode/internal/protos"
"github.com/hyperledger/fabric-chaincode-go/shim"
"github.com/hyperledger/fabric-protos-go/ledger/rwset/kvrwset"
"github.com/hyperledger/fabric/protoutil"
)
type MockEnclaveStub struct {
privateKey []byte
publicKey []byte
enclaveId string
ccPrivateKey []byte
}
// NewEnclave starts a new enclave
func NewEnclaveStub() StubInterface {
return &MockEnclaveStub{}
}
func (m *MockEnclaveStub) Init(serializedChaincodeParams, serializedHostParamsBytes, serializedAttestationParams []byte) ([]byte, error) {
hostParams := &protos.HostParameters{}
err := proto.Unmarshal(serializedHostParamsBytes, hostParams)
if err != nil {
return nil, err
}
chaincodeParams := &protos.CCParameters{}
err = proto.Unmarshal(serializedChaincodeParams, chaincodeParams)
if err != nil {
return nil, err
}
// create enclave keys
publicKey, privateKey, err := crypto.NewECDSAKeys()
if err != nil {
return nil, err
}
m.privateKey = privateKey
m.publicKey = publicKey
// create chaincode encryption keys keys
ccPublicKey, ccPrivateKey, err := crypto.NewRSAKeys()
if err != nil {
return nil, err
}
m.ccPrivateKey = ccPrivateKey
// calculate enclave id
m.enclaveId, _ = m.GetEnclaveId()
logger.Debug("Init")
credentials := &protos.Credentials{
Attestation: []byte("{\"attestation_type\":\"simulated\",\"attestation\":\"MA==\"}"),
SerializedAttestedData: &any.Any{
TypeUrl: proto.MessageName(&protos.AttestedData{}),
Value: protoutil.MarshalOrPanic(&protos.AttestedData{
EnclaveVk: publicKey,
CcParams: chaincodeParams,
HostParams: hostParams,
ChaincodeEk: ccPublicKey,
}),
},
}
logger.Infof("Create credentials: %s", credentials)
return proto.Marshal(credentials)
}
func (m MockEnclaveStub) GenerateCCKeys() ([]byte, error) {
panic("implement me")
// -> *protos.SignedCCKeyRegistrationMessage
}
func (m MockEnclaveStub) ExportCCKeys(credentials []byte) ([]byte, error) {
panic("implement me")
// credentials *protos.Credentials -> *protos.SignedExportMessage,
}
func (m MockEnclaveStub) ImportCCKeys() ([]byte, error) {
panic("implement me")
// -> *protos.SignedCCKeyRegistrationMessage
}
func (m *MockEnclaveStub) GetEnclaveId() (string, error) {
hash := sha256.Sum256(m.publicKey)
return strings.ToUpper(hex.EncodeToString(hash[:])), nil
}
func (m *MockEnclaveStub) ChaincodeInvoke(stub shim.ChaincodeStubInterface, chaincodeRequestMessage []byte) ([]byte, error) {
logger.Debug("ChaincodeInvoke")
signedProposal, err := stub.GetSignedProposal()
if err != nil {
shim.Error(err.Error())
}
// unmarshal request
chaincodeRequestMessageProto := &protos.ChaincodeRequestMessage{}
err = proto.Unmarshal(chaincodeRequestMessage, chaincodeRequestMessageProto)
if err != nil {
return nil, err
}
encryptedRequestBytes := chaincodeRequestMessageProto.GetEncryptedRequest()
if encryptedRequestBytes == nil {
return nil, fmt.Errorf("no encrypted request")
}
// decrypt request
requestBytes, err := crypto.PkDecryptMessage(m.ccPrivateKey, encryptedRequestBytes)
if err != nil {
return nil, err
}
requestProto := &protos.CleartextChaincodeRequest{}
err = proto.Unmarshal(requestBytes, requestProto)
if err != nil {
return nil, err
}
// get return encryption key
returnEncryptionKey := requestProto.GetReturnEncryptionKey()
if returnEncryptionKey == nil {
return nil, fmt.Errorf("no return encryption key")
}
v, _ := stub.GetState("SomeOtherKey")
v_hash := sha256.Sum256(v)
logger.Debug("get state: %s", v)
rwset := &kvrwset.KVRWSet{
Reads: []*kvrwset.KVRead{{
Key: "helloKey",
Version: nil,
}},
Writes: []*kvrwset.KVWrite{{
Key: "SomeOtherKey",
IsDelete: false,
Value: []byte("some value"),
}},
}
readValueHashes := [][]byte{v_hash[:]}
fpcKvSet := &protos.FPCKVSet{
RwSet: rwset,
ReadValueHashes: readValueHashes,
}
requestMessageHash := sha256.Sum256(chaincodeRequestMessage)
//create dummy response
responseData := []byte("some response")
//response must be encoded
b64ResponseData := base64.StdEncoding.EncodeToString(responseData)
//encrypt response
encryptedResponse, err := crypto.EncryptMessage(returnEncryptionKey, []byte(b64ResponseData))
if err != nil {
return nil, err
}
response := &protos.ChaincodeResponseMessage{
EncryptedResponse: encryptedResponse,
FpcRwSet: fpcKvSet,
EnclaveId: m.enclaveId,
Proposal: signedProposal,
ChaincodeRequestMessageHash: requestMessageHash[:],
}
responseBytes, err := proto.Marshal(response)
if err != nil {
return nil, err
}
// create signature
sig, err := crypto.SignMessage(m.privateKey, responseBytes)
if err != nil {
return nil, err
}
signedResponse := &protos.SignedChaincodeResponseMessage{
ChaincodeResponseMessage: responseBytes,
Signature: sig,
}
return proto.Marshal(signedResponse)
}