aesm_epid_blob.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. ae_ret = QE_EPIDBLOB_ERROR;
  46. goto CLEANUP_READ_FILE;
  47. }
  48. status = update_to_date;
  49. CLEANUP_READ_FILE:
  50. if(status!=update_to_date)
  51. status = not_available;//epid blob lost
  52. }
  53. if(status == update_to_date){
  54. if(memcpy_s(&blob, sizeof(blob), &blob_cache, sizeof(blob_cache))!=0){
  55. status = not_available; //invalid
  56. ae_ret = AE_FAILURE;
  57. }else{
  58. ae_ret = AE_SUCCESS;
  59. }
  60. }
  61. return ae_ret;
  62. }
  63. ae_error_t EPIDBlob::write(const epid_blob_with_cur_psvn_t& blob)
  64. {
  65. ae_error_t ae_ret = AE_FAILURE;
  66. status = not_available;
  67. if((ae_ret = aesm_write_data(FT_PERSISTENT_STORAGE, EPID_DATA_BLOB_FID,reinterpret_cast<const uint8_t *>(&blob), sizeof(blob)))!=AE_SUCCESS)
  68. {
  69. AESM_DBG_WARN("fail to write epid blob to persistent storage:%d",ae_ret);
  70. AESM_LOG_WARN("%s",g_event_string_table[SGX_EVENT_EPID_BLOB_PERSISTENT_STROAGE_FAILURE]);
  71. // continue to update cache
  72. }
  73. if(memcpy_s(&blob_cache, sizeof(blob_cache), &blob, sizeof(blob))!=0){
  74. status = not_available; //invalid status
  75. ae_ret = AE_FAILURE;
  76. }else{
  77. status = update_to_date;
  78. ae_ret = AE_SUCCESS;
  79. }
  80. return ae_ret;
  81. }
  82. //
  83. // get_sgx_gid
  84. //
  85. // get sgx gid from epid blob, specifically from stored group cert in epid blob
  86. //
  87. // inputs
  88. //
  89. // pgid: pointer to gid
  90. //
  91. // outputs
  92. //
  93. // *pgid: gid
  94. // status
  95. //
  96. ae_error_t EPIDBlob::get_sgx_gid(uint32_t* pgid)
  97. {
  98. ae_error_t aesm_result = AE_SUCCESS;
  99. epid_blob_with_cur_psvn_t epid_blob;
  100. sgx_sealed_data_t *sealed_epid = reinterpret_cast<sgx_sealed_data_t *>(epid_blob.trusted_epid_blob);
  101. if (NULL == pgid)
  102. return AE_INVALID_PARAMETER;
  103. //
  104. // get the epid blob
  105. //
  106. aesm_result = this->read(epid_blob);
  107. if (AE_SUCCESS == aesm_result) {
  108. //
  109. // get the gid
  110. //
  111. uint32_t plain_text_offset = sealed_epid->plain_text_offset;
  112. se_plaintext_epid_data_pak_t* plain_text = reinterpret_cast<se_plaintext_epid_data_pak_t *>(epid_blob.trusted_epid_blob + sizeof(sgx_sealed_data_t) + plain_text_offset);
  113. 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
  114. {
  115. AESM_DBG_ERROR("memcpy_s failed");
  116. aesm_result = AE_FAILURE;
  117. }
  118. else
  119. {
  120. //
  121. // return little-endian
  122. //
  123. *pgid = _htonl(*pgid);
  124. AESM_DBG_TRACE(": get gid %d from epid blob", *pgid);
  125. }
  126. }
  127. else {
  128. aesm_result = AE_INVALID_PARAMETER;
  129. }
  130. return aesm_result;
  131. }
  132. ae_error_t EPIDBlob::get_extended_epid_group_id(uint32_t* pxeid)
  133. {
  134. ae_error_t aesm_result = AE_SUCCESS;
  135. epid_blob_with_cur_psvn_t epid_blob;
  136. sgx_sealed_data_t *sealed_epid = reinterpret_cast<sgx_sealed_data_t *>(epid_blob.trusted_epid_blob);
  137. if (NULL == pxeid)
  138. return AE_INVALID_PARAMETER;
  139. //
  140. // get the epid blob
  141. //
  142. aesm_result = this->read(epid_blob);
  143. if (AE_SUCCESS == aesm_result) {
  144. //
  145. // get the xeid
  146. //
  147. uint32_t plain_text_offset = sealed_epid->plain_text_offset;
  148. se_plaintext_epid_data_pak_t* plain_text_new = reinterpret_cast<se_plaintext_epid_data_pak_t*>(epid_blob.trusted_epid_blob + sizeof(sgx_sealed_data_t)+plain_text_offset);
  149. switch (plain_text_new->epid_key_version)
  150. {
  151. case EPID_KEY_BLOB_VERSION_PAK:
  152. if (memcpy_s(pxeid, sizeof(*pxeid), &plain_text_new->xeid, sizeof(plain_text_new->xeid))) //read extended_epid_group_id from EPID Data blob
  153. {
  154. AESM_DBG_ERROR("memcpy_s failed");
  155. aesm_result = AE_FAILURE;
  156. }
  157. else
  158. {
  159. //
  160. // return little-endian
  161. //
  162. AESM_DBG_TRACE(": get gid %d from epid blob", *pxeid);
  163. aesm_result = AE_SUCCESS;
  164. }
  165. break;
  166. default:
  167. AESM_DBG_ERROR("unexpected epid_key_version");
  168. aesm_result = AE_FAILURE;
  169. break;
  170. }
  171. }
  172. return aesm_result;
  173. }
  174. ae_error_t EPIDBlob::remove(void)
  175. {
  176. ae_error_t ae_ret = AE_FAILURE;
  177. status = not_available;
  178. if ((ae_ret = aesm_remove_data(FT_PERSISTENT_STORAGE, EPID_DATA_BLOB_FID)) != AE_SUCCESS){
  179. status = not_initialized;
  180. return ae_ret;
  181. }
  182. status = not_initialized;
  183. return AE_SUCCESS;
  184. }