2 * util_crypt - cipher utilities for cryptsetup
4 * Copyright (C) 2004-2007, Clemens Fruhwirth <clemens@endorphin.org>
5 * Copyright (C) 2009-2011, Red Hat, Inc. All rights reserved.
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * version 2 as published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 #include <sys/types.h>
32 #include "libcryptsetup.h"
34 #include "utils_crypt.h"
36 struct safe_allocation {
41 int crypt_parse_name_and_mode(const char *s, char *cipher, int *key_nums,
44 if (sscanf(s, "%" MAX_CIPHER_LEN_STR "[^-]-%" MAX_CIPHER_LEN_STR "s",
45 cipher, cipher_mode) == 2) {
46 if (!strcmp(cipher_mode, "plain"))
47 strncpy(cipher_mode, "cbc-plain", 10);
49 char *tmp = strchr(cipher, ':');
50 *key_nums = tmp ? atoi(++tmp) : 1;
58 if (sscanf(s, "%" MAX_CIPHER_LEN_STR "[^-]", cipher) == 1) {
59 strncpy(cipher_mode, "cbc-plain", 10);
68 /* safe allocations */
69 void *crypt_safe_alloc(size_t size)
71 struct safe_allocation *alloc;
76 alloc = malloc(size + offsetof(struct safe_allocation, data));
85 void crypt_safe_free(void *data)
87 struct safe_allocation *alloc;
92 alloc = data - offsetof(struct safe_allocation, data);
94 memset(data, 0, alloc->size);
96 alloc->size = 0x55aa55aa;
100 void *crypt_safe_realloc(void *data, size_t size)
104 new_data = crypt_safe_alloc(size);
106 if (new_data && data) {
107 struct safe_allocation *alloc;
109 alloc = data - offsetof(struct safe_allocation, data);
111 if (size > alloc->size)
114 memcpy(new_data, data, size);
117 crypt_safe_free(data);
121 /* Password reading helpers */
122 static int untimed_read(int fd, char *pass, size_t maxlen)
126 i = read(fd, pass, maxlen);
130 } else if (i == 0) { /* EOF */
137 static int timed_read(int fd, char *pass, size_t maxlen, long timeout)
148 if (select(fd+1, &fds, NULL, NULL, &t) > 0)
149 failed = untimed_read(fd, pass, maxlen);
154 static int interactive_pass(const char *prompt, char *pass, size_t maxlen,
157 struct termios orig, tmp;
159 int infd = STDIN_FILENO, outfd;
164 /* Read and write to /dev/tty if available */
165 if ((infd = outfd = open("/dev/tty", O_RDWR)) == -1) {
167 outfd = STDERR_FILENO;
170 if (tcgetattr(infd, &orig))
173 memcpy(&tmp, &orig, sizeof(tmp));
174 tmp.c_lflag &= ~ECHO;
176 if (prompt && write(outfd, prompt, strlen(prompt)) < 0)
179 tcsetattr(infd, TCSAFLUSH, &tmp);
181 failed = timed_read(infd, pass, maxlen, timeout);
183 failed = untimed_read(infd, pass, maxlen);
184 tcsetattr(infd, TCSAFLUSH, &orig);
187 if (!failed && write(outfd, "\n", 1)) {};
189 if (infd != STDIN_FILENO)
195 * Password reading behaviour matrix of get_key
196 * FIXME: rewrite this from scratch.
198 * -----------------+---+---+---+---
199 * interactive | Y | Y | Y | Inf
200 * from fd | N | N | Y | Inf
201 * from binary file | N | N | N | Inf or options->key_size
203 * Legend: p..prompt, v..can verify, n..newline-stop, h..read horizon
205 * Note: --key-file=- is interpreted as a read from a binary file (stdin)
208 int crypt_get_key(char *prompt, char **key, unsigned int *passLen, int key_size,
209 const char *key_file, int timeout, int verify,
210 struct crypt_device *cd)
215 int regular_file = 0;
220 /* Passphrase read from stdin? */
221 read_stdin = (!key_file || !strcmp(key_file, "-")) ? 1 : 0;
223 /* read_horizon applies only for real keyfile, not stdin or terminal */
224 read_horizon = (key_file && !read_stdin) ? key_size : 0 /* until EOF */;
226 /* Setup file descriptior */
227 fd = read_stdin ? STDIN_FILENO : open(key_file, O_RDONLY);
229 crypt_log(cd, CRYPT_LOG_ERROR,
230 _("Failed to open key file.\n"));
234 /* Interactive case */
238 pass = crypt_safe_alloc(MAX_TTY_PASSWORD_LEN);
239 if (!pass || interactive_pass(prompt, pass, MAX_TTY_PASSWORD_LEN, timeout)) {
240 crypt_log(cd, CRYPT_LOG_ERROR,
241 _("Error reading passphrase from terminal.\n"));
245 char pass_verify[MAX_TTY_PASSWORD_LEN];
246 i = interactive_pass(_("Verify passphrase: "), pass_verify, sizeof(pass_verify), timeout);
247 if (i || strcmp(pass, pass_verify) != 0) {
248 crypt_log(cd, CRYPT_LOG_ERROR,
249 _("Passphrases do not match.\n"));
252 memset(pass_verify, 0, sizeof(pass_verify));
254 *passLen = strlen(pass);
258 * This is either a fd-input or a file, in neither case we can verify the input,
259 * however we don't stop on new lines if it's a binary file.
263 /* The following for control loop does an exhausting
264 * read on the key material file, if requested with
265 * key_size == 0, as it's done by LUKS. However, we
266 * should warn the user, if it's a non-regular file,
267 * such as /dev/random, because in this case, the loop
270 if(!read_stdin && read_horizon == 0) {
271 if(stat(key_file, &st) < 0) {
272 crypt_log(cd, CRYPT_LOG_ERROR,
273 _("Failed to stat key file.\n"));
276 if(!S_ISREG(st.st_mode))
277 crypt_log(cd, CRYPT_LOG_NORMAL,
278 _("Warning: exhausting read requested, but key file"
279 " is not a regular file, function might never return.\n"));
284 for(i = 0; read_horizon == 0 || i < read_horizon; i++) {
285 if(i >= buflen - 1) {
287 pass = crypt_safe_realloc(pass, buflen);
289 crypt_log(cd, CRYPT_LOG_ERROR,
290 _("Out of memory while reading passphrase.\n"));
295 r = read(fd, pass + i, 1);
297 crypt_log(cd, CRYPT_LOG_ERROR,
298 _("Error reading passphrase.\n"));
302 /* Stop on newline only if not requested read from keyfile */
303 if(r == 0 || (!key_file && pass[i] == '\n'))
306 /* Fail if piped input dies reading nothing */
307 if(!i && !regular_file)
313 if(fd != STDIN_FILENO)
318 if(fd >= 0 && fd != STDIN_FILENO)
321 crypt_safe_free(pass);