2014-08-03 08:46:42 +02:00
|
|
|
/*
|
|
|
|
This file is part of libcapwap.
|
|
|
|
|
|
|
|
libcapwap 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/>.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
2015-02-07 10:54:32 +01:00
|
|
|
#include <stdlib.h>
|
2015-02-08 11:42:01 +01:00
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
2014-08-03 13:35:09 +02:00
|
|
|
|
|
|
|
#include <gnutls/gnutls.h>
|
2015-02-08 11:42:01 +01:00
|
|
|
#include <gnutls/dtls.h>
|
2014-08-03 13:35:09 +02:00
|
|
|
|
|
|
|
|
|
|
|
#include "conn.h"
|
|
|
|
#include "cw_log.h"
|
2015-02-08 11:42:01 +01:00
|
|
|
#include "sock.h"
|
|
|
|
#include "cw_util.h"
|
2015-02-08 16:28:04 +01:00
|
|
|
#include "dtls_gnutls.h"
|
2015-02-07 10:54:32 +01:00
|
|
|
|
2014-08-03 13:35:09 +02:00
|
|
|
|
2015-02-08 11:42:01 +01:00
|
|
|
int dtls_gnutls_accept(struct conn *conn)
|
2014-08-03 08:46:42 +02:00
|
|
|
{
|
2015-02-07 10:54:32 +01:00
|
|
|
struct dtls_gnutls_data *d;
|
2015-02-08 11:42:01 +01:00
|
|
|
|
|
|
|
gnutls_datum_t cookie_key;
|
|
|
|
|
|
|
|
gnutls_key_generate(&cookie_key, GNUTLS_COOKIE_KEY_SIZE);
|
|
|
|
cw_dbg(DBG_DTLS, "DTLS session cookie for %s generated: %s",
|
|
|
|
sock_addr2str(&conn->addr), sock_hwaddr2idstr((uint8_t *) (&cookie_key),
|
|
|
|
sizeof(cookie_key)));
|
|
|
|
|
|
|
|
gnutls_dtls_prestate_st prestate;
|
|
|
|
memset(&prestate, 0, sizeof(prestate));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
uint8_t buffer[2048];
|
|
|
|
int tlen;
|
|
|
|
tlen = dtls_gnutls_bio_read(conn, buffer, sizeof(buffer));
|
|
|
|
|
|
|
|
gnutls_dtls_cookie_send(&cookie_key, &conn->addr, sizeof(conn->addr),
|
|
|
|
&prestate, (gnutls_transport_ptr_t) conn, dtls_gnutls_bio_write);
|
|
|
|
|
|
|
|
int rc=-1;
|
|
|
|
|
|
|
|
time_t c_timer = cw_timer_start(10);
|
|
|
|
|
|
|
|
while(!cw_timer_timeout(c_timer)){
|
|
|
|
|
|
|
|
tlen = conn_q_recv_packet_peek(conn,buffer,sizeof(buffer));
|
|
|
|
|
|
|
|
if (tlen <0 && errno == EAGAIN)
|
|
|
|
continue;
|
|
|
|
if (tlen < 0 ){
|
2015-02-08 21:07:55 +01:00
|
|
|
/* something went wrong, iwe should log a message */
|
2015-02-08 11:42:01 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
rc = gnutls_dtls_cookie_verify(&cookie_key,
|
|
|
|
&conn->addr,
|
|
|
|
sizeof(conn->addr), buffer+4, tlen-4, &prestate);
|
|
|
|
if (rc<0){
|
|
|
|
cw_dbg(DBG_DTLS, "DTLS - Cookie couldn't be verified: %s", gnutls_strerror(rc));
|
|
|
|
dtls_gnutls_bio_read(conn, buffer, sizeof(buffer));
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2015-02-08 21:07:55 +01:00
|
|
|
// dtls_gnutls_bio_read(conn, buffer, sizeof(buffer));
|
2015-02-08 11:42:01 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if (rc <0 ){
|
|
|
|
cw_log(LOG_ERR, "DTLS - Cookie couldn't be verified: %s", gnutls_strerror(rc));
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
cw_dbg(DBG_DTLS, "DTLS - Cookie verified! Starting handshake ...");
|
|
|
|
|
|
|
|
|
2015-02-08 21:07:55 +01:00
|
|
|
d = dtls_gnutls_data_create(conn,GNUTLS_SERVER | GNUTLS_DATAGRAM);
|
2015-02-07 10:54:32 +01:00
|
|
|
if (!d)
|
|
|
|
return 0;
|
|
|
|
|
2015-02-08 21:07:55 +01:00
|
|
|
gnutls_certificate_server_set_request(d->session,GNUTLS_CERT_REQUEST);
|
2015-02-08 11:42:01 +01:00
|
|
|
gnutls_dtls_prestate_set(d->session, &prestate);
|
2015-02-07 10:54:32 +01:00
|
|
|
|
2015-02-08 11:42:01 +01:00
|
|
|
c_timer = cw_timer_start(10);
|
|
|
|
do{
|
2015-02-07 10:54:32 +01:00
|
|
|
rc = gnutls_handshake(d->session);
|
2015-02-08 11:42:01 +01:00
|
|
|
}while(!cw_timer_timeout(c_timer) && rc==GNUTLS_E_AGAIN);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if ( rc < 0 ) {
|
|
|
|
cw_log(LOG_ERR, "DTLS - Error in handshake: %s", gnutls_strerror(rc));
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-02-07 10:54:32 +01:00
|
|
|
|
2015-02-08 11:42:01 +01:00
|
|
|
cw_dbg(DBG_DTLS,"DTLS - Handshake successful");
|
2014-08-03 13:35:09 +02:00
|
|
|
|
2015-02-08 11:42:01 +01:00
|
|
|
conn->dtls_data=d;
|
|
|
|
conn->read = dtls_gnutls_read;
|
|
|
|
conn->write = dtls_gnutls_write;
|
2014-08-03 13:35:09 +02:00
|
|
|
|
2015-02-08 11:42:01 +01:00
|
|
|
return 1;
|
2014-08-03 08:46:42 +02:00
|
|
|
}
|
|
|
|
|
2014-08-03 13:35:09 +02:00
|
|
|
|