test_hs_descriptor.c 37 KB

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