hs_test_helpers.c 9.2 KB

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