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)
35 vasprintf(&error, fmt, va);
38 void set_error(const char *fmt, ...)
43 set_error_va(fmt, va);
47 const char *get_error(void)
52 void *safe_alloc(size_t size)
54 struct safe_allocation *alloc;
59 alloc = malloc(size + offsetof(struct safe_allocation, data));
68 void safe_free(void *data)
70 struct safe_allocation *alloc;
75 alloc = data - offsetof(struct safe_allocation, data);
77 memset(data, 0, alloc->size);
79 alloc->size = 0x55aa55aa;
83 void *safe_realloc(void *data, size_t size)
87 new_data = safe_alloc(size);
89 if (new_data && data) {
90 struct safe_allocation *alloc;
92 alloc = data - offsetof(struct safe_allocation, data);
94 if (size > alloc->size)
97 memcpy(new_data, data, size);
104 char *safe_strdup(const char *s)
106 char *s2 = safe_alloc(strlen(s) + 1);
111 return strcpy(s2, s);
114 /* Credits go to Michal's padlock patches for this alignment code */
116 static void *aligned_malloc(char **base, int size, int alignment)
120 ptr = malloc(size + alignment);
121 if(ptr == NULL) return NULL;
124 if(alignment > 1 && ((long)ptr & (alignment - 1))) {
125 ptr += alignment - ((long)(ptr) & (alignment - 1));
130 static int sector_size(int fd)
133 if (ioctl(fd,BLKSSZGET, &bsize) < 0)
139 int sector_size_for_device(const char *device)
141 int fd = open(device, O_RDONLY);
150 ssize_t write_blockwise(int fd, const void *orig_buf, size_t count)
152 char *padbuf; char *padbuf_base;
153 char *buf = (char *)orig_buf;
155 int hangover; int solid; int bsize;
157 if ((bsize = sector_size(fd)) < 0)
160 hangover = count % bsize;
161 solid = count - hangover;
163 padbuf = aligned_malloc(&padbuf_base, bsize, bsize);
164 if(padbuf == NULL) return -ENOMEM;
167 memcpy(padbuf, buf, bsize);
168 r = write(fd, padbuf, bsize);
169 if(r < 0 || r != bsize) goto out;
175 r = read(fd,padbuf,bsize);
176 if(r < 0 || r != bsize) goto out;
178 lseek(fd,-bsize,SEEK_CUR);
179 memcpy(padbuf,buf,hangover);
181 r = write(fd,padbuf, bsize);
182 if(r < 0 || r != bsize) goto out;
187 return (buf-(char *)orig_buf)?(buf-(char *)orig_buf):r;
191 ssize_t read_blockwise(int fd, void *orig_buf, size_t count) {
192 char *padbuf; char *padbuf_base;
193 char *buf = (char *)orig_buf;
198 if ((bsize = sector_size(fd)) < 0)
201 padbuf = aligned_malloc(&padbuf_base, bsize, bsize);
202 if(padbuf == NULL) return -ENOMEM;
205 r = read(fd,padbuf,bsize);
206 if(r < 0 || r != bsize) {
207 set_error("read failed in read_blockwise.\n");
210 step = count<bsize?count:bsize;
211 memcpy(buf,padbuf,step);
217 return (buf-(char *)orig_buf)?(buf-(char *)orig_buf):r;
221 * Combines llseek with blockwise write. write_blockwise can already deal with short writes
222 * but we also need a function to deal with short writes at the start. But this information
223 * is implicitly included in the read/write offset, which can not be set to non-aligned
224 * boundaries. Hence, we combine llseek with write.
227 ssize_t write_lseek_blockwise(int fd, const char *buf, size_t count, off_t offset) {
228 int bsize = sector_size(fd);
229 const char *orig_buf = buf;
230 char frontPadBuf[bsize];
231 int frontHang = offset % bsize;
233 int innerCount = count < bsize ? count : bsize;
238 lseek(fd, offset - frontHang, SEEK_SET);
240 r = read(fd,frontPadBuf,bsize);
243 memcpy(frontPadBuf+frontHang, buf, innerCount);
245 lseek(fd, offset - frontHang, SEEK_SET);
246 r = write(fd,frontPadBuf,bsize);
252 if(count <= 0) return buf - orig_buf;
254 return write_blockwise(fd, buf, count) + innerCount;
257 /* Password reading helpers */
259 static int untimed_read(int fd, char *pass, size_t maxlen)
263 i = read(fd, pass, maxlen);
267 } else if (i == 0) { /* EOF */
274 static int timed_read(int fd, char *pass, size_t maxlen, long timeout)
285 if (select(fd+1, &fds, NULL, NULL, &t) > 0)
286 failed = untimed_read(fd, pass, maxlen);
288 set_error("Operation timed out");
292 static int interactive_pass(const char *prompt, char *pass, size_t maxlen,
295 struct termios orig, tmp;
297 int infd = STDIN_FILENO, outfd;
302 /* Read and write to /dev/tty if available */
303 if ((infd = outfd = open("/dev/tty", O_RDWR)) == -1) {
305 outfd = STDERR_FILENO;
308 if (tcgetattr(infd, &orig)) {
309 set_error("Unable to get terminal");
312 memcpy(&tmp, &orig, sizeof(tmp));
313 tmp.c_lflag &= ~ECHO;
315 write(outfd, prompt, strlen(prompt));
316 tcsetattr(infd, TCSAFLUSH, &tmp);
318 failed = timed_read(infd, pass, maxlen, timeout);
320 failed = untimed_read(infd, pass, maxlen);
321 tcsetattr(infd, TCSAFLUSH, &orig);
325 write(outfd, "\n", 1);
326 if (infd != STDIN_FILENO)
332 * Password reading behaviour matrix of get_key
335 * -----------------+---+---+---+---
336 * interactive | Y | Y | Y | Inf
337 * from fd | N | N | Y | Inf
338 * from binary file | N | N | N | Inf or options->key_size
340 * Legend: p..prompt, v..can verify, n..newline-stop, h..read horizon
342 * Note: --key-file=- is interpreted as a read from a binary file (stdin)
344 * Returns true when more keys are available (that is when password
345 * reading can be retried as for interactive terminals).
348 int get_key(char *prompt, char **key, unsigned int *passLen, int key_size,
349 const char *key_file, int passphrase_fd, int timeout, int how2verify)
352 const int verify = how2verify & CRYPT_FLAG_VERIFY;
353 const int verify_if_possible = how2verify & CRYPT_FLAG_VERIFY_IF_POSSIBLE;
358 if(key_file && !strcmp(key_file, "-")) {
359 /* Allow binary reading from stdin */
363 } else if (key_file) {
364 fd = open(key_file, O_RDONLY);
367 set_error("Error opening key file: %s",
368 strerror_r(errno, buf, 128));
373 /* This can either be 0 (LUKS) or the actually number
374 * of key bytes (default or passed by -s) */
375 read_horizon = key_size;
379 read_horizon = 0; /* Infinite, if read from terminal or fd */
382 /* Interactive case */
386 pass = safe_alloc(512);
387 if (!pass || (i = interactive_pass(prompt, pass, 512, timeout))) {
388 set_error("Error reading passphrase");
391 if (verify || verify_if_possible) {
392 char pass_verify[512];
393 i = interactive_pass("Verify passphrase: ", pass_verify, sizeof(pass_verify), timeout);
394 if (i || strcmp(pass, pass_verify) != 0) {
395 set_error("Passphrases do not match");
398 memset(pass_verify, 0, sizeof(pass_verify));
400 *passLen = strlen(pass);
404 * This is either a fd-input or a file, in neither case we can verify the input,
405 * however we don't stop on new lines if it's a binary file.
410 set_error("Can't do passphrase verification on non-tty inputs");
413 /* The following for control loop does an exhausting
414 * read on the key material file, if requested with
415 * key_size == 0, as it's done by LUKS. However, we
416 * should warn the user, if it's a non-regular file,
417 * such as /dev/random, because in this case, the loop
420 if(key_file && strcmp(key_file, "-") && read_horizon == 0) {
422 if(stat(key_file, &st) < 0) {
423 set_error("Can't stat key file");
426 if(!S_ISREG(st.st_mode)) {
427 // set_error("Can't do exhausting read on non regular files");
429 fprintf(stderr,"Warning: exhausting read requested, but key file is not a regular file, function might never return.\n");
433 for(i = 0; read_horizon == 0 || i < read_horizon; i++) {
434 if(i >= buflen - 1) {
436 pass = safe_realloc(pass, buflen);
438 set_error("Not enough memory while "
439 "reading passphrase");
443 if(read(fd, pass + i, 1) != 1 || (newline_stop && pass[i] == '\n'))
453 return isatty(fd); /* Return true, when password reading can be tried on interactive fds */