Define an interface for SASL mechanisms and provide a static factory
authorMartin Willi <martin@revosec.ch>
Tue, 26 Feb 2013 13:54:36 +0000 (14:54 +0100)
committerMartin Willi <martin@revosec.ch>
Thu, 28 Feb 2013 15:46:07 +0000 (16:46 +0100)
src/libpttls/Makefile.am
src/libpttls/sasl/sasl_mechanism.c [new file with mode: 0644]
src/libpttls/sasl/sasl_mechanism.h [new file with mode: 0644]

index d19af03..b019d23 100644 (file)
@@ -7,4 +7,5 @@ libpttls_la_LIBADD = $(top_builddir)/src/libtls/libtls.la
 libpttls_la_SOURCES = pt_tls.c pt_tls.h \
        pt_tls_client.c pt_tls_client.h \
        pt_tls_server.c pt_tls_server.h \
-       pt_tls_dispatcher.c pt_tls_dispatcher.h
+       pt_tls_dispatcher.c pt_tls_dispatcher.h \
+       sasl/sasl_mechanism.c sasl/sasl_mechanism.h
diff --git a/src/libpttls/sasl/sasl_mechanism.c b/src/libpttls/sasl/sasl_mechanism.c
new file mode 100644 (file)
index 0000000..4e0f876
--- /dev/null
@@ -0,0 +1,88 @@
+/*
+ * Copyright (C) 2013 Martin Willi
+ * Copyright (C) 2013 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 "sasl_mechanism.h"
+
+/**
+ * Available SASL mechanisms.
+ */
+static struct {
+       char *name;
+       bool server;
+       sasl_mechanism_constructor_t create;
+} mechs[] = {
+};
+
+/**
+ * See header.
+ */
+sasl_mechanism_t *sasl_mechanism_create(char *name, identification_t *client)
+{
+       int i;
+
+       for (i = 0; i < countof(mechs); i++)
+       {
+               if (streq(mechs[i].name, name) && mechs[i].server == (client == NULL))
+               {
+                       return mechs[i].create(name, client);
+               }
+       }
+       return NULL;
+}
+
+/**
+ * SASL mechanism enumerator
+ */
+typedef struct {
+       /** implements enumerator_t */
+       enumerator_t public;
+       /** looking for client or server? */
+       bool server;
+       /** position in mechs[] */
+       int i;
+} mech_enumerator_t;
+
+METHOD(enumerator_t, mech_enumerate, bool,
+       mech_enumerator_t *this, char **name)
+{
+       while (this->i < countof(mechs))
+       {
+               if (mechs[this->i].server == this->server)
+               {
+                       *name = mechs[this->i].name;
+                       this->i++;
+                       return TRUE;
+               }
+               this->i++;
+       }
+       return FALSE;
+}
+
+/**
+ * See header.
+ */
+enumerator_t* sasl_mechanism_create_enumerator(bool server)
+{
+       mech_enumerator_t *enumerator;
+
+       INIT(enumerator,
+               .public = {
+                       .enumerate = (void*)_mech_enumerate,
+                       .destroy = (void*)free,
+               },
+               .server = server,
+       );
+       return &enumerator->public;
+}
diff --git a/src/libpttls/sasl/sasl_mechanism.h b/src/libpttls/sasl/sasl_mechanism.h
new file mode 100644 (file)
index 0000000..1a23a11
--- /dev/null
@@ -0,0 +1,103 @@
+/*
+ * Copyright (C) 2013 Martin Willi
+ * Copyright (C) 2013 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 sasl_mechanism sasl_mechanism
+ * @{ @ingroup sasl
+ */
+
+#ifndef SASL_MECHANISM_H_
+#define SASL_MECHANISM_H_
+
+typedef struct sasl_mechanism_t sasl_mechanism_t;
+
+#include <library.h>
+
+/**
+ * Constructor function for SASL mechansims.
+ *
+ * @param name                 name of the requested SASL mechanism
+ * @param client               client identity, NULL to act as server
+ * @return                             SASL mechanism, NULL on failure
+ */
+typedef sasl_mechanism_t*(*sasl_mechanism_constructor_t)(char *name,
+                                                                                                       identification_t *client);
+
+/**
+ * Generic interface for SASL mechanisms.
+ */
+struct sasl_mechanism_t {
+
+       /**
+        * Get the name of this SASL mechanism.
+        *
+        * @return                      name of SASL mechanism
+        */
+       char* (*get_name)(sasl_mechanism_t *this);
+
+       /**
+        * Build a SASL message to send to remote host.
+        *
+        * A message is returned if the return value is NEED_MORE or SUCCESS. A
+        * client MUST NOT return SUCCESS in build(), as the final message
+        * is always from server to client (even if it is an empty result message).
+        *
+        * @param message       receives allocated SASL message, to free
+        * @return
+        *                                      - FAILED if mechanism failed
+        *                                      - NEED_MORE if additional exchanges required
+        *                                      - INVALID_STATE if currently nothing to build
+        *                                      - SUCCESS if mechanism authenticated successfully
+        */
+       status_t (*build)(sasl_mechanism_t *this, chunk_t *message);
+
+       /**
+        * Process a SASL message received from remote host.
+        *
+        * If a server returns SUCCESS during process(), an empty result message
+        * is sent to complete the SASL exchange.
+        *
+        * @param message       received SASL message to process
+        * @return
+        *                                      - FAILED if mechanism failed
+        *                                      - NEED_MORE if additional exchanges required
+        *                                      - SUCCESS if mechanism authenticated successfully
+        */
+       status_t (*process)(sasl_mechanism_t *this, chunk_t message);
+
+       /**
+        * Destroy a sasl_mechanism_t.
+        */
+       void (*destroy)(sasl_mechanism_t *this);
+};
+
+/**
+ * Create a sasl_mechanism instance.
+ *
+ * @param name                 name of SASL mechanism to create
+ * @param client               client identity, NULL to act as server
+ * @return                             SASL mechanism instance, NULL if not found
+ */
+sasl_mechanism_t *sasl_mechanism_create(char *name, identification_t *client);
+
+/**
+ * Create an enumerator over supported SASL mechanism names.
+ *
+ * @param server               TRUE for server instance, FALSE for client
+ * @return                             enumerator over char*
+ */
+enumerator_t* sasl_mechanism_create_enumerator(bool server);
+
+#endif /** SASL_MECHANISM_H_ @}*/