crypto_format.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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. ! fast_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, including trailing "="
  120. * characters, in the buffer <b>output</b>, which must have at least
  121. * CURVE25519_BASE64_PADDED_LEN+1 bytes available.
  122. * Can not fail.
  123. *
  124. * Careful! CURVE25519_BASE64_PADDED_LEN is one byte longer than
  125. * ED25519_BASE64_LEN.
  126. */
  127. void
  128. curve25519_public_to_base64(char *output,
  129. const curve25519_public_key_t *pkey)
  130. {
  131. char buf[128];
  132. int n = base64_encode(buf, sizeof(buf),
  133. (const char*)pkey->public_key,
  134. CURVE25519_PUBKEY_LEN, 0);
  135. /* These asserts should always succeed, unless there is a bug in
  136. * base64_encode(). */
  137. tor_assert(n == CURVE25519_BASE64_PADDED_LEN);
  138. tor_assert(buf[CURVE25519_BASE64_PADDED_LEN] == '\0');
  139. memcpy(output, buf, CURVE25519_BASE64_PADDED_LEN+1);
  140. }
  141. /** Try to decode a base64-encoded curve25519 public key from <b>input</b>
  142. * into the object at <b>pkey</b>. Return 0 on success, -1 on failure.
  143. * Accepts keys with or without a trailing "=". */
  144. int
  145. curve25519_public_from_base64(curve25519_public_key_t *pkey,
  146. const char *input)
  147. {
  148. size_t len = strlen(input);
  149. if (len == CURVE25519_BASE64_PADDED_LEN - 1) {
  150. /* not padded */
  151. return digest256_from_base64((char*)pkey->public_key, input);
  152. } else if (len == CURVE25519_BASE64_PADDED_LEN) {
  153. char buf[128];
  154. if (base64_decode(buf, sizeof(buf), input, len) != CURVE25519_PUBKEY_LEN)
  155. return -1;
  156. memcpy(pkey->public_key, buf, CURVE25519_PUBKEY_LEN);
  157. return 0;
  158. } else {
  159. return -1;
  160. }
  161. }
  162. /** For logging convenience: Convert <b>pkey</b> to a statically allocated
  163. * base64 string and return it. Not threadsafe. Format not meant to be
  164. * computer-readable; it may change in the future. Subsequent calls invalidate
  165. * previous returns. */
  166. const char *
  167. ed25519_fmt(const ed25519_public_key_t *pkey)
  168. {
  169. static char formatted[ED25519_BASE64_LEN+1];
  170. if (pkey) {
  171. if (ed25519_public_key_is_zero(pkey)) {
  172. strlcpy(formatted, "<unset>", sizeof(formatted));
  173. } else {
  174. ed25519_public_to_base64(formatted, pkey);
  175. }
  176. } else {
  177. strlcpy(formatted, "<null>", sizeof(formatted));
  178. }
  179. return formatted;
  180. }
  181. /** Try to decode the string <b>input</b> into an ed25519 public key. On
  182. * success, store the value in <b>pkey</b> and return 0. Otherwise return
  183. * -1. */
  184. int
  185. ed25519_public_from_base64(ed25519_public_key_t *pkey,
  186. const char *input)
  187. {
  188. return digest256_from_base64((char*)pkey->pubkey, input);
  189. }
  190. /** Encode the public key <b>pkey</b> into the buffer at <b>output</b>,
  191. * which must have space for ED25519_BASE64_LEN bytes of encoded key,
  192. * plus one byte for a terminating NUL.
  193. * Can not fail.
  194. *
  195. * Careful! ED25519_BASE64_LEN is one byte shorter than
  196. * CURVE25519_BASE64_PADDED_LEN.
  197. */
  198. void
  199. ed25519_public_to_base64(char *output,
  200. const ed25519_public_key_t *pkey)
  201. {
  202. digest256_to_base64(output, (const char *)pkey->pubkey);
  203. }
  204. /** Encode the signature <b>sig</b> into the buffer at <b>output</b>,
  205. * which must have space for ED25519_SIG_BASE64_LEN bytes of encoded signature,
  206. * plus one byte for a terminating NUL.
  207. * Can not fail.
  208. */
  209. void
  210. ed25519_signature_to_base64(char *output,
  211. const ed25519_signature_t *sig)
  212. {
  213. char buf[256];
  214. int n = base64_encode_nopad(buf, sizeof(buf), sig->sig, ED25519_SIG_LEN);
  215. /* These asserts should always succeed, unless there is a bug in
  216. * base64_encode_nopad(). */
  217. tor_assert(n == ED25519_SIG_BASE64_LEN);
  218. tor_assert(buf[ED25519_SIG_BASE64_LEN] == '\0');
  219. memcpy(output, buf, ED25519_SIG_BASE64_LEN+1);
  220. }
  221. /** Try to decode the string <b>input</b> into an ed25519 signature. On
  222. * success, store the value in <b>sig</b> and return 0. Otherwise return
  223. * -1. */
  224. int
  225. ed25519_signature_from_base64(ed25519_signature_t *sig,
  226. const char *input)
  227. {
  228. if (strlen(input) != ED25519_SIG_BASE64_LEN)
  229. return -1;
  230. char decoded[128];
  231. int n = base64_decode(decoded, sizeof(decoded), input,
  232. ED25519_SIG_BASE64_LEN);
  233. if (n < 0 || n != ED25519_SIG_LEN)
  234. return -1;
  235. memcpy(sig->sig, decoded, ED25519_SIG_LEN);
  236. return 0;
  237. }
  238. /** Base64 encode DIGEST_LEN bytes from <b>digest</b>, remove the trailing =
  239. * characters, and store the nul-terminated result in the first
  240. * BASE64_DIGEST_LEN+1 bytes of <b>d64</b>.
  241. * Can not fail. */
  242. void
  243. digest_to_base64(char *d64, const char *digest)
  244. {
  245. char buf[256];
  246. int n = base64_encode_nopad(buf, sizeof(buf),
  247. (const uint8_t *)digest, DIGEST_LEN);
  248. /* These asserts should always succeed, unless there is a bug in
  249. * base64_encode_nopad(). */
  250. tor_assert(n == BASE64_DIGEST_LEN);
  251. tor_assert(buf[BASE64_DIGEST_LEN] == '\0');
  252. memcpy(d64, buf, BASE64_DIGEST_LEN+1);
  253. }
  254. /** Given a base64 encoded, nul-terminated digest in <b>d64</b> (without
  255. * trailing newline or = characters), decode it and store the result in the
  256. * first DIGEST_LEN bytes at <b>digest</b>. */
  257. int
  258. digest_from_base64(char *digest, const char *d64)
  259. {
  260. if (base64_decode(digest, DIGEST_LEN, d64, strlen(d64)) == DIGEST_LEN)
  261. return 0;
  262. else
  263. return -1;
  264. }
  265. /** Base64 encode DIGEST256_LINE bytes from <b>digest</b>, remove the
  266. * trailing = characters, and store the nul-terminated result in the first
  267. * BASE64_DIGEST256_LEN+1 bytes of <b>d64</b>.
  268. * Can not fail. */
  269. void
  270. digest256_to_base64(char *d64, const char *digest)
  271. {
  272. char buf[256];
  273. int n = base64_encode_nopad(buf, sizeof(buf),
  274. (const uint8_t *)digest, DIGEST256_LEN);
  275. /* These asserts should always succeed, unless there is a bug in
  276. * base64_encode_nopad(). */
  277. tor_assert(n == BASE64_DIGEST256_LEN);
  278. tor_assert(buf[BASE64_DIGEST256_LEN] == '\0');
  279. memcpy(d64, buf, BASE64_DIGEST256_LEN+1);
  280. }
  281. /** Given a base64 encoded, nul-terminated digest in <b>d64</b> (without
  282. * trailing newline or = characters), decode it and store the result in the
  283. * first DIGEST256_LEN bytes at <b>digest</b>. */
  284. int
  285. digest256_from_base64(char *digest, const char *d64)
  286. {
  287. if (base64_decode(digest, DIGEST256_LEN, d64, strlen(d64)) == DIGEST256_LEN)
  288. return 0;
  289. else
  290. return -1;
  291. }