test_hs_descriptor.c 26 KB

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