Skip to content
This repository was archived by the owner on Jan 27, 2022. It is now read-only.

Commit 8962aaa

Browse files
Implemented Key Sharing and Seperation Config id Feature for Singleton.
Signed-off-by: Karthika Murthy <[email protected]>
1 parent 235402f commit 8962aaa

File tree

20 files changed

+227
-41
lines changed

20 files changed

+227
-41
lines changed

enclave_manager/avalon_enclave_manager/kme/kme_enclave_info.py

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import os
1616
import json
1717
import time
18+
import random
1819
import logging
1920

2021
from ssl import SSLError
@@ -23,27 +24,29 @@
2324
import utility.hex_utils as hex_utils
2425
import utility.file_utils as file_utils
2526
import avalon_enclave_manager.kme.kme_enclave as enclave
26-
from avalon_enclave_manager.base_enclave_info import BaseEnclaveInfo
27+
import avalon_enclave_manager.base_enclave_info as enclave_info
2728

2829
logger = logging.getLogger(__name__)
2930

3031

31-
class KeyManagementEnclaveInfo(BaseEnclaveInfo):
32+
class KeyManagementEnclaveInfo(enclave_info.BaseEnclaveInfo):
3233
"""
3334
KME info class to initialize enclave, signup enclave and hold
3435
data obtained post signup.
3536
"""
3637

3738
# -------------------------------------------------------
38-
def __init__(self, config, worker_id, enlcave_type):
39+
def __init__(self, config, worker_id):
3940

4041
enclave._SetLogger(logger)
41-
super().__init__(config, enlcave_type)
42+
super().__init__(enclave.is_sgx_simulator())
4243

44+
self._config = config
4345
self._worker_id = worker_id
4446
self._initialize_enclave()
4547
enclave_info = self._create_enclave_signup_data()
4648
try:
49+
self.ias_nonce = enclave_info['ias_nonce']
4750
self.sealed_data = enclave_info['sealed_data']
4851
self.verifying_key = enclave_info['verifying_key']
4952
self.encryption_key = enclave_info['encryption_key']
@@ -66,43 +69,52 @@ def _create_enclave_signup_data(self):
6669
@returns enclave_info - A dictionary of enclave data
6770
"""
6871

72+
ias_nonce = '{0:032X}'.format(random.getrandbits(128))
6973
try:
70-
enclave_data = self._create_signup_info()
74+
enclave_data = self._create_signup_info(ias_nonce)
7175
except Exception as err:
7276
raise Exception('failed to create enclave signup data; {}'
7377
.format(str(err)))
7478

7579
enclave_info = dict()
80+
enclave_info['ias_nonce'] = ias_nonce
7681
enclave_info['sealed_data'] = enclave_data.sealed_signup_data
7782
enclave_info['verifying_key'] = enclave_data.verifying_key
7883
enclave_info['encryption_key'] = enclave_data.encryption_key
7984
enclave_info['encryption_key_signature'] = \
8085
enclave_data.encryption_key_signature
8186
enclave_info['enclave_id'] = enclave_data.verifying_key
8287
enclave_info['proof_data'] = ''
83-
if not self.is_sgx_simulator():
88+
if not enclave.is_sgx_simulator():
8489
enclave_info['proof_data'] = enclave_data.proof_data
8590

8691
return enclave_info
8792

8893
# -----------------------------------------------------------------
8994

90-
def _create_signup_info(self):
95+
def _create_signup_info(self, ias_nonce):
9196
"""
9297
Create enclave signup data
9398
99+
Parameters :
100+
@param ias_nonce - Used in IAS request to verify attestation
101+
as a distinguishing factor
94102
Returns :
95103
@returns signup_info_obj - Signup info data
96104
"""
97105

106+
# Part of what is returned with the signup data is an enclave quote, we
107+
# want to update the revocation list first.
108+
self._update_sig_rl()
109+
# Now, let the enclave create the signup data
110+
98111
signup_cpp_obj = enclave.SignupInfoKME()
99112

100113
if "wpe_mrenclave" in self._config:
101114
self._wpe_mrenclave = self._config["wpe_mrenclave"]
102115
else:
103-
tcf_home = os.environ.get("TCF_HOME", '../../../')
104116
self._wpe_mrenclave = hex_utils.mrenclave_hex_string(
105-
tcf_home + "/"
117+
enclave_info.TCF_HOME + "/"
106118
+ self._config["wpe_mrenclave_read_from_file"])
107119

108120
# @TODO : Passing in_ext_data_signature as empty string "" as of now
@@ -112,7 +124,7 @@ def _create_signup_info(self):
112124
return None
113125

114126
signup_info = self._get_signup_info(
115-
signup_data, signup_cpp_obj)
127+
signup_data, signup_cpp_obj, ias_nonce)
116128

117129
# Now we can finally serialize the signup info and create a
118130
# corresponding signup info object. Because we don't want the
@@ -160,22 +172,23 @@ def _verify_enclave_info(self, enclave_info, mr_enclave, signup_cpp_obj):
160172
self._wpe_mrenclave)
161173

162174
# ----------------------------------------------------------------
163-
def _init_enclave_with(self, signed_enclave):
175+
def _init_enclave_with(self, signed_enclave, config):
164176
"""
165177
Initialize and return tcf_enclave_info that holds details about
166178
the KME enclave
167179
168180
Parameters :
169181
@param signed_enclave - The enclave binary read from filesystem
182+
@param config - A dictionary of configurations
170183
Returns :
171184
@returns tcf_enclave_info - An instance of the tcf_enclave_info
172185
"""
173186
# Get sealed data if persisted from previous startup.
174187
persisted_sealed_data = file_utils.read_file(
175188
self._get_sealed_data_file_name(self._config["sealed_data_path"],
176189
self._worker_id))
177-
return self._attestation.init_enclave_info(
178-
signed_enclave, persisted_sealed_data,
179-
int(self._config['num_of_enclaves']))
190+
return enclave.tcf_enclave_info(
191+
signed_enclave, config['spid'], persisted_sealed_data,
192+
int(config['num_of_enclaves']))
180193

181194
# -----------------------------------------------------------------

enclave_manager/avalon_enclave_manager/singleton/singleton_enclave_info.py

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,12 @@ def _create_signup_info(self):
9696
"""
9797

9898
signup_cpp_obj = enclave.SignupInfoSingleton()
99+
if self._config.get("kss_config") is not None:
100+
signup_data = signup_cpp_obj.CreateEnclaveData(
101+
self._config.get("kss_config"))
102+
else:
103+
signup_data = signup_cpp_obj.CreateEnclaveData()
99104

100-
signup_data = signup_cpp_obj.CreateEnclaveData()
101105
if signup_data is None:
102106
return None
103107

@@ -114,10 +118,11 @@ def _create_signup_info(self):
114118
if signup_info_obj.sealed_signup_data is None:
115119
logger.info("Sealed data is None, so nothing to persist.")
116120
else:
117-
file_utils.write_to_file(signup_info_obj.sealed_signup_data,
118-
self._get_sealed_data_file_name(
119-
self._config["sealed_data_path"],
120-
self._worker_id))
121+
file_utils.write_to_file(
122+
signup_info_obj.sealed_signup_data,
123+
self._get_sealed_data_file_name(
124+
self._config["sealed_data_path"],
125+
self._worker_id))
121126
# Now we can return the real object
122127
return signup_info_obj
123128

@@ -166,8 +171,15 @@ def _init_enclave_with(self, signed_enclave):
166171
persisted_sealed_data = file_utils.read_file(
167172
self._get_sealed_data_file_name(self._config["sealed_data_path"],
168173
self._worker_id))
169-
return self._attestation.init_enclave_info(
170-
signed_enclave, persisted_sealed_data,
171-
int(self._config['num_of_enclaves']))
174+
175+
if self._config.get("kss_config") is not None:
176+
return self._attestation.init_enclave_info(
177+
signed_enclave, persisted_sealed_data,
178+
int(self._config['num_of_enclaves']),
179+
self._config.get("kss_config"))
180+
else:
181+
return self._attestation.init_enclave_info(
182+
signed_enclave, persisted_sealed_data,
183+
int(self._config['num_of_enclaves']))
172184

173185
# -----------------------------------------------------------------

enclave_manager/avalon_enclave_manager/singleton/singleton_enclave_manager.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,9 @@ def main(args=None):
119119
parser.add_argument("--config-dir", help="configuration folder", nargs="+")
120120
parser.add_argument("--worker_id",
121121
help="Id of worker in plain text", type=str)
122+
parser.add_argument("--kss_config",
123+
help="Key sharing and separation configuration id",
124+
type=str)
122125

123126
(options, remainder) = parser.parse_known_args(args)
124127

@@ -138,6 +141,9 @@ def main(args=None):
138141
if options.worker_id:
139142
config["WorkerConfig"]["worker_id"] = options.worker_id
140143

144+
if options.kss_config:
145+
config["EnclaveModule"]["kss_config"] = options.kss_config
146+
141147
plogger.setup_loggers(config.get("Logging", {}))
142148
sys.stdout = plogger.stream_to_logger(
143149
logging.getLogger("STDOUT"), logging.DEBUG)

tc/sgx/trusted_worker_manager/enclave/kme/signup_enclave_kme.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include <sgx_tseal.h>
2323
#include <sgx_utils.h>
2424
#include <sgx_quote.h>
25+
#include <sgx_key.h>
2526

2627
#include "crypto.h"
2728
#include "error.h"

tc/sgx/trusted_worker_manager/enclave/wpe/signup_enclave_wpe.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
#include <sgx_utils.h>
2424
#include <sgx_quote.h>
25+
#include <sgx_key.h>
2526

2627
#include "crypto.h"
2728
#include "error.h"

tc/sgx/trusted_worker_manager/enclave_untrusted/enclave_bridge/base.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ int tcf::enclave_api::base::IsSgxSimulator() {
4848
#endif // defined(SGX_SIMULATOR)
4949
} // tcf::enclave_api::base::IsSgxSimulator
5050

51-
5251
// XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
5352
tcf::enclave_queue::ReadyEnclave tcf::enclave_api::base::GetReadyEnclave() {
5453
return tcf::enclave_queue::ReadyEnclave(g_EnclaveReadyQueue);
@@ -60,7 +59,6 @@ void tcf::enclave_api::base::SetLastError(
6059
const std::string& message) {
6160
g_LastError = message;
6261
} // tcf::enclave_api::base::SetLastError
63-
6462
// XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
6563
std::string tcf::enclave_api::base::GetLastError(void) {
6664
return g_LastError;
@@ -71,7 +69,8 @@ tcf_err_t tcf::enclave_api::base::Initialize(
7169
const std::string& inPathToEnclave,
7270
const Attestation *attestation,
7371
const std::string& persisted_sealed_data,
74-
const int numOfEnclaves) {
72+
const int numOfEnclaves,
73+
const uint8_t (&kss_config_id)[SGX_CONFIGID_SIZE]) {
7574
tcf_err_t ret = TCF_SUCCESS;
7675

7776
try {
@@ -86,7 +85,7 @@ tcf_err_t tcf::enclave_api::base::Initialize(
8685
}
8786

8887
for (tcf::enclave_api::Enclave& enc : g_Enclave) {
89-
enc.Load(inPathToEnclave, persisted_sealed_data);
88+
enc.Load(inPathToEnclave, persisted_sealed_data, kss_config_id);
9089
}
9190

9291
g_IsInitialized = true;

tc/sgx/trusted_worker_manager/enclave_untrusted/enclave_bridge/base.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
#include <stdlib.h>
1919
#include <string>
20-
20+
#include "sgx_key.h"
2121
#include "error.h"
2222
#include "tcf_error.h"
2323
#include "types.h"
@@ -60,10 +60,12 @@ namespace tcf {
6060
persisted_sealed_data - Sealed data persisted from last bootup
6161
numOfEnclaves -- Number of worker enclaves to create
6262
*/
63+
6364
tcf_err_t Initialize(const std::string& inPathToEnclave,
6465
const Attestation *attestation,
6566
const std::string& persisted_sealed_data,
66-
const int numOfEnclaves);
67+
const int numOfEnclaves,
68+
const uint8_t (&kss_config_id)[SGX_CONFIGID_SIZE]);
6769

6870
/*
6971
Stop Avalon services

tc/sgx/trusted_worker_manager/enclave_untrusted/enclave_bridge/enclave.cpp

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,15 +70,21 @@ namespace tcf {
7070
// XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
7171
void Enclave::Load(
7272
const std::string& inEnclaveFilePath,
73-
const Base64EncodedString& inSealedEnclaveData) {
73+
const Base64EncodedString& inSealedEnclaveData,
74+
const uint8_t (&kss_config_id)[SGX_CONFIGID_SIZE]) {
7475
tcf::error::ThrowIf<tcf::error::ValueError>(
7576
inEnclaveFilePath.empty() ||
7677
inEnclaveFilePath.length() > PATH_MAX,
7778
"Invalid enclave path.");
7879

7980
this->Unload();
8081
this->enclaveFilePath = inEnclaveFilePath;
82+
for(int i=0; i <SGX_CONFIGID_SIZE;i++ ){
83+
this->_kss_config[i] = kss_config_id[i];
84+
}
85+
8186
this->LoadEnclave(inSealedEnclaveData);
87+
8288
} // Enclave::Load
8389

8490
// XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
@@ -114,6 +120,11 @@ namespace tcf {
114120
"Attestation object is not initialized"
115121
);
116122
this->attestation->CreateQuoteFromReport(inEnclaveReport, outEnclaveQuote);
123+
124+
sgx_quote_t* enclaveQuote =
125+
reinterpret_cast<sgx_quote_t *>(&outEnclaveQuote[0]);
126+
tcf::Log(TCF_LOG_INFO,"KSS Config Id added to the EnclaveQuote : %s\n", enclaveQuote->report_body.config_id );
127+
117128
} // Enclave::GenerateSignupData
118129

119130

@@ -137,7 +148,6 @@ namespace tcf {
137148
if (!this->enclaveId) {
138149
/* Enclave id, used in communicating with enclave */
139150
Enclave::QuerySgxStatus();
140-
141151
sgx_launch_token_t token = { 0 };
142152
int flags = SGX_DEBUG_FLAG;
143153
tcf::error::ThrowSgxError((SGX_DEBUG_FLAG == 0 ?
@@ -147,7 +157,9 @@ namespace tcf {
147157

148158
// First attempt to load the enclave executable
149159
sgx_status_t ret = SGX_SUCCESS;
150-
ret = tcf::sgx_util::CallSgx([this, flags, &token] () {
160+
if(this->_kss_config[0] == NULL){
161+
162+
ret = tcf::sgx_util::CallSgx([this, flags, &token] () {
151163
int updated = 0;
152164
return sgx_create_enclave(
153165
this->enclaveFilePath.c_str(),
@@ -160,7 +172,30 @@ namespace tcf {
160172
10, // retries
161173
250 // retryWaitMs
162174
);
163-
tcf::error::ThrowSgxError(ret, "Unable to create enclave.");
175+
tcf::error::ThrowSgxError(ret, "Unable to create enclave.");
176+
177+
} else {
178+
tcf::Log(TCF_LOG_INFO, "Enclave::sgx_create_enclave_ex called" );
179+
void *enclave_ex_p[32] = { 0 };
180+
enclave_ex_p[SGX_CREATE_ENCLAVE_EX_KSS_BIT_IDX] = &this->_kss_config;
181+
182+
ret = tcf::sgx_util::CallSgx([this, flags, &token, enclave_ex_p] () {
183+
int updated = 0;
184+
return sgx_create_enclave_ex(
185+
this->enclaveFilePath.c_str(),
186+
flags,
187+
&token,
188+
&updated,
189+
&this->enclaveId,
190+
NULL,
191+
SGX_CREATE_ENCLAVE_EX_KSS,
192+
(const void** )enclave_ex_p);
193+
},
194+
10, // retries
195+
250 // retryWaitMs
196+
);
197+
tcf::error::ThrowSgxError(ret, "Unable to create enclave with Config id");
198+
}
164199
// Initialize the enclave
165200
tcf_err_t tcfError = TCF_SUCCESS;
166201

0 commit comments

Comments
 (0)