test_hs_descriptor.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970
  1. /* Copyright (c) 2016-2018, The Tor Project, Inc. */
  2. /* See LICENSE for licensing information */
  3. /**
  4. * \file test_hs_descriptor.c
  5. * \brief Test hidden service descriptor encoding and decoding.
  6. */
  7. #define HS_DESCRIPTOR_PRIVATE
  8. #include "lib/crypt_ops/crypto_ed25519.h"
  9. #include "lib/crypt_ops/crypto_format.h"
  10. #include "lib/crypt_ops/crypto_digest.h"
  11. #include "lib/crypt_ops/crypto_rand.h"
  12. #include "trunnel/ed25519_cert.h"
  13. #include "core/or/or.h"
  14. #include "feature/hs/hs_descriptor.h"
  15. #include "test/test.h"
  16. #include "feature/nodelist/torcert.h"
  17. #include "test/hs_test_helpers.h"
  18. #include "test/test_helpers.h"
  19. #include "test/log_test_helpers.h"
  20. #ifdef HAVE_CFLAG_WOVERLENGTH_STRINGS
  21. DISABLE_GCC_WARNING(overlength-strings)
  22. /* We allow huge string constants in the unit tests, but not in the code
  23. * at large. */
  24. #endif
  25. #include "test_hs_descriptor.inc"
  26. ENABLE_GCC_WARNING(overlength-strings)
  27. /* Mock function to fill all bytes with 1 */
  28. static void
  29. mock_crypto_strongest_rand(uint8_t *out, size_t out_len)
  30. {
  31. memset(out, 1, out_len);
  32. }
  33. /* Test certificate encoding put in a descriptor. */
  34. static void
  35. test_cert_encoding(void *arg)
  36. {
  37. int ret;
  38. char *encoded = NULL;
  39. time_t now = time(NULL);
  40. ed25519_keypair_t kp;
  41. ed25519_public_key_t signed_key;
  42. ed25519_secret_key_t secret_key;
  43. tor_cert_t *cert = NULL;
  44. (void) arg;
  45. ret = ed25519_keypair_generate(&kp, 0);
  46. tt_int_op(ret, == , 0);
  47. ret = ed25519_secret_key_generate(&secret_key, 0);
  48. tt_int_op(ret, == , 0);
  49. ret = ed25519_public_key_generate(&signed_key, &secret_key);
  50. tt_int_op(ret, == , 0);
  51. cert = tor_cert_create(&kp, CERT_TYPE_SIGNING_AUTH, &signed_key,
  52. now, 3600 * 2, CERT_FLAG_INCLUDE_SIGNING_KEY);
  53. tt_assert(cert);
  54. /* Test the certificate encoding function. */
  55. ret = tor_cert_encode_ed22519(cert, &encoded);
  56. tt_int_op(ret, OP_EQ, 0);
  57. /* Validated the certificate string. */
  58. {
  59. char *end, *pos = encoded;
  60. char *b64_cert, buf[256];
  61. size_t b64_cert_len;
  62. tor_cert_t *parsed_cert;
  63. tt_int_op(strcmpstart(pos, "-----BEGIN ED25519 CERT-----\n"), OP_EQ, 0);
  64. pos += strlen("-----BEGIN ED25519 CERT-----\n");
  65. /* Isolate the base64 encoded certificate and try to decode it. */
  66. end = strstr(pos, "-----END ED25519 CERT-----");
  67. tt_assert(end);
  68. b64_cert = pos;
  69. b64_cert_len = end - pos;
  70. ret = base64_decode(buf, sizeof(buf), b64_cert, b64_cert_len);
  71. tt_int_op(ret, OP_GT, 0);
  72. /* Parseable? */
  73. parsed_cert = tor_cert_parse((uint8_t *) buf, ret);
  74. tt_assert(parsed_cert);
  75. /* Signature is valid? */
  76. ret = tor_cert_checksig(parsed_cert, &kp.pubkey, now + 10);
  77. tt_int_op(ret, OP_EQ, 0);
  78. ret = tor_cert_eq(cert, parsed_cert);
  79. tt_int_op(ret, OP_EQ, 1);
  80. /* The cert did have the signing key? */
  81. ret= ed25519_pubkey_eq(&parsed_cert->signing_key, &kp.pubkey);
  82. tt_int_op(ret, OP_EQ, 1);
  83. tor_cert_free(parsed_cert);
  84. /* Get to the end part of the certificate. */
  85. pos += b64_cert_len;
  86. tt_int_op(strcmpstart(pos, "-----END ED25519 CERT-----"), OP_EQ, 0);
  87. pos += strlen("-----END ED25519 CERT-----");
  88. tt_str_op(pos, OP_EQ, "");
  89. }
  90. done:
  91. tor_cert_free(cert);
  92. tor_free(encoded);
  93. }
  94. /* Test the descriptor padding. */
  95. static void
  96. test_descriptor_padding(void *arg)
  97. {
  98. char *plaintext;
  99. size_t plaintext_len, padded_len;
  100. uint8_t *padded_plaintext = NULL;
  101. /* Example: if l = 129, the ceiled division gives 2 and then multiplied by 128
  102. * to give 256. With l = 127, ceiled division gives 1 then times 128. */
  103. #define PADDING_EXPECTED_LEN(l) \
  104. CEIL_DIV(l, HS_DESC_SUPERENC_PLAINTEXT_PAD_MULTIPLE) * \
  105. HS_DESC_SUPERENC_PLAINTEXT_PAD_MULTIPLE
  106. (void) arg;
  107. { /* test #1: no padding */
  108. plaintext_len = HS_DESC_SUPERENC_PLAINTEXT_PAD_MULTIPLE;
  109. plaintext = tor_malloc(plaintext_len);
  110. padded_len = build_plaintext_padding(plaintext, plaintext_len,
  111. &padded_plaintext);
  112. tt_assert(padded_plaintext);
  113. tor_free(plaintext);
  114. /* Make sure our padding has been zeroed. */
  115. tt_int_op(tor_mem_is_zero((char *) padded_plaintext + plaintext_len,
  116. padded_len - plaintext_len), OP_EQ, 1);
  117. tor_free(padded_plaintext);
  118. /* Never never have a padded length smaller than the plaintext. */
  119. tt_int_op(padded_len, OP_GE, plaintext_len);
  120. tt_int_op(padded_len, OP_EQ, PADDING_EXPECTED_LEN(plaintext_len));
  121. }
  122. { /* test #2: one byte padding? */
  123. plaintext_len = HS_DESC_SUPERENC_PLAINTEXT_PAD_MULTIPLE - 1;
  124. plaintext = tor_malloc(plaintext_len);
  125. padded_plaintext = NULL;
  126. padded_len = build_plaintext_padding(plaintext, plaintext_len,
  127. &padded_plaintext);
  128. tt_assert(padded_plaintext);
  129. tor_free(plaintext);
  130. /* Make sure our padding has been zeroed. */
  131. tt_int_op(tor_mem_is_zero((char *) padded_plaintext + plaintext_len,
  132. padded_len - plaintext_len), OP_EQ, 1);
  133. tor_free(padded_plaintext);
  134. /* Never never have a padded length smaller than the plaintext. */
  135. tt_int_op(padded_len, OP_GE, plaintext_len);
  136. tt_int_op(padded_len, OP_EQ, PADDING_EXPECTED_LEN(plaintext_len));
  137. }
  138. { /* test #3: Lots more bytes of padding? */
  139. plaintext_len = HS_DESC_SUPERENC_PLAINTEXT_PAD_MULTIPLE + 1;
  140. plaintext = tor_malloc(plaintext_len);
  141. padded_plaintext = NULL;
  142. padded_len = build_plaintext_padding(plaintext, plaintext_len,
  143. &padded_plaintext);
  144. tt_assert(padded_plaintext);
  145. tor_free(plaintext);
  146. /* Make sure our padding has been zeroed. */
  147. tt_int_op(tor_mem_is_zero((char *) padded_plaintext + plaintext_len,
  148. padded_len - plaintext_len), OP_EQ, 1);
  149. tor_free(padded_plaintext);
  150. /* Never never have a padded length smaller than the plaintext. */
  151. tt_int_op(padded_len, OP_GE, plaintext_len);
  152. tt_int_op(padded_len, OP_EQ, PADDING_EXPECTED_LEN(plaintext_len));
  153. }
  154. done:
  155. return;
  156. }
  157. static void
  158. test_link_specifier(void *arg)
  159. {
  160. ssize_t ret;
  161. hs_desc_link_specifier_t spec;
  162. smartlist_t *link_specifiers = smartlist_new();
  163. char buf[256];
  164. char *b64 = NULL;
  165. link_specifier_t *ls = NULL;
  166. (void) arg;
  167. /* Always this port. */
  168. spec.u.ap.port = 42;
  169. smartlist_add(link_specifiers, &spec);
  170. /* Test IPv4 for starter. */
  171. {
  172. uint32_t ipv4;
  173. spec.type = LS_IPV4;
  174. ret = tor_addr_parse(&spec.u.ap.addr, "1.2.3.4");
  175. tt_int_op(ret, OP_EQ, AF_INET);
  176. b64 = encode_link_specifiers(link_specifiers);
  177. tt_assert(b64);
  178. /* Decode it and validate the format. */
  179. ret = base64_decode(buf, sizeof(buf), b64, strlen(b64));
  180. tt_int_op(ret, OP_GT, 0);
  181. /* First byte is the number of link specifier. */
  182. tt_int_op(get_uint8(buf), OP_EQ, 1);
  183. ret = link_specifier_parse(&ls, (uint8_t *) buf + 1, ret - 1);
  184. tt_int_op(ret, OP_EQ, 8);
  185. /* Should be 2 bytes for port and 4 bytes for IPv4. */
  186. tt_int_op(link_specifier_get_ls_len(ls), OP_EQ, 6);
  187. ipv4 = link_specifier_get_un_ipv4_addr(ls);
  188. tt_int_op(tor_addr_to_ipv4h(&spec.u.ap.addr), OP_EQ, ipv4);
  189. tt_int_op(link_specifier_get_un_ipv4_port(ls), OP_EQ, spec.u.ap.port);
  190. link_specifier_free(ls);
  191. ls = NULL;
  192. tor_free(b64);
  193. }
  194. /* Test IPv6. */
  195. {
  196. uint8_t ipv6[16];
  197. spec.type = LS_IPV6;
  198. ret = tor_addr_parse(&spec.u.ap.addr, "[1:2:3:4::]");
  199. tt_int_op(ret, OP_EQ, AF_INET6);
  200. b64 = encode_link_specifiers(link_specifiers);
  201. tt_assert(b64);
  202. /* Decode it and validate the format. */
  203. ret = base64_decode(buf, sizeof(buf), b64, strlen(b64));
  204. tt_int_op(ret, OP_GT, 0);
  205. /* First byte is the number of link specifier. */
  206. tt_int_op(get_uint8(buf), OP_EQ, 1);
  207. ret = link_specifier_parse(&ls, (uint8_t *) buf + 1, ret - 1);
  208. tt_int_op(ret, OP_EQ, 20);
  209. /* Should be 2 bytes for port and 16 bytes for IPv6. */
  210. tt_int_op(link_specifier_get_ls_len(ls), OP_EQ, 18);
  211. for (unsigned int i = 0; i < sizeof(ipv6); i++) {
  212. ipv6[i] = link_specifier_get_un_ipv6_addr(ls, i);
  213. }
  214. tt_mem_op(tor_addr_to_in6_addr8(&spec.u.ap.addr), OP_EQ, ipv6,
  215. sizeof(ipv6));
  216. tt_int_op(link_specifier_get_un_ipv6_port(ls), OP_EQ, spec.u.ap.port);
  217. link_specifier_free(ls);
  218. ls = NULL;
  219. tor_free(b64);
  220. }
  221. /* Test legacy. */
  222. {
  223. uint8_t *id;
  224. spec.type = LS_LEGACY_ID;
  225. memset(spec.u.legacy_id, 'Y', sizeof(spec.u.legacy_id));
  226. b64 = encode_link_specifiers(link_specifiers);
  227. tt_assert(b64);
  228. /* Decode it and validate the format. */
  229. ret = base64_decode(buf, sizeof(buf), b64, strlen(b64));
  230. tt_int_op(ret, OP_GT, 0);
  231. /* First byte is the number of link specifier. */
  232. tt_int_op(get_uint8(buf), OP_EQ, 1);
  233. ret = link_specifier_parse(&ls, (uint8_t *) buf + 1, ret - 1);
  234. /* 20 bytes digest + 1 byte type + 1 byte len. */
  235. tt_int_op(ret, OP_EQ, 22);
  236. tt_int_op(link_specifier_getlen_un_legacy_id(ls), OP_EQ, DIGEST_LEN);
  237. /* Digest length is 20 bytes. */
  238. tt_int_op(link_specifier_get_ls_len(ls), OP_EQ, DIGEST_LEN);
  239. id = link_specifier_getarray_un_legacy_id(ls);
  240. tt_mem_op(spec.u.legacy_id, OP_EQ, id, DIGEST_LEN);
  241. link_specifier_free(ls);
  242. ls = NULL;
  243. tor_free(b64);
  244. }
  245. done:
  246. link_specifier_free(ls);
  247. tor_free(b64);
  248. smartlist_free(link_specifiers);
  249. }
  250. static void
  251. test_encode_descriptor(void *arg)
  252. {
  253. int ret;
  254. char *encoded = NULL;
  255. ed25519_keypair_t signing_kp;
  256. hs_descriptor_t *desc = NULL;
  257. (void) arg;
  258. ret = ed25519_keypair_generate(&signing_kp, 0);
  259. tt_int_op(ret, OP_EQ, 0);
  260. desc = hs_helper_build_hs_desc_with_ip(&signing_kp);
  261. ret = hs_desc_encode_descriptor(desc, &signing_kp, NULL, &encoded);
  262. tt_int_op(ret, OP_EQ, 0);
  263. tt_assert(encoded);
  264. done:
  265. hs_descriptor_free(desc);
  266. tor_free(encoded);
  267. }
  268. static void
  269. test_decode_descriptor(void *arg)
  270. {
  271. int ret;
  272. char *encoded = NULL;
  273. ed25519_keypair_t signing_kp;
  274. hs_descriptor_t *desc = NULL;
  275. hs_descriptor_t *decoded = NULL;
  276. hs_descriptor_t *desc_no_ip = NULL;
  277. uint8_t subcredential[DIGEST256_LEN];
  278. (void) arg;
  279. ret = ed25519_keypair_generate(&signing_kp, 0);
  280. tt_int_op(ret, OP_EQ, 0);
  281. desc = hs_helper_build_hs_desc_with_ip(&signing_kp);
  282. hs_helper_get_subcred_from_identity_keypair(&signing_kp,
  283. subcredential);
  284. /* Give some bad stuff to the decoding function. */
  285. ret = hs_desc_decode_descriptor("hladfjlkjadf", subcredential, &decoded);
  286. tt_int_op(ret, OP_EQ, -1);
  287. ret = hs_desc_encode_descriptor(desc, &signing_kp, NULL, &encoded);
  288. tt_int_op(ret, OP_EQ, 0);
  289. tt_assert(encoded);
  290. ret = hs_desc_decode_descriptor(encoded, subcredential, &decoded);
  291. tt_int_op(ret, OP_EQ, 0);
  292. tt_assert(decoded);
  293. hs_helper_desc_equal(desc, decoded);
  294. /* Decode a descriptor with _no_ introduction points. */
  295. {
  296. ed25519_keypair_t signing_kp_no_ip;
  297. ret = ed25519_keypair_generate(&signing_kp_no_ip, 0);
  298. tt_int_op(ret, OP_EQ, 0);
  299. hs_helper_get_subcred_from_identity_keypair(&signing_kp_no_ip,
  300. subcredential);
  301. desc_no_ip = hs_helper_build_hs_desc_no_ip(&signing_kp_no_ip);
  302. tt_assert(desc_no_ip);
  303. tor_free(encoded);
  304. ret = hs_desc_encode_descriptor(desc_no_ip, &signing_kp_no_ip,
  305. NULL, &encoded);
  306. tt_int_op(ret, OP_EQ, 0);
  307. tt_assert(encoded);
  308. hs_descriptor_free(decoded);
  309. ret = hs_desc_decode_descriptor(encoded, subcredential, &decoded);
  310. tt_int_op(ret, OP_EQ, 0);
  311. tt_assert(decoded);
  312. }
  313. done:
  314. hs_descriptor_free(desc);
  315. hs_descriptor_free(desc_no_ip);
  316. hs_descriptor_free(decoded);
  317. tor_free(encoded);
  318. }
  319. static void
  320. test_supported_version(void *arg)
  321. {
  322. int ret;
  323. (void) arg;
  324. /* Unsupported. */
  325. ret = hs_desc_is_supported_version(42);
  326. tt_int_op(ret, OP_EQ, 0);
  327. /* To early. */
  328. ret = hs_desc_is_supported_version(HS_DESC_SUPPORTED_FORMAT_VERSION_MIN - 1);
  329. tt_int_op(ret, OP_EQ, 0);
  330. /* One too new. */
  331. ret = hs_desc_is_supported_version(HS_DESC_SUPPORTED_FORMAT_VERSION_MAX + 1);
  332. tt_int_op(ret, OP_EQ, 0);
  333. /* Valid version. */
  334. ret = hs_desc_is_supported_version(3);
  335. tt_int_op(ret, OP_EQ, 1);
  336. done:
  337. ;
  338. }
  339. static void
  340. test_encrypted_data_len(void *arg)
  341. {
  342. int ret;
  343. size_t value;
  344. (void) arg;
  345. /* No length, error. */
  346. ret = encrypted_data_length_is_valid(0);
  347. tt_int_op(ret, OP_EQ, 0);
  348. /* Valid value. */
  349. value = HS_DESC_ENCRYPTED_SALT_LEN + DIGEST256_LEN + 1;
  350. ret = encrypted_data_length_is_valid(value);
  351. tt_int_op(ret, OP_EQ, 1);
  352. done:
  353. ;
  354. }
  355. static void
  356. test_decode_invalid_intro_point(void *arg)
  357. {
  358. int ret;
  359. char *encoded_ip = NULL;
  360. size_t len_out;
  361. hs_desc_intro_point_t *ip = NULL;
  362. ed25519_keypair_t signing_kp;
  363. hs_descriptor_t *desc = NULL;
  364. (void) arg;
  365. /* Separate pieces of a valid encoded introduction point. */
  366. const char *intro_point =
  367. "introduction-point AQIUMDI5OUYyNjhGQ0E5RDU1Q0QxNTc=";
  368. const char *auth_key =
  369. "auth-key\n"
  370. "-----BEGIN ED25519 CERT-----\n"
  371. "AQkACOhAAQW8ltYZMIWpyrfyE/b4Iyi8CNybCwYs6ADk7XfBaxsFAQAgBAD3/BE4\n"
  372. "XojGE/N2bW/wgnS9r2qlrkydGyuCKIGayYx3haZ39LD4ZTmSMRxwmplMAqzG/XNP\n"
  373. "0Kkpg4p2/VnLFJRdU1SMFo1lgQ4P0bqw7Tgx200fulZ4KUM5z5V7m+a/mgY=\n"
  374. "-----END ED25519 CERT-----";
  375. const char *enc_key =
  376. "enc-key ntor bpZKLsuhxP6woDQ3yVyjm5gUKSk7RjfAijT2qrzbQk0=";
  377. const char *enc_key_cert =
  378. "enc-key-cert\n"
  379. "-----BEGIN ED25519 CERT-----\n"
  380. "AQsACOhZAUpNvCZ1aJaaR49lS6MCdsVkhVGVrRqoj0Y2T4SzroAtAQAgBABFOcGg\n"
  381. "lbTt1DF5nKTE/gU3Fr8ZtlCIOhu1A+F5LM7fqCUupfesg0KTHwyIZOYQbJuM5/he\n"
  382. "/jDNyLy9woPJdjkxywaY2RPUxGjLYtMQV0E8PUxWyICV+7y52fTCYaKpYQw=\n"
  383. "-----END ED25519 CERT-----";
  384. /* Try to decode a junk string. */
  385. {
  386. hs_descriptor_free(desc);
  387. desc = NULL;
  388. ret = ed25519_keypair_generate(&signing_kp, 0);
  389. tt_int_op(ret, OP_EQ, 0);
  390. desc = hs_helper_build_hs_desc_with_ip(&signing_kp);
  391. const char *junk = "this is not a descriptor";
  392. ip = decode_introduction_point(desc, junk);
  393. tt_ptr_op(ip, OP_EQ, NULL);
  394. hs_desc_intro_point_free(ip);
  395. ip = NULL;
  396. }
  397. /* Invalid link specifiers. */
  398. {
  399. smartlist_t *lines = smartlist_new();
  400. const char *bad_line = "introduction-point blah";
  401. smartlist_add(lines, (char *) bad_line);
  402. smartlist_add(lines, (char *) auth_key);
  403. smartlist_add(lines, (char *) enc_key);
  404. smartlist_add(lines, (char *) enc_key_cert);
  405. encoded_ip = smartlist_join_strings(lines, "\n", 0, &len_out);
  406. tt_assert(encoded_ip);
  407. ip = decode_introduction_point(desc, encoded_ip);
  408. tt_ptr_op(ip, OP_EQ, NULL);
  409. tor_free(encoded_ip);
  410. smartlist_free(lines);
  411. hs_desc_intro_point_free(ip);
  412. ip = NULL;
  413. }
  414. /* Invalid auth key type. */
  415. {
  416. smartlist_t *lines = smartlist_new();
  417. /* Try to put a valid object that our tokenize function will be able to
  418. * parse but that has nothing to do with the auth_key. */
  419. const char *bad_line =
  420. "auth-key\n"
  421. "-----BEGIN UNICORN CERT-----\n"
  422. "MIGJAoGBAO4bATcW8kW4h6RQQAKEgg+aXCpF4JwbcO6vGZtzXTDB+HdPVQzwqkbh\n"
  423. "XzFM6VGArhYw4m31wcP1Z7IwULir7UMnAFd7Zi62aYfU6l+Y1yAoZ1wzu1XBaAMK\n"
  424. "ejpwQinW9nzJn7c2f69fVke3pkhxpNdUZ+vplSA/l9iY+y+v+415AgMBAAE=\n"
  425. "-----END UNICORN CERT-----";
  426. /* Build intro point text. */
  427. smartlist_add(lines, (char *) intro_point);
  428. smartlist_add(lines, (char *) bad_line);
  429. smartlist_add(lines, (char *) enc_key);
  430. smartlist_add(lines, (char *) enc_key_cert);
  431. encoded_ip = smartlist_join_strings(lines, "\n", 0, &len_out);
  432. tt_assert(encoded_ip);
  433. ip = decode_introduction_point(desc, encoded_ip);
  434. tt_ptr_op(ip, OP_EQ, NULL);
  435. tor_free(encoded_ip);
  436. smartlist_free(lines);
  437. }
  438. /* Invalid enc-key. */
  439. {
  440. smartlist_t *lines = smartlist_new();
  441. const char *bad_line =
  442. "enc-key unicorn bpZKLsuhxP6woDQ3yVyjm5gUKSk7RjfAijT2qrzbQk0=";
  443. /* Build intro point text. */
  444. smartlist_add(lines, (char *) intro_point);
  445. smartlist_add(lines, (char *) auth_key);
  446. smartlist_add(lines, (char *) bad_line);
  447. smartlist_add(lines, (char *) enc_key_cert);
  448. encoded_ip = smartlist_join_strings(lines, "\n", 0, &len_out);
  449. tt_assert(encoded_ip);
  450. ip = decode_introduction_point(desc, encoded_ip);
  451. tt_ptr_op(ip, OP_EQ, NULL);
  452. tor_free(encoded_ip);
  453. smartlist_free(lines);
  454. }
  455. /* Invalid enc-key object. */
  456. {
  457. smartlist_t *lines = smartlist_new();
  458. const char *bad_line = "enc-key ntor";
  459. /* Build intro point text. */
  460. smartlist_add(lines, (char *) intro_point);
  461. smartlist_add(lines, (char *) auth_key);
  462. smartlist_add(lines, (char *) bad_line);
  463. smartlist_add(lines, (char *) enc_key_cert);
  464. encoded_ip = smartlist_join_strings(lines, "\n", 0, &len_out);
  465. tt_assert(encoded_ip);
  466. ip = decode_introduction_point(desc, encoded_ip);
  467. tt_ptr_op(ip, OP_EQ, NULL);
  468. tor_free(encoded_ip);
  469. smartlist_free(lines);
  470. }
  471. /* Invalid enc-key base64 curv25519 key. */
  472. {
  473. smartlist_t *lines = smartlist_new();
  474. const char *bad_line = "enc-key ntor blah===";
  475. /* Build intro point text. */
  476. smartlist_add(lines, (char *) intro_point);
  477. smartlist_add(lines, (char *) auth_key);
  478. smartlist_add(lines, (char *) bad_line);
  479. smartlist_add(lines, (char *) enc_key_cert);
  480. encoded_ip = smartlist_join_strings(lines, "\n", 0, &len_out);
  481. tt_assert(encoded_ip);
  482. ip = decode_introduction_point(desc, encoded_ip);
  483. tt_ptr_op(ip, OP_EQ, NULL);
  484. tor_free(encoded_ip);
  485. smartlist_free(lines);
  486. }
  487. /* Invalid enc-key invalid legacy. */
  488. {
  489. smartlist_t *lines = smartlist_new();
  490. const char *bad_line = "legacy-key blah===";
  491. /* Build intro point text. */
  492. smartlist_add(lines, (char *) intro_point);
  493. smartlist_add(lines, (char *) auth_key);
  494. smartlist_add(lines, (char *) bad_line);
  495. smartlist_add(lines, (char *) enc_key_cert);
  496. encoded_ip = smartlist_join_strings(lines, "\n", 0, &len_out);
  497. tt_assert(encoded_ip);
  498. ip = decode_introduction_point(desc, encoded_ip);
  499. tt_ptr_op(ip, OP_EQ, NULL);
  500. tor_free(encoded_ip);
  501. smartlist_free(lines);
  502. }
  503. done:
  504. hs_descriptor_free(desc);
  505. hs_desc_intro_point_free(ip);
  506. }
  507. /** Make sure we fail gracefully when decoding the bad desc from #23233. */
  508. static void
  509. test_decode_bad_signature(void *arg)
  510. {
  511. hs_desc_plaintext_data_t desc_plaintext;
  512. int ret;
  513. (void) arg;
  514. /* Update approx time to dodge cert expiration */
  515. update_approx_time(1502661599);
  516. setup_full_capture_of_logs(LOG_WARN);
  517. ret = hs_desc_decode_plaintext(HS_DESC_BAD_SIG, &desc_plaintext);
  518. tt_int_op(ret, OP_EQ, -1);
  519. expect_log_msg_containing("Malformed signature line. Rejecting.");
  520. teardown_capture_of_logs();
  521. done:
  522. desc_plaintext_data_free_contents(&desc_plaintext);
  523. }
  524. static void
  525. test_decode_plaintext(void *arg)
  526. {
  527. int ret;
  528. hs_desc_plaintext_data_t desc_plaintext;
  529. const char *bad_value = "unicorn";
  530. (void) arg;
  531. #define template \
  532. "hs-descriptor %s\n" \
  533. "descriptor-lifetime %s\n" \
  534. "descriptor-signing-key-cert\n" \
  535. "-----BEGIN ED25519 CERT-----\n" \
  536. "AQgABjvPAQaG3g+dc6oV/oJV4ODAtkvx56uBnPtBT9mYVuHVOhn7AQAgBABUg3mQ\n" \
  537. "myBr4bu5LCr53wUEbW2EXui01CbUgU7pfo9LvJG3AcXRojj6HlfsUs9BkzYzYdjF\n" \
  538. "A69Apikgu0ewHYkFFASt7Il+gB3w6J8YstQJZT7dtbtl+doM7ug8B68Qdg8=\n" \
  539. "-----END ED25519 CERT-----\n" \
  540. "revision-counter %s\n" \
  541. "encrypted\n" \
  542. "-----BEGIN %s-----\n" \
  543. "UNICORN\n" \
  544. "-----END MESSAGE-----\n" \
  545. "signature m20WJH5agqvwhq7QeuEZ1mYyPWQDO+eJOZUjLhAiKu8DbL17DsDfJE6kXbWy" \
  546. "HimbNj2we0enV3cCOOAsmPOaAw\n"
  547. /* Invalid version. */
  548. {
  549. char *plaintext;
  550. tor_asprintf(&plaintext, template, bad_value, "180", "42", "MESSAGE");
  551. ret = hs_desc_decode_plaintext(plaintext, &desc_plaintext);
  552. tor_free(plaintext);
  553. tt_int_op(ret, OP_EQ, -1);
  554. }
  555. /* Missing fields. */
  556. {
  557. const char *plaintext = "hs-descriptor 3\n";
  558. ret = hs_desc_decode_plaintext(plaintext, &desc_plaintext);
  559. tt_int_op(ret, OP_EQ, -1);
  560. }
  561. /* Max length. */
  562. {
  563. size_t big = 64000;
  564. /* Must always be bigger than HS_DESC_MAX_LEN. */
  565. tt_int_op(HS_DESC_MAX_LEN, OP_LT, big);
  566. char *plaintext = tor_malloc_zero(big);
  567. memset(plaintext, 'a', big);
  568. plaintext[big - 1] = '\0';
  569. ret = hs_desc_decode_plaintext(plaintext, &desc_plaintext);
  570. tor_free(plaintext);
  571. tt_int_op(ret, OP_EQ, -1);
  572. }
  573. /* Bad lifetime value. */
  574. {
  575. char *plaintext;
  576. tor_asprintf(&plaintext, template, "3", bad_value, "42", "MESSAGE");
  577. ret = hs_desc_decode_plaintext(plaintext, &desc_plaintext);
  578. tor_free(plaintext);
  579. tt_int_op(ret, OP_EQ, -1);
  580. }
  581. /* Huge lifetime value. */
  582. {
  583. char *plaintext;
  584. tor_asprintf(&plaintext, template, "3", "7181615", "42", "MESSAGE");
  585. ret = hs_desc_decode_plaintext(plaintext, &desc_plaintext);
  586. tor_free(plaintext);
  587. tt_int_op(ret, OP_EQ, -1);
  588. }
  589. /* Invalid encrypted section. */
  590. {
  591. char *plaintext;
  592. tor_asprintf(&plaintext, template, "3", "180", "42", bad_value);
  593. ret = hs_desc_decode_plaintext(plaintext, &desc_plaintext);
  594. tor_free(plaintext);
  595. tt_int_op(ret, OP_EQ, -1);
  596. }
  597. /* Invalid revision counter. */
  598. {
  599. char *plaintext;
  600. tor_asprintf(&plaintext, template, "3", "180", bad_value, "MESSAGE");
  601. ret = hs_desc_decode_plaintext(plaintext, &desc_plaintext);
  602. tor_free(plaintext);
  603. tt_int_op(ret, OP_EQ, -1);
  604. }
  605. done:
  606. ;
  607. }
  608. static void
  609. test_validate_cert(void *arg)
  610. {
  611. int ret;
  612. time_t now = time(NULL);
  613. ed25519_keypair_t kp;
  614. tor_cert_t *cert = NULL;
  615. (void) arg;
  616. ret = ed25519_keypair_generate(&kp, 0);
  617. tt_int_op(ret, OP_EQ, 0);
  618. /* Cert of type CERT_TYPE_AUTH_HS_IP_KEY. */
  619. cert = tor_cert_create(&kp, CERT_TYPE_AUTH_HS_IP_KEY,
  620. &kp.pubkey, now, 3600,
  621. CERT_FLAG_INCLUDE_SIGNING_KEY);
  622. tt_assert(cert);
  623. /* Test with empty certificate. */
  624. ret = cert_is_valid(NULL, CERT_TYPE_AUTH_HS_IP_KEY, "unicorn");
  625. tt_int_op(ret, OP_EQ, 0);
  626. /* Test with a bad type. */
  627. ret = cert_is_valid(cert, CERT_TYPE_SIGNING_HS_DESC, "unicorn");
  628. tt_int_op(ret, OP_EQ, 0);
  629. /* Normal validation. */
  630. ret = cert_is_valid(cert, CERT_TYPE_AUTH_HS_IP_KEY, "unicorn");
  631. tt_int_op(ret, OP_EQ, 1);
  632. /* Break signing key so signature verification will fails. */
  633. memset(&cert->signing_key, 0, sizeof(cert->signing_key));
  634. ret = cert_is_valid(cert, CERT_TYPE_AUTH_HS_IP_KEY, "unicorn");
  635. tt_int_op(ret, OP_EQ, 0);
  636. tor_cert_free(cert);
  637. /* Try a cert without including the signing key. */
  638. cert = tor_cert_create(&kp, CERT_TYPE_AUTH_HS_IP_KEY, &kp.pubkey, now,
  639. 3600, 0);
  640. tt_assert(cert);
  641. /* Test with a bad type. */
  642. ret = cert_is_valid(cert, CERT_TYPE_AUTH_HS_IP_KEY, "unicorn");
  643. tt_int_op(ret, OP_EQ, 0);
  644. done:
  645. tor_cert_free(cert);
  646. }
  647. static void
  648. test_desc_signature(void *arg)
  649. {
  650. int ret;
  651. char *data = NULL, *desc = NULL;
  652. char sig_b64[ED25519_SIG_BASE64_LEN + 1];
  653. ed25519_keypair_t kp;
  654. ed25519_signature_t sig;
  655. (void) arg;
  656. ed25519_keypair_generate(&kp, 0);
  657. /* Setup a phoony descriptor but with a valid signature token that is the
  658. * signature is verifiable. */
  659. tor_asprintf(&data, "This is a signed descriptor\n");
  660. ret = ed25519_sign_prefixed(&sig, (const uint8_t *) data, strlen(data),
  661. "Tor onion service descriptor sig v3", &kp);
  662. tt_int_op(ret, OP_EQ, 0);
  663. ret = ed25519_signature_to_base64(sig_b64, &sig);
  664. tt_int_op(ret, OP_EQ, 0);
  665. /* Build the descriptor that should be valid. */
  666. tor_asprintf(&desc, "%ssignature %s\n", data, sig_b64);
  667. ret = desc_sig_is_valid(sig_b64, &kp.pubkey, desc, strlen(desc));
  668. tt_int_op(ret, OP_EQ, 1);
  669. /* Junk signature. */
  670. ret = desc_sig_is_valid("JUNK", &kp.pubkey, desc, strlen(desc));
  671. tt_int_op(ret, OP_EQ, 0);
  672. done:
  673. tor_free(desc);
  674. tor_free(data);
  675. }
  676. static void
  677. test_build_authorized_client(void *arg)
  678. {
  679. int ret;
  680. hs_desc_authorized_client_t *desc_client = NULL;
  681. uint8_t descriptor_cookie[HS_DESC_DESCRIPTOR_COOKIE_LEN];
  682. curve25519_secret_key_t auth_ephemeral_sk;
  683. curve25519_secret_key_t client_sk;
  684. curve25519_public_key_t client_pk;
  685. const char ephemeral_sk_b16[] =
  686. "d023b674d993a5c8446bd2ca97e9961149b3c0e88c7dc14e8777744dd3468d6a";
  687. const char descriptor_cookie_b16[] =
  688. "07d087f1d8c68393721f6e70316d3b29";
  689. const char client_pubkey_b16[] =
  690. "8c1298fa6050e372f8598f6deca32e27b0ad457741422c2629ebb132cf7fae37";
  691. char *mem_op_hex_tmp=NULL;
  692. (void) arg;
  693. ret = curve25519_secret_key_generate(&auth_ephemeral_sk, 0);
  694. tt_int_op(ret, OP_EQ, 0);
  695. ret = curve25519_secret_key_generate(&client_sk, 0);
  696. tt_int_op(ret, OP_EQ, 0);
  697. curve25519_public_key_generate(&client_pk, &client_sk);
  698. desc_client = tor_malloc_zero(sizeof(hs_desc_authorized_client_t));
  699. base16_decode((char *) &auth_ephemeral_sk,
  700. sizeof(auth_ephemeral_sk),
  701. ephemeral_sk_b16,
  702. strlen(ephemeral_sk_b16));
  703. base16_decode((char *) descriptor_cookie,
  704. sizeof(descriptor_cookie),
  705. descriptor_cookie_b16,
  706. strlen(descriptor_cookie_b16));
  707. base16_decode((char *) &client_pk,
  708. sizeof(client_pk),
  709. client_pubkey_b16,
  710. strlen(client_pubkey_b16));
  711. MOCK(crypto_strongest_rand, mock_crypto_strongest_rand);
  712. hs_desc_build_authorized_client(&client_pk, &auth_ephemeral_sk,
  713. descriptor_cookie, desc_client);
  714. test_memeq_hex((char *) desc_client->client_id,
  715. "b514ef67192cad5f");
  716. test_memeq_hex((char *) desc_client->iv,
  717. "01010101010101010101010101010101");
  718. test_memeq_hex((char *) desc_client->encrypted_cookie,
  719. "46860a9df37b9f6d708E0D7E730C10C1");
  720. done:
  721. tor_free(desc_client);
  722. tor_free(mem_op_hex_tmp);
  723. UNMOCK(crypto_strongest_rand);
  724. }
  725. /* bad desc auth type */
  726. static const char bad_superencrypted_text1[] = "desc-auth-type scoobysnack\n"
  727. "desc-auth-ephemeral-key A/O8DVtnUheb3r1JqoB8uJB7wxXL1XJX3eny4yB+eFA=\n"
  728. "auth-client oiNrQB8WwKo S5D02W7vKgiWIMygrBl8RQ FB//SfOBmLEx1kViEWWL1g\n"
  729. "encrypted\n"
  730. "-----BEGIN MESSAGE-----\n"
  731. "YmVpbmcgb24gbW91bnRhaW5zLCB0aGlua2luZyBhYm91dCBjb21wdXRlcnMsIGlzIG5vdC"
  732. "BiYWQgYXQgYWxs\n"
  733. "-----END MESSAGE-----\n";
  734. /* bad ephemeral key */
  735. static const char bad_superencrypted_text2[] = "desc-auth-type x25519\n"
  736. "desc-auth-ephemeral-key differentalphabet\n"
  737. "auth-client oiNrQB8WwKo S5D02W7vKgiWIMygrBl8RQ FB//SfOBmLEx1kViEWWL1g\n"
  738. "encrypted\n"
  739. "-----BEGIN MESSAGE-----\n"
  740. "YmVpbmcgb24gbW91bnRhaW5zLCB0aGlua2luZyBhYm91dCBjb21wdXRlcnMsIGlzIG5vdC"
  741. "BiYWQgYXQgYWxs\n"
  742. "-----END MESSAGE-----\n";
  743. /* bad encrypted msg */
  744. static const char bad_superencrypted_text3[] = "desc-auth-type x25519\n"
  745. "desc-auth-ephemeral-key A/O8DVtnUheb3r1JqoB8uJB7wxXL1XJX3eny4yB+eFA=\n"
  746. "auth-client oiNrQB8WwKo S5D02W7vKgiWIMygrBl8RQ FB//SfOBmLEx1kViEWWL1g\n"
  747. "encrypted\n"
  748. "-----BEGIN MESSAGE-----\n"
  749. "SO SMALL NOT GOOD\n"
  750. "-----END MESSAGE-----\n";
  751. static const char correct_superencrypted_text[] = "desc-auth-type x25519\n"
  752. "desc-auth-ephemeral-key A/O8DVtnUheb3r1JqoB8uJB7wxXL1XJX3eny4yB+eFA=\n"
  753. "auth-client oiNrQB8WwKo S5D02W7vKgiWIMygrBl8RQ FB//SfOBmLEx1kViEWWL1g\n"
  754. "auth-client Od09Qu636Qo /PKLzqewAdS/+0+vZC+MvQ dpw4NFo13zDnuPz45rxrOg\n"
  755. "auth-client JRr840iGYN0 8s8cxYqF7Lx23+NducC4Qg zAafl4wPLURkuEjJreZq1g\n"
  756. "encrypted\n"
  757. "-----BEGIN MESSAGE-----\n"
  758. "YmVpbmcgb24gbW91bnRhaW5zLCB0aGlua2luZyBhYm91dCBjb21wdXRlcnMsIGlzIG5vdC"
  759. "BiYWQgYXQgYWxs\n"
  760. "-----END MESSAGE-----\n";
  761. static const char correct_encrypted_plaintext[] = "being on mountains, "
  762. "thinking about computers, is not bad at all";
  763. static void
  764. test_parse_hs_desc_superencrypted(void *arg)
  765. {
  766. (void) arg;
  767. size_t retval;
  768. uint8_t *encrypted_out = NULL;
  769. {
  770. setup_full_capture_of_logs(LOG_WARN);
  771. retval = decode_superencrypted(bad_superencrypted_text1,
  772. strlen(bad_superencrypted_text1),
  773. &encrypted_out);
  774. tt_u64_op(retval, OP_EQ, 0);
  775. tt_ptr_op(encrypted_out, OP_EQ, NULL);
  776. expect_log_msg_containing("Unrecognized desc auth type");
  777. teardown_capture_of_logs();
  778. }
  779. {
  780. setup_full_capture_of_logs(LOG_WARN);
  781. retval = decode_superencrypted(bad_superencrypted_text2,
  782. strlen(bad_superencrypted_text2),
  783. &encrypted_out);
  784. tt_u64_op(retval, OP_EQ, 0);
  785. tt_ptr_op(encrypted_out, OP_EQ, NULL);
  786. expect_log_msg_containing("Bogus desc auth key in HS desc");
  787. teardown_capture_of_logs();
  788. }
  789. {
  790. setup_full_capture_of_logs(LOG_WARN);
  791. retval = decode_superencrypted(bad_superencrypted_text3,
  792. strlen(bad_superencrypted_text3),
  793. &encrypted_out);
  794. tt_u64_op(retval, OP_EQ, 0);
  795. tt_ptr_op(encrypted_out, OP_EQ, NULL);
  796. expect_log_msg_containing("Length of descriptor\'s encrypted data "
  797. "is too small.");
  798. teardown_capture_of_logs();
  799. }
  800. /* Now finally the good one */
  801. retval = decode_superencrypted(correct_superencrypted_text,
  802. strlen(correct_superencrypted_text),
  803. &encrypted_out);
  804. tt_u64_op(retval, OP_EQ, strlen(correct_encrypted_plaintext));
  805. tt_mem_op(encrypted_out, OP_EQ, correct_encrypted_plaintext,
  806. strlen(correct_encrypted_plaintext));
  807. done:
  808. tor_free(encrypted_out);
  809. }
  810. struct testcase_t hs_descriptor[] = {
  811. /* Encoding tests. */
  812. { "cert_encoding", test_cert_encoding, TT_FORK,
  813. NULL, NULL },
  814. { "link_specifier", test_link_specifier, TT_FORK,
  815. NULL, NULL },
  816. { "encode_descriptor", test_encode_descriptor, TT_FORK,
  817. NULL, NULL },
  818. { "descriptor_padding", test_descriptor_padding, TT_FORK,
  819. NULL, NULL },
  820. /* Decoding tests. */
  821. { "decode_descriptor", test_decode_descriptor, TT_FORK,
  822. NULL, NULL },
  823. { "encrypted_data_len", test_encrypted_data_len, TT_FORK,
  824. NULL, NULL },
  825. { "decode_invalid_intro_point", test_decode_invalid_intro_point, TT_FORK,
  826. NULL, NULL },
  827. { "decode_plaintext", test_decode_plaintext, TT_FORK,
  828. NULL, NULL },
  829. { "decode_bad_signature", test_decode_bad_signature, TT_FORK,
  830. NULL, NULL },
  831. /* Misc. */
  832. { "version", test_supported_version, TT_FORK,
  833. NULL, NULL },
  834. { "validate_cert", test_validate_cert, TT_FORK,
  835. NULL, NULL },
  836. { "desc_signature", test_desc_signature, TT_FORK,
  837. NULL, NULL },
  838. { "build_authorized_client", test_build_authorized_client, TT_FORK,
  839. NULL, NULL },
  840. { "parse_hs_desc_superencrypted", test_parse_hs_desc_superencrypted,
  841. TT_FORK, NULL, NULL },
  842. END_OF_TESTCASES
  843. };