test_hs_descriptor.c 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121
  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. tor_free(plaintext);
  871. tt_int_op(ret, OP_EQ, -1);
  872. }
  873. /* Huge lifetime value. */
  874. {
  875. char *plaintext;
  876. tor_asprintf(&plaintext, template, "3", "7181615", "42", "MESSAGE");
  877. ret = hs_desc_decode_plaintext(plaintext, &desc_plaintext);
  878. tor_free(plaintext);
  879. tt_int_op(ret, OP_EQ, -1);
  880. }
  881. /* Invalid encrypted section. */
  882. {
  883. char *plaintext;
  884. tor_asprintf(&plaintext, template, "3", "180", "42", bad_value);
  885. ret = hs_desc_decode_plaintext(plaintext, &desc_plaintext);
  886. tor_free(plaintext);
  887. tt_int_op(ret, OP_EQ, -1);
  888. }
  889. /* Invalid revision counter. */
  890. {
  891. char *plaintext;
  892. tor_asprintf(&plaintext, template, "3", "180", bad_value, "MESSAGE");
  893. ret = hs_desc_decode_plaintext(plaintext, &desc_plaintext);
  894. tor_free(plaintext);
  895. tt_int_op(ret, OP_EQ, -1);
  896. }
  897. done:
  898. ;
  899. }
  900. static void
  901. test_validate_cert(void *arg)
  902. {
  903. int ret;
  904. time_t now = time(NULL);
  905. ed25519_keypair_t kp;
  906. tor_cert_t *cert = NULL;
  907. (void) arg;
  908. ret = ed25519_keypair_generate(&kp, 0);
  909. tt_int_op(ret, ==, 0);
  910. /* Cert of type CERT_TYPE_AUTH_HS_IP_KEY. */
  911. cert = tor_cert_create(&kp, CERT_TYPE_AUTH_HS_IP_KEY,
  912. &kp.pubkey, now, 3600,
  913. CERT_FLAG_INCLUDE_SIGNING_KEY);
  914. tt_assert(cert);
  915. /* Test with empty certificate. */
  916. ret = cert_is_valid(NULL, CERT_TYPE_AUTH_HS_IP_KEY, "unicorn");
  917. tt_int_op(ret, OP_EQ, 0);
  918. /* Test with a bad type. */
  919. ret = cert_is_valid(cert, CERT_TYPE_SIGNING_HS_DESC, "unicorn");
  920. tt_int_op(ret, OP_EQ, 0);
  921. /* Normal validation. */
  922. ret = cert_is_valid(cert, CERT_TYPE_AUTH_HS_IP_KEY, "unicorn");
  923. tt_int_op(ret, OP_EQ, 1);
  924. /* Break signing key so signature verification will fails. */
  925. memset(&cert->signing_key, 0, sizeof(cert->signing_key));
  926. ret = cert_is_valid(cert, CERT_TYPE_AUTH_HS_IP_KEY, "unicorn");
  927. tt_int_op(ret, OP_EQ, 0);
  928. tor_cert_free(cert);
  929. /* Try a cert without including the signing key. */
  930. cert = tor_cert_create(&kp, CERT_TYPE_AUTH_HS_IP_KEY, &kp.pubkey, now,
  931. 3600, 0);
  932. tt_assert(cert);
  933. /* Test with a bad type. */
  934. ret = cert_is_valid(cert, CERT_TYPE_AUTH_HS_IP_KEY, "unicorn");
  935. tt_int_op(ret, OP_EQ, 0);
  936. done:
  937. tor_cert_free(cert);
  938. }
  939. static void
  940. test_desc_signature(void *arg)
  941. {
  942. int ret;
  943. char *data, *desc;
  944. char sig_b64[ED25519_SIG_BASE64_LEN + 1];
  945. ed25519_keypair_t kp;
  946. ed25519_signature_t sig;
  947. (void) arg;
  948. ed25519_keypair_generate(&kp, 0);
  949. /* Setup a phoony descriptor but with a valid signature token that is the
  950. * signature is verifiable. */
  951. tor_asprintf(&data, "This is a signed descriptor\n");
  952. ret = ed25519_sign_prefixed(&sig, (const uint8_t *) data, strlen(data),
  953. "Tor onion service descriptor sig v3", &kp);
  954. tt_int_op(ret, ==, 0);
  955. ret = ed25519_signature_to_base64(sig_b64, &sig);
  956. tt_int_op(ret, ==, 0);
  957. /* Build the descriptor that should be valid. */
  958. tor_asprintf(&desc, "%ssignature %s\n", data, sig_b64);
  959. ret = desc_sig_is_valid(sig_b64, &kp, desc, strlen(desc));
  960. tt_int_op(ret, ==, 1);
  961. /* Junk signature. */
  962. ret = desc_sig_is_valid("JUNK", &kp, desc, strlen(desc));
  963. tt_int_op(ret, ==, 0);
  964. done:
  965. tor_free(desc);
  966. tor_free(data);
  967. }
  968. struct testcase_t hs_descriptor[] = {
  969. /* Encoding tests. */
  970. { "cert_encoding", test_cert_encoding, TT_FORK,
  971. NULL, NULL },
  972. { "link_specifier", test_link_specifier, TT_FORK,
  973. NULL, NULL },
  974. { "encode_descriptor", test_encode_descriptor, TT_FORK,
  975. NULL, NULL },
  976. { "descriptor_padding", test_descriptor_padding, TT_FORK,
  977. NULL, NULL },
  978. /* Decoding tests. */
  979. { "decode_descriptor", test_decode_descriptor, TT_FORK,
  980. NULL, NULL },
  981. { "encrypted_data_len", test_encrypted_data_len, TT_FORK,
  982. NULL, NULL },
  983. { "decode_intro_point", test_decode_intro_point, TT_FORK,
  984. NULL, NULL },
  985. { "decode_multiple_intro_points", test_decode_multiple_intro_points, TT_FORK,
  986. NULL, NULL },
  987. { "decode_plaintext", test_decode_plaintext, TT_FORK,
  988. NULL, NULL },
  989. /* Misc. */
  990. { "version", test_supported_version, TT_FORK,
  991. NULL, NULL },
  992. { "validate_cert", test_validate_cert, TT_FORK,
  993. NULL, NULL },
  994. { "desc_signature", test_desc_signature, TT_FORK,
  995. NULL, NULL },
  996. END_OF_TESTCASES
  997. };