10 #include <sys/types.h>
12 #include <sys/ioctl.h>
16 #include "libcryptsetup.h"
20 struct safe_allocation {
25 static char *error=NULL;
27 void set_error_va(const char *fmt, va_list va)
37 if (vasprintf(&error, fmt, va) < 0) {
43 void set_error(const char *fmt, ...)
48 set_error_va(fmt, va);
52 const char *get_error(void)
57 void *safe_alloc(size_t size)
59 struct safe_allocation *alloc;
64 alloc = malloc(size + offsetof(struct safe_allocation, data));
73 void safe_free(void *data)
75 struct safe_allocation *alloc;
80 alloc = data - offsetof(struct safe_allocation, data);
82 memset(data, 0, alloc->size);
84 alloc->size = 0x55aa55aa;
88 void *safe_realloc(void *data, size_t size)
92 new_data = safe_alloc(size);
94 if (new_data && data) {
95 struct safe_allocation *alloc;
97 alloc = data - offsetof(struct safe_allocation, data);
99 if (size > alloc->size)
102 memcpy(new_data, data, size);
109 char *safe_strdup(const char *s)
111 char *s2 = safe_alloc(strlen(s) + 1);
116 return strcpy(s2, s);
119 /* Credits go to Michal's padlock patches for this alignment code */
121 static void *aligned_malloc(char **base, int size, int alignment)
125 ptr = malloc(size + alignment);
126 if(ptr == NULL) return NULL;
129 if(alignment > 1 && ((long)ptr & (alignment - 1))) {
130 ptr += alignment - ((long)(ptr) & (alignment - 1));
135 static int sector_size(int fd)
138 if (ioctl(fd,BLKSSZGET, &bsize) < 0)
144 int sector_size_for_device(const char *device)
146 int fd = open(device, O_RDONLY);
155 ssize_t write_blockwise(int fd, const void *orig_buf, size_t count)
157 char *padbuf; char *padbuf_base;
158 char *buf = (char *)orig_buf;
160 int hangover; int solid; int bsize;
162 if ((bsize = sector_size(fd)) < 0)
165 hangover = count % bsize;
166 solid = count - hangover;
168 padbuf = aligned_malloc(&padbuf_base, bsize, bsize);
169 if(padbuf == NULL) return -ENOMEM;
172 memcpy(padbuf, buf, bsize);
173 r = write(fd, padbuf, bsize);
174 if(r < 0 || r != bsize) goto out;
180 r = read(fd,padbuf,bsize);
181 if(r < 0 || r != bsize) goto out;
183 lseek(fd,-bsize,SEEK_CUR);
184 memcpy(padbuf,buf,hangover);
186 r = write(fd,padbuf, bsize);
187 if(r < 0 || r != bsize) goto out;
192 return (buf-(char *)orig_buf)?(buf-(char *)orig_buf):r;
196 ssize_t read_blockwise(int fd, void *orig_buf, size_t count) {
197 char *padbuf; char *padbuf_base;
198 char *buf = (char *)orig_buf;
203 if ((bsize = sector_size(fd)) < 0)
206 padbuf = aligned_malloc(&padbuf_base, bsize, bsize);
207 if(padbuf == NULL) return -ENOMEM;
210 r = read(fd,padbuf,bsize);
211 if(r < 0 || r != bsize) {
212 set_error("read failed in read_blockwise.\n");
215 step = count<bsize?count:bsize;
216 memcpy(buf,padbuf,step);
222 return (buf-(char *)orig_buf)?(buf-(char *)orig_buf):r;
226 * Combines llseek with blockwise write. write_blockwise can already deal with short writes
227 * but we also need a function to deal with short writes at the start. But this information
228 * is implicitly included in the read/write offset, which can not be set to non-aligned
229 * boundaries. Hence, we combine llseek with write.
232 ssize_t write_lseek_blockwise(int fd, const char *buf, size_t count, off_t offset) {
233 int bsize = sector_size(fd);
234 const char *orig_buf = buf;
235 char frontPadBuf[bsize];
236 int frontHang = offset % bsize;
238 int innerCount = count < bsize ? count : bsize;
243 lseek(fd, offset - frontHang, SEEK_SET);
245 r = read(fd,frontPadBuf,bsize);
248 memcpy(frontPadBuf+frontHang, buf, innerCount);
250 lseek(fd, offset - frontHang, SEEK_SET);
251 r = write(fd,frontPadBuf,bsize);
257 if(count <= 0) return buf - orig_buf;
259 return write_blockwise(fd, buf, count) + innerCount;
262 /* Password reading helpers */
264 static int untimed_read(int fd, char *pass, size_t maxlen)
268 i = read(fd, pass, maxlen);
272 } else if (i == 0) { /* EOF */
279 static int timed_read(int fd, char *pass, size_t maxlen, long timeout)
290 if (select(fd+1, &fds, NULL, NULL, &t) > 0)
291 failed = untimed_read(fd, pass, maxlen);
293 set_error("Operation timed out");
297 static int interactive_pass(const char *prompt, char *pass, size_t maxlen,
300 struct termios orig, tmp;
302 int infd = STDIN_FILENO, outfd;
307 /* Read and write to /dev/tty if available */
308 if ((infd = outfd = open("/dev/tty", O_RDWR)) == -1) {
310 outfd = STDERR_FILENO;
313 if (tcgetattr(infd, &orig)) {
314 set_error("Unable to get terminal");
317 memcpy(&tmp, &orig, sizeof(tmp));
318 tmp.c_lflag &= ~ECHO;
320 if (write(outfd, prompt, strlen(prompt)) < 0)
323 tcsetattr(infd, TCSAFLUSH, &tmp);
325 failed = timed_read(infd, pass, maxlen, timeout);
327 failed = untimed_read(infd, pass, maxlen);
328 tcsetattr(infd, TCSAFLUSH, &orig);
332 (void)write(outfd, "\n", 1);
333 if (infd != STDIN_FILENO)
339 * Password reading behaviour matrix of get_key
342 * -----------------+---+---+---+---
343 * interactive | Y | Y | Y | Inf
344 * from fd | N | N | Y | Inf
345 * from binary file | N | N | N | Inf or options->key_size
347 * Legend: p..prompt, v..can verify, n..newline-stop, h..read horizon
349 * Note: --key-file=- is interpreted as a read from a binary file (stdin)
351 * Returns true when more keys are available (that is when password
352 * reading can be retried as for interactive terminals).
355 int get_key(char *prompt, char **key, unsigned int *passLen, int key_size,
356 const char *key_file, int passphrase_fd, int timeout, int how2verify)
359 const int verify = how2verify & CRYPT_FLAG_VERIFY;
360 const int verify_if_possible = how2verify & CRYPT_FLAG_VERIFY_IF_POSSIBLE;
365 if(key_file && !strcmp(key_file, "-")) {
366 /* Allow binary reading from stdin */
370 } else if (key_file) {
371 fd = open(key_file, O_RDONLY);
374 set_error("Error opening key file: %s",
375 strerror_r(errno, buf, 128));
380 /* This can either be 0 (LUKS) or the actually number
381 * of key bytes (default or passed by -s) */
382 read_horizon = key_size;
386 read_horizon = 0; /* Infinite, if read from terminal or fd */
389 /* Interactive case */
393 pass = safe_alloc(512);
394 if (!pass || (i = interactive_pass(prompt, pass, 512, timeout))) {
395 set_error("Error reading passphrase");
398 if (verify || verify_if_possible) {
399 char pass_verify[512];
400 i = interactive_pass("Verify passphrase: ", pass_verify, sizeof(pass_verify), timeout);
401 if (i || strcmp(pass, pass_verify) != 0) {
402 set_error("Passphrases do not match");
405 memset(pass_verify, 0, sizeof(pass_verify));
407 *passLen = strlen(pass);
411 * This is either a fd-input or a file, in neither case we can verify the input,
412 * however we don't stop on new lines if it's a binary file.
417 set_error("Can't do passphrase verification on non-tty inputs");
420 /* The following for control loop does an exhausting
421 * read on the key material file, if requested with
422 * key_size == 0, as it's done by LUKS. However, we
423 * should warn the user, if it's a non-regular file,
424 * such as /dev/random, because in this case, the loop
427 if(key_file && strcmp(key_file, "-") && read_horizon == 0) {
429 if(stat(key_file, &st) < 0) {
430 set_error("Can't stat key file");
433 if(!S_ISREG(st.st_mode)) {
434 // set_error("Can't do exhausting read on non regular files");
436 fprintf(stderr,"Warning: exhausting read requested, but key file is not a regular file, function might never return.\n");
440 for(i = 0; read_horizon == 0 || i < read_horizon; i++) {
441 if(i >= buflen - 1) {
443 pass = safe_realloc(pass, buflen);
445 set_error("Not enough memory while "
446 "reading passphrase");
450 if(read(fd, pass + i, 1) != 1 || (newline_stop && pass[i] == '\n'))
460 return isatty(fd); /* Return true, when password reading can be tried on interactive fds */