2022-10-28 00:44:17 +00:00
|
|
|
/* $OpenBSD: ssh-ed25519-sk.c,v 1.14 2022/10/28 00:44:17 djm Exp $ */
|
2019-11-12 19:29:24 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2019 Markus Friedl. All rights reserved.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
2019-11-26 03:04:27 +00:00
|
|
|
|
|
|
|
/* #define DEBUG_SK 1 */
|
|
|
|
|
2020-01-14 03:26:41 +00:00
|
|
|
#include "includes.h"
|
|
|
|
|
2019-11-12 19:29:24 +00:00
|
|
|
#define SSHKEY_INTERNAL
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <limits.h>
|
|
|
|
|
|
|
|
#include "crypto_api.h"
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
|
|
|
|
#include "log.h"
|
|
|
|
#include "sshbuf.h"
|
|
|
|
#include "sshkey.h"
|
|
|
|
#include "ssherr.h"
|
|
|
|
#include "ssh.h"
|
|
|
|
#include "digest.h"
|
|
|
|
|
2022-10-28 00:36:31 +00:00
|
|
|
/* Reuse some ED25519 internals */
|
|
|
|
extern struct sshkey_impl_funcs sshkey_ed25519_funcs;
|
|
|
|
|
2022-10-28 00:35:40 +00:00
|
|
|
static void
|
|
|
|
ssh_ed25519_sk_cleanup(struct sshkey *k)
|
|
|
|
{
|
2022-10-28 00:36:31 +00:00
|
|
|
sshkey_sk_cleanup(k);
|
|
|
|
sshkey_ed25519_funcs.cleanup(k);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
ssh_ed25519_sk_equal(const struct sshkey *a, const struct sshkey *b)
|
|
|
|
{
|
|
|
|
if (!sshkey_sk_fields_equal(a, b))
|
|
|
|
return 0;
|
|
|
|
if (!sshkey_ed25519_funcs.equal(a, b))
|
|
|
|
return 0;
|
|
|
|
return 1;
|
2022-10-28 00:35:40 +00:00
|
|
|
}
|
|
|
|
|
2022-10-28 00:37:24 +00:00
|
|
|
static int
|
|
|
|
ssh_ed25519_sk_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;
|
|
|
|
|
2022-10-28 00:41:52 +00:00
|
|
|
if ((r = sshkey_ed25519_funcs.serialize_public(key, b, opts)) != 0)
|
2022-10-28 00:37:24 +00:00
|
|
|
return r;
|
|
|
|
if ((r = sshkey_serialize_sk(key, b)) != 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-10-28 00:44:17 +00:00
|
|
|
static int
|
|
|
|
ssh_ed25519_sk_serialize_private(const struct sshkey *key, struct sshbuf *b,
|
|
|
|
enum sshkey_serialize_rep opts)
|
|
|
|
{
|
|
|
|
int r;
|
|
|
|
|
|
|
|
if ((r = sshkey_ed25519_funcs.serialize_public(key, b, opts)) != 0)
|
|
|
|
return r;
|
|
|
|
if ((r = sshkey_serialize_private_sk(key, b)) != 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-10-28 00:41:17 +00:00
|
|
|
static int
|
|
|
|
ssh_ed25519_sk_copy_public(const struct sshkey *from, struct sshkey *to)
|
|
|
|
{
|
|
|
|
int r;
|
|
|
|
|
|
|
|
if ((r = sshkey_ed25519_funcs.copy_public(from, to)) != 0)
|
|
|
|
return r;
|
|
|
|
if ((r = sshkey_copy_public_sk(from, to)) != 0)
|
|
|
|
return r;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-10-28 00:41:52 +00:00
|
|
|
static int
|
|
|
|
ssh_ed25519_sk_deserialize_public(const char *ktype, struct sshbuf *b,
|
|
|
|
struct sshkey *key)
|
|
|
|
{
|
|
|
|
int r;
|
|
|
|
|
|
|
|
if ((r = sshkey_ed25519_funcs.deserialize_public(ktype, b, key)) != 0)
|
|
|
|
return r;
|
|
|
|
if ((r = sshkey_deserialize_sk(b, key)) != 0)
|
|
|
|
return r;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-10-28 00:43:08 +00:00
|
|
|
static int
|
2019-11-12 19:29:24 +00:00
|
|
|
ssh_ed25519_sk_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,
|
2019-11-25 00:51:37 +00:00
|
|
|
struct sshkey_sig_details **detailsp)
|
2019-11-12 19:29:24 +00:00
|
|
|
{
|
|
|
|
struct sshbuf *b = NULL;
|
|
|
|
struct sshbuf *encoded = NULL;
|
|
|
|
char *ktype = NULL;
|
|
|
|
const u_char *sigblob;
|
|
|
|
const u_char *sm;
|
|
|
|
u_char *m = NULL;
|
|
|
|
u_char apphash[32];
|
|
|
|
u_char msghash[32];
|
|
|
|
u_char sig_flags;
|
|
|
|
u_int sig_counter;
|
|
|
|
size_t len;
|
|
|
|
unsigned long long smlen = 0, mlen = 0;
|
|
|
|
int r = SSH_ERR_INTERNAL_ERROR;
|
|
|
|
int ret;
|
2019-11-25 00:51:37 +00:00
|
|
|
struct sshkey_sig_details *details = NULL;
|
|
|
|
|
|
|
|
if (detailsp != NULL)
|
|
|
|
*detailsp = NULL;
|
2019-11-12 19:29:24 +00:00
|
|
|
|
|
|
|
if (key == NULL ||
|
|
|
|
sshkey_type_plain(key->type) != KEY_ED25519_SK ||
|
|
|
|
key->ed25519_pk == NULL ||
|
2022-10-28 00:43:08 +00:00
|
|
|
sig == NULL || siglen == 0)
|
2019-11-12 19:29:24 +00:00
|
|
|
return SSH_ERR_INVALID_ARGUMENT;
|
|
|
|
|
2022-10-28 00:43:08 +00:00
|
|
|
if ((b = sshbuf_from(sig, siglen)) == NULL)
|
2019-11-12 19:29:24 +00:00
|
|
|
return SSH_ERR_ALLOC_FAIL;
|
|
|
|
if (sshbuf_get_cstring(b, &ktype, NULL) != 0 ||
|
2019-11-12 19:34:40 +00:00
|
|
|
sshbuf_get_string_direct(b, &sigblob, &len) != 0 ||
|
|
|
|
sshbuf_get_u8(b, &sig_flags) != 0 ||
|
|
|
|
sshbuf_get_u32(b, &sig_counter) != 0) {
|
2019-11-12 19:29:24 +00:00
|
|
|
r = SSH_ERR_INVALID_FORMAT;
|
|
|
|
goto out;
|
|
|
|
}
|
2019-11-26 03:04:27 +00:00
|
|
|
#ifdef DEBUG_SK
|
|
|
|
fprintf(stderr, "%s: data:\n", __func__);
|
|
|
|
/* sshbuf_dump_data(data, datalen, stderr); */
|
|
|
|
fprintf(stderr, "%s: sigblob:\n", __func__);
|
|
|
|
sshbuf_dump_data(sigblob, len, stderr);
|
|
|
|
fprintf(stderr, "%s: sig_flags = 0x%02x, sig_counter = %u\n",
|
|
|
|
__func__, sig_flags, sig_counter);
|
|
|
|
#endif
|
2019-11-12 19:29:24 +00:00
|
|
|
if (strcmp(sshkey_ssh_name_plain(key), ktype) != 0) {
|
|
|
|
r = SSH_ERR_KEY_TYPE_MISMATCH;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
if (sshbuf_len(b) != 0) {
|
|
|
|
r = SSH_ERR_UNEXPECTED_TRAILING_DATA;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
if (len > crypto_sign_ed25519_BYTES) {
|
|
|
|
r = SSH_ERR_INVALID_FORMAT;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
if (ssh_digest_memory(SSH_DIGEST_SHA256, key->sk_application,
|
|
|
|
strlen(key->sk_application), apphash, sizeof(apphash)) != 0 ||
|
2022-10-28 00:43:08 +00:00
|
|
|
ssh_digest_memory(SSH_DIGEST_SHA256, data, dlen,
|
2019-11-12 19:29:24 +00:00
|
|
|
msghash, sizeof(msghash)) != 0) {
|
|
|
|
r = SSH_ERR_INVALID_ARGUMENT;
|
|
|
|
goto out;
|
|
|
|
}
|
2019-11-26 03:04:27 +00:00
|
|
|
#ifdef DEBUG_SK
|
|
|
|
fprintf(stderr, "%s: hashed application:\n", __func__);
|
|
|
|
sshbuf_dump_data(apphash, sizeof(apphash), stderr);
|
|
|
|
fprintf(stderr, "%s: hashed message:\n", __func__);
|
|
|
|
sshbuf_dump_data(msghash, sizeof(msghash), stderr);
|
|
|
|
#endif
|
2019-11-25 00:51:37 +00:00
|
|
|
if ((details = calloc(1, sizeof(*details))) == NULL) {
|
|
|
|
r = SSH_ERR_ALLOC_FAIL;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
details->sk_counter = sig_counter;
|
|
|
|
details->sk_flags = sig_flags;
|
2019-11-12 19:29:24 +00:00
|
|
|
if ((encoded = sshbuf_new()) == NULL) {
|
|
|
|
r = SSH_ERR_ALLOC_FAIL;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
if (sshbuf_put(encoded, sigblob, len) != 0 ||
|
|
|
|
sshbuf_put(encoded, apphash, sizeof(apphash)) != 0 ||
|
|
|
|
sshbuf_put_u8(encoded, sig_flags) != 0 ||
|
|
|
|
sshbuf_put_u32(encoded, sig_counter) != 0 ||
|
|
|
|
sshbuf_put(encoded, msghash, sizeof(msghash)) != 0) {
|
|
|
|
r = SSH_ERR_ALLOC_FAIL;
|
|
|
|
goto out;
|
|
|
|
}
|
2019-11-26 03:04:27 +00:00
|
|
|
#ifdef DEBUG_SK
|
|
|
|
fprintf(stderr, "%s: signed buf:\n", __func__);
|
|
|
|
sshbuf_dump(encoded, stderr);
|
|
|
|
#endif
|
2019-11-12 19:29:24 +00:00
|
|
|
sm = sshbuf_ptr(encoded);
|
|
|
|
smlen = sshbuf_len(encoded);
|
|
|
|
mlen = smlen;
|
|
|
|
if ((m = malloc(smlen)) == NULL) {
|
|
|
|
r = SSH_ERR_ALLOC_FAIL;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
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);
|
2019-11-12 19:29:24 +00:00
|
|
|
}
|
|
|
|
if (ret != 0 || mlen != smlen - len) {
|
|
|
|
r = SSH_ERR_SIGNATURE_INVALID;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
/* XXX compare 'm' and 'sm + len' ? */
|
|
|
|
/* success */
|
|
|
|
r = 0;
|
2019-11-25 00:51:37 +00:00
|
|
|
if (detailsp != NULL) {
|
|
|
|
*detailsp = details;
|
|
|
|
details = NULL;
|
|
|
|
}
|
2019-11-12 19:29:24 +00:00
|
|
|
out:
|
2020-02-26 13:40:09 +00:00
|
|
|
if (m != NULL)
|
|
|
|
freezero(m, smlen); /* NB mlen may be invalid if r != 0 */
|
2019-11-25 00:51:37 +00:00
|
|
|
sshkey_sig_details_free(details);
|
2019-11-12 19:29:24 +00:00
|
|
|
sshbuf_free(b);
|
|
|
|
sshbuf_free(encoded);
|
|
|
|
free(ktype);
|
|
|
|
return r;
|
|
|
|
}
|
2022-10-28 00:35:40 +00:00
|
|
|
|
|
|
|
static const struct sshkey_impl_funcs sshkey_ed25519_sk_funcs = {
|
|
|
|
/* .size = */ NULL,
|
|
|
|
/* .alloc = */ NULL,
|
|
|
|
/* .cleanup = */ ssh_ed25519_sk_cleanup,
|
2022-10-28 00:36:31 +00:00
|
|
|
/* .equal = */ ssh_ed25519_sk_equal,
|
2022-10-28 00:37:24 +00:00
|
|
|
/* .ssh_serialize_public = */ ssh_ed25519_sk_serialize_public,
|
2022-10-28 00:41:52 +00:00
|
|
|
/* .ssh_deserialize_public = */ ssh_ed25519_sk_deserialize_public,
|
2022-10-28 00:44:17 +00:00
|
|
|
/* .ssh_serialize_private = */ ssh_ed25519_sk_serialize_private,
|
2022-10-28 00:39:29 +00:00
|
|
|
/* .generate = */ NULL,
|
2022-10-28 00:41:17 +00:00
|
|
|
/* .copy_public = */ ssh_ed25519_sk_copy_public,
|
2022-10-28 00:43:08 +00:00
|
|
|
/* .sign = */ NULL,
|
|
|
|
/* .verify = */ ssh_ed25519_sk_verify,
|
2022-10-28 00:35:40 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const struct sshkey_impl sshkey_ed25519_sk_impl = {
|
|
|
|
/* .name = */ "sk-ssh-ed25519@openssh.com",
|
|
|
|
/* .shortname = */ "ED25519-SK",
|
|
|
|
/* .sigalg = */ NULL,
|
|
|
|
/* .type = */ KEY_ED25519_SK,
|
|
|
|
/* .nid = */ 0,
|
|
|
|
/* .cert = */ 0,
|
|
|
|
/* .sigonly = */ 0,
|
|
|
|
/* .keybits = */ 256,
|
|
|
|
/* .funcs = */ &sshkey_ed25519_sk_funcs,
|
|
|
|
};
|
|
|
|
|
|
|
|
const struct sshkey_impl sshkey_ed25519_sk_cert_impl = {
|
|
|
|
/* .name = */ "sk-ssh-ed25519-cert-v01@openssh.com",
|
|
|
|
/* .shortname = */ "ED25519-SK-CERT",
|
|
|
|
/* .sigalg = */ NULL,
|
|
|
|
/* .type = */ KEY_ED25519_SK_CERT,
|
|
|
|
/* .nid = */ 0,
|
|
|
|
/* .cert = */ 1,
|
|
|
|
/* .sigonly = */ 0,
|
|
|
|
/* .keybits = */ 256,
|
|
|
|
/* .funcs = */ &sshkey_ed25519_sk_funcs,
|
|
|
|
};
|