PSDAService.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. /*
  2. * Copyright (C) 2011-2017 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 "PSDAService.h"
  32. #include <exception>
  33. #include <limits.h>
  34. #include "util.h"
  35. #include "se_string.h"
  36. #define PSDA_FILE_NAME "PSDA.dalp"
  37. static const char* g_psda_id = "cbede6f96ce4439ca1c76e2087786616";
  38. PSDAService::PSDAService(void)
  39. {
  40. jhi_handle = NULL;
  41. psda_session_handle = NULL;
  42. psda_svn = 0;
  43. csme_gid = 0;
  44. }
  45. PSDAService::~PSDAService(void)
  46. {
  47. stop_service();
  48. }
  49. bool PSDAService::start_service()
  50. {
  51. // session is active
  52. if (is_session_active())
  53. return true;
  54. for (int i = 0; i < AESM_RETRY_COUNT; i++)
  55. {
  56. if (!start_service_internal())
  57. {
  58. if (!is_session_active())
  59. {
  60. // session is invalid, maybe caused by power event. continue to RETRY
  61. continue;
  62. }
  63. else
  64. {
  65. // session is active , will not retry
  66. return false;
  67. }
  68. }
  69. else
  70. {
  71. // start service successfully
  72. return true;
  73. }
  74. }
  75. return false;
  76. }
  77. bool PSDAService::start_service_internal()
  78. {
  79. bool retVal = true;
  80. SGX_DBGPRINT_PRINT_ANSI_STRING(__FUNCTION__);
  81. JHI_RET jhi_ret = JHI_UNKNOWN_ERROR;
  82. __try {
  83. do {
  84. // Close JHI session
  85. if (jhi_handle != NULL && psda_session_handle != NULL)
  86. {
  87. JHI_CloseSession(jhi_handle, &psda_session_handle);
  88. psda_session_handle = NULL;
  89. }
  90. if (jhi_handle == NULL)
  91. {
  92. // Initialize PSDA
  93. if ((jhi_ret = JHI_Initialize(&jhi_handle, NULL, 0)) != JHI_SUCCESS)
  94. {
  95. AESM_DBG_ERROR("JHI_Initialize() failed. The return value is %d", jhi_ret);
  96. retVal = false;
  97. break;
  98. }
  99. else
  100. {
  101. // get PSDA full path
  102. TCHAR psda_path[MAX_PATH] = {0};
  103. if(aesm_get_pathname(FT_PERSISTENT_STORAGE, PSDA_FID, psda_path, MAX_PATH)!=AE_SUCCESS)
  104. {
  105. retVal = false;
  106. break;
  107. }
  108. else
  109. {
  110. // install the PSDA
  111. jhi_ret = JHI_Install2(jhi_handle, g_psda_id, psda_path);
  112. if (jhi_ret != JHI_SUCCESS)
  113. {
  114. AESM_DBG_ERROR("Failed to install PSDA. JHI_Install2() returned %d", jhi_ret);
  115. retVal = false;
  116. break;
  117. }
  118. // get the psda svn and keep it in memory
  119. if (!save_current_psda_svn())
  120. {
  121. AESM_DBG_ERROR("Failed to get PSDA SVN.");
  122. retVal = false;
  123. break;
  124. }
  125. }
  126. }
  127. }
  128. // Create JHI session
  129. if ((jhi_ret = JHI_CreateSession(jhi_handle, g_psda_id, 0, NULL, &psda_session_handle)) != JHI_SUCCESS)
  130. {
  131. AESM_DBG_ERROR("Failed to create session. JHI_CreateSession() returned %d", jhi_ret);
  132. retVal = false;
  133. break;
  134. }
  135. #if defined(DAL_DIAGNOSTICS)
  136. JVM_COMM_BUFFER appletProperty;
  137. char rxBuf[1000];
  138. appletProperty.RxBuf->buffer = rxBuf;
  139. appletProperty.RxBuf->length = sizeof(rxBuf);
  140. //
  141. // all this to get rid of const-ness of g_psda_id,
  142. // required by JHI_GetAppletProperty
  143. //
  144. unsigned len = strlen(g_psda_id) + 1;
  145. char* tempId = (char*) malloc(len);
  146. if (NULL != tempId)
  147. {
  148. strcpy_s(tempId, len, g_psda_id);
  149. char* txBuf = "security.version";
  150. appletProperty.TxBuf->buffer = txBuf;
  151. appletProperty.TxBuf->length = sizeof(*txBuf)*(strlen(txBuf)+1);
  152. JHI_RET jhiRet = JHI_GetAppletProperty(jhi_handle, tempId, &appletProperty);
  153. long tempSvn = strtol(rxBuf, NULL, 10);
  154. if (!(LONG_MIN == tempSvn || LONG_MAX == tempSvn || 0 == tempSvn))
  155. {
  156. SGX_DBGPRINT_ONE_STRING_ONE_INT("psdaSvn = ", tempSvn);
  157. }
  158. memset(rxBuf, 0xCC, sizeof(rxBuf));
  159. txBuf = "applet.name";
  160. appletProperty.TxBuf->buffer = txBuf;
  161. appletProperty.TxBuf->length = sizeof(*txBuf)*(strlen(txBuf)+1);
  162. appletProperty.RxBuf->length = sizeof(rxBuf);
  163. jhiRet = JHI_GetAppletProperty(jhi_handle, tempId, &appletProperty);
  164. memset(rxBuf, 0xCC, sizeof(rxBuf));
  165. txBuf = "applet.vendor";
  166. appletProperty.TxBuf->buffer = txBuf;
  167. appletProperty.TxBuf->length = sizeof(*txBuf)*(strlen(txBuf)+1);
  168. appletProperty.RxBuf->length = sizeof(rxBuf);
  169. jhiRet = JHI_GetAppletProperty(jhi_handle, tempId, &appletProperty);
  170. memset(rxBuf, 0xCC, sizeof(rxBuf));
  171. txBuf = "applet.description";
  172. appletProperty.TxBuf->buffer = txBuf;
  173. appletProperty.TxBuf->length = sizeof(*txBuf)*(strlen(txBuf)+1);
  174. appletProperty.RxBuf->length = sizeof(rxBuf);
  175. jhiRet = JHI_GetAppletProperty(jhi_handle, tempId, &appletProperty);
  176. memset(rxBuf, 0xCC, sizeof(rxBuf));
  177. txBuf = "applet.version";
  178. appletProperty.TxBuf->buffer = txBuf;
  179. appletProperty.TxBuf->length = sizeof(*txBuf)*(strlen(txBuf)+1);
  180. appletProperty.RxBuf->length = sizeof(rxBuf);
  181. jhiRet = JHI_GetAppletProperty(jhi_handle, tempId, &appletProperty);
  182. memset(rxBuf, 0xCC, sizeof(rxBuf));
  183. txBuf = "applet.flash.quota";
  184. appletProperty.TxBuf->buffer = txBuf;
  185. appletProperty.TxBuf->length = sizeof(*txBuf)*(strlen(txBuf)+1);
  186. appletProperty.RxBuf->length = sizeof(rxBuf);
  187. jhiRet = JHI_GetAppletProperty(jhi_handle, tempId, &appletProperty);
  188. memset(rxBuf, 0xCC, sizeof(rxBuf));
  189. txBuf = "applet.debug.enable";
  190. appletProperty.TxBuf->buffer = txBuf;
  191. appletProperty.TxBuf->length = sizeof(*txBuf)*(strlen(txBuf)+1);
  192. appletProperty.RxBuf->length = sizeof(rxBuf);
  193. jhiRet = JHI_GetAppletProperty(jhi_handle, tempId, &appletProperty);
  194. memset(rxBuf, 0xCC, sizeof(rxBuf));
  195. txBuf = "applet.platform";
  196. appletProperty.TxBuf->buffer = txBuf;
  197. appletProperty.TxBuf->length = sizeof(*txBuf)*(strlen(txBuf)+1);
  198. appletProperty.RxBuf->length = sizeof(rxBuf);
  199. jhiRet = JHI_GetAppletProperty(jhi_handle, tempId, &appletProperty);
  200. memset(rxBuf, 0xCC, sizeof(rxBuf));
  201. }
  202. #endif
  203. }
  204. while(false);
  205. }
  206. __except(1) {
  207. // On windows 7, if JHI.dll cannot be found, an SEH exception will be raised
  208. return false;
  209. }
  210. SGX_DBGPRINT_PRINT_ANSI_STRING("PSDAService::start_service_internal() exit");
  211. return retVal;
  212. }
  213. void PSDAService::stop_service()
  214. {
  215. JHI_RET jhi_ret = JHI_UNKNOWN_ERROR;
  216. try {
  217. if (jhi_handle != NULL)
  218. {
  219. if (psda_session_handle != NULL)
  220. {
  221. if ((jhi_ret = JHI_CloseSession(jhi_handle, &psda_session_handle)) != JHI_SUCCESS)
  222. {
  223. AESM_DBG_ERROR("JHI_CloseSession returned %d", jhi_ret);
  224. }
  225. }
  226. if ((jhi_ret = JHI_Uninstall(jhi_handle, (char*)g_psda_id)) != JHI_SUCCESS)
  227. {
  228. AESM_DBG_ERROR("Failed to uninstall PSDA. The return value is %d ", jhi_ret);
  229. }
  230. if ((jhi_ret = JHI_Deinit(jhi_handle)) != JHI_SUCCESS)
  231. {
  232. AESM_DBG_ERROR("Failed to Deinit JHI. The return value is %d ", jhi_ret);
  233. }
  234. }
  235. psda_session_handle = NULL;
  236. jhi_handle = NULL;
  237. }
  238. catch (std::exception e)
  239. {
  240. }
  241. }
  242. ae_error_t PSDAService::send_and_recv(
  243. INT32 nCommandId,
  244. JVM_COMM_BUFFER* pComm,
  245. INT32* responseCode,
  246. session_loss_retry_flag_t flag)
  247. {
  248. int retry = AESM_RETRY_COUNT;
  249. while (retry > 0) {
  250. JHI_RET ret = JHI_SendAndRecv2(this->jhi_handle,
  251. this->psda_session_handle,
  252. nCommandId,
  253. pComm,
  254. responseCode);
  255. if (ret != JHI_SUCCESS) {
  256. if (ret == JHI_SERVICE_UNAVAILABLE || ret == JHI_INVALID_SESSION_HANDLE) {
  257. // session is lost, create session anyway
  258. if (!start_service_internal()) {
  259. return AESM_PSDA_NOT_AVAILABLE;
  260. }
  261. //
  262. if (flag == NO_RETRY_ON_SESSION_LOSS)
  263. return AESM_PSDA_SESSION_LOST;
  264. else {
  265. retry--;
  266. continue;
  267. }
  268. }
  269. else {
  270. return AESM_PSDA_INTERNAL_ERROR;
  271. }
  272. }
  273. return AE_SUCCESS;
  274. }
  275. return AESM_PSDA_INTERNAL_ERROR;
  276. }
  277. bool PSDAService::is_session_active()
  278. {
  279. try {
  280. if (jhi_handle != NULL && psda_session_handle != NULL)
  281. {
  282. JHI_SESSION_INFO session_info;
  283. if (JHI_GetSessionInfo(jhi_handle, psda_session_handle, &session_info) == JHI_SUCCESS
  284. && session_info.state == JHI_SESSION_STATE_ACTIVE)
  285. {
  286. // session is valid
  287. return true;
  288. }
  289. }
  290. return false;
  291. }
  292. catch (std::exception e)
  293. {
  294. return false;
  295. }
  296. }
  297. bool PSDAService::save_current_psda_svn()
  298. {
  299. bool retVal = false;
  300. JVM_COMM_BUFFER appletProperty;
  301. char rxBuf[1000];
  302. appletProperty.RxBuf->buffer = rxBuf;
  303. appletProperty.RxBuf->length = sizeof(rxBuf);
  304. char* txBuf = "security.version";
  305. appletProperty.TxBuf->buffer = txBuf;
  306. appletProperty.TxBuf->length = (UINT32)(sizeof(*txBuf)*(strlen(txBuf)+1));
  307. //
  308. // all this to get rid of const-ness of g_psda_id,
  309. // required by JHI_GetAppletProperty
  310. //
  311. unsigned len = strnlen_s(g_psda_id, 128) + 1;
  312. char* tempId = (char*) malloc(len);
  313. if (NULL != tempId)
  314. {
  315. strcpy_s(tempId, len, g_psda_id);
  316. JHI_RET jhiRet = JHI_GetAppletProperty(jhi_handle, tempId, &appletProperty);
  317. if (JHI_SUCCESS == jhiRet)
  318. {
  319. long tempSvn = strtol(rxBuf, NULL, 10);
  320. if (!(LONG_MIN == tempSvn || LONG_MAX == tempSvn || 0 == tempSvn))
  321. {
  322. retVal = true;
  323. psda_svn = tempSvn;
  324. SGX_DBGPRINT_ONE_STRING_ONE_INT("psdaSvn = ", tempSvn);
  325. }
  326. else
  327. {
  328. AESM_DBG_ERROR("Invalid PSDA security.version.");
  329. }
  330. }
  331. else
  332. {
  333. AESM_DBG_ERROR("Failed to get PSDA security.version.");
  334. }
  335. free(tempId);
  336. }
  337. return retVal;
  338. }