1 /* $OpenBSD: authfile.c,v 1.107 2014/06/24 01:13:21 djm Exp $ */
3 * Copyright (c) 2000, 2013 Markus Friedl. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
28 #include <sys/types.h>
30 #include <sys/param.h>
51 #include "pathnames.h"
53 #define MAX_KEY_FILE_SIZE (1024 * 1024)
55 /* Save a key blob to a file */
57 sshkey_save_private_blob(struct sshbuf *keybuf, const char *filename)
61 if ((fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0600)) < 0)
62 return SSH_ERR_SYSTEM_ERROR;
63 if (atomicio(vwrite, fd, (u_char *)sshbuf_ptr(keybuf),
64 sshbuf_len(keybuf)) != sshbuf_len(keybuf)) {
69 return SSH_ERR_SYSTEM_ERROR;
76 sshkey_save_private(struct sshkey *key, const char *filename,
77 const char *passphrase, const char *comment,
78 int force_new_format, const char *new_format_cipher, int new_format_rounds)
80 struct sshbuf *keyblob = NULL;
83 if ((keyblob = sshbuf_new()) == NULL)
84 return SSH_ERR_ALLOC_FAIL;
85 if ((r = sshkey_private_to_fileblob(key, keyblob, passphrase, comment,
86 force_new_format, new_format_cipher, new_format_rounds)) != 0)
88 if ((r = sshkey_save_private_blob(keyblob, filename)) != 0)
96 /* Load a key from a fd into a buffer */
98 sshkey_load_file(int fd, const char *filename, struct sshbuf *blob)
105 if (fstat(fd, &st) < 0)
106 return SSH_ERR_SYSTEM_ERROR;
107 if ((st.st_mode & (S_IFSOCK|S_IFCHR|S_IFIFO)) == 0 &&
108 st.st_size > MAX_KEY_FILE_SIZE)
109 return SSH_ERR_INVALID_FORMAT;
111 if ((len = atomicio(read, fd, buf, sizeof(buf))) == 0) {
114 r = SSH_ERR_SYSTEM_ERROR;
117 if ((r = sshbuf_put(blob, buf, len)) != 0)
119 if (sshbuf_len(blob) > MAX_KEY_FILE_SIZE) {
120 r = SSH_ERR_INVALID_FORMAT;
124 if ((st.st_mode & (S_IFSOCK|S_IFCHR|S_IFIFO)) == 0 &&
125 st.st_size != (off_t)sshbuf_len(blob)) {
126 r = SSH_ERR_FILE_CHANGED;
132 explicit_bzero(buf, sizeof(buf));
140 * Loads the public part of the ssh v1 key file. Returns NULL if an error was
141 * encountered (the file does not exist or is not readable), and the key
145 sshkey_load_public_rsa1(int fd, const char *filename,
146 struct sshkey **keyp, char **commentp)
148 struct sshbuf *b = NULL;
152 if (commentp != NULL)
155 if ((b = sshbuf_new()) == NULL)
156 return SSH_ERR_ALLOC_FAIL;
157 if ((r = sshkey_load_file(fd, filename, b)) != 0)
159 if ((r = sshkey_parse_public_rsa1_fileblob(b, keyp, commentp)) != 0)
166 #endif /* WITH_SSH1 */
171 sshkey_load_private_pem(int fd, int type, const char *passphrase,
172 struct sshkey **keyp, char **commentp)
174 struct sshbuf *buffer = NULL;
178 if (commentp != NULL)
181 if ((buffer = sshbuf_new()) == NULL)
182 return SSH_ERR_ALLOC_FAIL;
183 if ((r = sshkey_load_file(fd, NULL, buffer)) != 0)
185 if ((r = sshkey_parse_private_pem_fileblob(buffer, type, passphrase,
186 keyp, commentp)) != 0)
193 #endif /* WITH_OPENSSL */
195 /* XXX remove error() calls from here? */
197 sshkey_perm_ok(int fd, const char *filename)
201 if (fstat(fd, &st) < 0)
202 return SSH_ERR_SYSTEM_ERROR;
204 * if a key owned by the user is accessed, then we check the
205 * permissions of the file. if the key owned by a different user,
206 * then we don't care.
209 if (check_ntsec(filename))
211 if ((st.st_uid == getuid()) && (st.st_mode & 077) != 0) {
212 error("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
213 error("@ WARNING: UNPROTECTED PRIVATE KEY FILE! @");
214 error("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
215 error("Permissions 0%3.3o for '%s' are too open.",
216 (u_int)st.st_mode & 0777, filename);
217 error("It is recommended that your private key files are NOT accessible by others.");
218 error("This private key will be ignored.");
219 return SSH_ERR_KEY_BAD_PERMISSIONS;
224 /* XXX kill perm_ok now that we have SSH_ERR_KEY_BAD_PERMISSIONS? */
226 sshkey_load_private_type(int type, const char *filename, const char *passphrase,
227 struct sshkey **keyp, char **commentp, int *perm_ok)
230 struct sshbuf *buffer = NULL;
233 if (commentp != NULL)
236 if ((fd = open(filename, O_RDONLY)) < 0) {
239 return SSH_ERR_SYSTEM_ERROR;
241 if (sshkey_perm_ok(fd, filename) != 0) {
244 r = SSH_ERR_KEY_BAD_PERMISSIONS;
250 if ((buffer = sshbuf_new()) == NULL) {
251 r = SSH_ERR_ALLOC_FAIL;
254 if ((r = sshkey_load_file(fd, filename, buffer)) != 0)
256 if ((r = sshkey_parse_private_fileblob_type(buffer, type, passphrase,
257 keyp, commentp)) != 0)
267 /* XXX this is almost identical to sshkey_load_private_type() */
269 sshkey_load_private(const char *filename, const char *passphrase,
270 struct sshkey **keyp, char **commentp)
272 struct sshbuf *buffer = NULL;
276 if (commentp != NULL)
279 if ((fd = open(filename, O_RDONLY)) < 0)
280 return SSH_ERR_SYSTEM_ERROR;
281 if (sshkey_perm_ok(fd, filename) != 0) {
282 r = SSH_ERR_KEY_BAD_PERMISSIONS;
286 if ((buffer = sshbuf_new()) == NULL) {
287 r = SSH_ERR_ALLOC_FAIL;
290 if ((r = sshkey_load_file(fd, filename, buffer)) != 0 ||
291 (r = sshkey_parse_private_fileblob(buffer, passphrase, filename,
292 keyp, commentp)) != 0)
303 sshkey_try_load_public(struct sshkey *k, const char *filename, char **commentp)
306 char line[SSH_MAX_PUBKEY_BYTES];
311 if (commentp != NULL)
313 if ((f = fopen(filename, "r")) == NULL)
314 return SSH_ERR_SYSTEM_ERROR;
315 while (read_keyfile_line(f, filename, line, sizeof(line),
324 /* Abort loading if this looks like a private key */
325 if (strncmp(cp, "-----BEGIN", 10) == 0 ||
326 strcmp(cp, "SSH PRIVATE KEY FILE") == 0)
328 /* Skip leading whitespace. */
329 for (; *cp && (*cp == ' ' || *cp == '\t'); cp++)
332 if ((r = sshkey_read(k, &cp)) == 0) {
333 cp[strcspn(cp, "\r\n")] = '\0';
335 *commentp = strdup(*cp ?
337 if (*commentp == NULL)
338 r = SSH_ERR_ALLOC_FAIL;
346 return SSH_ERR_INVALID_FORMAT;
349 /* load public key from ssh v1 private or any pubkey file */
351 sshkey_load_public(const char *filename, struct sshkey **keyp, char **commentp)
353 struct sshkey *pub = NULL;
354 char file[MAXPATHLEN];
359 if (commentp != NULL)
362 if ((fd = open(filename, O_RDONLY)) < 0)
365 /* try rsa1 private key */
366 r = sshkey_load_public_rsa1(fd, filename, keyp, commentp);
369 case SSH_ERR_INTERNAL_ERROR:
370 case SSH_ERR_ALLOC_FAIL:
371 case SSH_ERR_INVALID_ARGUMENT:
372 case SSH_ERR_SYSTEM_ERROR:
376 #endif /* WITH_SSH1 */
378 /* try ssh2 public key */
379 if ((pub = sshkey_new(KEY_UNSPEC)) == NULL)
380 return SSH_ERR_ALLOC_FAIL;
381 if ((r = sshkey_try_load_public(pub, filename, commentp)) == 0) {
389 /* try rsa1 public key */
390 if ((pub = sshkey_new(KEY_RSA1)) == NULL)
391 return SSH_ERR_ALLOC_FAIL;
392 if ((r = sshkey_try_load_public(pub, filename, commentp)) == 0) {
398 #endif /* WITH_SSH1 */
401 /* try .pub suffix */
402 if ((pub = sshkey_new(KEY_UNSPEC)) == NULL)
403 return SSH_ERR_ALLOC_FAIL;
404 r = SSH_ERR_ALLOC_FAIL; /* in case strlcpy or strlcat fail */
405 if ((strlcpy(file, filename, sizeof file) < sizeof(file)) &&
406 (strlcat(file, ".pub", sizeof file) < sizeof(file)) &&
407 (r = sshkey_try_load_public(pub, file, commentp)) == 0) {
417 blacklist_filename(const Key *key)
421 xasprintf(&name, "%s.%s-%u",
422 _PATH_BLACKLIST, key_type(key), key_size(key));
426 /* Scan a blacklist of known-vulnerable keys. */
428 blacklisted_key(Key *key)
430 char *blacklist_file;
432 char *dgst_hex = NULL;
433 char *dgst_packed = NULL, *p;
438 off_t start, lower, upper;
441 blacklist_file = blacklist_filename(key);
442 debug("Checking blacklist file %s", blacklist_file);
443 fd = open(blacklist_file, O_RDONLY);
447 dgst_hex = key_fingerprint(key, SSH_FP_MD5, SSH_FP_HEX);
448 /* Remove all colons */
449 dgst_packed = xcalloc(1, strlen(dgst_hex) + 1);
450 for (i = 0, p = dgst_packed; dgst_hex[i]; i++)
451 if (dgst_hex[i] != ':')
453 /* Only compare least-significant 80 bits (to keep the blacklist
456 line_len = strlen(dgst_packed + 12);
460 /* Skip leading comments */
466 r = atomicio(read, fd, buf, 256);
472 newline = memchr(buf, '\n', 256);
475 start += newline + 1 - buf;
476 if (lseek(fd, start, SEEK_SET) < 0)
480 /* Initialise binary search record numbers */
481 if (fstat(fd, &st) < 0)
484 upper = (st.st_size - start) / (line_len + 1);
486 while (lower != upper) {
491 cur = lower + (upper - lower) / 2;
493 /* Read this line and compare to digest; this is
494 * overflow-safe since cur < max(off_t) / (line_len + 1) */
495 if (lseek(fd, start + cur * (line_len + 1), SEEK_SET) < 0)
497 if (atomicio(read, fd, buf, line_len) != line_len)
499 cmp = memcmp(buf, dgst_packed + 12, line_len);
504 } else if (cmp > 0) {
509 debug("Found %s in blacklist", dgst_hex);
522 xfree(blacklist_file);
526 /* Load the certificate associated with the named private key */
528 sshkey_load_cert(const char *filename, struct sshkey **keyp)
530 struct sshkey *pub = NULL;
532 int r = SSH_ERR_INTERNAL_ERROR;
536 if (asprintf(&file, "%s-cert.pub", filename) == -1)
537 return SSH_ERR_ALLOC_FAIL;
539 if ((pub = sshkey_new(KEY_UNSPEC)) == NULL) {
542 if ((r = sshkey_try_load_public(pub, file, NULL)) != 0)
557 /* Load private key and certificate */
559 sshkey_load_private_cert(int type, const char *filename, const char *passphrase,
560 struct sshkey **keyp, int *perm_ok)
562 struct sshkey *key = NULL, *cert = NULL;
573 #endif /* WITH_OPENSSL */
577 return SSH_ERR_KEY_TYPE_UNKNOWN;
580 if ((r = sshkey_load_private_type(type, filename,
581 passphrase, &key, NULL, perm_ok)) != 0 ||
582 (r = sshkey_load_cert(filename, &cert)) != 0)
585 /* Make sure the private key matches the certificate */
586 if (sshkey_equal_public(key, cert) == 0) {
587 r = SSH_ERR_KEY_CERT_MISMATCH;
591 if ((r = sshkey_to_certified(key, sshkey_cert_is_legacy(cert))) != 0 ||
592 (r = sshkey_cert_copy(cert, key)) != 0)
606 * Returns success if the specified "key" is listed in the file "filename",
607 * SSH_ERR_KEY_NOT_FOUND: if the key is not listed or another error.
608 * If strict_type is set then the key type must match exactly,
609 * otherwise a comparison that ignores certficiate data is performed.
612 sshkey_in_file(struct sshkey *key, const char *filename, int strict_type)
615 char line[SSH_MAX_PUBKEY_BYTES];
619 struct sshkey *pub = NULL;
620 int (*sshkey_compare)(const struct sshkey *, const struct sshkey *) =
621 strict_type ? sshkey_equal : sshkey_equal_public;
623 if ((f = fopen(filename, "r")) == NULL) {
625 return SSH_ERR_KEY_NOT_FOUND;
627 return SSH_ERR_SYSTEM_ERROR;
630 while (read_keyfile_line(f, filename, line, sizeof(line),
634 /* Skip leading whitespace. */
635 for (; *cp && (*cp == ' ' || *cp == '\t'); cp++)
638 /* Skip comments and empty lines */
646 if ((pub = sshkey_new(KEY_UNSPEC)) == NULL) {
647 r = SSH_ERR_ALLOC_FAIL;
650 if ((r = sshkey_read(pub, &cp)) != 0)
652 if (sshkey_compare(key, pub)) {
659 r = SSH_ERR_KEY_NOT_FOUND;