hs_test_helpers.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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->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. if (intro_point == NULL)
  80. tor_free(ip);
  81. return intro_point;
  82. }
  83. /* Return a valid hs_descriptor_t object. If no_ip is set, no introduction
  84. * points are added. */
  85. static hs_descriptor_t *
  86. hs_helper_build_hs_desc_impl(unsigned int no_ip,
  87. const ed25519_keypair_t *signing_kp)
  88. {
  89. int ret;
  90. time_t now = time(NULL);
  91. ed25519_keypair_t blinded_kp;
  92. hs_descriptor_t *descp = NULL, *desc = tor_malloc_zero(sizeof(*desc));
  93. desc->plaintext_data.version = HS_DESC_SUPPORTED_FORMAT_VERSION_MAX;
  94. /* Copy only the public key into the descriptor. */
  95. memcpy(&desc->plaintext_data.signing_pubkey, &signing_kp->pubkey,
  96. sizeof(ed25519_public_key_t));
  97. ret = ed25519_keypair_generate(&blinded_kp, 0);
  98. tt_int_op(ret, ==, 0);
  99. /* Copy only the public key into the descriptor. */
  100. memcpy(&desc->plaintext_data.blinded_pubkey, &blinded_kp.pubkey,
  101. sizeof(ed25519_public_key_t));
  102. desc->plaintext_data.signing_key_cert =
  103. tor_cert_create(&blinded_kp, CERT_TYPE_SIGNING_HS_DESC,
  104. &signing_kp->pubkey, now, 3600,
  105. CERT_FLAG_INCLUDE_SIGNING_KEY);
  106. tt_assert(desc->plaintext_data.signing_key_cert);
  107. desc->plaintext_data.revision_counter = 42;
  108. desc->plaintext_data.lifetime_sec = 3 * 60 * 60;
  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. /* Build a descriptor with introduction points. */
  131. hs_descriptor_t *
  132. hs_helper_build_hs_desc_with_ip(const ed25519_keypair_t *signing_kp)
  133. {
  134. return hs_helper_build_hs_desc_impl(0, signing_kp);
  135. }
  136. /* Build a descriptor without any introduction points. */
  137. hs_descriptor_t *
  138. hs_helper_build_hs_desc_no_ip(const ed25519_keypair_t *signing_kp)
  139. {
  140. return hs_helper_build_hs_desc_impl(1, signing_kp);
  141. }
  142. void
  143. hs_helper_desc_equal(const hs_descriptor_t *desc1,
  144. const hs_descriptor_t *desc2)
  145. {
  146. char *addr1 = NULL, *addr2 = NULL;
  147. /* Plaintext data section. */
  148. tt_int_op(desc1->plaintext_data.version, OP_EQ,
  149. desc2->plaintext_data.version);
  150. tt_uint_op(desc1->plaintext_data.lifetime_sec, OP_EQ,
  151. desc2->plaintext_data.lifetime_sec);
  152. tt_assert(tor_cert_eq(desc1->plaintext_data.signing_key_cert,
  153. desc2->plaintext_data.signing_key_cert));
  154. tt_mem_op(desc1->plaintext_data.signing_pubkey.pubkey, OP_EQ,
  155. desc2->plaintext_data.signing_pubkey.pubkey,
  156. ED25519_PUBKEY_LEN);
  157. tt_mem_op(desc1->plaintext_data.blinded_pubkey.pubkey, OP_EQ,
  158. desc2->plaintext_data.blinded_pubkey.pubkey,
  159. ED25519_PUBKEY_LEN);
  160. tt_u64_op(desc1->plaintext_data.revision_counter, ==,
  161. desc2->plaintext_data.revision_counter);
  162. /* NOTE: We can't compare the encrypted blob because when encoding the
  163. * descriptor, the object is immutable thus we don't update it with the
  164. * encrypted blob. As contrast to the decoding process where we populate a
  165. * descriptor object. */
  166. /* Encrypted data section. */
  167. tt_uint_op(desc1->encrypted_data.create2_ntor, ==,
  168. desc2->encrypted_data.create2_ntor);
  169. /* Authentication type. */
  170. tt_int_op(!!desc1->encrypted_data.intro_auth_types, ==,
  171. !!desc2->encrypted_data.intro_auth_types);
  172. if (desc1->encrypted_data.intro_auth_types &&
  173. desc2->encrypted_data.intro_auth_types) {
  174. tt_int_op(smartlist_len(desc1->encrypted_data.intro_auth_types), ==,
  175. smartlist_len(desc2->encrypted_data.intro_auth_types));
  176. for (int i = 0;
  177. i < smartlist_len(desc1->encrypted_data.intro_auth_types);
  178. i++) {
  179. tt_str_op(smartlist_get(desc1->encrypted_data.intro_auth_types, i),OP_EQ,
  180. smartlist_get(desc2->encrypted_data.intro_auth_types, i));
  181. }
  182. }
  183. /* Introduction points. */
  184. {
  185. tt_assert(desc1->encrypted_data.intro_points);
  186. tt_assert(desc2->encrypted_data.intro_points);
  187. tt_int_op(smartlist_len(desc1->encrypted_data.intro_points), ==,
  188. smartlist_len(desc2->encrypted_data.intro_points));
  189. for (int i=0; i < smartlist_len(desc1->encrypted_data.intro_points); i++) {
  190. hs_desc_intro_point_t *ip1 = smartlist_get(desc1->encrypted_data
  191. .intro_points, i),
  192. *ip2 = smartlist_get(desc2->encrypted_data
  193. .intro_points, i);
  194. tt_assert(tor_cert_eq(ip1->auth_key_cert, ip2->auth_key_cert));
  195. if (ip1->legacy.key) {
  196. tt_int_op(crypto_pk_cmp_keys(ip1->legacy.key, ip2->legacy.key),
  197. OP_EQ, 0);
  198. } else {
  199. tt_mem_op(&ip1->enc_key, OP_EQ, &ip2->enc_key, CURVE25519_PUBKEY_LEN);
  200. }
  201. tt_int_op(smartlist_len(ip1->link_specifiers), ==,
  202. smartlist_len(ip2->link_specifiers));
  203. for (int j = 0; j < smartlist_len(ip1->link_specifiers); j++) {
  204. hs_desc_link_specifier_t *ls1 = smartlist_get(ip1->link_specifiers, j),
  205. *ls2 = smartlist_get(ip2->link_specifiers, j);
  206. tt_int_op(ls1->type, ==, ls2->type);
  207. switch (ls1->type) {
  208. case LS_IPV4:
  209. case LS_IPV6:
  210. {
  211. addr1 = tor_addr_to_str_dup(&ls1->u.ap.addr);
  212. addr2 = tor_addr_to_str_dup(&ls2->u.ap.addr);
  213. tt_str_op(addr1, OP_EQ, addr2);
  214. tor_free(addr1);
  215. tor_free(addr2);
  216. tt_int_op(ls1->u.ap.port, ==, ls2->u.ap.port);
  217. }
  218. break;
  219. case LS_LEGACY_ID:
  220. tt_mem_op(ls1->u.legacy_id, OP_EQ, ls2->u.legacy_id,
  221. sizeof(ls1->u.legacy_id));
  222. break;
  223. default:
  224. /* Unknown type, caught it and print its value. */
  225. tt_int_op(ls1->type, OP_EQ, -1);
  226. }
  227. }
  228. }
  229. }
  230. done:
  231. tor_free(addr1);
  232. tor_free(addr2);
  233. }