… | |
… | |
38 | #include "conf.h" |
38 | #include "conf.h" |
39 | #include "sockinfo.h" |
39 | #include "sockinfo.h" |
40 | #include "util.h" |
40 | #include "util.h" |
41 | #include "device.h" |
41 | #include "device.h" |
42 | #include "curve25519.h" |
42 | #include "curve25519.h" |
|
|
43 | #include "iv_gen.h" |
43 | |
44 | |
44 | struct vpn; |
45 | struct vpn; |
45 | |
46 | |
46 | // called after HUP etc. to (re-)initialize global data structures |
47 | // called after HUP etc. to (re-)initialize global data structures |
47 | void connection_init (); |
48 | void connection_init (); |
48 | |
49 | |
49 | typedef curve25519_key ecdh_key; |
50 | typedef curve25519_key ecdh_key; |
50 | |
51 | |
51 | struct rsa_data |
52 | struct rsa_data |
52 | { |
53 | { |
53 | u32 seqno; |
54 | u32 seqno; // (ictx) initial sequence nr (31 bits) |
54 | u8 auth_key[AUTH_SIZE]; |
|
|
55 | u8 mac_key[MAC_KEYSIZE]; // used to generate hmac key |
55 | u8 mac_key[MAC_IKMSIZE]; // (ictx) used to generate hmac key |
56 | u8 cipher_key[CIPHER_KEYSIZE]; // used to generate cipher key |
56 | u8 cipher_key[CIPHER_IKMSIZE]; // (ictx) used to generate cipher key |
57 | u8 hkdf_salt[HKDF_SALT]; // used as hkdf salt |
57 | u8 hkdf_salt[HKDF_SALT]; // (octx) used as hkdf salt |
58 | u8 ikm[IKM_SIZE]; // used as additional keying material for both sides |
58 | u8 extra_auth[ // (ictx) additional auth randomness |
59 | u8 pad[ |
|
|
60 | (RSABITS >> 3) |
59 | (RSABITS >> 3) |
61 | - 41 // OAEP |
60 | - RSA_OAEP_SIZE |
62 | - sizeof (u32) // seqno |
61 | - sizeof (u32) // seqno |
63 | - AUTH_SIZE |
|
|
64 | - MAC_KEYSIZE |
62 | - MAC_IKMSIZE |
65 | - CIPHER_KEYSIZE |
63 | - CIPHER_IKMSIZE |
66 | - HKDF_SALT |
64 | - HKDF_SALT |
67 | - IKM_SIZE |
|
|
68 | - 3 // struct alignment... |
65 | - 3 // struct alignment... |
69 | ]; |
66 | ]; |
70 | }; |
67 | }; |
71 | |
68 | |
72 | struct auth_data |
69 | struct auth_data |
… | |
… | |
101 | |
98 | |
102 | void hmac_set (crypto_ctx * ctx); |
99 | void hmac_set (crypto_ctx * ctx); |
103 | bool hmac_chk (crypto_ctx * ctx); |
100 | bool hmac_chk (crypto_ctx * ctx); |
104 | |
101 | |
105 | private: |
102 | private: |
106 | static unsigned char hmac_digest[EVP_MAX_MD_SIZE]; |
|
|
107 | |
|
|
108 | void hmac_gen (crypto_ctx * ctx); |
103 | void hmac_gen (crypto_ctx * ctx, u8 *hmac_digest); |
109 | }; |
104 | }; |
110 | |
105 | |
111 | struct vpn_packet : hmac_packet |
106 | struct vpn_packet : hmac_packet |
112 | { |
107 | { |
113 | enum ptype |
108 | enum ptype |
… | |
… | |
197 | u32 oseqno; |
192 | u32 oseqno; |
198 | sliding_window iseqno; |
193 | sliding_window iseqno; |
199 | |
194 | |
200 | u8 protocol; |
195 | u8 protocol; |
201 | u8 features; |
196 | u8 features; |
202 | bool is_direct; // current connection (si) is direct? |
|
|
203 | |
197 | |
204 | pkt_queue data_queue, vpn_queue; |
198 | pkt_queue data_queue, vpn_queue; |
205 | |
199 | |
206 | crypto_ctx *octx, *ictx; |
200 | crypto_ctx *octx, *ictx; |
207 | |
201 | |
208 | void generate_auth_data (); |
202 | void generate_auth_data (); |
209 | |
203 | |
210 | ev_tstamp auth_expire; // when the snd_* and *_ecdh values expire |
204 | ev_tstamp auth_expire; // when the snd_* and *_ecdh values expire |
|
|
205 | ev_tstamp hmac_error; // time of first hmac error in a series |
211 | |
206 | |
212 | // send auth data - used for octx |
207 | // send auth data - used for octx |
213 | auth_data snd_auth; |
208 | auth_data snd_auth; |
214 | auth_mac snd_auth_mac; // expected response mac |
|
|
215 | ecdh_key snd_ecdh_a; // the secret ecdh key we used for our request |
209 | ecdh_key snd_ecdh_a; // the secret ecdh key we used for our request |
216 | ecdh_key snd_ecdh_b; // the public ecdh key we received in the response |
210 | ecdh_key snd_ecdh_b; // the public ecdh key we received in the response |
217 | bool have_snd_auth; // received response for our req |
211 | bool have_snd_auth; // received response for our req |
218 | |
212 | |
219 | // receive auth data - used for ictx |
213 | // receive auth data - used for ictx |
… | |
… | |
232 | void reset_si (); |
226 | void reset_si (); |
233 | const sockinfo &forward_si (const sockinfo &si) const; |
227 | const sockinfo &forward_si (const sockinfo &si) const; |
234 | |
228 | |
235 | void shutdown (); |
229 | void shutdown (); |
236 | void connection_established (const sockinfo &rsi); |
230 | void connection_established (const sockinfo &rsi); |
237 | void reset_connection (); |
231 | void reset_connection (const char *reason); |
238 | |
232 | |
239 | void establish_connection_cb (ev::timer &w, int revents); ev::timer establish_connection; |
233 | void establish_connection_cb (ev::timer &w, int revents); ev::timer establish_connection; |
240 | void rekey_cb (ev::timer &w, int revents); ev::timer rekey; // next rekying (actually current reset + reestablishing) |
234 | void rekey_cb (ev::timer &w, int revents); ev::timer rekey; // next rekeying (actually current reset + reestablishing) |
241 | void keepalive_cb (ev::timer &w, int revents); ev::timer keepalive; // next keepalive probe |
235 | void keepalive_cb (ev::timer &w, int revents); ev::timer keepalive; // next keepalive probe |
242 | |
236 | |
243 | void send_connect_request (int id); |
237 | void send_connect_request (int id); |
244 | void send_auth_request (const sockinfo &si, bool initiate); |
238 | void send_auth_request (const sockinfo &si, bool initiate); |
245 | void send_auth_response (const sockinfo &si); |
239 | void send_auth_response (const sockinfo &si); |