PSDAService.cpp 13 KB

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