appconfig.cpp 7.4 KB

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