hs_service.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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 "networkstatus.h"
  13. #include "hs_intropoint.h"
  14. #include "hs_service.h"
  15. #include "hs_common.h"
  16. #include "hs/cell_establish_intro.h"
  17. #include "hs/cell_common.h"
  18. /* XXX We don't currently use these functions, apart from generating unittest
  19. data. When we start implementing the service-side support for prop224 we
  20. should revisit these functions and use them. */
  21. /** Given an ESTABLISH_INTRO <b>cell</b>, encode it and place its payload in
  22. * <b>buf_out</b> which has size <b>buf_out_len</b>. Return the number of
  23. * bytes written, or a negative integer if there was an error. */
  24. ssize_t
  25. get_establish_intro_payload(uint8_t *buf_out, size_t buf_out_len,
  26. const trn_cell_establish_intro_t *cell)
  27. {
  28. ssize_t bytes_used = 0;
  29. if (buf_out_len < RELAY_PAYLOAD_SIZE) {
  30. return -1;
  31. }
  32. bytes_used = trn_cell_establish_intro_encode(buf_out, buf_out_len,
  33. cell);
  34. return bytes_used;
  35. }
  36. /* Set the cell extensions of <b>cell</b>. */
  37. static void
  38. set_trn_cell_extensions(trn_cell_establish_intro_t *cell)
  39. {
  40. trn_cell_extension_t *trn_cell_extensions = trn_cell_extension_new();
  41. /* For now, we don't use extensions at all. */
  42. trn_cell_extensions->num = 0; /* It's already zeroed, but be explicit. */
  43. trn_cell_establish_intro_set_extensions(cell, trn_cell_extensions);
  44. }
  45. /** Given the circuit handshake info in <b>circuit_key_material</b>, create and
  46. * return an ESTABLISH_INTRO cell. Return NULL if something went wrong. The
  47. * returned cell is allocated on the heap and it's the responsibility of the
  48. * caller to free it. */
  49. trn_cell_establish_intro_t *
  50. generate_establish_intro_cell(const uint8_t *circuit_key_material,
  51. size_t circuit_key_material_len)
  52. {
  53. trn_cell_establish_intro_t *cell = NULL;
  54. ssize_t encoded_len;
  55. log_warn(LD_GENERAL,
  56. "Generating ESTABLISH_INTRO cell (key_material_len: %u)",
  57. (unsigned) circuit_key_material_len);
  58. /* Generate short-term keypair for use in ESTABLISH_INTRO */
  59. ed25519_keypair_t key_struct;
  60. if (ed25519_keypair_generate(&key_struct, 0) < 0) {
  61. goto err;
  62. }
  63. cell = trn_cell_establish_intro_new();
  64. /* Set AUTH_KEY_TYPE: 2 means ed25519 */
  65. trn_cell_establish_intro_set_auth_key_type(cell,
  66. HS_INTRO_AUTH_KEY_TYPE_ED25519);
  67. /* Set AUTH_KEY_LEN field */
  68. /* Must also set byte-length of AUTH_KEY to match */
  69. int auth_key_len = ED25519_PUBKEY_LEN;
  70. trn_cell_establish_intro_set_auth_key_len(cell, auth_key_len);
  71. trn_cell_establish_intro_setlen_auth_key(cell, auth_key_len);
  72. /* Set AUTH_KEY field */
  73. uint8_t *auth_key_ptr = trn_cell_establish_intro_getarray_auth_key(cell);
  74. memcpy(auth_key_ptr, key_struct.pubkey.pubkey, auth_key_len);
  75. /* No cell extensions needed */
  76. set_trn_cell_extensions(cell);
  77. /* Set signature size.
  78. We need to do this up here, because _encode() needs it and we need to call
  79. _encode() to calculate the MAC and signature.
  80. */
  81. int sig_len = ED25519_SIG_LEN;
  82. trn_cell_establish_intro_set_sig_len(cell, sig_len);
  83. trn_cell_establish_intro_setlen_sig(cell, sig_len);
  84. /* XXX How to make this process easier and nicer? */
  85. /* Calculate the cell MAC (aka HANDSHAKE_AUTH). */
  86. {
  87. /* To calculate HANDSHAKE_AUTH, we dump the cell in bytes, and then derive
  88. the MAC from it. */
  89. uint8_t cell_bytes_tmp[RELAY_PAYLOAD_SIZE] = {0};
  90. uint8_t mac[TRUNNEL_SHA3_256_LEN];
  91. encoded_len = trn_cell_establish_intro_encode(cell_bytes_tmp,
  92. sizeof(cell_bytes_tmp),
  93. cell);
  94. if (encoded_len < 0) {
  95. log_warn(LD_OR, "Unable to pre-encode ESTABLISH_INTRO cell.");
  96. goto err;
  97. }
  98. /* sanity check */
  99. tor_assert(encoded_len > ED25519_SIG_LEN + 2 + TRUNNEL_SHA3_256_LEN);
  100. /* Calculate MAC of all fields before HANDSHAKE_AUTH */
  101. crypto_mac_sha3_256(mac, sizeof(mac),
  102. circuit_key_material, circuit_key_material_len,
  103. cell_bytes_tmp,
  104. encoded_len -
  105. (ED25519_SIG_LEN + 2 + TRUNNEL_SHA3_256_LEN));
  106. /* Write the MAC to the cell */
  107. uint8_t *handshake_ptr =
  108. trn_cell_establish_intro_getarray_handshake_mac(cell);
  109. memcpy(handshake_ptr, mac, sizeof(mac));
  110. }
  111. /* Calculate the cell signature */
  112. {
  113. /* To calculate the sig we follow the same procedure as above. We first
  114. dump the cell up to the sig, and then calculate the sig */
  115. uint8_t cell_bytes_tmp[RELAY_PAYLOAD_SIZE] = {0};
  116. ed25519_signature_t sig;
  117. encoded_len = trn_cell_establish_intro_encode(cell_bytes_tmp,
  118. sizeof(cell_bytes_tmp),
  119. cell);
  120. if (encoded_len < 0) {
  121. log_warn(LD_OR, "Unable to pre-encode ESTABLISH_INTRO cell (2).");
  122. goto err;
  123. }
  124. tor_assert(encoded_len > ED25519_SIG_LEN);
  125. if (ed25519_sign_prefixed(&sig,
  126. cell_bytes_tmp,
  127. encoded_len -
  128. (ED25519_SIG_LEN + sizeof(cell->sig_len)),
  129. ESTABLISH_INTRO_SIG_PREFIX,
  130. &key_struct)) {
  131. log_warn(LD_BUG, "Unable to gen signature for ESTABLISH_INTRO cell.");
  132. goto err;
  133. }
  134. /* And write the signature to the cell */
  135. uint8_t *sig_ptr = trn_cell_establish_intro_getarray_sig(cell);
  136. memcpy(sig_ptr, sig.sig, sig_len);
  137. }
  138. /* We are done! Return the cell! */
  139. return cell;
  140. err:
  141. trn_cell_establish_intro_free(cell);
  142. return NULL;
  143. }