Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 1 | /* $OpenBSD: authfile.c,v 1.107 2014/06/24 01:13:21 djm Exp $ */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2 | /* |
Damien Miller | bcd00ab | 2013-12-07 10:41:55 +1100 | [diff] [blame] | 3 | * Copyright (c) 2000, 2013 Markus Friedl. All rights reserved. |
Damien Miller | e4340be | 2000-09-16 13:29:08 +1100 | [diff] [blame] | 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. |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 24 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 25 | |
| 26 | #include "includes.h" |
Damien Miller | f17883e | 2006-03-15 11:45:54 +1100 | [diff] [blame] | 27 | |
| 28 | #include <sys/types.h> |
| 29 | #include <sys/stat.h> |
Damien Miller | 8dbffe7 | 2006-08-05 11:02:17 +1000 | [diff] [blame] | 30 | #include <sys/param.h> |
Damien Miller | d783435 | 2006-08-05 12:39:39 +1000 | [diff] [blame] | 31 | #include <sys/uio.h> |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 32 | |
Darren Tucker | ba72405 | 2006-07-12 22:24:22 +1000 | [diff] [blame] | 33 | #include <errno.h> |
Damien Miller | 57cf638 | 2006-07-10 21:13:46 +1000 | [diff] [blame] | 34 | #include <fcntl.h> |
Damien Miller | a7a73ee | 2006-08-05 11:37:59 +1000 | [diff] [blame] | 35 | #include <stdio.h> |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 36 | #include <stdarg.h> |
Damien Miller | e7a1e5c | 2006-08-05 11:34:19 +1000 | [diff] [blame] | 37 | #include <stdlib.h> |
Damien Miller | e3476ed | 2006-07-24 14:13:33 +1000 | [diff] [blame] | 38 | #include <string.h> |
Damien Miller | e6b3b61 | 2006-07-24 14:01:23 +1000 | [diff] [blame] | 39 | #include <unistd.h> |
Damien Miller | 57cf638 | 2006-07-10 21:13:46 +1000 | [diff] [blame] | 40 | |
Damien Miller | d783435 | 2006-08-05 12:39:39 +1000 | [diff] [blame] | 41 | #include "cipher.h" |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 42 | #include "key.h" |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 43 | #include "ssh.h" |
| 44 | #include "log.h" |
Ben Lindstrom | 31ca54a | 2001-02-09 02:11:24 +0000 | [diff] [blame] | 45 | #include "authfile.h" |
Damien Miller | 040b64f | 2002-01-22 23:10:04 +1100 | [diff] [blame] | 46 | #include "rsa.h" |
Darren Tucker | f0f9098 | 2004-12-11 13:39:50 +1100 | [diff] [blame] | 47 | #include "misc.h" |
Damien Miller | eccb9de | 2005-06-17 12:59:34 +1000 | [diff] [blame] | 48 | #include "atomicio.h" |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 49 | #include "sshbuf.h" |
| 50 | #include "ssherr.h" |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 51 | |
Damien Miller | 2ce12ef | 2011-05-05 14:17:18 +1000 | [diff] [blame] | 52 | #define MAX_KEY_FILE_SIZE (1024 * 1024) |
| 53 | |
Damien Miller | a232792 | 2010-12-01 12:01:21 +1100 | [diff] [blame] | 54 | /* Save a key blob to a file */ |
| 55 | static int |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 56 | sshkey_save_private_blob(struct sshbuf *keybuf, const char *filename) |
Damien Miller | a232792 | 2010-12-01 12:01:21 +1100 | [diff] [blame] | 57 | { |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 58 | int fd, oerrno; |
Damien Miller | a232792 | 2010-12-01 12:01:21 +1100 | [diff] [blame] | 59 | |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 60 | if ((fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0600)) < 0) |
| 61 | return SSH_ERR_SYSTEM_ERROR; |
| 62 | if (atomicio(vwrite, fd, (u_char *)sshbuf_ptr(keybuf), |
| 63 | sshbuf_len(keybuf)) != sshbuf_len(keybuf)) { |
| 64 | oerrno = errno; |
Damien Miller | a232792 | 2010-12-01 12:01:21 +1100 | [diff] [blame] | 65 | close(fd); |
| 66 | unlink(filename); |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 67 | errno = oerrno; |
| 68 | return SSH_ERR_SYSTEM_ERROR; |
Damien Miller | a232792 | 2010-12-01 12:01:21 +1100 | [diff] [blame] | 69 | } |
| 70 | close(fd); |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 71 | return 0; |
Damien Miller | a232792 | 2010-12-01 12:01:21 +1100 | [diff] [blame] | 72 | } |
| 73 | |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 74 | int |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 75 | sshkey_save_private(struct sshkey *key, const char *filename, |
| 76 | const char *passphrase, const char *comment, |
| 77 | int force_new_format, const char *new_format_cipher, int new_format_rounds) |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 78 | { |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 79 | struct sshbuf *keyblob = NULL; |
| 80 | int r; |
Damien Miller | a232792 | 2010-12-01 12:01:21 +1100 | [diff] [blame] | 81 | |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 82 | if ((keyblob = sshbuf_new()) == NULL) |
| 83 | return SSH_ERR_ALLOC_FAIL; |
| 84 | if ((r = sshkey_private_to_fileblob(key, keyblob, passphrase, comment, |
| 85 | force_new_format, new_format_cipher, new_format_rounds)) != 0) |
Damien Miller | a232792 | 2010-12-01 12:01:21 +1100 | [diff] [blame] | 86 | goto out; |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 87 | if ((r = sshkey_save_private_blob(keyblob, filename)) != 0) |
Damien Miller | a232792 | 2010-12-01 12:01:21 +1100 | [diff] [blame] | 88 | goto out; |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 89 | r = 0; |
Damien Miller | a232792 | 2010-12-01 12:01:21 +1100 | [diff] [blame] | 90 | out: |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 91 | sshbuf_free(keyblob); |
| 92 | return r; |
Damien Miller | a232792 | 2010-12-01 12:01:21 +1100 | [diff] [blame] | 93 | } |
| 94 | |
Damien Miller | 2ce12ef | 2011-05-05 14:17:18 +1000 | [diff] [blame] | 95 | /* Load a key from a fd into a buffer */ |
| 96 | int |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 97 | sshkey_load_file(int fd, const char *filename, struct sshbuf *blob) |
Damien Miller | a232792 | 2010-12-01 12:01:21 +1100 | [diff] [blame] | 98 | { |
Damien Miller | 2ce12ef | 2011-05-05 14:17:18 +1000 | [diff] [blame] | 99 | u_char buf[1024]; |
Damien Miller | a232792 | 2010-12-01 12:01:21 +1100 | [diff] [blame] | 100 | size_t len; |
Damien Miller | a232792 | 2010-12-01 12:01:21 +1100 | [diff] [blame] | 101 | struct stat st; |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 102 | int r; |
Damien Miller | a232792 | 2010-12-01 12:01:21 +1100 | [diff] [blame] | 103 | |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 104 | if (fstat(fd, &st) < 0) |
| 105 | return SSH_ERR_SYSTEM_ERROR; |
Damien Miller | 2ce12ef | 2011-05-05 14:17:18 +1000 | [diff] [blame] | 106 | if ((st.st_mode & (S_IFSOCK|S_IFCHR|S_IFIFO)) == 0 && |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 107 | st.st_size > MAX_KEY_FILE_SIZE) |
| 108 | return SSH_ERR_INVALID_FORMAT; |
Damien Miller | 2ce12ef | 2011-05-05 14:17:18 +1000 | [diff] [blame] | 109 | for (;;) { |
| 110 | if ((len = atomicio(read, fd, buf, sizeof(buf))) == 0) { |
| 111 | if (errno == EPIPE) |
| 112 | break; |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 113 | r = SSH_ERR_SYSTEM_ERROR; |
| 114 | goto out; |
Damien Miller | 2ce12ef | 2011-05-05 14:17:18 +1000 | [diff] [blame] | 115 | } |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 116 | if ((r = sshbuf_put(blob, buf, len)) != 0) |
| 117 | goto out; |
| 118 | if (sshbuf_len(blob) > MAX_KEY_FILE_SIZE) { |
| 119 | r = SSH_ERR_INVALID_FORMAT; |
| 120 | goto out; |
Damien Miller | 2ce12ef | 2011-05-05 14:17:18 +1000 | [diff] [blame] | 121 | } |
| 122 | } |
Damien Miller | 2ce12ef | 2011-05-05 14:17:18 +1000 | [diff] [blame] | 123 | if ((st.st_mode & (S_IFSOCK|S_IFCHR|S_IFIFO)) == 0 && |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 124 | st.st_size != (off_t)sshbuf_len(blob)) { |
| 125 | r = SSH_ERR_FILE_CHANGED; |
| 126 | goto out; |
Damien Miller | a232792 | 2010-12-01 12:01:21 +1100 | [diff] [blame] | 127 | } |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 128 | r = 0; |
Damien Miller | 2ce12ef | 2011-05-05 14:17:18 +1000 | [diff] [blame] | 129 | |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 130 | out: |
| 131 | explicit_bzero(buf, sizeof(buf)); |
| 132 | if (r != 0) |
| 133 | sshbuf_reset(blob); |
| 134 | return r; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 135 | } |
| 136 | |
Damien Miller | 1f0311c | 2014-05-15 14:24:09 +1000 | [diff] [blame] | 137 | #ifdef WITH_SSH1 |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 138 | /* |
Ben Lindstrom | d0fca42 | 2001-03-26 13:44:06 +0000 | [diff] [blame] | 139 | * Loads the public part of the ssh v1 key file. Returns NULL if an error was |
| 140 | * encountered (the file does not exist or is not readable), and the key |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 141 | * otherwise. |
| 142 | */ |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 143 | static int |
| 144 | sshkey_load_public_rsa1(int fd, const char *filename, |
| 145 | struct sshkey **keyp, char **commentp) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 146 | { |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 147 | struct sshbuf *b = NULL; |
| 148 | int r; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 149 | |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 150 | *keyp = NULL; |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 151 | if (commentp != NULL) |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 152 | *commentp = NULL; |
| 153 | |
| 154 | if ((b = sshbuf_new()) == NULL) |
| 155 | return SSH_ERR_ALLOC_FAIL; |
| 156 | if ((r = sshkey_load_file(fd, filename, b)) != 0) |
| 157 | goto out; |
| 158 | if ((r = sshkey_parse_public_rsa1_fileblob(b, keyp, commentp)) != 0) |
| 159 | goto out; |
| 160 | r = 0; |
| 161 | out: |
| 162 | sshbuf_free(b); |
| 163 | return r; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 164 | } |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 165 | #endif /* WITH_SSH1 */ |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 166 | |
Damien Miller | 1f0311c | 2014-05-15 14:24:09 +1000 | [diff] [blame] | 167 | #ifdef WITH_OPENSSL |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 168 | /* XXX Deprecate? */ |
Damien Miller | 8275fad | 2006-03-15 12:06:23 +1100 | [diff] [blame] | 169 | int |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 170 | sshkey_load_private_pem(int fd, int type, const char *passphrase, |
| 171 | struct sshkey **keyp, char **commentp) |
| 172 | { |
| 173 | struct sshbuf *buffer = NULL; |
| 174 | int r; |
| 175 | |
| 176 | *keyp = NULL; |
| 177 | if (commentp != NULL) |
| 178 | *commentp = NULL; |
| 179 | |
| 180 | if ((buffer = sshbuf_new()) == NULL) |
| 181 | return SSH_ERR_ALLOC_FAIL; |
| 182 | if ((r = sshkey_load_file(fd, NULL, buffer)) != 0) |
| 183 | goto out; |
| 184 | if ((r = sshkey_parse_private_pem_fileblob(buffer, type, passphrase, |
| 185 | keyp, commentp)) != 0) |
| 186 | goto out; |
| 187 | r = 0; |
| 188 | out: |
| 189 | sshbuf_free(buffer); |
| 190 | return r; |
| 191 | } |
| 192 | #endif /* WITH_OPENSSL */ |
| 193 | |
| 194 | /* XXX remove error() calls from here? */ |
| 195 | int |
| 196 | sshkey_perm_ok(int fd, const char *filename) |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 197 | { |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 198 | struct stat st; |
| 199 | |
Ben Lindstrom | 7aff261 | 2001-09-23 13:53:22 +0000 | [diff] [blame] | 200 | if (fstat(fd, &st) < 0) |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 201 | return SSH_ERR_SYSTEM_ERROR; |
Ben Lindstrom | 7aff261 | 2001-09-23 13:53:22 +0000 | [diff] [blame] | 202 | /* |
| 203 | * if a key owned by the user is accessed, then we check the |
| 204 | * permissions of the file. if the key owned by a different user, |
| 205 | * then we don't care. |
| 206 | */ |
Damien Miller | b70b61f | 2000-09-16 16:25:12 +1100 | [diff] [blame] | 207 | #ifdef HAVE_CYGWIN |
Damien Miller | cb5e44a | 2000-09-29 12:12:36 +1100 | [diff] [blame] | 208 | if (check_ntsec(filename)) |
Damien Miller | b70b61f | 2000-09-16 16:25:12 +1100 | [diff] [blame] | 209 | #endif |
Ben Lindstrom | 7aff261 | 2001-09-23 13:53:22 +0000 | [diff] [blame] | 210 | if ((st.st_uid == getuid()) && (st.st_mode & 077) != 0) { |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 211 | error("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"); |
| 212 | error("@ WARNING: UNPROTECTED PRIVATE KEY FILE! @"); |
| 213 | error("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"); |
Ben Lindstrom | 7aff261 | 2001-09-23 13:53:22 +0000 | [diff] [blame] | 214 | error("Permissions 0%3.3o for '%s' are too open.", |
Damien Miller | 04bd8b0 | 2003-05-25 14:38:33 +1000 | [diff] [blame] | 215 | (u_int)st.st_mode & 0777, filename); |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 216 | error("It is recommended that your private key files are NOT accessible by others."); |
Ben Lindstrom | d0fca42 | 2001-03-26 13:44:06 +0000 | [diff] [blame] | 217 | error("This private key will be ignored."); |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 218 | return SSH_ERR_KEY_BAD_PERMISSIONS; |
Damien Miller | e4340be | 2000-09-16 13:29:08 +1100 | [diff] [blame] | 219 | } |
| 220 | return 0; |
| 221 | } |
| 222 | |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 223 | /* XXX kill perm_ok now that we have SSH_ERR_KEY_BAD_PERMISSIONS? */ |
| 224 | int |
| 225 | sshkey_load_private_type(int type, const char *filename, const char *passphrase, |
| 226 | struct sshkey **keyp, char **commentp, int *perm_ok) |
Damien Miller | e4340be | 2000-09-16 13:29:08 +1100 | [diff] [blame] | 227 | { |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 228 | int fd, r; |
| 229 | struct sshbuf *buffer = NULL; |
Damien Miller | e4340be | 2000-09-16 13:29:08 +1100 | [diff] [blame] | 230 | |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 231 | *keyp = NULL; |
| 232 | if (commentp != NULL) |
| 233 | *commentp = NULL; |
| 234 | |
| 235 | if ((fd = open(filename, O_RDONLY)) < 0) { |
| 236 | if (perm_ok != NULL) |
| 237 | *perm_ok = 0; |
| 238 | return SSH_ERR_SYSTEM_ERROR; |
| 239 | } |
| 240 | if (sshkey_perm_ok(fd, filename) != 0) { |
| 241 | if (perm_ok != NULL) |
| 242 | *perm_ok = 0; |
| 243 | r = SSH_ERR_KEY_BAD_PERMISSIONS; |
| 244 | goto out; |
| 245 | } |
| 246 | if (perm_ok != NULL) |
| 247 | *perm_ok = 1; |
| 248 | |
| 249 | if ((buffer = sshbuf_new()) == NULL) { |
| 250 | r = SSH_ERR_ALLOC_FAIL; |
| 251 | goto out; |
| 252 | } |
| 253 | if ((r = sshkey_load_file(fd, filename, buffer)) != 0) |
| 254 | goto out; |
| 255 | if ((r = sshkey_parse_private_fileblob_type(buffer, type, passphrase, |
| 256 | keyp, commentp)) != 0) |
| 257 | goto out; |
| 258 | r = 0; |
| 259 | out: |
| 260 | close(fd); |
| 261 | if (buffer != NULL) |
| 262 | sshbuf_free(buffer); |
| 263 | return r; |
| 264 | } |
| 265 | |
| 266 | /* XXX this is almost identical to sshkey_load_private_type() */ |
| 267 | int |
| 268 | sshkey_load_private(const char *filename, const char *passphrase, |
| 269 | struct sshkey **keyp, char **commentp) |
| 270 | { |
| 271 | struct sshbuf *buffer = NULL; |
| 272 | int r, fd; |
| 273 | |
| 274 | *keyp = NULL; |
| 275 | if (commentp != NULL) |
| 276 | *commentp = NULL; |
| 277 | |
| 278 | if ((fd = open(filename, O_RDONLY)) < 0) |
| 279 | return SSH_ERR_SYSTEM_ERROR; |
| 280 | if (sshkey_perm_ok(fd, filename) != 0) { |
| 281 | r = SSH_ERR_KEY_BAD_PERMISSIONS; |
| 282 | goto out; |
| 283 | } |
| 284 | |
| 285 | if ((buffer = sshbuf_new()) == NULL) { |
| 286 | r = SSH_ERR_ALLOC_FAIL; |
| 287 | goto out; |
| 288 | } |
| 289 | if ((r = sshkey_load_file(fd, filename, buffer)) != 0 || |
| 290 | (r = sshkey_parse_private_fileblob(buffer, passphrase, filename, |
| 291 | keyp, commentp)) != 0) |
| 292 | goto out; |
| 293 | r = 0; |
| 294 | out: |
| 295 | close(fd); |
| 296 | if (buffer != NULL) |
| 297 | sshbuf_free(buffer); |
| 298 | return r; |
| 299 | } |
| 300 | |
| 301 | static int |
| 302 | sshkey_try_load_public(struct sshkey *k, const char *filename, char **commentp) |
| 303 | { |
| 304 | FILE *f; |
| 305 | char line[SSH_MAX_PUBKEY_BYTES]; |
| 306 | char *cp; |
| 307 | u_long linenum = 0; |
| 308 | int r; |
| 309 | |
| 310 | if (commentp != NULL) |
| 311 | *commentp = NULL; |
| 312 | if ((f = fopen(filename, "r")) == NULL) |
| 313 | return SSH_ERR_SYSTEM_ERROR; |
| 314 | while (read_keyfile_line(f, filename, line, sizeof(line), |
| 315 | &linenum) != -1) { |
| 316 | cp = line; |
| 317 | switch (*cp) { |
| 318 | case '#': |
| 319 | case '\n': |
| 320 | case '\0': |
| 321 | continue; |
| 322 | } |
| 323 | /* Abort loading if this looks like a private key */ |
| 324 | if (strncmp(cp, "-----BEGIN", 10) == 0 || |
| 325 | strcmp(cp, "SSH PRIVATE KEY FILE") == 0) |
| 326 | break; |
| 327 | /* Skip leading whitespace. */ |
| 328 | for (; *cp && (*cp == ' ' || *cp == '\t'); cp++) |
| 329 | ; |
| 330 | if (*cp) { |
| 331 | if ((r = sshkey_read(k, &cp)) == 0) { |
| 332 | cp[strcspn(cp, "\r\n")] = '\0'; |
| 333 | if (commentp) { |
| 334 | *commentp = strdup(*cp ? |
| 335 | cp : filename); |
| 336 | if (*commentp == NULL) |
| 337 | r = SSH_ERR_ALLOC_FAIL; |
| 338 | } |
| 339 | fclose(f); |
| 340 | return r; |
| 341 | } |
| 342 | } |
| 343 | } |
| 344 | fclose(f); |
| 345 | return SSH_ERR_INVALID_FORMAT; |
| 346 | } |
| 347 | |
| 348 | /* load public key from ssh v1 private or any pubkey file */ |
| 349 | int |
| 350 | sshkey_load_public(const char *filename, struct sshkey **keyp, char **commentp) |
| 351 | { |
| 352 | struct sshkey *pub = NULL; |
| 353 | char file[MAXPATHLEN]; |
| 354 | int r, fd; |
| 355 | |
| 356 | if (keyp != NULL) |
| 357 | *keyp = NULL; |
| 358 | if (commentp != NULL) |
| 359 | *commentp = NULL; |
| 360 | |
| 361 | if ((fd = open(filename, O_RDONLY)) < 0) |
| 362 | goto skip; |
Damien Miller | 1f0311c | 2014-05-15 14:24:09 +1000 | [diff] [blame] | 363 | #ifdef WITH_SSH1 |
Damien Miller | db27472 | 2003-05-14 13:45:22 +1000 | [diff] [blame] | 364 | /* try rsa1 private key */ |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 365 | r = sshkey_load_public_rsa1(fd, filename, keyp, commentp); |
| 366 | close(fd); |
| 367 | switch (r) { |
| 368 | case SSH_ERR_INTERNAL_ERROR: |
| 369 | case SSH_ERR_ALLOC_FAIL: |
| 370 | case SSH_ERR_INVALID_ARGUMENT: |
| 371 | case SSH_ERR_SYSTEM_ERROR: |
| 372 | case 0: |
| 373 | return r; |
| 374 | } |
| 375 | #endif /* WITH_SSH1 */ |
Damien Miller | db27472 | 2003-05-14 13:45:22 +1000 | [diff] [blame] | 376 | |
| 377 | /* try ssh2 public key */ |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 378 | if ((pub = sshkey_new(KEY_UNSPEC)) == NULL) |
| 379 | return SSH_ERR_ALLOC_FAIL; |
| 380 | if ((r = sshkey_try_load_public(pub, filename, commentp)) == 0) { |
| 381 | if (keyp != NULL) |
| 382 | *keyp = pub; |
| 383 | return 0; |
| 384 | } |
| 385 | sshkey_free(pub); |
| 386 | |
| 387 | #ifdef WITH_SSH1 |
| 388 | /* try rsa1 public key */ |
| 389 | if ((pub = sshkey_new(KEY_RSA1)) == NULL) |
| 390 | return SSH_ERR_ALLOC_FAIL; |
| 391 | if ((r = sshkey_try_load_public(pub, filename, commentp)) == 0) { |
| 392 | if (keyp != NULL) |
| 393 | *keyp = pub; |
| 394 | return 0; |
| 395 | } |
| 396 | sshkey_free(pub); |
| 397 | #endif /* WITH_SSH1 */ |
| 398 | |
| 399 | skip: |
| 400 | /* try .pub suffix */ |
| 401 | if ((pub = sshkey_new(KEY_UNSPEC)) == NULL) |
| 402 | return SSH_ERR_ALLOC_FAIL; |
| 403 | r = SSH_ERR_ALLOC_FAIL; /* in case strlcpy or strlcat fail */ |
Ben Lindstrom | d0fca42 | 2001-03-26 13:44:06 +0000 | [diff] [blame] | 404 | if ((strlcpy(file, filename, sizeof file) < sizeof(file)) && |
| 405 | (strlcat(file, ".pub", sizeof file) < sizeof(file)) && |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 406 | (r = sshkey_try_load_public(pub, file, commentp)) == 0) { |
| 407 | if (keyp != NULL) |
| 408 | *keyp = pub; |
| 409 | return 0; |
| 410 | } |
| 411 | sshkey_free(pub); |
| 412 | return r; |
Damien Miller | e4340be | 2000-09-16 13:29:08 +1100 | [diff] [blame] | 413 | } |
Damien Miller | 1aed65e | 2010-03-04 21:53:35 +1100 | [diff] [blame] | 414 | |
Damien Miller | c158331 | 2010-08-05 13:04:50 +1000 | [diff] [blame] | 415 | /* Load the certificate associated with the named private key */ |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 416 | int |
| 417 | sshkey_load_cert(const char *filename, struct sshkey **keyp) |
Damien Miller | c158331 | 2010-08-05 13:04:50 +1000 | [diff] [blame] | 418 | { |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 419 | struct sshkey *pub = NULL; |
| 420 | char *file = NULL; |
| 421 | int r = SSH_ERR_INTERNAL_ERROR; |
Damien Miller | c158331 | 2010-08-05 13:04:50 +1000 | [diff] [blame] | 422 | |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 423 | *keyp = NULL; |
| 424 | |
| 425 | if (asprintf(&file, "%s-cert.pub", filename) == -1) |
| 426 | return SSH_ERR_ALLOC_FAIL; |
| 427 | |
| 428 | if ((pub = sshkey_new(KEY_UNSPEC)) == NULL) { |
| 429 | goto out; |
Damien Miller | 5458c4d | 2010-08-05 13:05:15 +1000 | [diff] [blame] | 430 | } |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 431 | if ((r = sshkey_try_load_public(pub, file, NULL)) != 0) |
| 432 | goto out; |
| 433 | |
| 434 | *keyp = pub; |
| 435 | pub = NULL; |
| 436 | r = 0; |
| 437 | |
| 438 | out: |
| 439 | if (file != NULL) |
| 440 | free(file); |
| 441 | if (pub != NULL) |
| 442 | sshkey_free(pub); |
| 443 | return r; |
Damien Miller | c158331 | 2010-08-05 13:04:50 +1000 | [diff] [blame] | 444 | } |
| 445 | |
| 446 | /* Load private key and certificate */ |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 447 | int |
| 448 | sshkey_load_private_cert(int type, const char *filename, const char *passphrase, |
| 449 | struct sshkey **keyp, int *perm_ok) |
Damien Miller | c158331 | 2010-08-05 13:04:50 +1000 | [diff] [blame] | 450 | { |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 451 | struct sshkey *key = NULL, *cert = NULL; |
| 452 | int r; |
| 453 | |
| 454 | *keyp = NULL; |
Damien Miller | c158331 | 2010-08-05 13:04:50 +1000 | [diff] [blame] | 455 | |
| 456 | switch (type) { |
Damien Miller | 1f0311c | 2014-05-15 14:24:09 +1000 | [diff] [blame] | 457 | #ifdef WITH_OPENSSL |
Damien Miller | c158331 | 2010-08-05 13:04:50 +1000 | [diff] [blame] | 458 | case KEY_RSA: |
| 459 | case KEY_DSA: |
Damien Miller | eb8b60e | 2010-08-31 22:41:14 +1000 | [diff] [blame] | 460 | case KEY_ECDSA: |
Damien Miller | b9a9549 | 2013-12-29 17:50:15 +1100 | [diff] [blame] | 461 | case KEY_ED25519: |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 462 | #endif /* WITH_OPENSSL */ |
| 463 | case KEY_UNSPEC: |
Damien Miller | c158331 | 2010-08-05 13:04:50 +1000 | [diff] [blame] | 464 | break; |
| 465 | default: |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 466 | return SSH_ERR_KEY_TYPE_UNKNOWN; |
Damien Miller | c158331 | 2010-08-05 13:04:50 +1000 | [diff] [blame] | 467 | } |
| 468 | |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 469 | if ((r = sshkey_load_private_type(type, filename, |
| 470 | passphrase, &key, NULL, perm_ok)) != 0 || |
| 471 | (r = sshkey_load_cert(filename, &cert)) != 0) |
| 472 | goto out; |
Damien Miller | c158331 | 2010-08-05 13:04:50 +1000 | [diff] [blame] | 473 | |
| 474 | /* Make sure the private key matches the certificate */ |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 475 | if (sshkey_equal_public(key, cert) == 0) { |
| 476 | r = SSH_ERR_KEY_CERT_MISMATCH; |
| 477 | goto out; |
Damien Miller | c158331 | 2010-08-05 13:04:50 +1000 | [diff] [blame] | 478 | } |
| 479 | |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 480 | if ((r = sshkey_to_certified(key, sshkey_cert_is_legacy(cert))) != 0 || |
| 481 | (r = sshkey_cert_copy(cert, key)) != 0) |
| 482 | goto out; |
| 483 | r = 0; |
| 484 | *keyp = key; |
| 485 | key = NULL; |
| 486 | out: |
| 487 | if (key != NULL) |
| 488 | sshkey_free(key); |
| 489 | if (cert != NULL) |
| 490 | sshkey_free(cert); |
| 491 | return r; |
Damien Miller | c158331 | 2010-08-05 13:04:50 +1000 | [diff] [blame] | 492 | } |
| 493 | |
Damien Miller | 1aed65e | 2010-03-04 21:53:35 +1100 | [diff] [blame] | 494 | /* |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 495 | * Returns success if the specified "key" is listed in the file "filename", |
| 496 | * SSH_ERR_KEY_NOT_FOUND: if the key is not listed or another error. |
Damien Miller | 1aed65e | 2010-03-04 21:53:35 +1100 | [diff] [blame] | 497 | * If strict_type is set then the key type must match exactly, |
| 498 | * otherwise a comparison that ignores certficiate data is performed. |
| 499 | */ |
| 500 | int |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 501 | sshkey_in_file(struct sshkey *key, const char *filename, int strict_type) |
Damien Miller | 1aed65e | 2010-03-04 21:53:35 +1100 | [diff] [blame] | 502 | { |
| 503 | FILE *f; |
| 504 | char line[SSH_MAX_PUBKEY_BYTES]; |
| 505 | char *cp; |
| 506 | u_long linenum = 0; |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 507 | int r = 0; |
| 508 | struct sshkey *pub = NULL; |
| 509 | int (*sshkey_compare)(const struct sshkey *, const struct sshkey *) = |
| 510 | strict_type ? sshkey_equal : sshkey_equal_public; |
Damien Miller | 1aed65e | 2010-03-04 21:53:35 +1100 | [diff] [blame] | 511 | |
| 512 | if ((f = fopen(filename, "r")) == NULL) { |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 513 | if (errno == ENOENT) |
| 514 | return SSH_ERR_KEY_NOT_FOUND; |
| 515 | else |
| 516 | return SSH_ERR_SYSTEM_ERROR; |
Damien Miller | 1aed65e | 2010-03-04 21:53:35 +1100 | [diff] [blame] | 517 | } |
| 518 | |
| 519 | while (read_keyfile_line(f, filename, line, sizeof(line), |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 520 | &linenum) != -1) { |
Damien Miller | 1aed65e | 2010-03-04 21:53:35 +1100 | [diff] [blame] | 521 | cp = line; |
| 522 | |
| 523 | /* Skip leading whitespace. */ |
| 524 | for (; *cp && (*cp == ' ' || *cp == '\t'); cp++) |
| 525 | ; |
| 526 | |
| 527 | /* Skip comments and empty lines */ |
| 528 | switch (*cp) { |
| 529 | case '#': |
| 530 | case '\n': |
| 531 | case '\0': |
| 532 | continue; |
| 533 | } |
| 534 | |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 535 | if ((pub = sshkey_new(KEY_UNSPEC)) == NULL) { |
| 536 | r = SSH_ERR_ALLOC_FAIL; |
| 537 | goto out; |
Damien Miller | 1aed65e | 2010-03-04 21:53:35 +1100 | [diff] [blame] | 538 | } |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 539 | if ((r = sshkey_read(pub, &cp)) != 0) |
| 540 | goto out; |
| 541 | if (sshkey_compare(key, pub)) { |
| 542 | r = 0; |
| 543 | goto out; |
Damien Miller | 1aed65e | 2010-03-04 21:53:35 +1100 | [diff] [blame] | 544 | } |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 545 | sshkey_free(pub); |
| 546 | pub = NULL; |
Damien Miller | 1aed65e | 2010-03-04 21:53:35 +1100 | [diff] [blame] | 547 | } |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 548 | r = SSH_ERR_KEY_NOT_FOUND; |
| 549 | out: |
| 550 | if (pub != NULL) |
| 551 | sshkey_free(pub); |
Damien Miller | 1aed65e | 2010-03-04 21:53:35 +1100 | [diff] [blame] | 552 | fclose(f); |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 553 | return r; |
Damien Miller | 1aed65e | 2010-03-04 21:53:35 +1100 | [diff] [blame] | 554 | } |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 555 | |