sgx/enclaveshim_ecalls.c
/* Initialize the enclave:
 *   Step 1: try to retrieve the launch token saved by last transaction
 *   Step 2: call sgx_create_enclave to initialize an enclave instance
 *   Step 3: save the launch token if it is updated
 */
Initialize the enclave:
Step 1: try to retrieve the launch token saved by last transaction
Step 2: call sgx_create_enclave to initialize an enclave instance
Step 3: save the launch token if it is updated
Initialize the enclave : Step 1 : try to retrieve the launch token saved by last transaction Step 2 : call sgx_create_enclave to initialize an enclave instance Step 3 : save the launch token if it is updated
int initialize_enclave(void)
{
    char token_path[MAX_PATH] = {'\0'};
    sgx_launch_token_t token = {0};
    sgx_status_t ret = SGX_ERROR_UNEXPECTED;
    int updated = 0;
    const char *home_dir = getpwuid(getuid())->pw_dir;
    if (home_dir != NULL &&
        (strlen(home_dir)+strlen("/")+sizeof(TOKEN_FILENAME)+1) <= MAX_PATH) {
        strncpy(token_path, home_dir, strlen(home_dir));
        strncat(token_path, "/", strlen("/"));
        strncat(token_path, TOKEN_FILENAME, sizeof(TOKEN_FILENAME)+1);
    } else {
        strncpy(token_path, TOKEN_FILENAME, sizeof(TOKEN_FILENAME));
    }
    FILE *fp = fopen(token_path, "rb");
    if (fp == NULL && (fp = fopen(token_path, "wb")) == NULL) {
        printf("Warning: Failed to create/open the launch token file \"%s\".\n", token_path);
    }
    if (fp != NULL) {
        size_t read_num = fread(token, 1, sizeof(sgx_launch_token_t), fp);
        if (read_num != 0 && read_num != sizeof(sgx_launch_token_t)) {
            memset(&token, 0x0, sizeof(sgx_launch_token_t));
            printf("Warning: Invalid launch token read from \"%s\".\n", token_path);
        }
    }
    ret = sgx_create_enclave(ENCLAVE_FILENAME, SGX_DEBUG_FLAG, &token, &updated, &global_eid, NULL);
    if (ret != SGX_SUCCESS) {
        print_error_message(ret, __func__);
        if (fp != NULL) fclose(fp);
        return -1;
    }
    if (updated == FALSE || fp == NULL) {
        if (fp != NULL) fclose(fp);
        return 0;
    }
    fp = freopen(token_path, "wb", fp);
    if (fp == NULL) return 0;
    size_t write_num = fwrite(token, 1, sizeof(sgx_launch_token_t), fp);
    if (write_num != sizeof(sgx_launch_token_t))
        printf("Warning: Failed to save launch token to \"%s\".\n", token_path);
    fclose(fp);
    return 0;
}
int initialize_enclave ( void ) { char token_path [ MAX_PATH ] = { ' \0 ' } ; sgx_launch_token_t token = { 0 } ; sgx_status_t ret = SGX_ERROR_UNEXPECTED ; int updated = 0 ; const char * home_dir = getpwuid ( getuid ( ) ) -> pw_dir ; if ( home_dir != NULL && ( strlen ( home_dir ) + strlen ( " " ) + sizeof ( TOKEN_FILENAME ) + 1 ) <= MAX_PATH ) { strncpy ( token_path , home_dir , strlen ( home_dir ) ) ; strncat ( token_path , " " , strlen ( " " ) ) ; strncat ( token_path , TOKEN_FILENAME , sizeof ( TOKEN_FILENAME ) + 1 ) ; } else { strncpy ( token_path , TOKEN_FILENAME , sizeof ( TOKEN_FILENAME ) ) ; } FILE * fp = fopen ( token_path , " " ) ; if ( fp == NULL && ( fp = fopen ( token_path , " " ) ) == NULL ) { printf ( " \" \" \n " , token_path ) ; } if ( fp != NULL ) { size_t read_num = fread ( token , 1 , sizeof ( sgx_launch_token_t ) , fp ) ; if ( read_num != 0 && read_num != sizeof ( sgx_launch_token_t ) ) { memset ( & token , 0x0 , sizeof ( sgx_launch_token_t ) ) ; printf ( " \" \" \n " , token_path ) ; } } ret = sgx_create_enclave ( ENCLAVE_FILENAME , SGX_DEBUG_FLAG , & token , & updated , & global_eid , NULL ) ; if ( ret != SGX_SUCCESS ) { print_error_message ( ret , __func__ ) ; if ( fp != NULL ) fclose ( fp ) ; return -1 ; } if ( updated == FALSE || fp == NULL ) { if ( fp != NULL ) fclose ( fp ) ; return 0 ; } fp = freopen ( token_path , " " , fp ) ; if ( fp == NULL ) return 0 ; size_t write_num = fwrite ( token , 1 , sizeof ( sgx_launch_token_t ) , fp ) ; if ( write_num != sizeof ( sgx_launch_token_t ) ) printf ( " \" \" \n " , token_path ) ; fclose ( fp ) ; return 0 ; }
Initialize the enclave:
   Step 1: try to retrieve the launch token saved by last transaction
   Step 2: call sgx_create_enclave to initialize an enclave instance
   Step 3: save the launch token if it is updated
Initialize the enclave : Step 1 : try to retrieve the launch token saved by last transaction Step 2 : call sgx_create_enclave to initialize an enclave instance Step 3 : save the launch token if it is updated
/* Step 1: try to retrieve the launch token saved by last transaction
     *		 *          *         if there is no token, then create a new one.
     *			 *                   */ /* try to get the token saved in $HOME */ /* compose the token path */ /* if token path is too long or $HOME is NULL */ /* read the token from saved file */ /* Step 2: call sgx_create_enclave to initialize an enclave instance */ /* Debug Support: set 2nd parameter to 1 */ /* Step 3: save the launch token if it is updated */ /* if the token is not updated, or file handler is invalid, do not perform saving */ /* reopen the file with write capablity */