1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
|
/*
Copyright (c) 2012-2014, Matthias Schiffer <mschiffer@universe-factory.net>
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "ec25519_fhmqvc.h"
static inline bool read_key(uint8_t key[32], const char *hexkey) {
if ((strlen(hexkey) != 64) || (strspn(hexkey, "0123456789abcdefABCDEF") != 64))
return false;
size_t i;
for (i = 0; i < 32; i++)
sscanf(&hexkey[2*i], "%02hhx", &key[i]);
return true;
}
static inline void check_session_refresh(fastd_peer_t *peer) {
protocol_session_t *session = &peer->protocol_state->session;
if (!session->refreshing && session->method->provider->session_want_refresh(session->method_state)) {
pr_verbose("refreshing session with %P", peer);
session->handshakes_cleaned = true;
session->refreshing = true;
fastd_peer_schedule_handshake(peer, 0);
}
}
static fastd_protocol_config_t* protocol_init(void) {
fastd_protocol_config_t *protocol_config = malloc(sizeof(fastd_protocol_config_t));
if (!conf.secret)
exit_error("no secret key configured");
if (!read_key(protocol_config->key.secret.p, conf.secret))
exit_error("invalid secret key");
ecc_25519_work_t work;
ecc_25519_scalarmult_base(&work, &protocol_config->key.secret);
ecc_25519_store_packed(&protocol_config->key.public.int256, &work);
return protocol_config;
}
static void protocol_peer_verify(fastd_peer_config_t *peer_conf) {
if (!peer_conf->key)
exit_error("no key configured for peer `%s'", peer_conf->name);
aligned_int256_t key;
if (!read_key(key.u8, peer_conf->key))
exit_error("invalid key configured for peer `%s'", peer_conf->name);
}
static void protocol_peer_configure(fastd_peer_config_t *peer_conf) {
if (peer_conf->protocol_config)
return;
if (!peer_conf->key) {
pr_warn("no key configured for `%s', disabling peer", peer_conf->name);
return;
}
aligned_int256_t key;
if (!read_key(key.u8, peer_conf->key)) {
pr_warn("invalid key configured for `%s', disabling peer", peer_conf->name);
return;
}
peer_conf->protocol_config = malloc(sizeof(fastd_protocol_peer_config_t));
peer_conf->protocol_config->public_key = key;
if (memcmp(&peer_conf->protocol_config->public_key, &conf.protocol_config->key.public, PUBLICKEYBYTES) == 0)
pr_debug("found own key as `%s', ignoring peer", peer_conf->name);
}
static inline bool check_session(fastd_peer_t *peer) {
if (is_session_valid(&peer->protocol_state->session))
return true;
pr_verbose("active session with %P timed out", peer);
fastd_peer_reset(peer);
return false;
}
static void protocol_handle_recv(fastd_peer_t *peer, fastd_buffer_t buffer) {
if (!peer->protocol_state || !check_session(peer))
goto fail;
fastd_buffer_t recv_buffer;
bool ok = false;
if (is_session_valid(&peer->protocol_state->old_session)) {
if (peer->protocol_state->old_session.method->provider->decrypt(peer, peer->protocol_state->old_session.method_state, &recv_buffer, buffer))
ok = true;
}
if (!ok) {
if (peer->protocol_state->session.method->provider->decrypt(peer, peer->protocol_state->session.method_state, &recv_buffer, buffer)) {
ok = true;
if (peer->protocol_state->old_session.method) {
pr_debug("invalidating old session with %P", peer);
peer->protocol_state->old_session.method->provider->session_free(peer->protocol_state->old_session.method_state);
peer->protocol_state->old_session = (protocol_session_t){};
}
if (!peer->protocol_state->session.handshakes_cleaned) {
pr_debug("cleaning left handshakes with %P", peer);
fastd_peer_unschedule_handshake(peer);
peer->protocol_state->session.handshakes_cleaned = true;
if (peer->protocol_state->session.method->provider->session_is_initiator(peer->protocol_state->session.method_state))
fastd_protocol_ec25519_fhmqvc_send_empty(peer, &peer->protocol_state->session);
}
check_session_refresh(peer);
}
}
if (!ok) {
pr_verbose("verification failed for packet received from %P", peer);
goto fail;
}
fastd_peer_seen(peer);
if (recv_buffer.len)
fastd_handle_receive(peer, recv_buffer);
else
fastd_buffer_free(recv_buffer);
return;
fail:
fastd_buffer_free(buffer);
}
static void session_send(fastd_peer_t *peer, fastd_buffer_t buffer, protocol_session_t *session) {
size_t stat_size = buffer.len;
fastd_buffer_t send_buffer;
if (!session->method->provider->encrypt(peer, session->method_state, &send_buffer, buffer)) {
fastd_buffer_free(buffer);
pr_error("failed to encrypt packet for %P", peer);
return;
}
fastd_send(peer->sock, &peer->local_address, &peer->address, peer, send_buffer, stat_size);
peer->keepalive_timeout = fastd_in_seconds(conf.keepalive_timeout);
}
static void protocol_send(fastd_peer_t *peer, fastd_buffer_t buffer) {
if (!peer->protocol_state || !fastd_peer_is_established(peer) || !check_session(peer)) {
fastd_buffer_free(buffer);
return;
}
check_session_refresh(peer);
if (peer->protocol_state->session.method->provider->session_is_initiator(peer->protocol_state->session.method_state) && is_session_valid(&peer->protocol_state->old_session)) {
pr_debug2("sending packet for old session to %P", peer);
session_send(peer, buffer, &peer->protocol_state->old_session);
}
else {
session_send(peer, buffer, &peer->protocol_state->session);
}
}
void fastd_protocol_ec25519_fhmqvc_send_empty(fastd_peer_t *peer, protocol_session_t *session) {
session_send(peer, fastd_buffer_alloc(0, alignto(session->method->provider->min_encrypt_head_space, 8), session->method->provider->min_encrypt_tail_space), session);
}
const fastd_protocol_t fastd_protocol_ec25519_fhmqvc = {
.name = "ec25519-fhmqvc",
.init = protocol_init,
.peer_verify = protocol_peer_verify,
.peer_configure = protocol_peer_configure,
.peer_check = fastd_protocol_ec25519_fhmqvc_peer_check,
.peer_check_temporary = fastd_protocol_ec25519_fhmqvc_peer_check_temporary,
.handshake_init = fastd_protocol_ec25519_fhmqvc_handshake_init,
.handshake_handle = fastd_protocol_ec25519_fhmqvc_handshake_handle,
.handle_verify_return = fastd_protocol_ec25519_fhmqvc_handle_verify_return,
.handle_recv = protocol_handle_recv,
.send = protocol_send,
.init_peer_state = fastd_protocol_ec25519_fhmqvc_init_peer_state,
.reset_peer_state = fastd_protocol_ec25519_fhmqvc_reset_peer_state,
.free_peer_state = fastd_protocol_ec25519_fhmqvc_free_peer_state,
.generate_key = fastd_protocol_ec25519_fhmqvc_generate_key,
.show_key = fastd_protocol_ec25519_fhmqvc_show_key,
.set_shell_env = fastd_protocol_ec25519_fhmqvc_set_shell_env,
.describe_peer = fastd_protocol_ec25519_fhmqvc_describe_peer,
};
|