Skip to content

Commit 3966571

Browse files
author
zhangsk01
committed
ADD: initialize enclave hyperledger#2
1 parent d702301 commit 3966571

File tree

8 files changed

+344
-0
lines changed

8 files changed

+344
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/*
2+
* Copyright 2019 Intel Corporation
3+
* Copyright IBM Corp. All Rights Reserved.
4+
*
5+
* SPDX-License-Identifier: Apache-2.0
6+
*/
7+
8+
#ifndef _CHECK_SGX_ERROR_H_
9+
#define _CHECK_SGX_ERROR_H_
10+
11+
#include "log-defines.h"
12+
13+
#define CHECK_SGX_ERROR_AND_RETURN_ON_ERROR(sgx_status_ret) \
14+
if (sgx_status_ret != SGX_SUCCESS) \
15+
{ \
16+
LOG_ERROR( \
17+
"Lib: ERROR - %s:%d: " #sgx_status_ret "=%d", __FUNCTION__, __LINE__, sgx_status_ret); \
18+
return sgx_status_ret; \
19+
}
20+
21+
#endif /* _CHECK_SGX_ERROR_H_ */
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* Copyright 2019 Intel Corporation
3+
* Copyright IBM Corp. All Rights Reserved.
4+
*
5+
* SPDX-License-Identifier: Apache-2.0
6+
*/
7+
8+
#include "common-sgxcclib.h"
9+
10+
#include "check-sgx-error.h"
11+
#include <unistd.h>
12+
#include <pwd.h>
13+
14+
int sgxcc_create_enclave(sgx_enclave_id_t* eid, const char* enclave_file){
15+
if (access(enclave_file, F_OK) == -1)
16+
{
17+
LOG_ERROR("Lib: enclave file does not exist! %s", enclave_file);
18+
return SGX_ERROR_UNEXPECTED;
19+
}
20+
21+
sgx_launch_token_t token = {0};
22+
int updated = 0;
23+
24+
int ret = sgx_create_enclave(enclave_file, SGX_DEBUG_FLAG, &token, &updated, eid, NULL);
25+
CHECK_SGX_ERROR_AND_RETURN_ON_ERROR(ret);
26+
27+
return SGX_SUCCESS;
28+
}
29+
30+
int sgxcc_destroy_enclave(enclave_id_t eid){
31+
int ret = sgx_destroy_enclave((sgx_enclave_id_t)eid);
32+
CHECK_SGX_ERROR_AND_RETURN_ON_ERROR(ret)
33+
return SGX_SUCCESS;
34+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* Copyright 2019 Intel Corporation
3+
* Copyright IBM Corp. All Rights Reserved.
4+
*
5+
* SPDX-License-Identifier: Apache-2.0
6+
*/
7+
8+
#ifndef _COMMON_SGXCCLIB_H_
9+
#define _COMMON_SGXCCLIB_H_
10+
11+
#include "fpc-types.h"
12+
#include "sgx_urts.h"
13+
#include "log-defines.h"
14+
15+
#ifdef __cplusplus
16+
extern "C" {
17+
#endif
18+
19+
int sgxcc_create_enclave(enclave_id_t* eid,
20+
const char* enclave_file);
21+
int sgxcc_destroy_enclave(enclave_id_t eid);
22+
23+
#ifdef __cplusplus
24+
}
25+
#endif /* __cplusplus */
26+
27+
#endif /* !_COMMON_SGXCCLIB_H_ */

peer/node/enclave/enclave.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package enclave
2+
import "C"
3+
4+
// #cgo CFLAGS: -I${SRCDIR}/sgxsdk/include
5+
// #cgo LDFLAGS: -L${SRCDIR}/sgxsdk/lib64 -lsgx_urts_sim -lsgx_uae_service_sim
6+
// #include "common-sgxcclib.h"
7+
//
8+
import "C"
9+
import (
10+
"github.com/pkg/errors"
11+
)
12+
13+
func CreateEnclave(enclaveLibFile string) (err error) {
14+
var eid C.enclave_id_t
15+
var ret = C.sgxcc_create_enclave(&eid, C.CString(enclaveLibFile))
16+
if ret != 0 {
17+
return errors.Errorf("can not create enclave (%s): Reason: %v", enclaveLibFile, ret)
18+
}
19+
return nil
20+
}

peer/node/enclave/fpc-types.h

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Copyright IBM Corp. All Rights Reserved.
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#ifndef _FPC_TYPES_H_
8+
#define _FPC_TYPES_H_
9+
10+
#include <stdarg.h>
11+
#include <stdint.h>
12+
#include <stdio.h>
13+
#include <stdlib.h>
14+
15+
typedef uint64_t enclave_id_t;
16+
typedef uint8_t* quote_t;
17+
typedef struct spid_t
18+
{
19+
uint8_t id[16];
20+
} spid_t;
21+
22+
typedef uint8_t report_t[432];
23+
typedef uint8_t target_info_t[512];
24+
typedef uint8_t cmac_t[16];
25+
26+
typedef struct ec256_public_t
27+
{
28+
uint8_t gx[32];
29+
uint8_t gy[32];
30+
} ec256_public_t;
31+
32+
typedef struct ec256_signature_t
33+
{
34+
uint32_t x[8];
35+
uint32_t y[8];
36+
} ec256_signature_t;
37+
38+
#endif

peer/node/enclave/log-defines.h

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/*
2+
* Copyright IBM Corp. All Rights Reserved.
3+
* Copyright 2020 Intel Corporation
4+
*
5+
* SPDX-License-Identifier: Apache-2.0
6+
*/
7+
8+
#ifndef LOG_DEFINES
9+
#define LOG_DEFINES
10+
11+
#ifndef TAG
12+
#define TAG ""
13+
#endif
14+
15+
#define LOC_FMT " (%s:%d) "
16+
17+
#define NRM "\x1B[0m"
18+
#define CYN "\x1B[36m"
19+
#define YEL "\x1B[33m"
20+
#define RED "\x1B[31m"
21+
22+
#include <stdio.h>
23+
24+
/*
25+
* Note: `DO_DEBUG` is set to `false` by default, so no `LOG_DEBUG` is displayed.
26+
* At compile time, this behaviour can be changed by defining `-DDO_DEBUG=true` before the header is
27+
* included. In SGX deployments, such define should be set "only" when the `SGX_BUILD` environment
28+
* variable is set to `DEBUG`. Finally, notice that `DO_INFO`, `DO_WARNING` and `DO_ERROR` are set
29+
* to `true` by default. So, unless they are explictly disabled at compile time, the respective logs
30+
* will be displayed.
31+
*/
32+
33+
#ifndef DO_DEBUG
34+
#define DO_DEBUG false
35+
#endif
36+
37+
#ifndef DO_INFO
38+
#define DO_INFO true
39+
#endif
40+
41+
#ifndef DO_WARNING
42+
#define DO_WARNING true
43+
#endif
44+
45+
#ifndef DO_ERROR
46+
#define DO_ERROR true
47+
#endif
48+
49+
#ifdef __cplusplus
50+
extern "C" {
51+
#endif
52+
int printf(const char* fmt, ...);
53+
#ifdef __cplusplus
54+
}
55+
#endif
56+
57+
#if DO_DEBUG == true
58+
#define LOG_DEBUG(fmt, ...) \
59+
printf(CYN "DEBUG " LOC_FMT TAG YEL fmt NRM "\n", __FILE__, __LINE__, ##__VA_ARGS__)
60+
#else // DO_DEBUG
61+
#define LOG_DEBUG(fmt, ...)
62+
#endif // DO_DEBUG
63+
64+
#if DO_INFO == true
65+
#define LOG_INFO(fmt, ...) \
66+
printf(CYN "INFO " LOC_FMT TAG NRM fmt "\n", __FILE__, __LINE__, ##__VA_ARGS__)
67+
#else // DO_INFO
68+
#define LOG_INFO(fmt, ...)
69+
#endif // DO_INFO
70+
71+
#if DO_WARNING == true
72+
#define LOG_WARNING(fmt, ...) \
73+
printf(CYN "WARNING " LOC_FMT TAG RED fmt NRM "\n", __FILE__, __LINE__, ##__VA_ARGS__)
74+
#else // DO_WARNING
75+
#define LOG_WARNING(fmt, ...)
76+
#endif // DO_WARNING
77+
78+
#if DO_ERROR == true
79+
#define LOG_ERROR(fmt, ...) \
80+
printf(CYN "ERROR " LOC_FMT TAG RED fmt NRM "\n", __FILE__, __LINE__, ##__VA_ARGS__)
81+
#else // DO_ERROR
82+
#define LOG_ERROR(fmt, ...)
83+
#endif // DO_ERROR
84+
85+
#define ERROR_LOG_STRING "error log - omitted"
86+
87+
#endif // LOG_DEFINES

peer/node/enclave/server/handle.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package server
2+
3+
import (
4+
"github.com/hyperledger/fabric/common/flogging"
5+
"github.com/hyperledger/fabric/peer/node/enclave"
6+
"github.com/pkg/errors"
7+
"github.com/spf13/viper"
8+
"go.etcd.io/etcd/pkg/fileutil"
9+
"net/http"
10+
)
11+
12+
var logger = flogging.MustGetLogger("enclaveCmd")
13+
14+
func CreateEnclave(enclaveSoPath string) (mrenclave string, enclavePk string, err error) {
15+
if !fileutil.Exist(enclaveSoPath) {
16+
err = errors.Errorf("no exists file path for enclave")
17+
return
18+
}
19+
20+
if e := enclave.CreateEnclave(enclaveSoPath); e == nil {
21+
logger.Info("Enclave create success")
22+
} else {
23+
err = e
24+
return
25+
}
26+
return
27+
}
28+
29+
// 创建安全区
30+
func (s *HttpServer) HttpCreateEnclave(w http.ResponseWriter, r *http.Request) {
31+
if viper.GetBool("peer.enclave.enabled") {
32+
logger.Info("Enclave is creating...")
33+
enclavePath := viper.GetString("peer.enclave.path")
34+
w.WriteHeader(http.StatusOK)
35+
if _, _, err := CreateEnclave(enclavePath) ; err != nil {
36+
logger.Errorf("Error creating enclave for reason: %s", err)
37+
w.WriteHeader(http.StatusBadRequest)
38+
}
39+
} else {
40+
logger.Info("peer.enclave.enabled not set yet")
41+
w.WriteHeader(http.StatusBadRequest)
42+
}
43+
}
44+
45+
// 获取安全区公钥
46+
func (s *HttpServer) HttpGetEnclavePubKey(w http.ResponseWriter, r *http.Request) {
47+
w.WriteHeader(http.StatusOK)
48+
}
49+
50+
func (s *HttpServer) HttpSaveKey(w http.ResponseWriter, r *http.Request) {
51+
w.WriteHeader(http.StatusOK)
52+
}
53+
54+
func (s *HttpServer) HttpGetKey(w http.ResponseWriter, r *http.Request) {
55+
w.WriteHeader(http.StatusOK)
56+
}

peer/node/enclave/server/server.go

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package server
2+
3+
import (
4+
"log"
5+
"net/http"
6+
"strconv"
7+
)
8+
9+
const (
10+
CreateEnclaveEntry = "/create-enclave"
11+
GetEnclavePubKeyEntry = "/get-enclave-pubkey"
12+
SaveKeyEntry = "/save-key"
13+
GetKeyEntry = "/get-key"
14+
)
15+
// http 监听请求
16+
type HttpServer struct {
17+
port int
18+
server *http.Server
19+
}
20+
21+
func NewServer(port int) *HttpServer {
22+
httpServer := &HttpServer{
23+
port: port,
24+
server: nil,
25+
}
26+
// set server
27+
return httpServer
28+
}
29+
30+
func (s *HttpServer) Run() {
31+
// register server service and run
32+
log.Printf("[Node] start the listen server")
33+
s.registerServer()
34+
}
35+
36+
func (s *HttpServer) registerServer() {
37+
log.Printf("[Server] set listen port:%d\n", s.port)
38+
39+
httpRegister := map[string]func(http.ResponseWriter, *http.Request){
40+
CreateEnclaveEntry: s.HttpCreateEnclave,
41+
GetEnclavePubKeyEntry: s.HttpGetEnclavePubKey,
42+
SaveKeyEntry: s.HttpSaveKey,
43+
GetKeyEntry: s.HttpGetKey,
44+
}
45+
46+
mux := http.NewServeMux()
47+
for k, v := range httpRegister {
48+
log.Printf("[Server] register the func for %s", k)
49+
mux.HandleFunc(k, v)
50+
}
51+
52+
s.server = &http.Server{
53+
Addr: ":" + strconv.Itoa(s.port),
54+
Handler: mux,
55+
}
56+
57+
if err := s.server.ListenAndServe(); err != nil {
58+
log.Printf("[Server Error] %s", err)
59+
return
60+
}
61+
}

0 commit comments

Comments
 (0)