2024-08-15 08:51:51 +08:00
|
|
|
/* $OpenBSD: ssh-rsa.c,v 1.80 2024/08/15 00:51:51 djm Exp $ */
|
2000-11-13 19:57:25 +08:00
|
|
|
/*
|
2003-06-22 18:45:15 +08:00
|
|
|
* Copyright (c) 2000, 2003 Markus Friedl <markus@openbsd.org>
|
2000-11-13 19:57:25 +08:00
|
|
|
*
|
2003-06-22 18:45:15 +08:00
|
|
|
* 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.
|
2000-11-13 19:57:25 +08:00
|
|
|
*
|
2003-06-22 18:45:15 +08:00
|
|
|
* 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.
|
2000-11-13 19:57:25 +08:00
|
|
|
*/
|
2006-08-05 10:39:39 +08:00
|
|
|
|
2000-11-13 19:57:25 +08:00
|
|
|
#include "includes.h"
|
|
|
|
|
2015-01-14 23:21:31 +08:00
|
|
|
#ifdef WITH_OPENSSL
|
|
|
|
|
2006-08-05 10:39:39 +08:00
|
|
|
#include <sys/types.h>
|
|
|
|
|
2000-11-13 19:57:25 +08:00
|
|
|
#include <openssl/evp.h>
|
|
|
|
#include <openssl/err.h>
|
|
|
|
|
2006-09-01 13:38:36 +08:00
|
|
|
#include <stdarg.h>
|
2006-07-24 12:13:33 +08:00
|
|
|
#include <string.h>
|
|
|
|
|
2014-07-02 13:28:02 +08:00
|
|
|
#include "sshbuf.h"
|
|
|
|
#include "ssherr.h"
|
|
|
|
#define SSHKEY_INTERNAL
|
|
|
|
#include "sshkey.h"
|
2014-01-10 07:58:53 +08:00
|
|
|
#include "digest.h"
|
2017-12-19 07:14:34 +08:00
|
|
|
#include "log.h"
|
2000-11-13 19:57:25 +08:00
|
|
|
|
2018-09-13 10:13:50 +08:00
|
|
|
#include "openbsd-compat/openssl-compat.h"
|
|
|
|
|
2022-10-28 08:35:40 +08:00
|
|
|
static u_int
|
2024-08-15 08:51:51 +08:00
|
|
|
ssh_rsa_size(const struct sshkey *k)
|
2022-10-28 08:35:40 +08:00
|
|
|
{
|
2024-08-15 08:51:51 +08:00
|
|
|
if (k->pkey == NULL)
|
2022-10-28 08:35:40 +08:00
|
|
|
return 0;
|
2024-08-15 08:51:51 +08:00
|
|
|
return EVP_PKEY_bits(k->pkey);
|
2022-10-28 08:35:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
ssh_rsa_alloc(struct sshkey *k)
|
|
|
|
{
|
2024-08-15 08:51:51 +08:00
|
|
|
if ((k->pkey = EVP_PKEY_new()) == NULL)
|
2022-10-28 08:35:40 +08:00
|
|
|
return SSH_ERR_ALLOC_FAIL;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
ssh_rsa_cleanup(struct sshkey *k)
|
|
|
|
{
|
2024-08-15 08:51:51 +08:00
|
|
|
EVP_PKEY_free(k->pkey);
|
|
|
|
k->pkey = NULL;
|
2022-10-28 08:35:40 +08:00
|
|
|
}
|
|
|
|
|
2022-10-28 08:36:31 +08:00
|
|
|
static int
|
|
|
|
ssh_rsa_equal(const struct sshkey *a, const struct sshkey *b)
|
|
|
|
{
|
2024-08-15 08:51:51 +08:00
|
|
|
if (a->pkey == NULL || b->pkey == NULL)
|
2022-10-28 08:36:31 +08:00
|
|
|
return 0;
|
2024-08-15 08:51:51 +08:00
|
|
|
return EVP_PKEY_cmp(a->pkey, b->pkey) == 1;
|
2022-10-28 08:36:31 +08:00
|
|
|
}
|
|
|
|
|
2022-10-28 08:37:24 +08:00
|
|
|
static int
|
|
|
|
ssh_rsa_serialize_public(const struct sshkey *key, struct sshbuf *b,
|
2022-10-28 08:41:52 +08:00
|
|
|
enum sshkey_serialize_rep opts)
|
2022-10-28 08:37:24 +08:00
|
|
|
{
|
|
|
|
int r;
|
|
|
|
const BIGNUM *rsa_n, *rsa_e;
|
2024-08-15 08:51:51 +08:00
|
|
|
const RSA *rsa;
|
2022-10-28 08:37:24 +08:00
|
|
|
|
2024-08-15 08:51:51 +08:00
|
|
|
if (key->pkey == NULL)
|
2022-10-28 08:37:24 +08:00
|
|
|
return SSH_ERR_INVALID_ARGUMENT;
|
2024-08-15 08:51:51 +08:00
|
|
|
if ((rsa = EVP_PKEY_get0_RSA(key->pkey)) == NULL)
|
|
|
|
return SSH_ERR_LIBCRYPTO_ERROR;
|
|
|
|
|
|
|
|
RSA_get0_key(rsa, &rsa_n, &rsa_e, NULL);
|
2022-10-28 08:41:52 +08:00
|
|
|
if ((r = sshbuf_put_bignum2(b, rsa_e)) != 0 ||
|
2022-10-28 08:37:24 +08:00
|
|
|
(r = sshbuf_put_bignum2(b, rsa_n)) != 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-10-28 08:44:17 +08:00
|
|
|
static int
|
|
|
|
ssh_rsa_serialize_private(const struct sshkey *key, struct sshbuf *b,
|
|
|
|
enum sshkey_serialize_rep opts)
|
|
|
|
{
|
|
|
|
int r;
|
|
|
|
const BIGNUM *rsa_n, *rsa_e, *rsa_d, *rsa_iqmp, *rsa_p, *rsa_q;
|
2024-08-15 08:51:51 +08:00
|
|
|
const RSA *rsa;
|
2022-10-28 08:44:17 +08:00
|
|
|
|
2024-08-15 08:51:51 +08:00
|
|
|
if ((rsa = EVP_PKEY_get0_RSA(key->pkey)) == NULL)
|
|
|
|
return SSH_ERR_LIBCRYPTO_ERROR;
|
|
|
|
RSA_get0_key(rsa, &rsa_n, &rsa_e, &rsa_d);
|
|
|
|
RSA_get0_factors(rsa, &rsa_p, &rsa_q);
|
|
|
|
RSA_get0_crt_params(rsa, NULL, NULL, &rsa_iqmp);
|
2022-10-28 08:44:17 +08:00
|
|
|
|
|
|
|
if (!sshkey_is_cert(key)) {
|
|
|
|
/* Note: can't reuse ssh_rsa_serialize_public: e, n vs. n, e */
|
|
|
|
if ((r = sshbuf_put_bignum2(b, rsa_n)) != 0 ||
|
|
|
|
(r = sshbuf_put_bignum2(b, rsa_e)) != 0)
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
if ((r = sshbuf_put_bignum2(b, rsa_d)) != 0 ||
|
|
|
|
(r = sshbuf_put_bignum2(b, rsa_iqmp)) != 0 ||
|
|
|
|
(r = sshbuf_put_bignum2(b, rsa_p)) != 0 ||
|
|
|
|
(r = sshbuf_put_bignum2(b, rsa_q)) != 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-10-28 08:39:29 +08:00
|
|
|
static int
|
|
|
|
ssh_rsa_generate(struct sshkey *k, int bits)
|
|
|
|
{
|
2024-08-15 08:51:51 +08:00
|
|
|
EVP_PKEY_CTX *ctx = NULL;
|
|
|
|
EVP_PKEY *res = NULL;
|
|
|
|
|
2022-10-28 08:39:29 +08:00
|
|
|
int ret = SSH_ERR_INTERNAL_ERROR;
|
|
|
|
|
|
|
|
if (bits < SSH_RSA_MINIMUM_MODULUS_SIZE ||
|
|
|
|
bits > SSHBUF_MAX_BIGNUM * 8)
|
|
|
|
return SSH_ERR_KEY_LENGTH;
|
2024-08-15 08:51:51 +08:00
|
|
|
|
|
|
|
if ((ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_RSA, NULL)) == NULL) {
|
2022-10-28 08:39:29 +08:00
|
|
|
ret = SSH_ERR_ALLOC_FAIL;
|
|
|
|
goto out;
|
|
|
|
}
|
2024-08-15 08:51:51 +08:00
|
|
|
if (EVP_PKEY_keygen_init(ctx) <= 0) {
|
|
|
|
ret = SSH_ERR_LIBCRYPTO_ERROR;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
if (EVP_PKEY_CTX_set_rsa_keygen_bits(ctx, bits) <= 0) {
|
|
|
|
ret = SSH_ERR_KEY_LENGTH;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
if (EVP_PKEY_keygen(ctx, &res) <= 0 || res == NULL) {
|
2022-10-28 08:39:29 +08:00
|
|
|
ret = SSH_ERR_LIBCRYPTO_ERROR;
|
|
|
|
goto out;
|
|
|
|
}
|
2024-08-15 08:51:51 +08:00
|
|
|
/* success */
|
|
|
|
k->pkey = res;
|
2022-10-28 08:39:29 +08:00
|
|
|
ret = 0;
|
|
|
|
out:
|
2024-08-15 08:51:51 +08:00
|
|
|
EVP_PKEY_CTX_free(ctx);
|
2022-10-28 08:39:29 +08:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2022-10-28 08:41:17 +08:00
|
|
|
static int
|
|
|
|
ssh_rsa_copy_public(const struct sshkey *from, struct sshkey *to)
|
|
|
|
{
|
|
|
|
const BIGNUM *rsa_n, *rsa_e;
|
|
|
|
BIGNUM *rsa_n_dup = NULL, *rsa_e_dup = NULL;
|
|
|
|
int r = SSH_ERR_INTERNAL_ERROR;
|
2024-08-15 08:51:51 +08:00
|
|
|
const RSA *rsa_from;
|
|
|
|
RSA *rsa_to = NULL;
|
|
|
|
|
|
|
|
if ((rsa_from = EVP_PKEY_get0_RSA(from->pkey)) == NULL ||
|
|
|
|
(rsa_to = RSA_new()) == NULL)
|
|
|
|
return SSH_ERR_LIBCRYPTO_ERROR;
|
2022-10-28 08:41:17 +08:00
|
|
|
|
2024-08-15 08:51:51 +08:00
|
|
|
RSA_get0_key(rsa_from, &rsa_n, &rsa_e, NULL);
|
2022-10-28 08:41:17 +08:00
|
|
|
if ((rsa_n_dup = BN_dup(rsa_n)) == NULL ||
|
|
|
|
(rsa_e_dup = BN_dup(rsa_e)) == NULL) {
|
|
|
|
r = SSH_ERR_ALLOC_FAIL;
|
|
|
|
goto out;
|
|
|
|
}
|
2024-08-15 08:51:51 +08:00
|
|
|
if (!RSA_set0_key(rsa_to, rsa_n_dup, rsa_e_dup, NULL)) {
|
2022-10-28 08:41:17 +08:00
|
|
|
r = SSH_ERR_LIBCRYPTO_ERROR;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
rsa_n_dup = rsa_e_dup = NULL; /* transferred */
|
2024-08-15 08:51:51 +08:00
|
|
|
|
|
|
|
if (EVP_PKEY_set1_RSA(to->pkey, rsa_to) != 1) {
|
|
|
|
r = SSH_ERR_LIBCRYPTO_ERROR;
|
|
|
|
goto out;
|
|
|
|
}
|
2022-10-28 08:41:17 +08:00
|
|
|
/* success */
|
|
|
|
r = 0;
|
|
|
|
out:
|
2024-08-15 08:51:51 +08:00
|
|
|
RSA_free(rsa_to);
|
2022-10-28 08:41:17 +08:00
|
|
|
BN_clear_free(rsa_n_dup);
|
|
|
|
BN_clear_free(rsa_e_dup);
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2022-10-28 08:41:52 +08:00
|
|
|
static int
|
|
|
|
ssh_rsa_deserialize_public(const char *ktype, struct sshbuf *b,
|
|
|
|
struct sshkey *key)
|
|
|
|
{
|
|
|
|
int ret = SSH_ERR_INTERNAL_ERROR;
|
|
|
|
BIGNUM *rsa_n = NULL, *rsa_e = NULL;
|
2024-08-15 08:51:51 +08:00
|
|
|
RSA *rsa = NULL;
|
|
|
|
|
|
|
|
if ((rsa = RSA_new()) == NULL)
|
|
|
|
return SSH_ERR_LIBCRYPTO_ERROR;
|
2022-10-28 08:41:52 +08:00
|
|
|
|
|
|
|
if (sshbuf_get_bignum2(b, &rsa_e) != 0 ||
|
|
|
|
sshbuf_get_bignum2(b, &rsa_n) != 0) {
|
|
|
|
ret = SSH_ERR_INVALID_FORMAT;
|
|
|
|
goto out;
|
|
|
|
}
|
2024-08-15 08:51:51 +08:00
|
|
|
if (!RSA_set0_key(rsa, rsa_n, rsa_e, NULL)) {
|
2022-10-28 08:41:52 +08:00
|
|
|
ret = SSH_ERR_LIBCRYPTO_ERROR;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
rsa_n = rsa_e = NULL; /* transferred */
|
2024-08-15 08:51:51 +08:00
|
|
|
if (EVP_PKEY_set1_RSA(key->pkey, rsa) != 1) {
|
|
|
|
ret = SSH_ERR_LIBCRYPTO_ERROR;
|
|
|
|
goto out;
|
|
|
|
}
|
2022-10-28 08:41:52 +08:00
|
|
|
if ((ret = sshkey_check_rsa_length(key, 0)) != 0)
|
|
|
|
goto out;
|
|
|
|
#ifdef DEBUG_PK
|
2024-08-15 08:51:51 +08:00
|
|
|
RSA_print_fp(stderr, rsa, 8);
|
2022-10-28 08:41:52 +08:00
|
|
|
#endif
|
|
|
|
/* success */
|
|
|
|
ret = 0;
|
|
|
|
out:
|
2024-08-15 08:51:51 +08:00
|
|
|
RSA_free(rsa);
|
2022-10-28 08:41:52 +08:00
|
|
|
BN_clear_free(rsa_n);
|
|
|
|
BN_clear_free(rsa_e);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2022-10-28 08:44:44 +08:00
|
|
|
static int
|
|
|
|
ssh_rsa_deserialize_private(const char *ktype, struct sshbuf *b,
|
|
|
|
struct sshkey *key)
|
|
|
|
{
|
|
|
|
int r;
|
|
|
|
BIGNUM *rsa_n = NULL, *rsa_e = NULL, *rsa_d = NULL;
|
|
|
|
BIGNUM *rsa_iqmp = NULL, *rsa_p = NULL, *rsa_q = NULL;
|
2024-08-15 08:51:51 +08:00
|
|
|
BIGNUM *rsa_dmp1 = NULL, *rsa_dmq1 = NULL;
|
|
|
|
RSA *rsa = NULL;
|
2022-10-28 08:44:44 +08:00
|
|
|
|
2024-08-15 08:51:51 +08:00
|
|
|
if (sshkey_is_cert(key)) {
|
|
|
|
/* sshkey_private_deserialize already has pubkey from cert */
|
|
|
|
if ((rsa = EVP_PKEY_get1_RSA(key->pkey)) == NULL) {
|
|
|
|
r = SSH_ERR_LIBCRYPTO_ERROR;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if ((rsa = RSA_new()) == NULL) {
|
|
|
|
r = SSH_ERR_LIBCRYPTO_ERROR;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
/* Note: can't reuse ssh_rsa_deserialize_public: e,n vs. n,e */
|
2022-10-28 08:44:44 +08:00
|
|
|
if ((r = sshbuf_get_bignum2(b, &rsa_n)) != 0 ||
|
|
|
|
(r = sshbuf_get_bignum2(b, &rsa_e)) != 0)
|
|
|
|
goto out;
|
2024-08-15 08:51:51 +08:00
|
|
|
if (!RSA_set0_key(rsa, rsa_n, rsa_e, NULL)) {
|
2022-10-28 08:44:44 +08:00
|
|
|
r = SSH_ERR_LIBCRYPTO_ERROR;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
rsa_n = rsa_e = NULL; /* transferred */
|
|
|
|
}
|
|
|
|
if ((r = sshbuf_get_bignum2(b, &rsa_d)) != 0 ||
|
|
|
|
(r = sshbuf_get_bignum2(b, &rsa_iqmp)) != 0 ||
|
|
|
|
(r = sshbuf_get_bignum2(b, &rsa_p)) != 0 ||
|
|
|
|
(r = sshbuf_get_bignum2(b, &rsa_q)) != 0)
|
|
|
|
goto out;
|
2024-08-15 08:51:51 +08:00
|
|
|
if ((r = ssh_rsa_complete_crt_parameters(rsa_d, rsa_p, rsa_q,
|
|
|
|
rsa_iqmp, &rsa_dmp1, &rsa_dmq1)) != 0)
|
|
|
|
goto out;
|
|
|
|
if (!RSA_set0_key(rsa, NULL, NULL, rsa_d)) {
|
2022-10-28 08:44:44 +08:00
|
|
|
r = SSH_ERR_LIBCRYPTO_ERROR;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
rsa_d = NULL; /* transferred */
|
2024-08-15 08:51:51 +08:00
|
|
|
if (!RSA_set0_factors(rsa, rsa_p, rsa_q)) {
|
2022-10-28 08:44:44 +08:00
|
|
|
r = SSH_ERR_LIBCRYPTO_ERROR;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
rsa_p = rsa_q = NULL; /* transferred */
|
2024-08-15 08:51:51 +08:00
|
|
|
if (!RSA_set0_crt_params(rsa, rsa_dmp1, rsa_dmq1, rsa_iqmp)) {
|
|
|
|
r = SSH_ERR_LIBCRYPTO_ERROR;
|
2022-10-28 08:44:44 +08:00
|
|
|
goto out;
|
2024-08-15 08:51:51 +08:00
|
|
|
}
|
|
|
|
rsa_dmp1 = rsa_dmq1 = rsa_iqmp = NULL;
|
|
|
|
if (RSA_blinding_on(rsa, NULL) != 1) {
|
|
|
|
r = SSH_ERR_LIBCRYPTO_ERROR;
|
2022-10-28 08:44:44 +08:00
|
|
|
goto out;
|
2024-08-15 08:51:51 +08:00
|
|
|
}
|
|
|
|
if (EVP_PKEY_set1_RSA(key->pkey, rsa) != 1) {
|
2022-10-28 08:44:44 +08:00
|
|
|
r = SSH_ERR_LIBCRYPTO_ERROR;
|
|
|
|
goto out;
|
|
|
|
}
|
2024-08-15 08:51:51 +08:00
|
|
|
if ((r = sshkey_check_rsa_length(key, 0)) != 0)
|
|
|
|
goto out;
|
2022-10-28 08:44:44 +08:00
|
|
|
/* success */
|
|
|
|
r = 0;
|
|
|
|
out:
|
2024-08-15 08:51:51 +08:00
|
|
|
RSA_free(rsa);
|
2022-10-28 08:44:44 +08:00
|
|
|
BN_clear_free(rsa_n);
|
|
|
|
BN_clear_free(rsa_e);
|
|
|
|
BN_clear_free(rsa_d);
|
|
|
|
BN_clear_free(rsa_p);
|
|
|
|
BN_clear_free(rsa_q);
|
|
|
|
BN_clear_free(rsa_iqmp);
|
2024-08-15 08:51:51 +08:00
|
|
|
BN_clear_free(rsa_dmp1);
|
|
|
|
BN_clear_free(rsa_dmq1);
|
2022-10-28 08:44:44 +08:00
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2015-12-05 00:41:28 +08:00
|
|
|
static const char *
|
|
|
|
rsa_hash_alg_ident(int hash_alg)
|
|
|
|
{
|
|
|
|
switch (hash_alg) {
|
|
|
|
case SSH_DIGEST_SHA1:
|
|
|
|
return "ssh-rsa";
|
|
|
|
case SSH_DIGEST_SHA256:
|
|
|
|
return "rsa-sha2-256";
|
|
|
|
case SSH_DIGEST_SHA512:
|
|
|
|
return "rsa-sha2-512";
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2018-07-03 19:39:54 +08:00
|
|
|
/*
|
|
|
|
* Returns the hash algorithm ID for a given algorithm identifier as used
|
|
|
|
* inside the signature blob,
|
|
|
|
*/
|
2015-12-05 00:41:28 +08:00
|
|
|
static int
|
2018-07-03 19:39:54 +08:00
|
|
|
rsa_hash_id_from_ident(const char *ident)
|
2015-12-05 00:41:28 +08:00
|
|
|
{
|
2018-07-03 19:39:54 +08:00
|
|
|
if (strcmp(ident, "ssh-rsa") == 0)
|
2015-12-05 00:41:28 +08:00
|
|
|
return SSH_DIGEST_SHA1;
|
|
|
|
if (strcmp(ident, "rsa-sha2-256") == 0)
|
|
|
|
return SSH_DIGEST_SHA256;
|
|
|
|
if (strcmp(ident, "rsa-sha2-512") == 0)
|
|
|
|
return SSH_DIGEST_SHA512;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2018-07-03 19:39:54 +08:00
|
|
|
/*
|
|
|
|
* Return the hash algorithm ID for the specified key name. This includes
|
|
|
|
* all the cases of rsa_hash_id_from_ident() but also the certificate key
|
|
|
|
* types.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
rsa_hash_id_from_keyname(const char *alg)
|
|
|
|
{
|
|
|
|
int r;
|
|
|
|
|
|
|
|
if ((r = rsa_hash_id_from_ident(alg)) != -1)
|
|
|
|
return r;
|
|
|
|
if (strcmp(alg, "ssh-rsa-cert-v01@openssh.com") == 0)
|
|
|
|
return SSH_DIGEST_SHA1;
|
|
|
|
if (strcmp(alg, "rsa-sha2-256-cert-v01@openssh.com") == 0)
|
|
|
|
return SSH_DIGEST_SHA256;
|
|
|
|
if (strcmp(alg, "rsa-sha2-512-cert-v01@openssh.com") == 0)
|
|
|
|
return SSH_DIGEST_SHA512;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2017-07-01 21:50:45 +08:00
|
|
|
int
|
2024-08-15 08:51:51 +08:00
|
|
|
ssh_rsa_complete_crt_parameters(const BIGNUM *rsa_d, const BIGNUM *rsa_p,
|
|
|
|
const BIGNUM *rsa_q, const BIGNUM *rsa_iqmp, BIGNUM **rsa_dmp1,
|
|
|
|
BIGNUM **rsa_dmq1)
|
2017-07-01 21:50:45 +08:00
|
|
|
{
|
2018-09-13 10:08:33 +08:00
|
|
|
BIGNUM *aux = NULL, *d_consttime = NULL;
|
2017-07-01 21:50:45 +08:00
|
|
|
BN_CTX *ctx = NULL;
|
|
|
|
int r;
|
|
|
|
|
2024-08-15 08:51:51 +08:00
|
|
|
*rsa_dmq1 = *rsa_dmp1 = NULL;
|
2017-07-01 21:50:45 +08:00
|
|
|
if ((ctx = BN_CTX_new()) == NULL)
|
|
|
|
return SSH_ERR_ALLOC_FAIL;
|
2018-09-13 10:08:33 +08:00
|
|
|
if ((aux = BN_new()) == NULL ||
|
2024-08-15 08:51:51 +08:00
|
|
|
(*rsa_dmq1 = BN_new()) == NULL ||
|
|
|
|
(*rsa_dmp1 = BN_new()) == NULL)
|
2018-09-13 10:08:33 +08:00
|
|
|
return SSH_ERR_ALLOC_FAIL;
|
2024-08-15 08:51:51 +08:00
|
|
|
if ((d_consttime = BN_dup(rsa_d)) == NULL) {
|
2017-07-01 21:50:45 +08:00
|
|
|
r = SSH_ERR_ALLOC_FAIL;
|
|
|
|
goto out;
|
|
|
|
}
|
2018-02-15 00:27:24 +08:00
|
|
|
BN_set_flags(aux, BN_FLG_CONSTTIME);
|
2018-09-13 10:08:33 +08:00
|
|
|
BN_set_flags(d_consttime, BN_FLG_CONSTTIME);
|
2017-07-01 21:50:45 +08:00
|
|
|
|
2018-09-13 10:08:33 +08:00
|
|
|
if ((BN_sub(aux, rsa_q, BN_value_one()) == 0) ||
|
2024-08-15 08:51:51 +08:00
|
|
|
(BN_mod(*rsa_dmq1, d_consttime, aux, ctx) == 0) ||
|
2018-09-13 10:08:33 +08:00
|
|
|
(BN_sub(aux, rsa_p, BN_value_one()) == 0) ||
|
2024-08-15 08:51:51 +08:00
|
|
|
(BN_mod(*rsa_dmp1, d_consttime, aux, ctx) == 0)) {
|
2017-07-01 21:50:45 +08:00
|
|
|
r = SSH_ERR_LIBCRYPTO_ERROR;
|
|
|
|
goto out;
|
|
|
|
}
|
2018-09-13 10:08:33 +08:00
|
|
|
/* success */
|
2017-07-01 21:50:45 +08:00
|
|
|
r = 0;
|
|
|
|
out:
|
|
|
|
BN_clear_free(aux);
|
2018-09-13 10:08:33 +08:00
|
|
|
BN_clear_free(d_consttime);
|
2017-07-01 21:50:45 +08:00
|
|
|
BN_CTX_free(ctx);
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2000-11-13 19:57:25 +08:00
|
|
|
/* RSASSA-PKCS1-v1_5 (PKCS #1 v2.0 signature) with SHA1 */
|
2022-10-28 08:43:08 +08:00
|
|
|
static int
|
|
|
|
ssh_rsa_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)
|
2000-11-13 19:57:25 +08:00
|
|
|
{
|
2024-08-15 08:51:51 +08:00
|
|
|
u_char *sig = NULL;
|
|
|
|
size_t diff, len = 0;
|
|
|
|
int slen = 0;
|
|
|
|
int hash_alg, ret = SSH_ERR_INTERNAL_ERROR;
|
2014-07-02 13:28:02 +08:00
|
|
|
struct sshbuf *b = NULL;
|
2000-11-13 19:57:25 +08:00
|
|
|
|
2014-07-02 13:28:02 +08:00
|
|
|
if (lenp != NULL)
|
|
|
|
*lenp = 0;
|
|
|
|
if (sigp != NULL)
|
|
|
|
*sigp = NULL;
|
|
|
|
|
2022-10-28 08:43:08 +08:00
|
|
|
if (alg == NULL || strlen(alg) == 0)
|
2015-12-08 04:04:09 +08:00
|
|
|
hash_alg = SSH_DIGEST_SHA1;
|
|
|
|
else
|
2022-10-28 08:43:08 +08:00
|
|
|
hash_alg = rsa_hash_id_from_keyname(alg);
|
2024-08-15 08:51:51 +08:00
|
|
|
|
|
|
|
if (key == NULL || key->pkey == NULL || hash_alg == -1 ||
|
2017-05-08 07:15:59 +08:00
|
|
|
sshkey_type_plain(key->type) != KEY_RSA)
|
2014-07-02 13:28:02 +08:00
|
|
|
return SSH_ERR_INVALID_ARGUMENT;
|
2024-08-15 08:51:51 +08:00
|
|
|
slen = EVP_PKEY_size(key->pkey);
|
2014-07-02 13:28:02 +08:00
|
|
|
if (slen <= 0 || slen > SSHBUF_MAX_BIGNUM)
|
|
|
|
return SSH_ERR_INVALID_ARGUMENT;
|
2024-08-15 08:51:51 +08:00
|
|
|
if (EVP_PKEY_bits(key->pkey) < SSH_RSA_MINIMUM_MODULUS_SIZE)
|
|
|
|
return SSH_ERR_KEY_LENGTH;
|
2013-12-29 14:47:50 +08:00
|
|
|
|
2024-08-15 08:51:51 +08:00
|
|
|
if ((ret = sshkey_pkey_digest_sign(key->pkey, hash_alg, &sig, &len,
|
|
|
|
data, datalen)) < 0)
|
2014-07-02 13:28:02 +08:00
|
|
|
goto out;
|
2024-08-15 08:51:51 +08:00
|
|
|
if (len < (size_t)slen) {
|
|
|
|
diff = slen - len;
|
2000-11-13 19:57:25 +08:00
|
|
|
memmove(sig + diff, sig, len);
|
2014-02-04 08:20:14 +08:00
|
|
|
explicit_bzero(sig, diff);
|
2024-08-15 08:51:51 +08:00
|
|
|
} else if (len > (size_t)slen) {
|
2014-07-02 13:28:02 +08:00
|
|
|
ret = SSH_ERR_INTERNAL_ERROR;
|
|
|
|
goto out;
|
2000-11-13 19:57:25 +08:00
|
|
|
}
|
2024-08-15 08:51:51 +08:00
|
|
|
|
2000-11-13 19:57:25 +08:00
|
|
|
/* encode signature */
|
2014-07-02 13:28:02 +08:00
|
|
|
if ((b = sshbuf_new()) == NULL) {
|
|
|
|
ret = SSH_ERR_ALLOC_FAIL;
|
|
|
|
goto out;
|
|
|
|
}
|
2015-12-05 00:41:28 +08:00
|
|
|
if ((ret = sshbuf_put_cstring(b, rsa_hash_alg_ident(hash_alg))) != 0 ||
|
2014-07-02 13:28:02 +08:00
|
|
|
(ret = sshbuf_put_string(b, sig, slen)) != 0)
|
|
|
|
goto out;
|
|
|
|
len = sshbuf_len(b);
|
|
|
|
if (sigp != NULL) {
|
|
|
|
if ((*sigp = malloc(len)) == NULL) {
|
|
|
|
ret = SSH_ERR_ALLOC_FAIL;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
memcpy(*sigp, sshbuf_ptr(b), len);
|
|
|
|
}
|
2002-07-08 06:13:31 +08:00
|
|
|
if (lenp != NULL)
|
|
|
|
*lenp = len;
|
2014-07-02 13:28:02 +08:00
|
|
|
ret = 0;
|
|
|
|
out:
|
2018-02-07 13:17:56 +08:00
|
|
|
freezero(sig, slen);
|
2015-12-11 12:21:11 +08:00
|
|
|
sshbuf_free(b);
|
2015-06-15 09:32:50 +08:00
|
|
|
return ret;
|
2000-11-13 19:57:25 +08:00
|
|
|
}
|
|
|
|
|
2022-10-28 08:43:08 +08:00
|
|
|
static int
|
2014-07-02 13:28:02 +08:00
|
|
|
ssh_rsa_verify(const struct sshkey *key,
|
2022-10-28 08:43:08 +08: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)
|
2000-11-13 19:57:25 +08:00
|
|
|
{
|
2017-12-18 10:25:15 +08:00
|
|
|
char *sigtype = NULL;
|
2018-07-03 19:39:54 +08:00
|
|
|
int hash_alg, want_alg, ret = SSH_ERR_INTERNAL_ERROR;
|
2024-08-15 08:51:51 +08:00
|
|
|
size_t len = 0, diff, modlen, rsasize;
|
2014-07-02 13:28:02 +08:00
|
|
|
struct sshbuf *b = NULL;
|
|
|
|
u_char digest[SSH_DIGEST_MAX_LENGTH], *osigblob, *sigblob = NULL;
|
2000-11-13 19:57:25 +08:00
|
|
|
|
2024-08-15 08:51:51 +08:00
|
|
|
if (key == NULL || key->pkey == NULL ||
|
2014-07-02 13:28:02 +08:00
|
|
|
sshkey_type_plain(key->type) != KEY_RSA ||
|
2016-04-21 14:08:02 +08:00
|
|
|
sig == NULL || siglen == 0)
|
2014-07-02 13:28:02 +08:00
|
|
|
return SSH_ERR_INVALID_ARGUMENT;
|
2024-08-15 08:51:51 +08:00
|
|
|
if (EVP_PKEY_bits(key->pkey) < SSH_RSA_MINIMUM_MODULUS_SIZE)
|
2017-05-08 07:15:59 +08:00
|
|
|
return SSH_ERR_KEY_LENGTH;
|
2013-12-29 14:47:50 +08:00
|
|
|
|
2015-12-05 00:41:28 +08:00
|
|
|
if ((b = sshbuf_from(sig, siglen)) == NULL)
|
2014-07-02 13:28:02 +08:00
|
|
|
return SSH_ERR_ALLOC_FAIL;
|
2017-12-18 10:25:15 +08:00
|
|
|
if (sshbuf_get_cstring(b, &sigtype, NULL) != 0) {
|
2014-07-02 13:28:02 +08:00
|
|
|
ret = SSH_ERR_INVALID_FORMAT;
|
|
|
|
goto out;
|
2001-01-18 10:04:35 +08:00
|
|
|
}
|
2018-07-03 19:39:54 +08:00
|
|
|
if ((hash_alg = rsa_hash_id_from_ident(sigtype)) == -1) {
|
2014-07-02 13:28:02 +08:00
|
|
|
ret = SSH_ERR_KEY_TYPE_MISMATCH;
|
|
|
|
goto out;
|
2000-11-13 19:57:25 +08:00
|
|
|
}
|
2018-07-03 19:39:54 +08:00
|
|
|
/*
|
|
|
|
* Allow ssh-rsa-cert-v01 certs to generate SHA2 signatures for
|
|
|
|
* legacy reasons, but otherwise the signature type should match.
|
|
|
|
*/
|
|
|
|
if (alg != NULL && strcmp(alg, "ssh-rsa-cert-v01@openssh.com") != 0) {
|
|
|
|
if ((want_alg = rsa_hash_id_from_keyname(alg)) == -1) {
|
|
|
|
ret = SSH_ERR_INVALID_ARGUMENT;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
if (hash_alg != want_alg) {
|
|
|
|
ret = SSH_ERR_SIGNATURE_INVALID;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
}
|
2014-07-02 13:28:02 +08:00
|
|
|
if (sshbuf_get_string(b, &sigblob, &len) != 0) {
|
|
|
|
ret = SSH_ERR_INVALID_FORMAT;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
if (sshbuf_len(b) != 0) {
|
|
|
|
ret = SSH_ERR_UNEXPECTED_TRAILING_DATA;
|
|
|
|
goto out;
|
2000-11-13 19:57:25 +08:00
|
|
|
}
|
2002-06-07 04:55:04 +08:00
|
|
|
/* RSA_verify expects a signature of RSA_size */
|
2024-08-15 08:51:51 +08:00
|
|
|
modlen = EVP_PKEY_size(key->pkey);
|
2002-06-07 04:55:04 +08:00
|
|
|
if (len > modlen) {
|
2014-07-02 13:28:02 +08:00
|
|
|
ret = SSH_ERR_KEY_BITS_MISMATCH;
|
|
|
|
goto out;
|
2002-06-07 04:55:04 +08:00
|
|
|
} else if (len < modlen) {
|
2014-07-02 13:28:02 +08:00
|
|
|
diff = modlen - len;
|
|
|
|
osigblob = sigblob;
|
|
|
|
if ((sigblob = realloc(sigblob, modlen)) == NULL) {
|
|
|
|
sigblob = osigblob; /* put it back for clear/free */
|
|
|
|
ret = SSH_ERR_ALLOC_FAIL;
|
|
|
|
goto out;
|
|
|
|
}
|
2002-06-07 04:55:04 +08:00
|
|
|
memmove(sigblob + diff, sigblob, len);
|
2014-02-04 08:20:14 +08:00
|
|
|
explicit_bzero(sigblob, diff);
|
2002-06-07 04:55:04 +08:00
|
|
|
len = modlen;
|
|
|
|
}
|
2024-08-15 08:51:51 +08:00
|
|
|
|
|
|
|
rsasize = EVP_PKEY_size(key->pkey);
|
|
|
|
if (rsasize <= 0 || rsasize > SSHBUF_MAX_BIGNUM ||
|
|
|
|
len == 0 || len > rsasize) {
|
|
|
|
ret = SSH_ERR_INVALID_ARGUMENT;
|
2014-07-02 13:28:02 +08:00
|
|
|
goto out;
|
2001-03-29 08:31:20 +08:00
|
|
|
}
|
2024-08-15 08:51:51 +08:00
|
|
|
ret = sshkey_pkey_digest_verify(key->pkey, hash_alg, data, dlen,
|
|
|
|
sigblob, len);
|
2000-11-13 19:57:25 +08:00
|
|
|
|
2014-07-02 13:28:02 +08:00
|
|
|
out:
|
2018-02-07 13:17:56 +08:00
|
|
|
freezero(sigblob, len);
|
2017-12-18 10:25:15 +08:00
|
|
|
free(sigtype);
|
2015-12-11 12:21:11 +08:00
|
|
|
sshbuf_free(b);
|
2014-02-04 08:20:14 +08:00
|
|
|
explicit_bzero(digest, sizeof(digest));
|
2000-11-13 19:57:25 +08:00
|
|
|
return ret;
|
|
|
|
}
|
2002-08-21 02:40:03 +08:00
|
|
|
|
2022-10-28 08:35:40 +08:00
|
|
|
static const struct sshkey_impl_funcs sshkey_rsa_funcs = {
|
|
|
|
/* .size = */ ssh_rsa_size,
|
|
|
|
/* .alloc = */ ssh_rsa_alloc,
|
|
|
|
/* .cleanup = */ ssh_rsa_cleanup,
|
2022-10-28 08:36:31 +08:00
|
|
|
/* .equal = */ ssh_rsa_equal,
|
2022-10-28 08:37:24 +08:00
|
|
|
/* .ssh_serialize_public = */ ssh_rsa_serialize_public,
|
2022-10-28 08:41:52 +08:00
|
|
|
/* .ssh_deserialize_public = */ ssh_rsa_deserialize_public,
|
2022-10-28 08:44:17 +08:00
|
|
|
/* .ssh_serialize_private = */ ssh_rsa_serialize_private,
|
2022-10-28 08:44:44 +08:00
|
|
|
/* .ssh_deserialize_private = */ ssh_rsa_deserialize_private,
|
2022-10-28 08:39:29 +08:00
|
|
|
/* .generate = */ ssh_rsa_generate,
|
2022-10-28 08:41:17 +08:00
|
|
|
/* .copy_public = */ ssh_rsa_copy_public,
|
2022-10-28 08:43:08 +08:00
|
|
|
/* .sign = */ ssh_rsa_sign,
|
|
|
|
/* .verify = */ ssh_rsa_verify,
|
2022-10-28 08:35:40 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
const struct sshkey_impl sshkey_rsa_impl = {
|
|
|
|
/* .name = */ "ssh-rsa",
|
|
|
|
/* .shortname = */ "RSA",
|
|
|
|
/* .sigalg = */ NULL,
|
|
|
|
/* .type = */ KEY_RSA,
|
|
|
|
/* .nid = */ 0,
|
|
|
|
/* .cert = */ 0,
|
|
|
|
/* .sigonly = */ 0,
|
|
|
|
/* .keybits = */ 0,
|
|
|
|
/* .funcs = */ &sshkey_rsa_funcs,
|
|
|
|
};
|
|
|
|
|
|
|
|
const struct sshkey_impl sshkey_rsa_cert_impl = {
|
|
|
|
/* .name = */ "ssh-rsa-cert-v01@openssh.com",
|
|
|
|
/* .shortname = */ "RSA-CERT",
|
|
|
|
/* .sigalg = */ NULL,
|
|
|
|
/* .type = */ KEY_RSA_CERT,
|
|
|
|
/* .nid = */ 0,
|
|
|
|
/* .cert = */ 1,
|
|
|
|
/* .sigonly = */ 0,
|
|
|
|
/* .keybits = */ 0,
|
|
|
|
/* .funcs = */ &sshkey_rsa_funcs,
|
|
|
|
};
|
|
|
|
|
|
|
|
/* SHA2 signature algorithms */
|
|
|
|
|
|
|
|
const struct sshkey_impl sshkey_rsa_sha256_impl = {
|
|
|
|
/* .name = */ "rsa-sha2-256",
|
|
|
|
/* .shortname = */ "RSA",
|
|
|
|
/* .sigalg = */ NULL,
|
|
|
|
/* .type = */ KEY_RSA,
|
|
|
|
/* .nid = */ 0,
|
|
|
|
/* .cert = */ 0,
|
|
|
|
/* .sigonly = */ 1,
|
|
|
|
/* .keybits = */ 0,
|
|
|
|
/* .funcs = */ &sshkey_rsa_funcs,
|
|
|
|
};
|
|
|
|
|
|
|
|
const struct sshkey_impl sshkey_rsa_sha512_impl = {
|
|
|
|
/* .name = */ "rsa-sha2-512",
|
|
|
|
/* .shortname = */ "RSA",
|
|
|
|
/* .sigalg = */ NULL,
|
|
|
|
/* .type = */ KEY_RSA,
|
|
|
|
/* .nid = */ 0,
|
|
|
|
/* .cert = */ 0,
|
|
|
|
/* .sigonly = */ 1,
|
|
|
|
/* .keybits = */ 0,
|
|
|
|
/* .funcs = */ &sshkey_rsa_funcs,
|
|
|
|
};
|
|
|
|
|
|
|
|
const struct sshkey_impl sshkey_rsa_sha256_cert_impl = {
|
|
|
|
/* .name = */ "rsa-sha2-256-cert-v01@openssh.com",
|
|
|
|
/* .shortname = */ "RSA-CERT",
|
|
|
|
/* .sigalg = */ "rsa-sha2-256",
|
|
|
|
/* .type = */ KEY_RSA_CERT,
|
|
|
|
/* .nid = */ 0,
|
|
|
|
/* .cert = */ 1,
|
|
|
|
/* .sigonly = */ 1,
|
|
|
|
/* .keybits = */ 0,
|
|
|
|
/* .funcs = */ &sshkey_rsa_funcs,
|
|
|
|
};
|
|
|
|
|
|
|
|
const struct sshkey_impl sshkey_rsa_sha512_cert_impl = {
|
|
|
|
/* .name = */ "rsa-sha2-512-cert-v01@openssh.com",
|
|
|
|
/* .shortname = */ "RSA-CERT",
|
|
|
|
/* .sigalg = */ "rsa-sha2-512",
|
|
|
|
/* .type = */ KEY_RSA_CERT,
|
|
|
|
/* .nid = */ 0,
|
|
|
|
/* .cert = */ 1,
|
|
|
|
/* .sigonly = */ 1,
|
|
|
|
/* .keybits = */ 0,
|
|
|
|
/* .funcs = */ &sshkey_rsa_funcs,
|
|
|
|
};
|
2015-01-14 23:21:31 +08:00
|
|
|
#endif /* WITH_OPENSSL */
|