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.12 by root, Sun Mar 6 13:49:50 2011 UTC vs.
Revision 1.17 by root, Fri Jul 19 18:18:28 2013 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-2011 Marc Lehmann <gvpe@schmorp.de> 5 2003-2013 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
44#include <sys/stat.h> 44#include <sys/stat.h>
45#include <sys/types.h> 45#include <sys/types.h>
46#include <unistd.h> 46#include <unistd.h>
47#include <signal.h> 47#include <signal.h>
48 48
49#include <openssl/bn.h>
49#include <openssl/rand.h> 50#include <openssl/rand.h>
50#include <openssl/rsa.h> 51#include <openssl/rsa.h>
51#include <openssl/pem.h> 52#include <openssl/pem.h>
52#include <openssl/evp.h> 53#include <openssl/evp.h>
53 54
73/* 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 */
74static int quiet; 75static int quiet;
75 76
76/* If nonzero, generate public/private keypair for this net. */ 77/* If nonzero, generate public/private keypair for this net. */
77static int generate_keys; 78static int generate_keys;
79
80// output some debugging info, interna constants &c
81static int debug_info;
78 82
79static struct option const long_options[] = 83static struct option const long_options[] =
80{ 84{
81 {"config", required_argument, NULL, 'c'}, 85 {"config", required_argument, NULL, 'c'},
82 {"kill", optional_argument, NULL, 'k'}, 86 {"kill", optional_argument, NULL, 'k'},
83 {"help", no_argument, &show_help, 1}, 87 {"help", no_argument, &show_help, 1},
84 {"version", no_argument, &show_version, 1}, 88 {"version", no_argument, &show_version, 1},
85 {"generate-keys", no_argument, NULL, 'g'}, 89 {"generate-keys", no_argument, NULL, 'g'},
86 {"quiet", no_argument, &quiet, 1}, 90 {"quiet", no_argument, &quiet, 1},
87 {"show-config", no_argument, &show_config, 's'}, 91 {"show-config", no_argument, &show_config, 's'},
92 {"debug-info", no_argument, &debug_info, 1},
88 {NULL, 0, NULL, 0} 93 {NULL, 0, NULL, 0}
89}; 94};
90 95
91static void 96static void
92usage (int status) 97usage (int status)
162 kill_gvpe = SIGTERM; 167 kill_gvpe = SIGTERM;
163 168
164 break; 169 break;
165 170
166 case 'g': /* generate public/private keypair */ 171 case 'g': /* generate public/private keypair */
167 generate_keys = RSA_KEYBITS; 172 generate_keys = RSABITS;
168 break; 173 break;
169 174
170 case 's': 175 case 's':
171 show_config = 1; 176 show_config = 1;
172 break; 177 break;
183 } 188 }
184 } 189 }
185} 190}
186 191
187// this function prettyprints the key generation process 192// this function prettyprints the key generation process
188static void 193static int
189indicator (int a, int b, void *p) 194indicator (int a, int b, BN_GENCB *cb)
190{ 195{
191 if (quiet) 196 if (quiet)
192 return; 197 return 1;
193 198
194 switch (a) 199 switch (a)
195 { 200 {
196 case 0: 201 case 0:
197 fprintf (stderr, "."); 202 fprintf (stderr, ".");
222 break; 227 break;
223 228
224 default: 229 default:
225 fprintf (stderr, "?"); 230 fprintf (stderr, "?");
226 } 231 }
232
233 return 1;
227} 234}
228 235
229/* 236/*
230 * generate public/private RSA keypairs for all hosts that don't have one. 237 * generate public/private RSA keypairs for all hosts that don't have one.
231 */ 238 */
232static int 239static int
233keygen (int bits) 240keygen (int bits)
234{ 241{
235 RSA *rsa_key; 242 FILE *f, *pubf;
236 FILE *f;
237 char *name = NULL;
238 char *fname; 243 char *fname;
239
240 asprintf (&fname, "%s/hostkeys", confbase);
241 mkdir (fname, 0700);
242 free (fname);
243 244
244 asprintf (&fname, "%s/pubkey", confbase); 245 asprintf (&fname, "%s/pubkey", confbase);
245 mkdir (fname, 0700); 246 mkdir (fname, 0700);
246 free (fname); 247 free (fname);
247 248
248 for (configuration::node_vector::iterator i = conf.nodes.begin (); i != conf.nodes.end (); ++i) 249 for (configuration::node_vector::iterator i = conf.nodes.begin (); i != conf.nodes.end (); ++i)
249 { 250 {
250 conf_node *node = *i; 251 conf_node *node = *i;
251 252
252 asprintf (&fname, "%s/pubkey/%s", confbase, node->nodename); 253 ::thisnode = node->nodename;
253 254
255 fname = conf.config_filename (conf.prikeyfile, "hostkey");
256
254 f = fopen (fname, "a"); 257 f = fopen (fname, "ab");
255 258
256 /* some libcs are buggy and require an extra seek to the end */ 259 /* some libcs are buggy and require an extra seek to the end */
257 if (!f || fseek (f, 0, SEEK_END)) 260 if (!f || fseek (f, 0, SEEK_END))
258 { 261 {
259 perror (fname); 262 perror (fname);
261 } 264 }
262 265
263 if (ftell (f)) 266 if (ftell (f))
264 { 267 {
265 if (!quiet) 268 if (!quiet)
266 fprintf (stderr, "'%s' already exists, skipping this node %d\n", 269 fprintf (stderr, "'%s' already exists, skipping node %s\n", fname, node->nodename);
267 fname, quiet);
268 270
271 free (fname);
269 fclose (f); 272 fclose (f);
270 continue; 273 continue;
271 } 274 }
272 275
273 fprintf (stderr, _("generating %d bits key for %s:\n"), bits,
274 node->nodename);
275
276 rsa_key = RSA_generate_key (bits, 0xFFFF, indicator, NULL);
277
278 if (!rsa_key)
279 {
280 fprintf (stderr, _("error during key generation!\n"));
281 return -1;
282 }
283 else
284 fprintf (stderr, _("Done.\n"));
285
286 require (PEM_write_RSAPublicKey (f, rsa_key));
287 fclose (f);
288 free (fname); 276 free (fname);
289 277
290 asprintf (&fname, "%s/hostkeys/%s", confbase, node->nodename); 278 fprintf (stderr, _("generating %d bits key for %s:\n"), bits, node->nodename);
291 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);
292 f = fopen (fname, "a"); 291 pubf = fopen (fname, "wb");
293 if (!f) 292 if (!pubf)
294 { 293 {
295 perror (fname); 294 perror (fname);
296 exit (EXIT_FAILURE); 295 exit (EXIT_FAILURE);
297 } 296 }
298 297
298 free (fname);
299
300 require (PEM_write_RSAPublicKey (pubf, rsa));
301 fclose (pubf);
302
299 require (PEM_write_RSAPrivateKey (f, rsa_key, NULL, NULL, 0, NULL, NULL)); 303 require (PEM_write_RSAPrivateKey (f, rsa, NULL, NULL, 0, NULL, NULL));
300 fclose (f); 304 fclose (f);
305
301 free (fname); 306 BN_free (e);
307 RSA_free (rsa);
302 } 308 }
303 309
304 return 0; 310 return 0;
305} 311}
306 312
320 { 326 {
321 printf (_("%s version %s (built %s %s, protocol version %d.%d)\n"), get_identity (), 327 printf (_("%s version %s (built %s %s, protocol version %d.%d)\n"), get_identity (),
322 VERSION, __DATE__, __TIME__, PROTOCOL_MAJOR, PROTOCOL_MINOR); 328 VERSION, __DATE__, __TIME__, PROTOCOL_MAJOR, PROTOCOL_MINOR);
323 printf (_("Built with kernel interface %s/%s.\n"), IFTYPE, IFSUBTYPE); 329 printf (_("Built with kernel interface %s/%s.\n"), IFTYPE, IFSUBTYPE);
324 printf (_ 330 printf (_
325 ("Copyright (C) 2003-2008 Marc Lehmann <gvpe@schmorp.de> and others.\n" 331 ("Copyright (C) 2003-2013 Marc Lehmann <gvpe@schmorp.de> and others.\n"
326 "See the AUTHORS file for a complete list.\n\n" 332 "See the AUTHORS file for a complete list.\n\n"
327 "vpe comes with ABSOLUTELY NO WARRANTY. This is free software,\n" 333 "vpe comes with ABSOLUTELY NO WARRANTY. This is free software,\n"
328 "and you are welcome to redistribute it under certain conditions;\n" 334 "and you are welcome to redistribute it under certain conditions;\n"
329 "see the file COPYING for details.\n")); 335 "see the file COPYING for details.\n"));
330 336
336 342
337 { 343 {
338 configuration_parser (conf, false, 0, 0); 344 configuration_parser (conf, false, 0, 0);
339 } 345 }
340 346
347 if (debug_info)
348 {
349 printf ("cipher_nid=%d\n", EVP_CIPHER_nid (CIPHER ()));
350 printf ("mac_nid=%d\n", EVP_MD_type (MAC_DIGEST ()));
351 printf ("auth_nid=%d\n", EVP_MD_type (AUTH_DIGEST ()));
352 printf ("sizeof_auth_data=%d\n", sizeof (auth_data));
353 printf ("sizeof_rsa_data=%d\n", sizeof (rsa_data));
354 printf ("sizeof_rsa_data_extra_auth=%d\n", sizeof (((rsa_data *)0)->extra_auth));
355 printf ("raw_overhead=%d\n", VPE_OVERHEAD);
356 printf ("vpn_overhead=%d\n", VPE_OVERHEAD + 6 + 6);
357 printf ("udp_overhead=%d\n", UDP_OVERHEAD + VPE_OVERHEAD + 6 + 6);
358 exit (EXIT_SUCCESS);
359 }
360
341 if (generate_keys) 361 if (generate_keys)
342 { 362 {
343 RAND_load_file ("/dev/urandom", 1024); 363 RAND_load_file (conf.seed_dev, SEED_SIZE);
344 exit (keygen (generate_keys)); 364 exit (keygen (generate_keys));
345 } 365 }
346 366
347 if (kill_gvpe) 367 if (kill_gvpe)
348 exit (kill_other (kill_gvpe)); 368 exit (kill_other (kill_gvpe));
353 exit (EXIT_SUCCESS); 373 exit (EXIT_SUCCESS);
354 } 374 }
355 375
356 usage (1); 376 usage (1);
357} 377}
378

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines