test_hs_descriptor.c 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130
  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, 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. 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. return;
  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. (void) arg;
  444. /* Give some bad stuff to the decoding function. */
  445. ret = hs_desc_decode_descriptor("hladfjlkjadf", NULL, &decoded);
  446. tt_int_op(ret, OP_EQ, -1);
  447. ret = hs_desc_encode_descriptor(desc, &encoded);
  448. tt_int_op(ret, ==, 0);
  449. tt_assert(encoded);
  450. ret = hs_desc_decode_descriptor(encoded, NULL, &decoded);
  451. tt_int_op(ret, ==, 0);
  452. tt_assert(decoded);
  453. helper_compare_hs_desc(desc, decoded);
  454. /* Decode a descriptor with _no_ introduction points. */
  455. {
  456. hs_descriptor_t *desc_no_ip = helper_build_hs_desc(1);
  457. tt_assert(desc_no_ip);
  458. tor_free(encoded);
  459. ret = hs_desc_encode_descriptor(desc_no_ip, &encoded);
  460. tt_int_op(ret, ==, 0);
  461. tt_assert(encoded);
  462. hs_descriptor_free(decoded);
  463. ret = hs_desc_decode_descriptor(encoded, NULL, &decoded);
  464. tt_int_op(ret, ==, 0);
  465. tt_assert(decoded);
  466. }
  467. done:
  468. hs_descriptor_free(desc);
  469. hs_descriptor_free(decoded);
  470. tor_free(encoded);
  471. }
  472. static void
  473. test_supported_version(void *arg)
  474. {
  475. int ret;
  476. (void) arg;
  477. /* Unsupported. */
  478. ret = hs_desc_is_supported_version(42);
  479. tt_int_op(ret, OP_EQ, 0);
  480. /* To early. */
  481. ret = hs_desc_is_supported_version(HS_DESC_SUPPORTED_FORMAT_VERSION_MIN - 1);
  482. tt_int_op(ret, OP_EQ, 0);
  483. /* One too new. */
  484. ret = hs_desc_is_supported_version(HS_DESC_SUPPORTED_FORMAT_VERSION_MAX + 1);
  485. tt_int_op(ret, OP_EQ, 0);
  486. /* Valid version. */
  487. ret = hs_desc_is_supported_version(3);
  488. tt_int_op(ret, OP_EQ, 1);
  489. done:
  490. ;
  491. }
  492. static void
  493. test_encrypted_data_len(void *arg)
  494. {
  495. int ret;
  496. size_t value;
  497. (void) arg;
  498. /* No length, error. */
  499. ret = encrypted_data_length_is_valid(0);
  500. tt_int_op(ret, OP_EQ, 0);
  501. /* Not a multiple of our encryption algorithm (thus no padding). It's
  502. * suppose to be aligned on HS_DESC_PLAINTEXT_PADDING_MULTIPLE. */
  503. value = HS_DESC_PLAINTEXT_PADDING_MULTIPLE * 10 - 1;
  504. ret = encrypted_data_length_is_valid(value);
  505. tt_int_op(ret, OP_EQ, 0);
  506. /* Valid value. */
  507. value = HS_DESC_PADDED_PLAINTEXT_MAX_LEN + HS_DESC_ENCRYPTED_SALT_LEN +
  508. DIGEST256_LEN;
  509. ret = encrypted_data_length_is_valid(value);
  510. tt_int_op(ret, OP_EQ, 1);
  511. /* XXX: Test maximum possible size. */
  512. done:
  513. ;
  514. }
  515. static void
  516. test_decode_intro_point(void *arg)
  517. {
  518. int ret;
  519. char *encoded_ip = NULL;
  520. size_t len_out;
  521. hs_desc_intro_point_t *ip;
  522. hs_descriptor_t *desc = NULL;
  523. (void) arg;
  524. /* The following certificate expires in 2036. After that, one of the test
  525. * will fail because of the expiry time. */
  526. /* Seperate pieces of a valid encoded introduction point. */
  527. const char *intro_point =
  528. "introduction-point AQIUMDI5OUYyNjhGQ0E5RDU1Q0QxNTc=";
  529. const char *auth_key =
  530. "auth-key\n"
  531. "-----BEGIN ED25519 CERT-----\n"
  532. "AQkACOhAAQW8ltYZMIWpyrfyE/b4Iyi8CNybCwYs6ADk7XfBaxsFAQAgBAD3/BE4\n"
  533. "XojGE/N2bW/wgnS9r2qlrkydGyuCKIGayYx3haZ39LD4ZTmSMRxwmplMAqzG/XNP\n"
  534. "0Kkpg4p2/VnLFJRdU1SMFo1lgQ4P0bqw7Tgx200fulZ4KUM5z5V7m+a/mgY=\n"
  535. "-----END ED25519 CERT-----";
  536. const char *enc_key =
  537. "enc-key ntor bpZKLsuhxP6woDQ3yVyjm5gUKSk7RjfAijT2qrzbQk0=";
  538. const char *enc_key_legacy =
  539. "enc-key legacy\n"
  540. "-----BEGIN RSA PUBLIC KEY-----\n"
  541. "MIGJAoGBAO4bATcW8kW4h6RQQAKEgg+aXCpF4JwbcO6vGZtzXTDB+HdPVQzwqkbh\n"
  542. "XzFM6VGArhYw4m31wcP1Z7IwULir7UMnAFd7Zi62aYfU6l+Y1yAoZ1wzu1XBaAMK\n"
  543. "ejpwQinW9nzJn7c2f69fVke3pkhxpNdUZ+vplSA/l9iY+y+v+415AgMBAAE=\n"
  544. "-----END RSA PUBLIC KEY-----";
  545. const char *enc_key_cert =
  546. "enc-key-certification\n"
  547. "-----BEGIN ED25519 CERT-----\n"
  548. "AQsACOhZAUpNvCZ1aJaaR49lS6MCdsVkhVGVrRqoj0Y2T4SzroAtAQAgBABFOcGg\n"
  549. "lbTt1DF5nKTE/gU3Fr8ZtlCIOhu1A+F5LM7fqCUupfesg0KTHwyIZOYQbJuM5/he\n"
  550. "/jDNyLy9woPJdjkxywaY2RPUxGjLYtMQV0E8PUxWyICV+7y52fTCYaKpYQw=\n"
  551. "-----END ED25519 CERT-----";
  552. const char *enc_key_cert_legacy =
  553. "enc-key-certification\n"
  554. "-----BEGIN CROSSCERT-----\n"
  555. "Sk28JnVolppHj2VLowJ2xWSFUZWtGqiPRjZPhLOugC0ACOhZgFPA5egeRDUXMM1U\n"
  556. "Fn3c7Je0gJS6mVma5FzwlgwggeriF13UZcaT71vEAN/ZJXbxOfQVGMZ0rXuFpjUq\n"
  557. "C8CvqmZIwEUaPE1nDFtmnTcucvNS1YQl9nsjH3ejbxc+4yqps/cXh46FmXsm5yz7\n"
  558. "NZjBM9U1fbJhlNtOvrkf70K8bLk6\n"
  559. "-----END CROSSCERT-----";
  560. (void) enc_key_legacy;
  561. (void) enc_key_cert_legacy;
  562. /* Start by testing the "decode all intro points" function. */
  563. {
  564. char *line;
  565. desc = helper_build_hs_desc(0);
  566. tt_assert(desc);
  567. /* Only try to decode an incomplete introduction point section. */
  568. tor_asprintf(&line, "\n%s", intro_point);
  569. ret = decode_intro_points(desc, &desc->encrypted_data, line);
  570. tor_free(line);
  571. tt_int_op(ret, ==, -1);
  572. /* Decode one complete intro point. */
  573. smartlist_t *lines = smartlist_new();
  574. smartlist_add(lines, (char *) intro_point);
  575. smartlist_add(lines, (char *) auth_key);
  576. smartlist_add(lines, (char *) enc_key);
  577. smartlist_add(lines, (char *) enc_key_cert);
  578. encoded_ip = smartlist_join_strings(lines, "\n", 0, &len_out);
  579. tt_assert(encoded_ip);
  580. tor_asprintf(&line, "\n%s", encoded_ip);
  581. tor_free(encoded_ip);
  582. ret = decode_intro_points(desc, &desc->encrypted_data, line);
  583. tor_free(line);
  584. smartlist_free(lines);
  585. tt_int_op(ret, ==, 0);
  586. }
  587. /* Try to decode a junk string. */
  588. {
  589. hs_descriptor_free(desc);
  590. desc = helper_build_hs_desc(0);
  591. const char *junk = "this is not a descriptor";
  592. ip = decode_introduction_point(desc, junk);
  593. tt_assert(!ip);
  594. }
  595. /* Invalid link specifiers. */
  596. {
  597. smartlist_t *lines = smartlist_new();
  598. const char *bad_line = "introduction-point blah";
  599. smartlist_add(lines, (char *) bad_line);
  600. smartlist_add(lines, (char *) auth_key);
  601. smartlist_add(lines, (char *) enc_key);
  602. smartlist_add(lines, (char *) enc_key_cert);
  603. encoded_ip = smartlist_join_strings(lines, "\n", 0, &len_out);
  604. tt_assert(encoded_ip);
  605. ip = decode_introduction_point(desc, encoded_ip);
  606. tt_assert(!ip);
  607. tor_free(encoded_ip);
  608. smartlist_free(lines);
  609. }
  610. /* Invalid auth key type. */
  611. {
  612. smartlist_t *lines = smartlist_new();
  613. /* Try to put a valid object that our tokenize function will be able to
  614. * parse but that has nothing to do with the auth_key. */
  615. const char *bad_line =
  616. "auth-key\n"
  617. "-----BEGIN UNICORN CERT-----\n"
  618. "MIGJAoGBAO4bATcW8kW4h6RQQAKEgg+aXCpF4JwbcO6vGZtzXTDB+HdPVQzwqkbh\n"
  619. "XzFM6VGArhYw4m31wcP1Z7IwULir7UMnAFd7Zi62aYfU6l+Y1yAoZ1wzu1XBaAMK\n"
  620. "ejpwQinW9nzJn7c2f69fVke3pkhxpNdUZ+vplSA/l9iY+y+v+415AgMBAAE=\n"
  621. "-----END UNICORN CERT-----";
  622. /* Build intro point text. */
  623. smartlist_add(lines, (char *) intro_point);
  624. smartlist_add(lines, (char *) bad_line);
  625. smartlist_add(lines, (char *) enc_key);
  626. smartlist_add(lines, (char *) enc_key_cert);
  627. encoded_ip = smartlist_join_strings(lines, "\n", 0, &len_out);
  628. tt_assert(encoded_ip);
  629. ip = decode_introduction_point(desc, encoded_ip);
  630. tt_assert(!ip);
  631. tor_free(encoded_ip);
  632. smartlist_free(lines);
  633. }
  634. /* Invalid enc-key. */
  635. {
  636. smartlist_t *lines = smartlist_new();
  637. const char *bad_line =
  638. "enc-key unicorn bpZKLsuhxP6woDQ3yVyjm5gUKSk7RjfAijT2qrzbQk0=";
  639. /* Build intro point text. */
  640. smartlist_add(lines, (char *) intro_point);
  641. smartlist_add(lines, (char *) auth_key);
  642. smartlist_add(lines, (char *) bad_line);
  643. smartlist_add(lines, (char *) enc_key_cert);
  644. encoded_ip = smartlist_join_strings(lines, "\n", 0, &len_out);
  645. tt_assert(encoded_ip);
  646. ip = decode_introduction_point(desc, encoded_ip);
  647. tt_assert(!ip);
  648. tor_free(encoded_ip);
  649. smartlist_free(lines);
  650. }
  651. /* Invalid enc-key object. */
  652. {
  653. smartlist_t *lines = smartlist_new();
  654. const char *bad_line = "enc-key ntor";
  655. /* Build intro point text. */
  656. smartlist_add(lines, (char *) intro_point);
  657. smartlist_add(lines, (char *) auth_key);
  658. smartlist_add(lines, (char *) bad_line);
  659. smartlist_add(lines, (char *) enc_key_cert);
  660. encoded_ip = smartlist_join_strings(lines, "\n", 0, &len_out);
  661. tt_assert(encoded_ip);
  662. ip = decode_introduction_point(desc, encoded_ip);
  663. tt_assert(!ip);
  664. tor_free(encoded_ip);
  665. smartlist_free(lines);
  666. }
  667. /* Invalid enc-key base64 curv25519 key. */
  668. {
  669. smartlist_t *lines = smartlist_new();
  670. const char *bad_line = "enc-key ntor blah===";
  671. /* Build intro point text. */
  672. smartlist_add(lines, (char *) intro_point);
  673. smartlist_add(lines, (char *) auth_key);
  674. smartlist_add(lines, (char *) bad_line);
  675. smartlist_add(lines, (char *) enc_key_cert);
  676. encoded_ip = smartlist_join_strings(lines, "\n", 0, &len_out);
  677. tt_assert(encoded_ip);
  678. ip = decode_introduction_point(desc, encoded_ip);
  679. tt_assert(!ip);
  680. tor_free(encoded_ip);
  681. smartlist_free(lines);
  682. }
  683. /* Invalid enc-key invalid legacy. */
  684. {
  685. smartlist_t *lines = smartlist_new();
  686. const char *bad_line = "enc-key legacy blah===";
  687. /* Build intro point text. */
  688. smartlist_add(lines, (char *) intro_point);
  689. smartlist_add(lines, (char *) auth_key);
  690. smartlist_add(lines, (char *) bad_line);
  691. smartlist_add(lines, (char *) enc_key_cert);
  692. encoded_ip = smartlist_join_strings(lines, "\n", 0, &len_out);
  693. tt_assert(encoded_ip);
  694. ip = decode_introduction_point(desc, encoded_ip);
  695. tt_assert(!ip);
  696. tor_free(encoded_ip);
  697. smartlist_free(lines);
  698. }
  699. /* Valid object. */
  700. {
  701. smartlist_t *lines = smartlist_new();
  702. /* Build intro point text. */
  703. smartlist_add(lines, (char *) intro_point);
  704. smartlist_add(lines, (char *) auth_key);
  705. smartlist_add(lines, (char *) enc_key);
  706. smartlist_add(lines, (char *) enc_key_cert);
  707. encoded_ip = smartlist_join_strings(lines, "\n", 0, &len_out);
  708. tt_assert(encoded_ip);
  709. ip = decode_introduction_point(desc, encoded_ip);
  710. tt_assert(ip);
  711. tor_free(encoded_ip);
  712. smartlist_free(lines);
  713. }
  714. done:
  715. hs_descriptor_free(desc);
  716. }
  717. const char encrypted_desc_portion[] = "create2-formats 2\n"
  718. "authentication-required ed25519\n"
  719. "introduction-point AQAGAQIDBCMp\n"
  720. "auth-key\n"
  721. "-----BEGIN ED25519 CERT-----\n"
  722. "AQkABmRZASMANx4sbMyDd4i+MciVUw29vPQ/nOFrLwUdTGEBXSXrAQAgBABo2zfd\n"
  723. "wyqAdzSeaIzH1TUcV3u8nAG2YhNCRw2/2vVWuD6Z4Fn0aNHnh1FONNkbismC9t1X\n"
  724. "Rf07hdZkVYEbOaPsHnFwhJULVSUo8YYuL19jghRjwMqPGeGfD4iuQqdo3QA=\n"
  725. "-----END ED25519 CERT-----\n"
  726. "enc-key ntor xo2n5anLMoyIMuhcKSLdVZISyISBW8j1vXRbpdbK+lU=\n"
  727. "enc-key-certification\n"
  728. "-----BEGIN ED25519 CERT-----\n"
  729. "AQsABmRZATUYQypFY7pr8FpmV61pcqUylt5fEr/QLfavfcwbzlA7AQAgBADSI5Ie\n"
  730. "Ekdy+qeHngLmz6Gr7fQ5xvilhxB91UDIjwRfP0ufoVF+HalsyXKskYvcYhH67+lw\n"
  731. "D947flCHzeJyfAT38jO/Cw42qM7H+SObBMcsTB93J0lPNBy4OHosH9ybtwA=\n"
  732. "-----END ED25519 CERT-----\n"
  733. "introduction-point AQESJgAAAAAAAAAAAAAAAAAAASMp\n"
  734. "auth-key\n"
  735. "-----BEGIN ED25519 CERT-----\n"
  736. "AQkABmRZAVdPeZyzfCyUDC1fnYyom8eOS2O1opzTytEU7dlOf9brAQAgBABo2zfd\n"
  737. "wyqAdzSeaIzH1TUcV3u8nAG2YhNCRw2/2vVWuHVSGTrO1EM6Eu1jyOw/qtSS6Exf\n"
  738. "omV417y8uK2gHQ+1FWqg/KaogELYzDG6pcj2NkziovnIfET0W7nZB85YjwQ=\n"
  739. "-----END ED25519 CERT-----\n"
  740. "enc-key ntor MbxzxI1K+zcl7e+wysLK96UZWwFEJQqI0G7b0muRXx4=\n"
  741. "enc-key-certification\n"
  742. "-----BEGIN ED25519 CERT-----\n"
  743. "AQsABmRZATUYQypFY7pr8FpmV61pcqUylt5fEr/QLfavfcwbzlA7AQAgBADimELh\n"
  744. "lLZvy/LjXnCdpvaVRhiGBeIRAGIDGz1SY/zD6BAnpDL420ha2TdvdGsg8cgfTcJZ\n"
  745. "g84x85+zhuh8kkdgt7bOmjOXLlButDCfTarMgCfy6pSI/hUckk+j5Q43uws=\n"
  746. "-----END ED25519 CERT-----\n"
  747. "introduction-point AQIUMDI5OUYyNjhGQ0E5RDU1Q0QxNTc=\n"
  748. "auth-key\n"
  749. "-----BEGIN ED25519 CERT-----\n"
  750. "AQkABmRZASnpBjHsw0Gpvi+KNlW4ouXegIsUBHMvJN1CQHDTLdfnAQAgBABo2zfd\n"
  751. "wyqAdzSeaIzH1TUcV3u8nAG2YhNCRw2/2vVWuOlbHs8s8LAeEb36urVKTJ5exgss\n"
  752. "V+ylIwHSWF0qanCnnTnDyNg/3YRUo0AZr0d/CoiNV+XsGE4Vuho/TBVC+wY=\n"
  753. "-----END ED25519 CERT-----\n"
  754. "enc-key legacy\n"
  755. "-----BEGIN RSA PUBLIC KEY-----\n"
  756. "MIGJAoGBALttUA1paMCQiuIZeCp26REztziej5dN0o6/kTU//ItT4MGxTfmnLmcq\n"
  757. "WpvK4jdX1h2OlDCZmtA7sb0HOkjELgrDU0ATVwOaeG+3icSddmQyaeT8+cxQEktj\n"
  758. "SXMQ+iJDxJIIWFPmLmWWQHqb4IRfl021l3iTErhtZKBz37JNK7E/AgMBAAE=\n"
  759. "-----END RSA PUBLIC KEY-----\n"
  760. "enc-key-certification\n"
  761. "-----BEGIN CROSSCERT-----\n"
  762. "NRhDKkVjumvwWmZXrWlypTKW3l8Sv9At9q99zBvOUDsABmRZgBROMZr2Mhj8H8zd\n"
  763. "xbU6ZvDUwD9xkptNHq0W04CyWb8p0y56y89y2kBF6RrSrVBJCyaHyph6Bmi5z0Lc\n"
  764. "f4jjakRlHwB7oYqSo7l8EE9DGE0rEat3hNhN+tBIAJL5gKOL4dgfD5gMi51zzSFl\n"
  765. "epv8idTwhqZ/sxRMUIQrb9AB8sOD\n"
  766. "-----END CROSSCERT-----\n"
  767. "introduction-point AQIUMDI5OUYyNjhGQ0E5RDU1Q0QxNTc=\n"
  768. "auth-key\n"
  769. "-----BEGIN ED25519 CERT-----\n"
  770. "AQkABmRZAdBFQcE23cIoCMFTycnQ1st2752vdjGME+QPMTTxvqZhAQAgBABo2zfd\n"
  771. "wyqAdzSeaIzH1TUcV3u8nAG2YhNCRw2/2vVWuOGXGPnb3g9J8aSyN7jYs71ET0wC\n"
  772. "TlDLcXCgAMnKA6of/a4QceFfAFsCnI3qCd8YUo5NYCMh2d5mtFpLK41Wpwo=\n"
  773. "-----END ED25519 CERT-----\n"
  774. "enc-key legacy\n"
  775. "-----BEGIN RSA PUBLIC KEY-----\n"
  776. "MIGJAoGBALuyEVMz4GwZ8LnBYxLZDHNg1DHUZJZNmE7HsQDcM/FYeZ1LjYLe/K8s\n"
  777. "BFzgFmjMU1ondIWGWpRCLYcZxQMZaSU0ObdezDwelTkHo/u7K2fQTLmI9EofcsK0\n"
  778. "4OkY6eo8BFtXXoQJhAw5WatRpzah2sGqMPXs2jr7Ku4Pd8JuRd35AgMBAAE=\n"
  779. "-----END RSA PUBLIC KEY-----\n"
  780. "enc-key-certification\n"
  781. "-----BEGIN CROSSCERT-----\n"
  782. "NRhDKkVjumvwWmZXrWlypTKW3l8Sv9At9q99zBvOUDsABmRZgGwpo67ybC7skFYk\n"
  783. "JjvqcbrKg8Fwrvue9yF66p1O90fqziVsvpKGcsr3tcIJHtNsrWVRDpyFwnc1wlVE\n"
  784. "O7rHftF4GUsKaoz3wxxmb0YyyYVQvLpH0Y6lFIvw8nGurnsMefQWLcxuEX7xZOPl\n"
  785. "VAlVp+XtJE1ZNQ62hpnNgBDi1ikJ\n"
  786. "-----END CROSSCERT-----";
  787. static void
  788. test_decode_multiple_intro_points(void *arg)
  789. {
  790. int ret;
  791. hs_descriptor_t *desc = NULL;
  792. (void) arg;
  793. {
  794. /* Build a descriptor with no intro points. */
  795. desc = helper_build_hs_desc(1);
  796. tt_assert(desc);
  797. }
  798. ret = decode_intro_points(desc, &desc->encrypted_data,
  799. encrypted_desc_portion);
  800. tt_int_op(ret, ==, 0);
  801. tt_int_op(smartlist_len(desc->encrypted_data.intro_points), ==, 4);
  802. done:
  803. ;
  804. }
  805. static void
  806. test_free_objects(void *arg)
  807. {
  808. (void) arg;
  809. {
  810. const char u[] = { 'U', 'U', 'U', 'U' };
  811. hs_desc_plaintext_data_t *data = tor_malloc_zero(sizeof(*data));
  812. /* Set a memory marker so we know if the data was properly wiped. */
  813. memset(&data->version, 'U', sizeof(data->version));
  814. hs_desc_plaintext_data_free(data);
  815. tt_mem_op(&data->version, OP_NE, u, sizeof(u));
  816. }
  817. {
  818. hs_desc_encrypted_data_t *data = tor_malloc_zero(sizeof(*data));
  819. /* Set a memory marker so we know if the data was properly wiped. */
  820. data->create2_ntor = 1;
  821. hs_desc_encrypted_data_free(data);
  822. tt_int_op(data->create2_ntor, OP_NE, 1);
  823. }
  824. done:
  825. ;
  826. }
  827. static void
  828. test_decode_plaintext(void *arg)
  829. {
  830. int ret;
  831. hs_desc_plaintext_data_t desc_plaintext;
  832. const char *bad_value = "unicorn";
  833. (void) arg;
  834. #define template \
  835. "hs-descriptor %s\n" \
  836. "descriptor-lifetime %s\n" \
  837. "descriptor-signing-key-cert\n" \
  838. "-----BEGIN ED25519 CERT-----\n" \
  839. "AQgABjvPAQaG3g+dc6oV/oJV4ODAtkvx56uBnPtBT9mYVuHVOhn7AQAgBABUg3mQ\n" \
  840. "myBr4bu5LCr53wUEbW2EXui01CbUgU7pfo9LvJG3AcXRojj6HlfsUs9BkzYzYdjF\n" \
  841. "A69Apikgu0ewHYkFFASt7Il+gB3w6J8YstQJZT7dtbtl+doM7ug8B68Qdg8=\n" \
  842. "-----END ED25519 CERT-----\n" \
  843. "revision-counter %s\n" \
  844. "encrypted\n" \
  845. "-----BEGIN %s-----\n" \
  846. "UNICORN\n" \
  847. "-----END MESSAGE-----\n" \
  848. "signature m20WJH5agqvwhq7QeuEZ1mYyPWQDO+eJOZUjLhAiKu8DbL17DsDfJE6kXbWy" \
  849. "HimbNj2we0enV3cCOOAsmPOaAw\n"
  850. /* Invalid version. */
  851. {
  852. char *plaintext;
  853. tor_asprintf(&plaintext, template, bad_value, "180", "42", "MESSAGE");
  854. ret = hs_desc_decode_plaintext(plaintext, &desc_plaintext);
  855. tor_free(plaintext);
  856. tt_int_op(ret, OP_EQ, -1);
  857. }
  858. /* Missing fields. */
  859. {
  860. const char *plaintext = "hs-descriptor 3\n";
  861. ret = hs_desc_decode_plaintext(plaintext, &desc_plaintext);
  862. tt_int_op(ret, OP_EQ, -1);
  863. }
  864. /* Max length. */
  865. {
  866. size_t big = 64000;
  867. /* Must always be bigger than HS_DESC_MAX_LEN. */
  868. tt_int_op(HS_DESC_MAX_LEN, <, big);
  869. char *plaintext = tor_malloc_zero(big);
  870. memset(plaintext, 'a', big);
  871. ret = hs_desc_decode_plaintext(plaintext, &desc_plaintext);
  872. tor_free(plaintext);
  873. tt_int_op(ret, OP_EQ, -1);
  874. }
  875. /* Bad lifetime value. */
  876. {
  877. char *plaintext;
  878. tor_asprintf(&plaintext, template, "3", bad_value, "42", "MESSAGE");
  879. ret = hs_desc_decode_plaintext(plaintext, &desc_plaintext);
  880. tt_int_op(ret, OP_EQ, -1);
  881. }
  882. /* Huge lifetime value. */
  883. {
  884. char *plaintext;
  885. tor_asprintf(&plaintext, template, "3", "7181615", "42", "MESSAGE");
  886. ret = hs_desc_decode_plaintext(plaintext, &desc_plaintext);
  887. tt_int_op(ret, OP_EQ, -1);
  888. }
  889. /* Invalid encrypted section. */
  890. {
  891. char *plaintext;
  892. tor_asprintf(&plaintext, template, "3", "180", "42", bad_value);
  893. ret = hs_desc_decode_plaintext(plaintext, &desc_plaintext);
  894. tt_int_op(ret, OP_EQ, -1);
  895. }
  896. /* Invalid revision counter. */
  897. {
  898. char *plaintext;
  899. tor_asprintf(&plaintext, template, "3", "180", bad_value, "MESSAGE");
  900. ret = hs_desc_decode_plaintext(plaintext, &desc_plaintext);
  901. tt_int_op(ret, OP_EQ, -1);
  902. }
  903. done:
  904. ;
  905. }
  906. static void
  907. test_validate_cert(void *arg)
  908. {
  909. int ret;
  910. time_t now = time(NULL);
  911. ed25519_keypair_t kp;
  912. (void) arg;
  913. ret = ed25519_keypair_generate(&kp, 0);
  914. tt_int_op(ret, ==, 0);
  915. /* Cert of type CERT_TYPE_AUTH_HS_IP_KEY. */
  916. tor_cert_t *cert = tor_cert_create(&kp, CERT_TYPE_AUTH_HS_IP_KEY,
  917. &kp.pubkey, now, 3600,
  918. CERT_FLAG_INCLUDE_SIGNING_KEY);
  919. tt_assert(cert);
  920. /* Test with empty certificate. */
  921. ret = cert_is_valid(NULL, CERT_TYPE_AUTH_HS_IP_KEY, "unicorn");
  922. tt_int_op(ret, OP_EQ, 0);
  923. /* Test with a bad type. */
  924. ret = cert_is_valid(cert, CERT_TYPE_SIGNING_HS_DESC, "unicorn");
  925. tt_int_op(ret, OP_EQ, 0);
  926. /* Normal validation. */
  927. ret = cert_is_valid(cert, CERT_TYPE_AUTH_HS_IP_KEY, "unicorn");
  928. tt_int_op(ret, OP_EQ, 1);
  929. /* Break signing key so signature verification will fails. */
  930. memset(&cert->signing_key, 0, sizeof(cert->signing_key));
  931. ret = cert_is_valid(cert, CERT_TYPE_AUTH_HS_IP_KEY, "unicorn");
  932. tt_int_op(ret, OP_EQ, 0);
  933. tor_cert_free(cert);
  934. /* Try a cert without including the signing key. */
  935. cert = tor_cert_create(&kp, CERT_TYPE_AUTH_HS_IP_KEY, &kp.pubkey, now, 3600, 0);
  936. tt_assert(cert);
  937. /* Test with a bad type. */
  938. ret = cert_is_valid(cert, CERT_TYPE_AUTH_HS_IP_KEY, "unicorn");
  939. tt_int_op(ret, OP_EQ, 0);
  940. done:
  941. ;
  942. }
  943. static void
  944. test_desc_signature(void *arg)
  945. {
  946. int ret;
  947. char *data, *desc;
  948. char sig_b64[ED25519_SIG_BASE64_LEN + 1];
  949. ed25519_keypair_t kp;
  950. ed25519_signature_t sig;
  951. (void) arg;
  952. ed25519_keypair_generate(&kp, 0);
  953. /* Setup a phoony descriptor but with a valid signature token that is the
  954. * signature is verifiable. */
  955. tor_asprintf(&data, "This is a signed descriptor\n");
  956. ret = ed25519_sign_prefixed(&sig, (const uint8_t *) data, strlen(data),
  957. "Tor onion service descriptor sig v3", &kp);
  958. tt_int_op(ret, ==, 0);
  959. ret = ed25519_signature_to_base64(sig_b64, &sig);
  960. tt_int_op(ret, ==, 0);
  961. /* Build the descriptor that should be valid. */
  962. tor_asprintf(&desc, "%ssignature %s\n", data, sig_b64);
  963. ret = desc_sig_is_valid(sig_b64, &kp, desc, strlen(desc));
  964. tt_int_op(ret, ==, 1);
  965. /* Junk signature. */
  966. ret = desc_sig_is_valid("JUNK", &kp, desc, strlen(desc));
  967. tt_int_op(ret, ==, 0);
  968. done:
  969. tor_free(desc);
  970. tor_free(data);
  971. }
  972. struct testcase_t hs_descriptor[] = {
  973. /* Encoding tests. */
  974. { "cert_encoding", test_cert_encoding, TT_FORK,
  975. NULL, NULL },
  976. { "link_specifier", test_link_specifier, TT_FORK,
  977. NULL, NULL },
  978. { "encode_descriptor", test_encode_descriptor, TT_FORK,
  979. NULL, NULL },
  980. { "descriptor_padding", test_descriptor_padding, TT_FORK,
  981. NULL, NULL },
  982. /* Decoding tests. */
  983. { "decode_descriptor", test_decode_descriptor, TT_FORK,
  984. NULL, NULL },
  985. { "encrypted_data_len", test_encrypted_data_len, TT_FORK,
  986. NULL, NULL },
  987. { "decode_intro_point", test_decode_intro_point, TT_FORK,
  988. NULL, NULL },
  989. { "decode_multiple_intro_points", test_decode_multiple_intro_points, TT_FORK,
  990. NULL, NULL },
  991. { "decode_plaintext", test_decode_plaintext, TT_FORK,
  992. NULL, NULL },
  993. /* Misc. */
  994. { "version", test_supported_version, TT_FORK,
  995. NULL, NULL },
  996. { "free_objects", test_free_objects, TT_FORK,
  997. NULL, NULL },
  998. { "validate_cert", test_validate_cert, TT_FORK,
  999. NULL, NULL },
  1000. { "desc_signature", test_desc_signature, TT_FORK,
  1001. NULL, NULL },
  1002. END_OF_TESTCASES
  1003. };