blob: 20b66d9bd5d48b1ed5d2b0d417b9c8578c152566 [file] [log] [blame]
djm@openbsd.org99aa8032020-01-25 23:02:13 +00001/* $OpenBSD: authfile.c,v 1.137 2020/01/25 23:02:13 djm Exp $ */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002/*
Damien Millerbcd00ab2013-12-07 10:41:55 +11003 * Copyright (c) 2000, 2013 Markus Friedl. All rights reserved.
Damien Millere4340be2000-09-16 13:29:08 +11004 *
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 Miller95def091999-11-25 00:26:21 +110024 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100025
26#include "includes.h"
Damien Millerf17883e2006-03-15 11:45:54 +110027
28#include <sys/types.h>
29#include <sys/stat.h>
Damien Millerd7834352006-08-05 12:39:39 +100030#include <sys/uio.h>
Damien Millerd4a8b7e1999-10-27 13:42:43 +100031
Darren Tuckerba724052006-07-12 22:24:22 +100032#include <errno.h>
Damien Miller57cf6382006-07-10 21:13:46 +100033#include <fcntl.h>
Damien Millera7a73ee2006-08-05 11:37:59 +100034#include <stdio.h>
Damien Miller86687062014-07-02 15:28:02 +100035#include <stdarg.h>
Damien Millere7a1e5c2006-08-05 11:34:19 +100036#include <stdlib.h>
Damien Millere3476ed2006-07-24 14:13:33 +100037#include <string.h>
Damien Millere6b3b612006-07-24 14:01:23 +100038#include <unistd.h>
deraadt@openbsd.org087266e2015-01-20 23:14:00 +000039#include <limits.h>
Damien Miller57cf6382006-07-10 21:13:46 +100040
Damien Millerd7834352006-08-05 12:39:39 +100041#include "cipher.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000042#include "ssh.h"
43#include "log.h"
Ben Lindstrom31ca54a2001-02-09 02:11:24 +000044#include "authfile.h"
Darren Tuckerf0f90982004-12-11 13:39:50 +110045#include "misc.h"
Damien Millereccb9de2005-06-17 12:59:34 +100046#include "atomicio.h"
markus@openbsd.org16db0a72015-07-09 09:49:46 +000047#include "sshkey.h"
Damien Miller86687062014-07-02 15:28:02 +100048#include "sshbuf.h"
49#include "ssherr.h"
djm@openbsd.org5e39a492014-12-04 02:24:32 +000050#include "krl.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100051
Damien Miller2ce12ef2011-05-05 14:17:18 +100052#define MAX_KEY_FILE_SIZE (1024 * 1024)
53
Damien Millera2327922010-12-01 12:01:21 +110054/* Save a key blob to a file */
55static int
Damien Miller86687062014-07-02 15:28:02 +100056sshkey_save_private_blob(struct sshbuf *keybuf, const char *filename)
Damien Millera2327922010-12-01 12:01:21 +110057{
djm@openbsd.org99aa8032020-01-25 23:02:13 +000058 int r;
59 mode_t omask;
Damien Millera2327922010-12-01 12:01:21 +110060
djm@openbsd.org99aa8032020-01-25 23:02:13 +000061 omask = umask(077);
62 r = sshbuf_write_file(filename, keybuf);
63 umask(omask);
64 return r;
Damien Millera2327922010-12-01 12:01:21 +110065}
66
Damien Millereba71ba2000-04-29 23:57:08 +100067int
Damien Miller86687062014-07-02 15:28:02 +100068sshkey_save_private(struct sshkey *key, const char *filename,
69 const char *passphrase, const char *comment,
djm@openbsd.orgeb0d8e72019-07-15 13:16:29 +000070 int format, const char *openssh_format_cipher, int openssh_format_rounds)
Damien Millereba71ba2000-04-29 23:57:08 +100071{
Damien Miller86687062014-07-02 15:28:02 +100072 struct sshbuf *keyblob = NULL;
73 int r;
Damien Millera2327922010-12-01 12:01:21 +110074
Damien Miller86687062014-07-02 15:28:02 +100075 if ((keyblob = sshbuf_new()) == NULL)
76 return SSH_ERR_ALLOC_FAIL;
77 if ((r = sshkey_private_to_fileblob(key, keyblob, passphrase, comment,
djm@openbsd.orgeb0d8e72019-07-15 13:16:29 +000078 format, openssh_format_cipher, openssh_format_rounds)) != 0)
Damien Millera2327922010-12-01 12:01:21 +110079 goto out;
Damien Miller86687062014-07-02 15:28:02 +100080 if ((r = sshkey_save_private_blob(keyblob, filename)) != 0)
Damien Millera2327922010-12-01 12:01:21 +110081 goto out;
Damien Miller86687062014-07-02 15:28:02 +100082 r = 0;
Damien Millera2327922010-12-01 12:01:21 +110083 out:
Damien Miller86687062014-07-02 15:28:02 +100084 sshbuf_free(keyblob);
85 return r;
Damien Millera2327922010-12-01 12:01:21 +110086}
87
Damien Miller86687062014-07-02 15:28:02 +100088/* XXX remove error() calls from here? */
89int
90sshkey_perm_ok(int fd, const char *filename)
Damien Millereba71ba2000-04-29 23:57:08 +100091{
Damien Millereba71ba2000-04-29 23:57:08 +100092 struct stat st;
93
deraadt@openbsd.org4d28fa72019-06-28 13:35:04 +000094 if (fstat(fd, &st) == -1)
Damien Miller86687062014-07-02 15:28:02 +100095 return SSH_ERR_SYSTEM_ERROR;
Ben Lindstrom7aff2612001-09-23 13:53:22 +000096 /*
97 * if a key owned by the user is accessed, then we check the
98 * permissions of the file. if the key owned by a different user,
99 * then we don't care.
100 */
Damien Millerb70b61f2000-09-16 16:25:12 +1100101#ifdef HAVE_CYGWIN
Damien Millercb5e44a2000-09-29 12:12:36 +1100102 if (check_ntsec(filename))
Damien Millerb70b61f2000-09-16 16:25:12 +1100103#endif
Ben Lindstrom7aff2612001-09-23 13:53:22 +0000104 if ((st.st_uid == getuid()) && (st.st_mode & 077) != 0) {
Damien Millereba71ba2000-04-29 23:57:08 +1000105 error("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
106 error("@ WARNING: UNPROTECTED PRIVATE KEY FILE! @");
107 error("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
Ben Lindstrom7aff2612001-09-23 13:53:22 +0000108 error("Permissions 0%3.3o for '%s' are too open.",
Damien Miller04bd8b02003-05-25 14:38:33 +1000109 (u_int)st.st_mode & 0777, filename);
djm@openbsd.org6b0d5762015-04-17 13:32:09 +0000110 error("It is required that your private key files are NOT accessible by others.");
Ben Lindstromd0fca422001-03-26 13:44:06 +0000111 error("This private key will be ignored.");
Damien Miller86687062014-07-02 15:28:02 +1000112 return SSH_ERR_KEY_BAD_PERMISSIONS;
Damien Millere4340be2000-09-16 13:29:08 +1100113 }
114 return 0;
115}
116
Damien Miller86687062014-07-02 15:28:02 +1000117int
118sshkey_load_private_type(int type, const char *filename, const char *passphrase,
dtucker@openbsd.org6b39a7b2019-08-05 11:50:33 +0000119 struct sshkey **keyp, char **commentp)
Damien Millere4340be2000-09-16 13:29:08 +1100120{
Damien Miller86687062014-07-02 15:28:02 +1000121 int fd, r;
Damien Millere4340be2000-09-16 13:29:08 +1100122
djm@openbsd.orgdce19bf2016-04-09 12:39:30 +0000123 if (keyp != NULL)
124 *keyp = NULL;
Damien Miller86687062014-07-02 15:28:02 +1000125 if (commentp != NULL)
126 *commentp = NULL;
127
dtucker@openbsd.org6b39a7b2019-08-05 11:50:33 +0000128 if ((fd = open(filename, O_RDONLY)) == -1)
Damien Miller86687062014-07-02 15:28:02 +1000129 return SSH_ERR_SYSTEM_ERROR;
dtucker@openbsd.org6b39a7b2019-08-05 11:50:33 +0000130
131 r = sshkey_perm_ok(fd, filename);
132 if (r != 0)
Damien Miller86687062014-07-02 15:28:02 +1000133 goto out;
Damien Miller86687062014-07-02 15:28:02 +1000134
djm@openbsd.org1195f4c2015-01-08 10:14:08 +0000135 r = sshkey_load_private_type_fd(fd, type, passphrase, keyp, commentp);
markus@openbsd.org1b11ea72018-02-23 15:58:37 +0000136 if (r == 0 && keyp && *keyp)
137 r = sshkey_set_filename(*keyp, filename);
djm@openbsd.org1195f4c2015-01-08 10:14:08 +0000138 out:
139 close(fd);
140 return r;
141}
142
143int
144sshkey_load_private_type_fd(int fd, int type, const char *passphrase,
145 struct sshkey **keyp, char **commentp)
146{
147 struct sshbuf *buffer = NULL;
148 int r;
149
djm@openbsd.orgdce19bf2016-04-09 12:39:30 +0000150 if (keyp != NULL)
151 *keyp = NULL;
djm@openbsd.org99aa8032020-01-25 23:02:13 +0000152 if ((r = sshbuf_load_fd(fd, &buffer)) != 0 ||
djm@openbsd.org1195f4c2015-01-08 10:14:08 +0000153 (r = sshkey_parse_private_fileblob_type(buffer, type,
154 passphrase, keyp, commentp)) != 0)
Damien Miller86687062014-07-02 15:28:02 +1000155 goto out;
djm@openbsd.org1195f4c2015-01-08 10:14:08 +0000156
157 /* success */
Damien Miller86687062014-07-02 15:28:02 +1000158 r = 0;
159 out:
mmcc@openbsd.org52d70782015-12-11 04:21:11 +0000160 sshbuf_free(buffer);
Damien Miller86687062014-07-02 15:28:02 +1000161 return r;
162}
163
164/* XXX this is almost identical to sshkey_load_private_type() */
165int
166sshkey_load_private(const char *filename, const char *passphrase,
167 struct sshkey **keyp, char **commentp)
168{
169 struct sshbuf *buffer = NULL;
170 int r, fd;
171
djm@openbsd.orgdce19bf2016-04-09 12:39:30 +0000172 if (keyp != NULL)
173 *keyp = NULL;
Damien Miller86687062014-07-02 15:28:02 +1000174 if (commentp != NULL)
175 *commentp = NULL;
176
deraadt@openbsd.org4d28fa72019-06-28 13:35:04 +0000177 if ((fd = open(filename, O_RDONLY)) == -1)
Damien Miller86687062014-07-02 15:28:02 +1000178 return SSH_ERR_SYSTEM_ERROR;
179 if (sshkey_perm_ok(fd, filename) != 0) {
180 r = SSH_ERR_KEY_BAD_PERMISSIONS;
181 goto out;
182 }
djm@openbsd.org99aa8032020-01-25 23:02:13 +0000183 if ((r = sshbuf_load_fd(fd, &buffer)) != 0 ||
tim@openbsd.org3c019a92015-09-13 14:39:16 +0000184 (r = sshkey_parse_private_fileblob(buffer, passphrase, keyp,
185 commentp)) != 0)
Damien Miller86687062014-07-02 15:28:02 +1000186 goto out;
markus@openbsd.org1b11ea72018-02-23 15:58:37 +0000187 if (keyp && *keyp &&
188 (r = sshkey_set_filename(*keyp, filename)) != 0)
189 goto out;
Damien Miller86687062014-07-02 15:28:02 +1000190 r = 0;
191 out:
192 close(fd);
mmcc@openbsd.org52d70782015-12-11 04:21:11 +0000193 sshbuf_free(buffer);
Damien Miller86687062014-07-02 15:28:02 +1000194 return r;
195}
196
197static int
198sshkey_try_load_public(struct sshkey *k, const char *filename, char **commentp)
199{
200 FILE *f;
markus@openbsd.org7f906352018-06-06 18:29:18 +0000201 char *line = NULL, *cp;
202 size_t linesize = 0;
Damien Miller86687062014-07-02 15:28:02 +1000203 int r;
204
205 if (commentp != NULL)
206 *commentp = NULL;
207 if ((f = fopen(filename, "r")) == NULL)
208 return SSH_ERR_SYSTEM_ERROR;
markus@openbsd.org7f906352018-06-06 18:29:18 +0000209 while (getline(&line, &linesize, f) != -1) {
Damien Miller86687062014-07-02 15:28:02 +1000210 cp = line;
211 switch (*cp) {
212 case '#':
213 case '\n':
214 case '\0':
215 continue;
216 }
217 /* Abort loading if this looks like a private key */
218 if (strncmp(cp, "-----BEGIN", 10) == 0 ||
219 strcmp(cp, "SSH PRIVATE KEY FILE") == 0)
220 break;
221 /* Skip leading whitespace. */
222 for (; *cp && (*cp == ' ' || *cp == '\t'); cp++)
223 ;
224 if (*cp) {
225 if ((r = sshkey_read(k, &cp)) == 0) {
226 cp[strcspn(cp, "\r\n")] = '\0';
227 if (commentp) {
228 *commentp = strdup(*cp ?
229 cp : filename);
230 if (*commentp == NULL)
231 r = SSH_ERR_ALLOC_FAIL;
232 }
markus@openbsd.org7f906352018-06-06 18:29:18 +0000233 free(line);
Damien Miller86687062014-07-02 15:28:02 +1000234 fclose(f);
235 return r;
236 }
237 }
238 }
markus@openbsd.org7f906352018-06-06 18:29:18 +0000239 free(line);
Damien Miller86687062014-07-02 15:28:02 +1000240 fclose(f);
241 return SSH_ERR_INVALID_FORMAT;
242}
243
markus@openbsd.orgafbfa682017-05-30 08:49:32 +0000244/* load public key from any pubkey file */
Damien Miller86687062014-07-02 15:28:02 +1000245int
246sshkey_load_public(const char *filename, struct sshkey **keyp, char **commentp)
247{
248 struct sshkey *pub = NULL;
markus@openbsd.orgafbfa682017-05-30 08:49:32 +0000249 char *file = NULL;
250 int r;
Damien Miller86687062014-07-02 15:28:02 +1000251
252 if (keyp != NULL)
253 *keyp = NULL;
254 if (commentp != NULL)
255 *commentp = NULL;
256
Damien Miller86687062014-07-02 15:28:02 +1000257 if ((pub = sshkey_new(KEY_UNSPEC)) == NULL)
258 return SSH_ERR_ALLOC_FAIL;
259 if ((r = sshkey_try_load_public(pub, filename, commentp)) == 0) {
markus@openbsd.orgafbfa682017-05-30 08:49:32 +0000260 if (keyp != NULL) {
Damien Miller86687062014-07-02 15:28:02 +1000261 *keyp = pub;
markus@openbsd.orgafbfa682017-05-30 08:49:32 +0000262 pub = NULL;
263 }
264 r = 0;
265 goto out;
Damien Miller86687062014-07-02 15:28:02 +1000266 }
267 sshkey_free(pub);
268
Damien Miller86687062014-07-02 15:28:02 +1000269 /* try .pub suffix */
markus@openbsd.orgafbfa682017-05-30 08:49:32 +0000270 if (asprintf(&file, "%s.pub", filename) == -1)
Damien Miller86687062014-07-02 15:28:02 +1000271 return SSH_ERR_ALLOC_FAIL;
markus@openbsd.orgafbfa682017-05-30 08:49:32 +0000272 if ((pub = sshkey_new(KEY_UNSPEC)) == NULL) {
273 r = SSH_ERR_ALLOC_FAIL;
274 goto out;
Damien Miller86687062014-07-02 15:28:02 +1000275 }
markus@openbsd.orgafbfa682017-05-30 08:49:32 +0000276 if ((r = sshkey_try_load_public(pub, file, commentp)) == 0) {
277 if (keyp != NULL) {
278 *keyp = pub;
279 pub = NULL;
280 }
281 r = 0;
282 }
283 out:
284 free(file);
Damien Miller86687062014-07-02 15:28:02 +1000285 sshkey_free(pub);
286 return r;
Damien Millere4340be2000-09-16 13:29:08 +1100287}
Damien Miller1aed65e2010-03-04 21:53:35 +1100288
Damien Millerc1583312010-08-05 13:04:50 +1000289/* Load the certificate associated with the named private key */
Damien Miller86687062014-07-02 15:28:02 +1000290int
291sshkey_load_cert(const char *filename, struct sshkey **keyp)
Damien Millerc1583312010-08-05 13:04:50 +1000292{
Damien Miller86687062014-07-02 15:28:02 +1000293 struct sshkey *pub = NULL;
294 char *file = NULL;
295 int r = SSH_ERR_INTERNAL_ERROR;
Damien Millerc1583312010-08-05 13:04:50 +1000296
djm@openbsd.orgdce19bf2016-04-09 12:39:30 +0000297 if (keyp != NULL)
298 *keyp = NULL;
Damien Miller86687062014-07-02 15:28:02 +1000299
300 if (asprintf(&file, "%s-cert.pub", filename) == -1)
301 return SSH_ERR_ALLOC_FAIL;
302
303 if ((pub = sshkey_new(KEY_UNSPEC)) == NULL) {
304 goto out;
Damien Miller5458c4d2010-08-05 13:05:15 +1000305 }
Damien Miller86687062014-07-02 15:28:02 +1000306 if ((r = sshkey_try_load_public(pub, file, NULL)) != 0)
307 goto out;
djm@openbsd.orgdce19bf2016-04-09 12:39:30 +0000308 /* success */
309 if (keyp != NULL) {
310 *keyp = pub;
311 pub = NULL;
312 }
Damien Miller86687062014-07-02 15:28:02 +1000313 r = 0;
Damien Miller86687062014-07-02 15:28:02 +1000314 out:
mmcc@openbsd.orgd59ce082015-12-10 17:08:40 +0000315 free(file);
mmcc@openbsd.org89540b62015-12-11 02:31:47 +0000316 sshkey_free(pub);
Damien Miller86687062014-07-02 15:28:02 +1000317 return r;
Damien Millerc1583312010-08-05 13:04:50 +1000318}
319
320/* Load private key and certificate */
Damien Miller86687062014-07-02 15:28:02 +1000321int
322sshkey_load_private_cert(int type, const char *filename, const char *passphrase,
dtucker@openbsd.org6b39a7b2019-08-05 11:50:33 +0000323 struct sshkey **keyp)
Damien Millerc1583312010-08-05 13:04:50 +1000324{
Damien Miller86687062014-07-02 15:28:02 +1000325 struct sshkey *key = NULL, *cert = NULL;
326 int r;
327
djm@openbsd.orgdce19bf2016-04-09 12:39:30 +0000328 if (keyp != NULL)
329 *keyp = NULL;
Damien Millerc1583312010-08-05 13:04:50 +1000330
331 switch (type) {
Damien Miller1f0311c2014-05-15 14:24:09 +1000332#ifdef WITH_OPENSSL
Damien Millerc1583312010-08-05 13:04:50 +1000333 case KEY_RSA:
334 case KEY_DSA:
Damien Millereb8b60e2010-08-31 22:41:14 +1000335 case KEY_ECDSA:
Damien Miller86687062014-07-02 15:28:02 +1000336#endif /* WITH_OPENSSL */
markus@openbsd.org16db0a72015-07-09 09:49:46 +0000337 case KEY_ED25519:
markus@openbsd.org1b11ea72018-02-23 15:58:37 +0000338 case KEY_XMSS:
Damien Miller86687062014-07-02 15:28:02 +1000339 case KEY_UNSPEC:
Damien Millerc1583312010-08-05 13:04:50 +1000340 break;
341 default:
Damien Miller86687062014-07-02 15:28:02 +1000342 return SSH_ERR_KEY_TYPE_UNKNOWN;
Damien Millerc1583312010-08-05 13:04:50 +1000343 }
344
Damien Miller86687062014-07-02 15:28:02 +1000345 if ((r = sshkey_load_private_type(type, filename,
dtucker@openbsd.org6b39a7b2019-08-05 11:50:33 +0000346 passphrase, &key, NULL)) != 0 ||
Damien Miller86687062014-07-02 15:28:02 +1000347 (r = sshkey_load_cert(filename, &cert)) != 0)
348 goto out;
Damien Millerc1583312010-08-05 13:04:50 +1000349
350 /* Make sure the private key matches the certificate */
Damien Miller86687062014-07-02 15:28:02 +1000351 if (sshkey_equal_public(key, cert) == 0) {
352 r = SSH_ERR_KEY_CERT_MISMATCH;
353 goto out;
Damien Millerc1583312010-08-05 13:04:50 +1000354 }
355
djm@openbsd.orgc28fc622015-07-03 03:43:18 +0000356 if ((r = sshkey_to_certified(key)) != 0 ||
Damien Miller86687062014-07-02 15:28:02 +1000357 (r = sshkey_cert_copy(cert, key)) != 0)
358 goto out;
359 r = 0;
djm@openbsd.orgdce19bf2016-04-09 12:39:30 +0000360 if (keyp != NULL) {
361 *keyp = key;
362 key = NULL;
363 }
Damien Miller86687062014-07-02 15:28:02 +1000364 out:
mmcc@openbsd.org89540b62015-12-11 02:31:47 +0000365 sshkey_free(key);
366 sshkey_free(cert);
Damien Miller86687062014-07-02 15:28:02 +1000367 return r;
Damien Millerc1583312010-08-05 13:04:50 +1000368}
369
Damien Miller1aed65e2010-03-04 21:53:35 +1100370/*
Damien Miller86687062014-07-02 15:28:02 +1000371 * Returns success if the specified "key" is listed in the file "filename",
372 * SSH_ERR_KEY_NOT_FOUND: if the key is not listed or another error.
djm@openbsd.org5e39a492014-12-04 02:24:32 +0000373 * If "strict_type" is set then the key type must match exactly,
Damien Miller1aed65e2010-03-04 21:53:35 +1100374 * otherwise a comparison that ignores certficiate data is performed.
djm@openbsd.org5e39a492014-12-04 02:24:32 +0000375 * If "check_ca" is set and "key" is a certificate, then its CA key is
376 * also checked and sshkey_in_file() will return success if either is found.
Damien Miller1aed65e2010-03-04 21:53:35 +1100377 */
378int
djm@openbsd.org5e39a492014-12-04 02:24:32 +0000379sshkey_in_file(struct sshkey *key, const char *filename, int strict_type,
380 int check_ca)
Damien Miller1aed65e2010-03-04 21:53:35 +1100381{
382 FILE *f;
markus@openbsd.org7f906352018-06-06 18:29:18 +0000383 char *line = NULL, *cp;
384 size_t linesize = 0;
Damien Miller86687062014-07-02 15:28:02 +1000385 int r = 0;
386 struct sshkey *pub = NULL;
markus@openbsd.org7f906352018-06-06 18:29:18 +0000387
Damien Miller86687062014-07-02 15:28:02 +1000388 int (*sshkey_compare)(const struct sshkey *, const struct sshkey *) =
389 strict_type ? sshkey_equal : sshkey_equal_public;
Damien Miller1aed65e2010-03-04 21:53:35 +1100390
djm@openbsd.org5e39a492014-12-04 02:24:32 +0000391 if ((f = fopen(filename, "r")) == NULL)
392 return SSH_ERR_SYSTEM_ERROR;
Damien Miller1aed65e2010-03-04 21:53:35 +1100393
markus@openbsd.org7f906352018-06-06 18:29:18 +0000394 while (getline(&line, &linesize, f) != -1) {
djm@openbsd.orgbbc8af72018-09-21 12:20:12 +0000395 sshkey_free(pub);
396 pub = NULL;
Damien Miller1aed65e2010-03-04 21:53:35 +1100397 cp = line;
398
399 /* Skip leading whitespace. */
400 for (; *cp && (*cp == ' ' || *cp == '\t'); cp++)
401 ;
402
403 /* Skip comments and empty lines */
404 switch (*cp) {
405 case '#':
406 case '\n':
407 case '\0':
408 continue;
409 }
410
Damien Miller86687062014-07-02 15:28:02 +1000411 if ((pub = sshkey_new(KEY_UNSPEC)) == NULL) {
412 r = SSH_ERR_ALLOC_FAIL;
413 goto out;
Damien Miller1aed65e2010-03-04 21:53:35 +1100414 }
djm@openbsd.orgbbc8af72018-09-21 12:20:12 +0000415 switch (r = sshkey_read(pub, &cp)) {
416 case 0:
417 break;
418 case SSH_ERR_KEY_LENGTH:
419 continue;
420 default:
Damien Miller86687062014-07-02 15:28:02 +1000421 goto out;
djm@openbsd.orgbbc8af72018-09-21 12:20:12 +0000422 }
djm@openbsd.org5e39a492014-12-04 02:24:32 +0000423 if (sshkey_compare(key, pub) ||
424 (check_ca && sshkey_is_cert(key) &&
425 sshkey_compare(key->cert->signature_key, pub))) {
Damien Miller86687062014-07-02 15:28:02 +1000426 r = 0;
427 goto out;
Damien Miller1aed65e2010-03-04 21:53:35 +1100428 }
Damien Miller1aed65e2010-03-04 21:53:35 +1100429 }
Damien Miller86687062014-07-02 15:28:02 +1000430 r = SSH_ERR_KEY_NOT_FOUND;
431 out:
markus@openbsd.org7f906352018-06-06 18:29:18 +0000432 free(line);
mmcc@openbsd.org89540b62015-12-11 02:31:47 +0000433 sshkey_free(pub);
Damien Miller1aed65e2010-03-04 21:53:35 +1100434 fclose(f);
Damien Miller86687062014-07-02 15:28:02 +1000435 return r;
Damien Miller1aed65e2010-03-04 21:53:35 +1100436}
Damien Miller86687062014-07-02 15:28:02 +1000437
djm@openbsd.org5e39a492014-12-04 02:24:32 +0000438/*
439 * Checks whether the specified key is revoked, returning 0 if not,
440 * SSH_ERR_KEY_REVOKED if it is or another error code if something
441 * unexpected happened.
442 * This will check both the key and, if it is a certificate, its CA key too.
443 * "revoked_keys_file" may be a KRL or a one-per-line list of public keys.
444 */
445int
446sshkey_check_revoked(struct sshkey *key, const char *revoked_keys_file)
447{
448 int r;
449
djm@openbsd.org5e39a492014-12-04 02:24:32 +0000450 r = ssh_krl_file_contains_key(revoked_keys_file, key);
451 /* If this was not a KRL to begin with then continue below */
452 if (r != SSH_ERR_KRL_BAD_MAGIC)
453 return r;
djm@openbsd.org5e39a492014-12-04 02:24:32 +0000454
455 /*
456 * If the file is not a KRL or we can't handle KRLs then attempt to
457 * parse the file as a flat list of keys.
458 */
459 switch ((r = sshkey_in_file(key, revoked_keys_file, 0, 1))) {
460 case 0:
461 /* Key found => revoked */
462 return SSH_ERR_KEY_REVOKED;
463 case SSH_ERR_KEY_NOT_FOUND:
464 /* Key not found => not revoked */
465 return 0;
466 default:
467 /* Some other error occurred */
468 return r;
469 }
470}
471
djm@openbsd.orgdd8002f2019-09-03 08:30:47 +0000472/*
473 * Advanced *cpp past the end of key options, defined as the first unquoted
474 * whitespace character. Returns 0 on success or -1 on failure (e.g.
475 * unterminated quotes).
476 */
477int
478sshkey_advance_past_options(char **cpp)
479{
480 char *cp = *cpp;
481 int quoted = 0;
482
483 for (; *cp && (quoted || (*cp != ' ' && *cp != '\t')); cp++) {
484 if (*cp == '\\' && cp[1] == '"')
485 cp++; /* Skip both */
486 else if (*cp == '"')
487 quoted = !quoted;
488 }
489 *cpp = cp;
490 /* return failure for unterminated quotes */
491 return (*cp == '\0' && quoted) ? -1 : 0;
492}
493
djm@openbsd.org878ba432020-01-02 22:38:33 +0000494/* Save a public key */
495int
496sshkey_save_public(const struct sshkey *key, const char *path,
497 const char *comment)
498{
499 int fd, oerrno;
500 FILE *f = NULL;
501 int r = SSH_ERR_INTERNAL_ERROR;
502
503 if ((fd = open(path, O_WRONLY|O_CREAT|O_TRUNC, 0644)) == -1)
504 return SSH_ERR_SYSTEM_ERROR;
505 if ((f = fdopen(fd, "w")) == NULL) {
506 r = SSH_ERR_SYSTEM_ERROR;
507 goto fail;
508 }
509 if ((r = sshkey_write(key, f)) != 0)
510 goto fail;
511 fprintf(f, " %s\n", comment);
512 if (ferror(f) || fclose(f) != 0) {
513 r = SSH_ERR_SYSTEM_ERROR;
514 fail:
515 oerrno = errno;
516 if (f != NULL)
517 fclose(f);
518 else
519 close(fd);
520 errno = oerrno;
521 return r;
522 }
523 return 0;
524}