test_hs_descriptor.c 26 KB

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