test_hs_descriptor.c 37 KB

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