aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMartin Willi <martin@revosec.ch>2011-02-21 13:05:21 +0000
committerMartin Willi <martin@revosec.ch>2011-02-28 16:39:40 +0100
commit007c47088cb1b4837eca5aa9a119a4f39df192ec (patch)
tree878cf88f08006b48d6f245b57667e1031085ae49
parent0d6d9925894dded418c12477c28fe9812db54ee4 (diff)
downloadstrongswan-007c47088cb1b4837eca5aa9a119a4f39df192ec.tar.bz2
strongswan-007c47088cb1b4837eca5aa9a119a4f39df192ec.tar.xz
Implemented permanent certificate coupling plugin
-rw-r--r--configure.in4
-rw-r--r--src/libcharon/Makefile.am7
-rw-r--r--src/libcharon/plugins/coupling/Makefile.am16
-rw-r--r--src/libcharon/plugins/coupling/coupling_plugin.c73
-rw-r--r--src/libcharon/plugins/coupling/coupling_plugin.h42
-rw-r--r--src/libcharon/plugins/coupling/coupling_validator.c258
-rw-r--r--src/libcharon/plugins/coupling/coupling_validator.h49
7 files changed, 449 insertions, 0 deletions
diff --git a/configure.in b/configure.in
index 938afb053..009af124d 100644
--- a/configure.in
+++ b/configure.in
@@ -169,6 +169,7 @@ ARG_ENABL_SET([ha], [enable high availability cluster plugin.])
ARG_ENABL_SET([whitelist], [enable peer identity whitelisting plugin.])
ARG_ENABL_SET([led], [enable plugin to control LEDs on IKEv2 activity using the Linux kernel LED subsystem.])
ARG_ENABL_SET([duplicheck], [advanced duplicate checking plugin using liveness checks.])
+ARG_ENABL_SET([coupling], [enable IKEv2 plugin to couple peer certificates permanently to authentication.])
ARG_ENABL_SET([vstr], [enforce using the Vstr string library to replace glibc-like printf hooks.])
ARG_ENABL_SET([monolithic], [build monolithic version of libstrongswan that includes all enabled plugins. Similarly, the plugins of charon are assembled in libcharon.])
@@ -779,6 +780,7 @@ ADD_PLUGIN([ha], [c libcharon])
ADD_PLUGIN([whitelist], [c libcharon])
ADD_PLUGIN([led], [c libcharon])
ADD_PLUGIN([duplicheck], [c libcharon])
+ADD_PLUGIN([coupling], [c libcharon])
ADD_PLUGIN([maemo], [c libcharon])
ADD_PLUGIN([uci], [c libcharon])
ADD_PLUGIN([addrblock], [c libcharon])
@@ -860,6 +862,7 @@ AM_CONDITIONAL(USE_HA, test x$ha = xtrue)
AM_CONDITIONAL(USE_WHITELIST, test x$whitelist = xtrue)
AM_CONDITIONAL(USE_LED, test x$led = xtrue)
AM_CONDITIONAL(USE_DUPLICHECK, test x$duplicheck = xtrue)
+AM_CONDITIONAL(USE_COUPLING, test x$coupling = xtrue)
AM_CONDITIONAL(USE_EAP_SIM, test x$eap_sim = xtrue)
AM_CONDITIONAL(USE_EAP_SIM_FILE, test x$eap_sim_file = xtrue)
AM_CONDITIONAL(USE_EAP_SIMAKA_SQL, test x$eap_simaka_sql = xtrue)
@@ -1043,6 +1046,7 @@ AC_OUTPUT(
src/libcharon/plugins/whitelist/Makefile
src/libcharon/plugins/led/Makefile
src/libcharon/plugins/duplicheck/Makefile
+ src/libcharon/plugins/coupling/Makefile
src/libcharon/plugins/android/Makefile
src/libcharon/plugins/maemo/Makefile
src/libcharon/plugins/stroke/Makefile
diff --git a/src/libcharon/Makefile.am b/src/libcharon/Makefile.am
index 8a4f8917d..a995cae9c 100644
--- a/src/libcharon/Makefile.am
+++ b/src/libcharon/Makefile.am
@@ -424,6 +424,13 @@ if MONOLITHIC
endif
endif
+if USE_COUPLING
+ SUBDIRS += plugins/coupling
+if MONOLITHIC
+ libcharon_la_LIBADD += plugins/coupling/libstrongswan-coupling.la
+endif
+endif
+
if USE_UCI
SUBDIRS += plugins/uci
if MONOLITHIC
diff --git a/src/libcharon/plugins/coupling/Makefile.am b/src/libcharon/plugins/coupling/Makefile.am
new file mode 100644
index 000000000..642ce820c
--- /dev/null
+++ b/src/libcharon/plugins/coupling/Makefile.am
@@ -0,0 +1,16 @@
+
+INCLUDES = -I$(top_srcdir)/src/libstrongswan -I$(top_srcdir)/src/libhydra \
+ -I$(top_srcdir)/src/libcharon
+
+AM_CFLAGS = -rdynamic
+
+if MONOLITHIC
+noinst_LTLIBRARIES = libstrongswan-coupling.la
+else
+plugin_LTLIBRARIES = libstrongswan-coupling.la
+endif
+
+libstrongswan_coupling_la_SOURCES = coupling_plugin.h coupling_plugin.c \
+ coupling_validator.h coupling_validator.c
+
+libstrongswan_coupling_la_LDFLAGS = -module -avoid-version
diff --git a/src/libcharon/plugins/coupling/coupling_plugin.c b/src/libcharon/plugins/coupling/coupling_plugin.c
new file mode 100644
index 000000000..8e0705c2e
--- /dev/null
+++ b/src/libcharon/plugins/coupling/coupling_plugin.c
@@ -0,0 +1,73 @@
+/*
+ * Copyright (C) 2011 Martin Willi
+ * Copyright (C) 2011 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 "coupling_plugin.h"
+
+#include "coupling_validator.h"
+
+#include <daemon.h>
+
+typedef struct private_coupling_plugin_t private_coupling_plugin_t;
+
+/**
+ * private data of coupling plugin
+ */
+struct private_coupling_plugin_t {
+
+ /**
+ * implements plugin interface
+ */
+ coupling_plugin_t public;
+
+ /**
+ * validator controlling couplings
+ */
+ coupling_validator_t *validator;
+};
+
+METHOD(plugin_t, destroy, void,
+ private_coupling_plugin_t *this)
+{
+ lib->credmgr->remove_validator(lib->credmgr, &this->validator->validator);
+ this->validator->destroy(this->validator);
+ free(this);
+}
+
+/**
+ * Plugin constructor
+ */
+plugin_t *coupling_plugin_create()
+{
+ private_coupling_plugin_t *this;
+
+ INIT(this,
+ .public = {
+ .plugin = {
+ .destroy = _destroy,
+ },
+ },
+ .validator = coupling_validator_create(),
+ );
+
+ if (!this->validator)
+ {
+ free(this);
+ return NULL;
+ }
+
+ lib->credmgr->add_validator(lib->credmgr, &this->validator->validator);
+
+ return &this->public.plugin;
+}
diff --git a/src/libcharon/plugins/coupling/coupling_plugin.h b/src/libcharon/plugins/coupling/coupling_plugin.h
new file mode 100644
index 000000000..645100b45
--- /dev/null
+++ b/src/libcharon/plugins/coupling/coupling_plugin.h
@@ -0,0 +1,42 @@
+/*
+ * Copyright (C) 2011 Martin Willi
+ * Copyright (C) 2011 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.
+ */
+
+/**
+ * @defgroup coupling coupling
+ * @ingroup cplugins
+ *
+ * @defgroup coupling_plugin coupling_plugin
+ * @{ @ingroup coupling
+ */
+
+#ifndef COUPLING_PLUGIN_H_
+#define COUPLING_PLUGIN_H_
+
+#include <plugins/plugin.h>
+
+typedef struct coupling_plugin_t coupling_plugin_t;
+
+/**
+ * Plugin to couple peer certificates permanently to peer authentication.
+ */
+struct coupling_plugin_t {
+
+ /**
+ * implements plugin interface
+ */
+ plugin_t plugin;
+};
+
+#endif /** COUPLING_PLUGIN_H_ @}*/
diff --git a/src/libcharon/plugins/coupling/coupling_validator.c b/src/libcharon/plugins/coupling/coupling_validator.c
new file mode 100644
index 000000000..06b6f7d86
--- /dev/null
+++ b/src/libcharon/plugins/coupling/coupling_validator.c
@@ -0,0 +1,258 @@
+/*
+ * Copyright (C) 2011 Martin Willi
+ * Copyright (C) 2011 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 "coupling_validator.h"
+
+#include <errno.h>
+#include <time.h>
+
+#include <daemon.h>
+#include <threading/mutex.h>
+
+/* buffer size for hex-encoded hash */
+#define MAX_HASH_SIZE (HASH_SIZE_SHA512 * 2 + 1)
+
+typedef struct private_coupling_validator_t private_coupling_validator_t;
+
+/**
+ * Private data of an coupling_validator_t object.
+ */
+struct private_coupling_validator_t {
+
+ /**
+ * Public coupling_validator_t interface.
+ */
+ coupling_validator_t public;
+
+ /**
+ * Mutex
+ */
+ mutex_t *mutex;
+
+ /**
+ * File with device couplings
+ */
+ FILE *f;
+
+ /**
+ * Hasher to create hashes
+ */
+ hasher_t *hasher;
+
+ /**
+ * maximum number of couplings
+ */
+ int max_couplings;
+};
+
+/**
+ * Get hash of a certificate
+ */
+static bool get_cert_hash(private_coupling_validator_t *this,
+ certificate_t *cert, char *hex)
+{
+ char buf[MAX_HASH_SIZE];
+ chunk_t encoding;
+
+ if (!cert->get_encoding(cert, CERT_ASN1_DER, &encoding))
+ {
+ return FALSE;
+ }
+ this->hasher->get_hash(this->hasher, encoding, buf);
+ free(encoding.ptr);
+ chunk_to_hex(chunk_create(buf, this->hasher->get_hash_size(this->hasher)),
+ hex, FALSE);
+ return TRUE;
+}
+
+/**
+ * Check if we have an entry for a given hash
+ */
+static bool has_entry(private_coupling_validator_t *this, char *hash)
+{
+ char line[256];
+ int hash_len;
+
+ hash_len = strlen(hash);
+ rewind(this->f);
+
+ while (fgets(line, sizeof(line), this->f))
+ {
+ if (strlen(line) >= hash_len &&
+ strncaseeq(line, hash, hash_len))
+ {
+ return TRUE;
+ }
+ }
+ return FALSE;
+}
+
+/**
+ * Get the number of coupling entries we currently have
+ */
+static int get_number_of_entries(private_coupling_validator_t *this)
+{
+ char line[256];
+ int count = 0;
+
+ rewind(this->f);
+
+ while (fgets(line, sizeof(line), this->f))
+ {
+ /* valid entries start with hex encoded hash */
+ if (strchr("1234567890abcdefABCDEF", line[0]))
+ {
+ count++;
+ }
+ }
+ return count;
+}
+
+/**
+ * Add a new entry to the file
+ */
+static bool add_entry(private_coupling_validator_t *this, char *hash,
+ identification_t *id)
+{
+ return fseek(this->f, 0, SEEK_END) == 0 &&
+ fprintf(this->f, "%s %u '%Y'\n", hash, time(NULL), id) > 0;
+}
+
+METHOD(cert_validator_t, validate, bool,
+ private_coupling_validator_t *this,
+ certificate_t *subject, certificate_t *issuer,
+ bool online, u_int pathlen, bool anchor, auth_cfg_t *auth)
+{
+ bool valid = FALSE;
+ char hash[MAX_HASH_SIZE];
+
+ if (pathlen != 0)
+ {
+ return TRUE;
+ }
+ if (get_cert_hash(this, subject, hash))
+ {
+ this->mutex->lock(this->mutex);
+ if (has_entry(this, hash))
+ {
+ DBG1(DBG_CFG, "coupled certificate '%Y' found, accepted",
+ subject->get_subject(subject));
+ valid = TRUE;
+ }
+ else if (get_number_of_entries(this) < this->max_couplings)
+ {
+ if (add_entry(this, hash, subject->get_subject(subject)))
+ {
+ DBG1(DBG_CFG, "coupled new certificate '%Y'",
+ subject->get_subject(subject));
+ valid = TRUE;
+ }
+ else
+ {
+ DBG1(DBG_CFG, "coupling new certificate '%Y' failed",
+ subject->get_subject(subject));
+ }
+ }
+ else
+ {
+ DBG1(DBG_CFG, "coupling new certificate '%Y' failed, limit of %d "
+ "couplings reached", subject->get_subject(subject),
+ this->max_couplings);
+ }
+ this->mutex->unlock(this->mutex);
+ }
+ return valid;
+}
+
+METHOD(coupling_validator_t, destroy, void,
+ private_coupling_validator_t *this)
+{
+ if (this->f)
+ {
+ fclose(this->f);
+ }
+ DESTROY_IF(this->hasher);
+ this->mutex->destroy(this->mutex);
+ free(this);
+}
+
+/**
+ * See header
+ */
+coupling_validator_t *coupling_validator_create()
+{
+ private_coupling_validator_t *this;
+ char *path, *hash;
+ int i;
+ struct {
+ hash_algorithm_t alg;
+ char *name;
+ } hash_types[] = {
+ { HASH_MD5, "md5"},
+ { HASH_SHA1, "sha1"},
+ { HASH_SHA256, "sha256"},
+ { HASH_SHA384, "sha384"},
+ { HASH_SHA512, "sha512"},
+ };
+
+ INIT(this,
+ .public = {
+ .validator = {
+ .validate = _validate,
+ },
+ .destroy = _destroy,
+ },
+ .mutex = mutex_create(MUTEX_TYPE_DEFAULT),
+ .max_couplings = lib->settings->get_int(lib->settings,
+ "charon.plugins.coupling.max", 1),
+ );
+
+ hash = lib->settings->get_str(lib->settings,
+ "charon.plugins.coupling.hash", "sha1");
+ for (i = 0; i < countof(hash_types); i++)
+ {
+ if (strcaseeq(hash_types[i].name, hash))
+ {
+ this->hasher = lib->crypto->create_hasher(lib->crypto,
+ hash_types[i].alg);
+ break;
+ }
+ }
+ if (!this->hasher)
+ {
+ DBG1(DBG_CFG, "unsupported coupling hash algorithm: %s", hash);
+ destroy(this);
+ return NULL;
+ }
+
+ path = lib->settings->get_str(lib->settings,
+ "charon.plugins.coupling.file", NULL);
+ if (!path)
+ {
+ DBG1(DBG_CFG, "coupling file path unspecified");
+ destroy(this);
+ return NULL;
+ }
+ this->f = fopen(path, "a+");
+ if (!this->f)
+ {
+ DBG1(DBG_CFG, "opening coupling file '%s' failed: %s",
+ path, strerror(errno));
+ destroy(this);
+ return NULL;
+ }
+ setlinebuf(this->f);
+ return &this->public;
+}
diff --git a/src/libcharon/plugins/coupling/coupling_validator.h b/src/libcharon/plugins/coupling/coupling_validator.h
new file mode 100644
index 000000000..d6b4d3718
--- /dev/null
+++ b/src/libcharon/plugins/coupling/coupling_validator.h
@@ -0,0 +1,49 @@
+/*
+ * Copyright (C) 2011 Martin Willi
+ * Copyright (C) 2011 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.
+ */
+
+/**
+ * @defgroup coupling_validator coupling_validator
+ * @{ @ingroup coupling
+ */
+
+#ifndef COUPLING_VALIDATOR_H_
+#define COUPLING_VALIDATOR_H_
+
+#include <credentials/cert_validator.h>
+
+typedef struct coupling_validator_t coupling_validator_t;
+
+/**
+ * Validator that couples authenticated certificates permanently.
+ */
+struct coupling_validator_t {
+
+ /**
+ * Implements cert_validator_t interface.
+ */
+ cert_validator_t validator;
+
+ /**
+ * Destroy a coupling_validator_t.
+ */
+ void (*destroy)(coupling_validator_t *this);
+};
+
+/**
+ * Create a coupling_validator instance.
+ */
+coupling_validator_t *coupling_validator_create();
+
+#endif /** COUPLING_VALIDATOR_H_ @}*/