]> git.ipfire.org Git - thirdparty/strongswan.git/commitdiff
blowfish: Register proposal keyword parser to support variable key lengths
authorTobias Brunner <tobias@strongswan.org>
Fri, 31 Oct 2014 14:32:43 +0000 (15:32 +0100)
committerTobias Brunner <tobias@strongswan.org>
Fri, 31 Oct 2014 14:32:43 +0000 (15:32 +0100)
src/libstrongswan/plugins/blowfish/blowfish_plugin.c

index 7494c52c3441c9c07a4f2c66ae13461fa9abf19e..f26d28e2c67ddafdf731255f41332ed1d9f78fb9 100644 (file)
@@ -1,4 +1,5 @@
 /*
+ * Copyright (C) 2014 Tobias Brunner
  * Copyright (C) 2009 Andreas Steffen
  * Copyright (C) 2008 Martin Willi
  * Hochschule fuer Technik Rapperswil
@@ -17,6 +18,7 @@
 #include "blowfish_plugin.h"
 
 #include <library.h>
+#include <collections/hashtable.h>
 #include "blowfish_crypter.h"
 
 typedef struct private_blowfish_plugin_t private_blowfish_plugin_t;
@@ -32,6 +34,12 @@ struct private_blowfish_plugin_t {
        blowfish_plugin_t public;
 };
 
+/**
+ * proposal tokens for variable key lengths
+ */
+static hashtable_t *tokens;
+
+
 METHOD(plugin_t, get_name, char*,
        private_blowfish_plugin_t *this)
 {
@@ -52,9 +60,53 @@ METHOD(plugin_t, get_features, int,
 METHOD(plugin_t, destroy, void,
        private_blowfish_plugin_t *this)
 {
+       enumerator_t *enumerator;
+       proposal_token_t *token;
+       char *name;
+
+       enumerator = tokens->create_enumerator(tokens);
+       while (enumerator->enumerate(enumerator, &name, &token))
+       {
+               free(name);
+               free(token);
+       }
+       enumerator->destroy(enumerator);
+       tokens->destroy(tokens);
        free(this);
 }
 
+/**
+ * Parse blowfish<keylen> for key lengths other than 128, 192 and 256.
+ */
+static proposal_token_t *blowfish_keyword(const char *algname)
+{
+       proposal_token_t *token;
+       int keylen;
+
+       if (!strcasepfx(algname, "blowfish"))
+       {
+               return NULL;
+       }
+       token = tokens->get(tokens, algname);
+       if (token)
+       {
+               return token;
+       }
+       keylen = atoi(algname + strlen("blowfish"));
+       if (keylen < 40 || keylen > 448)
+       {
+               return NULL;
+       }
+       INIT(token,
+               .name = strdup(algname),
+               .type = ENCRYPTION_ALGORITHM,
+               .algorithm = ENCR_BLOWFISH,
+               .keysize = keylen,
+       );
+       tokens->put(tokens, token->name, token);
+       return token;
+}
+
 /*
  * see header file
  */
@@ -72,5 +124,8 @@ plugin_t *blowfish_plugin_create()
                },
        );
 
+       tokens = hashtable_create(hashtable_hash_str, hashtable_equals_str, 4);
+       lib->proposal->register_algname_parser(lib->proposal, blowfish_keyword);
+
        return &this->public.plugin;
 }