1 /* $OpenBSD: authfile.c,v 1.121 2016/04/09 12:39:30 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>
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, 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, struct sshkey **keyp, char **commentp)
147 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, b)) != 0)
159 if ((r = sshkey_parse_public_rsa1_fileblob(b, keyp, commentp)) != 0)
166 #endif /* WITH_SSH1 */
168 /* XXX remove error() calls from here? */
170 sshkey_perm_ok(int fd, const char *filename)
174 if (fstat(fd, &st) < 0)
175 return SSH_ERR_SYSTEM_ERROR;
177 * if a key owned by the user is accessed, then we check the
178 * permissions of the file. if the key owned by a different user,
179 * then we don't care.
182 if (check_ntsec(filename))
184 if ((st.st_uid == getuid()) && (st.st_mode & 077) != 0) {
185 error("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
186 error("@ WARNING: UNPROTECTED PRIVATE KEY FILE! @");
187 error("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
188 error("Permissions 0%3.3o for '%s' are too open.",
189 (u_int)st.st_mode & 0777, filename);
190 error("It is required that your private key files are NOT accessible by others.");
191 error("This private key will be ignored.");
192 return SSH_ERR_KEY_BAD_PERMISSIONS;
197 /* XXX kill perm_ok now that we have SSH_ERR_KEY_BAD_PERMISSIONS? */
199 sshkey_load_private_type(int type, const char *filename, const char *passphrase,
200 struct sshkey **keyp, char **commentp, int *perm_ok)
206 if (commentp != NULL)
209 if ((fd = open(filename, O_RDONLY)) < 0) {
212 return SSH_ERR_SYSTEM_ERROR;
214 if (sshkey_perm_ok(fd, filename) != 0) {
217 r = SSH_ERR_KEY_BAD_PERMISSIONS;
223 r = sshkey_load_private_type_fd(fd, type, passphrase, keyp, commentp);
230 sshkey_load_private_type_fd(int fd, int type, const char *passphrase,
231 struct sshkey **keyp, char **commentp)
233 struct sshbuf *buffer = NULL;
238 if ((buffer = sshbuf_new()) == NULL) {
239 r = SSH_ERR_ALLOC_FAIL;
242 if ((r = sshkey_load_file(fd, buffer)) != 0 ||
243 (r = sshkey_parse_private_fileblob_type(buffer, type,
244 passphrase, keyp, commentp)) != 0)
254 /* XXX this is almost identical to sshkey_load_private_type() */
256 sshkey_load_private(const char *filename, const char *passphrase,
257 struct sshkey **keyp, char **commentp)
259 struct sshbuf *buffer = NULL;
264 if (commentp != NULL)
267 if ((fd = open(filename, O_RDONLY)) < 0)
268 return SSH_ERR_SYSTEM_ERROR;
269 if (sshkey_perm_ok(fd, filename) != 0) {
270 r = SSH_ERR_KEY_BAD_PERMISSIONS;
274 if ((buffer = sshbuf_new()) == NULL) {
275 r = SSH_ERR_ALLOC_FAIL;
278 if ((r = sshkey_load_file(fd, buffer)) != 0 ||
279 (r = sshkey_parse_private_fileblob(buffer, passphrase, keyp,
290 sshkey_try_load_public(struct sshkey *k, const char *filename, char **commentp)
293 char line[SSH_MAX_PUBKEY_BYTES];
298 if (commentp != NULL)
300 if ((f = fopen(filename, "r")) == NULL)
301 return SSH_ERR_SYSTEM_ERROR;
302 while (read_keyfile_line(f, filename, line, sizeof(line),
311 /* Abort loading if this looks like a private key */
312 if (strncmp(cp, "-----BEGIN", 10) == 0 ||
313 strcmp(cp, "SSH PRIVATE KEY FILE") == 0)
315 /* Skip leading whitespace. */
316 for (; *cp && (*cp == ' ' || *cp == '\t'); cp++)
319 if ((r = sshkey_read(k, &cp)) == 0) {
320 cp[strcspn(cp, "\r\n")] = '\0';
322 *commentp = strdup(*cp ?
324 if (*commentp == NULL)
325 r = SSH_ERR_ALLOC_FAIL;
333 return SSH_ERR_INVALID_FORMAT;
336 /* load public key from ssh v1 private or any pubkey file */
338 sshkey_load_public(const char *filename, struct sshkey **keyp, char **commentp)
340 struct sshkey *pub = NULL;
346 if (commentp != NULL)
349 /* XXX should load file once and attempt to parse each format */
351 if ((fd = open(filename, O_RDONLY)) < 0)
354 /* try rsa1 private key */
355 r = sshkey_load_public_rsa1(fd, keyp, commentp);
358 case SSH_ERR_INTERNAL_ERROR:
359 case SSH_ERR_ALLOC_FAIL:
360 case SSH_ERR_INVALID_ARGUMENT:
361 case SSH_ERR_SYSTEM_ERROR:
365 #else /* WITH_SSH1 */
367 #endif /* WITH_SSH1 */
369 /* try ssh2 public key */
370 if ((pub = sshkey_new(KEY_UNSPEC)) == NULL)
371 return SSH_ERR_ALLOC_FAIL;
372 if ((r = sshkey_try_load_public(pub, filename, commentp)) == 0) {
380 /* try rsa1 public key */
381 if ((pub = sshkey_new(KEY_RSA1)) == NULL)
382 return SSH_ERR_ALLOC_FAIL;
383 if ((r = sshkey_try_load_public(pub, filename, commentp)) == 0) {
389 #endif /* WITH_SSH1 */
392 /* try .pub suffix */
393 if ((pub = sshkey_new(KEY_UNSPEC)) == NULL)
394 return SSH_ERR_ALLOC_FAIL;
395 r = SSH_ERR_ALLOC_FAIL; /* in case strlcpy or strlcat fail */
396 if ((strlcpy(file, filename, sizeof file) < sizeof(file)) &&
397 (strlcat(file, ".pub", sizeof file) < sizeof(file)) &&
398 (r = sshkey_try_load_public(pub, file, commentp)) == 0) {
408 /* Load the certificate associated with the named private key */
410 sshkey_load_cert(const char *filename, struct sshkey **keyp)
412 struct sshkey *pub = NULL;
414 int r = SSH_ERR_INTERNAL_ERROR;
419 if (asprintf(&file, "%s-cert.pub", filename) == -1)
420 return SSH_ERR_ALLOC_FAIL;
422 if ((pub = sshkey_new(KEY_UNSPEC)) == NULL) {
425 if ((r = sshkey_try_load_public(pub, file, NULL)) != 0)
439 /* Load private key and certificate */
441 sshkey_load_private_cert(int type, const char *filename, const char *passphrase,
442 struct sshkey **keyp, int *perm_ok)
444 struct sshkey *key = NULL, *cert = NULL;
455 #endif /* WITH_OPENSSL */
460 return SSH_ERR_KEY_TYPE_UNKNOWN;
463 if ((r = sshkey_load_private_type(type, filename,
464 passphrase, &key, NULL, perm_ok)) != 0 ||
465 (r = sshkey_load_cert(filename, &cert)) != 0)
468 /* Make sure the private key matches the certificate */
469 if (sshkey_equal_public(key, cert) == 0) {
470 r = SSH_ERR_KEY_CERT_MISMATCH;
474 if ((r = sshkey_to_certified(key)) != 0 ||
475 (r = sshkey_cert_copy(cert, key)) != 0)
489 * Returns success if the specified "key" is listed in the file "filename",
490 * SSH_ERR_KEY_NOT_FOUND: if the key is not listed or another error.
491 * If "strict_type" is set then the key type must match exactly,
492 * otherwise a comparison that ignores certficiate data is performed.
493 * If "check_ca" is set and "key" is a certificate, then its CA key is
494 * also checked and sshkey_in_file() will return success if either is found.
497 sshkey_in_file(struct sshkey *key, const char *filename, int strict_type,
501 char line[SSH_MAX_PUBKEY_BYTES];
505 struct sshkey *pub = NULL;
506 int (*sshkey_compare)(const struct sshkey *, const struct sshkey *) =
507 strict_type ? sshkey_equal : sshkey_equal_public;
509 if ((f = fopen(filename, "r")) == NULL)
510 return SSH_ERR_SYSTEM_ERROR;
512 while (read_keyfile_line(f, filename, line, sizeof(line),
516 /* Skip leading whitespace. */
517 for (; *cp && (*cp == ' ' || *cp == '\t'); cp++)
520 /* Skip comments and empty lines */
528 if ((pub = sshkey_new(KEY_UNSPEC)) == NULL) {
529 r = SSH_ERR_ALLOC_FAIL;
532 if ((r = sshkey_read(pub, &cp)) != 0)
534 if (sshkey_compare(key, pub) ||
535 (check_ca && sshkey_is_cert(key) &&
536 sshkey_compare(key->cert->signature_key, pub))) {
543 r = SSH_ERR_KEY_NOT_FOUND;
551 * Checks whether the specified key is revoked, returning 0 if not,
552 * SSH_ERR_KEY_REVOKED if it is or another error code if something
553 * unexpected happened.
554 * This will check both the key and, if it is a certificate, its CA key too.
555 * "revoked_keys_file" may be a KRL or a one-per-line list of public keys.
558 sshkey_check_revoked(struct sshkey *key, const char *revoked_keys_file)
562 r = ssh_krl_file_contains_key(revoked_keys_file, key);
563 /* If this was not a KRL to begin with then continue below */
564 if (r != SSH_ERR_KRL_BAD_MAGIC)
568 * If the file is not a KRL or we can't handle KRLs then attempt to
569 * parse the file as a flat list of keys.
571 switch ((r = sshkey_in_file(key, revoked_keys_file, 0, 1))) {
573 /* Key found => revoked */
574 return SSH_ERR_KEY_REVOKED;
575 case SSH_ERR_KEY_NOT_FOUND:
576 /* Key not found => not revoked */
579 /* Some other error occurred */