test_hs_descriptor.c 27 KB

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