aboutsummaryrefslogtreecommitdiffstats
path: root/src/libtls
diff options
context:
space:
mode:
authorAndreas Steffen <andreas.steffen@strongswan.org>2010-08-12 23:56:44 +0200
committerAndreas Steffen <andreas.steffen@strongswan.org>2010-08-12 23:58:54 +0200
commit1327839da8e92c101dbe160d6e82d83b5ed6e788 (patch)
tree87136a6eae9a99ae5b58eb8f8fac3297ccc3a7ee /src/libtls
parent123a84d3dba9c5e88f101aab222db44e25db5a4a (diff)
downloadstrongswan-1327839da8e92c101dbe160d6e82d83b5ed6e788.tar.bz2
strongswan-1327839da8e92c101dbe160d6e82d83b5ed6e788.tar.xz
added generic TLS application data handler and specific EAP-TTLS instantiation
Diffstat (limited to 'src/libtls')
-rw-r--r--src/libtls/Makefile.am2
-rw-r--r--src/libtls/tls.c13
-rw-r--r--src/libtls/tls.h6
-rw-r--r--src/libtls/tls_application.h64
-rw-r--r--src/libtls/tls_fragmentation.c96
-rw-r--r--src/libtls/tls_fragmentation.h5
-rw-r--r--src/libtls/tls_handshake.h7
-rw-r--r--src/libtls/tls_peer.c7
-rw-r--r--src/libtls/tls_server.c7
9 files changed, 181 insertions, 26 deletions
diff --git a/src/libtls/Makefile.am b/src/libtls/Makefile.am
index d61cd8477..5e112f601 100644
--- a/src/libtls/Makefile.am
+++ b/src/libtls/Makefile.am
@@ -12,4 +12,4 @@ libtls_la_SOURCES = \
tls_writer.h tls_writer.c \
tls_peer.h tls_peer.c \
tls_server.h tls_server.c \
- tls_handshake.h tls.h tls.c
+ tls_handshake.h tls_application.h tls.h tls.c
diff --git a/src/libtls/tls.c b/src/libtls/tls.c
index f8f7e848e..24f442ca9 100644
--- a/src/libtls/tls.c
+++ b/src/libtls/tls.c
@@ -110,6 +110,11 @@ struct private_tls_t {
* TLS handshake protocol handler
*/
tls_handshake_t *handshake;
+
+ /**
+ * TLS application data handler
+ */
+ tls_application_t *application;
};
METHOD(tls_t, process, status_t,
@@ -164,6 +169,7 @@ METHOD(tls_t, destroy, void,
this->handshake->destroy(this->handshake);
this->peer->destroy(this->peer);
this->server->destroy(this->server);
+ DESTROY_IF(this->application);
free(this);
}
@@ -172,7 +178,8 @@ METHOD(tls_t, destroy, void,
* See header
*/
tls_t *tls_create(bool is_server, identification_t *server,
- identification_t *peer, char *msk_label)
+ identification_t *peer, char *msk_label,
+ tls_application_t *application)
{
private_tls_t *this;
@@ -191,6 +198,7 @@ tls_t *tls_create(bool is_server, identification_t *server,
.version = TLS_1_2,
.server = server->clone(server),
.peer = peer->clone(peer),
+ .application = application,
);
this->crypto = tls_crypto_create(&this->public, msk_label);
@@ -204,7 +212,8 @@ tls_t *tls_create(bool is_server, identification_t *server,
this->handshake = &tls_peer_create(&this->public, this->crypto,
this->peer, this->server)->handshake;
}
- this->fragmentation = tls_fragmentation_create(this->handshake);
+ this->fragmentation = tls_fragmentation_create(this->handshake,
+ this->application);
this->compression = tls_compression_create(this->fragmentation);
this->protection = tls_protection_create(&this->public, this->compression);
this->crypto->set_protection(this->crypto, this->protection);
diff --git a/src/libtls/tls.h b/src/libtls/tls.h
index 923c87ae1..ea66b7661 100644
--- a/src/libtls/tls.h
+++ b/src/libtls/tls.h
@@ -33,6 +33,8 @@ typedef struct tls_t tls_t;
#include <library.h>
+#include "tls_application.h"
+
/**
* TLS/SSL version numbers
*/
@@ -163,9 +165,11 @@ struct tls_t {
* @param server server identity
* @param peer peer identity
* @param msk_label ASCII string constant used as seed for MSK PRF
+ * @param application higher layer application or NULL if none
* @return TLS stack
*/
tls_t *tls_create(bool is_server, identification_t *server,
- identification_t *peer, char *msk_label);
+ identification_t *peer, char *msk_label,
+ tls_application_t *application);
#endif /** TLS_H_ @}*/
diff --git a/src/libtls/tls_application.h b/src/libtls/tls_application.h
new file mode 100644
index 000000000..dacd10ef7
--- /dev/null
+++ b/src/libtls/tls_application.h
@@ -0,0 +1,64 @@
+/*
+ * Copyright (C) 2010 Andreas Steffen
+ * Copyright (C) 2010 HSR Hochschule fuer Technik Rapperswil
+ *
+ * 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.
+ */
+
+/**
+ * @defgroup tls_handshake tls_handshake
+ * @{ @ingroup libtls
+ */
+
+#ifndef TLS_APPLICATION_H_
+#define TLS_APPLICATION_H_
+
+typedef struct tls_application_t tls_application_t;
+
+#include "tls.h"
+#include "tls_reader.h"
+#include "tls_writer.h"
+
+/**
+ * TLS application data interface.
+ */
+struct tls_application_t {
+
+ /**
+ * Process received TLS application data.
+ *
+ * @param reader TLS data buffer
+ * @return
+ * - SUCCESS if application completed
+ * - FAILED if application data processing failed
+ * - NEED_MORE if another invocation of process/build needed
+ */
+ status_t (*process)(tls_application_t *this, tls_reader_t *reader);
+
+ /**
+ * Build TLS application data to send out.
+ *
+ * @param writer TLS data buffer to write to
+ * @return
+ * - SUCCESS if application completed
+ * - FAILED if application data build failed
+ * - NEED_MORE if more data ready for delivery
+ * - INVALID_STATE if more input to process() required
+ */
+ status_t (*build)(tls_application_t *this, tls_writer_t *writer);
+
+ /**
+ * Destroy a tls_application_t.
+ */
+ void (*destroy)(tls_application_t *this);
+};
+
+#endif /** TLS_APPLICATION_H_ @}*/
diff --git a/src/libtls/tls_fragmentation.c b/src/libtls/tls_fragmentation.c
index f95fe4f2e..835a3c9f5 100644
--- a/src/libtls/tls_fragmentation.c
+++ b/src/libtls/tls_fragmentation.c
@@ -55,6 +55,11 @@ struct private_tls_fragmentation_t {
* Handshake output buffer
*/
chunk_t output;
+
+ /**
+ * Upper layer application data protocol
+ */
+ tls_application_t *application;
};
/**
@@ -133,6 +138,33 @@ static status_t process_handshake(private_tls_fragmentation_t *this,
return NEED_MORE;
}
+/**
+ * Process TLS application data
+ */
+static status_t process_application(private_tls_fragmentation_t *this,
+ tls_reader_t *reader)
+{
+ while (reader->remaining(reader))
+ {
+ u_int32_t len;
+ chunk_t data;
+
+ if (reader->remaining(reader) > MAX_TLS_FRAGMENT_LEN)
+ {
+ DBG1(DBG_IKE, "TLS fragment has invalid length");
+ return FAILED;
+ }
+
+ len = reader->remaining(reader);
+ if (!reader->read_data(reader, len, &data))
+ {
+ return FAILED;
+ }
+ DBG1(DBG_IKE, "received TLS application data: %B", &data);
+ }
+ return NEED_MORE;
+}
+
METHOD(tls_fragmentation_t, process, status_t,
private_tls_fragmentation_t *this, tls_content_type_t type, chunk_t data)
{
@@ -158,8 +190,7 @@ METHOD(tls_fragmentation_t, process, status_t,
status = process_handshake(this, reader);
break;
case TLS_APPLICATION_DATA:
- /* skip application data */
- status = NEED_MORE;
+ status = process_application(this, reader);
break;
default:
DBG1(DBG_IKE, "received unknown TLS content type %d, ignored", type);
@@ -175,7 +206,7 @@ METHOD(tls_fragmentation_t, build, status_t,
{
tls_handshake_type_t hs_type;
tls_writer_t *writer, *msg;
- status_t status;
+ status_t status = INVALID_STATE;
if (this->handshake->cipherspec_changed(this->handshake))
{
@@ -187,27 +218,47 @@ METHOD(tls_fragmentation_t, build, status_t,
if (!this->output.len)
{
msg = tls_writer_create(64);
- do
+
+ if (this->handshake->finished(this->handshake))
{
- writer = tls_writer_create(64);
- status = this->handshake->build(this->handshake, &hs_type, writer);
- switch (status)
+ if (this->application)
{
- case NEED_MORE:
- DBG2(DBG_IKE, "sending TLS %N message",
- tls_handshake_type_names, hs_type);
- msg->write_uint8(msg, hs_type);
- msg->write_data24(msg, writer->get_buf(writer));
- break;
- case INVALID_STATE:
+ status = this->application->build(this->application, msg);
+ if (status == INVALID_STATE)
+ {
this->output = chunk_clone(msg->get_buf(msg));
- break;
- default:
- break;
+ if (this->output.len)
+ {
+ DBG2(DBG_IKE, "sending TLS application data: %B",
+ &this->output);
+ }
+ }
+ }
+ }
+ else
+ {
+ do
+ {
+ writer = tls_writer_create(64);
+ status = this->handshake->build(this->handshake, &hs_type, writer);
+ switch (status)
+ {
+ case NEED_MORE:
+ DBG2(DBG_IKE, "sending TLS %N message",
+ tls_handshake_type_names, hs_type);
+ msg->write_uint8(msg, hs_type);
+ msg->write_data24(msg, writer->get_buf(writer));
+ break;
+ case INVALID_STATE:
+ this->output = chunk_clone(msg->get_buf(msg));
+ break;
+ default:
+ break;
+ }
+ writer->destroy(writer);
}
- writer->destroy(writer);
+ while (status == NEED_MORE);
}
- while (status == NEED_MORE);
msg->destroy(msg);
if (status != INVALID_STATE)
@@ -218,7 +269,8 @@ METHOD(tls_fragmentation_t, build, status_t,
if (this->output.len)
{
- *type = TLS_HANDSHAKE;
+ *type = this->handshake->finished(this->handshake) ?
+ TLS_APPLICATION_DATA : TLS_HANDSHAKE;
if (this->output.len <= MAX_TLS_FRAGMENT_LEN)
{
*data = this->output;
@@ -243,7 +295,8 @@ METHOD(tls_fragmentation_t, destroy, void,
/**
* See header
*/
-tls_fragmentation_t *tls_fragmentation_create(tls_handshake_t *handshake)
+tls_fragmentation_t *tls_fragmentation_create(tls_handshake_t *handshake,
+ tls_application_t *application)
{
private_tls_fragmentation_t *this;
@@ -254,6 +307,7 @@ tls_fragmentation_t *tls_fragmentation_create(tls_handshake_t *handshake)
.destroy = _destroy,
},
.handshake = handshake,
+ .application = application,
);
return &this->public;
diff --git a/src/libtls/tls_fragmentation.h b/src/libtls/tls_fragmentation.h
index e141a334b..6adbc36d0 100644
--- a/src/libtls/tls_fragmentation.h
+++ b/src/libtls/tls_fragmentation.h
@@ -27,6 +27,7 @@ typedef struct tls_fragmentation_t tls_fragmentation_t;
#include "tls.h"
#include "tls_handshake.h"
+#include "tls_handshake.h"
/**
* TLS record protocol fragmentation layer.
@@ -70,8 +71,10 @@ struct tls_fragmentation_t {
* Create a tls_fragmentation instance.
*
* @param handshake upper layer handshake protocol
+ * @param application upper layer application data or NULL
* @return TLS fragmentation layer.
*/
-tls_fragmentation_t *tls_fragmentation_create(tls_handshake_t *handshake);
+tls_fragmentation_t *tls_fragmentation_create(tls_handshake_t *handshake,
+ tls_application_t *application);
#endif /** TLS_FRAGMENTATION_H_ @}*/
diff --git a/src/libtls/tls_handshake.h b/src/libtls/tls_handshake.h
index c0798625e..3aab3c527 100644
--- a/src/libtls/tls_handshake.h
+++ b/src/libtls/tls_handshake.h
@@ -74,6 +74,13 @@ struct tls_handshake_t {
bool (*change_cipherspec)(tls_handshake_t *this);
/**
+ * Check if the finished message was decoded successfully.
+ *
+ * @return TRUE if finished message was decoded successfully
+ */
+ bool (*finished)(tls_handshake_t *this);
+
+ /**
* Destroy a tls_handshake_t.
*/
void (*destroy)(tls_handshake_t *this);
diff --git a/src/libtls/tls_peer.c b/src/libtls/tls_peer.c
index 221b629a5..79f97ae40 100644
--- a/src/libtls/tls_peer.c
+++ b/src/libtls/tls_peer.c
@@ -621,6 +621,12 @@ METHOD(tls_handshake_t, change_cipherspec, bool,
return FALSE;
}
+METHOD(tls_handshake_t, finished, bool,
+ private_tls_peer_t *this)
+{
+ return this->state == STATE_COMPLETE;
+}
+
METHOD(tls_handshake_t, destroy, void,
private_tls_peer_t *this)
{
@@ -644,6 +650,7 @@ tls_peer_t *tls_peer_create(tls_t *tls, tls_crypto_t *crypto,
.build = _build,
.cipherspec_changed = _cipherspec_changed,
.change_cipherspec = _change_cipherspec,
+ .finished = _finished,
.destroy = _destroy,
},
.state = STATE_INIT,
diff --git a/src/libtls/tls_server.c b/src/libtls/tls_server.c
index 8d2c961ea..673b20145 100644
--- a/src/libtls/tls_server.c
+++ b/src/libtls/tls_server.c
@@ -583,6 +583,12 @@ METHOD(tls_handshake_t, change_cipherspec, bool,
return FALSE;
}
+METHOD(tls_handshake_t, finished, bool,
+ private_tls_server_t *this)
+{
+ return this->state == STATE_FINISHED_SENT;
+}
+
METHOD(tls_handshake_t, destroy, void,
private_tls_server_t *this)
{
@@ -606,6 +612,7 @@ tls_server_t *tls_server_create(tls_t *tls, tls_crypto_t *crypto,
.build = _build,
.cipherspec_changed = _cipherspec_changed,
.change_cipherspec = _change_cipherspec,
+ .finished = _finished,
.destroy = _destroy,
},
.tls = tls,