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) {
28 if (sscanf(s, "%" MAX_CIPHER_LEN_STR "[^-]", cipher) == 1) {
29 strncpy(cipher_mode, "cbc-plain", 9);
36 /* safe allocations */
37 void *crypt_safe_alloc(size_t size)
39 struct safe_allocation *alloc;
44 alloc = malloc(size + offsetof(struct safe_allocation, data));
53 void crypt_safe_free(void *data)
55 struct safe_allocation *alloc;
60 alloc = data - offsetof(struct safe_allocation, data);
62 memset(data, 0, alloc->size);
64 alloc->size = 0x55aa55aa;
68 void *crypt_safe_realloc(void *data, size_t size)
72 new_data = crypt_safe_alloc(size);
74 if (new_data && data) {
75 struct safe_allocation *alloc;
77 alloc = data - offsetof(struct safe_allocation, data);
79 if (size > alloc->size)
82 memcpy(new_data, data, size);
85 crypt_safe_free(data);
89 /* Password reading helpers */
90 static int untimed_read(int fd, char *pass, size_t maxlen)
94 i = read(fd, pass, maxlen);
98 } else if (i == 0) { /* EOF */
105 static int timed_read(int fd, char *pass, size_t maxlen, long timeout)
116 if (select(fd+1, &fds, NULL, NULL, &t) > 0)
117 failed = untimed_read(fd, pass, maxlen);
122 static int interactive_pass(const char *prompt, char *pass, size_t maxlen,
125 struct termios orig, tmp;
127 int infd = STDIN_FILENO, outfd;
132 /* Read and write to /dev/tty if available */
133 if ((infd = outfd = open("/dev/tty", O_RDWR)) == -1) {
135 outfd = STDERR_FILENO;
138 if (tcgetattr(infd, &orig))
141 memcpy(&tmp, &orig, sizeof(tmp));
142 tmp.c_lflag &= ~ECHO;
144 if (write(outfd, prompt, strlen(prompt)) < 0)
147 tcsetattr(infd, TCSAFLUSH, &tmp);
149 failed = timed_read(infd, pass, maxlen, timeout);
151 failed = untimed_read(infd, pass, maxlen);
152 tcsetattr(infd, TCSAFLUSH, &orig);
155 if (!failed && write(outfd, "\n", 1));
157 if (infd != STDIN_FILENO)
163 * Password reading behaviour matrix of get_key
164 * FIXME: rewrite this from scratch.
166 * -----------------+---+---+---+---
167 * interactive | Y | Y | Y | Inf
168 * from fd | N | N | Y | Inf
169 * from binary file | N | N | N | Inf or options->key_size
171 * Legend: p..prompt, v..can verify, n..newline-stop, h..read horizon
173 * Note: --key-file=- is interpreted as a read from a binary file (stdin)
176 void crypt_get_key(char *prompt, char **key, unsigned int *passLen, int key_size,
177 const char *key_file, int timeout, int verify,
178 struct crypt_device *cd)
183 int regular_file = 0;
188 /* Passphrase read from stdin? */
189 read_stdin = (!key_file || !strcmp(key_file, "-")) ? 1 : 0;
191 /* read_horizon applies only for real keyfile, not stdin or terminal */
192 read_horizon = (key_file && !read_stdin) ? key_size : 0 /* until EOF */;
194 /* Setup file descriptior */
195 fd = read_stdin ? STDIN_FILENO : open(key_file, O_RDONLY);
197 crypt_log(cd, CRYPT_LOG_ERROR,
198 _("Failed to open key file.\n"));
202 /* Interactive case */
206 pass = crypt_safe_alloc(MAX_TTY_PASSWORD_LEN);
207 if (!pass || (i = interactive_pass(prompt, pass, MAX_TTY_PASSWORD_LEN, timeout))) {
208 crypt_log(cd, CRYPT_LOG_ERROR,
209 _("Error reading passphrase from terminal.\n"));
213 char pass_verify[MAX_TTY_PASSWORD_LEN];
214 i = interactive_pass(_("Verify passphrase: "), pass_verify, sizeof(pass_verify), timeout);
215 if (i || strcmp(pass, pass_verify) != 0) {
216 crypt_log(cd, CRYPT_LOG_ERROR,
217 _("Passphrases do not match.\n"));
220 memset(pass_verify, 0, sizeof(pass_verify));
222 *passLen = strlen(pass);
226 * This is either a fd-input or a file, in neither case we can verify the input,
227 * however we don't stop on new lines if it's a binary file.
231 /* The following for control loop does an exhausting
232 * read on the key material file, if requested with
233 * key_size == 0, as it's done by LUKS. However, we
234 * should warn the user, if it's a non-regular file,
235 * such as /dev/random, because in this case, the loop
238 if(!read_stdin && read_horizon == 0) {
239 if(stat(key_file, &st) < 0) {
240 crypt_log(cd, CRYPT_LOG_ERROR,
241 _("Failed to stat key file.\n"));
244 if(!S_ISREG(st.st_mode))
245 crypt_log(cd, CRYPT_LOG_NORMAL,
246 _("Warning: exhausting read requested, but key file"
247 " is not a regular file, function might never return.\n"));
252 for(i = 0; read_horizon == 0 || i < read_horizon; i++) {
253 if(i >= buflen - 1) {
255 pass = crypt_safe_realloc(pass, buflen);
257 crypt_log(cd, CRYPT_LOG_ERROR,
258 _("Out of memory while reading passphrase.\n"));
263 r = read(fd, pass + i, 1);
265 crypt_log(cd, CRYPT_LOG_ERROR,
266 _("Error reading passphrase.\n"));
270 /* Stop on newline only if not requested read from keyfile */
271 if(r == 0 || (!key_file && pass[i] == '\n'))
274 /* Fail if piped input dies reading nothing */
275 if(!i && !regular_file)
281 if(fd != STDIN_FILENO)
286 if(fd >= 0 && fd != STDIN_FILENO)
289 crypt_safe_free(pass);