12 #include "libcryptsetup.h"
14 #include "utils_crypt.h"
16 struct safe_allocation {
21 int crypt_parse_name_and_mode(const char *s, char *cipher, char *cipher_mode)
23 if (sscanf(s, "%" MAX_CIPHER_LEN_STR "[^-]-%" MAX_CIPHER_LEN_STR "s",
24 cipher, cipher_mode) == 2) {
25 if (!strcmp(cipher_mode, "plain"))
26 strncpy(cipher_mode, "cbc-plain", 10);
30 if (sscanf(s, "%" MAX_CIPHER_LEN_STR "[^-]", cipher) == 1) {
31 strncpy(cipher_mode, "cbc-plain", 10);
38 /* safe allocations */
39 void *crypt_safe_alloc(size_t size)
41 struct safe_allocation *alloc;
46 alloc = malloc(size + offsetof(struct safe_allocation, data));
55 void crypt_safe_free(void *data)
57 struct safe_allocation *alloc;
62 alloc = data - offsetof(struct safe_allocation, data);
64 memset(data, 0, alloc->size);
66 alloc->size = 0x55aa55aa;
70 void *crypt_safe_realloc(void *data, size_t size)
74 new_data = crypt_safe_alloc(size);
76 if (new_data && data) {
77 struct safe_allocation *alloc;
79 alloc = data - offsetof(struct safe_allocation, data);
81 if (size > alloc->size)
84 memcpy(new_data, data, size);
87 crypt_safe_free(data);
91 /* Password reading helpers */
92 static int untimed_read(int fd, char *pass, size_t maxlen)
96 i = read(fd, pass, maxlen);
100 } else if (i == 0) { /* EOF */
107 static int timed_read(int fd, char *pass, size_t maxlen, long timeout)
118 if (select(fd+1, &fds, NULL, NULL, &t) > 0)
119 failed = untimed_read(fd, pass, maxlen);
124 static int interactive_pass(const char *prompt, char *pass, size_t maxlen,
127 struct termios orig, tmp;
129 int infd = STDIN_FILENO, outfd;
134 /* Read and write to /dev/tty if available */
135 if ((infd = outfd = open("/dev/tty", O_RDWR)) == -1) {
137 outfd = STDERR_FILENO;
140 if (tcgetattr(infd, &orig))
143 memcpy(&tmp, &orig, sizeof(tmp));
144 tmp.c_lflag &= ~ECHO;
146 if (write(outfd, prompt, strlen(prompt)) < 0)
149 tcsetattr(infd, TCSAFLUSH, &tmp);
151 failed = timed_read(infd, pass, maxlen, timeout);
153 failed = untimed_read(infd, pass, maxlen);
154 tcsetattr(infd, TCSAFLUSH, &orig);
157 if (!failed && write(outfd, "\n", 1)) {};
159 if (infd != STDIN_FILENO)
165 * Password reading behaviour matrix of get_key
166 * FIXME: rewrite this from scratch.
168 * -----------------+---+---+---+---
169 * interactive | Y | Y | Y | Inf
170 * from fd | N | N | Y | Inf
171 * from binary file | N | N | N | Inf or options->key_size
173 * Legend: p..prompt, v..can verify, n..newline-stop, h..read horizon
175 * Note: --key-file=- is interpreted as a read from a binary file (stdin)
178 int crypt_get_key(char *prompt, char **key, unsigned int *passLen, int key_size,
179 const char *key_file, int timeout, int verify,
180 struct crypt_device *cd)
185 int regular_file = 0;
190 /* Passphrase read from stdin? */
191 read_stdin = (!key_file || !strcmp(key_file, "-")) ? 1 : 0;
193 /* read_horizon applies only for real keyfile, not stdin or terminal */
194 read_horizon = (key_file && !read_stdin) ? key_size : 0 /* until EOF */;
196 /* Setup file descriptior */
197 fd = read_stdin ? STDIN_FILENO : open(key_file, O_RDONLY);
199 crypt_log(cd, CRYPT_LOG_ERROR,
200 _("Failed to open key file.\n"));
204 /* Interactive case */
208 pass = crypt_safe_alloc(MAX_TTY_PASSWORD_LEN);
209 if (!pass || interactive_pass(prompt, pass, MAX_TTY_PASSWORD_LEN, timeout)) {
210 crypt_log(cd, CRYPT_LOG_ERROR,
211 _("Error reading passphrase from terminal.\n"));
215 char pass_verify[MAX_TTY_PASSWORD_LEN];
216 i = interactive_pass(_("Verify passphrase: "), pass_verify, sizeof(pass_verify), timeout);
217 if (i || strcmp(pass, pass_verify) != 0) {
218 crypt_log(cd, CRYPT_LOG_ERROR,
219 _("Passphrases do not match.\n"));
222 memset(pass_verify, 0, sizeof(pass_verify));
224 *passLen = strlen(pass);
228 * This is either a fd-input or a file, in neither case we can verify the input,
229 * however we don't stop on new lines if it's a binary file.
233 /* The following for control loop does an exhausting
234 * read on the key material file, if requested with
235 * key_size == 0, as it's done by LUKS. However, we
236 * should warn the user, if it's a non-regular file,
237 * such as /dev/random, because in this case, the loop
240 if(!read_stdin && read_horizon == 0) {
241 if(stat(key_file, &st) < 0) {
242 crypt_log(cd, CRYPT_LOG_ERROR,
243 _("Failed to stat key file.\n"));
246 if(!S_ISREG(st.st_mode))
247 crypt_log(cd, CRYPT_LOG_NORMAL,
248 _("Warning: exhausting read requested, but key file"
249 " is not a regular file, function might never return.\n"));
254 for(i = 0; read_horizon == 0 || i < read_horizon; i++) {
255 if(i >= buflen - 1) {
257 pass = crypt_safe_realloc(pass, buflen);
259 crypt_log(cd, CRYPT_LOG_ERROR,
260 _("Out of memory while reading passphrase.\n"));
265 r = read(fd, pass + i, 1);
267 crypt_log(cd, CRYPT_LOG_ERROR,
268 _("Error reading passphrase.\n"));
272 /* Stop on newline only if not requested read from keyfile */
273 if(r == 0 || (!key_file && pass[i] == '\n'))
276 /* Fail if piped input dies reading nothing */
277 if(!i && !regular_file)
283 if(fd != STDIN_FILENO)
288 if(fd >= 0 && fd != STDIN_FILENO)
291 crypt_safe_free(pass);