1 /* Copyright 2001 Matej Pfajfar.
2 * Copyright 2001-2004 Roger Dingledine.
3 * Copyright 2004 Roger Dingledine, Nick Mathewson. */
4 /* See LICENSE for licensing information */
6 const char onion_c_id
[] = "$Id$";
10 * \brief Functions to queue create cells, and handle onionskin
11 * parsing and creation.
16 struct onion_queue_t
{
18 struct onion_queue_t
*next
;
21 /** global (within this file) variables used by the next few functions */
22 static struct onion_queue_t
*ol_list
=NULL
;
23 static struct onion_queue_t
*ol_tail
=NULL
;
24 /** length of ol_list */
25 static int ol_length
=0;
27 /** Add <b>circ</b> to the end of ol_list and return 0, except
28 * if ol_list is too long, in which case do nothing and return -1.
30 int onion_pending_add(circuit_t
*circ
) {
31 struct onion_queue_t
*tmp
;
33 tmp
= tor_malloc_zero(sizeof(struct onion_queue_t
));
38 tor_assert(!ol_length
);
46 tor_assert(!ol_tail
->next
);
48 if (ol_length
>= get_options()->MaxOnionsPending
) {
49 log_fn(LOG_NOTICE
,"Already have %d onions queued. Closing.", ol_length
);
60 /** Remove the first item from ol_list and return it, or return
61 * NULL if the list is empty.
63 circuit_t
*onion_next_task(void) {
67 return NULL
; /* no onions pending, we're done */
69 tor_assert(ol_list
->circ
);
70 tor_assert(ol_list
->circ
->p_conn
); /* make sure it's still valid */
71 tor_assert(ol_length
> 0);
73 onion_pending_remove(ol_list
->circ
);
77 /** Go through ol_list, find the onion_queue_t element which points to
78 * circ, remove and free that element. Leave circ itself alone.
80 void onion_pending_remove(circuit_t
*circ
) {
81 struct onion_queue_t
*tmpo
, *victim
;
84 return; /* nothing here. */
86 /* first check to see if it's the first entry */
88 if (tmpo
->circ
== circ
) {
89 /* it's the first one. remove it from the list. */
95 } else { /* we need to hunt through the rest of the list */
96 for ( ;tmpo
->next
&& tmpo
->next
->circ
!= circ
; tmpo
=tmpo
->next
) ;
98 log_fn(LOG_DEBUG
,"circ (p_circ_id %d) not in list, probably at cpuworker.",circ
->p_circ_id
);
101 /* now we know tmpo->next->circ == circ */
103 tmpo
->next
= victim
->next
;
104 if (ol_tail
== victim
)
109 /* now victim points to the element that needs to be removed */
114 /*----------------------------------------------------------------------*/
116 /** Given a router's 128 byte public key,
117 * stores the following in onion_skin_out:
118 * - [42 bytes] OAEP padding
119 * - [16 bytes] Symmetric key for encrypting blob past RSA
120 * - [70 bytes] g^x part 1 (inside the RSA)
121 * - [58 bytes] g^x part 2 (symmetrically encrypted)
123 * Stores the DH private key into handshake_state_out for later completion
126 * The meeting point/cookies and auth are zeroed out for now.
129 onion_skin_create(crypto_pk_env_t
*dest_router_key
,
130 crypto_dh_env_t
**handshake_state_out
,
131 char *onion_skin_out
) /* Must be ONIONSKIN_CHALLENGE_LEN bytes */
133 char *challenge
= NULL
;
134 crypto_dh_env_t
*dh
= NULL
;
135 int dhbytes
, pkbytes
;
137 *handshake_state_out
= NULL
;
138 memset(onion_skin_out
, 0, ONIONSKIN_CHALLENGE_LEN
);
140 if (!(dh
= crypto_dh_new()))
143 dhbytes
= crypto_dh_get_bytes(dh
);
144 pkbytes
= crypto_pk_keysize(dest_router_key
);
145 tor_assert(dhbytes
== 128);
146 tor_assert(pkbytes
== 128);
147 challenge
= tor_malloc_zero(DH_KEY_LEN
);
149 if (crypto_dh_get_public(dh
, challenge
, dhbytes
))
152 #ifdef DEBUG_ONION_SKINS
154 { int _i; for (_i = 0; _i<n; ++_i) printf("%02x ",((int)(a)[_i])&0xFF); }
156 printf("Client: client g^x:");
162 printf("Client: client symkey:");
167 /* set meeting point, meeting cookie, etc here. Leave zero for now. */
168 if (crypto_pk_public_hybrid_encrypt(dest_router_key
, onion_skin_out
,
169 challenge
, DH_KEY_LEN
,
170 PK_PKCS1_OAEP_PADDING
, 1)<0)
174 *handshake_state_out
= dh
;
179 if (dh
) crypto_dh_free(dh
);
183 /** Given an encrypted DH public key as generated by onion_skin_create,
184 * and the private key for this onion router, generate the reply (128-byte
185 * DH plus the first 20 bytes of shared key material), and store the
186 * next key_out_len bytes of key material in key_out.
189 onion_skin_server_handshake(char *onion_skin
, /* ONIONSKIN_CHALLENGE_LEN bytes */
190 crypto_pk_env_t
*private_key
,
191 crypto_pk_env_t
*prev_private_key
,
192 char *handshake_reply_out
, /* ONIONSKIN_REPLY_LEN bytes */
196 char challenge
[ONIONSKIN_CHALLENGE_LEN
];
197 crypto_dh_env_t
*dh
= NULL
;
199 char *key_material
=NULL
;
205 k
= i
==0?private_key
:prev_private_key
;
208 len
= crypto_pk_private_hybrid_decrypt(k
, challenge
,
209 onion_skin
, ONIONSKIN_CHALLENGE_LEN
,
210 PK_PKCS1_OAEP_PADDING
,0);
215 log_fn(LOG_INFO
, "Couldn't decrypt onionskin: client may be using old onion key");
217 } else if (len
!= DH_KEY_LEN
) {
218 log_fn(LOG_WARN
, "Unexpected onionskin length after decryption: %d",
223 dh
= crypto_dh_new();
224 if (crypto_dh_get_public(dh
, handshake_reply_out
, DH_KEY_LEN
))
227 #ifdef DEBUG_ONION_SKINS
228 printf("Server: server g^y:");
229 PA(handshake_reply_out
+0,3);
231 PA(handshake_reply_out
+125,3);
235 key_material
= tor_malloc(DIGEST_LEN
+key_out_len
);
236 len
= crypto_dh_compute_secret(dh
, challenge
, DH_KEY_LEN
,
237 key_material
, DIGEST_LEN
+key_out_len
);
241 /* send back H(K|0) as proof that we learned K. */
242 memcpy(handshake_reply_out
+DH_KEY_LEN
, key_material
, DIGEST_LEN
);
244 /* use the rest of the key material for our shared keys, digests, etc */
245 memcpy(key_out
, key_material
+DIGEST_LEN
, key_out_len
);
247 #ifdef DEBUG_ONION_SKINS
248 printf("Server: key material:");
251 printf("Server: keys out:");
252 PA(key_out
, key_out_len
);
256 tor_free(key_material
);
260 tor_free(key_material
);
261 if (dh
) crypto_dh_free(dh
);
266 /** Finish the client side of the DH handshake.
267 * Given the 128 byte DH reply + 20 byte hash as generated by
268 * onion_skin_server_handshake and the handshake state generated by
269 * onion_skin_create, verify H(K) with the first 20 bytes of shared
270 * key material, then generate key_out_len more bytes of shared key
271 * material and store them in key_out.
273 * After the invocation, call crypto_dh_free on handshake_state.
276 onion_skin_client_handshake(crypto_dh_env_t
*handshake_state
,
277 char *handshake_reply
, /* Must be ONIONSKIN_REPLY_LEN bytes */
282 char *key_material
=NULL
;
283 tor_assert(crypto_dh_get_bytes(handshake_state
) == DH_KEY_LEN
);
285 #ifdef DEBUG_ONION_SKINS
286 printf("Client: server g^y:");
287 PA(handshake_reply
+0,3);
289 PA(handshake_reply
+125,3);
293 key_material
= tor_malloc(20+key_out_len
);
294 len
= crypto_dh_compute_secret(handshake_state
, handshake_reply
, DH_KEY_LEN
,
295 key_material
, 20+key_out_len
);
299 if (memcmp(key_material
, handshake_reply
+DH_KEY_LEN
, 20)) {
300 /* H(K) does *not* match. Something fishy. */
301 tor_free(key_material
);
302 log_fn(LOG_WARN
,"Digest DOES NOT MATCH on onion handshake. Bug or attack.");
306 /* use the rest of the key material for our shared keys, digests, etc */
307 memcpy(key_out
, key_material
+20, key_out_len
);
309 #ifdef DEBUG_ONION_SKINS
310 printf("Client: keys out:");
311 PA(key_out
, key_out_len
);
315 tor_free(key_material
);