Merge branch 'master' of git://tinc-vpn.org/tinc into 1.1
[tinc] / src / sptps_test.c
1 /*
2     sptps_test.c -- Simple Peer-to-Peer Security test program
3     Copyright (C) 2011 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 #include "poll.h"
22
23 #include "crypto.h"
24 #include "ecdsa.h"
25 #include "sptps.h"
26 #include "utils.h"
27
28 ecdsa_t mykey, hiskey;
29
30 static bool send_data(void *handle, const char *data, size_t len) {
31         char hex[len * 2 + 1];
32         bin2hex(data, hex, len);
33         fprintf(stderr, "Sending %zu bytes of data:\n%s\n", len, hex);
34         const int *sock = handle;
35         if(send(*sock, data, len, 0) != len)
36                 return false;
37         return true;
38 }
39
40 static bool receive_record(void *handle, uint8_t type, const char *data, uint16_t len) {
41         fprintf(stderr, "Received type %d record of %hu bytes:\n", type, len);
42         fwrite(data, len, 1, stdout);
43         return true;
44 }
45
46 int main(int argc, char *argv[]) {
47         bool initiator = false;
48
49         if(argc < 3) {
50                 fprintf(stderr, "Usage: %s my_ecdsa_key_file his_ecdsa_key_file [host] port\n", argv[0]);
51                 return 1;
52         }
53
54         if(argc > 4)
55                 initiator = true;
56
57         struct addrinfo *ai, hint;
58         memset(&hint, 0, sizeof hint);
59
60         hint.ai_family = AF_UNSPEC;
61         hint.ai_socktype = SOCK_STREAM;
62         hint.ai_protocol = IPPROTO_TCP;
63         hint.ai_flags = initiator ? 0 : AI_PASSIVE;
64         
65         if(getaddrinfo(initiator ? argv[3] : NULL, initiator ? argv[4] : argv[3], &hint, &ai) || !ai) {
66                 fprintf(stderr, "getaddrinfo() failed: %s\n", strerror(errno));
67                 return 1;
68         }
69
70         int sock = socket(ai->ai_family, SOCK_STREAM, IPPROTO_TCP);
71         if(sock < 0) {
72                 fprintf(stderr, "Could not create socket: %s\n", strerror(errno));
73                 return 1;
74         }
75
76         int one = 1;
77         setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (void *)&one, sizeof one);
78
79         if(initiator) {
80                 if(connect(sock, ai->ai_addr, ai->ai_addrlen)) {
81                         fprintf(stderr, "Could not connect to peer: %s\n", strerror(errno));
82                         return 1;
83                 }
84                 fprintf(stderr, "Connected\n");
85         } else {
86                 if(bind(sock, ai->ai_addr, ai->ai_addrlen)) {
87                         fprintf(stderr, "Could not bind socket: %s\n", strerror(errno));
88                         return 1;
89                 }
90                 if(listen(sock, 1)) {
91                         fprintf(stderr, "Could not listen on socket: %s\n", strerror(errno));
92                         return 1;
93                 }
94                 fprintf(stderr, "Listening...\n");
95
96                 sock = accept(sock, NULL, NULL);
97                 if(sock < 0) {
98                         fprintf(stderr, "Could not accept connection: %s\n", strerror(errno));
99                         return 1;
100                 }
101
102                 fprintf(stderr, "Connected\n");
103         }
104
105         crypto_init();
106
107         FILE *fp = fopen(argv[1], "r");
108         if(!ecdsa_read_pem_private_key(&mykey, fp))
109                 return 1;
110         fclose(fp);
111
112         fp = fopen(argv[2], "r");
113         if(!ecdsa_read_pem_public_key(&hiskey, fp))
114                 return 1;
115         fclose(fp);
116
117         fprintf(stderr, "Keys loaded\n");
118
119         sptps_t s;
120         if(!start_sptps(&s, &sock, initiator, mykey, hiskey, "sptps_test", 10, send_data, receive_record))
121                 return 1;
122
123         while(true) {
124                 char buf[65535] = "";
125
126                 struct pollfd fds[2];
127                 fds[0].fd = 0;
128                 fds[0].events = POLLIN;
129                 fds[1].fd = sock;
130                 fds[1].events = POLLIN;
131                 if(poll(fds, 2, -1) < 0)
132                         return 1;
133
134                 if(fds[0].revents) {
135                         ssize_t len = read(0, buf, sizeof buf);
136                         if(len < 0) {
137                                 fprintf(stderr, "Could not read from stdin: %s\n", strerror(errno));
138                                 return 1;
139                         }
140                         if(len == 0)
141                                 break;
142                         if(buf[0] == '^')
143                                 send_record(&s, SPTPS_HANDSHAKE, NULL, 0);
144                         else if(buf[0] == '$')
145                                 force_kex(&s);
146                         else
147                         if(!send_record(&s, buf[0] == '!' ? 1 : 0, buf, buf[0] == '\n' ? 0 : buf[0] == '*' ? sizeof buf : len))
148                                 return 1;
149                 }
150
151                 if(fds[1].revents) {
152                         ssize_t len = recv(sock, buf, sizeof buf, 0);
153                         if(len < 0) {
154                                 fprintf(stderr, "Could not read from socket: %s\n", strerror(errno));
155                                 return 1;
156                         }
157                         if(len == 0) {
158                                 fprintf(stderr, "Connection terminated by peer.\n");
159                                 break;
160                         }
161                         char hex[len * 2 + 1];
162                         bin2hex(buf, hex, len);
163                         fprintf(stderr, "Received %zd bytes of data:\n%s\n", len, hex);
164                         if(!receive_data(&s, buf, len))
165                                 return 1;
166                 }
167         }
168
169         return 0;
170 }