GNUnet  0.19.2
crypto_ecc_setup.c
Go to the documentation of this file.
1 /*
2  This file is part of GNUnet.
3  Copyright (C) 2012, 2013, 2015, 2020 GNUnet e.V.
4 
5  GNUnet is free software: you can redistribute it and/or modify it
6  under the terms of the GNU Affero General Public License as published
7  by the Free Software Foundation, either version 3 of the License,
8  or (at your 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  Affero General Public License for more details.
14 
15  You should have received a copy of the GNU Affero General Public License
16  along with this program. If not, see <http://www.gnu.org/licenses/>.
17 
18  SPDX-License-Identifier: AGPL3.0-or-later
19  */
20 
27 #include "platform.h"
28 #include <gcrypt.h>
29 #include "gnunet_util_lib.h"
30 
31 #define LOG(kind, ...) GNUNET_log_from (kind, "util-crypto-ecc", __VA_ARGS__)
32 
33 #define LOG_STRERROR(kind, syscall) \
34  GNUNET_log_from_strerror (kind, "util-crypto-ecc", syscall)
35 
36 #define LOG_STRERROR_FILE(kind, syscall, filename) \
37  GNUNET_log_from_strerror_file (kind, "util-crypto-ecc", syscall, filename)
38 
44 #define LOG_GCRY(level, cmd, rc) \
45  do \
46  { \
47  LOG (level, \
48  _ ("`%s' failed at %s:%d with error: %s\n"), \
49  cmd, \
50  __FILE__, \
51  __LINE__, \
52  gcry_strerror (rc)); \
53  } while (0)
54 
55 
65 static enum GNUNET_GenericReturnValue
66 read_from_file (const char *filename,
67  void *buf,
68  size_t buf_size)
69 {
70  int fd;
71  struct stat sb;
72 
73  fd = open (filename,
74  O_RDONLY);
75  if (-1 == fd)
76  {
77  memset (buf,
78  0,
79  buf_size);
80  return GNUNET_SYSERR;
81  }
82  if (0 != fstat (fd,
83  &sb))
84  {
86  "stat",
87  filename);
88  GNUNET_assert (0 == close (fd));
89  memset (buf,
90  0,
91  buf_size);
92  return GNUNET_SYSERR;
93  }
94  if (sb.st_size != buf_size)
95  {
97  "File `%s' has wrong size (%llu), expected %llu bytes\n",
98  filename,
99  (unsigned long long) sb.st_size,
100  (unsigned long long) buf_size);
101  GNUNET_assert (0 == close (fd));
102  memset (buf,
103  0,
104  buf_size);
105  return GNUNET_SYSERR;
106  }
107  if (buf_size !=
108  read (fd,
109  buf,
110  buf_size))
111  {
113  "read",
114  filename);
115  GNUNET_assert (0 == close (fd));
116  memset (buf,
117  0,
118  buf_size);
119  return GNUNET_SYSERR;
120  }
121  GNUNET_assert (0 == close (fd));
122  return GNUNET_OK;
123 }
124 
125 
145  int do_create,
147 {
149 
150  if (GNUNET_OK ==
152  pkey,
153  sizeof (*pkey)))
154  {
155  /* file existed, report that we didn't create it... */
156  return (do_create) ? GNUNET_NO : GNUNET_OK;
157  }
158  else if (! do_create)
159  {
160  return GNUNET_SYSERR;
161  }
162 
165  pkey,
166  sizeof (*pkey),
168  if ( (GNUNET_OK == ret) ||
169  (GNUNET_SYSERR == ret) )
170  return ret;
171  /* maybe another process succeeded in the meantime, try reading one more time */
172  if (GNUNET_OK ==
174  pkey,
175  sizeof (*pkey)))
176  {
177  /* file existed, report that *we* didn't create it... */
178  return GNUNET_NO;
179  }
180  /* give up */
181  return GNUNET_SYSERR;
182 }
183 
184 
202  int do_create,
204 {
205  if (GNUNET_OK ==
207  pkey,
208  sizeof (*pkey)))
209  {
210  /* file existed, report that we didn't create it... */
211  return (do_create) ? GNUNET_NO : GNUNET_OK;
212  }
213  else if (! do_create)
214  {
215  return GNUNET_SYSERR;
216  }
218  if (GNUNET_OK ==
220  pkey,
221  sizeof (*pkey),
223  return GNUNET_OK;
224  /* maybe another process succeeded in the meantime, try reading one more time */
225  if (GNUNET_OK ==
227  pkey,
228  sizeof (*pkey)))
229  {
230  /* file existed, report that *we* didn't create it... */
231  return GNUNET_NO;
232  }
233  /* give up */
234  return GNUNET_SYSERR;
235 }
236 
237 
248  const struct GNUNET_CONFIGURATION_Handle *cfg)
249 {
250  struct GNUNET_CRYPTO_EddsaPrivateKey *priv;
251  char *fn;
252 
253  if (GNUNET_OK !=
255  "PEER",
256  "PRIVATE_KEY",
257  &fn))
258  return NULL;
261  GNUNET_YES,
262  priv))
263  {
264  GNUNET_free (fn);
265  GNUNET_free (priv);
266  return NULL;
267  }
268  GNUNET_free (fn);
269  return priv;
270 }
271 
272 
275  struct GNUNET_PeerIdentity *dst)
276 {
277  struct GNUNET_CRYPTO_EddsaPrivateKey *priv;
278 
280  {
282  _ ("Could not load peer's private key\n"));
283  return GNUNET_SYSERR;
284  }
286  &dst->public_key);
287  GNUNET_free (priv);
288  return GNUNET_OK;
289 }
290 
291 
300 void
301 GNUNET_CRYPTO_eddsa_setup_key (const char *cfg_name)
302 {
304  struct GNUNET_CRYPTO_EddsaPrivateKey *priv;
305 
307  (void) GNUNET_CONFIGURATION_load (cfg, cfg_name);
309  if (NULL != priv)
310  GNUNET_free (priv);
312 }
313 
314 
315 /* end of crypto_ecc_setup.c */
static enum GNUNET_GenericReturnValue read_from_file(const char *filename, void *buf, size_t buf_size)
Read file to buf.
void GNUNET_CRYPTO_eddsa_setup_key(const char *cfg_name)
Setup a key file for a peer given the name of the configuration file (!).
static const struct GNUNET_CONFIGURATION_Handle * cfg
Configuration we are using.
Definition: gnunet-abd.c:36
static int ret
Return value of the commandline.
Definition: gnunet-abd.c:81
static char * filename
static char * pkey
Public key of the zone to look in, in ASCII.
static char buf[2048]
enum GNUNET_GenericReturnValue GNUNET_CONFIGURATION_get_value_filename(const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section, const char *option, char **value)
Get a configuration value that should be the name of a file or directory.
struct GNUNET_CONFIGURATION_Handle * GNUNET_CONFIGURATION_create(void)
Create a new configuration object.
void GNUNET_CONFIGURATION_destroy(struct GNUNET_CONFIGURATION_Handle *cfg)
Destroy configuration object.
enum GNUNET_GenericReturnValue GNUNET_CONFIGURATION_load(struct GNUNET_CONFIGURATION_Handle *cfg, const char *filename)
Load configuration.
struct GNUNET_CRYPTO_EddsaPrivateKey * GNUNET_CRYPTO_eddsa_key_create_from_configuration(const struct GNUNET_CONFIGURATION_Handle *cfg)
Create a new private key by reading our peer's key from the file specified in the configuration.
enum GNUNET_GenericReturnValue GNUNET_CRYPTO_ecdsa_key_from_file(const char *filename, int do_create, struct GNUNET_CRYPTO_EcdsaPrivateKey *pkey)
Create a new private key by reading it from a file.
enum GNUNET_GenericReturnValue GNUNET_CRYPTO_eddsa_key_from_file(const char *filename, int do_create, struct GNUNET_CRYPTO_EddsaPrivateKey *pkey)
Create a new private key by reading it from a file.
void GNUNET_CRYPTO_eddsa_key_get_public(const struct GNUNET_CRYPTO_EddsaPrivateKey *priv, struct GNUNET_CRYPTO_EddsaPublicKey *pub)
Extract the public key for the given private key.
Definition: crypto_ecc.c:197
void GNUNET_CRYPTO_eddsa_key_create(struct GNUNET_CRYPTO_EddsaPrivateKey *pk)
Create a new private key.
Definition: crypto_ecc.c:461
void GNUNET_CRYPTO_ecdsa_key_create(struct GNUNET_CRYPTO_EcdsaPrivateKey *pk)
Create a new private key.
Definition: crypto_ecc.c:446
enum GNUNET_GenericReturnValue GNUNET_CRYPTO_get_peer_identity(const struct GNUNET_CONFIGURATION_Handle *cfg, struct GNUNET_PeerIdentity *dst)
Retrieve the identity of the host's peer.
enum GNUNET_GenericReturnValue GNUNET_DISK_fn_write(const char *fn, const void *buf, size_t buf_size, enum GNUNET_DISK_AccessPermissions mode)
Write a buffer to a file atomically.
Definition: disk.c:724
@ GNUNET_DISK_PERM_USER_READ
Owner can read.
#define GNUNET_log(kind,...)
GNUNET_GenericReturnValue
Named constants for return values.
@ GNUNET_OK
@ GNUNET_YES
@ GNUNET_NO
@ GNUNET_SYSERR
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
#define GNUNET_log_strerror_file(level, cmd, filename)
Log an error message at log-level 'level' that indicates a failure of the command 'cmd' with the mess...
@ GNUNET_ERROR_TYPE_WARNING
@ GNUNET_ERROR_TYPE_ERROR
#define GNUNET_new(type)
Allocate a struct or union of the given type.
#define GNUNET_free(ptr)
Wrapper around free.
#define _(String)
GNU gettext support macro.
Definition: platform.h:177
Private ECC key encoded for transmission.
Private ECC key encoded for transmission.
The identity of the host (wraps the signing key of the peer).
struct GNUNET_CRYPTO_EddsaPublicKey public_key