Remove another compile warnings.
[platform/upstream/cryptsetup.git] / lib / utils_crypt.c
1 /*
2  * util_crypt - cipher utilities for cryptsetup
3  *
4  * Copyright (C) 2004-2007, Clemens Fruhwirth <clemens@endorphin.org>
5  * Copyright (C) 2009-2011, Red Hat, Inc. All rights reserved.
6  *
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.
10  *
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.
15  *
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
19  */
20
21 #include <stdlib.h>
22 #include <stddef.h>
23 #include <stdio.h>
24 #include <string.h>
25 #include <errno.h>
26 #include <sys/types.h>
27 #include <sys/stat.h>
28 #include <unistd.h>
29 #include <fcntl.h>
30 #include <termios.h>
31
32 #include "libcryptsetup.h"
33 #include "nls.h"
34 #include "utils_crypt.h"
35
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)
38
39 struct safe_allocation {
40         size_t  size;
41         char    data[0];
42 };
43
44 int crypt_parse_name_and_mode(const char *s, char *cipher, int *key_nums,
45                               char *cipher_mode)
46 {
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);
51                 if (key_nums) {
52                         char *tmp = strchr(cipher, ':');
53                         *key_nums = tmp ? atoi(++tmp) : 1;
54                         if (!*key_nums)
55                                 return -EINVAL;
56                 }
57
58                 return 0;
59         }
60
61         if (sscanf(s, "%" MAX_CIPHER_LEN_STR "[^-]", cipher) == 1) {
62                 strncpy(cipher_mode, "cbc-plain", 10);
63                 if (key_nums)
64                         *key_nums = 1;
65                 return 0;
66         }
67
68         return -EINVAL;
69 }
70
71 /* safe allocations */
72 void *crypt_safe_alloc(size_t size)
73 {
74         struct safe_allocation *alloc;
75
76         if (!size)
77                 return NULL;
78
79         alloc = malloc(size + offsetof(struct safe_allocation, data));
80         if (!alloc)
81                 return NULL;
82
83         alloc->size = size;
84
85         return &alloc->data;
86 }
87
88 void crypt_safe_free(void *data)
89 {
90         struct safe_allocation *alloc;
91
92         if (!data)
93                 return;
94
95         alloc = (struct safe_allocation *)
96                 ((char *)data - offsetof(struct safe_allocation, data));
97
98         memset(data, 0, alloc->size);
99
100         alloc->size = 0x55aa55aa;
101         free(alloc);
102 }
103
104 void *crypt_safe_realloc(void *data, size_t size)
105 {
106         struct safe_allocation *alloc;
107         void *new_data;
108
109         new_data = crypt_safe_alloc(size);
110
111         if (new_data && data) {
112
113                 alloc = (struct safe_allocation *)
114                         ((char *)data - offsetof(struct safe_allocation, data));
115
116                 if (size > alloc->size)
117                         size = alloc->size;
118
119                 memcpy(new_data, data, size);
120         }
121
122         crypt_safe_free(data);
123         return new_data;
124 }
125
126 /* Password reading helpers */
127 static int untimed_read(int fd, char *pass, size_t maxlen)
128 {
129         ssize_t i;
130
131         i = read(fd, pass, maxlen);
132         if (i > 0) {
133                 pass[i-1] = '\0';
134                 i = 0;
135         } else if (i == 0) { /* EOF */
136                 *pass = 0;
137                 i = -1;
138         }
139         return i;
140 }
141
142 static int timed_read(int fd, char *pass, size_t maxlen, long timeout)
143 {
144         struct timeval t;
145         fd_set fds;
146         int failed = -1;
147
148         FD_ZERO(&fds);
149         FD_SET(fd, &fds);
150         t.tv_sec = timeout;
151         t.tv_usec = 0;
152
153         if (select(fd+1, &fds, NULL, NULL, &t) > 0)
154                 failed = untimed_read(fd, pass, maxlen);
155
156         return failed;
157 }
158
159 static int interactive_pass(const char *prompt, char *pass, size_t maxlen,
160                 long timeout)
161 {
162         struct termios orig, tmp;
163         int failed = -1;
164         int infd = STDIN_FILENO, outfd;
165
166         if (maxlen < 1)
167                 goto out_err;
168
169         /* Read and write to /dev/tty if available */
170         if ((infd = outfd = open("/dev/tty", O_RDWR)) == -1) {
171                 infd = STDIN_FILENO;
172                 outfd = STDERR_FILENO;
173         }
174
175         if (tcgetattr(infd, &orig))
176                 goto out_err;
177
178         memcpy(&tmp, &orig, sizeof(tmp));
179         tmp.c_lflag &= ~ECHO;
180
181         if (prompt && write(outfd, prompt, strlen(prompt)) < 0)
182                 goto out_err;
183
184         tcsetattr(infd, TCSAFLUSH, &tmp);
185         if (timeout)
186                 failed = timed_read(infd, pass, maxlen, timeout);
187         else
188                 failed = untimed_read(infd, pass, maxlen);
189         tcsetattr(infd, TCSAFLUSH, &orig);
190
191 out_err:
192         if (!failed && write(outfd, "\n", 1)) {};
193
194         if (infd != STDIN_FILENO)
195                 close(infd);
196         return failed;
197 }
198
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)
203 {
204         int key_size_max = DEFAULT_PASSPHRASE_SIZE_MAX;
205         int r = -EINVAL;
206         char *pass = NULL, *pass_verify = NULL;
207
208         log_dbg("Interactive passphrase entry requested.");
209
210         pass = crypt_safe_alloc(key_size_max + 1);
211         if (!pass) {
212                 log_err(cd, _("Out of memory while reading passphrase.\n"));
213                 return -ENOMEM;
214         }
215
216         if (interactive_pass(prompt, pass, key_size_max, timeout)) {
217                 log_err(cd, _("Error reading passphrase from terminal.\n"));
218                 goto out_err;
219         }
220         pass[key_size_max] = '\0';
221
222         if (verify) {
223                 pass_verify = crypt_safe_alloc(key_size_max);
224                 if (!pass_verify) {
225                         log_err(cd, _("Out of memory while reading passphrase.\n"));
226                         r = -ENOMEM;
227                         goto out_err;
228                 }
229
230                 if (interactive_pass(_("Verify passphrase: "),
231                     pass_verify, key_size_max, timeout)) {
232                         log_err(cd, _("Error reading passphrase from terminal.\n"));
233                         goto out_err;
234                 }
235
236                 if (strncmp(pass, pass_verify, key_size_max)) {
237                         log_err(cd, _("Passphrases do not match.\n"));
238                         goto out_err;
239                 }
240         }
241
242         *key = pass;
243         *key_size = strlen(pass);
244         r = 0;
245 out_err:
246         crypt_safe_free(pass_verify);
247         if (r)
248                 crypt_safe_free(pass);
249         return r;
250 }
251
252 /*
253  * Note: --key-file=- is interpreted as a read from a binary file (stdin)
254  * key_size_max == 0 means detect maximum according to input type (tty/file)
255  * timeout and verify options only applies to tty input
256  */
257 int crypt_get_key(const char *prompt,
258                   char **key, size_t *key_size,
259                   size_t keyfile_size_max, const char *key_file,
260                   int timeout, int verify,
261                   struct crypt_device *cd)
262 {
263         int fd, regular_file, read_stdin, char_read, unlimited_read = 0;
264         int r = -EINVAL;
265         char *pass = NULL;
266         size_t buflen, i;
267         struct stat st;
268
269         *key = NULL;
270         *key_size = 0;
271
272         /* Passphrase read from stdin? */
273         read_stdin = (!key_file || !strcmp(key_file, "-")) ? 1 : 0;
274
275         if(read_stdin && isatty(STDIN_FILENO))
276                 return crypt_get_key_tty(prompt, key, key_size, timeout, verify, cd);
277
278         if (read_stdin)
279                 log_dbg("STDIN descriptor passphrase entry requested.");
280         else
281                 log_dbg("File descriptor passphrase entry requested.");
282
283         /* If not requsted otherwise, we limit input to prevent memory exhaustion */
284         if (keyfile_size_max == 0) {
285                 keyfile_size_max = DEFAULT_KEYFILE_SIZE_MAXKB * 1024;
286                 unlimited_read = 1;
287         }
288
289         fd = read_stdin ? STDIN_FILENO : open(key_file, O_RDONLY);
290         if (fd < 0) {
291                 log_err(cd, _("Failed to open key file.\n"));
292                 return -EINVAL;
293         }
294
295         /* use 4k for buffer (page divisor but avoid huge pages) */
296         buflen = 4096 - sizeof(struct safe_allocation);
297         regular_file = 0;
298         if(!read_stdin) {
299                 if(stat(key_file, &st) < 0) {
300                         log_err(cd, _("Failed to stat key file.\n"));
301                         goto out_err;
302                 }
303                 if(S_ISREG(st.st_mode)) {
304                         regular_file = 1;
305                         /* known keyfile size, alloc it in one step */
306                         if ((size_t)st.st_size >= keyfile_size_max)
307                                 buflen = keyfile_size_max;
308                         else
309                                 buflen = st.st_size;
310                 }
311         }
312
313         pass = crypt_safe_alloc(buflen);
314         if (!pass) {
315                 log_err(cd, _("Out of memory while reading passphrase.\n"));
316                 goto out_err;
317         }
318
319         for(i = 0; i < keyfile_size_max; i++) {
320                 if(i == buflen) {
321                         buflen += 4096;
322                         pass = crypt_safe_realloc(pass, buflen);
323                         if (!pass) {
324                                 log_err(cd, _("Out of memory while reading passphrase.\n"));
325                                 r = -ENOMEM;
326                                 goto out_err;
327                         }
328                 }
329
330                 char_read = read(fd, &pass[i], 1);
331                 if (char_read < 0) {
332                         log_err(cd, _("Error reading passphrase.\n"));
333                         goto out_err;
334                 }
335
336                 /* Stop on newline only if not requested read from keyfile */
337                 if(char_read == 0 || (!key_file && pass[i] == '\n'))
338                         break;
339         }
340
341         /* Fail if piped input dies reading nothing */
342         if(!i && !regular_file) {
343                 log_dbg("Nothing read on input.");
344                 r = -EPIPE;
345                 goto out_err;
346         }
347
348         /* Fail if we exceeded internal default (no specified size) */
349         if (unlimited_read && i == keyfile_size_max) {
350                 log_err(cd, _("Maximum keyfile size exceeeded.\n"));
351                 goto out_err;
352         }
353
354         if (!unlimited_read && i != keyfile_size_max) {
355                 log_err(cd, _("Cannot read requested amount of data.\n"));
356                 goto out_err;
357         }
358
359         /* Well, for historical reasons reading empty keyfile is not fail. */
360         if(!i) {
361                 crypt_safe_free(pass);
362                 pass = NULL;
363         }
364
365         *key = pass;
366         *key_size = i;
367         r = 0;
368 out_err:
369         if(fd != STDIN_FILENO)
370                 close(fd);
371
372         if (r)
373                 crypt_safe_free(pass);
374         return r;
375 }