pse_op_vmc_sqlite_ocall.cpp 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971
  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 <stdio.h>
  32. #include <stdlib.h>
  33. #include <string.h>
  34. #include <malloc.h>
  35. #include <stdint.h>
  36. #include <math.h>
  37. #include <assert.h>
  38. #include "se_wrapper.h"
  39. #include "se_memcpy.h"
  40. #include "sqlite3.h"
  41. #include "monotonic_counter_database_types.h"
  42. #include "oal/oal.h"
  43. #include "sgx_profile.h"
  44. #define ANCESTOR_ID(x) ((x)>>1)
  45. #define BROTHER_ID(x) (((x)%2 == 0)?((x)+1):((x)-1))
  46. #define HASH_TREE_NODE_TYPE_UNKNOWN 0
  47. #define HASH_TREE_NODE_TYPE_ROOT 1
  48. #define HASH_TREE_NODE_TYPE_INTERNAL 2
  49. #define HASH_TREE_NODE_TYPE_LEAF 3
  50. #define HASH_TREE_LEAF_NODE_GET 0
  51. #define HASH_TREE_LEAF_NODE_PUT 1
  52. #define HASH_TREE_LEAF_NODE_UPDATE 2
  53. #define EXIT_IFNOT_SQLITE_OK(rc, lable) if(SQLITE_OK!=(rc)){ret=OP_ERROR_SQLITE_INTERNAL;goto lable;}
  54. static pse_vmc_db_state_t pse_vmc_db_state = PSE_VMC_DB_STATE_DOWN;
  55. // layout of node_index_array is : Leaf Node ID + ancestor Node IDs(12 nodes) + Brother Node IDs(13 nodes).
  56. static void find_all_related_node_index(uint32_t leaf_node_index, uint32_t node_index_array[])
  57. {
  58. uint32_t leaf_index = leaf_node_index;
  59. uint32_t ancestor_index, brother_index;
  60. uint32_t i = 0;
  61. uint32_t* ancestor_node_index_array = node_index_array + 1; // node_index_array[0] is for the leaf node
  62. uint32_t* brother_node_index_array = ancestor_node_index_array + INIT_TOTAL_ANCESTORS_NODE_NUMBER;
  63. assert(leaf_node_index >= INIT_MIN_LEAF_NODE_ID && leaf_node_index <= INIT_MAX_LEAF_NODE_ID);
  64. node_index_array[0] = leaf_node_index;
  65. do{
  66. assert(i <= INIT_TOTAL_ANCESTORS_NODE_NUMBER);
  67. brother_index = BROTHER_ID(leaf_index);
  68. brother_node_index_array[i] = brother_index;
  69. ancestor_index = ANCESTOR_ID(leaf_index);
  70. if(1 != ancestor_index)
  71. {
  72. ancestor_node_index_array[i] = ancestor_index ;
  73. leaf_index = ancestor_index ;
  74. i++;
  75. }
  76. }while(1 != ancestor_index); // 1 is root node index
  77. }
  78. static pse_op_error_t sqlite_open_db(sqlite3 **db)
  79. {
  80. int rc = 0;
  81. pse_op_error_t ret = OP_SUCCESS;
  82. char vmc_db_path[MAX_PATH] = {0};
  83. pse_vmc_db_state = PSE_VMC_DB_STATE_WORKABLE;
  84. if(aesm_get_cpathname(FT_PERSISTENT_STORAGE, VMC_DATABASE_FID, vmc_db_path, MAX_PATH) != AE_SUCCESS)
  85. {
  86. *db = NULL;
  87. return OP_ERROR_INTERNAL;
  88. }
  89. rc = sqlite3_open_v2(vmc_db_path, db, SQLITE_OPEN_READWRITE, NULL);
  90. if( SQLITE_OK != rc )
  91. {
  92. // Can't open database
  93. sqlite3_close_v2(*db);
  94. *db = NULL;
  95. if(SQLITE_ERROR == rc || SQLITE_CORRUPT == rc || SQLITE_CANTOPEN == rc)
  96. {
  97. ret = OP_ERROR_DATABASE_FATAL;
  98. }
  99. else
  100. {
  101. ret = OP_ERROR_SQLITE_INTERNAL;
  102. }
  103. pse_vmc_db_state = PSE_VMC_DB_STATE_DOWN;
  104. }
  105. return ret;
  106. }
  107. static pse_op_error_t sqlite_query_int_value(sqlite3 *db, const char* sql_sentence, int* value)
  108. {
  109. pse_op_error_t ret = OP_SUCCESS;
  110. int rc;
  111. sqlite3_stmt *stat = NULL;
  112. rc = sqlite3_prepare_v2(db, sql_sentence, -1, &stat, 0);
  113. if(SQLITE_OK != rc)
  114. {
  115. ret = OP_ERROR_SQLITE_INTERNAL;
  116. goto exit;
  117. }
  118. rc = sqlite3_step(stat);
  119. if (SQLITE_DONE == rc)
  120. {
  121. *value = 0;
  122. ret = OP_ERROR_SQLITE_NOT_FOUND;
  123. goto exit;
  124. }
  125. if(SQLITE_ROW != rc)
  126. {
  127. ret = OP_ERROR_SQLITE_INTERNAL;
  128. goto exit;
  129. }
  130. rc = sqlite3_column_type(stat, 0);
  131. if(SQLITE_INTEGER != rc)
  132. {
  133. if(SQLITE_NULL == rc)
  134. {
  135. ret = OP_ERROR_SQLITE_NOT_FOUND; // SQlite returned a NULL record that indicates "NOT FOUND"
  136. }
  137. else
  138. {
  139. ret = OP_ERROR_INVALID_VMC_DB; // this will trigger DB re-initialization
  140. }
  141. goto exit;
  142. }
  143. *value = sqlite3_column_int(stat, 0);
  144. exit:
  145. sqlite3_finalize(stat);
  146. return ret;
  147. }
  148. #if 0
  149. inline ULARGE_INTEGER get_system_time()
  150. {
  151. SYSTEMTIME systemtime;
  152. FILETIME filetime;
  153. ULARGE_INTEGER current_time = {0};
  154. // record time for performance measurement
  155. GetSystemTime(&systemtime);
  156. if (FALSE == SystemTimeToFileTime(&systemtime, &filetime))
  157. {
  158. return current_time;
  159. }
  160. memcpy(&current_time, &filetime, sizeof(ULARGE_INTEGER));
  161. return current_time;
  162. }
  163. #endif
  164. static pse_op_error_t sqlite_update_node(sqlite3_stmt* stat, const uint8_t* blob, uint32_t blob_size, uint32_t id)
  165. {
  166. pse_op_error_t ret = OP_SUCCESS;
  167. int rc;
  168. rc = sqlite3_bind_blob(stat, 1, blob, blob_size, NULL);
  169. EXIT_IFNOT_SQLITE_OK(rc, error)
  170. rc = sqlite3_bind_int(stat, 2, id);
  171. EXIT_IFNOT_SQLITE_OK(rc, error)
  172. rc = sqlite3_step(stat);
  173. if(rc != SQLITE_DONE)
  174. {
  175. ret = OP_ERROR_SQLITE_INTERNAL;
  176. goto error;
  177. }
  178. rc = sqlite3_clear_bindings(stat);
  179. EXIT_IFNOT_SQLITE_OK(rc, error)
  180. rc = sqlite3_reset(stat);
  181. EXIT_IFNOT_SQLITE_OK(rc, error)
  182. return OP_SUCCESS;
  183. error:
  184. return ret;
  185. }
  186. static pse_op_error_t backup_vmc_db_file()
  187. {
  188. char backup_vmc_db_path[MAX_PATH] = {0};
  189. char vmc_db_path[MAX_PATH] = {0};
  190. if(aesm_get_pathname(FT_PERSISTENT_STORAGE, VMC_DATABASE_BK_FID, backup_vmc_db_path, MAX_PATH) != AE_SUCCESS)
  191. {
  192. return OP_ERROR_BACKUP_CURRENT_DB;
  193. }
  194. if(aesm_get_pathname(FT_PERSISTENT_STORAGE, VMC_DATABASE_FID, vmc_db_path, MAX_PATH) != AE_SUCCESS)
  195. {
  196. return OP_ERROR_BACKUP_CURRENT_DB;
  197. }
  198. if(se_copy_file(backup_vmc_db_path, vmc_db_path)!=0){
  199. return OP_ERROR_BACKUP_CURRENT_DB;
  200. }
  201. return OP_SUCCESS;
  202. }
  203. pse_op_error_t sqlite_rollback_db_file()
  204. {
  205. char backup_vmc_db_path[MAX_PATH] = {0};
  206. char vmc_db_path[MAX_PATH] = {0};
  207. if(aesm_get_pathname(FT_PERSISTENT_STORAGE, VMC_DATABASE_BK_FID, backup_vmc_db_path, MAX_PATH) != AE_SUCCESS)
  208. {
  209. return OP_ERROR_BACKUP_CURRENT_DB;
  210. }
  211. if(aesm_get_pathname(FT_PERSISTENT_STORAGE, VMC_DATABASE_FID, vmc_db_path, MAX_PATH) != AE_SUCCESS)
  212. {
  213. return OP_ERROR_BACKUP_CURRENT_DB;
  214. }
  215. if(se_copy_file(vmc_db_path, backup_vmc_db_path)!=0){
  216. return OP_ERROR_BACKUP_CURRENT_DB;
  217. }
  218. return OP_SUCCESS;
  219. }
  220. static pse_op_error_t copy_prebuild_vmc_db()
  221. {
  222. char prebuild_vmc_db_path[MAX_PATH] = {0};
  223. char vmc_db_path[MAX_PATH] = {0};
  224. if(aesm_get_pathname(FT_PERSISTENT_STORAGE, VMC_DATABASE_PREBUILD_FID, prebuild_vmc_db_path, MAX_PATH) != AE_SUCCESS)
  225. {
  226. return OP_ERROR_COPY_PREBUILD_DB;
  227. }
  228. if(aesm_get_pathname(FT_PERSISTENT_STORAGE, VMC_DATABASE_FID, vmc_db_path, MAX_PATH) != AE_SUCCESS)
  229. {
  230. return OP_ERROR_COPY_PREBUILD_DB;
  231. }
  232. if(se_copy_file(vmc_db_path, prebuild_vmc_db_path)!=0){
  233. return OP_ERROR_COPY_PREBUILD_DB;
  234. }
  235. return OP_SUCCESS;
  236. }
  237. pse_op_error_t sqlite_read_children_of_root(pse_vmc_children_of_root_t* children)
  238. {
  239. PROFILE_START("sqlite_read_children_of_root");
  240. int rc;
  241. pse_op_error_t ret = OP_SUCCESS;
  242. sqlite3_stmt* stat = NULL;
  243. char sql_sentence[1024] = {0};
  244. int result;
  245. uint32_t node_id;
  246. const void* ptr_blob_content;
  247. uint32_t blob_len;
  248. uint32_t record_count = 0;
  249. uint32_t read_list_array[2] = {2,3};
  250. sqlite3 *db = NULL;
  251. assert(children != NULL);
  252. ret = sqlite_open_db(&db);
  253. if(OP_SUCCESS != ret)
  254. {
  255. pse_vmc_db_state = PSE_VMC_DB_STATE_DOWN;
  256. PROFILE_END("sqlite_read_children_of_root");
  257. return ret;
  258. }
  259. // prepare sql statement
  260. if (_snprintf_s(sql_sentence, sizeof(sql_sentence), "select * from HASH_TREE_NODE_TABLE where ID IN (2,3) order by ID asc") < 0)
  261. {
  262. ret = OP_ERROR_INTERNAL;
  263. goto clean_up;
  264. }
  265. // prepare sql statement
  266. rc = sqlite3_prepare_v2(db, sql_sentence, -1, &stat, 0);
  267. EXIT_IFNOT_SQLITE_OK(rc, clean_up)
  268. // query
  269. while ((result = sqlite3_step(stat)) == SQLITE_ROW)
  270. {
  271. // to calculate number of records returned
  272. record_count++;
  273. if (record_count > 2)
  274. {
  275. ret = OP_ERROR_INVALID_VMC_DB;
  276. goto clean_up;
  277. }
  278. node_id = sqlite3_column_int(stat, 0);
  279. // The array read_list_array[] contains {2,3}, and the node id read from DB must be 2 or 3.
  280. if (node_id != read_list_array[record_count-1])
  281. {
  282. ret = OP_ERROR_INVALID_VMC_DB;
  283. goto clean_up;
  284. }
  285. ptr_blob_content = sqlite3_column_blob(stat, 1);
  286. if(!ptr_blob_content)
  287. {
  288. ret = OP_ERROR_INVALID_VMC_DB;
  289. goto clean_up;
  290. }
  291. blob_len = sqlite3_column_bytes(stat, 1);
  292. // Child Node
  293. if(blob_len != INTERNAL_NODE_SIZE)
  294. {
  295. ret = OP_ERROR_INVALID_VMC_DB;
  296. goto clean_up;
  297. }
  298. // Copy children
  299. hash_tree_internal_node_t* internal_node_ptr = NULL;
  300. if (node_id == 2)
  301. {
  302. internal_node_ptr = &children->left_child.internal;
  303. }
  304. else
  305. {
  306. internal_node_ptr = &children->rigth_child.internal;
  307. }
  308. if(0 != memcpy_s(internal_node_ptr,
  309. blob_len,
  310. ptr_blob_content,
  311. blob_len))
  312. {
  313. ret = OP_ERROR_INTERNAL;
  314. goto clean_up;
  315. }
  316. }
  317. if (record_count != 2)
  318. {
  319. ret = OP_ERROR_INVALID_VMC_DB;
  320. goto clean_up;
  321. }
  322. if (result != SQLITE_DONE)
  323. {
  324. ret = OP_ERROR_SQLITE_INTERNAL;
  325. }
  326. clean_up:
  327. sqlite3_finalize(stat);
  328. assert(db != NULL);
  329. sqlite3_close_v2(db);
  330. PROFILE_END("sqlite_read_children_of_root");
  331. return ret;
  332. }
  333. pse_op_error_t sqlite_read_db(uint32_t leaf_id, pse_vmc_hash_tree_cache_t* cache)
  334. {
  335. PROFILE_START("sqlite_read_db");
  336. int rc;
  337. pse_op_error_t ret = OP_SUCCESS;
  338. sqlite3_stmt* stat = NULL;
  339. char sql_sentence[1024] = {0};
  340. int result;
  341. uint32_t node_id;
  342. const void* ptr_blob_content;
  343. uint32_t blob_len;
  344. uint32_t record_count = 0;
  345. uint32_t read_list_array[INIT_TOTAL_NODE_NUMBER_FOR_READING] = {0};
  346. sqlite3 *db = NULL;
  347. uint8_t* node_ptr = NULL;
  348. uint32_t child_node_id = leaf_id;
  349. uint32_t internal_index = 0;
  350. uint32_t count = 0;
  351. if( !cache)
  352. {
  353. PROFILE_END("sqlite_read_db");
  354. return OP_ERROR_INVALID_PARAMETER;
  355. }
  356. if(leaf_id < INIT_MIN_LEAF_NODE_ID || leaf_id > INIT_MAX_LEAF_NODE_ID)
  357. {
  358. PROFILE_END("sqlite_read_db");
  359. return OP_ERROR_INVALID_PARAMETER;
  360. }
  361. ret = sqlite_open_db(&db);
  362. if(OP_SUCCESS != ret)
  363. {
  364. pse_vmc_db_state = PSE_VMC_DB_STATE_DOWN;
  365. PROFILE_END("sqlite_read_db");
  366. return ret;
  367. }
  368. // layout of read_list_array is : Leaf Node ID + ancestor Node IDs + Brother Node IDs.
  369. find_all_related_node_index(leaf_id, read_list_array);
  370. // prepare sql statement
  371. if (_snprintf_s(sql_sentence, sizeof(sql_sentence), "select * from HASH_TREE_NODE_TABLE where ID IN (") < 0)
  372. {
  373. ret = OP_ERROR_INTERNAL;
  374. goto clean_up;
  375. }
  376. for(uint32_t index=0; index < INIT_TOTAL_NODE_NUMBER_FOR_READING; index++)
  377. {
  378. char id[10];
  379. if (_snprintf_s(id, sizeof(id), "%u", read_list_array[index]) < 0)
  380. {
  381. ret = OP_ERROR_INTERNAL;
  382. goto clean_up;
  383. }
  384. if (strncat_s(sql_sentence, sizeof(sql_sentence), id, strnlen_s(id, 10)) != 0)
  385. {
  386. ret = OP_ERROR_INTERNAL;
  387. goto clean_up;
  388. }
  389. if (index != INIT_TOTAL_NODE_NUMBER_FOR_READING - 1)
  390. {
  391. if (strncat_s(sql_sentence, sizeof(sql_sentence), ",", 1) != 0)
  392. {
  393. ret = OP_ERROR_INTERNAL;
  394. goto clean_up;
  395. }
  396. }
  397. }
  398. if (strcat_s(sql_sentence, sizeof(sql_sentence), ") order by ID desc") != 0)
  399. {
  400. ret = OP_ERROR_INTERNAL;
  401. goto clean_up;
  402. }
  403. // prepare sql statement
  404. rc = sqlite3_prepare_v2(db, sql_sentence, -1, &stat, 0);
  405. EXIT_IFNOT_SQLITE_OK(rc, clean_up)
  406. // query
  407. // the result set are sorted, from leaf to up layers
  408. while ((result = sqlite3_step(stat)) == SQLITE_ROW)
  409. {
  410. // to calculate number of records returned
  411. record_count++;
  412. node_id = sqlite3_column_int(stat, 0);
  413. ptr_blob_content = sqlite3_column_blob(stat, 1);
  414. if(!ptr_blob_content)
  415. {
  416. ret = OP_ERROR_INVALID_VMC_DB;
  417. goto clean_up;
  418. }
  419. blob_len = sqlite3_column_bytes(stat, 1);
  420. if(1 == node_id)
  421. {
  422. assert(0);
  423. ret = OP_ERROR_INVALID_VMC_DB;
  424. goto clean_up;
  425. }
  426. else if(INIT_MIN_LEAF_NODE_ID <= node_id)
  427. {
  428. // node_id has already been checked and
  429. // it will not exceed INIT_MAX_LEAF_NODE_ID
  430. assert(node_id <= INIT_MAX_LEAF_NODE_ID);
  431. // leaf node
  432. if(blob_len != LEAF_NODE_SIZE)
  433. {
  434. ret = OP_ERROR_INVALID_VMC_DB;
  435. goto clean_up;
  436. }
  437. if (node_id == leaf_id)
  438. {
  439. cache->self.node_id = node_id;
  440. node_ptr = (uint8_t*)&cache->self.leaf;
  441. }
  442. else
  443. {
  444. cache->brother.node_id = node_id;
  445. node_ptr = (uint8_t*)&cache->brother.leaf;
  446. }
  447. }
  448. else
  449. {
  450. assert(node_id <= INIT_MAX_LEAF_NODE_ID);
  451. // internal nodes
  452. if(blob_len != INTERNAL_NODE_SIZE)
  453. {
  454. ret = OP_ERROR_INVALID_VMC_DB;
  455. goto clean_up;
  456. }
  457. if (node_id == child_node_id / 2)
  458. {
  459. // this is ancestor node
  460. node_ptr = (uint8_t*)&cache->ancestors[internal_index].internal;
  461. cache->ancestors[internal_index].node_id = node_id;
  462. }
  463. else
  464. {
  465. // this is brother of ancestors
  466. node_ptr = (uint8_t*)&cache->brother_of_ancestors[internal_index].internal;
  467. cache->brother_of_ancestors[internal_index].node_id = node_id;
  468. }
  469. count++;
  470. if (count == 2)
  471. {
  472. // internal nodes are arranged as pairs, one is ancestor, another is the brother of the ancestor
  473. count = 0;
  474. internal_index++; // go up a level
  475. child_node_id = node_id; // now current node becomes child
  476. }
  477. }
  478. // copy blob data to output buffer
  479. if(0 != memcpy_s(node_ptr,
  480. blob_len,
  481. ptr_blob_content,
  482. blob_len))
  483. {
  484. ret = OP_ERROR_INTERNAL;
  485. goto clean_up;
  486. }
  487. }
  488. if (record_count != INIT_TOTAL_NODE_NUMBER_FOR_READING)
  489. {
  490. ret = OP_ERROR_INVALID_VMC_DB;
  491. goto clean_up;
  492. }
  493. if (result != SQLITE_DONE)
  494. {
  495. ret = OP_ERROR_SQLITE_INTERNAL;
  496. }
  497. clean_up:
  498. assert(db != NULL);
  499. sqlite3_finalize(stat);
  500. sqlite3_close_v2(db);
  501. PROFILE_END("sqlite_read_db");
  502. return ret;
  503. }
  504. pse_op_error_t sqlite_write_db(pse_vmc_hash_tree_cache_t* cache,
  505. uint8_t is_for_update_flag,
  506. op_leafnode_flag_t* op_flag_info)
  507. {
  508. PROFILE_START("sqlite_write_db");
  509. int rc;
  510. pse_op_error_t ret = OP_SUCCESS;
  511. sqlite3_stmt* stat = NULL;
  512. sqlite3 *db = NULL;
  513. char sql_sentence[512] = {0};
  514. int refid = 0;
  515. if( !cache)
  516. {
  517. PROFILE_END("sqlite_write_db");
  518. return OP_ERROR_INVALID_PARAMETER;
  519. }
  520. if(is_for_update_flag && !op_flag_info)
  521. {
  522. PROFILE_END("sqlite_write_db");
  523. return OP_ERROR_INVALID_PARAMETER;
  524. }
  525. // Backup DB first
  526. ret = backup_vmc_db_file();
  527. if(OP_SUCCESS != ret)
  528. {
  529. pse_vmc_db_state = PSE_VMC_DB_STATE_DOWN;
  530. PROFILE_END("sqlite_write_db");
  531. return ret;
  532. }
  533. ret = sqlite_open_db(&db);
  534. if(OP_SUCCESS != ret)
  535. {
  536. pse_vmc_db_state = PSE_VMC_DB_STATE_DOWN;
  537. PROFILE_END("sqlite_write_db");
  538. return ret;
  539. }
  540. rc = sqlite3_exec(db, "BEGIN TRANSACTION;", NULL, NULL, NULL);
  541. EXIT_IFNOT_SQLITE_OK(rc, error)
  542. if (_snprintf_s(sql_sentence, sizeof(sql_sentence), "update HASH_TREE_NODE_TABLE set node_content=? where ID=?") < 0)
  543. {
  544. ret = OP_ERROR_INTERNAL;
  545. goto error;
  546. }
  547. rc = sqlite3_prepare_v2(db, sql_sentence, -1, &stat, 0);
  548. EXIT_IFNOT_SQLITE_OK(rc, error)
  549. // update internal nodes
  550. for (uint32_t index = 0; index < INIT_INTERNAL_NODE_NR; index++)
  551. {
  552. // update ancestors
  553. if ((ret = sqlite_update_node(stat,
  554. (uint8_t*)&cache->ancestors[index].internal,
  555. INTERNAL_NODE_SIZE,
  556. cache->ancestors[index].node_id)) != OP_SUCCESS)
  557. goto error;
  558. // update brothers of ancestors
  559. if ((ret = sqlite_update_node(stat,
  560. (uint8_t*)&cache->brother_of_ancestors[index].internal,
  561. INTERNAL_NODE_SIZE,
  562. cache->brother_of_ancestors[index].node_id)) != OP_SUCCESS)
  563. goto error;
  564. }
  565. // update leaf and its brother
  566. if ((ret = sqlite_update_node(stat,
  567. (uint8_t*)&cache->self.leaf,
  568. LEAF_NODE_SIZE,
  569. cache->self.node_id)) != OP_SUCCESS)
  570. goto error;
  571. if ((ret = sqlite_update_node(stat,
  572. (uint8_t*)&cache->brother.leaf,
  573. LEAF_NODE_SIZE,
  574. cache->brother.node_id)) != OP_SUCCESS)
  575. goto error;
  576. if(is_for_update_flag)
  577. {
  578. // update USED flag and QUOTA record
  579. char mrsigner[65] = {0};
  580. // convert mr_signer to hex string
  581. for(uint32_t i=0; i < sizeof(sgx_measurement_t); i++)
  582. {
  583. char tmp[3];
  584. if(_snprintf_s(tmp, sizeof(tmp), "%02x", ((uint8_t*)(&op_flag_info->mr_signer))[i]) < 0)
  585. {
  586. ret = OP_ERROR_INTERNAL;
  587. goto error;
  588. }
  589. if(0 != strncat_s(mrsigner, sizeof(mrsigner), tmp, sizeof(tmp)))
  590. {
  591. ret = OP_ERROR_INTERNAL;
  592. goto error;
  593. }
  594. }
  595. switch(op_flag_info->op_type)
  596. {
  597. case CLR_LEAFNODE_FLAG:
  598. // read REFID saved in HASH_TREE_NODE_TABLE before deleting the record.
  599. if (_snprintf_s(sql_sentence, sizeof(sql_sentence), "select REFID from HASH_TREE_NODE_TABLE where ID=%d;", cache->self.node_id) < 0)
  600. {
  601. ret = OP_ERROR_INTERNAL;
  602. goto error;
  603. }
  604. ret = sqlite_query_int_value(db, sql_sentence, &refid);
  605. if(OP_SUCCESS != ret && OP_ERROR_SQLITE_NOT_FOUND != ret)
  606. {
  607. goto error;
  608. }
  609. // clear REFID and USED flag
  610. if (_snprintf_s(sql_sentence, sizeof(sql_sentence), "update HASH_TREE_NODE_TABLE set USED=0 and REFID=0 where ID=%d;", cache->self.node_id) < 0)
  611. {
  612. ret = OP_ERROR_INTERNAL;
  613. goto error;
  614. }
  615. rc = sqlite3_exec(db, sql_sentence, NULL, NULL, NULL);
  616. if(SQLITE_OK != rc)
  617. {
  618. ret = OP_ERROR_SQLITE_INTERNAL;
  619. goto error;
  620. }
  621. if(1 != sqlite3_changes(db))
  622. {
  623. ret = OP_ERROR_SQLITE_INTERNAL;
  624. goto error;
  625. }
  626. // update QUOTA, counter--;
  627. if (_snprintf_s(sql_sentence, sizeof(sql_sentence), "update VMC_QUOTA_TABLE set COUNTER=COUNTER-1 where ID=%d and COUNTER>0;", refid) < 0)
  628. {
  629. ret = OP_ERROR_INTERNAL;
  630. goto error;
  631. }
  632. rc = sqlite3_exec(db, sql_sentence, NULL, NULL, NULL);
  633. if(SQLITE_OK != rc)
  634. {
  635. ret = OP_ERROR_SQLITE_INTERNAL;
  636. goto error;
  637. }
  638. if(1 != sqlite3_changes(db))
  639. {
  640. ret = OP_ERROR_SQLITE_INTERNAL;
  641. goto error;
  642. }
  643. break;
  644. case SET_LEAFNODE_FLAG:
  645. // update QUOTA, counter++;
  646. if (_snprintf_s(sql_sentence, sizeof(sql_sentence), "update VMC_QUOTA_TABLE set COUNTER=COUNTER+1 where MRSIGNER='%s';", mrsigner) < 0)
  647. {
  648. ret = OP_ERROR_INTERNAL;
  649. goto error;
  650. }
  651. rc = sqlite3_exec(db, sql_sentence, NULL, NULL, NULL);
  652. if(SQLITE_OK != rc)
  653. {
  654. ret = OP_ERROR_SQLITE_INTERNAL;
  655. goto error;
  656. }
  657. rc = sqlite3_changes(db);
  658. if(0 == rc)
  659. {
  660. // the mrsigner isn't in quota table yet, so insert it.
  661. if (_snprintf_s(sql_sentence, sizeof(sql_sentence), "insert into VMC_QUOTA_TABLE(MRSIGNER,COUNTER) values('%s', 1);", mrsigner) < 0)
  662. {
  663. ret = OP_ERROR_INTERNAL;
  664. goto error;
  665. }
  666. rc = sqlite3_exec(db, sql_sentence, NULL, NULL, NULL);
  667. if(SQLITE_OK != rc)
  668. {
  669. ret = OP_ERROR_SQLITE_INTERNAL;
  670. goto error;
  671. }
  672. if(1 != sqlite3_changes(db))
  673. {
  674. ret = OP_ERROR_SQLITE_INTERNAL;
  675. goto error;
  676. }
  677. }
  678. else if(1 == rc)
  679. {
  680. // the mrsigner has been in quota table
  681. }
  682. else
  683. {
  684. ret = OP_ERROR_SQLITE_INTERNAL;
  685. goto error;
  686. }
  687. // read refid
  688. if (_snprintf_s(sql_sentence, sizeof(sql_sentence), "select ID from VMC_QUOTA_TABLE where MRSIGNER='%s';", mrsigner) < 0)
  689. {
  690. ret = OP_ERROR_INTERNAL;
  691. goto error;
  692. }
  693. ret = sqlite_query_int_value(db, sql_sentence, &refid);
  694. if(OP_SUCCESS != ret && OP_ERROR_SQLITE_NOT_FOUND != ret)
  695. {
  696. goto error;
  697. }
  698. // Update HASH_TREE_NODE_TABLE
  699. if (_snprintf_s(sql_sentence, sizeof(sql_sentence), "update HASH_TREE_NODE_TABLE set USED=1 where ID=%d;", cache->self.node_id) < 0)
  700. {
  701. ret = OP_ERROR_INTERNAL;
  702. goto error;
  703. }
  704. rc = sqlite3_exec(db, sql_sentence, NULL, NULL, NULL);
  705. if(SQLITE_OK != rc)
  706. {
  707. ret = OP_ERROR_SQLITE_INTERNAL;
  708. goto error;
  709. }
  710. if(1 != sqlite3_changes(db))
  711. {
  712. ret = OP_ERROR_SQLITE_INTERNAL;
  713. goto error;
  714. }
  715. if (_snprintf_s(sql_sentence, sizeof(sql_sentence), "update HASH_TREE_NODE_TABLE set REFID=%d where ID=%d;", refid, cache->self.node_id) < 0)
  716. {
  717. ret = OP_ERROR_INTERNAL;
  718. goto error;
  719. }
  720. rc = sqlite3_exec(db, sql_sentence, NULL, NULL, NULL);
  721. if(SQLITE_OK != rc)
  722. {
  723. ret = OP_ERROR_SQLITE_INTERNAL;
  724. goto error;
  725. }
  726. if(1 != sqlite3_changes(db))
  727. {
  728. ret = OP_ERROR_SQLITE_INTERNAL;
  729. goto error;
  730. }
  731. break;
  732. default:
  733. ret = OP_ERROR_INVALID_PARAMETER;
  734. goto error;
  735. }
  736. }
  737. rc = sqlite3_exec(db, "END TRANSACTION;", NULL, NULL, NULL);
  738. EXIT_IFNOT_SQLITE_OK(rc, error)
  739. sqlite3_finalize(stat);
  740. sqlite3_close_v2(db);
  741. PROFILE_END("sqlite_write_db");
  742. return OP_SUCCESS;
  743. error:
  744. assert(db != NULL);
  745. sqlite3_finalize(stat);
  746. sqlite3_exec(db, "ROLLBACK TRANSACTION;", NULL, NULL, NULL);
  747. sqlite3_close_v2(db);
  748. PROFILE_END("sqlite_write_db");
  749. return ret;
  750. }
  751. pse_op_error_t sqlite_get_empty_leafnode(int* leaf_node_id, sgx_measurement_t* mr_signer)
  752. {
  753. PROFILE_START("sqlite_get_empty_leafnode");
  754. char mrsigner[65] = {0};
  755. char sql_sentence[512] = {0};
  756. int counter = 0;
  757. pse_op_error_t ret = OP_SUCCESS;
  758. sqlite3 *db = NULL;
  759. if(!leaf_node_id || !mr_signer)
  760. {
  761. PROFILE_END("sqlite_get_empty_leafnode");
  762. return OP_ERROR_INVALID_PARAMETER;
  763. }
  764. // convert mr_signer to hex string
  765. for(uint32_t i=0; i < sizeof(sgx_measurement_t); i++)
  766. {
  767. char tmp[3];
  768. if(_snprintf_s(tmp, sizeof(tmp), "%02x", ((uint8_t*)mr_signer)[i]) < 0)
  769. {
  770. ret = OP_ERROR_INTERNAL;
  771. goto clean_up;
  772. }
  773. if(0 != strncat_s(mrsigner, sizeof(mrsigner), tmp, sizeof(tmp)))
  774. {
  775. ret = OP_ERROR_INTERNAL;
  776. goto clean_up;
  777. }
  778. }
  779. ret = sqlite_open_db(&db);
  780. if(OP_SUCCESS != ret)
  781. {
  782. pse_vmc_db_state = PSE_VMC_DB_STATE_DOWN;
  783. PROFILE_END("sqlite_get_empty_leafnode");
  784. return ret;
  785. }
  786. // check QUOTA
  787. if (_snprintf_s(sql_sentence, sizeof(sql_sentence), "select COUNTER from VMC_QUOTA_TABLE where MRSIGNER='%s';", mrsigner) < 0)
  788. {
  789. ret = OP_ERROR_INTERNAL;
  790. goto clean_up;
  791. }
  792. ret = sqlite_query_int_value(db, sql_sentence, &counter);
  793. if(OP_SUCCESS != ret && OP_ERROR_SQLITE_NOT_FOUND != ret)
  794. {
  795. goto clean_up;
  796. }
  797. if(PSE_VMC_QUOTA_SIZE <= counter)
  798. {
  799. // exceeds quota, return error.
  800. ret = OP_ERROR_DATABASE_OVER_QUOTA;
  801. goto clean_up;
  802. }
  803. // the specified MR SIGNER doesn't exceed quota.
  804. *leaf_node_id = 0;
  805. if (_snprintf_s(sql_sentence, sizeof(sql_sentence), "select min(ID) from HASH_TREE_NODE_TABLE where USED=0 and ID>%d and ID<%d;",
  806. INIT_MIN_LEAF_NODE_ID-1,
  807. INIT_MAX_LEAF_NODE_ID+1) < 0)
  808. {
  809. ret = OP_ERROR_INTERNAL;
  810. goto clean_up;
  811. }
  812. ret = sqlite_query_int_value(db, sql_sentence, leaf_node_id);
  813. if(OP_ERROR_SQLITE_NOT_FOUND == ret)
  814. {
  815. ret = OP_ERROR_DATABASE_FULL;
  816. goto clean_up;
  817. }
  818. clean_up:
  819. if(db)
  820. {
  821. sqlite3_close_v2(db);
  822. }
  823. PROFILE_END("sqlite_get_empty_leafnode");
  824. return ret;
  825. }
  826. pse_op_error_t sqlite_db_init_hash_tree_table()
  827. {
  828. pse_op_error_t ret;
  829. ret = copy_prebuild_vmc_db();
  830. if(ret != OP_SUCCESS)
  831. {
  832. pse_vmc_db_state = PSE_VMC_DB_STATE_DOWN;
  833. return ret;
  834. }
  835. return OP_SUCCESS;
  836. }