test_hs_descriptor.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861
  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. uint8_t subcredential[DIGEST256_LEN];
  258. (void) arg;
  259. ret = ed25519_keypair_generate(&signing_kp, 0);
  260. tt_int_op(ret, ==, 0);
  261. desc = hs_helper_build_hs_desc_with_ip(&signing_kp);
  262. hs_helper_get_subcred_from_identity_keypair(&signing_kp,
  263. subcredential);
  264. /* Give some bad stuff to the decoding function. */
  265. ret = hs_desc_decode_descriptor("hladfjlkjadf", subcredential, &decoded);
  266. tt_int_op(ret, OP_EQ, -1);
  267. ret = hs_desc_encode_descriptor(desc, &signing_kp, &encoded);
  268. tt_int_op(ret, ==, 0);
  269. tt_assert(encoded);
  270. ret = hs_desc_decode_descriptor(encoded, subcredential, &decoded);
  271. tt_int_op(ret, ==, 0);
  272. tt_assert(decoded);
  273. hs_helper_desc_equal(desc, decoded);
  274. /* Decode a descriptor with _no_ introduction points. */
  275. {
  276. ed25519_keypair_t signing_kp_no_ip;
  277. ret = ed25519_keypair_generate(&signing_kp_no_ip, 0);
  278. tt_int_op(ret, ==, 0);
  279. hs_helper_get_subcred_from_identity_keypair(&signing_kp_no_ip,
  280. subcredential);
  281. desc_no_ip = hs_helper_build_hs_desc_no_ip(&signing_kp_no_ip);
  282. tt_assert(desc_no_ip);
  283. tor_free(encoded);
  284. ret = hs_desc_encode_descriptor(desc_no_ip, &signing_kp_no_ip, &encoded);
  285. tt_int_op(ret, ==, 0);
  286. tt_assert(encoded);
  287. hs_descriptor_free(decoded);
  288. ret = hs_desc_decode_descriptor(encoded, subcredential, &decoded);
  289. tt_int_op(ret, ==, 0);
  290. tt_assert(decoded);
  291. }
  292. done:
  293. hs_descriptor_free(desc);
  294. hs_descriptor_free(desc_no_ip);
  295. hs_descriptor_free(decoded);
  296. tor_free(encoded);
  297. }
  298. static void
  299. test_supported_version(void *arg)
  300. {
  301. int ret;
  302. (void) arg;
  303. /* Unsupported. */
  304. ret = hs_desc_is_supported_version(42);
  305. tt_int_op(ret, OP_EQ, 0);
  306. /* To early. */
  307. ret = hs_desc_is_supported_version(HS_DESC_SUPPORTED_FORMAT_VERSION_MIN - 1);
  308. tt_int_op(ret, OP_EQ, 0);
  309. /* One too new. */
  310. ret = hs_desc_is_supported_version(HS_DESC_SUPPORTED_FORMAT_VERSION_MAX + 1);
  311. tt_int_op(ret, OP_EQ, 0);
  312. /* Valid version. */
  313. ret = hs_desc_is_supported_version(3);
  314. tt_int_op(ret, OP_EQ, 1);
  315. done:
  316. ;
  317. }
  318. static void
  319. test_encrypted_data_len(void *arg)
  320. {
  321. int ret;
  322. size_t value;
  323. (void) arg;
  324. /* No length, error. */
  325. ret = encrypted_data_length_is_valid(0);
  326. tt_int_op(ret, OP_EQ, 0);
  327. /* Valid value. */
  328. value = HS_DESC_ENCRYPTED_SALT_LEN + DIGEST256_LEN + 1;
  329. ret = encrypted_data_length_is_valid(value);
  330. tt_int_op(ret, OP_EQ, 1);
  331. done:
  332. ;
  333. }
  334. static void
  335. test_decode_invalid_intro_point(void *arg)
  336. {
  337. int ret;
  338. char *encoded_ip = NULL;
  339. size_t len_out;
  340. hs_desc_intro_point_t *ip = NULL;
  341. ed25519_keypair_t signing_kp;
  342. hs_descriptor_t *desc = NULL;
  343. (void) arg;
  344. /* Seperate pieces of a valid encoded introduction point. */
  345. const char *intro_point =
  346. "introduction-point AQIUMDI5OUYyNjhGQ0E5RDU1Q0QxNTc=";
  347. const char *auth_key =
  348. "auth-key\n"
  349. "-----BEGIN ED25519 CERT-----\n"
  350. "AQkACOhAAQW8ltYZMIWpyrfyE/b4Iyi8CNybCwYs6ADk7XfBaxsFAQAgBAD3/BE4\n"
  351. "XojGE/N2bW/wgnS9r2qlrkydGyuCKIGayYx3haZ39LD4ZTmSMRxwmplMAqzG/XNP\n"
  352. "0Kkpg4p2/VnLFJRdU1SMFo1lgQ4P0bqw7Tgx200fulZ4KUM5z5V7m+a/mgY=\n"
  353. "-----END ED25519 CERT-----";
  354. const char *enc_key =
  355. "enc-key ntor bpZKLsuhxP6woDQ3yVyjm5gUKSk7RjfAijT2qrzbQk0=";
  356. const char *enc_key_cert =
  357. "enc-key-cert\n"
  358. "-----BEGIN ED25519 CERT-----\n"
  359. "AQsACOhZAUpNvCZ1aJaaR49lS6MCdsVkhVGVrRqoj0Y2T4SzroAtAQAgBABFOcGg\n"
  360. "lbTt1DF5nKTE/gU3Fr8ZtlCIOhu1A+F5LM7fqCUupfesg0KTHwyIZOYQbJuM5/he\n"
  361. "/jDNyLy9woPJdjkxywaY2RPUxGjLYtMQV0E8PUxWyICV+7y52fTCYaKpYQw=\n"
  362. "-----END ED25519 CERT-----";
  363. /* Try to decode a junk string. */
  364. {
  365. hs_descriptor_free(desc);
  366. desc = NULL;
  367. ret = ed25519_keypair_generate(&signing_kp, 0);
  368. tt_int_op(ret, ==, 0);
  369. desc = hs_helper_build_hs_desc_with_ip(&signing_kp);
  370. const char *junk = "this is not a descriptor";
  371. ip = decode_introduction_point(desc, junk);
  372. tt_assert(!ip);
  373. hs_desc_intro_point_free(ip);
  374. ip = NULL;
  375. }
  376. /* Invalid link specifiers. */
  377. {
  378. smartlist_t *lines = smartlist_new();
  379. const char *bad_line = "introduction-point blah";
  380. smartlist_add(lines, (char *) bad_line);
  381. smartlist_add(lines, (char *) auth_key);
  382. smartlist_add(lines, (char *) enc_key);
  383. smartlist_add(lines, (char *) enc_key_cert);
  384. encoded_ip = smartlist_join_strings(lines, "\n", 0, &len_out);
  385. tt_assert(encoded_ip);
  386. ip = decode_introduction_point(desc, encoded_ip);
  387. tt_assert(!ip);
  388. tor_free(encoded_ip);
  389. smartlist_free(lines);
  390. hs_desc_intro_point_free(ip);
  391. ip = NULL;
  392. }
  393. /* Invalid auth key type. */
  394. {
  395. smartlist_t *lines = smartlist_new();
  396. /* Try to put a valid object that our tokenize function will be able to
  397. * parse but that has nothing to do with the auth_key. */
  398. const char *bad_line =
  399. "auth-key\n"
  400. "-----BEGIN UNICORN CERT-----\n"
  401. "MIGJAoGBAO4bATcW8kW4h6RQQAKEgg+aXCpF4JwbcO6vGZtzXTDB+HdPVQzwqkbh\n"
  402. "XzFM6VGArhYw4m31wcP1Z7IwULir7UMnAFd7Zi62aYfU6l+Y1yAoZ1wzu1XBaAMK\n"
  403. "ejpwQinW9nzJn7c2f69fVke3pkhxpNdUZ+vplSA/l9iY+y+v+415AgMBAAE=\n"
  404. "-----END UNICORN CERT-----";
  405. /* Build intro point text. */
  406. smartlist_add(lines, (char *) intro_point);
  407. smartlist_add(lines, (char *) bad_line);
  408. smartlist_add(lines, (char *) enc_key);
  409. smartlist_add(lines, (char *) enc_key_cert);
  410. encoded_ip = smartlist_join_strings(lines, "\n", 0, &len_out);
  411. tt_assert(encoded_ip);
  412. ip = decode_introduction_point(desc, encoded_ip);
  413. tt_assert(!ip);
  414. tor_free(encoded_ip);
  415. smartlist_free(lines);
  416. }
  417. /* Invalid enc-key. */
  418. {
  419. smartlist_t *lines = smartlist_new();
  420. const char *bad_line =
  421. "enc-key unicorn bpZKLsuhxP6woDQ3yVyjm5gUKSk7RjfAijT2qrzbQk0=";
  422. /* Build intro point text. */
  423. smartlist_add(lines, (char *) intro_point);
  424. smartlist_add(lines, (char *) auth_key);
  425. smartlist_add(lines, (char *) bad_line);
  426. smartlist_add(lines, (char *) enc_key_cert);
  427. encoded_ip = smartlist_join_strings(lines, "\n", 0, &len_out);
  428. tt_assert(encoded_ip);
  429. ip = decode_introduction_point(desc, encoded_ip);
  430. tt_assert(!ip);
  431. tor_free(encoded_ip);
  432. smartlist_free(lines);
  433. }
  434. /* Invalid enc-key object. */
  435. {
  436. smartlist_t *lines = smartlist_new();
  437. const char *bad_line = "enc-key ntor";
  438. /* Build intro point text. */
  439. smartlist_add(lines, (char *) intro_point);
  440. smartlist_add(lines, (char *) auth_key);
  441. smartlist_add(lines, (char *) bad_line);
  442. smartlist_add(lines, (char *) enc_key_cert);
  443. encoded_ip = smartlist_join_strings(lines, "\n", 0, &len_out);
  444. tt_assert(encoded_ip);
  445. ip = decode_introduction_point(desc, encoded_ip);
  446. tt_assert(!ip);
  447. tor_free(encoded_ip);
  448. smartlist_free(lines);
  449. }
  450. /* Invalid enc-key base64 curv25519 key. */
  451. {
  452. smartlist_t *lines = smartlist_new();
  453. const char *bad_line = "enc-key ntor blah===";
  454. /* Build intro point text. */
  455. smartlist_add(lines, (char *) intro_point);
  456. smartlist_add(lines, (char *) auth_key);
  457. smartlist_add(lines, (char *) bad_line);
  458. smartlist_add(lines, (char *) enc_key_cert);
  459. encoded_ip = smartlist_join_strings(lines, "\n", 0, &len_out);
  460. tt_assert(encoded_ip);
  461. ip = decode_introduction_point(desc, encoded_ip);
  462. tt_assert(!ip);
  463. tor_free(encoded_ip);
  464. smartlist_free(lines);
  465. }
  466. /* Invalid enc-key invalid legacy. */
  467. {
  468. smartlist_t *lines = smartlist_new();
  469. const char *bad_line = "legacy-key blah===";
  470. /* Build intro point text. */
  471. smartlist_add(lines, (char *) intro_point);
  472. smartlist_add(lines, (char *) auth_key);
  473. smartlist_add(lines, (char *) bad_line);
  474. smartlist_add(lines, (char *) enc_key_cert);
  475. encoded_ip = smartlist_join_strings(lines, "\n", 0, &len_out);
  476. tt_assert(encoded_ip);
  477. ip = decode_introduction_point(desc, encoded_ip);
  478. tt_assert(!ip);
  479. tor_free(encoded_ip);
  480. smartlist_free(lines);
  481. }
  482. done:
  483. hs_descriptor_free(desc);
  484. hs_desc_intro_point_free(ip);
  485. }
  486. static void
  487. test_decode_plaintext(void *arg)
  488. {
  489. int ret;
  490. hs_desc_plaintext_data_t desc_plaintext;
  491. const char *bad_value = "unicorn";
  492. (void) arg;
  493. #define template \
  494. "hs-descriptor %s\n" \
  495. "descriptor-lifetime %s\n" \
  496. "descriptor-signing-key-cert\n" \
  497. "-----BEGIN ED25519 CERT-----\n" \
  498. "AQgABjvPAQaG3g+dc6oV/oJV4ODAtkvx56uBnPtBT9mYVuHVOhn7AQAgBABUg3mQ\n" \
  499. "myBr4bu5LCr53wUEbW2EXui01CbUgU7pfo9LvJG3AcXRojj6HlfsUs9BkzYzYdjF\n" \
  500. "A69Apikgu0ewHYkFFASt7Il+gB3w6J8YstQJZT7dtbtl+doM7ug8B68Qdg8=\n" \
  501. "-----END ED25519 CERT-----\n" \
  502. "revision-counter %s\n" \
  503. "encrypted\n" \
  504. "-----BEGIN %s-----\n" \
  505. "UNICORN\n" \
  506. "-----END MESSAGE-----\n" \
  507. "signature m20WJH5agqvwhq7QeuEZ1mYyPWQDO+eJOZUjLhAiKu8DbL17DsDfJE6kXbWy" \
  508. "HimbNj2we0enV3cCOOAsmPOaAw\n"
  509. /* Invalid version. */
  510. {
  511. char *plaintext;
  512. tor_asprintf(&plaintext, template, bad_value, "180", "42", "MESSAGE");
  513. ret = hs_desc_decode_plaintext(plaintext, &desc_plaintext);
  514. tor_free(plaintext);
  515. tt_int_op(ret, OP_EQ, -1);
  516. }
  517. /* Missing fields. */
  518. {
  519. const char *plaintext = "hs-descriptor 3\n";
  520. ret = hs_desc_decode_plaintext(plaintext, &desc_plaintext);
  521. tt_int_op(ret, OP_EQ, -1);
  522. }
  523. /* Max length. */
  524. {
  525. size_t big = 64000;
  526. /* Must always be bigger than HS_DESC_MAX_LEN. */
  527. tt_int_op(HS_DESC_MAX_LEN, <, big);
  528. char *plaintext = tor_malloc_zero(big);
  529. memset(plaintext, 'a', big);
  530. plaintext[big - 1] = '\0';
  531. ret = hs_desc_decode_plaintext(plaintext, &desc_plaintext);
  532. tor_free(plaintext);
  533. tt_int_op(ret, OP_EQ, -1);
  534. }
  535. /* Bad lifetime value. */
  536. {
  537. char *plaintext;
  538. tor_asprintf(&plaintext, template, "3", bad_value, "42", "MESSAGE");
  539. ret = hs_desc_decode_plaintext(plaintext, &desc_plaintext);
  540. tor_free(plaintext);
  541. tt_int_op(ret, OP_EQ, -1);
  542. }
  543. /* Huge lifetime value. */
  544. {
  545. char *plaintext;
  546. tor_asprintf(&plaintext, template, "3", "7181615", "42", "MESSAGE");
  547. ret = hs_desc_decode_plaintext(plaintext, &desc_plaintext);
  548. tor_free(plaintext);
  549. tt_int_op(ret, OP_EQ, -1);
  550. }
  551. /* Invalid encrypted section. */
  552. {
  553. char *plaintext;
  554. tor_asprintf(&plaintext, template, "3", "180", "42", bad_value);
  555. ret = hs_desc_decode_plaintext(plaintext, &desc_plaintext);
  556. tor_free(plaintext);
  557. tt_int_op(ret, OP_EQ, -1);
  558. }
  559. /* Invalid revision counter. */
  560. {
  561. char *plaintext;
  562. tor_asprintf(&plaintext, template, "3", "180", bad_value, "MESSAGE");
  563. ret = hs_desc_decode_plaintext(plaintext, &desc_plaintext);
  564. tor_free(plaintext);
  565. tt_int_op(ret, OP_EQ, -1);
  566. }
  567. done:
  568. ;
  569. }
  570. static void
  571. test_validate_cert(void *arg)
  572. {
  573. int ret;
  574. time_t now = time(NULL);
  575. ed25519_keypair_t kp;
  576. tor_cert_t *cert = NULL;
  577. (void) arg;
  578. ret = ed25519_keypair_generate(&kp, 0);
  579. tt_int_op(ret, ==, 0);
  580. /* Cert of type CERT_TYPE_AUTH_HS_IP_KEY. */
  581. cert = tor_cert_create(&kp, CERT_TYPE_AUTH_HS_IP_KEY,
  582. &kp.pubkey, now, 3600,
  583. CERT_FLAG_INCLUDE_SIGNING_KEY);
  584. tt_assert(cert);
  585. /* Test with empty certificate. */
  586. ret = cert_is_valid(NULL, CERT_TYPE_AUTH_HS_IP_KEY, "unicorn");
  587. tt_int_op(ret, OP_EQ, 0);
  588. /* Test with a bad type. */
  589. ret = cert_is_valid(cert, CERT_TYPE_SIGNING_HS_DESC, "unicorn");
  590. tt_int_op(ret, OP_EQ, 0);
  591. /* Normal validation. */
  592. ret = cert_is_valid(cert, CERT_TYPE_AUTH_HS_IP_KEY, "unicorn");
  593. tt_int_op(ret, OP_EQ, 1);
  594. /* Break signing key so signature verification will fails. */
  595. memset(&cert->signing_key, 0, sizeof(cert->signing_key));
  596. ret = cert_is_valid(cert, CERT_TYPE_AUTH_HS_IP_KEY, "unicorn");
  597. tt_int_op(ret, OP_EQ, 0);
  598. tor_cert_free(cert);
  599. /* Try a cert without including the signing key. */
  600. cert = tor_cert_create(&kp, CERT_TYPE_AUTH_HS_IP_KEY, &kp.pubkey, now,
  601. 3600, 0);
  602. tt_assert(cert);
  603. /* Test with a bad type. */
  604. ret = cert_is_valid(cert, CERT_TYPE_AUTH_HS_IP_KEY, "unicorn");
  605. tt_int_op(ret, OP_EQ, 0);
  606. done:
  607. tor_cert_free(cert);
  608. }
  609. static void
  610. test_desc_signature(void *arg)
  611. {
  612. int ret;
  613. char *data = NULL, *desc = NULL;
  614. char sig_b64[ED25519_SIG_BASE64_LEN + 1];
  615. ed25519_keypair_t kp;
  616. ed25519_signature_t sig;
  617. (void) arg;
  618. ed25519_keypair_generate(&kp, 0);
  619. /* Setup a phoony descriptor but with a valid signature token that is the
  620. * signature is verifiable. */
  621. tor_asprintf(&data, "This is a signed descriptor\n");
  622. ret = ed25519_sign_prefixed(&sig, (const uint8_t *) data, strlen(data),
  623. "Tor onion service descriptor sig v3", &kp);
  624. tt_int_op(ret, ==, 0);
  625. ret = ed25519_signature_to_base64(sig_b64, &sig);
  626. tt_int_op(ret, ==, 0);
  627. /* Build the descriptor that should be valid. */
  628. tor_asprintf(&desc, "%ssignature %s\n", data, sig_b64);
  629. ret = desc_sig_is_valid(sig_b64, &kp.pubkey, desc, strlen(desc));
  630. tt_int_op(ret, ==, 1);
  631. /* Junk signature. */
  632. ret = desc_sig_is_valid("JUNK", &kp.pubkey, desc, strlen(desc));
  633. tt_int_op(ret, ==, 0);
  634. done:
  635. tor_free(desc);
  636. tor_free(data);
  637. }
  638. /* bad desc auth type */
  639. static const char bad_superencrypted_text1[] = "desc-auth-type scoobysnack\n"
  640. "desc-auth-ephemeral-key A/O8DVtnUheb3r1JqoB8uJB7wxXL1XJX3eny4yB+eFA=\n"
  641. "auth-client oiNrQB8WwKo S5D02W7vKgiWIMygrBl8RQ FB//SfOBmLEx1kViEWWL1g\n"
  642. "encrypted\n"
  643. "-----BEGIN MESSAGE-----\n"
  644. "YmVpbmcgb24gbW91bnRhaW5zLCB0aGlua2luZyBhYm91dCBjb21wdXRlcnMsIGlzIG5vdC"
  645. "BiYWQgYXQgYWxs\n"
  646. "-----END MESSAGE-----\n";
  647. /* bad ephemeral key */
  648. static const char bad_superencrypted_text2[] = "desc-auth-type x25519\n"
  649. "desc-auth-ephemeral-key differentalphabet\n"
  650. "auth-client oiNrQB8WwKo S5D02W7vKgiWIMygrBl8RQ FB//SfOBmLEx1kViEWWL1g\n"
  651. "encrypted\n"
  652. "-----BEGIN MESSAGE-----\n"
  653. "YmVpbmcgb24gbW91bnRhaW5zLCB0aGlua2luZyBhYm91dCBjb21wdXRlcnMsIGlzIG5vdC"
  654. "BiYWQgYXQgYWxs\n"
  655. "-----END MESSAGE-----\n";
  656. /* bad encrypted msg */
  657. static const char bad_superencrypted_text3[] = "desc-auth-type x25519\n"
  658. "desc-auth-ephemeral-key A/O8DVtnUheb3r1JqoB8uJB7wxXL1XJX3eny4yB+eFA=\n"
  659. "auth-client oiNrQB8WwKo S5D02W7vKgiWIMygrBl8RQ FB//SfOBmLEx1kViEWWL1g\n"
  660. "encrypted\n"
  661. "-----BEGIN MESSAGE-----\n"
  662. "SO SMALL NOT GOOD\n"
  663. "-----END MESSAGE-----\n";
  664. static const char correct_superencrypted_text[] = "desc-auth-type x25519\n"
  665. "desc-auth-ephemeral-key A/O8DVtnUheb3r1JqoB8uJB7wxXL1XJX3eny4yB+eFA=\n"
  666. "auth-client oiNrQB8WwKo S5D02W7vKgiWIMygrBl8RQ FB//SfOBmLEx1kViEWWL1g\n"
  667. "auth-client Od09Qu636Qo /PKLzqewAdS/+0+vZC+MvQ dpw4NFo13zDnuPz45rxrOg\n"
  668. "auth-client JRr840iGYN0 8s8cxYqF7Lx23+NducC4Qg zAafl4wPLURkuEjJreZq1g\n"
  669. "encrypted\n"
  670. "-----BEGIN MESSAGE-----\n"
  671. "YmVpbmcgb24gbW91bnRhaW5zLCB0aGlua2luZyBhYm91dCBjb21wdXRlcnMsIGlzIG5vdC"
  672. "BiYWQgYXQgYWxs\n"
  673. "-----END MESSAGE-----\n";
  674. static const char correct_encrypted_plaintext[] = "being on mountains, "
  675. "thinking about computers, is not bad at all";
  676. static void
  677. test_parse_hs_desc_superencrypted(void *arg)
  678. {
  679. (void) arg;
  680. size_t retval;
  681. uint8_t *encrypted_out = NULL;
  682. {
  683. setup_full_capture_of_logs(LOG_WARN);
  684. retval = decode_superencrypted(bad_superencrypted_text1,
  685. strlen(bad_superencrypted_text1),
  686. &encrypted_out);
  687. tt_u64_op(retval, ==, 0);
  688. tt_assert(!encrypted_out);
  689. expect_log_msg_containing("Unrecognized desc auth type");
  690. teardown_capture_of_logs();
  691. }
  692. {
  693. setup_full_capture_of_logs(LOG_WARN);
  694. retval = decode_superencrypted(bad_superencrypted_text2,
  695. strlen(bad_superencrypted_text2),
  696. &encrypted_out);
  697. tt_u64_op(retval, ==, 0);
  698. tt_assert(!encrypted_out);
  699. expect_log_msg_containing("Bogus desc auth key in HS desc");
  700. teardown_capture_of_logs();
  701. }
  702. {
  703. setup_full_capture_of_logs(LOG_WARN);
  704. retval = decode_superencrypted(bad_superencrypted_text3,
  705. strlen(bad_superencrypted_text3),
  706. &encrypted_out);
  707. tt_u64_op(retval, ==, 0);
  708. tt_assert(!encrypted_out);
  709. expect_log_msg_containing("Length of descriptor\'s encrypted data "
  710. "is too small.");
  711. teardown_capture_of_logs();
  712. }
  713. /* Now finally the good one */
  714. retval = decode_superencrypted(correct_superencrypted_text,
  715. strlen(correct_superencrypted_text),
  716. &encrypted_out);
  717. tt_u64_op(retval, ==, strlen(correct_encrypted_plaintext));
  718. tt_mem_op(encrypted_out, OP_EQ, correct_encrypted_plaintext,
  719. strlen(correct_encrypted_plaintext));
  720. done:
  721. tor_free(encrypted_out);
  722. }
  723. struct testcase_t hs_descriptor[] = {
  724. /* Encoding tests. */
  725. { "cert_encoding", test_cert_encoding, TT_FORK,
  726. NULL, NULL },
  727. { "link_specifier", test_link_specifier, TT_FORK,
  728. NULL, NULL },
  729. { "encode_descriptor", test_encode_descriptor, TT_FORK,
  730. NULL, NULL },
  731. { "descriptor_padding", test_descriptor_padding, TT_FORK,
  732. NULL, NULL },
  733. /* Decoding tests. */
  734. { "decode_descriptor", test_decode_descriptor, TT_FORK,
  735. NULL, NULL },
  736. { "encrypted_data_len", test_encrypted_data_len, TT_FORK,
  737. NULL, NULL },
  738. { "decode_invalid_intro_point", test_decode_invalid_intro_point, TT_FORK,
  739. NULL, NULL },
  740. { "decode_plaintext", test_decode_plaintext, TT_FORK,
  741. NULL, NULL },
  742. /* Misc. */
  743. { "version", test_supported_version, TT_FORK,
  744. NULL, NULL },
  745. { "validate_cert", test_validate_cert, TT_FORK,
  746. NULL, NULL },
  747. { "desc_signature", test_desc_signature, TT_FORK,
  748. NULL, NULL },
  749. { "parse_hs_desc_superencrypted", test_parse_hs_desc_superencrypted,
  750. TT_FORK, NULL, NULL },
  751. END_OF_TESTCASES
  752. };