xref: /openbsd/usr.bin/ssh/authfile.c (revision b6a756d5)
1 /* $OpenBSD: authfile.c,v 1.112 2015/03/18 01:44:21 djm Exp $ */
2 /*
3  * Copyright (c) 2000, 2013 Markus Friedl.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */
25 
26 
27 #include <sys/types.h>
28 #include <sys/stat.h>
29 #include <sys/uio.h>
30 
31 #include <errno.h>
32 #include <fcntl.h>
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <string.h>
36 #include <unistd.h>
37 #include <limits.h>
38 
39 #include "cipher.h"
40 #include "key.h"
41 #include "ssh.h"
42 #include "log.h"
43 #include "authfile.h"
44 #include "rsa.h"
45 #include "misc.h"
46 #include "atomicio.h"
47 #include "sshbuf.h"
48 #include "ssherr.h"
49 #include "krl.h"
50 
51 #define MAX_KEY_FILE_SIZE	(1024 * 1024)
52 
53 /* Save a key blob to a file */
54 static int
55 sshkey_save_private_blob(struct sshbuf *keybuf, const char *filename)
56 {
57 	int fd, oerrno;
58 
59 	if ((fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0600)) < 0)
60 		return SSH_ERR_SYSTEM_ERROR;
61 	if (atomicio(vwrite, fd, (u_char *)sshbuf_ptr(keybuf),
62 	    sshbuf_len(keybuf)) != sshbuf_len(keybuf)) {
63 		oerrno = errno;
64 		close(fd);
65 		unlink(filename);
66 		errno = oerrno;
67 		return SSH_ERR_SYSTEM_ERROR;
68 	}
69 	close(fd);
70 	return 0;
71 }
72 
73 int
74 sshkey_save_private(struct sshkey *key, const char *filename,
75     const char *passphrase, const char *comment,
76     int force_new_format, const char *new_format_cipher, int new_format_rounds)
77 {
78 	struct sshbuf *keyblob = NULL;
79 	int r;
80 
81 	if ((keyblob = sshbuf_new()) == NULL)
82 		return SSH_ERR_ALLOC_FAIL;
83 	if ((r = sshkey_private_to_fileblob(key, keyblob, passphrase, comment,
84 	    force_new_format, new_format_cipher, new_format_rounds)) != 0)
85 		goto out;
86 	if ((r = sshkey_save_private_blob(keyblob, filename)) != 0)
87 		goto out;
88 	r = 0;
89  out:
90 	sshbuf_free(keyblob);
91 	return r;
92 }
93 
94 /* Load a key from a fd into a buffer */
95 int
96 sshkey_load_file(int fd, struct sshbuf *blob)
97 {
98 	u_char buf[1024];
99 	size_t len;
100 	struct stat st;
101 	int r;
102 
103 	if (fstat(fd, &st) < 0)
104 		return SSH_ERR_SYSTEM_ERROR;
105 	if ((st.st_mode & (S_IFSOCK|S_IFCHR|S_IFIFO)) == 0 &&
106 	    st.st_size > MAX_KEY_FILE_SIZE)
107 		return SSH_ERR_INVALID_FORMAT;
108 	for (;;) {
109 		if ((len = atomicio(read, fd, buf, sizeof(buf))) == 0) {
110 			if (errno == EPIPE)
111 				break;
112 			r = SSH_ERR_SYSTEM_ERROR;
113 			goto out;
114 		}
115 		if ((r = sshbuf_put(blob, buf, len)) != 0)
116 			goto out;
117 		if (sshbuf_len(blob) > MAX_KEY_FILE_SIZE) {
118 			r = SSH_ERR_INVALID_FORMAT;
119 			goto out;
120 		}
121 	}
122 	if ((st.st_mode & (S_IFSOCK|S_IFCHR|S_IFIFO)) == 0 &&
123 	    st.st_size != (off_t)sshbuf_len(blob)) {
124 		r = SSH_ERR_FILE_CHANGED;
125 		goto out;
126 	}
127 	r = 0;
128 
129  out:
130 	explicit_bzero(buf, sizeof(buf));
131 	if (r != 0)
132 		sshbuf_reset(blob);
133 	return r;
134 }
135 
136 #ifdef WITH_SSH1
137 /*
138  * Loads the public part of the ssh v1 key file.  Returns NULL if an error was
139  * encountered (the file does not exist or is not readable), and the key
140  * otherwise.
141  */
142 static int
143 sshkey_load_public_rsa1(int fd, struct sshkey **keyp, char **commentp)
144 {
145 	struct sshbuf *b = NULL;
146 	int r;
147 
148 	*keyp = NULL;
149 	if (commentp != NULL)
150 		*commentp = NULL;
151 
152 	if ((b = sshbuf_new()) == NULL)
153 		return SSH_ERR_ALLOC_FAIL;
154 	if ((r = sshkey_load_file(fd, b)) != 0)
155 		goto out;
156 	if ((r = sshkey_parse_public_rsa1_fileblob(b, keyp, commentp)) != 0)
157 		goto out;
158 	r = 0;
159  out:
160 	sshbuf_free(b);
161 	return r;
162 }
163 #endif /* WITH_SSH1 */
164 
165 /* XXX remove error() calls from here? */
166 int
167 sshkey_perm_ok(int fd, const char *filename)
168 {
169 	struct stat st;
170 
171 	if (fstat(fd, &st) < 0)
172 		return SSH_ERR_SYSTEM_ERROR;
173 	/*
174 	 * if a key owned by the user is accessed, then we check the
175 	 * permissions of the file. if the key owned by a different user,
176 	 * then we don't care.
177 	 */
178 	if ((st.st_uid == getuid()) && (st.st_mode & 077) != 0) {
179 		error("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
180 		error("@         WARNING: UNPROTECTED PRIVATE KEY FILE!          @");
181 		error("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
182 		error("Permissions 0%3.3o for '%s' are too open.",
183 		    (u_int)st.st_mode & 0777, filename);
184 		error("It is recommended that your private key files are NOT accessible by others.");
185 		error("This private key will be ignored.");
186 		return SSH_ERR_KEY_BAD_PERMISSIONS;
187 	}
188 	return 0;
189 }
190 
191 /* XXX kill perm_ok now that we have SSH_ERR_KEY_BAD_PERMISSIONS? */
192 int
193 sshkey_load_private_type(int type, const char *filename, const char *passphrase,
194     struct sshkey **keyp, char **commentp, int *perm_ok)
195 {
196 	int fd, r;
197 
198 	*keyp = NULL;
199 	if (commentp != NULL)
200 		*commentp = NULL;
201 
202 	if ((fd = open(filename, O_RDONLY)) < 0) {
203 		if (perm_ok != NULL)
204 			*perm_ok = 0;
205 		return SSH_ERR_SYSTEM_ERROR;
206 	}
207 	if (sshkey_perm_ok(fd, filename) != 0) {
208 		if (perm_ok != NULL)
209 			*perm_ok = 0;
210 		r = SSH_ERR_KEY_BAD_PERMISSIONS;
211 		goto out;
212 	}
213 	if (perm_ok != NULL)
214 		*perm_ok = 1;
215 
216 	r = sshkey_load_private_type_fd(fd, type, passphrase, keyp, commentp);
217  out:
218 	close(fd);
219 	return r;
220 }
221 
222 int
223 sshkey_load_private_type_fd(int fd, int type, const char *passphrase,
224     struct sshkey **keyp, char **commentp)
225 {
226 	struct sshbuf *buffer = NULL;
227 	int r;
228 
229 	if ((buffer = sshbuf_new()) == NULL) {
230 		r = SSH_ERR_ALLOC_FAIL;
231 		goto out;
232 	}
233 	if ((r = sshkey_load_file(fd, buffer)) != 0 ||
234 	    (r = sshkey_parse_private_fileblob_type(buffer, type,
235 	    passphrase, keyp, commentp)) != 0)
236 		goto out;
237 
238 	/* success */
239 	r = 0;
240  out:
241 	if (buffer != NULL)
242 		sshbuf_free(buffer);
243 	return r;
244 }
245 
246 /* XXX this is almost identical to sshkey_load_private_type() */
247 int
248 sshkey_load_private(const char *filename, const char *passphrase,
249     struct sshkey **keyp, char **commentp)
250 {
251 	struct sshbuf *buffer = NULL;
252 	int r, fd;
253 
254 	*keyp = NULL;
255 	if (commentp != NULL)
256 		*commentp = NULL;
257 
258 	if ((fd = open(filename, O_RDONLY)) < 0)
259 		return SSH_ERR_SYSTEM_ERROR;
260 	if (sshkey_perm_ok(fd, filename) != 0) {
261 		r = SSH_ERR_KEY_BAD_PERMISSIONS;
262 		goto out;
263 	}
264 
265 	if ((buffer = sshbuf_new()) == NULL) {
266 		r = SSH_ERR_ALLOC_FAIL;
267 		goto out;
268 	}
269 	if ((r = sshkey_load_file(fd, buffer)) != 0 ||
270 	    (r = sshkey_parse_private_fileblob(buffer, passphrase, filename,
271 	    keyp, commentp)) != 0)
272 		goto out;
273 	r = 0;
274  out:
275 	close(fd);
276 	if (buffer != NULL)
277 		sshbuf_free(buffer);
278 	return r;
279 }
280 
281 static int
282 sshkey_try_load_public(struct sshkey *k, const char *filename, char **commentp)
283 {
284 	FILE *f;
285 	char line[SSH_MAX_PUBKEY_BYTES];
286 	char *cp;
287 	u_long linenum = 0;
288 	int r;
289 
290 	if (commentp != NULL)
291 		*commentp = NULL;
292 	if ((f = fopen(filename, "r")) == NULL)
293 		return SSH_ERR_SYSTEM_ERROR;
294 	while (read_keyfile_line(f, filename, line, sizeof(line),
295 		    &linenum) != -1) {
296 		cp = line;
297 		switch (*cp) {
298 		case '#':
299 		case '\n':
300 		case '\0':
301 			continue;
302 		}
303 		/* Abort loading if this looks like a private key */
304 		if (strncmp(cp, "-----BEGIN", 10) == 0 ||
305 		    strcmp(cp, "SSH PRIVATE KEY FILE") == 0)
306 			break;
307 		/* Skip leading whitespace. */
308 		for (; *cp && (*cp == ' ' || *cp == '\t'); cp++)
309 			;
310 		if (*cp) {
311 			if ((r = sshkey_read(k, &cp)) == 0) {
312 				cp[strcspn(cp, "\r\n")] = '\0';
313 				if (commentp) {
314 					*commentp = strdup(*cp ?
315 					    cp : filename);
316 					if (*commentp == NULL)
317 						r = SSH_ERR_ALLOC_FAIL;
318 				}
319 				fclose(f);
320 				return r;
321 			}
322 		}
323 	}
324 	fclose(f);
325 	return SSH_ERR_INVALID_FORMAT;
326 }
327 
328 /* load public key from ssh v1 private or any pubkey file */
329 int
330 sshkey_load_public(const char *filename, struct sshkey **keyp, char **commentp)
331 {
332 	struct sshkey *pub = NULL;
333 	char file[PATH_MAX];
334 	int r, fd;
335 
336 	if (keyp != NULL)
337 		*keyp = NULL;
338 	if (commentp != NULL)
339 		*commentp = NULL;
340 
341 	/* XXX should load file once and attempt to parse each format */
342 
343 	if ((fd = open(filename, O_RDONLY)) < 0)
344 		goto skip;
345 #ifdef WITH_SSH1
346 	/* try rsa1 private key */
347 	r = sshkey_load_public_rsa1(fd, keyp, commentp);
348 	close(fd);
349 	switch (r) {
350 	case SSH_ERR_INTERNAL_ERROR:
351 	case SSH_ERR_ALLOC_FAIL:
352 	case SSH_ERR_INVALID_ARGUMENT:
353 	case SSH_ERR_SYSTEM_ERROR:
354 	case 0:
355 		return r;
356 	}
357 #endif /* WITH_SSH1 */
358 
359 	/* try ssh2 public key */
360 	if ((pub = sshkey_new(KEY_UNSPEC)) == NULL)
361 		return SSH_ERR_ALLOC_FAIL;
362 	if ((r = sshkey_try_load_public(pub, filename, commentp)) == 0) {
363 		if (keyp != NULL)
364 			*keyp = pub;
365 		return 0;
366 	}
367 	sshkey_free(pub);
368 
369 #ifdef WITH_SSH1
370 	/* try rsa1 public key */
371 	if ((pub = sshkey_new(KEY_RSA1)) == NULL)
372 		return SSH_ERR_ALLOC_FAIL;
373 	if ((r = sshkey_try_load_public(pub, filename, commentp)) == 0) {
374 		if (keyp != NULL)
375 			*keyp = pub;
376 		return 0;
377 	}
378 	sshkey_free(pub);
379 #endif /* WITH_SSH1 */
380 
381  skip:
382 	/* try .pub suffix */
383 	if ((pub = sshkey_new(KEY_UNSPEC)) == NULL)
384 		return SSH_ERR_ALLOC_FAIL;
385 	r = SSH_ERR_ALLOC_FAIL;	/* in case strlcpy or strlcat fail */
386 	if ((strlcpy(file, filename, sizeof file) < sizeof(file)) &&
387 	    (strlcat(file, ".pub", sizeof file) < sizeof(file)) &&
388 	    (r = sshkey_try_load_public(pub, file, commentp)) == 0) {
389 		if (keyp != NULL)
390 			*keyp = pub;
391 		return 0;
392 	}
393 	sshkey_free(pub);
394 
395 	return r;
396 }
397 
398 /* Load the certificate associated with the named private key */
399 int
400 sshkey_load_cert(const char *filename, struct sshkey **keyp)
401 {
402 	struct sshkey *pub = NULL;
403 	char *file = NULL;
404 	int r = SSH_ERR_INTERNAL_ERROR;
405 
406 	*keyp = NULL;
407 
408 	if (asprintf(&file, "%s-cert.pub", filename) == -1)
409 		return SSH_ERR_ALLOC_FAIL;
410 
411 	if ((pub = sshkey_new(KEY_UNSPEC)) == NULL) {
412 		goto out;
413 	}
414 	if ((r = sshkey_try_load_public(pub, file, NULL)) != 0)
415 		goto out;
416 
417 	*keyp = pub;
418 	pub = NULL;
419 	r = 0;
420 
421  out:
422 	if (file != NULL)
423 		free(file);
424 	if (pub != NULL)
425 		sshkey_free(pub);
426 	return r;
427 }
428 
429 /* Load private key and certificate */
430 int
431 sshkey_load_private_cert(int type, const char *filename, const char *passphrase,
432     struct sshkey **keyp, int *perm_ok)
433 {
434 	struct sshkey *key = NULL, *cert = NULL;
435 	int r;
436 
437 	*keyp = NULL;
438 
439 	switch (type) {
440 #ifdef WITH_OPENSSL
441 	case KEY_RSA:
442 	case KEY_DSA:
443 	case KEY_ECDSA:
444 	case KEY_ED25519:
445 #endif /* WITH_OPENSSL */
446 	case KEY_UNSPEC:
447 		break;
448 	default:
449 		return SSH_ERR_KEY_TYPE_UNKNOWN;
450 	}
451 
452 	if ((r = sshkey_load_private_type(type, filename,
453 	    passphrase, &key, NULL, perm_ok)) != 0 ||
454 	    (r = sshkey_load_cert(filename, &cert)) != 0)
455 		goto out;
456 
457 	/* Make sure the private key matches the certificate */
458 	if (sshkey_equal_public(key, cert) == 0) {
459 		r = SSH_ERR_KEY_CERT_MISMATCH;
460 		goto out;
461 	}
462 
463 	if ((r = sshkey_to_certified(key, sshkey_cert_is_legacy(cert))) != 0 ||
464 	    (r = sshkey_cert_copy(cert, key)) != 0)
465 		goto out;
466 	r = 0;
467 	*keyp = key;
468 	key = NULL;
469  out:
470 	if (key != NULL)
471 		sshkey_free(key);
472 	if (cert != NULL)
473 		sshkey_free(cert);
474 	return r;
475 }
476 
477 /*
478  * Returns success if the specified "key" is listed in the file "filename",
479  * SSH_ERR_KEY_NOT_FOUND: if the key is not listed or another error.
480  * If "strict_type" is set then the key type must match exactly,
481  * otherwise a comparison that ignores certficiate data is performed.
482  * If "check_ca" is set and "key" is a certificate, then its CA key is
483  * also checked and sshkey_in_file() will return success if either is found.
484  */
485 int
486 sshkey_in_file(struct sshkey *key, const char *filename, int strict_type,
487     int check_ca)
488 {
489 	FILE *f;
490 	char line[SSH_MAX_PUBKEY_BYTES];
491 	char *cp;
492 	u_long linenum = 0;
493 	int r = 0;
494 	struct sshkey *pub = NULL;
495 	int (*sshkey_compare)(const struct sshkey *, const struct sshkey *) =
496 	    strict_type ?  sshkey_equal : sshkey_equal_public;
497 
498 	if ((f = fopen(filename, "r")) == NULL)
499 		return SSH_ERR_SYSTEM_ERROR;
500 
501 	while (read_keyfile_line(f, filename, line, sizeof(line),
502 	    &linenum) != -1) {
503 		cp = line;
504 
505 		/* Skip leading whitespace. */
506 		for (; *cp && (*cp == ' ' || *cp == '\t'); cp++)
507 			;
508 
509 		/* Skip comments and empty lines */
510 		switch (*cp) {
511 		case '#':
512 		case '\n':
513 		case '\0':
514 			continue;
515 		}
516 
517 		if ((pub = sshkey_new(KEY_UNSPEC)) == NULL) {
518 			r = SSH_ERR_ALLOC_FAIL;
519 			goto out;
520 		}
521 		if ((r = sshkey_read(pub, &cp)) != 0)
522 			goto out;
523 		if (sshkey_compare(key, pub) ||
524 		    (check_ca && sshkey_is_cert(key) &&
525 		    sshkey_compare(key->cert->signature_key, pub))) {
526 			r = 0;
527 			goto out;
528 		}
529 		sshkey_free(pub);
530 		pub = NULL;
531 	}
532 	r = SSH_ERR_KEY_NOT_FOUND;
533  out:
534 	if (pub != NULL)
535 		sshkey_free(pub);
536 	fclose(f);
537 	return r;
538 }
539 
540 /*
541  * Checks whether the specified key is revoked, returning 0 if not,
542  * SSH_ERR_KEY_REVOKED if it is or another error code if something
543  * unexpected happened.
544  * This will check both the key and, if it is a certificate, its CA key too.
545  * "revoked_keys_file" may be a KRL or a one-per-line list of public keys.
546  */
547 int
548 sshkey_check_revoked(struct sshkey *key, const char *revoked_keys_file)
549 {
550 	int r;
551 
552 	r = ssh_krl_file_contains_key(revoked_keys_file, key);
553 	/* If this was not a KRL to begin with then continue below */
554 	if (r != SSH_ERR_KRL_BAD_MAGIC)
555 		return r;
556 
557 	/*
558 	 * If the file is not a KRL or we can't handle KRLs then attempt to
559 	 * parse the file as a flat list of keys.
560 	 */
561 	switch ((r = sshkey_in_file(key, revoked_keys_file, 0, 1))) {
562 	case 0:
563 		/* Key found => revoked */
564 		return SSH_ERR_KEY_REVOKED;
565 	case SSH_ERR_KEY_NOT_FOUND:
566 		/* Key not found => not revoked */
567 		return 0;
568 	default:
569 		/* Some other error occurred */
570 		return r;
571 	}
572 }
573 
574