crypto_format.c 9.4 KB

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