summaryrefslogtreecommitdiffhomepage
path: root/common-algo.c
diff options
context:
space:
mode:
Diffstat (limited to 'common-algo.c')
-rw-r--r--common-algo.c98
1 files changed, 96 insertions, 2 deletions
diff --git a/common-algo.c b/common-algo.c
index acc3964..5ed809b 100644
--- a/common-algo.c
+++ b/common-algo.c
@@ -260,8 +260,6 @@ int have_algo(char* algo, size_t algolen, algo_type algos[]) {
return DROPBEAR_FAILURE;
}
-
-
/* Output a comma separated list of algorithms to a buffer */
void buf_put_algolist(buffer * buf, algo_type localalgos[]) {
@@ -282,3 +280,99 @@ void buf_put_algolist(buffer * buf, algo_type localalgos[]) {
buf_putstring(buf, algolist->data, algolist->len);
buf_free(algolist);
}
+
+#ifdef ENABLE_USER_ALGO_LIST
+
+char *
+algolist_string(algo_type algos[])
+{
+ char *ret_list;
+ buffer *b = buf_new(200);
+ buf_put_algolist(b, algos);
+ buf_setpos(b, b->len);
+ buf_putbyte(b, '\0');
+ buf_setpos(b, 4);
+ ret_list = m_strdup(buf_getptr(b, b->len - b->pos));
+ buf_free(b);
+ return ret_list;
+}
+
+static int
+check_algo(const char* algo_name, algo_type *algos)
+{
+ algo_type *a;
+ for (a = algos; a->name != NULL; a++)
+ {
+ if (strcmp(a->name, algo_name) == 0)
+ {
+ a->usable = 2;
+ return DROPBEAR_SUCCESS;
+ }
+ }
+
+ return DROPBEAR_FAILURE;
+}
+
+/* helper for check_user_algos */
+static void
+try_add_algo(const char *algo_name, algo_type *algos,
+ const char *algo_desc, char ** out_list, int *num_ret)
+{
+ if (check_algo(algo_name, algos) == DROPBEAR_FAILURE)
+ {
+ dropbear_log(LOG_WARNING, "This Dropbear program does not support '%s' %s algorithm", algo_name, algo_desc);
+ return;
+ }
+
+ if (*num_ret != 0)
+ {
+ **out_list = ',';
+ (*out_list)++;
+ }
+
+ *out_list += sprintf(*out_list, "%s", algo_name);
+ (*num_ret)++;
+}
+
+/* Checks a user provided comma-separated algorithm list for available
+ * options. Any that are not acceptable are removed in-place. Returns the
+ * number of valid algorithms. */
+int
+check_user_algos(char* user_algo_list, algo_type * algos,
+ const char *algo_desc)
+{
+ /* this has two passes. first we sweep through the given list of
+ * algorithms and mark them as usable=2 in the algo_type[] array... */
+ int num_ret = 0;
+ char *work_list = m_strdup(user_algo_list);
+ char *last_name = work_list;
+ char *out_list = user_algo_list;
+ char *c;
+ for (c = work_list; *c; c++)
+ {
+ if (*c == ',')
+ {
+ *c = '\0';
+ try_add_algo(last_name, algos, algo_desc, &out_list, &num_ret);
+ last_name = c++;
+ }
+ }
+ try_add_algo(last_name, algos, algo_desc, &out_list, &num_ret);
+ m_free(work_list);
+
+ /* ...then we mark anything with usable==1 as usable=0, and
+ * usable==2 as usable=1. */
+ algo_type *a;
+ for (a = algos; a->name != NULL; a++)
+ {
+ if (a->usable == 1)
+ {
+ a->usable = 0;
+ } else if (a->usable == 2)
+ {
+ a->usable = 1;
+ }
+ }
+ return num_ret;
+}
+#endif // ENABLE_USER_ALGO_LIST