crypto_format.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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. /**
  7. * \file crypto_format.c
  8. *
  9. * \brief Formatting and parsing code for crypto-related data structures.
  10. */
  11. #include "orconfig.h"
  12. #ifdef HAVE_SYS_STAT_H
  13. #include <sys/stat.h>
  14. #endif
  15. #include "lib/container/smartlist.h"
  16. #include "lib/crypt_ops/crypto_curve25519.h"
  17. #include "lib/crypt_ops/crypto_digest.h"
  18. #include "lib/crypt_ops/crypto_ed25519.h"
  19. #include "lib/crypt_ops/crypto_format.h"
  20. #include "lib/crypt_ops/crypto_util.h"
  21. #include "lib/string/compat_string.h"
  22. #include "lib/string/util_string.h"
  23. #include "lib/string/printf.h"
  24. #include "lib/encoding/binascii.h"
  25. #include "lib/log/log.h"
  26. #include "lib/log/util_bug.h"
  27. #include "lib/fs/files.h"
  28. #include <string.h>
  29. #include <errno.h>
  30. /** Write the <b>datalen</b> bytes from <b>data</b> to the file named
  31. * <b>fname</b> in the tagged-data format. This format contains a
  32. * 32-byte header, followed by the data itself. The header is the
  33. * NUL-padded string "== <b>typestring</b>: <b>tag</b> ==". The length
  34. * of <b>typestring</b> and <b>tag</b> must therefore be no more than
  35. * 24.
  36. **/
  37. int
  38. crypto_write_tagged_contents_to_file(const char *fname,
  39. const char *typestring,
  40. const char *tag,
  41. const uint8_t *data,
  42. size_t datalen)
  43. {
  44. char header[32];
  45. smartlist_t *chunks = smartlist_new();
  46. sized_chunk_t ch0, ch1;
  47. int r = -1;
  48. memset(header, 0, sizeof(header));
  49. if (tor_snprintf(header, sizeof(header),
  50. "== %s: %s ==", typestring, tag) < 0)
  51. goto end;
  52. ch0.bytes = header;
  53. ch0.len = 32;
  54. ch1.bytes = (const char*) data;
  55. ch1.len = datalen;
  56. smartlist_add(chunks, &ch0);
  57. smartlist_add(chunks, &ch1);
  58. r = write_chunks_to_file(fname, chunks, 1, 0);
  59. end:
  60. smartlist_free(chunks);
  61. return r;
  62. }
  63. /** Read a tagged-data file from <b>fname</b> into the
  64. * <b>data_out_len</b>-byte buffer in <b>data_out</b>. Check that the
  65. * typestring matches <b>typestring</b>; store the tag into a newly allocated
  66. * string in <b>tag_out</b>. Return -1 on failure, and the number of bytes of
  67. * data on success. Preserves the errno from reading the file. */
  68. ssize_t
  69. crypto_read_tagged_contents_from_file(const char *fname,
  70. const char *typestring,
  71. char **tag_out,
  72. uint8_t *data_out,
  73. ssize_t data_out_len)
  74. {
  75. char prefix[33];
  76. char *content = NULL;
  77. struct stat st;
  78. ssize_t r = -1;
  79. size_t st_size = 0;
  80. int saved_errno = 0;
  81. *tag_out = NULL;
  82. st.st_size = 0;
  83. content = read_file_to_str(fname, RFTS_BIN|RFTS_IGNORE_MISSING, &st);
  84. if (! content) {
  85. saved_errno = errno;
  86. goto end;
  87. }
  88. if (st.st_size < 32 || st.st_size > 32 + data_out_len) {
  89. saved_errno = EINVAL;
  90. goto end;
  91. }
  92. st_size = (size_t)st.st_size;
  93. memcpy(prefix, content, 32);
  94. prefix[32] = 0;
  95. /* Check type, extract tag. */
  96. if (strcmpstart(prefix, "== ") || strcmpend(prefix, " ==") ||
  97. ! tor_mem_is_zero(prefix+strlen(prefix), 32-strlen(prefix))) {
  98. saved_errno = EINVAL;
  99. goto end;
  100. }
  101. if (strcmpstart(prefix+3, typestring) ||
  102. 3+strlen(typestring) >= 32 ||
  103. strcmpstart(prefix+3+strlen(typestring), ": ")) {
  104. saved_errno = EINVAL;
  105. goto end;
  106. }
  107. *tag_out = tor_strndup(prefix+5+strlen(typestring),
  108. strlen(prefix)-8-strlen(typestring));
  109. memcpy(data_out, content+32, st_size-32);
  110. r = st_size - 32;
  111. end:
  112. if (content)
  113. memwipe(content, 0, st_size);
  114. tor_free(content);
  115. if (saved_errno)
  116. errno = saved_errno;
  117. return r;
  118. }
  119. /** Encode <b>pkey</b> as a base64-encoded string, without trailing "="
  120. * characters, in the buffer <b>output</b>, which must have at least
  121. * CURVE25519_BASE64_PADDED_LEN+1 bytes available. Return 0 on success, -1 on
  122. * failure. */
  123. int
  124. curve25519_public_to_base64(char *output,
  125. const curve25519_public_key_t *pkey)
  126. {
  127. char buf[128];
  128. base64_encode(buf, sizeof(buf),
  129. (const char*)pkey->public_key, CURVE25519_PUBKEY_LEN, 0);
  130. buf[CURVE25519_BASE64_PADDED_LEN] = '\0';
  131. memcpy(output, buf, CURVE25519_BASE64_PADDED_LEN+1);
  132. return 0;
  133. }
  134. /** Try to decode a base64-encoded curve25519 public key from <b>input</b>
  135. * into the object at <b>pkey</b>. Return 0 on success, -1 on failure.
  136. * Accepts keys with or without a trailing "=". */
  137. int
  138. curve25519_public_from_base64(curve25519_public_key_t *pkey,
  139. const char *input)
  140. {
  141. size_t len = strlen(input);
  142. if (len == CURVE25519_BASE64_PADDED_LEN - 1) {
  143. /* not padded */
  144. return digest256_from_base64((char*)pkey->public_key, input);
  145. } else if (len == CURVE25519_BASE64_PADDED_LEN) {
  146. char buf[128];
  147. if (base64_decode(buf, sizeof(buf), input, len) != CURVE25519_PUBKEY_LEN)
  148. return -1;
  149. memcpy(pkey->public_key, buf, CURVE25519_PUBKEY_LEN);
  150. return 0;
  151. } else {
  152. return -1;
  153. }
  154. }
  155. /** For logging convenience: Convert <b>pkey</b> to a statically allocated
  156. * base64 string and return it. Not threadsafe. Format not meant to be
  157. * computer-readable; it may change in the future. Subsequent calls invalidate
  158. * previous returns. */
  159. const char *
  160. ed25519_fmt(const ed25519_public_key_t *pkey)
  161. {
  162. static char formatted[ED25519_BASE64_LEN+1];
  163. if (pkey) {
  164. if (ed25519_public_key_is_zero(pkey)) {
  165. strlcpy(formatted, "<unset>", sizeof(formatted));
  166. } else {
  167. int r = ed25519_public_to_base64(formatted, pkey);
  168. tor_assert(!r);
  169. }
  170. } else {
  171. strlcpy(formatted, "<null>", sizeof(formatted));
  172. }
  173. return formatted;
  174. }
  175. /** Try to decode the string <b>input</b> into an ed25519 public key. On
  176. * success, store the value in <b>pkey</b> and return 0. Otherwise return
  177. * -1. */
  178. int
  179. ed25519_public_from_base64(ed25519_public_key_t *pkey,
  180. const char *input)
  181. {
  182. return digest256_from_base64((char*)pkey->pubkey, input);
  183. }
  184. /** Encode the public key <b>pkey</b> into the buffer at <b>output</b>,
  185. * which must have space for ED25519_BASE64_LEN bytes of encoded key,
  186. * plus one byte for a terminating NUL. Return 0 on success, -1 on failure.
  187. */
  188. int
  189. ed25519_public_to_base64(char *output,
  190. const ed25519_public_key_t *pkey)
  191. {
  192. return digest256_to_base64(output, (const char *)pkey->pubkey);
  193. }
  194. /** Encode the signature <b>sig</b> into the buffer at <b>output</b>,
  195. * which must have space for ED25519_SIG_BASE64_LEN bytes of encoded signature,
  196. * plus one byte for a terminating NUL. Return 0 on success, -1 on failure.
  197. */
  198. int
  199. ed25519_signature_to_base64(char *output,
  200. const ed25519_signature_t *sig)
  201. {
  202. char buf[256];
  203. int n = base64_encode_nopad(buf, sizeof(buf), sig->sig, ED25519_SIG_LEN);
  204. tor_assert(n == ED25519_SIG_BASE64_LEN);
  205. memcpy(output, buf, ED25519_SIG_BASE64_LEN+1);
  206. return 0;
  207. }
  208. /** Try to decode the string <b>input</b> into an ed25519 signature. On
  209. * success, store the value in <b>sig</b> and return 0. Otherwise return
  210. * -1. */
  211. int
  212. ed25519_signature_from_base64(ed25519_signature_t *sig,
  213. const char *input)
  214. {
  215. if (strlen(input) != ED25519_SIG_BASE64_LEN)
  216. return -1;
  217. char buf[ED25519_SIG_BASE64_LEN+3];
  218. memcpy(buf, input, ED25519_SIG_BASE64_LEN);
  219. buf[ED25519_SIG_BASE64_LEN+0] = '=';
  220. buf[ED25519_SIG_BASE64_LEN+1] = '=';
  221. buf[ED25519_SIG_BASE64_LEN+2] = 0;
  222. char decoded[128];
  223. int n = base64_decode(decoded, sizeof(decoded), buf, strlen(buf));
  224. if (n < 0 || n != ED25519_SIG_LEN)
  225. return -1;
  226. memcpy(sig->sig, decoded, ED25519_SIG_LEN);
  227. return 0;
  228. }
  229. /** Base64 encode DIGEST_LINE bytes from <b>digest</b>, remove the trailing =
  230. * characters, and store the nul-terminated result in the first
  231. * BASE64_DIGEST_LEN+1 bytes of <b>d64</b>. */
  232. /* XXXX unify with crypto_format.c code */
  233. int
  234. digest_to_base64(char *d64, const char *digest)
  235. {
  236. char buf[256];
  237. base64_encode(buf, sizeof(buf), digest, DIGEST_LEN, 0);
  238. buf[BASE64_DIGEST_LEN] = '\0';
  239. memcpy(d64, buf, BASE64_DIGEST_LEN+1);
  240. return 0;
  241. }
  242. /** Given a base64 encoded, nul-terminated digest in <b>d64</b> (without
  243. * trailing newline or = characters), decode it and store the result in the
  244. * first DIGEST_LEN bytes at <b>digest</b>. */
  245. /* XXXX unify with crypto_format.c code */
  246. int
  247. digest_from_base64(char *digest, const char *d64)
  248. {
  249. if (base64_decode(digest, DIGEST_LEN, d64, strlen(d64)) == DIGEST_LEN)
  250. return 0;
  251. else
  252. return -1;
  253. }
  254. /** Base64 encode DIGEST256_LINE bytes from <b>digest</b>, remove the
  255. * trailing = characters, and store the nul-terminated result in the first
  256. * BASE64_DIGEST256_LEN+1 bytes of <b>d64</b>. */
  257. /* XXXX unify with crypto_format.c code */
  258. int
  259. digest256_to_base64(char *d64, const char *digest)
  260. {
  261. char buf[256];
  262. base64_encode(buf, sizeof(buf), digest, DIGEST256_LEN, 0);
  263. buf[BASE64_DIGEST256_LEN] = '\0';
  264. memcpy(d64, buf, BASE64_DIGEST256_LEN+1);
  265. return 0;
  266. }
  267. /** Given a base64 encoded, nul-terminated digest in <b>d64</b> (without
  268. * trailing newline or = characters), decode it and store the result in the
  269. * first DIGEST256_LEN bytes at <b>digest</b>. */
  270. /* XXXX unify with crypto_format.c code */
  271. int
  272. digest256_from_base64(char *digest, const char *d64)
  273. {
  274. if (base64_decode(digest, DIGEST256_LEN, d64, strlen(d64)) == DIGEST256_LEN)
  275. return 0;
  276. else
  277. return -1;
  278. }