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, int *key_nums,
24 if (sscanf(s, "%" MAX_CIPHER_LEN_STR "[^-]-%" MAX_CIPHER_LEN_STR "s",
25 cipher, cipher_mode) == 2) {
26 if (!strcmp(cipher_mode, "plain"))
27 strncpy(cipher_mode, "cbc-plain", 10);
29 char *tmp = strchr(cipher, ':');
30 *key_nums = tmp ? atoi(++tmp) : 1;
38 if (sscanf(s, "%" MAX_CIPHER_LEN_STR "[^-]", cipher) == 1) {
39 strncpy(cipher_mode, "cbc-plain", 10);
48 /* safe allocations */
49 void *crypt_safe_alloc(size_t size)
51 struct safe_allocation *alloc;
56 alloc = malloc(size + offsetof(struct safe_allocation, data));
65 void crypt_safe_free(void *data)
67 struct safe_allocation *alloc;
72 alloc = data - offsetof(struct safe_allocation, data);
74 memset(data, 0, alloc->size);
76 alloc->size = 0x55aa55aa;
80 void *crypt_safe_realloc(void *data, size_t size)
84 new_data = crypt_safe_alloc(size);
86 if (new_data && data) {
87 struct safe_allocation *alloc;
89 alloc = data - offsetof(struct safe_allocation, data);
91 if (size > alloc->size)
94 memcpy(new_data, data, size);
97 crypt_safe_free(data);
101 /* Password reading helpers */
102 static int untimed_read(int fd, char *pass, size_t maxlen)
106 i = read(fd, pass, maxlen);
110 } else if (i == 0) { /* EOF */
117 static int timed_read(int fd, char *pass, size_t maxlen, long timeout)
128 if (select(fd+1, &fds, NULL, NULL, &t) > 0)
129 failed = untimed_read(fd, pass, maxlen);
134 static int interactive_pass(const char *prompt, char *pass, size_t maxlen,
137 struct termios orig, tmp;
139 int infd = STDIN_FILENO, outfd;
144 /* Read and write to /dev/tty if available */
145 if ((infd = outfd = open("/dev/tty", O_RDWR)) == -1) {
147 outfd = STDERR_FILENO;
150 if (tcgetattr(infd, &orig))
153 memcpy(&tmp, &orig, sizeof(tmp));
154 tmp.c_lflag &= ~ECHO;
156 if (prompt && write(outfd, prompt, strlen(prompt)) < 0)
159 tcsetattr(infd, TCSAFLUSH, &tmp);
161 failed = timed_read(infd, pass, maxlen, timeout);
163 failed = untimed_read(infd, pass, maxlen);
164 tcsetattr(infd, TCSAFLUSH, &orig);
167 if (!failed && write(outfd, "\n", 1)) {};
169 if (infd != STDIN_FILENO)
175 * Password reading behaviour matrix of get_key
176 * FIXME: rewrite this from scratch.
178 * -----------------+---+---+---+---
179 * interactive | Y | Y | Y | Inf
180 * from fd | N | N | Y | Inf
181 * from binary file | N | N | N | Inf or options->key_size
183 * Legend: p..prompt, v..can verify, n..newline-stop, h..read horizon
185 * Note: --key-file=- is interpreted as a read from a binary file (stdin)
188 int crypt_get_key(char *prompt, char **key, unsigned int *passLen, int key_size,
189 const char *key_file, int timeout, int verify,
190 struct crypt_device *cd)
195 int regular_file = 0;
200 /* Passphrase read from stdin? */
201 read_stdin = (!key_file || !strcmp(key_file, "-")) ? 1 : 0;
203 /* read_horizon applies only for real keyfile, not stdin or terminal */
204 read_horizon = (key_file && !read_stdin) ? key_size : 0 /* until EOF */;
206 /* Setup file descriptior */
207 fd = read_stdin ? STDIN_FILENO : open(key_file, O_RDONLY);
209 crypt_log(cd, CRYPT_LOG_ERROR,
210 _("Failed to open key file.\n"));
214 /* Interactive case */
218 pass = crypt_safe_alloc(MAX_TTY_PASSWORD_LEN);
219 if (!pass || interactive_pass(prompt, pass, MAX_TTY_PASSWORD_LEN, timeout)) {
220 crypt_log(cd, CRYPT_LOG_ERROR,
221 _("Error reading passphrase from terminal.\n"));
225 char pass_verify[MAX_TTY_PASSWORD_LEN];
226 i = interactive_pass(_("Verify passphrase: "), pass_verify, sizeof(pass_verify), timeout);
227 if (i || strcmp(pass, pass_verify) != 0) {
228 crypt_log(cd, CRYPT_LOG_ERROR,
229 _("Passphrases do not match.\n"));
232 memset(pass_verify, 0, sizeof(pass_verify));
234 *passLen = strlen(pass);
238 * This is either a fd-input or a file, in neither case we can verify the input,
239 * however we don't stop on new lines if it's a binary file.
243 /* The following for control loop does an exhausting
244 * read on the key material file, if requested with
245 * key_size == 0, as it's done by LUKS. However, we
246 * should warn the user, if it's a non-regular file,
247 * such as /dev/random, because in this case, the loop
250 if(!read_stdin && read_horizon == 0) {
251 if(stat(key_file, &st) < 0) {
252 crypt_log(cd, CRYPT_LOG_ERROR,
253 _("Failed to stat key file.\n"));
256 if(!S_ISREG(st.st_mode))
257 crypt_log(cd, CRYPT_LOG_NORMAL,
258 _("Warning: exhausting read requested, but key file"
259 " is not a regular file, function might never return.\n"));
264 for(i = 0; read_horizon == 0 || i < read_horizon; i++) {
265 if(i >= buflen - 1) {
267 pass = crypt_safe_realloc(pass, buflen);
269 crypt_log(cd, CRYPT_LOG_ERROR,
270 _("Out of memory while reading passphrase.\n"));
275 r = read(fd, pass + i, 1);
277 crypt_log(cd, CRYPT_LOG_ERROR,
278 _("Error reading passphrase.\n"));
282 /* Stop on newline only if not requested read from keyfile */
283 if(r == 0 || (!key_file && pass[i] == '\n'))
286 /* Fail if piped input dies reading nothing */
287 if(!i && !regular_file)
293 if(fd != STDIN_FILENO)
298 if(fd >= 0 && fd != STDIN_FILENO)
301 crypt_safe_free(pass);