test_hs_descriptor.c 28 KB

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