tor  0.4.2.0-alpha-dev
hs_cell.c
Go to the documentation of this file.
1 /* Copyright (c) 2017-2019, The Tor Project, Inc. */
2 /* See LICENSE for licensing information */
3 
9 #include "core/or/or.h"
10 #include "app/config/config.h"
14 
15 #include "feature/hs/hs_cell.h"
16 #include "core/crypto/hs_ntor.h"
17 
18 #include "core/or/origin_circuit_st.h"
19 
20 /* Trunnel. */
21 #include "trunnel/ed25519_cert.h"
22 #include "trunnel/hs/cell_common.h"
23 #include "trunnel/hs/cell_establish_intro.h"
24 #include "trunnel/hs/cell_introduce1.h"
25 #include "trunnel/hs/cell_rendezvous.h"
26 
27 /* Compute the MAC of an INTRODUCE cell in mac_out. The encoded_cell param is
28  * the cell content up to the ENCRYPTED section of length encoded_cell_len.
29  * The encrypted param is the start of the ENCRYPTED section of length
30  * encrypted_len. The mac_key is the key needed for the computation of the MAC
31  * derived from the ntor handshake of length mac_key_len.
32  *
33  * The length mac_out_len must be at least DIGEST256_LEN. */
34 static void
35 compute_introduce_mac(const uint8_t *encoded_cell, size_t encoded_cell_len,
36  const uint8_t *encrypted, size_t encrypted_len,
37  const uint8_t *mac_key, size_t mac_key_len,
38  uint8_t *mac_out, size_t mac_out_len)
39 {
40  size_t offset = 0;
41  size_t mac_msg_len;
42  uint8_t mac_msg[RELAY_PAYLOAD_SIZE] = {0};
43 
44  tor_assert(encoded_cell);
45  tor_assert(encrypted);
46  tor_assert(mac_key);
47  tor_assert(mac_out);
48  tor_assert(mac_out_len >= DIGEST256_LEN);
49 
50  /* Compute the size of the message which is basically the entire cell until
51  * the MAC field of course. */
52  mac_msg_len = encoded_cell_len + (encrypted_len - DIGEST256_LEN);
53  tor_assert(mac_msg_len <= sizeof(mac_msg));
54 
55  /* First, put the encoded cell in the msg. */
56  memcpy(mac_msg, encoded_cell, encoded_cell_len);
57  offset += encoded_cell_len;
58  /* Second, put the CLIENT_PK + ENCRYPTED_DATA but ommit the MAC field (which
59  * is junk at this point). */
60  memcpy(mac_msg + offset, encrypted, (encrypted_len - DIGEST256_LEN));
61  offset += (encrypted_len - DIGEST256_LEN);
62  tor_assert(offset == mac_msg_len);
63 
64  crypto_mac_sha3_256(mac_out, mac_out_len,
65  mac_key, mac_key_len,
66  mac_msg, mac_msg_len);
67  memwipe(mac_msg, 0, sizeof(mac_msg));
68 }
69 
70 /* From a set of keys, subcredential and the ENCRYPTED section of an
71  * INTRODUCE2 cell, return a newly allocated intro cell keys structure.
72  * Finally, the client public key is copied in client_pk. On error, return
73  * NULL. */
75 get_introduce2_key_material(const ed25519_public_key_t *auth_key,
76  const curve25519_keypair_t *enc_key,
77  const uint8_t *subcredential,
78  const uint8_t *encrypted_section,
79  curve25519_public_key_t *client_pk)
80 {
82 
83  tor_assert(auth_key);
84  tor_assert(enc_key);
85  tor_assert(subcredential);
86  tor_assert(encrypted_section);
87  tor_assert(client_pk);
88 
89  keys = tor_malloc_zero(sizeof(*keys));
90 
91  /* First bytes of the ENCRYPTED section are the client public key. */
92  memcpy(client_pk->public_key, encrypted_section, CURVE25519_PUBKEY_LEN);
93 
94  if (hs_ntor_service_get_introduce1_keys(auth_key, enc_key, client_pk,
95  subcredential, keys) < 0) {
96  /* Don't rely on the caller to wipe this on error. */
97  memwipe(client_pk, 0, sizeof(curve25519_public_key_t));
98  tor_free(keys);
99  keys = NULL;
100  }
101  return keys;
102 }
103 
104 /* Using the given encryption key, decrypt the encrypted_section of length
105  * encrypted_section_len of an INTRODUCE2 cell and return a newly allocated
106  * buffer containing the decrypted data. On decryption failure, NULL is
107  * returned. */
108 static uint8_t *
109 decrypt_introduce2(const uint8_t *enc_key, const uint8_t *encrypted_section,
110  size_t encrypted_section_len)
111 {
112  uint8_t *decrypted = NULL;
113  crypto_cipher_t *cipher = NULL;
114 
115  tor_assert(enc_key);
116  tor_assert(encrypted_section);
117 
118  /* Decrypt ENCRYPTED section. */
119  cipher = crypto_cipher_new_with_bits((char *) enc_key,
121  tor_assert(cipher);
122 
123  /* This is symmetric encryption so can't be bigger than the encrypted
124  * section length. */
125  decrypted = tor_malloc_zero(encrypted_section_len);
126  if (crypto_cipher_decrypt(cipher, (char *) decrypted,
127  (const char *) encrypted_section,
128  encrypted_section_len) < 0) {
129  tor_free(decrypted);
130  decrypted = NULL;
131  goto done;
132  }
133 
134  done:
135  crypto_cipher_free(cipher);
136  return decrypted;
137 }
138 
139 /* Given a pointer to the decrypted data of the ENCRYPTED section of an
140  * INTRODUCE2 cell of length decrypted_len, parse and validate the cell
141  * content. Return a newly allocated cell structure or NULL on error. The
142  * circuit and service object are only used for logging purposes. */
143 static trn_cell_introduce_encrypted_t *
144 parse_introduce2_encrypted(const uint8_t *decrypted_data,
145  size_t decrypted_len, const origin_circuit_t *circ,
146  const hs_service_t *service)
147 {
148  trn_cell_introduce_encrypted_t *enc_cell = NULL;
149 
150  tor_assert(decrypted_data);
151  tor_assert(circ);
152  tor_assert(service);
153 
154  if (trn_cell_introduce_encrypted_parse(&enc_cell, decrypted_data,
155  decrypted_len) < 0) {
156  log_info(LD_REND, "Unable to parse the decrypted ENCRYPTED section of "
157  "the INTRODUCE2 cell on circuit %u for service %s",
158  TO_CIRCUIT(circ)->n_circ_id,
159  safe_str_client(service->onion_address));
160  goto err;
161  }
162 
163  if (trn_cell_introduce_encrypted_get_onion_key_type(enc_cell) !=
164  TRUNNEL_HS_INTRO_ONION_KEY_TYPE_NTOR) {
165  log_info(LD_REND, "INTRODUCE2 onion key type is invalid. Got %u but "
166  "expected %u on circuit %u for service %s",
167  trn_cell_introduce_encrypted_get_onion_key_type(enc_cell),
168  TRUNNEL_HS_INTRO_ONION_KEY_TYPE_NTOR,
169  TO_CIRCUIT(circ)->n_circ_id,
170  safe_str_client(service->onion_address));
171  goto err;
172  }
173 
174  if (trn_cell_introduce_encrypted_getlen_onion_key(enc_cell) !=
176  log_info(LD_REND, "INTRODUCE2 onion key length is invalid. Got %u but "
177  "expected %d on circuit %u for service %s",
178  (unsigned)trn_cell_introduce_encrypted_getlen_onion_key(enc_cell),
179  CURVE25519_PUBKEY_LEN, TO_CIRCUIT(circ)->n_circ_id,
180  safe_str_client(service->onion_address));
181  goto err;
182  }
183  /* XXX: Validate NSPEC field as well. */
184 
185  return enc_cell;
186  err:
187  trn_cell_introduce_encrypted_free(enc_cell);
188  return NULL;
189 }
190 
191 /* Build a legacy ESTABLISH_INTRO cell with the given circuit nonce and RSA
192  * encryption key. The encoded cell is put in cell_out that MUST at least be
193  * of the size of RELAY_PAYLOAD_SIZE. Return the encoded cell length on
194  * success else a negative value and cell_out is untouched. */
195 static ssize_t
196 build_legacy_establish_intro(const char *circ_nonce, crypto_pk_t *enc_key,
197  uint8_t *cell_out)
198 {
199  ssize_t cell_len;
200 
201  tor_assert(circ_nonce);
202  tor_assert(enc_key);
203  tor_assert(cell_out);
204 
205  memwipe(cell_out, 0, RELAY_PAYLOAD_SIZE);
206 
207  cell_len = rend_service_encode_establish_intro_cell((char*)cell_out,
209  enc_key, circ_nonce);
210  return cell_len;
211 }
212 
213 /* Parse an INTRODUCE2 cell from payload of size payload_len for the given
214  * service and circuit which are used only for logging purposes. The resulting
215  * parsed cell is put in cell_ptr_out.
216  *
217  * This function only parses prop224 INTRODUCE2 cells even when the intro point
218  * is a legacy intro point. That's because intro points don't actually care
219  * about the contents of the introduce cell. Legacy INTRODUCE cells are only
220  * used by the legacy system now.
221  *
222  * Return 0 on success else a negative value and cell_ptr_out is untouched. */
223 static int
224 parse_introduce2_cell(const hs_service_t *service,
225  const origin_circuit_t *circ, const uint8_t *payload,
226  size_t payload_len,
227  trn_cell_introduce1_t **cell_ptr_out)
228 {
229  trn_cell_introduce1_t *cell = NULL;
230 
231  tor_assert(service);
232  tor_assert(circ);
233  tor_assert(payload);
234  tor_assert(cell_ptr_out);
235 
236  /* Parse the cell so we can start cell validation. */
237  if (trn_cell_introduce1_parse(&cell, payload, payload_len) < 0) {
238  log_info(LD_PROTOCOL, "Unable to parse INTRODUCE2 cell on circuit %u "
239  "for service %s",
240  TO_CIRCUIT(circ)->n_circ_id,
241  safe_str_client(service->onion_address));
242  goto err;
243  }
244 
245  /* Success. */
246  *cell_ptr_out = cell;
247  return 0;
248  err:
249  return -1;
250 }
251 
252 /* Set the onion public key onion_pk in cell, the encrypted section of an
253  * INTRODUCE1 cell. */
254 static void
255 introduce1_set_encrypted_onion_key(trn_cell_introduce_encrypted_t *cell,
256  const uint8_t *onion_pk)
257 {
258  tor_assert(cell);
259  tor_assert(onion_pk);
260  /* There is only one possible key type for a non legacy cell. */
261  trn_cell_introduce_encrypted_set_onion_key_type(cell,
262  TRUNNEL_HS_INTRO_ONION_KEY_TYPE_NTOR);
263  trn_cell_introduce_encrypted_set_onion_key_len(cell, CURVE25519_PUBKEY_LEN);
264  trn_cell_introduce_encrypted_setlen_onion_key(cell, CURVE25519_PUBKEY_LEN);
265  memcpy(trn_cell_introduce_encrypted_getarray_onion_key(cell), onion_pk,
266  trn_cell_introduce_encrypted_getlen_onion_key(cell));
267 }
268 
269 /* Set the link specifiers in lspecs in cell, the encrypted section of an
270  * INTRODUCE1 cell. */
271 static void
272 introduce1_set_encrypted_link_spec(trn_cell_introduce_encrypted_t *cell,
273  const smartlist_t *lspecs)
274 {
275  tor_assert(cell);
276  tor_assert(lspecs);
277  tor_assert(smartlist_len(lspecs) > 0);
278  tor_assert(smartlist_len(lspecs) <= UINT8_MAX);
279 
280  uint8_t lspecs_num = (uint8_t) smartlist_len(lspecs);
281  trn_cell_introduce_encrypted_set_nspec(cell, lspecs_num);
282  /* We aren't duplicating the link specifiers object here which means that
283  * the ownership goes to the trn_cell_introduce_encrypted_t cell and those
284  * object will be freed when the cell is. */
285  SMARTLIST_FOREACH(lspecs, link_specifier_t *, ls,
286  trn_cell_introduce_encrypted_add_nspecs(cell, ls));
287 }
288 
289 /* Set padding in the enc_cell only if needed that is the total length of both
290  * sections are below the mininum required for an INTRODUCE1 cell. */
291 static void
292 introduce1_set_encrypted_padding(const trn_cell_introduce1_t *cell,
293  trn_cell_introduce_encrypted_t *enc_cell)
294 {
295  tor_assert(cell);
296  tor_assert(enc_cell);
297  /* This is the length we expect to have once encoded of the whole cell. */
298  ssize_t full_len = trn_cell_introduce1_encoded_len(cell) +
299  trn_cell_introduce_encrypted_encoded_len(enc_cell);
300  tor_assert(full_len > 0);
301  if (full_len < HS_CELL_INTRODUCE1_MIN_SIZE) {
302  size_t padding = HS_CELL_INTRODUCE1_MIN_SIZE - full_len;
303  trn_cell_introduce_encrypted_setlen_pad(enc_cell, padding);
304  memset(trn_cell_introduce_encrypted_getarray_pad(enc_cell), 0,
305  trn_cell_introduce_encrypted_getlen_pad(enc_cell));
306  }
307 }
308 
309 /* Encrypt the ENCRYPTED payload and encode it in the cell using the enc_cell
310  * and the INTRODUCE1 data.
311  *
312  * This can't fail but it is very important that the caller sets every field
313  * in data so the computation of the INTRODUCE1 keys doesn't fail. */
314 static void
315 introduce1_encrypt_and_encode(trn_cell_introduce1_t *cell,
316  const trn_cell_introduce_encrypted_t *enc_cell,
317  const hs_cell_introduce1_data_t *data)
318 {
319  size_t offset = 0;
320  ssize_t encrypted_len;
321  ssize_t encoded_cell_len, encoded_enc_cell_len;
322  uint8_t encoded_cell[RELAY_PAYLOAD_SIZE] = {0};
323  uint8_t encoded_enc_cell[RELAY_PAYLOAD_SIZE] = {0};
324  uint8_t *encrypted = NULL;
325  uint8_t mac[DIGEST256_LEN];
326  crypto_cipher_t *cipher = NULL;
328 
329  tor_assert(cell);
330  tor_assert(enc_cell);
331  tor_assert(data);
332 
333  /* Encode the cells up to now of what we have to we can perform the MAC
334  * computation on it. */
335  encoded_cell_len = trn_cell_introduce1_encode(encoded_cell,
336  sizeof(encoded_cell), cell);
337  /* We have a much more serious issue if this isn't true. */
338  tor_assert(encoded_cell_len > 0);
339 
340  encoded_enc_cell_len =
341  trn_cell_introduce_encrypted_encode(encoded_enc_cell,
342  sizeof(encoded_enc_cell), enc_cell);
343  /* We have a much more serious issue if this isn't true. */
344  tor_assert(encoded_enc_cell_len > 0);
345 
346  /* Get the key material for the encryption. */
347  if (hs_ntor_client_get_introduce1_keys(data->auth_pk, data->enc_pk,
348  data->client_kp,
349  data->subcredential, &keys) < 0) {
350  tor_assert_unreached();
351  }
352 
353  /* Prepare cipher with the encryption key just computed. */
354  cipher = crypto_cipher_new_with_bits((const char *) keys.enc_key,
355  sizeof(keys.enc_key) * 8);
356  tor_assert(cipher);
357 
358  /* Compute the length of the ENCRYPTED section which is the CLIENT_PK,
359  * ENCRYPTED_DATA and MAC length. */
360  encrypted_len = sizeof(data->client_kp->pubkey) + encoded_enc_cell_len +
361  sizeof(mac);
362  tor_assert(encrypted_len < RELAY_PAYLOAD_SIZE);
363  encrypted = tor_malloc_zero(encrypted_len);
364 
365  /* Put the CLIENT_PK first. */
366  memcpy(encrypted, data->client_kp->pubkey.public_key,
367  sizeof(data->client_kp->pubkey.public_key));
368  offset += sizeof(data->client_kp->pubkey.public_key);
369  /* Then encrypt and set the ENCRYPTED_DATA. This can't fail. */
370  crypto_cipher_encrypt(cipher, (char *) encrypted + offset,
371  (const char *) encoded_enc_cell, encoded_enc_cell_len);
372  crypto_cipher_free(cipher);
373  offset += encoded_enc_cell_len;
374  /* Compute MAC from the above and put it in the buffer. This function will
375  * make the adjustment to the encrypted_len to omit the MAC length. */
376  compute_introduce_mac(encoded_cell, encoded_cell_len,
377  encrypted, encrypted_len,
378  keys.mac_key, sizeof(keys.mac_key),
379  mac, sizeof(mac));
380  memcpy(encrypted + offset, mac, sizeof(mac));
381  offset += sizeof(mac);
382  tor_assert(offset == (size_t) encrypted_len);
383 
384  /* Set the ENCRYPTED section in the cell. */
385  trn_cell_introduce1_setlen_encrypted(cell, encrypted_len);
386  memcpy(trn_cell_introduce1_getarray_encrypted(cell),
387  encrypted, encrypted_len);
388 
389  /* Cleanup. */
390  memwipe(&keys, 0, sizeof(keys));
391  memwipe(mac, 0, sizeof(mac));
392  memwipe(encrypted, 0, sizeof(encrypted_len));
393  memwipe(encoded_enc_cell, 0, sizeof(encoded_enc_cell));
394  tor_free(encrypted);
395 }
396 
397 /* Using the INTRODUCE1 data, setup the ENCRYPTED section in cell. This means
398  * set it, encrypt it and encode it. */
399 static void
400 introduce1_set_encrypted(trn_cell_introduce1_t *cell,
401  const hs_cell_introduce1_data_t *data)
402 {
403  trn_cell_introduce_encrypted_t *enc_cell;
404  trn_cell_extension_t *ext;
405 
406  tor_assert(cell);
407  tor_assert(data);
408 
409  enc_cell = trn_cell_introduce_encrypted_new();
410  tor_assert(enc_cell);
411 
412  /* Set extension data. None are used. */
413  ext = trn_cell_extension_new();
414  tor_assert(ext);
415  trn_cell_extension_set_num(ext, 0);
416  trn_cell_introduce_encrypted_set_extensions(enc_cell, ext);
417 
418  /* Set the rendezvous cookie. */
419  memcpy(trn_cell_introduce_encrypted_getarray_rend_cookie(enc_cell),
420  data->rendezvous_cookie, REND_COOKIE_LEN);
421 
422  /* Set the onion public key. */
423  introduce1_set_encrypted_onion_key(enc_cell, data->onion_pk->public_key);
424 
425  /* Set the link specifiers. */
426  introduce1_set_encrypted_link_spec(enc_cell, data->link_specifiers);
427 
428  /* Set padding. */
429  introduce1_set_encrypted_padding(cell, enc_cell);
430 
431  /* Encrypt and encode it in the cell. */
432  introduce1_encrypt_and_encode(cell, enc_cell, data);
433 
434  /* Cleanup. */
435  trn_cell_introduce_encrypted_free(enc_cell);
436 }
437 
438 /* Set the authentication key in the INTRODUCE1 cell from the given data. */
439 static void
440 introduce1_set_auth_key(trn_cell_introduce1_t *cell,
441  const hs_cell_introduce1_data_t *data)
442 {
443  tor_assert(cell);
444  tor_assert(data);
445  /* There is only one possible type for a non legacy cell. */
446  trn_cell_introduce1_set_auth_key_type(cell,
447  TRUNNEL_HS_INTRO_AUTH_KEY_TYPE_ED25519);
448  trn_cell_introduce1_set_auth_key_len(cell, ED25519_PUBKEY_LEN);
449  trn_cell_introduce1_setlen_auth_key(cell, ED25519_PUBKEY_LEN);
450  memcpy(trn_cell_introduce1_getarray_auth_key(cell),
451  data->auth_pk->pubkey, trn_cell_introduce1_getlen_auth_key(cell));
452 }
453 
454 /* Set the legacy ID field in the INTRODUCE1 cell from the given data. */
455 static void
456 introduce1_set_legacy_id(trn_cell_introduce1_t *cell,
457  const hs_cell_introduce1_data_t *data)
458 {
459  tor_assert(cell);
460  tor_assert(data);
461 
462  if (data->is_legacy) {
463  uint8_t digest[DIGEST_LEN];
464  if (BUG(crypto_pk_get_digest(data->legacy_key, (char *) digest) < 0)) {
465  return;
466  }
467  memcpy(trn_cell_introduce1_getarray_legacy_key_id(cell),
468  digest, trn_cell_introduce1_getlen_legacy_key_id(cell));
469  } else {
470  /* We have to zeroed the LEGACY_KEY_ID field. */
471  memset(trn_cell_introduce1_getarray_legacy_key_id(cell), 0,
472  trn_cell_introduce1_getlen_legacy_key_id(cell));
473  }
474 }
475 
476 /* ========== */
477 /* Public API */
478 /* ========== */
479 
480 /* Build an ESTABLISH_INTRO cell with the given circuit nonce and intro point
481  * object. The encoded cell is put in cell_out that MUST at least be of the
482  * size of RELAY_PAYLOAD_SIZE. Return the encoded cell length on success else
483  * a negative value and cell_out is untouched. This function also supports
484  * legacy cell creation. */
485 ssize_t
486 hs_cell_build_establish_intro(const char *circ_nonce,
487  const hs_service_intro_point_t *ip,
488  uint8_t *cell_out)
489 {
490  ssize_t cell_len = -1;
491  uint16_t sig_len = ED25519_SIG_LEN;
492  trn_cell_extension_t *ext;
493  trn_cell_establish_intro_t *cell = NULL;
494 
495  tor_assert(circ_nonce);
496  tor_assert(ip);
497 
498  /* Quickly handle the legacy IP. */
499  if (ip->base.is_only_legacy) {
500  tor_assert(ip->legacy_key);
501  cell_len = build_legacy_establish_intro(circ_nonce, ip->legacy_key,
502  cell_out);
503  tor_assert(cell_len <= RELAY_PAYLOAD_SIZE);
504  /* Success or not we are done here. */
505  goto done;
506  }
507 
508  /* Set extension data. None used here. */
509  ext = trn_cell_extension_new();
510  trn_cell_extension_set_num(ext, 0);
511  cell = trn_cell_establish_intro_new();
512  trn_cell_establish_intro_set_extensions(cell, ext);
513  /* Set signature size. Array is then allocated in the cell. We need to do
514  * this early so we can use trunnel API to get the signature length. */
515  trn_cell_establish_intro_set_sig_len(cell, sig_len);
516  trn_cell_establish_intro_setlen_sig(cell, sig_len);
517 
518  /* Set AUTH_KEY_TYPE: 2 means ed25519 */
519  trn_cell_establish_intro_set_auth_key_type(cell,
520  TRUNNEL_HS_INTRO_AUTH_KEY_TYPE_ED25519);
521 
522  /* Set AUTH_KEY and AUTH_KEY_LEN field. Must also set byte-length of
523  * AUTH_KEY to match */
524  {
525  uint16_t auth_key_len = ED25519_PUBKEY_LEN;
526  trn_cell_establish_intro_set_auth_key_len(cell, auth_key_len);
527  trn_cell_establish_intro_setlen_auth_key(cell, auth_key_len);
528  /* We do this call _after_ setting the length because it's reallocated at
529  * that point only. */
530  uint8_t *auth_key_ptr = trn_cell_establish_intro_getarray_auth_key(cell);
531  memcpy(auth_key_ptr, ip->auth_key_kp.pubkey.pubkey, auth_key_len);
532  }
533 
534  /* Calculate HANDSHAKE_AUTH field (MAC). */
535  {
536  ssize_t tmp_cell_enc_len = 0;
537  ssize_t tmp_cell_mac_offset =
538  sig_len + sizeof(cell->sig_len) +
539  trn_cell_establish_intro_getlen_handshake_mac(cell);
540  uint8_t tmp_cell_enc[RELAY_PAYLOAD_SIZE] = {0};
541  uint8_t mac[TRUNNEL_SHA3_256_LEN], *handshake_ptr;
542 
543  /* We first encode the current fields we have in the cell so we can
544  * compute the MAC using the raw bytes. */
545  tmp_cell_enc_len = trn_cell_establish_intro_encode(tmp_cell_enc,
546  sizeof(tmp_cell_enc),
547  cell);
548  if (BUG(tmp_cell_enc_len < 0)) {
549  goto done;
550  }
551  /* Sanity check. */
552  tor_assert(tmp_cell_enc_len > tmp_cell_mac_offset);
553 
554  /* Circuit nonce is always DIGEST_LEN according to tor-spec.txt. */
555  crypto_mac_sha3_256(mac, sizeof(mac),
556  (uint8_t *) circ_nonce, DIGEST_LEN,
557  tmp_cell_enc, tmp_cell_enc_len - tmp_cell_mac_offset);
558  handshake_ptr = trn_cell_establish_intro_getarray_handshake_mac(cell);
559  memcpy(handshake_ptr, mac, sizeof(mac));
560 
561  memwipe(mac, 0, sizeof(mac));
562  memwipe(tmp_cell_enc, 0, sizeof(tmp_cell_enc));
563  }
564 
565  /* Calculate the cell signature SIG. */
566  {
567  ssize_t tmp_cell_enc_len = 0;
568  ssize_t tmp_cell_sig_offset = (sig_len + sizeof(cell->sig_len));
569  uint8_t tmp_cell_enc[RELAY_PAYLOAD_SIZE] = {0}, *sig_ptr;
571 
572  /* We first encode the current fields we have in the cell so we can
573  * compute the signature from the raw bytes of the cell. */
574  tmp_cell_enc_len = trn_cell_establish_intro_encode(tmp_cell_enc,
575  sizeof(tmp_cell_enc),
576  cell);
577  if (BUG(tmp_cell_enc_len < 0)) {
578  goto done;
579  }
580 
581  if (ed25519_sign_prefixed(&sig, tmp_cell_enc,
582  tmp_cell_enc_len - tmp_cell_sig_offset,
583  ESTABLISH_INTRO_SIG_PREFIX, &ip->auth_key_kp)) {
584  log_warn(LD_BUG, "Unable to make signature for ESTABLISH_INTRO cell.");
585  goto done;
586  }
587  /* Copy the signature into the cell. */
588  sig_ptr = trn_cell_establish_intro_getarray_sig(cell);
589  memcpy(sig_ptr, sig.sig, sig_len);
590 
591  memwipe(tmp_cell_enc, 0, sizeof(tmp_cell_enc));
592  }
593 
594  /* Encode the cell. Can't be bigger than a standard cell. */
595  cell_len = trn_cell_establish_intro_encode(cell_out, RELAY_PAYLOAD_SIZE,
596  cell);
597 
598  done:
599  trn_cell_establish_intro_free(cell);
600  return cell_len;
601 }
602 
603 /* Parse the INTRO_ESTABLISHED cell in the payload of size payload_len. If we
604  * are successful at parsing it, return the length of the parsed cell else a
605  * negative value on error. */
606 ssize_t
607 hs_cell_parse_intro_established(const uint8_t *payload, size_t payload_len)
608 {
609  ssize_t ret;
610  trn_cell_intro_established_t *cell = NULL;
611 
612  tor_assert(payload);
613 
614  /* Try to parse the payload into a cell making sure we do actually have a
615  * valid cell. */
616  ret = trn_cell_intro_established_parse(&cell, payload, payload_len);
617  if (ret >= 0) {
618  /* On success, we do not keep the cell, we just notify the caller that it
619  * was successfully parsed. */
620  trn_cell_intro_established_free(cell);
621  }
622  return ret;
623 }
624 
625 /* Parse the INTRODUCE2 cell using data which contains everything we need to
626  * do so and contains the destination buffers of information we extract and
627  * compute from the cell. Return 0 on success else a negative value. The
628  * service and circ are only used for logging purposes. */
629 ssize_t
630 hs_cell_parse_introduce2(hs_cell_introduce2_data_t *data,
631  const origin_circuit_t *circ,
632  const hs_service_t *service)
633 {
634  int ret = -1;
635  time_t elapsed;
636  uint8_t *decrypted = NULL;
637  size_t encrypted_section_len;
638  const uint8_t *encrypted_section;
639  trn_cell_introduce1_t *cell = NULL;
640  trn_cell_introduce_encrypted_t *enc_cell = NULL;
641  hs_ntor_intro_cell_keys_t *intro_keys = NULL;
642 
643  tor_assert(data);
644  tor_assert(circ);
645  tor_assert(service);
646 
647  /* Parse the cell into a decoded data structure pointed by cell_ptr. */
648  if (parse_introduce2_cell(service, circ, data->payload, data->payload_len,
649  &cell) < 0) {
650  goto done;
651  }
652 
653  log_info(LD_REND, "Received a decodable INTRODUCE2 cell on circuit %u "
654  "for service %s. Decoding encrypted section...",
655  TO_CIRCUIT(circ)->n_circ_id,
656  safe_str_client(service->onion_address));
657 
658  encrypted_section = trn_cell_introduce1_getconstarray_encrypted(cell);
659  encrypted_section_len = trn_cell_introduce1_getlen_encrypted(cell);
660 
661  /* Encrypted section must at least contain the CLIENT_PK and MAC which is
662  * defined in section 3.3.2 of the specification. */
663  if (encrypted_section_len < (CURVE25519_PUBKEY_LEN + DIGEST256_LEN)) {
664  log_info(LD_REND, "Invalid INTRODUCE2 encrypted section length "
665  "for service %s. Dropping cell.",
666  safe_str_client(service->onion_address));
667  goto done;
668  }
669 
670  /* Check our replay cache for this introduction point. */
671  if (replaycache_add_test_and_elapsed(data->replay_cache, encrypted_section,
672  encrypted_section_len, &elapsed)) {
673  log_warn(LD_REND, "Possible replay detected! An INTRODUCE2 cell with the"
674  "same ENCRYPTED section was seen %ld seconds ago. "
675  "Dropping cell.", (long int) elapsed);
676  goto done;
677  }
678 
679  /* Build the key material out of the key material found in the cell. */
680  intro_keys = get_introduce2_key_material(data->auth_pk, data->enc_kp,
681  data->subcredential,
682  encrypted_section,
683  &data->client_pk);
684  if (intro_keys == NULL) {
685  log_info(LD_REND, "Invalid INTRODUCE2 encrypted data. Unable to "
686  "compute key material on circuit %u for service %s",
687  TO_CIRCUIT(circ)->n_circ_id,
688  safe_str_client(service->onion_address));
689  goto done;
690  }
691 
692  /* Validate MAC from the cell and our computed key material. The MAC field
693  * in the cell is at the end of the encrypted section. */
694  {
695  uint8_t mac[DIGEST256_LEN];
696  /* The MAC field is at the very end of the ENCRYPTED section. */
697  size_t mac_offset = encrypted_section_len - sizeof(mac);
698  /* Compute the MAC. Use the entire encoded payload with a length up to the
699  * ENCRYPTED section. */
700  compute_introduce_mac(data->payload,
701  data->payload_len - encrypted_section_len,
702  encrypted_section, encrypted_section_len,
703  intro_keys->mac_key, sizeof(intro_keys->mac_key),
704  mac, sizeof(mac));
705  if (tor_memcmp(mac, encrypted_section + mac_offset, sizeof(mac))) {
706  log_info(LD_REND, "Invalid MAC validation for INTRODUCE2 cell on "
707  "circuit %u for service %s",
708  TO_CIRCUIT(circ)->n_circ_id,
709  safe_str_client(service->onion_address));
710  goto done;
711  }
712  }
713 
714  {
715  /* The ENCRYPTED_DATA section starts just after the CLIENT_PK. */
716  const uint8_t *encrypted_data =
717  encrypted_section + sizeof(data->client_pk);
718  /* It's symmetric encryption so it's correct to use the ENCRYPTED length
719  * for decryption. Computes the length of ENCRYPTED_DATA meaning removing
720  * the CLIENT_PK and MAC length. */
721  size_t encrypted_data_len =
722  encrypted_section_len - (sizeof(data->client_pk) + DIGEST256_LEN);
723 
724  /* This decrypts the ENCRYPTED_DATA section of the cell. */
725  decrypted = decrypt_introduce2(intro_keys->enc_key,
726  encrypted_data, encrypted_data_len);
727  if (decrypted == NULL) {
728  log_info(LD_REND, "Unable to decrypt the ENCRYPTED section of an "
729  "INTRODUCE2 cell on circuit %u for service %s",
730  TO_CIRCUIT(circ)->n_circ_id,
731  safe_str_client(service->onion_address));
732  goto done;
733  }
734 
735  /* Parse this blob into an encrypted cell structure so we can then extract
736  * the data we need out of it. */
737  enc_cell = parse_introduce2_encrypted(decrypted, encrypted_data_len,
738  circ, service);
739  memwipe(decrypted, 0, encrypted_data_len);
740  if (enc_cell == NULL) {
741  goto done;
742  }
743  }
744 
745  /* XXX: Implement client authorization checks. */
746 
747  /* Extract onion key and rendezvous cookie from the cell used for the
748  * rendezvous point circuit e2e encryption. */
749  memcpy(data->onion_pk.public_key,
750  trn_cell_introduce_encrypted_getconstarray_onion_key(enc_cell),
752  memcpy(data->rendezvous_cookie,
753  trn_cell_introduce_encrypted_getconstarray_rend_cookie(enc_cell),
754  sizeof(data->rendezvous_cookie));
755 
756  /* Extract rendezvous link specifiers. */
757  for (size_t idx = 0;
758  idx < trn_cell_introduce_encrypted_get_nspec(enc_cell); idx++) {
759  link_specifier_t *lspec =
760  trn_cell_introduce_encrypted_get_nspecs(enc_cell, idx);
761  if (BUG(!lspec)) {
762  goto done;
763  }
764  link_specifier_t *lspec_dup = link_specifier_dup(lspec);
765  if (BUG(!lspec_dup)) {
766  goto done;
767  }
768  smartlist_add(data->link_specifiers, lspec_dup);
769  }
770 
771  /* Success. */
772  ret = 0;
773  log_info(LD_REND, "Valid INTRODUCE2 cell. Launching rendezvous circuit.");
774 
775  done:
776  if (intro_keys) {
777  memwipe(intro_keys, 0, sizeof(hs_ntor_intro_cell_keys_t));
778  tor_free(intro_keys);
779  }
780  tor_free(decrypted);
781  trn_cell_introduce_encrypted_free(enc_cell);
782  trn_cell_introduce1_free(cell);
783  return ret;
784 }
785 
786 /* Build a RENDEZVOUS1 cell with the given rendezvous cookie and handshake
787  * info. The encoded cell is put in cell_out and the length of the data is
788  * returned. This can't fail. */
789 ssize_t
790 hs_cell_build_rendezvous1(const uint8_t *rendezvous_cookie,
791  size_t rendezvous_cookie_len,
792  const uint8_t *rendezvous_handshake_info,
793  size_t rendezvous_handshake_info_len,
794  uint8_t *cell_out)
795 {
796  ssize_t cell_len;
797  trn_cell_rendezvous1_t *cell;
798 
799  tor_assert(rendezvous_cookie);
800  tor_assert(rendezvous_handshake_info);
801  tor_assert(cell_out);
802 
803  cell = trn_cell_rendezvous1_new();
804  /* Set the RENDEZVOUS_COOKIE. */
805  memcpy(trn_cell_rendezvous1_getarray_rendezvous_cookie(cell),
806  rendezvous_cookie, rendezvous_cookie_len);
807  /* Set the HANDSHAKE_INFO. */
808  trn_cell_rendezvous1_setlen_handshake_info(cell,
809  rendezvous_handshake_info_len);
810  memcpy(trn_cell_rendezvous1_getarray_handshake_info(cell),
811  rendezvous_handshake_info, rendezvous_handshake_info_len);
812  /* Encoding. */
813  cell_len = trn_cell_rendezvous1_encode(cell_out, RELAY_PAYLOAD_SIZE, cell);
814  tor_assert(cell_len > 0);
815 
816  trn_cell_rendezvous1_free(cell);
817  return cell_len;
818 }
819 
820 /* Build an INTRODUCE1 cell from the given data. The encoded cell is put in
821  * cell_out which must be of at least size RELAY_PAYLOAD_SIZE. On success, the
822  * encoded length is returned else a negative value and the content of
823  * cell_out should be ignored. */
824 ssize_t
825 hs_cell_build_introduce1(const hs_cell_introduce1_data_t *data,
826  uint8_t *cell_out)
827 {
828  ssize_t cell_len;
829  trn_cell_introduce1_t *cell;
830  trn_cell_extension_t *ext;
831 
832  tor_assert(data);
833  tor_assert(cell_out);
834 
835  cell = trn_cell_introduce1_new();
836  tor_assert(cell);
837 
838  /* Set extension data. None are used. */
839  ext = trn_cell_extension_new();
840  tor_assert(ext);
841  trn_cell_extension_set_num(ext, 0);
842  trn_cell_introduce1_set_extensions(cell, ext);
843 
844  /* Set the legacy ID field. */
845  introduce1_set_legacy_id(cell, data);
846 
847  /* Set the authentication key. */
848  introduce1_set_auth_key(cell, data);
849 
850  /* Set the encrypted section. This will set, encrypt and encode the
851  * ENCRYPTED section in the cell. After this, we'll be ready to encode. */
852  introduce1_set_encrypted(cell, data);
853 
854  /* Final encoding. */
855  cell_len = trn_cell_introduce1_encode(cell_out, RELAY_PAYLOAD_SIZE, cell);
856 
857  trn_cell_introduce1_free(cell);
858  return cell_len;
859 }
860 
861 /* Build an ESTABLISH_RENDEZVOUS cell from the given rendezvous_cookie. The
862  * encoded cell is put in cell_out which must be of at least
863  * RELAY_PAYLOAD_SIZE. On success, the encoded length is returned and the
864  * caller should clear up the content of the cell.
865  *
866  * This function can't fail. */
867 ssize_t
868 hs_cell_build_establish_rendezvous(const uint8_t *rendezvous_cookie,
869  uint8_t *cell_out)
870 {
871  tor_assert(rendezvous_cookie);
872  tor_assert(cell_out);
873 
874  memcpy(cell_out, rendezvous_cookie, HS_REND_COOKIE_LEN);
875  return HS_REND_COOKIE_LEN;
876 }
877 
878 /* Handle an INTRODUCE_ACK cell encoded in payload of length payload_len.
879  * Return the status code on success else a negative value if the cell as not
880  * decodable. */
881 int
882 hs_cell_parse_introduce_ack(const uint8_t *payload, size_t payload_len)
883 {
884  int ret = -1;
885  trn_cell_introduce_ack_t *cell = NULL;
886 
887  tor_assert(payload);
888 
889  /* If it is a legacy IP, rend-spec.txt specifies that a ACK is 0 byte and a
890  * NACK is 1 byte. We can't use the legacy function for this so we have to
891  * do a special case. */
892  if (payload_len <= 1) {
893  if (payload_len == 0) {
894  ret = TRUNNEL_HS_INTRO_ACK_STATUS_SUCCESS;
895  } else {
896  ret = TRUNNEL_HS_INTRO_ACK_STATUS_UNKNOWN_ID;
897  }
898  goto end;
899  }
900 
901  if (trn_cell_introduce_ack_parse(&cell, payload, payload_len) < 0) {
902  log_info(LD_REND, "Invalid INTRODUCE_ACK cell. Unable to parse it.");
903  goto end;
904  }
905 
906  ret = trn_cell_introduce_ack_get_status(cell);
907 
908  end:
909  trn_cell_introduce_ack_free(cell);
910  return ret;
911 }
912 
913 /* Handle a RENDEZVOUS2 cell encoded in payload of length payload_len. On
914  * success, handshake_info contains the data in the HANDSHAKE_INFO field, and
915  * 0 is returned. On error, a negative value is returned. */
916 int
917 hs_cell_parse_rendezvous2(const uint8_t *payload, size_t payload_len,
918  uint8_t *handshake_info, size_t handshake_info_len)
919 {
920  int ret = -1;
921  trn_cell_rendezvous2_t *cell = NULL;
922 
923  tor_assert(payload);
924  tor_assert(handshake_info);
925 
926  if (trn_cell_rendezvous2_parse(&cell, payload, payload_len) < 0) {
927  log_info(LD_REND, "Invalid RENDEZVOUS2 cell. Unable to parse it.");
928  goto end;
929  }
930 
931  /* Static size, we should never have an issue with this else we messed up
932  * our code flow. */
933  tor_assert(trn_cell_rendezvous2_getlen_handshake_info(cell) ==
934  handshake_info_len);
935  memcpy(handshake_info,
936  trn_cell_rendezvous2_getconstarray_handshake_info(cell),
937  handshake_info_len);
938  ret = 0;
939 
940  end:
941  trn_cell_rendezvous2_free(cell);
942  return ret;
943 }
944 
945 /* Clear the given INTRODUCE1 data structure data. */
946 void
947 hs_cell_introduce1_data_clear(hs_cell_introduce1_data_t *data)
948 {
949  if (data == NULL) {
950  return;
951  }
952  /* Object in this list have been moved to the cell object when building it
953  * so they've been freed earlier. We do that in order to avoid duplicating
954  * them leading to more memory and CPU time being used for nothing. */
955  smartlist_free(data->link_specifiers);
956  /* The data object has no ownership of any members. */
957  memwipe(data, 0, sizeof(hs_cell_introduce1_data_t));
958 }
959 
#define RELAY_PAYLOAD_SIZE
Definition: or.h:605
int crypto_cipher_encrypt(crypto_cipher_t *env, char *to, const char *from, size_t fromlen)
Definition: crypto_cipher.c:88
void smartlist_add(smartlist_t *sl, void *element)
#define TO_CIRCUIT(x)
Definition: or.h:947
Header file for config.c.
Header file for replaycache.c.
#define tor_free(p)
Definition: malloc.h:52
int replaycache_add_test_and_elapsed(replaycache_t *r, const void *data, size_t len, time_t *elapsed)
Definition: replaycache.c:195
void memwipe(void *mem, uint8_t byte, size_t sz)
Definition: crypto_util.c:57
#define DIGEST256_LEN
Definition: digest_sizes.h:23
Common functions for cryptographic routines.
tor_assert(buffer)
int tor_memcmp(const void *a, const void *b, size_t len)
Definition: di_ops.c:31
#define DIGEST_LEN
Definition: digest_sizes.h:20
Header file containing cell data for the whole HS subsytem.
Master header file for Tor-specific functionality.
int crypto_cipher_decrypt(crypto_cipher_t *env, char *to, const char *from, size_t fromlen)
#define LD_REND
Definition: log.h:81
Header file for rendservice.c.
int crypto_pk_get_digest(const crypto_pk_t *pk, char *digest_out)
Definition: crypto_rsa.c:356
#define SMARTLIST_FOREACH(sl, type, var, cmd)
crypto_cipher_t * crypto_cipher_new_with_bits(const char *key, int bits)
Definition: crypto_cipher.c:54
void crypto_mac_sha3_256(uint8_t *mac_out, size_t len_out, const uint8_t *key, size_t key_len, const uint8_t *msg, size_t msg_len)
#define REND_COOKIE_LEN
Definition: or.h:399
#define LD_PROTOCOL
Definition: log.h:69
#define LD_BUG
Definition: log.h:83
#define CURVE25519_PUBKEY_LEN
Definition: x25519_sizes.h:20