Mercurial > dropbear
comparison common-algo.c @ 682:4edea9f363d0
Add rough support for choosing ciphers/hashes with "-c" or "-m"
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Thu, 17 May 2012 00:12:42 +0800 |
parents | 2895626d864f |
children | 63f8d6c469cf |
comparison
equal
deleted
inserted
replaced
678:6e0899b56ac4 | 682:4edea9f363d0 |
---|---|
258 } | 258 } |
259 | 259 |
260 return DROPBEAR_FAILURE; | 260 return DROPBEAR_FAILURE; |
261 } | 261 } |
262 | 262 |
263 | |
264 | |
265 /* Output a comma separated list of algorithms to a buffer */ | 263 /* Output a comma separated list of algorithms to a buffer */ |
266 void buf_put_algolist(buffer * buf, algo_type localalgos[]) { | 264 void buf_put_algolist(buffer * buf, algo_type localalgos[]) { |
267 | 265 |
268 unsigned int i, len; | 266 unsigned int i, len; |
269 unsigned int donefirst = 0; | 267 unsigned int donefirst = 0; |
280 } | 278 } |
281 } | 279 } |
282 buf_putstring(buf, algolist->data, algolist->len); | 280 buf_putstring(buf, algolist->data, algolist->len); |
283 buf_free(algolist); | 281 buf_free(algolist); |
284 } | 282 } |
283 | |
284 #ifdef ENABLE_USER_ALGO_LIST | |
285 | |
286 char * | |
287 algolist_string(algo_type algos[]) | |
288 { | |
289 char *ret_list; | |
290 buffer *b = buf_new(200); | |
291 buf_put_algolist(b, algos); | |
292 buf_setpos(b, b->len); | |
293 buf_putbyte(b, '\0'); | |
294 buf_setpos(b, 4); | |
295 ret_list = m_strdup(buf_getptr(b, b->len - b->pos)); | |
296 buf_free(b); | |
297 return ret_list; | |
298 } | |
299 | |
300 static int | |
301 check_algo(const char* algo_name, algo_type *algos) | |
302 { | |
303 algo_type *a; | |
304 for (a = algos; a->name != NULL; a++) | |
305 { | |
306 if (strcmp(a->name, algo_name) == 0) | |
307 { | |
308 a->usable = 2; | |
309 return DROPBEAR_SUCCESS; | |
310 } | |
311 } | |
312 | |
313 return DROPBEAR_FAILURE; | |
314 } | |
315 | |
316 /* helper for check_user_algos */ | |
317 static void | |
318 try_add_algo(const char *algo_name, algo_type *algos, | |
319 const char *algo_desc, char ** out_list, int *num_ret) | |
320 { | |
321 if (check_algo(algo_name, algos) == DROPBEAR_FAILURE) | |
322 { | |
323 dropbear_log(LOG_WARNING, "This Dropbear program does not support '%s' %s algorithm", algo_name, algo_desc); | |
324 return; | |
325 } | |
326 | |
327 if (*num_ret != 0) | |
328 { | |
329 **out_list = ','; | |
330 (*out_list)++; | |
331 } | |
332 | |
333 *out_list += sprintf(*out_list, "%s", algo_name); | |
334 (*num_ret)++; | |
335 } | |
336 | |
337 /* Checks a user provided comma-separated algorithm list for available | |
338 * options. Any that are not acceptable are removed in-place. Returns the | |
339 * number of valid algorithms. */ | |
340 int | |
341 check_user_algos(char* user_algo_list, algo_type * algos, | |
342 const char *algo_desc) | |
343 { | |
344 /* this has two passes. first we sweep through the given list of | |
345 * algorithms and mark them as usable=2 in the algo_type[] array... */ | |
346 int num_ret = 0; | |
347 char *work_list = m_strdup(user_algo_list); | |
348 char *last_name = work_list; | |
349 char *out_list = user_algo_list; | |
350 char *c; | |
351 for (c = work_list; *c; c++) | |
352 { | |
353 if (*c == ',') | |
354 { | |
355 *c = '\0'; | |
356 try_add_algo(last_name, algos, algo_desc, &out_list, &num_ret); | |
357 last_name = c++; | |
358 } | |
359 } | |
360 try_add_algo(last_name, algos, algo_desc, &out_list, &num_ret); | |
361 m_free(work_list); | |
362 | |
363 /* ...then we mark anything with usable==1 as usable=0, and | |
364 * usable==2 as usable=1. */ | |
365 algo_type *a; | |
366 for (a = algos; a->name != NULL; a++) | |
367 { | |
368 if (a->usable == 1) | |
369 { | |
370 a->usable = 0; | |
371 } else if (a->usable == 2) | |
372 { | |
373 a->usable = 1; | |
374 } | |
375 } | |
376 return num_ret; | |
377 } | |
378 #endif // ENABLE_USER_ALGO_LIST |