crypto_format.c 9.2 KB

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