tor  0.4.0.1-alpha
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  ! tor_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 
138 int
140  const curve25519_public_key_t *pkey)
141 {
142  char buf[128];
143  base64_encode(buf, sizeof(buf),
144  (const char*)pkey->public_key, CURVE25519_PUBKEY_LEN, 0);
145  buf[CURVE25519_BASE64_PADDED_LEN] = '\0';
146  memcpy(output, buf, CURVE25519_BASE64_PADDED_LEN+1);
147  return 0;
148 }
149 
153 int
155  const char *input)
156 {
157  size_t len = strlen(input);
158  if (len == CURVE25519_BASE64_PADDED_LEN - 1) {
159  /* not padded */
160  return digest256_from_base64((char*)pkey->public_key, input);
161  } else if (len == CURVE25519_BASE64_PADDED_LEN) {
162  char buf[128];
163  if (base64_decode(buf, sizeof(buf), input, len) != CURVE25519_PUBKEY_LEN)
164  return -1;
165  memcpy(pkey->public_key, buf, CURVE25519_PUBKEY_LEN);
166  return 0;
167  } else {
168  return -1;
169  }
170 }
171 
176 const char *
178 {
179  static char formatted[ED25519_BASE64_LEN+1];
180  if (pkey) {
181  if (ed25519_public_key_is_zero(pkey)) {
182  strlcpy(formatted, "<unset>", sizeof(formatted));
183  } else {
184  int r = ed25519_public_to_base64(formatted, pkey);
185  tor_assert(!r);
186  }
187  } else {
188  strlcpy(formatted, "<null>", sizeof(formatted));
189  }
190  return formatted;
191 }
192 
196 int
198  const char *input)
199 {
200  return digest256_from_base64((char*)pkey->pubkey, input);
201 }
202 
207 int
209  const ed25519_public_key_t *pkey)
210 {
211  return digest256_to_base64(output, (const char *)pkey->pubkey);
212 }
213 
218 int
220  const ed25519_signature_t *sig)
221 {
222  char buf[256];
223  int n = base64_encode_nopad(buf, sizeof(buf), sig->sig, ED25519_SIG_LEN);
224  tor_assert(n == ED25519_SIG_BASE64_LEN);
225  memcpy(output, buf, ED25519_SIG_BASE64_LEN+1);
226  return 0;
227 }
228 
232 int
234  const char *input)
235 {
236 
237  if (strlen(input) != ED25519_SIG_BASE64_LEN)
238  return -1;
239  char buf[ED25519_SIG_BASE64_LEN+3];
240  memcpy(buf, input, ED25519_SIG_BASE64_LEN);
241  buf[ED25519_SIG_BASE64_LEN+0] = '=';
242  buf[ED25519_SIG_BASE64_LEN+1] = '=';
243  buf[ED25519_SIG_BASE64_LEN+2] = 0;
244  char decoded[128];
245  int n = base64_decode(decoded, sizeof(decoded), buf, strlen(buf));
246  if (n < 0 || n != ED25519_SIG_LEN)
247  return -1;
248  memcpy(sig->sig, decoded, ED25519_SIG_LEN);
249 
250  return 0;
251 }
252 
256 /* XXXX unify with crypto_format.c code */
257 int
258 digest_to_base64(char *d64, const char *digest)
259 {
260  char buf[256];
261  base64_encode(buf, sizeof(buf), digest, DIGEST_LEN, 0);
262  buf[BASE64_DIGEST_LEN] = '\0';
263  memcpy(d64, buf, BASE64_DIGEST_LEN+1);
264  return 0;
265 }
266 
270 /* XXXX unify with crypto_format.c code */
271 int
272 digest_from_base64(char *digest, const char *d64)
273 {
274  if (base64_decode(digest, DIGEST_LEN, d64, strlen(d64)) == DIGEST_LEN)
275  return 0;
276  else
277  return -1;
278 }
279 
283  /* XXXX unify with crypto_format.c code */
284 int
285 digest256_to_base64(char *d64, const char *digest)
286 {
287  char buf[256];
288  base64_encode(buf, sizeof(buf), digest, DIGEST256_LEN, 0);
289  buf[BASE64_DIGEST256_LEN] = '\0';
290  memcpy(d64, buf, BASE64_DIGEST256_LEN+1);
291  return 0;
292 }
293 
297 /* XXXX unify with crypto_format.c code */
298 int
299 digest256_from_base64(char *digest, const char *d64)
300 {
301  if (base64_decode(digest, DIGEST256_LEN, d64, strlen(d64)) == DIGEST256_LEN)
302  return 0;
303  else
304  return -1;
305 }
int digest256_to_base64(char *d64, const char *digest)
Header for printf.c.
Header for smartlist.c.
int tor_mem_is_zero(const char *mem, size_t len)
Definition: util_string.c:74
int ed25519_signature_to_base64(char *output, const ed25519_signature_t *sig)
void smartlist_add(smartlist_t *sl, void *element)
int strcmpend(const char *s1, const char *s2)
Definition: util_string.c:245
int ed25519_public_to_base64(char *output, const ed25519_public_key_t *pkey)
int digest256_from_base64(char *digest, const char *d64)
int digest_to_base64(char *d64, const char *digest)
int strcmpstart(const char *s1, const char *s2)
Definition: util_string.c:209
#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)
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)
int curve25519_public_to_base64(char *output, const curve25519_public_key_t *pkey)
Header for binascii.c.
Header for crypto_ed25519.c.
#define RFTS_IGNORE_MISSING
Definition: files.h:97
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:327
int base64_decode(char *dest, size_t destlen, const char *src, size_t srclen)
Definition: binascii.c:394
#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