Line data Source code
1 : /** 2 : * The MIT License (MIT) 3 : * 4 : * Copyright (c) 2021 RSK Labs Ltd 5 : * 6 : * Permission is hereby granted, free of charge, to any person obtaining a copy 7 : * of this software and associated documentation files (the "Software"), to 8 : * deal in the Software without restriction, including without limitation the 9 : * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 10 : * sell copies of the Software, and to permit persons to whom the Software is 11 : * furnished to do so, subject to the following conditions: 12 : * 13 : * The above copyright notice and this permission notice shall be included in 14 : * all copies or substantial portions of the Software. 15 : * 16 : * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 : * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 : * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 : * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 : * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 21 : * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 22 : * IN THE SOFTWARE. 23 : */ 24 : 25 : #include <unistd.h> 26 : 27 : #include "hsm_u.h" 28 : #include "enclave_provider.h" 29 : #include "log.h" 30 : 31 : // Simulation build 32 : #ifndef SIM_BUILD 33 : #define CREATE_ENCLAVE_FLAGS 0 34 : #else 35 : #define CREATE_ENCLAVE_FLAGS OE_ENCLAVE_FLAG_SIMULATE 36 : #endif 37 : 38 : // Global pointer to the enclave. This should be the only global pointer to the 39 : // enclave 40 : static char* G_enclave_path = NULL; 41 : static oe_enclave_t* G_enclave = NULL; 42 : 43 8 : bool epro_init(char* enclave_path) { 44 8 : G_enclave_path = enclave_path; 45 8 : if (access(G_enclave_path, F_OK) != 0) { 46 2 : LOG("Invalid enclave path given: %s\n", G_enclave_path); 47 2 : return false; 48 : } 49 6 : return true; 50 : } 51 : 52 7 : oe_enclave_t* epro_get_enclave() { 53 7 : if (NULL == G_enclave) { 54 5 : oe_enclave_t* enclave = NULL; 55 5 : LOG("Creating HSM enclave...\n"); 56 5 : oe_result_t result = oe_create_hsm_enclave(G_enclave_path, 57 : OE_ENCLAVE_TYPE_AUTO, 58 : CREATE_ENCLAVE_FLAGS, 59 : NULL, 60 : 0, 61 : &enclave); 62 5 : if (OE_OK != result) { 63 1 : LOG("Failed to create enclave: oe_result=%u (%s)\n", 64 : result, 65 : oe_result_str(result)); 66 1 : return NULL; 67 : } 68 : 69 4 : LOG("HSM enclave created\n"); 70 4 : G_enclave = enclave; 71 : } 72 : 73 6 : return G_enclave; 74 : } 75 : 76 13 : void epro_finalize_enclave() { 77 13 : if (NULL != G_enclave) { 78 4 : oe_terminate_enclave(G_enclave); 79 4 : LOG("HSM enclave terminated\n"); 80 4 : G_enclave = NULL; 81 : } 82 13 : }