hs_test_helpers.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. /* Copyright (c) 2017-2018, The Tor Project, Inc. */
  2. /* See LICENSE for licensing information */
  3. #include "core/or/or.h"
  4. #include "lib/crypt_ops/crypto_ed25519.h"
  5. #include "test/test.h"
  6. #include "feature/nodelist/torcert.h"
  7. #include "feature/hs/hs_common.h"
  8. #include "test/hs_test_helpers.h"
  9. hs_desc_intro_point_t *
  10. hs_helper_build_intro_point(const ed25519_keypair_t *signing_kp, time_t now,
  11. const char *addr, int legacy)
  12. {
  13. int ret;
  14. ed25519_keypair_t auth_kp;
  15. hs_desc_intro_point_t *intro_point = NULL;
  16. hs_desc_intro_point_t *ip = hs_desc_intro_point_new();
  17. /* For a usable intro point we need at least two link specifiers: One legacy
  18. * keyid and one ipv4 */
  19. {
  20. hs_desc_link_specifier_t *ls_legacy = tor_malloc_zero(sizeof(*ls_legacy));
  21. hs_desc_link_specifier_t *ls_v4 = tor_malloc_zero(sizeof(*ls_v4));
  22. ls_legacy->type = LS_LEGACY_ID;
  23. memcpy(ls_legacy->u.legacy_id, "0299F268FCA9D55CD157976D39AE92B4B455B3A8",
  24. DIGEST_LEN);
  25. ls_v4->u.ap.port = 9001;
  26. int family = tor_addr_parse(&ls_v4->u.ap.addr, addr);
  27. switch (family) {
  28. case AF_INET:
  29. ls_v4->type = LS_IPV4;
  30. break;
  31. case AF_INET6:
  32. ls_v4->type = LS_IPV6;
  33. break;
  34. default:
  35. /* Stop the test, not suppose to have an error. */
  36. tt_int_op(family, OP_EQ, AF_INET);
  37. }
  38. smartlist_add(ip->link_specifiers, ls_legacy);
  39. smartlist_add(ip->link_specifiers, ls_v4);
  40. }
  41. ret = ed25519_keypair_generate(&auth_kp, 0);
  42. tt_int_op(ret, ==, 0);
  43. ip->auth_key_cert = tor_cert_create(signing_kp, CERT_TYPE_AUTH_HS_IP_KEY,
  44. &auth_kp.pubkey, now,
  45. HS_DESC_CERT_LIFETIME,
  46. CERT_FLAG_INCLUDE_SIGNING_KEY);
  47. tt_assert(ip->auth_key_cert);
  48. if (legacy) {
  49. ip->legacy.key = crypto_pk_new();
  50. tt_assert(ip->legacy.key);
  51. ret = crypto_pk_generate_key(ip->legacy.key);
  52. tt_int_op(ret, ==, 0);
  53. ssize_t cert_len = tor_make_rsa_ed25519_crosscert(
  54. &signing_kp->pubkey, ip->legacy.key,
  55. now + HS_DESC_CERT_LIFETIME,
  56. &ip->legacy.cert.encoded);
  57. tt_assert(ip->legacy.cert.encoded);
  58. tt_u64_op(cert_len, OP_GT, 0);
  59. ip->legacy.cert.len = cert_len;
  60. }
  61. /* Encryption key. */
  62. {
  63. int signbit;
  64. curve25519_keypair_t curve25519_kp;
  65. ed25519_keypair_t ed25519_kp;
  66. tor_cert_t *cross_cert;
  67. ret = curve25519_keypair_generate(&curve25519_kp, 0);
  68. tt_int_op(ret, ==, 0);
  69. ed25519_keypair_from_curve25519_keypair(&ed25519_kp, &signbit,
  70. &curve25519_kp);
  71. cross_cert = tor_cert_create(signing_kp, CERT_TYPE_CROSS_HS_IP_KEYS,
  72. &ed25519_kp.pubkey, time(NULL),
  73. HS_DESC_CERT_LIFETIME,
  74. CERT_FLAG_INCLUDE_SIGNING_KEY);
  75. tt_assert(cross_cert);
  76. ip->enc_key_cert = cross_cert;
  77. }
  78. intro_point = ip;
  79. done:
  80. if (intro_point == NULL)
  81. tor_free(ip);
  82. return intro_point;
  83. }
  84. /* Return a valid hs_descriptor_t object. If no_ip is set, no introduction
  85. * points are added. */
  86. static hs_descriptor_t *
  87. hs_helper_build_hs_desc_impl(unsigned int no_ip,
  88. const ed25519_keypair_t *signing_kp)
  89. {
  90. time_t now = approx_time();
  91. ed25519_keypair_t blinded_kp;
  92. hs_descriptor_t *descp = NULL, *desc = tor_malloc_zero(sizeof(*desc));
  93. desc->plaintext_data.version = HS_DESC_SUPPORTED_FORMAT_VERSION_MAX;
  94. /* Copy only the public key into the descriptor. */
  95. memcpy(&desc->plaintext_data.signing_pubkey, &signing_kp->pubkey,
  96. sizeof(ed25519_public_key_t));
  97. uint64_t current_time_period = hs_get_time_period_num(0);
  98. hs_build_blinded_keypair(signing_kp, NULL, 0,
  99. current_time_period, &blinded_kp);
  100. /* Copy only the public key into the descriptor. */
  101. memcpy(&desc->plaintext_data.blinded_pubkey, &blinded_kp.pubkey,
  102. sizeof(ed25519_public_key_t));
  103. desc->plaintext_data.signing_key_cert =
  104. tor_cert_create(&blinded_kp, CERT_TYPE_SIGNING_HS_DESC,
  105. &signing_kp->pubkey, now, 3600,
  106. CERT_FLAG_INCLUDE_SIGNING_KEY);
  107. tt_assert(desc->plaintext_data.signing_key_cert);
  108. desc->plaintext_data.revision_counter = 42;
  109. desc->plaintext_data.lifetime_sec = 3 * 60 * 60;
  110. hs_get_subcredential(&signing_kp->pubkey, &blinded_kp.pubkey,
  111. desc->subcredential);
  112. /* Setup encrypted data section. */
  113. desc->encrypted_data.create2_ntor = 1;
  114. desc->encrypted_data.intro_auth_types = smartlist_new();
  115. desc->encrypted_data.single_onion_service = 1;
  116. smartlist_add(desc->encrypted_data.intro_auth_types, tor_strdup("ed25519"));
  117. desc->encrypted_data.intro_points = smartlist_new();
  118. if (!no_ip) {
  119. /* Add four intro points. */
  120. smartlist_add(desc->encrypted_data.intro_points,
  121. hs_helper_build_intro_point(signing_kp, now, "1.2.3.4", 0));
  122. smartlist_add(desc->encrypted_data.intro_points,
  123. hs_helper_build_intro_point(signing_kp, now, "[2600::1]", 0));
  124. smartlist_add(desc->encrypted_data.intro_points,
  125. hs_helper_build_intro_point(signing_kp, now, "3.2.1.4", 1));
  126. smartlist_add(desc->encrypted_data.intro_points,
  127. hs_helper_build_intro_point(signing_kp, now, "5.6.7.8", 1));
  128. }
  129. descp = desc;
  130. done:
  131. if (descp == NULL)
  132. tor_free(desc);
  133. return descp;
  134. }
  135. /** Helper function to get the HS subcredential using the identity keypair of
  136. * an HS. Used to decrypt descriptors in unittests. */
  137. void
  138. hs_helper_get_subcred_from_identity_keypair(ed25519_keypair_t *signing_kp,
  139. uint8_t *subcred_out)
  140. {
  141. ed25519_keypair_t blinded_kp;
  142. uint64_t current_time_period = hs_get_time_period_num(approx_time());
  143. hs_build_blinded_keypair(signing_kp, NULL, 0,
  144. current_time_period, &blinded_kp);
  145. hs_get_subcredential(&signing_kp->pubkey, &blinded_kp.pubkey,
  146. subcred_out);
  147. }
  148. /* Build a descriptor with introduction points. */
  149. hs_descriptor_t *
  150. hs_helper_build_hs_desc_with_ip(const ed25519_keypair_t *signing_kp)
  151. {
  152. return hs_helper_build_hs_desc_impl(0, signing_kp);
  153. }
  154. /* Build a descriptor without any introduction points. */
  155. hs_descriptor_t *
  156. hs_helper_build_hs_desc_no_ip(const ed25519_keypair_t *signing_kp)
  157. {
  158. return hs_helper_build_hs_desc_impl(1, signing_kp);
  159. }
  160. void
  161. hs_helper_desc_equal(const hs_descriptor_t *desc1,
  162. const hs_descriptor_t *desc2)
  163. {
  164. char *addr1 = NULL, *addr2 = NULL;
  165. /* Plaintext data section. */
  166. tt_int_op(desc1->plaintext_data.version, OP_EQ,
  167. desc2->plaintext_data.version);
  168. tt_uint_op(desc1->plaintext_data.lifetime_sec, OP_EQ,
  169. desc2->plaintext_data.lifetime_sec);
  170. tt_assert(tor_cert_eq(desc1->plaintext_data.signing_key_cert,
  171. desc2->plaintext_data.signing_key_cert));
  172. tt_mem_op(desc1->plaintext_data.signing_pubkey.pubkey, OP_EQ,
  173. desc2->plaintext_data.signing_pubkey.pubkey,
  174. ED25519_PUBKEY_LEN);
  175. tt_mem_op(desc1->plaintext_data.blinded_pubkey.pubkey, OP_EQ,
  176. desc2->plaintext_data.blinded_pubkey.pubkey,
  177. ED25519_PUBKEY_LEN);
  178. tt_u64_op(desc1->plaintext_data.revision_counter, ==,
  179. desc2->plaintext_data.revision_counter);
  180. /* NOTE: We can't compare the encrypted blob because when encoding the
  181. * descriptor, the object is immutable thus we don't update it with the
  182. * encrypted blob. As contrast to the decoding process where we populate a
  183. * descriptor object. */
  184. /* Encrypted data section. */
  185. tt_uint_op(desc1->encrypted_data.create2_ntor, ==,
  186. desc2->encrypted_data.create2_ntor);
  187. /* Authentication type. */
  188. tt_int_op(!!desc1->encrypted_data.intro_auth_types, ==,
  189. !!desc2->encrypted_data.intro_auth_types);
  190. if (desc1->encrypted_data.intro_auth_types &&
  191. desc2->encrypted_data.intro_auth_types) {
  192. tt_int_op(smartlist_len(desc1->encrypted_data.intro_auth_types), ==,
  193. smartlist_len(desc2->encrypted_data.intro_auth_types));
  194. for (int i = 0;
  195. i < smartlist_len(desc1->encrypted_data.intro_auth_types);
  196. i++) {
  197. tt_str_op(smartlist_get(desc1->encrypted_data.intro_auth_types, i),OP_EQ,
  198. smartlist_get(desc2->encrypted_data.intro_auth_types, i));
  199. }
  200. }
  201. /* Introduction points. */
  202. {
  203. tt_assert(desc1->encrypted_data.intro_points);
  204. tt_assert(desc2->encrypted_data.intro_points);
  205. tt_int_op(smartlist_len(desc1->encrypted_data.intro_points), ==,
  206. smartlist_len(desc2->encrypted_data.intro_points));
  207. for (int i=0; i < smartlist_len(desc1->encrypted_data.intro_points); i++) {
  208. hs_desc_intro_point_t *ip1 = smartlist_get(desc1->encrypted_data
  209. .intro_points, i),
  210. *ip2 = smartlist_get(desc2->encrypted_data
  211. .intro_points, i);
  212. tt_assert(tor_cert_eq(ip1->auth_key_cert, ip2->auth_key_cert));
  213. if (ip1->legacy.key) {
  214. tt_int_op(crypto_pk_cmp_keys(ip1->legacy.key, ip2->legacy.key),
  215. OP_EQ, 0);
  216. } else {
  217. tt_mem_op(&ip1->enc_key, OP_EQ, &ip2->enc_key, CURVE25519_PUBKEY_LEN);
  218. }
  219. tt_int_op(smartlist_len(ip1->link_specifiers), ==,
  220. smartlist_len(ip2->link_specifiers));
  221. for (int j = 0; j < smartlist_len(ip1->link_specifiers); j++) {
  222. hs_desc_link_specifier_t *ls1 = smartlist_get(ip1->link_specifiers, j),
  223. *ls2 = smartlist_get(ip2->link_specifiers, j);
  224. tt_int_op(ls1->type, ==, ls2->type);
  225. switch (ls1->type) {
  226. case LS_IPV4:
  227. case LS_IPV6:
  228. {
  229. addr1 = tor_addr_to_str_dup(&ls1->u.ap.addr);
  230. addr2 = tor_addr_to_str_dup(&ls2->u.ap.addr);
  231. tt_str_op(addr1, OP_EQ, addr2);
  232. tor_free(addr1);
  233. tor_free(addr2);
  234. tt_int_op(ls1->u.ap.port, ==, ls2->u.ap.port);
  235. }
  236. break;
  237. case LS_LEGACY_ID:
  238. tt_mem_op(ls1->u.legacy_id, OP_EQ, ls2->u.legacy_id,
  239. sizeof(ls1->u.legacy_id));
  240. break;
  241. default:
  242. /* Unknown type, caught it and print its value. */
  243. tt_int_op(ls1->type, OP_EQ, -1);
  244. }
  245. }
  246. }
  247. }
  248. done:
  249. tor_free(addr1);
  250. tor_free(addr2);
  251. }