2016-03-12 09:52:14 +01:00
|
|
|
/*
|
|
|
|
This file is part of actube.
|
2015-02-09 22:04:14 +01:00
|
|
|
|
2016-03-12 09:52:14 +01:00
|
|
|
actube is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
libcapwap is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with Foobar. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @file
|
|
|
|
* @brief Implements dtls_gnutls_connect
|
|
|
|
*/
|
2015-03-23 07:48:27 +01:00
|
|
|
|
|
|
|
#include "gnutls/dtls.h"
|
|
|
|
|
2018-03-17 17:29:09 +01:00
|
|
|
|
2018-04-02 01:39:08 +02:00
|
|
|
#include "dtls_common.h"
|
2015-02-09 22:04:14 +01:00
|
|
|
#include "dtls_gnutls.h"
|
|
|
|
|
|
|
|
#include "conn.h"
|
2015-04-10 17:52:01 +02:00
|
|
|
#include "log.h"
|
2015-04-11 19:00:51 +02:00
|
|
|
#include "dbg.h"
|
2015-02-09 22:04:14 +01:00
|
|
|
|
2015-04-14 07:42:23 +02:00
|
|
|
#include "sock.h"
|
2016-03-12 09:52:14 +01:00
|
|
|
/**
|
|
|
|
* Establish a DTLS connection using gnutls library
|
2018-04-04 10:59:07 +02:00
|
|
|
* @see #dtls_connec
|
2016-03-12 09:52:14 +01:00
|
|
|
*/
|
2015-02-09 22:04:14 +01:00
|
|
|
int dtls_gnutls_connect(struct conn *conn)
|
|
|
|
{
|
2018-03-25 10:07:39 +02:00
|
|
|
int rc;
|
2018-03-05 12:23:16 +01:00
|
|
|
char sock_buf[SOCK_ADDR_BUFSIZE];
|
2016-03-06 11:07:25 +01:00
|
|
|
struct dtls_gnutls_data *d;
|
2018-04-04 10:59:07 +02:00
|
|
|
gnutls_datum_t key;
|
2016-03-06 11:07:25 +01:00
|
|
|
d = dtls_gnutls_data_create(conn,
|
|
|
|
GNUTLS_CLIENT | GNUTLS_DATAGRAM | GNUTLS_NONBLOCK);
|
2015-02-09 22:04:14 +01:00
|
|
|
|
2016-03-12 03:56:48 +01:00
|
|
|
if (!d)
|
|
|
|
return 0;
|
|
|
|
|
2018-03-25 10:07:39 +02:00
|
|
|
/*// gnutls_dh_set_prime_bits(d->session, 512);*/
|
2016-03-12 22:30:12 +01:00
|
|
|
/*#if GNUTLS_VERSION_NUMBER >= 0x030100
|
2016-03-06 11:07:25 +01:00
|
|
|
gnutls_handshake_set_timeout(d->session, GNUTLS_DEFAULT_HANDSHAKE_TIMEOUT);
|
2015-03-12 23:21:57 +01:00
|
|
|
|
2016-03-06 11:07:25 +01:00
|
|
|
gnutls_dtls_set_data_mtu(d->session, 1500);
|
2015-04-30 08:36:40 +02:00
|
|
|
#endif
|
2016-03-06 11:07:25 +01:00
|
|
|
gnutls_dtls_set_mtu(d->session, 1500);
|
2016-03-12 22:30:12 +01:00
|
|
|
*/
|
2015-03-23 07:48:27 +01:00
|
|
|
|
2018-04-04 10:59:07 +02:00
|
|
|
/*
|
|
|
|
if (conn->dtls_psk != NULL){
|
|
|
|
key.data=(unsigned char*)conn->dtls_psk;
|
|
|
|
key.size=strlen(conn->dtls_psk);
|
|
|
|
|
|
|
|
rc = gnutls_credentials_set(d->session, GNUTLS_CRD_PSK, &key);
|
|
|
|
if (rc<0) {
|
|
|
|
cw_log(LOG_ERR, "DTLS - Can't set x.509 credentials: %s", gnutls_strerror(rc));
|
|
|
|
dtls_gnutls_data_destroy(d);
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
if (conn->dtls_psk_enable){
|
|
|
|
gnutls_psk_client_credentials_t cred;
|
|
|
|
rc = gnutls_psk_allocate_client_credentials(&cred);
|
|
|
|
if (rc != 0) {
|
|
|
|
cw_dbg(DBG_DTLS,"gnutls_psk_allocate_client_credentials() failed.\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
key.size = bstr16_len(conn->dtls_psk);
|
|
|
|
key.data = bstr16_data(conn->dtls_psk);
|
|
|
|
|
|
|
|
|
|
|
|
/* Put the username and key into the structure we use to tell GnuTLs what
|
|
|
|
// the credentials are. The example server doesn't care about usernames, so
|
|
|
|
// we use "Alice" here.*/
|
|
|
|
rc = gnutls_psk_set_client_credentials(cred, "Alice", &key, GNUTLS_PSK_KEY_RAW);
|
|
|
|
rc = gnutls_credentials_set(d->session, GNUTLS_CRD_PSK, cred);
|
|
|
|
if (rc != 0) {
|
|
|
|
cw_log(LOG_ERR,"gnutls_credentials_set() failed.");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-03-25 10:07:39 +02:00
|
|
|
|
2016-03-12 09:52:14 +01:00
|
|
|
cw_dbg(DBG_DTLS,"Starting handshake");
|
2015-02-09 22:04:14 +01:00
|
|
|
do {
|
|
|
|
rc = gnutls_handshake(d->session);
|
2016-03-06 11:07:25 +01:00
|
|
|
} while (rc == GNUTLS_E_AGAIN);
|
2015-02-09 22:04:14 +01:00
|
|
|
|
2016-03-06 11:07:25 +01:00
|
|
|
|
|
|
|
if (rc < 0) {
|
2020-03-08 14:37:12 +01:00
|
|
|
if (errno)
|
|
|
|
cw_log(LOG_ERR, "DTLS (gnutls bio): %s",strerror(errno));
|
2016-03-06 11:07:25 +01:00
|
|
|
cw_log(LOG_ERR, "DTLS (gnutls) Can't connect to %s: %s",
|
2018-03-05 12:23:16 +01:00
|
|
|
sock_addr2str(&conn->addr,sock_buf), gnutls_strerror(rc));
|
2015-02-09 22:04:14 +01:00
|
|
|
return 0;
|
|
|
|
}
|
2015-03-12 23:21:57 +01:00
|
|
|
|
|
|
|
|
2018-03-05 12:23:16 +01:00
|
|
|
cw_dbg(DBG_DTLS, "Handshake with %s successful", sock_addr2str(&conn->addr,sock_buf));
|
2015-03-12 23:21:57 +01:00
|
|
|
|
2016-03-06 11:07:25 +01:00
|
|
|
conn->dtls_data = d;
|
2015-03-12 23:21:57 +01:00
|
|
|
conn->read = dtls_gnutls_read;
|
|
|
|
conn->write = dtls_gnutls_write;
|
|
|
|
|
2016-03-06 11:07:25 +01:00
|
|
|
|
2015-02-09 22:04:14 +01:00
|
|
|
return 1;
|
|
|
|
}
|