hs_test_helpers.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. /* Copyright (c) 2017-2019, 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. tor_addr_t a;
  21. tor_addr_make_unspec(&a);
  22. link_specifier_t *ls_legacy = link_specifier_new();
  23. link_specifier_t *ls_ip = link_specifier_new();
  24. link_specifier_set_ls_type(ls_legacy, LS_LEGACY_ID);
  25. memset(link_specifier_getarray_un_legacy_id(ls_legacy), 'C',
  26. link_specifier_getlen_un_legacy_id(ls_legacy));
  27. int family = tor_addr_parse(&a, addr);
  28. switch (family) {
  29. case AF_INET:
  30. link_specifier_set_ls_type(ls_ip, LS_IPV4);
  31. link_specifier_set_un_ipv4_addr(ls_ip, tor_addr_to_ipv4h(&a));
  32. link_specifier_set_un_ipv4_port(ls_ip, 9001);
  33. break;
  34. case AF_INET6:
  35. link_specifier_set_ls_type(ls_ip, LS_IPV6);
  36. memcpy(link_specifier_getarray_un_ipv6_addr(ls_ip),
  37. tor_addr_to_in6_addr8(&a),
  38. link_specifier_getlen_un_ipv6_addr(ls_ip));
  39. link_specifier_set_un_ipv6_port(ls_ip, 9001);
  40. break;
  41. default:
  42. /* Stop the test, not supposed to have an error.
  43. * Compare with -1 to show the actual family.
  44. */
  45. tt_int_op(family, OP_EQ, -1);
  46. }
  47. smartlist_add(ip->link_specifiers, ls_legacy);
  48. smartlist_add(ip->link_specifiers, ls_ip);
  49. }
  50. ret = ed25519_keypair_generate(&auth_kp, 0);
  51. tt_int_op(ret, ==, 0);
  52. ip->auth_key_cert = tor_cert_create(signing_kp, CERT_TYPE_AUTH_HS_IP_KEY,
  53. &auth_kp.pubkey, now,
  54. HS_DESC_CERT_LIFETIME,
  55. CERT_FLAG_INCLUDE_SIGNING_KEY);
  56. tt_assert(ip->auth_key_cert);
  57. if (legacy) {
  58. ip->legacy.key = crypto_pk_new();
  59. tt_assert(ip->legacy.key);
  60. ret = crypto_pk_generate_key(ip->legacy.key);
  61. tt_int_op(ret, ==, 0);
  62. ssize_t cert_len = tor_make_rsa_ed25519_crosscert(
  63. &signing_kp->pubkey, ip->legacy.key,
  64. now + HS_DESC_CERT_LIFETIME,
  65. &ip->legacy.cert.encoded);
  66. tt_assert(ip->legacy.cert.encoded);
  67. tt_u64_op(cert_len, OP_GT, 0);
  68. ip->legacy.cert.len = cert_len;
  69. }
  70. /* Encryption key. */
  71. {
  72. int signbit;
  73. curve25519_keypair_t curve25519_kp;
  74. ed25519_keypair_t ed25519_kp;
  75. tor_cert_t *cross_cert;
  76. ret = curve25519_keypair_generate(&curve25519_kp, 0);
  77. tt_int_op(ret, ==, 0);
  78. ed25519_keypair_from_curve25519_keypair(&ed25519_kp, &signbit,
  79. &curve25519_kp);
  80. cross_cert = tor_cert_create(signing_kp, CERT_TYPE_CROSS_HS_IP_KEYS,
  81. &ed25519_kp.pubkey, time(NULL),
  82. HS_DESC_CERT_LIFETIME,
  83. CERT_FLAG_INCLUDE_SIGNING_KEY);
  84. tt_assert(cross_cert);
  85. ip->enc_key_cert = cross_cert;
  86. }
  87. intro_point = ip;
  88. done:
  89. if (intro_point == NULL)
  90. tor_free(ip);
  91. return intro_point;
  92. }
  93. /* Return a valid hs_descriptor_t object. If no_ip is set, no introduction
  94. * points are added. */
  95. static hs_descriptor_t *
  96. hs_helper_build_hs_desc_impl(unsigned int no_ip,
  97. const ed25519_keypair_t *signing_kp)
  98. {
  99. int ret;
  100. int i;
  101. time_t now = approx_time();
  102. ed25519_keypair_t blinded_kp;
  103. curve25519_keypair_t auth_ephemeral_kp;
  104. hs_descriptor_t *descp = NULL, *desc = tor_malloc_zero(sizeof(*desc));
  105. desc->plaintext_data.version = HS_DESC_SUPPORTED_FORMAT_VERSION_MAX;
  106. /* Copy only the public key into the descriptor. */
  107. memcpy(&desc->plaintext_data.signing_pubkey, &signing_kp->pubkey,
  108. sizeof(ed25519_public_key_t));
  109. uint64_t current_time_period = hs_get_time_period_num(0);
  110. hs_build_blinded_keypair(signing_kp, NULL, 0,
  111. current_time_period, &blinded_kp);
  112. /* Copy only the public key into the descriptor. */
  113. memcpy(&desc->plaintext_data.blinded_pubkey, &blinded_kp.pubkey,
  114. sizeof(ed25519_public_key_t));
  115. desc->plaintext_data.signing_key_cert =
  116. tor_cert_create(&blinded_kp, CERT_TYPE_SIGNING_HS_DESC,
  117. &signing_kp->pubkey, now, 3600,
  118. CERT_FLAG_INCLUDE_SIGNING_KEY);
  119. tt_assert(desc->plaintext_data.signing_key_cert);
  120. desc->plaintext_data.revision_counter = 42;
  121. desc->plaintext_data.lifetime_sec = 3 * 60 * 60;
  122. hs_get_subcredential(&signing_kp->pubkey, &blinded_kp.pubkey,
  123. desc->subcredential);
  124. /* Setup superencrypted data section. */
  125. ret = curve25519_keypair_generate(&auth_ephemeral_kp, 0);
  126. tt_int_op(ret, ==, 0);
  127. memcpy(&desc->superencrypted_data.auth_ephemeral_pubkey,
  128. &auth_ephemeral_kp.pubkey,
  129. sizeof(curve25519_public_key_t));
  130. desc->superencrypted_data.clients = smartlist_new();
  131. for (i = 0; i < HS_DESC_AUTH_CLIENT_MULTIPLE; i++) {
  132. hs_desc_authorized_client_t *desc_client =
  133. hs_desc_build_fake_authorized_client();
  134. smartlist_add(desc->superencrypted_data.clients, desc_client);
  135. }
  136. /* Setup encrypted data section. */
  137. desc->encrypted_data.create2_ntor = 1;
  138. desc->encrypted_data.intro_auth_types = smartlist_new();
  139. desc->encrypted_data.single_onion_service = 1;
  140. smartlist_add(desc->encrypted_data.intro_auth_types, tor_strdup("ed25519"));
  141. desc->encrypted_data.intro_points = smartlist_new();
  142. if (!no_ip) {
  143. /* Add four intro points. */
  144. smartlist_add(desc->encrypted_data.intro_points,
  145. hs_helper_build_intro_point(signing_kp, now, "1.2.3.4", 0));
  146. smartlist_add(desc->encrypted_data.intro_points,
  147. hs_helper_build_intro_point(signing_kp, now, "[2600::1]", 0));
  148. smartlist_add(desc->encrypted_data.intro_points,
  149. hs_helper_build_intro_point(signing_kp, now, "3.2.1.4", 1));
  150. smartlist_add(desc->encrypted_data.intro_points,
  151. hs_helper_build_intro_point(signing_kp, now, "5.6.7.8", 1));
  152. }
  153. descp = desc;
  154. done:
  155. if (descp == NULL)
  156. tor_free(desc);
  157. return descp;
  158. }
  159. /** Helper function to get the HS subcredential using the identity keypair of
  160. * an HS. Used to decrypt descriptors in unittests. */
  161. void
  162. hs_helper_get_subcred_from_identity_keypair(ed25519_keypair_t *signing_kp,
  163. uint8_t *subcred_out)
  164. {
  165. ed25519_keypair_t blinded_kp;
  166. uint64_t current_time_period = hs_get_time_period_num(approx_time());
  167. hs_build_blinded_keypair(signing_kp, NULL, 0,
  168. current_time_period, &blinded_kp);
  169. hs_get_subcredential(&signing_kp->pubkey, &blinded_kp.pubkey,
  170. subcred_out);
  171. }
  172. /* Build a descriptor with introduction points. */
  173. hs_descriptor_t *
  174. hs_helper_build_hs_desc_with_ip(const ed25519_keypair_t *signing_kp)
  175. {
  176. return hs_helper_build_hs_desc_impl(0, signing_kp);
  177. }
  178. /* Build a descriptor without any introduction points. */
  179. hs_descriptor_t *
  180. hs_helper_build_hs_desc_no_ip(const ed25519_keypair_t *signing_kp)
  181. {
  182. return hs_helper_build_hs_desc_impl(1, signing_kp);
  183. }
  184. void
  185. hs_helper_desc_equal(const hs_descriptor_t *desc1,
  186. const hs_descriptor_t *desc2)
  187. {
  188. /* Plaintext data section. */
  189. tt_int_op(desc1->plaintext_data.version, OP_EQ,
  190. desc2->plaintext_data.version);
  191. tt_uint_op(desc1->plaintext_data.lifetime_sec, OP_EQ,
  192. desc2->plaintext_data.lifetime_sec);
  193. tt_assert(tor_cert_eq(desc1->plaintext_data.signing_key_cert,
  194. desc2->plaintext_data.signing_key_cert));
  195. tt_mem_op(desc1->plaintext_data.signing_pubkey.pubkey, OP_EQ,
  196. desc2->plaintext_data.signing_pubkey.pubkey,
  197. ED25519_PUBKEY_LEN);
  198. tt_mem_op(desc1->plaintext_data.blinded_pubkey.pubkey, OP_EQ,
  199. desc2->plaintext_data.blinded_pubkey.pubkey,
  200. ED25519_PUBKEY_LEN);
  201. tt_u64_op(desc1->plaintext_data.revision_counter, ==,
  202. desc2->plaintext_data.revision_counter);
  203. /* NOTE: We can't compare the encrypted blob because when encoding the
  204. * descriptor, the object is immutable thus we don't update it with the
  205. * encrypted blob. As contrast to the decoding process where we populate a
  206. * descriptor object. */
  207. /* Superencrypted data section. */
  208. tt_mem_op(desc1->superencrypted_data.auth_ephemeral_pubkey.public_key, OP_EQ,
  209. desc2->superencrypted_data.auth_ephemeral_pubkey.public_key,
  210. CURVE25519_PUBKEY_LEN);
  211. /* Auth clients. */
  212. {
  213. tt_assert(desc1->superencrypted_data.clients);
  214. tt_assert(desc2->superencrypted_data.clients);
  215. tt_int_op(smartlist_len(desc1->superencrypted_data.clients), ==,
  216. smartlist_len(desc2->superencrypted_data.clients));
  217. for (int i=0;
  218. i < smartlist_len(desc1->superencrypted_data.clients);
  219. i++) {
  220. hs_desc_authorized_client_t
  221. *client1 = smartlist_get(desc1->superencrypted_data.clients, i),
  222. *client2 = smartlist_get(desc2->superencrypted_data.clients, i);
  223. tt_mem_op(client1->client_id, OP_EQ, client2->client_id,
  224. sizeof(client1->client_id));
  225. tt_mem_op(client1->iv, OP_EQ, client2->iv,
  226. sizeof(client1->iv));
  227. tt_mem_op(client1->encrypted_cookie, OP_EQ, client2->encrypted_cookie,
  228. sizeof(client1->encrypted_cookie));
  229. }
  230. }
  231. /* Encrypted data section. */
  232. tt_uint_op(desc1->encrypted_data.create2_ntor, ==,
  233. desc2->encrypted_data.create2_ntor);
  234. /* Authentication type. */
  235. tt_int_op(!!desc1->encrypted_data.intro_auth_types, ==,
  236. !!desc2->encrypted_data.intro_auth_types);
  237. if (desc1->encrypted_data.intro_auth_types &&
  238. desc2->encrypted_data.intro_auth_types) {
  239. tt_int_op(smartlist_len(desc1->encrypted_data.intro_auth_types), ==,
  240. smartlist_len(desc2->encrypted_data.intro_auth_types));
  241. for (int i = 0;
  242. i < smartlist_len(desc1->encrypted_data.intro_auth_types);
  243. i++) {
  244. tt_str_op(smartlist_get(desc1->encrypted_data.intro_auth_types, i),OP_EQ,
  245. smartlist_get(desc2->encrypted_data.intro_auth_types, i));
  246. }
  247. }
  248. /* Introduction points. */
  249. {
  250. tt_assert(desc1->encrypted_data.intro_points);
  251. tt_assert(desc2->encrypted_data.intro_points);
  252. tt_int_op(smartlist_len(desc1->encrypted_data.intro_points), ==,
  253. smartlist_len(desc2->encrypted_data.intro_points));
  254. for (int i=0; i < smartlist_len(desc1->encrypted_data.intro_points); i++) {
  255. hs_desc_intro_point_t *ip1 = smartlist_get(desc1->encrypted_data
  256. .intro_points, i),
  257. *ip2 = smartlist_get(desc2->encrypted_data
  258. .intro_points, i);
  259. tt_assert(tor_cert_eq(ip1->auth_key_cert, ip2->auth_key_cert));
  260. if (ip1->legacy.key) {
  261. tt_int_op(crypto_pk_cmp_keys(ip1->legacy.key, ip2->legacy.key),
  262. OP_EQ, 0);
  263. } else {
  264. tt_mem_op(&ip1->enc_key, OP_EQ, &ip2->enc_key, CURVE25519_PUBKEY_LEN);
  265. }
  266. tt_int_op(smartlist_len(ip1->link_specifiers), ==,
  267. smartlist_len(ip2->link_specifiers));
  268. for (int j = 0; j < smartlist_len(ip1->link_specifiers); j++) {
  269. link_specifier_t *ls1 = smartlist_get(ip1->link_specifiers, j),
  270. *ls2 = smartlist_get(ip2->link_specifiers, j);
  271. tt_int_op(link_specifier_get_ls_type(ls1), ==,
  272. link_specifier_get_ls_type(ls2));
  273. switch (link_specifier_get_ls_type(ls1)) {
  274. case LS_IPV4:
  275. {
  276. uint32_t addr1 = link_specifier_get_un_ipv4_addr(ls1);
  277. uint32_t addr2 = link_specifier_get_un_ipv4_addr(ls2);
  278. tt_int_op(addr1, OP_EQ, addr2);
  279. uint16_t port1 = link_specifier_get_un_ipv4_port(ls1);
  280. uint16_t port2 = link_specifier_get_un_ipv4_port(ls2);
  281. tt_int_op(port1, ==, port2);
  282. }
  283. break;
  284. case LS_IPV6:
  285. {
  286. const uint8_t *addr1 =
  287. link_specifier_getconstarray_un_ipv6_addr(ls1);
  288. const uint8_t *addr2 =
  289. link_specifier_getconstarray_un_ipv6_addr(ls2);
  290. tt_int_op(link_specifier_getlen_un_ipv6_addr(ls1), OP_EQ,
  291. link_specifier_getlen_un_ipv6_addr(ls2));
  292. tt_mem_op(addr1, OP_EQ, addr2,
  293. link_specifier_getlen_un_ipv6_addr(ls1));
  294. uint16_t port1 = link_specifier_get_un_ipv6_port(ls1);
  295. uint16_t port2 = link_specifier_get_un_ipv6_port(ls2);
  296. tt_int_op(port1, ==, port2);
  297. }
  298. break;
  299. case LS_LEGACY_ID:
  300. {
  301. const uint8_t *id1 =
  302. link_specifier_getconstarray_un_legacy_id(ls1);
  303. const uint8_t *id2 =
  304. link_specifier_getconstarray_un_legacy_id(ls2);
  305. tt_int_op(link_specifier_getlen_un_legacy_id(ls1), OP_EQ,
  306. link_specifier_getlen_un_legacy_id(ls2));
  307. tt_mem_op(id1, OP_EQ, id2,
  308. link_specifier_getlen_un_legacy_id(ls1));
  309. }
  310. break;
  311. default:
  312. /* Unknown type, caught it and print its value. */
  313. tt_int_op(link_specifier_get_ls_type(ls1), OP_EQ, -1);
  314. }
  315. }
  316. }
  317. }
  318. done:
  319. ;
  320. }