keygen_actual.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. #include <sys/types.h>
  2. #include <sys/file.h>
  3. #include <sys/stat.h>
  4. #include <ctype.h>
  5. #include <fcntl.h>
  6. #include <stdint.h>
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <string.h>
  10. #include <termios.h>
  11. #include <unistd.h>
  12. #include "crypto.h"
  13. #include "insecure_memzero.h"
  14. #include "keyfile.h"
  15. #include "keygen.h"
  16. #include "passphrase_entry.h"
  17. #include "readpass.h"
  18. #include "sysendian.h"
  19. #include "tarsnap_opt.h"
  20. #include "warnp.h"
  21. static int
  22. check_printable(const char * str)
  23. {
  24. size_t i;
  25. /*
  26. * Bail if there's any unprintable character in the "C" locale. This
  27. * assumes that we haven't run setlocale(), so this process is using
  28. * the "C" locale by default (as specified in C99).
  29. */
  30. for (i = 0; i < strlen(str); i++) {
  31. if (!isprint(str[i]))
  32. return (1);
  33. }
  34. /* Success! */
  35. return (0);
  36. }
  37. int
  38. keygen_actual(struct register_internal * C, const char * keyfilename,
  39. const int passphrased, const uint64_t maxmem,
  40. const double maxtime,
  41. const char *oldkeyfilename)
  42. {
  43. FILE * keyfile;
  44. char * passphrase = NULL;
  45. int keymask = CRYPTO_KEYMASK_USER;
  46. uint64_t dummy;
  47. /* Sanity-check the user name. */
  48. if (strlen(C->user) > 255) {
  49. fprintf(stderr, "User name too long: %s\n", C->user);
  50. goto err0;
  51. }
  52. if (strlen(C->user) == 0) {
  53. fprintf(stderr, "User name must be non-empty\n");
  54. goto err0;
  55. }
  56. /* Sanity-check the machine name. */
  57. if (strlen(C->name) > 255) {
  58. fprintf(stderr, "Machine name too long: %s\n", C->name);
  59. goto err0;
  60. }
  61. if (strlen(C->name) == 0) {
  62. fprintf(stderr, "Machine name must be non-empty\n");
  63. goto err0;
  64. }
  65. /* The machine name must be printable. */
  66. if (check_printable(C->name)) {
  67. warn0("Machine name must be printable 7-bit ASCII: %s",
  68. C->name);
  69. goto err0;
  70. }
  71. /* Sanity-check the memory size. */
  72. if (maxmem > SIZE_MAX) {
  73. fprintf(stderr, "Passphrase memory size is too large\n");
  74. goto err0;
  75. }
  76. /* Get a password. */
  77. if (readpass(&C->passwd, "Enter tarsnap account password", NULL, 0)) {
  78. warnp("Error reading password");
  79. goto err0;
  80. }
  81. /*
  82. * Create key file -- we do this now rather than later so that we
  83. * avoid registering with the server if we won't be able to create
  84. * the key file later.
  85. */
  86. if ((keyfile = keyfile_write_open(keyfilename)) == NULL) {
  87. warnp("Cannot create %s", keyfilename);
  88. goto err1;
  89. }
  90. /* Initialize key cache. */
  91. if (crypto_keys_init()) {
  92. warnp("Key cache initialization failed");
  93. goto err3;
  94. }
  95. /* keyregen (with oldkeyfilename) only regenerates certain keys. */
  96. if (oldkeyfilename != NULL) {
  97. /*
  98. * Load the keys CRYPTO_KEY_HMAC_{CHUNK, NAME, CPARAMS}
  99. * from the old key file, since these are the keys which need
  100. * to be consistent in order for two key sets to be
  101. * compatible. (CHUNK and NAME are used to compute the
  102. * 32-byte keys for blocks; CPARAMS is used to compute
  103. * parameters used to split a stream of bytes into chunks.)
  104. */
  105. if (keyfile_read(oldkeyfilename, &dummy,
  106. CRYPTO_KEYMASK_HMAC_CHUNK |
  107. CRYPTO_KEYMASK_HMAC_NAME |
  108. CRYPTO_KEYMASK_HMAC_CPARAMS, 0,
  109. PASSPHRASE_TTY_STDIN, NULL)) {
  110. warnp("Error reading old key file");
  111. goto err3;
  112. }
  113. /*
  114. * Adjust the keymask to avoid regenerating keys we read from
  115. * the old keyfile.
  116. */
  117. keymask &= ~CRYPTO_KEYMASK_HMAC_CHUNK &
  118. ~CRYPTO_KEYMASK_HMAC_NAME &
  119. ~CRYPTO_KEYMASK_HMAC_CPARAMS;
  120. }
  121. /* Generate keys. */
  122. if (crypto_keys_generate(keymask)) {
  123. warnp("Error generating keys");
  124. goto err3;
  125. }
  126. /* Register the keys with the server. */
  127. if (keygen_network_register(C) != 0)
  128. goto err3;
  129. /* Exit with a code of 1 if we couldn't register. */
  130. if (C->machinenum == (uint64_t)(-1))
  131. goto err3;
  132. /* If the user wants to passphrase the keyfile, get the passphrase. */
  133. if (passphrased != 0) {
  134. if (readpass(&passphrase,
  135. "Please enter passphrase for keyfile encryption",
  136. "Please confirm passphrase for keyfile encryption", 1)) {
  137. warnp("Error reading password");
  138. goto err3;
  139. }
  140. }
  141. /* Write keys to file. */
  142. if (keyfile_write_file(keyfile, C->machinenum,
  143. CRYPTO_KEYMASK_USER, passphrase, (size_t)maxmem, maxtime))
  144. goto err3;
  145. /* Close the key file. */
  146. if (fclose(keyfile)) {
  147. warnp("Error closing key file");
  148. goto err2;
  149. }
  150. /* Free allocated memory. C->passwd is a NUL-terminated string. */
  151. insecure_memzero(C->passwd, strlen(C->passwd));
  152. free(C->passwd);
  153. /* Free passphrase, if used. passphrase is a NUL-terminated string. */
  154. if (passphrase != NULL) {
  155. insecure_memzero(passphrase, strlen(passphrase));
  156. free(passphrase);
  157. }
  158. /* Success! */
  159. return (0);
  160. err3:
  161. fclose(keyfile);
  162. err2:
  163. unlink(keyfilename);
  164. err1:
  165. insecure_memzero(C->passwd, strlen(C->passwd));
  166. free(C->passwd);
  167. if (passphrase != NULL) {
  168. insecure_memzero(passphrase, strlen(passphrase));
  169. free(passphrase);
  170. }
  171. err0:
  172. /* Failure! */
  173. return (-1);
  174. }