/
enclave.go
160 lines (129 loc) · 4.39 KB
/
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
158
159
160
// +build !mock_ecc
/*
Copyright IBM Corp. All Rights Reserved.
Copyright 2020 Intel Corporation
SPDX-License-Identifier: Apache-2.0
*/
package enclave
import (
"context"
"fmt"
"unsafe"
"github.com/hyperledger/fabric-chaincode-go/shim"
"github.com/hyperledger/fabric/protoutil"
"golang.org/x/sync/semaphore"
)
// #cgo CFLAGS: -I${SRCDIR}/ecc-enclave-include -I${SRCDIR}/../../../common/sgxcclib
// #cgo LDFLAGS: -L${SRCDIR}/ecc-enclave-lib -lsgxcc -lgcov
// #include "common-sgxcclib.h"
// #include "sgxcclib.h"
//
import "C"
const enclaveLibFile = "enclave/lib/enclave.signed.so"
// EnclaveStub translates invocations into an enclave using cgo
type EnclaveStub struct {
eid C.enclave_id_t
sem *semaphore.Weighted
isInitialized bool
}
func NewEnclaveStub() *EnclaveStub {
return &EnclaveStub{sem: semaphore.NewWeighted(8)}
}
func (e *EnclaveStub) Init(chaincodeParams, hostParams, attestationParams []byte) ([]byte, error) {
// Estimate of the buffer length that is necessary for the credentials. It should be conservative.
const credentialsBufferMaxLen = 16 * 1024
if e.isInitialized {
return nil, fmt.Errorf("enclave already initialized")
}
var eid C.enclave_id_t
// prepare output buffer for credentials
credentialsBuffer := C.malloc(credentialsBufferMaxLen)
defer C.free(credentialsBuffer)
credentialsSize := C.uint32_t(0)
err := e.sem.Acquire(context.Background(), 1)
if err != nil {
return nil, err
}
// call the enclave
ret := C.sgxcc_create_enclave(
&eid,
C.CString(enclaveLibFile),
(*C.uint8_t)(C.CBytes(attestationParams)),
C.uint32_t(len(attestationParams)),
(*C.uint8_t)(C.CBytes(chaincodeParams)),
C.uint32_t(len(chaincodeParams)),
(*C.uint8_t)(C.CBytes(hostParams)),
C.uint32_t(len(hostParams)),
(*C.uint8_t)(credentialsBuffer),
C.uint32_t(credentialsBufferMaxLen),
&credentialsSize)
if ret != 0 {
msg := fmt.Sprintf("can not create enclave (%s): Reason: %v", enclaveLibFile, ret)
logger.Error(msg)
return nil, fmt.Errorf(msg)
}
e.eid = eid
e.sem.Release(1)
logger.Infof("Enclave created with eid=%d", e.eid)
e.isInitialized = true
// return credential bytes from sgx call
return C.GoBytes(credentialsBuffer, C.int(credentialsSize)), nil
}
func (e *EnclaveStub) GenerateCCKeys() ([]byte, error) {
panic("implement me")
}
func (e *EnclaveStub) ExportCCKeys(credentials []byte) ([]byte, error) {
panic("implement me")
}
func (e *EnclaveStub) ImportCCKeys() ([]byte, error) {
panic("implement me")
}
func (e *EnclaveStub) GetEnclaveId() (string, error) {
panic("implement me")
}
// ChaincodeInvoke calls the enclave for transaction processing
func (e *EnclaveStub) ChaincodeInvoke(stub shim.ChaincodeStubInterface, crmProtoBytes []byte) ([]byte, error) {
// Estimate of the buffer length where the enclave will write the response.
const scresmProtoBytesMaxLen = 1024 * 100 // Let's be really conservative ...
if !e.isInitialized {
return nil, fmt.Errorf("enclave not yet initialized")
}
// register our stub for callbacks
index := registry.register(&Stubs{stub})
defer registry.release(index)
ctx := unsafe.Pointer(&index)
// prep signed proposal input
proposal, err := stub.GetSignedProposal()
if err != nil {
return nil, fmt.Errorf("cannot get signed proposal: %s", err.Error())
}
signedProposalBytes, err := protoutil.Marshal(proposal)
if err != nil {
return nil, fmt.Errorf("cannot marshal signed proposal: %s", err.Error())
}
signedProposalPtr := C.CBytes(signedProposalBytes)
defer C.free(unsafe.Pointer(signedProposalPtr))
// prep response
scresmProtoBytesLenOut := C.uint32_t(0) // We pass maximal length separately; set to zero so we can detect valid responses
scresmProtoBytesPtr := C.malloc(scresmProtoBytesMaxLen)
defer C.free(scresmProtoBytesPtr)
crmProtoBytesPtr := C.CBytes(crmProtoBytes)
defer C.free(unsafe.Pointer(crmProtoBytesPtr))
err = e.sem.Acquire(context.Background(), 1)
if err != nil {
return nil, err
}
// invoke enclave
invokeRet := C.sgxcc_invoke(e.eid,
(*C.uint8_t)(signedProposalPtr),
(C.uint32_t)(len(signedProposalBytes)),
(*C.uint8_t)(crmProtoBytesPtr),
(C.uint32_t)(len(crmProtoBytes)),
(*C.uint8_t)(scresmProtoBytesPtr), (C.uint32_t)(scresmProtoBytesMaxLen), &scresmProtoBytesLenOut,
ctx)
e.sem.Release(1)
if invokeRet != 0 {
return nil, fmt.Errorf("invoke failed. Reason: %d", int(invokeRet))
}
return C.GoBytes(scresmProtoBytesPtr, C.int(scresmProtoBytesLenOut)), nil
}