hs_test_helpers.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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. 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. int ret;
  88. time_t now = time(NULL);
  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. ret = ed25519_keypair_generate(&blinded_kp, 0);
  96. tt_int_op(ret, ==, 0);
  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. /* Setup encrypted data section. */
  108. desc->encrypted_data.create2_ntor = 1;
  109. desc->encrypted_data.intro_auth_types = smartlist_new();
  110. desc->encrypted_data.single_onion_service = 1;
  111. smartlist_add(desc->encrypted_data.intro_auth_types, tor_strdup("ed25519"));
  112. desc->encrypted_data.intro_points = smartlist_new();
  113. if (!no_ip) {
  114. /* Add four intro points. */
  115. smartlist_add(desc->encrypted_data.intro_points,
  116. hs_helper_build_intro_point(signing_kp, now, "1.2.3.4", 0));
  117. smartlist_add(desc->encrypted_data.intro_points,
  118. hs_helper_build_intro_point(signing_kp, now, "[2600::1]", 0));
  119. smartlist_add(desc->encrypted_data.intro_points,
  120. hs_helper_build_intro_point(signing_kp, now, "3.2.1.4", 1));
  121. smartlist_add(desc->encrypted_data.intro_points,
  122. hs_helper_build_intro_point(signing_kp, now, "", 1));
  123. }
  124. descp = desc;
  125. done:
  126. return descp;
  127. }
  128. /* Build a descriptor with introduction points. */
  129. hs_descriptor_t *
  130. hs_helper_build_hs_desc_with_ip(const ed25519_keypair_t *signing_kp)
  131. {
  132. return hs_helper_build_hs_desc_impl(0, signing_kp);
  133. }
  134. /* Build a descriptor without any introduction points. */
  135. hs_descriptor_t *
  136. hs_helper_build_hs_desc_no_ip(const ed25519_keypair_t *signing_kp)
  137. {
  138. return hs_helper_build_hs_desc_impl(1, signing_kp);
  139. }
  140. void
  141. hs_helper_desc_equal(const hs_descriptor_t *desc1,
  142. const hs_descriptor_t *desc2)
  143. {
  144. char *addr1 = NULL, *addr2 = NULL;
  145. /* Plaintext data section. */
  146. tt_int_op(desc1->plaintext_data.version, OP_EQ,
  147. desc2->plaintext_data.version);
  148. tt_uint_op(desc1->plaintext_data.lifetime_sec, OP_EQ,
  149. desc2->plaintext_data.lifetime_sec);
  150. tt_assert(tor_cert_eq(desc1->plaintext_data.signing_key_cert,
  151. desc2->plaintext_data.signing_key_cert));
  152. tt_mem_op(desc1->plaintext_data.signing_pubkey.pubkey, OP_EQ,
  153. desc2->plaintext_data.signing_pubkey.pubkey,
  154. ED25519_PUBKEY_LEN);
  155. tt_mem_op(desc1->plaintext_data.blinded_pubkey.pubkey, OP_EQ,
  156. desc2->plaintext_data.blinded_pubkey.pubkey,
  157. ED25519_PUBKEY_LEN);
  158. tt_u64_op(desc1->plaintext_data.revision_counter, ==,
  159. desc2->plaintext_data.revision_counter);
  160. /* NOTE: We can't compare the encrypted blob because when encoding the
  161. * descriptor, the object is immutable thus we don't update it with the
  162. * encrypted blob. As contrast to the decoding process where we populate a
  163. * descriptor object. */
  164. /* Encrypted data section. */
  165. tt_uint_op(desc1->encrypted_data.create2_ntor, ==,
  166. desc2->encrypted_data.create2_ntor);
  167. /* Authentication type. */
  168. tt_int_op(!!desc1->encrypted_data.intro_auth_types, ==,
  169. !!desc2->encrypted_data.intro_auth_types);
  170. if (desc1->encrypted_data.intro_auth_types &&
  171. desc2->encrypted_data.intro_auth_types) {
  172. tt_int_op(smartlist_len(desc1->encrypted_data.intro_auth_types), ==,
  173. smartlist_len(desc2->encrypted_data.intro_auth_types));
  174. for (int i = 0;
  175. i < smartlist_len(desc1->encrypted_data.intro_auth_types);
  176. i++) {
  177. tt_str_op(smartlist_get(desc1->encrypted_data.intro_auth_types, i),OP_EQ,
  178. smartlist_get(desc2->encrypted_data.intro_auth_types, i));
  179. }
  180. }
  181. /* Introduction points. */
  182. {
  183. tt_assert(desc1->encrypted_data.intro_points);
  184. tt_assert(desc2->encrypted_data.intro_points);
  185. tt_int_op(smartlist_len(desc1->encrypted_data.intro_points), ==,
  186. smartlist_len(desc2->encrypted_data.intro_points));
  187. for (int i=0; i < smartlist_len(desc1->encrypted_data.intro_points); i++) {
  188. hs_desc_intro_point_t *ip1 = smartlist_get(desc1->encrypted_data
  189. .intro_points, i),
  190. *ip2 = smartlist_get(desc2->encrypted_data
  191. .intro_points, i);
  192. tt_assert(tor_cert_eq(ip1->auth_key_cert, ip2->auth_key_cert));
  193. if (ip1->legacy.key) {
  194. tt_int_op(crypto_pk_cmp_keys(ip1->legacy.key, ip2->legacy.key),
  195. OP_EQ, 0);
  196. } else {
  197. tt_mem_op(&ip1->enc_key, OP_EQ, &ip2->enc_key, CURVE25519_PUBKEY_LEN);
  198. }
  199. tt_int_op(smartlist_len(ip1->link_specifiers), ==,
  200. smartlist_len(ip2->link_specifiers));
  201. for (int j = 0; j < smartlist_len(ip1->link_specifiers); j++) {
  202. hs_desc_link_specifier_t *ls1 = smartlist_get(ip1->link_specifiers, j),
  203. *ls2 = smartlist_get(ip2->link_specifiers, j);
  204. tt_int_op(ls1->type, ==, ls2->type);
  205. switch (ls1->type) {
  206. case LS_IPV4:
  207. case LS_IPV6:
  208. {
  209. addr1 = tor_addr_to_str_dup(&ls1->u.ap.addr);
  210. addr2 = tor_addr_to_str_dup(&ls2->u.ap.addr);
  211. tt_str_op(addr1, OP_EQ, addr2);
  212. tor_free(addr1);
  213. tor_free(addr2);
  214. tt_int_op(ls1->u.ap.port, ==, ls2->u.ap.port);
  215. }
  216. break;
  217. case LS_LEGACY_ID:
  218. tt_mem_op(ls1->u.legacy_id, OP_EQ, ls2->u.legacy_id,
  219. sizeof(ls1->u.legacy_id));
  220. break;
  221. default:
  222. /* Unknown type, caught it and print its value. */
  223. tt_int_op(ls1->type, OP_EQ, -1);
  224. }
  225. }
  226. }
  227. }
  228. done:
  229. tor_free(addr1);
  230. tor_free(addr2);
  231. }