Line data Source code
1 : #include <openenclave/host.h> 2 : #include <stdio.h> 3 : #include <stdbool.h> 4 : 5 : #include "enclave_proxy.h" 6 : 7 : #include "hsm_u.h" 8 : #include "enclave_provider.h" 9 : #include "keyvalue_store.h" 10 : #include "log.h" 11 : 12 : #define CHECK_ECALL_RESULT(oe_result, error_msg, ret) \ 13 : { \ 14 : if (OE_OK != oe_result) { \ 15 : LOG(error_msg); \ 16 : LOG(": oe_result=%u (%s)\n", oe_result, oe_result_str(oe_result)); \ 17 : return (ret); \ 18 : } \ 19 : } 20 : 21 : /** 22 : * ECALLS 23 : */ 24 : 25 4 : bool eprx_system_init(unsigned char* msg_buffer, size_t msg_buffer_size) { 26 4 : oe_enclave_t* enclave = epro_get_enclave(); 27 4 : if (enclave == NULL) { 28 1 : LOG("Failed to retrieve the enclave. " 29 : "Unable to call system_init().\n"); 30 1 : return false; 31 : } 32 : 33 : bool result; 34 : oe_result_t oe_result = 35 3 : ecall_system_init(enclave, &result, msg_buffer, msg_buffer_size); 36 3 : CHECK_ECALL_RESULT(oe_result, "Failed to call system_init()", false); 37 2 : return result; 38 : } 39 : 40 3 : void eprx_system_finalise() { 41 3 : oe_enclave_t* enclave = epro_get_enclave(); 42 3 : if (enclave == NULL) { 43 1 : LOG("Failed to retrieve the enclave. " 44 : "Unable to call system_finalise().\n"); 45 1 : return; 46 : } 47 : 48 2 : oe_result_t oe_result = ecall_system_finalise(enclave); 49 2 : if (OE_OK != oe_result) { 50 1 : LOG("Failed to call system_finalise(): oe_result=%u (%s)\n", 51 : oe_result, 52 : oe_result_str(oe_result)); 53 : } 54 : } 55 : 56 3 : unsigned int eprx_system_process_apdu(unsigned int rx) { 57 3 : oe_enclave_t* enclave = epro_get_enclave(); 58 3 : if (enclave == NULL) { 59 1 : LOG("Failed to retrieve the enclave. " 60 : "Unable to call system_process_command().\n"); 61 1 : return false; 62 : } 63 : 64 : unsigned int result; 65 2 : oe_result_t oe_result = ecall_system_process_apdu(enclave, &result, rx); 66 : 67 2 : CHECK_ECALL_RESULT( 68 : oe_result, "Failed to call ecall_system_process_apdu()", false); 69 1 : return result; 70 : } 71 : 72 : /** 73 : * OCALLS 74 : */ 75 : 76 : #define OCALL_PREFIX "[Ocall] " 77 : 78 2 : bool ocall_kvstore_save(char* key, uint8_t* data, size_t data_size) { 79 2 : log_set_prefix(OCALL_PREFIX); 80 2 : bool retval = kvstore_save(key, data, data_size); 81 2 : log_clear_prefix(); 82 2 : return retval; 83 : } 84 : 85 2 : bool ocall_kvstore_exists(char* key) { 86 2 : log_set_prefix(OCALL_PREFIX); 87 2 : bool retval = kvstore_exists(key); 88 2 : log_clear_prefix(); 89 2 : return retval; 90 : } 91 : 92 2 : size_t ocall_kvstore_get(char* key, uint8_t* data_buf, size_t buffer_size) { 93 2 : log_set_prefix(OCALL_PREFIX); 94 2 : size_t retval = kvstore_get(key, data_buf, buffer_size); 95 2 : log_clear_prefix(); 96 2 : return retval; 97 : } 98 : 99 2 : bool ocall_kvstore_remove(char* key) { 100 2 : log_set_prefix(OCALL_PREFIX); 101 2 : bool retval = kvstore_remove(key); 102 2 : log_clear_prefix(); 103 2 : return retval; 104 : }