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> // <<
26
21
#define SOCKET_ERR(err,s) if(err<0) {perror(s);exit(1);}
32
27
#define CRLFILE "crl.pem"
33
28
#define DH_BITS 1024
40
30
/* These are global */
41
31
gnutls_certificate_credentials_t x509_cred;
42
map<string,string> table;
44
33
static gnutls_dh_params_t dh_params;
112
void tcpreply(int sd, struct sockaddr_in6 *sa_cli, gnutls_session_t session){
101
void tcpreply(int sd, struct sockaddr_in6 sa_cli, gnutls_session_t session){
115
103
unsigned int status;
116
104
char buffer[512];
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));
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));
127
111
gnutls_transport_set_ptr (session, reinterpret_cast<gnutls_transport_ptr_t> (sd));
134
118
gnutls_deinit (session);
135
119
fprintf (stderr, "*** Handshake has failed (%s)\n\n",
136
120
gnutls_strerror (ret));
139
123
printf ("- Handshake was completed\n");
141
125
//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");
143
144
if (gnutls_certificate_type_get (session) != GNUTLS_CRT_X509){
144
145
printf("Recived certificate not X.509\n");
148
149
const gnutls_datum_t *cert_list;
149
150
unsigned int cert_list_size = 0;
150
151
gnutls_x509_crt_t cert;
153
155
cert_list = gnutls_certificate_get_peers (session, &cert_list_size);
155
157
printf ("Peer provided %d certificates.\n", cert_list_size);
157
159
if (cert_list_size == 0){
158
printf("No certificates recived\n");
160
printf("No certificates recived\n"); //should never happen because verify_peers2 should fail if so
162
164
gnutls_x509_crt_init (&cert);
170
172
printf ("DN: %s\n", dn);
173
ret = gnutls_certificate_verify_peers2 (session, &status);
176
printf ("Verify failed\n");
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");
175
ret = gnutls_record_recv (session, buffer, sizeof(buffer));
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");
179
write(1, buffer, ret);
200
printf("dn not in list of allowed clients\n");
182
fprintf (stderr, "\n*** Received corrupted "
183
"data(%d). Closing the connection.\n\n", ret);
205
186
gnutls_bye (session, GNUTLS_SHUT_WR);
207
188
gnutls_deinit (session);
208
189
gnutls_certificate_free_credentials (x509_cred);
209
190
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';
264
204
fd_set rfds_orig;
266
badconfigparser(string("clients.conf"));
268
206
session = initialize_tls_session ();
270
//UDP IPv6 socket creation
208
//UDP socket creation
271
209
udp_listen_sd = socket (PF_INET6, SOCK_DGRAM, 0);
272
210
SOCKET_ERR (udp_listen_sd, "socket");
276
214
sa_serv.sin6_addr = in6addr_any; //XXX only listen to link local?
277
215
sa_serv.sin6_port = htons (PORT); /* Server Port number */
279
ret = setsockopt (udp_listen_sd, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof (optval));
217
ret = setsockopt (udp_listen_sd, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof (int));
280
218
SOCKET_ERR(ret,"setsockopt reuseaddr");
282
220
ret = setsockopt(udp_listen_sd, SOL_SOCKET, SO_BINDTODEVICE, "eth0", 5);
295
233
//UDP socket creation done
298
//TCP IPv6 socket creation
236
//TCP socket creation
300
238
tcp_listen_sd = socket(PF_INET6, SOCK_STREAM, 0);
301
239
SOCKET_ERR(tcp_listen_sd,"socket");
303
241
setsockopt(tcp_listen_sd, SOL_SOCKET, SO_BINDTODEVICE, "eth0", 5);
304
242
SOCKET_ERR(ret,"setsockopt bindtodevice");
306
ret = setsockopt (tcp_listen_sd, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof (optval));
244
ret = setsockopt (tcp_listen_sd, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof (int));
307
245
SOCKET_ERR(ret,"setsockopt reuseaddr");
309
247
err = bind (tcp_listen_sd, reinterpret_cast<const sockaddr *> (& sa_serv),
313
251
err = listen (tcp_listen_sd, 1024);
314
252
SOCKET_ERR (err, "listen");
316
//TCP IPv6 sockets creation done
254
//TCP sockets creation done
318
256
FD_ZERO(&rfds_orig);
319
257
FD_SET(udp_listen_sd, &rfds_orig);
334
272
if (FD_ISSET(tcp_listen_sd, &rfds)){
335
274
client_len = sizeof(sa_cli);
336
276
int sd = accept (tcp_listen_sd,
337
277
reinterpret_cast<struct sockaddr *> (& sa_cli),
339
279
SOCKET_ERR(sd,"accept"); //xxx not dieing when just connection abort
342
tcpreply(sd, &sa_cli, session);
282
tcpreply(sd, sa_cli, session);