forked from hyperledger/fabric-private-chaincode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathecc.go
207 lines (171 loc) · 6.45 KB
/
ecc.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
/*
Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package chaincode
import (
"encoding/base64"
"fmt"
"github.com/hyperledger/fabric/protoutil"
"github.com/golang/protobuf/ptypes"
"github.com/hyperledger-labs/fabric-private-chaincode/ecc/chaincode/enclave"
"github.com/hyperledger-labs/fabric-private-chaincode/ecc/chaincode/ercc"
"github.com/hyperledger-labs/fabric-private-chaincode/internal/protos"
"github.com/hyperledger-labs/fabric-private-chaincode/internal/utils"
"github.com/hyperledger/fabric-chaincode-go/shim"
pb "github.com/hyperledger/fabric-protos-go/peer"
"github.com/hyperledger/fabric/common/flogging"
)
var logger = flogging.MustGetLogger("ecc")
// EnclaveChaincode struct
type EnclaveChaincode struct {
enclave enclave.StubInterface
}
func NewChaincodeEnclave() shim.Chaincode {
return &EnclaveChaincode{
enclave: enclave.NewEnclaveStub(),
}
}
// Init sets the chaincode state to "init"
func (t *EnclaveChaincode) Init(stub shim.ChaincodeStubInterface) pb.Response {
return shim.Success(nil)
}
// Invoke receives transactions and forwards to op handlers
func (t *EnclaveChaincode) Invoke(stub shim.ChaincodeStubInterface) pb.Response {
function, _ := stub.GetFunctionAndParameters()
logger.Infof("Invoke is running [%s]", function)
switch function {
case "__initEnclave":
return t.initEnclave(stub)
case "__invoke":
return t.invoke(stub)
case "__endorse":
return t.endorse(stub)
default:
return shim.Error("invalid invocation")
}
}
func (t *EnclaveChaincode) initEnclave(stub shim.ChaincodeStubInterface) pb.Response {
initMsg, err := extractInitEnclaveMessage(stub)
if err != nil {
errMsg := fmt.Sprintf("InitEnclave msg extraction failed: %s", err.Error())
return shim.Error(errMsg)
}
// fetch cc params and host params
chaincodeParams, err := extractChaincodeParams(stub)
if err != nil {
errMsg := fmt.Sprintf("chaincode params extraction failed: %s", err.Error())
return shim.Error(errMsg)
}
serializedChaincodeParams, err := protoutil.Marshal(chaincodeParams)
if err != nil {
errMsg := fmt.Sprintf("chaincode params marshalling failed: %s", err.Error())
return shim.Error(errMsg)
}
hostParams, err := extractHostParams(stub, initMsg)
if err != nil {
errMsg := fmt.Sprintf("host params extraction failed: %s", err.Error())
return shim.Error(errMsg)
}
serializedHostParams, err := protoutil.Marshal(hostParams)
if err != nil {
errMsg := fmt.Sprintf("host params marshalling failed: %s", err.Error())
return shim.Error(errMsg)
}
// main enclave initialization function
credentialsBytes, err := t.enclave.Init(serializedChaincodeParams, serializedHostParams, initMsg.AttestationParams)
if err != nil {
errMsg := fmt.Sprintf("Enclave Init function failed: %s", err.Error())
return shim.Error(errMsg)
}
// return credentials
return shim.Success([]byte(base64.StdEncoding.EncodeToString(credentialsBytes)))
}
func (t *EnclaveChaincode) invoke(stub shim.ChaincodeStubInterface) pb.Response {
// call enclave
var errMsg string
// prep chaincode request message as input
_, args := stub.GetFunctionAndParameters()
if len(args) != 1 {
return shim.Error("no chaincodeRequestMessage as argument found")
}
chaincodeRequestMessageB64 := args[0]
chaincodeRequestMessage, err := base64.StdEncoding.DecodeString(chaincodeRequestMessageB64)
if err != nil {
errMsg := fmt.Sprintf("cannot base64 decode ChaincodeRequestMessage ('%s'): %s", chaincodeRequestMessageB64, err.Error())
return shim.Error(errMsg)
}
chaincodeResponseMessage, errInvoke := t.enclave.ChaincodeInvoke(stub, chaincodeRequestMessage)
if errInvoke != nil {
errMsg = fmt.Sprintf("t.enclave.Invoke failed: %s", errInvoke)
logger.Errorf(errMsg)
// likely a chaincode error, so we still want response go back ...
}
chaincodeResponseMessageB64 := []byte(base64.StdEncoding.EncodeToString(chaincodeResponseMessage))
logger.Debugf("base64-encoded response message: '%s'", chaincodeResponseMessageB64)
var response pb.Response
if errInvoke == nil {
response = pb.Response{
Status: shim.OK,
Payload: chaincodeResponseMessageB64,
Message: errMsg,
}
} else {
response = pb.Response{
Status: shim.ERROR,
Payload: chaincodeResponseMessageB64,
Message: errMsg,
}
}
return response
}
func (t *EnclaveChaincode) endorse(stub shim.ChaincodeStubInterface) pb.Response {
chaincodeParams, err := extractChaincodeParams(stub)
if err != nil {
errMsg := fmt.Sprintf("cannot extract chaincode params: %s", err.Error())
logger.Errorf(errMsg)
return shim.Error(errMsg)
}
responseMsg, err := extractChaincodeResponseMessage(stub)
if err != nil {
errMsg := fmt.Sprintf("cannot extract chaincode response message: %s", err.Error())
logger.Errorf(errMsg)
return shim.Error(errMsg)
}
logger.Infof("try to get credentials from ERCC for channel: %s ccId: %s EnclaveId: %s ", chaincodeParams.ChannelId, chaincodeParams.ChaincodeId, responseMsg.EnclaveId)
// get corresponding enclave credentials from ercc
credentials, err := ercc.QueryEnclaveCredentials(stub, chaincodeParams.ChannelId, chaincodeParams.ChaincodeId, responseMsg.EnclaveId)
if err != nil {
return shim.Error(err.Error())
}
if credentials == nil {
return shim.Error(fmt.Sprintf("no credentials found for enclaveId = %s", responseMsg.EnclaveId))
}
var attestedData protos.AttestedData
if err := ptypes.UnmarshalAny(credentials.SerializedAttestedData, &attestedData); err != nil {
return shim.Error(err.Error())
}
// check cc params match credentials
// check cc params chaincode def
if ccParamsMatch(attestedData.CcParams, chaincodeParams) {
return shim.Error("ccParams don't match")
}
// check cc param.MSPID matches MSPID of endorser (Post-MVP)
// replay read/writes from kvrwset from enclave (to prepare commitment to ledger) and extract kvrwset for subsequent validation
readset, writeset, err := utils.ReplayReadWrites(stub, responseMsg.RwSet)
if err != nil {
return shim.Error(err.Error())
}
// validate enclave endorsement signature
err = utils.Validate(responseMsg, readset, writeset, &attestedData)
if err != nil {
return shim.Error(err.Error())
}
return shim.Success([]byte("OK")) // make sure we have a non-empty return on success so we can distinguish success from failure in cli ...
}
func ccParamsMatch(expected, actual *protos.CCParameters) bool {
return expected.ChaincodeId != actual.ChaincodeId ||
expected.ChannelId != actual.ChannelId ||
expected.Version != actual.Version ||
expected.Sequence != actual.Sequence
}