aesm_epid_blob.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /*
  2. * Copyright (C) 2011-2016 Intel Corporation. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. *
  8. * * Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * * Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in
  12. * the documentation and/or other materials provided with the
  13. * distribution.
  14. * * Neither the name of Intel Corporation nor the names of its
  15. * contributors may be used to endorse or promote products derived
  16. * from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  21. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  22. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  24. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  25. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  26. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  28. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. *
  30. */
  31. #include <stdio.h>
  32. #include "aesm_epid_blob.h"
  33. #include "internal/se_memcpy.h"
  34. #include "oal/oal.h"
  35. #include "byte_order.h"
  36. ae_error_t EPIDBlob::read(epid_blob_with_cur_psvn_t& blob)
  37. {
  38. ae_error_t ae_ret = AE_FAILURE;
  39. if(status == not_initialized){
  40. uint32_t data_size = sizeof(blob_cache);
  41. if((ae_ret=aesm_read_data(FT_PERSISTENT_STORAGE, EPID_DATA_BLOB_FID, reinterpret_cast<uint8_t *>(&blob_cache), &data_size))!=AE_SUCCESS){
  42. goto CLEANUP_READ_FILE;
  43. }
  44. if (data_size != sizeof(blob_cache)&&
  45. data_size != sizeof(epid_blob_v2_with_cur_psvn_t)){//support previous version epid blob too
  46. ae_ret = QE_EPIDBLOB_ERROR;
  47. goto CLEANUP_READ_FILE;
  48. }
  49. if(data_size == sizeof(epid_blob_v2_with_cur_psvn_t)){
  50. //move bk_platform_info_t so that all other parts of aesm service could access the field consistently
  51. epid_blob_v2_with_cur_psvn_t *old_format = (epid_blob_v2_with_cur_psvn_t *)&blob_cache;
  52. memmove(&blob_cache.cur_pi, &old_format->cur_pi, sizeof(old_format->cur_pi));
  53. }
  54. status = update_to_date;
  55. CLEANUP_READ_FILE:
  56. if(status!=update_to_date)
  57. status = not_available;//epid blob lost
  58. }
  59. if(status == update_to_date){
  60. if(memcpy_s(&blob, sizeof(blob), &blob_cache, sizeof(blob_cache))!=0){
  61. status = not_available; //invalid
  62. ae_ret = AE_FAILURE;
  63. }else{
  64. ae_ret = AE_SUCCESS;
  65. }
  66. }
  67. return ae_ret;
  68. }
  69. ae_error_t EPIDBlob::write(const epid_blob_with_cur_psvn_t& blob)
  70. {
  71. ae_error_t ae_ret = AE_FAILURE;
  72. status = not_available;
  73. if((ae_ret = aesm_write_data(FT_PERSISTENT_STORAGE, EPID_DATA_BLOB_FID,reinterpret_cast<const uint8_t *>(&blob), sizeof(blob)))!=AE_SUCCESS)
  74. {
  75. AESM_DBG_WARN("fail to write epid blob to persistent storage:%d",ae_ret);
  76. AESM_LOG_WARN("%s",g_event_string_table[SGX_EVENT_EPID_BLOB_PERSISTENT_STROAGE_FAILURE]);
  77. // continue to update cache
  78. }
  79. if(memcpy_s(&blob_cache, sizeof(blob_cache), &blob, sizeof(blob))!=0){
  80. status = not_available; //invalid status
  81. ae_ret = AE_FAILURE;
  82. }else{
  83. status = update_to_date;
  84. ae_ret = AE_SUCCESS;
  85. }
  86. return ae_ret;
  87. }
  88. //
  89. // get_sgx_gid
  90. //
  91. // get sgx gid from epid blob, specifically from stored group cert in epid blob
  92. //
  93. // inputs
  94. //
  95. // pgid: pointer to gid
  96. //
  97. // outputs
  98. //
  99. // *pgid: gid
  100. // status
  101. //
  102. ae_error_t EPIDBlob::get_sgx_gid(uint32_t* pgid)
  103. {
  104. ae_error_t aesm_result = AE_SUCCESS;
  105. epid_blob_with_cur_psvn_t epid_blob;
  106. sgx_sealed_data_t *sealed_epid = reinterpret_cast<sgx_sealed_data_t *>(epid_blob.trusted_epid_blob);
  107. if (NULL == pgid)
  108. return AE_INVALID_PARAMETER;
  109. //
  110. // get the epid blob
  111. //
  112. aesm_result = this->read(epid_blob);
  113. if (AE_SUCCESS == aesm_result) {
  114. //
  115. // get the gid
  116. //
  117. uint32_t plain_text_offset = sealed_epid->plain_text_offset;
  118. se_plaintext_epid_data_sdk_t* plain_text = reinterpret_cast<se_plaintext_epid_data_sdk_t *>(epid_blob.trusted_epid_blob + sizeof(sgx_sealed_data_t) + plain_text_offset);
  119. if(memcpy_s(pgid, sizeof(*pgid), &plain_text->epid_group_cert.gid, sizeof(plain_text->epid_group_cert.gid))) //read gid from EPID Data blob
  120. {
  121. AESM_DBG_ERROR("memcpy_s failed");
  122. aesm_result = AE_FAILURE;
  123. }
  124. else
  125. {
  126. //
  127. // return little-endian
  128. //
  129. *pgid = _htonl(*pgid);
  130. AESM_DBG_TRACE(": get gid %d from epid blob", *pgid);
  131. }
  132. }
  133. else {
  134. aesm_result = AE_INVALID_PARAMETER;
  135. }
  136. return aesm_result;
  137. }
  138. ae_error_t EPIDBlob::get_extended_epid_group_id(uint32_t* pxeid)
  139. {
  140. ae_error_t aesm_result = AE_SUCCESS;
  141. epid_blob_with_cur_psvn_t epid_blob;
  142. sgx_sealed_data_t *sealed_epid = reinterpret_cast<sgx_sealed_data_t *>(epid_blob.trusted_epid_blob);
  143. if (NULL == pxeid)
  144. return AE_INVALID_PARAMETER;
  145. //
  146. // get the epid blob
  147. //
  148. aesm_result = this->read(epid_blob);
  149. if (AE_SUCCESS == aesm_result) {
  150. //
  151. // get the xeid
  152. //
  153. uint32_t plain_text_offset = sealed_epid->plain_text_offset;
  154. se_plaintext_epid_data_sdk_t* plain_text_new = reinterpret_cast<se_plaintext_epid_data_sdk_t*>(epid_blob.trusted_epid_blob + sizeof(sgx_sealed_data_t)+plain_text_offset);
  155. se_plaintext_epid_data_sik_t *plain_text_old = reinterpret_cast<se_plaintext_epid_data_sik_t *>(plain_text_new);
  156. switch (plain_text_new->epid_key_version)
  157. {
  158. case EPID_KEY_BLOB_VERSION_SDK:
  159. if (memcpy_s(pxeid, sizeof(*pxeid), &plain_text_new->xeid, sizeof(plain_text_new->xeid))) //read extended_epid_group_id from EPID Data blob
  160. {
  161. AESM_DBG_ERROR("memcpy_s failed");
  162. aesm_result = AE_FAILURE;
  163. }
  164. else
  165. {
  166. //
  167. // return little-endian
  168. //
  169. AESM_DBG_TRACE(": get gid %d from epid blob", *pxeid);
  170. aesm_result = AE_SUCCESS;
  171. }
  172. break;
  173. case EPID_KEY_BLOB_VERSION_SIK:
  174. if (memcpy_s(pxeid, sizeof(*pxeid), &plain_text_old->xeid, sizeof(plain_text_old->xeid))) //read extended_epid_group_id from EPID Data blob
  175. {
  176. AESM_DBG_ERROR("memcpy_s failed");
  177. aesm_result = AE_FAILURE;
  178. }
  179. else
  180. {
  181. //
  182. // return little-endian
  183. //
  184. AESM_DBG_TRACE(": get gid %d from epid blob", *pxeid);
  185. aesm_result = AE_SUCCESS;
  186. }
  187. break;
  188. default:
  189. AESM_DBG_ERROR("unexpected epid_key_version");
  190. aesm_result = AE_FAILURE;
  191. break;
  192. }
  193. }
  194. return aesm_result;
  195. }
  196. ae_error_t EPIDBlob::remove(void)
  197. {
  198. ae_error_t ae_ret = AE_FAILURE;
  199. status = not_available;
  200. if ((ae_ret = aesm_remove_data(FT_PERSISTENT_STORAGE, EPID_DATA_BLOB_FID)) != AE_SUCCESS){
  201. status = not_initialized;
  202. return ae_ret;
  203. }
  204. status = not_initialized;
  205. return AE_SUCCESS;
  206. }