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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
|
extern "C" {
#include <sys/types.h> //socket, setsockopt, bind, listen, accept,
// inet_ntop,
#include <sys/socket.h> //socket, setsockopt, bind, listen, accept,
// inet_ntop
#include <sys/ioctl.h> //ioctl, sockaddr_ll, ifreq
#include <unistd.h> //write, close
#include <netinet/ip.h> // sockaddr_in
#include <gnutls/gnutls.h>
#include <gnutls/x509.h> // gnutls_x509_crt_init, gnutls_x509_crt_import, gnutls_x509_crt_get_dn
#include <arpa/inet.h> // inet_ntop, htons
#include <net/if.h> //ifreq
}
#include <cstdio>
#include <cstring>
#include <cerrno>
#include <algorithm> // std::max
#include <cstdlib> // exit()
#define SOCKET_ERR(err,s) if(err<0) {perror(s);exit(1);}
#define PORT 49001
#define KEYFILE "key.pem"
#define CERTFILE "cert.pem"
#define CAFILE "ca.pem"
#define CRLFILE "crl.pem"
#define DH_BITS 1024
/* These are global */
gnutls_certificate_credentials_t x509_cred;
static gnutls_dh_params_t dh_params;
static int
generate_dh_params ()
{
/* Generate Diffie Hellman parameters - for use with DHE
* kx algorithms. These should be discarded and regenerated
* once a day, once a week or once a month. Depending on the
* security requirements.
*/
gnutls_dh_params_init (&dh_params);
gnutls_dh_params_generate2 (dh_params, DH_BITS);
return 0;
}
gnutls_session_t
initialize_tls_session ()
{
gnutls_session_t session;
gnutls_global_init ();
gnutls_certificate_allocate_credentials (&x509_cred);
gnutls_certificate_set_x509_trust_file (x509_cred, CAFILE,
GNUTLS_X509_FMT_PEM);
gnutls_certificate_set_x509_crl_file (x509_cred, CRLFILE,
GNUTLS_X509_FMT_PEM);
gnutls_certificate_set_x509_key_file (x509_cred, CERTFILE, KEYFILE,
GNUTLS_X509_FMT_PEM);
generate_dh_params ();
gnutls_certificate_set_dh_params (x509_cred, dh_params);
gnutls_init (&session, GNUTLS_SERVER);
gnutls_set_default_priority (session);
gnutls_credentials_set (session, GNUTLS_CRD_CERTIFICATE, x509_cred);
// request client certificate if any.
gnutls_certificate_server_set_request (session, GNUTLS_CERT_REQUEST);
gnutls_dh_set_prime_bits (session, DH_BITS);
return session;
}
void udpreply(int &sd){
struct sockaddr_in6 sa_cli;
int ret;
char buffer[512];
{
socklen_t sa_cli_len = sizeof(sa_cli);
ret = recvfrom(sd, buffer, 512,0,
reinterpret_cast<sockaddr *>(& sa_cli), & sa_cli_len);
SOCKET_ERR (ret, "recvfrom");
}
if (strncmp(buffer,"Marco", 5) == 0){
ret = sendto(sd, "Polo", 4, 0, reinterpret_cast<sockaddr *>(& sa_cli),
sizeof(sa_cli));
SOCKET_ERR (ret, "sendto");
}
}
void tcpreply(int sd, struct sockaddr_in6 sa_cli, gnutls_session_t session){
int ret;
unsigned int status;
char buffer[512];
printf ("- connection from %s, port %d\n",
inet_ntop (AF_INET6, &sa_cli.sin6_addr, buffer,
sizeof (buffer)), ntohs (sa_cli.sin6_port));
gnutls_transport_set_ptr (session, reinterpret_cast<gnutls_transport_ptr_t> (sd));
ret = gnutls_handshake (session);
if (ret < 0)
{
close (sd);
gnutls_deinit (session);
fprintf (stderr, "*** Handshake has failed (%s)\n\n",
gnutls_strerror (ret));
exit(1);
}
printf ("- Handshake was completed\n");
//time to validate
ret = gnutls_certificate_verify_peers2 (session, &status);
if (ret < 0)
{
printf ("Verify failed\n");
exit(1);
}
if (status & GNUTLS_CERT_INVALID)
printf ("The certificate is not trusted.\n");
if (status & GNUTLS_CERT_SIGNER_NOT_FOUND)
printf ("The certificate hasn't got a known issuer.\n");
if (status & GNUTLS_CERT_REVOKED)
printf ("The certificate has been revoked.\n");
if (gnutls_certificate_type_get (session) != GNUTLS_CRT_X509){
printf("Recived certificate not X.509\n");
exit(1);
}
{
const gnutls_datum_t *cert_list;
unsigned int cert_list_size = 0;
gnutls_x509_crt_t cert;
size_t size;
char dn[128];
cert_list = gnutls_certificate_get_peers (session, &cert_list_size);
printf ("Peer provided %d certificates.\n", cert_list_size);
if (cert_list_size == 0){
printf("No certificates recived\n"); //should never happen because verify_peers2 should fail if so
exit(1);
}
gnutls_x509_crt_init (&cert);
// XXX -Checking only first cert, might want to check them all
gnutls_x509_crt_import (cert, &cert_list[0], GNUTLS_X509_FMT_DER);
size = sizeof (dn);
gnutls_x509_crt_get_dn (cert, dn, &size);
printf ("DN: %s\n", dn);
}
ret = gnutls_record_recv (session, buffer, sizeof(buffer));
if (ret > 0)
{
write(1, buffer, ret);
}
else {
fprintf (stderr, "\n*** Received corrupted "
"data(%d). Closing the connection.\n\n", ret);
}
gnutls_bye (session, GNUTLS_SHUT_WR);
close(sd);
gnutls_deinit (session);
gnutls_certificate_free_credentials (x509_cred);
gnutls_global_deinit ();
}
int main (){
int ret, err, udp_listen_sd, tcp_listen_sd;
struct sockaddr_in6 sa_serv;
struct sockaddr_in6 sa_cli;
int optval = 1;
socklen_t client_len;
gnutls_session_t session;
fd_set rfds_orig;
session = initialize_tls_session ();
//UDP socket creation
udp_listen_sd = socket (PF_INET6, SOCK_DGRAM, 0);
SOCKET_ERR (udp_listen_sd, "socket");
memset (&sa_serv, '\0', sizeof (sa_serv));
sa_serv.sin6_family = AF_INET6;
sa_serv.sin6_addr = in6addr_any; //XXX only listen to link local?
sa_serv.sin6_port = htons (PORT); /* Server Port number */
ret = setsockopt (udp_listen_sd, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof (int));
SOCKET_ERR(ret,"setsockopt reuseaddr");
ret = setsockopt(udp_listen_sd, SOL_SOCKET, SO_BINDTODEVICE, "eth0", 5);
SOCKET_ERR(ret,"setsockopt bindtodevice");
{
int flag = 1;
ret = setsockopt(udp_listen_sd, SOL_SOCKET, SO_BROADCAST, & flag, sizeof(flag));
SOCKET_ERR(ret,"setsockopt broadcast");
}
err = bind (udp_listen_sd, reinterpret_cast<const sockaddr *> (& sa_serv),
sizeof (sa_serv));
SOCKET_ERR (err, "bind");
//UDP socket creation done
//TCP socket creation
tcp_listen_sd = socket(PF_INET6, SOCK_STREAM, 0);
SOCKET_ERR(tcp_listen_sd,"socket");
setsockopt(tcp_listen_sd, SOL_SOCKET, SO_BINDTODEVICE, "eth0", 5);
SOCKET_ERR(ret,"setsockopt bindtodevice");
ret = setsockopt (tcp_listen_sd, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof (int));
SOCKET_ERR(ret,"setsockopt reuseaddr");
err = bind (tcp_listen_sd, reinterpret_cast<const sockaddr *> (& sa_serv),
sizeof (sa_serv));
SOCKET_ERR (err, "bind");
err = listen (tcp_listen_sd, 1024);
SOCKET_ERR (err, "listen");
//TCP sockets creation done
FD_ZERO(&rfds_orig);
FD_SET(udp_listen_sd, &rfds_orig);
FD_SET(tcp_listen_sd, &rfds_orig);
printf ("Server ready. Listening to port '%d' on UDP and TCP.\n\n", PORT);
for(;;){
fd_set rfds = rfds_orig;
ret = select(std::max(udp_listen_sd, tcp_listen_sd)+1, &rfds, 0, 0, 0);
SOCKET_ERR(ret,"select");
if (FD_ISSET(udp_listen_sd, &rfds)){
udpreply(udp_listen_sd);
}
if (FD_ISSET(tcp_listen_sd, &rfds)){
client_len = sizeof(sa_cli);
int sd = accept (tcp_listen_sd,
reinterpret_cast<struct sockaddr *> (& sa_cli),
&client_len);
SOCKET_ERR(sd,"accept"); //xxx not dieing when just connection abort
switch(fork()){
case 0:
tcpreply(sd, sa_cli, session);
return 0;
break;
case -1:
perror("fork");
close(tcp_listen_sd);
close(udp_listen_sd);
return 1;
break;
default:
break;
}
}
}
close(tcp_listen_sd);
close(udp_listen_sd);
return 0;
}
|