2020-06-19 01:33:38 +02:00
|
|
|
/* $OpenBSD: authfile.c,v 1.141 2020/06/18 23:33:38 djm Exp $ */
|
1999-10-27 05:42:43 +02:00
|
|
|
/*
|
2013-12-07 00:41:55 +01:00
|
|
|
* Copyright (c) 2000, 2013 Markus Friedl. All rights reserved.
|
2000-09-16 04:29:08 +02:00
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|
|
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
|
|
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|
|
|
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|
|
|
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|
|
|
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
1999-11-24 14:26:21 +01:00
|
|
|
*/
|
1999-10-27 05:42:43 +02:00
|
|
|
|
|
|
|
#include "includes.h"
|
2006-03-15 01:45:54 +01:00
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
2006-08-05 04:39:39 +02:00
|
|
|
#include <sys/uio.h>
|
1999-10-27 05:42:43 +02:00
|
|
|
|
2006-07-12 14:24:22 +02:00
|
|
|
#include <errno.h>
|
2006-07-10 13:13:46 +02:00
|
|
|
#include <fcntl.h>
|
2006-08-05 03:37:59 +02:00
|
|
|
#include <stdio.h>
|
2014-07-02 07:28:02 +02:00
|
|
|
#include <stdarg.h>
|
2006-08-05 03:34:19 +02:00
|
|
|
#include <stdlib.h>
|
2006-07-24 06:13:33 +02:00
|
|
|
#include <string.h>
|
2006-07-24 06:01:23 +02:00
|
|
|
#include <unistd.h>
|
2015-01-21 00:14:00 +01:00
|
|
|
#include <limits.h>
|
2006-07-10 13:13:46 +02:00
|
|
|
|
2006-08-05 04:39:39 +02:00
|
|
|
#include "cipher.h"
|
2001-01-22 06:34:40 +01:00
|
|
|
#include "ssh.h"
|
|
|
|
#include "log.h"
|
2001-02-09 03:11:24 +01:00
|
|
|
#include "authfile.h"
|
2004-12-11 03:39:50 +01:00
|
|
|
#include "misc.h"
|
2005-06-17 04:59:34 +02:00
|
|
|
#include "atomicio.h"
|
2015-07-09 11:49:46 +02:00
|
|
|
#include "sshkey.h"
|
2014-07-02 07:28:02 +02:00
|
|
|
#include "sshbuf.h"
|
|
|
|
#include "ssherr.h"
|
2014-12-04 03:24:32 +01:00
|
|
|
#include "krl.h"
|
1999-10-27 05:42:43 +02:00
|
|
|
|
2011-05-05 06:17:18 +02:00
|
|
|
#define MAX_KEY_FILE_SIZE (1024 * 1024)
|
|
|
|
|
2010-12-01 02:01:21 +01:00
|
|
|
/* Save a key blob to a file */
|
|
|
|
static int
|
2014-07-02 07:28:02 +02:00
|
|
|
sshkey_save_private_blob(struct sshbuf *keybuf, const char *filename)
|
2010-12-01 02:01:21 +01:00
|
|
|
{
|
2020-01-26 00:02:13 +01:00
|
|
|
int r;
|
|
|
|
mode_t omask;
|
2010-12-01 02:01:21 +01:00
|
|
|
|
2020-01-26 00:02:13 +01:00
|
|
|
omask = umask(077);
|
|
|
|
r = sshbuf_write_file(filename, keybuf);
|
|
|
|
umask(omask);
|
|
|
|
return r;
|
2010-12-01 02:01:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2014-07-02 07:28:02 +02:00
|
|
|
sshkey_save_private(struct sshkey *key, const char *filename,
|
|
|
|
const char *passphrase, const char *comment,
|
2019-07-15 15:16:29 +02:00
|
|
|
int format, const char *openssh_format_cipher, int openssh_format_rounds)
|
2010-12-01 02:01:21 +01:00
|
|
|
{
|
2014-07-02 07:28:02 +02:00
|
|
|
struct sshbuf *keyblob = NULL;
|
|
|
|
int r;
|
2010-12-01 02:01:21 +01:00
|
|
|
|
2014-07-02 07:28:02 +02:00
|
|
|
if ((keyblob = sshbuf_new()) == NULL)
|
|
|
|
return SSH_ERR_ALLOC_FAIL;
|
|
|
|
if ((r = sshkey_private_to_fileblob(key, keyblob, passphrase, comment,
|
2019-07-15 15:16:29 +02:00
|
|
|
format, openssh_format_cipher, openssh_format_rounds)) != 0)
|
2010-12-01 02:01:21 +01:00
|
|
|
goto out;
|
2014-07-02 07:28:02 +02:00
|
|
|
if ((r = sshkey_save_private_blob(keyblob, filename)) != 0)
|
2010-12-01 02:01:21 +01:00
|
|
|
goto out;
|
2014-07-02 07:28:02 +02:00
|
|
|
r = 0;
|
2010-12-01 02:01:21 +01:00
|
|
|
out:
|
2014-07-02 07:28:02 +02:00
|
|
|
sshbuf_free(keyblob);
|
|
|
|
return r;
|
2010-12-01 02:01:21 +01:00
|
|
|
}
|
|
|
|
|
2014-07-02 07:28:02 +02:00
|
|
|
/* XXX remove error() calls from here? */
|
2006-03-15 02:06:23 +01:00
|
|
|
int
|
2014-07-02 07:28:02 +02:00
|
|
|
sshkey_perm_ok(int fd, const char *filename)
|
2000-04-29 15:57:08 +02:00
|
|
|
{
|
|
|
|
struct stat st;
|
|
|
|
|
2019-06-28 15:35:04 +02:00
|
|
|
if (fstat(fd, &st) == -1)
|
2014-07-02 07:28:02 +02:00
|
|
|
return SSH_ERR_SYSTEM_ERROR;
|
2001-09-23 15:53:22 +02:00
|
|
|
/*
|
|
|
|
* if a key owned by the user is accessed, then we check the
|
|
|
|
* permissions of the file. if the key owned by a different user,
|
|
|
|
* then we don't care.
|
|
|
|
*/
|
2000-09-16 07:25:12 +02:00
|
|
|
#ifdef HAVE_CYGWIN
|
2000-09-29 03:12:36 +02:00
|
|
|
if (check_ntsec(filename))
|
2000-09-16 07:25:12 +02:00
|
|
|
#endif
|
2001-09-23 15:53:22 +02:00
|
|
|
if ((st.st_uid == getuid()) && (st.st_mode & 077) != 0) {
|
2000-04-29 15:57:08 +02:00
|
|
|
error("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
|
|
|
|
error("@ WARNING: UNPROTECTED PRIVATE KEY FILE! @");
|
|
|
|
error("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
|
2001-09-23 15:53:22 +02:00
|
|
|
error("Permissions 0%3.3o for '%s' are too open.",
|
2003-05-25 06:38:33 +02:00
|
|
|
(u_int)st.st_mode & 0777, filename);
|
2015-04-17 15:32:09 +02:00
|
|
|
error("It is required that your private key files are NOT accessible by others.");
|
2001-03-26 15:44:06 +02:00
|
|
|
error("This private key will be ignored.");
|
2014-07-02 07:28:02 +02:00
|
|
|
return SSH_ERR_KEY_BAD_PERMISSIONS;
|
2000-04-29 15:57:08 +02:00
|
|
|
}
|
2014-07-02 07:28:02 +02:00
|
|
|
return 0;
|
2001-03-26 15:44:06 +02:00
|
|
|
}
|
2001-03-05 05:59:27 +01:00
|
|
|
|
2014-07-02 07:28:02 +02:00
|
|
|
int
|
|
|
|
sshkey_load_private_type(int type, const char *filename, const char *passphrase,
|
2019-08-05 13:50:33 +02:00
|
|
|
struct sshkey **keyp, char **commentp)
|
2010-12-01 02:01:21 +01:00
|
|
|
{
|
2014-07-02 07:28:02 +02:00
|
|
|
int fd, r;
|
2013-12-07 00:41:55 +01:00
|
|
|
|
2016-04-09 14:39:30 +02:00
|
|
|
if (keyp != NULL)
|
|
|
|
*keyp = NULL;
|
2014-07-02 07:28:02 +02:00
|
|
|
if (commentp != NULL)
|
|
|
|
*commentp = NULL;
|
2001-03-26 15:44:06 +02:00
|
|
|
|
2019-08-05 13:50:33 +02:00
|
|
|
if ((fd = open(filename, O_RDONLY)) == -1)
|
2014-07-02 07:28:02 +02:00
|
|
|
return SSH_ERR_SYSTEM_ERROR;
|
2019-08-05 13:50:33 +02:00
|
|
|
|
|
|
|
r = sshkey_perm_ok(fd, filename);
|
|
|
|
if (r != 0)
|
2014-07-02 07:28:02 +02:00
|
|
|
goto out;
|
2010-12-01 02:01:21 +01:00
|
|
|
|
2015-01-08 11:14:08 +01:00
|
|
|
r = sshkey_load_private_type_fd(fd, type, passphrase, keyp, commentp);
|
2018-02-23 16:58:37 +01:00
|
|
|
if (r == 0 && keyp && *keyp)
|
|
|
|
r = sshkey_set_filename(*keyp, filename);
|
2015-01-08 11:14:08 +01:00
|
|
|
out:
|
|
|
|
close(fd);
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2020-04-17 09:15:11 +02:00
|
|
|
int
|
|
|
|
sshkey_load_private(const char *filename, const char *passphrase,
|
|
|
|
struct sshkey **keyp, char **commentp)
|
|
|
|
{
|
|
|
|
return sshkey_load_private_type(KEY_UNSPEC, filename, passphrase,
|
|
|
|
keyp, commentp);
|
|
|
|
}
|
|
|
|
|
2015-01-08 11:14:08 +01:00
|
|
|
int
|
|
|
|
sshkey_load_private_type_fd(int fd, int type, const char *passphrase,
|
|
|
|
struct sshkey **keyp, char **commentp)
|
|
|
|
{
|
|
|
|
struct sshbuf *buffer = NULL;
|
|
|
|
int r;
|
|
|
|
|
2016-04-09 14:39:30 +02:00
|
|
|
if (keyp != NULL)
|
|
|
|
*keyp = NULL;
|
2020-01-26 00:02:13 +01:00
|
|
|
if ((r = sshbuf_load_fd(fd, &buffer)) != 0 ||
|
2015-01-08 11:14:08 +01:00
|
|
|
(r = sshkey_parse_private_fileblob_type(buffer, type,
|
|
|
|
passphrase, keyp, commentp)) != 0)
|
2014-07-02 07:28:02 +02:00
|
|
|
goto out;
|
2015-01-08 11:14:08 +01:00
|
|
|
|
|
|
|
/* success */
|
2014-07-02 07:28:02 +02:00
|
|
|
r = 0;
|
|
|
|
out:
|
2015-12-11 05:21:11 +01:00
|
|
|
sshbuf_free(buffer);
|
2014-07-02 07:28:02 +02:00
|
|
|
return r;
|
2001-03-26 15:44:06 +02:00
|
|
|
}
|
|
|
|
|
2020-04-08 02:10:37 +02:00
|
|
|
/* Load a pubkey from the unencrypted envelope of a new-format private key */
|
|
|
|
static int
|
|
|
|
sshkey_load_pubkey_from_private(const char *filename, struct sshkey **pubkeyp)
|
|
|
|
{
|
|
|
|
struct sshbuf *buffer = NULL;
|
|
|
|
struct sshkey *pubkey = NULL;
|
|
|
|
int r, fd;
|
|
|
|
|
|
|
|
if (pubkeyp != NULL)
|
|
|
|
*pubkeyp = NULL;
|
|
|
|
|
|
|
|
if ((fd = open(filename, O_RDONLY)) == -1)
|
|
|
|
return SSH_ERR_SYSTEM_ERROR;
|
|
|
|
if ((r = sshbuf_load_fd(fd, &buffer)) != 0 ||
|
|
|
|
(r = sshkey_parse_pubkey_from_private_fileblob_type(buffer,
|
|
|
|
KEY_UNSPEC, &pubkey)) != 0)
|
|
|
|
goto out;
|
|
|
|
if ((r = sshkey_set_filename(pubkey, filename)) != 0)
|
|
|
|
goto out;
|
|
|
|
/* success */
|
|
|
|
if (pubkeyp != NULL) {
|
|
|
|
*pubkeyp = pubkey;
|
|
|
|
pubkey = NULL;
|
|
|
|
}
|
|
|
|
r = 0;
|
|
|
|
out:
|
|
|
|
close(fd);
|
|
|
|
sshbuf_free(buffer);
|
|
|
|
sshkey_free(pubkey);
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2001-06-25 07:01:22 +02:00
|
|
|
static int
|
2020-04-08 02:09:24 +02:00
|
|
|
sshkey_try_load_public(struct sshkey **kp, const char *filename,
|
|
|
|
char **commentp)
|
2000-09-16 04:29:08 +02:00
|
|
|
{
|
|
|
|
FILE *f;
|
2018-06-06 20:29:18 +02:00
|
|
|
char *line = NULL, *cp;
|
|
|
|
size_t linesize = 0;
|
2014-07-02 07:28:02 +02:00
|
|
|
int r;
|
2020-04-08 02:09:24 +02:00
|
|
|
struct sshkey *k = NULL;
|
2000-09-16 04:29:08 +02:00
|
|
|
|
2020-04-08 02:09:24 +02:00
|
|
|
*kp = NULL;
|
2014-07-02 07:28:02 +02:00
|
|
|
if (commentp != NULL)
|
|
|
|
*commentp = NULL;
|
|
|
|
if ((f = fopen(filename, "r")) == NULL)
|
|
|
|
return SSH_ERR_SYSTEM_ERROR;
|
2020-04-08 02:09:24 +02:00
|
|
|
if ((k = sshkey_new(KEY_UNSPEC)) == NULL) {
|
|
|
|
fclose(f);
|
|
|
|
return SSH_ERR_ALLOC_FAIL;
|
|
|
|
}
|
2018-06-06 20:29:18 +02:00
|
|
|
while (getline(&line, &linesize, f) != -1) {
|
2014-07-02 07:28:02 +02:00
|
|
|
cp = line;
|
|
|
|
switch (*cp) {
|
|
|
|
case '#':
|
|
|
|
case '\n':
|
|
|
|
case '\0':
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
/* Abort loading if this looks like a private key */
|
|
|
|
if (strncmp(cp, "-----BEGIN", 10) == 0 ||
|
|
|
|
strcmp(cp, "SSH PRIVATE KEY FILE") == 0)
|
|
|
|
break;
|
|
|
|
/* Skip leading whitespace. */
|
|
|
|
for (; *cp && (*cp == ' ' || *cp == '\t'); cp++)
|
|
|
|
;
|
|
|
|
if (*cp) {
|
|
|
|
if ((r = sshkey_read(k, &cp)) == 0) {
|
|
|
|
cp[strcspn(cp, "\r\n")] = '\0';
|
|
|
|
if (commentp) {
|
|
|
|
*commentp = strdup(*cp ?
|
|
|
|
cp : filename);
|
|
|
|
if (*commentp == NULL)
|
|
|
|
r = SSH_ERR_ALLOC_FAIL;
|
2000-09-16 04:29:08 +02:00
|
|
|
}
|
2020-04-08 02:09:24 +02:00
|
|
|
/* success */
|
|
|
|
*kp = k;
|
2018-06-06 20:29:18 +02:00
|
|
|
free(line);
|
2014-07-02 07:28:02 +02:00
|
|
|
fclose(f);
|
|
|
|
return r;
|
2000-09-16 04:29:08 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-04-08 02:09:24 +02:00
|
|
|
free(k);
|
2018-06-06 20:29:18 +02:00
|
|
|
free(line);
|
2014-07-02 07:28:02 +02:00
|
|
|
fclose(f);
|
|
|
|
return SSH_ERR_INVALID_FORMAT;
|
2000-09-16 04:29:08 +02:00
|
|
|
}
|
|
|
|
|
2017-05-30 10:49:32 +02:00
|
|
|
/* load public key from any pubkey file */
|
2014-07-02 07:28:02 +02:00
|
|
|
int
|
|
|
|
sshkey_load_public(const char *filename, struct sshkey **keyp, char **commentp)
|
2000-09-16 04:29:08 +02:00
|
|
|
{
|
2020-04-08 02:09:24 +02:00
|
|
|
char *pubfile = NULL;
|
2020-06-19 01:33:38 +02:00
|
|
|
int r, oerrno;
|
2014-07-02 07:28:02 +02:00
|
|
|
|
|
|
|
if (keyp != NULL)
|
|
|
|
*keyp = NULL;
|
|
|
|
if (commentp != NULL)
|
|
|
|
*commentp = NULL;
|
2001-03-26 15:44:06 +02:00
|
|
|
|
2020-04-08 02:09:24 +02:00
|
|
|
if ((r = sshkey_try_load_public(keyp, filename, commentp)) == 0)
|
2017-05-30 10:49:32 +02:00
|
|
|
goto out;
|
2003-05-14 05:45:22 +02:00
|
|
|
|
2014-07-02 07:28:02 +02:00
|
|
|
/* try .pub suffix */
|
2020-04-08 02:09:24 +02:00
|
|
|
if (asprintf(&pubfile, "%s.pub", filename) == -1)
|
2014-07-02 07:28:02 +02:00
|
|
|
return SSH_ERR_ALLOC_FAIL;
|
2020-04-08 02:09:24 +02:00
|
|
|
if ((r = sshkey_try_load_public(keyp, pubfile, commentp)) == 0)
|
2017-05-30 10:49:32 +02:00
|
|
|
goto out;
|
2020-04-08 02:09:24 +02:00
|
|
|
|
2020-04-08 02:10:37 +02:00
|
|
|
/* finally, try to extract public key from private key file */
|
|
|
|
if ((r = sshkey_load_pubkey_from_private(filename, keyp)) == 0)
|
|
|
|
goto out;
|
|
|
|
|
2020-06-19 01:33:38 +02:00
|
|
|
/* Pretend we couldn't find the key */
|
|
|
|
r = SSH_ERR_SYSTEM_ERROR;
|
|
|
|
errno = ENOENT;
|
|
|
|
|
2017-05-30 10:49:32 +02:00
|
|
|
out:
|
2020-06-19 01:33:38 +02:00
|
|
|
oerrno = errno;
|
2020-04-08 02:09:24 +02:00
|
|
|
free(pubfile);
|
2020-06-19 01:33:38 +02:00
|
|
|
errno = oerrno;
|
2014-07-02 07:28:02 +02:00
|
|
|
return r;
|
2000-09-16 04:29:08 +02:00
|
|
|
}
|
2010-03-04 11:53:35 +01:00
|
|
|
|
2010-08-05 05:04:50 +02:00
|
|
|
/* Load the certificate associated with the named private key */
|
2014-07-02 07:28:02 +02:00
|
|
|
int
|
|
|
|
sshkey_load_cert(const char *filename, struct sshkey **keyp)
|
2010-08-05 05:04:50 +02:00
|
|
|
{
|
2014-07-02 07:28:02 +02:00
|
|
|
struct sshkey *pub = NULL;
|
|
|
|
char *file = NULL;
|
|
|
|
int r = SSH_ERR_INTERNAL_ERROR;
|
2010-08-05 05:04:50 +02:00
|
|
|
|
2016-04-09 14:39:30 +02:00
|
|
|
if (keyp != NULL)
|
|
|
|
*keyp = NULL;
|
2014-07-02 07:28:02 +02:00
|
|
|
|
|
|
|
if (asprintf(&file, "%s-cert.pub", filename) == -1)
|
|
|
|
return SSH_ERR_ALLOC_FAIL;
|
|
|
|
|
2020-04-08 02:09:24 +02:00
|
|
|
r = sshkey_try_load_public(keyp, file, NULL);
|
2015-12-10 18:08:40 +01:00
|
|
|
free(file);
|
2015-12-11 03:31:47 +01:00
|
|
|
sshkey_free(pub);
|
2014-07-02 07:28:02 +02:00
|
|
|
return r;
|
2010-08-05 05:04:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Load private key and certificate */
|
2014-07-02 07:28:02 +02:00
|
|
|
int
|
|
|
|
sshkey_load_private_cert(int type, const char *filename, const char *passphrase,
|
2019-08-05 13:50:33 +02:00
|
|
|
struct sshkey **keyp)
|
2010-08-05 05:04:50 +02:00
|
|
|
{
|
2014-07-02 07:28:02 +02:00
|
|
|
struct sshkey *key = NULL, *cert = NULL;
|
|
|
|
int r;
|
|
|
|
|
2016-04-09 14:39:30 +02:00
|
|
|
if (keyp != NULL)
|
|
|
|
*keyp = NULL;
|
2010-08-05 05:04:50 +02:00
|
|
|
|
|
|
|
switch (type) {
|
2014-05-15 06:24:09 +02:00
|
|
|
#ifdef WITH_OPENSSL
|
2010-08-05 05:04:50 +02:00
|
|
|
case KEY_RSA:
|
|
|
|
case KEY_DSA:
|
2010-08-31 14:41:14 +02:00
|
|
|
case KEY_ECDSA:
|
2014-07-02 07:28:02 +02:00
|
|
|
#endif /* WITH_OPENSSL */
|
2015-07-09 11:49:46 +02:00
|
|
|
case KEY_ED25519:
|
2018-02-23 16:58:37 +01:00
|
|
|
case KEY_XMSS:
|
2014-07-02 07:28:02 +02:00
|
|
|
case KEY_UNSPEC:
|
2010-08-05 05:04:50 +02:00
|
|
|
break;
|
|
|
|
default:
|
2014-07-02 07:28:02 +02:00
|
|
|
return SSH_ERR_KEY_TYPE_UNKNOWN;
|
2010-08-05 05:04:50 +02:00
|
|
|
}
|
|
|
|
|
2014-07-02 07:28:02 +02:00
|
|
|
if ((r = sshkey_load_private_type(type, filename,
|
2019-08-05 13:50:33 +02:00
|
|
|
passphrase, &key, NULL)) != 0 ||
|
2014-07-02 07:28:02 +02:00
|
|
|
(r = sshkey_load_cert(filename, &cert)) != 0)
|
|
|
|
goto out;
|
2010-08-05 05:04:50 +02:00
|
|
|
|
|
|
|
/* Make sure the private key matches the certificate */
|
2014-07-02 07:28:02 +02:00
|
|
|
if (sshkey_equal_public(key, cert) == 0) {
|
|
|
|
r = SSH_ERR_KEY_CERT_MISMATCH;
|
|
|
|
goto out;
|
2010-08-05 05:04:50 +02:00
|
|
|
}
|
|
|
|
|
2015-07-03 05:43:18 +02:00
|
|
|
if ((r = sshkey_to_certified(key)) != 0 ||
|
2014-07-02 07:28:02 +02:00
|
|
|
(r = sshkey_cert_copy(cert, key)) != 0)
|
|
|
|
goto out;
|
|
|
|
r = 0;
|
2016-04-09 14:39:30 +02:00
|
|
|
if (keyp != NULL) {
|
|
|
|
*keyp = key;
|
|
|
|
key = NULL;
|
|
|
|
}
|
2014-07-02 07:28:02 +02:00
|
|
|
out:
|
2015-12-11 03:31:47 +01:00
|
|
|
sshkey_free(key);
|
|
|
|
sshkey_free(cert);
|
2014-07-02 07:28:02 +02:00
|
|
|
return r;
|
2010-08-05 05:04:50 +02:00
|
|
|
}
|
|
|
|
|
2010-03-04 11:53:35 +01:00
|
|
|
/*
|
2014-07-02 07:28:02 +02:00
|
|
|
* Returns success if the specified "key" is listed in the file "filename",
|
|
|
|
* SSH_ERR_KEY_NOT_FOUND: if the key is not listed or another error.
|
2014-12-04 03:24:32 +01:00
|
|
|
* If "strict_type" is set then the key type must match exactly,
|
2010-03-04 11:53:35 +01:00
|
|
|
* otherwise a comparison that ignores certficiate data is performed.
|
2014-12-04 03:24:32 +01:00
|
|
|
* If "check_ca" is set and "key" is a certificate, then its CA key is
|
|
|
|
* also checked and sshkey_in_file() will return success if either is found.
|
2010-03-04 11:53:35 +01:00
|
|
|
*/
|
|
|
|
int
|
2014-12-04 03:24:32 +01:00
|
|
|
sshkey_in_file(struct sshkey *key, const char *filename, int strict_type,
|
|
|
|
int check_ca)
|
2010-03-04 11:53:35 +01:00
|
|
|
{
|
|
|
|
FILE *f;
|
2018-06-06 20:29:18 +02:00
|
|
|
char *line = NULL, *cp;
|
|
|
|
size_t linesize = 0;
|
2014-07-02 07:28:02 +02:00
|
|
|
int r = 0;
|
|
|
|
struct sshkey *pub = NULL;
|
2018-06-06 20:29:18 +02:00
|
|
|
|
2014-07-02 07:28:02 +02:00
|
|
|
int (*sshkey_compare)(const struct sshkey *, const struct sshkey *) =
|
|
|
|
strict_type ? sshkey_equal : sshkey_equal_public;
|
2010-03-04 11:53:35 +01:00
|
|
|
|
2014-12-04 03:24:32 +01:00
|
|
|
if ((f = fopen(filename, "r")) == NULL)
|
|
|
|
return SSH_ERR_SYSTEM_ERROR;
|
2010-03-04 11:53:35 +01:00
|
|
|
|
2018-06-06 20:29:18 +02:00
|
|
|
while (getline(&line, &linesize, f) != -1) {
|
2018-09-21 14:20:12 +02:00
|
|
|
sshkey_free(pub);
|
|
|
|
pub = NULL;
|
2010-03-04 11:53:35 +01:00
|
|
|
cp = line;
|
|
|
|
|
|
|
|
/* Skip leading whitespace. */
|
|
|
|
for (; *cp && (*cp == ' ' || *cp == '\t'); cp++)
|
|
|
|
;
|
|
|
|
|
|
|
|
/* Skip comments and empty lines */
|
|
|
|
switch (*cp) {
|
|
|
|
case '#':
|
|
|
|
case '\n':
|
|
|
|
case '\0':
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2014-07-02 07:28:02 +02:00
|
|
|
if ((pub = sshkey_new(KEY_UNSPEC)) == NULL) {
|
|
|
|
r = SSH_ERR_ALLOC_FAIL;
|
|
|
|
goto out;
|
2010-03-04 11:53:35 +01:00
|
|
|
}
|
2018-09-21 14:20:12 +02:00
|
|
|
switch (r = sshkey_read(pub, &cp)) {
|
|
|
|
case 0:
|
|
|
|
break;
|
|
|
|
case SSH_ERR_KEY_LENGTH:
|
|
|
|
continue;
|
|
|
|
default:
|
2014-07-02 07:28:02 +02:00
|
|
|
goto out;
|
2018-09-21 14:20:12 +02:00
|
|
|
}
|
2014-12-04 03:24:32 +01:00
|
|
|
if (sshkey_compare(key, pub) ||
|
|
|
|
(check_ca && sshkey_is_cert(key) &&
|
|
|
|
sshkey_compare(key->cert->signature_key, pub))) {
|
2014-07-02 07:28:02 +02:00
|
|
|
r = 0;
|
|
|
|
goto out;
|
2010-03-04 11:53:35 +01:00
|
|
|
}
|
|
|
|
}
|
2014-07-02 07:28:02 +02:00
|
|
|
r = SSH_ERR_KEY_NOT_FOUND;
|
|
|
|
out:
|
2018-06-06 20:29:18 +02:00
|
|
|
free(line);
|
2015-12-11 03:31:47 +01:00
|
|
|
sshkey_free(pub);
|
2010-03-04 11:53:35 +01:00
|
|
|
fclose(f);
|
2014-07-02 07:28:02 +02:00
|
|
|
return r;
|
2010-03-04 11:53:35 +01:00
|
|
|
}
|
2014-07-02 07:28:02 +02:00
|
|
|
|
2014-12-04 03:24:32 +01:00
|
|
|
/*
|
|
|
|
* Checks whether the specified key is revoked, returning 0 if not,
|
|
|
|
* SSH_ERR_KEY_REVOKED if it is or another error code if something
|
|
|
|
* unexpected happened.
|
|
|
|
* This will check both the key and, if it is a certificate, its CA key too.
|
|
|
|
* "revoked_keys_file" may be a KRL or a one-per-line list of public keys.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
sshkey_check_revoked(struct sshkey *key, const char *revoked_keys_file)
|
|
|
|
{
|
|
|
|
int r;
|
|
|
|
|
|
|
|
r = ssh_krl_file_contains_key(revoked_keys_file, key);
|
|
|
|
/* If this was not a KRL to begin with then continue below */
|
|
|
|
if (r != SSH_ERR_KRL_BAD_MAGIC)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If the file is not a KRL or we can't handle KRLs then attempt to
|
|
|
|
* parse the file as a flat list of keys.
|
|
|
|
*/
|
|
|
|
switch ((r = sshkey_in_file(key, revoked_keys_file, 0, 1))) {
|
|
|
|
case 0:
|
|
|
|
/* Key found => revoked */
|
|
|
|
return SSH_ERR_KEY_REVOKED;
|
|
|
|
case SSH_ERR_KEY_NOT_FOUND:
|
|
|
|
/* Key not found => not revoked */
|
|
|
|
return 0;
|
|
|
|
default:
|
|
|
|
/* Some other error occurred */
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-03 10:30:47 +02:00
|
|
|
/*
|
|
|
|
* Advanced *cpp past the end of key options, defined as the first unquoted
|
|
|
|
* whitespace character. Returns 0 on success or -1 on failure (e.g.
|
|
|
|
* unterminated quotes).
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
sshkey_advance_past_options(char **cpp)
|
|
|
|
{
|
|
|
|
char *cp = *cpp;
|
|
|
|
int quoted = 0;
|
|
|
|
|
|
|
|
for (; *cp && (quoted || (*cp != ' ' && *cp != '\t')); cp++) {
|
|
|
|
if (*cp == '\\' && cp[1] == '"')
|
|
|
|
cp++; /* Skip both */
|
|
|
|
else if (*cp == '"')
|
|
|
|
quoted = !quoted;
|
|
|
|
}
|
|
|
|
*cpp = cp;
|
|
|
|
/* return failure for unterminated quotes */
|
|
|
|
return (*cp == '\0' && quoted) ? -1 : 0;
|
|
|
|
}
|
|
|
|
|
2020-01-02 23:38:33 +01:00
|
|
|
/* Save a public key */
|
|
|
|
int
|
|
|
|
sshkey_save_public(const struct sshkey *key, const char *path,
|
|
|
|
const char *comment)
|
|
|
|
{
|
|
|
|
int fd, oerrno;
|
|
|
|
FILE *f = NULL;
|
|
|
|
int r = SSH_ERR_INTERNAL_ERROR;
|
|
|
|
|
|
|
|
if ((fd = open(path, O_WRONLY|O_CREAT|O_TRUNC, 0644)) == -1)
|
|
|
|
return SSH_ERR_SYSTEM_ERROR;
|
|
|
|
if ((f = fdopen(fd, "w")) == NULL) {
|
|
|
|
r = SSH_ERR_SYSTEM_ERROR;
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
if ((r = sshkey_write(key, f)) != 0)
|
|
|
|
goto fail;
|
|
|
|
fprintf(f, " %s\n", comment);
|
|
|
|
if (ferror(f) || fclose(f) != 0) {
|
|
|
|
r = SSH_ERR_SYSTEM_ERROR;
|
|
|
|
fail:
|
|
|
|
oerrno = errno;
|
|
|
|
if (f != NULL)
|
|
|
|
fclose(f);
|
|
|
|
else
|
|
|
|
close(fd);
|
|
|
|
errno = oerrno;
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|