Skip to content

Commit 6393e54

Browse files
committed
Revert "vk rpc protocol version 1 supported, with optional encryption (#1463)"
This reverts commit b168a45.
1 parent b168a45 commit 6393e54

13 files changed

Lines changed: 87 additions & 122 deletions

net/net-crypto-aes.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ int aes_generate_nonce (char res[16]) {
205205

206206
int aes_create_keys(aes_key_t *key, struct aes_session_key *R, int am_client, const char nonce_server[16], const char nonce_client[16], int client_timestamp,
207207
unsigned server_ip, unsigned short server_port, const unsigned char server_ipv6[16], unsigned client_ip, unsigned short client_port,
208-
const unsigned char client_ipv6[16], bool is_ip_v6) {
208+
const unsigned char client_ipv6[16]) {
209209
unsigned char str[16 + 16 + 4 + 4 + 2 + 6 + 4 + 2 + AES_KEY_MAX_LEN + 16 + 16 + 4 + 16 * 2];
210210
int str_len;
211211

@@ -228,10 +228,13 @@ int aes_create_keys(aes_key_t *key, struct aes_session_key *R, int am_client, co
228228
memcpy(str + 54 + pwd_len, nonce_server, 16);
229229
str_len = 70 + pwd_len;
230230

231-
if (is_ip_v6) {
231+
if (!server_ip) {
232+
assert(!client_ip);
232233
memcpy(str + str_len, client_ipv6, 16);
233234
memcpy(str + str_len + 16, server_ipv6, 16);
234235
str_len += 32;
236+
} else {
237+
assert(client_ip);
235238
}
236239

237240
memcpy(str + str_len, nonce_client, 16);

net/net-crypto-aes.h

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -56,22 +56,19 @@ int aes_generate_nonce (char res[16]);
5656

5757
int aes_create_keys(aes_key_t *key, struct aes_session_key *R, int am_client, const char nonce_server[16], const char nonce_client[16], int client_timestamp,
5858
unsigned server_ip, unsigned short server_port, const unsigned char server_ipv6[16], unsigned client_ip, unsigned short client_port,
59-
const unsigned char client_ipv6[16], bool is_ip_v6);
59+
const unsigned char client_ipv6[16]);
6060
int aes_create_udp_keys (const aes_key_t* key, aes_udp_session_key_t *R, const process_id_t *local_pid, const process_id_t *remote_pid, int generation);
6161

62-
static inline int aes_create_connection_keys(unsigned char protocol_version, aes_key_t *key, struct aes_session_key *R, int am_client, char nonce_server[16], char nonce_client[16],
63-
int server_timestamp, int client_timestamp, struct connection *c) {
62+
static inline int aes_create_connection_keys(aes_key_t *key, struct aes_session_key *R, int am_client, char nonce_server[16], char nonce_client[16],
63+
int client_timestamp, struct connection *c) {
6464
uint32_t our_ip, remote_ip;
6565
uint16_t our_port, remote_port;
6666
uint8_t remote_ipv6[16] = {'\0'};
6767
uint8_t our_ipv6[16] = {'\0'};
6868
our_ip = remote_ip = our_port = remote_port = 0;
6969

70-
bool is_ipv6 = false;
71-
72-
if (protocol_version == 0) {
73-
assert(c->local_endpoint.ss_family == c->remote_endpoint.ss_family);
74-
switch (c->local_endpoint.ss_family) {
70+
assert(c->local_endpoint.ss_family == c->remote_endpoint.ss_family);
71+
switch (c->local_endpoint.ss_family) {
7572
case AF_INET:
7673
our_ip = inet_sockaddr_address(&c->local_endpoint);
7774
remote_ip = inet_sockaddr_address(&c->remote_endpoint);
@@ -83,16 +80,15 @@ static inline int aes_create_connection_keys(unsigned char protocol_version, aes
8380
memcpy(our_ipv6, inet6_sockaddr_address(&c->local_endpoint), sizeof(our_ipv6));
8481
our_port = inet6_sockaddr_port(&c->local_endpoint);
8582
remote_port = inet6_sockaddr_port(&c->remote_endpoint);
86-
is_ipv6 = true;
8783
break;
8884
default:
8985
assert(0);
90-
}
9186
}
9287

9388
return am_client
94-
? aes_create_keys(key, R, am_client, nonce_server, nonce_client, client_timestamp, protocol_version == 0 ? remote_ip : server_timestamp, remote_port, remote_ipv6, our_ip, our_port, our_ipv6, is_ipv6)
95-
: aes_create_keys(key, R, am_client, nonce_server, nonce_client, client_timestamp, protocol_version == 0 ? our_ip : server_timestamp, our_port, our_ipv6, remote_ip, remote_port, remote_ipv6, is_ipv6);
89+
? aes_create_keys(key, R, am_client, nonce_server, nonce_client, client_timestamp, remote_ip, remote_port, remote_ipv6, our_ip, our_port, our_ipv6)
90+
91+
: aes_create_keys(key, R, am_client, nonce_server, nonce_client, client_timestamp, our_ip, our_port, our_ipv6, remote_ip, remote_port, remote_ipv6);
9692
}
9793

9894
static inline int get_crypto_key_id(const aes_key_t *key) {

net/net-memcache-client.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -611,7 +611,7 @@ int mcc_start_crypto (struct connection *c, char *key, int key_len) {
611611

612612
struct aes_session_key aes_keys;
613613

614-
if (aes_create_connection_keys (0, default_aes_key, &aes_keys, 1, nonce_in, D->nonce, 0, D->nonce_time, c) < 0) {
614+
if (aes_create_connection_keys (default_aes_key, &aes_keys, 1, nonce_in, D->nonce, D->nonce_time, c) < 0) {
615615
return -1;
616616
}
617617

net/net-memcache-server.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1029,7 +1029,7 @@ int mcs_init_crypto (struct connection *c, char *key, int key_len) {
10291029

10301030
struct aes_session_key aes_keys;
10311031

1032-
if (aes_create_connection_keys (0, default_aes_key, &aes_keys, 0, nonce_out, nonce_in, 0, utime, c) < 0) {
1032+
if (aes_create_connection_keys (default_aes_key, &aes_keys, 0, nonce_out, nonce_in, utime, c) < 0) {
10331033
return -1;
10341034
}
10351035

net/net-mysql-client.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -567,7 +567,7 @@ int sqlc_init_crypto (struct connection *c, char *key, int key_len) {
567567

568568
struct aes_session_key aes_keys;
569569

570-
if (aes_create_connection_keys (0, default_aes_key, &aes_keys, 1, nonce_in, nonce_out, 0, utime, c) < 0) {
570+
if (aes_create_connection_keys (default_aes_key, &aes_keys, 1, nonce_in, nonce_out, utime, c) < 0) {
571571
return -1;
572572
}
573573

net/net-tcp-rpc-client.cpp

Lines changed: 28 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,9 @@ int tcp_rpcc_default_execute (struct connection *c, int op, raw_message_t *raw)
7373
tvkprintf(net_connections, 3, "rpcc_execute: fd=%d, op=%d, len=%d\n", c->fd, op, raw->total_bytes);
7474
if (op == TL_RPC_PING && raw->total_bytes == 12) {
7575
c->last_response_time = precise_now;
76-
int Q[12];
76+
static int Q[12];
7777
assert (rwm_fetch_data (raw, Q, 12) == 12);
78-
int P[12];
78+
static int P[12];
7979
P[0] = TL_RPC_PONG;
8080
P[1] = Q[1];
8181
P[2] = Q[2];
@@ -90,24 +90,18 @@ int tcp_rpcc_default_execute (struct connection *c, int op, raw_message_t *raw)
9090

9191
static int tcp_rpcc_process_nonce_packet (struct connection *c, raw_message_t *msg) {
9292
struct tcp_rpc_data *D = TCP_RPC_DATA(c);
93-
struct tcp_rpc_nonce_packet P{};
93+
static struct tcp_rpc_nonce_packet P;
9494
int res;
9595

9696
if (D->packet_num != -2 || D->packet_type != RPC_NONCE) {
9797
return -2;
9898
}
99-
if (D->packet_len < sizeof (P) || D->packet_len >= 1024) {
99+
if (D->packet_len != sizeof (struct tcp_rpc_nonce_packet)) {
100100
return -3;
101101
}
102-
int excess_data_size = D->packet_len - sizeof(P); // fields from newer protocol version
103102

104-
assert (rwm_fetch_data (msg, &P, sizeof(P)) == sizeof(P));
105-
assert (rwm_fetch_data (msg, 0, excess_data_size) == excess_data_size);
106-
tvkprintf(net_connections, 4, "Processing nonce packet, crypto schema: %d, version: %d, excess_data: %d, key select: %d\n", P.crypto_schema, P.protocol_version, excess_data_size, P.key_select);
107-
108-
if (P.protocol_version > 1) { // server must not reply with version > client
109-
return -3;
110-
}
103+
assert (rwm_fetch_data (msg, &P, D->packet_len) == D->packet_len);
104+
tvkprintf(net_connections, 4, "Processing nonce packet, crypto schema: %d, key select: %d\n", P.crypto_schema, P.key_select);
111105

112106
switch (P.crypto_schema) {
113107
case RPC_CRYPTO_NONE:
@@ -133,7 +127,7 @@ static int tcp_rpcc_process_nonce_packet (struct connection *c, raw_message_t *m
133127
if (abs (P.crypto_ts - D->nonce_time) > 30) {
134128
return -6; //less'om
135129
}
136-
res = TCP_RPCC_FUNC(c)->rpc_start_crypto (c, &P);
130+
res = TCP_RPCC_FUNC(c)->rpc_start_crypto (c, P.crypto_nonce, P.key_select);
137131
if (res < 0) {
138132
return -6;
139133
}
@@ -148,13 +142,14 @@ static int tcp_rpcc_process_nonce_packet (struct connection *c, raw_message_t *m
148142
static int tcp_rpcc_send_handshake_packet (struct connection *c) {
149143
tvkprintf(net_connections, 4, "tcp_rpcc_send_handshake_packet\n");
150144
struct tcp_rpc_data *D = TCP_RPC_DATA (c);
151-
struct tcp_rpc_handshake_packet P{};
145+
static struct tcp_rpc_handshake_packet P;
152146
if (!PID.ip) {
153147
init_client_PID(c->local_endpoint.ss_family == AF_INET ? inet_sockaddr_address(&c->local_endpoint): 0);
154148
if (!PID.ip) {
155149
PID.ip = get_my_ipv4 ();
156150
}
157151
}
152+
memset (&P, 0, sizeof (P));
158153
P.type = RPC_HANDSHAKE;
159154
P.flags = default_rpc_flags & RPC_CRYPTO_USE_CRC32C;
160155
if (!D->remote_pid.port) {
@@ -172,10 +167,11 @@ static int tcp_rpcc_send_handshake_packet (struct connection *c) {
172167
}
173168

174169
static int tcp_rpcc_send_handshake_error_packet (struct connection *c, int error_code) {
175-
struct tcp_rpc_handshake_error_packet P{};
170+
static struct tcp_rpc_handshake_error_packet P;
176171
if (!PID.pid) {
177172
init_client_PID(inet_sockaddr_address(&c->local_endpoint));
178173
}
174+
memset (&P, 0, sizeof (P));
179175
P.type = RPC_HANDSHAKE_ERROR;
180176
P.error_code = error_code;
181177
memcpy (&P.sender_pid, &PID, sizeof (PID));
@@ -189,11 +185,11 @@ static int tcp_rpcc_process_handshake_packet (struct connection *c, raw_message_
189185
tvkprintf(net_connections, 4, "tcp_rpcc_process_handshake_packet\n");
190186

191187
struct tcp_rpc_data *D = TCP_RPC_DATA(c);
192-
struct tcp_rpc_handshake_packet P{};
188+
static struct tcp_rpc_handshake_packet P;
193189
if (D->packet_num != -1 || D->packet_type != RPC_HANDSHAKE) {
194190
return -2;
195191
}
196-
if (D->packet_len != sizeof (P)) {
192+
if (D->packet_len != sizeof (struct tcp_rpc_handshake_packet)) {
197193
tcp_rpcc_send_handshake_error_packet (c, -3);
198194
return -3;
199195
}
@@ -231,23 +227,17 @@ int tcp_rpcc_parse_execute (struct connection *c) {
231227
int len;
232228

233229
while (true) {
234-
len = c->in.total_bytes;
230+
len = c->in.total_bytes;
235231
if (len <= 0) {
236232
break;
237233
}
238234
if (!D->packet_len) {
239-
if (len < D->packet_v1_padding + 4) {
235+
if (len < 4) {
240236
c->status = conn_reading_answer;
241-
return D->packet_v1_padding + 4 - len;
242-
}
243-
if (D->packet_v1_padding) {
244-
assert(D->packet_v1_padding < 4);
245-
assert(rwm_fetch_data(&c->in, 0, D->packet_v1_padding) == D->packet_v1_padding);
246-
D->packet_v1_padding = 0;
237+
return 4 - len;
247238
}
248239
assert (rwm_fetch_lookup (&c->in, &D->packet_len, 4) == 4);
249-
// We skip checks for len&3 == 0 for protocol version 0, because there is little value in it.
250-
if (D->packet_len > TCP_RPCC_FUNC(c)->max_packet_len && TCP_RPCC_FUNC(c)->max_packet_len > 0) {
240+
if (D->packet_len <= 0 || (D->packet_len & 3) || (D->packet_len > TCP_RPCC_FUNC(c)->max_packet_len && TCP_RPCC_FUNC(c)->max_packet_len > 0)) {
251241
tvkprintf(net_connections, 1, "error while parsing packet: bad packet length %d\n", D->packet_len);
252242
c->status = conn_error;
253243
c->error = -1;
@@ -269,6 +259,7 @@ int tcp_rpcc_parse_execute (struct connection *c) {
269259
c->status = conn_reading_answer;
270260
return D->packet_len - len;
271261
}
262+
272263

273264
raw_message_t msg;
274265
if (c->in.total_bytes == D->packet_len) {
@@ -348,9 +339,6 @@ int tcp_rpcc_parse_execute (struct connection *c) {
348339
}
349340

350341
D->in_packet_num++;
351-
if (c->crypto) {
352-
D->packet_v1_padding = (-D->packet_len) & 3;
353-
}
354342
D->packet_len = 0;
355343
if (c->status == conn_running) {
356344
c->status = conn_wait_answer;
@@ -433,10 +421,10 @@ int tcp_rpcc_init_fake_crypto (struct connection *c) {
433421
return -1;
434422
}
435423

436-
struct tcp_rpc_nonce_packet buf{};
424+
static struct tcp_rpc_nonce_packet buf;
425+
memset (&buf, 0, sizeof (buf));
437426
buf.type = RPC_NONCE;
438427
buf.crypto_schema = RPC_CRYPTO_NONE;
439-
buf.protocol_version = 1; // ask for latest version we support
440428

441429
tcp_rpc_conn_send_data (c, sizeof (buf), &buf);
442430

@@ -489,13 +477,13 @@ int tcp_rpcc_init_crypto (struct connection *c) {
489477

490478
aes_generate_nonce (TCP_RPC_DATA(c)->nonce);
491479

492-
struct tcp_rpc_nonce_packet buf{};
480+
static struct tcp_rpc_nonce_packet buf;
481+
memset (&buf, 0, sizeof (buf));
493482
memcpy (buf.crypto_nonce, TCP_RPC_DATA(c)->nonce, 16);
494483
buf.crypto_ts = TCP_RPC_DATA(c)->nonce_time;
495484
buf.type = RPC_NONCE;
496485
buf.key_select = get_crypto_key_id (default_aes_key);
497486
buf.crypto_schema = (TCP_RPC_DATA(c)->crypto_flags & RPC_CRYPTO_ALLOW_UNENCRYPTED) ? RPC_CRYPTO_NONE_OR_AES : RPC_CRYPTO_AES;
498-
buf.protocol_version = 1; // ask for latest version we support
499487

500488
tcp_rpc_conn_send_data (c, sizeof (buf), &buf);
501489

@@ -506,9 +494,9 @@ int tcp_rpcc_init_crypto (struct connection *c) {
506494
return 1;
507495
}
508496

509-
int tcp_rpcc_start_crypto (struct connection *c, struct tcp_rpc_nonce_packet *P) {
497+
int tcp_rpcc_start_crypto (struct connection *c, char *nonce, int key_select) {
510498
struct tcp_rpc_data *D = TCP_RPC_DATA(c);
511-
tvkprintf(net_connections, 4, "rpcc_start_crypto: key_select = %d\n", P->key_select);
499+
tvkprintf(net_connections, 4, "rpcc_start_crypto: key_select = %d\n", key_select);
512500

513501
if (c->crypto) {
514502
return -1;
@@ -518,13 +506,13 @@ int tcp_rpcc_start_crypto (struct connection *c, struct tcp_rpc_nonce_packet *P)
518506
return -1;
519507
}
520508

521-
if (!P->key_select || P->key_select != get_crypto_key_id (default_aes_key)) {
509+
if (!key_select || key_select != get_crypto_key_id (default_aes_key)) {
522510
return -1;
523511
}
524512

525513
struct aes_session_key aes_keys;
526514

527-
if (aes_create_connection_keys (P->protocol_version, default_aes_key, &aes_keys, 1, P->crypto_nonce, D->nonce, P->crypto_ts, D->nonce_time, c) < 0) {
515+
if (aes_create_connection_keys (default_aes_key, &aes_keys, 1, nonce, D->nonce, D->nonce_time, c) < 0) {
528516
return -1;
529517
}
530518

@@ -541,7 +529,7 @@ void tcp_rpcc_flush_crypto (struct connection *c) {
541529
tvkprintf(net_connections, 4, "rpcc_flush_packet: padding with %d bytes\n", pad_bytes);
542530
if (pad_bytes > 0) {
543531
assert (!(pad_bytes & 3));
544-
int pad_str[3] = {4, 4, 4};
532+
static int pad_str[3] = {4, 4, 4};
545533
assert (pad_bytes <= 12);
546534
assert (rwm_push_data (&c->out, pad_str, pad_bytes) == pad_bytes);
547535
}
@@ -564,7 +552,7 @@ int tcp_rpcc_flush (struct connection *c) {
564552
tvkprintf(net_connections, 4, "rpcs_flush: padding with %d bytes\n", pad_bytes);
565553
if (pad_bytes > 0) {
566554
assert (!(pad_bytes & 3));
567-
int pad_str[3] = {4, 4, 4};
555+
static int pad_str[3] = {4, 4, 4};
568556
assert (pad_bytes <= 12);
569557
assert (rwm_push_data (&c->out, pad_str, pad_bytes) == pad_bytes);
570558
}

net/net-tcp-rpc-client.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
#include <sys/cdefs.h>
99

1010
#include "net/net-connections.h"
11-
#include "net/net-tcp-rpc-common.h"
1211
#include "net/net-msg.h"
1312

1413
struct tcp_rpc_client_functions {
@@ -18,7 +17,7 @@ struct tcp_rpc_client_functions {
1817
int (*flush_packet)(struct connection *c); /* execute this to push query to server */
1918
int (*rpc_check_perm)(struct connection *c); /* 1 = allow unencrypted, 2 = allow encrypted */
2019
int (*rpc_init_crypto)(struct connection *c); /* 1 = ok; -1 = no crypto */
21-
int (*rpc_start_crypto)(struct connection *c, struct tcp_rpc_nonce_packet *P); /* 1 = ok; -1 = no crypto */
20+
int (*rpc_start_crypto)(struct connection *c, char *nonce, int key_select); /* 1 = ok; -1 = no crypto */
2221
int (*rpc_wakeup)(struct connection *c);
2322
int (*rpc_alarm)(struct connection *c);
2423
int (*rpc_ready)(struct connection *c);
@@ -39,7 +38,7 @@ int tcp_rpcc_flush_packet_later (struct connection *c);
3938
int tcp_rpcc_default_check_perm (struct connection *c);
4039
int tcp_rpcc_default_check_perm_crypto (struct connection *c);
4140
int tcp_rpcc_init_crypto (struct connection *c);
42-
int tcp_rpcc_start_crypto (struct connection *c, struct tcp_rpc_nonce_packet *P);
41+
int tcp_rpcc_start_crypto (struct connection *c, char *nonce, int key_select);
4342
int default_tcp_rpc_client_check_ready(struct connection *c);
4443

4544
#define TCP_RPCC_FUNC(c) ((struct tcp_rpc_client_functions *) ((c)->extra))

net/net-tcp-rpc-common.cpp

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ OPTION_PARSER(OPT_RPC, "no-crc32c", no_argument, "Force use of CRC32 instead of
2727

2828
void tcp_rpc_conn_send (struct connection *c, raw_message_t *raw, int flags) {
2929
tvkprintf(net_connections, 4, "%s: sending message of size %d to conn fd=%d\n", __func__, raw->total_bytes, c->fd);
30+
assert (!(raw->total_bytes & 3));
3031
int Q[2];
3132
Q[0] = raw->total_bytes + 12;
3233
Q[1] = TCP_RPC_DATA(c)->out_packet_num ++;
@@ -39,15 +40,11 @@ void tcp_rpc_conn_send (struct connection *c, raw_message_t *raw, int flags) {
3940
rwm_push_data_front (&r, Q, 8);
4041
unsigned crc32 = rwm_custom_crc32 (&r, r.total_bytes, TCP_RPC_DATA(c)->custom_crc_partial);
4142
rwm_push_data (&r, &crc32, 4);
42-
if (c->crypto) {
43-
int packet_v1_padding = (-raw->total_bytes) & 3;
44-
char padding[3]{};
45-
rwm_push_data (&r, &padding, packet_v1_padding);
46-
}
4743
rwm_union (&c->out, &r);
4844
}
4945

5046
void tcp_rpc_conn_send_data (struct connection *c, int len, void *Q) {
47+
assert (!(len & 3));
5148
raw_message_t r;
5249
assert (rwm_create (&r, Q, len) == len);
5350
tcp_rpc_conn_send (c, &r, 0);

net/net-tcp-rpc-common.h

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,7 @@
1717
struct tcp_rpc_nonce_packet {
1818
int type;
1919
int key_select; /* least significant 32 bits of key to use */
20-
unsigned char crypto_schema; /* 0 = NONE, 1 = AES */
21-
unsigned char protocol_version;
22-
unsigned short protocol_flags;
20+
int crypto_schema; /* 0 = NONE, 1 = AES */
2321
int crypto_ts;
2422
char crypto_nonce[16];
2523
};
@@ -45,7 +43,6 @@ void tcp_rpc_conn_send_data (struct connection *c, int len, void *Q);
4543
/* in conn->custom_data */
4644
struct tcp_rpc_data {
4745
int packet_len;
48-
int packet_v1_padding;
4946
int packet_num;
5047
int packet_type;
5148
int packet_crc32;
@@ -70,8 +67,6 @@ struct tcp_rpc_data {
7067

7168
#define TCP_RPC_DATA(c) ((struct tcp_rpc_data *) ((c)->custom_data))
7269

73-
static_assert(sizeof(connection_t::custom_data) >= sizeof(tcp_rpc_data));
74-
7570
#define RPC_CRYPTO_ALLOW_UNENCRYPTED 0x00000001
7671
#define RPC_CRYPTO_ALLOW_ENCRYPTED 0x00000002
7772
#define RPC_CRYPTO_ALLOW_MASK (RPC_CRYPTO_ALLOW_UNENCRYPTED | RPC_CRYPTO_ALLOW_ENCRYPTED)

0 commit comments

Comments
 (0)