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 vasprintf(&error, fmt, va);
40 void set_error(const char *fmt, ...)
45 set_error_va(fmt, va);
49 const char *get_error(void)
54 void *safe_alloc(size_t size)
56 struct safe_allocation *alloc;
61 alloc = malloc(size + offsetof(struct safe_allocation, data));
70 void safe_free(void *data)
72 struct safe_allocation *alloc;
77 alloc = data - offsetof(struct safe_allocation, data);
79 memset(data, 0, alloc->size);
81 alloc->size = 0x55aa55aa;
85 void *safe_realloc(void *data, size_t size)
89 new_data = safe_alloc(size);
91 if (new_data && data) {
92 struct safe_allocation *alloc;
94 alloc = data - offsetof(struct safe_allocation, data);
96 if (size > alloc->size)
99 memcpy(new_data, data, size);
106 char *safe_strdup(const char *s)
108 char *s2 = safe_alloc(strlen(s) + 1);
113 return strcpy(s2, s);
116 /* Credits go to Michal's padlock patches for this alignment code */
118 static void *aligned_malloc(char **base, int size, int alignment)
122 ptr = malloc(size + alignment);
123 if(ptr == NULL) return NULL;
126 if(alignment > 1 && ((long)ptr & (alignment - 1))) {
127 ptr += alignment - ((long)(ptr) & (alignment - 1));
132 static int sector_size(int fd)
135 if (ioctl(fd,BLKSSZGET, &bsize) < 0)
141 int sector_size_for_device(const char *device)
143 int fd = open(device, O_RDONLY);
152 ssize_t write_blockwise(int fd, const void *orig_buf, size_t count)
154 char *padbuf; char *padbuf_base;
155 char *buf = (char *)orig_buf;
157 int hangover; int solid; int bsize;
159 if ((bsize = sector_size(fd)) < 0)
162 hangover = count % bsize;
163 solid = count - hangover;
165 padbuf = aligned_malloc(&padbuf_base, bsize, bsize);
166 if(padbuf == NULL) return -ENOMEM;
169 memcpy(padbuf, buf, bsize);
170 r = write(fd, padbuf, bsize);
171 if(r < 0 || r != bsize) goto out;
177 r = read(fd,padbuf,bsize);
178 if(r < 0 || r != bsize) goto out;
180 lseek(fd,-bsize,SEEK_CUR);
181 memcpy(padbuf,buf,hangover);
183 r = write(fd,padbuf, bsize);
184 if(r < 0 || r != bsize) goto out;
189 return (buf-(char *)orig_buf)?(buf-(char *)orig_buf):r;
193 ssize_t read_blockwise(int fd, void *orig_buf, size_t count) {
194 char *padbuf; char *padbuf_base;
195 char *buf = (char *)orig_buf;
200 if ((bsize = sector_size(fd)) < 0)
203 padbuf = aligned_malloc(&padbuf_base, bsize, bsize);
204 if(padbuf == NULL) return -ENOMEM;
207 r = read(fd,padbuf,bsize);
208 if(r < 0 || r != bsize) {
209 set_error("read failed in read_blockwise.\n");
212 step = count<bsize?count:bsize;
213 memcpy(buf,padbuf,step);
219 return (buf-(char *)orig_buf)?(buf-(char *)orig_buf):r;
223 * Combines llseek with blockwise write. write_blockwise can already deal with short writes
224 * but we also need a function to deal with short writes at the start. But this information
225 * is implicitly included in the read/write offset, which can not be set to non-aligned
226 * boundaries. Hence, we combine llseek with write.
229 ssize_t write_lseek_blockwise(int fd, const char *buf, size_t count, off_t offset) {
230 int bsize = sector_size(fd);
231 const char *orig_buf = buf;
232 char frontPadBuf[bsize];
233 int frontHang = offset % bsize;
235 int innerCount = count < bsize ? count : bsize;
240 lseek(fd, offset - frontHang, SEEK_SET);
242 r = read(fd,frontPadBuf,bsize);
245 memcpy(frontPadBuf+frontHang, buf, innerCount);
247 lseek(fd, offset - frontHang, SEEK_SET);
248 r = write(fd,frontPadBuf,bsize);
254 if(count <= 0) return buf - orig_buf;
256 return write_blockwise(fd, buf, count) + innerCount;
259 /* Password reading helpers */
261 static int untimed_read(int fd, char *pass, size_t maxlen)
265 i = read(fd, pass, maxlen);
269 } else if (i == 0) { /* EOF */
276 static int timed_read(int fd, char *pass, size_t maxlen, long timeout)
287 if (select(fd+1, &fds, NULL, NULL, &t) > 0)
288 failed = untimed_read(fd, pass, maxlen);
290 set_error("Operation timed out");
294 static int interactive_pass(const char *prompt, char *pass, size_t maxlen,
297 struct termios orig, tmp;
299 int infd = STDIN_FILENO, outfd;
304 /* Read and write to /dev/tty if available */
305 if ((infd = outfd = open("/dev/tty", O_RDWR)) == -1) {
307 outfd = STDERR_FILENO;
310 if (tcgetattr(infd, &orig)) {
311 set_error("Unable to get terminal");
314 memcpy(&tmp, &orig, sizeof(tmp));
315 tmp.c_lflag &= ~ECHO;
317 write(outfd, prompt, strlen(prompt));
318 tcsetattr(infd, TCSAFLUSH, &tmp);
320 failed = timed_read(infd, pass, maxlen, timeout);
322 failed = untimed_read(infd, pass, maxlen);
323 tcsetattr(infd, TCSAFLUSH, &orig);
327 write(outfd, "\n", 1);
328 if (infd != STDIN_FILENO)
334 * Password reading behaviour matrix of get_key
337 * -----------------+---+---+---+---
338 * interactive | Y | Y | Y | Inf
339 * from fd | N | N | Y | Inf
340 * from binary file | N | N | N | Inf or options->key_size
342 * Legend: p..prompt, v..can verify, n..newline-stop, h..read horizon
344 * Note: --key-file=- is interpreted as a read from a binary file (stdin)
346 * Returns true when more keys are available (that is when password
347 * reading can be retried as for interactive terminals).
350 int get_key(char *prompt, char **key, unsigned int *passLen, int key_size,
351 const char *key_file, int passphrase_fd, int timeout, int how2verify)
354 const int verify = how2verify & CRYPT_FLAG_VERIFY;
355 const int verify_if_possible = how2verify & CRYPT_FLAG_VERIFY_IF_POSSIBLE;
360 if(key_file && !strcmp(key_file, "-")) {
361 /* Allow binary reading from stdin */
365 } else if (key_file) {
366 fd = open(key_file, O_RDONLY);
369 set_error("Error opening key file: %s",
370 strerror_r(errno, buf, 128));
375 /* This can either be 0 (LUKS) or the actually number
376 * of key bytes (default or passed by -s) */
377 read_horizon = key_size;
381 read_horizon = 0; /* Infinite, if read from terminal or fd */
384 /* Interactive case */
388 pass = safe_alloc(512);
389 if (!pass || (i = interactive_pass(prompt, pass, 512, timeout))) {
390 set_error("Error reading passphrase");
393 if (verify || verify_if_possible) {
394 char pass_verify[512];
395 i = interactive_pass("Verify passphrase: ", pass_verify, sizeof(pass_verify), timeout);
396 if (i || strcmp(pass, pass_verify) != 0) {
397 set_error("Passphrases do not match");
400 memset(pass_verify, 0, sizeof(pass_verify));
402 *passLen = strlen(pass);
406 * This is either a fd-input or a file, in neither case we can verify the input,
407 * however we don't stop on new lines if it's a binary file.
412 set_error("Can't do passphrase verification on non-tty inputs");
415 /* The following for control loop does an exhausting
416 * read on the key material file, if requested with
417 * key_size == 0, as it's done by LUKS. However, we
418 * should warn the user, if it's a non-regular file,
419 * such as /dev/random, because in this case, the loop
422 if(key_file && strcmp(key_file, "-") && read_horizon == 0) {
424 if(stat(key_file, &st) < 0) {
425 set_error("Can't stat key file");
428 if(!S_ISREG(st.st_mode)) {
429 // set_error("Can't do exhausting read on non regular files");
431 fprintf(stderr,"Warning: exhausting read requested, but key file is not a regular file, function might never return.\n");
435 for(i = 0; read_horizon == 0 || i < read_horizon; i++) {
436 if(i >= buflen - 1) {
438 pass = safe_realloc(pass, buflen);
440 set_error("Not enough memory while "
441 "reading passphrase");
445 if(read(fd, pass + i, 1) != 1 || (newline_stop && pass[i] == '\n'))
455 return isatty(fd); /* Return true, when password reading can be tried on interactive fds */