Unverified Commit 5dc4b50e authored by Stan Kladko's avatar Stan Kladko Committed by GitHub

Merge pull request #149 from skalenetwork/SKALE-3151-cannot-decrypt-storage-key

Skale 3151 cannot decrypt storage key
parents f5a5395c f46249b5
......@@ -36,6 +36,10 @@ public:
SGXException(int32_t _status, const char* _errString) : status(_status), errString(_errString) {}
std::string getMessage() {
return "SGXException:status:" + std::to_string(status) + ":" + errString;
}
};
#endif //SGXD_RPCEXCEPTION_H
......@@ -54,6 +54,7 @@
#include "CSRManagerServer.h"
#include "BLSCrypto.h"
#include "ServerInit.h"
#include "SGXException.h"
#include "SGXWalletServer.hpp"
void initUserSpace() {
......@@ -104,23 +105,43 @@ void initEnclave(uint32_t _logLevel) {
spdlog::info("Enclave libtgmp library and logging initialized successfully");
}
void initAll(uint32_t _logLevel, bool _checkCert, bool _autoSign) {
static atomic<int> sgxServerInited(0);
void initAll(uint32_t _logLevel, bool _checkCert, bool _autoSign) {
cout << "Running sgxwallet version:" << SGXWalletServer::getVersion() << endl;
static atomic<bool> sgxServerInited(false);
static mutex initMutex;
CHECK_STATE(sgxServerInited != 1)
sgxServerInited = 1;
initEnclave(_logLevel);
initUserSpace();
initSEK();
lock_guard <mutex> lock(initMutex);
if (useHTTPS) {
SGXWalletServer::initHttpsServer(_checkCert);
SGXRegistrationServer::initRegistrationServer(_autoSign);
CSRManagerServer::initCSRManagerServer();
} else {
SGXWalletServer::initHttpServer();
if (sgxServerInited)
return;
try {
cout << "Running sgxwallet version:" << SGXWalletServer::getVersion() << endl;
CHECK_STATE(sgxServerInited != 1)
sgxServerInited = 1;
initEnclave(_logLevel);
initUserSpace();
initSEK();
if (useHTTPS) {
SGXWalletServer::initHttpsServer(_checkCert);
SGXRegistrationServer::initRegistrationServer(_autoSign);
CSRManagerServer::initCSRManagerServer();
} else {
SGXWalletServer::initHttpServer();
}
sgxServerInited = true;
} catch (SGXException &_e) {
spdlog::error(_e.getMessage());
} catch (exception &_e) {
spdlog::error(_e.what());
}
}
catch (...) {
exception_ptr p = current_exception();
printf("Exception %s \n", p.__cxa_exception_type()->name());
spdlog::error("Unknown exception");
}
};
1.56.0
\ No newline at end of file
1.57.0
\ No newline at end of file
version: '3'
services:
sgxwallet:
image: skalenetwork/sgxwallet_sim:latest
image: skalenetwork/sgxwallet_sim:develop-latest
ports:
- "1026:1026"
- "1027:1027"
......@@ -16,5 +16,5 @@ services:
max-size: "10m"
max-file: "4"
restart: unless-stopped
command: -s -y
command: -s
......@@ -67,6 +67,8 @@ extern domain_parameters curve;
#define SAFE_FREE(__X__) if (__X__) {free(__X__); __X__ = NULL;}
#define SAFE_DELETE(__X__) if (__X__) {delete(__X__); __X__ = NULL;}
#define SAFE_CHAR_BUF(__X__, __Y__) ;char __X__ [ __Y__ ]; memset(__X__, 0, __Y__);
#define RANDOM_CHAR_BUF(__X__, __Y__) ;char __X__ [ __Y__ ]; get_global_random( \
(unsigned char*) __X__, __Y__);
#define CHECK_ARG_CLEAN(_EXPRESSION_) \
if (!(_EXPRESSION_)) { \
......
#define SIGNED_ENCLAVE_VERSION "0"
\ No newline at end of file
......@@ -57,6 +57,8 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include "EnclaveConstants.h"
#include "EnclaveCommon.h"
#include "SIGNED_ENCLAVE_VERSION"
#define STRINGIFY(x) #x
#define TOSTRING(x) STRINGIFY(x)
......@@ -128,7 +130,24 @@ void trustedEnclaveInit(uint32_t _logLevel) {
enclave_init();
LOG_INFO("Successfully inited enclave");
LOG_INFO("Successfully inited enclave. Signed enclave version:" SIGNED_ENCLAVE_VERSION );
#ifndef SGX_DEBUG
LOG_INFO("SECURITY WARNING: sgxwallet is running in INSECURE DEBUG MODE! NEVER USE IN PRODUCTION!");
#endif
#if SGX_DEBUG != 0
LOG_INFO("SECURITY WARNING: sgxwallet is running in INSECURE DEBUG MODE! NEVER USE IN PRODUCTION!");
#endif
#if SGX_MODE == SIM
LOG_INFO("SECURITY WARNING: sgxwallet is running in INSECURE SIMULATION MODE! NEVER USE IN PRODUCTION!");
#endif
}
void free_function(void *ptr, size_t sz) {
......@@ -189,37 +208,43 @@ void get_global_random(unsigned char *_randBuff, uint64_t _size) {
}
void trustedGenerateSEK(int *errStatus, char *errString,
uint8_t *encrypted_SEK, uint32_t *enc_len, char *SEK_hex) {
void sealHexSEK(int *errStatus, char *errString,
uint8_t *encrypted_sek, uint32_t *enc_len, char *sek_hex) {
LOG_INFO(__FUNCTION__);
INIT_ERROR_STATE
CHECK_STATE(encrypted_SEK);
CHECK_STATE(SEK_hex);
CHECK_STATE(encrypted_sek);
CHECK_STATE(sek_hex);
SAFE_CHAR_BUF(SEK_raw, SGX_AESGCM_KEY_SIZE);;
uint64_t plaintextLen = strlen(sek_hex + 1);
uint64_t sealedLen = sgx_calc_sealed_data_size(0, plaintextLen);
uint32_t hex_aes_key_length = SGX_AESGCM_KEY_SIZE * 2;
carray2Hex((uint8_t*) SEK_raw, SGX_AESGCM_KEY_SIZE, SEK_hex);
sgx_attributes_t attribute_mask;
attribute_mask.flags = 0xfffffffffffffff3;
attribute_mask.xfrm = 0x0;
sgx_misc_select_t misc = 0xF0000000;
uint32_t sealedLen = sgx_calc_sealed_data_size(0, hex_aes_key_length + 1);
sgx_status_t status = sgx_seal_data_ex(SGX_KEYPOLICY_MRENCLAVE, attribute_mask, misc, 0, NULL, plaintextLen, (uint8_t *) sek_hex, sealedLen,
(sgx_sealed_data_t *) encrypted_sek);
CHECK_STATUS("seal SEK failed after SEK generation");
for (uint8_t i = 0; i < 16; i++) {
AES_key[i] = SEK_raw[i];
}
uint32_t encrypt_text_length = sgx_get_encrypt_txt_len((const sgx_sealed_data_t *)encrypted_sek);
CHECK_STATE(encrypt_text_length = plaintextLen);
sgx_attributes_t attribute_mask;
attribute_mask.flags = 0xfffffffffffffff3;
attribute_mask.xfrm = 0x0;
sgx_misc_select_t misc = 0xF0000000;
SAFE_CHAR_BUF(unsealedKey, BUF_LEN);
uint32_t decLen = BUF_LEN;
sgx_status_t status = sgx_seal_data_ex(SGX_KEYPOLICY_MRENCLAVE, attribute_mask, misc, 0, NULL, hex_aes_key_length + 1, (uint8_t *) SEK_hex, sealedLen,
(sgx_sealed_data_t *) encrypted_SEK);
CHECK_STATUS("seal SEK failed");
uint32_t add_text_length = sgx_get_add_mac_txt_len((const sgx_sealed_data_t *)encrypted_sek);
CHECK_STATE(add_text_length == 0);
CHECK_STATE(sgx_is_within_enclave(encrypted_sek,sizeof(sgx_sealed_data_t)));
status = sgx_unseal_data((const sgx_sealed_data_t *)encrypted_sek, NULL, NULL,
(uint8_t *) unsealedKey, &decLen );
CHECK_STATUS("seal/unseal SEK failed after SEK generation in unseal");
*enc_len = sealedLen;
SET_SUCCESS
......@@ -228,22 +253,55 @@ void trustedGenerateSEK(int *errStatus, char *errString,
LOG_INFO("SGX call completed");
}
void trustedSetSEK(int *errStatus, char *errString, uint8_t *encrypted_SEK) {
void trustedGenerateSEK(int *errStatus, char *errString,
uint8_t *encrypted_sek, uint32_t *enc_len, char *sek_hex) {
LOG_INFO(__FUNCTION__);
INIT_ERROR_STATE
CHECK_STATE(encrypted_SEK);
CHECK_STATE(encrypted_sek);
CHECK_STATE(sek_hex);
RANDOM_CHAR_BUF(SEK_raw, SGX_AESGCM_KEY_SIZE);
carray2Hex((uint8_t*) SEK_raw, SGX_AESGCM_KEY_SIZE, sek_hex);
memcpy(AES_key, SEK_raw, SGX_AESGCM_KEY_SIZE);
sealHexSEK(errStatus, errString, encrypted_sek, enc_len, sek_hex);
if (errStatus != 0) {
LOG_ERROR("sealHexSEK failed");
goto clean;
}
SET_SUCCESS
clean:
;
LOG_INFO("SGX call completed");
}
void trustedSetSEK(int *errStatus, char *errString, uint8_t *encrypted_sek) {
LOG_INFO(__FUNCTION__);
INIT_ERROR_STATE
CHECK_STATE(encrypted_sek);
SAFE_CHAR_BUF(aes_key_hex, BUF_LEN);
uint32_t dec_len;
uint32_t dec_len = BUF_LEN;
sgx_status_t status = sgx_unseal_data(
(const sgx_sealed_data_t *) encrypted_SEK, NULL, 0,
(const sgx_sealed_data_t *) encrypted_sek, NULL, 0,
(uint8_t *)aes_key_hex, &dec_len);
if (status == 0x3001) {
LOG_ERROR("Could not decrypt LevelDB storage! \n"
"If you upgraded sgxwallet software or if you are restoring from backup, please run sgxwallet with -b flag and "
"pass your backup key.");
}
CHECK_STATUS2("sgx unseal SEK failed with status %d");
uint64_t len;
hex2carray(aes_key_hex, &len, (uint8_t *) AES_key);
SET_SUCCESS
......@@ -253,32 +311,22 @@ void trustedSetSEK(int *errStatus, char *errString, uint8_t *encrypted_SEK) {
}
void trustedSetSEK_backup(int *errStatus, char *errString,
uint8_t *encrypted_SEK, uint32_t *enc_len, const char *SEK_hex) {
uint8_t *encrypted_sek, uint32_t *enc_len, const char *sek_hex) {
LOG_INFO(__FUNCTION__);
INIT_ERROR_STATE
CHECK_STATE(encrypted_SEK);
CHECK_STATE(SEK_hex);
CHECK_STATE(encrypted_sek);
CHECK_STATE(sek_hex);
uint64_t len;
hex2carray(SEK_hex, &len, (uint8_t *) AES_key);
uint32_t sealedLen = sgx_calc_sealed_data_size(0, strlen(SEK_hex) + 1);
hex2carray(sek_hex, &len, (uint8_t *) AES_key);
sealHexSEK(errStatus, errString, encrypted_sek, enc_len, (char *)sek_hex);
sgx_attributes_t attribute_mask;
attribute_mask.flags = 0xfffffffffffffff3;
attribute_mask.xfrm = 0x0;
sgx_misc_select_t misc = 0xF0000000;
sgx_status_t status = sgx_seal_data_ex(SGX_KEYPOLICY_MRENCLAVE,
attribute_mask, misc, 0, NULL, strlen(SEK_hex) + 1, (uint8_t *) SEK_hex, sealedLen,
(sgx_sealed_data_t *) encrypted_SEK);
CHECK_STATUS2("seal SEK failed with status %d")
*enc_len = sealedLen;
if (errStatus != 0) {
LOG_ERROR("sealHexSEK failed");
goto clean;
}
SET_SUCCESS
clean:
......@@ -286,6 +334,8 @@ void trustedSetSEK_backup(int *errStatus, char *errString,
LOG_INFO("SGX call completed");
}
void trustedGenerateEcdsaKeyAES(int *errStatus, char *errString,
uint8_t *encryptedPrivateKey, uint32_t *enc_len, char *pub_key_x, char *pub_key_y) {
LOG_INFO(__FUNCTION__);
......@@ -295,7 +345,7 @@ void trustedGenerateEcdsaKeyAES(int *errStatus, char *errString,
CHECK_STATE(pub_key_x);
CHECK_STATE(pub_key_y);
SAFE_CHAR_BUF(rand_char, 32);
RANDOM_CHAR_BUF(rand_char, 32);
mpz_t seed;
mpz_init(seed);
......@@ -304,8 +354,6 @@ void trustedGenerateEcdsaKeyAES(int *errStatus, char *errString,
point Pkey = point_init();
get_global_random((unsigned char *)rand_char, 32);
mpz_import(seed, 32, 1, sizeof(rand_char[0]), 0, 0, rand_char);
mpz_mod(skey, seed, curve->p);
......
......@@ -82,6 +82,18 @@ public:
}
};
class TestFixtureNoReset {
public:
TestFixtureNoReset() {
setOptions(L_INFO, false, true);
initAll(L_INFO, false, true);
}
~TestFixtureNoReset() {
TestUtils::destroyEnclave();
}
};
class TestFixtureHTTPS {
public:
TestFixtureHTTPS() {
......@@ -691,4 +703,10 @@ TEST_CASE_METHOD(TestFixture, "Many threads ecdsa dkg bls", "[many-threads-crypt
}
}
TEST_CASE_METHOD(TestFixture, "First run", "[first-run]") {
}
TEST_CASE_METHOD(TestFixtureNoReset, "Second run", "[second-run]") {
}
......@@ -28,8 +28,9 @@ username = getpass.getuser()
topDir = os.getcwd() + "/sgxwallet"
print("Top directory is:" + topDir)
testList = [ "[cert-sign]",
testList = ["[first-run]",
"[second-run]",
"[cert-sign]",
"[get-server-status]",
"[get-server-version]",
"[backup-key]",
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment