test_hs_descriptor.c 27 KB

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