hs_descriptor.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768
  1. /* Copyright (c) 2016, The Tor Project, Inc. */
  2. /* See LICENSE for licensing information */
  3. /**
  4. * \file hs_descriptor.c
  5. * \brief Handle hidden service descriptor encoding/decoding.
  6. **/
  7. #include "hs_descriptor.h"
  8. #include "or.h"
  9. #include "ed25519_cert.h" /* Trunnel interface. */
  10. /* Constant string value used for the descriptor format. */
  11. static const char *str_hs_desc = "hs-descriptor";
  12. static const char *str_desc_cert = "descriptor-signing-key-cert";
  13. static const char *str_rev_counter = "revision-counter";
  14. static const char *str_encrypted = "encrypted";
  15. static const char *str_signature = "signature";
  16. static const char *str_lifetime = "descriptor-lifetime";
  17. /* Constant string value for the encrypted part of the descriptor. */
  18. static const char *str_create2_formats = "create2-formats";
  19. static const char *str_auth_required = "authentication-required";
  20. static const char *str_intro_point = "introduction-point";
  21. static const char *str_ip_auth_key = "auth-key";
  22. static const char *str_ip_enc_key = "enc-key";
  23. static const char *str_ip_enc_key_cert = "enc-key-certification";
  24. /* Constant string value for the construction to encrypt the encrypted data
  25. * section. */
  26. static const char *str_enc_hsdir_data = "hsdir-encrypted-data";
  27. /* Encode the ed25519 certificate <b>cert</b> and put the newly allocated
  28. * string in <b>cert_str_out</b>. Return 0 on success else a negative value. */
  29. static int
  30. encode_cert(const tor_cert_t *cert, char **cert_str_out)
  31. {
  32. int ret = -1;
  33. char *ed_cert_b64 = NULL;
  34. size_t ed_cert_b64_len;
  35. tor_assert(cert);
  36. tor_assert(cert_str_out);
  37. /* Get the encoded size and add the NUL byte. */
  38. ed_cert_b64_len = base64_encode_size(cert->encoded_len,
  39. BASE64_ENCODE_MULTILINE) + 1;
  40. ed_cert_b64 = tor_malloc_zero(ed_cert_b64_len);
  41. /* Base64 encode the encoded certificate. */
  42. if (base64_encode(ed_cert_b64, ed_cert_b64_len,
  43. (const char *) cert->encoded, cert->encoded_len,
  44. BASE64_ENCODE_MULTILINE) < 0) {
  45. log_err(LD_BUG, "Couldn't base64-encode descriptor signing key cert!");
  46. goto err;
  47. }
  48. /* Put everything together in a NUL terminated string. */
  49. tor_asprintf(cert_str_out,
  50. "-----BEGIN ED25519 CERT-----\n"
  51. "%s"
  52. "-----END ED25519 CERT-----",
  53. ed_cert_b64);
  54. /* Success! */
  55. ret = 0;
  56. err:
  57. tor_free(ed_cert_b64);
  58. return ret;
  59. }
  60. /* Encode the given link specifier objects into a newly allocated string.
  61. * This can't fail so caller can always assume a valid string being
  62. * returned. */
  63. static char *
  64. encode_link_specifiers(const smartlist_t *specs)
  65. {
  66. char *encoded_b64 = NULL;
  67. link_specifier_list_t *lslist = link_specifier_list_new();
  68. tor_assert(specs);
  69. /* No link specifiers is a code flow error, can't happen. */
  70. tor_assert(smartlist_len(specs) > 0);
  71. tor_assert(smartlist_len(specs) <= UINT8_MAX);
  72. link_specifier_list_set_n_spec(lslist, smartlist_len(specs));
  73. SMARTLIST_FOREACH_BEGIN(specs, const hs_desc_link_specifier_t *,
  74. spec) {
  75. link_specifier_t *ls = link_specifier_new();
  76. link_specifier_set_ls_type(ls, spec->type);
  77. switch (spec->type) {
  78. case LS_IPV4:
  79. link_specifier_set_un_ipv4_addr(ls,
  80. tor_addr_to_ipv4h(&spec->u.ap.addr));
  81. link_specifier_set_un_ipv4_port(ls, spec->u.ap.port);
  82. /* Four bytes IPv4 and two bytes port. */
  83. link_specifier_set_ls_len(ls, sizeof(spec->u.ap.addr.addr.in_addr) +
  84. sizeof(spec->u.ap.port));
  85. break;
  86. case LS_IPV6:
  87. {
  88. size_t addr_len = link_specifier_getlen_un_ipv6_addr(ls);
  89. const uint8_t *in6_addr = tor_addr_to_in6_addr8(&spec->u.ap.addr);
  90. uint8_t *ipv6_array = link_specifier_getarray_un_ipv6_addr(ls);
  91. memcpy(ipv6_array, in6_addr, addr_len);
  92. link_specifier_set_un_ipv6_port(ls, spec->u.ap.port);
  93. /* Sixteen bytes IPv6 and two bytes port. */
  94. link_specifier_set_ls_len(ls, addr_len + sizeof(spec->u.ap.port));
  95. break;
  96. }
  97. case LS_LEGACY_ID:
  98. {
  99. size_t legacy_id_len = link_specifier_getlen_un_legacy_id(ls);
  100. uint8_t *legacy_id_array = link_specifier_getarray_un_legacy_id(ls);
  101. memcpy(legacy_id_array, spec->u.legacy_id, legacy_id_len);
  102. link_specifier_set_ls_len(ls, legacy_id_len);
  103. break;
  104. }
  105. default:
  106. tor_assert(0);
  107. }
  108. link_specifier_list_add_spec(lslist, ls);
  109. } SMARTLIST_FOREACH_END(spec);
  110. {
  111. uint8_t *encoded;
  112. ssize_t encoded_len, encoded_b64_len, ret;
  113. encoded_len = link_specifier_list_encoded_len(lslist);
  114. tor_assert(encoded_len > 0);
  115. encoded = tor_malloc_zero(encoded_len);
  116. ret = link_specifier_list_encode(encoded, encoded_len, lslist);
  117. tor_assert(ret == encoded_len);
  118. /* Base64 encode our binary format. Add extra NUL byte for the base64
  119. * encoded value. */
  120. encoded_b64_len = base64_encode_size(encoded_len, 0) + 1;
  121. encoded_b64 = tor_malloc_zero(encoded_b64_len);
  122. ret = base64_encode(encoded_b64, encoded_b64_len, (const char *) encoded,
  123. encoded_len, 0);
  124. tor_assert(ret == (encoded_b64_len - 1));
  125. tor_free(encoded);
  126. }
  127. link_specifier_list_free(lslist);
  128. return encoded_b64;
  129. }
  130. /* Encode an introduction point encryption key and return a newly allocated
  131. * string with it. On failure, return NULL. */
  132. static char *
  133. encode_enc_key(const ed25519_keypair_t *sig_key,
  134. const hs_desc_intro_point_t *ip)
  135. {
  136. char *encoded = NULL;
  137. time_t now = time(NULL);
  138. tor_assert(sig_key);
  139. tor_assert(ip);
  140. switch (ip->enc_key_type) {
  141. case HS_DESC_KEY_TYPE_LEGACY:
  142. {
  143. char *key_str, b64_cert[256];
  144. ssize_t cert_len;
  145. size_t key_str_len;
  146. uint8_t *cert_data;
  147. /* Create cross certification cert. */
  148. cert_len = tor_make_rsa_ed25519_crosscert(&sig_key->pubkey,
  149. ip->enc_key.legacy,
  150. now + HS_DESC_CERT_LIFETIME,
  151. &cert_data);
  152. if (cert_len < 0) {
  153. log_warn(LD_REND, "Unable to create legacy crosscert.");
  154. goto err;
  155. }
  156. /* Encode cross cert. */
  157. if (base64_encode(b64_cert, sizeof(b64_cert), (const char *) cert_data,
  158. cert_len, BASE64_ENCODE_MULTILINE) < 0) {
  159. log_warn(LD_REND, "Unable to encode legacy crosscert.");
  160. goto err;
  161. }
  162. /* Convert the encryption key to a string. */
  163. if (crypto_pk_write_public_key_to_string(ip->enc_key.legacy, &key_str,
  164. &key_str_len) < 0) {
  165. log_warn(LD_REND, "Unable to encode legacy encryption key.");
  166. goto err;
  167. }
  168. tor_asprintf(&encoded,
  169. "%s legacy\n%s" /* Newline is added by the call above. */
  170. "%s\n"
  171. "-----BEGIN CROSSCERT-----\n"
  172. "%s"
  173. "-----END CROSSCERT-----",
  174. str_ip_enc_key, key_str,
  175. str_ip_enc_key_cert, b64_cert);
  176. tor_free(key_str);
  177. break;
  178. }
  179. case HS_DESC_KEY_TYPE_CURVE25519:
  180. {
  181. int signbit;
  182. char *encoded_cert, key_fp_b64[CURVE25519_BASE64_PADDED_LEN + 1];
  183. ed25519_keypair_t curve_kp;
  184. if (ed25519_keypair_from_curve25519_keypair(&curve_kp, &signbit,
  185. &ip->enc_key.curve25519)) {
  186. goto err;
  187. }
  188. tor_cert_t *cross_cert = tor_cert_create(&curve_kp, CERT_TYPE_HS_IP_ENC,
  189. &sig_key->pubkey, now,
  190. HS_DESC_CERT_LIFETIME,
  191. CERT_FLAG_INCLUDE_SIGNING_KEY);
  192. memwipe(&curve_kp, 0, sizeof(curve_kp));
  193. if (!cross_cert) {
  194. goto err;
  195. }
  196. if (encode_cert(cross_cert, &encoded_cert)) {
  197. goto err;
  198. }
  199. if (curve25519_public_to_base64(key_fp_b64,
  200. &ip->enc_key.curve25519.pubkey) < 0) {
  201. tor_free(encoded_cert);
  202. goto err;
  203. }
  204. tor_asprintf(&encoded,
  205. "%s ntor %s\n"
  206. "%s\n%s",
  207. str_ip_enc_key, key_fp_b64,
  208. str_ip_enc_key_cert, encoded_cert);
  209. tor_free(encoded_cert);
  210. break;
  211. }
  212. default:
  213. tor_assert(0);
  214. }
  215. err:
  216. return encoded;
  217. }
  218. /* Encode an introduction point object and return a newly allocated string
  219. * with it. On failure, return NULL. */
  220. static char *
  221. encode_intro_point(const ed25519_keypair_t *sig_key,
  222. const hs_desc_intro_point_t *ip)
  223. {
  224. char *encoded_ip = NULL;
  225. smartlist_t *lines = smartlist_new();
  226. tor_assert(ip);
  227. tor_assert(sig_key);
  228. /* Encode link specifier. */
  229. {
  230. char *ls_str = encode_link_specifiers(ip->link_specifiers);
  231. smartlist_add_asprintf(lines, "%s %s", str_intro_point, ls_str);
  232. tor_free(ls_str);
  233. }
  234. /* Authentication key encoding. */
  235. {
  236. char *encoded_cert;
  237. if (encode_cert(ip->auth_key_cert, &encoded_cert) < 0) {
  238. goto err;
  239. }
  240. smartlist_add_asprintf(lines, "%s\n%s", str_ip_auth_key, encoded_cert);
  241. tor_free(encoded_cert);
  242. }
  243. /* Encryption key encoding. */
  244. {
  245. char *encoded_enc_key = encode_enc_key(sig_key, ip);
  246. if (encoded_enc_key == NULL) {
  247. goto err;
  248. }
  249. smartlist_add_asprintf(lines, "%s", encoded_enc_key);
  250. tor_free(encoded_enc_key);
  251. }
  252. /* Join them all in one blob of text. */
  253. encoded_ip = smartlist_join_strings(lines, "\n", 1, NULL);
  254. err:
  255. SMARTLIST_FOREACH(lines, char *, l, tor_free(l));
  256. smartlist_free(lines);
  257. return encoded_ip;
  258. }
  259. /* Using a given decriptor object, build the secret input needed for the
  260. * KDF and put it in the dst pointer which is an already allocated buffer
  261. * of size dstlen. */
  262. static void
  263. build_secret_input(const hs_descriptor_t *desc, uint8_t *dst, size_t dstlen)
  264. {
  265. size_t offset = 0;
  266. tor_assert(desc);
  267. tor_assert(dst);
  268. tor_assert(HS_DESC_ENCRYPTED_SECRET_INPUT_LEN <= dstlen);
  269. /* XXX use the destination length as the memcpy length */
  270. /* Copy blinded public key. */
  271. memcpy(dst, desc->plaintext_data.blinded_kp.pubkey.pubkey,
  272. sizeof(desc->plaintext_data.blinded_kp.pubkey.pubkey));
  273. offset += sizeof(desc->plaintext_data.blinded_kp.pubkey.pubkey);
  274. /* Copy subcredential. */
  275. memcpy(dst + offset, desc->subcredential, sizeof(desc->subcredential));
  276. offset += sizeof(desc->subcredential);
  277. /* Copy revision counter value. */
  278. set_uint64(dst + offset, tor_ntohll(desc->plaintext_data.revision_counter));
  279. offset += sizeof(uint64_t);
  280. tor_assert(HS_DESC_ENCRYPTED_SECRET_INPUT_LEN == offset);
  281. }
  282. /* Do the KDF construction and put the resulting data in key_out which is of
  283. * key_out_len length. It uses SHAKE-256 as specified in the spec. */
  284. static void
  285. build_kdf_key(const hs_descriptor_t *desc,
  286. const uint8_t *salt, size_t salt_len,
  287. uint8_t *key_out, size_t key_out_len)
  288. {
  289. uint8_t secret_input[HS_DESC_ENCRYPTED_SECRET_INPUT_LEN];
  290. crypto_xof_t *xof;
  291. tor_assert(desc);
  292. tor_assert(salt);
  293. tor_assert(key_out);
  294. /* Build the secret input for the KDF computation. */
  295. build_secret_input(desc, secret_input, sizeof(secret_input));
  296. xof = crypto_xof_new();
  297. /* Feed our KDF. [SHAKE it like a polaroid picture --Yawning]. */
  298. crypto_xof_add_bytes(xof, secret_input, sizeof(secret_input));
  299. crypto_xof_add_bytes(xof, salt, salt_len);
  300. crypto_xof_add_bytes(xof, (const uint8_t *) str_enc_hsdir_data,
  301. strlen(str_enc_hsdir_data));
  302. /* Eat from our KDF. */
  303. crypto_xof_squeeze_bytes(xof, key_out, key_out_len);
  304. crypto_xof_free(xof);
  305. memwipe(secret_input, 0, sizeof(secret_input));
  306. }
  307. /* Using the given descriptor and salt, run it through our KDF function and
  308. * then extract a secret key in key_out, the IV in iv_out and MAC in mac_out.
  309. * This function can't fail. */
  310. static void
  311. build_secret_key_iv_mac(const hs_descriptor_t *desc,
  312. const uint8_t *salt, size_t salt_len,
  313. uint8_t *key_out, size_t key_len,
  314. uint8_t *iv_out, size_t iv_len,
  315. uint8_t *mac_out, size_t mac_len)
  316. {
  317. size_t offset = 0;
  318. uint8_t kdf_key[HS_DESC_ENCRYPTED_KDF_OUTPUT_LEN];
  319. tor_assert(desc);
  320. tor_assert(salt);
  321. tor_assert(key_out);
  322. tor_assert(iv_out);
  323. tor_assert(mac_out);
  324. build_kdf_key(desc, salt, salt_len, kdf_key, sizeof(kdf_key));
  325. /* Copy the bytes we need for both the secret key and IV. */
  326. memcpy(key_out, kdf_key, key_len);
  327. offset += key_len;
  328. memcpy(iv_out, kdf_key + offset, iv_len);
  329. offset += iv_len;
  330. memcpy(mac_out, kdf_key + offset, mac_len);
  331. /* Extra precaution to make sure we are not out of bound. */
  332. tor_assert((offset + mac_len) == sizeof(kdf_key));
  333. memwipe(kdf_key, 0, sizeof(kdf_key));
  334. }
  335. /* Using a key, salt and encrypted payload, build a MAC and put it in mac_out.
  336. * The length of the mac key and salt must be fixed and if not, you can't rely
  337. * on the result to be a valid MAC. We use SHA3-256 for the MAC computation.
  338. * This function can't fail. */
  339. static void
  340. build_mac(const uint8_t *mac_key, size_t mac_key_len,
  341. const uint8_t *salt, size_t salt_len,
  342. const uint8_t *encrypted, size_t encrypted_len,
  343. uint8_t *mac_out, size_t mac_len)
  344. {
  345. crypto_digest_t *digest;
  346. tor_assert(mac_key);
  347. tor_assert(salt);
  348. tor_assert(encrypted);
  349. tor_assert(mac_out);
  350. digest = crypto_digest256_new(DIGEST_SHA3_256);
  351. /* As specified in section 2.5 of proposal 224, first add the mac key
  352. * then add the salt first and then the encrypted section. */
  353. crypto_digest_add_bytes(digest, (const char *) mac_key, mac_key_len);
  354. crypto_digest_add_bytes(digest, (const char *) salt, salt_len);
  355. crypto_digest_add_bytes(digest, (const char *) encrypted, encrypted_len);
  356. crypto_digest_get_digest(digest, (char *) mac_out, mac_len);
  357. crypto_digest_free(digest);
  358. }
  359. /* Given a source length, return the new size including padding for the
  360. * plaintext encryption. */
  361. static size_t
  362. compute_padded_plaintext_length(size_t plaintext_len)
  363. {
  364. size_t plaintext_padded_len;
  365. /* Make sure we won't overflow. */
  366. tor_assert(plaintext_len <=
  367. (SIZE_T_CEILING - HS_DESC_PLAINTEXT_PADDING_MULTIPLE));
  368. /* Get the extra length we need to add. For example, if srclen is 234 bytes,
  369. * this will expand to (2 * 128) == 256 thus an extra 22 bytes. */
  370. plaintext_padded_len = CEIL_DIV(plaintext_len,
  371. HS_DESC_PLAINTEXT_PADDING_MULTIPLE) *
  372. HS_DESC_PLAINTEXT_PADDING_MULTIPLE;
  373. /* Can never be extra careful. Make sure we are _really_ padded. */
  374. tor_assert(!(plaintext_padded_len % HS_DESC_PLAINTEXT_PADDING_MULTIPLE));
  375. return plaintext_padded_len;
  376. }
  377. /* Given a buffer, pad it up to the encrypted section padding requirement. Set
  378. * the newly allocated string in padded_out and return the length of the
  379. * padded buffer. */
  380. static size_t
  381. build_plaintext_padding(const char *plaintext, size_t plaintext_len,
  382. uint8_t **padded_out)
  383. {
  384. size_t padded_len;
  385. uint8_t *padded;
  386. tor_assert(plaintext);
  387. tor_assert(padded_out);
  388. /* Allocate the final length including padding. */
  389. padded_len = compute_padded_plaintext_length(plaintext_len);
  390. tor_assert(padded_len >= plaintext_len);
  391. padded = tor_malloc_zero(padded_len);
  392. memcpy(padded, plaintext, plaintext_len);
  393. *padded_out = padded;
  394. return padded_len;
  395. }
  396. /* Using a key, IV and plaintext data of length plaintext_len, create the
  397. * encrypted section by encrypting it and setting encrypted_out with the
  398. * data. Return size of the encrypted data buffer. */
  399. static size_t
  400. build_encrypted(const uint8_t *key, const uint8_t *iv, const char *plaintext,
  401. size_t plaintext_len, uint8_t **encrypted_out)
  402. {
  403. size_t encrypted_len;
  404. uint8_t *padded_plaintext, *encrypted;
  405. crypto_cipher_t *cipher;
  406. tor_assert(key);
  407. tor_assert(iv);
  408. tor_assert(plaintext);
  409. tor_assert(encrypted_out);
  410. /* This creates a cipher for AES128. It can't fail. */
  411. cipher = crypto_cipher_new_with_iv((const char *) key, (const char *) iv);
  412. /* This can't fail. */
  413. encrypted_len = build_plaintext_padding(plaintext, plaintext_len,
  414. &padded_plaintext);
  415. /* Extra precautions that we have a valie padding length. */
  416. tor_assert(encrypted_len <= HS_DESC_PADDED_PLAINTEXT_MAX_LEN);
  417. tor_assert(!(encrypted_len % HS_DESC_PLAINTEXT_PADDING_MULTIPLE));
  418. /* We use a stream cipher so the encrypted length will be the same as the
  419. * plaintext padded length. */
  420. encrypted = tor_malloc_zero(encrypted_len);
  421. /* This can't fail. */
  422. crypto_cipher_encrypt(cipher, (char *) encrypted,
  423. (const char *) padded_plaintext, encrypted_len);
  424. *encrypted_out = encrypted;
  425. /* Cleanup. */
  426. crypto_cipher_free(cipher);
  427. tor_free(padded_plaintext);
  428. return encrypted_len;
  429. }
  430. /* Encrypt the given plaintext buffer and using the descriptor to get the
  431. * keys. Set encrypted_out with the encrypted data and return the length of
  432. * it. */
  433. static size_t
  434. encrypt_descriptor_data(const hs_descriptor_t *desc, const char *plaintext,
  435. char **encrypted_out)
  436. {
  437. char *final_blob;
  438. size_t encrypted_len, final_blob_len, offset = 0;
  439. uint8_t *encrypted;
  440. uint8_t salt[HS_DESC_ENCRYPTED_SALT_LEN];
  441. uint8_t secret_key[CIPHER_KEY_LEN], secret_iv[CIPHER_IV_LEN];
  442. uint8_t mac_key[DIGEST256_LEN], mac[DIGEST256_LEN];
  443. tor_assert(desc);
  444. tor_assert(plaintext);
  445. tor_assert(encrypted_out);
  446. /* Get our salt. The returned bytes are already hashed. */
  447. crypto_strongest_rand(salt, sizeof(salt));
  448. /* KDF construction resulting in a key from which the secret key, IV and MAC
  449. * key are extracted which is what we need for the encryption. */
  450. build_secret_key_iv_mac(desc, salt, sizeof(salt),
  451. secret_key, sizeof(secret_key),
  452. secret_iv, sizeof(secret_iv),
  453. mac_key, sizeof(mac_key));
  454. /* Build the encrypted part that is do the actual encryption. */
  455. encrypted_len = build_encrypted(secret_key, secret_iv, plaintext,
  456. strlen(plaintext), &encrypted);
  457. memwipe(secret_key, 0, sizeof(secret_key));
  458. memwipe(secret_iv, 0, sizeof(secret_iv));
  459. /* This construction is specified in section 2.5 of proposal 224. */
  460. final_blob_len = sizeof(salt) + encrypted_len + DIGEST256_LEN;
  461. final_blob = tor_malloc_zero(final_blob_len);
  462. /* Build the MAC. */
  463. build_mac(mac_key, sizeof(mac_key), salt, sizeof(salt),
  464. encrypted, encrypted_len, mac, sizeof(mac));
  465. memwipe(mac_key, 0, sizeof(mac_key));
  466. /* The salt is the first value. */
  467. memcpy(final_blob, salt, sizeof(salt));
  468. offset = sizeof(salt);
  469. /* Second value is the encrypted data. */
  470. memcpy(final_blob + offset, encrypted, encrypted_len);
  471. offset += encrypted_len;
  472. /* Third value is the MAC. */
  473. memcpy(final_blob + offset, mac, sizeof(mac));
  474. offset += sizeof(mac);
  475. /* Cleanup the buffers. */
  476. memwipe(salt, 0, sizeof(salt));
  477. memwipe(encrypted, 0, encrypted_len);
  478. tor_free(encrypted);
  479. /* Extra precaution. */
  480. tor_assert(offset == final_blob_len);
  481. *encrypted_out = final_blob;
  482. return final_blob_len;
  483. }
  484. /* Take care of encoding the encrypted data section and then encrypting it
  485. * with the descriptor's key. A newly allocated NUL terminated string pointer
  486. * containing the encrypted encoded blob is put in encrypted_blob_out. Return
  487. * 0 on success else a negative value. */
  488. static int
  489. encode_encrypted_data(const hs_descriptor_t *desc,
  490. char **encrypted_blob_out)
  491. {
  492. int ret = -1;
  493. char *encoded_str, *encrypted_blob;
  494. smartlist_t *lines = smartlist_new();
  495. tor_assert(desc);
  496. tor_assert(encrypted_blob_out);
  497. /* Build the start of the section prior to the introduction points. */
  498. {
  499. if (!desc->encrypted_data.create2_ntor) {
  500. log_err(LD_BUG, "HS desc doesn't have recognized handshake type.");
  501. goto err;
  502. }
  503. smartlist_add_asprintf(lines, "%s %d\n", str_create2_formats,
  504. ONION_HANDSHAKE_TYPE_NTOR);
  505. if (desc->encrypted_data.auth_types &&
  506. smartlist_len(desc->encrypted_data.auth_types)) {
  507. /* Put the authentication-required line. */
  508. char *buf = smartlist_join_strings(desc->encrypted_data.auth_types, " ",
  509. 0, NULL);
  510. smartlist_add_asprintf(lines, "%s %s\n", str_auth_required, buf);
  511. tor_free(buf);
  512. }
  513. }
  514. /* Build the introduction point(s) section. */
  515. SMARTLIST_FOREACH_BEGIN(desc->encrypted_data.intro_points,
  516. const hs_desc_intro_point_t *, ip) {
  517. char *encoded_ip = encode_intro_point(&desc->plaintext_data.signing_kp,
  518. ip);
  519. if (encoded_ip == NULL) {
  520. log_err(LD_BUG, "HS desc intro point is malformed.");
  521. goto err;
  522. }
  523. smartlist_add(lines, encoded_ip);
  524. } SMARTLIST_FOREACH_END(ip);
  525. /* Build the entire encrypted data section into one encoded plaintext and
  526. * then encrypt it. */
  527. encoded_str = smartlist_join_strings(lines, "", 0, NULL);
  528. /* Encrypt the section into an encrypted blob that we'll base64 encode
  529. * before returning it. */
  530. {
  531. char *enc_b64;
  532. ssize_t enc_b64_len, ret_len, enc_len;
  533. enc_len = encrypt_descriptor_data(desc, encoded_str, &encrypted_blob);
  534. tor_free(encoded_str);
  535. /* Get the encoded size plus a NUL terminating byte. */
  536. enc_b64_len = base64_encode_size(enc_len, BASE64_ENCODE_MULTILINE) + 1;
  537. enc_b64 = tor_malloc_zero(enc_b64_len);
  538. /* Base64 the encrypted blob before returning it. */
  539. ret_len = base64_encode(enc_b64, enc_b64_len, encrypted_blob, enc_len,
  540. BASE64_ENCODE_MULTILINE);
  541. /* Return length doesn't count the NUL byte. */
  542. tor_assert(ret_len == (enc_b64_len - 1));
  543. tor_free(encrypted_blob);
  544. *encrypted_blob_out = enc_b64;
  545. }
  546. /* Success! */
  547. ret = 0;
  548. err:
  549. SMARTLIST_FOREACH(lines, char *, l, tor_free(l));
  550. smartlist_free(lines);
  551. return ret;
  552. }
  553. /* Encode a v3 HS descriptor. Return 0 on success and set encoded_out to the
  554. * newly allocated string of the encoded descriptor. On error, -1 is returned
  555. * and encoded_out is untouched. */
  556. static int
  557. desc_encode_v3(const hs_descriptor_t *desc, char **encoded_out)
  558. {
  559. int ret = -1;
  560. char *encoded_str = NULL;
  561. size_t encoded_len;
  562. smartlist_t *lines = smartlist_new();
  563. tor_assert(desc);
  564. tor_assert(encoded_out);
  565. tor_assert(desc->plaintext_data.version == 3);
  566. /* Build the non-encrypted values. */
  567. {
  568. char *encoded_cert;
  569. /* Encode certificate then create the first line of the descriptor. */
  570. if (desc->plaintext_data.signing_key_cert->cert_type
  571. != CERT_TYPE_HS_DESC_SIGN) {
  572. log_err(LD_BUG, "HS descriptor signing key has an unexpected cert type "
  573. "(%d)", (int) desc->plaintext_data.signing_key_cert->cert_type);
  574. goto err;
  575. }
  576. if (encode_cert(desc->plaintext_data.signing_key_cert,
  577. &encoded_cert) < 0) {
  578. /* The function will print error logs. */
  579. goto err;
  580. }
  581. /* Create the hs descriptor line. */
  582. smartlist_add_asprintf(lines, "%s %" PRIu32, str_hs_desc,
  583. desc->plaintext_data.version);
  584. /* Add the descriptor lifetime line (in minutes). */
  585. smartlist_add_asprintf(lines, "%s %" PRIu32, str_lifetime,
  586. desc->plaintext_data.lifetime_sec / 60);
  587. /* Create the descriptor certificate line. */
  588. smartlist_add_asprintf(lines, "%s\n%s", str_desc_cert, encoded_cert);
  589. tor_free(encoded_cert);
  590. /* Create the revision counter line. */
  591. smartlist_add_asprintf(lines, "%s %" PRIu64, str_rev_counter,
  592. desc->plaintext_data.revision_counter);
  593. }
  594. /* Build the encrypted data section. */
  595. {
  596. char *enc_b64_blob;
  597. if (encode_encrypted_data(desc, &enc_b64_blob) < 0) {
  598. goto err;
  599. }
  600. smartlist_add_asprintf(lines,
  601. "%s\n"
  602. "-----BEGIN MESSAGE-----\n"
  603. "%s"
  604. "-----END MESSAGE-----",
  605. str_encrypted, enc_b64_blob);
  606. tor_free(enc_b64_blob);
  607. }
  608. /* Join all lines in one string so we can generate a signature and append
  609. * it to the descriptor. */
  610. encoded_str = smartlist_join_strings(lines, "\n", 1, &encoded_len);
  611. /* Sign all fields of the descriptor with our short term signing key. */
  612. {
  613. /* XXX: Add signature prefix. */
  614. ed25519_signature_t sig;
  615. char ed_sig_b64[ED25519_SIG_BASE64_LEN + 1];
  616. if (ed25519_sign(&sig, (const uint8_t *) encoded_str, encoded_len,
  617. &desc->plaintext_data.signing_kp) < 0) {
  618. log_warn(LD_BUG, "Can't sign encoded HS descriptor!");
  619. tor_free(encoded_str);
  620. goto err;
  621. }
  622. if (ed25519_signature_to_base64(ed_sig_b64, &sig) < 0) {
  623. log_warn(LD_BUG, "Can't base64 encode descriptor signature!");
  624. tor_free(encoded_str);
  625. goto err;
  626. }
  627. /* Create the signature line. */
  628. smartlist_add_asprintf(lines, "%s %s", str_signature, ed_sig_b64);
  629. }
  630. /* Free previous string that we used so compute the signature. */
  631. tor_free(encoded_str);
  632. encoded_str = smartlist_join_strings(lines, "\n", 1, NULL);
  633. *encoded_out = encoded_str;
  634. /* XXX: Decode the generated descriptor as an extra validation. */
  635. /* XXX: Trigger a control port event. */
  636. /* Success! */
  637. ret = 0;
  638. err:
  639. SMARTLIST_FOREACH(lines, char *, l, tor_free(l));
  640. smartlist_free(lines);
  641. return ret;
  642. }
  643. /* Table of encode function version specific. The function are indexed by the
  644. * version number so v3 callback is at index 3 in the array. */
  645. static int
  646. (*encode_handlers[])(
  647. const hs_descriptor_t *desc,
  648. char **encoded_out) =
  649. {
  650. /* v0 */ NULL, /* v1 */ NULL, /* v2 */ NULL,
  651. desc_encode_v3,
  652. };
  653. /* Encode the given descriptor desc. On success, encoded_out points to a newly
  654. * allocated NUL terminated string that contains the encoded descriptor as a
  655. * string.
  656. *
  657. * Return 0 on success and encoded_out is a valid pointer. On error, -1 is
  658. * returned and encoded_out is untouched. */
  659. int
  660. hs_desc_encode_descriptor(const hs_descriptor_t *desc, char **encoded_out)
  661. {
  662. int ret = -1;
  663. tor_assert(desc);
  664. tor_assert(encoded_out);
  665. /* Make sure we support the version of the descriptor format. */
  666. if (!hs_desc_is_supported_version(desc->plaintext_data.version)) {
  667. goto err;
  668. }
  669. /* Extra precaution. Having no handler for the supported version should
  670. * never happened else we forgot to add it but we bumped the version. */
  671. tor_assert(ARRAY_LENGTH(encode_handlers) >= desc->plaintext_data.version);
  672. tor_assert(encode_handlers[desc->plaintext_data.version]);
  673. ret = encode_handlers[desc->plaintext_data.version](desc, encoded_out);
  674. if (ret < 0) {
  675. goto err;
  676. }
  677. err:
  678. return ret;
  679. }