123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485 |
- /*
- * Copyright (C) 2011-2016 Intel Corporation. All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- *
- * * Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * * Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in
- * the documentation and/or other materials provided with the
- * distribution.
- * * Neither the name of Intel Corporation nor the names of its
- * contributors may be used to endorse or promote products derived
- * from this software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
- * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
- * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
- * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
- * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
- * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
- * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
- * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- *
- */
- //tae_service_sim.cpp : Defines the exported functions
- //
- #include "sgx_tae_service.h"
- #include "tae_service_internal.h"
- #include "pse_types.h"
- #include "stdlib.h"
- #include "string.h"
- #include "sgx_spinlock.h"
- #include "sgx_tae_service_t.h"
- #define RETRY_TIMES 2
- #define DEFAULT_VMC_ATTRIBUTE_MASK 0xFFFFFFFFFFFFFFCB
- #define DEFAULT_VMC_XFRM_MASK 0x0
- #ifdef _DEBUG
- //wait for 10min at most for debug
- #define DEFAULT_AESM_TIMEOUT 600000
- #else
- //wait for 10sec at most
- #define DEFAULT_AESM_TIMEOUT 10000
- #endif
- static sgx_spinlock_t g_spin_lock;
- static bool g_b_session_established = false;
- sgx_status_t sgx_create_pse_session()
- {
- sgx_spin_lock(&g_spin_lock);
- if(!g_b_session_established)
- g_b_session_established = true;
- sgx_spin_unlock(&g_spin_lock);
- return SGX_SUCCESS;
- }
- sgx_status_t sgx_close_pse_session()
- {
- sgx_spin_lock(&g_spin_lock);
- if(g_b_session_established)
- g_b_session_established = false;
- sgx_spin_unlock(&g_spin_lock);
- return SGX_SUCCESS;
- }
- sgx_status_t sgx_get_ps_sec_prop(sgx_ps_sec_prop_desc_t *p_security_property)
- {
- sgx_status_t ret = SGX_SUCCESS;
- if(!p_security_property)
- return SGX_ERROR_INVALID_PARAMETER;
- if (!g_b_session_established){
- return SGX_ERROR_AE_SESSION_INVALID;
- }
- else
- {
- se_ps_sec_prop_desc_internal_t *p_security_property_internal = (se_ps_sec_prop_desc_internal_t*)p_security_property;
- p_security_property_internal->desc_type = 0;
- p_security_property_internal->pse_miscselect = 0;
- p_security_property_internal->reserved1 = 0;
- memset(p_security_property_internal->reserved2, 0, sizeof(p_security_property_internal->reserved2));
- p_security_property_internal->pse_prod_id = 2;
- p_security_property_internal->pse_isvsvn = 1;
- p_security_property_internal->pse_attributes.flags = SGX_FLAGS_INITTED;
- p_security_property_internal->pse_attributes.xfrm = SGX_XFRM_LEGACY;
- memset(&(p_security_property_internal->pse_mr_signer), 0xEE,
- sizeof(p_security_property_internal->pse_mr_signer));
- p_security_property_internal->cse_sec_prop.gid_cse = 0;
- p_security_property_internal->cse_sec_prop.prvrl_version = 1;
- p_security_property_internal->cse_sec_prop.sigrl_version = 1;
- p_security_property_internal->cse_sec_prop.sec_info_type = 0;
- memset(&p_security_property_internal->cse_sec_prop.ca_id_cse, 0, sizeof(p_security_property_internal->cse_sec_prop.ca_id_cse));
- memset(&p_security_property_internal->cse_sec_prop.sec_info, 0, sizeof(p_security_property_internal->cse_sec_prop.sec_info));
- }
- return ret;
- }
- sgx_status_t sgx_get_trusted_time
- (
- sgx_time_t *p_current_time,
- sgx_time_source_nonce_t *p_time_source_nonce)
- {
- if(!p_current_time || !p_time_source_nonce)
- return SGX_ERROR_INVALID_PARAMETER;
- if (!g_b_session_established){
- return SGX_ERROR_AE_SESSION_INVALID;
- }
- pse_message_t *p_req_msg =
- (pse_message_t *)malloc(PSE_TIMER_READ_REQ_SIZE);
- if(!p_req_msg){
- return SGX_ERROR_OUT_OF_MEMORY;
- }
- pse_message_t *p_resp_msg =
- (pse_message_t *)malloc(PSE_TIMER_READ_RESP_SIZE);
- if(!p_resp_msg){
- free(p_req_msg);
- return SGX_ERROR_OUT_OF_MEMORY;
- }
- p_req_msg->exp_resp_size = sizeof(pse_timer_read_resp_t);
- p_req_msg->payload_size = sizeof(pse_timer_read_req_t);
- pse_timer_read_req_t *p_timer_req
- = (pse_timer_read_req_t *)p_req_msg->payload;
- p_timer_req->req_hdr.service_id = PSE_TRUSTED_TIME_SERVICE;
- p_timer_req->req_hdr.service_cmd = PSE_TIMER_READ;
- pse_timer_read_resp_t *p_timer_resp
- = (pse_timer_read_resp_t *)p_resp_msg->payload;
- sgx_status_t status = SGX_SUCCESS;
- sgx_status_t ret = SGX_SUCCESS;
- int retry = RETRY_TIMES;
- do {
- status = invoke_service_ocall(&ret,
- (uint8_t *)p_req_msg,
- PSE_TIMER_READ_REQ_SIZE,
- (uint8_t *)p_resp_msg,
- PSE_TIMER_READ_RESP_SIZE,
- DEFAULT_AESM_TIMEOUT);
- if(status != SGX_SUCCESS || ret != SGX_SUCCESS){
- status = SGX_ERROR_UNEXPECTED;
- continue;
- }
- if(p_timer_resp->resp_hdr.service_id != PSE_TRUSTED_TIME_SERVICE
- || p_timer_resp->resp_hdr.service_cmd != PSE_TIMER_READ
- || p_timer_resp->resp_hdr.status != PSE_SUCCESS){
- status = SGX_ERROR_UNEXPECTED;
- } else {
- memcpy(p_current_time, &p_timer_resp->timestamp, sizeof(sgx_time_t));
- memcpy(p_time_source_nonce, p_timer_resp->time_source_nonce,
- sizeof(sgx_time_source_nonce_t));
- status = SGX_SUCCESS;
- break;
- }
- } while(retry--);
- free(p_req_msg);
- free(p_resp_msg);
- return status;
- }
- sgx_status_t sgx_create_monotonic_counter_ex(
- uint16_t owner_policy,
- const sgx_attributes_t* owner_attribute_mask,
- sgx_mc_uuid_t *p_counter_uuid,
- uint32_t *p_counter_value)
- {
- if(!p_counter_value || !p_counter_uuid || !owner_attribute_mask){
- return SGX_ERROR_INVALID_PARAMETER;
- }
- if (0!= (~(MC_POLICY_SIGNER | MC_POLICY_ENCLAVE) & owner_policy)
- || 0 == ((MC_POLICY_SIGNER | MC_POLICY_ENCLAVE)& owner_policy))
- {
- return SGX_ERROR_INVALID_PARAMETER;
- }
- if (!g_b_session_established){
- return SGX_ERROR_AE_SESSION_INVALID;
- }
- pse_message_t *p_req_msg = (pse_message_t *)malloc(PSE_CREATE_MC_REQ_SIZE);
- if(!p_req_msg){
- return SGX_ERROR_OUT_OF_MEMORY;
- }
- pse_message_t *p_resp_msg = (pse_message_t *)malloc(PSE_CREATE_MC_RESP_SIZE);
- if(!p_resp_msg){
- free(p_req_msg);
- return SGX_ERROR_OUT_OF_MEMORY;
- }
- p_req_msg->exp_resp_size = sizeof(pse_mc_create_resp_t);
- p_req_msg->payload_size = sizeof(pse_mc_create_req_t);
- pse_mc_create_req_t *p_mc_req
- = (pse_mc_create_req_t *)p_req_msg->payload;
- p_mc_req->req_hdr.service_id = PSE_MC_SERVICE;
- p_mc_req->req_hdr.service_cmd = PSE_MC_CREATE;
- p_mc_req->policy = owner_policy;
- memcpy(p_mc_req->attr_mask , owner_attribute_mask, sizeof(p_mc_req->attr_mask));
- pse_mc_create_resp_t *p_mc_resp
- = (pse_mc_create_resp_t *)p_resp_msg->payload;
- sgx_status_t status = SGX_SUCCESS;
- sgx_status_t ret = SGX_SUCCESS;
- int retry = RETRY_TIMES;
- do {
- sgx_spin_lock(&g_spin_lock);
- status = invoke_service_ocall(&ret,
- (uint8_t *)p_req_msg,
- PSE_CREATE_MC_REQ_SIZE,
- (uint8_t *)p_resp_msg,
- PSE_CREATE_MC_RESP_SIZE,
- DEFAULT_AESM_TIMEOUT);
- sgx_spin_unlock(&g_spin_lock);
- if(status != SGX_SUCCESS || ret != SGX_SUCCESS){
- status = SGX_ERROR_UNEXPECTED;
- continue;
- }
- if(p_mc_resp->resp_hdr.service_id != PSE_MC_SERVICE
- || p_mc_resp->resp_hdr.service_cmd != PSE_MC_CREATE
- || p_mc_resp->resp_hdr.status != PSE_SUCCESS){
- status = SGX_ERROR_UNEXPECTED;
- } else {
- memcpy(p_counter_uuid->counter_id,
- &p_mc_resp->counter_id,
- sizeof(p_counter_uuid->counter_id));
- memcpy(p_counter_uuid->nonce,
- &p_mc_resp->nonce,
- sizeof(p_counter_uuid->nonce));
- *p_counter_value = 0;
- status = SGX_SUCCESS;
- break;
- }
- } while(retry--);
- free(p_req_msg);
- free(p_resp_msg);
- return status;
- }
- sgx_status_t sgx_increment_monotonic_counter(
- const sgx_mc_uuid_t *p_counter_uuid,
- uint32_t *p_counter_value)
- {
- if(!p_counter_value || !p_counter_uuid ){
- return SGX_ERROR_INVALID_PARAMETER;
- }
- if (!g_b_session_established){
- return SGX_ERROR_AE_SESSION_INVALID;
- }
- pse_message_t *p_req_msg = (pse_message_t *)malloc(PSE_INC_MC_REQ_SIZE);
- if(!p_req_msg){
- return SGX_ERROR_OUT_OF_MEMORY;
- }
- pse_message_t *p_resp_msg = (pse_message_t *)malloc(PSE_INC_MC_RESP_SIZE);
- if(!p_resp_msg){
- free(p_req_msg);
- return SGX_ERROR_OUT_OF_MEMORY;
- }
- p_req_msg->exp_resp_size = sizeof(pse_mc_inc_resp_t);
- p_req_msg->payload_size = sizeof(pse_mc_inc_req_t);
- pse_mc_inc_req_t *p_mc_req
- = (pse_mc_inc_req_t *)p_req_msg->payload;
- memcpy(p_mc_req->counter_id,
- p_counter_uuid->counter_id,
- sizeof(p_mc_req->counter_id));
- memcpy(p_mc_req->nonce,
- p_counter_uuid->nonce,
- sizeof(p_mc_req->nonce));
- p_mc_req->req_hdr.service_id = PSE_MC_SERVICE;
- p_mc_req->req_hdr.service_cmd = PSE_MC_INC;
- pse_mc_inc_resp_t *p_mc_resp
- = (pse_mc_inc_resp_t *)p_resp_msg->payload;
- sgx_status_t status = SGX_SUCCESS;
- sgx_status_t ret = SGX_SUCCESS;
- int retry = RETRY_TIMES;
- do {
- sgx_spin_lock(&g_spin_lock);
- status = invoke_service_ocall(&ret,
- (uint8_t *)p_req_msg,
- PSE_INC_MC_REQ_SIZE,
- (uint8_t *)p_resp_msg,
- PSE_INC_MC_RESP_SIZE,
- DEFAULT_AESM_TIMEOUT);
- sgx_spin_unlock(&g_spin_lock);
- if(status != SGX_SUCCESS || ret != SGX_SUCCESS){
- if(SGX_ERROR_MC_NOT_FOUND != ret)
- status = SGX_ERROR_UNEXPECTED;
- else
- status = SGX_ERROR_MC_NOT_FOUND;
- continue;
- }
- if(p_mc_resp->resp_hdr.service_id != PSE_MC_SERVICE
- || p_mc_resp->resp_hdr.service_cmd != PSE_MC_INC
- || p_mc_resp->resp_hdr.status != PSE_SUCCESS){
- if(PSE_ERROR_MC_NOT_FOUND == p_mc_resp->resp_hdr.status)
- status = SGX_ERROR_MC_NOT_FOUND;
- else
- status = SGX_ERROR_UNEXPECTED;
- } else
- {
- *p_counter_value = p_mc_resp->counter_value;
- status = SGX_SUCCESS;
- break;
- }
- } while(retry--);
- free(p_req_msg);
- free(p_resp_msg);
- return status;
- }
- sgx_status_t sgx_read_monotonic_counter(
- const sgx_mc_uuid_t *p_counter_uuid,
- uint32_t *p_counter_value)
- {
- if(!p_counter_value || !p_counter_uuid){
- return SGX_ERROR_INVALID_PARAMETER;
- }
- if (!g_b_session_established){
- return SGX_ERROR_AE_SESSION_INVALID;
- }
- pse_message_t *p_req_msg = (pse_message_t *)malloc(PSE_READ_MC_REQ_SIZE);
- if(!p_req_msg){
- return SGX_ERROR_OUT_OF_MEMORY;
- }
- pse_message_t *p_resp_msg = (pse_message_t *)malloc(PSE_READ_MC_RESP_SIZE);
- if(!p_resp_msg){
- free(p_req_msg);
- return SGX_ERROR_OUT_OF_MEMORY;
- }
- p_req_msg->exp_resp_size = sizeof(pse_mc_read_resp_t);
- p_req_msg->payload_size = sizeof(pse_mc_read_req_t);
- pse_mc_read_req_t *p_mc_req
- = (pse_mc_read_req_t *)p_req_msg->payload;
- memcpy(p_mc_req->counter_id,
- p_counter_uuid->counter_id,
- sizeof(p_mc_req->counter_id));
- memcpy(p_mc_req->nonce,
- p_counter_uuid->nonce,
- sizeof(p_mc_req->nonce));
- p_mc_req->req_hdr.service_id = PSE_MC_SERVICE;
- p_mc_req->req_hdr.service_cmd = PSE_MC_READ;
- pse_mc_read_resp_t *p_mc_resp
- = (pse_mc_read_resp_t *)p_resp_msg->payload;
- sgx_status_t status = SGX_SUCCESS;
- sgx_status_t ret = SGX_SUCCESS;
- int retry = RETRY_TIMES;
- do {
- sgx_spin_lock(&g_spin_lock);
- status = invoke_service_ocall(&ret,
- (uint8_t *)p_req_msg,
- PSE_READ_MC_REQ_SIZE,
- (uint8_t *)p_resp_msg,
- PSE_READ_MC_RESP_SIZE,
- DEFAULT_AESM_TIMEOUT);
- sgx_spin_unlock(&g_spin_lock);
- if(status != SGX_SUCCESS || ret != SGX_SUCCESS){
- if(SGX_ERROR_MC_NOT_FOUND != ret)
- status = SGX_ERROR_UNEXPECTED;
- else
- status = SGX_ERROR_MC_NOT_FOUND;
- continue;
- }
- if(p_mc_resp->resp_hdr.service_id != PSE_MC_SERVICE
- || p_mc_resp->resp_hdr.service_cmd != PSE_MC_READ
- || p_mc_resp->resp_hdr.status != PSE_SUCCESS){
- if(PSE_ERROR_MC_NOT_FOUND == p_mc_resp->resp_hdr.status)
- status = SGX_ERROR_MC_NOT_FOUND;
- else
- status = SGX_ERROR_UNEXPECTED;
- } else {
- *p_counter_value = p_mc_resp->counter_value;
- status = SGX_SUCCESS;
- break;
- }
- } while(retry--);
- free(p_req_msg);
- free(p_resp_msg);
- return status;
- }
- sgx_status_t sgx_create_monotonic_counter(
- sgx_mc_uuid_t *p_counter_uuid,
- uint32_t *p_counter_value)
- {
- //Default attribute mask
- sgx_attributes_t attr_mask;
- attr_mask.flags = DEFAULT_VMC_ATTRIBUTE_MASK;
- attr_mask.xfrm = DEFAULT_VMC_XFRM_MASK;
- return sgx_create_monotonic_counter_ex(MC_POLICY_SIGNER,
- &attr_mask,
- p_counter_uuid,
- p_counter_value);
- }
- sgx_status_t sgx_destroy_monotonic_counter(const sgx_mc_uuid_t *p_counter_uuid)
- {
- if(!p_counter_uuid){
- return SGX_ERROR_INVALID_PARAMETER;
- }
- if (!g_b_session_established){
- return SGX_ERROR_AE_SESSION_INVALID;
- }
- pse_message_t *p_req_msg = (pse_message_t *)malloc(PSE_DEL_MC_REQ_SIZE);
- if(!p_req_msg){
- return SGX_ERROR_OUT_OF_MEMORY;
- }
- pse_message_t *p_resp_msg = (pse_message_t *)malloc(PSE_DEL_MC_RESP_SIZE);
- if(!p_resp_msg){
- free(p_req_msg);
- return SGX_ERROR_OUT_OF_MEMORY;
- }
- p_req_msg->exp_resp_size = sizeof(pse_mc_del_resp_t);
- p_req_msg->payload_size = sizeof(pse_mc_del_req_t);
- pse_mc_del_req_t *p_mc_req
- = (pse_mc_del_req_t *)p_req_msg->payload;
- memcpy(p_mc_req->counter_id,
- p_counter_uuid->counter_id,
- sizeof(p_mc_req->counter_id));
- memcpy(p_mc_req->nonce,
- p_counter_uuid->nonce,
- sizeof(p_mc_req->nonce));
- p_mc_req->req_hdr.service_id = PSE_MC_SERVICE;
- p_mc_req->req_hdr.service_cmd = PSE_MC_DEL;
- pse_mc_del_resp_t *p_mc_resp
- = (pse_mc_del_resp_t *)p_resp_msg->payload;
- sgx_status_t status = SGX_SUCCESS;
- sgx_status_t ret = SGX_SUCCESS;
- int retry = RETRY_TIMES;
- do {
- status = invoke_service_ocall(&ret,
- (uint8_t *)p_req_msg,
- PSE_DEL_MC_REQ_SIZE,
- (uint8_t *)p_resp_msg,
- PSE_DEL_MC_RESP_SIZE,
- DEFAULT_AESM_TIMEOUT);
- if(status != SGX_SUCCESS || ret != SGX_SUCCESS){
- if(SGX_ERROR_MC_NOT_FOUND != ret)
- status = SGX_ERROR_UNEXPECTED;
- else
- status = SGX_ERROR_MC_NOT_FOUND;
- continue;
- }
- if(p_mc_resp->resp_hdr.service_id != PSE_MC_SERVICE
- || p_mc_resp->resp_hdr.service_cmd != PSE_MC_DEL
- || p_mc_resp->resp_hdr.status != PSE_SUCCESS){
- if(PSE_ERROR_MC_NOT_FOUND == p_mc_resp->resp_hdr.status)
- status = SGX_ERROR_MC_NOT_FOUND;
- else
- status = SGX_ERROR_UNEXPECTED;
- } else {
- status = SGX_SUCCESS;
- break;
- }
- } while(retry--);
- free(p_req_msg);
- free(p_resp_msg);
- return status;
- }
|