hs_service.c 5.8 KB

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