test_hs_descriptor.c 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022
  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. desc->encrypted_data.single_onion_service = 1;
  94. smartlist_add(desc->encrypted_data.auth_types, tor_strdup("ed25519"));
  95. desc->encrypted_data.intro_points = smartlist_new();
  96. if (!no_ip) {
  97. /* Add four intro points. */
  98. smartlist_add(desc->encrypted_data.intro_points,
  99. helper_build_intro_point(&desc->plaintext_data.blinded_kp, now,
  100. "1.2.3.4", 0));
  101. smartlist_add(desc->encrypted_data.intro_points,
  102. helper_build_intro_point(&desc->plaintext_data.blinded_kp, now,
  103. "[2600::1]", 0));
  104. smartlist_add(desc->encrypted_data.intro_points,
  105. helper_build_intro_point(&desc->plaintext_data.blinded_kp, now,
  106. "3.2.1.4", 1));
  107. smartlist_add(desc->encrypted_data.intro_points,
  108. helper_build_intro_point(&desc->plaintext_data.blinded_kp, now,
  109. "", 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_kp.pubkey.pubkey, OP_EQ,
  128. desc2->plaintext_data.signing_kp.pubkey.pubkey,
  129. ED25519_PUBKEY_LEN);
  130. tt_mem_op(desc1->plaintext_data.blinded_kp.pubkey.pubkey, OP_EQ,
  131. desc2->plaintext_data.blinded_kp.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.auth_types, ==,
  144. !!desc2->encrypted_data.auth_types);
  145. if (desc1->encrypted_data.auth_types && desc2->encrypted_data.auth_types) {
  146. tt_int_op(smartlist_len(desc1->encrypted_data.auth_types), ==,
  147. smartlist_len(desc2->encrypted_data.auth_types));
  148. for (int i = 0; i < smartlist_len(desc1->encrypted_data.auth_types); i++) {
  149. tt_str_op(smartlist_get(desc1->encrypted_data.auth_types, i), OP_EQ,
  150. smartlist_get(desc2->encrypted_data.auth_types, i));
  151. }
  152. }
  153. /* Introduction points. */
  154. {
  155. tt_assert(desc1->encrypted_data.intro_points);
  156. tt_assert(desc2->encrypted_data.intro_points);
  157. tt_int_op(smartlist_len(desc1->encrypted_data.intro_points), ==,
  158. smartlist_len(desc2->encrypted_data.intro_points));
  159. for (int i=0; i < smartlist_len(desc1->encrypted_data.intro_points); i++) {
  160. hs_desc_intro_point_t *ip1 = smartlist_get(desc1->encrypted_data
  161. .intro_points, i),
  162. *ip2 = smartlist_get(desc2->encrypted_data
  163. .intro_points, i);
  164. tt_assert(tor_cert_eq(ip1->auth_key_cert, ip2->auth_key_cert));
  165. tt_int_op(ip1->enc_key_type, OP_EQ, ip2->enc_key_type);
  166. tt_assert(ip1->enc_key_type == HS_DESC_KEY_TYPE_LEGACY ||
  167. ip1->enc_key_type == HS_DESC_KEY_TYPE_CURVE25519);
  168. switch (ip1->enc_key_type) {
  169. case HS_DESC_KEY_TYPE_LEGACY:
  170. tt_int_op(crypto_pk_cmp_keys(ip1->enc_key.legacy, ip2->enc_key.legacy),
  171. OP_EQ, 0);
  172. break;
  173. case HS_DESC_KEY_TYPE_CURVE25519:
  174. tt_mem_op(ip1->enc_key.curve25519.pubkey.public_key, OP_EQ,
  175. ip2->enc_key.curve25519.pubkey.public_key,
  176. CURVE25519_PUBKEY_LEN);
  177. break;
  178. }
  179. tt_int_op(smartlist_len(ip1->link_specifiers), ==,
  180. smartlist_len(ip2->link_specifiers));
  181. for (int j = 0; j < smartlist_len(ip1->link_specifiers); j++) {
  182. hs_desc_link_specifier_t *ls1 = smartlist_get(ip1->link_specifiers, j),
  183. *ls2 = smartlist_get(ip2->link_specifiers, j);
  184. tt_int_op(ls1->type, ==, ls2->type);
  185. switch (ls1->type) {
  186. case LS_IPV4:
  187. case LS_IPV6:
  188. {
  189. addr1 = tor_addr_to_str_dup(&ls1->u.ap.addr);
  190. addr2 = tor_addr_to_str_dup(&ls2->u.ap.addr);
  191. tt_str_op(addr1, OP_EQ, addr2);
  192. tor_free(addr1);
  193. tor_free(addr2);
  194. tt_int_op(ls1->u.ap.port, ==, ls2->u.ap.port);
  195. }
  196. break;
  197. case LS_LEGACY_ID:
  198. tt_mem_op(ls1->u.legacy_id, OP_EQ, ls2->u.legacy_id,
  199. sizeof(ls1->u.legacy_id));
  200. break;
  201. default:
  202. /* Unknown type, caught it and print its value. */
  203. tt_int_op(ls1->type, OP_EQ, -1);
  204. }
  205. }
  206. }
  207. }
  208. done:
  209. tor_free(addr1);
  210. tor_free(addr2);
  211. }
  212. /* Test certificate encoding put in a descriptor. */
  213. static void
  214. test_cert_encoding(void *arg)
  215. {
  216. int ret;
  217. char *encoded = NULL;
  218. time_t now = time(NULL);
  219. ed25519_keypair_t kp;
  220. ed25519_public_key_t signed_key;
  221. ed25519_secret_key_t secret_key;
  222. tor_cert_t *cert = NULL;
  223. (void) arg;
  224. ret = ed25519_keypair_generate(&kp, 0);
  225. tt_int_op(ret, == , 0);
  226. ret = ed25519_secret_key_generate(&secret_key, 0);
  227. tt_int_op(ret, == , 0);
  228. ret = ed25519_public_key_generate(&signed_key, &secret_key);
  229. tt_int_op(ret, == , 0);
  230. cert = tor_cert_create(&kp, CERT_TYPE_SIGNING_AUTH, &signed_key,
  231. now, 3600 * 2, CERT_FLAG_INCLUDE_SIGNING_KEY);
  232. tt_assert(cert);
  233. /* Test the certificate encoding function. */
  234. ret = tor_cert_encode_ed22519(cert, &encoded);
  235. tt_int_op(ret, ==, 0);
  236. /* Validated the certificate string. */
  237. {
  238. char *end, *pos = encoded;
  239. char *b64_cert, buf[256];
  240. size_t b64_cert_len;
  241. tor_cert_t *parsed_cert;
  242. tt_int_op(strcmpstart(pos, "-----BEGIN ED25519 CERT-----\n"), ==, 0);
  243. pos += strlen("-----BEGIN ED25519 CERT-----\n");
  244. /* Isolate the base64 encoded certificate and try to decode it. */
  245. end = strstr(pos, "-----END ED25519 CERT-----");
  246. tt_assert(end);
  247. b64_cert = pos;
  248. b64_cert_len = end - pos;
  249. ret = base64_decode(buf, sizeof(buf), b64_cert, b64_cert_len);
  250. tt_int_op(ret, >, 0);
  251. /* Parseable? */
  252. parsed_cert = tor_cert_parse((uint8_t *) buf, ret);
  253. tt_assert(parsed_cert);
  254. /* Signature is valid? */
  255. ret = tor_cert_checksig(parsed_cert, &kp.pubkey, now + 10);
  256. tt_int_op(ret, ==, 0);
  257. ret = tor_cert_eq(cert, parsed_cert);
  258. tt_int_op(ret, ==, 1);
  259. /* The cert did have the signing key? */
  260. ret= ed25519_pubkey_eq(&parsed_cert->signing_key, &kp.pubkey);
  261. tt_int_op(ret, ==, 1);
  262. tor_cert_free(parsed_cert);
  263. /* Get to the end part of the certificate. */
  264. pos += b64_cert_len;
  265. tt_int_op(strcmpstart(pos, "-----END ED25519 CERT-----"), ==, 0);
  266. pos += strlen("-----END ED25519 CERT-----");
  267. }
  268. done:
  269. tor_cert_free(cert);
  270. tor_free(encoded);
  271. }
  272. /* Test the descriptor padding. */
  273. static void
  274. test_descriptor_padding(void *arg)
  275. {
  276. char *plaintext;
  277. size_t plaintext_len, padded_len;
  278. uint8_t *padded_plaintext = NULL;
  279. /* Example: if l = 129, the ceiled division gives 2 and then multiplied by 128
  280. * to give 256. With l = 127, ceiled division gives 1 then times 128. */
  281. #define PADDING_EXPECTED_LEN(l) \
  282. CEIL_DIV(l, HS_DESC_PLAINTEXT_PADDING_MULTIPLE) * \
  283. HS_DESC_PLAINTEXT_PADDING_MULTIPLE
  284. (void) arg;
  285. { /* test #1: no padding */
  286. plaintext_len = HS_DESC_PLAINTEXT_PADDING_MULTIPLE;
  287. plaintext = tor_malloc(plaintext_len);
  288. padded_len = build_plaintext_padding(plaintext, plaintext_len,
  289. &padded_plaintext);
  290. tt_assert(padded_plaintext);
  291. tor_free(plaintext);
  292. /* Make sure our padding has been zeroed. */
  293. tt_int_op(tor_mem_is_zero((char *) padded_plaintext + plaintext_len,
  294. padded_len - plaintext_len), OP_EQ, 1);
  295. tor_free(padded_plaintext);
  296. /* Never never have a padded length smaller than the plaintext. */
  297. tt_int_op(padded_len, OP_GE, plaintext_len);
  298. tt_int_op(padded_len, OP_EQ, PADDING_EXPECTED_LEN(plaintext_len));
  299. }
  300. { /* test #2: one byte padding? */
  301. plaintext_len = HS_DESC_PLAINTEXT_PADDING_MULTIPLE - 1;
  302. plaintext = tor_malloc(plaintext_len);
  303. padded_plaintext = NULL;
  304. padded_len = build_plaintext_padding(plaintext, plaintext_len,
  305. &padded_plaintext);
  306. tt_assert(padded_plaintext);
  307. tor_free(plaintext);
  308. /* Make sure our padding has been zeroed. */
  309. tt_int_op(tor_mem_is_zero((char *) padded_plaintext + plaintext_len,
  310. padded_len - plaintext_len), OP_EQ, 1);
  311. tor_free(padded_plaintext);
  312. /* Never never have a padded length smaller than the plaintext. */
  313. tt_int_op(padded_len, OP_GE, plaintext_len);
  314. tt_int_op(padded_len, OP_EQ, PADDING_EXPECTED_LEN(plaintext_len));
  315. }
  316. { /* test #3: Lots more bytes of padding? */
  317. plaintext_len = HS_DESC_PLAINTEXT_PADDING_MULTIPLE + 1;
  318. plaintext = tor_malloc(plaintext_len);
  319. padded_plaintext = NULL;
  320. padded_len = build_plaintext_padding(plaintext, plaintext_len,
  321. &padded_plaintext);
  322. tt_assert(padded_plaintext);
  323. tor_free(plaintext);
  324. /* Make sure our padding has been zeroed. */
  325. tt_int_op(tor_mem_is_zero((char *) padded_plaintext + plaintext_len,
  326. padded_len - plaintext_len), OP_EQ, 1);
  327. tor_free(padded_plaintext);
  328. /* Never never have a padded length smaller than the plaintext. */
  329. tt_int_op(padded_len, OP_GE, plaintext_len);
  330. tt_int_op(padded_len, OP_EQ, PADDING_EXPECTED_LEN(plaintext_len));
  331. }
  332. done:
  333. return;
  334. }
  335. static void
  336. test_link_specifier(void *arg)
  337. {
  338. ssize_t ret;
  339. hs_desc_link_specifier_t spec;
  340. smartlist_t *link_specifiers = smartlist_new();
  341. (void) arg;
  342. /* Always this port. */
  343. spec.u.ap.port = 42;
  344. smartlist_add(link_specifiers, &spec);
  345. /* Test IPv4 for starter. */
  346. {
  347. char *b64, buf[256];
  348. uint32_t ipv4;
  349. link_specifier_t *ls;
  350. spec.type = LS_IPV4;
  351. ret = tor_addr_parse(&spec.u.ap.addr, "1.2.3.4");
  352. tt_int_op(ret, ==, AF_INET);
  353. b64 = encode_link_specifiers(link_specifiers);
  354. tt_assert(b64);
  355. /* Decode it and validate the format. */
  356. ret = base64_decode(buf, sizeof(buf), b64, strlen(b64));
  357. tt_int_op(ret, >, 0);
  358. /* First byte is the number of link specifier. */
  359. tt_int_op(get_uint8(buf), ==, 1);
  360. ret = link_specifier_parse(&ls, (uint8_t *) buf + 1, ret - 1);
  361. tt_int_op(ret, ==, 8);
  362. /* Should be 2 bytes for port and 4 bytes for IPv4. */
  363. tt_int_op(link_specifier_get_ls_len(ls), ==, 6);
  364. ipv4 = link_specifier_get_un_ipv4_addr(ls);
  365. tt_int_op(tor_addr_to_ipv4h(&spec.u.ap.addr), ==, ipv4);
  366. tt_int_op(link_specifier_get_un_ipv4_port(ls), ==, spec.u.ap.port);
  367. link_specifier_free(ls);
  368. tor_free(b64);
  369. }
  370. /* Test IPv6. */
  371. {
  372. char *b64, buf[256];
  373. uint8_t ipv6[16];
  374. link_specifier_t *ls;
  375. spec.type = LS_IPV6;
  376. ret = tor_addr_parse(&spec.u.ap.addr, "[1:2:3:4::]");
  377. tt_int_op(ret, ==, AF_INET6);
  378. b64 = encode_link_specifiers(link_specifiers);
  379. tt_assert(b64);
  380. /* Decode it and validate the format. */
  381. ret = base64_decode(buf, sizeof(buf), b64, strlen(b64));
  382. tt_int_op(ret, >, 0);
  383. /* First byte is the number of link specifier. */
  384. tt_int_op(get_uint8(buf), ==, 1);
  385. ret = link_specifier_parse(&ls, (uint8_t *) buf + 1, ret - 1);
  386. tt_int_op(ret, ==, 20);
  387. /* Should be 2 bytes for port and 16 bytes for IPv6. */
  388. tt_int_op(link_specifier_get_ls_len(ls), ==, 18);
  389. for (unsigned int i = 0; i < sizeof(ipv6); i++) {
  390. ipv6[i] = link_specifier_get_un_ipv6_addr(ls, i);
  391. }
  392. tt_mem_op(tor_addr_to_in6_addr8(&spec.u.ap.addr), ==, ipv6, sizeof(ipv6));
  393. tt_int_op(link_specifier_get_un_ipv6_port(ls), ==, spec.u.ap.port);
  394. link_specifier_free(ls);
  395. tor_free(b64);
  396. }
  397. /* Test legacy. */
  398. {
  399. char *b64, buf[256];
  400. uint8_t *id;
  401. link_specifier_t *ls;
  402. spec.type = LS_LEGACY_ID;
  403. memset(spec.u.legacy_id, 'Y', sizeof(spec.u.legacy_id));
  404. b64 = encode_link_specifiers(link_specifiers);
  405. tt_assert(b64);
  406. /* Decode it and validate the format. */
  407. ret = base64_decode(buf, sizeof(buf), b64, strlen(b64));
  408. tt_int_op(ret, >, 0);
  409. /* First byte is the number of link specifier. */
  410. tt_int_op(get_uint8(buf), ==, 1);
  411. ret = link_specifier_parse(&ls, (uint8_t *) buf + 1, ret - 1);
  412. /* 20 bytes digest + 1 byte type + 1 byte len. */
  413. tt_int_op(ret, ==, 22);
  414. tt_int_op(link_specifier_getlen_un_legacy_id(ls), OP_EQ, DIGEST_LEN);
  415. /* Digest length is 20 bytes. */
  416. tt_int_op(link_specifier_get_ls_len(ls), OP_EQ, DIGEST_LEN);
  417. id = link_specifier_getarray_un_legacy_id(ls);
  418. tt_mem_op(spec.u.legacy_id, OP_EQ, id, DIGEST_LEN);
  419. link_specifier_free(ls);
  420. tor_free(b64);
  421. }
  422. done:
  423. smartlist_free(link_specifiers);
  424. }
  425. static void
  426. test_encode_descriptor(void *arg)
  427. {
  428. int ret;
  429. char *encoded = NULL;
  430. hs_descriptor_t *desc = helper_build_hs_desc(0);
  431. (void) arg;
  432. ret = hs_desc_encode_descriptor(desc, &encoded);
  433. tt_int_op(ret, ==, 0);
  434. tt_assert(encoded);
  435. done:
  436. hs_descriptor_free(desc);
  437. tor_free(encoded);
  438. }
  439. static void
  440. test_decode_descriptor(void *arg)
  441. {
  442. int ret;
  443. char *encoded = NULL;
  444. hs_descriptor_t *desc = helper_build_hs_desc(0);
  445. hs_descriptor_t *decoded = NULL;
  446. hs_descriptor_t *desc_no_ip = NULL;
  447. (void) arg;
  448. /* Give some bad stuff to the decoding function. */
  449. ret = hs_desc_decode_descriptor("hladfjlkjadf", NULL, &decoded);
  450. tt_int_op(ret, OP_EQ, -1);
  451. ret = hs_desc_encode_descriptor(desc, &encoded);
  452. tt_int_op(ret, ==, 0);
  453. tt_assert(encoded);
  454. ret = hs_desc_decode_descriptor(encoded, NULL, &decoded);
  455. tt_int_op(ret, ==, 0);
  456. tt_assert(decoded);
  457. helper_compare_hs_desc(desc, decoded);
  458. /* Decode a descriptor with _no_ introduction points. */
  459. {
  460. desc_no_ip = helper_build_hs_desc(1);
  461. tt_assert(desc_no_ip);
  462. tor_free(encoded);
  463. ret = hs_desc_encode_descriptor(desc_no_ip, &encoded);
  464. tt_int_op(ret, ==, 0);
  465. tt_assert(encoded);
  466. hs_descriptor_free(decoded);
  467. ret = hs_desc_decode_descriptor(encoded, NULL, &decoded);
  468. tt_int_op(ret, ==, 0);
  469. tt_assert(decoded);
  470. }
  471. done:
  472. hs_descriptor_free(desc);
  473. hs_descriptor_free(desc_no_ip);
  474. hs_descriptor_free(decoded);
  475. tor_free(encoded);
  476. }
  477. static void
  478. test_supported_version(void *arg)
  479. {
  480. int ret;
  481. (void) arg;
  482. /* Unsupported. */
  483. ret = hs_desc_is_supported_version(42);
  484. tt_int_op(ret, OP_EQ, 0);
  485. /* To early. */
  486. ret = hs_desc_is_supported_version(HS_DESC_SUPPORTED_FORMAT_VERSION_MIN - 1);
  487. tt_int_op(ret, OP_EQ, 0);
  488. /* One too new. */
  489. ret = hs_desc_is_supported_version(HS_DESC_SUPPORTED_FORMAT_VERSION_MAX + 1);
  490. tt_int_op(ret, OP_EQ, 0);
  491. /* Valid version. */
  492. ret = hs_desc_is_supported_version(3);
  493. tt_int_op(ret, OP_EQ, 1);
  494. done:
  495. ;
  496. }
  497. static void
  498. test_encrypted_data_len(void *arg)
  499. {
  500. int ret;
  501. size_t value;
  502. (void) arg;
  503. /* No length, error. */
  504. ret = encrypted_data_length_is_valid(0);
  505. tt_int_op(ret, OP_EQ, 0);
  506. /* Not a multiple of our encryption algorithm (thus no padding). It's
  507. * suppose to be aligned on HS_DESC_PLAINTEXT_PADDING_MULTIPLE. */
  508. value = HS_DESC_PLAINTEXT_PADDING_MULTIPLE * 10 - 1;
  509. ret = encrypted_data_length_is_valid(value);
  510. tt_int_op(ret, OP_EQ, 0);
  511. /* Valid value. */
  512. value = HS_DESC_PADDED_PLAINTEXT_MAX_LEN + HS_DESC_ENCRYPTED_SALT_LEN +
  513. DIGEST256_LEN;
  514. ret = encrypted_data_length_is_valid(value);
  515. tt_int_op(ret, OP_EQ, 1);
  516. /* XXX: Test maximum possible size. */
  517. done:
  518. ;
  519. }
  520. static void
  521. test_decode_intro_point(void *arg)
  522. {
  523. int ret;
  524. char *encoded_ip = NULL;
  525. size_t len_out;
  526. hs_desc_intro_point_t *ip = NULL;
  527. hs_descriptor_t *desc = NULL;
  528. (void) arg;
  529. /* The following certificate expires in 2036. After that, one of the test
  530. * will fail because of the expiry time. */
  531. /* Seperate pieces of a valid encoded introduction point. */
  532. const char *intro_point =
  533. "introduction-point AQIUMDI5OUYyNjhGQ0E5RDU1Q0QxNTc=";
  534. const char *auth_key =
  535. "auth-key\n"
  536. "-----BEGIN ED25519 CERT-----\n"
  537. "AQkACOhAAQW8ltYZMIWpyrfyE/b4Iyi8CNybCwYs6ADk7XfBaxsFAQAgBAD3/BE4\n"
  538. "XojGE/N2bW/wgnS9r2qlrkydGyuCKIGayYx3haZ39LD4ZTmSMRxwmplMAqzG/XNP\n"
  539. "0Kkpg4p2/VnLFJRdU1SMFo1lgQ4P0bqw7Tgx200fulZ4KUM5z5V7m+a/mgY=\n"
  540. "-----END ED25519 CERT-----";
  541. const char *enc_key =
  542. "enc-key ntor bpZKLsuhxP6woDQ3yVyjm5gUKSk7RjfAijT2qrzbQk0=";
  543. const char *enc_key_legacy =
  544. "enc-key legacy\n"
  545. "-----BEGIN RSA PUBLIC KEY-----\n"
  546. "MIGJAoGBAO4bATcW8kW4h6RQQAKEgg+aXCpF4JwbcO6vGZtzXTDB+HdPVQzwqkbh\n"
  547. "XzFM6VGArhYw4m31wcP1Z7IwULir7UMnAFd7Zi62aYfU6l+Y1yAoZ1wzu1XBaAMK\n"
  548. "ejpwQinW9nzJn7c2f69fVke3pkhxpNdUZ+vplSA/l9iY+y+v+415AgMBAAE=\n"
  549. "-----END RSA PUBLIC KEY-----";
  550. const char *enc_key_cert =
  551. "enc-key-certification\n"
  552. "-----BEGIN ED25519 CERT-----\n"
  553. "AQsACOhZAUpNvCZ1aJaaR49lS6MCdsVkhVGVrRqoj0Y2T4SzroAtAQAgBABFOcGg\n"
  554. "lbTt1DF5nKTE/gU3Fr8ZtlCIOhu1A+F5LM7fqCUupfesg0KTHwyIZOYQbJuM5/he\n"
  555. "/jDNyLy9woPJdjkxywaY2RPUxGjLYtMQV0E8PUxWyICV+7y52fTCYaKpYQw=\n"
  556. "-----END ED25519 CERT-----";
  557. const char *enc_key_cert_legacy =
  558. "enc-key-certification\n"
  559. "-----BEGIN CROSSCERT-----\n"
  560. "Sk28JnVolppHj2VLowJ2xWSFUZWtGqiPRjZPhLOugC0ACOhZgFPA5egeRDUXMM1U\n"
  561. "Fn3c7Je0gJS6mVma5FzwlgwggeriF13UZcaT71vEAN/ZJXbxOfQVGMZ0rXuFpjUq\n"
  562. "C8CvqmZIwEUaPE1nDFtmnTcucvNS1YQl9nsjH3ejbxc+4yqps/cXh46FmXsm5yz7\n"
  563. "NZjBM9U1fbJhlNtOvrkf70K8bLk6\n"
  564. "-----END CROSSCERT-----";
  565. (void) enc_key_legacy;
  566. (void) enc_key_cert_legacy;
  567. /* Start by testing the "decode all intro points" function. */
  568. {
  569. char *line;
  570. desc = helper_build_hs_desc(0);
  571. tt_assert(desc);
  572. /* Only try to decode an incomplete introduction point section. */
  573. tor_asprintf(&line, "\n%s", intro_point);
  574. ret = decode_intro_points(desc, &desc->encrypted_data, line);
  575. tor_free(line);
  576. tt_int_op(ret, ==, -1);
  577. /* Decode one complete intro point. */
  578. smartlist_t *lines = smartlist_new();
  579. smartlist_add(lines, (char *) intro_point);
  580. smartlist_add(lines, (char *) auth_key);
  581. smartlist_add(lines, (char *) enc_key);
  582. smartlist_add(lines, (char *) enc_key_cert);
  583. encoded_ip = smartlist_join_strings(lines, "\n", 0, &len_out);
  584. tt_assert(encoded_ip);
  585. tor_asprintf(&line, "\n%s", encoded_ip);
  586. tor_free(encoded_ip);
  587. ret = decode_intro_points(desc, &desc->encrypted_data, line);
  588. tor_free(line);
  589. smartlist_free(lines);
  590. tt_int_op(ret, ==, 0);
  591. }
  592. /* Try to decode a junk string. */
  593. {
  594. hs_descriptor_free(desc);
  595. desc = helper_build_hs_desc(0);
  596. const char *junk = "this is not a descriptor";
  597. ip = decode_introduction_point(desc, junk);
  598. tt_assert(!ip);
  599. desc_intro_point_free(ip);
  600. ip = NULL;
  601. }
  602. /* Invalid link specifiers. */
  603. {
  604. smartlist_t *lines = smartlist_new();
  605. const char *bad_line = "introduction-point blah";
  606. smartlist_add(lines, (char *) bad_line);
  607. smartlist_add(lines, (char *) auth_key);
  608. smartlist_add(lines, (char *) enc_key);
  609. smartlist_add(lines, (char *) enc_key_cert);
  610. encoded_ip = smartlist_join_strings(lines, "\n", 0, &len_out);
  611. tt_assert(encoded_ip);
  612. ip = decode_introduction_point(desc, encoded_ip);
  613. tt_assert(!ip);
  614. tor_free(encoded_ip);
  615. smartlist_free(lines);
  616. desc_intro_point_free(ip);
  617. ip = NULL;
  618. }
  619. /* Invalid auth key type. */
  620. {
  621. smartlist_t *lines = smartlist_new();
  622. /* Try to put a valid object that our tokenize function will be able to
  623. * parse but that has nothing to do with the auth_key. */
  624. const char *bad_line =
  625. "auth-key\n"
  626. "-----BEGIN UNICORN CERT-----\n"
  627. "MIGJAoGBAO4bATcW8kW4h6RQQAKEgg+aXCpF4JwbcO6vGZtzXTDB+HdPVQzwqkbh\n"
  628. "XzFM6VGArhYw4m31wcP1Z7IwULir7UMnAFd7Zi62aYfU6l+Y1yAoZ1wzu1XBaAMK\n"
  629. "ejpwQinW9nzJn7c2f69fVke3pkhxpNdUZ+vplSA/l9iY+y+v+415AgMBAAE=\n"
  630. "-----END UNICORN CERT-----";
  631. /* Build intro point text. */
  632. smartlist_add(lines, (char *) intro_point);
  633. smartlist_add(lines, (char *) bad_line);
  634. smartlist_add(lines, (char *) enc_key);
  635. smartlist_add(lines, (char *) enc_key_cert);
  636. encoded_ip = smartlist_join_strings(lines, "\n", 0, &len_out);
  637. tt_assert(encoded_ip);
  638. ip = decode_introduction_point(desc, encoded_ip);
  639. tt_assert(!ip);
  640. tor_free(encoded_ip);
  641. smartlist_free(lines);
  642. }
  643. /* Invalid enc-key. */
  644. {
  645. smartlist_t *lines = smartlist_new();
  646. const char *bad_line =
  647. "enc-key unicorn bpZKLsuhxP6woDQ3yVyjm5gUKSk7RjfAijT2qrzbQk0=";
  648. /* Build intro point text. */
  649. smartlist_add(lines, (char *) intro_point);
  650. smartlist_add(lines, (char *) auth_key);
  651. smartlist_add(lines, (char *) bad_line);
  652. smartlist_add(lines, (char *) enc_key_cert);
  653. encoded_ip = smartlist_join_strings(lines, "\n", 0, &len_out);
  654. tt_assert(encoded_ip);
  655. ip = decode_introduction_point(desc, encoded_ip);
  656. tt_assert(!ip);
  657. tor_free(encoded_ip);
  658. smartlist_free(lines);
  659. }
  660. /* Invalid enc-key object. */
  661. {
  662. smartlist_t *lines = smartlist_new();
  663. const char *bad_line = "enc-key ntor";
  664. /* Build intro point text. */
  665. smartlist_add(lines, (char *) intro_point);
  666. smartlist_add(lines, (char *) auth_key);
  667. smartlist_add(lines, (char *) bad_line);
  668. smartlist_add(lines, (char *) enc_key_cert);
  669. encoded_ip = smartlist_join_strings(lines, "\n", 0, &len_out);
  670. tt_assert(encoded_ip);
  671. ip = decode_introduction_point(desc, encoded_ip);
  672. tt_assert(!ip);
  673. tor_free(encoded_ip);
  674. smartlist_free(lines);
  675. }
  676. /* Invalid enc-key base64 curv25519 key. */
  677. {
  678. smartlist_t *lines = smartlist_new();
  679. const char *bad_line = "enc-key ntor blah===";
  680. /* Build intro point text. */
  681. smartlist_add(lines, (char *) intro_point);
  682. smartlist_add(lines, (char *) auth_key);
  683. smartlist_add(lines, (char *) bad_line);
  684. smartlist_add(lines, (char *) enc_key_cert);
  685. encoded_ip = smartlist_join_strings(lines, "\n", 0, &len_out);
  686. tt_assert(encoded_ip);
  687. ip = decode_introduction_point(desc, encoded_ip);
  688. tt_assert(!ip);
  689. tor_free(encoded_ip);
  690. smartlist_free(lines);
  691. }
  692. /* Invalid enc-key invalid legacy. */
  693. {
  694. smartlist_t *lines = smartlist_new();
  695. const char *bad_line = "enc-key legacy blah===";
  696. /* Build intro point text. */
  697. smartlist_add(lines, (char *) intro_point);
  698. smartlist_add(lines, (char *) auth_key);
  699. smartlist_add(lines, (char *) bad_line);
  700. smartlist_add(lines, (char *) enc_key_cert);
  701. encoded_ip = smartlist_join_strings(lines, "\n", 0, &len_out);
  702. tt_assert(encoded_ip);
  703. ip = decode_introduction_point(desc, encoded_ip);
  704. tt_assert(!ip);
  705. tor_free(encoded_ip);
  706. smartlist_free(lines);
  707. }
  708. /* Valid object. */
  709. {
  710. smartlist_t *lines = smartlist_new();
  711. /* Build intro point text. */
  712. smartlist_add(lines, (char *) intro_point);
  713. smartlist_add(lines, (char *) auth_key);
  714. smartlist_add(lines, (char *) enc_key);
  715. smartlist_add(lines, (char *) enc_key_cert);
  716. encoded_ip = smartlist_join_strings(lines, "\n", 0, &len_out);
  717. tt_assert(encoded_ip);
  718. ip = decode_introduction_point(desc, encoded_ip);
  719. tt_assert(ip);
  720. tor_free(encoded_ip);
  721. smartlist_free(lines);
  722. }
  723. done:
  724. hs_descriptor_free(desc);
  725. desc_intro_point_free(ip);
  726. }
  727. static void
  728. test_decode_plaintext(void *arg)
  729. {
  730. int ret;
  731. hs_desc_plaintext_data_t desc_plaintext;
  732. const char *bad_value = "unicorn";
  733. (void) arg;
  734. #define template \
  735. "hs-descriptor %s\n" \
  736. "descriptor-lifetime %s\n" \
  737. "descriptor-signing-key-cert\n" \
  738. "-----BEGIN ED25519 CERT-----\n" \
  739. "AQgABjvPAQaG3g+dc6oV/oJV4ODAtkvx56uBnPtBT9mYVuHVOhn7AQAgBABUg3mQ\n" \
  740. "myBr4bu5LCr53wUEbW2EXui01CbUgU7pfo9LvJG3AcXRojj6HlfsUs9BkzYzYdjF\n" \
  741. "A69Apikgu0ewHYkFFASt7Il+gB3w6J8YstQJZT7dtbtl+doM7ug8B68Qdg8=\n" \
  742. "-----END ED25519 CERT-----\n" \
  743. "revision-counter %s\n" \
  744. "encrypted\n" \
  745. "-----BEGIN %s-----\n" \
  746. "UNICORN\n" \
  747. "-----END MESSAGE-----\n" \
  748. "signature m20WJH5agqvwhq7QeuEZ1mYyPWQDO+eJOZUjLhAiKu8DbL17DsDfJE6kXbWy" \
  749. "HimbNj2we0enV3cCOOAsmPOaAw\n"
  750. /* Invalid version. */
  751. {
  752. char *plaintext;
  753. tor_asprintf(&plaintext, template, bad_value, "180", "42", "MESSAGE");
  754. ret = hs_desc_decode_plaintext(plaintext, &desc_plaintext);
  755. tor_free(plaintext);
  756. tt_int_op(ret, OP_EQ, -1);
  757. }
  758. /* Missing fields. */
  759. {
  760. const char *plaintext = "hs-descriptor 3\n";
  761. ret = hs_desc_decode_plaintext(plaintext, &desc_plaintext);
  762. tt_int_op(ret, OP_EQ, -1);
  763. }
  764. /* Max length. */
  765. {
  766. size_t big = 64000;
  767. /* Must always be bigger than HS_DESC_MAX_LEN. */
  768. tt_int_op(HS_DESC_MAX_LEN, <, big);
  769. char *plaintext = tor_malloc_zero(big);
  770. memset(plaintext, 'a', big);
  771. plaintext[big - 1] = '\0';
  772. ret = hs_desc_decode_plaintext(plaintext, &desc_plaintext);
  773. tor_free(plaintext);
  774. tt_int_op(ret, OP_EQ, -1);
  775. }
  776. /* Bad lifetime value. */
  777. {
  778. char *plaintext;
  779. tor_asprintf(&plaintext, template, "3", bad_value, "42", "MESSAGE");
  780. ret = hs_desc_decode_plaintext(plaintext, &desc_plaintext);
  781. tor_free(plaintext);
  782. tt_int_op(ret, OP_EQ, -1);
  783. }
  784. /* Huge lifetime value. */
  785. {
  786. char *plaintext;
  787. tor_asprintf(&plaintext, template, "3", "7181615", "42", "MESSAGE");
  788. ret = hs_desc_decode_plaintext(plaintext, &desc_plaintext);
  789. tor_free(plaintext);
  790. tt_int_op(ret, OP_EQ, -1);
  791. }
  792. /* Invalid encrypted section. */
  793. {
  794. char *plaintext;
  795. tor_asprintf(&plaintext, template, "3", "180", "42", bad_value);
  796. ret = hs_desc_decode_plaintext(plaintext, &desc_plaintext);
  797. tor_free(plaintext);
  798. tt_int_op(ret, OP_EQ, -1);
  799. }
  800. /* Invalid revision counter. */
  801. {
  802. char *plaintext;
  803. tor_asprintf(&plaintext, template, "3", "180", bad_value, "MESSAGE");
  804. ret = hs_desc_decode_plaintext(plaintext, &desc_plaintext);
  805. tor_free(plaintext);
  806. tt_int_op(ret, OP_EQ, -1);
  807. }
  808. done:
  809. ;
  810. }
  811. static void
  812. test_validate_cert(void *arg)
  813. {
  814. int ret;
  815. time_t now = time(NULL);
  816. ed25519_keypair_t kp;
  817. tor_cert_t *cert = NULL;
  818. (void) arg;
  819. ret = ed25519_keypair_generate(&kp, 0);
  820. tt_int_op(ret, ==, 0);
  821. /* Cert of type CERT_TYPE_AUTH_HS_IP_KEY. */
  822. cert = tor_cert_create(&kp, CERT_TYPE_AUTH_HS_IP_KEY,
  823. &kp.pubkey, now, 3600,
  824. CERT_FLAG_INCLUDE_SIGNING_KEY);
  825. tt_assert(cert);
  826. /* Test with empty certificate. */
  827. ret = cert_is_valid(NULL, CERT_TYPE_AUTH_HS_IP_KEY, "unicorn");
  828. tt_int_op(ret, OP_EQ, 0);
  829. /* Test with a bad type. */
  830. ret = cert_is_valid(cert, CERT_TYPE_SIGNING_HS_DESC, "unicorn");
  831. tt_int_op(ret, OP_EQ, 0);
  832. /* Normal validation. */
  833. ret = cert_is_valid(cert, CERT_TYPE_AUTH_HS_IP_KEY, "unicorn");
  834. tt_int_op(ret, OP_EQ, 1);
  835. /* Break signing key so signature verification will fails. */
  836. memset(&cert->signing_key, 0, sizeof(cert->signing_key));
  837. ret = cert_is_valid(cert, CERT_TYPE_AUTH_HS_IP_KEY, "unicorn");
  838. tt_int_op(ret, OP_EQ, 0);
  839. tor_cert_free(cert);
  840. /* Try a cert without including the signing key. */
  841. cert = tor_cert_create(&kp, CERT_TYPE_AUTH_HS_IP_KEY, &kp.pubkey, now,
  842. 3600, 0);
  843. tt_assert(cert);
  844. /* Test with a bad type. */
  845. ret = cert_is_valid(cert, CERT_TYPE_AUTH_HS_IP_KEY, "unicorn");
  846. tt_int_op(ret, OP_EQ, 0);
  847. done:
  848. tor_cert_free(cert);
  849. }
  850. static void
  851. test_desc_signature(void *arg)
  852. {
  853. int ret;
  854. char *data = NULL, *desc = NULL;
  855. char sig_b64[ED25519_SIG_BASE64_LEN + 1];
  856. ed25519_keypair_t kp;
  857. ed25519_signature_t sig;
  858. (void) arg;
  859. ed25519_keypair_generate(&kp, 0);
  860. /* Setup a phoony descriptor but with a valid signature token that is the
  861. * signature is verifiable. */
  862. tor_asprintf(&data, "This is a signed descriptor\n");
  863. ret = ed25519_sign_prefixed(&sig, (const uint8_t *) data, strlen(data),
  864. "Tor onion service descriptor sig v3", &kp);
  865. tt_int_op(ret, ==, 0);
  866. ret = ed25519_signature_to_base64(sig_b64, &sig);
  867. tt_int_op(ret, ==, 0);
  868. /* Build the descriptor that should be valid. */
  869. tor_asprintf(&desc, "%ssignature %s\n", data, sig_b64);
  870. ret = desc_sig_is_valid(sig_b64, &kp, desc, strlen(desc));
  871. tt_int_op(ret, ==, 1);
  872. /* Junk signature. */
  873. ret = desc_sig_is_valid("JUNK", &kp, desc, strlen(desc));
  874. tt_int_op(ret, ==, 0);
  875. done:
  876. tor_free(desc);
  877. tor_free(data);
  878. }
  879. struct testcase_t hs_descriptor[] = {
  880. /* Encoding tests. */
  881. { "cert_encoding", test_cert_encoding, TT_FORK,
  882. NULL, NULL },
  883. { "link_specifier", test_link_specifier, TT_FORK,
  884. NULL, NULL },
  885. { "encode_descriptor", test_encode_descriptor, TT_FORK,
  886. NULL, NULL },
  887. { "descriptor_padding", test_descriptor_padding, TT_FORK,
  888. NULL, NULL },
  889. /* Decoding tests. */
  890. { "decode_descriptor", test_decode_descriptor, TT_FORK,
  891. NULL, NULL },
  892. { "encrypted_data_len", test_encrypted_data_len, TT_FORK,
  893. NULL, NULL },
  894. { "decode_intro_point", test_decode_intro_point, TT_FORK,
  895. NULL, NULL },
  896. { "decode_plaintext", test_decode_plaintext, TT_FORK,
  897. NULL, NULL },
  898. /* Misc. */
  899. { "version", test_supported_version, TT_FORK,
  900. NULL, NULL },
  901. { "validate_cert", test_validate_cert, TT_FORK,
  902. NULL, NULL },
  903. { "desc_signature", test_desc_signature, TT_FORK,
  904. NULL, NULL },
  905. END_OF_TESTCASES
  906. };