appconfig.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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("priv_out")) {
  72. config.m_priv_out = pentry.second.get_value<uint8_t>();
  73. } else if (!pentry.first.compare("priv_in")) {
  74. config.m_priv_in = pentry.second.get_value<uint8_t>();
  75. } else if (!pentry.first.compare("pub_out")) {
  76. config.m_pub_out = pentry.second.get_value<uint8_t>();
  77. } else if (!pentry.first.compare("pub_in")) {
  78. config.m_pub_in = pentry.second.get_value<uint8_t>();
  79. // Currently hardcoding an AES key for client -> server communication
  80. } else if (!pentry.first.compare("client_aes_key")) {
  81. std::string hex_key = pentry.second.data();
  82. memcpy(config.client_aes_key, hex_key.c_str(), SGX_AESGCM_KEY_SIZE);
  83. } else {
  84. std::cerr << "Unknown field in params: " <<
  85. pentry.first << "\n";
  86. ret = false;
  87. }
  88. }
  89. found_params = true;
  90. } else if (!entry.first.compare("nodes")) {
  91. for (auto & node : entry.second) {
  92. NodeConfig nc;
  93. // defaults
  94. nc.weight = 1;
  95. nc.roles = ROLE_INGESTION | ROLE_ROUTING | ROLE_STORAGE;
  96. for (auto & nentry : node.second) {
  97. if (!nentry.first.compare("name")) {
  98. nc.name = nentry.second.get_value<std::string>();
  99. if (!myname.compare(nc.name)) {
  100. config.my_node_num =
  101. nodenum_t(config.nodes.size());
  102. found_my_node = true;
  103. }
  104. } else if (!nentry.first.compare("pubkey")) {
  105. ret &= hextobuf((unsigned char *)&nc.pubkey,
  106. nentry.second.get_value<std::string>().c_str(),
  107. sizeof(nc.pubkey));
  108. } else if (!nentry.first.compare("weight")) {
  109. nc.weight = nentry.second.get_value<std::uint8_t>();
  110. } else if (!nentry.first.compare("roles")) {
  111. nc.roles = nentry.second.get_value<std::uint8_t>();
  112. } else if (!nentry.first.compare("listen")) {
  113. ret &= split_host_port(nc.listenhost, nc.listenport,
  114. nentry.second.get_value<std::string>());
  115. } else if (!nentry.first.compare("clisten")) {
  116. ret &= split_host_port(nc.clistenhost, nc.clistenport,
  117. nentry.second.get_value<std::string>());
  118. } else {
  119. std::cerr << "Unknown field in host config: " <<
  120. nentry.first << "\n";
  121. ret = false;
  122. }
  123. }
  124. config.nodes.push_back(std::move(nc));
  125. }
  126. } else {
  127. std::cerr << "Unknown key in config: " <<
  128. entry.first << "\n";
  129. ret = false;
  130. }
  131. }
  132. if (!found_params) {
  133. std::cerr << "Could not find params in config\n";
  134. ret = false;
  135. }
  136. if (!found_my_node) {
  137. std::cerr << "Could not find my own node entry in config\n";
  138. ret = false;
  139. }
  140. if (!ret) return ret;
  141. // Now load the config into the enclave
  142. EnclaveAPIParams apiparams;
  143. apiparams.user_count = config.user_count;
  144. apiparams.msg_size = config.msg_size;
  145. apiparams.m_priv_out = config.m_priv_out;
  146. apiparams.m_priv_in = config.m_priv_in;
  147. apiparams.m_pub_out = config.m_pub_out;
  148. apiparams.m_pub_in = config.m_pub_in;
  149. memcpy(apiparams.client_aes_key, config.client_aes_key, SGX_AESGCM_KEY_SIZE);
  150. nodenum_t num_nodes = (nodenum_t)(config.nodes.size());
  151. std::vector<EnclaveAPINodeConfig> apinodeconfigs;
  152. apinodeconfigs.resize(num_nodes);
  153. for (nodenum_t i=0; i<num_nodes; ++i) {
  154. memmove(&apinodeconfigs[i].pubkey,
  155. &config.nodes[i].pubkey, sizeof(apinodeconfigs[i].pubkey));
  156. apinodeconfigs[i].weight = config.nodes[i].weight;
  157. apinodeconfigs[i].roles = config.nodes[i].roles;
  158. }
  159. bool private_routing = true;
  160. ret &= ecall_config_load(nthreads, private_routing, &apiparams,
  161. apinodeconfigs.data(), num_nodes, config.my_node_num);
  162. if (!ret) {
  163. std::cerr << "Loading config into enclave failed\n";
  164. }
  165. return ret;
  166. }