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 #define log_dbg(x) crypt_log(NULL, CRYPT_LOG_DEBUG, x)
37 #define log_err(cd, x) crypt_log(cd, CRYPT_LOG_ERROR, x)
39 struct safe_allocation {
44 int crypt_parse_name_and_mode(const char *s, char *cipher, int *key_nums,
47 if (sscanf(s, "%" MAX_CIPHER_LEN_STR "[^-]-%" MAX_CIPHER_LEN_STR "s",
48 cipher, cipher_mode) == 2) {
49 if (!strcmp(cipher_mode, "plain"))
50 strncpy(cipher_mode, "cbc-plain", 10);
52 char *tmp = strchr(cipher, ':');
53 *key_nums = tmp ? atoi(++tmp) : 1;
61 if (sscanf(s, "%" MAX_CIPHER_LEN_STR "[^-]", cipher) == 1) {
62 strncpy(cipher_mode, "cbc-plain", 10);
71 /* safe allocations */
72 void *crypt_safe_alloc(size_t size)
74 struct safe_allocation *alloc;
79 alloc = malloc(size + offsetof(struct safe_allocation, data));
88 void crypt_safe_free(void *data)
90 struct safe_allocation *alloc;
95 alloc = (struct safe_allocation *)
96 ((char *)data - offsetof(struct safe_allocation, data));
98 memset(data, 0, alloc->size);
100 alloc->size = 0x55aa55aa;
104 void *crypt_safe_realloc(void *data, size_t size)
106 struct safe_allocation *alloc;
109 new_data = crypt_safe_alloc(size);
111 if (new_data && data) {
113 alloc = (struct safe_allocation *)
114 ((char *)data - offsetof(struct safe_allocation, data));
116 if (size > alloc->size)
119 memcpy(new_data, data, size);
122 crypt_safe_free(data);
126 /* Password reading helpers */
127 static int untimed_read(int fd, char *pass, size_t maxlen)
131 i = read(fd, pass, maxlen);
135 } else if (i == 0) { /* EOF */
142 static int timed_read(int fd, char *pass, size_t maxlen, long timeout)
153 if (select(fd+1, &fds, NULL, NULL, &t) > 0)
154 failed = untimed_read(fd, pass, maxlen);
159 static int interactive_pass(const char *prompt, char *pass, size_t maxlen,
162 struct termios orig, tmp;
164 int infd = STDIN_FILENO, outfd;
169 /* Read and write to /dev/tty if available */
170 if ((infd = outfd = open("/dev/tty", O_RDWR)) == -1) {
172 outfd = STDERR_FILENO;
175 if (tcgetattr(infd, &orig))
178 memcpy(&tmp, &orig, sizeof(tmp));
179 tmp.c_lflag &= ~ECHO;
181 if (prompt && write(outfd, prompt, strlen(prompt)) < 0)
184 tcsetattr(infd, TCSAFLUSH, &tmp);
186 failed = timed_read(infd, pass, maxlen, timeout);
188 failed = untimed_read(infd, pass, maxlen);
189 tcsetattr(infd, TCSAFLUSH, &orig);
192 if (!failed && write(outfd, "\n", 1)) {};
194 if (infd != STDIN_FILENO)
199 static int crypt_get_key_tty(const char *prompt,
200 char **key, size_t *key_size,
201 int timeout, int verify,
202 struct crypt_device *cd)
204 int key_size_max = DEFAULT_PASSPHRASE_SIZE_MAX;
206 char *pass = NULL, *pass_verify = NULL;
208 log_dbg("Interactive passphrase entry requested.");
210 pass = crypt_safe_alloc(key_size_max + 1);
212 log_err(cd, _("Out of memory while reading passphrase.\n"));
216 if (interactive_pass(prompt, pass, key_size_max, timeout)) {
217 log_err(cd, _("Error reading passphrase from terminal.\n"));
220 pass[key_size_max] = '\0';
223 pass_verify = crypt_safe_alloc(key_size_max);
225 log_err(cd, _("Out of memory while reading passphrase.\n"));
230 if (interactive_pass(_("Verify passphrase: "),
231 pass_verify, key_size_max, timeout)) {
232 log_err(cd, _("Error reading passphrase from terminal.\n"));
236 if (strncmp(pass, pass_verify, key_size_max)) {
237 log_err(cd, _("Passphrases do not match.\n"));
244 *key_size = strlen(pass);
247 crypt_safe_free(pass_verify);
249 crypt_safe_free(pass);
254 * Note: --key-file=- is interpreted as a read from a binary file (stdin)
255 * key_size_max == 0 means detect maximum according to input type (tty/file)
256 * timeout and verify options only applies to tty input
258 int crypt_get_key(const char *prompt,
259 char **key, size_t *key_size,
260 size_t keyfile_size_max, const char *key_file,
261 int timeout, int verify,
262 struct crypt_device *cd)
264 int fd, regular_file, read_stdin, char_read, unlimited_read = 0;
273 /* Passphrase read from stdin? */
274 read_stdin = (!key_file || !strcmp(key_file, "-")) ? 1 : 0;
276 if(read_stdin && isatty(STDIN_FILENO))
277 return crypt_get_key_tty(prompt, key, key_size, timeout, verify, cd);
280 log_dbg("STDIN descriptor passphrase entry requested.");
282 log_dbg("File descriptor passphrase entry requested.");
284 /* If not requsted otherwise, we limit input to prevent memory exhaustion */
285 if (keyfile_size_max == 0) {
286 keyfile_size_max = DEFAULT_KEYFILE_SIZE_MAXKB * 1024;
290 fd = read_stdin ? STDIN_FILENO : open(key_file, O_RDONLY);
292 log_err(cd, _("Failed to open key file.\n"));
296 /* use 4k for buffer (page divisor but avoid huge pages) */
297 buflen = 4096 - sizeof(struct safe_allocation);
300 if(stat(key_file, &st) < 0) {
301 log_err(cd, _("Failed to stat key file.\n"));
304 if(S_ISREG(st.st_mode)) {
306 /* known keyfile size, alloc it in one step */
307 if ((size_t)st.st_size >= keyfile_size_max)
308 buflen = keyfile_size_max;
314 pass = crypt_safe_alloc(buflen);
316 log_err(cd, _("Out of memory while reading passphrase.\n"));
320 for(i = 0; i < keyfile_size_max; i++) {
323 pass = crypt_safe_realloc(pass, buflen);
325 log_err(cd, _("Out of memory while reading passphrase.\n"));
331 char_read = read(fd, &pass[i], 1);
333 log_err(cd, _("Error reading passphrase.\n"));
337 /* Stop on newline only if not requested read from keyfile */
338 if(char_read == 0 || (!key_file && pass[i] == '\n'))
342 /* Fail if piped input dies reading nothing */
343 if(!i && !regular_file) {
344 log_dbg("Nothing read on input.");
349 /* Fail if we exceeded internal default (no specified size) */
350 if (unlimited_read && i == keyfile_size_max) {
351 log_err(cd, _("Maximum keyfile size exceeeded.\n"));
355 if (!unlimited_read && i != keyfile_size_max) {
356 log_err(cd, _("Cannot read requested amount of data.\n"));
360 /* Well, for historical reasons reading empty keyfile is not fail. */
362 crypt_safe_free(pass);
370 if(fd != STDIN_FILENO)
374 crypt_safe_free(pass);