test_hs_descriptor.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117
  1. /* Copyright (c) 2016, 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. static hs_desc_intro_point_t *
  15. helper_build_intro_point(const ed25519_keypair_t *blinded_kp, time_t now,
  16. const char *addr, int legacy)
  17. {
  18. int ret;
  19. ed25519_keypair_t auth_kp;
  20. hs_desc_intro_point_t *intro_point = NULL;
  21. hs_desc_intro_point_t *ip = tor_malloc_zero(sizeof(*ip));
  22. ip->link_specifiers = smartlist_new();
  23. {
  24. hs_desc_link_specifier_t *ls = tor_malloc_zero(sizeof(*ls));
  25. if (legacy) {
  26. ls->type = LS_LEGACY_ID;
  27. memcpy(ls->u.legacy_id, "0299F268FCA9D55CD157976D39AE92B4B455B3A8",
  28. DIGEST_LEN);
  29. } else {
  30. ls->u.ap.port = 9001;
  31. int family = tor_addr_parse(&ls->u.ap.addr, addr);
  32. switch (family) {
  33. case AF_INET:
  34. ls->type = LS_IPV4;
  35. break;
  36. case AF_INET6:
  37. ls->type = LS_IPV6;
  38. break;
  39. default:
  40. /* Stop the test, not suppose to have an error. */
  41. tt_int_op(family, OP_EQ, AF_INET);
  42. }
  43. }
  44. smartlist_add(ip->link_specifiers, ls);
  45. }
  46. ret = ed25519_keypair_generate(&auth_kp, 0);
  47. tt_int_op(ret, ==, 0);
  48. ip->auth_key_cert = tor_cert_create(blinded_kp, CERT_TYPE_AUTH_HS_IP_KEY,
  49. &auth_kp.pubkey, now,
  50. HS_DESC_CERT_LIFETIME,
  51. CERT_FLAG_INCLUDE_SIGNING_KEY);
  52. tt_assert(ip->auth_key_cert);
  53. if (legacy) {
  54. ip->enc_key.legacy = crypto_pk_new();
  55. ip->enc_key_type = HS_DESC_KEY_TYPE_LEGACY;
  56. tt_assert(ip->enc_key.legacy);
  57. ret = crypto_pk_generate_key(ip->enc_key.legacy);
  58. tt_int_op(ret, ==, 0);
  59. } else {
  60. ret = curve25519_keypair_generate(&ip->enc_key.curve25519, 0);
  61. tt_int_op(ret, ==, 0);
  62. ip->enc_key_type = HS_DESC_KEY_TYPE_CURVE25519;
  63. }
  64. intro_point = ip;
  65. done:
  66. return intro_point;
  67. }
  68. /* Return a valid hs_descriptor_t object. If no_ip is set, no introduction
  69. * points are added. */
  70. static hs_descriptor_t *
  71. helper_build_hs_desc(unsigned int no_ip)
  72. {
  73. int ret;
  74. time_t now = time(NULL);
  75. hs_descriptor_t *descp = NULL, *desc = tor_malloc_zero(sizeof(*desc));
  76. desc->plaintext_data.version = HS_DESC_SUPPORTED_FORMAT_VERSION_MAX;
  77. ret = ed25519_keypair_generate(&desc->plaintext_data.signing_kp, 0);
  78. tt_int_op(ret, ==, 0);
  79. ret = ed25519_keypair_generate(&desc->plaintext_data.blinded_kp, 0);
  80. tt_int_op(ret, ==, 0);
  81. desc->plaintext_data.signing_key_cert =
  82. tor_cert_create(&desc->plaintext_data.blinded_kp,
  83. CERT_TYPE_SIGNING_HS_DESC,
  84. &desc->plaintext_data.signing_kp.pubkey, now,
  85. 3600,
  86. CERT_FLAG_INCLUDE_SIGNING_KEY);
  87. tt_assert(desc->plaintext_data.signing_key_cert);
  88. desc->plaintext_data.revision_counter = 42;
  89. desc->plaintext_data.lifetime_sec = 3 * 60 * 60;
  90. /* Setup encrypted data section. */
  91. desc->encrypted_data.create2_ntor = 1;
  92. desc->encrypted_data.auth_types = smartlist_new();
  93. smartlist_add(desc->encrypted_data.auth_types, tor_strdup("ed25519"));
  94. desc->encrypted_data.intro_points = smartlist_new();
  95. if (!no_ip) {
  96. /* Add four intro points. */
  97. smartlist_add(desc->encrypted_data.intro_points,
  98. helper_build_intro_point(&desc->plaintext_data.blinded_kp, now,
  99. "1.2.3.4", 0));
  100. smartlist_add(desc->encrypted_data.intro_points,
  101. helper_build_intro_point(&desc->plaintext_data.blinded_kp, now,
  102. "[2600::1]", 0));
  103. smartlist_add(desc->encrypted_data.intro_points,
  104. helper_build_intro_point(&desc->plaintext_data.blinded_kp, now,
  105. "3.2.1.4", 1));
  106. smartlist_add(desc->encrypted_data.intro_points,
  107. helper_build_intro_point(&desc->plaintext_data.blinded_kp, now,
  108. "", 1));
  109. }
  110. descp = desc;
  111. done:
  112. return descp;
  113. }
  114. static void
  115. helper_compare_hs_desc(const hs_descriptor_t *desc1,
  116. const hs_descriptor_t *desc2)
  117. {
  118. /* Plaintext data section. */
  119. tt_int_op(desc1->plaintext_data.version, OP_EQ,
  120. desc2->plaintext_data.version);
  121. tt_uint_op(desc1->plaintext_data.lifetime_sec, OP_EQ,
  122. desc2->plaintext_data.lifetime_sec);
  123. tt_assert(tor_cert_eq(desc1->plaintext_data.signing_key_cert,
  124. desc2->plaintext_data.signing_key_cert));
  125. tt_mem_op(desc1->plaintext_data.signing_kp.pubkey.pubkey, OP_EQ,
  126. desc2->plaintext_data.signing_kp.pubkey.pubkey,
  127. ED25519_PUBKEY_LEN);
  128. tt_mem_op(desc1->plaintext_data.blinded_kp.pubkey.pubkey, OP_EQ,
  129. desc2->plaintext_data.blinded_kp.pubkey.pubkey,
  130. ED25519_PUBKEY_LEN);
  131. tt_uint_op(desc1->plaintext_data.revision_counter, ==,
  132. desc2->plaintext_data.revision_counter);
  133. /* NOTE: We can't compare the encrypted blob because when encoding the
  134. * descriptor, the object is immutable thus we don't update it with the
  135. * encrypted blob. As contrast to the decoding process where we populate a
  136. * descriptor object. */
  137. /* Encrypted data section. */
  138. tt_uint_op(desc1->encrypted_data.create2_ntor, ==,
  139. desc2->encrypted_data.create2_ntor);
  140. /* Authentication type. */
  141. tt_int_op(!!desc1->encrypted_data.auth_types, ==,
  142. !!desc2->encrypted_data.auth_types);
  143. if (desc1->encrypted_data.auth_types && desc2->encrypted_data.auth_types) {
  144. tt_int_op(smartlist_len(desc1->encrypted_data.auth_types), ==,
  145. smartlist_len(desc2->encrypted_data.auth_types));
  146. for (int i = 0; i < smartlist_len(desc1->encrypted_data.auth_types); i++) {
  147. tt_str_op(smartlist_get(desc1->encrypted_data.auth_types, i), OP_EQ,
  148. smartlist_get(desc2->encrypted_data.auth_types, i));
  149. }
  150. }
  151. /* Introduction points. */
  152. {
  153. tt_assert(desc1->encrypted_data.intro_points);
  154. tt_assert(desc2->encrypted_data.intro_points);
  155. tt_int_op(smartlist_len(desc1->encrypted_data.intro_points), ==,
  156. smartlist_len(desc2->encrypted_data.intro_points));
  157. for (int i=0; i < smartlist_len(desc1->encrypted_data.intro_points); i++) {
  158. hs_desc_intro_point_t *ip1 = smartlist_get(desc1->encrypted_data
  159. .intro_points, i),
  160. *ip2 = smartlist_get(desc2->encrypted_data
  161. .intro_points, i);
  162. tt_assert(tor_cert_eq(ip1->auth_key_cert, ip2->auth_key_cert));
  163. tt_int_op(ip1->enc_key_type, OP_EQ, ip2->enc_key_type);
  164. tt_assert(ip1->enc_key_type == HS_DESC_KEY_TYPE_LEGACY ||
  165. ip1->enc_key_type == HS_DESC_KEY_TYPE_CURVE25519);
  166. switch (ip1->enc_key_type) {
  167. case HS_DESC_KEY_TYPE_LEGACY:
  168. tt_int_op(crypto_pk_cmp_keys(ip1->enc_key.legacy, ip2->enc_key.legacy),
  169. OP_EQ, 0);
  170. break;
  171. case HS_DESC_KEY_TYPE_CURVE25519:
  172. tt_mem_op(ip1->enc_key.curve25519.pubkey.public_key, OP_EQ,
  173. ip2->enc_key.curve25519.pubkey.public_key,
  174. CURVE25519_PUBKEY_LEN);
  175. break;
  176. }
  177. tt_int_op(smartlist_len(ip1->link_specifiers), ==,
  178. smartlist_len(ip2->link_specifiers));
  179. for (int j = 0; j < smartlist_len(ip1->link_specifiers); j++) {
  180. hs_desc_link_specifier_t *ls1 = smartlist_get(ip1->link_specifiers, j),
  181. *ls2 = smartlist_get(ip2->link_specifiers, j);
  182. tt_int_op(ls1->type, ==, ls2->type);
  183. switch (ls1->type) {
  184. case LS_IPV4:
  185. case LS_IPV6:
  186. {
  187. char *addr1 = tor_addr_to_str_dup(&ls1->u.ap.addr),
  188. *addr2 = tor_addr_to_str_dup(&ls2->u.ap.addr);
  189. tt_str_op(addr1, OP_EQ, addr2);
  190. tor_free(addr1);
  191. tor_free(addr2);
  192. tt_int_op(ls1->u.ap.port, ==, ls2->u.ap.port);
  193. }
  194. break;
  195. case LS_LEGACY_ID:
  196. tt_mem_op(ls1->u.legacy_id, OP_EQ, ls2->u.legacy_id,
  197. sizeof(ls1->u.legacy_id));
  198. break;
  199. default:
  200. /* Unknown type, caught it and print its value. */
  201. tt_int_op(ls1->type, OP_EQ, -1);
  202. }
  203. }
  204. }
  205. }
  206. done:
  207. ;
  208. }
  209. /* Test certificate encoding put in a descriptor. */
  210. static void
  211. test_cert_encoding(void *arg)
  212. {
  213. int ret;
  214. char *encoded = NULL;
  215. time_t now = time(NULL);
  216. ed25519_keypair_t kp;
  217. ed25519_public_key_t signed_key;
  218. ed25519_secret_key_t secret_key;
  219. tor_cert_t *cert = NULL;
  220. (void) arg;
  221. ret = ed25519_keypair_generate(&kp, 0);
  222. tt_int_op(ret, == , 0);
  223. ret = ed25519_secret_key_generate(&secret_key, 0);
  224. tt_int_op(ret, == , 0);
  225. ret = ed25519_public_key_generate(&signed_key, &secret_key);
  226. tt_int_op(ret, == , 0);
  227. cert = tor_cert_create(&kp, CERT_TYPE_SIGNING_AUTH, &signed_key,
  228. now, 3600 * 2, CERT_FLAG_INCLUDE_SIGNING_KEY);
  229. tt_assert(cert);
  230. /* Test the certificate encoding function. */
  231. ret = encode_cert(cert, &encoded);
  232. tt_int_op(ret, ==, 0);
  233. /* Validated the certificate string. */
  234. {
  235. char *end, *pos = encoded;
  236. char *b64_cert, buf[256];
  237. size_t b64_cert_len;
  238. tor_cert_t *parsed_cert;
  239. tt_int_op(strcmpstart(pos, "-----BEGIN ED25519 CERT-----\n"), ==, 0);
  240. pos += strlen("-----BEGIN ED25519 CERT-----\n");
  241. /* Isolate the base64 encoded certificate and try to decode it. */
  242. end = strstr(pos, "-----END ED25519 CERT-----");
  243. tt_assert(end);
  244. b64_cert = pos;
  245. b64_cert_len = end - pos;
  246. ret = base64_decode(buf, sizeof(buf), b64_cert, b64_cert_len);
  247. tt_int_op(ret, >, 0);
  248. /* Parseable? */
  249. parsed_cert = tor_cert_parse((uint8_t *) buf, ret);
  250. tt_assert(parsed_cert);
  251. /* Signature is valid? */
  252. ret = tor_cert_checksig(parsed_cert, &kp.pubkey, now + 10);
  253. tt_int_op(ret, ==, 0);
  254. ret = tor_cert_eq(cert, parsed_cert);
  255. tt_int_op(ret, ==, 1);
  256. /* The cert did have the signing key? */
  257. ret= ed25519_pubkey_eq(&parsed_cert->signing_key, &kp.pubkey);
  258. tt_int_op(ret, ==, 1);
  259. tor_cert_free(parsed_cert);
  260. /* Get to the end part of the certificate. */
  261. pos += b64_cert_len;
  262. tt_int_op(strcmpstart(pos, "-----END ED25519 CERT-----"), ==, 0);
  263. pos += strlen("-----END ED25519 CERT-----");
  264. }
  265. done:
  266. tor_cert_free(cert);
  267. tor_free(encoded);
  268. }
  269. /* Test the descriptor padding. */
  270. static void
  271. test_descriptor_padding(void *arg)
  272. {
  273. char *plaintext;
  274. size_t plaintext_len, padded_len;
  275. uint8_t *padded_plaintext = NULL;
  276. /* Example: if l = 129, the ceiled division gives 2 and then multiplied by 128
  277. * to give 256. With l = 127, ceiled division gives 1 then times 128. */
  278. #define PADDING_EXPECTED_LEN(l) \
  279. CEIL_DIV(l, HS_DESC_PLAINTEXT_PADDING_MULTIPLE) * \
  280. HS_DESC_PLAINTEXT_PADDING_MULTIPLE
  281. (void) arg;
  282. { /* test #1: no padding */
  283. plaintext_len = HS_DESC_PLAINTEXT_PADDING_MULTIPLE;
  284. plaintext = tor_malloc(plaintext_len);
  285. padded_len = build_plaintext_padding(plaintext, plaintext_len,
  286. &padded_plaintext);
  287. tt_assert(padded_plaintext);
  288. tor_free(plaintext);
  289. /* Make sure our padding has been zeroed. */
  290. tt_int_op(tor_mem_is_zero((char *) padded_plaintext + plaintext_len,
  291. padded_len - plaintext_len), OP_EQ, 1);
  292. tor_free(padded_plaintext);
  293. /* Never never have a padded length smaller than the plaintext. */
  294. tt_int_op(padded_len, OP_GE, plaintext_len);
  295. tt_int_op(padded_len, OP_EQ, PADDING_EXPECTED_LEN(plaintext_len));
  296. }
  297. { /* test #2: one byte padding? */
  298. plaintext_len = HS_DESC_PLAINTEXT_PADDING_MULTIPLE - 1;
  299. plaintext = tor_malloc(plaintext_len);
  300. padded_plaintext = NULL;
  301. padded_len = build_plaintext_padding(plaintext, plaintext_len,
  302. &padded_plaintext);
  303. tt_assert(padded_plaintext);
  304. tor_free(plaintext);
  305. /* Make sure our padding has been zeroed. */
  306. tt_int_op(tor_mem_is_zero((char *) padded_plaintext + plaintext_len,
  307. padded_len - plaintext_len), OP_EQ, 1);
  308. tor_free(padded_plaintext);
  309. /* Never never have a padded length smaller than the plaintext. */
  310. tt_int_op(padded_len, OP_GE, plaintext_len);
  311. tt_int_op(padded_len, OP_EQ, PADDING_EXPECTED_LEN(plaintext_len));
  312. }
  313. { /* test #3: Lots more bytes of padding? */
  314. plaintext_len = HS_DESC_PLAINTEXT_PADDING_MULTIPLE + 1;
  315. plaintext = tor_malloc(plaintext_len);
  316. padded_plaintext = NULL;
  317. padded_len = build_plaintext_padding(plaintext, plaintext_len,
  318. &padded_plaintext);
  319. tt_assert(padded_plaintext);
  320. tor_free(plaintext);
  321. /* Make sure our padding has been zeroed. */
  322. tt_int_op(tor_mem_is_zero((char *) padded_plaintext + plaintext_len,
  323. padded_len - plaintext_len), OP_EQ, 1);
  324. tor_free(padded_plaintext);
  325. /* Never never have a padded length smaller than the plaintext. */
  326. tt_int_op(padded_len, OP_GE, plaintext_len);
  327. tt_int_op(padded_len, OP_EQ, PADDING_EXPECTED_LEN(plaintext_len));
  328. }
  329. done:
  330. return;
  331. }
  332. static void
  333. test_link_specifier(void *arg)
  334. {
  335. ssize_t ret;
  336. hs_desc_link_specifier_t spec;
  337. smartlist_t *link_specifiers = smartlist_new();
  338. (void) arg;
  339. /* Always this port. */
  340. spec.u.ap.port = 42;
  341. smartlist_add(link_specifiers, &spec);
  342. /* Test IPv4 for starter. */
  343. {
  344. char *b64, buf[256];
  345. uint32_t ipv4;
  346. link_specifier_t *ls;
  347. spec.type = LS_IPV4;
  348. ret = tor_addr_parse(&spec.u.ap.addr, "1.2.3.4");
  349. tt_int_op(ret, ==, AF_INET);
  350. b64 = encode_link_specifiers(link_specifiers);
  351. tt_assert(b64);
  352. /* Decode it and validate the format. */
  353. ret = base64_decode(buf, sizeof(buf), b64, strlen(b64));
  354. tt_int_op(ret, >, 0);
  355. /* First byte is the number of link specifier. */
  356. tt_int_op(get_uint8(buf), ==, 1);
  357. ret = link_specifier_parse(&ls, (uint8_t *) buf + 1, ret - 1);
  358. tt_int_op(ret, ==, 8);
  359. /* Should be 2 bytes for port and 4 bytes for IPv4. */
  360. tt_int_op(link_specifier_get_ls_len(ls), ==, 6);
  361. ipv4 = link_specifier_get_un_ipv4_addr(ls);
  362. tt_int_op(tor_addr_to_ipv4h(&spec.u.ap.addr), ==, ipv4);
  363. tt_int_op(link_specifier_get_un_ipv4_port(ls), ==, spec.u.ap.port);
  364. link_specifier_free(ls);
  365. tor_free(b64);
  366. }
  367. /* Test IPv6. */
  368. {
  369. char *b64, buf[256];
  370. uint8_t ipv6[16];
  371. link_specifier_t *ls;
  372. spec.type = LS_IPV6;
  373. ret = tor_addr_parse(&spec.u.ap.addr, "[1:2:3:4::]");
  374. tt_int_op(ret, ==, AF_INET6);
  375. b64 = encode_link_specifiers(link_specifiers);
  376. tt_assert(b64);
  377. /* Decode it and validate the format. */
  378. ret = base64_decode(buf, sizeof(buf), b64, strlen(b64));
  379. tt_int_op(ret, >, 0);
  380. /* First byte is the number of link specifier. */
  381. tt_int_op(get_uint8(buf), ==, 1);
  382. ret = link_specifier_parse(&ls, (uint8_t *) buf + 1, ret - 1);
  383. tt_int_op(ret, ==, 20);
  384. /* Should be 2 bytes for port and 16 bytes for IPv6. */
  385. tt_int_op(link_specifier_get_ls_len(ls), ==, 18);
  386. for (unsigned int i = 0; i < sizeof(ipv6); i++) {
  387. ipv6[i] = link_specifier_get_un_ipv6_addr(ls, i);
  388. }
  389. tt_mem_op(tor_addr_to_in6_addr8(&spec.u.ap.addr), ==, ipv6, sizeof(ipv6));
  390. tt_int_op(link_specifier_get_un_ipv6_port(ls), ==, spec.u.ap.port);
  391. link_specifier_free(ls);
  392. tor_free(b64);
  393. }
  394. /* Test legacy. */
  395. {
  396. char *b64, buf[256];
  397. uint8_t *id;
  398. link_specifier_t *ls;
  399. spec.type = LS_LEGACY_ID;
  400. memset(spec.u.legacy_id, 'Y', sizeof(spec.u.legacy_id));
  401. b64 = encode_link_specifiers(link_specifiers);
  402. tt_assert(b64);
  403. /* Decode it and validate the format. */
  404. ret = base64_decode(buf, sizeof(buf), b64, strlen(b64));
  405. tt_int_op(ret, >, 0);
  406. /* First byte is the number of link specifier. */
  407. tt_int_op(get_uint8(buf), ==, 1);
  408. ret = link_specifier_parse(&ls, (uint8_t *) buf + 1, ret - 1);
  409. /* 20 bytes digest + 1 byte type + 1 byte len. */
  410. tt_int_op(ret, ==, 22);
  411. tt_int_op(link_specifier_getlen_un_legacy_id(ls), OP_EQ, DIGEST_LEN);
  412. /* Digest length is 20 bytes. */
  413. tt_int_op(link_specifier_get_ls_len(ls), OP_EQ, DIGEST_LEN);
  414. id = link_specifier_getarray_un_legacy_id(ls);
  415. tt_mem_op(spec.u.legacy_id, OP_EQ, id, DIGEST_LEN);
  416. link_specifier_free(ls);
  417. tor_free(b64);
  418. }
  419. done:
  420. smartlist_free(link_specifiers);
  421. }
  422. static void
  423. test_encode_descriptor(void *arg)
  424. {
  425. int ret;
  426. char *encoded = NULL;
  427. hs_descriptor_t *desc = helper_build_hs_desc(0);
  428. (void) arg;
  429. ret = hs_desc_encode_descriptor(desc, &encoded);
  430. tt_int_op(ret, ==, 0);
  431. tt_assert(encoded);
  432. done:
  433. hs_descriptor_free(desc);
  434. tor_free(encoded);
  435. }
  436. static void
  437. test_decode_descriptor(void *arg)
  438. {
  439. int ret;
  440. char *encoded = NULL;
  441. hs_descriptor_t *desc = helper_build_hs_desc(0);
  442. hs_descriptor_t *decoded = NULL;
  443. hs_descriptor_t *desc_no_ip = NULL;
  444. (void) arg;
  445. /* Give some bad stuff to the decoding function. */
  446. ret = hs_desc_decode_descriptor("hladfjlkjadf", NULL, &decoded);
  447. tt_int_op(ret, OP_EQ, -1);
  448. ret = hs_desc_encode_descriptor(desc, &encoded);
  449. tt_int_op(ret, ==, 0);
  450. tt_assert(encoded);
  451. ret = hs_desc_decode_descriptor(encoded, NULL, &decoded);
  452. tt_int_op(ret, ==, 0);
  453. tt_assert(decoded);
  454. helper_compare_hs_desc(desc, decoded);
  455. /* Decode a descriptor with _no_ introduction points. */
  456. {
  457. desc_no_ip = helper_build_hs_desc(1);
  458. tt_assert(desc_no_ip);
  459. tor_free(encoded);
  460. ret = hs_desc_encode_descriptor(desc_no_ip, &encoded);
  461. tt_int_op(ret, ==, 0);
  462. tt_assert(encoded);
  463. hs_descriptor_free(decoded);
  464. ret = hs_desc_decode_descriptor(encoded, NULL, &decoded);
  465. tt_int_op(ret, ==, 0);
  466. tt_assert(decoded);
  467. }
  468. done:
  469. hs_descriptor_free(desc);
  470. hs_descriptor_free(desc_no_ip);
  471. hs_descriptor_free(decoded);
  472. tor_free(encoded);
  473. }
  474. static void
  475. test_supported_version(void *arg)
  476. {
  477. int ret;
  478. (void) arg;
  479. /* Unsupported. */
  480. ret = hs_desc_is_supported_version(42);
  481. tt_int_op(ret, OP_EQ, 0);
  482. /* To early. */
  483. ret = hs_desc_is_supported_version(HS_DESC_SUPPORTED_FORMAT_VERSION_MIN - 1);
  484. tt_int_op(ret, OP_EQ, 0);
  485. /* One too new. */
  486. ret = hs_desc_is_supported_version(HS_DESC_SUPPORTED_FORMAT_VERSION_MAX + 1);
  487. tt_int_op(ret, OP_EQ, 0);
  488. /* Valid version. */
  489. ret = hs_desc_is_supported_version(3);
  490. tt_int_op(ret, OP_EQ, 1);
  491. done:
  492. ;
  493. }
  494. static void
  495. test_encrypted_data_len(void *arg)
  496. {
  497. int ret;
  498. size_t value;
  499. (void) arg;
  500. /* No length, error. */
  501. ret = encrypted_data_length_is_valid(0);
  502. tt_int_op(ret, OP_EQ, 0);
  503. /* Not a multiple of our encryption algorithm (thus no padding). It's
  504. * suppose to be aligned on HS_DESC_PLAINTEXT_PADDING_MULTIPLE. */
  505. value = HS_DESC_PLAINTEXT_PADDING_MULTIPLE * 10 - 1;
  506. ret = encrypted_data_length_is_valid(value);
  507. tt_int_op(ret, OP_EQ, 0);
  508. /* Valid value. */
  509. value = HS_DESC_PADDED_PLAINTEXT_MAX_LEN + HS_DESC_ENCRYPTED_SALT_LEN +
  510. DIGEST256_LEN;
  511. ret = encrypted_data_length_is_valid(value);
  512. tt_int_op(ret, OP_EQ, 1);
  513. /* XXX: Test maximum possible size. */
  514. done:
  515. ;
  516. }
  517. static void
  518. test_decode_intro_point(void *arg)
  519. {
  520. int ret;
  521. char *encoded_ip = NULL;
  522. size_t len_out;
  523. hs_desc_intro_point_t *ip = NULL;
  524. hs_descriptor_t *desc = NULL;
  525. (void) arg;
  526. /* The following certificate expires in 2036. After that, one of the test
  527. * will fail because of the expiry time. */
  528. /* Seperate pieces of a valid encoded introduction point. */
  529. const char *intro_point =
  530. "introduction-point AQIUMDI5OUYyNjhGQ0E5RDU1Q0QxNTc=";
  531. const char *auth_key =
  532. "auth-key\n"
  533. "-----BEGIN ED25519 CERT-----\n"
  534. "AQkACOhAAQW8ltYZMIWpyrfyE/b4Iyi8CNybCwYs6ADk7XfBaxsFAQAgBAD3/BE4\n"
  535. "XojGE/N2bW/wgnS9r2qlrkydGyuCKIGayYx3haZ39LD4ZTmSMRxwmplMAqzG/XNP\n"
  536. "0Kkpg4p2/VnLFJRdU1SMFo1lgQ4P0bqw7Tgx200fulZ4KUM5z5V7m+a/mgY=\n"
  537. "-----END ED25519 CERT-----";
  538. const char *enc_key =
  539. "enc-key ntor bpZKLsuhxP6woDQ3yVyjm5gUKSk7RjfAijT2qrzbQk0=";
  540. const char *enc_key_legacy =
  541. "enc-key legacy\n"
  542. "-----BEGIN RSA PUBLIC KEY-----\n"
  543. "MIGJAoGBAO4bATcW8kW4h6RQQAKEgg+aXCpF4JwbcO6vGZtzXTDB+HdPVQzwqkbh\n"
  544. "XzFM6VGArhYw4m31wcP1Z7IwULir7UMnAFd7Zi62aYfU6l+Y1yAoZ1wzu1XBaAMK\n"
  545. "ejpwQinW9nzJn7c2f69fVke3pkhxpNdUZ+vplSA/l9iY+y+v+415AgMBAAE=\n"
  546. "-----END RSA PUBLIC KEY-----";
  547. const char *enc_key_cert =
  548. "enc-key-certification\n"
  549. "-----BEGIN ED25519 CERT-----\n"
  550. "AQsACOhZAUpNvCZ1aJaaR49lS6MCdsVkhVGVrRqoj0Y2T4SzroAtAQAgBABFOcGg\n"
  551. "lbTt1DF5nKTE/gU3Fr8ZtlCIOhu1A+F5LM7fqCUupfesg0KTHwyIZOYQbJuM5/he\n"
  552. "/jDNyLy9woPJdjkxywaY2RPUxGjLYtMQV0E8PUxWyICV+7y52fTCYaKpYQw=\n"
  553. "-----END ED25519 CERT-----";
  554. const char *enc_key_cert_legacy =
  555. "enc-key-certification\n"
  556. "-----BEGIN CROSSCERT-----\n"
  557. "Sk28JnVolppHj2VLowJ2xWSFUZWtGqiPRjZPhLOugC0ACOhZgFPA5egeRDUXMM1U\n"
  558. "Fn3c7Je0gJS6mVma5FzwlgwggeriF13UZcaT71vEAN/ZJXbxOfQVGMZ0rXuFpjUq\n"
  559. "C8CvqmZIwEUaPE1nDFtmnTcucvNS1YQl9nsjH3ejbxc+4yqps/cXh46FmXsm5yz7\n"
  560. "NZjBM9U1fbJhlNtOvrkf70K8bLk6\n"
  561. "-----END CROSSCERT-----";
  562. (void) enc_key_legacy;
  563. (void) enc_key_cert_legacy;
  564. /* Start by testing the "decode all intro points" function. */
  565. {
  566. char *line;
  567. desc = helper_build_hs_desc(0);
  568. tt_assert(desc);
  569. /* Only try to decode an incomplete introduction point section. */
  570. tor_asprintf(&line, "\n%s", intro_point);
  571. ret = decode_intro_points(desc, &desc->encrypted_data, line);
  572. tor_free(line);
  573. tt_int_op(ret, ==, -1);
  574. /* Decode one complete intro point. */
  575. smartlist_t *lines = smartlist_new();
  576. smartlist_add(lines, (char *) intro_point);
  577. smartlist_add(lines, (char *) auth_key);
  578. smartlist_add(lines, (char *) enc_key);
  579. smartlist_add(lines, (char *) enc_key_cert);
  580. encoded_ip = smartlist_join_strings(lines, "\n", 0, &len_out);
  581. tt_assert(encoded_ip);
  582. tor_asprintf(&line, "\n%s", encoded_ip);
  583. tor_free(encoded_ip);
  584. ret = decode_intro_points(desc, &desc->encrypted_data, line);
  585. tor_free(line);
  586. smartlist_free(lines);
  587. tt_int_op(ret, ==, 0);
  588. }
  589. /* Try to decode a junk string. */
  590. {
  591. hs_descriptor_free(desc);
  592. desc = helper_build_hs_desc(0);
  593. const char *junk = "this is not a descriptor";
  594. ip = decode_introduction_point(desc, junk);
  595. tt_assert(!ip);
  596. desc_intro_point_free(ip);
  597. ip = NULL;
  598. }
  599. /* Invalid link specifiers. */
  600. {
  601. smartlist_t *lines = smartlist_new();
  602. const char *bad_line = "introduction-point blah";
  603. smartlist_add(lines, (char *) bad_line);
  604. smartlist_add(lines, (char *) auth_key);
  605. smartlist_add(lines, (char *) enc_key);
  606. smartlist_add(lines, (char *) enc_key_cert);
  607. encoded_ip = smartlist_join_strings(lines, "\n", 0, &len_out);
  608. tt_assert(encoded_ip);
  609. ip = decode_introduction_point(desc, encoded_ip);
  610. tt_assert(!ip);
  611. tor_free(encoded_ip);
  612. smartlist_free(lines);
  613. desc_intro_point_free(ip);
  614. ip = NULL;
  615. }
  616. /* Invalid auth key type. */
  617. {
  618. smartlist_t *lines = smartlist_new();
  619. /* Try to put a valid object that our tokenize function will be able to
  620. * parse but that has nothing to do with the auth_key. */
  621. const char *bad_line =
  622. "auth-key\n"
  623. "-----BEGIN UNICORN CERT-----\n"
  624. "MIGJAoGBAO4bATcW8kW4h6RQQAKEgg+aXCpF4JwbcO6vGZtzXTDB+HdPVQzwqkbh\n"
  625. "XzFM6VGArhYw4m31wcP1Z7IwULir7UMnAFd7Zi62aYfU6l+Y1yAoZ1wzu1XBaAMK\n"
  626. "ejpwQinW9nzJn7c2f69fVke3pkhxpNdUZ+vplSA/l9iY+y+v+415AgMBAAE=\n"
  627. "-----END UNICORN CERT-----";
  628. /* Build intro point text. */
  629. smartlist_add(lines, (char *) intro_point);
  630. smartlist_add(lines, (char *) bad_line);
  631. smartlist_add(lines, (char *) enc_key);
  632. smartlist_add(lines, (char *) enc_key_cert);
  633. encoded_ip = smartlist_join_strings(lines, "\n", 0, &len_out);
  634. tt_assert(encoded_ip);
  635. ip = decode_introduction_point(desc, encoded_ip);
  636. tt_assert(!ip);
  637. tor_free(encoded_ip);
  638. smartlist_free(lines);
  639. }
  640. /* Invalid enc-key. */
  641. {
  642. smartlist_t *lines = smartlist_new();
  643. const char *bad_line =
  644. "enc-key unicorn bpZKLsuhxP6woDQ3yVyjm5gUKSk7RjfAijT2qrzbQk0=";
  645. /* Build intro point text. */
  646. smartlist_add(lines, (char *) intro_point);
  647. smartlist_add(lines, (char *) auth_key);
  648. smartlist_add(lines, (char *) bad_line);
  649. smartlist_add(lines, (char *) enc_key_cert);
  650. encoded_ip = smartlist_join_strings(lines, "\n", 0, &len_out);
  651. tt_assert(encoded_ip);
  652. ip = decode_introduction_point(desc, encoded_ip);
  653. tt_assert(!ip);
  654. tor_free(encoded_ip);
  655. smartlist_free(lines);
  656. }
  657. /* Invalid enc-key object. */
  658. {
  659. smartlist_t *lines = smartlist_new();
  660. const char *bad_line = "enc-key ntor";
  661. /* Build intro point text. */
  662. smartlist_add(lines, (char *) intro_point);
  663. smartlist_add(lines, (char *) auth_key);
  664. smartlist_add(lines, (char *) bad_line);
  665. smartlist_add(lines, (char *) enc_key_cert);
  666. encoded_ip = smartlist_join_strings(lines, "\n", 0, &len_out);
  667. tt_assert(encoded_ip);
  668. ip = decode_introduction_point(desc, encoded_ip);
  669. tt_assert(!ip);
  670. tor_free(encoded_ip);
  671. smartlist_free(lines);
  672. }
  673. /* Invalid enc-key base64 curv25519 key. */
  674. {
  675. smartlist_t *lines = smartlist_new();
  676. const char *bad_line = "enc-key ntor blah===";
  677. /* Build intro point text. */
  678. smartlist_add(lines, (char *) intro_point);
  679. smartlist_add(lines, (char *) auth_key);
  680. smartlist_add(lines, (char *) bad_line);
  681. smartlist_add(lines, (char *) enc_key_cert);
  682. encoded_ip = smartlist_join_strings(lines, "\n", 0, &len_out);
  683. tt_assert(encoded_ip);
  684. ip = decode_introduction_point(desc, encoded_ip);
  685. tt_assert(!ip);
  686. tor_free(encoded_ip);
  687. smartlist_free(lines);
  688. }
  689. /* Invalid enc-key invalid legacy. */
  690. {
  691. smartlist_t *lines = smartlist_new();
  692. const char *bad_line = "enc-key legacy blah===";
  693. /* Build intro point text. */
  694. smartlist_add(lines, (char *) intro_point);
  695. smartlist_add(lines, (char *) auth_key);
  696. smartlist_add(lines, (char *) bad_line);
  697. smartlist_add(lines, (char *) enc_key_cert);
  698. encoded_ip = smartlist_join_strings(lines, "\n", 0, &len_out);
  699. tt_assert(encoded_ip);
  700. ip = decode_introduction_point(desc, encoded_ip);
  701. tt_assert(!ip);
  702. tor_free(encoded_ip);
  703. smartlist_free(lines);
  704. }
  705. /* Valid object. */
  706. {
  707. smartlist_t *lines = smartlist_new();
  708. /* Build intro point text. */
  709. smartlist_add(lines, (char *) intro_point);
  710. smartlist_add(lines, (char *) auth_key);
  711. smartlist_add(lines, (char *) enc_key);
  712. smartlist_add(lines, (char *) enc_key_cert);
  713. encoded_ip = smartlist_join_strings(lines, "\n", 0, &len_out);
  714. tt_assert(encoded_ip);
  715. ip = decode_introduction_point(desc, encoded_ip);
  716. tt_assert(ip);
  717. tor_free(encoded_ip);
  718. smartlist_free(lines);
  719. }
  720. done:
  721. hs_descriptor_free(desc);
  722. desc_intro_point_free(ip);
  723. }
  724. const char encrypted_desc_portion[] = "create2-formats 2\n"
  725. "authentication-required ed25519\n"
  726. "introduction-point AQAGAQIDBCMp\n"
  727. "auth-key\n"
  728. "-----BEGIN ED25519 CERT-----\n"
  729. "AQkABmRZASMANx4sbMyDd4i+MciVUw29vPQ/nOFrLwUdTGEBXSXrAQAgBABo2zfd\n"
  730. "wyqAdzSeaIzH1TUcV3u8nAG2YhNCRw2/2vVWuD6Z4Fn0aNHnh1FONNkbismC9t1X\n"
  731. "Rf07hdZkVYEbOaPsHnFwhJULVSUo8YYuL19jghRjwMqPGeGfD4iuQqdo3QA=\n"
  732. "-----END ED25519 CERT-----\n"
  733. "enc-key ntor xo2n5anLMoyIMuhcKSLdVZISyISBW8j1vXRbpdbK+lU=\n"
  734. "enc-key-certification\n"
  735. "-----BEGIN ED25519 CERT-----\n"
  736. "AQsABmRZATUYQypFY7pr8FpmV61pcqUylt5fEr/QLfavfcwbzlA7AQAgBADSI5Ie\n"
  737. "Ekdy+qeHngLmz6Gr7fQ5xvilhxB91UDIjwRfP0ufoVF+HalsyXKskYvcYhH67+lw\n"
  738. "D947flCHzeJyfAT38jO/Cw42qM7H+SObBMcsTB93J0lPNBy4OHosH9ybtwA=\n"
  739. "-----END ED25519 CERT-----\n"
  740. "introduction-point AQESJgAAAAAAAAAAAAAAAAAAASMp\n"
  741. "auth-key\n"
  742. "-----BEGIN ED25519 CERT-----\n"
  743. "AQkABmRZAVdPeZyzfCyUDC1fnYyom8eOS2O1opzTytEU7dlOf9brAQAgBABo2zfd\n"
  744. "wyqAdzSeaIzH1TUcV3u8nAG2YhNCRw2/2vVWuHVSGTrO1EM6Eu1jyOw/qtSS6Exf\n"
  745. "omV417y8uK2gHQ+1FWqg/KaogELYzDG6pcj2NkziovnIfET0W7nZB85YjwQ=\n"
  746. "-----END ED25519 CERT-----\n"
  747. "enc-key ntor MbxzxI1K+zcl7e+wysLK96UZWwFEJQqI0G7b0muRXx4=\n"
  748. "enc-key-certification\n"
  749. "-----BEGIN ED25519 CERT-----\n"
  750. "AQsABmRZATUYQypFY7pr8FpmV61pcqUylt5fEr/QLfavfcwbzlA7AQAgBADimELh\n"
  751. "lLZvy/LjXnCdpvaVRhiGBeIRAGIDGz1SY/zD6BAnpDL420ha2TdvdGsg8cgfTcJZ\n"
  752. "g84x85+zhuh8kkdgt7bOmjOXLlButDCfTarMgCfy6pSI/hUckk+j5Q43uws=\n"
  753. "-----END ED25519 CERT-----\n"
  754. "introduction-point AQIUMDI5OUYyNjhGQ0E5RDU1Q0QxNTc=\n"
  755. "auth-key\n"
  756. "-----BEGIN ED25519 CERT-----\n"
  757. "AQkABmRZASnpBjHsw0Gpvi+KNlW4ouXegIsUBHMvJN1CQHDTLdfnAQAgBABo2zfd\n"
  758. "wyqAdzSeaIzH1TUcV3u8nAG2YhNCRw2/2vVWuOlbHs8s8LAeEb36urVKTJ5exgss\n"
  759. "V+ylIwHSWF0qanCnnTnDyNg/3YRUo0AZr0d/CoiNV+XsGE4Vuho/TBVC+wY=\n"
  760. "-----END ED25519 CERT-----\n"
  761. "enc-key legacy\n"
  762. "-----BEGIN RSA PUBLIC KEY-----\n"
  763. "MIGJAoGBALttUA1paMCQiuIZeCp26REztziej5dN0o6/kTU//ItT4MGxTfmnLmcq\n"
  764. "WpvK4jdX1h2OlDCZmtA7sb0HOkjELgrDU0ATVwOaeG+3icSddmQyaeT8+cxQEktj\n"
  765. "SXMQ+iJDxJIIWFPmLmWWQHqb4IRfl021l3iTErhtZKBz37JNK7E/AgMBAAE=\n"
  766. "-----END RSA PUBLIC KEY-----\n"
  767. "enc-key-certification\n"
  768. "-----BEGIN CROSSCERT-----\n"
  769. "NRhDKkVjumvwWmZXrWlypTKW3l8Sv9At9q99zBvOUDsABmRZgBROMZr2Mhj8H8zd\n"
  770. "xbU6ZvDUwD9xkptNHq0W04CyWb8p0y56y89y2kBF6RrSrVBJCyaHyph6Bmi5z0Lc\n"
  771. "f4jjakRlHwB7oYqSo7l8EE9DGE0rEat3hNhN+tBIAJL5gKOL4dgfD5gMi51zzSFl\n"
  772. "epv8idTwhqZ/sxRMUIQrb9AB8sOD\n"
  773. "-----END CROSSCERT-----\n"
  774. "introduction-point AQIUMDI5OUYyNjhGQ0E5RDU1Q0QxNTc=\n"
  775. "auth-key\n"
  776. "-----BEGIN ED25519 CERT-----\n"
  777. "AQkABmRZAdBFQcE23cIoCMFTycnQ1st2752vdjGME+QPMTTxvqZhAQAgBABo2zfd\n"
  778. "wyqAdzSeaIzH1TUcV3u8nAG2YhNCRw2/2vVWuOGXGPnb3g9J8aSyN7jYs71ET0wC\n"
  779. "TlDLcXCgAMnKA6of/a4QceFfAFsCnI3qCd8YUo5NYCMh2d5mtFpLK41Wpwo=\n"
  780. "-----END ED25519 CERT-----\n"
  781. "enc-key legacy\n"
  782. "-----BEGIN RSA PUBLIC KEY-----\n"
  783. "MIGJAoGBALuyEVMz4GwZ8LnBYxLZDHNg1DHUZJZNmE7HsQDcM/FYeZ1LjYLe/K8s\n"
  784. "BFzgFmjMU1ondIWGWpRCLYcZxQMZaSU0ObdezDwelTkHo/u7K2fQTLmI9EofcsK0\n"
  785. "4OkY6eo8BFtXXoQJhAw5WatRpzah2sGqMPXs2jr7Ku4Pd8JuRd35AgMBAAE=\n"
  786. "-----END RSA PUBLIC KEY-----\n"
  787. "enc-key-certification\n"
  788. "-----BEGIN CROSSCERT-----\n"
  789. "NRhDKkVjumvwWmZXrWlypTKW3l8Sv9At9q99zBvOUDsABmRZgGwpo67ybC7skFYk\n"
  790. "JjvqcbrKg8Fwrvue9yF66p1O90fqziVsvpKGcsr3tcIJHtNsrWVRDpyFwnc1wlVE\n"
  791. "O7rHftF4GUsKaoz3wxxmb0YyyYVQvLpH0Y6lFIvw8nGurnsMefQWLcxuEX7xZOPl\n"
  792. "VAlVp+XtJE1ZNQ62hpnNgBDi1ikJ\n"
  793. "-----END CROSSCERT-----";
  794. static void
  795. test_decode_multiple_intro_points(void *arg)
  796. {
  797. int ret;
  798. hs_descriptor_t *desc = NULL;
  799. /* XXXX test is broken! Assumes that signing key is as hardcoded in
  800. * crosscert code above. */
  801. if (1)
  802. tt_skip();
  803. (void) arg;
  804. {
  805. /* Build a descriptor with no intro points. */
  806. desc = helper_build_hs_desc(1);
  807. tt_assert(desc);
  808. }
  809. ret = decode_intro_points(desc, &desc->encrypted_data,
  810. encrypted_desc_portion);
  811. tt_int_op(ret, ==, 0);
  812. tt_int_op(smartlist_len(desc->encrypted_data.intro_points), ==, 4);
  813. done:
  814. ;
  815. }
  816. static void
  817. test_decode_plaintext(void *arg)
  818. {
  819. int ret;
  820. hs_desc_plaintext_data_t desc_plaintext;
  821. const char *bad_value = "unicorn";
  822. (void) arg;
  823. #define template \
  824. "hs-descriptor %s\n" \
  825. "descriptor-lifetime %s\n" \
  826. "descriptor-signing-key-cert\n" \
  827. "-----BEGIN ED25519 CERT-----\n" \
  828. "AQgABjvPAQaG3g+dc6oV/oJV4ODAtkvx56uBnPtBT9mYVuHVOhn7AQAgBABUg3mQ\n" \
  829. "myBr4bu5LCr53wUEbW2EXui01CbUgU7pfo9LvJG3AcXRojj6HlfsUs9BkzYzYdjF\n" \
  830. "A69Apikgu0ewHYkFFASt7Il+gB3w6J8YstQJZT7dtbtl+doM7ug8B68Qdg8=\n" \
  831. "-----END ED25519 CERT-----\n" \
  832. "revision-counter %s\n" \
  833. "encrypted\n" \
  834. "-----BEGIN %s-----\n" \
  835. "UNICORN\n" \
  836. "-----END MESSAGE-----\n" \
  837. "signature m20WJH5agqvwhq7QeuEZ1mYyPWQDO+eJOZUjLhAiKu8DbL17DsDfJE6kXbWy" \
  838. "HimbNj2we0enV3cCOOAsmPOaAw\n"
  839. /* Invalid version. */
  840. {
  841. char *plaintext;
  842. tor_asprintf(&plaintext, template, bad_value, "180", "42", "MESSAGE");
  843. ret = hs_desc_decode_plaintext(plaintext, &desc_plaintext);
  844. tor_free(plaintext);
  845. tt_int_op(ret, OP_EQ, -1);
  846. }
  847. /* Missing fields. */
  848. {
  849. const char *plaintext = "hs-descriptor 3\n";
  850. ret = hs_desc_decode_plaintext(plaintext, &desc_plaintext);
  851. tt_int_op(ret, OP_EQ, -1);
  852. }
  853. /* Max length. */
  854. {
  855. size_t big = 64000;
  856. /* Must always be bigger than HS_DESC_MAX_LEN. */
  857. tt_int_op(HS_DESC_MAX_LEN, <, big);
  858. char *plaintext = tor_malloc_zero(big);
  859. memset(plaintext, 'a', big);
  860. plaintext[big - 1] = '\0';
  861. ret = hs_desc_decode_plaintext(plaintext, &desc_plaintext);
  862. tor_free(plaintext);
  863. tt_int_op(ret, OP_EQ, -1);
  864. }
  865. /* Bad lifetime value. */
  866. {
  867. char *plaintext;
  868. tor_asprintf(&plaintext, template, "3", bad_value, "42", "MESSAGE");
  869. ret = hs_desc_decode_plaintext(plaintext, &desc_plaintext);
  870. tt_int_op(ret, OP_EQ, -1);
  871. }
  872. /* Huge lifetime value. */
  873. {
  874. char *plaintext;
  875. tor_asprintf(&plaintext, template, "3", "7181615", "42", "MESSAGE");
  876. ret = hs_desc_decode_plaintext(plaintext, &desc_plaintext);
  877. tt_int_op(ret, OP_EQ, -1);
  878. }
  879. /* Invalid encrypted section. */
  880. {
  881. char *plaintext;
  882. tor_asprintf(&plaintext, template, "3", "180", "42", bad_value);
  883. ret = hs_desc_decode_plaintext(plaintext, &desc_plaintext);
  884. tt_int_op(ret, OP_EQ, -1);
  885. }
  886. /* Invalid revision counter. */
  887. {
  888. char *plaintext;
  889. tor_asprintf(&plaintext, template, "3", "180", bad_value, "MESSAGE");
  890. ret = hs_desc_decode_plaintext(plaintext, &desc_plaintext);
  891. tt_int_op(ret, OP_EQ, -1);
  892. }
  893. done:
  894. ;
  895. }
  896. static void
  897. test_validate_cert(void *arg)
  898. {
  899. int ret;
  900. time_t now = time(NULL);
  901. ed25519_keypair_t kp;
  902. tor_cert_t *cert = NULL;
  903. (void) arg;
  904. ret = ed25519_keypair_generate(&kp, 0);
  905. tt_int_op(ret, ==, 0);
  906. /* Cert of type CERT_TYPE_AUTH_HS_IP_KEY. */
  907. cert = tor_cert_create(&kp, CERT_TYPE_AUTH_HS_IP_KEY,
  908. &kp.pubkey, now, 3600,
  909. CERT_FLAG_INCLUDE_SIGNING_KEY);
  910. tt_assert(cert);
  911. /* Test with empty certificate. */
  912. ret = cert_is_valid(NULL, CERT_TYPE_AUTH_HS_IP_KEY, "unicorn");
  913. tt_int_op(ret, OP_EQ, 0);
  914. /* Test with a bad type. */
  915. ret = cert_is_valid(cert, CERT_TYPE_SIGNING_HS_DESC, "unicorn");
  916. tt_int_op(ret, OP_EQ, 0);
  917. /* Normal validation. */
  918. ret = cert_is_valid(cert, CERT_TYPE_AUTH_HS_IP_KEY, "unicorn");
  919. tt_int_op(ret, OP_EQ, 1);
  920. /* Break signing key so signature verification will fails. */
  921. memset(&cert->signing_key, 0, sizeof(cert->signing_key));
  922. ret = cert_is_valid(cert, CERT_TYPE_AUTH_HS_IP_KEY, "unicorn");
  923. tt_int_op(ret, OP_EQ, 0);
  924. tor_cert_free(cert);
  925. /* Try a cert without including the signing key. */
  926. cert = tor_cert_create(&kp, CERT_TYPE_AUTH_HS_IP_KEY, &kp.pubkey, now,
  927. 3600, 0);
  928. tt_assert(cert);
  929. /* Test with a bad type. */
  930. ret = cert_is_valid(cert, CERT_TYPE_AUTH_HS_IP_KEY, "unicorn");
  931. tt_int_op(ret, OP_EQ, 0);
  932. done:
  933. tor_cert_free(cert);
  934. }
  935. static void
  936. test_desc_signature(void *arg)
  937. {
  938. int ret;
  939. char *data, *desc;
  940. char sig_b64[ED25519_SIG_BASE64_LEN + 1];
  941. ed25519_keypair_t kp;
  942. ed25519_signature_t sig;
  943. (void) arg;
  944. ed25519_keypair_generate(&kp, 0);
  945. /* Setup a phoony descriptor but with a valid signature token that is the
  946. * signature is verifiable. */
  947. tor_asprintf(&data, "This is a signed descriptor\n");
  948. ret = ed25519_sign_prefixed(&sig, (const uint8_t *) data, strlen(data),
  949. "Tor onion service descriptor sig v3", &kp);
  950. tt_int_op(ret, ==, 0);
  951. ret = ed25519_signature_to_base64(sig_b64, &sig);
  952. tt_int_op(ret, ==, 0);
  953. /* Build the descriptor that should be valid. */
  954. tor_asprintf(&desc, "%ssignature %s\n", data, sig_b64);
  955. ret = desc_sig_is_valid(sig_b64, &kp, desc, strlen(desc));
  956. tt_int_op(ret, ==, 1);
  957. /* Junk signature. */
  958. ret = desc_sig_is_valid("JUNK", &kp, desc, strlen(desc));
  959. tt_int_op(ret, ==, 0);
  960. done:
  961. tor_free(desc);
  962. tor_free(data);
  963. }
  964. struct testcase_t hs_descriptor[] = {
  965. /* Encoding tests. */
  966. { "cert_encoding", test_cert_encoding, TT_FORK,
  967. NULL, NULL },
  968. { "link_specifier", test_link_specifier, TT_FORK,
  969. NULL, NULL },
  970. { "encode_descriptor", test_encode_descriptor, TT_FORK,
  971. NULL, NULL },
  972. { "descriptor_padding", test_descriptor_padding, TT_FORK,
  973. NULL, NULL },
  974. /* Decoding tests. */
  975. { "decode_descriptor", test_decode_descriptor, TT_FORK,
  976. NULL, NULL },
  977. { "encrypted_data_len", test_encrypted_data_len, TT_FORK,
  978. NULL, NULL },
  979. { "decode_intro_point", test_decode_intro_point, TT_FORK,
  980. NULL, NULL },
  981. { "decode_multiple_intro_points", test_decode_multiple_intro_points, TT_FORK,
  982. NULL, NULL },
  983. { "decode_plaintext", test_decode_plaintext, TT_FORK,
  984. NULL, NULL },
  985. /* Misc. */
  986. { "version", test_supported_version, TT_FORK,
  987. NULL, NULL },
  988. { "validate_cert", test_validate_cert, TT_FORK,
  989. NULL, NULL },
  990. { "desc_signature", test_desc_signature, TT_FORK,
  991. NULL, NULL },
  992. END_OF_TESTCASES
  993. };