test_hs_descriptor.c 26 KB

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