t_long_term_pairing.cpp 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045
  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. memcpy(SerializedPublicKey, (EcDsaPubKey*)Keys::EpidVerifyKey(), SIGMA_SESSION_PUBKEY_LENGTH);
  489. SwapEndian_32B(SerializedPublicKey);
  490. SwapEndian_32B(&(SerializedPublicKey[32]));
  491. if (0 != X509Parser::ParseGroupCertificate( /*in */ (EcDsaPubKey*)SerializedPublicKey,
  492. /*in */ X509GroupCertVlr, /*out*/ &S3GID, /*out*/ &groupPubKey ))
  493. {
  494. status = PSE_PR_X509_PARSE_ERROR;
  495. break;
  496. }
  497. BREAK_IF_FALSE((S3GID == m_gid), status, PSE_PR_GID_MISMATCH_ERROR );
  498. //*********************************************************************
  499. // Verify PrivRL
  500. //*********************************************************************
  501. tmp_status = ValidatePrivRL(pPrivRL, privRL_entries, privRL_size, &m_nPrivRLVersion);
  502. BREAK_IF_TRUE(AE_SUCCESS != tmp_status, status, tmp_status);
  503. KeysToSign_t combinedKeys;
  504. memset(&combinedKeys, 0, sizeof(combinedKeys));
  505. // Combine over g^a || g^b to the struct
  506. memcpy(combinedKeys.first, m_sigmaAlg.get_remote_pub_key_ga_be(),
  507. SIGMA_SESSION_PUBKEY_LENGTH);
  508. memcpy(combinedKeys.second, m_sigmaAlg.get_pub_key_gb_be(),
  509. SIGMA_SESSION_PUBKEY_LENGTH);
  510. //*********************************************************************
  511. // the input pPrivRL has a type definition of EPID11_PRIV_RL,
  512. // but the epid-sdk-3.0 library takes Epid11PrivRl as input parameter.
  513. // EPID11_PRIV_RL has 4 addtional bytes at the header so we offset the pointer
  514. // by 4 bytes . Also we need to exclude RL signature because epid-sdk3.0
  515. // checks the RL's size shouldn't include the signature. Similar for SigRL and GroupRL.
  516. //*********************************************************************
  517. uint8_t* pEpid11PrivRL = (pPrivRL == NULL)? NULL:(uint8_t*)pPrivRL+RL_OFFSET;
  518. uint32_t nEpid11PrivRLSize = (pPrivRL == NULL)? 0:privRL_size-RL_OFFSET-ECDSA_SIG_LENGTH;
  519. uint8_t* pEpid11SigRL = (m_pSigRL == NULL)? NULL:m_pSigRL+RL_OFFSET;
  520. uint32_t nEpid11SigRLSize = (m_pSigRL == NULL)? 0:m_nSigRL-RL_OFFSET-ECDSA_SIG_LENGTH;
  521. tmp_status = m_sigmaAlg.MsgVerifyPch((UINT8 *)&groupPubKey,
  522. (uint32_t)(sizeof(EpidCert) - ECDSA_SIG_LENGTH),
  523. NULL, // not required for EPID SDK 3.0
  524. (Ipp8u*)&combinedKeys,
  525. (uint32_t)sizeof(combinedKeys),
  526. NULL, // Bsn
  527. 0, // BsnLen
  528. (UINT8 *)EpidSigVlr->EpidSig,
  529. VLR_UNPADDED_PAYLOAD_SIZE(EpidSigVlr->VlrHeader),
  530. pEpid11PrivRL, nEpid11PrivRLSize, // PrivRL
  531. pEpid11SigRL, nEpid11SigRLSize, // SigRL
  532. NULL, 0); // GroupRL
  533. BREAK_IF_TRUE(AE_SUCCESS != tmp_status, status, tmp_status);
  534. //*********************************************************************
  535. // Calculate Id_pse and Id_cse
  536. // Id_pse = hash(sk || mk || 1)
  537. // Id_cse = hash(sk || mk || 2)
  538. //*********************************************************************
  539. SHA256_HASH Id_pse = {0};
  540. SHA256_HASH Id_cse = {0};
  541. tmp_status = m_sigmaAlg.ComputeId(1, &Id_pse);
  542. BREAK_IF_TRUE(AE_SUCCESS != tmp_status, status, tmp_status);
  543. tmp_status = m_sigmaAlg.ComputeId(2, &Id_cse);
  544. BREAK_IF_TRUE(AE_SUCCESS != tmp_status, status, tmp_status);
  545. //*********************************************************************
  546. // Verify Pr_cse, where Pr_cse is HMAC_SHA256(MK, OLD_SK || 0x02)
  547. // if OLD_SK is available, or 256-bit 0x0 if OLD_SK is not available
  548. //*********************************************************************
  549. size_t nSizePr = sizeof(PR_PSE_T);
  550. PR_PSE_T *pS3_PR_cse = (PR_PSE_T*)((const uint8_t*)pS3 + nLen_S3 - nSizePr);
  551. // OutputOctets("S3", pS3, nLen_S3);
  552. // OutputOctets("VerifyM8 - pS3_PR_cse", pS3_PR_cse, nSizePr);
  553. bNewPairing = true;
  554. PR_PSE_T pr_cse = {0};
  555. const Nonce128_t zeroNonce = {0};
  556. if (0 != memcmp(&pr_cse, pS3_PR_cse, sizeof(pr_cse)) && 0 != memcmp(&m_pairingNonce, &zeroNonce, sizeof(Nonce128_t)))
  557. {
  558. tmp_status = m_sigmaAlg.ComputePR(&m_pairingID, 0x02, (SIGMA_HMAC*)pr_cse);
  559. BREAK_IF_TRUE(AE_SUCCESS != tmp_status, status, tmp_status);
  560. // OutputOctets("::VerifyM8:: Computed Pr HMAC[(m_pairingID || 0x02)] using m_Mk", pr_cse, nSizePr);
  561. if (0 == memcmp(&pr_cse, pS3_PR_cse, sizeof(pr_cse)))
  562. bNewPairing = false;
  563. }
  564. if (bNewPairing)
  565. {
  566. memcpy(&m_pairingID, m_sigmaAlg.get_SK(), sizeof(m_pairingID));
  567. sgx_status_t seStatus = sgx_read_rand((uint8_t*)&m_pairingNonce, sizeof(m_pairingNonce));
  568. BREAK_IF_TRUE(SGX_SUCCESS != seStatus, status, PSE_PR_READ_RAND_ERROR);
  569. // LTPBlob.pairingNonce = 0 is used to indicate invalid pairing Info in the LTP blob.
  570. // Under the rare situation of a random number of 0 is returned for pairingNonce generation,
  571. // PSE-Pr declares pairing or re-pairing attempt failure. The next pairing/re-pairing attempt
  572. // most likely will generate a non-zero pairingNonce
  573. BREAK_IF_TRUE(memcmp(&m_pairingNonce, &zeroNonce, sizeof(Nonce128_t)) == 0, status, PSE_PR_READ_RAND_ERROR);
  574. // OutputOctets("VerifyM8 - new pairing", NULL, 0);
  575. }
  576. else
  577. {
  578. // OutputOctets("VerifyM8 - pS3_PR_cse matches pr_cse", NULL, 0);
  579. }
  580. //*********************************************************************
  581. // Update the unsealed pairing data
  582. // [VerifierPrivateKey, id_pse || id_cse || sk || mk ||
  583. // PairingNonce || SigRLVersion_cse || PrvRLVersion_cse ||
  584. // DalAppletVersion]
  585. //*********************************************************************
  586. memset(&pairing_data, 0, sizeof(pairing_data));
  587. memcpy(pairing_data.secret_data.VerifierPrivateKey, m_verifierPrivateKey, sizeof(EcDsaPrivKey));
  588. memcpy(pairing_data.secret_data.Id_cse, &Id_cse, sizeof(SHA256_HASH));
  589. memcpy(pairing_data.secret_data.Id_pse, &Id_pse, sizeof(SHA256_HASH));
  590. memcpy(pairing_data.secret_data.mk, m_sigmaAlg.get_MK(), sizeof(pairing_data.secret_data.mk));
  591. memcpy(pairing_data.secret_data.sk, m_sigmaAlg.get_SK(), sizeof(pairing_data.secret_data.sk));
  592. memcpy(pairing_data.secret_data.pairingID, m_pairingID, sizeof(m_pairingID));
  593. memcpy(pairing_data.secret_data.pairingNonce, m_pairingNonce, sizeof(Nonce128_t));
  594. pairing_data.plaintext.cse_sec_prop.ps_hw_gid = m_gid;
  595. pairing_data.plaintext.cse_sec_prop.ps_hw_sig_rlversion = m_nSigRLVersion;
  596. pairing_data.plaintext.cse_sec_prop.ps_hw_privkey_rlversion = m_nPrivRLVersion;
  597. //NRG: Definition of ME_TASK_INFO and PS_HW_SEC_INFO is still open
  598. // for SunrisePoint from TaskInfo of SIGMA1.1 message:
  599. // byte[ 0- 3] ME_TASK_INFO.TaskID, for SunrisePoint must be 8
  600. // byte[ 4- 7] Reserved, must be 0
  601. // byte[ 8-11] PSDA ID, mapped from the PSDA Applet ID in ME_TASK_INFO (1)
  602. // byte[12-15] PSDA SVN from ME_TASK_INFO
  603. // byte[16-31] Reserved, must be 0
  604. pairing_data.plaintext.cse_sec_prop.ps_hw_sec_info.taskId = pS3->TaskInfo.TaskId;
  605. pairing_data.plaintext.cse_sec_prop.ps_hw_sec_info.psdaId = 1;
  606. pairing_data.plaintext.cse_sec_prop.ps_hw_sec_info.psdaSvn = m_nDalAppletVersion;
  607. //NRG:
  608. // keep instance id
  609. memcpy(pairing_data.plaintext.pse_instance_id,
  610. pPairingBlob->plaintext.pse_instance_id,
  611. sizeof(pairing_data.plaintext.pse_instance_id));
  612. //*********************************************************************
  613. // Seal the pairing blob
  614. //*********************************************************************
  615. tmp_status = SealPairingBlob(&pairing_data, pPairingBlob);
  616. BREAK_IF_TRUE(AE_SUCCESS != tmp_status, status, tmp_status);
  617. *pbNewPairing = bNewPairing;
  618. //*********************************************************************
  619. // WE PASSED ALL BARRIERS TO SUCCESS
  620. //*********************************************************************
  621. status = AE_SUCCESS;
  622. m_nextState = STATE_DONE;
  623. } while (false);
  624. if (AE_FAILED(status))
  625. m_nextState = STATE_ERROR;
  626. /* Defense-in-depth: clear the data on stack that contains enclave secret.*/
  627. memset_s(&pairing_data, sizeof(pairing_data), 0, sizeof(pairing_data));
  628. delete[] m_pSigRL;
  629. m_pSigRL = NULL;
  630. m_nSigRL = 0;
  631. return map_VerifyM8_error_for_return(status);
  632. }
  633. bool TEpidSigma11Verifier::TaskInfoIsValid( const ME_TASK_INFO& taskInfo)
  634. {
  635. uint32_t taskInfoType = SwapEndian_DW(taskInfo.Hdr.Type);
  636. if (taskInfoType != ME_TASK) return false;
  637. //check TaskID and Applet ID according to SunrisePoint specification
  638. /* Check the TaskId matches the hardcoded JVM-On-ME Task ID */
  639. if (taskInfo.TaskId != JOM_TASK_ID) return false;
  640. /* Check the first 16 bytes of RsvdforApp matches the hardcoded PSDA Applet ID */
  641. if (memcmp(taskInfo.RsvdforApp, PSDA_APPLET_ID, DAL_APPLET_ID_LEN))
  642. {
  643. return false;
  644. }
  645. /* retrieve the PSDA SVN */
  646. memcpy(&m_nDalAppletVersion, (const_cast<uint8_t *>(taskInfo.RsvdforApp) + DAL_APPLET_ID_LEN), DAL_APPLET_SVN_LEN);
  647. return true;
  648. }
  649. ae_error_t TEpidSigma11Verifier::ValidateS3DataBlock(const SIGMA_S3_MESSAGE* pS3, uint32_t nLen_S3, X509_GROUP_CERTIFICATE_VLR** X509GroupCertVlr, EPID_SIGNATURE_VLR** EpidSigVlr)
  650. {
  651. X509_GROUP_CERTIFICATE_VLR* pX;
  652. EPID_SIGNATURE_VLR* pE;
  653. uint32_t data_offset = offsetof(SIGMA_S3_MESSAGE, Data);
  654. if (NULL == pS3 || NULL == X509GroupCertVlr || NULL == EpidSigVlr)
  655. return AESM_PSE_PR_BAD_POINTER_ERROR;
  656. // Make sure certificate is within bounds of S3 message allocated in trusted memory
  657. if (data_offset + sizeof(X509_GROUP_CERTIFICATE_VLR) >= nLen_S3)
  658. return PSE_PR_S3_DATA_ERROR;
  659. pX = (X509_GROUP_CERTIFICATE_VLR *)(((uint8_t*)pS3) + data_offset);
  660. // Make sure epid signature VLR is within bounds of S3 message allocated in trusted memory
  661. if ((data_offset + sizeof(EPID_SIGNATURE_VLR) + pX->VlrHeader.Length) >= nLen_S3)
  662. return PSE_PR_S3_DATA_ERROR;
  663. pE = (EPID_SIGNATURE_VLR*)((UINT8*)(pX) + pX->VlrHeader.Length);
  664. // Make sure epid signature data is within bounds of S3 message allocated in trusted memory
  665. if ((data_offset + pX->VlrHeader.Length + pE->VlrHeader.Length) >= nLen_S3)
  666. return PSE_PR_S3_DATA_ERROR;
  667. *X509GroupCertVlr = pX;
  668. *EpidSigVlr = pE;
  669. return AE_SUCCESS;
  670. }
  671. ae_error_t TEpidSigma11Verifier::AddCertificateChain(SIGMA_S2_MESSAGE* pS2,
  672. size_t& index, size_t nMaxS2, const UINT8* pCertChain, size_t nCertChain)
  673. {
  674. ae_error_t status = PSE_PR_INTERNAL_ERROR;
  675. do
  676. {
  677. if (nMaxS2 < ((pS2->Data - (uint8_t*)pS2) + index + nCertChain))
  678. break;
  679. memcpy((pS2->Data + index), pCertChain, nCertChain);
  680. index += nCertChain;
  681. status = AE_SUCCESS;
  682. } while (false);
  683. return status;
  684. }
  685. ae_error_t TEpidSigma11Verifier::AddRevocationList(SIGMA_S2_MESSAGE* pS2,
  686. size_t& index, size_t nMaxS2, const EPID11_SIG_RL* pRL, uint32_t nSigRL)
  687. {
  688. ae_error_t status = PSE_PR_INTERNAL_ERROR;
  689. do
  690. {
  691. if (NULL != m_pSigRL)
  692. delete [] m_pSigRL;
  693. m_nSigRL = 0;
  694. m_pSigRL = NULL;
  695. if (nSigRL > 0)
  696. {
  697. m_nSigRL = nSigRL;
  698. m_pSigRL = new (std::nothrow) UINT8[m_nSigRL];
  699. BREAK_IF_TRUE( (NULL == m_pSigRL), status,
  700. PSE_PR_INSUFFICIENT_MEMORY_ERROR);
  701. int nPaddedBytes = REQUIRED_PADDING_DWORD_ALIGNMENT(m_nSigRL);
  702. memcpy(m_pSigRL, pRL , m_nSigRL);
  703. SIGNATURE_REV_LIST_VLR sigRL_VLR;
  704. sigRL_VLR.VlrHeader.ID = SIGNATURE_REVOCATION_LIST_VLR_ID;
  705. sigRL_VLR.VlrHeader.PaddedBytes = (uint8_t)nPaddedBytes;
  706. if (sizeof(SIGMA_VLR_HEADER) + nPaddedBytes + m_nSigRL > UINT16_MAX)
  707. break;
  708. sigRL_VLR.VlrHeader.Length = (uint16_t)(sizeof(SIGMA_VLR_HEADER) + nPaddedBytes + m_nSigRL);
  709. if (nMaxS2 < ((pS2->Data - (uint8_t*)pS2) + index + nSigRL + sizeof(SIGNATURE_REV_LIST_VLR)))
  710. break;
  711. memcpy((pS2->Data + index), &sigRL_VLR, sizeof(SIGNATURE_REV_LIST_VLR));
  712. index += sizeof(SIGNATURE_REV_LIST_VLR);
  713. memcpy((pS2->Data + index), m_pSigRL, m_nSigRL);
  714. index += m_nSigRL;
  715. }
  716. status = AE_SUCCESS;
  717. } while (false);
  718. return status;
  719. }
  720. ae_error_t TEpidSigma11Verifier::AddOcspResponses(SIGMA_S2_MESSAGE* pS2,
  721. size_t& index, size_t nMaxS2, const UINT8* pOcspResp, size_t nOcspResp)
  722. {
  723. ae_error_t status = PSE_PR_INTERNAL_ERROR;
  724. do
  725. {
  726. if (pS2->OcspReq.ReqType == NO_OCSP)
  727. {
  728. status = AE_SUCCESS;
  729. break;
  730. }
  731. BREAK_IF_TRUE( (0 == nOcspResp), status ,
  732. PSE_PR_NO_OCSP_RESPONSE_ERROR);
  733. if (nMaxS2 < ((pS2->Data - (uint8_t*)pS2) + index + nOcspResp))
  734. break;
  735. memcpy((pS2->Data+index), pOcspResp, nOcspResp);
  736. index += nOcspResp;
  737. status = AE_SUCCESS;
  738. } while (false);
  739. return status;
  740. }
  741. ae_error_t TEpidSigma11Verifier::ValidateSigRL(const EPID11_SIG_RL* pSigRL, uint32_t sigRL_entries, uint32_t sigRL_size, uint32_t* pVersion)
  742. {
  743. sgx_ecc_state_handle_t ivk_ecc_handle = NULL;
  744. uint8_t result;
  745. ae_error_t status = PSE_PR_MSG_COMPARE_ERROR;
  746. if (NULL == pVersion)
  747. return PSE_PR_BAD_POINTER_ERROR;
  748. *pVersion = 0;
  749. if (0 == sigRL_size || NULL == pSigRL)
  750. return AE_SUCCESS;
  751. do
  752. {
  753. uint32_t nBaseSigRL_size = sigRL_size - EPID11_SIG_RL_SIGNATURE_SIZE;
  754. if (sigRL_entries > MAX_SIGRL_ENTRIES)
  755. break;
  756. uint8_t* p_rl_version = const_cast<uint8_t*>(pSigRL->rl_version);
  757. *pVersion = SwapEndian_DW(*reinterpret_cast<UINT32*>(p_rl_version));
  758. sgx_status_t sgx_status = sgx_ecc256_open_context(&ivk_ecc_handle);
  759. BREAK_IF_TRUE((SGX_ERROR_OUT_OF_MEMORY == sgx_status), status, PSE_PR_INSUFFICIENT_MEMORY_ERROR);
  760. BREAK_IF_TRUE((SGX_SUCCESS != sgx_status), status, PSE_PR_MSG_COMPARE_ERROR);
  761. //Convert the big endian signature in the Cert to little endian
  762. uint8_t ecc_sig[ECDSA_SIG_LENGTH ];
  763. memcpy(ecc_sig, (uint8_t*)pSigRL + nBaseSigRL_size, ECDSA_SIG_LENGTH );
  764. SwapEndian_32B(ecc_sig);
  765. SwapEndian_32B(&(ecc_sig[32]));
  766. sgx_status = sgx_ecdsa_verify((uint8_t*)pSigRL,
  767. nBaseSigRL_size,
  768. (sgx_ec256_public_t *)(Keys::EpidVerifyKey()), /* requiring little endian format */
  769. (sgx_ec256_signature_t *) ecc_sig,
  770. &result,
  771. ivk_ecc_handle);
  772. BREAK_IF_TRUE((SGX_ERROR_OUT_OF_MEMORY == sgx_status), status, PSE_PR_INSUFFICIENT_MEMORY_ERROR);
  773. BREAK_IF_TRUE((SGX_SUCCESS != sgx_status), status, PSE_PR_MSG_COMPARE_ERROR);
  774. BREAK_IF_TRUE((SGX_EC_VALID != result), status, PSE_PR_MSG_COMPARE_ERROR);
  775. status = AE_SUCCESS;
  776. } while (false);
  777. if (ivk_ecc_handle != NULL) sgx_ecc256_close_context(ivk_ecc_handle);
  778. return status;
  779. }
  780. ae_error_t TEpidSigma11Verifier::ValidatePrivRL(const EPID11_PRIV_RL* pPrivRL, uint32_t privRL_entries, uint32_t privRL_size, uint32_t* pVersion)
  781. {
  782. sgx_ecc_state_handle_t ivk_ecc_handle = NULL;
  783. uint8_t result;
  784. ae_error_t status = PSE_PR_MSG_COMPARE_ERROR;
  785. if (NULL == pVersion)
  786. return PSE_PR_BAD_POINTER_ERROR;
  787. *pVersion = 0;
  788. if (0 == privRL_size || NULL == pPrivRL)
  789. return AE_SUCCESS;
  790. do
  791. {
  792. uint32_t nBasePrivRL_size = privRL_size - EPID11_PRIV_RL_SIGNATURE_SIZE;
  793. if (privRL_entries > MAX_SIGRL_ENTRIES)
  794. break;
  795. uint8_t* p_rl_version = const_cast<uint8_t*>(pPrivRL->rl_version);
  796. *pVersion = SwapEndian_DW(*reinterpret_cast<UINT32*>(p_rl_version));
  797. sgx_status_t sgx_status = sgx_ecc256_open_context(&ivk_ecc_handle);
  798. BREAK_IF_TRUE((SGX_ERROR_OUT_OF_MEMORY == sgx_status), status, PSE_PR_INSUFFICIENT_MEMORY_ERROR);
  799. BREAK_IF_TRUE((SGX_SUCCESS != sgx_status), status, PSE_PR_MSG_COMPARE_ERROR);
  800. //Convert the big endian signature in the Cert to little endian
  801. uint8_t ecc_sig[ECDSA_SIG_LENGTH];
  802. memcpy(ecc_sig, (uint8_t*)pPrivRL + nBasePrivRL_size, ECDSA_SIG_LENGTH);
  803. SwapEndian_32B(ecc_sig);
  804. SwapEndian_32B(&(ecc_sig[32]));
  805. sgx_status = sgx_ecdsa_verify((uint8_t*)pPrivRL,
  806. nBasePrivRL_size,
  807. (sgx_ec256_public_t*)(Keys::EpidVerifyKey()), /* should hardcode it in little endian format */
  808. (sgx_ec256_signature_t *) ecc_sig,
  809. &result,
  810. ivk_ecc_handle);
  811. BREAK_IF_TRUE((SGX_ERROR_OUT_OF_MEMORY == sgx_status), status, PSE_PR_INSUFFICIENT_MEMORY_ERROR);
  812. BREAK_IF_TRUE((SGX_SUCCESS != sgx_status), status, PSE_PR_MSG_COMPARE_ERROR);
  813. BREAK_IF_TRUE((SGX_EC_VALID != result), status, PSE_PR_MSG_COMPARE_ERROR);
  814. status = AE_SUCCESS;
  815. } while (false);
  816. if (ivk_ecc_handle != NULL) sgx_ecc256_close_context(ivk_ecc_handle);
  817. return status;
  818. }