2022-10-28 00:44:44 +00:00
|
|
|
/* $OpenBSD: ssh-ed25519.c,v 1.19 2022/10/28 00:44:44 djm Exp $ */
|
2013-12-07 00:24:01 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2013 Markus Friedl <markus@openbsd.org>
|
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
|
|
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2013-12-07 01:37:53 +00:00
|
|
|
#include "includes.h"
|
|
|
|
|
2013-12-07 00:24:01 +00:00
|
|
|
#include <sys/types.h>
|
2014-07-02 05:28:02 +00:00
|
|
|
#include <limits.h>
|
2013-12-07 00:24:01 +00:00
|
|
|
|
|
|
|
#include "crypto_api.h"
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
|
|
|
|
#include "log.h"
|
2015-01-15 21:38:50 +00:00
|
|
|
#include "sshbuf.h"
|
2014-07-02 05:28:02 +00:00
|
|
|
#define SSHKEY_INTERNAL
|
|
|
|
#include "sshkey.h"
|
|
|
|
#include "ssherr.h"
|
2013-12-07 00:24:01 +00:00
|
|
|
#include "ssh.h"
|
|
|
|
|
2022-10-28 00:35:40 +00:00
|
|
|
static void
|
|
|
|
ssh_ed25519_cleanup(struct sshkey *k)
|
|
|
|
{
|
|
|
|
freezero(k->ed25519_pk, ED25519_PK_SZ);
|
|
|
|
freezero(k->ed25519_sk, ED25519_SK_SZ);
|
|
|
|
k->ed25519_pk = NULL;
|
|
|
|
k->ed25519_sk = NULL;
|
|
|
|
}
|
|
|
|
|
2022-10-28 00:36:31 +00:00
|
|
|
static int
|
|
|
|
ssh_ed25519_equal(const struct sshkey *a, const struct sshkey *b)
|
|
|
|
{
|
|
|
|
if (a->ed25519_pk == NULL || b->ed25519_pk == NULL)
|
|
|
|
return 0;
|
|
|
|
if (memcmp(a->ed25519_pk, b->ed25519_pk, ED25519_PK_SZ) != 0)
|
|
|
|
return 0;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2022-10-28 00:37:24 +00:00
|
|
|
static int
|
|
|
|
ssh_ed25519_serialize_public(const struct sshkey *key, struct sshbuf *b,
|
2022-10-28 00:41:52 +00:00
|
|
|
enum sshkey_serialize_rep opts)
|
2022-10-28 00:37:24 +00:00
|
|
|
{
|
|
|
|
int r;
|
|
|
|
|
|
|
|
if (key->ed25519_pk == NULL)
|
|
|
|
return SSH_ERR_INVALID_ARGUMENT;
|
2022-10-28 00:41:52 +00:00
|
|
|
if ((r = sshbuf_put_string(b, key->ed25519_pk, ED25519_PK_SZ)) != 0)
|
2022-10-28 00:37:24 +00:00
|
|
|
return r;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-10-28 00:44:17 +00:00
|
|
|
static int
|
|
|
|
ssh_ed25519_serialize_private(const struct sshkey *key, struct sshbuf *b,
|
|
|
|
enum sshkey_serialize_rep opts)
|
|
|
|
{
|
|
|
|
int r;
|
|
|
|
|
|
|
|
if ((r = sshbuf_put_string(b, key->ed25519_pk, ED25519_PK_SZ)) != 0 ||
|
|
|
|
(r = sshbuf_put_string(b, key->ed25519_sk, ED25519_SK_SZ)) != 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-10-28 00:39:29 +00:00
|
|
|
static int
|
|
|
|
ssh_ed25519_generate(struct sshkey *k, int bits)
|
|
|
|
{
|
|
|
|
if ((k->ed25519_pk = malloc(ED25519_PK_SZ)) == NULL ||
|
|
|
|
(k->ed25519_sk = malloc(ED25519_SK_SZ)) == NULL)
|
|
|
|
return SSH_ERR_ALLOC_FAIL;
|
|
|
|
crypto_sign_ed25519_keypair(k->ed25519_pk, k->ed25519_sk);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-10-28 00:41:17 +00:00
|
|
|
static int
|
|
|
|
ssh_ed25519_copy_public(const struct sshkey *from, struct sshkey *to)
|
|
|
|
{
|
|
|
|
if (from->ed25519_pk == NULL)
|
|
|
|
return 0; /* XXX SSH_ERR_INTERNAL_ERROR ? */
|
|
|
|
if ((to->ed25519_pk = malloc(ED25519_PK_SZ)) == NULL)
|
|
|
|
return SSH_ERR_ALLOC_FAIL;
|
|
|
|
memcpy(to->ed25519_pk, from->ed25519_pk, ED25519_PK_SZ);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-10-28 00:41:52 +00:00
|
|
|
static int
|
|
|
|
ssh_ed25519_deserialize_public(const char *ktype, struct sshbuf *b,
|
|
|
|
struct sshkey *key)
|
|
|
|
{
|
|
|
|
u_char *pk = NULL;
|
|
|
|
size_t len = 0;
|
|
|
|
int r;
|
|
|
|
|
|
|
|
if ((r = sshbuf_get_string(b, &pk, &len)) != 0)
|
|
|
|
return r;
|
|
|
|
if (len != ED25519_PK_SZ) {
|
|
|
|
freezero(pk, len);
|
|
|
|
return SSH_ERR_INVALID_FORMAT;
|
|
|
|
}
|
|
|
|
key->ed25519_pk = pk;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-10-28 00:44:44 +00:00
|
|
|
static int
|
|
|
|
ssh_ed25519_deserialize_private(const char *ktype, struct sshbuf *b,
|
|
|
|
struct sshkey *key)
|
|
|
|
{
|
|
|
|
int r;
|
|
|
|
size_t sklen = 0;
|
|
|
|
u_char *ed25519_sk = NULL;
|
|
|
|
|
|
|
|
if ((r = ssh_ed25519_deserialize_public(NULL, b, key)) != 0)
|
|
|
|
goto out;
|
|
|
|
if ((r = sshbuf_get_string(b, &ed25519_sk, &sklen)) != 0)
|
|
|
|
goto out;
|
|
|
|
if (sklen != ED25519_SK_SZ) {
|
|
|
|
r = SSH_ERR_INVALID_FORMAT;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
key->ed25519_sk = ed25519_sk;
|
|
|
|
ed25519_sk = NULL; /* transferred */
|
|
|
|
/* success */
|
|
|
|
r = 0;
|
|
|
|
out:
|
|
|
|
freezero(ed25519_sk, sklen);
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2022-10-28 00:43:08 +00:00
|
|
|
static int
|
|
|
|
ssh_ed25519_sign(struct sshkey *key,
|
|
|
|
u_char **sigp, size_t *lenp,
|
|
|
|
const u_char *data, size_t datalen,
|
|
|
|
const char *alg, const char *sk_provider, const char *sk_pin, u_int compat)
|
2013-12-07 00:24:01 +00:00
|
|
|
{
|
2014-07-02 05:28:02 +00:00
|
|
|
u_char *sig = NULL;
|
|
|
|
size_t slen = 0, len;
|
2013-12-07 00:24:01 +00:00
|
|
|
unsigned long long smlen;
|
2014-07-02 05:28:02 +00:00
|
|
|
int r, ret;
|
|
|
|
struct sshbuf *b = NULL;
|
2013-12-07 00:24:01 +00:00
|
|
|
|
2014-07-02 05:28:02 +00:00
|
|
|
if (lenp != NULL)
|
|
|
|
*lenp = 0;
|
|
|
|
if (sigp != NULL)
|
|
|
|
*sigp = NULL;
|
2014-02-24 04:57:22 +00:00
|
|
|
|
2014-07-02 05:28:02 +00:00
|
|
|
if (key == NULL ||
|
|
|
|
sshkey_type_plain(key->type) != KEY_ED25519 ||
|
|
|
|
key->ed25519_sk == NULL ||
|
|
|
|
datalen >= INT_MAX - crypto_sign_ed25519_BYTES)
|
|
|
|
return SSH_ERR_INVALID_ARGUMENT;
|
2013-12-07 00:24:01 +00:00
|
|
|
smlen = slen = datalen + crypto_sign_ed25519_BYTES;
|
2014-07-02 05:28:02 +00:00
|
|
|
if ((sig = malloc(slen)) == NULL)
|
|
|
|
return SSH_ERR_ALLOC_FAIL;
|
2013-12-07 00:24:01 +00:00
|
|
|
|
|
|
|
if ((ret = crypto_sign_ed25519(sig, &smlen, data, datalen,
|
|
|
|
key->ed25519_sk)) != 0 || smlen <= datalen) {
|
2014-07-02 05:28:02 +00:00
|
|
|
r = SSH_ERR_INVALID_ARGUMENT; /* XXX better error? */
|
|
|
|
goto out;
|
2013-12-07 00:24:01 +00:00
|
|
|
}
|
|
|
|
/* encode signature */
|
2014-07-02 05:28:02 +00:00
|
|
|
if ((b = sshbuf_new()) == NULL) {
|
|
|
|
r = SSH_ERR_ALLOC_FAIL;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
if ((r = sshbuf_put_cstring(b, "ssh-ed25519")) != 0 ||
|
|
|
|
(r = sshbuf_put_string(b, sig, smlen - datalen)) != 0)
|
|
|
|
goto out;
|
|
|
|
len = sshbuf_len(b);
|
|
|
|
if (sigp != NULL) {
|
|
|
|
if ((*sigp = malloc(len)) == NULL) {
|
|
|
|
r = SSH_ERR_ALLOC_FAIL;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
memcpy(*sigp, sshbuf_ptr(b), len);
|
|
|
|
}
|
2013-12-07 00:24:01 +00:00
|
|
|
if (lenp != NULL)
|
|
|
|
*lenp = len;
|
2014-07-02 05:28:02 +00:00
|
|
|
/* success */
|
|
|
|
r = 0;
|
|
|
|
out:
|
|
|
|
sshbuf_free(b);
|
2022-08-26 08:12:56 +00:00
|
|
|
if (sig != NULL)
|
2020-02-26 13:40:09 +00:00
|
|
|
freezero(sig, slen);
|
2013-12-07 00:24:01 +00:00
|
|
|
|
2014-07-02 05:28:02 +00:00
|
|
|
return r;
|
2013-12-07 00:24:01 +00:00
|
|
|
}
|
|
|
|
|
2022-10-28 00:43:08 +00:00
|
|
|
static int
|
2014-07-02 05:28:02 +00:00
|
|
|
ssh_ed25519_verify(const struct sshkey *key,
|
2022-10-28 00:43:08 +00:00
|
|
|
const u_char *sig, size_t siglen,
|
|
|
|
const u_char *data, size_t dlen, const char *alg, u_int compat,
|
|
|
|
struct sshkey_sig_details **detailsp)
|
2013-12-07 00:24:01 +00:00
|
|
|
{
|
2014-07-02 05:28:02 +00:00
|
|
|
struct sshbuf *b = NULL;
|
|
|
|
char *ktype = NULL;
|
|
|
|
const u_char *sigblob;
|
|
|
|
u_char *sm = NULL, *m = NULL;
|
|
|
|
size_t len;
|
|
|
|
unsigned long long smlen = 0, mlen = 0;
|
|
|
|
int r, ret;
|
2013-12-07 00:24:01 +00:00
|
|
|
|
2014-07-02 05:28:02 +00:00
|
|
|
if (key == NULL ||
|
|
|
|
sshkey_type_plain(key->type) != KEY_ED25519 ||
|
|
|
|
key->ed25519_pk == NULL ||
|
2022-10-28 00:43:08 +00:00
|
|
|
dlen >= INT_MAX - crypto_sign_ed25519_BYTES ||
|
|
|
|
sig == NULL || siglen == 0)
|
2014-07-02 05:28:02 +00:00
|
|
|
return SSH_ERR_INVALID_ARGUMENT;
|
|
|
|
|
2022-10-28 00:43:08 +00:00
|
|
|
if ((b = sshbuf_from(sig, siglen)) == NULL)
|
2014-07-02 05:28:02 +00:00
|
|
|
return SSH_ERR_ALLOC_FAIL;
|
|
|
|
if ((r = sshbuf_get_cstring(b, &ktype, NULL)) != 0 ||
|
|
|
|
(r = sshbuf_get_string_direct(b, &sigblob, &len)) != 0)
|
|
|
|
goto out;
|
2013-12-07 00:24:01 +00:00
|
|
|
if (strcmp("ssh-ed25519", ktype) != 0) {
|
2014-07-02 05:28:02 +00:00
|
|
|
r = SSH_ERR_KEY_TYPE_MISMATCH;
|
|
|
|
goto out;
|
2013-12-07 00:24:01 +00:00
|
|
|
}
|
2014-07-02 05:28:02 +00:00
|
|
|
if (sshbuf_len(b) != 0) {
|
|
|
|
r = SSH_ERR_UNEXPECTED_TRAILING_DATA;
|
|
|
|
goto out;
|
2013-12-07 00:24:01 +00:00
|
|
|
}
|
|
|
|
if (len > crypto_sign_ed25519_BYTES) {
|
2014-07-02 05:28:02 +00:00
|
|
|
r = SSH_ERR_INVALID_FORMAT;
|
|
|
|
goto out;
|
2013-12-07 00:24:01 +00:00
|
|
|
}
|
2022-10-28 00:43:08 +00:00
|
|
|
if (dlen >= SIZE_MAX - len) {
|
2014-10-14 03:09:59 +00:00
|
|
|
r = SSH_ERR_INVALID_ARGUMENT;
|
|
|
|
goto out;
|
|
|
|
}
|
2022-10-28 00:43:08 +00:00
|
|
|
smlen = len + dlen;
|
2014-07-02 05:28:02 +00:00
|
|
|
mlen = smlen;
|
2015-01-15 21:38:50 +00:00
|
|
|
if ((sm = malloc(smlen)) == NULL || (m = malloc(mlen)) == NULL) {
|
2014-07-02 05:28:02 +00:00
|
|
|
r = SSH_ERR_ALLOC_FAIL;
|
|
|
|
goto out;
|
|
|
|
}
|
2013-12-07 00:24:01 +00:00
|
|
|
memcpy(sm, sigblob, len);
|
2022-10-28 00:43:08 +00:00
|
|
|
memcpy(sm+len, data, dlen);
|
2013-12-07 00:24:01 +00:00
|
|
|
if ((ret = crypto_sign_ed25519_open(m, &mlen, sm, smlen,
|
|
|
|
key->ed25519_pk)) != 0) {
|
2020-10-18 11:32:01 +00:00
|
|
|
debug2_f("crypto_sign_ed25519_open failed: %d", ret);
|
2013-12-07 00:24:01 +00:00
|
|
|
}
|
2022-10-28 00:43:08 +00:00
|
|
|
if (ret != 0 || mlen != dlen) {
|
2014-07-02 05:28:02 +00:00
|
|
|
r = SSH_ERR_SIGNATURE_INVALID;
|
|
|
|
goto out;
|
2013-12-07 00:24:01 +00:00
|
|
|
}
|
|
|
|
/* XXX compare 'm' and 'data' ? */
|
2014-07-02 05:28:02 +00:00
|
|
|
/* success */
|
|
|
|
r = 0;
|
|
|
|
out:
|
2022-08-26 08:12:56 +00:00
|
|
|
if (sm != NULL)
|
2020-02-26 13:40:09 +00:00
|
|
|
freezero(sm, smlen);
|
2022-08-26 08:12:56 +00:00
|
|
|
if (m != NULL)
|
2020-02-26 13:40:09 +00:00
|
|
|
freezero(m, smlen); /* NB mlen may be invalid if r != 0 */
|
2014-07-02 05:28:02 +00:00
|
|
|
sshbuf_free(b);
|
|
|
|
free(ktype);
|
|
|
|
return r;
|
2013-12-07 00:24:01 +00:00
|
|
|
}
|
2022-10-28 00:35:40 +00:00
|
|
|
|
2022-10-28 00:36:31 +00:00
|
|
|
/* NB. not static; used by ED25519-SK */
|
|
|
|
const struct sshkey_impl_funcs sshkey_ed25519_funcs = {
|
2022-10-28 00:35:40 +00:00
|
|
|
/* .size = */ NULL,
|
|
|
|
/* .alloc = */ NULL,
|
|
|
|
/* .cleanup = */ ssh_ed25519_cleanup,
|
2022-10-28 00:36:31 +00:00
|
|
|
/* .equal = */ ssh_ed25519_equal,
|
2022-10-28 00:37:24 +00:00
|
|
|
/* .ssh_serialize_public = */ ssh_ed25519_serialize_public,
|
2022-10-28 00:41:52 +00:00
|
|
|
/* .ssh_deserialize_public = */ ssh_ed25519_deserialize_public,
|
2022-10-28 00:44:17 +00:00
|
|
|
/* .ssh_serialize_private = */ ssh_ed25519_serialize_private,
|
2022-10-28 00:44:44 +00:00
|
|
|
/* .ssh_deserialize_private = */ ssh_ed25519_deserialize_private,
|
2022-10-28 00:39:29 +00:00
|
|
|
/* .generate = */ ssh_ed25519_generate,
|
2022-10-28 00:41:17 +00:00
|
|
|
/* .copy_public = */ ssh_ed25519_copy_public,
|
2022-10-28 00:43:08 +00:00
|
|
|
/* .sign = */ ssh_ed25519_sign,
|
|
|
|
/* .verify = */ ssh_ed25519_verify,
|
2022-10-28 00:35:40 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const struct sshkey_impl sshkey_ed25519_impl = {
|
|
|
|
/* .name = */ "ssh-ed25519",
|
|
|
|
/* .shortname = */ "ED25519",
|
|
|
|
/* .sigalg = */ NULL,
|
|
|
|
/* .type = */ KEY_ED25519,
|
|
|
|
/* .nid = */ 0,
|
|
|
|
/* .cert = */ 0,
|
|
|
|
/* .sigonly = */ 0,
|
|
|
|
/* .keybits = */ 256,
|
|
|
|
/* .funcs = */ &sshkey_ed25519_funcs,
|
|
|
|
};
|
|
|
|
|
|
|
|
const struct sshkey_impl sshkey_ed25519_cert_impl = {
|
|
|
|
/* .name = */ "ssh-ed25519-cert-v01@openssh.com",
|
|
|
|
/* .shortname = */ "ED25519-CERT",
|
|
|
|
/* .sigalg = */ NULL,
|
|
|
|
/* .type = */ KEY_ED25519_CERT,
|
|
|
|
/* .nid = */ 0,
|
|
|
|
/* .cert = */ 1,
|
|
|
|
/* .sigonly = */ 0,
|
|
|
|
/* .keybits = */ 256,
|
|
|
|
/* .funcs = */ &sshkey_ed25519_funcs,
|
|
|
|
};
|