You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

427 lines
15 KiB

  1. diff --git a/CREDITS b/CREDITS
  2. index de17d9e..38e2108 100644
  3. --- a/CREDITS
  4. +++ b/CREDITS
  5. @@ -1,5 +1,6 @@
  6. Jonathan Bennett
  7. - Contributed OpenWRT support - see the extras/openwrt/ directory.
  8. + - Suggested the addition of the --key-gen option to fwknopd.
  9. Sebastien Jeanquier
  10. - Assisted with getting fwknop included in BackTrack Linux - the choice
  11. diff --git a/ChangeLog b/ChangeLog
  12. index 21a5093..4daf008 100644
  13. --- a/ChangeLog
  14. +++ b/ChangeLog
  15. @@ -1,3 +1,8 @@
  16. +fwknop-2.6.7 (05//2015):
  17. + - Added --key-gen to fwknopd. This feature was suggested by Jonathan
  18. + Bennett, and will help with ease of use efforts. The first platform to
  19. + take advantage of this will likely be OpenWRT thanks to Jonathan.
  20. +
  21. fwknop-2.6.6 (04/23/2015):
  22. - [server] Add the ability for fwknopd to function as an generic SPA
  23. gateway. This allows scenarios such as the fwknopd system providing DHCP
  24. diff --git a/client/config_init.c b/client/config_init.c
  25. index cdb233d..f0ae135 100644
  26. --- a/client/config_init.c
  27. +++ b/client/config_init.c
  28. @@ -199,7 +199,7 @@ static int critical_var_array[] =
  29. };
  30. /**
  31. - * @brief Generate Rijndael + HMAC keys from /dev/random (base64 encoded) and exit.
  32. + * @brief Generate Rijndael + HMAC keys from /dev/urandom (base64 encoded).
  33. *
  34. * @param options FKO command line option structure
  35. */
  36. diff --git a/client/fwknop_common.h b/client/fwknop_common.h
  37. index aef20c4..c57db02 100644
  38. --- a/client/fwknop_common.h
  39. +++ b/client/fwknop_common.h
  40. @@ -69,8 +69,6 @@
  41. #define MAX_HOSTNAME_LEN 70
  42. #define MAX_URL_HOST_LEN 256
  43. #define MAX_URL_PATH_LEN 1024
  44. -#define MAX_KEY_LEN 128
  45. -#define MAX_B64_KEY_LEN 180
  46. /* fwknop client configuration parameters and values
  47. */
  48. @@ -156,10 +154,10 @@ typedef struct fko_cli_options
  49. unsigned char use_gpg;
  50. unsigned char use_gpg_agent;
  51. unsigned char gpg_no_signing_pw;
  52. + unsigned char key_gen;
  53. int time_offset_plus;
  54. int time_offset_minus;
  55. int fw_timeout;
  56. - int key_gen;
  57. char use_rc_stanza[MAX_LINE_LEN];
  58. unsigned char got_named_stanza;
  59. diff --git a/common/common.h b/common/common.h
  60. index b63e7c2..c7b2e57 100644
  61. --- a/common/common.h
  62. +++ b/common/common.h
  63. @@ -157,6 +157,9 @@ enum {
  64. #define MAX_GPG_KEY_ID 128
  65. #define MAX_USERNAME_LEN 30
  66. +#define MAX_KEY_LEN 128
  67. +#define MAX_B64_KEY_LEN 180
  68. +
  69. /* Command line argument / argv handling
  70. */
  71. #define MAX_CMDLINE_ARGS 30 /*!< should be way more than enough */
  72. diff --git a/doc/fwknop.man.asciidoc b/doc/fwknop.man.asciidoc
  73. index 070ac77..efa99a7 100644
  74. --- a/doc/fwknop.man.asciidoc
  75. +++ b/doc/fwknop.man.asciidoc
  76. @@ -196,6 +196,11 @@ GENERAL OPTIONS
  77. keys are generally more secure than passphrases that are typed in from the
  78. command line.
  79. +*--key-gen-file*='<file>'::
  80. + Write generated keys to the specified file. Note that the file is
  81. + overwritten if it already exists. If this option is not given, then
  82. + *--key-gen* writes the keys to stdout.
  83. +
  84. *--key-len*='<length>'::
  85. Specify the number of bytes for a generated Rijndael key. The maximum size
  86. is currently 128 bytes.
  87. diff --git a/server/cmd_opts.h b/server/cmd_opts.h
  88. index bc1eee1..d7a645c 100644
  89. --- a/server/cmd_opts.h
  90. +++ b/server/cmd_opts.h
  91. @@ -141,6 +141,10 @@ enum {
  92. FW_LIST = 0x200,
  93. FW_LIST_ALL,
  94. FW_FLUSH,
  95. + KEY_GEN_FILE,
  96. + KEY_LEN,
  97. + HMAC_KEY_LEN,
  98. + HMAC_DIGEST_TYPE,
  99. AFL_PKT_FILE,
  100. GPG_HOME_DIR,
  101. GPG_EXE_PATH,
  102. @@ -178,7 +182,12 @@ static struct option cmd_opts[] =
  103. {"fault-injection-tag", 1, NULL, FAULT_INJECTION_TAG},
  104. {"help", 0, NULL, 'h'},
  105. {"interface", 1, NULL, 'i'},
  106. - {"kill", 0, NULL, 'K'},
  107. + {"key-gen", 0, NULL, 'k'},
  108. + {"key-gen-file", 1, NULL, KEY_GEN_FILE },
  109. + {"key-len", 1, NULL, KEY_LEN },
  110. + {"hmac-key-len", 1, NULL, HMAC_KEY_LEN },
  111. + {"hmac-digest-type", 1, NULL, HMAC_DIGEST_TYPE },
  112. + {"kill", 0, NULL, 'K' },
  113. {"fw-flush", 0, NULL, FW_FLUSH },
  114. {"fw-list", 0, NULL, FW_LIST },
  115. {"fw-list-all", 0, NULL, FW_LIST_ALL },
  116. diff --git a/server/config_init.c b/server/config_init.c
  117. index 0ddceee..2f1d293 100644
  118. --- a/server/config_init.c
  119. +++ b/server/config_init.c
  120. @@ -201,6 +201,69 @@ validate_int_var_ranges(fko_srv_options_t *opts)
  121. return;
  122. }
  123. +/**
  124. + * @brief Generate Rijndael + HMAC keys from /dev/urandom (base64 encoded).
  125. + *
  126. + * @param options FKO command line option structure
  127. + */
  128. +static void
  129. +generate_keys(fko_srv_options_t *options)
  130. +{
  131. + char key_base64[MAX_B64_KEY_LEN+1];
  132. + char hmac_key_base64[MAX_B64_KEY_LEN+1];
  133. +
  134. + FILE *key_gen_file_ptr = NULL;
  135. + int res;
  136. +
  137. + /* Set defaults and validate for --key-gen mode
  138. + */
  139. + if(options->key_len == 0)
  140. + options->key_len = FKO_DEFAULT_KEY_LEN;
  141. +
  142. + if(options->hmac_key_len == 0)
  143. + options->hmac_key_len = FKO_DEFAULT_HMAC_KEY_LEN;
  144. +
  145. + if(options->hmac_type == 0)
  146. + options->hmac_type = FKO_DEFAULT_HMAC_MODE;
  147. +
  148. + /* Zero out the key buffers */
  149. + memset(key_base64, 0x00, sizeof(key_base64));
  150. + memset(hmac_key_base64, 0x00, sizeof(hmac_key_base64));
  151. +
  152. + /* Generate the key through libfko */
  153. + res = fko_key_gen(key_base64, options->key_len,
  154. + hmac_key_base64, options->hmac_key_len,
  155. + options->hmac_type);
  156. +
  157. + if(res != FKO_SUCCESS)
  158. + {
  159. + log_msg(LOG_ERR, "%s: fko_key_gen: Error %i - %s",
  160. + MY_NAME, res, fko_errstr(res));
  161. + clean_exit(options, NO_FW_CLEANUP, EXIT_FAILURE);
  162. + }
  163. +
  164. + if(options->key_gen_file[0] != '\0')
  165. + {
  166. + if ((key_gen_file_ptr = fopen(options->key_gen_file, "w")) == NULL)
  167. + {
  168. + log_msg(LOG_ERR, "Unable to create key gen file: %s: %s",
  169. + options->key_gen_file, strerror(errno));
  170. + clean_exit(options, NO_FW_CLEANUP, EXIT_FAILURE);
  171. + }
  172. + fprintf(key_gen_file_ptr, "KEY_BASE64: %s\nHMAC_KEY_BASE64: %s\n",
  173. + key_base64, hmac_key_base64);
  174. + fclose(key_gen_file_ptr);
  175. + fprintf(stdout, "[+] Wrote Rijndael and HMAC keys to: %s",
  176. + options->key_gen_file);
  177. + }
  178. + else
  179. + {
  180. + fprintf(stdout, "KEY_BASE64: %s\nHMAC_KEY_BASE64: %s\n",
  181. + key_base64, hmac_key_base64);
  182. + }
  183. + clean_exit(options, NO_FW_CLEANUP, EXIT_SUCCESS);
  184. +}
  185. +
  186. /* Parse the config file...
  187. */
  188. static void
  189. @@ -427,7 +490,7 @@ validate_options(fko_srv_options_t *opts)
  190. if(opts->config[CONF_ENABLE_DIGEST_PERSISTENCE] == NULL)
  191. set_config_entry(opts, CONF_ENABLE_DIGEST_PERSISTENCE,
  192. DEF_ENABLE_DIGEST_PERSISTENCE);
  193. -
  194. +
  195. /* Enable destination rule.
  196. */
  197. if(opts->config[CONF_ENABLE_DESTINATION_RULE] == NULL)
  198. @@ -928,8 +991,9 @@ config_init(fko_srv_options_t *opts, int argc, char **argv)
  199. /* First, scan the command-line args for -h/--help or an alternate
  200. * configuration file. If we find an alternate config file, use it,
  201. - * otherwise use the default. We also grab any override config files
  202. - * as well.
  203. + * otherwise use the default. We also grab any override config files
  204. + * as well. In addition, we handle key generation here since this is
  205. + * independent of configuration parsing.
  206. */
  207. while ((cmd_arg = getopt_long(argc, argv,
  208. GETOPTS_OPTION_STRING, cmd_opts, &index)) != -1) {
  209. @@ -952,6 +1016,45 @@ config_init(fko_srv_options_t *opts, int argc, char **argv)
  210. if(got_override_config > 0)
  211. break;
  212. + case 'k':
  213. + opts->key_gen = 1;
  214. + break;
  215. + case KEY_GEN_FILE:
  216. + opts->key_gen = 1;
  217. + strlcpy(opts->key_gen_file, optarg, sizeof(opts->key_gen_file));
  218. + break;
  219. + case KEY_LEN: /* used in --key-gen mode only */
  220. + opts->key_len = strtol_wrapper(optarg, 1,
  221. + MAX_KEY_LEN, NO_EXIT_UPON_ERR, &is_err);
  222. + if(is_err != FKO_SUCCESS)
  223. + {
  224. + log_msg(LOG_ERR,
  225. + "Invalid key length '%s', must be in [%d-%d]",
  226. + optarg, 1, MAX_KEY_LEN);
  227. + clean_exit(opts, NO_FW_CLEANUP, EXIT_FAILURE);
  228. + }
  229. + break;
  230. + case HMAC_DIGEST_TYPE: /* used in --key-gen mode only */
  231. + if((opts->hmac_type = hmac_digest_strtoint(optarg)) < 0)
  232. + {
  233. + log_msg(LOG_ERR,
  234. + "* Invalid hmac digest type: %s, use {md5,sha1,sha256,sha384,sha512}",
  235. + optarg);
  236. + clean_exit(opts, NO_FW_CLEANUP, EXIT_FAILURE);
  237. + }
  238. + break;
  239. + case HMAC_KEY_LEN: /* used in --key-gen mode only */
  240. + opts->hmac_key_len = strtol_wrapper(optarg, 1,
  241. + MAX_KEY_LEN, NO_EXIT_UPON_ERR, &is_err);
  242. + if(is_err != FKO_SUCCESS)
  243. + {
  244. + log_msg(LOG_ERR,
  245. + "Invalid hmac key length '%s', must be in [%d-%d]",
  246. + optarg, 1, MAX_KEY_LEN);
  247. + clean_exit(opts, NO_FW_CLEANUP, EXIT_FAILURE);
  248. + }
  249. + break;
  250. +
  251. /* Look for override configuration file arg.
  252. */
  253. case 'O':
  254. @@ -965,6 +1068,9 @@ config_init(fko_srv_options_t *opts, int argc, char **argv)
  255. }
  256. }
  257. + if(opts->key_gen)
  258. + generate_keys(opts);
  259. +
  260. /* If no alternate configuration file was specified, we use the
  261. * default.
  262. */
  263. diff --git a/server/fwknopd_common.h b/server/fwknopd_common.h
  264. index ecf2a81..8c33eaa 100644
  265. --- a/server/fwknopd_common.h
  266. +++ b/server/fwknopd_common.h
  267. @@ -585,10 +585,14 @@ typedef struct fko_srv_options
  268. unsigned char fw_list; /* List current firewall rules */
  269. unsigned char fw_list_all; /* List all current firewall rules */
  270. unsigned char fw_flush; /* Flush current firewall rules */
  271. + unsigned char key_gen; /* Generate keys and exit */
  272. + unsigned char exit_after_parse_config; /* Parse config and exit */
  273. +
  274. + /* Operational flags
  275. + */
  276. unsigned char test; /* Test mode flag */
  277. unsigned char afl_fuzzing; /* SPA pkts from stdin for AFL fuzzing */
  278. unsigned char verbose; /* Verbose mode flag */
  279. - unsigned char exit_after_parse_config; /* Parse config and exit */
  280. unsigned char enable_udp_server; /* Enable UDP server mode */
  281. unsigned char firewd_disable_check_support; /* Don't use firewall-cmd ... -C */
  282. @@ -605,6 +609,13 @@ typedef struct fko_srv_options
  283. int tcp_server_pid;
  284. int lock_fd;
  285. + /* Values used in --key-gen mode only
  286. + */
  287. + char key_gen_file[MAX_PATH_LEN];
  288. + int key_len;
  289. + int hmac_key_len;
  290. + int hmac_type;
  291. +
  292. #if USE_FILE_CACHE
  293. struct digest_cache_list *digest_cache; /* In-memory digest cache list */
  294. #endif
  295. diff --git a/test/tests/basic_operations.pl b/test/tests/basic_operations.pl
  296. index f4dde2e..76a509d 100644
  297. --- a/test/tests/basic_operations.pl
  298. +++ b/test/tests/basic_operations.pl
  299. @@ -390,6 +390,14 @@
  300. 'exec_err' => $YES,
  301. 'cmdline' => "$default_client_args --key-gen -K " . 'A'x1030
  302. },
  303. + {
  304. + 'category' => 'basic operations',
  305. + 'subcategory' => 'server',
  306. + 'detail' => '--key-gen file path (-K) too long',
  307. + 'function' => \&generic_exec,
  308. + 'exec_err' => $YES,
  309. + 'cmdline' => "$fwknopdCmd --key-gen --key-gen-file " . 'A'x1030
  310. + },
  311. {
  312. 'category' => 'basic operations',
  313. diff --git a/test/tests/rijndael.pl b/test/tests/rijndael.pl
  314. index 26aab6a..34af65e 100644
  315. --- a/test/tests/rijndael.pl
  316. +++ b/test/tests/rijndael.pl
  317. @@ -421,33 +421,6 @@
  318. 'key_file' => $cf{'rc_named_key'},
  319. },
  320. - ### --key-gen tests
  321. - {
  322. - 'category' => 'Rijndael',
  323. - 'subcategory' => 'client',
  324. - 'detail' => '--key-gen',
  325. - 'function' => \&generic_exec,
  326. - 'cmdline' => "$fwknopCmd --key-gen",
  327. - 'positive_output_matches' => [qr/^KEY_BASE64\:?\s\S{10}/,
  328. - qw/HMAC_KEY_BASE64\:?\s\S{10}/],
  329. - },
  330. - {
  331. - 'category' => 'Rijndael',
  332. - 'subcategory' => 'client',
  333. - 'detail' => "--key-gen $uniq_keys key uniqueness",
  334. - 'function' => \&key_gen_uniqueness,
  335. - 'cmdline' => "$fwknopCmd --key-gen", ### no valgrind string (too slow for 100 client exec's)
  336. - 'disable_valgrind' => $YES,
  337. - },
  338. - {
  339. - 'category' => 'Rijndael',
  340. - 'subcategory' => 'client',
  341. - 'detail' => '--key-gen to file',
  342. - 'function' => \&generic_exec,
  343. - 'cmdline' => "$fwknopCmd --key-gen --key-gen-file $key_gen_file",
  344. - 'positive_output_matches' => [qr/Wrote.*\skeys/],
  345. - },
  346. -
  347. ### rc file tests
  348. {
  349. 'category' => 'Rijndael',
  350. diff --git a/test/tests/rijndael_hmac.pl b/test/tests/rijndael_hmac.pl
  351. index fc1a8af..fd80f04 100644
  352. --- a/test/tests/rijndael_hmac.pl
  353. +++ b/test/tests/rijndael_hmac.pl
  354. @@ -58,6 +58,59 @@
  355. 'exec_err' => $YES,
  356. },
  357. + ### --key-gen tests
  358. + {
  359. + 'category' => 'Rijndael+HMAC',
  360. + 'subcategory' => 'client',
  361. + 'detail' => '--key-gen',
  362. + 'function' => \&generic_exec,
  363. + 'cmdline' => "$fwknopCmd --key-gen",
  364. + 'positive_output_matches' => [qr/^KEY_BASE64\:?\s\S{10}/,
  365. + qw/HMAC_KEY_BASE64\:?\s\S{10}/],
  366. + },
  367. + {
  368. + 'category' => 'Rijndael+HMAC',
  369. + 'subcategory' => 'server',
  370. + 'detail' => '--key-gen',
  371. + 'function' => \&generic_exec,
  372. + 'cmdline' => "$fwknopdCmd --key-gen",
  373. + 'positive_output_matches' => [qr/^KEY_BASE64\:?\s\S{10}/,
  374. + qw/HMAC_KEY_BASE64\:?\s\S{10}/],
  375. + },
  376. + {
  377. + 'category' => 'Rijndael+HMAC',
  378. + 'subcategory' => 'client',
  379. + 'detail' => "--key-gen $uniq_keys key uniqueness",
  380. + 'function' => \&key_gen_uniqueness,
  381. + 'cmdline' => "$fwknopCmd --key-gen", ### no valgrind string (too slow for 100 exec's)
  382. + 'disable_valgrind' => $YES,
  383. + },
  384. + {
  385. + 'category' => 'Rijndael+HMAC',
  386. + 'subcategory' => 'server',
  387. + 'detail' => "--key-gen $uniq_keys key uniqueness",
  388. + 'function' => \&key_gen_uniqueness,
  389. + 'cmdline' => "$fwknopdCmd --key-gen", ### no valgrind string (too slow for 100 exec's)
  390. + 'disable_valgrind' => $YES,
  391. + },
  392. + {
  393. + 'category' => 'Rijndael+HMAC',
  394. + 'subcategory' => 'client',
  395. + 'detail' => '--key-gen to file',
  396. + 'function' => \&generic_exec,
  397. + 'cmdline' => "$fwknopCmd --key-gen --key-gen-file $key_gen_file",
  398. + 'positive_output_matches' => [qr/Wrote.*\skeys/],
  399. + },
  400. + {
  401. + 'category' => 'Rijndael+HMAC',
  402. + 'subcategory' => 'server',
  403. + 'detail' => '--key-gen to file',
  404. + 'function' => \&generic_exec,
  405. + 'cmdline' => "$fwknopdCmd --key-gen --key-gen-file $key_gen_file",
  406. + 'positive_output_matches' => [qr/Wrote.*\skeys/],
  407. + },
  408. +
  409. + ### complete cycle tests
  410. {
  411. 'category' => 'Rijndael+HMAC',
  412. 'subcategory' => 'client+server',