hs_test_helpers.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. /* Copyright (c) 2017, The Tor Project, Inc. */
  2. /* See LICENSE for licensing information */
  3. #include "or.h"
  4. #include "crypto_ed25519.h"
  5. #include "test.h"
  6. #include "torcert.h"
  7. #include "hs_common.h"
  8. #include "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. return intro_point;
  81. }
  82. /* Return a valid hs_descriptor_t object. If no_ip is set, no introduction
  83. * points are added. */
  84. static hs_descriptor_t *
  85. hs_helper_build_hs_desc_impl(unsigned int no_ip,
  86. const ed25519_keypair_t *signing_kp)
  87. {
  88. time_t now = approx_time();
  89. ed25519_keypair_t blinded_kp;
  90. hs_descriptor_t *descp = NULL, *desc = tor_malloc_zero(sizeof(*desc));
  91. desc->plaintext_data.version = HS_DESC_SUPPORTED_FORMAT_VERSION_MAX;
  92. /* Copy only the public key into the descriptor. */
  93. memcpy(&desc->plaintext_data.signing_pubkey, &signing_kp->pubkey,
  94. sizeof(ed25519_public_key_t));
  95. uint64_t current_time_period = hs_get_time_period_num(0);
  96. hs_build_blinded_keypair(signing_kp, NULL, 0,
  97. current_time_period, &blinded_kp);
  98. /* Copy only the public key into the descriptor. */
  99. memcpy(&desc->plaintext_data.blinded_pubkey, &blinded_kp.pubkey,
  100. sizeof(ed25519_public_key_t));
  101. desc->plaintext_data.signing_key_cert =
  102. tor_cert_create(&blinded_kp, CERT_TYPE_SIGNING_HS_DESC,
  103. &signing_kp->pubkey, now, 3600,
  104. CERT_FLAG_INCLUDE_SIGNING_KEY);
  105. tt_assert(desc->plaintext_data.signing_key_cert);
  106. desc->plaintext_data.revision_counter = 42;
  107. desc->plaintext_data.lifetime_sec = 3 * 60 * 60;
  108. hs_get_subcredential(&signing_kp->pubkey, &blinded_kp.pubkey,
  109. desc->subcredential);
  110. /* Setup encrypted data section. */
  111. desc->encrypted_data.create2_ntor = 1;
  112. desc->encrypted_data.intro_auth_types = smartlist_new();
  113. desc->encrypted_data.single_onion_service = 1;
  114. smartlist_add(desc->encrypted_data.intro_auth_types, tor_strdup("ed25519"));
  115. desc->encrypted_data.intro_points = smartlist_new();
  116. if (!no_ip) {
  117. /* Add four intro points. */
  118. smartlist_add(desc->encrypted_data.intro_points,
  119. hs_helper_build_intro_point(signing_kp, now, "1.2.3.4", 0));
  120. smartlist_add(desc->encrypted_data.intro_points,
  121. hs_helper_build_intro_point(signing_kp, now, "[2600::1]", 0));
  122. smartlist_add(desc->encrypted_data.intro_points,
  123. hs_helper_build_intro_point(signing_kp, now, "3.2.1.4", 1));
  124. smartlist_add(desc->encrypted_data.intro_points,
  125. hs_helper_build_intro_point(signing_kp, now, "5.6.7.8", 1));
  126. }
  127. descp = desc;
  128. done:
  129. return descp;
  130. }
  131. /** Helper function to get the HS subcredential using the identity keypair of
  132. * an HS. Used to decrypt descriptors in unittests. */
  133. void
  134. hs_helper_get_subcred_from_identity_keypair(ed25519_keypair_t *signing_kp,
  135. uint8_t *subcred_out)
  136. {
  137. ed25519_keypair_t blinded_kp;
  138. uint64_t current_time_period = hs_get_time_period_num(approx_time());
  139. hs_build_blinded_keypair(signing_kp, NULL, 0,
  140. current_time_period, &blinded_kp);
  141. hs_get_subcredential(&signing_kp->pubkey, &blinded_kp.pubkey,
  142. subcred_out);
  143. }
  144. /* Build a descriptor with introduction points. */
  145. hs_descriptor_t *
  146. hs_helper_build_hs_desc_with_ip(const ed25519_keypair_t *signing_kp)
  147. {
  148. return hs_helper_build_hs_desc_impl(0, signing_kp);
  149. }
  150. /* Build a descriptor without any introduction points. */
  151. hs_descriptor_t *
  152. hs_helper_build_hs_desc_no_ip(const ed25519_keypair_t *signing_kp)
  153. {
  154. return hs_helper_build_hs_desc_impl(1, signing_kp);
  155. }
  156. void
  157. hs_helper_desc_equal(const hs_descriptor_t *desc1,
  158. const hs_descriptor_t *desc2)
  159. {
  160. char *addr1 = NULL, *addr2 = NULL;
  161. /* Plaintext data section. */
  162. tt_int_op(desc1->plaintext_data.version, OP_EQ,
  163. desc2->plaintext_data.version);
  164. tt_uint_op(desc1->plaintext_data.lifetime_sec, OP_EQ,
  165. desc2->plaintext_data.lifetime_sec);
  166. tt_assert(tor_cert_eq(desc1->plaintext_data.signing_key_cert,
  167. desc2->plaintext_data.signing_key_cert));
  168. tt_mem_op(desc1->plaintext_data.signing_pubkey.pubkey, OP_EQ,
  169. desc2->plaintext_data.signing_pubkey.pubkey,
  170. ED25519_PUBKEY_LEN);
  171. tt_mem_op(desc1->plaintext_data.blinded_pubkey.pubkey, OP_EQ,
  172. desc2->plaintext_data.blinded_pubkey.pubkey,
  173. ED25519_PUBKEY_LEN);
  174. tt_u64_op(desc1->plaintext_data.revision_counter, ==,
  175. desc2->plaintext_data.revision_counter);
  176. /* NOTE: We can't compare the encrypted blob because when encoding the
  177. * descriptor, the object is immutable thus we don't update it with the
  178. * encrypted blob. As contrast to the decoding process where we populate a
  179. * descriptor object. */
  180. /* Encrypted data section. */
  181. tt_uint_op(desc1->encrypted_data.create2_ntor, ==,
  182. desc2->encrypted_data.create2_ntor);
  183. /* Authentication type. */
  184. tt_int_op(!!desc1->encrypted_data.intro_auth_types, ==,
  185. !!desc2->encrypted_data.intro_auth_types);
  186. if (desc1->encrypted_data.intro_auth_types &&
  187. desc2->encrypted_data.intro_auth_types) {
  188. tt_int_op(smartlist_len(desc1->encrypted_data.intro_auth_types), ==,
  189. smartlist_len(desc2->encrypted_data.intro_auth_types));
  190. for (int i = 0;
  191. i < smartlist_len(desc1->encrypted_data.intro_auth_types);
  192. i++) {
  193. tt_str_op(smartlist_get(desc1->encrypted_data.intro_auth_types, i),OP_EQ,
  194. smartlist_get(desc2->encrypted_data.intro_auth_types, i));
  195. }
  196. }
  197. /* Introduction points. */
  198. {
  199. tt_assert(desc1->encrypted_data.intro_points);
  200. tt_assert(desc2->encrypted_data.intro_points);
  201. tt_int_op(smartlist_len(desc1->encrypted_data.intro_points), ==,
  202. smartlist_len(desc2->encrypted_data.intro_points));
  203. for (int i=0; i < smartlist_len(desc1->encrypted_data.intro_points); i++) {
  204. hs_desc_intro_point_t *ip1 = smartlist_get(desc1->encrypted_data
  205. .intro_points, i),
  206. *ip2 = smartlist_get(desc2->encrypted_data
  207. .intro_points, i);
  208. tt_assert(tor_cert_eq(ip1->auth_key_cert, ip2->auth_key_cert));
  209. if (ip1->legacy.key) {
  210. tt_int_op(crypto_pk_cmp_keys(ip1->legacy.key, ip2->legacy.key),
  211. OP_EQ, 0);
  212. } else {
  213. tt_mem_op(&ip1->enc_key, OP_EQ, &ip2->enc_key, CURVE25519_PUBKEY_LEN);
  214. }
  215. tt_int_op(smartlist_len(ip1->link_specifiers), ==,
  216. smartlist_len(ip2->link_specifiers));
  217. for (int j = 0; j < smartlist_len(ip1->link_specifiers); j++) {
  218. hs_desc_link_specifier_t *ls1 = smartlist_get(ip1->link_specifiers, j),
  219. *ls2 = smartlist_get(ip2->link_specifiers, j);
  220. tt_int_op(ls1->type, ==, ls2->type);
  221. switch (ls1->type) {
  222. case LS_IPV4:
  223. case LS_IPV6:
  224. {
  225. addr1 = tor_addr_to_str_dup(&ls1->u.ap.addr);
  226. addr2 = tor_addr_to_str_dup(&ls2->u.ap.addr);
  227. tt_str_op(addr1, OP_EQ, addr2);
  228. tor_free(addr1);
  229. tor_free(addr2);
  230. tt_int_op(ls1->u.ap.port, ==, ls2->u.ap.port);
  231. }
  232. break;
  233. case LS_LEGACY_ID:
  234. tt_mem_op(ls1->u.legacy_id, OP_EQ, ls2->u.legacy_id,
  235. sizeof(ls1->u.legacy_id));
  236. break;
  237. default:
  238. /* Unknown type, caught it and print its value. */
  239. tt_int_op(ls1->type, OP_EQ, -1);
  240. }
  241. }
  242. }
  243. }
  244. done:
  245. tor_free(addr1);
  246. tor_free(addr2);
  247. }