hs_test_helpers.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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_test_helpers.h"
  8. hs_desc_intro_point_t *
  9. hs_helper_build_intro_point(const ed25519_keypair_t *signing_kp, time_t now,
  10. const char *addr, int legacy)
  11. {
  12. int ret;
  13. ed25519_keypair_t auth_kp;
  14. hs_desc_intro_point_t *intro_point = NULL;
  15. hs_desc_intro_point_t *ip = tor_malloc_zero(sizeof(*ip));
  16. ip->link_specifiers = smartlist_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->enc_key.legacy = crypto_pk_new();
  49. ip->enc_key_type = HS_DESC_KEY_TYPE_LEGACY;
  50. tt_assert(ip->enc_key.legacy);
  51. ret = crypto_pk_generate_key(ip->enc_key.legacy);
  52. tt_int_op(ret, ==, 0);
  53. } else {
  54. ret = curve25519_keypair_generate(&ip->enc_key.curve25519, 0);
  55. tt_int_op(ret, ==, 0);
  56. ip->enc_key_type = HS_DESC_KEY_TYPE_CURVE25519;
  57. }
  58. intro_point = ip;
  59. done:
  60. return intro_point;
  61. }
  62. /* Return a valid hs_descriptor_t object. If no_ip is set, no introduction
  63. * points are added. */
  64. static hs_descriptor_t *
  65. hs_helper_build_hs_desc_impl(unsigned int no_ip,
  66. const ed25519_keypair_t *signing_kp)
  67. {
  68. int ret;
  69. time_t now = time(NULL);
  70. ed25519_keypair_t blinded_kp;
  71. hs_descriptor_t *descp = NULL, *desc = tor_malloc_zero(sizeof(*desc));
  72. desc->plaintext_data.version = HS_DESC_SUPPORTED_FORMAT_VERSION_MAX;
  73. /* Copy only the public key into the descriptor. */
  74. memcpy(&desc->plaintext_data.signing_pubkey, &signing_kp->pubkey,
  75. sizeof(ed25519_public_key_t));
  76. ret = ed25519_keypair_generate(&blinded_kp, 0);
  77. tt_int_op(ret, ==, 0);
  78. /* Copy only the public key into the descriptor. */
  79. memcpy(&desc->plaintext_data.blinded_pubkey, &blinded_kp.pubkey,
  80. sizeof(ed25519_public_key_t));
  81. desc->plaintext_data.signing_key_cert =
  82. tor_cert_create(&blinded_kp, CERT_TYPE_SIGNING_HS_DESC,
  83. &signing_kp->pubkey, now, 3600,
  84. CERT_FLAG_INCLUDE_SIGNING_KEY);
  85. tt_assert(desc->plaintext_data.signing_key_cert);
  86. desc->plaintext_data.revision_counter = 42;
  87. desc->plaintext_data.lifetime_sec = 3 * 60 * 60;
  88. /* Setup encrypted data section. */
  89. desc->encrypted_data.create2_ntor = 1;
  90. desc->encrypted_data.intro_auth_types = smartlist_new();
  91. desc->encrypted_data.single_onion_service = 1;
  92. smartlist_add(desc->encrypted_data.intro_auth_types, tor_strdup("ed25519"));
  93. desc->encrypted_data.intro_points = smartlist_new();
  94. if (!no_ip) {
  95. /* Add four intro points. */
  96. smartlist_add(desc->encrypted_data.intro_points,
  97. hs_helper_build_intro_point(signing_kp, now, "1.2.3.4", 0));
  98. smartlist_add(desc->encrypted_data.intro_points,
  99. hs_helper_build_intro_point(signing_kp, now, "[2600::1]", 0));
  100. smartlist_add(desc->encrypted_data.intro_points,
  101. hs_helper_build_intro_point(signing_kp, now, "3.2.1.4", 1));
  102. smartlist_add(desc->encrypted_data.intro_points,
  103. hs_helper_build_intro_point(signing_kp, now, "", 1));
  104. }
  105. descp = desc;
  106. done:
  107. return descp;
  108. }
  109. /* Build a descriptor with introduction points. */
  110. hs_descriptor_t *
  111. hs_helper_build_hs_desc_with_ip(const ed25519_keypair_t *signing_kp)
  112. {
  113. return hs_helper_build_hs_desc_impl(0, signing_kp);
  114. }
  115. /* Build a descriptor without any introduction points. */
  116. hs_descriptor_t *
  117. hs_helper_build_hs_desc_no_ip(const ed25519_keypair_t *signing_kp)
  118. {
  119. return hs_helper_build_hs_desc_impl(1, signing_kp);
  120. }
  121. void
  122. hs_helper_desc_equal(const hs_descriptor_t *desc1,
  123. const hs_descriptor_t *desc2)
  124. {
  125. char *addr1 = NULL, *addr2 = NULL;
  126. /* Plaintext data section. */
  127. tt_int_op(desc1->plaintext_data.version, OP_EQ,
  128. desc2->plaintext_data.version);
  129. tt_uint_op(desc1->plaintext_data.lifetime_sec, OP_EQ,
  130. desc2->plaintext_data.lifetime_sec);
  131. tt_assert(tor_cert_eq(desc1->plaintext_data.signing_key_cert,
  132. desc2->plaintext_data.signing_key_cert));
  133. tt_mem_op(desc1->plaintext_data.signing_pubkey.pubkey, OP_EQ,
  134. desc2->plaintext_data.signing_pubkey.pubkey,
  135. ED25519_PUBKEY_LEN);
  136. tt_mem_op(desc1->plaintext_data.blinded_pubkey.pubkey, OP_EQ,
  137. desc2->plaintext_data.blinded_pubkey.pubkey,
  138. ED25519_PUBKEY_LEN);
  139. tt_u64_op(desc1->plaintext_data.revision_counter, ==,
  140. desc2->plaintext_data.revision_counter);
  141. /* NOTE: We can't compare the encrypted blob because when encoding the
  142. * descriptor, the object is immutable thus we don't update it with the
  143. * encrypted blob. As contrast to the decoding process where we populate a
  144. * descriptor object. */
  145. /* Encrypted data section. */
  146. tt_uint_op(desc1->encrypted_data.create2_ntor, ==,
  147. desc2->encrypted_data.create2_ntor);
  148. /* Authentication type. */
  149. tt_int_op(!!desc1->encrypted_data.intro_auth_types, ==,
  150. !!desc2->encrypted_data.intro_auth_types);
  151. if (desc1->encrypted_data.intro_auth_types &&
  152. desc2->encrypted_data.intro_auth_types) {
  153. tt_int_op(smartlist_len(desc1->encrypted_data.intro_auth_types), ==,
  154. smartlist_len(desc2->encrypted_data.intro_auth_types));
  155. for (int i = 0;
  156. i < smartlist_len(desc1->encrypted_data.intro_auth_types);
  157. i++) {
  158. tt_str_op(smartlist_get(desc1->encrypted_data.intro_auth_types, i),OP_EQ,
  159. smartlist_get(desc2->encrypted_data.intro_auth_types, i));
  160. }
  161. }
  162. /* Introduction points. */
  163. {
  164. tt_assert(desc1->encrypted_data.intro_points);
  165. tt_assert(desc2->encrypted_data.intro_points);
  166. tt_int_op(smartlist_len(desc1->encrypted_data.intro_points), ==,
  167. smartlist_len(desc2->encrypted_data.intro_points));
  168. for (int i=0; i < smartlist_len(desc1->encrypted_data.intro_points); i++) {
  169. hs_desc_intro_point_t *ip1 = smartlist_get(desc1->encrypted_data
  170. .intro_points, i),
  171. *ip2 = smartlist_get(desc2->encrypted_data
  172. .intro_points, i);
  173. tt_assert(tor_cert_eq(ip1->auth_key_cert, ip2->auth_key_cert));
  174. tt_int_op(ip1->enc_key_type, OP_EQ, ip2->enc_key_type);
  175. tt_assert(ip1->enc_key_type == HS_DESC_KEY_TYPE_LEGACY ||
  176. ip1->enc_key_type == HS_DESC_KEY_TYPE_CURVE25519);
  177. switch (ip1->enc_key_type) {
  178. case HS_DESC_KEY_TYPE_LEGACY:
  179. tt_int_op(crypto_pk_cmp_keys(ip1->enc_key.legacy,
  180. ip2->enc_key.legacy), OP_EQ, 0);
  181. break;
  182. case HS_DESC_KEY_TYPE_CURVE25519:
  183. tt_mem_op(ip1->enc_key.curve25519.pubkey.public_key, OP_EQ,
  184. ip2->enc_key.curve25519.pubkey.public_key,
  185. CURVE25519_PUBKEY_LEN);
  186. break;
  187. }
  188. tt_int_op(smartlist_len(ip1->link_specifiers), ==,
  189. smartlist_len(ip2->link_specifiers));
  190. for (int j = 0; j < smartlist_len(ip1->link_specifiers); j++) {
  191. hs_desc_link_specifier_t *ls1 = smartlist_get(ip1->link_specifiers, j),
  192. *ls2 = smartlist_get(ip2->link_specifiers, j);
  193. tt_int_op(ls1->type, ==, ls2->type);
  194. switch (ls1->type) {
  195. case LS_IPV4:
  196. case LS_IPV6:
  197. {
  198. addr1 = tor_addr_to_str_dup(&ls1->u.ap.addr);
  199. addr2 = tor_addr_to_str_dup(&ls2->u.ap.addr);
  200. tt_str_op(addr1, OP_EQ, addr2);
  201. tor_free(addr1);
  202. tor_free(addr2);
  203. tt_int_op(ls1->u.ap.port, ==, ls2->u.ap.port);
  204. }
  205. break;
  206. case LS_LEGACY_ID:
  207. tt_mem_op(ls1->u.legacy_id, OP_EQ, ls2->u.legacy_id,
  208. sizeof(ls1->u.legacy_id));
  209. break;
  210. default:
  211. /* Unknown type, caught it and print its value. */
  212. tt_int_op(ls1->type, OP_EQ, -1);
  213. }
  214. }
  215. }
  216. }
  217. done:
  218. tor_free(addr1);
  219. tor_free(addr2);
  220. }