4 Copyright (C) 2003-2004 Guus Sliepen <guus@tinc-vpn.org>,
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 #include <gnutls/gnutls.h>
27 #include "support/avl.h"
28 #include "support/sockaddr.h"
29 #include "support/xalloc.h"
32 static avl_tree_t *tnls, *listeners;
35 tnls = avl_tree_new(NULL, (avl_action_t)free);
36 listeners = avl_tree_new(NULL, (avl_action_t)free);
42 avl_tree_del(listeners);
48 #define tnl_add(t) avl_add(tnls, t)
49 #define tnl_del(t) avl_del(tnls, t)
50 #define tnl_listen_add(l) avl_add(listeners, l)
51 #define tnl_listen_del(l) avl_del(listeners, l)
53 static bool tnl_send(tnl_t *tnl, const char *buf, int len) {
57 result = gnutls_record_send(tnl->session, buf, len);
59 if(result == GNUTLS_E_INTERRUPTED || result == GNUTLS_E_AGAIN)
63 logger(LOG_ERR, _("tnl: error while sending: %s"), gnutls_strerror(result));
65 logger(LOG_INFO, _("tnl: connection closed by peer"));
67 tnl->error(tnl, result);
79 static bool tnl_recv(tnl_t *tnl) {
81 tnl_record_t *record = (tnl_record_t *)tnl->buf;
83 result = gnutls_record_recv(tnl->session, tnl->buf + tnl->bufread, sizeof tnl->buf - tnl->bufread);
85 if(result == GNUTLS_E_INTERRUPTED || result == GNUTLS_E_AGAIN)
89 logger(LOG_ERR, _("tnl: error while receiving: %s"), gnutls_strerror(result));
91 logger(LOG_INFO, _("tnl: connection closed by peer"));
93 tnl->error(tnl, result);
98 tnl->bufread += result;
100 while(tnl->bufread >= sizeof *record && tnl->bufread - sizeof *record >= record->len) {
101 switch(record->type) {
102 case TNL_RECORD_META:
103 tnl->recv_meta(tnl, record->data, record->len);
106 case TNL_RECORD_PACKET:
107 tnl->recv_packet(tnl, record->data, record->len);
111 logger(LOG_ERR, _("tnl: error while receiving: %s"), _("unknown record type"));
112 tnl->error(tnl, EINVAL);
117 tnl->bufread -= sizeof *record + record->len;
118 memmove(tnl->buf, record->data + record->len, tnl->bufread);
122 static bool tnl_recv_handler(fd_t *fd) {
123 tnl_t *tnl = fd->data;
126 result = gnutls_record_recv(tnl->session, tnl->buf + tnl->bufread, sizeof(tnl->buf) - tnl->bufread);
128 if(gnutls_error_is_fatal(result)) {
129 logger(LOG_DEBUG, _("tnl: reception failed: %s\n"), gnutls_strerror(result));
130 tnl->error(tnl, result);
138 tnl->bufread += result;
139 return tnl_recv(tnl);
142 static bool tnl_authenticate(tnl_t *tnl) {
143 gnutls_x509_crt cert;
144 const gnutls_datum *certs;
145 int ncerts = 0, result;
146 char buf[1024], *name, *p;
149 certs = gnutls_certificate_get_peers(tnl->session, &ncerts);
151 if (!certs || !ncerts) {
152 logger(LOG_ERR, _("tnl: no certificates from %s"), tnl->remote.hostname);
157 gnutls_x509_crt_init(&cert);
158 result = gnutls_x509_crt_import(cert, certs, GNUTLS_X509_FMT_DER) ?: gnutls_x509_crt_get_dn(cert, buf, &len);
161 logger(LOG_ERR, _("tnl: error importing certificate from %s: %s"), tnl->remote.hostname, gnutls_strerror(errno));
162 gnutls_x509_crt_deinit(cert);
166 name = strstr(buf, "CN=");
168 logger(LOG_ERR, _("tnl: no name in certificate from %s"), tnl->remote.hostname);
169 gnutls_x509_crt_deinit(cert);
174 for(p = name; *p && *p != ','; p++);
177 if(tnl->remote.id && strcmp(tnl->remote.id, name)) {
178 logger(LOG_ERR, _("tnl: peer %s is %s instead of %s"), tnl->remote.hostname, name, tnl->remote.id);
182 replace(tnl->remote.id, name);
184 result = gnutls_certificate_verify_peers(tnl->session);
187 logger(LOG_ERR, "tnl: error verifying certificate from %s (%s): %s\n", tnl->remote.id, tnl->remote.hostname, gnutls_strerror(result));
192 logger(LOG_ERR, "tnl: certificate from %s (%s) not good, verification result %x", tnl->remote.id, tnl->remote.hostname, result);
199 static bool tnl_handshake_handler(fd_t *fd) {
201 tnl_t *tnl = fd->data;
204 result = gnutls_handshake(tnl->session);
206 if(gnutls_error_is_fatal(result)) {
207 logger(LOG_ERR, "tnl: handshake error: %s\n", gnutls_strerror(result));
212 /* check other stuff? */
216 logger(LOG_DEBUG, _("tnl: handshake finished"));
218 if(!tnl_authenticate(tnl))
221 tnl->status == TNL_STATUS_UP;
222 tnl->fd.read = tnl_recv_handler;
227 static bool tnl_send_meta(tnl_t *tnl, const char *buf, int len) {
228 tnl_record_t record = {
229 .type = TNL_RECORD_META,
233 return tnl_send(tnl, (char *)&record, sizeof(record)) && tnl_send(tnl, buf, len);
236 static bool tnl_send_packet(tnl_t *tnl, const char *buf, int len) {
237 tnl_record_t record = {
238 .type = TNL_RECORD_PACKET,
242 return tnl_send(tnl, (char *)&record, sizeof(record)) && tnl_send(tnl, buf, len);
245 static bool tnl_close(tnl_t *tnl) {
247 gnutls_bye(tnl->session, GNUTLS_SHUT_RDWR);
248 gnutls_deinit(tnl->session);
259 static bool tnl_accept_error(tnl_t *tnl, int errnum) {
260 logger(LOG_ERR, _("tnl: error %d on accepted tunnel"));
264 static bool tnl_accept_handler(fd_t *fd) {
265 tnl_listen_t *listener = fd->data;
267 struct sockaddr_storage ss;
268 socklen_t len = sizeof ss;
271 sock = accept(fd->fd, sa(&ss), &len);
274 logger(LOG_ERR, _("tnl: could not accept incoming connection: %s"), strerror(errno));
278 logger(LOG_DEBUG, _("tnl: accepted incoming connection"));
283 tnl->local = listener->local;
284 tnl->remote.address = ss;
285 len = sizeof tnl->local.address;
286 getsockname(sock, sa(&tnl->local.address), &len);
287 sa_unmap(&tnl->local.address);
288 tnl->type = listener->type;
289 tnl->protocol = listener->protocol;
290 tnl->status = TNL_STATUS_CONNECTING;
291 tnl->error = tnl_accept_error;
292 tnl->close = tnl_close;
295 tnl->fd.read = tnl_handshake_handler;
298 fcntl(sock, F_SETFL, fcntl(sock, F_GETFL) | O_NONBLOCK);
302 gnutls_init(&tnl->session, GNUTLS_SERVER);
303 //gnutls_handshake_set_private_extensions(tnl->session, 1);
304 gnutls_set_default_priority(tnl->session);
305 gnutls_credentials_set(tnl->session, GNUTLS_CRD_CERTIFICATE, tnl->local.cred);
306 gnutls_certificate_server_set_request(tnl->session, GNUTLS_CERT_REQUEST);
307 gnutls_transport_set_ptr(tnl->session, (gnutls_transport_ptr)sock);
308 gnutls_handshake(tnl->session);
310 tnl->accept = listener->accept;
317 static bool tnl_connect_handler(fd_t *fd) {
318 tnl_t *tnl = fd->data;
323 getsockopt(fd->fd, SOL_SOCKET, SO_ERROR, &result, &len);
325 logger(LOG_ERR, "tnl: error while connecting: %s", strerror(result));
326 tnl->error(tnl, result);
331 fcntl(tnl->fd.fd, F_SETFL, fcntl(tnl->fd.fd, F_GETFL) | O_NONBLOCK);
333 tnl->status = TNL_STATUS_HANDSHAKE;
334 gnutls_init(&tnl->session, GNUTLS_CLIENT);
335 //gnutls_handshake_set_private_extensions(tnl->session, 1);
336 gnutls_set_default_priority(tnl->session);
337 gnutls_credentials_set(tnl->session, GNUTLS_CRD_CERTIFICATE, tnl->local.cred);
338 gnutls_certificate_server_set_request(tnl->session, GNUTLS_CERT_REQUEST);
339 gnutls_transport_set_ptr(tnl->session, (gnutls_transport_ptr)fd->fd);
340 gnutls_handshake(tnl->session);
342 tnl->fd.write = NULL;
343 tnl->fd.read = tnl_handshake_handler;
346 logger(LOG_DEBUG, _("tnl: connected"));
351 bool tnl_connect(tnl_t *tnl) {
354 sock = socket(sa_family(&tnl->remote.address), tnl->type, tnl->protocol);
357 logger(LOG_ERR, _("tnl: could not create socket: %s"), strerror(errno));
362 if(sa_nonzero(&tnl->local.address) && bind(sock, sa(&tnl->local.address), sa_len(&tnl->local.address)) == -1) {
363 logger(LOG_ERR, _("tnl: could not bind socket: %s"), strerror(errno));
369 if(connect(sock, sa(&tnl->remote.address), sa_len(&tnl->remote.address)) == -1) {
370 logger(LOG_ERR, _("tnl: could not connect: %s"), strerror(errno));
375 tnl->status = TNL_STATUS_CONNECTING;
378 tnl->fd.write = tnl_connect_handler;
381 tnl->send_packet = tnl_send_packet;
382 tnl->send_meta = tnl_send_meta;
383 tnl->close = tnl_close;
392 static bool tnl_listen_close(tnl_listen_t *listener) {
393 fd_del(&listener->fd);
394 close(listener->fd.fd);
395 tnl_listen_del(listener);
399 bool tnl_listen(tnl_listen_t *listener) {
402 sock = socket(sa_family(&listener->local.address), listener->type, listener->protocol);
405 logger(LOG_ERR, _("tnl: could not create listener socket: %s"), strerror(errno));
409 if(bind(sock, sa(&listener->local.address), sa_len(&listener->local.address)) == -1) {
410 logger(LOG_ERR, _("tnl: could not bind listener socket: %s"), strerror(errno));
414 if(listen(sock, 10) == -1) {
415 logger(LOG_ERR, _("tnl: could not listen on listener socket: %s"), strerror(errno));
419 listener->fd.fd = sock;
420 listener->fd.read = tnl_accept_handler;
421 listener->fd.data = listener;
422 listener->close = tnl_listen_close;
424 tnl_listen_add(listener);
425 fd_add(&listener->fd);