2010-01-21 14:11:38 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2010 Martin Willi
|
|
|
|
* Copyright (C) 2010 revosec AG
|
|
|
|
*
|
|
|
|
* This program 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 2 of the License, or (at your
|
|
|
|
* option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
|
|
|
|
*
|
|
|
|
* This program 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "tls.h"
|
|
|
|
|
2010-08-23 14:21:49 +00:00
|
|
|
#include <debug.h>
|
|
|
|
|
2010-01-22 16:24:17 +00:00
|
|
|
#include "tls_protection.h"
|
|
|
|
#include "tls_compression.h"
|
|
|
|
#include "tls_fragmentation.h"
|
2010-01-25 11:21:57 +00:00
|
|
|
#include "tls_crypto.h"
|
2010-01-25 09:44:35 +00:00
|
|
|
#include "tls_server.h"
|
|
|
|
#include "tls_peer.h"
|
2010-01-22 16:24:17 +00:00
|
|
|
|
2010-01-25 16:31:55 +00:00
|
|
|
ENUM_BEGIN(tls_version_names, SSL_2_0, SSL_2_0,
|
|
|
|
"SSLv2");
|
|
|
|
ENUM_NEXT(tls_version_names, SSL_3_0, TLS_1_2, SSL_2_0,
|
2010-01-21 14:11:38 +00:00
|
|
|
"SSLv3",
|
|
|
|
"TLS 1.0",
|
|
|
|
"TLS 1.1",
|
2010-01-25 16:31:55 +00:00
|
|
|
"TLS 1.2");
|
|
|
|
ENUM_END(tls_version_names, TLS_1_2);
|
2010-01-21 14:11:38 +00:00
|
|
|
|
|
|
|
ENUM(tls_content_type_names, TLS_CHANGE_CIPHER_SPEC, TLS_APPLICATION_DATA,
|
|
|
|
"ChangeCipherSpec",
|
|
|
|
"Alert",
|
|
|
|
"Handshake",
|
|
|
|
"ApplicationData",
|
|
|
|
);
|
|
|
|
|
|
|
|
ENUM_BEGIN(tls_handshake_type_names, TLS_HELLO_REQUEST, TLS_SERVER_HELLO,
|
|
|
|
"HelloRequest",
|
|
|
|
"ClientHello",
|
|
|
|
"ServerHello");
|
|
|
|
ENUM_NEXT(tls_handshake_type_names, TLS_CERTIFICATE, TLS_CLIENT_KEY_EXCHANGE, TLS_SERVER_HELLO,
|
|
|
|
"Certificate",
|
|
|
|
"ServerKeyExchange",
|
|
|
|
"CertificateRequest",
|
|
|
|
"ServerHelloDone",
|
|
|
|
"CertificateVerify",
|
|
|
|
"ClientKeyExchange");
|
|
|
|
ENUM_NEXT(tls_handshake_type_names, TLS_FINISHED, TLS_FINISHED, TLS_CLIENT_KEY_EXCHANGE,
|
|
|
|
"Finished");
|
|
|
|
ENUM_END(tls_handshake_type_names, TLS_FINISHED);
|
2010-01-22 14:35:29 +00:00
|
|
|
|
|
|
|
|
|
|
|
typedef struct private_tls_t private_tls_t;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Private data of an tls_protection_t object.
|
|
|
|
*/
|
|
|
|
struct private_tls_t {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Public tls_t interface.
|
|
|
|
*/
|
|
|
|
tls_t public;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Role this TLS stack acts as.
|
|
|
|
*/
|
|
|
|
bool is_server;
|
2010-01-22 16:24:17 +00:00
|
|
|
|
2010-02-09 13:57:50 +00:00
|
|
|
/**
|
|
|
|
* Server identity
|
|
|
|
*/
|
|
|
|
identification_t *server;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Peer identity
|
|
|
|
*/
|
|
|
|
identification_t *peer;
|
|
|
|
|
2010-01-25 16:31:55 +00:00
|
|
|
/**
|
|
|
|
* Negotiated TLS version
|
|
|
|
*/
|
|
|
|
tls_version_t version;
|
|
|
|
|
2010-08-20 13:02:25 +00:00
|
|
|
/**
|
|
|
|
* TLS stack purpose, as given to constructor
|
|
|
|
*/
|
|
|
|
tls_purpose_t purpose;
|
|
|
|
|
2010-01-22 16:24:17 +00:00
|
|
|
/**
|
|
|
|
* TLS record protection layer
|
|
|
|
*/
|
|
|
|
tls_protection_t *protection;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* TLS record compression layer
|
|
|
|
*/
|
|
|
|
tls_compression_t *compression;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* TLS record fragmentation layer
|
|
|
|
*/
|
|
|
|
tls_fragmentation_t *fragmentation;
|
2010-01-25 09:44:35 +00:00
|
|
|
|
2010-08-23 12:22:38 +00:00
|
|
|
/**
|
|
|
|
* TLS alert handler
|
|
|
|
*/
|
|
|
|
tls_alert_t *alert;
|
|
|
|
|
2010-01-25 11:21:57 +00:00
|
|
|
/**
|
|
|
|
* TLS crypto helper context
|
|
|
|
*/
|
|
|
|
tls_crypto_t *crypto;
|
|
|
|
|
2010-01-25 09:44:35 +00:00
|
|
|
/**
|
|
|
|
* TLS handshake protocol handler
|
|
|
|
*/
|
|
|
|
tls_handshake_t *handshake;
|
2010-08-12 21:56:44 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* TLS application data handler
|
|
|
|
*/
|
|
|
|
tls_application_t *application;
|
2010-01-22 14:35:29 +00:00
|
|
|
};
|
|
|
|
|
2010-08-23 14:21:49 +00:00
|
|
|
/**
|
|
|
|
* TLS record
|
|
|
|
*/
|
|
|
|
typedef struct __attribute__((packed)) {
|
|
|
|
u_int8_t type;
|
|
|
|
u_int16_t version;
|
|
|
|
u_int16_t length;
|
|
|
|
char data[];
|
|
|
|
} tls_record_t;
|
|
|
|
|
2010-01-22 14:35:29 +00:00
|
|
|
METHOD(tls_t, process, status_t,
|
2010-08-23 14:21:49 +00:00
|
|
|
private_tls_t *this, chunk_t data)
|
2010-01-22 14:35:29 +00:00
|
|
|
{
|
2010-08-23 14:21:49 +00:00
|
|
|
tls_record_t *record;
|
|
|
|
u_int16_t len;
|
|
|
|
status_t status;
|
|
|
|
|
|
|
|
while (data.len > sizeof(tls_record_t))
|
|
|
|
{
|
|
|
|
record = (tls_record_t*)data.ptr;
|
|
|
|
len = untoh16(&record->length);
|
|
|
|
|
|
|
|
DBG2(DBG_TLS, "received TLS %N record (%u bytes)",
|
|
|
|
tls_content_type_names, record->type, sizeof(tls_record_t) + len);
|
|
|
|
if (len > data.len - sizeof(tls_record_t))
|
|
|
|
{
|
|
|
|
DBG1(DBG_IKE, "TLS record length invalid");
|
|
|
|
return FAILED;
|
|
|
|
}
|
|
|
|
status = this->protection->process(this->protection, record->type,
|
|
|
|
chunk_create(record->data, len));
|
|
|
|
if (status != NEED_MORE)
|
|
|
|
{
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
data = chunk_skip(data, len + sizeof(tls_record_t));
|
|
|
|
}
|
|
|
|
|
|
|
|
return NEED_MORE;
|
2010-01-22 14:35:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
METHOD(tls_t, build, status_t,
|
2010-08-23 14:21:49 +00:00
|
|
|
private_tls_t *this, chunk_t *data)
|
2010-01-22 14:35:29 +00:00
|
|
|
{
|
2010-08-23 14:21:49 +00:00
|
|
|
tls_record_t record;
|
|
|
|
status_t status;
|
|
|
|
|
2010-08-25 10:43:21 +00:00
|
|
|
*data = chunk_empty;
|
2010-08-23 14:21:49 +00:00
|
|
|
while (TRUE)
|
|
|
|
{
|
|
|
|
tls_content_type_t type;
|
|
|
|
chunk_t body;
|
|
|
|
|
|
|
|
status = this->protection->build(this->protection, &type, &body);
|
|
|
|
switch (status)
|
|
|
|
{
|
|
|
|
case INVALID_STATE:
|
|
|
|
return NEED_MORE;
|
|
|
|
case NEED_MORE:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
record.type = type;
|
|
|
|
htoun16(&record.version, this->version);
|
|
|
|
htoun16(&record.length, body.len);
|
|
|
|
*data = chunk_cat("mcm", *data, chunk_from_thing(record), body);
|
|
|
|
DBG2(DBG_TLS, "sending TLS %N record (%u bytes)",
|
|
|
|
tls_content_type_names, type, sizeof(tls_record_t) + body.len);
|
|
|
|
}
|
2010-01-22 14:35:29 +00:00
|
|
|
}
|
|
|
|
|
2010-02-04 17:18:10 +00:00
|
|
|
METHOD(tls_t, is_server, bool,
|
|
|
|
private_tls_t *this)
|
|
|
|
{
|
|
|
|
return this->is_server;
|
|
|
|
}
|
|
|
|
|
2010-01-25 16:31:55 +00:00
|
|
|
METHOD(tls_t, get_version, tls_version_t,
|
|
|
|
private_tls_t *this)
|
|
|
|
{
|
|
|
|
return this->version;
|
|
|
|
}
|
|
|
|
|
2010-08-20 14:08:59 +00:00
|
|
|
METHOD(tls_t, set_version, bool,
|
2010-01-25 16:31:55 +00:00
|
|
|
private_tls_t *this, tls_version_t version)
|
|
|
|
{
|
2010-08-20 14:08:59 +00:00
|
|
|
if (version > this->version)
|
|
|
|
{
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
switch (version)
|
|
|
|
{
|
|
|
|
case TLS_1_0:
|
|
|
|
case TLS_1_1:
|
|
|
|
case TLS_1_2:
|
|
|
|
this->version = version;
|
2010-08-23 12:22:38 +00:00
|
|
|
this->protection->set_version(this->protection, version);
|
2010-08-20 14:08:59 +00:00
|
|
|
return TRUE;
|
|
|
|
case SSL_2_0:
|
|
|
|
case SSL_3_0:
|
|
|
|
default:
|
|
|
|
return FALSE;
|
|
|
|
}
|
2010-01-25 16:31:55 +00:00
|
|
|
}
|
|
|
|
|
2010-08-20 13:02:25 +00:00
|
|
|
METHOD(tls_t, get_purpose, tls_purpose_t,
|
|
|
|
private_tls_t *this)
|
|
|
|
{
|
|
|
|
return this->purpose;
|
|
|
|
}
|
|
|
|
|
2010-02-09 17:19:25 +00:00
|
|
|
METHOD(tls_t, is_complete, bool,
|
|
|
|
private_tls_t *this)
|
|
|
|
{
|
2010-08-24 06:40:28 +00:00
|
|
|
if (this->handshake->finished(this->handshake))
|
|
|
|
{
|
|
|
|
if (!this->application)
|
|
|
|
{
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
return this->fragmentation->application_finished(this->fragmentation);
|
|
|
|
}
|
|
|
|
return FALSE;
|
2010-02-09 17:19:25 +00:00
|
|
|
}
|
|
|
|
|
2010-02-05 11:28:48 +00:00
|
|
|
METHOD(tls_t, get_eap_msk, chunk_t,
|
|
|
|
private_tls_t *this)
|
|
|
|
{
|
|
|
|
return this->crypto->get_eap_msk(this->crypto);
|
|
|
|
}
|
|
|
|
|
2010-01-22 14:35:29 +00:00
|
|
|
METHOD(tls_t, destroy, void,
|
|
|
|
private_tls_t *this)
|
|
|
|
{
|
2010-01-22 16:24:17 +00:00
|
|
|
this->protection->destroy(this->protection);
|
|
|
|
this->compression->destroy(this->compression);
|
|
|
|
this->fragmentation->destroy(this->fragmentation);
|
2010-01-25 11:21:57 +00:00
|
|
|
this->crypto->destroy(this->crypto);
|
2010-01-25 09:44:35 +00:00
|
|
|
this->handshake->destroy(this->handshake);
|
2010-08-24 09:34:43 +00:00
|
|
|
DESTROY_IF(this->peer);
|
2010-02-09 13:57:50 +00:00
|
|
|
this->server->destroy(this->server);
|
2010-08-12 21:56:44 +00:00
|
|
|
DESTROY_IF(this->application);
|
2010-08-23 12:22:38 +00:00
|
|
|
this->alert->destroy(this->alert);
|
2010-01-22 16:24:17 +00:00
|
|
|
|
2010-01-22 14:35:29 +00:00
|
|
|
free(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* See header
|
|
|
|
*/
|
2010-02-03 18:53:40 +00:00
|
|
|
tls_t *tls_create(bool is_server, identification_t *server,
|
2010-08-20 13:02:25 +00:00
|
|
|
identification_t *peer, tls_purpose_t purpose,
|
|
|
|
tls_application_t *application)
|
2010-01-22 14:35:29 +00:00
|
|
|
{
|
|
|
|
private_tls_t *this;
|
|
|
|
|
2010-08-20 13:02:25 +00:00
|
|
|
switch (purpose)
|
|
|
|
{
|
|
|
|
case TLS_PURPOSE_EAP_TLS:
|
|
|
|
case TLS_PURPOSE_EAP_TTLS:
|
2010-08-25 10:51:01 +00:00
|
|
|
case TLS_PURPOSE_GENERIC:
|
2010-08-20 13:02:25 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2010-01-22 14:35:29 +00:00
|
|
|
INIT(this,
|
|
|
|
.public = {
|
|
|
|
.process = _process,
|
|
|
|
.build = _build,
|
2010-02-04 17:18:10 +00:00
|
|
|
.is_server = _is_server,
|
2010-01-25 16:31:55 +00:00
|
|
|
.get_version = _get_version,
|
|
|
|
.set_version = _set_version,
|
2010-08-20 13:02:25 +00:00
|
|
|
.get_purpose = _get_purpose,
|
2010-02-09 17:19:25 +00:00
|
|
|
.is_complete = _is_complete,
|
2010-02-05 11:28:48 +00:00
|
|
|
.get_eap_msk = _get_eap_msk,
|
2010-01-22 14:35:29 +00:00
|
|
|
.destroy = _destroy,
|
|
|
|
},
|
|
|
|
.is_server = is_server,
|
2010-01-25 16:31:55 +00:00
|
|
|
.version = TLS_1_2,
|
2010-02-09 13:57:50 +00:00
|
|
|
.server = server->clone(server),
|
2010-08-24 09:34:43 +00:00
|
|
|
.peer = peer ? peer->clone(peer) : NULL,
|
2010-08-12 21:56:44 +00:00
|
|
|
.application = application,
|
2010-08-20 13:02:25 +00:00
|
|
|
.purpose = purpose,
|
2010-01-22 14:35:29 +00:00
|
|
|
);
|
|
|
|
|
2010-08-20 13:02:25 +00:00
|
|
|
this->crypto = tls_crypto_create(&this->public);
|
2010-08-23 12:22:38 +00:00
|
|
|
this->alert = tls_alert_create();
|
2010-01-25 09:44:35 +00:00
|
|
|
if (is_server)
|
|
|
|
{
|
2010-02-03 18:53:40 +00:00
|
|
|
this->handshake = &tls_server_create(&this->public, this->crypto,
|
2010-08-23 12:22:38 +00:00
|
|
|
this->alert, this->server, this->peer)->handshake;
|
2010-01-25 09:44:35 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2010-02-03 18:53:40 +00:00
|
|
|
this->handshake = &tls_peer_create(&this->public, this->crypto,
|
2010-08-23 12:22:38 +00:00
|
|
|
this->alert, this->peer, this->server)->handshake;
|
2010-01-25 09:44:35 +00:00
|
|
|
}
|
2010-08-23 12:22:38 +00:00
|
|
|
this->fragmentation = tls_fragmentation_create(this->handshake, this->alert,
|
2010-08-12 21:56:44 +00:00
|
|
|
this->application);
|
2010-08-23 12:22:38 +00:00
|
|
|
this->compression = tls_compression_create(this->fragmentation, this->alert);
|
|
|
|
this->protection = tls_protection_create(this->compression, this->alert);
|
2010-02-05 13:39:19 +00:00
|
|
|
this->crypto->set_protection(this->crypto, this->protection);
|
2010-01-22 16:24:17 +00:00
|
|
|
|
2010-01-22 14:35:29 +00:00
|
|
|
return &this->public;
|
|
|
|
}
|