Use Ed25519 keys.
[oweals/tinc.git] / src / sptps_test.c
1 /*
2     sptps_test.c -- Simple Peer-to-Peer Security test program
3     Copyright (C) 2011-2013 Guus Sliepen <guus@tinc-vpn.org>,
4
5     This program is free software; you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation; either version 2 of the License, or
8     (at your option) any later version.
9
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14
15     You should have received a copy of the GNU General Public License along
16     with this program; if not, write to the Free Software Foundation, Inc.,
17     51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #include "system.h"
21
22 #include <getopt.h>
23
24 #include "crypto.h"
25 #include "ecdsa.h"
26 #include "sptps.h"
27 #include "utils.h"
28
29 // Symbols necessary to link with logger.o
30 bool send_request(void *c, const char *msg, ...) { return false; }
31 struct list_t *connection_list = NULL;
32 bool send_meta(void *c, const char *msg , int len) { return false; }
33 char *logfilename = NULL;
34 struct timeval now;
35
36 static bool verbose;
37 static bool readonly;
38 static bool writeonly;
39
40 static bool send_data(void *handle, uint8_t type, const char *data, size_t len) {
41         char hex[len * 2 + 1];
42         bin2hex(data, hex, len);
43         if(verbose)
44                 fprintf(stderr, "Sending %d bytes of data:\n%s\n", (int)len, hex);
45         const int *sock = handle;
46         if(send(*sock, data, len, 0) != len)
47                 return false;
48         return true;
49 }
50
51 static bool receive_record(void *handle, uint8_t type, const char *data, uint16_t len) {
52         if(verbose)
53                 fprintf(stderr, "Received type %d record of %hu bytes:\n", type, len);
54         if(!writeonly)
55                 fwrite(data, len, 1, stdout);
56         return true;
57 }
58
59 static struct option const long_options[] = {
60         {"datagram", no_argument, NULL, 'd'},
61         {"quit", no_argument, NULL, 'q'},
62         {"readonly", no_argument, NULL, 'r'},
63         {"writeonly", no_argument, NULL, 'w'},
64         {"packet-loss", required_argument, NULL, 'L'},
65         {"replay-window", required_argument, NULL, 'W'},
66         {"verbose", required_argument, NULL, 'v'},
67         {"help", no_argument, NULL, 1},
68         {NULL, 0, NULL, 0}
69 };
70
71 const char *program_name;
72
73 static void usage() {
74         fprintf(stderr, "Usage: %s [options] my_ecdsa_key_file his_ecdsa_key_file [host] port\n\n", program_name);
75         fprintf(stderr, "Valid options are:\n"
76                         "  -d, --datagram          Enable datagram mode.\n"
77                         "  -q, --quit              Quit when EOF occurs on stdin.\n"
78                         "  -r, --readonly          Only send data from the socket to stdout.\n"
79                         "  -w, --writeonly         Only send data from stdin to the socket.\n"
80                         "  -L, --packet-loss RATE  Fake packet loss of RATE percent.\n"
81                         "  -R, --replay-window N   Set replay window to N bytes.\n"
82                         "  -v, --verbose           Display debug messages.\n"
83                         "\n");
84         fprintf(stderr, "Report bugs to tinc@tinc-vpn.org.\n");
85 }
86
87 int main(int argc, char *argv[]) {
88         program_name = argv[0];
89         bool initiator = false;
90         bool datagram = false;
91         int packetloss = 0;
92         int r;
93         int option_index = 0;
94         ecdsa_t *mykey = NULL, *hiskey = NULL;
95         bool quit = false;
96
97         while((r = getopt_long(argc, argv, "dqrwL:W:v", long_options, &option_index)) != EOF) {
98                 switch (r) {
99                         case 0:   /* long option */
100                                 break;
101
102                         case 'd': /* datagram mode */
103                                 datagram = true;
104                                 break;
105
106                         case 'q': /* close connection on EOF from stdin */
107                                 quit = true;
108                                 break;
109
110                         case 'r': /* read only */
111                                 readonly = true;
112                                 break;
113
114                         case 'w': /* write only */
115                                 writeonly = true;
116                                 break;
117
118                         case 'L': /* packet loss rate */
119                                 packetloss = atoi(optarg);
120                                 break;
121
122                         case 'W': /* replay window size */
123                                 sptps_replaywin = atoi(optarg);
124                                 break;
125
126                         case 'v': /* be verbose */
127                                 verbose = true;
128                                 break;
129
130                         case '?': /* wrong options */
131                                 usage();
132                                 return 1;
133
134                         case 1: /* help */
135                                 usage();
136                                 return 0;
137
138                         default:
139                                 break;
140                 }
141         }
142
143         argc -= optind - 1;
144         argv += optind - 1;
145
146         if(argc < 4 || argc > 5) {
147                 fprintf(stderr, "Wrong number of arguments.\n");
148                 usage();
149                 return 1;
150         }
151
152         if(argc > 4)
153                 initiator = true;
154
155         srand(time(NULL));
156
157 #ifdef HAVE_MINGW
158         static struct WSAData wsa_state;
159         if(WSAStartup(MAKEWORD(2, 2), &wsa_state))
160                 return 1;
161 #endif
162
163         struct addrinfo *ai, hint;
164         memset(&hint, 0, sizeof hint);
165
166         hint.ai_family = AF_UNSPEC;
167         hint.ai_socktype = datagram ? SOCK_DGRAM : SOCK_STREAM;
168         hint.ai_protocol = datagram ? IPPROTO_UDP : IPPROTO_TCP;
169         hint.ai_flags = initiator ? 0 : AI_PASSIVE;
170
171         if(getaddrinfo(initiator ? argv[3] : NULL, initiator ? argv[4] : argv[3], &hint, &ai) || !ai) {
172                 fprintf(stderr, "getaddrinfo() failed: %s\n", strerror(errno));
173                 return 1;
174         }
175
176         int sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
177         if(sock < 0) {
178                 fprintf(stderr, "Could not create socket: %s\n", strerror(errno));
179                 return 1;
180         }
181
182         int one = 1;
183         setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (void *)&one, sizeof one);
184
185         if(initiator) {
186                 if(connect(sock, ai->ai_addr, ai->ai_addrlen)) {
187                         fprintf(stderr, "Could not connect to peer: %s\n", strerror(errno));
188                         return 1;
189                 }
190                 fprintf(stderr, "Connected\n");
191         } else {
192                 if(bind(sock, ai->ai_addr, ai->ai_addrlen)) {
193                         fprintf(stderr, "Could not bind socket: %s\n", strerror(errno));
194                         return 1;
195                 }
196
197                 if(!datagram) {
198                         if(listen(sock, 1)) {
199                                 fprintf(stderr, "Could not listen on socket: %s\n", strerror(errno));
200                                 return 1;
201                         }
202                         fprintf(stderr, "Listening...\n");
203
204                         sock = accept(sock, NULL, NULL);
205                         if(sock < 0) {
206                                 fprintf(stderr, "Could not accept connection: %s\n", strerror(errno));
207                                 return 1;
208                         }
209                 } else {
210                         fprintf(stderr, "Listening...\n");
211
212                         char buf[65536];
213                         struct sockaddr addr;
214                         socklen_t addrlen = sizeof addr;
215
216                         if(recvfrom(sock, buf, sizeof buf, MSG_PEEK, &addr, &addrlen) <= 0) {
217                                 fprintf(stderr, "Could not read from socket: %s\n", strerror(errno));
218                                 return 1;
219                         }
220
221                         if(connect(sock, &addr, addrlen)) {
222                                 fprintf(stderr, "Could not accept connection: %s\n", strerror(errno));
223                                 return 1;
224                         }
225                 }
226
227                 fprintf(stderr, "Connected\n");
228         }
229
230         crypto_init();
231
232         FILE *fp = fopen(argv[1], "r");
233         if(!(mykey = ecdsa_read_pem_private_key(fp)))
234                 return 1;
235         fclose(fp);
236
237         fp = fopen(argv[2], "r");
238         if(!(hiskey = ecdsa_read_pem_public_key(fp)))
239                 return 1;
240         fclose(fp);
241
242         if(verbose)
243                 fprintf(stderr, "Keys loaded\n");
244
245         sptps_t s;
246         if(!sptps_start(&s, &sock, initiator, datagram, mykey, hiskey, "sptps_test", 10, send_data, receive_record))
247                 return 1;
248
249         while(true) {
250                 if(writeonly && readonly)
251                         break;
252
253                 char buf[65535] = "";
254
255                 fd_set fds;
256                 FD_ZERO(&fds);
257 #ifndef HAVE_MINGW
258                 if(!readonly && s.instate)
259                         FD_SET(0, &fds);
260 #endif
261                 FD_SET(sock, &fds);
262                 if(select(sock + 1, &fds, NULL, NULL, NULL) <= 0)
263                         return 1;
264
265                 if(FD_ISSET(0, &fds)) {
266                         ssize_t len = read(0, buf, sizeof buf);
267                         if(len < 0) {
268                                 fprintf(stderr, "Could not read from stdin: %s\n", strerror(errno));
269                                 return 1;
270                         }
271                         if(len == 0) {
272                                 if(quit)
273                                         break;
274                                 readonly = true;
275                                 continue;
276                         }
277                         if(buf[0] == '#')
278                                 s.outseqno = atoi(buf + 1);
279                         if(buf[0] == '^')
280                                 sptps_send_record(&s, SPTPS_HANDSHAKE, NULL, 0);
281                         else if(buf[0] == '$') {
282                                 sptps_force_kex(&s);
283                                 if(len > 1)
284                                         sptps_send_record(&s, 0, buf, len);
285                         } else
286                         if(!sptps_send_record(&s, buf[0] == '!' ? 1 : 0, buf, (len == 1 && buf[0] == '\n') ? 0 : buf[0] == '*' ? sizeof buf : len))
287                                 return 1;
288                 }
289
290                 if(FD_ISSET(sock, &fds)) {
291                         ssize_t len = recv(sock, buf, sizeof buf, 0);
292                         if(len < 0) {
293                                 fprintf(stderr, "Could not read from socket: %s\n", strerror(errno));
294                                 return 1;
295                         }
296                         if(len == 0) {
297                                 fprintf(stderr, "Connection terminated by peer.\n");
298                                 break;
299                         }
300                         if(verbose) {
301                                 char hex[len * 2 + 1];
302                                 bin2hex(buf, hex, len);
303                                 fprintf(stderr, "Received %d bytes of data:\n%s\n", (int)len, hex);
304                         }
305                         if(packetloss && (rand() % 100) < packetloss) {
306                                 if(verbose)
307                                         fprintf(stderr, "Dropped.\n");
308                                 continue;
309                         }
310                         if(!sptps_receive_data(&s, buf, len) && !datagram)
311                                 return 1;
312                 }
313         }
314
315         if(!sptps_stop(&s))
316                 return 1;
317
318         return 0;
319 }