aboutsummaryrefslogtreecommitdiff
path: root/src/util/gnunet-ecc.c
diff options
context:
space:
mode:
authorMatthias Wachs <wachs@net.in.tum.de>2013-11-12 12:10:19 +0000
committerMatthias Wachs <wachs@net.in.tum.de>2013-11-12 12:10:19 +0000
commitd02c1b6f39f908b095b2ba663f3277829e90d908 (patch)
tree8349358cad83592cfe4c8c12068604df9caa745e /src/util/gnunet-ecc.c
parentc7954a8ae4bd0e745de626e22b72328e7b2491f4 (diff)
downloadgnunet-d02c1b6f39f908b095b2ba663f3277829e90d908.tar.gz
gnunet-d02c1b6f39f908b095b2ba663f3277829e90d908.zip
functionality to print ids in hostkey file
Diffstat (limited to 'src/util/gnunet-ecc.c')
-rw-r--r--src/util/gnunet-ecc.c93
1 files changed, 93 insertions, 0 deletions
diff --git a/src/util/gnunet-ecc.c b/src/util/gnunet-ecc.c
index e76321e5e..f2d2756fb 100644
--- a/src/util/gnunet-ecc.c
+++ b/src/util/gnunet-ecc.c
@@ -30,6 +30,16 @@
30 30
31 31
32/** 32/**
33 * Flag for listing public key.
34 */
35static int list_keys;
36
37/**
38 * Flag for listing public key.
39 */
40static int list_keys_count;
41
42/**
33 * Flag for printing public key. 43 * Flag for printing public key.
34 */ 44 */
35static int print_public_key; 45static int print_public_key;
@@ -97,6 +107,77 @@ create_keys (const char *fn)
97} 107}
98 108
99 109
110static void
111print_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
100/** 181/**
101 * Main function that will be run by the scheduler. 182 * Main function that will be run by the scheduler.
102 * 183 *
@@ -119,6 +200,11 @@ run (void *cls, char *const *args, const char *cfgfile,
119 _("No hostkey file specified on command line\n")); 200 _("No hostkey file specified on command line\n"));
120 return; 201 return;
121 } 202 }
203 if (list_keys)
204 {
205 print_key(args[0]);
206 return;
207 }
122 if (make_keys > 0) 208 if (make_keys > 0)
123 { 209 {
124 create_keys (args[0]); 210 create_keys (args[0]);
@@ -163,7 +249,14 @@ run (void *cls, char *const *args, const char *cfgfile,
163int 249int
164main (int argc, char *const *argv) 250main (int argc, char *const *argv)
165{ 251{
252 list_keys_count = UINT32_MAX;
166 static const struct GNUNET_GETOPT_CommandLineOption options[] = { 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 },
167 { 'g', "generate-keys", "COUNT", 260 { 'g', "generate-keys", "COUNT",
168 gettext_noop ("create COUNT public-private key pairs (for testing)"), 261 gettext_noop ("create COUNT public-private key pairs (for testing)"),
169 1, &GNUNET_GETOPT_set_uint, &make_keys }, 262 1, &GNUNET_GETOPT_set_uint, &make_keys },