hs_service.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. /* Copyright (c) 2016-2017, The Tor Project, Inc. */
  2. /* See LICENSE for licensing information */
  3. /**
  4. * \file hs_service.c
  5. * \brief Implement next generation hidden service functionality
  6. **/
  7. #include "or.h"
  8. #include "relay.h"
  9. #include "rendservice.h"
  10. #include "circuitlist.h"
  11. #include "circpathbias.h"
  12. #include "hs_intropoint.h"
  13. #include "hs_service.h"
  14. #include "hs_common.h"
  15. #include "hs/cell_establish_intro.h"
  16. #include "hs/cell_common.h"
  17. /* Set the default values for a service configuration object <b>c</b>. */
  18. static void
  19. set_service_default_config(hs_service_config_t *c,
  20. const or_options_t *options)
  21. {
  22. tor_assert(c);
  23. c->ports = smartlist_new();
  24. c->directory_path = NULL;
  25. c->descriptor_post_period = options->RendPostPeriod;
  26. c->max_streams_per_rdv_circuit = 0;
  27. c->max_streams_close_circuit = 0;
  28. c->num_intro_points = NUM_INTRO_POINTS_DEFAULT;
  29. c->allow_unknown_ports = 0;
  30. c->is_single_onion = 0;
  31. c->dir_group_readable = 0;
  32. c->is_ephemeral = 0;
  33. }
  34. /* Allocate and initilize a service object. The service configuration will
  35. * contain the default values. Return the newly allocated object pointer. This
  36. * function can't fail. */
  37. hs_service_t *
  38. hs_service_new(const or_options_t *options)
  39. {
  40. hs_service_t *service = tor_malloc_zero(sizeof(hs_service_t));
  41. /* Set default configuration value. */
  42. set_service_default_config(&service->config, options);
  43. /* Set the default service version. */
  44. service->version = HS_SERVICE_DEFAULT_VERSION;
  45. return service;
  46. }
  47. /* Free the given <b>service</b> object and all its content. This function
  48. * also takes care of wiping service keys from memory. It is safe to pass a
  49. * NULL pointer. */
  50. void
  51. hs_service_free(hs_service_t *service)
  52. {
  53. if (service == NULL) {
  54. return;
  55. }
  56. /* Free descriptors. */
  57. if (service->desc_current) {
  58. hs_descriptor_free(service->desc_current->desc);
  59. /* Wipe keys. */
  60. memwipe(&service->desc_current->signing_kp, 0,
  61. sizeof(service->desc_current->signing_kp));
  62. memwipe(&service->desc_current->blinded_kp, 0,
  63. sizeof(service->desc_current->blinded_kp));
  64. /* XXX: Free intro points. */
  65. tor_free(service->desc_current);
  66. }
  67. if (service->desc_next) {
  68. hs_descriptor_free(service->desc_next->desc);
  69. /* Wipe keys. */
  70. memwipe(&service->desc_next->signing_kp, 0,
  71. sizeof(service->desc_next->signing_kp));
  72. memwipe(&service->desc_next->blinded_kp, 0,
  73. sizeof(service->desc_next->blinded_kp));
  74. /* XXX: Free intro points. */
  75. tor_free(service->desc_next);
  76. }
  77. /* Free service configuration. */
  78. tor_free(service->config.directory_path);
  79. if (service->config.ports) {
  80. SMARTLIST_FOREACH(service->config.ports, rend_service_port_config_t *, p,
  81. rend_service_port_config_free(p););
  82. smartlist_free(service->config.ports);
  83. }
  84. /* Wipe service keys. */
  85. memwipe(&service->keys.identity_sk, 0, sizeof(service->keys.identity_sk));
  86. tor_free(service);
  87. }
  88. /* Release all global the storage of hidden service subsystem. */
  89. void
  90. hs_service_free_all(void)
  91. {
  92. rend_service_free_all();
  93. }
  94. /* XXX We don't currently use these functions, apart from generating unittest
  95. data. When we start implementing the service-side support for prop224 we
  96. should revisit these functions and use them. */
  97. /** Given an ESTABLISH_INTRO <b>cell</b>, encode it and place its payload in
  98. * <b>buf_out</b> which has size <b>buf_out_len</b>. Return the number of
  99. * bytes written, or a negative integer if there was an error. */
  100. ssize_t
  101. get_establish_intro_payload(uint8_t *buf_out, size_t buf_out_len,
  102. const trn_cell_establish_intro_t *cell)
  103. {
  104. ssize_t bytes_used = 0;
  105. if (buf_out_len < RELAY_PAYLOAD_SIZE) {
  106. return -1;
  107. }
  108. bytes_used = trn_cell_establish_intro_encode(buf_out, buf_out_len,
  109. cell);
  110. return bytes_used;
  111. }
  112. /* Set the cell extensions of <b>cell</b>. */
  113. static void
  114. set_trn_cell_extensions(trn_cell_establish_intro_t *cell)
  115. {
  116. trn_cell_extension_t *trn_cell_extensions = trn_cell_extension_new();
  117. /* For now, we don't use extensions at all. */
  118. trn_cell_extensions->num = 0; /* It's already zeroed, but be explicit. */
  119. trn_cell_establish_intro_set_extensions(cell, trn_cell_extensions);
  120. }
  121. /** Given the circuit handshake info in <b>circuit_key_material</b>, create and
  122. * return an ESTABLISH_INTRO cell. Return NULL if something went wrong. The
  123. * returned cell is allocated on the heap and it's the responsibility of the
  124. * caller to free it. */
  125. trn_cell_establish_intro_t *
  126. generate_establish_intro_cell(const uint8_t *circuit_key_material,
  127. size_t circuit_key_material_len)
  128. {
  129. trn_cell_establish_intro_t *cell = NULL;
  130. ssize_t encoded_len;
  131. log_warn(LD_GENERAL,
  132. "Generating ESTABLISH_INTRO cell (key_material_len: %u)",
  133. (unsigned) circuit_key_material_len);
  134. /* Generate short-term keypair for use in ESTABLISH_INTRO */
  135. ed25519_keypair_t key_struct;
  136. if (ed25519_keypair_generate(&key_struct, 0) < 0) {
  137. goto err;
  138. }
  139. cell = trn_cell_establish_intro_new();
  140. /* Set AUTH_KEY_TYPE: 2 means ed25519 */
  141. trn_cell_establish_intro_set_auth_key_type(cell,
  142. HS_INTRO_AUTH_KEY_TYPE_ED25519);
  143. /* Set AUTH_KEY_LEN field */
  144. /* Must also set byte-length of AUTH_KEY to match */
  145. int auth_key_len = ED25519_PUBKEY_LEN;
  146. trn_cell_establish_intro_set_auth_key_len(cell, auth_key_len);
  147. trn_cell_establish_intro_setlen_auth_key(cell, auth_key_len);
  148. /* Set AUTH_KEY field */
  149. uint8_t *auth_key_ptr = trn_cell_establish_intro_getarray_auth_key(cell);
  150. memcpy(auth_key_ptr, key_struct.pubkey.pubkey, auth_key_len);
  151. /* No cell extensions needed */
  152. set_trn_cell_extensions(cell);
  153. /* Set signature size.
  154. We need to do this up here, because _encode() needs it and we need to call
  155. _encode() to calculate the MAC and signature.
  156. */
  157. int sig_len = ED25519_SIG_LEN;
  158. trn_cell_establish_intro_set_sig_len(cell, sig_len);
  159. trn_cell_establish_intro_setlen_sig(cell, sig_len);
  160. /* XXX How to make this process easier and nicer? */
  161. /* Calculate the cell MAC (aka HANDSHAKE_AUTH). */
  162. {
  163. /* To calculate HANDSHAKE_AUTH, we dump the cell in bytes, and then derive
  164. the MAC from it. */
  165. uint8_t cell_bytes_tmp[RELAY_PAYLOAD_SIZE] = {0};
  166. uint8_t mac[TRUNNEL_SHA3_256_LEN];
  167. encoded_len = trn_cell_establish_intro_encode(cell_bytes_tmp,
  168. sizeof(cell_bytes_tmp),
  169. cell);
  170. if (encoded_len < 0) {
  171. log_warn(LD_OR, "Unable to pre-encode ESTABLISH_INTRO cell.");
  172. goto err;
  173. }
  174. /* sanity check */
  175. tor_assert(encoded_len > ED25519_SIG_LEN + 2 + TRUNNEL_SHA3_256_LEN);
  176. /* Calculate MAC of all fields before HANDSHAKE_AUTH */
  177. crypto_mac_sha3_256(mac, sizeof(mac),
  178. circuit_key_material, circuit_key_material_len,
  179. cell_bytes_tmp,
  180. encoded_len -
  181. (ED25519_SIG_LEN + 2 + TRUNNEL_SHA3_256_LEN));
  182. /* Write the MAC to the cell */
  183. uint8_t *handshake_ptr =
  184. trn_cell_establish_intro_getarray_handshake_mac(cell);
  185. memcpy(handshake_ptr, mac, sizeof(mac));
  186. }
  187. /* Calculate the cell signature */
  188. {
  189. /* To calculate the sig we follow the same procedure as above. We first
  190. dump the cell up to the sig, and then calculate the sig */
  191. uint8_t cell_bytes_tmp[RELAY_PAYLOAD_SIZE] = {0};
  192. ed25519_signature_t sig;
  193. encoded_len = trn_cell_establish_intro_encode(cell_bytes_tmp,
  194. sizeof(cell_bytes_tmp),
  195. cell);
  196. if (encoded_len < 0) {
  197. log_warn(LD_OR, "Unable to pre-encode ESTABLISH_INTRO cell (2).");
  198. goto err;
  199. }
  200. tor_assert(encoded_len > ED25519_SIG_LEN);
  201. if (ed25519_sign_prefixed(&sig,
  202. cell_bytes_tmp,
  203. encoded_len -
  204. (ED25519_SIG_LEN + sizeof(cell->sig_len)),
  205. ESTABLISH_INTRO_SIG_PREFIX,
  206. &key_struct)) {
  207. log_warn(LD_BUG, "Unable to gen signature for ESTABLISH_INTRO cell.");
  208. goto err;
  209. }
  210. /* And write the signature to the cell */
  211. uint8_t *sig_ptr = trn_cell_establish_intro_getarray_sig(cell);
  212. memcpy(sig_ptr, sig.sig, sig_len);
  213. }
  214. /* We are done! Return the cell! */
  215. return cell;
  216. err:
  217. trn_cell_establish_intro_free(cell);
  218. return NULL;
  219. }