tor  0.4.2.0-alpha-dev
crypto_format.c
Go to the documentation of this file.
1 /* Copyright (c) 2001, Matej Pfajfar.
2  * Copyright (c) 2001-2004, Roger Dingledine.
3  * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
4  * Copyright (c) 2007-2019, The Tor Project, Inc. */
5 /* See LICENSE for licensing information */
6 
13 #include "orconfig.h"
14 #ifdef HAVE_SYS_STAT_H
15 #include <sys/stat.h>
16 #endif
24 #include "lib/string/util_string.h"
25 #include "lib/string/printf.h"
26 #include "lib/encoding/binascii.h"
27 #include "lib/log/log.h"
28 #include "lib/log/util_bug.h"
29 #include "lib/fs/files.h"
30 
31 #include <string.h>
32 #include <errno.h>
33 
41 int
43  const char *typestring,
44  const char *tag,
45  const uint8_t *data,
46  size_t datalen)
47 {
48  char header[32];
49  smartlist_t *chunks = smartlist_new();
50  sized_chunk_t ch0, ch1;
51  int r = -1;
52 
53  memset(header, 0, sizeof(header));
54  if (tor_snprintf(header, sizeof(header),
55  "== %s: %s ==", typestring, tag) < 0)
56  goto end;
57  ch0.bytes = header;
58  ch0.len = 32;
59  ch1.bytes = (const char*) data;
60  ch1.len = datalen;
61  smartlist_add(chunks, &ch0);
62  smartlist_add(chunks, &ch1);
63 
64  r = write_chunks_to_file(fname, chunks, 1, 0);
65 
66  end:
67  smartlist_free(chunks);
68  return r;
69 }
70 
76 ssize_t
78  const char *typestring,
79  char **tag_out,
80  uint8_t *data_out,
81  ssize_t data_out_len)
82 {
83  char prefix[33];
84  char *content = NULL;
85  struct stat st;
86  ssize_t r = -1;
87  size_t st_size = 0;
88  int saved_errno = 0;
89 
90  *tag_out = NULL;
91  st.st_size = 0;
92  content = read_file_to_str(fname, RFTS_BIN|RFTS_IGNORE_MISSING, &st);
93  if (! content) {
94  saved_errno = errno;
95  goto end;
96  }
97  if (st.st_size < 32 || st.st_size > 32 + data_out_len) {
98  saved_errno = EINVAL;
99  goto end;
100  }
101  st_size = (size_t)st.st_size;
102 
103  memcpy(prefix, content, 32);
104  prefix[32] = 0;
105  /* Check type, extract tag. */
106  if (strcmpstart(prefix, "== ") || strcmpend(prefix, " ==") ||
107  ! fast_mem_is_zero(prefix+strlen(prefix), 32-strlen(prefix))) {
108  saved_errno = EINVAL;
109  goto end;
110  }
111 
112  if (strcmpstart(prefix+3, typestring) ||
113  3+strlen(typestring) >= 32 ||
114  strcmpstart(prefix+3+strlen(typestring), ": ")) {
115  saved_errno = EINVAL;
116  goto end;
117  }
118 
119  *tag_out = tor_strndup(prefix+5+strlen(typestring),
120  strlen(prefix)-8-strlen(typestring));
121 
122  memcpy(data_out, content+32, st_size-32);
123  r = st_size - 32;
124 
125  end:
126  if (content)
127  memwipe(content, 0, st_size);
128  tor_free(content);
129  if (saved_errno)
130  errno = saved_errno;
131  return r;
132 }
133 
142 void
144  const curve25519_public_key_t *pkey)
145 {
146  char buf[128];
147  int n = base64_encode(buf, sizeof(buf),
148  (const char*)pkey->public_key,
150  /* These asserts should always succeed, unless there is a bug in
151  * base64_encode(). */
152  tor_assert(n == CURVE25519_BASE64_PADDED_LEN);
153  tor_assert(buf[CURVE25519_BASE64_PADDED_LEN] == '\0');
154  memcpy(output, buf, CURVE25519_BASE64_PADDED_LEN+1);
155 }
156 
160 int
162  const char *input)
163 {
164  size_t len = strlen(input);
165  if (len == CURVE25519_BASE64_PADDED_LEN - 1) {
166  /* not padded */
167  return digest256_from_base64((char*)pkey->public_key, input);
168  } else if (len == CURVE25519_BASE64_PADDED_LEN) {
169  char buf[128];
170  if (base64_decode(buf, sizeof(buf), input, len) != CURVE25519_PUBKEY_LEN)
171  return -1;
172  memcpy(pkey->public_key, buf, CURVE25519_PUBKEY_LEN);
173  return 0;
174  } else {
175  return -1;
176  }
177 }
178 
183 const char *
185 {
186  static char formatted[ED25519_BASE64_LEN+1];
187  if (pkey) {
188  if (ed25519_public_key_is_zero(pkey)) {
189  strlcpy(formatted, "<unset>", sizeof(formatted));
190  } else {
191  ed25519_public_to_base64(formatted, pkey);
192  }
193  } else {
194  strlcpy(formatted, "<null>", sizeof(formatted));
195  }
196  return formatted;
197 }
198 
202 int
204  const char *input)
205 {
206  return digest256_from_base64((char*)pkey->pubkey, input);
207 }
208 
217 void
219  const ed25519_public_key_t *pkey)
220 {
221  digest256_to_base64(output, (const char *)pkey->pubkey);
222 }
223 
229 void
231  const ed25519_signature_t *sig)
232 {
233  char buf[256];
234  int n = base64_encode_nopad(buf, sizeof(buf), sig->sig, ED25519_SIG_LEN);
235  /* These asserts should always succeed, unless there is a bug in
236  * base64_encode_nopad(). */
237  tor_assert(n == ED25519_SIG_BASE64_LEN);
238  tor_assert(buf[ED25519_SIG_BASE64_LEN] == '\0');
239  memcpy(output, buf, ED25519_SIG_BASE64_LEN+1);
240 }
241 
245 int
247  const char *input)
248 {
249  if (strlen(input) != ED25519_SIG_BASE64_LEN)
250  return -1;
251  char decoded[128];
252  int n = base64_decode(decoded, sizeof(decoded), input,
253  ED25519_SIG_BASE64_LEN);
254  if (n < 0 || n != ED25519_SIG_LEN)
255  return -1;
256  memcpy(sig->sig, decoded, ED25519_SIG_LEN);
257 
258  return 0;
259 }
260 
265 void
266 digest_to_base64(char *d64, const char *digest)
267 {
268  char buf[256];
269  int n = base64_encode_nopad(buf, sizeof(buf),
270  (const uint8_t *)digest, DIGEST_LEN);
271  /* These asserts should always succeed, unless there is a bug in
272  * base64_encode_nopad(). */
274  tor_assert(buf[BASE64_DIGEST_LEN] == '\0');
275  memcpy(d64, buf, BASE64_DIGEST_LEN+1);
276 }
277 
281 int
282 digest_from_base64(char *digest, const char *d64)
283 {
284  if (base64_decode(digest, DIGEST_LEN, d64, strlen(d64)) == DIGEST_LEN)
285  return 0;
286  else
287  return -1;
288 }
289 
294 void
295 digest256_to_base64(char *d64, const char *digest)
296 {
297  char buf[256];
298  int n = base64_encode_nopad(buf, sizeof(buf),
299  (const uint8_t *)digest, DIGEST256_LEN);
300  /* These asserts should always succeed, unless there is a bug in
301  * base64_encode_nopad(). */
303  tor_assert(buf[BASE64_DIGEST256_LEN] == '\0');
304  memcpy(d64, buf, BASE64_DIGEST256_LEN+1);
305 }
306 
310 int
311 digest256_from_base64(char *digest, const char *d64)
312 {
313  if (base64_decode(digest, DIGEST256_LEN, d64, strlen(d64)) == DIGEST256_LEN)
314  return 0;
315  else
316  return -1;
317 }
Header for printf.c.
Header for smartlist.c.
void curve25519_public_to_base64(char *output, const curve25519_public_key_t *pkey)
void smartlist_add(smartlist_t *sl, void *element)
int strcmpend(const char *s1, const char *s2)
Definition: util_string.c:242
int digest256_from_base64(char *digest, const char *d64)
void digest_to_base64(char *d64, const char *digest)
int fast_mem_is_zero(const char *mem, size_t len)
Definition: util_string.c:74
void ed25519_public_to_base64(char *output, const ed25519_public_key_t *pkey)
int strcmpstart(const char *s1, const char *s2)
Definition: util_string.c:206
#define tor_free(p)
Definition: malloc.h:52
Header for util_string.c.
void memwipe(void *mem, uint8_t byte, size_t sz)
Definition: crypto_util.c:57
int curve25519_public_from_base64(curve25519_public_key_t *pkey, const char *input)
void ed25519_signature_to_base64(char *output, const ed25519_signature_t *sig)
int base64_encode(char *dest, size_t destlen, const char *src, size_t srclen, int flags)
Definition: binascii.c:215
#define DIGEST256_LEN
Definition: digest_sizes.h:23
int digest_from_base64(char *digest, const char *d64)
int crypto_write_tagged_contents_to_file(const char *fname, const char *typestring, const char *tag, const uint8_t *data, size_t datalen)
Definition: crypto_format.c:42
const char * ed25519_fmt(const ed25519_public_key_t *pkey)
Common functions for cryptographic routines.
tor_assert(buffer)
ssize_t crypto_read_tagged_contents_from_file(const char *fname, const char *typestring, char **tag_out, uint8_t *data_out, ssize_t data_out_len)
Definition: crypto_format.c:77
Header for crypto_format.c.
#define DIGEST_LEN
Definition: digest_sizes.h:20
int ed25519_public_key_is_zero(const ed25519_public_key_t *pubkey)
Header for binascii.c.
Header for crypto_ed25519.c.
#define RFTS_IGNORE_MISSING
Definition: files.h:97
void digest256_to_base64(char *d64, const char *digest)
Headers for crypto_digest.c.
int tor_snprintf(char *str, size_t size, const char *format,...)
Definition: printf.c:27
int base64_encode_nopad(char *dest, size_t destlen, const uint8_t *src, size_t srclen)
Definition: binascii.c:329
int base64_decode(char *dest, size_t destlen, const char *src, size_t srclen)
Definition: binascii.c:396
#define RFTS_BIN
Definition: files.h:95
#define BASE64_DIGEST_LEN
Definition: crypto_digest.h:26
Header for compat_string.c.
int ed25519_public_from_base64(ed25519_public_key_t *pkey, const char *input)
Wrappers for reading and writing data to files on disk.
#define BASE64_DIGEST256_LEN
Definition: crypto_digest.h:29
Headers for log.c.
int ed25519_signature_from_base64(ed25519_signature_t *sig, const char *input)
Macros to manage assertions, fatal and non-fatal.
Header for crypto_curve25519.c.
#define CURVE25519_PUBKEY_LEN
Definition: x25519_sizes.h:20