appconfig.cpp 7.1 KB

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