test_hs_descriptor.c 29 KB

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