helper.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  1. /*
  2. * Copyright (C) 2011-2018 Intel Corporation. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. *
  8. * * Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * * Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in
  12. * the documentation and/or other materials provided with the
  13. * distribution.
  14. * * Neither the name of Intel Corporation nor the names of its
  15. * contributors may be used to endorse or promote products derived
  16. * from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  21. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  22. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  24. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  25. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  26. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  28. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. *
  30. */
  31. #include "helper.h"
  32. #include <Buffer.h>
  33. #include "aeerror.h"
  34. #include "pse_pr_sigma_common_defs.h"
  35. #include "oal/oal.h"
  36. #include "se_wrapper.h"
  37. #include <limits.h>
  38. #include <stdint.h>
  39. #include <stdio.h>
  40. #include <cstddef>
  41. #ifndef UINT16_MAX
  42. #define UINT16_MAX 0xFFFF
  43. #endif
  44. #ifndef INT32_MAX
  45. #define INT32_MAX 0x7FFFFFFF
  46. #endif
  47. #define CERT_FILENAME_POSTFIX_FORMAT "%02d.cer"
  48. #define TOKEN_SEPARATOR ';'
  49. #define SAFE_DELETE_ARRAY(x) if (x) { delete[] (unsigned char*)(x); (x) = NULL; }
  50. static char* get_next_token(char* _str, const char _delim, char** _context)
  51. {
  52. char* result;
  53. if (NULL == _context)
  54. return NULL;
  55. if (NULL == _str && NULL == *_context)
  56. return NULL;
  57. if (NULL == _str)
  58. _str = *_context;
  59. char *p = strchr(_str, _delim);
  60. if (NULL == p)
  61. {
  62. result = _str;
  63. *_context = NULL;
  64. }
  65. else
  66. {
  67. *p = '\0';
  68. result = _str;
  69. *_context = result + strlen(result) + 1;
  70. }
  71. return result;
  72. }
  73. bool Helper::noPseCert()
  74. {
  75. std::list<upse::Buffer> certChain;
  76. return AE_FAILED(Helper::LoadCertificateChain(certChain));
  77. }
  78. bool Helper::noLtpBlob()
  79. {
  80. upse::Buffer pairing_blob;
  81. return AE_FAILED(read_ltp_blob(pairing_blob));
  82. }
  83. ae_error_t Helper::read_ltp_blob(upse::Buffer& pairing_blob)
  84. {
  85. return upsePersistentStorage::Read(PSE_PR_LT_PAIRING_FID, pairing_blob);
  86. }
  87. ae_error_t Helper::read_ltp_blob(pairing_blob_t& pairing_blob)
  88. {
  89. upse::Buffer buffer_pairing_blob;
  90. memset(&pairing_blob, 0, sizeof(pairing_blob));
  91. ae_error_t status = read_ltp_blob(buffer_pairing_blob);
  92. if (AE_SUCCESS != status)
  93. return status;
  94. if (sizeof(pairing_blob) != buffer_pairing_blob.getSize())
  95. return AE_FAILURE;
  96. memcpy_s(&pairing_blob, sizeof(pairing_blob), buffer_pairing_blob.getData(), buffer_pairing_blob.getSize());
  97. return AE_SUCCESS;
  98. }
  99. ae_error_t Helper::write_ltp_blob(upse::Buffer& pairing_blob)
  100. {
  101. return upsePersistentStorage::Write(PSE_PR_LT_PAIRING_FID, pairing_blob);
  102. }
  103. ae_error_t Helper::delete_ltp_blob()
  104. {
  105. return upsePersistentStorage::Delete(PSE_PR_LT_PAIRING_FID);
  106. }
  107. ae_error_t Helper::read_ocsp_response_vlr(upse::Buffer& ocsp_response_vlr)
  108. {
  109. return upsePersistentStorage::Read(PSE_PR_OCSPRESP_FID, ocsp_response_vlr);
  110. }
  111. ae_error_t Helper::write_ocsp_response_vlr(upse::Buffer& ocsp_response_vlr)
  112. {
  113. return upsePersistentStorage::Write(PSE_PR_OCSPRESP_FID, ocsp_response_vlr);
  114. }
  115. ae_error_t Helper::delete_ocsp_response_vlr()
  116. {
  117. return upsePersistentStorage::Delete(PSE_PR_OCSPRESP_FID);
  118. }
  119. uint32_t Helper::ltpBlobPsdaSvn(const pairing_blob_t& pairing_blob)
  120. {
  121. uint32_t retval = pairing_blob.plaintext.cse_sec_prop.ps_hw_sec_info.psdaSvn;
  122. SGX_DBGPRINT_ONE_STRING_TWO_INTS_CREATE_SESSION(__FUNCTION__" returning ", retval, retval);
  123. return retval;
  124. }
  125. uint32_t Helper::ltpBlobCseGid(const pairing_blob_t& pairing_blob)
  126. {
  127. return *const_cast<uint32_t*>(&pairing_blob.plaintext.cse_sec_prop.ps_hw_gid);
  128. }
  129. #define REQUIRED_PADDING_DWORD_ALIGNMENT(x) ((x % 4) ? (4 - (x%4)) : 0)
  130. ae_error_t Helper::RemoveCertificateChain()
  131. {
  132. ae_error_t status = AESM_PSE_PR_CERT_DELETE_ERROR;
  133. char* szParseString = NULL;
  134. int nError = 0;
  135. do
  136. {
  137. // Read the delimited file of certificate names
  138. upse::Buffer certChainListBuffer;
  139. if (AE_SUCCESS != upsePersistentStorage::Read(PSE_PR_CERTIFICATE_CHAIN_FID, certChainListBuffer))
  140. {
  141. break;
  142. }
  143. szParseString = (char*) calloc(1, certChainListBuffer.getSize() + 1);
  144. if (NULL == szParseString)
  145. {
  146. break;
  147. }
  148. memcpy_s(szParseString, certChainListBuffer.getSize(), certChainListBuffer.getData(), certChainListBuffer.getSize());
  149. szParseString[certChainListBuffer.getSize()] = '\0';
  150. char* nextToken = NULL;
  151. char* szCertificateNamePostfix = get_next_token(szParseString, TOKEN_SEPARATOR, &nextToken);
  152. aesm_data_id_t fileid = PSE_PR_CERTIFICATE_FID;
  153. // For each certificate name, delete the file
  154. while (NULL != szCertificateNamePostfix)
  155. {
  156. if (AE_SUCCESS != upsePersistentStorage::Delete(fileid++))
  157. {
  158. ++nError;
  159. }
  160. if (PSE_PR_CERTIFICATE_FID_MAX == fileid)
  161. {
  162. break;
  163. }
  164. szCertificateNamePostfix = get_next_token(NULL, TOKEN_SEPARATOR, &nextToken);
  165. }
  166. if (PSE_PR_CERTIFICATE_FID_MAX == fileid)
  167. {
  168. break;
  169. }
  170. if (AE_SUCCESS != upsePersistentStorage::Delete(PSE_PR_CERTIFICATE_CHAIN_FID))
  171. {
  172. ++nError;
  173. }
  174. if (0 != nError)
  175. {
  176. break;
  177. }
  178. status = AE_SUCCESS;
  179. } while (0);
  180. if (NULL != szParseString)
  181. {
  182. free(szParseString);
  183. }
  184. SGX_DBGPRINT_PRINT_FUNCTION_AND_RETURNVAL(__FUNCTION__, status);
  185. return status;
  186. }
  187. ae_error_t Helper::SaveCertificateChain( /*in */ std::list<upse::Buffer>& certChain)
  188. {
  189. ae_error_t status = AESM_PSE_PR_CERT_SAVE_ERROR;
  190. char* szParseString = NULL;
  191. int nLen, pos=0;
  192. do
  193. {
  194. RemoveCertificateChain();
  195. // Allocate enough space for the list of names and separator character
  196. if(((size_t)(INT32_MAX-1))/sizeof(CERT_FILENAME_POSTFIX_FORMAT) < certChain.size() )
  197. break;
  198. int nBytes = static_cast<int>(certChain.size() * sizeof(CERT_FILENAME_POSTFIX_FORMAT)) + 1;
  199. szParseString = (char*)calloc(1, nBytes);
  200. if (NULL == szParseString)
  201. {
  202. break;
  203. }
  204. char* szNextCertNamePostfix = szParseString;
  205. int fileNo = 0;
  206. char szFilenamePostfix[80];
  207. std::list<upse::Buffer>::iterator it = certChain.begin();
  208. aesm_data_id_t fileid = PSE_PR_CERTIFICATE_FID;
  209. while (it != certChain.end())
  210. {
  211. nLen = sprintf_s(szFilenamePostfix, sizeof(szFilenamePostfix), CERT_FILENAME_POSTFIX_FORMAT, ++fileNo);
  212. if (AE_SUCCESS != upsePersistentStorage::Write(fileid++, *it))
  213. {
  214. break;
  215. }
  216. if (PSE_PR_CERTIFICATE_FID_MAX == fileid)
  217. {
  218. break;
  219. }
  220. strcpy_s(szNextCertNamePostfix, nBytes-pos, szFilenamePostfix);
  221. pos += nLen;
  222. szNextCertNamePostfix += nLen;
  223. ++it;
  224. if (it != certChain.end())
  225. {
  226. if (pos >= nBytes)
  227. {
  228. break;
  229. }
  230. *szNextCertNamePostfix = ';';
  231. pos++;
  232. ++szNextCertNamePostfix;
  233. }
  234. }
  235. if ((szNextCertNamePostfix - szParseString) > nBytes)
  236. {
  237. break;
  238. }
  239. nBytes = static_cast<int>(szNextCertNamePostfix - szParseString);
  240. upse::Buffer nameListBuffer;
  241. if (AE_FAILED(nameListBuffer.Alloc(nBytes)))
  242. break;
  243. upse::BufferWriter bw(nameListBuffer);
  244. bw.writeRaw((uint8_t*)szParseString, nBytes);
  245. if (AE_SUCCESS != upsePersistentStorage::Write(PSE_PR_CERTIFICATE_CHAIN_FID, nameListBuffer))
  246. {
  247. break;
  248. }
  249. status = AE_SUCCESS;
  250. } while (0);
  251. if (NULL != szParseString)
  252. {
  253. free(szParseString);
  254. }
  255. SGX_DBGPRINT_PRINT_FUNCTION_AND_RETURNVAL(__FUNCTION__, status);
  256. return status;
  257. }
  258. ae_error_t Helper::LoadCertificateChain( /*out*/ std::list<upse::Buffer>& certChain)
  259. {
  260. ae_error_t status = AESM_PSE_PR_CERT_LOAD_ERROR;
  261. char* szParseString = NULL;
  262. do
  263. {
  264. // Read the delimited file of certificate names
  265. upse::Buffer certChainListBuffer;
  266. if (AE_SUCCESS != upsePersistentStorage::Read(PSE_PR_CERTIFICATE_CHAIN_FID, certChainListBuffer))
  267. break;
  268. szParseString = (char*)calloc(1, certChainListBuffer.getSize() + 1);
  269. if (NULL == szParseString)
  270. break;
  271. memcpy_s(szParseString, certChainListBuffer.getSize(), certChainListBuffer.getData(), certChainListBuffer.getSize());
  272. szParseString[certChainListBuffer.getSize()] = '\0';
  273. char* nextToken = NULL;
  274. char* szCertificateNamePostfix = get_next_token(szParseString, TOKEN_SEPARATOR, &nextToken);
  275. // For each certificate name, read the certificate and save in list
  276. aesm_data_id_t fileid = PSE_PR_CERTIFICATE_FID;
  277. while (NULL != szCertificateNamePostfix)
  278. {
  279. upse::Buffer cert;
  280. if (AE_SUCCESS != upsePersistentStorage::Read(fileid++, cert))
  281. {
  282. break;
  283. }
  284. if (PSE_PR_CERTIFICATE_FID_MAX == fileid)
  285. {
  286. break;
  287. }
  288. certChain.push_back(cert);
  289. szCertificateNamePostfix = get_next_token(NULL, TOKEN_SEPARATOR, &nextToken);
  290. }
  291. if (NULL != szCertificateNamePostfix)
  292. {
  293. break;
  294. }
  295. status = AE_SUCCESS;
  296. } while (0);
  297. if (NULL != szParseString)
  298. {
  299. free(szParseString);
  300. }
  301. SGX_DBGPRINT_PRINT_FUNCTION_AND_RETURNVAL(__FUNCTION__, status);
  302. return status;
  303. }
  304. ae_error_t Helper::PrepareCertificateChainVLR( /*in*/ std::list<upse::Buffer>& certChain, /*out*/ upse::Buffer& certChainVLR)
  305. {
  306. ae_error_t status = AESM_PSE_PR_LOAD_VERIFIER_CERT_ERROR;
  307. try
  308. {
  309. do
  310. {
  311. int nPaddedBytes = 0;
  312. int nCertChain = 0;
  313. #if !defined(LEAFTOROOT)
  314. #error LEAFTOROOT not #defined
  315. #endif
  316. //
  317. // spec'd behavior is to receive certs in leaft to root order
  318. // then, it only makes sense to store them leaf to root
  319. // but sigma wants them root to leaf
  320. // we'll leave the #if here since, cumulatively, it shows how to traverse
  321. // in both directions
  322. //
  323. #if !LEAFTOROOT
  324. SGX_DBGPRINT_PRINT_STRING_LTP("leaf cert to root cert direction, padding");
  325. std::list<upse::Buffer>::reverse_iterator it;
  326. for (it = certChain.rbegin(); it != certChain.rend(); ++it)
  327. {
  328. int nSize = (*it).getSize();
  329. nPaddedBytes += REQUIRED_PADDING_DWORD_ALIGNMENT(nSize);
  330. nCertChain += nSize;
  331. }
  332. #else
  333. SGX_DBGPRINT_PRINT_STRING_LTP("root cert to leaf cert direction, padding");
  334. std::list<upse::Buffer>::iterator it;
  335. for (it = certChain.begin(); it != certChain.end(); ++it)
  336. {
  337. int nSize = (*it).getSize();
  338. nPaddedBytes += REQUIRED_PADDING_DWORD_ALIGNMENT(nSize);
  339. nCertChain += nSize;
  340. }
  341. #endif
  342. SGX_DBGPRINT_PRINT_STRING_LTP("less cert padding");
  343. //NRG: This doesn't work, but should. It should replace the previous
  344. nPaddedBytes = REQUIRED_PADDING_DWORD_ALIGNMENT(nCertChain);
  345. if(UINT16_MAX - ((int)sizeof(SIGMA_VLR_HEADER) + nPaddedBytes) < nCertChain){
  346. break;
  347. }
  348. int nLength = static_cast<int>(sizeof(SIGMA_VLR_HEADER)) + nPaddedBytes + nCertChain;
  349. certChainVLR.Alloc(nLength);
  350. upse::BufferWriter bw(certChainVLR);
  351. VERIFIER_CERT_CHAIN_VLR* pVLR;
  352. uint8_t* p;
  353. if (AE_FAILED(bw.reserve(nLength, &p)))
  354. break;
  355. pVLR = (VERIFIER_CERT_CHAIN_VLR*)p;
  356. pVLR->VlrHeader.ID = VERIFIER_CERTIFICATE_CHAIN_VLR_ID;
  357. pVLR->VlrHeader.PaddedBytes = (UINT8)nPaddedBytes;
  358. pVLR->VlrHeader.Length = (UINT16)nLength;
  359. memset(pVLR->VerifierCertificateChain, 0, nPaddedBytes + nCertChain);
  360. int ndx = 0;
  361. //
  362. // see above
  363. //
  364. #if (!LEAFTOROOT)
  365. SGX_DBGPRINT_PRINT_STRING_LTP("leaf cert to root cert direction");
  366. for (it = certChain.rbegin(); it != certChain.rend(); ++it)
  367. {
  368. memcpy_s(pVLR->VerifierCertificateChain + ndx, (*it).getSize(), (*it).getData(), (*it).getSize());
  369. ndx += (*it).getSize();
  370. }
  371. #else
  372. SGX_DBGPRINT_PRINT_STRING_LTP("root cert to leaf cert direction");
  373. for (it = certChain.begin(); it != certChain.end(); ++it)
  374. {
  375. memcpy_s(pVLR->VerifierCertificateChain + ndx, (*it).getSize(), (*it).getData(), (*it).getSize());
  376. ndx += (*it).getSize();
  377. }
  378. #endif
  379. status = AE_SUCCESS;
  380. } while (0);
  381. } catch(...)
  382. {
  383. }
  384. SGX_DBGPRINT_PRINT_FUNCTION_AND_RETURNVAL(__FUNCTION__, status);
  385. return status;
  386. }
  387. ae_error_t upsePersistentStorage::Delete(aesm_data_id_t data_id)
  388. {
  389. ae_error_t status = AESM_PSE_PR_PERSISTENT_STORAGE_DELETE_ERROR;
  390. do {
  391. char filepath[MAX_PATH];
  392. ae_error_t delerror = aesm_get_pathname(FT_PERSISTENT_STORAGE, data_id, filepath, MAX_PATH);
  393. if (AE_SUCCESS != delerror)
  394. {
  395. break;
  396. }
  397. if (0 != se_delete_tfile(filepath))
  398. {
  399. break;
  400. }
  401. status = AE_SUCCESS;
  402. } while (0);
  403. return status;
  404. }
  405. ae_error_t upsePersistentStorage::Read(aesm_data_id_t data_id, upse::Buffer& data)
  406. {
  407. ae_error_t status = AESM_PSE_PR_PERSISTENT_STORAGE_READ_ERROR;
  408. uint8_t* tempData = NULL;
  409. do {
  410. ae_error_t readError;
  411. uint32_t sizeInout;
  412. readError = aesm_query_data_size(FT_PERSISTENT_STORAGE, data_id, &sizeInout);
  413. if ((readError != AE_SUCCESS) || (sizeInout == 0))
  414. {
  415. break;
  416. }
  417. tempData = (uint8_t*) malloc(sizeInout);
  418. if (tempData == NULL)
  419. {
  420. break;
  421. }
  422. readError = aesm_read_data(FT_PERSISTENT_STORAGE, data_id, tempData, &sizeInout);
  423. if (AE_SUCCESS != readError)
  424. {
  425. break;
  426. }
  427. data.Alloc(sizeInout);
  428. upse::BufferWriter bw(data);
  429. bw.writeRaw(tempData, sizeInout);
  430. status = AE_SUCCESS;
  431. } while (0);
  432. if (NULL != tempData)
  433. free(tempData);
  434. return status;
  435. }
  436. ae_error_t upsePersistentStorage::Write(aesm_data_id_t data_id, upse::Buffer& data)
  437. {
  438. ae_error_t status = AESM_PSE_PR_PERSISTENT_STORAGE_WRITE_ERROR;
  439. do
  440. {
  441. if (AE_FAILED(aesm_write_data(FT_PERSISTENT_STORAGE, data_id, data.getData(), data.getSize())))
  442. break;
  443. status = AE_SUCCESS;
  444. } while (0);
  445. return status;
  446. }