diff options
author | nfkd <nfkd@koszmial.pl> | 2013-07-26 19:39:04 -0300 |
---|---|---|
committer | nfkd <nfkd@koszmial.pl> | 2013-07-26 19:39:04 -0300 |
commit | 16c9e23b3c7f047135af2a87887a412cea100197 (patch) | |
tree | 5911de3a16eef1e789094b27ce435033f37e99ce | |
parent | bd48ef52ec99cc45a6b361d8072eef34c343045a (diff) |
Fix braces
-rw-r--r-- | core/net_crypto.c | 144 |
1 files changed, 26 insertions, 118 deletions
diff --git a/core/net_crypto.c b/core/net_crypto.c index 044845f0..28cb83e8 100644 --- a/core/net_crypto.c +++ b/core/net_crypto.c | |||
@@ -62,9 +62,7 @@ int encrypt_data(uint8_t * public_key, uint8_t * secret_key, uint8_t * nonce, | |||
62 | uint8_t * plain, uint32_t length, uint8_t * encrypted) | 62 | uint8_t * plain, uint32_t length, uint8_t * encrypted) |
63 | { | 63 | { |
64 | if(length - crypto_box_BOXZEROBYTES + crypto_box_ZEROBYTES > MAX_DATA_SIZE || length == 0) | 64 | if(length - crypto_box_BOXZEROBYTES + crypto_box_ZEROBYTES > MAX_DATA_SIZE || length == 0) |
65 | { | ||
66 | return -1; | 65 | return -1; |
67 | } | ||
68 | 66 | ||
69 | uint8_t temp_plain[MAX_DATA_SIZE + crypto_box_ZEROBYTES - crypto_box_BOXZEROBYTES] = {0}; | 67 | uint8_t temp_plain[MAX_DATA_SIZE + crypto_box_ZEROBYTES - crypto_box_BOXZEROBYTES] = {0}; |
70 | uint8_t temp_encrypted[MAX_DATA_SIZE + crypto_box_ZEROBYTES]; | 68 | uint8_t temp_encrypted[MAX_DATA_SIZE + crypto_box_ZEROBYTES]; |
@@ -76,9 +74,8 @@ int encrypt_data(uint8_t * public_key, uint8_t * secret_key, uint8_t * nonce, | |||
76 | 74 | ||
77 | /* if encryption is successful the first crypto_box_BOXZEROBYTES of the message will be zero */ | 75 | /* if encryption is successful the first crypto_box_BOXZEROBYTES of the message will be zero */ |
78 | if(memcmp(temp_encrypted, zeroes, crypto_box_BOXZEROBYTES) != 0) | 76 | if(memcmp(temp_encrypted, zeroes, crypto_box_BOXZEROBYTES) != 0) |
79 | { | ||
80 | return -1; | 77 | return -1; |
81 | } | 78 | |
82 | /* unpad the encrypted message */ | 79 | /* unpad the encrypted message */ |
83 | memcpy(encrypted, temp_encrypted + crypto_box_BOXZEROBYTES, length - crypto_box_BOXZEROBYTES + crypto_box_ZEROBYTES); | 80 | memcpy(encrypted, temp_encrypted + crypto_box_BOXZEROBYTES, length - crypto_box_BOXZEROBYTES + crypto_box_ZEROBYTES); |
84 | return length - crypto_box_BOXZEROBYTES + crypto_box_ZEROBYTES; | 81 | return length - crypto_box_BOXZEROBYTES + crypto_box_ZEROBYTES; |
@@ -92,9 +89,8 @@ int decrypt_data(uint8_t * public_key, uint8_t * secret_key, uint8_t * nonce, | |||
92 | uint8_t * encrypted, uint32_t length, uint8_t * plain) | 89 | uint8_t * encrypted, uint32_t length, uint8_t * plain) |
93 | { | 90 | { |
94 | if(length > MAX_DATA_SIZE || length <= crypto_box_BOXZEROBYTES) | 91 | if(length > MAX_DATA_SIZE || length <= crypto_box_BOXZEROBYTES) |
95 | { | ||
96 | return -1; | 92 | return -1; |
97 | } | 93 | |
98 | uint8_t temp_plain[MAX_DATA_SIZE - crypto_box_ZEROBYTES + crypto_box_BOXZEROBYTES]; | 94 | uint8_t temp_plain[MAX_DATA_SIZE - crypto_box_ZEROBYTES + crypto_box_BOXZEROBYTES]; |
99 | uint8_t temp_encrypted[MAX_DATA_SIZE + crypto_box_ZEROBYTES] = {0}; | 95 | uint8_t temp_encrypted[MAX_DATA_SIZE + crypto_box_ZEROBYTES] = {0}; |
100 | uint8_t zeroes[crypto_box_ZEROBYTES] = {0}; | 96 | uint8_t zeroes[crypto_box_ZEROBYTES] = {0}; |
@@ -103,14 +99,12 @@ int decrypt_data(uint8_t * public_key, uint8_t * secret_key, uint8_t * nonce, | |||
103 | 99 | ||
104 | if(crypto_box_open(temp_plain, temp_encrypted, length + crypto_box_BOXZEROBYTES, | 100 | if(crypto_box_open(temp_plain, temp_encrypted, length + crypto_box_BOXZEROBYTES, |
105 | nonce, public_key, secret_key) == -1) | 101 | nonce, public_key, secret_key) == -1) |
106 | { | ||
107 | return -1; | 102 | return -1; |
108 | } | 103 | |
109 | /* if decryption is successful the first crypto_box_ZEROBYTES of the message will be zero */ | 104 | /* if decryption is successful the first crypto_box_ZEROBYTES of the message will be zero */ |
110 | if(memcmp(temp_plain, zeroes, crypto_box_ZEROBYTES) != 0) | 105 | if(memcmp(temp_plain, zeroes, crypto_box_ZEROBYTES) != 0) |
111 | { | ||
112 | return -1; | 106 | return -1; |
113 | } | 107 | |
114 | /* unpad the plain message */ | 108 | /* unpad the plain message */ |
115 | memcpy(plain, temp_plain + crypto_box_ZEROBYTES, length - crypto_box_ZEROBYTES + crypto_box_BOXZEROBYTES); | 109 | memcpy(plain, temp_plain + crypto_box_ZEROBYTES, length - crypto_box_ZEROBYTES + crypto_box_BOXZEROBYTES); |
116 | return length - crypto_box_ZEROBYTES + crypto_box_BOXZEROBYTES; | 110 | return length - crypto_box_ZEROBYTES + crypto_box_BOXZEROBYTES; |
@@ -120,13 +114,10 @@ int decrypt_data(uint8_t * public_key, uint8_t * secret_key, uint8_t * nonce, | |||
120 | void increment_nonce(uint8_t * nonce) | 114 | void increment_nonce(uint8_t * nonce) |
121 | { | 115 | { |
122 | uint32_t i; | 116 | uint32_t i; |
123 | for(i = 0; i < crypto_box_NONCEBYTES; ++i) | 117 | for(i = 0; i < crypto_box_NONCEBYTES; ++i) { |
124 | { | ||
125 | ++nonce[i]; | 118 | ++nonce[i]; |
126 | if(nonce[i] != 0) | 119 | if(nonce[i] != 0) |
127 | { | ||
128 | break; | 120 | break; |
129 | } | ||
130 | } | 121 | } |
131 | } | 122 | } |
132 | 123 | ||
@@ -134,8 +125,7 @@ void increment_nonce(uint8_t * nonce) | |||
134 | void random_nonce(uint8_t * nonce) | 125 | void random_nonce(uint8_t * nonce) |
135 | { | 126 | { |
136 | uint32_t i, temp; | 127 | uint32_t i, temp; |
137 | for (i = 0; i < crypto_box_NONCEBYTES / 4; ++i) | 128 | for (i = 0; i < crypto_box_NONCEBYTES / 4; ++i) { |
138 | { | ||
139 | temp = random_int(); | 129 | temp = random_int(); |
140 | memcpy(nonce + 4 * i, &temp, 4); | 130 | memcpy(nonce + 4 * i, &temp, 4); |
141 | } | 131 | } |
@@ -147,28 +137,19 @@ void random_nonce(uint8_t * nonce) | |||
147 | int read_cryptpacket(int crypt_connection_id, uint8_t * data) | 137 | int read_cryptpacket(int crypt_connection_id, uint8_t * data) |
148 | { | 138 | { |
149 | if(crypt_connection_id < 0 || crypt_connection_id >= MAX_CRYPTO_CONNECTIONS) | 139 | if(crypt_connection_id < 0 || crypt_connection_id >= MAX_CRYPTO_CONNECTIONS) |
150 | { | ||
151 | return 0; | 140 | return 0; |
152 | } | ||
153 | if(crypto_connections[crypt_connection_id].status != 3) | 141 | if(crypto_connections[crypt_connection_id].status != 3) |
154 | { | ||
155 | return 0; | 142 | return 0; |
156 | } | ||
157 | uint8_t temp_data[MAX_DATA_SIZE]; | 143 | uint8_t temp_data[MAX_DATA_SIZE]; |
158 | int length = read_packet(crypto_connections[crypt_connection_id].number, temp_data); | 144 | int length = read_packet(crypto_connections[crypt_connection_id].number, temp_data); |
159 | if(length == 0) | 145 | if(length == 0) |
160 | { | ||
161 | return 0; | 146 | return 0; |
162 | } | ||
163 | if(temp_data[0] != 3) | 147 | if(temp_data[0] != 3) |
164 | { | ||
165 | return -1; | 148 | return -1; |
166 | } | ||
167 | int len = decrypt_data(crypto_connections[crypt_connection_id].peersessionpublic_key, | 149 | int len = decrypt_data(crypto_connections[crypt_connection_id].peersessionpublic_key, |
168 | crypto_connections[crypt_connection_id].sessionsecret_key, | 150 | crypto_connections[crypt_connection_id].sessionsecret_key, |
169 | crypto_connections[crypt_connection_id].recv_nonce, temp_data + 1, length - 1, data); | 151 | crypto_connections[crypt_connection_id].recv_nonce, temp_data + 1, length - 1, data); |
170 | if(len != -1) | 152 | if(len != -1) { |
171 | { | ||
172 | increment_nonce(crypto_connections[crypt_connection_id].recv_nonce); | 153 | increment_nonce(crypto_connections[crypt_connection_id].recv_nonce); |
173 | return len; | 154 | return len; |
174 | } | 155 | } |
@@ -180,30 +161,20 @@ int read_cryptpacket(int crypt_connection_id, uint8_t * data) | |||
180 | int write_cryptpacket(int crypt_connection_id, uint8_t * data, uint32_t length) | 161 | int write_cryptpacket(int crypt_connection_id, uint8_t * data, uint32_t length) |
181 | { | 162 | { |
182 | if(crypt_connection_id < 0 || crypt_connection_id >= MAX_CRYPTO_CONNECTIONS) | 163 | if(crypt_connection_id < 0 || crypt_connection_id >= MAX_CRYPTO_CONNECTIONS) |
183 | { | ||
184 | return 0; | 164 | return 0; |
185 | } | ||
186 | if(length - crypto_box_BOXZEROBYTES + crypto_box_ZEROBYTES > MAX_DATA_SIZE - 1) | 165 | if(length - crypto_box_BOXZEROBYTES + crypto_box_ZEROBYTES > MAX_DATA_SIZE - 1) |
187 | { | ||
188 | return 0; | 166 | return 0; |
189 | } | ||
190 | if(crypto_connections[crypt_connection_id].status != 3) | 167 | if(crypto_connections[crypt_connection_id].status != 3) |
191 | { | ||
192 | return 0; | 168 | return 0; |
193 | } | ||
194 | uint8_t temp_data[MAX_DATA_SIZE]; | 169 | uint8_t temp_data[MAX_DATA_SIZE]; |
195 | int len = encrypt_data(crypto_connections[crypt_connection_id].peersessionpublic_key, | 170 | int len = encrypt_data(crypto_connections[crypt_connection_id].peersessionpublic_key, |
196 | crypto_connections[crypt_connection_id].sessionsecret_key, | 171 | crypto_connections[crypt_connection_id].sessionsecret_key, |
197 | crypto_connections[crypt_connection_id].sent_nonce, data, length, temp_data + 1); | 172 | crypto_connections[crypt_connection_id].sent_nonce, data, length, temp_data + 1); |
198 | if(len == -1) | 173 | if(len == -1) |
199 | { | ||
200 | return 0; | 174 | return 0; |
201 | } | ||
202 | temp_data[0] = 3; | 175 | temp_data[0] = 3; |
203 | if(write_packet(crypto_connections[crypt_connection_id].number, temp_data, len + 1) == 0) | 176 | if(write_packet(crypto_connections[crypt_connection_id].number, temp_data, len + 1) == 0) |
204 | { | ||
205 | return 0; | 177 | return 0; |
206 | } | ||
207 | increment_nonce(crypto_connections[crypt_connection_id].sent_nonce); | 178 | increment_nonce(crypto_connections[crypt_connection_id].sent_nonce); |
208 | return 1; | 179 | return 1; |
209 | } | 180 | } |
@@ -217,17 +188,13 @@ int write_cryptpacket(int crypt_connection_id, uint8_t * data, uint32_t length) | |||
217 | int create_request(uint8_t * packet, uint8_t * public_key, uint8_t * data, uint32_t length, uint8_t request_id) | 188 | int create_request(uint8_t * packet, uint8_t * public_key, uint8_t * data, uint32_t length, uint8_t request_id) |
218 | { | 189 | { |
219 | if(MAX_DATA_SIZE < length + 1 + crypto_box_PUBLICKEYBYTES * 2 + crypto_box_NONCEBYTES + ENCRYPTION_PADDING) | 190 | if(MAX_DATA_SIZE < length + 1 + crypto_box_PUBLICKEYBYTES * 2 + crypto_box_NONCEBYTES + ENCRYPTION_PADDING) |
220 | { | ||
221 | return -1; | 191 | return -1; |
222 | } | ||
223 | uint8_t nonce[crypto_box_NONCEBYTES]; | 192 | uint8_t nonce[crypto_box_NONCEBYTES]; |
224 | random_nonce(nonce); | 193 | random_nonce(nonce); |
225 | int len = encrypt_data(public_key, self_secret_key, nonce, data, length, | 194 | int len = encrypt_data(public_key, self_secret_key, nonce, data, length, |
226 | 1 + crypto_box_PUBLICKEYBYTES * 2 + crypto_box_NONCEBYTES + packet); | 195 | 1 + crypto_box_PUBLICKEYBYTES * 2 + crypto_box_NONCEBYTES + packet); |
227 | if(len == -1) | 196 | if(len == -1) |
228 | { | ||
229 | return -1; | 197 | return -1; |
230 | } | ||
231 | packet[0] = request_id; | 198 | packet[0] = request_id; |
232 | memcpy(packet + 1, public_key, crypto_box_PUBLICKEYBYTES); | 199 | memcpy(packet + 1, public_key, crypto_box_PUBLICKEYBYTES); |
233 | memcpy(packet + 1 + crypto_box_PUBLICKEYBYTES, self_public_key, crypto_box_PUBLICKEYBYTES); | 200 | memcpy(packet + 1 + crypto_box_PUBLICKEYBYTES, self_public_key, crypto_box_PUBLICKEYBYTES); |
@@ -253,15 +220,11 @@ int handle_request(uint8_t * public_key, uint8_t * data, uint8_t * packet, uint1 | |||
253 | int len1 = decrypt_data(public_key, self_secret_key, nonce, packet + 1 + crypto_box_PUBLICKEYBYTES * 2 + crypto_box_NONCEBYTES, | 220 | int len1 = decrypt_data(public_key, self_secret_key, nonce, packet + 1 + crypto_box_PUBLICKEYBYTES * 2 + crypto_box_NONCEBYTES, |
254 | length - (crypto_box_PUBLICKEYBYTES * 2 + crypto_box_NONCEBYTES + 1), data); | 221 | length - (crypto_box_PUBLICKEYBYTES * 2 + crypto_box_NONCEBYTES + 1), data); |
255 | if(len1 == -1) | 222 | if(len1 == -1) |
256 | { | ||
257 | return -1; | 223 | return -1; |
258 | } | ||
259 | return len1; | 224 | return len1; |
260 | } | 225 | } |
261 | else | 226 | else |
262 | { | ||
263 | return -1; | 227 | return -1; |
264 | } | ||
265 | } | 228 | } |
266 | 229 | ||
267 | /* Send a crypto handshake packet containing an encrypted secret nonce and session public key | 230 | /* Send a crypto handshake packet containing an encrypted secret nonce and session public key |
@@ -280,9 +243,7 @@ int send_cryptohandshake(int connection_id, uint8_t * public_key, uint8_t * secr | |||
280 | int len = encrypt_data(public_key, self_secret_key, nonce, temp, crypto_box_NONCEBYTES + crypto_box_PUBLICKEYBYTES, | 243 | int len = encrypt_data(public_key, self_secret_key, nonce, temp, crypto_box_NONCEBYTES + crypto_box_PUBLICKEYBYTES, |
281 | 1 + crypto_box_PUBLICKEYBYTES + crypto_box_NONCEBYTES + temp_data); | 244 | 1 + crypto_box_PUBLICKEYBYTES + crypto_box_NONCEBYTES + temp_data); |
282 | if(len == -1) | 245 | if(len == -1) |
283 | { | ||
284 | return 0; | 246 | return 0; |
285 | } | ||
286 | temp_data[0] = 2; | 247 | temp_data[0] = 2; |
287 | memcpy(temp_data + 1, self_public_key, crypto_box_PUBLICKEYBYTES); | 248 | memcpy(temp_data + 1, self_public_key, crypto_box_PUBLICKEYBYTES); |
288 | memcpy(temp_data + 1 + crypto_box_PUBLICKEYBYTES, nonce, crypto_box_NONCEBYTES); | 249 | memcpy(temp_data + 1 + crypto_box_PUBLICKEYBYTES, nonce, crypto_box_NONCEBYTES); |
@@ -302,9 +263,7 @@ int handle_cryptohandshake(uint8_t * public_key, uint8_t * secret_nonce, | |||
302 | return 0; | 263 | return 0; |
303 | } | 264 | } |
304 | if(data[0] != 2) | 265 | if(data[0] != 2) |
305 | { | ||
306 | return 0; | 266 | return 0; |
307 | } | ||
308 | uint8_t temp[crypto_box_NONCEBYTES + crypto_box_PUBLICKEYBYTES]; | 267 | uint8_t temp[crypto_box_NONCEBYTES + crypto_box_PUBLICKEYBYTES]; |
309 | 268 | ||
310 | memcpy(public_key, data + 1, crypto_box_PUBLICKEYBYTES); | 269 | memcpy(public_key, data + 1, crypto_box_PUBLICKEYBYTES); |
@@ -314,9 +273,7 @@ int handle_cryptohandshake(uint8_t * public_key, uint8_t * secret_nonce, | |||
314 | crypto_box_NONCEBYTES + crypto_box_PUBLICKEYBYTES + pad, temp); | 273 | crypto_box_NONCEBYTES + crypto_box_PUBLICKEYBYTES + pad, temp); |
315 | 274 | ||
316 | if(len != crypto_box_NONCEBYTES + crypto_box_PUBLICKEYBYTES) | 275 | if(len != crypto_box_NONCEBYTES + crypto_box_PUBLICKEYBYTES) |
317 | { | ||
318 | return 0; | 276 | return 0; |
319 | } | ||
320 | 277 | ||
321 | memcpy(secret_nonce, temp, crypto_box_NONCEBYTES); | 278 | memcpy(secret_nonce, temp, crypto_box_NONCEBYTES); |
322 | memcpy(session_key, temp + crypto_box_NONCEBYTES, crypto_box_PUBLICKEYBYTES); | 279 | memcpy(session_key, temp + crypto_box_NONCEBYTES, crypto_box_PUBLICKEYBYTES); |
@@ -330,15 +287,9 @@ int getcryptconnection_id(uint8_t * public_key) | |||
330 | { | 287 | { |
331 | uint32_t i; | 288 | uint32_t i; |
332 | for(i = 0; i < MAX_CRYPTO_CONNECTIONS; ++i) | 289 | for(i = 0; i < MAX_CRYPTO_CONNECTIONS; ++i) |
333 | { | ||
334 | if(crypto_connections[i].status > 0) | 290 | if(crypto_connections[i].status > 0) |
335 | { | ||
336 | if(memcmp(public_key, crypto_connections[i].public_key, crypto_box_PUBLICKEYBYTES) == 0) | 291 | if(memcmp(public_key, crypto_connections[i].public_key, crypto_box_PUBLICKEYBYTES) == 0) |
337 | { | ||
338 | return i; | 292 | return i; |
339 | } | ||
340 | } | ||
341 | } | ||
342 | return -1; | 293 | return -1; |
343 | } | 294 | } |
344 | 295 | ||
@@ -349,23 +300,17 @@ int crypto_connect(uint8_t * public_key, IP_Port ip_port) | |||
349 | { | 300 | { |
350 | uint32_t i; | 301 | uint32_t i; |
351 | int id = getcryptconnection_id(public_key); | 302 | int id = getcryptconnection_id(public_key); |
352 | if(id != -1) | 303 | if(id != -1) { |
353 | { | ||
354 | IP_Port c_ip = connection_ip(crypto_connections[id].number); | 304 | IP_Port c_ip = connection_ip(crypto_connections[id].number); |
355 | if(c_ip.ip.i == ip_port.ip.i && c_ip.port == ip_port.port) | 305 | if(c_ip.ip.i == ip_port.ip.i && c_ip.port == ip_port.port) |
356 | { | ||
357 | return -1; | 306 | return -1; |
358 | } | ||
359 | } | 307 | } |
360 | for(i = 0; i < MAX_CRYPTO_CONNECTIONS; ++i) | 308 | for(i = 0; i < MAX_CRYPTO_CONNECTIONS; ++i) |
361 | { | 309 | { |
362 | if(crypto_connections[i].status == 0) | 310 | if(crypto_connections[i].status == 0) { |
363 | { | ||
364 | int id = new_connection(ip_port); | 311 | int id = new_connection(ip_port); |
365 | if(id == -1) | 312 | if(id == -1) |
366 | { | ||
367 | return -1; | 313 | return -1; |
368 | } | ||
369 | crypto_connections[i].number = id; | 314 | crypto_connections[i].number = id; |
370 | crypto_connections[i].status = 1; | 315 | crypto_connections[i].status = 1; |
371 | random_nonce(crypto_connections[i].recv_nonce); | 316 | random_nonce(crypto_connections[i].recv_nonce); |
@@ -396,20 +341,16 @@ int crypto_inbound(uint8_t * public_key, uint8_t * secret_nonce, uint8_t * sessi | |||
396 | uint32_t i; | 341 | uint32_t i; |
397 | for(i = 0; i < MAX_INCOMING; ++i) | 342 | for(i = 0; i < MAX_INCOMING; ++i) |
398 | { | 343 | { |
399 | if(incoming_connections[i] != -1) | 344 | if(incoming_connections[i] != -1) { |
400 | { | 345 | if(is_connected(incoming_connections[i]) == 4 || is_connected(incoming_connections[i]) == 0) { |
401 | if(is_connected(incoming_connections[i]) == 4 || is_connected(incoming_connections[i]) == 0) | ||
402 | { | ||
403 | kill_connection(incoming_connections[i]); | 346 | kill_connection(incoming_connections[i]); |
404 | incoming_connections[i] = -1; | 347 | incoming_connections[i] = -1; |
405 | continue; | 348 | continue; |
406 | } | 349 | } |
407 | if(id_packet(incoming_connections[i]) == 2) | 350 | if(id_packet(incoming_connections[i]) == 2) { |
408 | { | ||
409 | uint8_t temp_data[MAX_DATA_SIZE]; | 351 | uint8_t temp_data[MAX_DATA_SIZE]; |
410 | uint16_t len = read_packet(incoming_connections[i], temp_data); | 352 | uint16_t len = read_packet(incoming_connections[i], temp_data); |
411 | if(handle_cryptohandshake(public_key, secret_nonce, session_key, temp_data, len)) | 353 | if(handle_cryptohandshake(public_key, secret_nonce, session_key, temp_data, len)) { |
412 | { | ||
413 | int connection_id = incoming_connections[i]; | 354 | int connection_id = incoming_connections[i]; |
414 | incoming_connections[i] = -1; /* remove this connection from the incoming connection list. */ | 355 | incoming_connections[i] = -1; /* remove this connection from the incoming connection list. */ |
415 | return connection_id; | 356 | return connection_id; |
@@ -426,11 +367,8 @@ int crypto_inbound(uint8_t * public_key, uint8_t * secret_nonce, uint8_t * sessi | |||
426 | int crypto_kill(int crypt_connection_id) | 367 | int crypto_kill(int crypt_connection_id) |
427 | { | 368 | { |
428 | if(crypt_connection_id < 0 || crypt_connection_id >= MAX_CRYPTO_CONNECTIONS) | 369 | if(crypt_connection_id < 0 || crypt_connection_id >= MAX_CRYPTO_CONNECTIONS) |
429 | { | ||
430 | return 1; | 370 | return 1; |
431 | } | 371 | if(crypto_connections[crypt_connection_id].status != 0) { |
432 | if(crypto_connections[crypt_connection_id].status != 0) | ||
433 | { | ||
434 | crypto_connections[crypt_connection_id].status = 0; | 372 | crypto_connections[crypt_connection_id].status = 0; |
435 | kill_connection(crypto_connections[crypt_connection_id].number); | 373 | kill_connection(crypto_connections[crypt_connection_id].number); |
436 | crypto_connections[crypt_connection_id].number = ~0; | 374 | crypto_connections[crypt_connection_id].number = ~0; |
@@ -446,9 +384,7 @@ int accept_crypto_inbound(int connection_id, uint8_t * public_key, uint8_t * sec | |||
446 | { | 384 | { |
447 | uint32_t i; | 385 | uint32_t i; |
448 | if(connection_id == -1) | 386 | if(connection_id == -1) |
449 | { | ||
450 | return -1; | 387 | return -1; |
451 | } | ||
452 | /* | 388 | /* |
453 | if(getcryptconnection_id(public_key) != -1) | 389 | if(getcryptconnection_id(public_key) != -1) |
454 | { | 390 | { |
@@ -456,8 +392,7 @@ int accept_crypto_inbound(int connection_id, uint8_t * public_key, uint8_t * sec | |||
456 | }*/ | 392 | }*/ |
457 | for(i = 0; i < MAX_CRYPTO_CONNECTIONS; ++i) | 393 | for(i = 0; i < MAX_CRYPTO_CONNECTIONS; ++i) |
458 | { | 394 | { |
459 | if(crypto_connections[i].status == 0) | 395 | if(crypto_connections[i].status == 0) { |
460 | { | ||
461 | crypto_connections[i].number = connection_id; | 396 | crypto_connections[i].number = connection_id; |
462 | crypto_connections[i].status = 2; | 397 | crypto_connections[i].status = 2; |
463 | random_nonce(crypto_connections[i].recv_nonce); | 398 | random_nonce(crypto_connections[i].recv_nonce); |
@@ -490,9 +425,7 @@ int accept_crypto_inbound(int connection_id, uint8_t * public_key, uint8_t * sec | |||
490 | int is_cryptoconnected(int crypt_connection_id) | 425 | int is_cryptoconnected(int crypt_connection_id) |
491 | { | 426 | { |
492 | if(crypt_connection_id >= 0 && crypt_connection_id < MAX_CRYPTO_CONNECTIONS) | 427 | if(crypt_connection_id >= 0 && crypt_connection_id < MAX_CRYPTO_CONNECTIONS) |
493 | { | ||
494 | return crypto_connections[crypt_connection_id].status; | 428 | return crypto_connections[crypt_connection_id].status; |
495 | } | ||
496 | return 0; | 429 | return 0; |
497 | } | 430 | } |
498 | 431 | ||
@@ -526,10 +459,8 @@ void load_keys(uint8_t * keys) | |||
526 | int new_incoming(int id) | 459 | int new_incoming(int id) |
527 | { | 460 | { |
528 | uint32_t i; | 461 | uint32_t i; |
529 | for(i = 0; i < MAX_INCOMING; ++i) | 462 | for(i = 0; i < MAX_INCOMING; ++i) { |
530 | { | 463 | if(incoming_connections[i] == -1) { |
531 | if(incoming_connections[i] == -1) | ||
532 | { | ||
533 | incoming_connections[i] = id; | 464 | incoming_connections[i] = id; |
534 | return 0; | 465 | return 0; |
535 | } | 466 | } |
@@ -542,13 +473,10 @@ int new_incoming(int id) | |||
542 | static void handle_incomings() | 473 | static void handle_incomings() |
543 | { | 474 | { |
544 | int income; | 475 | int income; |
545 | while(1) | 476 | while(1) { |
546 | { | ||
547 | income = incoming_connection(); | 477 | income = incoming_connection(); |
548 | if(income == -1 || new_incoming(income) ) | 478 | if(income == -1 || new_incoming(income) ) |
549 | { | ||
550 | break; | 479 | break; |
551 | } | ||
552 | } | 480 | } |
553 | } | 481 | } |
554 | 482 | ||
@@ -567,17 +495,11 @@ static void receive_crypto() | |||
567 | uint16_t len; | 495 | uint16_t len; |
568 | if(id_packet(crypto_connections[i].number) == 1) | 496 | if(id_packet(crypto_connections[i].number) == 1) |
569 | /* if the packet is a friend request drop it (because we are already friends) */ | 497 | /* if the packet is a friend request drop it (because we are already friends) */ |
570 | { | ||
571 | len = read_packet(crypto_connections[i].number, temp_data); | 498 | len = read_packet(crypto_connections[i].number, temp_data); |
572 | 499 | if(id_packet(crypto_connections[i].number) == 2) { /* handle handshake packet. */ | |
573 | } | ||
574 | if(id_packet(crypto_connections[i].number) == 2) /* handle handshake packet. */ | ||
575 | { | ||
576 | len = read_packet(crypto_connections[i].number, temp_data); | 500 | len = read_packet(crypto_connections[i].number, temp_data); |
577 | if(handle_cryptohandshake(public_key, secret_nonce, session_key, temp_data, len)) | 501 | if(handle_cryptohandshake(public_key, secret_nonce, session_key, temp_data, len)) { |
578 | { | 502 | if(memcmp(public_key, crypto_connections[i].public_key, crypto_box_PUBLICKEYBYTES) == 0) { |
579 | if(memcmp(public_key, crypto_connections[i].public_key, crypto_box_PUBLICKEYBYTES) == 0) | ||
580 | { | ||
581 | memcpy(crypto_connections[i].sent_nonce, secret_nonce, crypto_box_NONCEBYTES); | 503 | memcpy(crypto_connections[i].sent_nonce, secret_nonce, crypto_box_NONCEBYTES); |
582 | memcpy(crypto_connections[i].peersessionpublic_key, session_key, crypto_box_PUBLICKEYBYTES); | 504 | memcpy(crypto_connections[i].peersessionpublic_key, session_key, crypto_box_PUBLICKEYBYTES); |
583 | increment_nonce(crypto_connections[i].sent_nonce); | 505 | increment_nonce(crypto_connections[i].sent_nonce); |
@@ -589,17 +511,14 @@ static void receive_crypto() | |||
589 | } | 511 | } |
590 | } | 512 | } |
591 | else if(id_packet(crypto_connections[i].number) != -1) | 513 | else if(id_packet(crypto_connections[i].number) != -1) |
592 | { | ||
593 | /* This should not happen | 514 | /* This should not happen |
594 | kill the connection if it does */ | 515 | kill the connection if it does */ |
595 | crypto_kill(crypto_connections[i].number); | 516 | crypto_kill(crypto_connections[i].number); |
596 | } | ||
597 | 517 | ||
598 | } | 518 | } |
599 | if(crypto_connections[i].status == 2) | 519 | if(crypto_connections[i].status == 2) |
600 | { | 520 | { |
601 | if(id_packet(crypto_connections[i].number) == 3) | 521 | if(id_packet(crypto_connections[i].number) == 3) { |
602 | { | ||
603 | uint8_t temp_data[MAX_DATA_SIZE]; | 522 | uint8_t temp_data[MAX_DATA_SIZE]; |
604 | uint8_t data[MAX_DATA_SIZE]; | 523 | uint8_t data[MAX_DATA_SIZE]; |
605 | int length = read_packet(crypto_connections[i].number, temp_data); | 524 | int length = read_packet(crypto_connections[i].number, temp_data); |
@@ -607,8 +526,7 @@ static void receive_crypto() | |||
607 | crypto_connections[i].sessionsecret_key, | 526 | crypto_connections[i].sessionsecret_key, |
608 | crypto_connections[i].recv_nonce, temp_data + 1, length - 1, data); | 527 | crypto_connections[i].recv_nonce, temp_data + 1, length - 1, data); |
609 | uint32_t zero = 0; | 528 | uint32_t zero = 0; |
610 | if(len == sizeof(uint32_t) && memcmp(((uint8_t *)&zero), data, sizeof(uint32_t)) == 0) | 529 | if(len == sizeof(uint32_t) && memcmp(((uint8_t *)&zero), data, sizeof(uint32_t)) == 0) { |
611 | { | ||
612 | increment_nonce(crypto_connections[i].recv_nonce); | 530 | increment_nonce(crypto_connections[i].recv_nonce); |
613 | crypto_connections[i].status = 3; | 531 | crypto_connections[i].status = 3; |
614 | 532 | ||
@@ -616,18 +534,14 @@ static void receive_crypto() | |||
616 | kill_connection_in(crypto_connections[i].number, 3000000); | 534 | kill_connection_in(crypto_connections[i].number, 3000000); |
617 | } | 535 | } |
618 | else | 536 | else |
619 | { | ||
620 | /* This should not happen | 537 | /* This should not happen |
621 | kill the connection if it does */ | 538 | kill the connection if it does */ |
622 | crypto_kill(crypto_connections[i].number); | 539 | crypto_kill(crypto_connections[i].number); |
623 | } | ||
624 | } | 540 | } |
625 | else if(id_packet(crypto_connections[i].number) != -1) | 541 | else if(id_packet(crypto_connections[i].number) != -1) |
626 | { | ||
627 | /* This should not happen | 542 | /* This should not happen |
628 | kill the connection if it does */ | 543 | kill the connection if it does */ |
629 | crypto_kill(crypto_connections[i].number); | 544 | crypto_kill(crypto_connections[i].number); |
630 | } | ||
631 | } | 545 | } |
632 | } | 546 | } |
633 | } | 547 | } |
@@ -640,22 +554,16 @@ void initNetCrypto() | |||
640 | memset(incoming_connections, -1 ,sizeof(incoming_connections)); | 554 | memset(incoming_connections, -1 ,sizeof(incoming_connections)); |
641 | uint32_t i; | 555 | uint32_t i; |
642 | for(i = 0; i < MAX_CRYPTO_CONNECTIONS; ++i) | 556 | for(i = 0; i < MAX_CRYPTO_CONNECTIONS; ++i) |
643 | { | ||
644 | crypto_connections[i].number = ~0; | 557 | crypto_connections[i].number = ~0; |
645 | } | ||
646 | } | 558 | } |
647 | 559 | ||
648 | static void killTimedout() | 560 | static void killTimedout() |
649 | { | 561 | { |
650 | uint32_t i; | 562 | uint32_t i; |
651 | for(i = 0; i < MAX_CRYPTO_CONNECTIONS; ++i) | 563 | for(i = 0; i < MAX_CRYPTO_CONNECTIONS; ++i) { |
652 | { | ||
653 | if(crypto_connections[i].status != 0 && is_connected(crypto_connections[i].number) == 4) | 564 | if(crypto_connections[i].status != 0 && is_connected(crypto_connections[i].number) == 4) |
654 | { | ||
655 | crypto_connections[i].status = 4; | 565 | crypto_connections[i].status = 4; |
656 | } | 566 | else if(is_connected(crypto_connections[i].number) == 4) { |
657 | else if(is_connected(crypto_connections[i].number) == 4) | ||
658 | { | ||
659 | kill_connection(crypto_connections[i].number); | 567 | kill_connection(crypto_connections[i].number); |
660 | crypto_connections[i].number = ~0; | 568 | crypto_connections[i].number = ~0; |
661 | } | 569 | } |
@@ -671,4 +579,4 @@ void doNetCrypto() | |||
671 | handle_incomings(); | 579 | handle_incomings(); |
672 | receive_crypto(); | 580 | receive_crypto(); |
673 | killTimedout(); | 581 | killTimedout(); |
674 | } \ No newline at end of file | 582 | } |