hs_test_helpers.c 10 KB

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