test_hs_descriptor.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893
  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. }
  510. static void
  511. test_decode_plaintext(void *arg)
  512. {
  513. int ret;
  514. hs_desc_plaintext_data_t desc_plaintext;
  515. const char *bad_value = "unicorn";
  516. (void) arg;
  517. #define template \
  518. "hs-descriptor %s\n" \
  519. "descriptor-lifetime %s\n" \
  520. "descriptor-signing-key-cert\n" \
  521. "-----BEGIN ED25519 CERT-----\n" \
  522. "AQgABjvPAQaG3g+dc6oV/oJV4ODAtkvx56uBnPtBT9mYVuHVOhn7AQAgBABUg3mQ\n" \
  523. "myBr4bu5LCr53wUEbW2EXui01CbUgU7pfo9LvJG3AcXRojj6HlfsUs9BkzYzYdjF\n" \
  524. "A69Apikgu0ewHYkFFASt7Il+gB3w6J8YstQJZT7dtbtl+doM7ug8B68Qdg8=\n" \
  525. "-----END ED25519 CERT-----\n" \
  526. "revision-counter %s\n" \
  527. "encrypted\n" \
  528. "-----BEGIN %s-----\n" \
  529. "UNICORN\n" \
  530. "-----END MESSAGE-----\n" \
  531. "signature m20WJH5agqvwhq7QeuEZ1mYyPWQDO+eJOZUjLhAiKu8DbL17DsDfJE6kXbWy" \
  532. "HimbNj2we0enV3cCOOAsmPOaAw\n"
  533. /* Invalid version. */
  534. {
  535. char *plaintext;
  536. tor_asprintf(&plaintext, template, bad_value, "180", "42", "MESSAGE");
  537. ret = hs_desc_decode_plaintext(plaintext, &desc_plaintext);
  538. tor_free(plaintext);
  539. tt_int_op(ret, OP_EQ, -1);
  540. }
  541. /* Missing fields. */
  542. {
  543. const char *plaintext = "hs-descriptor 3\n";
  544. ret = hs_desc_decode_plaintext(plaintext, &desc_plaintext);
  545. tt_int_op(ret, OP_EQ, -1);
  546. }
  547. /* Max length. */
  548. {
  549. size_t big = 64000;
  550. /* Must always be bigger than HS_DESC_MAX_LEN. */
  551. tt_int_op(HS_DESC_MAX_LEN, OP_LT, big);
  552. char *plaintext = tor_malloc_zero(big);
  553. memset(plaintext, 'a', big);
  554. plaintext[big - 1] = '\0';
  555. ret = hs_desc_decode_plaintext(plaintext, &desc_plaintext);
  556. tor_free(plaintext);
  557. tt_int_op(ret, OP_EQ, -1);
  558. }
  559. /* Bad lifetime value. */
  560. {
  561. char *plaintext;
  562. tor_asprintf(&plaintext, template, "3", bad_value, "42", "MESSAGE");
  563. ret = hs_desc_decode_plaintext(plaintext, &desc_plaintext);
  564. tor_free(plaintext);
  565. tt_int_op(ret, OP_EQ, -1);
  566. }
  567. /* Huge lifetime value. */
  568. {
  569. char *plaintext;
  570. tor_asprintf(&plaintext, template, "3", "7181615", "42", "MESSAGE");
  571. ret = hs_desc_decode_plaintext(plaintext, &desc_plaintext);
  572. tor_free(plaintext);
  573. tt_int_op(ret, OP_EQ, -1);
  574. }
  575. /* Invalid encrypted section. */
  576. {
  577. char *plaintext;
  578. tor_asprintf(&plaintext, template, "3", "180", "42", bad_value);
  579. ret = hs_desc_decode_plaintext(plaintext, &desc_plaintext);
  580. tor_free(plaintext);
  581. tt_int_op(ret, OP_EQ, -1);
  582. }
  583. /* Invalid revision counter. */
  584. {
  585. char *plaintext;
  586. tor_asprintf(&plaintext, template, "3", "180", bad_value, "MESSAGE");
  587. ret = hs_desc_decode_plaintext(plaintext, &desc_plaintext);
  588. tor_free(plaintext);
  589. tt_int_op(ret, OP_EQ, -1);
  590. }
  591. done:
  592. ;
  593. }
  594. static void
  595. test_validate_cert(void *arg)
  596. {
  597. int ret;
  598. time_t now = time(NULL);
  599. ed25519_keypair_t kp;
  600. tor_cert_t *cert = NULL;
  601. (void) arg;
  602. ret = ed25519_keypair_generate(&kp, 0);
  603. tt_int_op(ret, OP_EQ, 0);
  604. /* Cert of type CERT_TYPE_AUTH_HS_IP_KEY. */
  605. cert = tor_cert_create(&kp, CERT_TYPE_AUTH_HS_IP_KEY,
  606. &kp.pubkey, now, 3600,
  607. CERT_FLAG_INCLUDE_SIGNING_KEY);
  608. tt_assert(cert);
  609. /* Test with empty certificate. */
  610. ret = cert_is_valid(NULL, CERT_TYPE_AUTH_HS_IP_KEY, "unicorn");
  611. tt_int_op(ret, OP_EQ, 0);
  612. /* Test with a bad type. */
  613. ret = cert_is_valid(cert, CERT_TYPE_SIGNING_HS_DESC, "unicorn");
  614. tt_int_op(ret, OP_EQ, 0);
  615. /* Normal validation. */
  616. ret = cert_is_valid(cert, CERT_TYPE_AUTH_HS_IP_KEY, "unicorn");
  617. tt_int_op(ret, OP_EQ, 1);
  618. /* Break signing key so signature verification will fails. */
  619. memset(&cert->signing_key, 0, sizeof(cert->signing_key));
  620. ret = cert_is_valid(cert, CERT_TYPE_AUTH_HS_IP_KEY, "unicorn");
  621. tt_int_op(ret, OP_EQ, 0);
  622. tor_cert_free(cert);
  623. /* Try a cert without including the signing key. */
  624. cert = tor_cert_create(&kp, CERT_TYPE_AUTH_HS_IP_KEY, &kp.pubkey, now,
  625. 3600, 0);
  626. tt_assert(cert);
  627. /* Test with a bad type. */
  628. ret = cert_is_valid(cert, CERT_TYPE_AUTH_HS_IP_KEY, "unicorn");
  629. tt_int_op(ret, OP_EQ, 0);
  630. done:
  631. tor_cert_free(cert);
  632. }
  633. static void
  634. test_desc_signature(void *arg)
  635. {
  636. int ret;
  637. char *data = NULL, *desc = NULL;
  638. char sig_b64[ED25519_SIG_BASE64_LEN + 1];
  639. ed25519_keypair_t kp;
  640. ed25519_signature_t sig;
  641. (void) arg;
  642. ed25519_keypair_generate(&kp, 0);
  643. /* Setup a phoony descriptor but with a valid signature token that is the
  644. * signature is verifiable. */
  645. tor_asprintf(&data, "This is a signed descriptor\n");
  646. ret = ed25519_sign_prefixed(&sig, (const uint8_t *) data, strlen(data),
  647. "Tor onion service descriptor sig v3", &kp);
  648. tt_int_op(ret, OP_EQ, 0);
  649. ret = ed25519_signature_to_base64(sig_b64, &sig);
  650. tt_int_op(ret, OP_EQ, 0);
  651. /* Build the descriptor that should be valid. */
  652. tor_asprintf(&desc, "%ssignature %s\n", data, sig_b64);
  653. ret = desc_sig_is_valid(sig_b64, &kp.pubkey, desc, strlen(desc));
  654. tt_int_op(ret, OP_EQ, 1);
  655. /* Junk signature. */
  656. ret = desc_sig_is_valid("JUNK", &kp.pubkey, desc, strlen(desc));
  657. tt_int_op(ret, OP_EQ, 0);
  658. done:
  659. tor_free(desc);
  660. tor_free(data);
  661. }
  662. /* bad desc auth type */
  663. static const char bad_superencrypted_text1[] = "desc-auth-type scoobysnack\n"
  664. "desc-auth-ephemeral-key A/O8DVtnUheb3r1JqoB8uJB7wxXL1XJX3eny4yB+eFA=\n"
  665. "auth-client oiNrQB8WwKo S5D02W7vKgiWIMygrBl8RQ FB//SfOBmLEx1kViEWWL1g\n"
  666. "encrypted\n"
  667. "-----BEGIN MESSAGE-----\n"
  668. "YmVpbmcgb24gbW91bnRhaW5zLCB0aGlua2luZyBhYm91dCBjb21wdXRlcnMsIGlzIG5vdC"
  669. "BiYWQgYXQgYWxs\n"
  670. "-----END MESSAGE-----\n";
  671. /* bad ephemeral key */
  672. static const char bad_superencrypted_text2[] = "desc-auth-type x25519\n"
  673. "desc-auth-ephemeral-key differentalphabet\n"
  674. "auth-client oiNrQB8WwKo S5D02W7vKgiWIMygrBl8RQ FB//SfOBmLEx1kViEWWL1g\n"
  675. "encrypted\n"
  676. "-----BEGIN MESSAGE-----\n"
  677. "YmVpbmcgb24gbW91bnRhaW5zLCB0aGlua2luZyBhYm91dCBjb21wdXRlcnMsIGlzIG5vdC"
  678. "BiYWQgYXQgYWxs\n"
  679. "-----END MESSAGE-----\n";
  680. /* bad encrypted msg */
  681. static const char bad_superencrypted_text3[] = "desc-auth-type x25519\n"
  682. "desc-auth-ephemeral-key A/O8DVtnUheb3r1JqoB8uJB7wxXL1XJX3eny4yB+eFA=\n"
  683. "auth-client oiNrQB8WwKo S5D02W7vKgiWIMygrBl8RQ FB//SfOBmLEx1kViEWWL1g\n"
  684. "encrypted\n"
  685. "-----BEGIN MESSAGE-----\n"
  686. "SO SMALL NOT GOOD\n"
  687. "-----END MESSAGE-----\n";
  688. static const char correct_superencrypted_text[] = "desc-auth-type x25519\n"
  689. "desc-auth-ephemeral-key A/O8DVtnUheb3r1JqoB8uJB7wxXL1XJX3eny4yB+eFA=\n"
  690. "auth-client oiNrQB8WwKo S5D02W7vKgiWIMygrBl8RQ FB//SfOBmLEx1kViEWWL1g\n"
  691. "auth-client Od09Qu636Qo /PKLzqewAdS/+0+vZC+MvQ dpw4NFo13zDnuPz45rxrOg\n"
  692. "auth-client JRr840iGYN0 8s8cxYqF7Lx23+NducC4Qg zAafl4wPLURkuEjJreZq1g\n"
  693. "encrypted\n"
  694. "-----BEGIN MESSAGE-----\n"
  695. "YmVpbmcgb24gbW91bnRhaW5zLCB0aGlua2luZyBhYm91dCBjb21wdXRlcnMsIGlzIG5vdC"
  696. "BiYWQgYXQgYWxs\n"
  697. "-----END MESSAGE-----\n";
  698. static const char correct_encrypted_plaintext[] = "being on mountains, "
  699. "thinking about computers, is not bad at all";
  700. static void
  701. test_parse_hs_desc_superencrypted(void *arg)
  702. {
  703. (void) arg;
  704. size_t retval;
  705. uint8_t *encrypted_out = NULL;
  706. {
  707. setup_full_capture_of_logs(LOG_WARN);
  708. retval = decode_superencrypted(bad_superencrypted_text1,
  709. strlen(bad_superencrypted_text1),
  710. &encrypted_out);
  711. tt_u64_op(retval, OP_EQ, 0);
  712. tt_ptr_op(encrypted_out, OP_EQ, NULL);
  713. expect_log_msg_containing("Unrecognized desc auth type");
  714. teardown_capture_of_logs();
  715. }
  716. {
  717. setup_full_capture_of_logs(LOG_WARN);
  718. retval = decode_superencrypted(bad_superencrypted_text2,
  719. strlen(bad_superencrypted_text2),
  720. &encrypted_out);
  721. tt_u64_op(retval, OP_EQ, 0);
  722. tt_ptr_op(encrypted_out, OP_EQ, NULL);
  723. expect_log_msg_containing("Bogus desc auth key in HS desc");
  724. teardown_capture_of_logs();
  725. }
  726. {
  727. setup_full_capture_of_logs(LOG_WARN);
  728. retval = decode_superencrypted(bad_superencrypted_text3,
  729. strlen(bad_superencrypted_text3),
  730. &encrypted_out);
  731. tt_u64_op(retval, OP_EQ, 0);
  732. tt_ptr_op(encrypted_out, OP_EQ, NULL);
  733. expect_log_msg_containing("Length of descriptor\'s encrypted data "
  734. "is too small.");
  735. teardown_capture_of_logs();
  736. }
  737. /* Now finally the good one */
  738. retval = decode_superencrypted(correct_superencrypted_text,
  739. strlen(correct_superencrypted_text),
  740. &encrypted_out);
  741. tt_u64_op(retval, OP_EQ, strlen(correct_encrypted_plaintext));
  742. tt_mem_op(encrypted_out, OP_EQ, correct_encrypted_plaintext,
  743. strlen(correct_encrypted_plaintext));
  744. done:
  745. tor_free(encrypted_out);
  746. }
  747. struct testcase_t hs_descriptor[] = {
  748. /* Encoding tests. */
  749. { "cert_encoding", test_cert_encoding, TT_FORK,
  750. NULL, NULL },
  751. { "link_specifier", test_link_specifier, TT_FORK,
  752. NULL, NULL },
  753. { "encode_descriptor", test_encode_descriptor, TT_FORK,
  754. NULL, NULL },
  755. { "descriptor_padding", test_descriptor_padding, TT_FORK,
  756. NULL, NULL },
  757. /* Decoding tests. */
  758. { "decode_descriptor", test_decode_descriptor, TT_FORK,
  759. NULL, NULL },
  760. { "encrypted_data_len", test_encrypted_data_len, TT_FORK,
  761. NULL, NULL },
  762. { "decode_invalid_intro_point", test_decode_invalid_intro_point, TT_FORK,
  763. NULL, NULL },
  764. { "decode_plaintext", test_decode_plaintext, TT_FORK,
  765. NULL, NULL },
  766. { "decode_bad_signature", test_decode_bad_signature, TT_FORK,
  767. NULL, NULL },
  768. /* Misc. */
  769. { "version", test_supported_version, TT_FORK,
  770. NULL, NULL },
  771. { "validate_cert", test_validate_cert, TT_FORK,
  772. NULL, NULL },
  773. { "desc_signature", test_desc_signature, TT_FORK,
  774. NULL, NULL },
  775. { "parse_hs_desc_superencrypted", test_parse_hs_desc_superencrypted,
  776. TT_FORK, NULL, NULL },
  777. END_OF_TESTCASES
  778. };