2013-05-01 14:52:55 +02:00
|
|
|
#include "capwap.h"
|
2016-08-22 16:59:55 +02:00
|
|
|
#include "dtls.h"
|
|
|
|
#include "protocol.h"
|
2016-02-04 14:59:55 +01:00
|
|
|
#include <wolfssl/options.h>
|
2016-03-07 15:32:36 +01:00
|
|
|
#include <wolfssl/ssl.h>
|
|
|
|
#include <wolfssl/wolfcrypt/sha.h>
|
2013-05-27 23:10:49 +02:00
|
|
|
|
2013-05-01 14:52:55 +02:00
|
|
|
/* */
|
2014-05-15 21:43:21 +02:00
|
|
|
static const char g_char2hex[] = {
|
|
|
|
0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
|
|
|
|
-1, -1, -1, -1, -1, -1, -1,
|
|
|
|
10, 11, 12, 13, 14, 15, /* Upper Case A - F */
|
|
|
|
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
|
|
|
|
10, 11, 12, 13, 14, 15 /* Lower Case a - f */
|
2013-05-01 14:52:55 +02:00
|
|
|
};
|
2014-05-15 21:43:21 +02:00
|
|
|
static const int g_char2hex_length = sizeof(g_char2hex) / sizeof(g_char2hex[0]);
|
2013-05-01 14:52:55 +02:00
|
|
|
|
|
|
|
/* */
|
2016-03-07 15:32:36 +01:00
|
|
|
static int capwap_bio_method_recv(WOLFSSL* ssl, char* buffer, int length, void* context) {
|
2014-05-15 21:43:21 +02:00
|
|
|
struct capwap_dtls* dtls = (struct capwap_dtls*)context;
|
2013-05-01 14:52:55 +02:00
|
|
|
struct capwap_dtls_header* dtlspreamble;
|
|
|
|
int size;
|
|
|
|
|
|
|
|
/* Check read packet */
|
2014-05-15 21:43:21 +02:00
|
|
|
if ((dtls->length < sizeof(struct capwap_dtls_header)) || !dtls->buffer) {
|
|
|
|
if (!dtls->length && !dtls->buffer) {
|
2016-03-07 15:32:36 +01:00
|
|
|
return WOLFSSL_CBIO_ERR_WANT_READ; /* Notify empty buffer */
|
2013-05-01 14:52:55 +02:00
|
|
|
}
|
|
|
|
|
2016-03-07 15:32:36 +01:00
|
|
|
return WOLFSSL_CBIO_ERR_GENERAL;
|
2013-05-01 14:52:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Check DTLS Capwap Preamble */
|
2014-05-15 21:43:21 +02:00
|
|
|
dtlspreamble = (struct capwap_dtls_header*)dtls->buffer;
|
2013-05-01 14:52:55 +02:00
|
|
|
if ((dtlspreamble->preamble.version != CAPWAP_PROTOCOL_VERSION) || (dtlspreamble->preamble.type != CAPWAP_PREAMBLE_DTLS_HEADER)) {
|
2016-03-30 14:47:57 +02:00
|
|
|
log_printf(LOG_DEBUG, "Wrong DTLS Capwap Preamble");
|
2016-03-07 15:32:36 +01:00
|
|
|
return WOLFSSL_CBIO_ERR_GENERAL; /* Wrong DTLS Capwap Preamble */
|
2013-05-01 14:52:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* */
|
2014-05-15 21:43:21 +02:00
|
|
|
size = dtls->length - sizeof(struct capwap_dtls_header);
|
|
|
|
dtls->length = 0;
|
2013-05-01 14:52:55 +02:00
|
|
|
|
2014-05-15 21:43:21 +02:00
|
|
|
dtls->buffer += sizeof(struct capwap_dtls_header);
|
2013-05-01 14:52:55 +02:00
|
|
|
if (size > length) {
|
2014-05-15 21:43:21 +02:00
|
|
|
dtls->buffer = NULL;
|
2016-03-07 15:32:36 +01:00
|
|
|
return WOLFSSL_CBIO_ERR_GENERAL;
|
2013-05-01 14:52:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Copy DTLS packet */
|
2014-05-15 21:43:21 +02:00
|
|
|
memcpy(buffer, dtls->buffer, size);
|
|
|
|
dtls->buffer = NULL;
|
2013-05-01 14:52:55 +02:00
|
|
|
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* */
|
2016-03-07 15:32:36 +01:00
|
|
|
static int capwap_bio_method_send(WOLFSSL* ssl, char* buffer, int length, void* context) {
|
2014-12-27 18:45:09 +01:00
|
|
|
int err;
|
2014-05-15 21:43:21 +02:00
|
|
|
char data[CAPWAP_MAX_PACKET_SIZE];
|
|
|
|
struct capwap_dtls* dtls = (struct capwap_dtls*)context;
|
|
|
|
struct capwap_dtls_header* dtlspreamble = (struct capwap_dtls_header*)data;
|
2013-05-01 14:52:55 +02:00
|
|
|
|
|
|
|
/* Check for maxium size of packet */
|
|
|
|
if (length > (CAPWAP_MAX_PACKET_SIZE - sizeof(struct capwap_dtls_header))) {
|
2016-03-07 15:32:36 +01:00
|
|
|
return WOLFSSL_CBIO_ERR_GENERAL;
|
2013-05-01 14:52:55 +02:00
|
|
|
}
|
2014-05-15 21:43:21 +02:00
|
|
|
|
2013-05-01 14:52:55 +02:00
|
|
|
/* Create DTLS Capwap Preamble */
|
|
|
|
dtlspreamble->preamble.version = CAPWAP_PROTOCOL_VERSION;
|
|
|
|
dtlspreamble->preamble.type = CAPWAP_PREAMBLE_DTLS_HEADER;
|
|
|
|
dtlspreamble->reserved1 = dtlspreamble->reserved2 = dtlspreamble->reserved3 = 0;
|
2014-05-15 21:43:21 +02:00
|
|
|
memcpy(&data[0] + sizeof(struct capwap_dtls_header), buffer, length);
|
2013-05-01 14:52:55 +02:00
|
|
|
|
|
|
|
/* Send packet */
|
2014-12-27 18:45:09 +01:00
|
|
|
err = capwap_sendto(dtls->sock, data, length + sizeof(struct capwap_dtls_header), &dtls->peeraddr);
|
|
|
|
if (err <= 0) {
|
2016-03-30 14:47:57 +02:00
|
|
|
log_printf(LOG_WARNING, "Unable to send crypt packet, sentto return error %d", err);
|
2016-03-07 15:32:36 +01:00
|
|
|
return WOLFSSL_CBIO_ERR_GENERAL;
|
2013-05-01 14:52:55 +02:00
|
|
|
}
|
2014-05-15 21:43:21 +02:00
|
|
|
|
2013-05-01 14:52:55 +02:00
|
|
|
/* Don't return size of DTLS Capwap Preamble */
|
|
|
|
return length;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* */
|
2014-05-15 21:43:21 +02:00
|
|
|
int capwap_crypt_init() {
|
|
|
|
int result;
|
2013-05-01 14:52:55 +02:00
|
|
|
|
2014-05-15 21:43:21 +02:00
|
|
|
/* Init library */
|
2016-03-07 15:32:36 +01:00
|
|
|
result = wolfSSL_Init();
|
2014-05-15 21:43:21 +02:00
|
|
|
if (result != SSL_SUCCESS) {
|
|
|
|
return -1;
|
2013-05-01 14:52:55 +02:00
|
|
|
}
|
|
|
|
|
2014-05-15 21:43:21 +02:00
|
|
|
return 0;
|
2013-05-23 22:38:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* */
|
2014-05-15 21:43:21 +02:00
|
|
|
void capwap_crypt_free() {
|
2016-03-07 15:32:36 +01:00
|
|
|
wolfSSL_Cleanup();
|
2013-05-23 22:38:48 +02:00
|
|
|
}
|
|
|
|
|
2013-05-01 14:52:55 +02:00
|
|
|
/* */
|
2016-03-07 15:32:36 +01:00
|
|
|
static int capwap_crypt_verifycertificate(int preverify_ok, WOLFSSL_X509_STORE_CTX* ctx) {
|
2014-05-15 21:43:21 +02:00
|
|
|
return preverify_ok;
|
2013-05-01 14:52:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* */
|
2016-03-07 15:32:36 +01:00
|
|
|
static unsigned int capwap_crypt_psk_client(WOLFSSL* ssl, const char* hint, char* identity, unsigned int max_identity_len, unsigned char* psk, unsigned int max_psk_len) {
|
|
|
|
struct capwap_dtls* dtls = (struct capwap_dtls*)wolfSSL_GetIOReadCtx(ssl);
|
2013-05-27 21:33:23 +02:00
|
|
|
|
2014-05-15 21:43:21 +02:00
|
|
|
ASSERT(dtls != NULL);
|
|
|
|
ASSERT(dtls->dtlscontext != NULL);
|
2013-05-23 22:38:48 +02:00
|
|
|
|
|
|
|
/* */
|
2014-05-15 21:43:21 +02:00
|
|
|
if ((max_identity_len < strlen(dtls->dtlscontext->presharedkey.identity)) || (max_psk_len < dtls->dtlscontext->presharedkey.pskkeylength)) {
|
|
|
|
return 0;
|
2013-05-23 22:38:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* */
|
2014-05-15 21:43:21 +02:00
|
|
|
strcpy(identity, dtls->dtlscontext->presharedkey.identity);
|
|
|
|
memcpy(psk, dtls->dtlscontext->presharedkey.pskkey, dtls->dtlscontext->presharedkey.pskkeylength);
|
|
|
|
return dtls->dtlscontext->presharedkey.pskkeylength;
|
2013-05-01 14:52:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* */
|
2016-03-07 15:32:36 +01:00
|
|
|
static unsigned int capwap_crypt_psk_server(WOLFSSL* ssl, const char* identity, unsigned char* psk, unsigned int max_psk_len) {
|
|
|
|
struct capwap_dtls* dtls = (struct capwap_dtls*)wolfSSL_GetIOReadCtx(ssl);
|
2014-05-15 21:43:21 +02:00
|
|
|
|
|
|
|
ASSERT(dtls != NULL);
|
|
|
|
ASSERT(dtls->dtlscontext != NULL);
|
2013-05-01 14:52:55 +02:00
|
|
|
|
2014-05-15 21:43:21 +02:00
|
|
|
/* */
|
|
|
|
if (strcmp(identity, dtls->dtlscontext->presharedkey.identity) || (max_psk_len < dtls->dtlscontext->presharedkey.pskkeylength)) {
|
2013-05-01 14:52:55 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-05-15 21:43:21 +02:00
|
|
|
/* */
|
|
|
|
memcpy(psk, dtls->dtlscontext->presharedkey.pskkey, dtls->dtlscontext->presharedkey.pskkeylength);
|
|
|
|
return dtls->dtlscontext->presharedkey.pskkeylength;
|
2013-05-01 14:52:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* */
|
2014-05-15 21:43:21 +02:00
|
|
|
static unsigned int capwap_crypt_psk_to_bin(char* pskkey, unsigned char** pskbin) {
|
|
|
|
int i, j;
|
2013-05-01 14:52:55 +02:00
|
|
|
int length;
|
2014-05-15 21:43:21 +02:00
|
|
|
int result;
|
2013-05-01 14:52:55 +02:00
|
|
|
unsigned char* buffer;
|
|
|
|
|
2014-05-15 21:43:21 +02:00
|
|
|
/* Convert string to hex */
|
|
|
|
length = strlen(pskkey);
|
|
|
|
if (!length || (length % 2)) {
|
2013-05-01 14:52:55 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* */
|
2014-05-15 21:43:21 +02:00
|
|
|
result = length / 2;
|
|
|
|
buffer = (unsigned char*)capwap_alloc(result);
|
|
|
|
for (i = 0, j = 0; i < length; i += 2, j++) {
|
|
|
|
char valuehi = pskkey[i] - 48;
|
|
|
|
char valuelo = pskkey[i + 1] - 48;
|
|
|
|
|
|
|
|
/* Check value */
|
|
|
|
if ((valuehi < 0) || (valuehi >= g_char2hex_length) || (valuelo < 0) || (valuelo >= g_char2hex_length)) {
|
|
|
|
capwap_free(buffer);
|
|
|
|
return 0;
|
|
|
|
}
|
2013-05-01 14:52:55 +02:00
|
|
|
|
2014-05-15 21:43:21 +02:00
|
|
|
/* */
|
|
|
|
valuehi = g_char2hex[(int)valuehi];
|
|
|
|
valuelo = g_char2hex[(int)valuelo];
|
2013-05-01 14:52:55 +02:00
|
|
|
|
2014-05-15 21:43:21 +02:00
|
|
|
/* Check value */
|
|
|
|
if ((valuehi < 0) || (valuelo < 0)) {
|
|
|
|
capwap_free(buffer);
|
|
|
|
return 0;
|
|
|
|
}
|
2013-05-01 14:52:55 +02:00
|
|
|
|
2014-05-15 21:43:21 +02:00
|
|
|
/* */
|
|
|
|
buffer[j] = (unsigned char)(((unsigned char)valuehi << 4) | (unsigned char)valuelo);
|
2013-05-01 14:52:55 +02:00
|
|
|
}
|
|
|
|
|
2014-05-15 21:43:21 +02:00
|
|
|
/* */
|
|
|
|
*pskbin = buffer;
|
|
|
|
return result;
|
2013-05-01 14:52:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* */
|
2016-03-07 15:32:36 +01:00
|
|
|
static int capwap_crypt_createcookie(WOLFSSL* ssl, unsigned char* buffer, int size, void* context) {
|
2014-05-15 21:43:21 +02:00
|
|
|
int length;
|
|
|
|
unsigned char temp[32];
|
|
|
|
Sha sha;
|
|
|
|
byte digest[SHA_DIGEST_SIZE];
|
|
|
|
struct capwap_dtls* dtls = (struct capwap_dtls*)context;
|
2013-05-01 14:52:55 +02:00
|
|
|
|
2014-05-15 21:43:21 +02:00
|
|
|
if (size != SHA_DIGEST_SIZE) {
|
|
|
|
return -1;
|
2013-05-01 14:52:55 +02:00
|
|
|
}
|
|
|
|
|
2014-05-15 21:43:21 +02:00
|
|
|
/* Create buffer with peer's address and port */
|
2014-09-10 21:58:23 +02:00
|
|
|
if (dtls->peeraddr.ss.ss_family == AF_INET) {
|
2014-05-15 21:43:21 +02:00
|
|
|
length = sizeof(struct in_addr) + sizeof(in_port_t);
|
2014-09-10 21:58:23 +02:00
|
|
|
memcpy(temp, &dtls->peeraddr.sin.sin_port, sizeof(in_port_t));
|
|
|
|
memcpy(temp + sizeof(in_port_t), &dtls->peeraddr.sin.sin_addr, sizeof(struct in_addr));
|
|
|
|
} else if (dtls->peeraddr.ss.ss_family == AF_INET6) {
|
2014-05-15 21:43:21 +02:00
|
|
|
length = sizeof(struct in6_addr) + sizeof(in_port_t);
|
2014-09-10 21:58:23 +02:00
|
|
|
memcpy(temp, &dtls->peeraddr.sin6.sin6_port, sizeof(in_port_t));
|
|
|
|
memcpy(temp + sizeof(in_port_t), &dtls->peeraddr.sin6.sin6_addr, sizeof(struct in6_addr));
|
2014-05-15 21:43:21 +02:00
|
|
|
} else {
|
|
|
|
return -1;
|
2013-05-27 23:10:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* */
|
2016-03-07 15:32:36 +01:00
|
|
|
if (wc_InitSha(&sha)) {
|
2014-05-15 21:43:21 +02:00
|
|
|
return -1;
|
2013-05-27 23:10:49 +02:00
|
|
|
}
|
|
|
|
|
2016-03-07 15:32:36 +01:00
|
|
|
wc_ShaUpdate(&sha, temp, length);
|
|
|
|
wc_ShaFinal(&sha, digest);
|
2013-05-27 23:10:49 +02:00
|
|
|
|
|
|
|
/* */
|
2014-05-15 21:43:21 +02:00
|
|
|
memcpy(buffer, digest, SHA_DIGEST_SIZE);
|
|
|
|
return SHA_DIGEST_SIZE;
|
2013-05-27 23:10:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* */
|
|
|
|
int capwap_crypt_createcontext(struct capwap_dtls_context* dtlscontext, struct capwap_dtls_param* param) {
|
2013-05-01 14:52:55 +02:00
|
|
|
ASSERT(dtlscontext != NULL);
|
|
|
|
ASSERT(param != NULL);
|
2013-05-27 23:10:49 +02:00
|
|
|
|
2013-05-01 14:52:55 +02:00
|
|
|
memset(dtlscontext, 0, sizeof(struct capwap_dtls_context));
|
|
|
|
dtlscontext->type = param->type;
|
|
|
|
dtlscontext->mode = param->mode;
|
2013-05-27 23:10:49 +02:00
|
|
|
|
2013-05-01 14:52:55 +02:00
|
|
|
/* Alloc context */
|
2016-03-07 15:32:36 +01:00
|
|
|
dtlscontext->sslcontext = (void*)wolfSSL_CTX_new(((param->type == CAPWAP_DTLS_SERVER) ? wolfDTLSv1_server_method() : wolfDTLSv1_client_method()));
|
2013-05-01 14:52:55 +02:00
|
|
|
if (!dtlscontext->sslcontext) {
|
2016-03-30 14:47:57 +02:00
|
|
|
log_printf(LOG_DEBUG, "Error to initialize dtls context");
|
2013-05-01 14:52:55 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2013-05-27 23:10:49 +02:00
|
|
|
|
2014-05-15 21:43:21 +02:00
|
|
|
/* Set context IO */
|
2016-03-07 15:32:36 +01:00
|
|
|
wolfSSL_SetIORecv((WOLFSSL_CTX*)dtlscontext->sslcontext, capwap_bio_method_recv);
|
|
|
|
wolfSSL_SetIOSend((WOLFSSL_CTX*)dtlscontext->sslcontext, capwap_bio_method_send);
|
|
|
|
wolfSSL_CTX_SetGenCookie((WOLFSSL_CTX*)dtlscontext->sslcontext, capwap_crypt_createcookie);
|
2014-05-15 21:43:21 +02:00
|
|
|
|
|
|
|
/* */
|
2013-05-01 14:52:55 +02:00
|
|
|
if (dtlscontext->mode == CAPWAP_DTLS_MODE_CERTIFICATE) {
|
|
|
|
/* Check context */
|
|
|
|
if (!param->cert.filecert || !strlen(param->cert.filecert)) {
|
2016-03-30 14:47:57 +02:00
|
|
|
log_printf(LOG_DEBUG, "Error, request certificate file");
|
2013-05-01 14:52:55 +02:00
|
|
|
capwap_crypt_freecontext(dtlscontext);
|
|
|
|
return 0;
|
|
|
|
} else if (!param->cert.filekey || !strlen(param->cert.filekey)) {
|
2016-03-30 14:47:57 +02:00
|
|
|
log_printf(LOG_DEBUG, "Error, request privatekey file");
|
2013-05-01 14:52:55 +02:00
|
|
|
capwap_crypt_freecontext(dtlscontext);
|
|
|
|
return 0;
|
|
|
|
} else if (!param->cert.fileca || !strlen(param->cert.fileca)) {
|
2016-03-30 14:47:57 +02:00
|
|
|
log_printf(LOG_DEBUG, "Error, request ca file");
|
2013-05-01 14:52:55 +02:00
|
|
|
capwap_crypt_freecontext(dtlscontext);
|
|
|
|
return 0;
|
|
|
|
}
|
2013-05-27 23:10:49 +02:00
|
|
|
|
2013-05-01 14:52:55 +02:00
|
|
|
/* Public certificate */
|
2016-03-07 15:32:36 +01:00
|
|
|
if (!wolfSSL_CTX_use_certificate_file((WOLFSSL_CTX*)dtlscontext->sslcontext, param->cert.filecert, SSL_FILETYPE_PEM)) {
|
2016-03-30 14:47:57 +02:00
|
|
|
log_printf(LOG_DEBUG, "Error to load certificate file");
|
2013-05-01 14:52:55 +02:00
|
|
|
capwap_crypt_freecontext(dtlscontext);
|
|
|
|
return 0;
|
|
|
|
}
|
2013-05-27 23:10:49 +02:00
|
|
|
|
2013-05-01 14:52:55 +02:00
|
|
|
/* Private key */
|
2016-03-07 15:32:36 +01:00
|
|
|
if (!wolfSSL_CTX_use_PrivateKey_file((WOLFSSL_CTX*)dtlscontext->sslcontext, param->cert.filekey, SSL_FILETYPE_PEM)) {
|
2016-03-30 14:47:57 +02:00
|
|
|
log_printf(LOG_DEBUG, "Error to load private key file");
|
2013-05-01 14:52:55 +02:00
|
|
|
capwap_crypt_freecontext(dtlscontext);
|
|
|
|
return 0;
|
|
|
|
}
|
2013-05-27 23:10:49 +02:00
|
|
|
|
2016-03-07 15:32:36 +01:00
|
|
|
if (!wolfSSL_CTX_check_private_key((WOLFSSL_CTX*)dtlscontext->sslcontext)) {
|
2016-03-30 14:47:57 +02:00
|
|
|
log_printf(LOG_DEBUG, "Error to check private key");
|
2013-05-01 14:52:55 +02:00
|
|
|
capwap_crypt_freecontext(dtlscontext);
|
|
|
|
return 0;
|
|
|
|
}
|
2013-05-27 23:10:49 +02:00
|
|
|
|
2013-05-01 14:52:55 +02:00
|
|
|
/* Certificate Authority */
|
2016-03-07 15:32:36 +01:00
|
|
|
if (!wolfSSL_CTX_load_verify_locations((WOLFSSL_CTX*)dtlscontext->sslcontext, param->cert.fileca, NULL)) {
|
2016-03-30 14:47:57 +02:00
|
|
|
log_printf(LOG_DEBUG, "Error to load ca file");
|
2013-05-01 14:52:55 +02:00
|
|
|
capwap_crypt_freecontext(dtlscontext);
|
|
|
|
return 0;
|
|
|
|
}
|
2013-05-27 23:10:49 +02:00
|
|
|
|
2013-05-01 14:52:55 +02:00
|
|
|
/* Verify certificate callback */
|
2016-03-07 15:32:36 +01:00
|
|
|
wolfSSL_CTX_set_verify((WOLFSSL_CTX*)dtlscontext->sslcontext, ((param->type == CAPWAP_DTLS_SERVER) ? SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT : SSL_VERIFY_PEER), capwap_crypt_verifycertificate);
|
2013-05-01 14:52:55 +02:00
|
|
|
|
|
|
|
/* Cipher list:
|
|
|
|
TLS_RSA_WITH_AES_128_CBC_SHA
|
|
|
|
TLS_DHE_RSA_WITH_AES_128_CBC_SHA
|
|
|
|
TLS_RSA_WITH_AES_256_CBC_SHA
|
|
|
|
TLS_DHE_RSA_WITH_AES_256_CBC_SHA
|
|
|
|
*/
|
2016-03-07 15:32:36 +01:00
|
|
|
if (!wolfSSL_CTX_set_cipher_list((WOLFSSL_CTX*)dtlscontext->sslcontext, "AES128-SHA:DHE-RSA-AES128-SHA:AES256-SHA:DHE-RSA-AES256-SHA")) {
|
2016-03-30 14:47:57 +02:00
|
|
|
log_printf(LOG_DEBUG, "Error to select cipher list");
|
2013-05-01 14:52:55 +02:00
|
|
|
capwap_crypt_freecontext(dtlscontext);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
} else if (dtlscontext->mode == CAPWAP_DTLS_MODE_PRESHAREDKEY) {
|
2013-05-27 23:10:49 +02:00
|
|
|
/* Cipher list:
|
|
|
|
TLS_PSK_WITH_AES_128_CBC_SHA
|
|
|
|
TLS_DHE_PSK_WITH_AES_128_CBC_SHA
|
|
|
|
TLS_PSK_WITH_AES_256_CBC_SHA
|
|
|
|
TLS_DHE_PSK_WITH_AES_256_CBC_SHA
|
|
|
|
*/
|
2016-03-07 15:32:36 +01:00
|
|
|
if (!wolfSSL_CTX_set_cipher_list((WOLFSSL_CTX*)dtlscontext->sslcontext, "PSK-AES128-CBC-SHA:PSK-AES256-CBC-SHA")) {
|
2016-03-30 14:47:57 +02:00
|
|
|
log_printf(LOG_DEBUG, "Error to select cipher list");
|
2013-05-27 23:10:49 +02:00
|
|
|
capwap_crypt_freecontext(dtlscontext);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* */
|
|
|
|
if (dtlscontext->type == CAPWAP_DTLS_SERVER) {
|
|
|
|
if (param->presharedkey.hint) {
|
2016-03-07 15:32:36 +01:00
|
|
|
wolfSSL_CTX_use_psk_identity_hint((WOLFSSL_CTX*)dtlscontext->sslcontext, param->presharedkey.hint);
|
2013-05-27 23:10:49 +02:00
|
|
|
} else {
|
2016-03-30 14:47:57 +02:00
|
|
|
log_printf(LOG_DEBUG, "Error to presharedkey hint");
|
2013-05-27 23:10:49 +02:00
|
|
|
capwap_crypt_freecontext(dtlscontext);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* */
|
|
|
|
dtlscontext->presharedkey.identity = capwap_duplicate_string(param->presharedkey.identity);
|
|
|
|
dtlscontext->presharedkey.pskkeylength = capwap_crypt_psk_to_bin(param->presharedkey.pskkey, &dtlscontext->presharedkey.pskkey);
|
|
|
|
if (!dtlscontext->presharedkey.pskkeylength) {
|
2016-03-30 14:47:57 +02:00
|
|
|
log_printf(LOG_DEBUG, "Error to presharedkey");
|
2013-05-27 23:10:49 +02:00
|
|
|
capwap_crypt_freecontext(dtlscontext);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* */
|
|
|
|
if (dtlscontext->type == CAPWAP_DTLS_SERVER) {
|
2016-03-07 15:32:36 +01:00
|
|
|
wolfSSL_CTX_set_psk_server_callback((WOLFSSL_CTX*)dtlscontext->sslcontext, capwap_crypt_psk_server);
|
2013-05-27 23:10:49 +02:00
|
|
|
} else {
|
2016-03-07 15:32:36 +01:00
|
|
|
wolfSSL_CTX_set_psk_client_callback((WOLFSSL_CTX*)dtlscontext->sslcontext, capwap_crypt_psk_client);
|
2013-05-27 23:10:49 +02:00
|
|
|
}
|
2013-05-01 14:52:55 +02:00
|
|
|
} else {
|
2016-03-30 14:47:57 +02:00
|
|
|
log_printf(LOG_DEBUG, "Invalid DTLS mode");
|
2013-05-01 14:52:55 +02:00
|
|
|
capwap_crypt_freecontext(dtlscontext);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* */
|
|
|
|
void capwap_crypt_freecontext(struct capwap_dtls_context* dtlscontext) {
|
|
|
|
ASSERT(dtlscontext != NULL);
|
|
|
|
|
|
|
|
/* */
|
2014-05-15 21:43:21 +02:00
|
|
|
if (dtlscontext->mode == CAPWAP_DTLS_MODE_PRESHAREDKEY) {
|
2013-05-27 23:10:49 +02:00
|
|
|
if (dtlscontext->presharedkey.identity) {
|
|
|
|
capwap_free(dtlscontext->presharedkey.identity);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dtlscontext->presharedkey.pskkey) {
|
|
|
|
capwap_free(dtlscontext->presharedkey.pskkey);
|
|
|
|
}
|
2013-05-01 14:52:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Free context */
|
|
|
|
if (dtlscontext->sslcontext) {
|
2016-03-07 15:32:36 +01:00
|
|
|
wolfSSL_CTX_free((WOLFSSL_CTX*)dtlscontext->sslcontext);
|
2013-05-01 14:52:55 +02:00
|
|
|
}
|
|
|
|
|
2014-05-15 21:43:21 +02:00
|
|
|
memset(dtlscontext, 0, sizeof(struct capwap_dtls_context));
|
2013-05-01 14:52:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* */
|
2014-09-10 21:58:23 +02:00
|
|
|
int capwap_crypt_createsession(struct capwap_dtls* dtls, struct capwap_dtls_context* dtlscontext) {
|
2013-05-01 14:52:55 +02:00
|
|
|
ASSERT(dtls != NULL);
|
|
|
|
ASSERT(dtlscontext != NULL);
|
2014-05-15 21:43:21 +02:00
|
|
|
ASSERT(dtlscontext->sslcontext != NULL);
|
2013-05-01 14:52:55 +02:00
|
|
|
|
|
|
|
/* Create ssl session */
|
2016-03-07 15:32:36 +01:00
|
|
|
dtls->sslsession = (void*)wolfSSL_new((WOLFSSL_CTX*)dtlscontext->sslcontext);
|
2013-05-01 14:52:55 +02:00
|
|
|
if (!dtls->sslsession) {
|
2016-03-30 14:47:57 +02:00
|
|
|
log_printf(LOG_DEBUG, "Error to initialize dtls session");
|
2013-05-01 14:52:55 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* */
|
2016-03-07 15:32:36 +01:00
|
|
|
wolfSSL_set_using_nonblock((WOLFSSL*)dtls->sslsession, 1);
|
|
|
|
wolfSSL_SetIOReadCtx((WOLFSSL*)dtls->sslsession, (void*)dtls);
|
|
|
|
wolfSSL_SetIOWriteCtx((WOLFSSL*)dtls->sslsession, (void*)dtls);
|
|
|
|
wolfSSL_SetCookieCtx((WOLFSSL*)dtls->sslsession, (void*)dtls);
|
2013-05-01 14:52:55 +02:00
|
|
|
|
|
|
|
/* */
|
|
|
|
dtls->action = CAPWAP_DTLS_ACTION_NONE;
|
2014-05-15 21:43:21 +02:00
|
|
|
dtls->dtlscontext = dtlscontext;
|
2013-05-01 14:52:55 +02:00
|
|
|
dtls->enable = 1;
|
2014-09-10 21:58:23 +02:00
|
|
|
dtls->buffer = NULL;
|
|
|
|
dtls->length = 0;
|
|
|
|
|
2013-05-01 14:52:55 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* */
|
|
|
|
static int capwap_crypt_handshake(struct capwap_dtls* dtls) {
|
|
|
|
int result;
|
|
|
|
|
|
|
|
ASSERT(dtls != NULL);
|
|
|
|
ASSERT(dtls->enable != 0);
|
|
|
|
ASSERT((dtls->action == CAPWAP_DTLS_ACTION_NONE) || (dtls->action == CAPWAP_DTLS_ACTION_HANDSHAKE));
|
|
|
|
|
2014-05-15 21:43:21 +02:00
|
|
|
/* */
|
|
|
|
if (dtls->dtlscontext->type == CAPWAP_DTLS_SERVER) {
|
2016-03-07 15:32:36 +01:00
|
|
|
result = wolfSSL_accept((WOLFSSL*)dtls->sslsession);
|
2014-05-15 21:43:21 +02:00
|
|
|
} else {
|
2016-03-07 15:32:36 +01:00
|
|
|
result = wolfSSL_connect((WOLFSSL*)dtls->sslsession);
|
2014-05-15 21:43:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* */
|
|
|
|
if (result != SSL_SUCCESS) {
|
2016-08-17 15:18:35 +02:00
|
|
|
char buffer[WOLFSSL_MAX_ERROR_SZ];
|
|
|
|
|
2016-03-07 15:32:36 +01:00
|
|
|
result = wolfSSL_get_error((WOLFSSL*)dtls->sslsession, 0);
|
2013-05-01 14:52:55 +02:00
|
|
|
if ((result == SSL_ERROR_WANT_READ) || (result == SSL_ERROR_WANT_WRITE)) {
|
|
|
|
/* Incomplete handshake */
|
|
|
|
dtls->action = CAPWAP_DTLS_ACTION_HANDSHAKE;
|
|
|
|
return CAPWAP_HANDSHAKE_CONTINUE;
|
|
|
|
}
|
|
|
|
|
2016-08-17 15:18:35 +02:00
|
|
|
log_printf(LOG_DEBUG, "Error in DTLS handshake: %s",
|
|
|
|
wolfSSL_ERR_error_string(result, buffer));
|
|
|
|
|
2013-05-01 14:52:55 +02:00
|
|
|
/* Handshake error */
|
|
|
|
dtls->action = CAPWAP_DTLS_ACTION_ERROR;
|
|
|
|
return CAPWAP_HANDSHAKE_ERROR;
|
|
|
|
}
|
2014-05-15 21:43:21 +02:00
|
|
|
|
2013-05-01 14:52:55 +02:00
|
|
|
/* Handshake complete */
|
|
|
|
dtls->action = CAPWAP_DTLS_ACTION_DATA;
|
|
|
|
return CAPWAP_HANDSHAKE_COMPLETE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* */
|
2014-09-10 21:58:23 +02:00
|
|
|
void capwap_crypt_setconnection(struct capwap_dtls* dtls, int sock, union sockaddr_capwap* localaddr, union sockaddr_capwap* peeraddr) {
|
|
|
|
ASSERT(sock >= 0);
|
|
|
|
ASSERT(localaddr != NULL);
|
|
|
|
ASSERT(peeraddr != NULL);
|
|
|
|
|
|
|
|
dtls->sock = sock;
|
|
|
|
|
|
|
|
/* */
|
|
|
|
memcpy(&dtls->localaddr, localaddr, sizeof(union sockaddr_capwap));
|
|
|
|
if (dtls->localaddr.ss.ss_family == AF_INET6) {
|
|
|
|
capwap_ipv4_mapped_ipv6(&dtls->localaddr);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* */
|
|
|
|
memcpy(&dtls->peeraddr, peeraddr, sizeof(union sockaddr_capwap));
|
|
|
|
if (dtls->peeraddr.ss.ss_family == AF_INET6) {
|
|
|
|
capwap_ipv4_mapped_ipv6(&dtls->peeraddr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* */
|
|
|
|
int capwap_crypt_open(struct capwap_dtls* dtls) {
|
2013-05-01 14:52:55 +02:00
|
|
|
return capwap_crypt_handshake(dtls);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* */
|
|
|
|
void capwap_crypt_close(struct capwap_dtls* dtls) {
|
|
|
|
ASSERT(dtls != NULL);
|
|
|
|
ASSERT(dtls->enable != 0);
|
2013-05-27 21:33:23 +02:00
|
|
|
|
2014-05-15 21:43:21 +02:00
|
|
|
if (dtls->sslsession) {
|
2016-03-07 15:32:36 +01:00
|
|
|
wolfSSL_shutdown((WOLFSSL*)dtls->sslsession);
|
2013-05-01 14:52:55 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* */
|
|
|
|
void capwap_crypt_freesession(struct capwap_dtls* dtls) {
|
|
|
|
ASSERT(dtls != NULL);
|
2013-05-27 21:33:23 +02:00
|
|
|
|
2013-05-01 14:52:55 +02:00
|
|
|
/* Free SSL session */
|
|
|
|
if (dtls->sslsession) {
|
2016-03-07 15:32:36 +01:00
|
|
|
wolfSSL_free((WOLFSSL*)dtls->sslsession);
|
2013-05-01 14:52:55 +02:00
|
|
|
}
|
2013-05-27 21:33:23 +02:00
|
|
|
|
|
|
|
/* */
|
2013-05-01 14:52:55 +02:00
|
|
|
memset(dtls, 0, sizeof(struct capwap_dtls));
|
|
|
|
}
|
|
|
|
|
2014-09-10 21:58:23 +02:00
|
|
|
/* */
|
|
|
|
int capwap_crypt_sendto(struct capwap_dtls* dtls, void* buffer, int size) {
|
2014-12-27 18:45:09 +01:00
|
|
|
int err;
|
|
|
|
|
2014-09-10 21:58:23 +02:00
|
|
|
ASSERT(dtls != NULL);
|
|
|
|
ASSERT(dtls->sock >= 0);
|
2013-05-01 14:52:55 +02:00
|
|
|
ASSERT(buffer != NULL);
|
|
|
|
ASSERT(size > 0);
|
|
|
|
|
2014-09-10 21:58:23 +02:00
|
|
|
if (!dtls->enable) {
|
2014-12-27 18:45:09 +01:00
|
|
|
err = capwap_sendto(dtls->sock, buffer, size, &dtls->peeraddr);
|
|
|
|
if (err <= 0) {
|
2016-03-30 14:47:57 +02:00
|
|
|
log_printf(LOG_WARNING, "Unable to send plain packet, sentto return error %d", err);
|
2014-12-27 18:45:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return err;
|
2013-05-01 14:52:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Valid DTLS status */
|
|
|
|
if (dtls->action != CAPWAP_DTLS_ACTION_DATA) {
|
2014-12-27 18:45:09 +01:00
|
|
|
return -ENOTCONN;
|
2013-05-01 14:52:55 +02:00
|
|
|
}
|
|
|
|
|
2016-03-07 15:32:36 +01:00
|
|
|
return wolfSSL_write((WOLFSSL*)dtls->sslsession, buffer, size);
|
2013-05-01 14:52:55 +02:00
|
|
|
}
|
|
|
|
|
2013-05-27 21:33:23 +02:00
|
|
|
/* */
|
2014-09-10 21:58:23 +02:00
|
|
|
int capwap_crypt_sendto_fragmentpacket(struct capwap_dtls* dtls, struct capwap_list* fragmentlist) {
|
2014-12-27 18:45:09 +01:00
|
|
|
int err;
|
2013-05-27 21:33:23 +02:00
|
|
|
struct capwap_list_item* item;
|
|
|
|
|
2014-09-10 21:58:23 +02:00
|
|
|
ASSERT(dtls != NULL);
|
|
|
|
ASSERT(dtls->sock >= 0);
|
2013-05-27 21:33:23 +02:00
|
|
|
ASSERT(fragmentlist != NULL);
|
|
|
|
|
2014-12-27 18:45:09 +01:00
|
|
|
/* */
|
|
|
|
if (!dtls->enable) {
|
|
|
|
return capwap_sendto_fragmentpacket(dtls->sock, fragmentlist, &dtls->peeraddr);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* */
|
2013-05-27 21:33:23 +02:00
|
|
|
item = fragmentlist->first;
|
|
|
|
while (item) {
|
|
|
|
struct capwap_fragment_packet_item* fragmentpacket = (struct capwap_fragment_packet_item*)item->item;
|
|
|
|
ASSERT(fragmentpacket != NULL);
|
|
|
|
ASSERT(fragmentpacket->offset > 0);
|
|
|
|
|
2014-12-27 18:45:09 +01:00
|
|
|
err = capwap_crypt_sendto(dtls, fragmentpacket->buffer, fragmentpacket->offset);
|
|
|
|
if (err <= 0) {
|
2016-03-30 14:47:57 +02:00
|
|
|
log_printf(LOG_WARNING, "Unable to send crypt fragment, sentto return error %d", err);
|
2013-05-27 21:33:23 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* */
|
|
|
|
item = item->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2013-05-01 14:52:55 +02:00
|
|
|
/* */
|
|
|
|
int capwap_decrypt_packet(struct capwap_dtls* dtls, void* encrybuffer, int size, void* plainbuffer, int maxsize) {
|
|
|
|
int sslerror;
|
|
|
|
int result = -1;
|
|
|
|
char* clone = NULL;
|
|
|
|
|
|
|
|
ASSERT(dtls != NULL);
|
|
|
|
ASSERT(dtls->enable != 0);
|
|
|
|
ASSERT((dtls->action == CAPWAP_DTLS_ACTION_HANDSHAKE) || (dtls->action == CAPWAP_DTLS_ACTION_DATA));
|
|
|
|
ASSERT(dtls->buffer == NULL);
|
|
|
|
ASSERT(dtls->length == 0);
|
|
|
|
ASSERT(encrybuffer != NULL);
|
|
|
|
ASSERT(size > 0);
|
|
|
|
ASSERT(maxsize > 0);
|
|
|
|
|
|
|
|
/* */
|
|
|
|
if (!plainbuffer) {
|
|
|
|
clone = capwap_clone(encrybuffer, size);
|
|
|
|
}
|
2014-09-10 21:58:23 +02:00
|
|
|
|
2013-05-01 14:52:55 +02:00
|
|
|
dtls->buffer = (clone ? clone : encrybuffer);
|
|
|
|
dtls->length = size;
|
|
|
|
|
|
|
|
/* */
|
|
|
|
if (dtls->action == CAPWAP_DTLS_ACTION_HANDSHAKE) {
|
|
|
|
if (capwap_crypt_handshake(dtls) == CAPWAP_HANDSHAKE_ERROR) {
|
|
|
|
result = CAPWAP_ERROR_CLOSE; /* Error handshake */
|
|
|
|
} else {
|
|
|
|
result = CAPWAP_ERROR_AGAIN; /* Don't parsing DTLS packet */
|
|
|
|
}
|
|
|
|
} else if (dtls->action == CAPWAP_DTLS_ACTION_DATA) {
|
2016-03-07 15:32:36 +01:00
|
|
|
result = wolfSSL_read((WOLFSSL*)dtls->sslsession, (plainbuffer ? plainbuffer : encrybuffer), maxsize);
|
2013-05-01 14:52:55 +02:00
|
|
|
if (!result) {
|
2014-05-15 21:43:21 +02:00
|
|
|
dtls->action = CAPWAP_DTLS_ACTION_SHUTDOWN;
|
|
|
|
result = CAPWAP_ERROR_SHUTDOWN;
|
2013-05-01 14:52:55 +02:00
|
|
|
} else if (result < 0) {
|
|
|
|
/* Check error */
|
2016-03-07 15:32:36 +01:00
|
|
|
sslerror = wolfSSL_get_error((WOLFSSL*)dtls->sslsession, 0);
|
2013-05-01 14:52:55 +02:00
|
|
|
if ((sslerror == SSL_ERROR_WANT_READ) || (sslerror == SSL_ERROR_WANT_WRITE)) {
|
|
|
|
result = CAPWAP_ERROR_AGAIN; /* DTLS Renegotiation */
|
|
|
|
} else {
|
|
|
|
result = CAPWAP_ERROR_CLOSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Verify BIO read */
|
|
|
|
ASSERT(dtls->buffer == NULL);
|
|
|
|
ASSERT(dtls->length == 0);
|
|
|
|
|
|
|
|
/* Free clone */
|
|
|
|
if (clone) {
|
|
|
|
capwap_free(clone);
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
2013-10-21 18:44:37 +02:00
|
|
|
|
|
|
|
/* */
|
|
|
|
#define SIZEOF_DTLS_LAYERS 14
|
|
|
|
#define DTLS_RECORD_LAYER_HANDSHAKE_CONTENT_TYPE 22
|
|
|
|
#define DTLS_1_0_VERSION 0xfeff
|
|
|
|
#define DTLS_1_2_VERSION 0xfefd
|
|
|
|
#define DTLS_HANDSHAKE_LAYER_CLIENT_HELLO 1
|
|
|
|
|
|
|
|
/* */
|
2014-05-15 21:43:21 +02:00
|
|
|
int capwap_crypt_has_dtls_clienthello(void* buffer, int buffersize) {
|
2013-10-21 18:44:37 +02:00
|
|
|
unsigned char* dtlsdata = (unsigned char*)buffer;
|
|
|
|
|
|
|
|
/* Read DTLS packet in RAW mode */
|
|
|
|
if ((buffer != NULL) && (buffersize > SIZEOF_DTLS_LAYERS)) {
|
|
|
|
if (dtlsdata[0] == DTLS_RECORD_LAYER_HANDSHAKE_CONTENT_TYPE) {
|
|
|
|
uint16_t version = ntohs(*(uint16_t*)(dtlsdata + 1));
|
|
|
|
if (((version == DTLS_1_0_VERSION) || (version == DTLS_1_2_VERSION)) && (dtlsdata[13] == DTLS_HANDSHAKE_LAYER_CLIENT_HELLO)) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|