ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/gvpe/src/gvpectrl.C
(Generate patch)

Comparing gvpe/src/gvpectrl.C (file contents):
Revision 1.16 by root, Thu Jul 18 13:35:16 2013 UTC vs.
Revision 1.24 by root, Thu Oct 25 03:13:13 2018 UTC

1/* 1/*
2 gvpectrl.C -- the main file for gvpectrl 2 gvpectrl.C -- the main file for gvpectrl
3 Copyright (C) 1998-2002 Ivo Timmermans <ivo@o2w.nl> 3 Copyright (C) 1998-2002 Ivo Timmermans <ivo@o2w.nl>
4 2000-2002 Guus Sliepen <guus@sliepen.eu.org> 4 2000-2002 Guus Sliepen <guus@sliepen.eu.org>
5 2003-2013 Marc Lehmann <gvpe@schmorp.de> 5 2003-2016 Marc Lehmann <gvpe@schmorp.de>
6 6
7 This file is part of GVPE. 7 This file is part of GVPE.
8 8
9 GVPE is free software; you can redistribute it and/or modify it 9 GVPE is free software; you can redistribute it and/or modify it
10 under the terms of the GNU General Public License as published by the 10 under the terms of the GNU General Public License as published by the
72static int show_config; 72static int show_config;
73 73
74/* If nonzero, do not output anything but warnings/errors/very unusual conditions */ 74/* If nonzero, do not output anything but warnings/errors/very unusual conditions */
75static int quiet; 75static int quiet;
76 76
77/* If nonzero, generate single public/private keypair. */
78static const char *generate_key;
79
77/* If nonzero, generate public/private keypair for this net. */ 80/* If nonzero, generate public/private keypair for this net. */
78static int generate_keys; 81static int generate_keys;
79 82
80// output some debugging info, interna constants &c 83// output some debugging info, interna constants &c
81static int debug_info; 84static int debug_info;
84{ 87{
85 {"config", required_argument, NULL, 'c'}, 88 {"config", required_argument, NULL, 'c'},
86 {"kill", optional_argument, NULL, 'k'}, 89 {"kill", optional_argument, NULL, 'k'},
87 {"help", no_argument, &show_help, 1}, 90 {"help", no_argument, &show_help, 1},
88 {"version", no_argument, &show_version, 1}, 91 {"version", no_argument, &show_version, 1},
92 {"generate-key", required_argument, NULL, 'g'},
89 {"generate-keys", no_argument, NULL, 'g'}, 93 {"generate-keys", no_argument, NULL, 'G'},
90 {"quiet", no_argument, &quiet, 1}, 94 {"quiet", no_argument, &quiet, 1},
91 {"show-config", no_argument, &show_config, 's'}, 95 {"show-config", no_argument, &show_config, 's'},
92 {"debug-info", no_argument, &debug_info, 1}, 96 {"debug-info", no_argument, &debug_info, 1},
93 {NULL, 0, NULL, 0} 97 {NULL, 0, NULL, 0}
94}; 98};
102 { 106 {
103 printf (_("Usage: %s [option]...\n\n"), get_identity ()); 107 printf (_("Usage: %s [option]...\n\n"), get_identity ());
104 printf (_ 108 printf (_
105 (" -c, --config=DIR Read configuration options from DIR.\n" 109 (" -c, --config=DIR Read configuration options from DIR.\n"
106 " -k, --kill[=SIGNAL] Attempt to kill a running gvpe and exit.\n" 110 " -k, --kill[=SIGNAL] Attempt to kill a running gvpe and exit.\n"
111 " -g, --generate-key=file Generate public/private RSA keypair.\n"
107 " -g, --generate-keys Generate public/private RSA keypair.\n" 112 " -G, --generate-keys Generate all public/private RSA keypairs.\n"
108 " -s, --show-config Display the configuration information.\n" 113 " -s, --show-config Display the configuration information.\n"
109 " -q, --quiet Be quite quiet.\n" 114 " -q, --quiet Be quite quiet.\n"
110 " --help Display this help and exit.\n" 115 " --help Display this help and exit.\n"
111 " --version Output version information and exit.\n\n")); 116 " --version Output version information and exit.\n\n"));
112 printf (_("Report bugs to <gvpe@schmorp.de>.\n")); 117 printf (_("Report bugs to <gvpe@schmorp.de>.\n"));
119parse_options (int argc, char **argv, char **envp) 124parse_options (int argc, char **argv, char **envp)
120{ 125{
121 int r; 126 int r;
122 int option_index = 0; 127 int option_index = 0;
123 128
124 while ((r = getopt_long (argc, argv, "c:k::qgs", long_options, &option_index)) != EOF) 129 while ((r = getopt_long (argc, argv, "c:k::qg:Gs", long_options, &option_index)) != EOF)
125 { 130 {
126 switch (r) 131 switch (r)
127 { 132 {
128 case 0: /* long option */ 133 case 0: /* long option */
129 break; 134 break;
167 kill_gvpe = SIGTERM; 172 kill_gvpe = SIGTERM;
168 173
169 break; 174 break;
170 175
171 case 'g': /* generate public/private keypair */ 176 case 'g': /* generate public/private keypair */
177 generate_key = optarg;
178 break;
179
180 case 'G': /* generate public/private keypairs */
172 generate_keys = RSABITS; 181 generate_keys = 1;
173 break; 182 break;
174 183
175 case 's': 184 case 's':
176 show_config = 1; 185 show_config = 1;
177 break; 186 break;
235 244
236/* 245/*
237 * generate public/private RSA keypairs for all hosts that don't have one. 246 * generate public/private RSA keypairs for all hosts that don't have one.
238 */ 247 */
239static int 248static int
240keygen (int bits) 249keygen (const char *pub, const char *priv)
241{ 250{
242 FILE *f, *pubf; 251
252 FILE *pubf = fopen (pub, "ab");
253 if (!pubf || fseek (pubf, 0, SEEK_END))
254 {
255 perror (pub);
256 exit (EXIT_FAILURE);
257 }
258
259 if (ftell (pubf))
260 {
261 fclose (pubf);
262 return 1;
263 }
264
265 FILE *privf = fopen (priv, "ab");
266
267 /* some libcs are buggy and require an extra seek to the end */
268 if (!privf || fseek (privf, 0, SEEK_END))
269 {
270 perror (priv);
271 exit (EXIT_FAILURE);
272 }
273
274 if (ftell (privf))
275 {
276 fclose (pubf);
277 fclose (privf);
278 return 1;
279 }
280
281 RSA *rsa = RSA_new ();
282 BIGNUM *e = BN_new ();
283 BN_set_bit (e, 0); BN_set_bit (e, 16); // 0x10001, 65537
284
285#if 0
286#if OPENSSL_VERSION_NUMBER < 0x10100000
287 BN_GENCB cb_100;
288 BN_GENCB *cb = &cb_100;
289#else
290 BN_GENCB *cb = BN_GENCB_new ();
291 require (cb);
292#endif
293
294 BN_GENCB_set (cb, indicator, 0);
295 require (RSA_generate_key_ex (rsa, RSABITS, e, cb));
296#else
297 require (RSA_generate_key_ex (rsa, RSABITS, e, 0));
298#endif
299
300 require (PEM_write_RSAPublicKey (pubf, rsa));
301 require (PEM_write_RSAPrivateKey (privf, rsa, NULL, NULL, 0, NULL, NULL));
302
303 fclose (pubf);
304 fclose (privf);
305
306 BN_free (e);
307 RSA_free (rsa);
308
309 return 0;
310}
311
312static int
313keygen_all ()
314{
243 char *fname; 315 char *fname;
244 316
245 asprintf (&fname, "%s/pubkey", confbase); 317 asprintf (&fname, "%s/pubkey", confbase);
246 mkdir (fname, 0700); 318 mkdir (fname, 0700);
247 free (fname); 319 free (fname);
250 { 322 {
251 conf_node *node = *i; 323 conf_node *node = *i;
252 324
253 ::thisnode = node->nodename; 325 ::thisnode = node->nodename;
254 326
327 char *pub = conf.config_filename ("pubkey/%s", 0);
255 fname = conf.config_filename (conf.prikeyfile, "hostkey"); 328 char *priv = conf.config_filename (conf.prikeyfile, "hostkey");
256 329
257 f = fopen (fname, "ab"); 330 int status = keygen (pub, priv);
258 331
259 /* some libcs are buggy and require an extra seek to the end */ 332 if (status == 0)
260 if (!f || fseek (f, 0, SEEK_END))
261 {
262 perror (fname);
263 exit (EXIT_FAILURE);
264 }
265
266 if (ftell (f))
267 { 333 {
268 if (!quiet) 334 if (!quiet)
269 fprintf (stderr, "'%s' already exists, skipping node %s\n", fname, node->nodename); 335 fprintf (stderr, _("generated %d bits key for %s.\n"), RSABITS, node->nodename);
270
271 free (fname);
272 fclose (f);
273 continue;
274 } 336 }
337 else if (status == 1)
338 fprintf (stderr, _("'%s' keypair already exists, skipping node %s.\n"), pub, node->nodename);
275 339
276 free (fname); 340 free (priv);
277
278 fprintf (stderr, _("generating %d bits key for %s:\n"), bits, node->nodename);
279
280 RSA *rsa = RSA_new ();
281 BIGNUM *e = BN_new ();
282 BN_set_bit (e, 0); BN_set_bit (e, 16); // 0x10001, 65537
283 BN_GENCB cb;
284 BN_GENCB_set (&cb, indicator, 0);
285
286 require (RSA_generate_key_ex (rsa, bits, e, &cb));
287
288 fprintf (stderr, _("Done.\n"));
289
290 fname = conf.config_filename ("pubkey/%s", 0);
291 pubf = fopen (fname, "wb");
292 if (!pubf) 341 free (pub);
293 { 342 }
294 perror (fname); 343
344 return 0;
345}
346
347static int
348keygen_one (const char *pubname)
349{
350 char *privname;
351
352 asprintf (&privname, "%s.privkey", pubname);
353
354 int status = keygen (pubname, privname);
355
356 if (status == 0)
357 {
358 if (!quiet)
359 fprintf (stderr, _("generated %d bits key as %s.\n"), RSABITS, pubname);
360 }
361 else if (status == 1)
362 {
363 fprintf (stderr, _("'%s' keypair already exists, not generating key.\n"), pubname);
295 exit (EXIT_FAILURE); 364 exit (EXIT_FAILURE);
296 } 365 }
297 366
298 free (fname); 367 free (privname);
299
300 require (PEM_write_RSAPublicKey (pubf, rsa));
301 fclose (pubf);
302
303 require (PEM_write_RSAPrivateKey (f, rsa, NULL, NULL, 0, NULL, NULL));
304 fclose (f);
305
306 BN_free (e);
307 RSA_free (rsa);
308 }
309 368
310 return 0; 369 return 0;
311} 370}
312 371
313int 372int
349 printf ("cipher_nid=%d\n", EVP_CIPHER_nid (CIPHER ())); 408 printf ("cipher_nid=%d\n", EVP_CIPHER_nid (CIPHER ()));
350 printf ("mac_nid=%d\n", EVP_MD_type (MAC_DIGEST ())); 409 printf ("mac_nid=%d\n", EVP_MD_type (MAC_DIGEST ()));
351 printf ("auth_nid=%d\n", EVP_MD_type (AUTH_DIGEST ())); 410 printf ("auth_nid=%d\n", EVP_MD_type (AUTH_DIGEST ()));
352 printf ("sizeof_auth_data=%d\n", sizeof (auth_data)); 411 printf ("sizeof_auth_data=%d\n", sizeof (auth_data));
353 printf ("sizeof_rsa_data=%d\n", sizeof (rsa_data)); 412 printf ("sizeof_rsa_data=%d\n", sizeof (rsa_data));
354 printf ("sizeof_rsa_data_pad=%d\n", sizeof (((rsa_data *)0)->pad)); 413 printf ("sizeof_rsa_data_extra_auth=%d\n", sizeof (((rsa_data *)0)->extra_auth));
355 printf ("raw_overhead=%d\n", VPE_OVERHEAD); 414 printf ("raw_overhead=%d\n", VPE_OVERHEAD);
356 printf ("vpn_overhead=%d\n", VPE_OVERHEAD + 6 + 6); 415 printf ("vpn_overhead=%d\n", VPE_OVERHEAD + 6 + 6);
357 printf ("udp_overhead=%d\n", UDP_OVERHEAD + VPE_OVERHEAD + 6 + 6); 416 printf ("udp_overhead=%d\n", UDP_OVERHEAD + VPE_OVERHEAD + 6 + 6);
358 exit (EXIT_SUCCESS); 417 exit (EXIT_SUCCESS);
359 } 418 }
360 419
420 if (generate_key)
421 {
422 RAND_load_file (conf.seed_dev, SEED_SIZE);
423 exit (keygen_one (generate_key));
424 }
425
361 if (generate_keys) 426 if (generate_keys)
362 { 427 {
363 RAND_load_file (conf.seed_dev, SEED_SIZE); 428 RAND_load_file (conf.seed_dev, SEED_SIZE);
364 exit (keygen (generate_keys)); 429 exit (keygen_all ());
365 } 430 }
366 431
367 if (kill_gvpe) 432 if (kill_gvpe)
368 exit (kill_other (kill_gvpe)); 433 exit (kill_other (kill_gvpe));
369 434

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines