test_hs_descriptor.c 27 KB

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