crypt_path.c 7.21 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
/*
 * Copyright (c) 2019, The Tor Project, Inc. */
/* See LICENSE for licensing information */

/**
 * \file crypt_path.c
 *
 * \brief Functions dealing with layered circuit encryption. This file aims to
 *   provide an API around the crypt_path_t structure which holds crypto
 *   information about a specific hop of a circuit.
11
12
13
14
15
16
17
18
19
20
21
 *
 * TODO: We should eventually move all functions dealing and manipulating
 *   crypt_path_t to this file, so that eventually we encapsulate more and more
 *   of crypt_path_t. Here are some more functions that can be moved here with
 *   some more effort:
 *
 *   - circuit_list_path_impl()
 *   - Functions dealing with cpaths in HSv2 create_rend_cpath() and
 *     create_rend_cpath_legacy()
 *   - The cpath related parts of rend_service_receive_introduction() and
 *     rend_client_send_introduction().
22
23
24
25
26
27
28
29
 **/

#define CRYPT_PATH_PRIVATE

#include "core/or/or.h"
#include "core/or/crypt_path.h"

#include "core/crypto/relay_crypto.h"
30
#include "core/crypto/onion_crypto.h"
31
32
#include "core/or/circuitbuild.h"
#include "core/or/circuitlist.h"
33

34
35
36
#include "lib/crypt_ops/crypto_dh.h"
#include "lib/crypt_ops/crypto_util.h"

37
#include "core/or/crypt_path_st.h"
38
#include "core/or/cell_st.h"
39

40
41
42
43
/** Add <b>new_hop</b> to the end of the doubly-linked-list <b>head_ptr</b>.
 * This function is used to extend cpath by another hop.
 */
void
44
cpath_extend_linked_list(crypt_path_t **head_ptr, crypt_path_t *new_hop)
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
{
  if (*head_ptr) {
    new_hop->next = (*head_ptr);
    new_hop->prev = (*head_ptr)->prev;
    (*head_ptr)->prev->next = new_hop;
    (*head_ptr)->prev = new_hop;
  } else {
    *head_ptr = new_hop;
    new_hop->prev = new_hop->next = new_hop;
  }
}

/** Create a new hop, annotate it with information about its
 * corresponding router <b>choice</b>, and append it to the
 * end of the cpath <b>head_ptr</b>. */
int
61
cpath_append_hop(crypt_path_t **head_ptr, extend_info_t *choice)
62
{
63
  crypt_path_t *hop = tor_malloc_zero(sizeof(crypt_path_t));
64
65

  /* link hop into the cpath, at the end. */
66
  cpath_extend_linked_list(head_ptr, hop);
67

68
  hop->magic = CRYPT_PATH_MAGIC;
69
70
71
72
73
74
75
76
77
78
  hop->state = CPATH_STATE_CLOSED;

  hop->extend_info = extend_info_dup(choice);

  hop->package_window = circuit_initial_package_window();
  hop->deliver_window = CIRCWINDOW_START;

  return 0;
}

79
80
81
82
/** Verify that cpath <b>cp</b> has all of its invariants
 * correct. Trigger an assert if anything is invalid.
 */
void
83
cpath_assert_ok(const crypt_path_t *cp)
84
85
86
87
{
  const crypt_path_t *start = cp;

  do {
88
    cpath_assert_layer_ok(cp);
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
    /* layers must be in sequence of: "open* awaiting? closed*" */
    if (cp != start) {
      if (cp->state == CPATH_STATE_AWAITING_KEYS) {
        tor_assert(cp->prev->state == CPATH_STATE_OPEN);
      } else if (cp->state == CPATH_STATE_OPEN) {
        tor_assert(cp->prev->state == CPATH_STATE_OPEN);
      }
    }
    cp = cp->next;
    tor_assert(cp);
  } while (cp != start);
}

/** Verify that cpath layer <b>cp</b> has all of its invariants
 * correct. Trigger an assert if anything is invalid.
 */
void
106
cpath_assert_layer_ok(const crypt_path_t *cp)
107
108
109
110
111
112
113
114
{
//  tor_assert(cp->addr); /* these are zero for rendezvous extra-hops */
//  tor_assert(cp->port);
  tor_assert(cp);
  tor_assert(cp->magic == CRYPT_PATH_MAGIC);
  switch (cp->state)
    {
    case CPATH_STATE_OPEN:
115
      relay_crypto_assert_ok(&cp->pvt_crypto);
116
      FALLTHROUGH;
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
    case CPATH_STATE_CLOSED:
      /*XXXX Assert that there's no handshake_state either. */
      tor_assert(!cp->rend_dh_handshake_state);
      break;
    case CPATH_STATE_AWAITING_KEYS:
      /* tor_assert(cp->dh_handshake_state); */
      break;
    default:
      log_fn(LOG_ERR, LD_BUG, "Unexpected state %d", cp->state);
      tor_assert(0);
    }
  tor_assert(cp->package_window >= 0);
  tor_assert(cp->deliver_window >= 0);
}

132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/** Initialize cpath-\>{f|b}_{crypto|digest} from the key material in key_data.
 *
 * If <b>is_hs_v3</b> is set, this cpath will be used for next gen hidden
 * service circuits and <b>key_data</b> must be at least
 * HS_NTOR_KEY_EXPANSION_KDF_OUT_LEN bytes in length.
 *
 * If <b>is_hs_v3</b> is not set, key_data must contain CPATH_KEY_MATERIAL_LEN
 * bytes, which are used as follows:
 *   - 20 to initialize f_digest
 *   - 20 to initialize b_digest
 *   - 16 to key f_crypto
 *   - 16 to key b_crypto
 *
 * (If 'reverse' is true, then f_XX and b_XX are swapped.)
 *
 * Return 0 if init was successful, else -1 if it failed.
 */
int
150
cpath_init_circuit_crypto(crypt_path_t *cpath,
151
152
153
154
155
                          const char *key_data, size_t key_data_len,
                          int reverse, int is_hs_v3)
{

  tor_assert(cpath);
156
  return relay_crypto_init(&cpath->pvt_crypto, key_data, key_data_len,
157
                           reverse, is_hs_v3);
158
159
}

160
161
/** Deallocate space associated with the cpath node <b>victim</b>. */
void
162
cpath_free(crypt_path_t *victim)
163
{
164
  if (!victim)
165
166
    return;

167
  relay_crypto_clear(&victim->pvt_crypto);
168
169
170
171
172
173
174
  onion_handshake_state_release(&victim->handshake_state);
  crypto_dh_free(victim->rend_dh_handshake_state);
  extend_info_free(victim->extend_info);

  memwipe(victim, 0xBB, sizeof(crypt_path_t)); /* poison memory */
  tor_free(victim);
}
175

176
177
178
179
180
181
182
183
/********************** cpath crypto API *******************************/

/** Encrypt or decrypt <b>payload</b> using the crypto of <b>cpath</b>. Actual
 *  operation decided by <b>is_decrypt</b>.  */
void
cpath_crypt_cell(const crypt_path_t *cpath, uint8_t *payload, bool is_decrypt)
{
  if (is_decrypt) {
184
    relay_crypt_one_payload(cpath->pvt_crypto.b_crypto, payload);
185
  } else {
186
    relay_crypt_one_payload(cpath->pvt_crypto.f_crypto, payload);
187
188
189
190
191
192
193
  }
}

/** Getter for the incoming digest of <b>cpath</b>. */
struct crypto_digest_t *
cpath_get_incoming_digest(const crypt_path_t *cpath)
{
194
  return cpath->pvt_crypto.b_digest;
195
196
197
198
199
200
201
}

/** Set the right integrity digest on the outgoing <b>cell</b> based on the
 *  cell payload and update the forward digest of <b>cpath</b>. */
void
cpath_set_cell_forward_digest(crypt_path_t *cpath, cell_t *cell)
{
202
  relay_set_digest(cpath->pvt_crypto.f_digest, cell);
203
}
204

205
206
207
208
209
210
211
212
213
/************ cpath sendme API ***************************/

/** Return the sendme_digest of this <b>cpath</b>. */
uint8_t *
cpath_get_sendme_digest(crypt_path_t *cpath)
{
  return relay_crypto_get_sendme_digest(&cpath->pvt_crypto);
}

214
215
216
217
218
219
220
221
222
/** Record the cell digest, indicated by is_foward_digest or not, as the
 * SENDME cell digest. */
void
cpath_sendme_record_cell_digest(crypt_path_t *cpath, bool is_foward_digest)
{
  tor_assert(cpath);
  relay_crypto_record_sendme_digest(&cpath->pvt_crypto, is_foward_digest);
}

223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
/************ other cpath functions ***************************/

/** Return the first non-open hop in cpath, or return NULL if all
 * hops are open. */
crypt_path_t *
cpath_get_next_non_open_hop(crypt_path_t *cpath)
{
  crypt_path_t *hop = cpath;
  do {
    if (hop->state != CPATH_STATE_OPEN)
      return hop;
    hop = hop->next;
  } while (hop != cpath);
  return NULL;
}

#ifdef TOR_UNIT_TESTS

/** Unittest helper function: Count number of hops in cpath linked list. */
unsigned int
cpath_get_n_hops(crypt_path_t **head_ptr)
{
  unsigned int n_hops = 0;
  crypt_path_t *tmp;

  if (!*head_ptr) {
    return 0;
  }

  tmp = *head_ptr;
  do {
    n_hops++;
    tmp = tmp->next;
  } while (tmp != *head_ptr);

  return n_hops;
}

#endif /* defined(TOR_UNIT_TESTS) */