t_long_term_pairing.cpp 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065
  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 <cstddef>
  32. #include "sgx_trts.h"
  33. #include "sgx_utils.h"
  34. #include "sgx_tseal.h"
  35. #include "t_long_term_pairing.h"
  36. #include "prepare_hash_sha256.h"
  37. #include "pse_pr_inc.h"
  38. #include "pse_pr_common.h"
  39. #include "pse_pr_sigma_1_1_defs.h"
  40. #include "le2be_macros.h"
  41. #include "t_pairing_blob.h"
  42. #include "epid/common/1.1/types.h"
  43. #include "Keys.h"
  44. #include <string.h>
  45. #include "X509Parser.h"
  46. #include "Epid11_rl.h"
  47. #include "sgx_tcrypto.h"
  48. // Each of the following definitions is larger than
  49. // should ever be encountered.
  50. // MAX possible should be 19280 based on 150 SIGRL entries
  51. #define MAX_ALLOWED_SIGRL_SIZE 20480
  52. #define MAX_ALLOWED_OCSP_SIZE 8192
  53. #define MAX_ALLOWED_CERT_SIZE 8192
  54. // MAX of S2 is defined larger than we should ever encounter
  55. #define MAX_ALLOWED_S2_SIZE 35840
  56. // MAX possible should be 3280 based on 100 PRIVRL entries
  57. #define MAX_ALLOWED_PRIVRL_SIZE 4096
  58. // MAX of S3 is defined larger than we should ever encounter
  59. #define MAX_ALLOWED_S3_SIZE 30720
  60. static ae_error_t map_GenM7_error_for_return(ae_error_t status)
  61. {
  62. #if !defined(_DEBUG)
  63. // Switch to limit errors returned when building for RELEASE
  64. switch (status)
  65. {
  66. case AE_SUCCESS: break;
  67. case PSE_PR_INSUFFICIENT_MEMORY_ERROR:
  68. case PSE_PAIRING_BLOB_UNSEALING_ERROR:
  69. break;
  70. default:
  71. status = AE_FAILURE;
  72. break;
  73. }
  74. #endif
  75. return status;
  76. }
  77. static ae_error_t map_VerifyM8_error_for_return(ae_error_t status)
  78. {
  79. #if !defined(_DEBUG)
  80. // Switch to limit errors returned when building for RELEASE
  81. switch (status)
  82. {
  83. case AE_SUCCESS: break;
  84. case PSE_PR_INSUFFICIENT_MEMORY_ERROR: break;
  85. case PSE_PR_PCH_EPID_NO_MEMORY_ERR:
  86. status = PSE_PR_INSUFFICIENT_MEMORY_ERROR;
  87. break;
  88. case PSE_PR_PCH_EPID_SIG_REVOKED_IN_GROUPRL: break;
  89. default:
  90. status = AE_FAILURE;
  91. break;
  92. }
  93. #else
  94. switch (status)
  95. {
  96. case PSE_PR_PCH_EPID_OUTOFMEMORY:
  97. status = PSE_PR_INSUFFICIENT_MEMORY_ERROR;
  98. break;
  99. }
  100. #endif
  101. return status;
  102. }
  103. // GUID format is DWORD-WORD-WORD-BYTES ARRAY(8)
  104. // Current PSDA applet ID is cbede6f9-6ce4-439c-a1c7-6e2087786616
  105. static const uint8_t PSDA_APPLET_ID[16] = {0xf9, 0xe6, 0xed, 0xcb, 0xe4, 0x6c, 0x9c, 0x43, 0xa1, 0xc7, 0x6e, 0x20, 0x87, 0x78, 0x66, 0x16};
  106. //extern void OutputOctets(const char* pMsg, const void* pData, size_t nData);
  107. TEpidSigma11Verifier::TEpidSigma11Verifier()
  108. {
  109. m_gid = 0;
  110. m_pSigRL = NULL;
  111. m_nSigRL = 0;
  112. m_nSigRLVersion = 0;
  113. m_nPrivRLVersion = 0;
  114. m_nDalAppletVersion = 0;
  115. memset(m_pairingID, 0, sizeof(m_pairingID));
  116. memset(m_pairingNonce, 0, sizeof(m_pairingNonce));
  117. m_nextState = STATE_GENM7;
  118. }
  119. TEpidSigma11Verifier::~TEpidSigma11Verifier(void)
  120. {
  121. if (m_pSigRL)
  122. {
  123. delete[] m_pSigRL;
  124. m_pSigRL = NULL;
  125. }
  126. m_nSigRL = 0;
  127. // Defense-in-depth: clear class members that contain Enclave secrets
  128. memset_s(m_pairingID, sizeof(m_pairingID), 0, sizeof(m_pairingID));
  129. memset_s(m_pairingNonce, sizeof(m_pairingNonce), 0, sizeof(m_pairingNonce));
  130. memset_s(m_verifierPrivateKey, sizeof(m_verifierPrivateKey), 0, sizeof(m_verifierPrivateKey));
  131. }
  132. bool TEpidSigma11Verifier::get_sigRL_info(const EPID11_SIG_RL* pSigRL, uint32_t& sigRL_entries, uint32_t& sigRL_size)
  133. {
  134. if (NULL == pSigRL)
  135. {
  136. // null sigRL is acceptable
  137. sigRL_entries = 0;
  138. sigRL_size = 0;
  139. return true;
  140. }
  141. uint32_t entries = *(uint32_t*)pSigRL->entries;
  142. entries = SwapEndian_DW(entries);
  143. if (entries > MAX_SIGRL_ENTRIES) // invalid sigRL
  144. return false;
  145. sigRL_entries = entries;
  146. sigRL_size = (uint32_t)(sizeof(EPID11_SIG_RL) + (sigRL_entries * EPID11_SIG_RL_ENTRY_SIZE) + EPID11_SIG_RL_SIGNATURE_SIZE);
  147. return true;
  148. }
  149. bool TEpidSigma11Verifier::get_privRL_info(const EPID11_PRIV_RL* pPrivRL, uint32_t& privRL_entries, uint32_t& privRL_size)
  150. {
  151. if (NULL == pPrivRL)
  152. {
  153. // null privRL is acceptable
  154. privRL_entries = 0;
  155. privRL_size = 0;
  156. return true;
  157. }
  158. uint32_t entries = *(uint32_t*)pPrivRL->entries;
  159. entries = SwapEndian_DW(entries);
  160. if (entries > MAX_PRIVRL_ENTRIES) // invalid privRL
  161. return false;
  162. privRL_entries = entries;
  163. privRL_size = (uint32_t)(sizeof(EPID11_PRIV_RL) + (privRL_entries * EPID11_PRIV_RL_ENTRY_SIZE) + EPID11_PRIV_RL_SIGNATURE_SIZE);
  164. return true;
  165. }
  166. //****************************************************************************
  167. //****************************************************************************
  168. //****************************************************************************
  169. // FLOW
  170. // Verifier Prover Intel Server
  171. // uRequestS1FromME |--M1: Start Pairing->| |
  172. // |<-M2: SIGMA S1-------| |
  173. // uGetR2 | | |
  174. // | | |
  175. // uLoadPairingBlob | | |
  176. // | | |
  177. // uGetSigRLFromServer |--M3: GID_cse || R2----------------->|
  178. // |<-M4: Sig_is(RL_cse || R2)-----------|
  179. // uGetOCSPResponseFromServer |--M5: OCSPReq----------------------->|
  180. // |<-M6: OCSPResp-----------------------|
  181. // uCheckOCSPResponseForExpiration | | |
  182. // | | |
  183. // tGenM7 (enclave call) Send S1, Receive S2
  184. // | | |
  185. // uExchangeS2AndS3WithME |--M7: SIGMA S2------>| |
  186. // |<-M8: SIGMA S3-------| |
  187. // uGetGroupIdFromME | | |
  188. // | | |
  189. // tVerifyM8 (enclave call) Send S3, Receive updated pairing blob
  190. // | | |
  191. // uSavePairingBlob | | |
  192. ae_error_t TEpidSigma11Verifier::GenM7
  193. (
  194. /*in */ const SIGMA_S1_MESSAGE* pS1,
  195. /*in */ const EPID11_SIG_RL* pSigRL,
  196. /*in */ const UINT8* pOcspResp,
  197. /*in */ UINT32 nLen_OcspResp,
  198. /*in */ const UINT8* pVerifierCert,
  199. /*in */ UINT32 nLen_VerifierCert,
  200. /*in */ const pairing_blob_t* pPairingBlob,
  201. /*in */ UINT32 nMax_S2,
  202. /*out*/ SIGMA_S2_MESSAGE* pS2,
  203. /*out*/ UINT32* pnLen_S2
  204. )
  205. {
  206. ae_error_t status = AE_FAILURE;
  207. bool bResult;
  208. pairing_data_t pairing_data;
  209. memset(&pairing_data, 0, sizeof(pairing_data));
  210. sgx_ecc_state_handle_t sigma_ecc_handle = NULL;
  211. do
  212. {
  213. ae_error_t tmp_status;
  214. // sigRL_size allows for the sigRL header, array of RL entries, and signature at the end
  215. uint32_t sigRL_entries = 0;
  216. uint32_t sigRL_size = 0;
  217. bResult = TEpidSigma11Verifier::get_sigRL_info(pSigRL, sigRL_entries, sigRL_size);
  218. BREAK_IF_FALSE((bResult), status, PSE_PR_BAD_POINTER_ERROR);
  219. BREAK_IF_TRUE((STATE_GENM7 != m_nextState), status, PSE_PR_CALL_ORDER_ERROR);
  220. //*********************************************************************
  221. // Validate pointers and sizes
  222. //*********************************************************************
  223. BREAK_IF_TRUE((NULL == pS1), status, PSE_PR_BAD_POINTER_ERROR);
  224. // SigRL is allowed to be NULL and will be checked in ValidateSigRL()
  225. BREAK_IF_TRUE((nLen_OcspResp > 0 && NULL == pOcspResp), status, PSE_PR_BAD_POINTER_ERROR);
  226. BREAK_IF_TRUE((NULL == pVerifierCert), status, PSE_PR_BAD_POINTER_ERROR);
  227. BREAK_IF_TRUE((NULL == pPairingBlob), status, PSE_PR_BAD_POINTER_ERROR);
  228. BREAK_IF_TRUE((nLen_VerifierCert > MAX_ALLOWED_CERT_SIZE), status, PSE_PR_PARAMETER_ERROR);
  229. BREAK_IF_TRUE((sigRL_size > MAX_ALLOWED_SIGRL_SIZE), status, PSE_PR_PARAMETER_ERROR);
  230. BREAK_IF_TRUE((nLen_OcspResp > MAX_ALLOWED_OCSP_SIZE), status, PSE_PR_PARAMETER_ERROR);
  231. uint32_t nNeededBytesForS2 = ::NeededBytesForS2(nLen_VerifierCert, sigRL_size, nLen_OcspResp);
  232. BREAK_IF_TRUE((nNeededBytesForS2 > MAX_ALLOWED_S2_SIZE), status, PSE_PR_PARAMETER_ERROR);
  233. BREAK_IF_TRUE((NULL == pS2 || NULL == pnLen_S2 || nMax_S2 < nNeededBytesForS2),
  234. status, PSE_PR_PARAMETER_ERROR);
  235. //*********************************************************************
  236. // Start SIGMA processing of S1 and generate S2
  237. //*********************************************************************
  238. //*********************************************************************
  239. // Extract components of Msg S1
  240. // g^a || GID || OCSPReq
  241. //*********************************************************************
  242. m_sigmaAlg.set_remote_pub_key_ga_be((Ipp8u*)pS1->Ga);
  243. memcpy(&m_gid, &pS1->Gid, sizeof(SAFEID_GID));
  244. //*********************************************************************
  245. // Choose random value 'b' as ephemeral DH private key
  246. // Compute 'g^b' as ephemeral DH public key
  247. //*********************************************************************
  248. sgx_status_t sgx_status = sgx_ecc256_open_context(&sigma_ecc_handle);
  249. BREAK_IF_TRUE((SGX_ERROR_OUT_OF_MEMORY == sgx_status), status, PSE_PR_INSUFFICIENT_MEMORY_ERROR);
  250. BREAK_IF_TRUE((SGX_SUCCESS != sgx_status), status, PSE_PR_KEY_PAIR_GENERATION_ERROR);
  251. /* Get private key b and public key g^b, in little-endian format */
  252. uint8_t Publickey_little_endian[SIGMA_SESSION_PUBKEY_LENGTH];
  253. uint8_t Privatekey_b_little_endian[SIGMA_SESSION_PRIVKEY_LENGTH];
  254. if (SGX_SUCCESS != sgx_ecc256_create_key_pair((sgx_ec256_private_t *)Privatekey_b_little_endian,
  255. (sgx_ec256_public_t*)Publickey_little_endian, sigma_ecc_handle))
  256. {
  257. break;
  258. }
  259. m_sigmaAlg.set_prv_key_b_le(Privatekey_b_little_endian);
  260. // clear buffer containing secrets
  261. memset_s(Privatekey_b_little_endian, sizeof(Privatekey_b_little_endian), 0, sizeof(Privatekey_b_little_endian));
  262. /* Convert to big endian for m_localPublicKey_gb_big_endian */
  263. SwapEndian_32B(&(Publickey_little_endian[0]));
  264. SwapEndian_32B(&(Publickey_little_endian[32]));
  265. m_sigmaAlg.set_pub_key_gb_be(Publickey_little_endian);
  266. // OutputOctets("::GenM7:: g^a (BE)", m_remotePublicKey_ga_big_endian, SIGMA_SESSION_PUBKEY_LENGTH);
  267. // OutputOctets("::GenM7:: b (LE)", m_localPrivateKey_b_little_endian, sizeof(sgx_ec256_private_t));
  268. // OutputOctets("::GenM7:: g^b (BE)", m_localPublicKey_gb_big_endian, SIGMA_SESSION_PUBKEY_LENGTH);
  269. //*********************************************************************
  270. // Compute ((g^a)^b)
  271. // Derive SMK
  272. // SMK := HMAC-SHA256(0x00, g^(ab) || 0x00) with the HMAC key being
  273. // 32 bytes of 0x00 and the data element being g^(ab) little endian
  274. // Derive SK and MK
  275. // a) Compute HMAC-SHA256(0x00, g^(ab) || 0x01)
  276. // with the HMAC key being 32 bytes of 0x00, g^(ab) in little endian
  277. // b) SK is taken as the first 128 bits of the HMAC result
  278. // c) MK is taken as the second 128 bits of the HMAC result
  279. //*********************************************************************
  280. tmp_status = m_sigmaAlg.DeriveSkMk(sigma_ecc_handle);
  281. BREAK_IF_TRUE(AE_SUCCESS != tmp_status, status, tmp_status);
  282. // OutputOctets("::GenM7: m_Sk", m_Sk, SIGMA_SK_LENGTH);
  283. // OutputOctets("::GenM7:: m_Mk", m_Mk, SIGMA_MK_LENGTH);
  284. // OutputOctets("::GenM7:: m_SMK", m_SMK, SIGMA_SMK_LENGTH);
  285. //*********************************************************************
  286. // Unseal pairing blob
  287. //*********************************************************************
  288. tmp_status = UnsealPairingBlob(pPairingBlob, &pairing_data);
  289. BREAK_IF_TRUE(AE_SUCCESS != tmp_status, status, tmp_status);
  290. //*************************************************************
  291. // Extract Private Key from pairing blob
  292. //*************************************************************
  293. // OutputOctets("::GenM7:: Verifier PrivateKey", unsealedBlobData.VerifierPrivateKey, ECDSA_PRIVKEY_LEN);
  294. memcpy(m_verifierPrivateKey, pairing_data.secret_data.VerifierPrivateKey, ECDSA_PRIVKEY_LEN);
  295. //*************************************************************
  296. // Extract pairing data
  297. //*************************************************************
  298. memcpy(m_pairingID, pairing_data.secret_data.pairingID, sizeof(m_pairingID));
  299. memcpy(m_pairingNonce, pairing_data.secret_data.pairingNonce, sizeof(m_pairingNonce));
  300. // OutputOctets("::GenM7:: m_pairingID", m_pairingID, sizeof(m_pairingID));
  301. // OutputOctets("::GenM7:: m_pairingNonce", m_pairingNonce, sizeof(m_pairingNonce));
  302. //*********************************************************************
  303. // Prepare S2
  304. //*********************************************************************
  305. memset(pS2, 0, nMax_S2);
  306. // Copy Gb in big endian to S2
  307. memcpy(pS2->Gb, m_sigmaAlg.get_pub_key_gb_be(), SIGMA_SESSION_PUBKEY_LENGTH);
  308. // Copy OCSP request sent in S1
  309. memcpy(&pS2->OcspReq, &pS1->OcspReq, sizeof(OCSP_REQ));
  310. // Basename is always set to 0
  311. memset(pS2->Basename, 0, SIGMA_BASENAME_LENGTH);
  312. // Location within pS2->Data where data gets added
  313. size_t index = 0;
  314. //*********************************************************************
  315. // Add verifier certificate chain to S2
  316. //*********************************************************************
  317. tmp_status = AddCertificateChain(pS2, index, nMax_S2,
  318. pVerifierCert, nLen_VerifierCert);
  319. BREAK_IF_TRUE(AE_SUCCESS != tmp_status, status, tmp_status);
  320. //*********************************************************************
  321. // Verify SigRL (verify Cert signature and get the RL version)
  322. //*********************************************************************
  323. tmp_status = ValidateSigRL(pSigRL, sigRL_entries, sigRL_size, &m_nSigRLVersion);
  324. BREAK_IF_TRUE(AE_SUCCESS != tmp_status, status, tmp_status);
  325. //*********************************************************************
  326. // Add revocation list to S2
  327. //*********************************************************************
  328. tmp_status = AddRevocationList(pS2, index, nMax_S2, pSigRL, sigRL_size);
  329. BREAK_IF_TRUE(AE_SUCCESS != tmp_status, status, tmp_status);
  330. //*********************************************************************
  331. // Add OCSP Responses to S2
  332. //*********************************************************************
  333. tmp_status = AddOcspResponses(pS2, index, nMax_S2,
  334. pOcspResp, nLen_OcspResp);
  335. BREAK_IF_TRUE(AE_SUCCESS != tmp_status, status, tmp_status);
  336. //*********************************************************************
  337. // Compute the HMAC over S2 using SMK (exclude SigGbGa)
  338. // [g^b || Basename || OCSPReq || Certver || SIG-RL || OCSPResp]SMK
  339. //*********************************************************************
  340. // index is portion of S2 in pS2->Data
  341. tmp_status = m_sigmaAlg.calc_s2_hmac(&pS2->S2Icv, pS2, index);
  342. BREAK_IF_TRUE(AE_SUCCESS != tmp_status, status, tmp_status);
  343. //*********************************************************************
  344. // Append Pr_pse, where Pr_pse is HMAC_SHA256(MK, OLD_SK || 0x01)
  345. // if OLD_SK is available, or 256-bit 0x0 if OLD_SK is not available
  346. //*********************************************************************
  347. PR_PSE_T pr = {0};
  348. int nSizePr = sizeof(pr);
  349. const Nonce128_t zeroNonce = {0};
  350. if (0 != memcmp(zeroNonce, m_pairingNonce, sizeof(Nonce128_t)))
  351. {
  352. // A non-zero pairing nonce indicates valid pairing info is available
  353. tmp_status = m_sigmaAlg.ComputePR(&m_pairingID, 0x01, (SIGMA_HMAC*)pr);
  354. BREAK_IF_TRUE(AE_SUCCESS != tmp_status, status, tmp_status);
  355. // OutputOctets("::GenM7:: Pr_pse HMAC[(m_pairingID || 0x01)] using m_Mk", pr, nSizePr);
  356. }
  357. nSizePr = sizeof(pr);
  358. memcpy((pS2->Data + index), pr, nSizePr);
  359. index += nSizePr;
  360. //*********************************************************************
  361. // Sign SigGaGb
  362. // Sig_pse(g^a || g^b)
  363. //*********************************************************************
  364. uint8_t combined_pubkeys[SIGMA_SESSION_PUBKEY_LENGTH * 2];
  365. uint8_t ecc_sig[ECDSA_SIG_LENGTH] = {0};
  366. /* GaGb in big endian format */
  367. memcpy(combined_pubkeys, m_sigmaAlg.get_remote_pub_key_ga_be(), SIGMA_SESSION_PUBKEY_LENGTH);
  368. memcpy(combined_pubkeys + SIGMA_SESSION_PUBKEY_LENGTH, m_sigmaAlg.get_pub_key_gb_be(), SIGMA_SESSION_PUBKEY_LENGTH);
  369. if (SGX_SUCCESS == sgx_ecdsa_sign(combined_pubkeys,
  370. sizeof(combined_pubkeys),
  371. (sgx_ec256_private_t *)pairing_data.secret_data.VerifierPrivateKey,
  372. (sgx_ec256_signature_t *)ecc_sig,
  373. sigma_ecc_handle))
  374. {
  375. /* Convert the signature to big endian format for pS2->SigGaGb */
  376. SwapEndian_32B(ecc_sig);
  377. SwapEndian_32B(&(ecc_sig[32]));
  378. memcpy(pS2->SigGaGb, ecc_sig, ECDSA_SIG_LENGTH);
  379. }
  380. else
  381. {
  382. status = PSE_PR_MSG_SIGNING_ERROR;
  383. break;
  384. }
  385. //*********************************************************************
  386. // Set the size of S2 that is being returned
  387. //*********************************************************************
  388. size_t S2IcvSize = SIGMA_S2_ICV_CONSTANT_BUFFER_SIZE + index;
  389. if ( UINT32_MAX - SIGMA_S2_ICV_CONSTANT_BUFFER_SIZE - ECDSA_SIG_LENGTH - SIGMA_HMAC_LENGTH < index)
  390. {
  391. status = PSE_PR_BAD_POINTER_ERROR;
  392. break;
  393. }
  394. *pnLen_S2 = (uint32_t)(S2IcvSize + ECDSA_SIG_LENGTH + SIGMA_HMAC_LENGTH);
  395. //*********************************************************************
  396. // WE PASSED ALL BARRIERS TO SUCCESS
  397. //*********************************************************************
  398. status = AE_SUCCESS;
  399. m_nextState = STATE_VERIFYM8;
  400. } while (false);
  401. /* Defense-in-depth: clear the data on stack that contains enclave secret.*/
  402. memset_s(&pairing_data, sizeof(pairing_data), 0, sizeof(pairing_data));
  403. /* close ecc context handle, the generic crypto lib will free the context memory */
  404. if (sigma_ecc_handle != NULL) sgx_ecc256_close_context(sigma_ecc_handle);
  405. return map_GenM7_error_for_return(status);
  406. }
  407. #define RL_OFFSET 4
  408. /*
  409. This function will check if the S3 ICV is correct.
  410. Then it will verify the EPID signature
  411. */
  412. ae_error_t TEpidSigma11Verifier::VerifyM8
  413. (
  414. /*in */ const SIGMA_S3_MESSAGE* pS3,
  415. /*in */ UINT32 nLen_S3,
  416. /*in */ const EPID11_PRIV_RL* pPrivRL,
  417. /*in, out*/ pairing_blob_t* pPairingBlob,
  418. /*out*/ bool* pbNewPairing
  419. )
  420. {
  421. // S3 --> [TaskInfo || g^a || EpidCert || EpidSig(g^a || g^b) || SIG-RL]SMK
  422. ae_error_t status = AE_FAILURE;
  423. pairing_data_t pairing_data;
  424. //
  425. // This is misleading, PR_PSE_T isn't part of SIGMA, S3, it's part of our (sgx) m8.
  426. // Also, min_s3 is a very low lower bound. m8 message (not s3) is hmac || taskinfo || g**a || group cert || epid sig || sig-rl || pr_pse.
  427. // Group cert, EPID sig and SigRL are variable-length, but they have fixed length parts so lengths of the fixed
  428. // length parts could be included here.
  429. //
  430. const size_t min_s3 = sizeof(SIGMA_S3_MESSAGE) + sizeof(PR_PSE_T);
  431. bool bResult;
  432. bool bNewPairing = false;
  433. do
  434. {
  435. ae_error_t tmp_status;
  436. // privRL_size allows for the privRL header, array of RL entries, and signature at the end
  437. uint32_t privRL_entries = 0;
  438. uint32_t privRL_size = 0;
  439. bResult = TEpidSigma11Verifier::get_privRL_info(pPrivRL, privRL_entries, privRL_size);
  440. BREAK_IF_FALSE((bResult), status, PSE_PR_BAD_POINTER_ERROR);
  441. BREAK_IF_TRUE( (STATE_VERIFYM8 != m_nextState), status, PSE_PR_CALL_ORDER_ERROR);
  442. //*********************************************************************
  443. // Validate pointers and sizes
  444. //*********************************************************************
  445. BREAK_IF_TRUE((NULL == pS3 || nLen_S3 < min_s3), status, PSE_PR_BAD_POINTER_ERROR);
  446. // pPrivRL is allowed to be NULL and will be checked in ValidatePrivRL()
  447. BREAK_IF_TRUE((NULL == pPairingBlob), status, PSE_PR_BAD_POINTER_ERROR);
  448. BREAK_IF_TRUE((NULL == pbNewPairing), status, PSE_PR_BAD_POINTER_ERROR);
  449. BREAK_IF_TRUE((privRL_size > MAX_ALLOWED_PRIVRL_SIZE), status, PSE_PR_PARAMETER_ERROR);
  450. BREAK_IF_TRUE((nLen_S3 > MAX_ALLOWED_S3_SIZE), status, PSE_PR_PARAMETER_ERROR);
  451. BREAK_IF_FALSE(sgx_is_within_enclave(pS3, nLen_S3), status, PSE_PR_BAD_POINTER_ERROR);
  452. //*********************************************************************
  453. // Start SIGMA processing S3
  454. //*********************************************************************
  455. //*********************************************************************
  456. // Initialize for calculating HMAC and indexing to data
  457. //*********************************************************************
  458. size_t S3VLDataLen = nLen_S3 - (sizeof(SIGMA_S3_MESSAGE) + sizeof(PR_PSE_T));
  459. //*********************************************************************
  460. // Verify the S3 HMAC using SMK
  461. // [TaskInfo || g^a || EpidCert || EpidSig(g^a || g^b) || SIG-RL]SMK
  462. //*********************************************************************
  463. SIGMA_HMAC calcHMAC;
  464. tmp_status = m_sigmaAlg.calc_s3_hmac(&calcHMAC, pS3, S3VLDataLen);
  465. BREAK_IF_TRUE(AE_SUCCESS != tmp_status, status, tmp_status);
  466. bResult = (1 == consttime_memequal(calcHMAC, pS3->S3Icv, sizeof(SIGMA_HMAC)));
  467. BREAK_IF_FALSE( (bResult), status, PSE_PR_HMAC_COMPARE_ERROR);
  468. //*********************************************************************
  469. // Verify that g^a is the same that arrived in S1
  470. //*********************************************************************
  471. bResult = (0 == memcmp(m_sigmaAlg.get_remote_pub_key_ga_be(), pS3->Ga, sizeof(pS3->Ga)));
  472. BREAK_IF_FALSE( (bResult), status, PSE_PR_GA_COMPARE_ERROR);
  473. //*********************************************************************
  474. // Verify TaskInfo
  475. //*********************************************************************
  476. BREAK_IF_FALSE(TaskInfoIsValid(pS3->TaskInfo), status, PSE_PR_TASK_INFO_ERROR);
  477. //*********************************************************************
  478. // Check the EPID signature
  479. //*********************************************************************
  480. X509_GROUP_CERTIFICATE_VLR* X509GroupCertVlr = NULL;
  481. EPID_SIGNATURE_VLR* EpidSigVlr = NULL;
  482. tmp_status = ValidateS3DataBlock(pS3, nLen_S3, &X509GroupCertVlr, &EpidSigVlr);
  483. BREAK_IF_TRUE(AE_SUCCESS != tmp_status, status, tmp_status);
  484. UINT32 S3GID;
  485. Epid11GroupPubKey groupPubKey;
  486. /* X509Parser::ParseGroupCertificate() expecting big endian format public key */
  487. uint8_t SerializedPublicKey[SIGMA_SESSION_PUBKEY_LENGTH];
  488. for (uint32_t i = 0; i < Keys::EpidVerifyKeyNum(); i++)
  489. {
  490. memcpy(SerializedPublicKey, (EcDsaPubKey*)Keys::EpidVerifyKeys()[i], SIGMA_SESSION_PUBKEY_LENGTH);
  491. SwapEndian_32B(SerializedPublicKey);
  492. SwapEndian_32B(&(SerializedPublicKey[32]));
  493. if (0 == X509Parser::ParseGroupCertificate( /*in */ (EcDsaPubKey*)SerializedPublicKey,
  494. /*in */ X509GroupCertVlr, /*out*/ &S3GID, /*out*/ &groupPubKey))
  495. {
  496. tmp_status = AE_SUCCESS;
  497. break;
  498. }
  499. else
  500. {
  501. tmp_status = PSE_PR_X509_PARSE_ERROR;
  502. }
  503. }
  504. BREAK_IF_TRUE(AE_SUCCESS != tmp_status, status, tmp_status);
  505. BREAK_IF_FALSE((S3GID == m_gid), status, PSE_PR_GID_MISMATCH_ERROR );
  506. //*********************************************************************
  507. // Verify PrivRL
  508. //*********************************************************************
  509. tmp_status = ValidatePrivRL(pPrivRL, privRL_entries, privRL_size, &m_nPrivRLVersion);
  510. BREAK_IF_TRUE(AE_SUCCESS != tmp_status, status, tmp_status);
  511. KeysToSign_t combinedKeys;
  512. memset(&combinedKeys, 0, sizeof(combinedKeys));
  513. // Combine over g^a || g^b to the struct
  514. memcpy(combinedKeys.first, m_sigmaAlg.get_remote_pub_key_ga_be(),
  515. SIGMA_SESSION_PUBKEY_LENGTH);
  516. memcpy(combinedKeys.second, m_sigmaAlg.get_pub_key_gb_be(),
  517. SIGMA_SESSION_PUBKEY_LENGTH);
  518. //*********************************************************************
  519. // the input pPrivRL has a type definition of EPID11_PRIV_RL,
  520. // but the epid-sdk-3.0 library takes Epid11PrivRl as input parameter.
  521. // EPID11_PRIV_RL has 4 addtional bytes at the header so we offset the pointer
  522. // by 4 bytes . Also we need to exclude RL signature because epid-sdk3.0
  523. // checks the RL's size shouldn't include the signature. Similar for SigRL and GroupRL.
  524. //*********************************************************************
  525. uint8_t* pEpid11PrivRL = (pPrivRL == NULL)? NULL:(uint8_t*)pPrivRL+RL_OFFSET;
  526. uint32_t nEpid11PrivRLSize = (pPrivRL == NULL)? 0:privRL_size-RL_OFFSET-ECDSA_SIG_LENGTH;
  527. uint8_t* pEpid11SigRL = (m_pSigRL == NULL)? NULL:m_pSigRL+RL_OFFSET;
  528. uint32_t nEpid11SigRLSize = (m_pSigRL == NULL)? 0:m_nSigRL-RL_OFFSET-ECDSA_SIG_LENGTH;
  529. tmp_status = m_sigmaAlg.MsgVerifyPch((UINT8 *)&groupPubKey,
  530. (uint32_t)(sizeof(EpidCert) - ECDSA_SIG_LENGTH),
  531. NULL, // not required for EPID SDK 3.0
  532. (Ipp8u*)&combinedKeys,
  533. (uint32_t)sizeof(combinedKeys),
  534. NULL, // Bsn
  535. 0, // BsnLen
  536. (UINT8 *)EpidSigVlr->EpidSig,
  537. VLR_UNPADDED_PAYLOAD_SIZE(EpidSigVlr->VlrHeader),
  538. pEpid11PrivRL, nEpid11PrivRLSize, // PrivRL
  539. pEpid11SigRL, nEpid11SigRLSize, // SigRL
  540. NULL, 0); // GroupRL
  541. BREAK_IF_TRUE(AE_SUCCESS != tmp_status, status, tmp_status);
  542. //*********************************************************************
  543. // Calculate Id_pse and Id_cse
  544. // Id_pse = hash(sk || mk || 1)
  545. // Id_cse = hash(sk || mk || 2)
  546. //*********************************************************************
  547. SHA256_HASH Id_pse = {0};
  548. SHA256_HASH Id_cse = {0};
  549. tmp_status = m_sigmaAlg.ComputeId(1, &Id_pse);
  550. BREAK_IF_TRUE(AE_SUCCESS != tmp_status, status, tmp_status);
  551. tmp_status = m_sigmaAlg.ComputeId(2, &Id_cse);
  552. BREAK_IF_TRUE(AE_SUCCESS != tmp_status, status, tmp_status);
  553. //*********************************************************************
  554. // Verify Pr_cse, where Pr_cse is HMAC_SHA256(MK, OLD_SK || 0x02)
  555. // if OLD_SK is available, or 256-bit 0x0 if OLD_SK is not available
  556. //*********************************************************************
  557. size_t nSizePr = sizeof(PR_PSE_T);
  558. PR_PSE_T *pS3_PR_cse = (PR_PSE_T*)((const uint8_t*)pS3 + nLen_S3 - nSizePr);
  559. // OutputOctets("S3", pS3, nLen_S3);
  560. // OutputOctets("VerifyM8 - pS3_PR_cse", pS3_PR_cse, nSizePr);
  561. bNewPairing = true;
  562. PR_PSE_T pr_cse = {0};
  563. const Nonce128_t zeroNonce = {0};
  564. if (0 != memcmp(&pr_cse, pS3_PR_cse, sizeof(pr_cse)) && 0 != memcmp(&m_pairingNonce, &zeroNonce, sizeof(Nonce128_t)))
  565. {
  566. tmp_status = m_sigmaAlg.ComputePR(&m_pairingID, 0x02, (SIGMA_HMAC*)pr_cse);
  567. BREAK_IF_TRUE(AE_SUCCESS != tmp_status, status, tmp_status);
  568. // OutputOctets("::VerifyM8:: Computed Pr HMAC[(m_pairingID || 0x02)] using m_Mk", pr_cse, nSizePr);
  569. if (0 == memcmp(&pr_cse, pS3_PR_cse, sizeof(pr_cse)))
  570. bNewPairing = false;
  571. }
  572. if (bNewPairing)
  573. {
  574. memcpy(&m_pairingID, m_sigmaAlg.get_SK(), sizeof(m_pairingID));
  575. sgx_status_t seStatus = sgx_read_rand((uint8_t*)&m_pairingNonce, sizeof(m_pairingNonce));
  576. BREAK_IF_TRUE(SGX_SUCCESS != seStatus, status, PSE_PR_READ_RAND_ERROR);
  577. // LTPBlob.pairingNonce = 0 is used to indicate invalid pairing Info in the LTP blob.
  578. // Under the rare situation of a random number of 0 is returned for pairingNonce generation,
  579. // PSE-Pr declares pairing or re-pairing attempt failure. The next pairing/re-pairing attempt
  580. // most likely will generate a non-zero pairingNonce
  581. BREAK_IF_TRUE(memcmp(&m_pairingNonce, &zeroNonce, sizeof(Nonce128_t)) == 0, status, PSE_PR_READ_RAND_ERROR);
  582. // OutputOctets("VerifyM8 - new pairing", NULL, 0);
  583. }
  584. else
  585. {
  586. // OutputOctets("VerifyM8 - pS3_PR_cse matches pr_cse", NULL, 0);
  587. }
  588. //*********************************************************************
  589. // Update the unsealed pairing data
  590. // [VerifierPrivateKey, id_pse || id_cse || sk || mk ||
  591. // PairingNonce || SigRLVersion_cse || PrvRLVersion_cse ||
  592. // DalAppletVersion]
  593. //*********************************************************************
  594. memset(&pairing_data, 0, sizeof(pairing_data));
  595. memcpy(pairing_data.secret_data.VerifierPrivateKey, m_verifierPrivateKey, sizeof(EcDsaPrivKey));
  596. memcpy(pairing_data.secret_data.Id_cse, &Id_cse, sizeof(SHA256_HASH));
  597. memcpy(pairing_data.secret_data.Id_pse, &Id_pse, sizeof(SHA256_HASH));
  598. memcpy(pairing_data.secret_data.mk, m_sigmaAlg.get_MK(), sizeof(pairing_data.secret_data.mk));
  599. memcpy(pairing_data.secret_data.sk, m_sigmaAlg.get_SK(), sizeof(pairing_data.secret_data.sk));
  600. memcpy(pairing_data.secret_data.pairingID, m_pairingID, sizeof(m_pairingID));
  601. memcpy(pairing_data.secret_data.pairingNonce, m_pairingNonce, sizeof(Nonce128_t));
  602. pairing_data.plaintext.cse_sec_prop.ps_hw_gid = m_gid;
  603. pairing_data.plaintext.cse_sec_prop.ps_hw_sig_rlversion = m_nSigRLVersion;
  604. pairing_data.plaintext.cse_sec_prop.ps_hw_privkey_rlversion = m_nPrivRLVersion;
  605. //NRG: Definition of ME_TASK_INFO and PS_HW_SEC_INFO is still open
  606. // for SunrisePoint from TaskInfo of SIGMA1.1 message:
  607. // byte[ 0- 3] ME_TASK_INFO.TaskID, for SunrisePoint must be 8
  608. // byte[ 4- 7] Reserved, must be 0
  609. // byte[ 8-11] PSDA ID, mapped from the PSDA Applet ID in ME_TASK_INFO (1)
  610. // byte[12-15] PSDA SVN from ME_TASK_INFO
  611. // byte[16-31] Reserved, must be 0
  612. pairing_data.plaintext.cse_sec_prop.ps_hw_sec_info.taskId = pS3->TaskInfo.TaskId;
  613. pairing_data.plaintext.cse_sec_prop.ps_hw_sec_info.psdaId = 1;
  614. pairing_data.plaintext.cse_sec_prop.ps_hw_sec_info.psdaSvn = m_nDalAppletVersion;
  615. //NRG:
  616. // keep instance id
  617. memcpy(pairing_data.plaintext.pse_instance_id,
  618. pPairingBlob->plaintext.pse_instance_id,
  619. sizeof(pairing_data.plaintext.pse_instance_id));
  620. //*********************************************************************
  621. // Seal the pairing blob
  622. //*********************************************************************
  623. tmp_status = SealPairingBlob(&pairing_data, pPairingBlob);
  624. BREAK_IF_TRUE(AE_SUCCESS != tmp_status, status, tmp_status);
  625. *pbNewPairing = bNewPairing;
  626. //*********************************************************************
  627. // WE PASSED ALL BARRIERS TO SUCCESS
  628. //*********************************************************************
  629. status = AE_SUCCESS;
  630. m_nextState = STATE_DONE;
  631. } while (false);
  632. if (AE_FAILED(status))
  633. m_nextState = STATE_ERROR;
  634. /* Defense-in-depth: clear the data on stack that contains enclave secret.*/
  635. memset_s(&pairing_data, sizeof(pairing_data), 0, sizeof(pairing_data));
  636. delete[] m_pSigRL;
  637. m_pSigRL = NULL;
  638. m_nSigRL = 0;
  639. return map_VerifyM8_error_for_return(status);
  640. }
  641. bool TEpidSigma11Verifier::TaskInfoIsValid( const ME_TASK_INFO& taskInfo)
  642. {
  643. uint32_t taskInfoType = SwapEndian_DW(taskInfo.Hdr.Type);
  644. if (taskInfoType != ME_TASK) return false;
  645. //check TaskID and Applet ID according to SunrisePoint specification
  646. /* Check the TaskId matches the hardcoded JVM-On-ME Task ID */
  647. if (taskInfo.TaskId != JOM_TASK_ID) return false;
  648. /* Check the first 16 bytes of RsvdforApp matches the hardcoded PSDA Applet ID */
  649. if (memcmp(taskInfo.RsvdforApp, PSDA_APPLET_ID, DAL_APPLET_ID_LEN))
  650. {
  651. return false;
  652. }
  653. /* retrieve the PSDA SVN */
  654. memcpy(&m_nDalAppletVersion, (const_cast<uint8_t *>(taskInfo.RsvdforApp) + DAL_APPLET_ID_LEN), DAL_APPLET_SVN_LEN);
  655. return true;
  656. }
  657. ae_error_t TEpidSigma11Verifier::ValidateS3DataBlock(const SIGMA_S3_MESSAGE* pS3, uint32_t nLen_S3, X509_GROUP_CERTIFICATE_VLR** X509GroupCertVlr, EPID_SIGNATURE_VLR** EpidSigVlr)
  658. {
  659. X509_GROUP_CERTIFICATE_VLR* pX;
  660. EPID_SIGNATURE_VLR* pE;
  661. uint32_t data_offset = offsetof(SIGMA_S3_MESSAGE, Data);
  662. if (NULL == pS3 || NULL == X509GroupCertVlr || NULL == EpidSigVlr)
  663. return AESM_PSE_PR_BAD_POINTER_ERROR;
  664. // Make sure certificate is within bounds of S3 message allocated in trusted memory
  665. if (data_offset + sizeof(X509_GROUP_CERTIFICATE_VLR) >= nLen_S3)
  666. return PSE_PR_S3_DATA_ERROR;
  667. pX = (X509_GROUP_CERTIFICATE_VLR *)(((uint8_t*)pS3) + data_offset);
  668. // Make sure epid signature VLR is within bounds of S3 message allocated in trusted memory
  669. if ((data_offset + sizeof(EPID_SIGNATURE_VLR) + pX->VlrHeader.Length) >= nLen_S3)
  670. return PSE_PR_S3_DATA_ERROR;
  671. pE = (EPID_SIGNATURE_VLR*)((UINT8*)(pX) + pX->VlrHeader.Length);
  672. // Make sure epid signature data is within bounds of S3 message allocated in trusted memory
  673. if ((data_offset + pX->VlrHeader.Length + pE->VlrHeader.Length) >= nLen_S3)
  674. return PSE_PR_S3_DATA_ERROR;
  675. *X509GroupCertVlr = pX;
  676. *EpidSigVlr = pE;
  677. return AE_SUCCESS;
  678. }
  679. ae_error_t TEpidSigma11Verifier::AddCertificateChain(SIGMA_S2_MESSAGE* pS2,
  680. size_t& index, size_t nMaxS2, const UINT8* pCertChain, size_t nCertChain)
  681. {
  682. ae_error_t status = PSE_PR_INTERNAL_ERROR;
  683. do
  684. {
  685. if (nMaxS2 < ((pS2->Data - (uint8_t*)pS2) + index + nCertChain))
  686. break;
  687. memcpy((pS2->Data + index), pCertChain, nCertChain);
  688. index += nCertChain;
  689. status = AE_SUCCESS;
  690. } while (false);
  691. return status;
  692. }
  693. ae_error_t TEpidSigma11Verifier::AddRevocationList(SIGMA_S2_MESSAGE* pS2,
  694. size_t& index, size_t nMaxS2, const EPID11_SIG_RL* pRL, uint32_t nSigRL)
  695. {
  696. ae_error_t status = PSE_PR_INTERNAL_ERROR;
  697. do
  698. {
  699. if (NULL != m_pSigRL)
  700. delete [] m_pSigRL;
  701. m_nSigRL = 0;
  702. m_pSigRL = NULL;
  703. if (nSigRL > 0)
  704. {
  705. m_nSigRL = nSigRL;
  706. m_pSigRL = new (std::nothrow) UINT8[m_nSigRL];
  707. BREAK_IF_TRUE( (NULL == m_pSigRL), status,
  708. PSE_PR_INSUFFICIENT_MEMORY_ERROR);
  709. int nPaddedBytes = REQUIRED_PADDING_DWORD_ALIGNMENT(m_nSigRL);
  710. memcpy(m_pSigRL, pRL , m_nSigRL);
  711. SIGNATURE_REV_LIST_VLR sigRL_VLR;
  712. sigRL_VLR.VlrHeader.ID = SIGNATURE_REVOCATION_LIST_VLR_ID;
  713. sigRL_VLR.VlrHeader.PaddedBytes = (uint8_t)nPaddedBytes;
  714. if (sizeof(SIGMA_VLR_HEADER) + nPaddedBytes + m_nSigRL > UINT16_MAX)
  715. break;
  716. sigRL_VLR.VlrHeader.Length = (uint16_t)(sizeof(SIGMA_VLR_HEADER) + nPaddedBytes + m_nSigRL);
  717. if (nMaxS2 < ((pS2->Data - (uint8_t*)pS2) + index + nSigRL + sizeof(SIGNATURE_REV_LIST_VLR)))
  718. break;
  719. memcpy((pS2->Data + index), &sigRL_VLR, sizeof(SIGNATURE_REV_LIST_VLR));
  720. index += sizeof(SIGNATURE_REV_LIST_VLR);
  721. memcpy((pS2->Data + index), m_pSigRL, m_nSigRL);
  722. index += m_nSigRL;
  723. }
  724. status = AE_SUCCESS;
  725. } while (false);
  726. return status;
  727. }
  728. ae_error_t TEpidSigma11Verifier::AddOcspResponses(SIGMA_S2_MESSAGE* pS2,
  729. size_t& index, size_t nMaxS2, const UINT8* pOcspResp, size_t nOcspResp)
  730. {
  731. ae_error_t status = PSE_PR_INTERNAL_ERROR;
  732. do
  733. {
  734. if (pS2->OcspReq.ReqType == NO_OCSP)
  735. {
  736. status = AE_SUCCESS;
  737. break;
  738. }
  739. BREAK_IF_TRUE( (0 == nOcspResp), status ,
  740. PSE_PR_NO_OCSP_RESPONSE_ERROR);
  741. if (nMaxS2 < ((pS2->Data - (uint8_t*)pS2) + index + nOcspResp))
  742. break;
  743. memcpy((pS2->Data+index), pOcspResp, nOcspResp);
  744. index += nOcspResp;
  745. status = AE_SUCCESS;
  746. } while (false);
  747. return status;
  748. }
  749. ae_error_t TEpidSigma11Verifier::ValidateSigRL(const EPID11_SIG_RL* pSigRL, uint32_t sigRL_entries, uint32_t sigRL_size, uint32_t* pVersion)
  750. {
  751. sgx_ecc_state_handle_t ivk_ecc_handle = NULL;
  752. uint8_t result;
  753. ae_error_t status = PSE_PR_MSG_COMPARE_ERROR;
  754. if (NULL == pVersion)
  755. return PSE_PR_BAD_POINTER_ERROR;
  756. *pVersion = 0;
  757. if (0 == sigRL_size || NULL == pSigRL)
  758. return AE_SUCCESS;
  759. do
  760. {
  761. uint32_t nBaseSigRL_size = sigRL_size - EPID11_SIG_RL_SIGNATURE_SIZE;
  762. if (sigRL_entries > MAX_SIGRL_ENTRIES)
  763. break;
  764. uint8_t* p_rl_version = const_cast<uint8_t*>(pSigRL->rl_version);
  765. *pVersion = SwapEndian_DW(*reinterpret_cast<UINT32*>(p_rl_version));
  766. sgx_status_t sgx_status = sgx_ecc256_open_context(&ivk_ecc_handle);
  767. BREAK_IF_TRUE((SGX_ERROR_OUT_OF_MEMORY == sgx_status), status, PSE_PR_INSUFFICIENT_MEMORY_ERROR);
  768. BREAK_IF_TRUE((SGX_SUCCESS != sgx_status), status, PSE_PR_MSG_COMPARE_ERROR);
  769. //Convert the big endian signature in the Cert to little endian
  770. uint8_t ecc_sig[ECDSA_SIG_LENGTH ];
  771. memcpy(ecc_sig, (uint8_t*)pSigRL + nBaseSigRL_size, ECDSA_SIG_LENGTH );
  772. SwapEndian_32B(ecc_sig);
  773. SwapEndian_32B(&(ecc_sig[32]));
  774. const uint8_t** pEpidVerifyKeys = Keys::EpidVerifyKeys();
  775. for (uint32_t i = 0; i < Keys::EpidVerifyKeyNum(); i++)
  776. {
  777. sgx_status = sgx_ecdsa_verify((uint8_t*)pSigRL,
  778. nBaseSigRL_size,
  779. (sgx_ec256_public_t *)(pEpidVerifyKeys[i]), /* requiring little endian format */
  780. (sgx_ec256_signature_t *)ecc_sig,
  781. &result,
  782. ivk_ecc_handle);
  783. if (sgx_status == SGX_SUCCESS && result == SGX_EC_VALID)
  784. break;
  785. }
  786. BREAK_IF_TRUE((SGX_ERROR_OUT_OF_MEMORY == sgx_status), status, PSE_PR_INSUFFICIENT_MEMORY_ERROR);
  787. BREAK_IF_TRUE((SGX_SUCCESS != sgx_status), status, PSE_PR_MSG_COMPARE_ERROR);
  788. BREAK_IF_TRUE((SGX_EC_VALID != result), status, PSE_PR_MSG_COMPARE_ERROR);
  789. status = AE_SUCCESS;
  790. } while (false);
  791. if (ivk_ecc_handle != NULL) sgx_ecc256_close_context(ivk_ecc_handle);
  792. return status;
  793. }
  794. ae_error_t TEpidSigma11Verifier::ValidatePrivRL(const EPID11_PRIV_RL* pPrivRL, uint32_t privRL_entries, uint32_t privRL_size, uint32_t* pVersion)
  795. {
  796. sgx_ecc_state_handle_t ivk_ecc_handle = NULL;
  797. uint8_t result;
  798. ae_error_t status = PSE_PR_MSG_COMPARE_ERROR;
  799. if (NULL == pVersion)
  800. return PSE_PR_BAD_POINTER_ERROR;
  801. *pVersion = 0;
  802. if (0 == privRL_size || NULL == pPrivRL)
  803. return AE_SUCCESS;
  804. do
  805. {
  806. uint32_t nBasePrivRL_size = privRL_size - EPID11_PRIV_RL_SIGNATURE_SIZE;
  807. if (privRL_entries > MAX_SIGRL_ENTRIES)
  808. break;
  809. uint8_t* p_rl_version = const_cast<uint8_t*>(pPrivRL->rl_version);
  810. *pVersion = SwapEndian_DW(*reinterpret_cast<UINT32*>(p_rl_version));
  811. sgx_status_t sgx_status = sgx_ecc256_open_context(&ivk_ecc_handle);
  812. BREAK_IF_TRUE((SGX_ERROR_OUT_OF_MEMORY == sgx_status), status, PSE_PR_INSUFFICIENT_MEMORY_ERROR);
  813. BREAK_IF_TRUE((SGX_SUCCESS != sgx_status), status, PSE_PR_MSG_COMPARE_ERROR);
  814. //Convert the big endian signature in the Cert to little endian
  815. uint8_t ecc_sig[ECDSA_SIG_LENGTH];
  816. memcpy(ecc_sig, (uint8_t*)pPrivRL + nBasePrivRL_size, ECDSA_SIG_LENGTH);
  817. SwapEndian_32B(ecc_sig);
  818. SwapEndian_32B(&(ecc_sig[32]));
  819. const uint8_t** pEpidVerifyKeys = Keys::EpidVerifyKeys();
  820. for (uint32_t i = 0; i < Keys::EpidVerifyKeyNum(); i++)
  821. {
  822. sgx_status = sgx_ecdsa_verify((uint8_t*)pPrivRL,
  823. nBasePrivRL_size,
  824. (sgx_ec256_public_t *)(pEpidVerifyKeys[i]), /* requiring little endian format */
  825. (sgx_ec256_signature_t *)ecc_sig,
  826. &result,
  827. ivk_ecc_handle);
  828. if (sgx_status == SGX_SUCCESS && result == SGX_EC_VALID)
  829. break;
  830. }
  831. BREAK_IF_TRUE((SGX_ERROR_OUT_OF_MEMORY == sgx_status), status, PSE_PR_INSUFFICIENT_MEMORY_ERROR);
  832. BREAK_IF_TRUE((SGX_SUCCESS != sgx_status), status, PSE_PR_MSG_COMPARE_ERROR);
  833. BREAK_IF_TRUE((SGX_EC_VALID != result), status, PSE_PR_MSG_COMPARE_ERROR);
  834. status = AE_SUCCESS;
  835. } while (false);
  836. if (ivk_ecc_handle != NULL) sgx_ecc256_close_context(ivk_ecc_handle);
  837. return status;
  838. }