18
18
#include <algorithm> // std::max
19
19
#include <cstdlib> // exit()
20
#include <fstream> // std::ifstream
21
#include <string> // std::string
22
#include <map> // std::map
23
#include <iostream> // cout
24
#include <ostream> // <<
21
26
#define SOCKET_ERR(err,s) if(err<0) {perror(s);exit(1);}
27
32
#define CRLFILE "crl.pem"
28
33
#define DH_BITS 1024
30
40
/* These are global */
31
41
gnutls_certificate_credentials_t x509_cred;
42
map<string,string> table;
33
44
static gnutls_dh_params_t dh_params;
101
void tcpreply(int sd, struct sockaddr_in6 sa_cli, gnutls_session_t session){
112
void tcpreply(int sd, struct sockaddr_in6 *sa_cli, gnutls_session_t session){
103
115
unsigned int status;
104
116
char buffer[512];
106
printf ("- connection from %s, port %d\n",
107
inet_ntop (AF_INET6, &sa_cli.sin6_addr, buffer,
108
sizeof (buffer)), ntohs (sa_cli.sin6_port));
120
#define DIE(s){ exit_status = s; goto tcpreply_die; }
122
printf ("- TCP connection from %s, port %d\n",
123
inet_ntop (AF_INET6, &(sa_cli->sin6_addr), buffer,
124
sizeof (buffer)), ntohs (sa_cli->sin6_port));
111
127
gnutls_transport_set_ptr (session, reinterpret_cast<gnutls_transport_ptr_t> (sd));
118
134
gnutls_deinit (session);
119
135
fprintf (stderr, "*** Handshake has failed (%s)\n\n",
120
136
gnutls_strerror (ret));
123
139
printf ("- Handshake was completed\n");
125
141
//time to validate
127
ret = gnutls_certificate_verify_peers2 (session, &status);
131
printf ("Verify failed\n");
135
if (status & GNUTLS_CERT_INVALID)
136
printf ("The certificate is not trusted.\n");
138
if (status & GNUTLS_CERT_SIGNER_NOT_FOUND)
139
printf ("The certificate hasn't got a known issuer.\n");
141
if (status & GNUTLS_CERT_REVOKED)
142
printf ("The certificate has been revoked.\n");
144
143
if (gnutls_certificate_type_get (session) != GNUTLS_CRT_X509){
145
144
printf("Recived certificate not X.509\n");
149
148
const gnutls_datum_t *cert_list;
150
149
unsigned int cert_list_size = 0;
151
150
gnutls_x509_crt_t cert;
155
153
cert_list = gnutls_certificate_get_peers (session, &cert_list_size);
157
155
printf ("Peer provided %d certificates.\n", cert_list_size);
159
157
if (cert_list_size == 0){
160
printf("No certificates recived\n"); //should never happen because verify_peers2 should fail if so
158
printf("No certificates recived\n");
164
162
gnutls_x509_crt_init (&cert);
172
170
printf ("DN: %s\n", dn);
173
ret = gnutls_certificate_verify_peers2 (session, &status);
176
printf ("Verify failed\n");
175
ret = gnutls_record_recv (session, buffer, sizeof(buffer));
180
if (status & (GNUTLS_CERT_INVALID | GNUTLS_CERT_SIGNER_NOT_FOUND | GNUTLS_CERT_REVOKED)) {
181
if (status & GNUTLS_CERT_INVALID) {
182
printf ("The certificate is not trusted.\n");
185
if (status & GNUTLS_CERT_SIGNER_NOT_FOUND){
186
printf ("The certificate hasn't got a known issuer.\n");
189
if (status & GNUTLS_CERT_REVOKED){
190
printf ("The certificate has been revoked.\n");
179
write(1, buffer, ret);
195
if (table.find(dn) != table.end()){
196
gnutls_record_send (session, table[dn].c_str(), table[dn].size());
197
printf("Password sent to client\n");
182
fprintf (stderr, "\n*** Received corrupted "
183
"data(%d). Closing the connection.\n\n", ret);
200
printf("dn not in list of allowed clients\n");
186
205
gnutls_bye (session, GNUTLS_SHUT_WR);
188
207
gnutls_deinit (session);
189
208
gnutls_certificate_free_credentials (x509_cred);
190
209
gnutls_global_deinit ();
214
void badconfigparser(string file){
219
ifstream infile (file.c_str());
222
getline(infile, dn, '\n');
226
getline(infile, pw, '\n');
230
getline(infile, pwfile, '\n');
235
ifstream pwf(pwfile.c_str());
243
pw = pw + tmp + '\n';
204
264
fd_set rfds_orig;
266
badconfigparser(string("clients.conf"));
206
268
session = initialize_tls_session ();
208
//UDP socket creation
270
//UDP IPv6 socket creation
209
271
udp_listen_sd = socket (PF_INET6, SOCK_DGRAM, 0);
210
272
SOCKET_ERR (udp_listen_sd, "socket");
214
276
sa_serv.sin6_addr = in6addr_any; //XXX only listen to link local?
215
277
sa_serv.sin6_port = htons (PORT); /* Server Port number */
217
ret = setsockopt (udp_listen_sd, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof (int));
279
ret = setsockopt (udp_listen_sd, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof (optval));
218
280
SOCKET_ERR(ret,"setsockopt reuseaddr");
220
282
ret = setsockopt(udp_listen_sd, SOL_SOCKET, SO_BINDTODEVICE, "eth0", 5);
233
295
//UDP socket creation done
236
//TCP socket creation
298
//TCP IPv6 socket creation
238
300
tcp_listen_sd = socket(PF_INET6, SOCK_STREAM, 0);
239
301
SOCKET_ERR(tcp_listen_sd,"socket");
241
303
setsockopt(tcp_listen_sd, SOL_SOCKET, SO_BINDTODEVICE, "eth0", 5);
242
304
SOCKET_ERR(ret,"setsockopt bindtodevice");
244
ret = setsockopt (tcp_listen_sd, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof (int));
306
ret = setsockopt (tcp_listen_sd, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof (optval));
245
307
SOCKET_ERR(ret,"setsockopt reuseaddr");
247
309
err = bind (tcp_listen_sd, reinterpret_cast<const sockaddr *> (& sa_serv),
251
313
err = listen (tcp_listen_sd, 1024);
252
314
SOCKET_ERR (err, "listen");
254
//TCP sockets creation done
316
//TCP IPv6 sockets creation done
256
318
FD_ZERO(&rfds_orig);
257
319
FD_SET(udp_listen_sd, &rfds_orig);
272
334
if (FD_ISSET(tcp_listen_sd, &rfds)){
274
335
client_len = sizeof(sa_cli);
276
336
int sd = accept (tcp_listen_sd,
277
337
reinterpret_cast<struct sockaddr *> (& sa_cli),
279
339
SOCKET_ERR(sd,"accept"); //xxx not dieing when just connection abort
282
tcpreply(sd, sa_cli, session);
342
tcpreply(sd, &sa_cli, session);