interface_psda.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. /*
  2. * Copyright (C) 2011-2018 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 "interface_psda.h"
  32. #include "Buffer.h"
  33. #include "byte_order.h"
  34. #include "PSDAService.h"
  35. #include "pse_types.h"
  36. //
  37. // need to fix this
  38. //
  39. #define PSDA_COMMAND_LT 1
  40. #define BREAK_IF_TRUE(x, Sts, ErrCode) if (x) { Sts = ErrCode; break; }
  41. #define BREAK_IF_FALSE(x, Sts, ErrCode) if (!(x)) { Sts = ErrCode; break; }
  42. using namespace std;
  43. static JVM_COMM_BUFFER commBuf_s1, commBuf_s3;
  44. static INT32 responseCode;
  45. #pragma pack(1)
  46. typedef struct _pse_cse_lt_msg2_t
  47. {
  48. uint8_t s1[104];
  49. }pse_cse_lt_msg2_t;
  50. typedef struct _pse_cse_msg7_t
  51. {
  52. uint8_t s2[0];
  53. }pse_cse_msg7_t;
  54. typedef struct _pse_cse_msg8_t
  55. {
  56. uint8_t s3[0];
  57. }pse_cse_msg8_t;
  58. typedef struct _lt_session_m1_t
  59. {
  60. psda_msg_hdr_t msg_hdr;
  61. }lt_session_m1_t;
  62. typedef struct _lt_session_m2_t
  63. {
  64. psda_msg_hdr_t msg_hdr;
  65. pse_cse_lt_msg2_t msg2;
  66. }lt_session_m2_t;
  67. typedef struct _lt_session_m7_t
  68. {
  69. psda_msg_hdr_t msg_hdr;
  70. pse_cse_msg7_t msg7;
  71. }lt_session_m7_t;
  72. typedef struct _lt_session_m8_t
  73. {
  74. psda_msg_hdr_t msg_hdr;
  75. pse_cse_msg8_t msg8;
  76. }lt_session_m8_t;
  77. #pragma pack()
  78. pse_pr_interface_psda::pse_pr_interface_psda(void)
  79. {
  80. }
  81. pse_pr_interface_psda::~pse_pr_interface_psda(void)
  82. {
  83. }
  84. ae_error_t pse_pr_interface_psda::GetS1(/*in*/const uint8_t* pse_instance_id, /*out*/ upse::Buffer& s1)
  85. {
  86. ae_error_t status = AE_FAILURE;
  87. lt_session_m1_t lt_session_m1;
  88. lt_session_m2_t lt_session_m2;
  89. do
  90. {
  91. // endian-ness doesn't matter for these two; they're 0
  92. memcpy_s(lt_session_m1.msg_hdr.pse_instance_id, SW_INSTANCE_ID_SIZE, pse_instance_id, SW_INSTANCE_ID_SIZE);
  93. lt_session_m1.msg_hdr.msg_type = PSDA_MSG_TYPE_LT_M1;
  94. lt_session_m1.msg_hdr.msg_len = 0;
  95. memset(&lt_session_m2, 0, sizeof(lt_session_m2));
  96. commBuf_s1.TxBuf->buffer = &lt_session_m1;
  97. commBuf_s1.TxBuf->length = sizeof(lt_session_m1_t);
  98. commBuf_s1.RxBuf->buffer = &lt_session_m2;
  99. commBuf_s1.RxBuf->length = sizeof(lt_session_m2_t);
  100. status = PSDAService::instance().send_and_recv(
  101. PSDA_COMMAND_LT,
  102. &commBuf_s1,
  103. &responseCode,
  104. AUTO_RETRY_ON_SESSION_LOSS);
  105. AESM_DBG_INFO("JHI_SendAndRecv2 response_code is %d", responseCode);
  106. if (status != AE_SUCCESS) {
  107. AESM_LOG_ERROR("%s", g_event_string_table[SGX_EVENT_DAL_COMM_FAILURE]);
  108. break;
  109. }
  110. if (PSDA_SUCCESS != responseCode) {
  111. AESM_LOG_ERROR("%s", g_event_string_table[SGX_EVENT_DAL_SIGMA_ERROR]);
  112. BREAK_IF_TRUE( (responseCode == PSDA_NOT_PROVISIONED), status, AESM_PSDA_NOT_PROVISONED_ERROR);
  113. BREAK_IF_TRUE( (responseCode == PSDA_PROTOCOL_NOT_SUPPORTED), status, AESM_PSDA_PROTOCOL_NOT_SUPPORTED);
  114. BREAK_IF_TRUE( (responseCode == PSDA_INTERNAL_ERROR) , status, AESM_PSDA_INTERNAL_ERROR);
  115. BREAK_IF_TRUE( (responseCode == PSDA_PERSISTENT_DATA_WRITE_THROTTLED) , status, AESM_PSDA_WRITE_THROTTLED);
  116. }
  117. uint32_t msg_len = _ntohl(lt_session_m2.msg_hdr.msg_len);
  118. uint32_t msg_type = _ntohl(lt_session_m2.msg_hdr.msg_type);
  119. if (responseCode != PSDA_SUCCESS || msg_type != PSDA_MSG_TYPE_LT_M2 || msg_len != sizeof(pse_cse_lt_msg2_t))
  120. {
  121. status = AE_FAILURE;
  122. break;
  123. }
  124. if (commBuf_s1.RxBuf->length <= sizeof(lt_session_m2.msg_hdr)
  125. || msg_len != commBuf_s1.RxBuf->length - sizeof(lt_session_m2.msg_hdr))
  126. {
  127. AESM_DBG_INFO("Received invalid S1 message from PSDA!");
  128. status = AE_FAILURE;
  129. break;
  130. }
  131. status = s1.Alloc(msg_len);
  132. BREAK_IF_FAILED(status);
  133. upse::BufferWriter bw(s1);
  134. status = bw.writeRaw((UINT8*)&lt_session_m2.msg2, msg_len);
  135. BREAK_IF_FAILED(status);
  136. } while (0);
  137. return status;
  138. }
  139. ae_error_t pse_pr_interface_psda::ExchangeS2AndS3(/*in*/ const uint8_t* pse_instance_id,
  140. /*in */ const upse::Buffer& s2,
  141. /*out*/ upse::Buffer& s3)
  142. {
  143. ae_error_t status = AE_FAILURE;
  144. lt_session_m7_t* pLt_session_m7 = NULL;
  145. lt_session_m8_t* pLt_session_m8 = NULL;
  146. uint32_t lt_session_m7_size = static_cast<uint32_t>(sizeof(lt_session_m7_t) + s2.getSize());
  147. uint32_t lt_session_m8_size = 10000;
  148. do
  149. {
  150. pLt_session_m7 = (lt_session_m7_t*)malloc(lt_session_m7_size);
  151. BREAK_IF_TRUE( (NULL == pLt_session_m7), status, AESM_PSE_PR_INSUFFICIENT_MEMORY_ERROR);
  152. pLt_session_m8 = (lt_session_m8_t*)malloc(lt_session_m8_size);
  153. BREAK_IF_TRUE( (NULL == pLt_session_m8), status, AESM_PSE_PR_INSUFFICIENT_MEMORY_ERROR);
  154. memset(pLt_session_m8, 0, lt_session_m8_size);
  155. memcpy_s(pLt_session_m7->msg_hdr.pse_instance_id, SW_INSTANCE_ID_SIZE, pse_instance_id, SW_INSTANCE_ID_SIZE);
  156. pLt_session_m7->msg_hdr.msg_type = _htonl(PSDA_MSG_TYPE_LT_M7);
  157. pLt_session_m7->msg_hdr.msg_len = _htonl(s2.getSize());
  158. memcpy_s(&pLt_session_m7->msg7, s2.getSize(), s2.getData(), s2.getSize());
  159. commBuf_s3.TxBuf->buffer = pLt_session_m7;
  160. commBuf_s3.TxBuf->length = lt_session_m7_size;
  161. commBuf_s3.RxBuf->buffer = pLt_session_m8;
  162. commBuf_s3.RxBuf->length = lt_session_m8_size;
  163. status = PSDAService::instance().send_and_recv(
  164. PSDA_COMMAND_LT,
  165. &commBuf_s3,
  166. &responseCode,
  167. NO_RETRY_ON_SESSION_LOSS);
  168. AESM_DBG_INFO("JHI_SendAndRecv2 response_code is %d", responseCode);
  169. if (status != AE_SUCCESS) {
  170. AESM_LOG_ERROR("%s", g_event_string_table[SGX_EVENT_DAL_COMM_FAILURE]);
  171. break;
  172. }
  173. if (PSDA_SUCCESS != responseCode) {
  174. AESM_LOG_ERROR("%s", g_event_string_table[SGX_EVENT_DAL_SIGMA_ERROR]);
  175. BREAK_IF_TRUE( (responseCode == PSDA_INTEGRITY_ERROR), status, AESM_PSDA_LT_SESSION_INTEGRITY_ERROR);
  176. BREAK_IF_TRUE( (responseCode == PSDA_INTERNAL_ERROR) , status, AESM_PSDA_INTERNAL_ERROR);
  177. BREAK_IF_TRUE( (responseCode == PSDA_PLATFORM_KEYS_REVOKED) , status, AESM_PSDA_PLATFORM_KEYS_REVOKED);
  178. BREAK_IF_TRUE( (responseCode == PSDA_PERSISTENT_DATA_WRITE_THROTTLED) , status, AESM_PSDA_WRITE_THROTTLED);
  179. BREAK_IF_TRUE( (responseCode != PSDA_SUCCESS) , status, AESM_PSDA_INTERNAL_ERROR);
  180. }
  181. uint32_t msg_len = _ntohl(pLt_session_m8->msg_hdr.msg_len);
  182. uint32_t msg_type = _ntohl(pLt_session_m8->msg_hdr.msg_type);
  183. if (msg_type != PSDA_MSG_TYPE_LT_M8)
  184. {
  185. status = AE_FAILURE;
  186. break;
  187. }
  188. if (commBuf_s3.RxBuf->length <= sizeof(pLt_session_m8->msg_hdr)
  189. || msg_len != commBuf_s3.RxBuf->length - sizeof(pLt_session_m8->msg_hdr))
  190. {
  191. AESM_DBG_INFO("Received invalid S3 message from PSDA!");
  192. status = AE_FAILURE;
  193. break;
  194. }
  195. status = s3.Alloc(msg_len);
  196. BREAK_IF_FAILED(status);
  197. upse::BufferWriter bw(s3);
  198. status = bw.writeRaw((UINT8*)&pLt_session_m8->msg8, msg_len);
  199. BREAK_IF_FAILED(status);
  200. status = AE_SUCCESS;
  201. } while (0);
  202. if (NULL != pLt_session_m7)
  203. free(pLt_session_m7);
  204. if (NULL != pLt_session_m8)
  205. free(pLt_session_m8);
  206. return status;
  207. }
  208. ae_error_t pse_pr_interface_psda::get_csme_gid(EPID_GID* p_cse_gid)
  209. {
  210. if (p_cse_gid == NULL)
  211. {
  212. AESM_DBG_ERROR("input p_cse_gid is NULL");
  213. return AE_FAILURE;
  214. }
  215. psda_info_query_msg_t psda_cert_query_msg;
  216. psda_cert_query_msg.msg_hdr.msg_type = _htonl(PSDA_MSG_TYPE_CERT_INFO_QUERY);
  217. psda_cert_query_msg.msg_hdr.msg_len = 0;
  218. psda_cert_result_msg_t psda_cert_result_msg;
  219. memset(&psda_cert_result_msg, 0, sizeof(psda_cert_result_msg_t));
  220. JVM_COMM_BUFFER commBuf;
  221. commBuf.TxBuf->buffer = &psda_cert_query_msg;
  222. commBuf.TxBuf->length = sizeof(psda_info_query_msg_t);
  223. commBuf.RxBuf->buffer = &psda_cert_result_msg;
  224. commBuf.RxBuf->length = sizeof(psda_cert_result_msg_t);
  225. int response_code;
  226. ae_error_t status = PSDAService::instance().send_and_recv(
  227. PSDA_COMMAND_INFO,
  228. &commBuf,
  229. &response_code,
  230. AUTO_RETRY_ON_SESSION_LOSS);
  231. if (status != AE_SUCCESS || response_code != 0) {
  232. AESM_DBG_ERROR("JHI_SendAndRecv2 ret %d, response_code %d",status, response_code);
  233. AESM_LOG_ERROR("%s", g_event_string_table[SGX_EVENT_DAL_COMM_FAILURE]);
  234. if (response_code == PSDA_NOT_PROVISIONED)
  235. return AESM_PSDA_NOT_PROVISONED_ERROR;
  236. return AE_FAILURE;
  237. }
  238. if (_ntohl(psda_cert_result_msg.msg_hdr.msg_type) != PSDA_MSG_TYPE_CERT_INFO_RESULT
  239. || _ntohl(psda_cert_result_msg.msg_hdr.msg_len) != 24) {
  240. AESM_DBG_ERROR("msg_type %d, msg_len %d while expected value type %d, len %d",
  241. _ntohl(psda_cert_result_msg.msg_hdr.msg_type), _ntohl(psda_cert_result_msg.msg_hdr.msg_len),
  242. PSDA_MSG_TYPE_CERT_INFO_RESULT, 24);
  243. return AE_FAILURE;
  244. }
  245. memcpy_s(p_cse_gid, sizeof(EPID_GID), psda_cert_result_msg.cert_info, sizeof(EPID_GID));
  246. return AE_SUCCESS;
  247. }