hs_service.c 5.9 KB

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