Add a third default.
[oweals/gnunet.git] / src / util / gnunet-ecc.c
1 /*
2      This file is part of GNUnet.
3      (C) 2012, 2013 Christian Grothoff (and other contributing authors)
4
5      GNUnet is free software; you can redistribute it and/or modify
6      it under the terms of the GNU General Public License as published
7      by the Free Software Foundation; either version 3, or (at your
8      option) any later version.
9
10      GNUnet is distributed in the hope that it will be useful, but
11      WITHOUT ANY WARRANTY; without even the implied warranty of
12      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13      General Public License for more details.
14
15      You should have received a copy of the GNU General Public License
16      along with GNUnet; see the file COPYING.  If not, write to the
17      Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18      Boston, MA 02111-1307, USA.
19 */
20
21 /**
22  * @file util/gnunet-ecc.c
23  * @brief tool to manipulate EDDSA key files
24  * @author Christian Grothoff
25  */
26 #include "platform.h"
27 #include "gnunet_util_lib.h"
28 #include "gnunet_testing_lib.h"
29 #include <gcrypt.h>
30
31
32 /**
33  * Flag for listing public key.
34  */
35 static int list_keys;
36
37 /**
38  * Flag for listing public key.
39  */
40 static int list_keys_count;
41
42 /**
43  * Flag for printing public key.
44  */
45 static int print_public_key;
46
47 /**
48  * Flag for printing hash of public key.
49  */
50 static int print_peer_identity;
51
52 /**
53  * Option set to create a bunch of keys at once.
54  */
55 static unsigned int make_keys;
56
57
58 /**
59  * Create a flat file with a large number of key pairs for testing.
60  */
61 static void
62 create_keys (const char *fn)
63 {
64   FILE *f;
65   struct GNUNET_CRYPTO_EddsaPrivateKey *pk;
66
67   if (NULL == (f = fopen (fn, "w+")))
68   {
69     fprintf (stderr,
70              _("Failed to open `%s': %s\n"),
71              fn,
72              STRERROR (errno));
73     return;
74   }
75   fprintf (stderr,
76            _("Generating %u keys, please wait"),
77            make_keys);
78   while (0 < make_keys--)
79   {
80     fprintf (stderr,
81              ".");
82     if (NULL == (pk = GNUNET_CRYPTO_eddsa_key_create ()))
83     {
84        GNUNET_break (0);
85        break;
86     }
87     if (GNUNET_TESTING_HOSTKEYFILESIZE !=
88         fwrite (pk, 1,
89                 GNUNET_TESTING_HOSTKEYFILESIZE, f))
90     {
91       fprintf (stderr,
92                _("\nFailed to write to `%s': %s\n"),
93                fn,
94                STRERROR (errno));
95       GNUNET_free (pk);
96       break;
97     }
98     GNUNET_free (pk);
99   }
100   if (UINT_MAX == make_keys)
101     fprintf (stderr,
102              _("\nFinished!\n"));
103   else
104     fprintf (stderr,
105              _("\nError, %u keys not generated\n"), make_keys);
106   fclose (f);
107 }
108
109
110 static void
111 print_key (const char *filename)
112 {
113   struct GNUNET_DISK_FileHandle *fd;
114   struct GNUNET_CRYPTO_EddsaPrivateKey private_key;
115   struct GNUNET_CRYPTO_EddsaPublicKey public_key;
116   char *hostkeys_data;
117   char *hostkey_str;
118   uint64_t fs;
119   unsigned int total_hostkeys;
120   unsigned int c;
121
122   if (GNUNET_YES != GNUNET_DISK_file_test (filename))
123   {
124     fprintf (stderr, _("Hostkeys file not found: %s\n"), filename);
125     return;
126   }
127
128   /* Check hostkey file size, read entire thing into memory */
129   if (GNUNET_OK != GNUNET_DISK_file_size (filename, &fs, GNUNET_YES, GNUNET_YES))
130     fs = 0;
131   if (0 == fs)
132   {
133     fprintf (stderr, _("Hostkeys file is empty: %s\n"), filename);
134     return;       /* File is empty */
135   }
136   if (0 != (fs % GNUNET_TESTING_HOSTKEYFILESIZE))
137   {
138     fprintf (stderr,
139          _("Incorrect hostkey file format: %s\n"), filename);
140     return;
141   }
142   fd = GNUNET_DISK_file_open (filename, GNUNET_DISK_OPEN_READ,
143                                          GNUNET_DISK_PERM_NONE);
144   if (NULL == fd)
145   {
146     GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "open", filename);
147     return;
148   }
149   hostkeys_data = GNUNET_malloc (fs);
150   if (fs != GNUNET_DISK_file_read(fd, hostkeys_data, fs))
151   {
152     fprintf (stderr,
153          _("Could not readk hostkey file: %s\n"), filename);
154     GNUNET_free (hostkeys_data);
155     return;
156   }
157   GNUNET_DISK_file_close (fd);
158
159   if (NULL == hostkeys_data)
160     return;
161   total_hostkeys = fs / GNUNET_TESTING_HOSTKEYFILESIZE;
162   for (c = 0; (c < total_hostkeys) && (c < list_keys_count); c++)
163   {
164     memcpy (&private_key,
165             hostkeys_data + (c * GNUNET_TESTING_HOSTKEYFILESIZE),
166             GNUNET_TESTING_HOSTKEYFILESIZE);
167     GNUNET_CRYPTO_eddsa_key_get_public (&private_key, &public_key);
168     hostkey_str = GNUNET_CRYPTO_eddsa_public_key_to_string (&public_key);
169     if (NULL != hostkey_str)
170     {
171       fprintf (stderr, "%4u: %s\n", c, hostkey_str);
172       GNUNET_free (hostkey_str);
173     }
174     else
175       fprintf (stderr, "%4u: %s\n", c, "invalid");
176   }
177   GNUNET_free (hostkeys_data);
178 }
179
180
181 /**
182  * Main function that will be run by the scheduler.
183  *
184  * @param cls closure
185  * @param args remaining command-line arguments
186  * @param cfgfile name of the configuration file used (for saving, can be NULL!)
187  * @param cfg configuration
188  */
189 static void
190 run (void *cls, char *const *args, const char *cfgfile,
191      const struct GNUNET_CONFIGURATION_Handle *cfg)
192 {
193   struct GNUNET_CRYPTO_EddsaPrivateKey *pk;
194   struct GNUNET_CRYPTO_EddsaPublicKey pub;
195
196   if (NULL == args[0])
197   {
198     FPRINTF (stderr,
199              "%s",
200              _("No hostkey file specified on command line\n"));
201     return;
202   }
203   if (list_keys)
204   {
205     print_key(args[0]);
206     return;
207   }
208   if (make_keys > 0)
209   {
210     create_keys (args[0]);
211     return;
212   }
213   pk = GNUNET_CRYPTO_eddsa_key_create_from_file (args[0]);
214   if (NULL == pk)
215     return;
216   if (print_public_key)
217   {
218     char *s;
219
220     GNUNET_CRYPTO_eddsa_key_get_public (pk, &pub);
221     s = GNUNET_CRYPTO_eddsa_public_key_to_string (&pub);
222     FPRINTF (stdout,
223              "%s\n",
224              s);
225     GNUNET_free (s);
226   }
227   if (print_peer_identity)
228   {
229     char *str;
230
231     GNUNET_CRYPTO_eddsa_key_get_public (pk, &pub);
232     str = GNUNET_CRYPTO_eddsa_public_key_to_string (&pub);
233     FPRINTF (stdout,
234              "%s\n",
235              str);
236     GNUNET_free (str);
237   }
238   GNUNET_free (pk);
239 }
240
241
242 /**
243  * Program to manipulate ECC key files.
244  *
245  * @param argc number of arguments from the command line
246  * @param argv command line arguments
247  * @return 0 ok, 1 on error
248  */
249 int
250 main (int argc, char *const *argv)
251 {
252   list_keys_count = UINT32_MAX;
253   static const struct GNUNET_GETOPT_CommandLineOption options[] = {
254     { 'i', "iterate", "FILE",
255       gettext_noop ("list keys included in a file (for testing)"),
256       0, &GNUNET_GETOPT_set_one, &list_keys },
257     { 'e', "end=", "COUNT",
258       gettext_noop ("number of keys to list included in a file (for testing)"),
259       1, &GNUNET_GETOPT_set_uint, &list_keys_count },
260     { 'g', "generate-keys", "COUNT",
261       gettext_noop ("create COUNT public-private key pairs (for testing)"),
262       1, &GNUNET_GETOPT_set_uint, &make_keys },
263     { 'p', "print-public-key", NULL,
264       gettext_noop ("print the public key in ASCII format"),
265       0, &GNUNET_GETOPT_set_one, &print_public_key },
266     { 'P', "print-peer-identity", NULL,
267       gettext_noop ("print the hash of the public key in ASCII format"),
268       0, &GNUNET_GETOPT_set_one, &print_peer_identity },
269     GNUNET_GETOPT_OPTION_END
270   };
271   int ret;
272
273   if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
274     return 2;
275
276   ret = (GNUNET_OK ==
277          GNUNET_PROGRAM_run (argc, argv, "gnunet-ecc [OPTIONS] keyfile",
278                              gettext_noop ("Manipulate GNUnet private ECC key files"),
279                              options, &run, NULL)) ? 0 : 1;
280   GNUNET_free ((void*) argv);
281   return ret;
282 }
283
284 /* end of gnunet-ecc.c */