u_long_term_pairing.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  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 "u_long_term_pairing.h"
  32. #include <cstddef>
  33. #include "helper.h"
  34. #include "uecall_bridge.h"
  35. #include <Buffer.h>
  36. #include "interface_psda.h"
  37. #include "pse_pr_sigma_1_1_defs.h"
  38. #include "sigma_helper.h"
  39. #include "aeerror.h"
  40. #include <list>
  41. #include "oal/oal.h"
  42. #include "PSEPRClass.h"
  43. #include "pairing_blob.h"
  44. #include "byte_order.h"
  45. #if defined(_DEBUG)
  46. #include "PSDAService.h"
  47. #include "aesm_epid_blob.h"
  48. #include "aesm_encode.h"
  49. #endif
  50. #define PSEPR_LOST_ENCLAVE_RETRY_COUNT 3
  51. extern uint32_t upse_iclsInit();
  52. // FLOW
  53. // Verifier Prover Intel Server
  54. // uRequestS1FromME |--M1: Start Pairing->| |
  55. // |<-M2: SIGMA S1-------| |
  56. // uGetR2 | | |
  57. // | | |
  58. // uLoadPairingBlob | | |
  59. // | | |
  60. // uGetSigRLFromServer |--M3: GID_cse || R2----------------->|
  61. // |<-M4: Sig_is(RL_cse || R2)-----------|
  62. // uGetOCSPResponseFromServer |--M5: OCSPReq----------------------->|
  63. // |<-M6: OCSPResp-----------------------|
  64. // | | |
  65. // tGenM7 (enclave call) Send S1, Receive S2
  66. // | | |
  67. // uExchangeS2AndS3WithME |--M7: SIGMA S2------>| |
  68. // |<-M8: SIGMA S3-------| |
  69. // uGetGroupIdFromME | | |
  70. // | | |
  71. // tVerifyM8 (enclave call) Send S3, Receive updated pairing blob
  72. // | | |
  73. // uSavePairingBlob | | |
  74. //*********************************************************************
  75. // Prototypes of static functions
  76. //*********************************************************************
  77. static ae_error_t DoLongTermPairing(bool* p_new_pairing);
  78. //*********************************************************************
  79. // Main engine routine for Long-Term Pairing
  80. //*********************************************************************
  81. ae_error_t create_sigma_long_term_pairing(bool* p_new_pairing)
  82. {
  83. ae_error_t status = AE_FAILURE;
  84. SGX_DBGPRINT_PRINT_ANSI_STRING("Begin Long Term Pairing");
  85. try
  86. {
  87. unsigned rcount = AESM_RETRY_COUNT;
  88. do
  89. {
  90. status = DoLongTermPairing(p_new_pairing);
  91. if(status == AESM_PSDA_NOT_PROVISONED_ERROR)
  92. {
  93. // retry CSE Provision
  94. if (upse_iclsInit() == 0)
  95. {
  96. rcount--;
  97. continue;
  98. }
  99. break;
  100. }
  101. if (status == PSE_PR_ENCLAVE_LOST_ERROR || status == AESM_PSDA_SESSION_LOST
  102. || status == AESM_PSDA_WRITE_THROTTLED)
  103. {
  104. //
  105. // went to sleep while in enclave
  106. // in this case (beginning of flow), we should just retry, after first destroying and then reloading
  107. // note that this code gets significantly more complicated if the PSE-pr ever becomes multi-threaded
  108. //
  109. if (status == PSE_PR_ENCLAVE_LOST_ERROR)
  110. {
  111. CPSEPRClass::instance().unload_enclave();
  112. if ((status = CPSEPRClass::instance().load_enclave()) != AE_SUCCESS)
  113. {
  114. if(status != AESM_AE_OUT_OF_EPC)
  115. status = AE_FAILURE;
  116. break;
  117. }
  118. SaveEnclaveID(CPSEPRClass::instance().GetEID());
  119. }
  120. rcount--;
  121. continue;
  122. }
  123. break;
  124. } while (rcount > 0);
  125. }
  126. catch (...)
  127. {
  128. status = AESM_PSE_PR_EXCEPTION;
  129. }
  130. // if (AE_SUCCESS != status)
  131. // {
  132. // upsePersistentStorage::Delete(PSE_PR_LT_PAIRING_FID);
  133. // Helper::RemoveCertificateChain();
  134. // }
  135. SGX_DBGPRINT_PRINT_FUNCTION_AND_RETURNVAL(__FUNCTION__, status);
  136. SGX_DBGPRINT_PRINT_ANSI_STRING("End Long Term Pairing");
  137. return status;
  138. }
  139. //*********************************************************************
  140. // Do the long term pairing logic
  141. //*********************************************************************
  142. static ae_error_t DoLongTermPairing(bool* p_new_pairing)
  143. {
  144. std::list<upse::Buffer> certChain;
  145. upse::Buffer certChainVLR;
  146. upse::Buffer keyBlob;
  147. upse::Buffer pairingBlob;
  148. upse::Buffer ocspReq;
  149. upse::Buffer ocspResp;
  150. upse::Buffer s1;
  151. upse::Buffer s2;
  152. upse::Buffer s3;
  153. upse::Buffer sigRL;
  154. upse::Buffer privRL;
  155. if (NULL == p_new_pairing)
  156. return AESM_PSE_PR_BAD_POINTER_ERROR;
  157. ae_error_t status = AE_FAILURE;
  158. pse_pr_interface_psda* pPSDA = NULL;
  159. do
  160. {
  161. pPSDA = new pse_pr_interface_psda();
  162. BREAK_IF_FALSE( (NULL != pPSDA),
  163. status, AESM_PSE_PR_INSUFFICIENT_MEMORY_ERROR);
  164. //=====================================================================
  165. // Start: LONG TERM PAIRING protocol
  166. //=====================================================================
  167. //*********************************************************************
  168. // Load the pairing blob from persistent storage.
  169. // Load the verifier certificate and CA certificate chain.
  170. // The ECDSA key pair was generated during certificate provisioning.
  171. //*********************************************************************
  172. status = Helper::read_ltp_blob(pairingBlob);
  173. pairing_blob_t* pairing_blob = (pairing_blob_t*)pairingBlob.getData();
  174. BREAK_IF_FAILED_ERR(status, AESM_NLTP_NO_LTP_BLOB);
  175. SGX_DBGPRINT_PRINT_ANSI_STRING("pairing blob load success");
  176. // Received during Certificate Chain Provisioning
  177. status = Helper::LoadCertificateChain(certChain);
  178. BREAK_IF_FAILED_ERR(status, AESM_NPC_NO_PSE_CERT);
  179. SGX_DBGPRINT_PRINT_ANSI_STRING("Certificate Chain load success");
  180. //*********************************************************************
  181. // Retrieve S1 from ME/CSE
  182. //*********************************************************************
  183. status = pPSDA->GetS1(pairing_blob->plaintext.pse_instance_id, s1);
  184. SGX_DBGPRINT_ONE_STRING_ONE_INT_LTP("Function: pPSDA->GetS1(s1), Return Value: ", status);
  185. BREAK_IF_FAILED(status);
  186. BREAK_IF_FALSE( (s1.getSize() == sizeof(SIGMA_S1_MESSAGE)),
  187. status, AESM_PSE_PR_INTERNAL_ERROR);
  188. const SIGMA_S1_MESSAGE* pS1 = (const SIGMA_S1_MESSAGE*)s1.getData();
  189. #if 1
  190. upse::Buffer tGID;
  191. uint32_t serializedGID = (uint32_t)pS1->Gid;
  192. status = tGID.Alloc((uint8_t*)&serializedGID, sizeof(uint32_t));
  193. BREAK_IF_FAILED(status);
  194. SigmaHelper::SetGID(tGID);
  195. #endif
  196. //*********************************************************************
  197. // Retrieve Sig RL and Priv RL from Intel Server (okay if it can't retrieve them)
  198. //*********************************************************************
  199. status = SigmaHelper::GetRLsFromServer(sigRL, privRL);
  200. if (AE_SUCCESS != status)
  201. AESM_LOG_WARN(g_event_string_table[SGX_EVENT_EPID11_RL_RETRIEVAL_FAILURE]);
  202. SGX_DBGPRINT_PRINT_ANSI_STRING("RL requested");
  203. //*********************************************************************
  204. // Retrieve OCSP Responses from Intel Server
  205. //*********************************************************************
  206. status = SigmaHelper::GetOcspResponseFromServer(certChain, pS1->OcspReq, ocspResp);
  207. if (AE_SUCCESS != status) {
  208. AESM_LOG_WARN(g_event_string_table[SGX_EVENT_OCSP_FAILURE]);
  209. }
  210. if (OAL_PROXY_SETTING_ASSIST == status) {
  211. SGX_DBGPRINT_PRINT_ANSI_STRING("proxy error during OCSP");
  212. break;
  213. }
  214. if (AESM_LTP_PSE_CERT_REVOKED == status) {
  215. SGX_DBGPRINT_PRINT_ANSI_STRING("OCSP server returns cert_revoked");
  216. break;
  217. }
  218. BREAK_IF_FALSE( (status == AE_SUCCESS), status, AESM_PSE_PR_GET_OCSPRESP_ERROR);
  219. SGX_DBGPRINT_PRINT_ANSI_STRING("OCSP retrieval success");
  220. //*********************************************************************
  221. // Package the Certificate Chain as a VLR
  222. //*********************************************************************
  223. status = Helper::PrepareCertificateChainVLR(certChain, certChainVLR);
  224. BREAK_IF_FAILED(status);
  225. SGX_DBGPRINT_PRINT_ANSI_STRING("Certificate Chain prepared success");
  226. //*********************************************************************
  227. // Communicate with PSE_pr enclave
  228. // Send: s1, sigRL, ocspResp, verifierCert, pairingBlob
  229. // Receive: s2
  230. //*********************************************************************
  231. status = tGenM7(s1, sigRL, ocspResp, certChainVLR, pairingBlob, s2);
  232. if (PSE_PR_MSG_COMPARE_ERROR == status) {
  233. AESM_LOG_ERROR("%s", g_event_string_table[SGX_EVENT_EPID11_SIGRL_INTEGRITY_ERROR]);
  234. }
  235. BREAK_IF_FAILED(status);
  236. SGX_DBGPRINT_PRINT_ANSI_STRING("M7 success");
  237. //*********************************************************************
  238. // Communicate with ME/CSE
  239. // Send: s2
  240. // Receive: s3
  241. //*********************************************************************
  242. status = pPSDA->ExchangeS2AndS3(pairing_blob->plaintext.pse_instance_id, s2, s3);
  243. SGX_DBGPRINT_ONE_STRING_ONE_INT_LTP("Function: pPSDA->ExchangeS2AndS3(s2, s3), Return Value: ", status);
  244. if (AESM_PSDA_LT_SESSION_INTEGRITY_ERROR == status) {
  245. AESM_LOG_ERROR("%s", g_event_string_table[SGX_EVENT_SIGMA_S2_INTEGRITY_ERROR]);
  246. SGX_DBGPRINT_PRINT_ANSI_STRING("pairing blob deleted");
  247. Helper::delete_ltp_blob();
  248. }
  249. BREAK_IF_FAILED(status);
  250. SGX_DBGPRINT_PRINT_ANSI_STRING("PSDA Exchange success");
  251. //*********************************************************************
  252. // Communicate with PSE_pr enclave
  253. // Send: s3, privRL, epidGroupCert, epidParamsCert, pairingBlob
  254. // Receive: pairingBlob, bNewPairing flag
  255. //*********************************************************************
  256. status = tVerifyM8(s3, privRL, pairingBlob, *p_new_pairing);
  257. if (PSE_PR_MSG_COMPARE_ERROR == status) {
  258. AESM_LOG_ERROR("%s", g_event_string_table[SGX_EVENT_EPID11_PRIVRL_INTEGRITY_ERROR]);
  259. }
  260. BREAK_IF_TRUE((status == PSE_PR_PCH_EPID_SIG_REVOKED_IN_GROUPRL), status, AESM_LTP_PSE_CERT_REVOKED);
  261. BREAK_IF_FAILED(status);
  262. SGX_DBGPRINT_PRINT_ANSI_STRING("M8 success");
  263. //*********************************************************************
  264. // Save the sealed pairing blob to persistent storage.
  265. //*********************************************************************
  266. status = Helper::write_ltp_blob(pairingBlob);
  267. BREAK_IF_FAILED(status);
  268. SGX_DBGPRINT_PRINT_ANSI_STRING("pairing blob written success");
  269. status = AE_SUCCESS;
  270. #if defined(_DEBUG)
  271. uint32_t pseSvn = certPseSvn();
  272. SGX_DBGPRINT_ONE_STRING_ONE_INT("certPseSvn() returns ", pseSvn);
  273. uint32_t sgxGid = 0;
  274. EPIDBlob::instance().get_sgx_gid(&sgxGid);
  275. SGX_DBGPRINT_ONE_STRING_ONE_INT("get_sgx_gid() returns ", sgxGid);
  276. uint32_t psdaSvn = 0;
  277. psdaSvn = Helper::ltpBlobPsdaSvn(*(pairing_blob_t*)pairingBlob.getData());
  278. SGX_DBGPRINT_ONE_STRING_ONE_INT("ltpBlobPsdaSvn() returns ", psdaSvn);
  279. unsigned currentPsdaSvn = 0;
  280. PSDAService::instance().current_psda_svn(&currentPsdaSvn);
  281. SGX_DBGPRINT_ONE_STRING_ONE_INT("current_psda_svn() returns ", currentPsdaSvn);
  282. uint32_t cseGid = 0;
  283. ae_error_t ltpBlobCseGid(uint32_t* pGid);
  284. ltpBlobCseGid(&cseGid);
  285. SGX_DBGPRINT_ONE_STRING_ONE_INT("ltpBlobCseGid() returns ", cseGid);
  286. #endif
  287. } while (false);
  288. if (NULL != pPSDA)
  289. {
  290. delete pPSDA;
  291. }
  292. if (PSE_PAIRING_BLOB_UNSEALING_ERROR == status || PSE_PAIRING_BLOB_INVALID_ERROR == status)
  293. {
  294. SGX_DBGPRINT_PRINT_ANSI_STRING("Invalid pairing blob.");
  295. Helper::delete_ltp_blob();
  296. }
  297. if (AE_FAILED(status))
  298. {
  299. switch (status)
  300. {
  301. case OAL_NETWORK_UNAVAILABLE_ERROR: AESM_LOG_FATAL(g_event_string_table[SGX_EVENT_OCSP_FAILURE]); break;
  302. case PSE_PAIRING_BLOB_UNSEALING_ERROR: AESM_LOG_FATAL(g_event_string_table[SGX_EVENT_LTP_BLOB_INTEGRITY_ERROR]); break;
  303. case PSE_PAIRING_BLOB_INVALID_ERROR: AESM_LOG_FATAL(g_event_string_table[SGX_EVENT_LTP_BLOB_INVALID_ERROR]); break;
  304. case AESM_LTP_PSE_CERT_REVOKED:
  305. {
  306. AESM_LOG_FATAL("%s", g_event_string_table[SGX_EVENT_ME_EPID_GROUP_REVOCATION]);
  307. break;
  308. }
  309. case PSE_PR_PCH_EPID_SIG_REVOKED_IN_PRIVRL:
  310. {
  311. AESM_LOG_FATAL("%s", g_event_string_table[SGX_EVENT_ME_EPID_KEY_REVOCATION]);
  312. break;
  313. }
  314. case PSE_PR_PCH_EPID_SIG_REVOKED_IN_SIGRL:
  315. {
  316. AESM_LOG_FATAL("%s", g_event_string_table[SGX_EVENT_ME_EPID_SIG_REVOCATION]);
  317. break;
  318. }
  319. case AE_FAILURE:
  320. {
  321. AESM_LOG_FATAL("%s", g_event_string_table[SGX_EVENT_LTP_FAILURE]);
  322. break;
  323. }
  324. default: break;
  325. }
  326. }
  327. SGX_DBGPRINT_PRINT_FUNCTION_AND_RETURNVAL(__FUNCTION__, status);
  328. return status;
  329. }
  330. //
  331. // ltpBlobCseGid
  332. //
  333. // return value of CSE GID from long-term pairing blob
  334. //
  335. // inputs
  336. // pGid: pointer to uint32_t that will hold GID
  337. //
  338. // outputs
  339. // *pGid: CSE GID
  340. // status
  341. //
  342. // different return type?
  343. //
  344. ae_error_t ltpBlobCseGid(uint32_t* pGid)
  345. {
  346. upse::Buffer pairing_blob;
  347. ae_error_t retVal = AE_SUCCESS;
  348. if (NULL != pGid) {
  349. //
  350. // read blob
  351. //
  352. retVal = upsePersistentStorage::Read(PSE_PR_LT_PAIRING_FID, pairing_blob);
  353. if (AE_SUCCESS == retVal) {
  354. const pairing_blob_t* pb = (const pairing_blob_t*) pairing_blob.getData();
  355. if (NULL != pb) {
  356. *pGid = pb->plaintext.cse_sec_prop.ps_hw_gid;
  357. }
  358. else {
  359. retVal = AESM_PSE_PR_INTERNAL_ERROR;
  360. }
  361. }
  362. else {
  363. retVal = AESM_PSE_PR_PERSISTENT_STORAGE_READ_ERROR;
  364. }
  365. }
  366. else {
  367. retVal = AESM_PSE_PR_BAD_POINTER_ERROR;
  368. }
  369. return retVal;
  370. }