xref: /openbsd-src/usr.bin/ssh/authfile.c (revision 99fd087599a8791921855f21bd7e36130f39aadc)
1 /* $OpenBSD: authfile.c,v 1.137 2020/01/25 23:02:13 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 "ssh.h"
41 #include "log.h"
42 #include "authfile.h"
43 #include "misc.h"
44 #include "atomicio.h"
45 #include "sshkey.h"
46 #include "sshbuf.h"
47 #include "ssherr.h"
48 #include "krl.h"
49 
50 #define MAX_KEY_FILE_SIZE	(1024 * 1024)
51 
52 /* Save a key blob to a file */
53 static int
54 sshkey_save_private_blob(struct sshbuf *keybuf, const char *filename)
55 {
56 	int r;
57 	mode_t omask;
58 
59 	omask = umask(077);
60 	r = sshbuf_write_file(filename, keybuf);
61 	umask(omask);
62 	return r;
63 }
64 
65 int
66 sshkey_save_private(struct sshkey *key, const char *filename,
67     const char *passphrase, const char *comment,
68     int format, const char *openssh_format_cipher, int openssh_format_rounds)
69 {
70 	struct sshbuf *keyblob = NULL;
71 	int r;
72 
73 	if ((keyblob = sshbuf_new()) == NULL)
74 		return SSH_ERR_ALLOC_FAIL;
75 	if ((r = sshkey_private_to_fileblob(key, keyblob, passphrase, comment,
76 	    format, openssh_format_cipher, openssh_format_rounds)) != 0)
77 		goto out;
78 	if ((r = sshkey_save_private_blob(keyblob, filename)) != 0)
79 		goto out;
80 	r = 0;
81  out:
82 	sshbuf_free(keyblob);
83 	return r;
84 }
85 
86 /* XXX remove error() calls from here? */
87 int
88 sshkey_perm_ok(int fd, const char *filename)
89 {
90 	struct stat st;
91 
92 	if (fstat(fd, &st) == -1)
93 		return SSH_ERR_SYSTEM_ERROR;
94 	/*
95 	 * if a key owned by the user is accessed, then we check the
96 	 * permissions of the file. if the key owned by a different user,
97 	 * then we don't care.
98 	 */
99 	if ((st.st_uid == getuid()) && (st.st_mode & 077) != 0) {
100 		error("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
101 		error("@         WARNING: UNPROTECTED PRIVATE KEY FILE!          @");
102 		error("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
103 		error("Permissions 0%3.3o for '%s' are too open.",
104 		    (u_int)st.st_mode & 0777, filename);
105 		error("It is required that your private key files are NOT accessible by others.");
106 		error("This private key will be ignored.");
107 		return SSH_ERR_KEY_BAD_PERMISSIONS;
108 	}
109 	return 0;
110 }
111 
112 int
113 sshkey_load_private_type(int type, const char *filename, const char *passphrase,
114     struct sshkey **keyp, char **commentp)
115 {
116 	int fd, r;
117 
118 	if (keyp != NULL)
119 		*keyp = NULL;
120 	if (commentp != NULL)
121 		*commentp = NULL;
122 
123 	if ((fd = open(filename, O_RDONLY)) == -1)
124 		return SSH_ERR_SYSTEM_ERROR;
125 
126 	r = sshkey_perm_ok(fd, filename);
127 	if (r != 0)
128 		goto out;
129 
130 	r = sshkey_load_private_type_fd(fd, type, passphrase, keyp, commentp);
131 	if (r == 0 && keyp && *keyp)
132 		r = sshkey_set_filename(*keyp, filename);
133  out:
134 	close(fd);
135 	return r;
136 }
137 
138 int
139 sshkey_load_private_type_fd(int fd, int type, const char *passphrase,
140     struct sshkey **keyp, char **commentp)
141 {
142 	struct sshbuf *buffer = NULL;
143 	int r;
144 
145 	if (keyp != NULL)
146 		*keyp = NULL;
147 	if ((r = sshbuf_load_fd(fd, &buffer)) != 0 ||
148 	    (r = sshkey_parse_private_fileblob_type(buffer, type,
149 	    passphrase, keyp, commentp)) != 0)
150 		goto out;
151 
152 	/* success */
153 	r = 0;
154  out:
155 	sshbuf_free(buffer);
156 	return r;
157 }
158 
159 /* XXX this is almost identical to sshkey_load_private_type() */
160 int
161 sshkey_load_private(const char *filename, const char *passphrase,
162     struct sshkey **keyp, char **commentp)
163 {
164 	struct sshbuf *buffer = NULL;
165 	int r, fd;
166 
167 	if (keyp != NULL)
168 		*keyp = NULL;
169 	if (commentp != NULL)
170 		*commentp = NULL;
171 
172 	if ((fd = open(filename, O_RDONLY)) == -1)
173 		return SSH_ERR_SYSTEM_ERROR;
174 	if (sshkey_perm_ok(fd, filename) != 0) {
175 		r = SSH_ERR_KEY_BAD_PERMISSIONS;
176 		goto out;
177 	}
178 	if ((r = sshbuf_load_fd(fd, &buffer)) != 0 ||
179 	    (r = sshkey_parse_private_fileblob(buffer, passphrase, keyp,
180 	    commentp)) != 0)
181 		goto out;
182 	if (keyp && *keyp &&
183 	    (r = sshkey_set_filename(*keyp, filename)) != 0)
184 		goto out;
185 	r = 0;
186  out:
187 	close(fd);
188 	sshbuf_free(buffer);
189 	return r;
190 }
191 
192 static int
193 sshkey_try_load_public(struct sshkey *k, const char *filename, char **commentp)
194 {
195 	FILE *f;
196 	char *line = NULL, *cp;
197 	size_t linesize = 0;
198 	int r;
199 
200 	if (commentp != NULL)
201 		*commentp = NULL;
202 	if ((f = fopen(filename, "r")) == NULL)
203 		return SSH_ERR_SYSTEM_ERROR;
204 	while (getline(&line, &linesize, f) != -1) {
205 		cp = line;
206 		switch (*cp) {
207 		case '#':
208 		case '\n':
209 		case '\0':
210 			continue;
211 		}
212 		/* Abort loading if this looks like a private key */
213 		if (strncmp(cp, "-----BEGIN", 10) == 0 ||
214 		    strcmp(cp, "SSH PRIVATE KEY FILE") == 0)
215 			break;
216 		/* Skip leading whitespace. */
217 		for (; *cp && (*cp == ' ' || *cp == '\t'); cp++)
218 			;
219 		if (*cp) {
220 			if ((r = sshkey_read(k, &cp)) == 0) {
221 				cp[strcspn(cp, "\r\n")] = '\0';
222 				if (commentp) {
223 					*commentp = strdup(*cp ?
224 					    cp : filename);
225 					if (*commentp == NULL)
226 						r = SSH_ERR_ALLOC_FAIL;
227 				}
228 				free(line);
229 				fclose(f);
230 				return r;
231 			}
232 		}
233 	}
234 	free(line);
235 	fclose(f);
236 	return SSH_ERR_INVALID_FORMAT;
237 }
238 
239 /* load public key from any pubkey file */
240 int
241 sshkey_load_public(const char *filename, struct sshkey **keyp, char **commentp)
242 {
243 	struct sshkey *pub = NULL;
244 	char *file = NULL;
245 	int r;
246 
247 	if (keyp != NULL)
248 		*keyp = NULL;
249 	if (commentp != NULL)
250 		*commentp = NULL;
251 
252 	if ((pub = sshkey_new(KEY_UNSPEC)) == NULL)
253 		return SSH_ERR_ALLOC_FAIL;
254 	if ((r = sshkey_try_load_public(pub, filename, commentp)) == 0) {
255 		if (keyp != NULL) {
256 			*keyp = pub;
257 			pub = NULL;
258 		}
259 		r = 0;
260 		goto out;
261 	}
262 	sshkey_free(pub);
263 
264 	/* try .pub suffix */
265 	if (asprintf(&file, "%s.pub", filename) == -1)
266 		return SSH_ERR_ALLOC_FAIL;
267 	if ((pub = sshkey_new(KEY_UNSPEC)) == NULL) {
268 		r = SSH_ERR_ALLOC_FAIL;
269 		goto out;
270 	}
271 	if ((r = sshkey_try_load_public(pub, file, commentp)) == 0) {
272 		if (keyp != NULL) {
273 			*keyp = pub;
274 			pub = NULL;
275 		}
276 		r = 0;
277 	}
278  out:
279 	free(file);
280 	sshkey_free(pub);
281 	return r;
282 }
283 
284 /* Load the certificate associated with the named private key */
285 int
286 sshkey_load_cert(const char *filename, struct sshkey **keyp)
287 {
288 	struct sshkey *pub = NULL;
289 	char *file = NULL;
290 	int r = SSH_ERR_INTERNAL_ERROR;
291 
292 	if (keyp != NULL)
293 		*keyp = NULL;
294 
295 	if (asprintf(&file, "%s-cert.pub", filename) == -1)
296 		return SSH_ERR_ALLOC_FAIL;
297 
298 	if ((pub = sshkey_new(KEY_UNSPEC)) == NULL) {
299 		goto out;
300 	}
301 	if ((r = sshkey_try_load_public(pub, file, NULL)) != 0)
302 		goto out;
303 	/* success */
304 	if (keyp != NULL) {
305 		*keyp = pub;
306 		pub = NULL;
307 	}
308 	r = 0;
309  out:
310 	free(file);
311 	sshkey_free(pub);
312 	return r;
313 }
314 
315 /* Load private key and certificate */
316 int
317 sshkey_load_private_cert(int type, const char *filename, const char *passphrase,
318     struct sshkey **keyp)
319 {
320 	struct sshkey *key = NULL, *cert = NULL;
321 	int r;
322 
323 	if (keyp != NULL)
324 		*keyp = NULL;
325 
326 	switch (type) {
327 #ifdef WITH_OPENSSL
328 	case KEY_RSA:
329 	case KEY_DSA:
330 	case KEY_ECDSA:
331 #endif /* WITH_OPENSSL */
332 	case KEY_ED25519:
333 	case KEY_XMSS:
334 	case KEY_UNSPEC:
335 		break;
336 	default:
337 		return SSH_ERR_KEY_TYPE_UNKNOWN;
338 	}
339 
340 	if ((r = sshkey_load_private_type(type, filename,
341 	    passphrase, &key, NULL)) != 0 ||
342 	    (r = sshkey_load_cert(filename, &cert)) != 0)
343 		goto out;
344 
345 	/* Make sure the private key matches the certificate */
346 	if (sshkey_equal_public(key, cert) == 0) {
347 		r = SSH_ERR_KEY_CERT_MISMATCH;
348 		goto out;
349 	}
350 
351 	if ((r = sshkey_to_certified(key)) != 0 ||
352 	    (r = sshkey_cert_copy(cert, key)) != 0)
353 		goto out;
354 	r = 0;
355 	if (keyp != NULL) {
356 		*keyp = key;
357 		key = NULL;
358 	}
359  out:
360 	sshkey_free(key);
361 	sshkey_free(cert);
362 	return r;
363 }
364 
365 /*
366  * Returns success if the specified "key" is listed in the file "filename",
367  * SSH_ERR_KEY_NOT_FOUND: if the key is not listed or another error.
368  * If "strict_type" is set then the key type must match exactly,
369  * otherwise a comparison that ignores certficiate data is performed.
370  * If "check_ca" is set and "key" is a certificate, then its CA key is
371  * also checked and sshkey_in_file() will return success if either is found.
372  */
373 int
374 sshkey_in_file(struct sshkey *key, const char *filename, int strict_type,
375     int check_ca)
376 {
377 	FILE *f;
378 	char *line = NULL, *cp;
379 	size_t linesize = 0;
380 	int r = 0;
381 	struct sshkey *pub = NULL;
382 
383 	int (*sshkey_compare)(const struct sshkey *, const struct sshkey *) =
384 	    strict_type ?  sshkey_equal : sshkey_equal_public;
385 
386 	if ((f = fopen(filename, "r")) == NULL)
387 		return SSH_ERR_SYSTEM_ERROR;
388 
389 	while (getline(&line, &linesize, f) != -1) {
390 		sshkey_free(pub);
391 		pub = NULL;
392 		cp = line;
393 
394 		/* Skip leading whitespace. */
395 		for (; *cp && (*cp == ' ' || *cp == '\t'); cp++)
396 			;
397 
398 		/* Skip comments and empty lines */
399 		switch (*cp) {
400 		case '#':
401 		case '\n':
402 		case '\0':
403 			continue;
404 		}
405 
406 		if ((pub = sshkey_new(KEY_UNSPEC)) == NULL) {
407 			r = SSH_ERR_ALLOC_FAIL;
408 			goto out;
409 		}
410 		switch (r = sshkey_read(pub, &cp)) {
411 		case 0:
412 			break;
413 		case SSH_ERR_KEY_LENGTH:
414 			continue;
415 		default:
416 			goto out;
417 		}
418 		if (sshkey_compare(key, pub) ||
419 		    (check_ca && sshkey_is_cert(key) &&
420 		    sshkey_compare(key->cert->signature_key, pub))) {
421 			r = 0;
422 			goto out;
423 		}
424 	}
425 	r = SSH_ERR_KEY_NOT_FOUND;
426  out:
427 	free(line);
428 	sshkey_free(pub);
429 	fclose(f);
430 	return r;
431 }
432 
433 /*
434  * Checks whether the specified key is revoked, returning 0 if not,
435  * SSH_ERR_KEY_REVOKED if it is or another error code if something
436  * unexpected happened.
437  * This will check both the key and, if it is a certificate, its CA key too.
438  * "revoked_keys_file" may be a KRL or a one-per-line list of public keys.
439  */
440 int
441 sshkey_check_revoked(struct sshkey *key, const char *revoked_keys_file)
442 {
443 	int r;
444 
445 	r = ssh_krl_file_contains_key(revoked_keys_file, key);
446 	/* If this was not a KRL to begin with then continue below */
447 	if (r != SSH_ERR_KRL_BAD_MAGIC)
448 		return r;
449 
450 	/*
451 	 * If the file is not a KRL or we can't handle KRLs then attempt to
452 	 * parse the file as a flat list of keys.
453 	 */
454 	switch ((r = sshkey_in_file(key, revoked_keys_file, 0, 1))) {
455 	case 0:
456 		/* Key found => revoked */
457 		return SSH_ERR_KEY_REVOKED;
458 	case SSH_ERR_KEY_NOT_FOUND:
459 		/* Key not found => not revoked */
460 		return 0;
461 	default:
462 		/* Some other error occurred */
463 		return r;
464 	}
465 }
466 
467 /*
468  * Advanced *cpp past the end of key options, defined as the first unquoted
469  * whitespace character. Returns 0 on success or -1 on failure (e.g.
470  * unterminated quotes).
471  */
472 int
473 sshkey_advance_past_options(char **cpp)
474 {
475 	char *cp = *cpp;
476 	int quoted = 0;
477 
478 	for (; *cp && (quoted || (*cp != ' ' && *cp != '\t')); cp++) {
479 		if (*cp == '\\' && cp[1] == '"')
480 			cp++;	/* Skip both */
481 		else if (*cp == '"')
482 			quoted = !quoted;
483 	}
484 	*cpp = cp;
485 	/* return failure for unterminated quotes */
486 	return (*cp == '\0' && quoted) ? -1 : 0;
487 }
488 
489 /* Save a public key */
490 int
491 sshkey_save_public(const struct sshkey *key, const char *path,
492     const char *comment)
493 {
494 	int fd, oerrno;
495 	FILE *f = NULL;
496 	int r = SSH_ERR_INTERNAL_ERROR;
497 
498 	if ((fd = open(path, O_WRONLY|O_CREAT|O_TRUNC, 0644)) == -1)
499 		return SSH_ERR_SYSTEM_ERROR;
500 	if ((f = fdopen(fd, "w")) == NULL) {
501 		r = SSH_ERR_SYSTEM_ERROR;
502 		goto fail;
503 	}
504 	if ((r = sshkey_write(key, f)) != 0)
505 		goto fail;
506 	fprintf(f, " %s\n", comment);
507 	if (ferror(f) || fclose(f) != 0) {
508 		r = SSH_ERR_SYSTEM_ERROR;
509  fail:
510 		oerrno = errno;
511 		if (f != NULL)
512 			fclose(f);
513 		else
514 			close(fd);
515 		errno = oerrno;
516 		return r;
517 	}
518 	return 0;
519 }
520