test_hs_descriptor.c 28 KB

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