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