blob: 95877e1596482848d736909cf2c030bcfd4eca2c [file] [log] [blame]
djm@openbsd.org5e39a492014-12-04 02:24:32 +00001/* $OpenBSD: authfile.c,v 1.108 2014/12/04 02:24:32 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 Miller8dbffe72006-08-05 11:02:17 +100030#include <sys/param.h>
Damien Millerd7834352006-08-05 12:39:39 +100031#include <sys/uio.h>
Damien Millerd4a8b7e1999-10-27 13:42:43 +100032
Darren Tuckerba724052006-07-12 22:24:22 +100033#include <errno.h>
Damien Miller57cf6382006-07-10 21:13:46 +100034#include <fcntl.h>
Damien Millera7a73ee2006-08-05 11:37:59 +100035#include <stdio.h>
Damien Miller86687062014-07-02 15:28:02 +100036#include <stdarg.h>
Damien Millere7a1e5c2006-08-05 11:34:19 +100037#include <stdlib.h>
Damien Millere3476ed2006-07-24 14:13:33 +100038#include <string.h>
Damien Millere6b3b612006-07-24 14:01:23 +100039#include <unistd.h>
Damien Miller57cf6382006-07-10 21:13:46 +100040
Damien Millerd7834352006-08-05 12:39:39 +100041#include "cipher.h"
Damien Millereba71ba2000-04-29 23:57:08 +100042#include "key.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000043#include "ssh.h"
44#include "log.h"
Ben Lindstrom31ca54a2001-02-09 02:11:24 +000045#include "authfile.h"
Damien Miller040b64f2002-01-22 23:10:04 +110046#include "rsa.h"
Darren Tuckerf0f90982004-12-11 13:39:50 +110047#include "misc.h"
Damien Millereccb9de2005-06-17 12:59:34 +100048#include "atomicio.h"
Damien Miller86687062014-07-02 15:28:02 +100049#include "sshbuf.h"
50#include "ssherr.h"
djm@openbsd.org5e39a492014-12-04 02:24:32 +000051#include "krl.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100052
Damien Miller2ce12ef2011-05-05 14:17:18 +100053#define MAX_KEY_FILE_SIZE (1024 * 1024)
54
Damien Millera2327922010-12-01 12:01:21 +110055/* Save a key blob to a file */
56static int
Damien Miller86687062014-07-02 15:28:02 +100057sshkey_save_private_blob(struct sshbuf *keybuf, const char *filename)
Damien Millera2327922010-12-01 12:01:21 +110058{
Damien Miller86687062014-07-02 15:28:02 +100059 int fd, oerrno;
Damien Millera2327922010-12-01 12:01:21 +110060
Damien Miller86687062014-07-02 15:28:02 +100061 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)) {
65 oerrno = errno;
Damien Millera2327922010-12-01 12:01:21 +110066 close(fd);
67 unlink(filename);
Damien Miller86687062014-07-02 15:28:02 +100068 errno = oerrno;
69 return SSH_ERR_SYSTEM_ERROR;
Damien Millera2327922010-12-01 12:01:21 +110070 }
71 close(fd);
Damien Miller86687062014-07-02 15:28:02 +100072 return 0;
Damien Millera2327922010-12-01 12:01:21 +110073}
74
Damien Millereba71ba2000-04-29 23:57:08 +100075int
Damien Miller86687062014-07-02 15:28:02 +100076sshkey_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)
Damien Millereba71ba2000-04-29 23:57:08 +100079{
Damien Miller86687062014-07-02 15:28:02 +100080 struct sshbuf *keyblob = NULL;
81 int r;
Damien Millera2327922010-12-01 12:01:21 +110082
Damien Miller86687062014-07-02 15:28:02 +100083 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)
Damien Millera2327922010-12-01 12:01:21 +110087 goto out;
Damien Miller86687062014-07-02 15:28:02 +100088 if ((r = sshkey_save_private_blob(keyblob, filename)) != 0)
Damien Millera2327922010-12-01 12:01:21 +110089 goto out;
Damien Miller86687062014-07-02 15:28:02 +100090 r = 0;
Damien Millera2327922010-12-01 12:01:21 +110091 out:
Damien Miller86687062014-07-02 15:28:02 +100092 sshbuf_free(keyblob);
93 return r;
Damien Millera2327922010-12-01 12:01:21 +110094}
95
Damien Miller2ce12ef2011-05-05 14:17:18 +100096/* Load a key from a fd into a buffer */
97int
Damien Miller86687062014-07-02 15:28:02 +100098sshkey_load_file(int fd, const char *filename, struct sshbuf *blob)
Damien Millera2327922010-12-01 12:01:21 +110099{
Damien Miller2ce12ef2011-05-05 14:17:18 +1000100 u_char buf[1024];
Damien Millera2327922010-12-01 12:01:21 +1100101 size_t len;
Damien Millera2327922010-12-01 12:01:21 +1100102 struct stat st;
Damien Miller86687062014-07-02 15:28:02 +1000103 int r;
Damien Millera2327922010-12-01 12:01:21 +1100104
Damien Miller86687062014-07-02 15:28:02 +1000105 if (fstat(fd, &st) < 0)
106 return SSH_ERR_SYSTEM_ERROR;
Damien Miller2ce12ef2011-05-05 14:17:18 +1000107 if ((st.st_mode & (S_IFSOCK|S_IFCHR|S_IFIFO)) == 0 &&
Damien Miller86687062014-07-02 15:28:02 +1000108 st.st_size > MAX_KEY_FILE_SIZE)
109 return SSH_ERR_INVALID_FORMAT;
Damien Miller2ce12ef2011-05-05 14:17:18 +1000110 for (;;) {
111 if ((len = atomicio(read, fd, buf, sizeof(buf))) == 0) {
112 if (errno == EPIPE)
113 break;
Damien Miller86687062014-07-02 15:28:02 +1000114 r = SSH_ERR_SYSTEM_ERROR;
115 goto out;
Damien Miller2ce12ef2011-05-05 14:17:18 +1000116 }
Damien Miller86687062014-07-02 15:28:02 +1000117 if ((r = sshbuf_put(blob, buf, len)) != 0)
118 goto out;
119 if (sshbuf_len(blob) > MAX_KEY_FILE_SIZE) {
120 r = SSH_ERR_INVALID_FORMAT;
121 goto out;
Damien Miller2ce12ef2011-05-05 14:17:18 +1000122 }
123 }
Damien Miller2ce12ef2011-05-05 14:17:18 +1000124 if ((st.st_mode & (S_IFSOCK|S_IFCHR|S_IFIFO)) == 0 &&
Damien Miller86687062014-07-02 15:28:02 +1000125 st.st_size != (off_t)sshbuf_len(blob)) {
126 r = SSH_ERR_FILE_CHANGED;
127 goto out;
Damien Millera2327922010-12-01 12:01:21 +1100128 }
Damien Miller86687062014-07-02 15:28:02 +1000129 r = 0;
Damien Miller2ce12ef2011-05-05 14:17:18 +1000130
Damien Miller86687062014-07-02 15:28:02 +1000131 out:
132 explicit_bzero(buf, sizeof(buf));
133 if (r != 0)
134 sshbuf_reset(blob);
135 return r;
Damien Millereba71ba2000-04-29 23:57:08 +1000136}
137
Damien Miller1f0311c2014-05-15 14:24:09 +1000138#ifdef WITH_SSH1
Damien Miller5428f641999-11-25 11:54:57 +1100139/*
Ben Lindstromd0fca422001-03-26 13:44:06 +0000140 * 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
Damien Miller5428f641999-11-25 11:54:57 +1100142 * otherwise.
143 */
Damien Miller86687062014-07-02 15:28:02 +1000144static int
145sshkey_load_public_rsa1(int fd, const char *filename,
146 struct sshkey **keyp, char **commentp)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000147{
Damien Miller86687062014-07-02 15:28:02 +1000148 struct sshbuf *b = NULL;
149 int r;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000150
Damien Miller86687062014-07-02 15:28:02 +1000151 *keyp = NULL;
Darren Tuckera627d422013-06-02 07:31:17 +1000152 if (commentp != NULL)
Damien Miller86687062014-07-02 15:28:02 +1000153 *commentp = NULL;
154
155 if ((b = sshbuf_new()) == NULL)
156 return SSH_ERR_ALLOC_FAIL;
157 if ((r = sshkey_load_file(fd, filename, b)) != 0)
158 goto out;
159 if ((r = sshkey_parse_public_rsa1_fileblob(b, keyp, commentp)) != 0)
160 goto out;
161 r = 0;
162 out:
163 sshbuf_free(b);
164 return r;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000165}
Damien Miller86687062014-07-02 15:28:02 +1000166#endif /* WITH_SSH1 */
Damien Millereba71ba2000-04-29 23:57:08 +1000167
Damien Miller1f0311c2014-05-15 14:24:09 +1000168#ifdef WITH_OPENSSL
Damien Miller86687062014-07-02 15:28:02 +1000169/* XXX Deprecate? */
Damien Miller8275fad2006-03-15 12:06:23 +1100170int
Damien Miller86687062014-07-02 15:28:02 +1000171sshkey_load_private_pem(int fd, int type, const char *passphrase,
172 struct sshkey **keyp, char **commentp)
173{
174 struct sshbuf *buffer = NULL;
175 int r;
176
177 *keyp = NULL;
178 if (commentp != NULL)
179 *commentp = NULL;
180
181 if ((buffer = sshbuf_new()) == NULL)
182 return SSH_ERR_ALLOC_FAIL;
183 if ((r = sshkey_load_file(fd, NULL, buffer)) != 0)
184 goto out;
185 if ((r = sshkey_parse_private_pem_fileblob(buffer, type, passphrase,
186 keyp, commentp)) != 0)
187 goto out;
188 r = 0;
189 out:
190 sshbuf_free(buffer);
191 return r;
192}
193#endif /* WITH_OPENSSL */
194
195/* XXX remove error() calls from here? */
196int
197sshkey_perm_ok(int fd, const char *filename)
Damien Millereba71ba2000-04-29 23:57:08 +1000198{
Damien Millereba71ba2000-04-29 23:57:08 +1000199 struct stat st;
200
Ben Lindstrom7aff2612001-09-23 13:53:22 +0000201 if (fstat(fd, &st) < 0)
Damien Miller86687062014-07-02 15:28:02 +1000202 return SSH_ERR_SYSTEM_ERROR;
Ben Lindstrom7aff2612001-09-23 13:53:22 +0000203 /*
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.
207 */
Damien Millerb70b61f2000-09-16 16:25:12 +1100208#ifdef HAVE_CYGWIN
Damien Millercb5e44a2000-09-29 12:12:36 +1100209 if (check_ntsec(filename))
Damien Millerb70b61f2000-09-16 16:25:12 +1100210#endif
Ben Lindstrom7aff2612001-09-23 13:53:22 +0000211 if ((st.st_uid == getuid()) && (st.st_mode & 077) != 0) {
Damien Millereba71ba2000-04-29 23:57:08 +1000212 error("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
213 error("@ WARNING: UNPROTECTED PRIVATE KEY FILE! @");
214 error("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
Ben Lindstrom7aff2612001-09-23 13:53:22 +0000215 error("Permissions 0%3.3o for '%s' are too open.",
Damien Miller04bd8b02003-05-25 14:38:33 +1000216 (u_int)st.st_mode & 0777, filename);
Damien Miller86687062014-07-02 15:28:02 +1000217 error("It is recommended that your private key files are NOT accessible by others.");
Ben Lindstromd0fca422001-03-26 13:44:06 +0000218 error("This private key will be ignored.");
Damien Miller86687062014-07-02 15:28:02 +1000219 return SSH_ERR_KEY_BAD_PERMISSIONS;
Damien Millere4340be2000-09-16 13:29:08 +1100220 }
221 return 0;
222}
223
Damien Miller86687062014-07-02 15:28:02 +1000224/* XXX kill perm_ok now that we have SSH_ERR_KEY_BAD_PERMISSIONS? */
225int
226sshkey_load_private_type(int type, const char *filename, const char *passphrase,
227 struct sshkey **keyp, char **commentp, int *perm_ok)
Damien Millere4340be2000-09-16 13:29:08 +1100228{
Damien Miller86687062014-07-02 15:28:02 +1000229 int fd, r;
230 struct sshbuf *buffer = NULL;
Damien Millere4340be2000-09-16 13:29:08 +1100231
Damien Miller86687062014-07-02 15:28:02 +1000232 *keyp = NULL;
233 if (commentp != NULL)
234 *commentp = NULL;
235
236 if ((fd = open(filename, O_RDONLY)) < 0) {
237 if (perm_ok != NULL)
238 *perm_ok = 0;
239 return SSH_ERR_SYSTEM_ERROR;
240 }
241 if (sshkey_perm_ok(fd, filename) != 0) {
242 if (perm_ok != NULL)
243 *perm_ok = 0;
244 r = SSH_ERR_KEY_BAD_PERMISSIONS;
245 goto out;
246 }
247 if (perm_ok != NULL)
248 *perm_ok = 1;
249
250 if ((buffer = sshbuf_new()) == NULL) {
251 r = SSH_ERR_ALLOC_FAIL;
252 goto out;
253 }
254 if ((r = sshkey_load_file(fd, filename, buffer)) != 0)
255 goto out;
256 if ((r = sshkey_parse_private_fileblob_type(buffer, type, passphrase,
257 keyp, commentp)) != 0)
258 goto out;
259 r = 0;
260 out:
261 close(fd);
262 if (buffer != NULL)
263 sshbuf_free(buffer);
264 return r;
265}
266
267/* XXX this is almost identical to sshkey_load_private_type() */
268int
269sshkey_load_private(const char *filename, const char *passphrase,
270 struct sshkey **keyp, char **commentp)
271{
272 struct sshbuf *buffer = NULL;
273 int r, fd;
274
275 *keyp = NULL;
276 if (commentp != NULL)
277 *commentp = NULL;
278
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;
283 goto out;
284 }
285
286 if ((buffer = sshbuf_new()) == NULL) {
287 r = SSH_ERR_ALLOC_FAIL;
288 goto out;
289 }
290 if ((r = sshkey_load_file(fd, filename, buffer)) != 0 ||
291 (r = sshkey_parse_private_fileblob(buffer, passphrase, filename,
292 keyp, commentp)) != 0)
293 goto out;
294 r = 0;
295 out:
296 close(fd);
297 if (buffer != NULL)
298 sshbuf_free(buffer);
299 return r;
300}
301
302static int
303sshkey_try_load_public(struct sshkey *k, const char *filename, char **commentp)
304{
305 FILE *f;
306 char line[SSH_MAX_PUBKEY_BYTES];
307 char *cp;
308 u_long linenum = 0;
309 int r;
310
311 if (commentp != NULL)
312 *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),
316 &linenum) != -1) {
317 cp = line;
318 switch (*cp) {
319 case '#':
320 case '\n':
321 case '\0':
322 continue;
323 }
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)
327 break;
328 /* Skip leading whitespace. */
329 for (; *cp && (*cp == ' ' || *cp == '\t'); cp++)
330 ;
331 if (*cp) {
332 if ((r = sshkey_read(k, &cp)) == 0) {
333 cp[strcspn(cp, "\r\n")] = '\0';
334 if (commentp) {
335 *commentp = strdup(*cp ?
336 cp : filename);
337 if (*commentp == NULL)
338 r = SSH_ERR_ALLOC_FAIL;
339 }
340 fclose(f);
341 return r;
342 }
343 }
344 }
345 fclose(f);
346 return SSH_ERR_INVALID_FORMAT;
347}
348
349/* load public key from ssh v1 private or any pubkey file */
350int
351sshkey_load_public(const char *filename, struct sshkey **keyp, char **commentp)
352{
353 struct sshkey *pub = NULL;
354 char file[MAXPATHLEN];
355 int r, fd;
356
357 if (keyp != NULL)
358 *keyp = NULL;
359 if (commentp != NULL)
360 *commentp = NULL;
361
362 if ((fd = open(filename, O_RDONLY)) < 0)
363 goto skip;
Damien Miller1f0311c2014-05-15 14:24:09 +1000364#ifdef WITH_SSH1
Damien Millerdb274722003-05-14 13:45:22 +1000365 /* try rsa1 private key */
Damien Miller86687062014-07-02 15:28:02 +1000366 r = sshkey_load_public_rsa1(fd, filename, keyp, commentp);
367 close(fd);
368 switch (r) {
369 case SSH_ERR_INTERNAL_ERROR:
370 case SSH_ERR_ALLOC_FAIL:
371 case SSH_ERR_INVALID_ARGUMENT:
372 case SSH_ERR_SYSTEM_ERROR:
373 case 0:
374 return r;
375 }
376#endif /* WITH_SSH1 */
Damien Millerdb274722003-05-14 13:45:22 +1000377
378 /* try ssh2 public key */
Damien Miller86687062014-07-02 15:28:02 +1000379 if ((pub = sshkey_new(KEY_UNSPEC)) == NULL)
380 return SSH_ERR_ALLOC_FAIL;
381 if ((r = sshkey_try_load_public(pub, filename, commentp)) == 0) {
382 if (keyp != NULL)
383 *keyp = pub;
384 return 0;
385 }
386 sshkey_free(pub);
387
388#ifdef WITH_SSH1
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) {
393 if (keyp != NULL)
394 *keyp = pub;
395 return 0;
396 }
397 sshkey_free(pub);
398#endif /* WITH_SSH1 */
399
400 skip:
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 */
Ben Lindstromd0fca422001-03-26 13:44:06 +0000405 if ((strlcpy(file, filename, sizeof file) < sizeof(file)) &&
406 (strlcat(file, ".pub", sizeof file) < sizeof(file)) &&
Damien Miller86687062014-07-02 15:28:02 +1000407 (r = sshkey_try_load_public(pub, file, commentp)) == 0) {
408 if (keyp != NULL)
409 *keyp = pub;
410 return 0;
411 }
412 sshkey_free(pub);
413 return r;
Damien Millere4340be2000-09-16 13:29:08 +1100414}
Damien Miller1aed65e2010-03-04 21:53:35 +1100415
Damien Millerc1583312010-08-05 13:04:50 +1000416/* Load the certificate associated with the named private key */
Damien Miller86687062014-07-02 15:28:02 +1000417int
418sshkey_load_cert(const char *filename, struct sshkey **keyp)
Damien Millerc1583312010-08-05 13:04:50 +1000419{
Damien Miller86687062014-07-02 15:28:02 +1000420 struct sshkey *pub = NULL;
421 char *file = NULL;
422 int r = SSH_ERR_INTERNAL_ERROR;
Damien Millerc1583312010-08-05 13:04:50 +1000423
Damien Miller86687062014-07-02 15:28:02 +1000424 *keyp = NULL;
425
426 if (asprintf(&file, "%s-cert.pub", filename) == -1)
427 return SSH_ERR_ALLOC_FAIL;
428
429 if ((pub = sshkey_new(KEY_UNSPEC)) == NULL) {
430 goto out;
Damien Miller5458c4d2010-08-05 13:05:15 +1000431 }
Damien Miller86687062014-07-02 15:28:02 +1000432 if ((r = sshkey_try_load_public(pub, file, NULL)) != 0)
433 goto out;
434
435 *keyp = pub;
436 pub = NULL;
437 r = 0;
438
439 out:
440 if (file != NULL)
441 free(file);
442 if (pub != NULL)
443 sshkey_free(pub);
444 return r;
Damien Millerc1583312010-08-05 13:04:50 +1000445}
446
447/* Load private key and certificate */
Damien Miller86687062014-07-02 15:28:02 +1000448int
449sshkey_load_private_cert(int type, const char *filename, const char *passphrase,
450 struct sshkey **keyp, int *perm_ok)
Damien Millerc1583312010-08-05 13:04:50 +1000451{
Damien Miller86687062014-07-02 15:28:02 +1000452 struct sshkey *key = NULL, *cert = NULL;
453 int r;
454
455 *keyp = NULL;
Damien Millerc1583312010-08-05 13:04:50 +1000456
457 switch (type) {
Damien Miller1f0311c2014-05-15 14:24:09 +1000458#ifdef WITH_OPENSSL
Damien Millerc1583312010-08-05 13:04:50 +1000459 case KEY_RSA:
460 case KEY_DSA:
Damien Millereb8b60e2010-08-31 22:41:14 +1000461 case KEY_ECDSA:
Damien Millerb9a95492013-12-29 17:50:15 +1100462 case KEY_ED25519:
Damien Miller86687062014-07-02 15:28:02 +1000463#endif /* WITH_OPENSSL */
464 case KEY_UNSPEC:
Damien Millerc1583312010-08-05 13:04:50 +1000465 break;
466 default:
Damien Miller86687062014-07-02 15:28:02 +1000467 return SSH_ERR_KEY_TYPE_UNKNOWN;
Damien Millerc1583312010-08-05 13:04:50 +1000468 }
469
Damien Miller86687062014-07-02 15:28:02 +1000470 if ((r = sshkey_load_private_type(type, filename,
471 passphrase, &key, NULL, perm_ok)) != 0 ||
472 (r = sshkey_load_cert(filename, &cert)) != 0)
473 goto out;
Damien Millerc1583312010-08-05 13:04:50 +1000474
475 /* Make sure the private key matches the certificate */
Damien Miller86687062014-07-02 15:28:02 +1000476 if (sshkey_equal_public(key, cert) == 0) {
477 r = SSH_ERR_KEY_CERT_MISMATCH;
478 goto out;
Damien Millerc1583312010-08-05 13:04:50 +1000479 }
480
Damien Miller86687062014-07-02 15:28:02 +1000481 if ((r = sshkey_to_certified(key, sshkey_cert_is_legacy(cert))) != 0 ||
482 (r = sshkey_cert_copy(cert, key)) != 0)
483 goto out;
484 r = 0;
485 *keyp = key;
486 key = NULL;
487 out:
488 if (key != NULL)
489 sshkey_free(key);
490 if (cert != NULL)
491 sshkey_free(cert);
492 return r;
Damien Millerc1583312010-08-05 13:04:50 +1000493}
494
Damien Miller1aed65e2010-03-04 21:53:35 +1100495/*
Damien Miller86687062014-07-02 15:28:02 +1000496 * Returns success if the specified "key" is listed in the file "filename",
497 * SSH_ERR_KEY_NOT_FOUND: if the key is not listed or another error.
djm@openbsd.org5e39a492014-12-04 02:24:32 +0000498 * If "strict_type" is set then the key type must match exactly,
Damien Miller1aed65e2010-03-04 21:53:35 +1100499 * otherwise a comparison that ignores certficiate data is performed.
djm@openbsd.org5e39a492014-12-04 02:24:32 +0000500 * If "check_ca" is set and "key" is a certificate, then its CA key is
501 * also checked and sshkey_in_file() will return success if either is found.
Damien Miller1aed65e2010-03-04 21:53:35 +1100502 */
503int
djm@openbsd.org5e39a492014-12-04 02:24:32 +0000504sshkey_in_file(struct sshkey *key, const char *filename, int strict_type,
505 int check_ca)
Damien Miller1aed65e2010-03-04 21:53:35 +1100506{
507 FILE *f;
508 char line[SSH_MAX_PUBKEY_BYTES];
509 char *cp;
510 u_long linenum = 0;
Damien Miller86687062014-07-02 15:28:02 +1000511 int r = 0;
512 struct sshkey *pub = NULL;
513 int (*sshkey_compare)(const struct sshkey *, const struct sshkey *) =
514 strict_type ? sshkey_equal : sshkey_equal_public;
Damien Miller1aed65e2010-03-04 21:53:35 +1100515
djm@openbsd.org5e39a492014-12-04 02:24:32 +0000516 if ((f = fopen(filename, "r")) == NULL)
517 return SSH_ERR_SYSTEM_ERROR;
Damien Miller1aed65e2010-03-04 21:53:35 +1100518
519 while (read_keyfile_line(f, filename, line, sizeof(line),
Damien Miller86687062014-07-02 15:28:02 +1000520 &linenum) != -1) {
Damien Miller1aed65e2010-03-04 21:53:35 +1100521 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 Miller86687062014-07-02 15:28:02 +1000535 if ((pub = sshkey_new(KEY_UNSPEC)) == NULL) {
536 r = SSH_ERR_ALLOC_FAIL;
537 goto out;
Damien Miller1aed65e2010-03-04 21:53:35 +1100538 }
Damien Miller86687062014-07-02 15:28:02 +1000539 if ((r = sshkey_read(pub, &cp)) != 0)
540 goto out;
djm@openbsd.org5e39a492014-12-04 02:24:32 +0000541 if (sshkey_compare(key, pub) ||
542 (check_ca && sshkey_is_cert(key) &&
543 sshkey_compare(key->cert->signature_key, pub))) {
Damien Miller86687062014-07-02 15:28:02 +1000544 r = 0;
545 goto out;
Damien Miller1aed65e2010-03-04 21:53:35 +1100546 }
Damien Miller86687062014-07-02 15:28:02 +1000547 sshkey_free(pub);
548 pub = NULL;
Damien Miller1aed65e2010-03-04 21:53:35 +1100549 }
Damien Miller86687062014-07-02 15:28:02 +1000550 r = SSH_ERR_KEY_NOT_FOUND;
551 out:
552 if (pub != NULL)
553 sshkey_free(pub);
Damien Miller1aed65e2010-03-04 21:53:35 +1100554 fclose(f);
Damien Miller86687062014-07-02 15:28:02 +1000555 return r;
Damien Miller1aed65e2010-03-04 21:53:35 +1100556}
Damien Miller86687062014-07-02 15:28:02 +1000557
djm@openbsd.org5e39a492014-12-04 02:24:32 +0000558/*
559 * Checks whether the specified key is revoked, returning 0 if not,
560 * SSH_ERR_KEY_REVOKED if it is or another error code if something
561 * unexpected happened.
562 * This will check both the key and, if it is a certificate, its CA key too.
563 * "revoked_keys_file" may be a KRL or a one-per-line list of public keys.
564 */
565int
566sshkey_check_revoked(struct sshkey *key, const char *revoked_keys_file)
567{
568 int r;
569
570#ifdef WITH_OPENSSL
571 r = ssh_krl_file_contains_key(revoked_keys_file, key);
572 /* If this was not a KRL to begin with then continue below */
573 if (r != SSH_ERR_KRL_BAD_MAGIC)
574 return r;
575#endif
576
577 /*
578 * If the file is not a KRL or we can't handle KRLs then attempt to
579 * parse the file as a flat list of keys.
580 */
581 switch ((r = sshkey_in_file(key, revoked_keys_file, 0, 1))) {
582 case 0:
583 /* Key found => revoked */
584 return SSH_ERR_KEY_REVOKED;
585 case SSH_ERR_KEY_NOT_FOUND:
586 /* Key not found => not revoked */
587 return 0;
588 default:
589 /* Some other error occurred */
590 return r;
591 }
592}
593