appconfig.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. #include <iostream>
  2. #include "Untrusted.hpp"
  3. #include "appconfig.hpp"
  4. // The next line suppresses a deprecation warning within boost
  5. #define BOOST_BIND_GLOBAL_PLACEHOLDERS
  6. #include "boost/property_tree/ptree.hpp"
  7. #include "boost/property_tree/json_parser.hpp"
  8. // Split a hostport string like "127.0.0.1:12000" at the rightmost colon
  9. // into a host part "127.0.0.1" and a port part "12000".
  10. static bool split_host_port(std::string &host, std::string &port,
  11. const std::string &hostport)
  12. {
  13. size_t colon = hostport.find_last_of(':');
  14. if (colon == std::string::npos) {
  15. std::cerr << "Cannot parse \"" << hostport << "\" as host:port\n";
  16. return false;
  17. }
  18. host = hostport.substr(0, colon);
  19. port = hostport.substr(colon+1);
  20. return true;
  21. }
  22. // Convert a single hex character into its value from 0 to 15. Return
  23. // true on success, false if it wasn't a hex character.
  24. static inline bool hextoval(unsigned char &val, char hex)
  25. {
  26. if (hex >= '0' && hex <= '9') {
  27. val = ((unsigned char)hex)-'0';
  28. } else if (hex >= 'a' && hex <= 'f') {
  29. val = ((unsigned char)hex)-'a'+10;
  30. } else if (hex >= 'A' && hex <= 'F') {
  31. val = ((unsigned char)hex)-'A'+10;
  32. } else {
  33. return false;
  34. }
  35. return true;
  36. }
  37. // Convert a 2*len hex character string into a len-byte buffer. Return
  38. // true on success, false on failure.
  39. static bool hextobuf(unsigned char *buf, const char *str, size_t len)
  40. {
  41. if (strlen(str) != 2*len) {
  42. std::cerr << "Hex string was not the expected size\n";
  43. return false;
  44. }
  45. for (size_t i=0;i<len;++i) {
  46. unsigned char hi, lo;
  47. if (!hextoval(hi, str[2*i]) || !hextoval(lo, str[2*i+1])) {
  48. std::cerr << "Cannot parse string as hex\n";
  49. return false;
  50. }
  51. buf[i] = (unsigned char)((hi << 4) + lo);
  52. }
  53. return true;
  54. }
  55. bool config_parse(Config &config, const std::string configstr,
  56. const std::string &myname, threadid_t nthreads)
  57. {
  58. bool found_my_node = false;
  59. bool found_params = false;
  60. bool ret = true;
  61. std::istringstream configstream(configstr);
  62. boost::property_tree::ptree conftree;
  63. read_json(configstream, conftree);
  64. for (auto & entry : conftree) {
  65. if (!entry.first.compare("params")) {
  66. for (auto & pentry : entry.second) {
  67. if (!pentry.first.compare("msg_size")) {
  68. config.msg_size = pentry.second.get_value<uint16_t>();
  69. } else if (!pentry.first.compare("user_count")) {
  70. config.user_count = pentry.second.get_value<uint32_t>();
  71. } else if (!pentry.first.compare("token_out")) {
  72. config.m_token_out = pentry.second.get_value<uint8_t>();
  73. } else if (!pentry.first.compare("token_in")) {
  74. config.m_token_in = pentry.second.get_value<uint8_t>();
  75. } else if (!pentry.first.compare("id_out")) {
  76. config.m_id_out = pentry.second.get_value<uint8_t>();
  77. } else if (!pentry.first.compare("id_in")) {
  78. config.m_id_in = pentry.second.get_value<uint8_t>();
  79. } else if (!pentry.first.compare("master_secret")) {
  80. // Currently hardcoding an AES key for client <->
  81. // server communication, but in reality, a key
  82. // exchange would be done
  83. std::string hex_key = pentry.second.data();
  84. memcpy(config.master_secret, hex_key.c_str(), SGX_AESGCM_KEY_SIZE);
  85. } else if (!pentry.first.compare("token_channel")) {
  86. config.token_channel = pentry.second.get_value<bool>();
  87. } else {
  88. std::cerr << "Unknown field in params: " <<
  89. pentry.first << "\n";
  90. ret = false;
  91. }
  92. }
  93. found_params = true;
  94. } else if (!entry.first.compare("nodes")) {
  95. for (auto & node : entry.second) {
  96. NodeConfig nc;
  97. // defaults
  98. nc.weight = 1;
  99. nc.roles = ROLE_INGESTION | ROLE_ROUTING | ROLE_STORAGE;
  100. for (auto & nentry : node.second) {
  101. if (!nentry.first.compare("name")) {
  102. nc.name = nentry.second.get_value<std::string>();
  103. if (!myname.compare(nc.name)) {
  104. config.my_node_num =
  105. nodenum_t(config.nodes.size());
  106. found_my_node = true;
  107. }
  108. } else if (!nentry.first.compare("pubkey")) {
  109. ret &= hextobuf((unsigned char *)&nc.pubkey,
  110. nentry.second.get_value<std::string>().c_str(),
  111. sizeof(nc.pubkey));
  112. } else if (!nentry.first.compare("weight")) {
  113. nc.weight = nentry.second.get_value<std::uint8_t>();
  114. } else if (!nentry.first.compare("roles")) {
  115. nc.roles = nentry.second.get_value<std::uint8_t>();
  116. } else if (!nentry.first.compare("listen")) {
  117. ret &= split_host_port(nc.listenhost, nc.listenport,
  118. nentry.second.get_value<std::string>());
  119. } else if (!nentry.first.compare("clisten")) {
  120. ret &= split_host_port(nc.clistenhost, nc.clistenport,
  121. nentry.second.get_value<std::string>());
  122. } else if (!nentry.first.compare("slisten")) {
  123. ret &= split_host_port(nc.slistenhost, nc.slistenport,
  124. nentry.second.get_value<std::string>());
  125. } else {
  126. std::cerr << "Unknown field in host config: " <<
  127. nentry.first << "\n";
  128. ret = false;
  129. }
  130. }
  131. config.nodes.push_back(std::move(nc));
  132. }
  133. } else {
  134. std::cerr << "Unknown key in config: " <<
  135. entry.first << "\n";
  136. ret = false;
  137. }
  138. }
  139. if (!found_params) {
  140. std::cerr << "Could not find params in config\n";
  141. ret = false;
  142. }
  143. if (!found_my_node) {
  144. std::cerr << "Could not find my own node entry in config\n";
  145. ret = false;
  146. }
  147. config.nthreads = nthreads;
  148. if (!ret) return ret;
  149. // Now load the config into the enclave
  150. EnclaveAPIParams apiparams;
  151. apiparams.user_count = config.user_count;
  152. apiparams.msg_size = config.msg_size;
  153. apiparams.m_token_out = config.m_token_out;
  154. apiparams.m_token_in = config.m_token_in;
  155. apiparams.m_id_out = config.m_id_out;
  156. apiparams.m_id_in = config.m_id_in;
  157. memcpy(apiparams.master_secret, config.master_secret, SGX_AESGCM_KEY_SIZE);
  158. apiparams.token_channel = config.token_channel;
  159. nodenum_t num_nodes = (nodenum_t)(config.nodes.size());
  160. std::vector<EnclaveAPINodeConfig> apinodeconfigs;
  161. apinodeconfigs.resize(num_nodes);
  162. for (nodenum_t i=0; i<num_nodes; ++i) {
  163. memmove(&apinodeconfigs[i].pubkey,
  164. &config.nodes[i].pubkey, sizeof(apinodeconfigs[i].pubkey));
  165. apinodeconfigs[i].weight = config.nodes[i].weight;
  166. apinodeconfigs[i].roles = config.nodes[i].roles;
  167. }
  168. ret &= ecall_config_load(nthreads, &apiparams, apinodeconfigs.data(),
  169. num_nodes, config.my_node_num);
  170. if (!ret) {
  171. std::cerr << "Loading config into enclave failed\n";
  172. }
  173. return ret;
  174. }