2024-08-20 11:48:30 +08:00
|
|
|
/* $OpenBSD: sshkey.c,v 1.144 2024/08/20 03:48:30 djm Exp $ */
|
2014-07-02 13:28:02 +08:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2000, 2001 Markus Friedl. All rights reserved.
|
|
|
|
* Copyright (c) 2008 Alexander von Gernler. All rights reserved.
|
|
|
|
* Copyright (c) 2010,2011 Damien Miller. All rights reserved.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "includes.h"
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
2024-08-20 11:48:30 +08:00
|
|
|
#include <sys/mman.h>
|
2014-12-22 06:27:55 +08:00
|
|
|
#include <netinet/in.h>
|
2014-07-02 13:28:02 +08:00
|
|
|
|
2015-01-14 18:46:28 +08:00
|
|
|
#ifdef WITH_OPENSSL
|
2014-07-02 13:28:02 +08:00
|
|
|
#include <openssl/evp.h>
|
|
|
|
#include <openssl/err.h>
|
|
|
|
#include <openssl/pem.h>
|
2015-01-14 18:46:28 +08:00
|
|
|
#endif
|
2014-07-02 13:28:02 +08:00
|
|
|
|
|
|
|
#include "crypto_api.h"
|
|
|
|
|
|
|
|
#include <errno.h>
|
2015-01-16 14:40:12 +08:00
|
|
|
#include <limits.h>
|
2014-07-02 13:28:02 +08:00
|
|
|
#include <stdio.h>
|
2023-08-21 12:36:46 +08:00
|
|
|
#include <stdlib.h>
|
2014-07-02 13:28:02 +08:00
|
|
|
#include <string.h>
|
2014-12-22 07:18:09 +08:00
|
|
|
#include <resolv.h>
|
2019-09-08 12:48:11 +08:00
|
|
|
#include <time.h>
|
2014-07-02 15:43:41 +08:00
|
|
|
#ifdef HAVE_UTIL_H
|
2014-07-02 13:28:02 +08:00
|
|
|
#include <util.h>
|
2014-07-02 15:43:41 +08:00
|
|
|
#endif /* HAVE_UTIL_H */
|
2014-07-02 13:28:02 +08:00
|
|
|
|
|
|
|
#include "ssh2.h"
|
|
|
|
#include "ssherr.h"
|
|
|
|
#include "misc.h"
|
|
|
|
#include "sshbuf.h"
|
|
|
|
#include "cipher.h"
|
|
|
|
#include "digest.h"
|
|
|
|
#define SSHKEY_INTERNAL
|
|
|
|
#include "sshkey.h"
|
2015-01-13 15:39:19 +08:00
|
|
|
#include "match.h"
|
2019-11-01 05:23:19 +08:00
|
|
|
#include "ssh-sk.h"
|
2014-07-02 13:28:02 +08:00
|
|
|
|
2019-05-03 11:25:18 +08:00
|
|
|
#ifdef WITH_XMSS
|
|
|
|
#include "sshkey-xmss.h"
|
2018-02-23 23:58:37 +08:00
|
|
|
#include "xmss_fast.h"
|
2019-05-03 11:25:18 +08:00
|
|
|
#endif
|
2018-02-23 23:58:37 +08:00
|
|
|
|
2018-09-13 10:13:50 +08:00
|
|
|
#include "openbsd-compat/openssl-compat.h"
|
|
|
|
|
2014-07-02 13:28:02 +08:00
|
|
|
/* openssh private key file format */
|
|
|
|
#define MARK_BEGIN "-----BEGIN OPENSSH PRIVATE KEY-----\n"
|
|
|
|
#define MARK_END "-----END OPENSSH PRIVATE KEY-----\n"
|
|
|
|
#define MARK_BEGIN_LEN (sizeof(MARK_BEGIN) - 1)
|
|
|
|
#define MARK_END_LEN (sizeof(MARK_END) - 1)
|
|
|
|
#define KDFNAME "bcrypt"
|
|
|
|
#define AUTH_MAGIC "openssh-key-v1"
|
|
|
|
#define SALT_LEN 16
|
2017-08-12 14:42:52 +08:00
|
|
|
#define DEFAULT_CIPHERNAME "aes256-ctr"
|
2023-07-28 06:23:05 +08:00
|
|
|
#define DEFAULT_ROUNDS 24
|
2014-07-02 13:28:02 +08:00
|
|
|
|
|
|
|
/* Version identification string for SSH v1 identity files. */
|
|
|
|
#define LEGACY_BEGIN "SSH PRIVATE KEY FILE FORMAT 1.1\n"
|
|
|
|
|
2019-06-21 12:21:04 +08:00
|
|
|
/*
|
|
|
|
* Constants relating to "shielding" support; protection of keys expected
|
|
|
|
* to remain in memory for long durations
|
|
|
|
*/
|
|
|
|
#define SSHKEY_SHIELD_PREKEY_LEN (16 * 1024)
|
|
|
|
#define SSHKEY_SHIELD_CIPHER "aes256-ctr" /* XXX want AES-EME* */
|
|
|
|
#define SSHKEY_SHIELD_PREKEY_HASH SSH_DIGEST_SHA512
|
|
|
|
|
|
|
|
int sshkey_private_serialize_opt(struct sshkey *key,
|
2018-02-23 23:58:37 +08:00
|
|
|
struct sshbuf *buf, enum sshkey_serialize_rep);
|
2015-01-26 10:59:11 +08:00
|
|
|
static int sshkey_from_blob_internal(struct sshbuf *buf,
|
2014-07-02 13:28:02 +08:00
|
|
|
struct sshkey **keyp, int allow_cert);
|
|
|
|
|
|
|
|
/* Supported key types */
|
2022-10-28 08:35:40 +08:00
|
|
|
extern const struct sshkey_impl sshkey_ed25519_impl;
|
|
|
|
extern const struct sshkey_impl sshkey_ed25519_cert_impl;
|
|
|
|
extern const struct sshkey_impl sshkey_ed25519_sk_impl;
|
|
|
|
extern const struct sshkey_impl sshkey_ed25519_sk_cert_impl;
|
2014-07-02 13:28:02 +08:00
|
|
|
#ifdef WITH_OPENSSL
|
|
|
|
# ifdef OPENSSL_HAS_ECC
|
2021-10-07 11:28:02 +08:00
|
|
|
# ifdef ENABLE_SK
|
2022-10-28 08:35:40 +08:00
|
|
|
extern const struct sshkey_impl sshkey_ecdsa_sk_impl;
|
|
|
|
extern const struct sshkey_impl sshkey_ecdsa_sk_cert_impl;
|
|
|
|
extern const struct sshkey_impl sshkey_ecdsa_sk_webauthn_impl;
|
2021-10-07 11:28:02 +08:00
|
|
|
# endif /* ENABLE_SK */
|
2022-10-28 08:35:40 +08:00
|
|
|
extern const struct sshkey_impl sshkey_ecdsa_nistp256_impl;
|
|
|
|
extern const struct sshkey_impl sshkey_ecdsa_nistp256_cert_impl;
|
|
|
|
extern const struct sshkey_impl sshkey_ecdsa_nistp384_impl;
|
|
|
|
extern const struct sshkey_impl sshkey_ecdsa_nistp384_cert_impl;
|
|
|
|
# ifdef OPENSSL_HAS_NISTP521
|
|
|
|
extern const struct sshkey_impl sshkey_ecdsa_nistp521_impl;
|
|
|
|
extern const struct sshkey_impl sshkey_ecdsa_nistp521_cert_impl;
|
|
|
|
# endif /* OPENSSL_HAS_NISTP521 */
|
2014-07-02 13:28:02 +08:00
|
|
|
# endif /* OPENSSL_HAS_ECC */
|
2022-10-28 08:35:40 +08:00
|
|
|
extern const struct sshkey_impl sshkey_rsa_impl;
|
|
|
|
extern const struct sshkey_impl sshkey_rsa_cert_impl;
|
|
|
|
extern const struct sshkey_impl sshkey_rsa_sha256_impl;
|
|
|
|
extern const struct sshkey_impl sshkey_rsa_sha256_cert_impl;
|
|
|
|
extern const struct sshkey_impl sshkey_rsa_sha512_impl;
|
|
|
|
extern const struct sshkey_impl sshkey_rsa_sha512_cert_impl;
|
2024-01-11 09:45:36 +08:00
|
|
|
# ifdef WITH_DSA
|
2022-10-28 08:35:40 +08:00
|
|
|
extern const struct sshkey_impl sshkey_dss_impl;
|
|
|
|
extern const struct sshkey_impl sshkey_dsa_cert_impl;
|
2024-01-11 09:45:36 +08:00
|
|
|
# endif
|
2022-10-28 08:35:40 +08:00
|
|
|
#endif /* WITH_OPENSSL */
|
|
|
|
#ifdef WITH_XMSS
|
|
|
|
extern const struct sshkey_impl sshkey_xmss_impl;
|
|
|
|
extern const struct sshkey_impl sshkey_xmss_cert_impl;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
const struct sshkey_impl * const keyimpls[] = {
|
|
|
|
&sshkey_ed25519_impl,
|
|
|
|
&sshkey_ed25519_cert_impl,
|
|
|
|
#ifdef ENABLE_SK
|
|
|
|
&sshkey_ed25519_sk_impl,
|
|
|
|
&sshkey_ed25519_sk_cert_impl,
|
|
|
|
#endif
|
|
|
|
#ifdef WITH_OPENSSL
|
2014-07-02 13:28:02 +08:00
|
|
|
# ifdef OPENSSL_HAS_ECC
|
2022-10-28 08:35:40 +08:00
|
|
|
&sshkey_ecdsa_nistp256_impl,
|
|
|
|
&sshkey_ecdsa_nistp256_cert_impl,
|
|
|
|
&sshkey_ecdsa_nistp384_impl,
|
|
|
|
&sshkey_ecdsa_nistp384_cert_impl,
|
2014-07-02 13:28:02 +08:00
|
|
|
# ifdef OPENSSL_HAS_NISTP521
|
2022-10-28 08:35:40 +08:00
|
|
|
&sshkey_ecdsa_nistp521_impl,
|
|
|
|
&sshkey_ecdsa_nistp521_cert_impl,
|
2014-07-02 13:28:02 +08:00
|
|
|
# endif /* OPENSSL_HAS_NISTP521 */
|
2021-10-07 11:28:02 +08:00
|
|
|
# ifdef ENABLE_SK
|
2022-10-28 08:35:40 +08:00
|
|
|
&sshkey_ecdsa_sk_impl,
|
|
|
|
&sshkey_ecdsa_sk_cert_impl,
|
|
|
|
&sshkey_ecdsa_sk_webauthn_impl,
|
2021-10-07 11:28:02 +08:00
|
|
|
# endif /* ENABLE_SK */
|
2014-07-02 13:28:02 +08:00
|
|
|
# endif /* OPENSSL_HAS_ECC */
|
2024-01-11 09:45:36 +08:00
|
|
|
# ifdef WITH_DSA
|
2022-10-28 08:35:40 +08:00
|
|
|
&sshkey_dss_impl,
|
|
|
|
&sshkey_dsa_cert_impl,
|
2024-01-11 09:45:36 +08:00
|
|
|
# endif
|
2022-10-28 08:35:40 +08:00
|
|
|
&sshkey_rsa_impl,
|
|
|
|
&sshkey_rsa_cert_impl,
|
|
|
|
&sshkey_rsa_sha256_impl,
|
|
|
|
&sshkey_rsa_sha256_cert_impl,
|
|
|
|
&sshkey_rsa_sha512_impl,
|
|
|
|
&sshkey_rsa_sha512_cert_impl,
|
2014-07-02 13:28:02 +08:00
|
|
|
#endif /* WITH_OPENSSL */
|
2022-10-28 08:35:40 +08:00
|
|
|
#ifdef WITH_XMSS
|
|
|
|
&sshkey_xmss_impl,
|
|
|
|
&sshkey_xmss_cert_impl,
|
|
|
|
#endif
|
|
|
|
NULL
|
2014-07-02 13:28:02 +08:00
|
|
|
};
|
|
|
|
|
2022-10-28 08:35:40 +08:00
|
|
|
static const struct sshkey_impl *
|
|
|
|
sshkey_impl_from_type(int type)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; keyimpls[i] != NULL; i++) {
|
|
|
|
if (keyimpls[i]->type == type)
|
|
|
|
return keyimpls[i];
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct sshkey_impl *
|
|
|
|
sshkey_impl_from_type_nid(int type, int nid)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; keyimpls[i] != NULL; i++) {
|
|
|
|
if (keyimpls[i]->type == type &&
|
|
|
|
(keyimpls[i]->nid == 0 || keyimpls[i]->nid == nid))
|
|
|
|
return keyimpls[i];
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2022-10-28 08:38:58 +08:00
|
|
|
static const struct sshkey_impl *
|
|
|
|
sshkey_impl_from_key(const struct sshkey *k)
|
|
|
|
{
|
|
|
|
if (k == NULL)
|
|
|
|
return NULL;
|
|
|
|
return sshkey_impl_from_type_nid(k->type, k->ecdsa_nid);
|
|
|
|
}
|
|
|
|
|
2014-07-02 13:28:02 +08:00
|
|
|
const char *
|
|
|
|
sshkey_type(const struct sshkey *k)
|
|
|
|
{
|
2022-10-28 08:35:40 +08:00
|
|
|
const struct sshkey_impl *impl;
|
2014-07-02 13:28:02 +08:00
|
|
|
|
2022-10-28 08:38:58 +08:00
|
|
|
if ((impl = sshkey_impl_from_key(k)) == NULL)
|
2022-10-28 08:35:40 +08:00
|
|
|
return "unknown";
|
|
|
|
return impl->shortname;
|
2014-07-02 13:28:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static const char *
|
|
|
|
sshkey_ssh_name_from_type_nid(int type, int nid)
|
|
|
|
{
|
2022-10-28 08:35:40 +08:00
|
|
|
const struct sshkey_impl *impl;
|
2014-07-02 13:28:02 +08:00
|
|
|
|
2022-10-28 08:35:40 +08:00
|
|
|
if ((impl = sshkey_impl_from_type_nid(type, nid)) == NULL)
|
|
|
|
return "ssh-unknown";
|
|
|
|
return impl->name;
|
2014-07-02 13:28:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
sshkey_type_is_cert(int type)
|
|
|
|
{
|
2022-10-28 08:35:40 +08:00
|
|
|
const struct sshkey_impl *impl;
|
2014-07-02 13:28:02 +08:00
|
|
|
|
2022-10-28 08:35:40 +08:00
|
|
|
if ((impl = sshkey_impl_from_type(type)) == NULL)
|
|
|
|
return 0;
|
|
|
|
return impl->cert;
|
2014-07-02 13:28:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const char *
|
|
|
|
sshkey_ssh_name(const struct sshkey *k)
|
|
|
|
{
|
|
|
|
return sshkey_ssh_name_from_type_nid(k->type, k->ecdsa_nid);
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *
|
|
|
|
sshkey_ssh_name_plain(const struct sshkey *k)
|
|
|
|
{
|
|
|
|
return sshkey_ssh_name_from_type_nid(sshkey_type_plain(k->type),
|
|
|
|
k->ecdsa_nid);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
sshkey_type_from_name(const char *name)
|
|
|
|
{
|
2022-10-28 08:35:40 +08:00
|
|
|
int i;
|
|
|
|
const struct sshkey_impl *impl;
|
2014-07-02 13:28:02 +08:00
|
|
|
|
2022-10-28 08:35:40 +08:00
|
|
|
for (i = 0; keyimpls[i] != NULL; i++) {
|
|
|
|
impl = keyimpls[i];
|
2014-07-02 13:28:02 +08:00
|
|
|
/* Only allow shortname matches for plain key types */
|
2022-10-28 08:35:40 +08:00
|
|
|
if ((impl->name != NULL && strcmp(name, impl->name) == 0) ||
|
|
|
|
(!impl->cert && strcasecmp(impl->shortname, name) == 0))
|
|
|
|
return impl->type;
|
2014-07-02 13:28:02 +08:00
|
|
|
}
|
|
|
|
return KEY_UNSPEC;
|
|
|
|
}
|
|
|
|
|
2019-11-01 05:15:14 +08:00
|
|
|
static int
|
|
|
|
key_type_is_ecdsa_variant(int type)
|
|
|
|
{
|
|
|
|
switch (type) {
|
|
|
|
case KEY_ECDSA:
|
|
|
|
case KEY_ECDSA_CERT:
|
|
|
|
case KEY_ECDSA_SK:
|
|
|
|
case KEY_ECDSA_SK_CERT:
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-07-02 13:28:02 +08:00
|
|
|
int
|
|
|
|
sshkey_ecdsa_nid_from_name(const char *name)
|
|
|
|
{
|
2022-10-28 08:35:40 +08:00
|
|
|
int i;
|
2014-07-02 13:28:02 +08:00
|
|
|
|
2022-10-28 08:35:40 +08:00
|
|
|
for (i = 0; keyimpls[i] != NULL; i++) {
|
|
|
|
if (!key_type_is_ecdsa_variant(keyimpls[i]->type))
|
2014-10-09 05:45:48 +08:00
|
|
|
continue;
|
2022-10-28 08:35:40 +08:00
|
|
|
if (keyimpls[i]->name != NULL &&
|
|
|
|
strcmp(name, keyimpls[i]->name) == 0)
|
|
|
|
return keyimpls[i]->nid;
|
2014-10-09 05:45:48 +08:00
|
|
|
}
|
2014-07-02 13:28:02 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2022-01-07 06:05:42 +08:00
|
|
|
int
|
|
|
|
sshkey_match_keyname_to_sigalgs(const char *keyname, const char *sigalgs)
|
|
|
|
{
|
|
|
|
int ktype;
|
|
|
|
|
|
|
|
if (sigalgs == NULL || *sigalgs == '\0' ||
|
|
|
|
(ktype = sshkey_type_from_name(keyname)) == KEY_UNSPEC)
|
|
|
|
return 0;
|
|
|
|
else if (ktype == KEY_RSA) {
|
|
|
|
return match_pattern_list("ssh-rsa", sigalgs, 0) == 1 ||
|
|
|
|
match_pattern_list("rsa-sha2-256", sigalgs, 0) == 1 ||
|
|
|
|
match_pattern_list("rsa-sha2-512", sigalgs, 0) == 1;
|
|
|
|
} else if (ktype == KEY_RSA_CERT) {
|
|
|
|
return match_pattern_list("ssh-rsa-cert-v01@openssh.com",
|
|
|
|
sigalgs, 0) == 1 ||
|
|
|
|
match_pattern_list("rsa-sha2-256-cert-v01@openssh.com",
|
|
|
|
sigalgs, 0) == 1 ||
|
|
|
|
match_pattern_list("rsa-sha2-512-cert-v01@openssh.com",
|
|
|
|
sigalgs, 0) == 1;
|
|
|
|
} else
|
|
|
|
return match_pattern_list(keyname, sigalgs, 0) == 1;
|
|
|
|
}
|
|
|
|
|
2014-07-02 13:28:02 +08:00
|
|
|
char *
|
2017-03-10 12:07:20 +08:00
|
|
|
sshkey_alg_list(int certs_only, int plain_only, int include_sigonly, char sep)
|
2014-07-02 13:28:02 +08:00
|
|
|
{
|
|
|
|
char *tmp, *ret = NULL;
|
2022-10-28 08:35:40 +08:00
|
|
|
size_t i, nlen, rlen = 0;
|
|
|
|
const struct sshkey_impl *impl;
|
2014-07-02 13:28:02 +08:00
|
|
|
|
2022-10-28 08:35:40 +08:00
|
|
|
for (i = 0; keyimpls[i] != NULL; i++) {
|
|
|
|
impl = keyimpls[i];
|
|
|
|
if (impl->name == NULL)
|
2017-03-10 12:07:20 +08:00
|
|
|
continue;
|
2022-10-28 08:35:40 +08:00
|
|
|
if (!include_sigonly && impl->sigonly)
|
2014-07-02 13:28:02 +08:00
|
|
|
continue;
|
2022-10-28 08:35:40 +08:00
|
|
|
if ((certs_only && !impl->cert) || (plain_only && impl->cert))
|
2014-07-02 13:28:02 +08:00
|
|
|
continue;
|
|
|
|
if (ret != NULL)
|
2016-09-13 07:31:27 +08:00
|
|
|
ret[rlen++] = sep;
|
2022-10-28 08:35:40 +08:00
|
|
|
nlen = strlen(impl->name);
|
2014-07-02 13:28:02 +08:00
|
|
|
if ((tmp = realloc(ret, rlen + nlen + 2)) == NULL) {
|
|
|
|
free(ret);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
ret = tmp;
|
2022-10-28 08:35:40 +08:00
|
|
|
memcpy(ret + rlen, impl->name, nlen + 1);
|
2014-07-02 13:28:02 +08:00
|
|
|
rlen += nlen;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2023-06-21 13:10:26 +08:00
|
|
|
sshkey_names_valid2(const char *names, int allow_wildcard, int plain_only)
|
2014-07-02 13:28:02 +08:00
|
|
|
{
|
|
|
|
char *s, *cp, *p;
|
2022-10-28 08:35:40 +08:00
|
|
|
const struct sshkey_impl *impl;
|
|
|
|
int i, type;
|
2014-07-02 13:28:02 +08:00
|
|
|
|
|
|
|
if (names == NULL || strcmp(names, "") == 0)
|
|
|
|
return 0;
|
|
|
|
if ((s = cp = strdup(names)) == NULL)
|
|
|
|
return 0;
|
|
|
|
for ((p = strsep(&cp, ",")); p && *p != '\0';
|
|
|
|
(p = strsep(&cp, ","))) {
|
2015-01-13 15:39:19 +08:00
|
|
|
type = sshkey_type_from_name(p);
|
|
|
|
if (type == KEY_UNSPEC) {
|
|
|
|
if (allow_wildcard) {
|
|
|
|
/*
|
|
|
|
* Try matching key types against the string.
|
|
|
|
* If any has a positive or negative match then
|
|
|
|
* the component is accepted.
|
|
|
|
*/
|
2022-10-28 08:35:40 +08:00
|
|
|
impl = NULL;
|
|
|
|
for (i = 0; keyimpls[i] != NULL; i++) {
|
|
|
|
if (match_pattern_list(
|
|
|
|
keyimpls[i]->name, p, 0) != 0) {
|
|
|
|
impl = keyimpls[i];
|
2015-01-13 15:39:19 +08:00
|
|
|
break;
|
2022-10-28 08:35:40 +08:00
|
|
|
}
|
2015-01-13 15:39:19 +08:00
|
|
|
}
|
2022-10-28 08:35:40 +08:00
|
|
|
if (impl != NULL)
|
2015-01-13 15:39:19 +08:00
|
|
|
continue;
|
|
|
|
}
|
2014-07-02 13:28:02 +08:00
|
|
|
free(s);
|
|
|
|
return 0;
|
2023-06-21 13:10:26 +08:00
|
|
|
} else if (plain_only && sshkey_type_is_cert(type)) {
|
|
|
|
free(s);
|
|
|
|
return 0;
|
2014-07-02 13:28:02 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
free(s);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
u_int
|
|
|
|
sshkey_size(const struct sshkey *k)
|
|
|
|
{
|
2022-10-28 08:35:40 +08:00
|
|
|
const struct sshkey_impl *impl;
|
2018-09-13 10:08:33 +08:00
|
|
|
|
2022-10-28 08:38:58 +08:00
|
|
|
if ((impl = sshkey_impl_from_key(k)) == NULL)
|
2022-10-28 08:35:40 +08:00
|
|
|
return 0;
|
|
|
|
if (impl->funcs->size != NULL)
|
|
|
|
return impl->funcs->size(k);
|
|
|
|
return impl->keybits;
|
2014-07-02 13:28:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
sshkey_type_is_valid_ca(int type)
|
|
|
|
{
|
2022-10-28 08:35:40 +08:00
|
|
|
const struct sshkey_impl *impl;
|
|
|
|
|
|
|
|
if ((impl = sshkey_impl_from_type(type)) == NULL)
|
2014-07-02 13:28:02 +08:00
|
|
|
return 0;
|
2022-10-28 08:35:40 +08:00
|
|
|
/* All non-certificate types may act as CAs */
|
|
|
|
return !impl->cert;
|
2014-07-02 13:28:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
sshkey_is_cert(const struct sshkey *k)
|
|
|
|
{
|
|
|
|
if (k == NULL)
|
|
|
|
return 0;
|
|
|
|
return sshkey_type_is_cert(k->type);
|
|
|
|
}
|
|
|
|
|
2019-11-13 03:33:08 +08:00
|
|
|
int
|
|
|
|
sshkey_is_sk(const struct sshkey *k)
|
|
|
|
{
|
|
|
|
if (k == NULL)
|
|
|
|
return 0;
|
|
|
|
switch (sshkey_type_plain(k->type)) {
|
|
|
|
case KEY_ECDSA_SK:
|
|
|
|
case KEY_ED25519_SK:
|
|
|
|
return 1;
|
|
|
|
default:
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-02 13:28:02 +08:00
|
|
|
/* Return the cert-less equivalent to a certified key type */
|
|
|
|
int
|
|
|
|
sshkey_type_plain(int type)
|
|
|
|
{
|
|
|
|
switch (type) {
|
|
|
|
case KEY_RSA_CERT:
|
|
|
|
return KEY_RSA;
|
|
|
|
case KEY_DSA_CERT:
|
|
|
|
return KEY_DSA;
|
|
|
|
case KEY_ECDSA_CERT:
|
|
|
|
return KEY_ECDSA;
|
2019-11-01 05:15:14 +08:00
|
|
|
case KEY_ECDSA_SK_CERT:
|
|
|
|
return KEY_ECDSA_SK;
|
2014-07-02 13:28:02 +08:00
|
|
|
case KEY_ED25519_CERT:
|
|
|
|
return KEY_ED25519;
|
2019-11-13 03:33:08 +08:00
|
|
|
case KEY_ED25519_SK_CERT:
|
|
|
|
return KEY_ED25519_SK;
|
2018-02-23 23:58:37 +08:00
|
|
|
case KEY_XMSS_CERT:
|
|
|
|
return KEY_XMSS;
|
2014-07-02 13:28:02 +08:00
|
|
|
default:
|
|
|
|
return type;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-28 08:43:30 +08:00
|
|
|
/* Return the cert equivalent to a plain key type */
|
|
|
|
static int
|
|
|
|
sshkey_type_certified(int type)
|
|
|
|
{
|
|
|
|
switch (type) {
|
|
|
|
case KEY_RSA:
|
|
|
|
return KEY_RSA_CERT;
|
|
|
|
case KEY_DSA:
|
|
|
|
return KEY_DSA_CERT;
|
|
|
|
case KEY_ECDSA:
|
|
|
|
return KEY_ECDSA_CERT;
|
|
|
|
case KEY_ECDSA_SK:
|
|
|
|
return KEY_ECDSA_SK_CERT;
|
|
|
|
case KEY_ED25519:
|
|
|
|
return KEY_ED25519_CERT;
|
|
|
|
case KEY_ED25519_SK:
|
|
|
|
return KEY_ED25519_SK_CERT;
|
|
|
|
case KEY_XMSS:
|
|
|
|
return KEY_XMSS_CERT;
|
|
|
|
default:
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-02 13:28:02 +08:00
|
|
|
#ifdef WITH_OPENSSL
|
2024-08-15 08:51:51 +08:00
|
|
|
static const EVP_MD *
|
|
|
|
ssh_digest_to_md(int hash_alg)
|
|
|
|
{
|
|
|
|
switch (hash_alg) {
|
|
|
|
case SSH_DIGEST_SHA1:
|
|
|
|
return EVP_sha1();
|
|
|
|
case SSH_DIGEST_SHA256:
|
|
|
|
return EVP_sha256();
|
|
|
|
case SSH_DIGEST_SHA384:
|
|
|
|
return EVP_sha384();
|
|
|
|
case SSH_DIGEST_SHA512:
|
|
|
|
return EVP_sha512();
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
sshkey_pkey_digest_sign(EVP_PKEY *pkey, int hash_alg, u_char **sigp,
|
|
|
|
size_t *lenp, const u_char *data, size_t datalen)
|
|
|
|
{
|
|
|
|
EVP_MD_CTX *ctx = NULL;
|
|
|
|
u_char *sig = NULL;
|
|
|
|
int ret;
|
|
|
|
size_t slen;
|
|
|
|
const EVP_MD *evpmd;
|
|
|
|
|
|
|
|
*sigp = NULL;
|
|
|
|
*lenp = 0;
|
|
|
|
|
|
|
|
slen = EVP_PKEY_size(pkey);
|
|
|
|
if (slen <= 0 || slen > SSHBUF_MAX_BIGNUM ||
|
|
|
|
(evpmd = ssh_digest_to_md(hash_alg)) == NULL)
|
|
|
|
return SSH_ERR_INVALID_ARGUMENT;
|
|
|
|
|
|
|
|
if ((sig = malloc(slen)) == NULL)
|
|
|
|
return SSH_ERR_ALLOC_FAIL;
|
|
|
|
|
|
|
|
if ((ctx = EVP_MD_CTX_new()) == NULL) {
|
|
|
|
ret = SSH_ERR_ALLOC_FAIL;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
if (EVP_DigestSignInit(ctx, NULL, evpmd, NULL, pkey) != 1 ||
|
|
|
|
EVP_DigestSign(ctx, sig, &slen, data, datalen) != 1) {
|
|
|
|
ret = SSH_ERR_LIBCRYPTO_ERROR;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
*sigp = sig;
|
|
|
|
*lenp = slen;
|
|
|
|
/* Now owned by the caller */
|
|
|
|
sig = NULL;
|
|
|
|
ret = 0;
|
|
|
|
|
|
|
|
out:
|
|
|
|
EVP_MD_CTX_free(ctx);
|
|
|
|
free(sig);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
sshkey_pkey_digest_verify(EVP_PKEY *pkey, int hash_alg, const u_char *data,
|
|
|
|
size_t datalen, u_char *sigbuf, size_t siglen)
|
|
|
|
{
|
|
|
|
EVP_MD_CTX *ctx = NULL;
|
|
|
|
int ret = SSH_ERR_INTERNAL_ERROR;
|
|
|
|
const EVP_MD *evpmd;
|
|
|
|
|
|
|
|
if ((evpmd = ssh_digest_to_md(hash_alg)) == NULL)
|
|
|
|
return SSH_ERR_INVALID_ARGUMENT;
|
|
|
|
if ((ctx = EVP_MD_CTX_new()) == NULL)
|
|
|
|
return SSH_ERR_ALLOC_FAIL;
|
|
|
|
if (EVP_DigestVerifyInit(ctx, NULL, evpmd, NULL, pkey) != 1) {
|
|
|
|
ret = SSH_ERR_LIBCRYPTO_ERROR;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
switch (EVP_DigestVerify(ctx, sigbuf, siglen, data, datalen)) {
|
|
|
|
case 1:
|
|
|
|
ret = 0;
|
|
|
|
break;
|
|
|
|
case 0:
|
|
|
|
ret = SSH_ERR_SIGNATURE_INVALID;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
ret = SSH_ERR_LIBCRYPTO_ERROR;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
out:
|
|
|
|
EVP_MD_CTX_free(ctx);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2014-07-02 13:28:02 +08:00
|
|
|
/* XXX: these are really begging for a table-driven approach */
|
|
|
|
int
|
|
|
|
sshkey_curve_name_to_nid(const char *name)
|
|
|
|
{
|
|
|
|
if (strcmp(name, "nistp256") == 0)
|
|
|
|
return NID_X9_62_prime256v1;
|
|
|
|
else if (strcmp(name, "nistp384") == 0)
|
|
|
|
return NID_secp384r1;
|
|
|
|
# ifdef OPENSSL_HAS_NISTP521
|
|
|
|
else if (strcmp(name, "nistp521") == 0)
|
|
|
|
return NID_secp521r1;
|
|
|
|
# endif /* OPENSSL_HAS_NISTP521 */
|
|
|
|
else
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
u_int
|
|
|
|
sshkey_curve_nid_to_bits(int nid)
|
|
|
|
{
|
|
|
|
switch (nid) {
|
|
|
|
case NID_X9_62_prime256v1:
|
|
|
|
return 256;
|
|
|
|
case NID_secp384r1:
|
|
|
|
return 384;
|
|
|
|
# ifdef OPENSSL_HAS_NISTP521
|
|
|
|
case NID_secp521r1:
|
|
|
|
return 521;
|
|
|
|
# endif /* OPENSSL_HAS_NISTP521 */
|
|
|
|
default:
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
sshkey_ecdsa_bits_to_nid(int bits)
|
|
|
|
{
|
|
|
|
switch (bits) {
|
|
|
|
case 256:
|
|
|
|
return NID_X9_62_prime256v1;
|
|
|
|
case 384:
|
|
|
|
return NID_secp384r1;
|
|
|
|
# ifdef OPENSSL_HAS_NISTP521
|
|
|
|
case 521:
|
|
|
|
return NID_secp521r1;
|
|
|
|
# endif /* OPENSSL_HAS_NISTP521 */
|
|
|
|
default:
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *
|
|
|
|
sshkey_curve_nid_to_name(int nid)
|
|
|
|
{
|
|
|
|
switch (nid) {
|
|
|
|
case NID_X9_62_prime256v1:
|
|
|
|
return "nistp256";
|
|
|
|
case NID_secp384r1:
|
|
|
|
return "nistp384";
|
|
|
|
# ifdef OPENSSL_HAS_NISTP521
|
|
|
|
case NID_secp521r1:
|
|
|
|
return "nistp521";
|
|
|
|
# endif /* OPENSSL_HAS_NISTP521 */
|
|
|
|
default:
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
sshkey_ec_nid_to_hash_alg(int nid)
|
|
|
|
{
|
|
|
|
int kbits = sshkey_curve_nid_to_bits(nid);
|
|
|
|
|
|
|
|
if (kbits <= 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
/* RFC5656 section 6.2.1 */
|
|
|
|
if (kbits <= 256)
|
|
|
|
return SSH_DIGEST_SHA256;
|
|
|
|
else if (kbits <= 384)
|
|
|
|
return SSH_DIGEST_SHA384;
|
|
|
|
else
|
|
|
|
return SSH_DIGEST_SHA512;
|
|
|
|
}
|
|
|
|
#endif /* WITH_OPENSSL */
|
|
|
|
|
|
|
|
static void
|
|
|
|
cert_free(struct sshkey_cert *cert)
|
|
|
|
{
|
|
|
|
u_int i;
|
|
|
|
|
|
|
|
if (cert == NULL)
|
|
|
|
return;
|
2015-12-11 12:21:11 +08:00
|
|
|
sshbuf_free(cert->certblob);
|
|
|
|
sshbuf_free(cert->critical);
|
|
|
|
sshbuf_free(cert->extensions);
|
2015-12-11 01:08:40 +08:00
|
|
|
free(cert->key_id);
|
2014-07-02 13:28:02 +08:00
|
|
|
for (i = 0; i < cert->nprincipals; i++)
|
|
|
|
free(cert->principals[i]);
|
2015-12-11 01:08:40 +08:00
|
|
|
free(cert->principals);
|
2015-12-11 10:31:47 +08:00
|
|
|
sshkey_free(cert->signature_key);
|
2018-09-12 09:31:30 +08:00
|
|
|
free(cert->signature_type);
|
2018-02-15 00:03:32 +08:00
|
|
|
freezero(cert, sizeof(*cert));
|
2014-07-02 13:28:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static struct sshkey_cert *
|
|
|
|
cert_new(void)
|
|
|
|
{
|
|
|
|
struct sshkey_cert *cert;
|
|
|
|
|
|
|
|
if ((cert = calloc(1, sizeof(*cert))) == NULL)
|
|
|
|
return NULL;
|
|
|
|
if ((cert->certblob = sshbuf_new()) == NULL ||
|
|
|
|
(cert->critical = sshbuf_new()) == NULL ||
|
|
|
|
(cert->extensions = sshbuf_new()) == NULL) {
|
|
|
|
cert_free(cert);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
cert->key_id = NULL;
|
|
|
|
cert->principals = NULL;
|
|
|
|
cert->signature_key = NULL;
|
2018-09-12 09:31:30 +08:00
|
|
|
cert->signature_type = NULL;
|
2014-07-02 13:28:02 +08:00
|
|
|
return cert;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct sshkey *
|
|
|
|
sshkey_new(int type)
|
|
|
|
{
|
|
|
|
struct sshkey *k;
|
2022-10-28 08:35:40 +08:00
|
|
|
const struct sshkey_impl *impl = NULL;
|
|
|
|
|
|
|
|
if (type != KEY_UNSPEC &&
|
|
|
|
(impl = sshkey_impl_from_type(type)) == NULL)
|
|
|
|
return NULL;
|
2014-07-02 13:28:02 +08:00
|
|
|
|
2022-10-28 08:35:40 +08:00
|
|
|
/* All non-certificate types may act as CAs */
|
2014-07-02 13:28:02 +08:00
|
|
|
if ((k = calloc(1, sizeof(*k))) == NULL)
|
|
|
|
return NULL;
|
|
|
|
k->type = type;
|
|
|
|
k->ecdsa_nid = -1;
|
2022-10-28 08:35:40 +08:00
|
|
|
if (impl != NULL && impl->funcs->alloc != NULL) {
|
|
|
|
if (impl->funcs->alloc(k) != 0) {
|
2014-07-02 13:28:02 +08:00
|
|
|
free(k);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (sshkey_is_cert(k)) {
|
|
|
|
if ((k->cert = cert_new()) == NULL) {
|
|
|
|
sshkey_free(k);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return k;
|
|
|
|
}
|
|
|
|
|
2022-10-28 08:36:31 +08:00
|
|
|
/* Frees common FIDO fields */
|
|
|
|
void
|
|
|
|
sshkey_sk_cleanup(struct sshkey *k)
|
|
|
|
{
|
|
|
|
free(k->sk_application);
|
|
|
|
sshbuf_free(k->sk_key_handle);
|
|
|
|
sshbuf_free(k->sk_reserved);
|
|
|
|
k->sk_application = NULL;
|
|
|
|
k->sk_key_handle = k->sk_reserved = NULL;
|
|
|
|
}
|
|
|
|
|
2024-08-20 11:48:30 +08:00
|
|
|
static int
|
|
|
|
sshkey_prekey_alloc(u_char **prekeyp, size_t len)
|
|
|
|
{
|
|
|
|
u_char *prekey;
|
|
|
|
|
|
|
|
*prekeyp = NULL;
|
|
|
|
if ((prekey = mmap(NULL, SSHKEY_SHIELD_PREKEY_LEN, PROT_READ|PROT_WRITE,
|
|
|
|
MAP_ANON|MAP_PRIVATE|MAP_CONCEAL, -1, 0)) == MAP_FAILED)
|
|
|
|
return SSH_ERR_SYSTEM_ERROR;
|
|
|
|
*prekeyp = prekey;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
sshkey_prekey_free(void *prekey, size_t len)
|
|
|
|
{
|
|
|
|
if (prekey == NULL)
|
|
|
|
return;
|
|
|
|
munmap(prekey, len);
|
|
|
|
}
|
|
|
|
|
2022-10-28 08:38:58 +08:00
|
|
|
static void
|
|
|
|
sshkey_free_contents(struct sshkey *k)
|
2014-07-02 13:28:02 +08:00
|
|
|
{
|
2022-10-28 08:35:40 +08:00
|
|
|
const struct sshkey_impl *impl;
|
|
|
|
|
2014-07-02 13:28:02 +08:00
|
|
|
if (k == NULL)
|
|
|
|
return;
|
2022-10-28 08:35:40 +08:00
|
|
|
if ((impl = sshkey_impl_from_type(k->type)) != NULL &&
|
|
|
|
impl->funcs->cleanup != NULL)
|
|
|
|
impl->funcs->cleanup(k);
|
2014-07-02 13:28:02 +08:00
|
|
|
if (sshkey_is_cert(k))
|
|
|
|
cert_free(k->cert);
|
2019-06-21 12:21:04 +08:00
|
|
|
freezero(k->shielded_private, k->shielded_len);
|
2024-08-20 11:48:30 +08:00
|
|
|
sshkey_prekey_free(k->shield_prekey, k->shield_prekey_len);
|
2014-07-02 13:28:02 +08:00
|
|
|
}
|
|
|
|
|
2022-10-28 08:38:58 +08:00
|
|
|
void
|
|
|
|
sshkey_free(struct sshkey *k)
|
|
|
|
{
|
|
|
|
sshkey_free_contents(k);
|
|
|
|
freezero(k, sizeof(*k));
|
|
|
|
}
|
|
|
|
|
2014-07-02 13:28:02 +08:00
|
|
|
static int
|
|
|
|
cert_compare(struct sshkey_cert *a, struct sshkey_cert *b)
|
|
|
|
{
|
|
|
|
if (a == NULL && b == NULL)
|
|
|
|
return 1;
|
|
|
|
if (a == NULL || b == NULL)
|
|
|
|
return 0;
|
|
|
|
if (sshbuf_len(a->certblob) != sshbuf_len(b->certblob))
|
|
|
|
return 0;
|
|
|
|
if (timingsafe_bcmp(sshbuf_ptr(a->certblob), sshbuf_ptr(b->certblob),
|
|
|
|
sshbuf_len(a->certblob)) != 0)
|
|
|
|
return 0;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2022-10-28 08:36:31 +08:00
|
|
|
/* Compares FIDO-specific pubkey fields only */
|
|
|
|
int
|
|
|
|
sshkey_sk_fields_equal(const struct sshkey *a, const struct sshkey *b)
|
|
|
|
{
|
|
|
|
if (a->sk_application == NULL || b->sk_application == NULL)
|
|
|
|
return 0;
|
|
|
|
if (strcmp(a->sk_application, b->sk_application) != 0)
|
|
|
|
return 0;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2014-07-02 13:28:02 +08:00
|
|
|
/*
|
|
|
|
* Compare public portions of key only, allowing comparisons between
|
|
|
|
* certificates and plain keys too.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
sshkey_equal_public(const struct sshkey *a, const struct sshkey *b)
|
|
|
|
{
|
2022-10-28 08:36:31 +08:00
|
|
|
const struct sshkey_impl *impl;
|
2014-07-02 13:28:02 +08:00
|
|
|
|
|
|
|
if (a == NULL || b == NULL ||
|
|
|
|
sshkey_type_plain(a->type) != sshkey_type_plain(b->type))
|
|
|
|
return 0;
|
2022-10-28 08:36:31 +08:00
|
|
|
if ((impl = sshkey_impl_from_type(a->type)) == NULL)
|
2014-07-02 13:28:02 +08:00
|
|
|
return 0;
|
2022-10-28 08:36:31 +08:00
|
|
|
return impl->funcs->equal(a, b);
|
2014-07-02 13:28:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
sshkey_equal(const struct sshkey *a, const struct sshkey *b)
|
|
|
|
{
|
|
|
|
if (a == NULL || b == NULL || a->type != b->type)
|
|
|
|
return 0;
|
|
|
|
if (sshkey_is_cert(a)) {
|
|
|
|
if (!cert_compare(a->cert, b->cert))
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return sshkey_equal_public(a, b);
|
|
|
|
}
|
|
|
|
|
2022-10-28 08:37:24 +08:00
|
|
|
|
|
|
|
/* Serialise common FIDO key parts */
|
|
|
|
int
|
|
|
|
sshkey_serialize_sk(const struct sshkey *key, struct sshbuf *b)
|
|
|
|
{
|
|
|
|
int r;
|
|
|
|
|
|
|
|
if ((r = sshbuf_put_cstring(b, key->sk_application)) != 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-07-02 13:28:02 +08:00
|
|
|
static int
|
2018-02-23 23:58:37 +08:00
|
|
|
to_blob_buf(const struct sshkey *key, struct sshbuf *b, int force_plain,
|
|
|
|
enum sshkey_serialize_rep opts)
|
2014-07-02 13:28:02 +08:00
|
|
|
{
|
|
|
|
int type, ret = SSH_ERR_INTERNAL_ERROR;
|
|
|
|
const char *typename;
|
2022-10-28 08:37:24 +08:00
|
|
|
const struct sshkey_impl *impl;
|
2014-07-02 13:28:02 +08:00
|
|
|
|
|
|
|
if (key == NULL)
|
|
|
|
return SSH_ERR_INVALID_ARGUMENT;
|
|
|
|
|
2022-10-28 08:37:24 +08:00
|
|
|
type = force_plain ? sshkey_type_plain(key->type) : key->type;
|
|
|
|
|
|
|
|
if (sshkey_type_is_cert(type)) {
|
2015-05-21 12:55:51 +08:00
|
|
|
if (key->cert == NULL)
|
|
|
|
return SSH_ERR_EXPECTED_CERT;
|
|
|
|
if (sshbuf_len(key->cert->certblob) == 0)
|
|
|
|
return SSH_ERR_KEY_LACKS_CERTBLOB;
|
2014-07-02 13:28:02 +08:00
|
|
|
/* Use the existing blob */
|
|
|
|
if ((ret = sshbuf_putb(b, key->cert->certblob)) != 0)
|
|
|
|
return ret;
|
2022-10-28 08:37:24 +08:00
|
|
|
return 0;
|
2014-07-02 13:28:02 +08:00
|
|
|
}
|
2022-10-28 08:37:24 +08:00
|
|
|
if ((impl = sshkey_impl_from_type(type)) == NULL)
|
|
|
|
return SSH_ERR_KEY_TYPE_UNKNOWN;
|
|
|
|
|
|
|
|
typename = sshkey_ssh_name_from_type_nid(type, key->ecdsa_nid);
|
2022-10-28 08:41:52 +08:00
|
|
|
if ((ret = sshbuf_put_cstring(b, typename)) != 0)
|
|
|
|
return ret;
|
|
|
|
return impl->funcs->serialize_public(key, b, opts);
|
2014-07-02 13:28:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2015-01-26 10:59:11 +08:00
|
|
|
sshkey_putb(const struct sshkey *key, struct sshbuf *b)
|
2014-07-02 13:28:02 +08:00
|
|
|
{
|
2018-02-23 23:58:37 +08:00
|
|
|
return to_blob_buf(key, b, 0, SSHKEY_SERIALIZE_DEFAULT);
|
2014-07-02 13:28:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2018-02-23 23:58:37 +08:00
|
|
|
sshkey_puts_opts(const struct sshkey *key, struct sshbuf *b,
|
|
|
|
enum sshkey_serialize_rep opts)
|
2015-01-26 10:59:11 +08:00
|
|
|
{
|
|
|
|
struct sshbuf *tmp;
|
|
|
|
int r;
|
|
|
|
|
|
|
|
if ((tmp = sshbuf_new()) == NULL)
|
|
|
|
return SSH_ERR_ALLOC_FAIL;
|
2018-02-23 23:58:37 +08:00
|
|
|
r = to_blob_buf(key, tmp, 0, opts);
|
2015-01-26 10:59:11 +08:00
|
|
|
if (r == 0)
|
|
|
|
r = sshbuf_put_stringb(b, tmp);
|
|
|
|
sshbuf_free(tmp);
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2018-02-23 23:58:37 +08:00
|
|
|
int
|
|
|
|
sshkey_puts(const struct sshkey *key, struct sshbuf *b)
|
|
|
|
{
|
|
|
|
return sshkey_puts_opts(key, b, SSHKEY_SERIALIZE_DEFAULT);
|
|
|
|
}
|
|
|
|
|
2015-01-26 10:59:11 +08:00
|
|
|
int
|
|
|
|
sshkey_putb_plain(const struct sshkey *key, struct sshbuf *b)
|
2014-07-02 13:28:02 +08:00
|
|
|
{
|
2018-02-23 23:58:37 +08:00
|
|
|
return to_blob_buf(key, b, 1, SSHKEY_SERIALIZE_DEFAULT);
|
2014-07-02 13:28:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2018-02-23 23:58:37 +08:00
|
|
|
to_blob(const struct sshkey *key, u_char **blobp, size_t *lenp, int force_plain,
|
|
|
|
enum sshkey_serialize_rep opts)
|
2014-07-02 13:28:02 +08:00
|
|
|
{
|
|
|
|
int ret = SSH_ERR_INTERNAL_ERROR;
|
|
|
|
size_t len;
|
|
|
|
struct sshbuf *b = NULL;
|
|
|
|
|
|
|
|
if (lenp != NULL)
|
|
|
|
*lenp = 0;
|
|
|
|
if (blobp != NULL)
|
|
|
|
*blobp = NULL;
|
|
|
|
if ((b = sshbuf_new()) == NULL)
|
|
|
|
return SSH_ERR_ALLOC_FAIL;
|
2018-02-23 23:58:37 +08:00
|
|
|
if ((ret = to_blob_buf(key, b, force_plain, opts)) != 0)
|
2014-07-02 13:28:02 +08:00
|
|
|
goto out;
|
|
|
|
len = sshbuf_len(b);
|
|
|
|
if (lenp != NULL)
|
|
|
|
*lenp = len;
|
|
|
|
if (blobp != NULL) {
|
|
|
|
if ((*blobp = malloc(len)) == NULL) {
|
|
|
|
ret = SSH_ERR_ALLOC_FAIL;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
memcpy(*blobp, sshbuf_ptr(b), len);
|
|
|
|
}
|
|
|
|
ret = 0;
|
|
|
|
out:
|
|
|
|
sshbuf_free(b);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
sshkey_to_blob(const struct sshkey *key, u_char **blobp, size_t *lenp)
|
|
|
|
{
|
2018-02-23 23:58:37 +08:00
|
|
|
return to_blob(key, blobp, lenp, 0, SSHKEY_SERIALIZE_DEFAULT);
|
2014-07-02 13:28:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
sshkey_plain_to_blob(const struct sshkey *key, u_char **blobp, size_t *lenp)
|
|
|
|
{
|
2018-02-23 23:58:37 +08:00
|
|
|
return to_blob(key, blobp, lenp, 1, SSHKEY_SERIALIZE_DEFAULT);
|
2014-07-02 13:28:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2014-12-22 06:27:55 +08:00
|
|
|
sshkey_fingerprint_raw(const struct sshkey *k, int dgst_alg,
|
2014-07-02 13:28:02 +08:00
|
|
|
u_char **retp, size_t *lenp)
|
|
|
|
{
|
|
|
|
u_char *blob = NULL, *ret = NULL;
|
|
|
|
size_t blob_len = 0;
|
2014-12-22 06:27:55 +08:00
|
|
|
int r = SSH_ERR_INTERNAL_ERROR;
|
2014-07-02 13:28:02 +08:00
|
|
|
|
|
|
|
if (retp != NULL)
|
|
|
|
*retp = NULL;
|
|
|
|
if (lenp != NULL)
|
|
|
|
*lenp = 0;
|
2014-12-22 06:27:55 +08:00
|
|
|
if (ssh_digest_bytes(dgst_alg) == 0) {
|
2014-07-02 13:28:02 +08:00
|
|
|
r = SSH_ERR_INVALID_ARGUMENT;
|
|
|
|
goto out;
|
|
|
|
}
|
2018-02-23 23:58:37 +08:00
|
|
|
if ((r = to_blob(k, &blob, &blob_len, 1, SSHKEY_SERIALIZE_DEFAULT))
|
|
|
|
!= 0)
|
2014-07-02 13:28:02 +08:00
|
|
|
goto out;
|
|
|
|
if ((ret = calloc(1, SSH_DIGEST_MAX_LENGTH)) == NULL) {
|
|
|
|
r = SSH_ERR_ALLOC_FAIL;
|
|
|
|
goto out;
|
|
|
|
}
|
2014-12-22 06:27:55 +08:00
|
|
|
if ((r = ssh_digest_memory(dgst_alg, blob, blob_len,
|
2014-07-02 13:28:02 +08:00
|
|
|
ret, SSH_DIGEST_MAX_LENGTH)) != 0)
|
|
|
|
goto out;
|
|
|
|
/* success */
|
|
|
|
if (retp != NULL) {
|
|
|
|
*retp = ret;
|
|
|
|
ret = NULL;
|
|
|
|
}
|
|
|
|
if (lenp != NULL)
|
2014-12-22 06:27:55 +08:00
|
|
|
*lenp = ssh_digest_bytes(dgst_alg);
|
2014-07-02 13:28:02 +08:00
|
|
|
r = 0;
|
|
|
|
out:
|
|
|
|
free(ret);
|
2020-02-26 21:40:09 +08:00
|
|
|
if (blob != NULL)
|
|
|
|
freezero(blob, blob_len);
|
2014-07-02 13:28:02 +08:00
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
static char *
|
2014-12-22 06:27:55 +08:00
|
|
|
fingerprint_b64(const char *alg, u_char *dgst_raw, size_t dgst_raw_len)
|
2014-07-02 13:28:02 +08:00
|
|
|
{
|
2014-12-22 06:27:55 +08:00
|
|
|
char *ret;
|
|
|
|
size_t plen = strlen(alg) + 1;
|
|
|
|
size_t rlen = ((dgst_raw_len + 2) / 3) * 4 + plen + 1;
|
2014-07-02 13:28:02 +08:00
|
|
|
|
2014-12-22 06:27:55 +08:00
|
|
|
if (dgst_raw_len > 65536 || (ret = calloc(1, rlen)) == NULL)
|
|
|
|
return NULL;
|
|
|
|
strlcpy(ret, alg, rlen);
|
|
|
|
strlcat(ret, ":", rlen);
|
|
|
|
if (dgst_raw_len == 0)
|
|
|
|
return ret;
|
2019-07-07 09:05:00 +08:00
|
|
|
if (b64_ntop(dgst_raw, dgst_raw_len, ret + plen, rlen - plen) == -1) {
|
2018-02-15 00:03:32 +08:00
|
|
|
freezero(ret, rlen);
|
2014-07-02 13:28:02 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
2014-12-22 06:27:55 +08:00
|
|
|
/* Trim padding characters from end */
|
|
|
|
ret[strcspn(ret, "=")] = '\0';
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static char *
|
|
|
|
fingerprint_hex(const char *alg, u_char *dgst_raw, size_t dgst_raw_len)
|
|
|
|
{
|
|
|
|
char *retval, hex[5];
|
|
|
|
size_t i, rlen = dgst_raw_len * 3 + strlen(alg) + 2;
|
2014-07-02 13:28:02 +08:00
|
|
|
|
2014-12-22 06:27:55 +08:00
|
|
|
if (dgst_raw_len > 65536 || (retval = calloc(1, rlen)) == NULL)
|
|
|
|
return NULL;
|
|
|
|
strlcpy(retval, alg, rlen);
|
|
|
|
strlcat(retval, ":", rlen);
|
|
|
|
for (i = 0; i < dgst_raw_len; i++) {
|
|
|
|
snprintf(hex, sizeof(hex), "%s%02x",
|
|
|
|
i > 0 ? ":" : "", dgst_raw[i]);
|
|
|
|
strlcat(retval, hex, rlen);
|
|
|
|
}
|
2014-07-02 13:28:02 +08:00
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
static char *
|
|
|
|
fingerprint_bubblebabble(u_char *dgst_raw, size_t dgst_raw_len)
|
|
|
|
{
|
|
|
|
char vowels[] = { 'a', 'e', 'i', 'o', 'u', 'y' };
|
|
|
|
char consonants[] = { 'b', 'c', 'd', 'f', 'g', 'h', 'k', 'l', 'm',
|
|
|
|
'n', 'p', 'r', 's', 't', 'v', 'z', 'x' };
|
|
|
|
u_int i, j = 0, rounds, seed = 1;
|
|
|
|
char *retval;
|
|
|
|
|
|
|
|
rounds = (dgst_raw_len / 2) + 1;
|
|
|
|
if ((retval = calloc(rounds, 6)) == NULL)
|
|
|
|
return NULL;
|
|
|
|
retval[j++] = 'x';
|
|
|
|
for (i = 0; i < rounds; i++) {
|
|
|
|
u_int idx0, idx1, idx2, idx3, idx4;
|
|
|
|
if ((i + 1 < rounds) || (dgst_raw_len % 2 != 0)) {
|
|
|
|
idx0 = (((((u_int)(dgst_raw[2 * i])) >> 6) & 3) +
|
|
|
|
seed) % 6;
|
|
|
|
idx1 = (((u_int)(dgst_raw[2 * i])) >> 2) & 15;
|
|
|
|
idx2 = ((((u_int)(dgst_raw[2 * i])) & 3) +
|
|
|
|
(seed / 6)) % 6;
|
|
|
|
retval[j++] = vowels[idx0];
|
|
|
|
retval[j++] = consonants[idx1];
|
|
|
|
retval[j++] = vowels[idx2];
|
|
|
|
if ((i + 1) < rounds) {
|
|
|
|
idx3 = (((u_int)(dgst_raw[(2 * i) + 1])) >> 4) & 15;
|
|
|
|
idx4 = (((u_int)(dgst_raw[(2 * i) + 1]))) & 15;
|
|
|
|
retval[j++] = consonants[idx3];
|
|
|
|
retval[j++] = '-';
|
|
|
|
retval[j++] = consonants[idx4];
|
|
|
|
seed = ((seed * 5) +
|
|
|
|
((((u_int)(dgst_raw[2 * i])) * 7) +
|
|
|
|
((u_int)(dgst_raw[(2 * i) + 1])))) % 36;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
idx0 = seed % 6;
|
|
|
|
idx1 = 16;
|
|
|
|
idx2 = seed / 6;
|
|
|
|
retval[j++] = vowels[idx0];
|
|
|
|
retval[j++] = consonants[idx1];
|
|
|
|
retval[j++] = vowels[idx2];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
retval[j++] = 'x';
|
|
|
|
retval[j++] = '\0';
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Draw an ASCII-Art representing the fingerprint so human brain can
|
|
|
|
* profit from its built-in pattern recognition ability.
|
|
|
|
* This technique is called "random art" and can be found in some
|
|
|
|
* scientific publications like this original paper:
|
|
|
|
*
|
|
|
|
* "Hash Visualization: a New Technique to improve Real-World Security",
|
|
|
|
* Perrig A. and Song D., 1999, International Workshop on Cryptographic
|
|
|
|
* Techniques and E-Commerce (CrypTEC '99)
|
|
|
|
* sparrow.ece.cmu.edu/~adrian/projects/validation/validation.pdf
|
|
|
|
*
|
|
|
|
* The subject came up in a talk by Dan Kaminsky, too.
|
|
|
|
*
|
|
|
|
* If you see the picture is different, the key is different.
|
|
|
|
* If the picture looks the same, you still know nothing.
|
|
|
|
*
|
|
|
|
* The algorithm used here is a worm crawling over a discrete plane,
|
|
|
|
* leaving a trace (augmenting the field) everywhere it goes.
|
|
|
|
* Movement is taken from dgst_raw 2bit-wise. Bumping into walls
|
|
|
|
* makes the respective movement vector be ignored for this turn.
|
|
|
|
* Graphs are not unambiguous, because circles in graphs can be
|
|
|
|
* walked in either direction.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Field sizes for the random art. Have to be odd, so the starting point
|
|
|
|
* can be in the exact middle of the picture, and FLDBASE should be >=8 .
|
|
|
|
* Else pictures would be too dense, and drawing the frame would
|
|
|
|
* fail, too, because the key type would not fit in anymore.
|
|
|
|
*/
|
|
|
|
#define FLDBASE 8
|
|
|
|
#define FLDSIZE_Y (FLDBASE + 1)
|
|
|
|
#define FLDSIZE_X (FLDBASE * 2 + 1)
|
|
|
|
static char *
|
2014-12-22 06:27:55 +08:00
|
|
|
fingerprint_randomart(const char *alg, u_char *dgst_raw, size_t dgst_raw_len,
|
2014-07-02 13:28:02 +08:00
|
|
|
const struct sshkey *k)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Chars to be used after each other every time the worm
|
|
|
|
* intersects with itself. Matter of taste.
|
|
|
|
*/
|
|
|
|
char *augmentation_string = " .o+=*BOX@%&#/^SE";
|
2014-12-22 06:27:55 +08:00
|
|
|
char *retval, *p, title[FLDSIZE_X], hash[FLDSIZE_X];
|
2014-07-02 13:28:02 +08:00
|
|
|
u_char field[FLDSIZE_X][FLDSIZE_Y];
|
2014-12-22 06:27:55 +08:00
|
|
|
size_t i, tlen, hlen;
|
2014-07-02 13:28:02 +08:00
|
|
|
u_int b;
|
2014-07-03 19:22:22 +08:00
|
|
|
int x, y, r;
|
2014-07-02 13:28:02 +08:00
|
|
|
size_t len = strlen(augmentation_string) - 1;
|
|
|
|
|
|
|
|
if ((retval = calloc((FLDSIZE_X + 3), (FLDSIZE_Y + 2))) == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
/* initialize field */
|
|
|
|
memset(field, 0, FLDSIZE_X * FLDSIZE_Y * sizeof(char));
|
|
|
|
x = FLDSIZE_X / 2;
|
|
|
|
y = FLDSIZE_Y / 2;
|
|
|
|
|
|
|
|
/* process raw key */
|
|
|
|
for (i = 0; i < dgst_raw_len; i++) {
|
|
|
|
int input;
|
|
|
|
/* each byte conveys four 2-bit move commands */
|
|
|
|
input = dgst_raw[i];
|
|
|
|
for (b = 0; b < 4; b++) {
|
|
|
|
/* evaluate 2 bit, rest is shifted later */
|
|
|
|
x += (input & 0x1) ? 1 : -1;
|
|
|
|
y += (input & 0x2) ? 1 : -1;
|
|
|
|
|
|
|
|
/* assure we are still in bounds */
|
2016-09-12 09:22:38 +08:00
|
|
|
x = MAXIMUM(x, 0);
|
|
|
|
y = MAXIMUM(y, 0);
|
|
|
|
x = MINIMUM(x, FLDSIZE_X - 1);
|
|
|
|
y = MINIMUM(y, FLDSIZE_Y - 1);
|
2014-07-02 13:28:02 +08:00
|
|
|
|
|
|
|
/* augment the field */
|
|
|
|
if (field[x][y] < len - 2)
|
|
|
|
field[x][y]++;
|
|
|
|
input = input >> 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* mark starting point and end point*/
|
|
|
|
field[FLDSIZE_X / 2][FLDSIZE_Y / 2] = len - 1;
|
|
|
|
field[x][y] = len;
|
|
|
|
|
2014-07-03 19:22:22 +08:00
|
|
|
/* assemble title */
|
|
|
|
r = snprintf(title, sizeof(title), "[%s %u]",
|
|
|
|
sshkey_type(k), sshkey_size(k));
|
|
|
|
/* If [type size] won't fit, then try [type]; fits "[ED25519-CERT]" */
|
|
|
|
if (r < 0 || r > (int)sizeof(title))
|
2014-12-22 06:27:55 +08:00
|
|
|
r = snprintf(title, sizeof(title), "[%s]", sshkey_type(k));
|
|
|
|
tlen = (r <= 0) ? 0 : strlen(title);
|
|
|
|
|
|
|
|
/* assemble hash ID. */
|
|
|
|
r = snprintf(hash, sizeof(hash), "[%s]", alg);
|
|
|
|
hlen = (r <= 0) ? 0 : strlen(hash);
|
2014-07-02 13:28:02 +08:00
|
|
|
|
|
|
|
/* output upper border */
|
2014-07-03 19:22:22 +08:00
|
|
|
p = retval;
|
|
|
|
*p++ = '+';
|
|
|
|
for (i = 0; i < (FLDSIZE_X - tlen) / 2; i++)
|
|
|
|
*p++ = '-';
|
|
|
|
memcpy(p, title, tlen);
|
|
|
|
p += tlen;
|
2014-12-22 06:27:55 +08:00
|
|
|
for (i += tlen; i < FLDSIZE_X; i++)
|
2014-07-02 13:28:02 +08:00
|
|
|
*p++ = '-';
|
|
|
|
*p++ = '+';
|
|
|
|
*p++ = '\n';
|
|
|
|
|
|
|
|
/* output content */
|
|
|
|
for (y = 0; y < FLDSIZE_Y; y++) {
|
|
|
|
*p++ = '|';
|
|
|
|
for (x = 0; x < FLDSIZE_X; x++)
|
2016-09-12 09:22:38 +08:00
|
|
|
*p++ = augmentation_string[MINIMUM(field[x][y], len)];
|
2014-07-02 13:28:02 +08:00
|
|
|
*p++ = '|';
|
|
|
|
*p++ = '\n';
|
|
|
|
}
|
|
|
|
|
|
|
|
/* output lower border */
|
|
|
|
*p++ = '+';
|
2014-12-22 06:27:55 +08:00
|
|
|
for (i = 0; i < (FLDSIZE_X - hlen) / 2; i++)
|
|
|
|
*p++ = '-';
|
|
|
|
memcpy(p, hash, hlen);
|
|
|
|
p += hlen;
|
|
|
|
for (i += hlen; i < FLDSIZE_X; i++)
|
2014-07-02 13:28:02 +08:00
|
|
|
*p++ = '-';
|
|
|
|
*p++ = '+';
|
|
|
|
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
char *
|
2014-12-22 06:27:55 +08:00
|
|
|
sshkey_fingerprint(const struct sshkey *k, int dgst_alg,
|
2014-07-02 13:28:02 +08:00
|
|
|
enum sshkey_fp_rep dgst_rep)
|
|
|
|
{
|
|
|
|
char *retval = NULL;
|
|
|
|
u_char *dgst_raw;
|
|
|
|
size_t dgst_raw_len;
|
|
|
|
|
2014-12-22 06:27:55 +08:00
|
|
|
if (sshkey_fingerprint_raw(k, dgst_alg, &dgst_raw, &dgst_raw_len) != 0)
|
2014-07-02 13:28:02 +08:00
|
|
|
return NULL;
|
|
|
|
switch (dgst_rep) {
|
2014-12-22 06:27:55 +08:00
|
|
|
case SSH_FP_DEFAULT:
|
|
|
|
if (dgst_alg == SSH_DIGEST_MD5) {
|
|
|
|
retval = fingerprint_hex(ssh_digest_alg_name(dgst_alg),
|
|
|
|
dgst_raw, dgst_raw_len);
|
|
|
|
} else {
|
|
|
|
retval = fingerprint_b64(ssh_digest_alg_name(dgst_alg),
|
|
|
|
dgst_raw, dgst_raw_len);
|
|
|
|
}
|
|
|
|
break;
|
2014-07-02 13:28:02 +08:00
|
|
|
case SSH_FP_HEX:
|
2014-12-22 06:27:55 +08:00
|
|
|
retval = fingerprint_hex(ssh_digest_alg_name(dgst_alg),
|
|
|
|
dgst_raw, dgst_raw_len);
|
|
|
|
break;
|
|
|
|
case SSH_FP_BASE64:
|
|
|
|
retval = fingerprint_b64(ssh_digest_alg_name(dgst_alg),
|
|
|
|
dgst_raw, dgst_raw_len);
|
2014-07-02 13:28:02 +08:00
|
|
|
break;
|
|
|
|
case SSH_FP_BUBBLEBABBLE:
|
|
|
|
retval = fingerprint_bubblebabble(dgst_raw, dgst_raw_len);
|
|
|
|
break;
|
|
|
|
case SSH_FP_RANDOMART:
|
2014-12-22 06:27:55 +08:00
|
|
|
retval = fingerprint_randomart(ssh_digest_alg_name(dgst_alg),
|
|
|
|
dgst_raw, dgst_raw_len, k);
|
2014-07-02 13:28:02 +08:00
|
|
|
break;
|
|
|
|
default:
|
2020-02-26 21:40:09 +08:00
|
|
|
freezero(dgst_raw, dgst_raw_len);
|
2014-07-02 13:28:02 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
2020-02-26 21:40:09 +08:00
|
|
|
freezero(dgst_raw, dgst_raw_len);
|
2014-07-02 13:28:02 +08:00
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
2018-03-02 10:08:03 +08:00
|
|
|
static int
|
|
|
|
peek_type_nid(const char *s, size_t l, int *nid)
|
|
|
|
{
|
2022-10-28 08:35:40 +08:00
|
|
|
const struct sshkey_impl *impl;
|
|
|
|
int i;
|
2018-03-02 10:08:03 +08:00
|
|
|
|
2022-10-28 08:35:40 +08:00
|
|
|
for (i = 0; keyimpls[i] != NULL; i++) {
|
|
|
|
impl = keyimpls[i];
|
|
|
|
if (impl->name == NULL || strlen(impl->name) != l)
|
2018-03-02 10:08:03 +08:00
|
|
|
continue;
|
2022-10-28 08:35:40 +08:00
|
|
|
if (memcmp(s, impl->name, l) == 0) {
|
2018-03-02 10:08:03 +08:00
|
|
|
*nid = -1;
|
2022-10-28 08:35:40 +08:00
|
|
|
if (key_type_is_ecdsa_variant(impl->type))
|
|
|
|
*nid = impl->nid;
|
|
|
|
return impl->type;
|
2018-03-02 10:08:03 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return KEY_UNSPEC;
|
|
|
|
}
|
2014-07-02 13:28:02 +08:00
|
|
|
|
2018-03-02 10:08:03 +08:00
|
|
|
/* XXX this can now be made const char * */
|
2014-07-02 13:28:02 +08:00
|
|
|
int
|
|
|
|
sshkey_read(struct sshkey *ret, char **cpp)
|
|
|
|
{
|
|
|
|
struct sshkey *k;
|
2018-03-02 10:08:03 +08:00
|
|
|
char *cp, *blobcopy;
|
|
|
|
size_t space;
|
2014-07-02 13:28:02 +08:00
|
|
|
int r, type, curve_nid = -1;
|
|
|
|
struct sshbuf *blob;
|
|
|
|
|
2017-03-10 11:48:57 +08:00
|
|
|
if (ret == NULL)
|
|
|
|
return SSH_ERR_INVALID_ARGUMENT;
|
2022-10-28 08:38:58 +08:00
|
|
|
if (ret->type != KEY_UNSPEC && sshkey_impl_from_type(ret->type) == NULL)
|
2018-03-02 10:08:03 +08:00
|
|
|
return SSH_ERR_INVALID_ARGUMENT;
|
|
|
|
|
|
|
|
/* Decode type */
|
|
|
|
cp = *cpp;
|
|
|
|
space = strcspn(cp, " \t");
|
|
|
|
if (space == strlen(cp))
|
|
|
|
return SSH_ERR_INVALID_FORMAT;
|
|
|
|
if ((type = peek_type_nid(cp, space, &curve_nid)) == KEY_UNSPEC)
|
|
|
|
return SSH_ERR_INVALID_FORMAT;
|
|
|
|
|
|
|
|
/* skip whitespace */
|
|
|
|
for (cp += space; *cp == ' ' || *cp == '\t'; cp++)
|
|
|
|
;
|
|
|
|
if (*cp == '\0')
|
|
|
|
return SSH_ERR_INVALID_FORMAT;
|
|
|
|
if (ret->type != KEY_UNSPEC && ret->type != type)
|
|
|
|
return SSH_ERR_KEY_TYPE_MISMATCH;
|
|
|
|
if ((blob = sshbuf_new()) == NULL)
|
|
|
|
return SSH_ERR_ALLOC_FAIL;
|
|
|
|
|
|
|
|
/* find end of keyblob and decode */
|
|
|
|
space = strcspn(cp, " \t");
|
|
|
|
if ((blobcopy = strndup(cp, space)) == NULL) {
|
2014-07-02 13:28:02 +08:00
|
|
|
sshbuf_free(blob);
|
2018-03-02 10:08:03 +08:00
|
|
|
return SSH_ERR_ALLOC_FAIL;
|
|
|
|
}
|
|
|
|
if ((r = sshbuf_b64tod(blob, blobcopy)) != 0) {
|
|
|
|
free(blobcopy);
|
|
|
|
sshbuf_free(blob);
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
free(blobcopy);
|
|
|
|
if ((r = sshkey_fromb(blob, &k)) != 0) {
|
|
|
|
sshbuf_free(blob);
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
sshbuf_free(blob);
|
|
|
|
|
|
|
|
/* skip whitespace and leave cp at start of comment */
|
|
|
|
for (cp += space; *cp == ' ' || *cp == '\t'; cp++)
|
|
|
|
;
|
|
|
|
|
|
|
|
/* ensure type of blob matches type at start of line */
|
|
|
|
if (k->type != type) {
|
|
|
|
sshkey_free(k);
|
|
|
|
return SSH_ERR_KEY_TYPE_MISMATCH;
|
|
|
|
}
|
2019-11-01 05:15:14 +08:00
|
|
|
if (key_type_is_ecdsa_variant(type) && curve_nid != k->ecdsa_nid) {
|
2018-03-02 10:08:03 +08:00
|
|
|
sshkey_free(k);
|
|
|
|
return SSH_ERR_EC_CURVE_MISMATCH;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Fill in ret from parsed key */
|
2022-10-28 08:38:58 +08:00
|
|
|
sshkey_free_contents(ret);
|
|
|
|
*ret = *k;
|
|
|
|
freezero(k, sizeof(*k));
|
2018-03-02 10:08:03 +08:00
|
|
|
|
|
|
|
/* success */
|
|
|
|
*cpp = cp;
|
|
|
|
return 0;
|
2014-07-02 13:28:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2015-05-21 12:55:51 +08:00
|
|
|
sshkey_to_base64(const struct sshkey *key, char **b64p)
|
2014-07-02 13:28:02 +08:00
|
|
|
{
|
2015-05-21 12:55:51 +08:00
|
|
|
int r = SSH_ERR_INTERNAL_ERROR;
|
|
|
|
struct sshbuf *b = NULL;
|
2014-07-02 13:28:02 +08:00
|
|
|
char *uu = NULL;
|
2015-05-21 12:55:51 +08:00
|
|
|
|
|
|
|
if (b64p != NULL)
|
|
|
|
*b64p = NULL;
|
|
|
|
if ((b = sshbuf_new()) == NULL)
|
|
|
|
return SSH_ERR_ALLOC_FAIL;
|
|
|
|
if ((r = sshkey_putb(key, b)) != 0)
|
|
|
|
goto out;
|
2019-07-16 21:18:39 +08:00
|
|
|
if ((uu = sshbuf_dtob64_string(b, 0)) == NULL) {
|
2015-05-21 12:55:51 +08:00
|
|
|
r = SSH_ERR_ALLOC_FAIL;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
/* Success */
|
|
|
|
if (b64p != NULL) {
|
|
|
|
*b64p = uu;
|
|
|
|
uu = NULL;
|
|
|
|
}
|
|
|
|
r = 0;
|
|
|
|
out:
|
|
|
|
sshbuf_free(b);
|
|
|
|
free(uu);
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2017-06-09 14:40:24 +08:00
|
|
|
int
|
2015-05-21 12:55:51 +08:00
|
|
|
sshkey_format_text(const struct sshkey *key, struct sshbuf *b)
|
|
|
|
{
|
|
|
|
int r = SSH_ERR_INTERNAL_ERROR;
|
|
|
|
char *uu = NULL;
|
|
|
|
|
2017-05-01 07:18:44 +08:00
|
|
|
if ((r = sshkey_to_base64(key, &uu)) != 0)
|
|
|
|
goto out;
|
|
|
|
if ((r = sshbuf_putf(b, "%s %s",
|
|
|
|
sshkey_ssh_name(key), uu)) != 0)
|
|
|
|
goto out;
|
2015-05-21 12:55:51 +08:00
|
|
|
r = 0;
|
|
|
|
out:
|
|
|
|
free(uu);
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
sshkey_write(const struct sshkey *key, FILE *f)
|
|
|
|
{
|
|
|
|
struct sshbuf *b = NULL;
|
|
|
|
int r = SSH_ERR_INTERNAL_ERROR;
|
|
|
|
|
|
|
|
if ((b = sshbuf_new()) == NULL)
|
|
|
|
return SSH_ERR_ALLOC_FAIL;
|
|
|
|
if ((r = sshkey_format_text(key, b)) != 0)
|
|
|
|
goto out;
|
2014-07-02 13:28:02 +08:00
|
|
|
if (fwrite(sshbuf_ptr(b), sshbuf_len(b), 1, f) != 1) {
|
|
|
|
if (feof(f))
|
|
|
|
errno = EPIPE;
|
2015-05-21 12:55:51 +08:00
|
|
|
r = SSH_ERR_SYSTEM_ERROR;
|
2014-07-02 13:28:02 +08:00
|
|
|
goto out;
|
|
|
|
}
|
2015-05-21 12:55:51 +08:00
|
|
|
/* Success */
|
|
|
|
r = 0;
|
2014-07-02 13:28:02 +08:00
|
|
|
out:
|
2015-05-21 12:55:51 +08:00
|
|
|
sshbuf_free(b);
|
|
|
|
return r;
|
2014-07-02 13:28:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const char *
|
|
|
|
sshkey_cert_type(const struct sshkey *k)
|
|
|
|
{
|
|
|
|
switch (k->cert->type) {
|
|
|
|
case SSH2_CERT_TYPE_USER:
|
|
|
|
return "user";
|
|
|
|
case SSH2_CERT_TYPE_HOST:
|
|
|
|
return "host";
|
|
|
|
default:
|
|
|
|
return "unknown";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-28 10:47:04 +08:00
|
|
|
int
|
|
|
|
sshkey_check_rsa_length(const struct sshkey *k, int min_size)
|
|
|
|
{
|
|
|
|
#ifdef WITH_OPENSSL
|
|
|
|
int nbits;
|
|
|
|
|
2024-08-15 08:51:51 +08:00
|
|
|
if (k == NULL || k->pkey == NULL ||
|
2022-10-28 10:47:04 +08:00
|
|
|
(k->type != KEY_RSA && k->type != KEY_RSA_CERT))
|
|
|
|
return 0;
|
2024-08-15 08:51:51 +08:00
|
|
|
nbits = EVP_PKEY_bits(k->pkey);
|
2022-10-28 10:47:04 +08:00
|
|
|
if (nbits < SSH_RSA_MINIMUM_MODULUS_SIZE ||
|
|
|
|
(min_size > 0 && nbits < min_size))
|
|
|
|
return SSH_ERR_KEY_LENGTH;
|
|
|
|
#endif /* WITH_OPENSSL */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2024-08-15 08:51:51 +08:00
|
|
|
#if defined(WITH_OPENSSL) && defined(OPENSSL_HAS_ECC)
|
2014-07-02 13:28:02 +08:00
|
|
|
int
|
2024-08-15 08:51:51 +08:00
|
|
|
sshkey_ecdsa_key_to_nid(const EC_KEY *k)
|
2014-07-02 13:28:02 +08:00
|
|
|
{
|
2024-08-15 08:51:51 +08:00
|
|
|
const EC_GROUP *g;
|
2014-07-02 13:28:02 +08:00
|
|
|
int nid;
|
|
|
|
|
2024-08-15 08:51:51 +08:00
|
|
|
if (k == NULL || (g = EC_KEY_get0_group(k)) == NULL)
|
|
|
|
return -1;
|
|
|
|
if ((nid = EC_GROUP_get_curve_name(g)) <= 0)
|
|
|
|
return -1;
|
|
|
|
return nid;
|
2014-07-02 13:28:02 +08:00
|
|
|
}
|
2024-08-15 08:51:51 +08:00
|
|
|
|
|
|
|
int
|
|
|
|
sshkey_ecdsa_pkey_to_nid(EVP_PKEY *pkey)
|
|
|
|
{
|
|
|
|
return sshkey_ecdsa_key_to_nid(EVP_PKEY_get0_EC_KEY(pkey));
|
|
|
|
}
|
|
|
|
#endif /* defined(WITH_OPENSSL) && defined(OPENSSL_HAS_ECC) */
|
2014-07-02 13:28:02 +08:00
|
|
|
|
|
|
|
int
|
|
|
|
sshkey_generate(int type, u_int bits, struct sshkey **keyp)
|
|
|
|
{
|
|
|
|
struct sshkey *k;
|
|
|
|
int ret = SSH_ERR_INTERNAL_ERROR;
|
2022-10-28 08:39:29 +08:00
|
|
|
const struct sshkey_impl *impl;
|
2014-07-02 13:28:02 +08:00
|
|
|
|
2022-10-28 08:39:29 +08:00
|
|
|
if (keyp == NULL || sshkey_type_is_cert(type))
|
2014-07-02 13:28:02 +08:00
|
|
|
return SSH_ERR_INVALID_ARGUMENT;
|
|
|
|
*keyp = NULL;
|
2022-10-28 08:39:29 +08:00
|
|
|
if ((impl = sshkey_impl_from_type(type)) == NULL)
|
|
|
|
return SSH_ERR_KEY_TYPE_UNKNOWN;
|
|
|
|
if (impl->funcs->generate == NULL)
|
|
|
|
return SSH_ERR_FEATURE_UNSUPPORTED;
|
2014-07-02 13:28:02 +08:00
|
|
|
if ((k = sshkey_new(KEY_UNSPEC)) == NULL)
|
|
|
|
return SSH_ERR_ALLOC_FAIL;
|
2022-10-28 08:39:29 +08:00
|
|
|
k->type = type;
|
|
|
|
if ((ret = impl->funcs->generate(k, bits)) != 0) {
|
2014-07-02 13:28:02 +08:00
|
|
|
sshkey_free(k);
|
2022-10-28 08:39:29 +08:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
/* success */
|
|
|
|
*keyp = k;
|
|
|
|
return 0;
|
2014-07-02 13:28:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
sshkey_cert_copy(const struct sshkey *from_key, struct sshkey *to_key)
|
|
|
|
{
|
|
|
|
u_int i;
|
|
|
|
const struct sshkey_cert *from;
|
|
|
|
struct sshkey_cert *to;
|
2018-09-12 09:31:30 +08:00
|
|
|
int r = SSH_ERR_INTERNAL_ERROR;
|
2014-07-02 13:28:02 +08:00
|
|
|
|
2018-09-12 09:31:30 +08:00
|
|
|
if (to_key == NULL || (from = from_key->cert) == NULL)
|
2014-07-02 13:28:02 +08:00
|
|
|
return SSH_ERR_INVALID_ARGUMENT;
|
|
|
|
|
2018-09-12 09:31:30 +08:00
|
|
|
if ((to = cert_new()) == NULL)
|
2014-07-02 13:28:02 +08:00
|
|
|
return SSH_ERR_ALLOC_FAIL;
|
|
|
|
|
2018-09-12 09:31:30 +08:00
|
|
|
if ((r = sshbuf_putb(to->certblob, from->certblob)) != 0 ||
|
|
|
|
(r = sshbuf_putb(to->critical, from->critical)) != 0 ||
|
|
|
|
(r = sshbuf_putb(to->extensions, from->extensions)) != 0)
|
|
|
|
goto out;
|
2014-07-02 13:28:02 +08:00
|
|
|
|
|
|
|
to->serial = from->serial;
|
|
|
|
to->type = from->type;
|
|
|
|
if (from->key_id == NULL)
|
|
|
|
to->key_id = NULL;
|
2018-09-12 09:31:30 +08:00
|
|
|
else if ((to->key_id = strdup(from->key_id)) == NULL) {
|
|
|
|
r = SSH_ERR_ALLOC_FAIL;
|
|
|
|
goto out;
|
|
|
|
}
|
2014-07-02 13:28:02 +08:00
|
|
|
to->valid_after = from->valid_after;
|
|
|
|
to->valid_before = from->valid_before;
|
|
|
|
if (from->signature_key == NULL)
|
|
|
|
to->signature_key = NULL;
|
2018-09-12 09:31:30 +08:00
|
|
|
else if ((r = sshkey_from_private(from->signature_key,
|
2014-07-02 13:28:02 +08:00
|
|
|
&to->signature_key)) != 0)
|
2018-09-12 09:31:30 +08:00
|
|
|
goto out;
|
|
|
|
if (from->signature_type != NULL &&
|
|
|
|
(to->signature_type = strdup(from->signature_type)) == NULL) {
|
|
|
|
r = SSH_ERR_ALLOC_FAIL;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
if (from->nprincipals > SSHKEY_CERT_MAX_PRINCIPALS) {
|
|
|
|
r = SSH_ERR_INVALID_ARGUMENT;
|
|
|
|
goto out;
|
|
|
|
}
|
2014-07-02 13:28:02 +08:00
|
|
|
if (from->nprincipals > 0) {
|
|
|
|
if ((to->principals = calloc(from->nprincipals,
|
2018-09-12 09:31:30 +08:00
|
|
|
sizeof(*to->principals))) == NULL) {
|
|
|
|
r = SSH_ERR_ALLOC_FAIL;
|
|
|
|
goto out;
|
|
|
|
}
|
2014-07-02 13:28:02 +08:00
|
|
|
for (i = 0; i < from->nprincipals; i++) {
|
|
|
|
to->principals[i] = strdup(from->principals[i]);
|
|
|
|
if (to->principals[i] == NULL) {
|
|
|
|
to->nprincipals = i;
|
2018-09-12 09:31:30 +08:00
|
|
|
r = SSH_ERR_ALLOC_FAIL;
|
|
|
|
goto out;
|
2014-07-02 13:28:02 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
to->nprincipals = from->nprincipals;
|
2018-09-12 09:31:30 +08:00
|
|
|
|
|
|
|
/* success */
|
|
|
|
cert_free(to_key->cert);
|
|
|
|
to_key->cert = to;
|
|
|
|
to = NULL;
|
|
|
|
r = 0;
|
|
|
|
out:
|
|
|
|
cert_free(to);
|
|
|
|
return r;
|
2014-07-02 13:28:02 +08:00
|
|
|
}
|
|
|
|
|
2022-10-28 08:41:17 +08:00
|
|
|
int
|
|
|
|
sshkey_copy_public_sk(const struct sshkey *from, struct sshkey *to)
|
|
|
|
{
|
|
|
|
/* Append security-key application string */
|
|
|
|
if ((to->sk_application = strdup(from->sk_application)) == NULL)
|
|
|
|
return SSH_ERR_ALLOC_FAIL;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-07-02 13:28:02 +08:00
|
|
|
int
|
|
|
|
sshkey_from_private(const struct sshkey *k, struct sshkey **pkp)
|
|
|
|
{
|
|
|
|
struct sshkey *n = NULL;
|
2018-09-13 10:08:33 +08:00
|
|
|
int r = SSH_ERR_INTERNAL_ERROR;
|
2022-10-28 08:41:17 +08:00
|
|
|
const struct sshkey_impl *impl;
|
2014-07-02 13:28:02 +08:00
|
|
|
|
2015-10-16 07:08:23 +08:00
|
|
|
*pkp = NULL;
|
2022-10-28 08:41:17 +08:00
|
|
|
if ((impl = sshkey_impl_from_key(k)) == NULL)
|
|
|
|
return SSH_ERR_KEY_TYPE_UNKNOWN;
|
2019-11-01 05:15:14 +08:00
|
|
|
if ((n = sshkey_new(k->type)) == NULL) {
|
|
|
|
r = SSH_ERR_ALLOC_FAIL;
|
|
|
|
goto out;
|
|
|
|
}
|
2022-10-28 08:41:17 +08:00
|
|
|
if ((r = impl->funcs->copy_public(k, n)) != 0)
|
2018-09-13 10:08:33 +08:00
|
|
|
goto out;
|
|
|
|
if (sshkey_is_cert(k) && (r = sshkey_cert_copy(k, n)) != 0)
|
|
|
|
goto out;
|
|
|
|
/* success */
|
2014-07-02 13:28:02 +08:00
|
|
|
*pkp = n;
|
2018-09-13 10:08:33 +08:00
|
|
|
n = NULL;
|
|
|
|
r = 0;
|
|
|
|
out:
|
|
|
|
sshkey_free(n);
|
|
|
|
return r;
|
2014-07-02 13:28:02 +08:00
|
|
|
}
|
|
|
|
|
2019-06-21 12:21:04 +08:00
|
|
|
int
|
|
|
|
sshkey_is_shielded(struct sshkey *k)
|
|
|
|
{
|
|
|
|
return k != NULL && k->shielded_private != NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
sshkey_shield_private(struct sshkey *k)
|
|
|
|
{
|
|
|
|
struct sshbuf *prvbuf = NULL;
|
|
|
|
u_char *prekey = NULL, *enc = NULL, keyiv[SSH_DIGEST_MAX_LENGTH];
|
|
|
|
struct sshcipher_ctx *cctx = NULL;
|
|
|
|
const struct sshcipher *cipher;
|
|
|
|
size_t i, enclen = 0;
|
|
|
|
struct sshkey *kswap = NULL, tmp;
|
|
|
|
int r = SSH_ERR_INTERNAL_ERROR;
|
|
|
|
|
|
|
|
#ifdef DEBUG_PK
|
|
|
|
fprintf(stderr, "%s: entering for %s\n", __func__, sshkey_ssh_name(k));
|
|
|
|
#endif
|
|
|
|
if ((cipher = cipher_by_name(SSHKEY_SHIELD_CIPHER)) == NULL) {
|
|
|
|
r = SSH_ERR_INVALID_ARGUMENT;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
if (cipher_keylen(cipher) + cipher_ivlen(cipher) >
|
|
|
|
ssh_digest_bytes(SSHKEY_SHIELD_PREKEY_HASH)) {
|
|
|
|
r = SSH_ERR_INTERNAL_ERROR;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Prepare a random pre-key, and from it an ephemeral key */
|
2024-08-20 11:48:30 +08:00
|
|
|
if ((r = sshkey_prekey_alloc(&prekey, SSHKEY_SHIELD_PREKEY_LEN)) != 0)
|
2019-06-21 12:21:04 +08:00
|
|
|
goto out;
|
|
|
|
arc4random_buf(prekey, SSHKEY_SHIELD_PREKEY_LEN);
|
|
|
|
if ((r = ssh_digest_memory(SSHKEY_SHIELD_PREKEY_HASH,
|
|
|
|
prekey, SSHKEY_SHIELD_PREKEY_LEN,
|
|
|
|
keyiv, SSH_DIGEST_MAX_LENGTH)) != 0)
|
|
|
|
goto out;
|
|
|
|
#ifdef DEBUG_PK
|
|
|
|
fprintf(stderr, "%s: key+iv\n", __func__);
|
|
|
|
sshbuf_dump_data(keyiv, ssh_digest_bytes(SSHKEY_SHIELD_PREKEY_HASH),
|
|
|
|
stderr);
|
|
|
|
#endif
|
|
|
|
if ((r = cipher_init(&cctx, cipher, keyiv, cipher_keylen(cipher),
|
|
|
|
keyiv + cipher_keylen(cipher), cipher_ivlen(cipher), 1)) != 0)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
/* Serialise and encrypt the private key using the ephemeral key */
|
|
|
|
if ((prvbuf = sshbuf_new()) == NULL) {
|
|
|
|
r = SSH_ERR_ALLOC_FAIL;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
if (sshkey_is_shielded(k) && (r = sshkey_unshield_private(k)) != 0)
|
|
|
|
goto out;
|
|
|
|
if ((r = sshkey_private_serialize_opt(k, prvbuf,
|
2021-04-03 14:18:40 +08:00
|
|
|
SSHKEY_SERIALIZE_SHIELD)) != 0)
|
2019-06-21 12:21:04 +08:00
|
|
|
goto out;
|
|
|
|
/* pad to cipher blocksize */
|
|
|
|
i = 0;
|
|
|
|
while (sshbuf_len(prvbuf) % cipher_blocksize(cipher)) {
|
|
|
|
if ((r = sshbuf_put_u8(prvbuf, ++i & 0xff)) != 0)
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
#ifdef DEBUG_PK
|
|
|
|
fprintf(stderr, "%s: serialised\n", __func__);
|
|
|
|
sshbuf_dump(prvbuf, stderr);
|
|
|
|
#endif
|
|
|
|
/* encrypt */
|
|
|
|
enclen = sshbuf_len(prvbuf);
|
|
|
|
if ((enc = malloc(enclen)) == NULL) {
|
|
|
|
r = SSH_ERR_ALLOC_FAIL;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
if ((r = cipher_crypt(cctx, 0, enc,
|
|
|
|
sshbuf_ptr(prvbuf), sshbuf_len(prvbuf), 0, 0)) != 0)
|
|
|
|
goto out;
|
|
|
|
#ifdef DEBUG_PK
|
|
|
|
fprintf(stderr, "%s: encrypted\n", __func__);
|
|
|
|
sshbuf_dump_data(enc, enclen, stderr);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Make a scrubbed, public-only copy of our private key argument */
|
|
|
|
if ((r = sshkey_from_private(k, &kswap)) != 0)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
/* Swap the private key out (it will be destroyed below) */
|
|
|
|
tmp = *kswap;
|
|
|
|
*kswap = *k;
|
|
|
|
*k = tmp;
|
|
|
|
|
|
|
|
/* Insert the shielded key into our argument */
|
|
|
|
k->shielded_private = enc;
|
|
|
|
k->shielded_len = enclen;
|
|
|
|
k->shield_prekey = prekey;
|
|
|
|
k->shield_prekey_len = SSHKEY_SHIELD_PREKEY_LEN;
|
|
|
|
enc = prekey = NULL; /* transferred */
|
|
|
|
enclen = 0;
|
|
|
|
|
2020-01-21 13:56:56 +08:00
|
|
|
/* preserve key fields that are required for correct operation */
|
|
|
|
k->sk_flags = kswap->sk_flags;
|
|
|
|
|
2019-06-21 12:21:04 +08:00
|
|
|
/* success */
|
|
|
|
r = 0;
|
|
|
|
|
|
|
|
out:
|
|
|
|
/* XXX behaviour on error - invalidate original private key? */
|
|
|
|
cipher_free(cctx);
|
|
|
|
explicit_bzero(keyiv, sizeof(keyiv));
|
|
|
|
explicit_bzero(&tmp, sizeof(tmp));
|
2019-06-27 14:29:35 +08:00
|
|
|
freezero(enc, enclen);
|
2024-08-20 11:48:30 +08:00
|
|
|
sshkey_prekey_free(prekey, SSHKEY_SHIELD_PREKEY_LEN);
|
2019-06-21 12:21:04 +08:00
|
|
|
sshkey_free(kswap);
|
|
|
|
sshbuf_free(prvbuf);
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2022-05-05 09:04:14 +08:00
|
|
|
/* Check deterministic padding after private key */
|
|
|
|
static int
|
|
|
|
private2_check_padding(struct sshbuf *decrypted)
|
|
|
|
{
|
|
|
|
u_char pad;
|
|
|
|
size_t i;
|
|
|
|
int r;
|
|
|
|
|
|
|
|
i = 0;
|
|
|
|
while (sshbuf_len(decrypted)) {
|
|
|
|
if ((r = sshbuf_get_u8(decrypted, &pad)) != 0)
|
|
|
|
goto out;
|
|
|
|
if (pad != (++i & 0xff)) {
|
|
|
|
r = SSH_ERR_INVALID_FORMAT;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* success */
|
|
|
|
r = 0;
|
|
|
|
out:
|
|
|
|
explicit_bzero(&pad, sizeof(pad));
|
|
|
|
explicit_bzero(&i, sizeof(i));
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2019-06-21 12:21:04 +08:00
|
|
|
int
|
|
|
|
sshkey_unshield_private(struct sshkey *k)
|
|
|
|
{
|
|
|
|
struct sshbuf *prvbuf = NULL;
|
2022-05-05 09:04:14 +08:00
|
|
|
u_char *cp, keyiv[SSH_DIGEST_MAX_LENGTH];
|
2019-06-21 12:21:04 +08:00
|
|
|
struct sshcipher_ctx *cctx = NULL;
|
|
|
|
const struct sshcipher *cipher;
|
|
|
|
struct sshkey *kswap = NULL, tmp;
|
|
|
|
int r = SSH_ERR_INTERNAL_ERROR;
|
|
|
|
|
|
|
|
#ifdef DEBUG_PK
|
|
|
|
fprintf(stderr, "%s: entering for %s\n", __func__, sshkey_ssh_name(k));
|
|
|
|
#endif
|
|
|
|
if (!sshkey_is_shielded(k))
|
|
|
|
return 0; /* nothing to do */
|
|
|
|
|
|
|
|
if ((cipher = cipher_by_name(SSHKEY_SHIELD_CIPHER)) == NULL) {
|
|
|
|
r = SSH_ERR_INVALID_ARGUMENT;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
if (cipher_keylen(cipher) + cipher_ivlen(cipher) >
|
|
|
|
ssh_digest_bytes(SSHKEY_SHIELD_PREKEY_HASH)) {
|
|
|
|
r = SSH_ERR_INTERNAL_ERROR;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
/* check size of shielded key blob */
|
|
|
|
if (k->shielded_len < cipher_blocksize(cipher) ||
|
|
|
|
(k->shielded_len % cipher_blocksize(cipher)) != 0) {
|
|
|
|
r = SSH_ERR_INVALID_FORMAT;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Calculate the ephemeral key from the prekey */
|
|
|
|
if ((r = ssh_digest_memory(SSHKEY_SHIELD_PREKEY_HASH,
|
|
|
|
k->shield_prekey, k->shield_prekey_len,
|
|
|
|
keyiv, SSH_DIGEST_MAX_LENGTH)) != 0)
|
|
|
|
goto out;
|
|
|
|
if ((r = cipher_init(&cctx, cipher, keyiv, cipher_keylen(cipher),
|
|
|
|
keyiv + cipher_keylen(cipher), cipher_ivlen(cipher), 0)) != 0)
|
|
|
|
goto out;
|
|
|
|
#ifdef DEBUG_PK
|
|
|
|
fprintf(stderr, "%s: key+iv\n", __func__);
|
|
|
|
sshbuf_dump_data(keyiv, ssh_digest_bytes(SSHKEY_SHIELD_PREKEY_HASH),
|
|
|
|
stderr);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Decrypt and parse the shielded private key using the ephemeral key */
|
|
|
|
if ((prvbuf = sshbuf_new()) == NULL) {
|
|
|
|
r = SSH_ERR_ALLOC_FAIL;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
if ((r = sshbuf_reserve(prvbuf, k->shielded_len, &cp)) != 0)
|
|
|
|
goto out;
|
|
|
|
/* decrypt */
|
|
|
|
#ifdef DEBUG_PK
|
|
|
|
fprintf(stderr, "%s: encrypted\n", __func__);
|
|
|
|
sshbuf_dump_data(k->shielded_private, k->shielded_len, stderr);
|
|
|
|
#endif
|
|
|
|
if ((r = cipher_crypt(cctx, 0, cp,
|
|
|
|
k->shielded_private, k->shielded_len, 0, 0)) != 0)
|
|
|
|
goto out;
|
|
|
|
#ifdef DEBUG_PK
|
|
|
|
fprintf(stderr, "%s: serialised\n", __func__);
|
|
|
|
sshbuf_dump(prvbuf, stderr);
|
|
|
|
#endif
|
|
|
|
/* Parse private key */
|
|
|
|
if ((r = sshkey_private_deserialize(prvbuf, &kswap)) != 0)
|
|
|
|
goto out;
|
2022-05-05 09:04:14 +08:00
|
|
|
|
|
|
|
if ((r = private2_check_padding(prvbuf)) != 0)
|
|
|
|
goto out;
|
2019-06-21 12:21:04 +08:00
|
|
|
|
|
|
|
/* Swap the parsed key back into place */
|
|
|
|
tmp = *kswap;
|
|
|
|
*kswap = *k;
|
|
|
|
*k = tmp;
|
|
|
|
|
|
|
|
/* success */
|
|
|
|
r = 0;
|
|
|
|
|
|
|
|
out:
|
|
|
|
cipher_free(cctx);
|
|
|
|
explicit_bzero(keyiv, sizeof(keyiv));
|
|
|
|
explicit_bzero(&tmp, sizeof(tmp));
|
|
|
|
sshkey_free(kswap);
|
|
|
|
sshbuf_free(prvbuf);
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2014-07-02 13:28:02 +08:00
|
|
|
static int
|
2015-01-26 10:59:11 +08:00
|
|
|
cert_parse(struct sshbuf *b, struct sshkey *key, struct sshbuf *certbuf)
|
2014-07-02 13:28:02 +08:00
|
|
|
{
|
2015-01-26 10:59:11 +08:00
|
|
|
struct sshbuf *principals = NULL, *crit = NULL;
|
|
|
|
struct sshbuf *exts = NULL, *ca = NULL;
|
|
|
|
u_char *sig = NULL;
|
|
|
|
size_t signed_len = 0, slen = 0, kidlen = 0;
|
2014-07-02 13:28:02 +08:00
|
|
|
int ret = SSH_ERR_INTERNAL_ERROR;
|
|
|
|
|
|
|
|
/* Copy the entire key blob for verification and later serialisation */
|
2015-01-26 10:59:11 +08:00
|
|
|
if ((ret = sshbuf_putb(key->cert->certblob, certbuf)) != 0)
|
2014-07-02 13:28:02 +08:00
|
|
|
return ret;
|
|
|
|
|
2015-07-03 11:43:18 +08:00
|
|
|
/* Parse body of certificate up to signature */
|
|
|
|
if ((ret = sshbuf_get_u64(b, &key->cert->serial)) != 0 ||
|
2014-07-02 13:28:02 +08:00
|
|
|
(ret = sshbuf_get_u32(b, &key->cert->type)) != 0 ||
|
|
|
|
(ret = sshbuf_get_cstring(b, &key->cert->key_id, &kidlen)) != 0 ||
|
2014-10-09 05:45:48 +08:00
|
|
|
(ret = sshbuf_froms(b, &principals)) != 0 ||
|
2014-07-02 13:28:02 +08:00
|
|
|
(ret = sshbuf_get_u64(b, &key->cert->valid_after)) != 0 ||
|
|
|
|
(ret = sshbuf_get_u64(b, &key->cert->valid_before)) != 0 ||
|
2014-10-09 05:45:48 +08:00
|
|
|
(ret = sshbuf_froms(b, &crit)) != 0 ||
|
2015-07-03 11:43:18 +08:00
|
|
|
(ret = sshbuf_froms(b, &exts)) != 0 ||
|
2014-07-02 13:28:02 +08:00
|
|
|
(ret = sshbuf_get_string_direct(b, NULL, NULL)) != 0 ||
|
2015-01-26 10:59:11 +08:00
|
|
|
(ret = sshbuf_froms(b, &ca)) != 0) {
|
2014-07-02 13:28:02 +08:00
|
|
|
/* XXX debug print error for ret */
|
|
|
|
ret = SSH_ERR_INVALID_FORMAT;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Signature is left in the buffer so we can calculate this length */
|
|
|
|
signed_len = sshbuf_len(key->cert->certblob) - sshbuf_len(b);
|
|
|
|
|
|
|
|
if ((ret = sshbuf_get_string(b, &sig, &slen)) != 0) {
|
|
|
|
ret = SSH_ERR_INVALID_FORMAT;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (key->cert->type != SSH2_CERT_TYPE_USER &&
|
|
|
|
key->cert->type != SSH2_CERT_TYPE_HOST) {
|
|
|
|
ret = SSH_ERR_KEY_CERT_UNKNOWN_TYPE;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2014-10-09 05:45:48 +08:00
|
|
|
/* Parse principals section */
|
|
|
|
while (sshbuf_len(principals) > 0) {
|
|
|
|
char *principal = NULL;
|
|
|
|
char **oprincipals = NULL;
|
|
|
|
|
2014-07-02 13:28:02 +08:00
|
|
|
if (key->cert->nprincipals >= SSHKEY_CERT_MAX_PRINCIPALS) {
|
|
|
|
ret = SSH_ERR_INVALID_FORMAT;
|
|
|
|
goto out;
|
|
|
|
}
|
2014-10-09 05:45:48 +08:00
|
|
|
if ((ret = sshbuf_get_cstring(principals, &principal,
|
|
|
|
NULL)) != 0) {
|
2014-07-02 13:28:02 +08:00
|
|
|
ret = SSH_ERR_INVALID_FORMAT;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
oprincipals = key->cert->principals;
|
2017-05-31 17:15:42 +08:00
|
|
|
key->cert->principals = recallocarray(key->cert->principals,
|
|
|
|
key->cert->nprincipals, key->cert->nprincipals + 1,
|
|
|
|
sizeof(*key->cert->principals));
|
2014-07-02 13:28:02 +08:00
|
|
|
if (key->cert->principals == NULL) {
|
|
|
|
free(principal);
|
|
|
|
key->cert->principals = oprincipals;
|
|
|
|
ret = SSH_ERR_ALLOC_FAIL;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
key->cert->principals[key->cert->nprincipals++] = principal;
|
|
|
|
}
|
|
|
|
|
2014-10-09 05:45:48 +08:00
|
|
|
/*
|
|
|
|
* Stash a copies of the critical options and extensions sections
|
|
|
|
* for later use.
|
|
|
|
*/
|
|
|
|
if ((ret = sshbuf_putb(key->cert->critical, crit)) != 0 ||
|
|
|
|
(exts != NULL &&
|
|
|
|
(ret = sshbuf_putb(key->cert->extensions, exts)) != 0))
|
2014-07-02 13:28:02 +08:00
|
|
|
goto out;
|
|
|
|
|
2014-10-09 05:45:48 +08:00
|
|
|
/*
|
|
|
|
* Validate critical options and extensions sections format.
|
|
|
|
*/
|
|
|
|
while (sshbuf_len(crit) != 0) {
|
|
|
|
if ((ret = sshbuf_get_string_direct(crit, NULL, NULL)) != 0 ||
|
|
|
|
(ret = sshbuf_get_string_direct(crit, NULL, NULL)) != 0) {
|
|
|
|
sshbuf_reset(key->cert->critical);
|
2014-07-02 13:28:02 +08:00
|
|
|
ret = SSH_ERR_INVALID_FORMAT;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
}
|
2014-10-09 05:45:48 +08:00
|
|
|
while (exts != NULL && sshbuf_len(exts) != 0) {
|
|
|
|
if ((ret = sshbuf_get_string_direct(exts, NULL, NULL)) != 0 ||
|
|
|
|
(ret = sshbuf_get_string_direct(exts, NULL, NULL)) != 0) {
|
|
|
|
sshbuf_reset(key->cert->extensions);
|
2014-07-02 13:28:02 +08:00
|
|
|
ret = SSH_ERR_INVALID_FORMAT;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-09 05:45:48 +08:00
|
|
|
/* Parse CA key and check signature */
|
2015-01-26 10:59:11 +08:00
|
|
|
if (sshkey_from_blob_internal(ca, &key->cert->signature_key, 0) != 0) {
|
2014-07-02 13:28:02 +08:00
|
|
|
ret = SSH_ERR_KEY_CERT_INVALID_SIGN_KEY;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
if (!sshkey_type_is_valid_ca(key->cert->signature_key->type)) {
|
|
|
|
ret = SSH_ERR_KEY_CERT_INVALID_SIGN_KEY;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
if ((ret = sshkey_verify(key->cert->signature_key, sig, slen,
|
2019-11-25 08:51:37 +08:00
|
|
|
sshbuf_ptr(key->cert->certblob), signed_len, NULL, 0, NULL)) != 0)
|
2014-07-02 13:28:02 +08:00
|
|
|
goto out;
|
2019-09-03 16:31:20 +08:00
|
|
|
if ((ret = sshkey_get_sigtype(sig, slen,
|
|
|
|
&key->cert->signature_type)) != 0)
|
2018-09-12 09:31:30 +08:00
|
|
|
goto out;
|
2014-07-02 13:28:02 +08:00
|
|
|
|
2014-10-09 05:45:48 +08:00
|
|
|
/* Success */
|
|
|
|
ret = 0;
|
2014-07-02 13:28:02 +08:00
|
|
|
out:
|
2015-01-26 10:59:11 +08:00
|
|
|
sshbuf_free(ca);
|
2014-10-09 05:45:48 +08:00
|
|
|
sshbuf_free(crit);
|
|
|
|
sshbuf_free(exts);
|
|
|
|
sshbuf_free(principals);
|
2014-07-02 13:28:02 +08:00
|
|
|
free(sig);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2022-09-17 18:30:45 +08:00
|
|
|
int
|
2022-10-28 08:41:52 +08:00
|
|
|
sshkey_deserialize_sk(struct sshbuf *b, struct sshkey *key)
|
2018-09-13 10:08:33 +08:00
|
|
|
{
|
2022-10-28 08:41:52 +08:00
|
|
|
/* Parse additional security-key application string */
|
|
|
|
if (sshbuf_get_cstring(b, &key->sk_application, NULL) != 0)
|
|
|
|
return SSH_ERR_INVALID_FORMAT;
|
2018-09-13 10:08:33 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-07-02 13:28:02 +08:00
|
|
|
static int
|
2015-01-26 10:59:11 +08:00
|
|
|
sshkey_from_blob_internal(struct sshbuf *b, struct sshkey **keyp,
|
|
|
|
int allow_cert)
|
2014-07-02 13:28:02 +08:00
|
|
|
{
|
2015-01-14 18:46:28 +08:00
|
|
|
int type, ret = SSH_ERR_INTERNAL_ERROR;
|
2022-10-28 08:41:52 +08:00
|
|
|
char *ktype = NULL;
|
2014-07-02 13:28:02 +08:00
|
|
|
struct sshkey *key = NULL;
|
2015-01-26 10:59:11 +08:00
|
|
|
struct sshbuf *copy;
|
2022-10-28 08:41:52 +08:00
|
|
|
const struct sshkey_impl *impl;
|
2014-07-02 13:28:02 +08:00
|
|
|
|
|
|
|
#ifdef DEBUG_PK /* XXX */
|
2015-01-26 10:59:11 +08:00
|
|
|
sshbuf_dump(b, stderr);
|
2014-07-02 13:28:02 +08:00
|
|
|
#endif
|
2016-04-09 20:39:30 +08:00
|
|
|
if (keyp != NULL)
|
|
|
|
*keyp = NULL;
|
2015-01-26 10:59:11 +08:00
|
|
|
if ((copy = sshbuf_fromb(b)) == NULL) {
|
|
|
|
ret = SSH_ERR_ALLOC_FAIL;
|
|
|
|
goto out;
|
|
|
|
}
|
2014-07-02 13:28:02 +08:00
|
|
|
if (sshbuf_get_cstring(b, &ktype, NULL) != 0) {
|
|
|
|
ret = SSH_ERR_INVALID_FORMAT;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
type = sshkey_type_from_name(ktype);
|
|
|
|
if (!allow_cert && sshkey_type_is_cert(type)) {
|
|
|
|
ret = SSH_ERR_KEY_CERT_INVALID_SIGN_KEY;
|
|
|
|
goto out;
|
|
|
|
}
|
2022-10-28 08:41:52 +08:00
|
|
|
if ((impl = sshkey_impl_from_type(type)) == NULL) {
|
|
|
|
ret = SSH_ERR_KEY_TYPE_UNKNOWN;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
if ((key = sshkey_new(type)) == NULL) {
|
|
|
|
ret = SSH_ERR_ALLOC_FAIL;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
if (sshkey_type_is_cert(type)) {
|
2023-12-20 08:06:25 +08:00
|
|
|
/* Skip nonce that precedes all certificates */
|
2018-02-23 23:58:37 +08:00
|
|
|
if (sshbuf_get_string_direct(b, NULL, NULL) != 0) {
|
|
|
|
ret = SSH_ERR_INVALID_FORMAT;
|
|
|
|
goto out;
|
|
|
|
}
|
2014-07-02 13:28:02 +08:00
|
|
|
}
|
2022-10-28 08:41:52 +08:00
|
|
|
if ((ret = impl->funcs->deserialize_public(ktype, b, key)) != 0)
|
|
|
|
goto out;
|
2014-07-02 13:28:02 +08:00
|
|
|
|
|
|
|
/* Parse certificate potion */
|
2015-01-26 10:59:11 +08:00
|
|
|
if (sshkey_is_cert(key) && (ret = cert_parse(b, key, copy)) != 0)
|
2014-07-02 13:28:02 +08:00
|
|
|
goto out;
|
|
|
|
|
|
|
|
if (key != NULL && sshbuf_len(b) != 0) {
|
|
|
|
ret = SSH_ERR_INVALID_FORMAT;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
ret = 0;
|
2016-04-09 20:39:30 +08:00
|
|
|
if (keyp != NULL) {
|
|
|
|
*keyp = key;
|
|
|
|
key = NULL;
|
|
|
|
}
|
2014-07-02 13:28:02 +08:00
|
|
|
out:
|
2015-01-26 10:59:11 +08:00
|
|
|
sshbuf_free(copy);
|
2014-07-02 13:28:02 +08:00
|
|
|
sshkey_free(key);
|
|
|
|
free(ktype);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
sshkey_from_blob(const u_char *blob, size_t blen, struct sshkey **keyp)
|
|
|
|
{
|
2015-01-26 10:59:11 +08:00
|
|
|
struct sshbuf *b;
|
|
|
|
int r;
|
|
|
|
|
|
|
|
if ((b = sshbuf_from(blob, blen)) == NULL)
|
|
|
|
return SSH_ERR_ALLOC_FAIL;
|
|
|
|
r = sshkey_from_blob_internal(b, keyp, 1);
|
|
|
|
sshbuf_free(b);
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
sshkey_fromb(struct sshbuf *b, struct sshkey **keyp)
|
|
|
|
{
|
|
|
|
return sshkey_from_blob_internal(b, keyp, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
sshkey_froms(struct sshbuf *buf, struct sshkey **keyp)
|
|
|
|
{
|
|
|
|
struct sshbuf *b;
|
|
|
|
int r;
|
|
|
|
|
|
|
|
if ((r = sshbuf_froms(buf, &b)) != 0)
|
|
|
|
return r;
|
|
|
|
r = sshkey_from_blob_internal(b, keyp, 1);
|
|
|
|
sshbuf_free(b);
|
|
|
|
return r;
|
2014-07-02 13:28:02 +08:00
|
|
|
}
|
|
|
|
|
2019-09-03 16:31:20 +08:00
|
|
|
int
|
|
|
|
sshkey_get_sigtype(const u_char *sig, size_t siglen, char **sigtypep)
|
2017-12-18 10:22:29 +08:00
|
|
|
{
|
|
|
|
int r;
|
|
|
|
struct sshbuf *b = NULL;
|
|
|
|
char *sigtype = NULL;
|
|
|
|
|
|
|
|
if (sigtypep != NULL)
|
|
|
|
*sigtypep = NULL;
|
|
|
|
if ((b = sshbuf_from(sig, siglen)) == NULL)
|
|
|
|
return SSH_ERR_ALLOC_FAIL;
|
|
|
|
if ((r = sshbuf_get_cstring(b, &sigtype, NULL)) != 0)
|
|
|
|
goto out;
|
|
|
|
/* success */
|
|
|
|
if (sigtypep != NULL) {
|
|
|
|
*sigtypep = sigtype;
|
|
|
|
sigtype = NULL;
|
|
|
|
}
|
|
|
|
r = 0;
|
|
|
|
out:
|
|
|
|
free(sigtype);
|
|
|
|
sshbuf_free(b);
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2018-09-12 09:32:54 +08:00
|
|
|
/*
|
|
|
|
*
|
|
|
|
* Checks whether a certificate's signature type is allowed.
|
|
|
|
* Returns 0 (success) if the certificate signature type appears in the
|
|
|
|
* "allowed" pattern-list, or the key is not a certificate to begin with.
|
|
|
|
* Otherwise returns a ssherr.h code.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
sshkey_check_cert_sigtype(const struct sshkey *key, const char *allowed)
|
|
|
|
{
|
|
|
|
if (key == NULL || allowed == NULL)
|
|
|
|
return SSH_ERR_INVALID_ARGUMENT;
|
|
|
|
if (!sshkey_type_is_cert(key->type))
|
|
|
|
return 0;
|
|
|
|
if (key->cert == NULL || key->cert->signature_type == NULL)
|
|
|
|
return SSH_ERR_INVALID_ARGUMENT;
|
|
|
|
if (match_pattern_list(key->cert->signature_type, allowed, 0) != 1)
|
|
|
|
return SSH_ERR_SIGN_ALG_UNSUPPORTED;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-07-03 19:39:54 +08:00
|
|
|
/*
|
|
|
|
* Returns the expected signature algorithm for a given public key algorithm.
|
|
|
|
*/
|
2018-07-03 21:20:25 +08:00
|
|
|
const char *
|
|
|
|
sshkey_sigalg_by_name(const char *name)
|
2018-07-03 19:39:54 +08:00
|
|
|
{
|
2022-10-28 08:35:40 +08:00
|
|
|
const struct sshkey_impl *impl;
|
|
|
|
int i;
|
2018-07-03 19:39:54 +08:00
|
|
|
|
2022-10-28 08:35:40 +08:00
|
|
|
for (i = 0; keyimpls[i] != NULL; i++) {
|
|
|
|
impl = keyimpls[i];
|
|
|
|
if (strcmp(impl->name, name) != 0)
|
2018-07-03 19:39:54 +08:00
|
|
|
continue;
|
2022-10-28 08:35:40 +08:00
|
|
|
if (impl->sigalg != NULL)
|
|
|
|
return impl->sigalg;
|
|
|
|
if (!impl->cert)
|
|
|
|
return impl->name;
|
2018-07-03 19:39:54 +08:00
|
|
|
return sshkey_ssh_name_from_type_nid(
|
2022-10-28 08:35:40 +08:00
|
|
|
sshkey_type_plain(impl->type), impl->nid);
|
2018-07-03 19:39:54 +08:00
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Verifies that the signature algorithm appearing inside the signature blob
|
|
|
|
* matches that which was requested.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
sshkey_check_sigtype(const u_char *sig, size_t siglen,
|
|
|
|
const char *requested_alg)
|
|
|
|
{
|
|
|
|
const char *expected_alg;
|
|
|
|
char *sigtype = NULL;
|
|
|
|
int r;
|
|
|
|
|
|
|
|
if (requested_alg == NULL)
|
|
|
|
return 0;
|
2018-07-03 21:20:25 +08:00
|
|
|
if ((expected_alg = sshkey_sigalg_by_name(requested_alg)) == NULL)
|
2018-07-03 19:39:54 +08:00
|
|
|
return SSH_ERR_INVALID_ARGUMENT;
|
2019-09-03 16:31:20 +08:00
|
|
|
if ((r = sshkey_get_sigtype(sig, siglen, &sigtype)) != 0)
|
2018-07-03 19:39:54 +08:00
|
|
|
return r;
|
|
|
|
r = strcmp(expected_alg, sigtype) == 0;
|
|
|
|
free(sigtype);
|
|
|
|
return r ? 0 : SSH_ERR_SIGN_ALG_UNSUPPORTED;
|
|
|
|
}
|
|
|
|
|
2014-07-02 13:28:02 +08:00
|
|
|
int
|
2019-06-21 12:21:04 +08:00
|
|
|
sshkey_sign(struct sshkey *key,
|
2014-07-02 13:28:02 +08:00
|
|
|
u_char **sigp, size_t *lenp,
|
2019-11-01 05:23:19 +08:00
|
|
|
const u_char *data, size_t datalen,
|
2020-08-27 09:06:18 +08:00
|
|
|
const char *alg, const char *sk_provider, const char *sk_pin, u_int compat)
|
2014-07-02 13:28:02 +08:00
|
|
|
{
|
2019-06-21 12:21:04 +08:00
|
|
|
int was_shielded = sshkey_is_shielded(key);
|
|
|
|
int r2, r = SSH_ERR_INTERNAL_ERROR;
|
2022-10-28 08:43:08 +08:00
|
|
|
const struct sshkey_impl *impl;
|
2019-06-21 12:21:04 +08:00
|
|
|
|
2014-07-02 13:28:02 +08:00
|
|
|
if (sigp != NULL)
|
|
|
|
*sigp = NULL;
|
|
|
|
if (lenp != NULL)
|
|
|
|
*lenp = 0;
|
|
|
|
if (datalen > SSH_KEY_MAX_SIGN_DATA_SIZE)
|
|
|
|
return SSH_ERR_INVALID_ARGUMENT;
|
2022-10-28 08:43:08 +08:00
|
|
|
if ((impl = sshkey_impl_from_key(key)) == NULL)
|
|
|
|
return SSH_ERR_KEY_TYPE_UNKNOWN;
|
2019-06-21 12:21:04 +08:00
|
|
|
if ((r = sshkey_unshield_private(key)) != 0)
|
|
|
|
return r;
|
2022-10-28 08:43:08 +08:00
|
|
|
if (sshkey_is_sk(key)) {
|
2019-12-14 03:09:10 +08:00
|
|
|
r = sshsk_sign(sk_provider, key, sigp, lenp, data,
|
2020-08-27 09:06:18 +08:00
|
|
|
datalen, compat, sk_pin);
|
2022-10-28 08:43:08 +08:00
|
|
|
} else {
|
|
|
|
if (impl->funcs->sign == NULL)
|
|
|
|
r = SSH_ERR_SIGN_ALG_UNSUPPORTED;
|
|
|
|
else {
|
|
|
|
r = impl->funcs->sign(key, sigp, lenp, data, datalen,
|
|
|
|
alg, sk_provider, sk_pin, compat);
|
|
|
|
}
|
2014-07-02 13:28:02 +08:00
|
|
|
}
|
2019-06-21 12:21:04 +08:00
|
|
|
if (was_shielded && (r2 = sshkey_shield_private(key)) != 0)
|
|
|
|
return r2;
|
|
|
|
return r;
|
2014-07-02 13:28:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* ssh_key_verify returns 0 for a correct signature and < 0 on error.
|
2017-12-18 10:25:15 +08:00
|
|
|
* If "alg" specified, then the signature must use that algorithm.
|
2014-07-02 13:28:02 +08:00
|
|
|
*/
|
|
|
|
int
|
|
|
|
sshkey_verify(const struct sshkey *key,
|
|
|
|
const u_char *sig, size_t siglen,
|
2019-11-25 08:51:37 +08:00
|
|
|
const u_char *data, size_t dlen, const char *alg, u_int compat,
|
|
|
|
struct sshkey_sig_details **detailsp)
|
2014-07-02 13:28:02 +08:00
|
|
|
{
|
2022-10-28 08:43:08 +08:00
|
|
|
const struct sshkey_impl *impl;
|
|
|
|
|
2019-11-25 08:51:37 +08:00
|
|
|
if (detailsp != NULL)
|
|
|
|
*detailsp = NULL;
|
2014-12-10 09:24:09 +08:00
|
|
|
if (siglen == 0 || dlen > SSH_KEY_MAX_SIGN_DATA_SIZE)
|
2014-07-02 13:28:02 +08:00
|
|
|
return SSH_ERR_INVALID_ARGUMENT;
|
2022-10-28 08:43:08 +08:00
|
|
|
if ((impl = sshkey_impl_from_key(key)) == NULL)
|
2014-07-02 13:28:02 +08:00
|
|
|
return SSH_ERR_KEY_TYPE_UNKNOWN;
|
2022-10-28 08:43:08 +08:00
|
|
|
return impl->funcs->verify(key, sig, siglen, data, dlen,
|
|
|
|
alg, compat, detailsp);
|
2014-07-02 13:28:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Convert a plain key to their _CERT equivalent */
|
|
|
|
int
|
2015-07-03 11:43:18 +08:00
|
|
|
sshkey_to_certified(struct sshkey *k)
|
2014-07-02 13:28:02 +08:00
|
|
|
{
|
|
|
|
int newtype;
|
|
|
|
|
2022-10-28 08:43:30 +08:00
|
|
|
if ((newtype = sshkey_type_certified(k->type)) == -1)
|
2014-07-02 13:28:02 +08:00
|
|
|
return SSH_ERR_INVALID_ARGUMENT;
|
|
|
|
if ((k->cert = cert_new()) == NULL)
|
|
|
|
return SSH_ERR_ALLOC_FAIL;
|
|
|
|
k->type = newtype;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Convert a certificate to its raw key equivalent */
|
|
|
|
int
|
|
|
|
sshkey_drop_cert(struct sshkey *k)
|
|
|
|
{
|
|
|
|
if (!sshkey_type_is_cert(k->type))
|
|
|
|
return SSH_ERR_KEY_TYPE_UNKNOWN;
|
|
|
|
cert_free(k->cert);
|
|
|
|
k->cert = NULL;
|
|
|
|
k->type = sshkey_type_plain(k->type);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Sign a certified key, (re-)generating the signed certblob. */
|
|
|
|
int
|
2017-06-28 09:09:22 +08:00
|
|
|
sshkey_certify_custom(struct sshkey *k, struct sshkey *ca, const char *alg,
|
2020-08-27 09:06:18 +08:00
|
|
|
const char *sk_provider, const char *sk_pin,
|
|
|
|
sshkey_certify_signer *signer, void *signer_ctx)
|
2014-07-02 13:28:02 +08:00
|
|
|
{
|
2022-10-28 08:43:30 +08:00
|
|
|
const struct sshkey_impl *impl;
|
2014-07-02 13:28:02 +08:00
|
|
|
struct sshbuf *principals = NULL;
|
|
|
|
u_char *ca_blob = NULL, *sig_blob = NULL, nonce[32];
|
|
|
|
size_t i, ca_len, sig_len;
|
|
|
|
int ret = SSH_ERR_INTERNAL_ERROR;
|
2018-09-12 09:31:30 +08:00
|
|
|
struct sshbuf *cert = NULL;
|
|
|
|
char *sigtype = NULL;
|
2014-07-02 13:28:02 +08:00
|
|
|
|
|
|
|
if (k == NULL || k->cert == NULL ||
|
|
|
|
k->cert->certblob == NULL || ca == NULL)
|
|
|
|
return SSH_ERR_INVALID_ARGUMENT;
|
|
|
|
if (!sshkey_is_cert(k))
|
|
|
|
return SSH_ERR_KEY_TYPE_UNKNOWN;
|
|
|
|
if (!sshkey_type_is_valid_ca(ca->type))
|
|
|
|
return SSH_ERR_KEY_CERT_INVALID_SIGN_KEY;
|
2022-10-28 08:43:30 +08:00
|
|
|
if ((impl = sshkey_impl_from_key(k)) == NULL)
|
|
|
|
return SSH_ERR_INTERNAL_ERROR;
|
2014-07-02 13:28:02 +08:00
|
|
|
|
2018-09-12 09:31:30 +08:00
|
|
|
/*
|
|
|
|
* If no alg specified as argument but a signature_type was set,
|
|
|
|
* then prefer that. If both were specified, then they must match.
|
|
|
|
*/
|
|
|
|
if (alg == NULL)
|
|
|
|
alg = k->cert->signature_type;
|
|
|
|
else if (k->cert->signature_type != NULL &&
|
|
|
|
strcmp(alg, k->cert->signature_type) != 0)
|
|
|
|
return SSH_ERR_INVALID_ARGUMENT;
|
|
|
|
|
2019-05-20 08:20:35 +08:00
|
|
|
/*
|
|
|
|
* If no signing algorithm or signature_type was specified and we're
|
|
|
|
* using a RSA key, then default to a good signature algorithm.
|
|
|
|
*/
|
|
|
|
if (alg == NULL && ca->type == KEY_RSA)
|
|
|
|
alg = "rsa-sha2-512";
|
|
|
|
|
2014-07-02 13:28:02 +08:00
|
|
|
if ((ret = sshkey_to_blob(ca, &ca_blob, &ca_len)) != 0)
|
|
|
|
return SSH_ERR_KEY_CERT_INVALID_SIGN_KEY;
|
|
|
|
|
|
|
|
cert = k->cert->certblob; /* for readability */
|
|
|
|
sshbuf_reset(cert);
|
|
|
|
if ((ret = sshbuf_put_cstring(cert, sshkey_ssh_name(k))) != 0)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
/* -v01 certs put nonce first */
|
|
|
|
arc4random_buf(&nonce, sizeof(nonce));
|
2015-07-03 11:43:18 +08:00
|
|
|
if ((ret = sshbuf_put_string(cert, nonce, sizeof(nonce))) != 0)
|
|
|
|
goto out;
|
2014-07-02 13:28:02 +08:00
|
|
|
|
2022-10-28 08:43:30 +08:00
|
|
|
/* Public key next */
|
|
|
|
if ((ret = impl->funcs->serialize_public(k, cert,
|
|
|
|
SSHKEY_SERIALIZE_DEFAULT)) != 0)
|
2015-03-06 09:40:56 +08:00
|
|
|
goto out;
|
2014-07-02 13:28:02 +08:00
|
|
|
|
2022-10-28 08:43:30 +08:00
|
|
|
/* Then remaining cert fields */
|
2015-07-03 11:43:18 +08:00
|
|
|
if ((ret = sshbuf_put_u64(cert, k->cert->serial)) != 0 ||
|
|
|
|
(ret = sshbuf_put_u32(cert, k->cert->type)) != 0 ||
|
2014-07-02 13:28:02 +08:00
|
|
|
(ret = sshbuf_put_cstring(cert, k->cert->key_id)) != 0)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
if ((principals = sshbuf_new()) == NULL) {
|
|
|
|
ret = SSH_ERR_ALLOC_FAIL;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
for (i = 0; i < k->cert->nprincipals; i++) {
|
|
|
|
if ((ret = sshbuf_put_cstring(principals,
|
|
|
|
k->cert->principals[i])) != 0)
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
if ((ret = sshbuf_put_stringb(cert, principals)) != 0 ||
|
|
|
|
(ret = sshbuf_put_u64(cert, k->cert->valid_after)) != 0 ||
|
|
|
|
(ret = sshbuf_put_u64(cert, k->cert->valid_before)) != 0 ||
|
2015-07-03 11:43:18 +08:00
|
|
|
(ret = sshbuf_put_stringb(cert, k->cert->critical)) != 0 ||
|
|
|
|
(ret = sshbuf_put_stringb(cert, k->cert->extensions)) != 0 ||
|
|
|
|
(ret = sshbuf_put_string(cert, NULL, 0)) != 0 || /* Reserved */
|
2014-07-02 13:28:02 +08:00
|
|
|
(ret = sshbuf_put_string(cert, ca_blob, ca_len)) != 0)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
/* Sign the whole mess */
|
2017-06-28 09:09:22 +08:00
|
|
|
if ((ret = signer(ca, &sig_blob, &sig_len, sshbuf_ptr(cert),
|
2020-08-27 09:06:18 +08:00
|
|
|
sshbuf_len(cert), alg, sk_provider, sk_pin, 0, signer_ctx)) != 0)
|
2014-07-02 13:28:02 +08:00
|
|
|
goto out;
|
2018-09-12 09:31:30 +08:00
|
|
|
/* Check and update signature_type against what was actually used */
|
2019-09-03 16:31:20 +08:00
|
|
|
if ((ret = sshkey_get_sigtype(sig_blob, sig_len, &sigtype)) != 0)
|
2018-09-12 09:31:30 +08:00
|
|
|
goto out;
|
|
|
|
if (alg != NULL && strcmp(alg, sigtype) != 0) {
|
|
|
|
ret = SSH_ERR_SIGN_ALG_UNSUPPORTED;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
if (k->cert->signature_type == NULL) {
|
|
|
|
k->cert->signature_type = sigtype;
|
|
|
|
sigtype = NULL;
|
|
|
|
}
|
2014-07-02 13:28:02 +08:00
|
|
|
/* Append signature and we are done */
|
|
|
|
if ((ret = sshbuf_put_string(cert, sig_blob, sig_len)) != 0)
|
|
|
|
goto out;
|
|
|
|
ret = 0;
|
|
|
|
out:
|
|
|
|
if (ret != 0)
|
|
|
|
sshbuf_reset(cert);
|
2015-12-11 01:08:40 +08:00
|
|
|
free(sig_blob);
|
|
|
|
free(ca_blob);
|
2018-09-12 09:31:30 +08:00
|
|
|
free(sigtype);
|
2015-12-11 12:21:11 +08:00
|
|
|
sshbuf_free(principals);
|
2014-07-02 13:28:02 +08:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-06-28 09:09:22 +08:00
|
|
|
static int
|
2019-06-21 12:21:04 +08:00
|
|
|
default_key_sign(struct sshkey *key, u_char **sigp, size_t *lenp,
|
2017-06-28 09:09:22 +08:00
|
|
|
const u_char *data, size_t datalen,
|
2020-08-27 09:06:18 +08:00
|
|
|
const char *alg, const char *sk_provider, const char *sk_pin,
|
|
|
|
u_int compat, void *ctx)
|
2017-06-28 09:09:22 +08:00
|
|
|
{
|
|
|
|
if (ctx != NULL)
|
|
|
|
return SSH_ERR_INVALID_ARGUMENT;
|
2019-11-01 05:23:19 +08:00
|
|
|
return sshkey_sign(key, sigp, lenp, data, datalen, alg,
|
2020-08-27 09:06:18 +08:00
|
|
|
sk_provider, sk_pin, compat);
|
2017-06-28 09:09:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2019-11-01 05:23:19 +08:00
|
|
|
sshkey_certify(struct sshkey *k, struct sshkey *ca, const char *alg,
|
2020-08-27 09:06:18 +08:00
|
|
|
const char *sk_provider, const char *sk_pin)
|
2017-06-28 09:09:22 +08:00
|
|
|
{
|
2020-08-27 09:06:18 +08:00
|
|
|
return sshkey_certify_custom(k, ca, alg, sk_provider, sk_pin,
|
2019-11-01 05:23:19 +08:00
|
|
|
default_key_sign, NULL);
|
2017-06-28 09:09:22 +08:00
|
|
|
}
|
|
|
|
|
2014-07-02 13:28:02 +08:00
|
|
|
int
|
|
|
|
sshkey_cert_check_authority(const struct sshkey *k,
|
2021-01-26 08:49:30 +08:00
|
|
|
int want_host, int require_principal, int wildcard_pattern,
|
2021-07-23 11:37:52 +08:00
|
|
|
uint64_t verify_time, const char *name, const char **reason)
|
2014-07-02 13:28:02 +08:00
|
|
|
{
|
|
|
|
u_int i, principal_matches;
|
|
|
|
|
2020-03-07 02:23:17 +08:00
|
|
|
if (reason == NULL)
|
|
|
|
return SSH_ERR_INVALID_ARGUMENT;
|
2021-01-26 08:49:30 +08:00
|
|
|
if (!sshkey_is_cert(k)) {
|
|
|
|
*reason = "Key is not a certificate";
|
|
|
|
return SSH_ERR_KEY_CERT_INVALID;
|
|
|
|
}
|
2014-07-02 13:28:02 +08:00
|
|
|
if (want_host) {
|
|
|
|
if (k->cert->type != SSH2_CERT_TYPE_HOST) {
|
|
|
|
*reason = "Certificate invalid: not a host certificate";
|
|
|
|
return SSH_ERR_KEY_CERT_INVALID;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (k->cert->type != SSH2_CERT_TYPE_USER) {
|
|
|
|
*reason = "Certificate invalid: not a user certificate";
|
|
|
|
return SSH_ERR_KEY_CERT_INVALID;
|
|
|
|
}
|
|
|
|
}
|
2021-07-23 11:37:52 +08:00
|
|
|
if (verify_time < k->cert->valid_after) {
|
2014-07-02 13:28:02 +08:00
|
|
|
*reason = "Certificate invalid: not yet valid";
|
|
|
|
return SSH_ERR_KEY_CERT_INVALID;
|
|
|
|
}
|
2021-07-23 11:37:52 +08:00
|
|
|
if (verify_time >= k->cert->valid_before) {
|
2014-07-02 13:28:02 +08:00
|
|
|
*reason = "Certificate invalid: expired";
|
|
|
|
return SSH_ERR_KEY_CERT_INVALID;
|
|
|
|
}
|
|
|
|
if (k->cert->nprincipals == 0) {
|
|
|
|
if (require_principal) {
|
|
|
|
*reason = "Certificate lacks principal list";
|
|
|
|
return SSH_ERR_KEY_CERT_INVALID;
|
|
|
|
}
|
|
|
|
} else if (name != NULL) {
|
|
|
|
principal_matches = 0;
|
|
|
|
for (i = 0; i < k->cert->nprincipals; i++) {
|
2021-01-26 08:49:30 +08:00
|
|
|
if (wildcard_pattern) {
|
|
|
|
if (match_pattern(k->cert->principals[i],
|
|
|
|
name)) {
|
|
|
|
principal_matches = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else if (strcmp(name, k->cert->principals[i]) == 0) {
|
2014-07-02 13:28:02 +08:00
|
|
|
principal_matches = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!principal_matches) {
|
|
|
|
*reason = "Certificate invalid: name is not a listed "
|
|
|
|
"principal";
|
|
|
|
return SSH_ERR_KEY_CERT_INVALID;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-07-23 11:37:52 +08:00
|
|
|
int
|
|
|
|
sshkey_cert_check_authority_now(const struct sshkey *k,
|
|
|
|
int want_host, int require_principal, int wildcard_pattern,
|
|
|
|
const char *name, const char **reason)
|
|
|
|
{
|
|
|
|
time_t now;
|
|
|
|
|
|
|
|
if ((now = time(NULL)) < 0) {
|
|
|
|
/* yikes - system clock before epoch! */
|
|
|
|
*reason = "Certificate invalid: not yet valid";
|
|
|
|
return SSH_ERR_KEY_CERT_INVALID;
|
|
|
|
}
|
|
|
|
return sshkey_cert_check_authority(k, want_host, require_principal,
|
|
|
|
wildcard_pattern, (uint64_t)now, name, reason);
|
|
|
|
}
|
|
|
|
|
2021-01-26 08:49:30 +08:00
|
|
|
int
|
|
|
|
sshkey_cert_check_host(const struct sshkey *key, const char *host,
|
|
|
|
int wildcard_principals, const char *ca_sign_algorithms,
|
|
|
|
const char **reason)
|
|
|
|
{
|
|
|
|
int r;
|
|
|
|
|
2021-07-23 11:37:52 +08:00
|
|
|
if ((r = sshkey_cert_check_authority_now(key, 1, 0, wildcard_principals,
|
2021-01-26 08:49:30 +08:00
|
|
|
host, reason)) != 0)
|
|
|
|
return r;
|
|
|
|
if (sshbuf_len(key->cert->critical) != 0) {
|
|
|
|
*reason = "Certificate contains unsupported critical options";
|
|
|
|
return SSH_ERR_KEY_CERT_INVALID;
|
|
|
|
}
|
|
|
|
if (ca_sign_algorithms != NULL &&
|
|
|
|
(r = sshkey_check_cert_sigtype(key, ca_sign_algorithms)) != 0) {
|
|
|
|
*reason = "Certificate signed with disallowed algorithm";
|
|
|
|
return SSH_ERR_KEY_CERT_INVALID;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-11-19 09:08:55 +08:00
|
|
|
size_t
|
|
|
|
sshkey_format_cert_validity(const struct sshkey_cert *cert, char *s, size_t l)
|
|
|
|
{
|
2021-01-15 12:31:25 +08:00
|
|
|
char from[32], to[32], ret[128];
|
2015-11-19 09:08:55 +08:00
|
|
|
|
|
|
|
*from = *to = '\0';
|
|
|
|
if (cert->valid_after == 0 &&
|
|
|
|
cert->valid_before == 0xffffffffffffffffULL)
|
|
|
|
return strlcpy(s, "forever", l);
|
|
|
|
|
2021-07-12 14:08:57 +08:00
|
|
|
if (cert->valid_after != 0)
|
|
|
|
format_absolute_time(cert->valid_after, from, sizeof(from));
|
|
|
|
if (cert->valid_before != 0xffffffffffffffffULL)
|
|
|
|
format_absolute_time(cert->valid_before, to, sizeof(to));
|
2015-11-19 09:08:55 +08:00
|
|
|
|
|
|
|
if (cert->valid_after == 0)
|
|
|
|
snprintf(ret, sizeof(ret), "before %s", to);
|
|
|
|
else if (cert->valid_before == 0xffffffffffffffffULL)
|
|
|
|
snprintf(ret, sizeof(ret), "after %s", from);
|
|
|
|
else
|
|
|
|
snprintf(ret, sizeof(ret), "from %s to %s", from, to);
|
|
|
|
|
|
|
|
return strlcpy(s, ret, l);
|
|
|
|
}
|
|
|
|
|
2022-10-28 08:44:17 +08:00
|
|
|
/* Common serialization for FIDO private keys */
|
|
|
|
int
|
|
|
|
sshkey_serialize_private_sk(const struct sshkey *key, struct sshbuf *b)
|
|
|
|
{
|
|
|
|
int r;
|
|
|
|
|
|
|
|
if ((r = sshbuf_put_cstring(b, key->sk_application)) != 0 ||
|
|
|
|
(r = sshbuf_put_u8(b, key->sk_flags)) != 0 ||
|
|
|
|
(r = sshbuf_put_stringb(b, key->sk_key_handle)) != 0 ||
|
|
|
|
(r = sshbuf_put_stringb(b, key->sk_reserved)) != 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-07-02 13:28:02 +08:00
|
|
|
int
|
2019-06-21 12:21:04 +08:00
|
|
|
sshkey_private_serialize_opt(struct sshkey *key, struct sshbuf *buf,
|
2018-02-23 23:58:37 +08:00
|
|
|
enum sshkey_serialize_rep opts)
|
2014-07-02 13:28:02 +08:00
|
|
|
{
|
|
|
|
int r = SSH_ERR_INTERNAL_ERROR;
|
2019-06-21 12:21:04 +08:00
|
|
|
int was_shielded = sshkey_is_shielded(key);
|
|
|
|
struct sshbuf *b = NULL;
|
2022-10-28 08:44:17 +08:00
|
|
|
const struct sshkey_impl *impl;
|
2014-07-02 13:28:02 +08:00
|
|
|
|
2022-10-28 08:44:17 +08:00
|
|
|
if ((impl = sshkey_impl_from_key(key)) == NULL)
|
|
|
|
return SSH_ERR_INTERNAL_ERROR;
|
2019-06-21 12:21:04 +08:00
|
|
|
if ((r = sshkey_unshield_private(key)) != 0)
|
|
|
|
return r;
|
|
|
|
if ((b = sshbuf_new()) == NULL)
|
|
|
|
return SSH_ERR_ALLOC_FAIL;
|
2014-07-02 13:28:02 +08:00
|
|
|
if ((r = sshbuf_put_cstring(b, sshkey_ssh_name(key))) != 0)
|
|
|
|
goto out;
|
2022-10-28 08:44:17 +08:00
|
|
|
if (sshkey_is_cert(key)) {
|
|
|
|
if (key->cert == NULL ||
|
|
|
|
sshbuf_len(key->cert->certblob) == 0) {
|
2014-07-02 13:28:02 +08:00
|
|
|
r = SSH_ERR_INVALID_ARGUMENT;
|
|
|
|
goto out;
|
|
|
|
}
|
2022-10-28 08:44:17 +08:00
|
|
|
if ((r = sshbuf_put_stringb(b, key->cert->certblob)) != 0)
|
2019-11-13 03:33:08 +08:00
|
|
|
goto out;
|
2014-07-02 13:28:02 +08:00
|
|
|
}
|
2022-10-28 08:44:17 +08:00
|
|
|
if ((r = impl->funcs->serialize_private(key, b, opts)) != 0)
|
|
|
|
goto out;
|
|
|
|
|
2019-06-21 12:21:04 +08:00
|
|
|
/*
|
|
|
|
* success (but we still need to append the output to buf after
|
|
|
|
* possibly re-shielding the private key)
|
|
|
|
*/
|
2014-07-02 13:28:02 +08:00
|
|
|
r = 0;
|
|
|
|
out:
|
2019-06-21 12:21:04 +08:00
|
|
|
if (was_shielded)
|
|
|
|
r = sshkey_shield_private(key);
|
|
|
|
if (r == 0)
|
|
|
|
r = sshbuf_putb(buf, b);
|
|
|
|
sshbuf_free(b);
|
|
|
|
|
2014-07-02 13:28:02 +08:00
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2018-02-23 23:58:37 +08:00
|
|
|
int
|
2019-06-21 12:21:04 +08:00
|
|
|
sshkey_private_serialize(struct sshkey *key, struct sshbuf *b)
|
2018-02-23 23:58:37 +08:00
|
|
|
{
|
|
|
|
return sshkey_private_serialize_opt(key, b,
|
|
|
|
SSHKEY_SERIALIZE_DEFAULT);
|
|
|
|
}
|
|
|
|
|
2022-10-28 08:44:44 +08:00
|
|
|
/* Shared deserialization of FIDO private key components */
|
|
|
|
int
|
|
|
|
sshkey_private_deserialize_sk(struct sshbuf *buf, struct sshkey *k)
|
|
|
|
{
|
|
|
|
int r;
|
|
|
|
|
|
|
|
if ((k->sk_key_handle = sshbuf_new()) == NULL ||
|
|
|
|
(k->sk_reserved = sshbuf_new()) == NULL)
|
|
|
|
return SSH_ERR_ALLOC_FAIL;
|
|
|
|
if ((r = sshbuf_get_cstring(buf, &k->sk_application, NULL)) != 0 ||
|
|
|
|
(r = sshbuf_get_u8(buf, &k->sk_flags)) != 0 ||
|
|
|
|
(r = sshbuf_get_stringb(buf, k->sk_key_handle)) != 0 ||
|
|
|
|
(r = sshbuf_get_stringb(buf, k->sk_reserved)) != 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-07-02 13:28:02 +08:00
|
|
|
int
|
|
|
|
sshkey_private_deserialize(struct sshbuf *buf, struct sshkey **kp)
|
|
|
|
{
|
2022-10-28 08:44:44 +08:00
|
|
|
const struct sshkey_impl *impl;
|
|
|
|
char *tname = NULL;
|
2021-02-03 06:36:46 +08:00
|
|
|
char *expect_sk_application = NULL;
|
2022-10-28 08:44:44 +08:00
|
|
|
u_char *expect_ed25519_pk = NULL;
|
2014-07-02 13:28:02 +08:00
|
|
|
struct sshkey *k = NULL;
|
|
|
|
int type, r = SSH_ERR_INTERNAL_ERROR;
|
|
|
|
|
|
|
|
if (kp != NULL)
|
|
|
|
*kp = NULL;
|
|
|
|
if ((r = sshbuf_get_cstring(buf, &tname, NULL)) != 0)
|
|
|
|
goto out;
|
|
|
|
type = sshkey_type_from_name(tname);
|
2020-04-11 18:16:11 +08:00
|
|
|
if (sshkey_type_is_cert(type)) {
|
|
|
|
/*
|
|
|
|
* Certificate key private keys begin with the certificate
|
|
|
|
* itself. Make sure this matches the type of the enclosing
|
|
|
|
* private key.
|
|
|
|
*/
|
|
|
|
if ((r = sshkey_froms(buf, &k)) != 0)
|
|
|
|
goto out;
|
|
|
|
if (k->type != type) {
|
|
|
|
r = SSH_ERR_KEY_CERT_MISMATCH;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
/* For ECDSA keys, the group must match too */
|
|
|
|
if (k->type == KEY_ECDSA &&
|
|
|
|
k->ecdsa_nid != sshkey_ecdsa_nid_from_name(tname)) {
|
|
|
|
r = SSH_ERR_KEY_CERT_MISMATCH;
|
|
|
|
goto out;
|
|
|
|
}
|
2021-02-03 06:36:46 +08:00
|
|
|
/*
|
|
|
|
* Several fields are redundant between certificate and
|
|
|
|
* private key body, we require these to match.
|
|
|
|
*/
|
|
|
|
expect_sk_application = k->sk_application;
|
|
|
|
expect_ed25519_pk = k->ed25519_pk;
|
|
|
|
k->sk_application = NULL;
|
|
|
|
k->ed25519_pk = NULL;
|
2022-10-28 08:44:44 +08:00
|
|
|
/* XXX xmss too or refactor */
|
2020-04-11 18:16:11 +08:00
|
|
|
} else {
|
2018-09-14 12:17:44 +08:00
|
|
|
if ((k = sshkey_new(type)) == NULL) {
|
2014-07-02 13:28:02 +08:00
|
|
|
r = SSH_ERR_ALLOC_FAIL;
|
|
|
|
goto out;
|
|
|
|
}
|
2020-04-11 18:16:11 +08:00
|
|
|
}
|
2022-10-28 08:44:44 +08:00
|
|
|
if ((impl = sshkey_impl_from_type(type)) == NULL) {
|
|
|
|
r = SSH_ERR_INTERNAL_ERROR;
|
2014-07-02 13:28:02 +08:00
|
|
|
goto out;
|
|
|
|
}
|
2022-10-28 08:44:44 +08:00
|
|
|
if ((r = impl->funcs->deserialize_private(tname, buf, k)) != 0)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
/* XXX xmss too or refactor */
|
2021-02-03 06:36:46 +08:00
|
|
|
if ((expect_sk_application != NULL && (k->sk_application == NULL ||
|
|
|
|
strcmp(expect_sk_application, k->sk_application) != 0)) ||
|
|
|
|
(expect_ed25519_pk != NULL && (k->ed25519_pk == NULL ||
|
2021-04-03 14:18:40 +08:00
|
|
|
memcmp(expect_ed25519_pk, k->ed25519_pk, ED25519_PK_SZ) != 0))) {
|
2021-02-03 06:36:46 +08:00
|
|
|
r = SSH_ERR_KEY_CERT_MISMATCH;
|
|
|
|
goto out;
|
|
|
|
}
|
2014-07-02 13:28:02 +08:00
|
|
|
/* success */
|
|
|
|
r = 0;
|
|
|
|
if (kp != NULL) {
|
|
|
|
*kp = k;
|
|
|
|
k = NULL;
|
|
|
|
}
|
|
|
|
out:
|
|
|
|
free(tname);
|
|
|
|
sshkey_free(k);
|
2021-02-03 06:36:46 +08:00
|
|
|
free(expect_sk_application);
|
|
|
|
free(expect_ed25519_pk);
|
2014-07-02 13:28:02 +08:00
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if defined(WITH_OPENSSL) && defined(OPENSSL_HAS_ECC)
|
|
|
|
int
|
|
|
|
sshkey_ec_validate_public(const EC_GROUP *group, const EC_POINT *public)
|
|
|
|
{
|
|
|
|
EC_POINT *nq = NULL;
|
2019-11-15 14:00:20 +08:00
|
|
|
BIGNUM *order = NULL, *x = NULL, *y = NULL, *tmp = NULL;
|
2014-07-02 13:28:02 +08:00
|
|
|
int ret = SSH_ERR_KEY_INVALID_EC_VALUE;
|
|
|
|
|
2016-10-05 05:34:40 +08:00
|
|
|
/*
|
|
|
|
* NB. This assumes OpenSSL has already verified that the public
|
|
|
|
* point lies on the curve. This is done by EC_POINT_oct2point()
|
|
|
|
* implicitly calling EC_POINT_is_on_curve(). If this code is ever
|
|
|
|
* reachable with public points not unmarshalled using
|
|
|
|
* EC_POINT_oct2point then the caller will need to explicitly check.
|
|
|
|
*/
|
|
|
|
|
2014-07-02 13:28:02 +08:00
|
|
|
/*
|
|
|
|
* We shouldn't ever hit this case because bignum_get_ecpoint()
|
|
|
|
* refuses to load GF2m points.
|
|
|
|
*/
|
|
|
|
if (EC_METHOD_get_field_type(EC_GROUP_method_of(group)) !=
|
|
|
|
NID_X9_62_prime_field)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
/* Q != infinity */
|
|
|
|
if (EC_POINT_is_at_infinity(group, public))
|
|
|
|
goto out;
|
|
|
|
|
2019-11-15 14:00:20 +08:00
|
|
|
if ((x = BN_new()) == NULL ||
|
|
|
|
(y = BN_new()) == NULL ||
|
|
|
|
(order = BN_new()) == NULL ||
|
|
|
|
(tmp = BN_new()) == NULL) {
|
2014-07-02 13:28:02 +08:00
|
|
|
ret = SSH_ERR_ALLOC_FAIL;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* log2(x) > log2(order)/2, log2(y) > log2(order)/2 */
|
2019-11-15 14:00:20 +08:00
|
|
|
if (EC_GROUP_get_order(group, order, NULL) != 1 ||
|
2014-07-02 13:28:02 +08:00
|
|
|
EC_POINT_get_affine_coordinates_GFp(group, public,
|
2019-11-15 14:00:20 +08:00
|
|
|
x, y, NULL) != 1) {
|
2014-07-02 13:28:02 +08:00
|
|
|
ret = SSH_ERR_LIBCRYPTO_ERROR;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
if (BN_num_bits(x) <= BN_num_bits(order) / 2 ||
|
|
|
|
BN_num_bits(y) <= BN_num_bits(order) / 2)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
/* nQ == infinity (n == order of subgroup) */
|
|
|
|
if ((nq = EC_POINT_new(group)) == NULL) {
|
|
|
|
ret = SSH_ERR_ALLOC_FAIL;
|
|
|
|
goto out;
|
|
|
|
}
|
2019-11-15 14:00:20 +08:00
|
|
|
if (EC_POINT_mul(group, nq, NULL, public, order, NULL) != 1) {
|
2014-07-02 13:28:02 +08:00
|
|
|
ret = SSH_ERR_LIBCRYPTO_ERROR;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
if (EC_POINT_is_at_infinity(group, nq) != 1)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
/* x < order - 1, y < order - 1 */
|
|
|
|
if (!BN_sub(tmp, order, BN_value_one())) {
|
|
|
|
ret = SSH_ERR_LIBCRYPTO_ERROR;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
if (BN_cmp(x, tmp) >= 0 || BN_cmp(y, tmp) >= 0)
|
|
|
|
goto out;
|
|
|
|
ret = 0;
|
|
|
|
out:
|
2019-11-15 14:00:20 +08:00
|
|
|
BN_clear_free(x);
|
|
|
|
BN_clear_free(y);
|
|
|
|
BN_clear_free(order);
|
|
|
|
BN_clear_free(tmp);
|
2018-02-07 10:06:50 +08:00
|
|
|
EC_POINT_free(nq);
|
2014-07-02 13:28:02 +08:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
sshkey_ec_validate_private(const EC_KEY *key)
|
|
|
|
{
|
2019-11-15 14:00:20 +08:00
|
|
|
BIGNUM *order = NULL, *tmp = NULL;
|
2014-07-02 13:28:02 +08:00
|
|
|
int ret = SSH_ERR_KEY_INVALID_EC_VALUE;
|
|
|
|
|
2019-11-15 14:00:20 +08:00
|
|
|
if ((order = BN_new()) == NULL || (tmp = BN_new()) == NULL) {
|
2014-07-02 13:28:02 +08:00
|
|
|
ret = SSH_ERR_ALLOC_FAIL;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* log2(private) > log2(order)/2 */
|
2019-11-15 14:00:20 +08:00
|
|
|
if (EC_GROUP_get_order(EC_KEY_get0_group(key), order, NULL) != 1) {
|
2014-07-02 13:28:02 +08:00
|
|
|
ret = SSH_ERR_LIBCRYPTO_ERROR;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
if (BN_num_bits(EC_KEY_get0_private_key(key)) <=
|
|
|
|
BN_num_bits(order) / 2)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
/* private < order - 1 */
|
|
|
|
if (!BN_sub(tmp, order, BN_value_one())) {
|
|
|
|
ret = SSH_ERR_LIBCRYPTO_ERROR;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
if (BN_cmp(EC_KEY_get0_private_key(key), tmp) >= 0)
|
|
|
|
goto out;
|
|
|
|
ret = 0;
|
|
|
|
out:
|
2019-11-15 14:00:20 +08:00
|
|
|
BN_clear_free(order);
|
|
|
|
BN_clear_free(tmp);
|
2014-07-02 13:28:02 +08:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
sshkey_dump_ec_point(const EC_GROUP *group, const EC_POINT *point)
|
|
|
|
{
|
2019-11-15 14:00:20 +08:00
|
|
|
BIGNUM *x = NULL, *y = NULL;
|
2014-07-02 13:28:02 +08:00
|
|
|
|
|
|
|
if (point == NULL) {
|
|
|
|
fputs("point=(NULL)\n", stderr);
|
|
|
|
return;
|
|
|
|
}
|
2019-11-15 14:00:20 +08:00
|
|
|
if ((x = BN_new()) == NULL || (y = BN_new()) == NULL) {
|
|
|
|
fprintf(stderr, "%s: BN_new failed\n", __func__);
|
|
|
|
goto out;
|
2014-07-02 13:28:02 +08:00
|
|
|
}
|
|
|
|
if (EC_METHOD_get_field_type(EC_GROUP_method_of(group)) !=
|
|
|
|
NID_X9_62_prime_field) {
|
|
|
|
fprintf(stderr, "%s: group is not a prime field\n", __func__);
|
2019-11-15 14:00:20 +08:00
|
|
|
goto out;
|
2014-07-02 13:28:02 +08:00
|
|
|
}
|
2019-11-15 14:00:20 +08:00
|
|
|
if (EC_POINT_get_affine_coordinates_GFp(group, point,
|
|
|
|
x, y, NULL) != 1) {
|
2014-07-02 13:28:02 +08:00
|
|
|
fprintf(stderr, "%s: EC_POINT_get_affine_coordinates_GFp\n",
|
|
|
|
__func__);
|
2019-11-15 14:00:20 +08:00
|
|
|
goto out;
|
2014-07-02 13:28:02 +08:00
|
|
|
}
|
|
|
|
fputs("x=", stderr);
|
|
|
|
BN_print_fp(stderr, x);
|
|
|
|
fputs("\ny=", stderr);
|
|
|
|
BN_print_fp(stderr, y);
|
|
|
|
fputs("\n", stderr);
|
2019-11-15 14:00:20 +08:00
|
|
|
out:
|
|
|
|
BN_clear_free(x);
|
|
|
|
BN_clear_free(y);
|
2014-07-02 13:28:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
sshkey_dump_ec_key(const EC_KEY *key)
|
|
|
|
{
|
|
|
|
const BIGNUM *exponent;
|
|
|
|
|
|
|
|
sshkey_dump_ec_point(EC_KEY_get0_group(key),
|
|
|
|
EC_KEY_get0_public_key(key));
|
|
|
|
fputs("exponent=", stderr);
|
|
|
|
if ((exponent = EC_KEY_get0_private_key(key)) == NULL)
|
|
|
|
fputs("(NULL)", stderr);
|
|
|
|
else
|
|
|
|
BN_print_fp(stderr, EC_KEY_get0_private_key(key));
|
|
|
|
fputs("\n", stderr);
|
|
|
|
}
|
|
|
|
#endif /* WITH_OPENSSL && OPENSSL_HAS_ECC */
|
|
|
|
|
|
|
|
static int
|
2019-06-21 12:21:04 +08:00
|
|
|
sshkey_private_to_blob2(struct sshkey *prv, struct sshbuf *blob,
|
2014-07-02 13:28:02 +08:00
|
|
|
const char *passphrase, const char *comment, const char *ciphername,
|
|
|
|
int rounds)
|
|
|
|
{
|
2014-10-09 05:45:48 +08:00
|
|
|
u_char *cp, *key = NULL, *pubkeyblob = NULL;
|
2014-07-02 13:28:02 +08:00
|
|
|
u_char salt[SALT_LEN];
|
|
|
|
size_t i, pubkeylen, keylen, ivlen, blocksize, authlen;
|
|
|
|
u_int check;
|
|
|
|
int r = SSH_ERR_INTERNAL_ERROR;
|
2016-08-03 13:41:57 +08:00
|
|
|
struct sshcipher_ctx *ciphercontext = NULL;
|
2014-07-02 13:28:02 +08:00
|
|
|
const struct sshcipher *cipher;
|
|
|
|
const char *kdfname = KDFNAME;
|
|
|
|
struct sshbuf *encoded = NULL, *encrypted = NULL, *kdf = NULL;
|
|
|
|
|
|
|
|
if (rounds <= 0)
|
|
|
|
rounds = DEFAULT_ROUNDS;
|
|
|
|
if (passphrase == NULL || !strlen(passphrase)) {
|
|
|
|
ciphername = "none";
|
|
|
|
kdfname = "none";
|
|
|
|
} else if (ciphername == NULL)
|
|
|
|
ciphername = DEFAULT_CIPHERNAME;
|
|
|
|
if ((cipher = cipher_by_name(ciphername)) == NULL) {
|
2017-05-01 07:15:04 +08:00
|
|
|
r = SSH_ERR_INVALID_ARGUMENT;
|
2014-07-02 13:28:02 +08:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((kdf = sshbuf_new()) == NULL ||
|
|
|
|
(encoded = sshbuf_new()) == NULL ||
|
|
|
|
(encrypted = sshbuf_new()) == NULL) {
|
|
|
|
r = SSH_ERR_ALLOC_FAIL;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
blocksize = cipher_blocksize(cipher);
|
|
|
|
keylen = cipher_keylen(cipher);
|
|
|
|
ivlen = cipher_ivlen(cipher);
|
|
|
|
authlen = cipher_authlen(cipher);
|
|
|
|
if ((key = calloc(1, keylen + ivlen)) == NULL) {
|
|
|
|
r = SSH_ERR_ALLOC_FAIL;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
if (strcmp(kdfname, "bcrypt") == 0) {
|
|
|
|
arc4random_buf(salt, SALT_LEN);
|
|
|
|
if (bcrypt_pbkdf(passphrase, strlen(passphrase),
|
|
|
|
salt, SALT_LEN, key, keylen + ivlen, rounds) < 0) {
|
|
|
|
r = SSH_ERR_INVALID_ARGUMENT;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
if ((r = sshbuf_put_string(kdf, salt, SALT_LEN)) != 0 ||
|
|
|
|
(r = sshbuf_put_u32(kdf, rounds)) != 0)
|
|
|
|
goto out;
|
|
|
|
} else if (strcmp(kdfname, "none") != 0) {
|
|
|
|
/* Unsupported KDF type */
|
|
|
|
r = SSH_ERR_KEY_UNKNOWN_CIPHER;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
if ((r = cipher_init(&ciphercontext, cipher, key, keylen,
|
|
|
|
key + keylen, ivlen, 1)) != 0)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
if ((r = sshbuf_put(encoded, AUTH_MAGIC, sizeof(AUTH_MAGIC))) != 0 ||
|
|
|
|
(r = sshbuf_put_cstring(encoded, ciphername)) != 0 ||
|
|
|
|
(r = sshbuf_put_cstring(encoded, kdfname)) != 0 ||
|
|
|
|
(r = sshbuf_put_stringb(encoded, kdf)) != 0 ||
|
|
|
|
(r = sshbuf_put_u32(encoded, 1)) != 0 || /* number of keys */
|
|
|
|
(r = sshkey_to_blob(prv, &pubkeyblob, &pubkeylen)) != 0 ||
|
|
|
|
(r = sshbuf_put_string(encoded, pubkeyblob, pubkeylen)) != 0)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
/* set up the buffer that will be encrypted */
|
|
|
|
|
|
|
|
/* Random check bytes */
|
|
|
|
check = arc4random();
|
|
|
|
if ((r = sshbuf_put_u32(encrypted, check)) != 0 ||
|
|
|
|
(r = sshbuf_put_u32(encrypted, check)) != 0)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
/* append private key and comment*/
|
2018-02-23 23:58:37 +08:00
|
|
|
if ((r = sshkey_private_serialize_opt(prv, encrypted,
|
2021-04-03 14:18:40 +08:00
|
|
|
SSHKEY_SERIALIZE_FULL)) != 0 ||
|
2014-07-02 13:28:02 +08:00
|
|
|
(r = sshbuf_put_cstring(encrypted, comment)) != 0)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
/* padding */
|
|
|
|
i = 0;
|
|
|
|
while (sshbuf_len(encrypted) % blocksize) {
|
|
|
|
if ((r = sshbuf_put_u8(encrypted, ++i & 0xff)) != 0)
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* length in destination buffer */
|
|
|
|
if ((r = sshbuf_put_u32(encoded, sshbuf_len(encrypted))) != 0)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
/* encrypt */
|
|
|
|
if ((r = sshbuf_reserve(encoded,
|
|
|
|
sshbuf_len(encrypted) + authlen, &cp)) != 0)
|
|
|
|
goto out;
|
2016-08-03 13:41:57 +08:00
|
|
|
if ((r = cipher_crypt(ciphercontext, 0, cp,
|
2014-07-02 13:28:02 +08:00
|
|
|
sshbuf_ptr(encrypted), sshbuf_len(encrypted), 0, authlen)) != 0)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
sshbuf_reset(blob);
|
2019-07-16 21:18:39 +08:00
|
|
|
|
|
|
|
/* assemble uuencoded key */
|
|
|
|
if ((r = sshbuf_put(blob, MARK_BEGIN, MARK_BEGIN_LEN)) != 0 ||
|
|
|
|
(r = sshbuf_dtob64(encoded, blob, 1)) != 0 ||
|
|
|
|
(r = sshbuf_put(blob, MARK_END, MARK_END_LEN)) != 0)
|
2014-07-02 13:28:02 +08:00
|
|
|
goto out;
|
|
|
|
|
|
|
|
/* success */
|
|
|
|
r = 0;
|
|
|
|
|
|
|
|
out:
|
|
|
|
sshbuf_free(kdf);
|
|
|
|
sshbuf_free(encoded);
|
|
|
|
sshbuf_free(encrypted);
|
2016-08-03 13:41:57 +08:00
|
|
|
cipher_free(ciphercontext);
|
2014-07-02 13:28:02 +08:00
|
|
|
explicit_bzero(salt, sizeof(salt));
|
2020-02-26 21:40:09 +08:00
|
|
|
if (key != NULL)
|
|
|
|
freezero(key, keylen + ivlen);
|
2022-05-05 09:04:14 +08:00
|
|
|
if (pubkeyblob != NULL)
|
2020-02-26 21:40:09 +08:00
|
|
|
freezero(pubkeyblob, pubkeylen);
|
2014-07-02 13:28:02 +08:00
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2020-04-08 08:01:52 +08:00
|
|
|
private2_uudecode(struct sshbuf *blob, struct sshbuf **decodedp)
|
2014-07-02 13:28:02 +08:00
|
|
|
{
|
|
|
|
const u_char *cp;
|
|
|
|
size_t encoded_len;
|
2020-04-08 08:01:52 +08:00
|
|
|
int r;
|
|
|
|
u_char last;
|
2014-07-02 13:28:02 +08:00
|
|
|
struct sshbuf *encoded = NULL, *decoded = NULL;
|
|
|
|
|
2020-04-08 08:01:52 +08:00
|
|
|
if (blob == NULL || decodedp == NULL)
|
|
|
|
return SSH_ERR_INVALID_ARGUMENT;
|
|
|
|
|
|
|
|
*decodedp = NULL;
|
2014-07-02 13:28:02 +08:00
|
|
|
|
|
|
|
if ((encoded = sshbuf_new()) == NULL ||
|
2020-04-08 08:01:52 +08:00
|
|
|
(decoded = sshbuf_new()) == NULL) {
|
2014-07-02 13:28:02 +08:00
|
|
|
r = SSH_ERR_ALLOC_FAIL;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* check preamble */
|
|
|
|
cp = sshbuf_ptr(blob);
|
|
|
|
encoded_len = sshbuf_len(blob);
|
|
|
|
if (encoded_len < (MARK_BEGIN_LEN + MARK_END_LEN) ||
|
|
|
|
memcmp(cp, MARK_BEGIN, MARK_BEGIN_LEN) != 0) {
|
|
|
|
r = SSH_ERR_INVALID_FORMAT;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
cp += MARK_BEGIN_LEN;
|
|
|
|
encoded_len -= MARK_BEGIN_LEN;
|
|
|
|
|
|
|
|
/* Look for end marker, removing whitespace as we go */
|
|
|
|
while (encoded_len > 0) {
|
|
|
|
if (*cp != '\n' && *cp != '\r') {
|
|
|
|
if ((r = sshbuf_put_u8(encoded, *cp)) != 0)
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
last = *cp;
|
|
|
|
encoded_len--;
|
|
|
|
cp++;
|
|
|
|
if (last == '\n') {
|
|
|
|
if (encoded_len >= MARK_END_LEN &&
|
|
|
|
memcmp(cp, MARK_END, MARK_END_LEN) == 0) {
|
|
|
|
/* \0 terminate */
|
|
|
|
if ((r = sshbuf_put_u8(encoded, 0)) != 0)
|
|
|
|
goto out;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (encoded_len == 0) {
|
|
|
|
r = SSH_ERR_INVALID_FORMAT;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* decode base64 */
|
2014-10-09 05:45:48 +08:00
|
|
|
if ((r = sshbuf_b64tod(decoded, (char *)sshbuf_ptr(encoded))) != 0)
|
2014-07-02 13:28:02 +08:00
|
|
|
goto out;
|
|
|
|
|
|
|
|
/* check magic */
|
|
|
|
if (sshbuf_len(decoded) < sizeof(AUTH_MAGIC) ||
|
|
|
|
memcmp(sshbuf_ptr(decoded), AUTH_MAGIC, sizeof(AUTH_MAGIC))) {
|
|
|
|
r = SSH_ERR_INVALID_FORMAT;
|
|
|
|
goto out;
|
|
|
|
}
|
2020-04-08 08:01:52 +08:00
|
|
|
/* success */
|
|
|
|
*decodedp = decoded;
|
|
|
|
decoded = NULL;
|
|
|
|
r = 0;
|
|
|
|
out:
|
|
|
|
sshbuf_free(encoded);
|
|
|
|
sshbuf_free(decoded);
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2020-04-08 08:04:32 +08:00
|
|
|
private2_decrypt(struct sshbuf *decoded, const char *passphrase,
|
|
|
|
struct sshbuf **decryptedp, struct sshkey **pubkeyp)
|
2020-04-08 08:01:52 +08:00
|
|
|
{
|
|
|
|
char *ciphername = NULL, *kdfname = NULL;
|
|
|
|
const struct sshcipher *cipher = NULL;
|
|
|
|
int r = SSH_ERR_INTERNAL_ERROR;
|
|
|
|
size_t keylen = 0, ivlen = 0, authlen = 0, slen = 0;
|
|
|
|
struct sshbuf *kdf = NULL, *decrypted = NULL;
|
|
|
|
struct sshcipher_ctx *ciphercontext = NULL;
|
2020-04-08 08:04:32 +08:00
|
|
|
struct sshkey *pubkey = NULL;
|
2020-04-08 08:01:52 +08:00
|
|
|
u_char *key = NULL, *salt = NULL, *dp;
|
|
|
|
u_int blocksize, rounds, nkeys, encrypted_len, check1, check2;
|
|
|
|
|
2020-04-08 08:04:32 +08:00
|
|
|
if (decoded == NULL || decryptedp == NULL || pubkeyp == NULL)
|
2020-04-08 08:01:52 +08:00
|
|
|
return SSH_ERR_INVALID_ARGUMENT;
|
|
|
|
|
|
|
|
*decryptedp = NULL;
|
2020-04-08 08:04:32 +08:00
|
|
|
*pubkeyp = NULL;
|
2020-04-08 08:01:52 +08:00
|
|
|
|
|
|
|
if ((decrypted = sshbuf_new()) == NULL) {
|
|
|
|
r = SSH_ERR_ALLOC_FAIL;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2014-07-02 13:28:02 +08:00
|
|
|
/* parse public portion of key */
|
|
|
|
if ((r = sshbuf_consume(decoded, sizeof(AUTH_MAGIC))) != 0 ||
|
|
|
|
(r = sshbuf_get_cstring(decoded, &ciphername, NULL)) != 0 ||
|
|
|
|
(r = sshbuf_get_cstring(decoded, &kdfname, NULL)) != 0 ||
|
|
|
|
(r = sshbuf_froms(decoded, &kdf)) != 0 ||
|
2020-04-08 08:01:52 +08:00
|
|
|
(r = sshbuf_get_u32(decoded, &nkeys)) != 0)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
if (nkeys != 1) {
|
|
|
|
/* XXX only one key supported at present */
|
|
|
|
r = SSH_ERR_INVALID_FORMAT;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2020-04-08 08:04:32 +08:00
|
|
|
if ((r = sshkey_froms(decoded, &pubkey)) != 0 ||
|
2014-07-02 13:28:02 +08:00
|
|
|
(r = sshbuf_get_u32(decoded, &encrypted_len)) != 0)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
if ((cipher = cipher_by_name(ciphername)) == NULL) {
|
|
|
|
r = SSH_ERR_KEY_UNKNOWN_CIPHER;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
if (strcmp(kdfname, "none") != 0 && strcmp(kdfname, "bcrypt") != 0) {
|
|
|
|
r = SSH_ERR_KEY_UNKNOWN_CIPHER;
|
|
|
|
goto out;
|
|
|
|
}
|
2020-03-07 02:21:28 +08:00
|
|
|
if (strcmp(kdfname, "none") == 0 && strcmp(ciphername, "none") != 0) {
|
2014-07-02 13:28:02 +08:00
|
|
|
r = SSH_ERR_INVALID_FORMAT;
|
|
|
|
goto out;
|
|
|
|
}
|
2020-03-07 02:21:28 +08:00
|
|
|
if ((passphrase == NULL || strlen(passphrase) == 0) &&
|
|
|
|
strcmp(kdfname, "none") != 0) {
|
|
|
|
/* passphrase required */
|
|
|
|
r = SSH_ERR_KEY_WRONG_PASSPHRASE;
|
|
|
|
goto out;
|
|
|
|
}
|
2014-07-02 13:28:02 +08:00
|
|
|
|
|
|
|
/* check size of encrypted key blob */
|
|
|
|
blocksize = cipher_blocksize(cipher);
|
|
|
|
if (encrypted_len < blocksize || (encrypted_len % blocksize) != 0) {
|
|
|
|
r = SSH_ERR_INVALID_FORMAT;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* setup key */
|
|
|
|
keylen = cipher_keylen(cipher);
|
|
|
|
ivlen = cipher_ivlen(cipher);
|
2015-05-08 11:17:49 +08:00
|
|
|
authlen = cipher_authlen(cipher);
|
2014-07-02 13:28:02 +08:00
|
|
|
if ((key = calloc(1, keylen + ivlen)) == NULL) {
|
|
|
|
r = SSH_ERR_ALLOC_FAIL;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
if (strcmp(kdfname, "bcrypt") == 0) {
|
|
|
|
if ((r = sshbuf_get_string(kdf, &salt, &slen)) != 0 ||
|
|
|
|
(r = sshbuf_get_u32(kdf, &rounds)) != 0)
|
|
|
|
goto out;
|
|
|
|
if (bcrypt_pbkdf(passphrase, strlen(passphrase), salt, slen,
|
|
|
|
key, keylen + ivlen, rounds) < 0) {
|
|
|
|
r = SSH_ERR_INVALID_FORMAT;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-08 11:17:49 +08:00
|
|
|
/* check that an appropriate amount of auth data is present */
|
2019-10-09 08:04:42 +08:00
|
|
|
if (sshbuf_len(decoded) < authlen ||
|
|
|
|
sshbuf_len(decoded) - authlen < encrypted_len) {
|
2015-05-08 11:17:49 +08:00
|
|
|
r = SSH_ERR_INVALID_FORMAT;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2014-07-02 13:28:02 +08:00
|
|
|
/* decrypt private portion of key */
|
|
|
|
if ((r = sshbuf_reserve(decrypted, encrypted_len, &dp)) != 0 ||
|
|
|
|
(r = cipher_init(&ciphercontext, cipher, key, keylen,
|
|
|
|
key + keylen, ivlen, 0)) != 0)
|
|
|
|
goto out;
|
2016-08-03 13:41:57 +08:00
|
|
|
if ((r = cipher_crypt(ciphercontext, 0, dp, sshbuf_ptr(decoded),
|
2015-05-08 11:17:49 +08:00
|
|
|
encrypted_len, 0, authlen)) != 0) {
|
2014-07-02 13:28:02 +08:00
|
|
|
/* an integrity error here indicates an incorrect passphrase */
|
|
|
|
if (r == SSH_ERR_MAC_INVALID)
|
|
|
|
r = SSH_ERR_KEY_WRONG_PASSPHRASE;
|
|
|
|
goto out;
|
|
|
|
}
|
2015-05-08 11:17:49 +08:00
|
|
|
if ((r = sshbuf_consume(decoded, encrypted_len + authlen)) != 0)
|
2014-07-02 13:28:02 +08:00
|
|
|
goto out;
|
|
|
|
/* there should be no trailing data */
|
|
|
|
if (sshbuf_len(decoded) != 0) {
|
|
|
|
r = SSH_ERR_INVALID_FORMAT;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* check check bytes */
|
|
|
|
if ((r = sshbuf_get_u32(decrypted, &check1)) != 0 ||
|
|
|
|
(r = sshbuf_get_u32(decrypted, &check2)) != 0)
|
|
|
|
goto out;
|
|
|
|
if (check1 != check2) {
|
|
|
|
r = SSH_ERR_KEY_WRONG_PASSPHRASE;
|
|
|
|
goto out;
|
|
|
|
}
|
2020-04-08 08:01:52 +08:00
|
|
|
/* success */
|
|
|
|
*decryptedp = decrypted;
|
|
|
|
decrypted = NULL;
|
2020-04-08 08:04:32 +08:00
|
|
|
*pubkeyp = pubkey;
|
|
|
|
pubkey = NULL;
|
2020-04-08 08:01:52 +08:00
|
|
|
r = 0;
|
|
|
|
out:
|
|
|
|
cipher_free(ciphercontext);
|
|
|
|
free(ciphername);
|
|
|
|
free(kdfname);
|
2020-04-08 08:04:32 +08:00
|
|
|
sshkey_free(pubkey);
|
2020-04-08 08:01:52 +08:00
|
|
|
if (salt != NULL) {
|
|
|
|
explicit_bzero(salt, slen);
|
|
|
|
free(salt);
|
|
|
|
}
|
|
|
|
if (key != NULL) {
|
|
|
|
explicit_bzero(key, keylen + ivlen);
|
|
|
|
free(key);
|
|
|
|
}
|
|
|
|
sshbuf_free(kdf);
|
|
|
|
sshbuf_free(decrypted);
|
|
|
|
return r;
|
|
|
|
}
|
2014-07-02 13:28:02 +08:00
|
|
|
|
2020-04-08 08:01:52 +08:00
|
|
|
static int
|
|
|
|
sshkey_parse_private2(struct sshbuf *blob, int type, const char *passphrase,
|
|
|
|
struct sshkey **keyp, char **commentp)
|
|
|
|
{
|
|
|
|
char *comment = NULL;
|
|
|
|
int r = SSH_ERR_INTERNAL_ERROR;
|
|
|
|
struct sshbuf *decoded = NULL, *decrypted = NULL;
|
2020-04-08 08:04:32 +08:00
|
|
|
struct sshkey *k = NULL, *pubkey = NULL;
|
2020-04-08 08:01:52 +08:00
|
|
|
|
|
|
|
if (keyp != NULL)
|
|
|
|
*keyp = NULL;
|
|
|
|
if (commentp != NULL)
|
|
|
|
*commentp = NULL;
|
|
|
|
|
|
|
|
/* Undo base64 encoding and decrypt the private section */
|
|
|
|
if ((r = private2_uudecode(blob, &decoded)) != 0 ||
|
2020-04-08 08:04:32 +08:00
|
|
|
(r = private2_decrypt(decoded, passphrase,
|
|
|
|
&decrypted, &pubkey)) != 0)
|
2020-04-08 08:01:52 +08:00
|
|
|
goto out;
|
|
|
|
|
2020-04-08 08:05:59 +08:00
|
|
|
if (type != KEY_UNSPEC &&
|
|
|
|
sshkey_type_plain(type) != sshkey_type_plain(pubkey->type)) {
|
|
|
|
r = SSH_ERR_KEY_TYPE_MISMATCH;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2020-04-08 08:01:52 +08:00
|
|
|
/* Load the private key and comment */
|
|
|
|
if ((r = sshkey_private_deserialize(decrypted, &k)) != 0 ||
|
|
|
|
(r = sshbuf_get_cstring(decrypted, &comment, NULL)) != 0)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
/* Check deterministic padding after private section */
|
|
|
|
if ((r = private2_check_padding(decrypted)) != 0)
|
|
|
|
goto out;
|
2014-07-02 13:28:02 +08:00
|
|
|
|
2020-04-08 08:04:32 +08:00
|
|
|
/* Check that the public key in the envelope matches the private key */
|
|
|
|
if (!sshkey_equal(pubkey, k)) {
|
|
|
|
r = SSH_ERR_INVALID_FORMAT;
|
|
|
|
goto out;
|
|
|
|
}
|
2014-07-02 13:28:02 +08:00
|
|
|
|
|
|
|
/* success */
|
|
|
|
r = 0;
|
|
|
|
if (keyp != NULL) {
|
|
|
|
*keyp = k;
|
|
|
|
k = NULL;
|
|
|
|
}
|
|
|
|
if (commentp != NULL) {
|
|
|
|
*commentp = comment;
|
|
|
|
comment = NULL;
|
|
|
|
}
|
|
|
|
out:
|
|
|
|
free(comment);
|
|
|
|
sshbuf_free(decoded);
|
|
|
|
sshbuf_free(decrypted);
|
|
|
|
sshkey_free(k);
|
2020-04-08 08:04:32 +08:00
|
|
|
sshkey_free(pubkey);
|
2014-07-02 13:28:02 +08:00
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2020-04-08 08:08:46 +08:00
|
|
|
static int
|
|
|
|
sshkey_parse_private2_pubkey(struct sshbuf *blob, int type,
|
|
|
|
struct sshkey **keyp)
|
|
|
|
{
|
|
|
|
int r = SSH_ERR_INTERNAL_ERROR;
|
|
|
|
struct sshbuf *decoded = NULL;
|
|
|
|
struct sshkey *pubkey = NULL;
|
|
|
|
u_int nkeys = 0;
|
|
|
|
|
|
|
|
if (keyp != NULL)
|
|
|
|
*keyp = NULL;
|
|
|
|
|
|
|
|
if ((r = private2_uudecode(blob, &decoded)) != 0)
|
|
|
|
goto out;
|
|
|
|
/* parse public key from unencrypted envelope */
|
|
|
|
if ((r = sshbuf_consume(decoded, sizeof(AUTH_MAGIC))) != 0 ||
|
|
|
|
(r = sshbuf_skip_string(decoded)) != 0 || /* cipher */
|
|
|
|
(r = sshbuf_skip_string(decoded)) != 0 || /* KDF alg */
|
|
|
|
(r = sshbuf_skip_string(decoded)) != 0 || /* KDF hint */
|
|
|
|
(r = sshbuf_get_u32(decoded, &nkeys)) != 0)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
if (nkeys != 1) {
|
|
|
|
/* XXX only one key supported at present */
|
|
|
|
r = SSH_ERR_INVALID_FORMAT;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Parse the public key */
|
|
|
|
if ((r = sshkey_froms(decoded, &pubkey)) != 0)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
if (type != KEY_UNSPEC &&
|
|
|
|
sshkey_type_plain(type) != sshkey_type_plain(pubkey->type)) {
|
|
|
|
r = SSH_ERR_KEY_TYPE_MISMATCH;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* success */
|
|
|
|
r = 0;
|
|
|
|
if (keyp != NULL) {
|
|
|
|
*keyp = pubkey;
|
|
|
|
pubkey = NULL;
|
|
|
|
}
|
|
|
|
out:
|
|
|
|
sshbuf_free(decoded);
|
|
|
|
sshkey_free(pubkey);
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2014-07-02 13:28:02 +08:00
|
|
|
#ifdef WITH_OPENSSL
|
2019-07-15 21:16:29 +08:00
|
|
|
/* convert SSH v2 key to PEM or PKCS#8 format */
|
2014-07-02 13:28:02 +08:00
|
|
|
static int
|
2019-07-15 21:16:29 +08:00
|
|
|
sshkey_private_to_blob_pem_pkcs8(struct sshkey *key, struct sshbuf *buf,
|
|
|
|
int format, const char *_passphrase, const char *comment)
|
2014-07-02 13:28:02 +08:00
|
|
|
{
|
2019-06-21 12:21:04 +08:00
|
|
|
int was_shielded = sshkey_is_shielded(key);
|
2014-07-02 13:28:02 +08:00
|
|
|
int success, r;
|
|
|
|
int blen, len = strlen(_passphrase);
|
|
|
|
u_char *passphrase = (len > 0) ? (u_char *)_passphrase : NULL;
|
2017-03-29 07:16:15 +08:00
|
|
|
const EVP_CIPHER *cipher = (len > 0) ? EVP_aes_128_cbc() : NULL;
|
2017-10-13 14:24:51 +08:00
|
|
|
char *bptr;
|
2014-07-02 13:28:02 +08:00
|
|
|
BIO *bio = NULL;
|
2019-06-21 12:21:04 +08:00
|
|
|
struct sshbuf *blob;
|
2019-07-15 21:16:29 +08:00
|
|
|
EVP_PKEY *pkey = NULL;
|
2014-07-02 13:28:02 +08:00
|
|
|
|
|
|
|
if (len > 0 && len <= 4)
|
|
|
|
return SSH_ERR_PASSPHRASE_TOO_SHORT;
|
2019-06-21 12:21:04 +08:00
|
|
|
if ((blob = sshbuf_new()) == NULL)
|
2014-07-02 13:28:02 +08:00
|
|
|
return SSH_ERR_ALLOC_FAIL;
|
2021-04-03 14:47:37 +08:00
|
|
|
if ((bio = BIO_new(BIO_s_mem())) == NULL) {
|
2019-07-15 21:16:29 +08:00
|
|
|
r = SSH_ERR_ALLOC_FAIL;
|
|
|
|
goto out;
|
2019-06-21 12:21:04 +08:00
|
|
|
}
|
|
|
|
if ((r = sshkey_unshield_private(key)) != 0)
|
|
|
|
goto out;
|
2014-07-02 13:28:02 +08:00
|
|
|
|
|
|
|
switch (key->type) {
|
2024-01-11 09:45:36 +08:00
|
|
|
#ifdef WITH_DSA
|
2014-07-02 13:28:02 +08:00
|
|
|
case KEY_DSA:
|
2019-07-15 21:16:29 +08:00
|
|
|
if (format == SSHKEY_PRIVATE_PEM) {
|
|
|
|
success = PEM_write_bio_DSAPrivateKey(bio, key->dsa,
|
|
|
|
cipher, passphrase, len, NULL, NULL);
|
|
|
|
} else {
|
2024-08-15 08:51:51 +08:00
|
|
|
if ((pkey = EVP_PKEY_new()) == NULL) {
|
|
|
|
r = SSH_ERR_ALLOC_FAIL;
|
|
|
|
goto out;
|
|
|
|
}
|
2019-07-15 21:16:29 +08:00
|
|
|
success = EVP_PKEY_set1_DSA(pkey, key->dsa);
|
|
|
|
}
|
2014-07-02 13:28:02 +08:00
|
|
|
break;
|
2024-01-11 09:45:36 +08:00
|
|
|
#endif
|
2014-07-02 13:28:02 +08:00
|
|
|
#ifdef OPENSSL_HAS_ECC
|
|
|
|
case KEY_ECDSA:
|
2019-07-15 21:16:29 +08:00
|
|
|
if (format == SSHKEY_PRIVATE_PEM) {
|
2024-08-15 08:51:51 +08:00
|
|
|
success = PEM_write_bio_ECPrivateKey(bio,
|
|
|
|
EVP_PKEY_get0_EC_KEY(key->pkey),
|
2019-07-15 21:16:29 +08:00
|
|
|
cipher, passphrase, len, NULL, NULL);
|
|
|
|
} else {
|
2024-08-15 08:51:51 +08:00
|
|
|
pkey = key->pkey;
|
|
|
|
EVP_PKEY_up_ref(key->pkey);
|
|
|
|
success = 1;
|
2019-07-15 21:16:29 +08:00
|
|
|
}
|
2014-07-02 13:28:02 +08:00
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
case KEY_RSA:
|
2019-07-15 21:16:29 +08:00
|
|
|
if (format == SSHKEY_PRIVATE_PEM) {
|
2024-08-15 08:51:51 +08:00
|
|
|
success = PEM_write_bio_RSAPrivateKey(bio,
|
|
|
|
EVP_PKEY_get0_RSA(key->pkey),
|
2019-07-15 21:16:29 +08:00
|
|
|
cipher, passphrase, len, NULL, NULL);
|
|
|
|
} else {
|
2024-08-15 08:51:51 +08:00
|
|
|
pkey = key->pkey;
|
|
|
|
EVP_PKEY_up_ref(key->pkey);
|
|
|
|
success = 1;
|
2019-07-15 21:16:29 +08:00
|
|
|
}
|
2014-07-02 13:28:02 +08:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
success = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (success == 0) {
|
|
|
|
r = SSH_ERR_LIBCRYPTO_ERROR;
|
|
|
|
goto out;
|
|
|
|
}
|
2019-07-15 21:16:29 +08:00
|
|
|
if (format == SSHKEY_PRIVATE_PKCS8) {
|
|
|
|
if ((success = PEM_write_bio_PrivateKey(bio, pkey, cipher,
|
|
|
|
passphrase, len, NULL, NULL)) == 0) {
|
|
|
|
r = SSH_ERR_LIBCRYPTO_ERROR;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
}
|
2014-07-02 13:28:02 +08:00
|
|
|
if ((blen = BIO_get_mem_data(bio, &bptr)) <= 0) {
|
|
|
|
r = SSH_ERR_INTERNAL_ERROR;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
if ((r = sshbuf_put(blob, bptr, blen)) != 0)
|
|
|
|
goto out;
|
|
|
|
r = 0;
|
|
|
|
out:
|
2019-06-21 12:21:04 +08:00
|
|
|
if (was_shielded)
|
|
|
|
r = sshkey_shield_private(key);
|
|
|
|
if (r == 0)
|
|
|
|
r = sshbuf_putb(buf, blob);
|
|
|
|
|
2019-07-15 21:16:29 +08:00
|
|
|
EVP_PKEY_free(pkey);
|
|
|
|
sshbuf_free(blob);
|
2014-07-02 13:28:02 +08:00
|
|
|
BIO_free(bio);
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
#endif /* WITH_OPENSSL */
|
|
|
|
|
|
|
|
/* Serialise "key" to buffer "blob" */
|
|
|
|
int
|
|
|
|
sshkey_private_to_fileblob(struct sshkey *key, struct sshbuf *blob,
|
|
|
|
const char *passphrase, const char *comment,
|
2019-07-15 21:16:29 +08:00
|
|
|
int format, const char *openssh_format_cipher, int openssh_format_rounds)
|
2014-07-02 13:28:02 +08:00
|
|
|
{
|
|
|
|
switch (key->type) {
|
2015-01-12 21:29:27 +08:00
|
|
|
#ifdef WITH_OPENSSL
|
2014-07-02 13:28:02 +08:00
|
|
|
case KEY_DSA:
|
|
|
|
case KEY_ECDSA:
|
|
|
|
case KEY_RSA:
|
2019-07-15 21:16:29 +08:00
|
|
|
break; /* see below */
|
2014-07-02 13:28:02 +08:00
|
|
|
#endif /* WITH_OPENSSL */
|
|
|
|
case KEY_ED25519:
|
2019-11-13 03:33:08 +08:00
|
|
|
case KEY_ED25519_SK:
|
2018-02-23 23:58:37 +08:00
|
|
|
#ifdef WITH_XMSS
|
|
|
|
case KEY_XMSS:
|
|
|
|
#endif /* WITH_XMSS */
|
2019-11-01 05:15:14 +08:00
|
|
|
#ifdef WITH_OPENSSL
|
|
|
|
case KEY_ECDSA_SK:
|
|
|
|
#endif /* WITH_OPENSSL */
|
2014-07-02 13:28:02 +08:00
|
|
|
return sshkey_private_to_blob2(key, blob, passphrase,
|
2019-07-15 21:16:29 +08:00
|
|
|
comment, openssh_format_cipher, openssh_format_rounds);
|
2014-07-02 13:28:02 +08:00
|
|
|
default:
|
|
|
|
return SSH_ERR_KEY_TYPE_UNKNOWN;
|
|
|
|
}
|
2019-07-15 21:16:29 +08:00
|
|
|
|
|
|
|
#ifdef WITH_OPENSSL
|
|
|
|
switch (format) {
|
|
|
|
case SSHKEY_PRIVATE_OPENSSH:
|
|
|
|
return sshkey_private_to_blob2(key, blob, passphrase,
|
|
|
|
comment, openssh_format_cipher, openssh_format_rounds);
|
|
|
|
case SSHKEY_PRIVATE_PEM:
|
|
|
|
case SSHKEY_PRIVATE_PKCS8:
|
|
|
|
return sshkey_private_to_blob_pem_pkcs8(key, blob,
|
|
|
|
format, passphrase, comment);
|
|
|
|
default:
|
|
|
|
return SSH_ERR_INVALID_ARGUMENT;
|
|
|
|
}
|
|
|
|
#endif /* WITH_OPENSSL */
|
2014-07-02 13:28:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef WITH_OPENSSL
|
2017-06-09 14:40:24 +08:00
|
|
|
static int
|
|
|
|
translate_libcrypto_error(unsigned long pem_err)
|
|
|
|
{
|
|
|
|
int pem_reason = ERR_GET_REASON(pem_err);
|
|
|
|
|
|
|
|
switch (ERR_GET_LIB(pem_err)) {
|
|
|
|
case ERR_LIB_PEM:
|
|
|
|
switch (pem_reason) {
|
|
|
|
case PEM_R_BAD_PASSWORD_READ:
|
2023-03-24 12:23:05 +08:00
|
|
|
#ifdef PEM_R_PROBLEMS_GETTING_PASSWORD
|
2017-06-09 14:40:24 +08:00
|
|
|
case PEM_R_PROBLEMS_GETTING_PASSWORD:
|
2023-03-24 12:23:05 +08:00
|
|
|
#endif
|
|
|
|
#ifdef PEM_R_BAD_DECRYPT
|
2017-06-09 14:40:24 +08:00
|
|
|
case PEM_R_BAD_DECRYPT:
|
2023-03-24 12:23:05 +08:00
|
|
|
#endif
|
2017-06-09 14:40:24 +08:00
|
|
|
return SSH_ERR_KEY_WRONG_PASSPHRASE;
|
|
|
|
default:
|
|
|
|
return SSH_ERR_INVALID_FORMAT;
|
|
|
|
}
|
|
|
|
case ERR_LIB_EVP:
|
|
|
|
switch (pem_reason) {
|
2023-03-24 12:23:05 +08:00
|
|
|
#ifdef EVP_R_BAD_DECRYPT
|
2017-06-09 14:40:24 +08:00
|
|
|
case EVP_R_BAD_DECRYPT:
|
|
|
|
return SSH_ERR_KEY_WRONG_PASSPHRASE;
|
2023-03-24 12:23:05 +08:00
|
|
|
#endif
|
2018-09-13 10:08:33 +08:00
|
|
|
#ifdef EVP_R_BN_DECODE_ERROR
|
2017-06-09 14:40:24 +08:00
|
|
|
case EVP_R_BN_DECODE_ERROR:
|
2018-09-13 10:08:33 +08:00
|
|
|
#endif
|
2017-06-09 14:40:24 +08:00
|
|
|
case EVP_R_DECODE_ERROR:
|
|
|
|
#ifdef EVP_R_PRIVATE_KEY_DECODE_ERROR
|
|
|
|
case EVP_R_PRIVATE_KEY_DECODE_ERROR:
|
|
|
|
#endif
|
|
|
|
return SSH_ERR_INVALID_FORMAT;
|
|
|
|
default:
|
|
|
|
return SSH_ERR_LIBCRYPTO_ERROR;
|
|
|
|
}
|
|
|
|
case ERR_LIB_ASN1:
|
|
|
|
return SSH_ERR_INVALID_FORMAT;
|
|
|
|
}
|
|
|
|
return SSH_ERR_LIBCRYPTO_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
clear_libcrypto_errors(void)
|
|
|
|
{
|
|
|
|
while (ERR_get_error() != 0)
|
|
|
|
;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Translate OpenSSL error codes to determine whether
|
|
|
|
* passphrase is required/incorrect.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
convert_libcrypto_error(void)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Some password errors are reported at the beginning
|
|
|
|
* of the error queue.
|
|
|
|
*/
|
|
|
|
if (translate_libcrypto_error(ERR_peek_error()) ==
|
|
|
|
SSH_ERR_KEY_WRONG_PASSPHRASE)
|
|
|
|
return SSH_ERR_KEY_WRONG_PASSPHRASE;
|
|
|
|
return translate_libcrypto_error(ERR_peek_last_error());
|
|
|
|
}
|
|
|
|
|
2018-10-11 07:29:29 +08:00
|
|
|
static int
|
|
|
|
pem_passphrase_cb(char *buf, int size, int rwflag, void *u)
|
|
|
|
{
|
|
|
|
char *p = (char *)u;
|
|
|
|
size_t len;
|
|
|
|
|
|
|
|
if (p == NULL || (len = strlen(p)) == 0)
|
|
|
|
return -1;
|
|
|
|
if (size < 0 || len > (size_t)size)
|
|
|
|
return -1;
|
|
|
|
memcpy(buf, p, len);
|
|
|
|
return (int)len;
|
|
|
|
}
|
|
|
|
|
2015-01-08 18:14:08 +08:00
|
|
|
static int
|
2014-07-02 13:28:02 +08:00
|
|
|
sshkey_parse_private_pem_fileblob(struct sshbuf *blob, int type,
|
2015-01-08 18:14:08 +08:00
|
|
|
const char *passphrase, struct sshkey **keyp)
|
2014-07-02 13:28:02 +08:00
|
|
|
{
|
|
|
|
EVP_PKEY *pk = NULL;
|
|
|
|
struct sshkey *prv = NULL;
|
|
|
|
BIO *bio = NULL;
|
|
|
|
int r;
|
2024-08-15 08:51:51 +08:00
|
|
|
RSA *rsa = NULL;
|
|
|
|
EC_KEY *ecdsa = NULL;
|
2014-07-02 13:28:02 +08:00
|
|
|
|
2016-04-09 20:39:30 +08:00
|
|
|
if (keyp != NULL)
|
|
|
|
*keyp = NULL;
|
2014-07-02 13:28:02 +08:00
|
|
|
|
|
|
|
if ((bio = BIO_new(BIO_s_mem())) == NULL || sshbuf_len(blob) > INT_MAX)
|
|
|
|
return SSH_ERR_ALLOC_FAIL;
|
|
|
|
if (BIO_write(bio, sshbuf_ptr(blob), sshbuf_len(blob)) !=
|
|
|
|
(int)sshbuf_len(blob)) {
|
|
|
|
r = SSH_ERR_ALLOC_FAIL;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2017-06-09 14:40:24 +08:00
|
|
|
clear_libcrypto_errors();
|
2018-10-11 07:29:29 +08:00
|
|
|
if ((pk = PEM_read_bio_PrivateKey(bio, NULL, pem_passphrase_cb,
|
2014-07-02 13:28:02 +08:00
|
|
|
(char *)passphrase)) == NULL) {
|
2021-04-03 14:18:40 +08:00
|
|
|
/*
|
|
|
|
* libcrypto may return various ASN.1 errors when attempting
|
|
|
|
* to parse a key with an incorrect passphrase.
|
|
|
|
* Treat all format errors as "incorrect passphrase" if a
|
|
|
|
* passphrase was supplied.
|
|
|
|
*/
|
2018-10-09 13:42:23 +08:00
|
|
|
if (passphrase != NULL && *passphrase != '\0')
|
|
|
|
r = SSH_ERR_KEY_WRONG_PASSPHRASE;
|
|
|
|
else
|
|
|
|
r = convert_libcrypto_error();
|
2014-07-02 13:28:02 +08:00
|
|
|
goto out;
|
|
|
|
}
|
2018-09-13 10:08:33 +08:00
|
|
|
if (EVP_PKEY_base_id(pk) == EVP_PKEY_RSA &&
|
2014-07-02 13:28:02 +08:00
|
|
|
(type == KEY_UNSPEC || type == KEY_RSA)) {
|
|
|
|
if ((prv = sshkey_new(KEY_UNSPEC)) == NULL) {
|
|
|
|
r = SSH_ERR_ALLOC_FAIL;
|
|
|
|
goto out;
|
|
|
|
}
|
2024-08-15 08:51:51 +08:00
|
|
|
if ((rsa = EVP_PKEY_get1_RSA(pk)) == NULL) {
|
|
|
|
r = SSH_ERR_LIBCRYPTO_ERROR;
|
|
|
|
goto out;
|
|
|
|
}
|
2014-07-02 13:28:02 +08:00
|
|
|
prv->type = KEY_RSA;
|
|
|
|
#ifdef DEBUG_PK
|
2024-08-15 08:51:51 +08:00
|
|
|
RSA_print_fp(stderr, rsa, 8);
|
2014-07-02 13:28:02 +08:00
|
|
|
#endif
|
2024-08-15 08:51:51 +08:00
|
|
|
if (RSA_blinding_on(rsa, NULL) != 1 ||
|
|
|
|
EVP_PKEY_set1_RSA(pk, rsa) != 1) {
|
2014-07-02 13:28:02 +08:00
|
|
|
r = SSH_ERR_LIBCRYPTO_ERROR;
|
|
|
|
goto out;
|
|
|
|
}
|
2024-08-15 08:51:51 +08:00
|
|
|
EVP_PKEY_up_ref(pk);
|
|
|
|
prv->pkey = pk;
|
2022-09-17 18:30:45 +08:00
|
|
|
if ((r = sshkey_check_rsa_length(prv, 0)) != 0)
|
2017-05-08 07:15:59 +08:00
|
|
|
goto out;
|
2024-01-11 09:45:36 +08:00
|
|
|
#ifdef WITH_DSA
|
2018-09-13 10:08:33 +08:00
|
|
|
} else if (EVP_PKEY_base_id(pk) == EVP_PKEY_DSA &&
|
2014-07-02 13:28:02 +08:00
|
|
|
(type == KEY_UNSPEC || type == KEY_DSA)) {
|
|
|
|
if ((prv = sshkey_new(KEY_UNSPEC)) == NULL) {
|
|
|
|
r = SSH_ERR_ALLOC_FAIL;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
prv->dsa = EVP_PKEY_get1_DSA(pk);
|
|
|
|
prv->type = KEY_DSA;
|
|
|
|
#ifdef DEBUG_PK
|
|
|
|
DSA_print_fp(stderr, prv->dsa, 8);
|
|
|
|
#endif
|
2024-01-11 09:45:36 +08:00
|
|
|
#endif
|
2014-07-02 13:28:02 +08:00
|
|
|
#ifdef OPENSSL_HAS_ECC
|
2018-09-13 10:08:33 +08:00
|
|
|
} else if (EVP_PKEY_base_id(pk) == EVP_PKEY_EC &&
|
2014-07-02 13:28:02 +08:00
|
|
|
(type == KEY_UNSPEC || type == KEY_ECDSA)) {
|
|
|
|
if ((prv = sshkey_new(KEY_UNSPEC)) == NULL) {
|
|
|
|
r = SSH_ERR_ALLOC_FAIL;
|
|
|
|
goto out;
|
|
|
|
}
|
2024-08-15 08:51:51 +08:00
|
|
|
if ((prv->ecdsa_nid = sshkey_ecdsa_fixup_group(pk)) == -1 ||
|
|
|
|
(ecdsa = EVP_PKEY_get1_EC_KEY(pk)) == NULL) {
|
|
|
|
r = SSH_ERR_LIBCRYPTO_ERROR;
|
|
|
|
goto out;
|
|
|
|
}
|
2014-07-02 13:28:02 +08:00
|
|
|
prv->type = KEY_ECDSA;
|
2024-08-15 08:51:51 +08:00
|
|
|
if (sshkey_curve_nid_to_name(prv->ecdsa_nid) == NULL ||
|
|
|
|
sshkey_ec_validate_public(EC_KEY_get0_group(ecdsa),
|
|
|
|
EC_KEY_get0_public_key(ecdsa)) != 0 ||
|
|
|
|
sshkey_ec_validate_private(ecdsa) != 0) {
|
2014-07-02 13:28:02 +08:00
|
|
|
r = SSH_ERR_INVALID_FORMAT;
|
|
|
|
goto out;
|
|
|
|
}
|
2024-08-15 08:51:51 +08:00
|
|
|
EVP_PKEY_up_ref(pk);
|
|
|
|
prv->pkey = pk;
|
|
|
|
#ifdef DEBUG_PK
|
|
|
|
if (prv != NULL && prv->pkey != NULL)
|
|
|
|
sshkey_dump_ec_key(EVP_PKEY_get0_EC_KEY(prv->pkey));
|
|
|
|
#endif
|
2014-07-02 13:28:02 +08:00
|
|
|
#endif /* OPENSSL_HAS_ECC */
|
2023-10-12 06:41:05 +08:00
|
|
|
#ifdef OPENSSL_HAS_ED25519
|
|
|
|
} else if (EVP_PKEY_base_id(pk) == EVP_PKEY_ED25519 &&
|
|
|
|
(type == KEY_UNSPEC || type == KEY_ED25519)) {
|
2023-10-16 16:40:00 +08:00
|
|
|
size_t len;
|
|
|
|
|
2023-10-12 06:41:05 +08:00
|
|
|
if ((prv = sshkey_new(KEY_UNSPEC)) == NULL ||
|
|
|
|
(prv->ed25519_sk = calloc(1, ED25519_SK_SZ)) == NULL ||
|
|
|
|
(prv->ed25519_pk = calloc(1, ED25519_PK_SZ)) == NULL) {
|
|
|
|
r = SSH_ERR_ALLOC_FAIL;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
prv->type = KEY_ED25519;
|
|
|
|
len = ED25519_PK_SZ;
|
|
|
|
if (!EVP_PKEY_get_raw_public_key(pk, prv->ed25519_pk, &len)) {
|
|
|
|
r = SSH_ERR_LIBCRYPTO_ERROR;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
if (len != ED25519_PK_SZ) {
|
|
|
|
r = SSH_ERR_INVALID_FORMAT;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
len = ED25519_SK_SZ - ED25519_PK_SZ;
|
|
|
|
if (!EVP_PKEY_get_raw_private_key(pk, prv->ed25519_sk, &len)) {
|
|
|
|
r = SSH_ERR_LIBCRYPTO_ERROR;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
if (len != ED25519_SK_SZ - ED25519_PK_SZ) {
|
|
|
|
r = SSH_ERR_INVALID_FORMAT;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
/* Append the public key to our private key */
|
|
|
|
memcpy(prv->ed25519_sk + (ED25519_SK_SZ - ED25519_PK_SZ),
|
|
|
|
prv->ed25519_pk, ED25519_PK_SZ);
|
2024-08-15 08:51:51 +08:00
|
|
|
#ifdef DEBUG_PK
|
2023-10-12 06:41:05 +08:00
|
|
|
sshbuf_dump_data(prv->ed25519_sk, ED25519_SK_SZ, stderr);
|
2024-08-15 08:51:51 +08:00
|
|
|
#endif
|
2023-10-12 06:41:05 +08:00
|
|
|
#endif /* OPENSSL_HAS_ED25519 */
|
2014-07-02 13:28:02 +08:00
|
|
|
} else {
|
|
|
|
r = SSH_ERR_INVALID_FORMAT;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
r = 0;
|
2016-04-09 20:39:30 +08:00
|
|
|
if (keyp != NULL) {
|
|
|
|
*keyp = prv;
|
|
|
|
prv = NULL;
|
|
|
|
}
|
2014-07-02 13:28:02 +08:00
|
|
|
out:
|
|
|
|
BIO_free(bio);
|
2018-02-07 10:06:50 +08:00
|
|
|
EVP_PKEY_free(pk);
|
2024-08-15 08:51:51 +08:00
|
|
|
RSA_free(rsa);
|
2024-08-15 13:09:45 +08:00
|
|
|
#ifdef OPENSSL_HAS_ECC
|
2024-08-15 08:51:51 +08:00
|
|
|
EC_KEY_free(ecdsa);
|
2024-08-15 13:09:45 +08:00
|
|
|
#endif
|
2015-12-11 10:31:47 +08:00
|
|
|
sshkey_free(prv);
|
2014-07-02 13:28:02 +08:00
|
|
|
return r;
|
|
|
|
}
|
|
|
|
#endif /* WITH_OPENSSL */
|
|
|
|
|
|
|
|
int
|
|
|
|
sshkey_parse_private_fileblob_type(struct sshbuf *blob, int type,
|
|
|
|
const char *passphrase, struct sshkey **keyp, char **commentp)
|
|
|
|
{
|
2017-02-10 12:34:50 +08:00
|
|
|
int r = SSH_ERR_INTERNAL_ERROR;
|
|
|
|
|
2016-04-09 20:39:30 +08:00
|
|
|
if (keyp != NULL)
|
|
|
|
*keyp = NULL;
|
2014-07-02 13:28:02 +08:00
|
|
|
if (commentp != NULL)
|
|
|
|
*commentp = NULL;
|
|
|
|
|
|
|
|
switch (type) {
|
2018-02-23 23:58:37 +08:00
|
|
|
case KEY_XMSS:
|
2020-04-08 08:07:19 +08:00
|
|
|
/* No fallback for new-format-only keys */
|
2014-07-02 13:28:02 +08:00
|
|
|
return sshkey_parse_private2(blob, type, passphrase,
|
|
|
|
keyp, commentp);
|
2020-04-08 08:07:19 +08:00
|
|
|
default:
|
2017-02-10 12:34:50 +08:00
|
|
|
r = sshkey_parse_private2(blob, type, passphrase, keyp,
|
|
|
|
commentp);
|
2020-04-08 08:07:19 +08:00
|
|
|
/* Only fallback to PEM parser if a format error occurred. */
|
|
|
|
if (r != SSH_ERR_INVALID_FORMAT)
|
2017-02-10 12:34:50 +08:00
|
|
|
return r;
|
2014-07-02 13:28:02 +08:00
|
|
|
#ifdef WITH_OPENSSL
|
2015-01-08 18:14:08 +08:00
|
|
|
return sshkey_parse_private_pem_fileblob(blob, type,
|
|
|
|
passphrase, keyp);
|
2014-07-02 13:28:02 +08:00
|
|
|
#else
|
|
|
|
return SSH_ERR_INVALID_FORMAT;
|
|
|
|
#endif /* WITH_OPENSSL */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
sshkey_parse_private_fileblob(struct sshbuf *buffer, const char *passphrase,
|
2015-09-13 22:39:16 +08:00
|
|
|
struct sshkey **keyp, char **commentp)
|
2014-07-02 13:28:02 +08:00
|
|
|
{
|
|
|
|
if (keyp != NULL)
|
|
|
|
*keyp = NULL;
|
|
|
|
if (commentp != NULL)
|
|
|
|
*commentp = NULL;
|
|
|
|
|
2015-09-13 22:39:16 +08:00
|
|
|
return sshkey_parse_private_fileblob_type(buffer, KEY_UNSPEC,
|
|
|
|
passphrase, keyp, commentp);
|
2014-07-02 13:28:02 +08:00
|
|
|
}
|
2018-02-23 23:58:37 +08:00
|
|
|
|
2019-11-25 08:51:37 +08:00
|
|
|
void
|
|
|
|
sshkey_sig_details_free(struct sshkey_sig_details *details)
|
|
|
|
{
|
|
|
|
freezero(details, sizeof(*details));
|
|
|
|
}
|
|
|
|
|
2020-04-08 08:08:46 +08:00
|
|
|
int
|
|
|
|
sshkey_parse_pubkey_from_private_fileblob_type(struct sshbuf *blob, int type,
|
|
|
|
struct sshkey **pubkeyp)
|
|
|
|
{
|
|
|
|
int r = SSH_ERR_INTERNAL_ERROR;
|
|
|
|
|
|
|
|
if (pubkeyp != NULL)
|
|
|
|
*pubkeyp = NULL;
|
|
|
|
/* only new-format private keys bundle a public key inside */
|
|
|
|
if ((r = sshkey_parse_private2_pubkey(blob, type, pubkeyp)) != 0)
|
|
|
|
return r;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-02-23 23:58:37 +08:00
|
|
|
#ifdef WITH_XMSS
|
|
|
|
/*
|
|
|
|
* serialize the key with the current state and forward the state
|
|
|
|
* maxsign times.
|
|
|
|
*/
|
|
|
|
int
|
2019-06-23 20:21:46 +08:00
|
|
|
sshkey_private_serialize_maxsign(struct sshkey *k, struct sshbuf *b,
|
2020-10-20 06:49:23 +08:00
|
|
|
u_int32_t maxsign, int printerror)
|
2018-02-23 23:58:37 +08:00
|
|
|
{
|
|
|
|
int r, rupdate;
|
|
|
|
|
|
|
|
if (maxsign == 0 ||
|
|
|
|
sshkey_type_plain(k->type) != KEY_XMSS)
|
|
|
|
return sshkey_private_serialize_opt(k, b,
|
|
|
|
SSHKEY_SERIALIZE_DEFAULT);
|
2020-10-20 06:49:23 +08:00
|
|
|
if ((r = sshkey_xmss_get_state(k, printerror)) != 0 ||
|
2018-02-23 23:58:37 +08:00
|
|
|
(r = sshkey_private_serialize_opt(k, b,
|
|
|
|
SSHKEY_SERIALIZE_STATE)) != 0 ||
|
|
|
|
(r = sshkey_xmss_forward_state(k, maxsign)) != 0)
|
|
|
|
goto out;
|
|
|
|
r = 0;
|
|
|
|
out:
|
2020-10-20 06:49:23 +08:00
|
|
|
if ((rupdate = sshkey_xmss_update_state(k, printerror)) != 0) {
|
2018-02-23 23:58:37 +08:00
|
|
|
if (r == 0)
|
|
|
|
r = rupdate;
|
|
|
|
}
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
u_int32_t
|
|
|
|
sshkey_signatures_left(const struct sshkey *k)
|
|
|
|
{
|
|
|
|
if (sshkey_type_plain(k->type) == KEY_XMSS)
|
|
|
|
return sshkey_xmss_signatures_left(k);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
sshkey_enable_maxsign(struct sshkey *k, u_int32_t maxsign)
|
|
|
|
{
|
|
|
|
if (sshkey_type_plain(k->type) != KEY_XMSS)
|
|
|
|
return SSH_ERR_INVALID_ARGUMENT;
|
|
|
|
return sshkey_xmss_enable_maxsign(k, maxsign);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
sshkey_set_filename(struct sshkey *k, const char *filename)
|
|
|
|
{
|
|
|
|
if (k == NULL)
|
|
|
|
return SSH_ERR_INVALID_ARGUMENT;
|
|
|
|
if (sshkey_type_plain(k->type) != KEY_XMSS)
|
|
|
|
return 0;
|
|
|
|
if (filename == NULL)
|
|
|
|
return SSH_ERR_INVALID_ARGUMENT;
|
|
|
|
if ((k->xmss_filename = strdup(filename)) == NULL)
|
|
|
|
return SSH_ERR_ALLOC_FAIL;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
int
|
2019-06-21 12:21:04 +08:00
|
|
|
sshkey_private_serialize_maxsign(struct sshkey *k, struct sshbuf *b,
|
2020-10-20 06:49:23 +08:00
|
|
|
u_int32_t maxsign, int printerror)
|
2018-02-23 23:58:37 +08:00
|
|
|
{
|
|
|
|
return sshkey_private_serialize_opt(k, b, SSHKEY_SERIALIZE_DEFAULT);
|
|
|
|
}
|
|
|
|
|
|
|
|
u_int32_t
|
|
|
|
sshkey_signatures_left(const struct sshkey *k)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
sshkey_enable_maxsign(struct sshkey *k, u_int32_t maxsign)
|
|
|
|
{
|
|
|
|
return SSH_ERR_INVALID_ARGUMENT;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
sshkey_set_filename(struct sshkey *k, const char *filename)
|
|
|
|
{
|
|
|
|
if (k == NULL)
|
|
|
|
return SSH_ERR_INVALID_ARGUMENT;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif /* WITH_XMSS */
|