forked from hyperledger/fabric-private-chaincode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmock_enclave.go
157 lines (129 loc) · 3.96 KB
/
mock_enclave.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
// +build mock_ecc
/*
Copyright IBM Corp. All Rights Reserved.
Copyright 2020 Intel Corporation
SPDX-License-Identifier: Apache-2.0
*/
package enclave
import (
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rand"
"crypto/sha256"
"crypto/x509"
"encoding/hex"
"strings"
"github.com/golang/protobuf/proto"
"github.com/golang/protobuf/ptypes/any"
"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"
"github.com/hyperledger/fabric-protos-go/ledger/rwset/kvrwset"
"github.com/hyperledger/fabric/protoutil"
)
type MockEnclaveStub struct {
privateKey *ecdsa.PrivateKey
enclaveId string
}
// 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 some dummy keys for our mock enclave
privateKey, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
if err != nil {
return nil, err
}
m.privateKey = privateKey
pubBytes, err := x509.MarshalPKIXPublicKey(privateKey.Public())
if err != nil {
return nil, err
}
hash := sha256.Sum256(pubBytes)
m.enclaveId = strings.ToUpper(hex.EncodeToString(hash[:]))
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: pubBytes,
CcParams: chaincodeParams,
HostParams: hostParams,
}),
},
}
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) {
pubBytes, err := x509.MarshalPKIXPublicKey(m.privateKey.Public())
if err != nil {
return "", err
}
hash := sha256.Sum256(pubBytes)
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())
}
v, _ := stub.GetState("SomeOtherKey")
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"),
}},
}
response := &protos.ChaincodeResponseMessage{
EncryptedResponse: []byte("some response"),
RwSet: rwset,
Signature: nil,
EnclaveId: m.enclaveId,
Proposal: signedProposal,
}
// get the read/write set in the same format as processed by the chaincode enclaves
readset, writeset, err := utils.ReplayReadWrites(stub, response.RwSet)
if err != nil {
shim.Error(err.Error())
}
// create signature
hash := utils.ComputedHash(response, readset, writeset)
sig, err := ecdsa.SignASN1(rand.Reader, m.privateKey, hash[:])
if err != nil {
return nil, err
}
response.Signature = sig
return proto.Marshal(response)
}