test_hs_descriptor.c 28 KB

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