test_hs_descriptor.c 28 KB

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