2018-08-28 10:47:58 +00:00
|
|
|
/* $OpenBSD: ssh-rsa.c,v 1.67 2018/07/03 11:39:54 djm Exp $ */
|
2008-07-22 18:58:19 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2000, 2003 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "includes.h"
|
|
|
|
|
2015-07-02 13:15:34 +00:00
|
|
|
#ifdef WITH_OPENSSL
|
|
|
|
|
2008-07-22 18:58:19 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
|
|
|
|
#include <openssl/evp.h>
|
|
|
|
#include <openssl/err.h>
|
|
|
|
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2015-01-05 16:09:55 +00:00
|
|
|
#include "sshbuf.h"
|
2008-07-22 18:58:19 +00:00
|
|
|
#include "compat.h"
|
2015-01-05 16:09:55 +00:00
|
|
|
#include "ssherr.h"
|
|
|
|
#define SSHKEY_INTERNAL
|
|
|
|
#include "sshkey.h"
|
2014-01-30 10:56:49 +00:00
|
|
|
#include "digest.h"
|
2018-05-06 12:27:04 +00:00
|
|
|
#include "log.h"
|
2008-07-22 18:58:19 +00:00
|
|
|
|
2018-10-03 16:11:13 +00:00
|
|
|
#include "openbsd-compat/openssl-compat.h"
|
|
|
|
|
2015-01-05 16:09:55 +00:00
|
|
|
static int openssh_RSA_verify(int, u_char *, size_t, u_char *, size_t, RSA *);
|
2008-07-22 18:58:19 +00:00
|
|
|
|
2016-03-10 20:10:25 +00: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-08-28 10:47:58 +00:00
|
|
|
/*
|
|
|
|
* Returns the hash algorithm ID for a given algorithm identifier as used
|
|
|
|
* inside the signature blob,
|
|
|
|
*/
|
2016-03-10 20:10:25 +00:00
|
|
|
static int
|
2018-08-28 10:47:58 +00:00
|
|
|
rsa_hash_id_from_ident(const char *ident)
|
2016-03-10 20:10:25 +00:00
|
|
|
{
|
2018-08-28 10:47:58 +00:00
|
|
|
if (strcmp(ident, "ssh-rsa") == 0)
|
2016-03-10 20:10:25 +00: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-08-28 10:47:58 +00: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;
|
|
|
|
}
|
|
|
|
|
2016-03-10 20:10:25 +00:00
|
|
|
static int
|
|
|
|
rsa_hash_alg_nid(int type)
|
|
|
|
{
|
|
|
|
switch (type) {
|
|
|
|
case SSH_DIGEST_SHA1:
|
|
|
|
return NID_sha1;
|
|
|
|
case SSH_DIGEST_SHA256:
|
|
|
|
return NID_sha256;
|
|
|
|
case SSH_DIGEST_SHA512:
|
|
|
|
return NID_sha512;
|
|
|
|
default:
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-06 12:24:45 +00:00
|
|
|
int
|
2018-10-03 16:11:13 +00:00
|
|
|
ssh_rsa_complete_crt_parameters(struct sshkey *key, const BIGNUM *iqmp)
|
2018-05-06 12:24:45 +00:00
|
|
|
{
|
2018-10-03 16:11:13 +00:00
|
|
|
const BIGNUM *rsa_p, *rsa_q, *rsa_d;
|
|
|
|
BIGNUM *aux = NULL, *d_consttime = NULL;
|
|
|
|
BIGNUM *rsa_dmq1 = NULL, *rsa_dmp1 = NULL, *rsa_iqmp = NULL;
|
2018-05-06 12:24:45 +00:00
|
|
|
BN_CTX *ctx = NULL;
|
|
|
|
int r;
|
|
|
|
|
|
|
|
if (key == NULL || key->rsa == NULL ||
|
|
|
|
sshkey_type_plain(key->type) != KEY_RSA)
|
|
|
|
return SSH_ERR_INVALID_ARGUMENT;
|
|
|
|
|
2018-10-03 16:11:13 +00:00
|
|
|
RSA_get0_key(key->rsa, NULL, NULL, &rsa_d);
|
|
|
|
RSA_get0_factors(key->rsa, &rsa_p, &rsa_q);
|
|
|
|
|
2018-05-06 12:24:45 +00:00
|
|
|
if ((ctx = BN_CTX_new()) == NULL)
|
|
|
|
return SSH_ERR_ALLOC_FAIL;
|
2018-10-03 16:11:13 +00:00
|
|
|
if ((aux = BN_new()) == NULL ||
|
|
|
|
(rsa_dmq1 = BN_new()) == NULL ||
|
|
|
|
(rsa_dmp1 = BN_new()) == NULL)
|
|
|
|
return SSH_ERR_ALLOC_FAIL;
|
|
|
|
if ((d_consttime = BN_dup(rsa_d)) == NULL ||
|
|
|
|
(rsa_iqmp = BN_dup(iqmp)) == NULL) {
|
2018-05-06 12:24:45 +00:00
|
|
|
r = SSH_ERR_ALLOC_FAIL;
|
|
|
|
goto out;
|
|
|
|
}
|
2018-05-06 12:27:04 +00:00
|
|
|
BN_set_flags(aux, BN_FLG_CONSTTIME);
|
2018-10-03 16:11:13 +00:00
|
|
|
BN_set_flags(d_consttime, BN_FLG_CONSTTIME);
|
2018-05-06 12:24:45 +00:00
|
|
|
|
2018-10-03 16:11:13 +00:00
|
|
|
if ((BN_sub(aux, rsa_q, BN_value_one()) == 0) ||
|
|
|
|
(BN_mod(rsa_dmq1, d_consttime, aux, ctx) == 0) ||
|
|
|
|
(BN_sub(aux, rsa_p, BN_value_one()) == 0) ||
|
|
|
|
(BN_mod(rsa_dmp1, d_consttime, aux, ctx) == 0)) {
|
|
|
|
r = SSH_ERR_LIBCRYPTO_ERROR;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
if (!RSA_set0_crt_params(key->rsa, rsa_dmp1, rsa_dmq1, rsa_iqmp)) {
|
2018-05-06 12:24:45 +00:00
|
|
|
r = SSH_ERR_LIBCRYPTO_ERROR;
|
|
|
|
goto out;
|
|
|
|
}
|
2018-10-03 16:11:13 +00:00
|
|
|
rsa_dmp1 = rsa_dmq1 = rsa_iqmp = NULL; /* transferred */
|
|
|
|
/* success */
|
2018-05-06 12:24:45 +00:00
|
|
|
r = 0;
|
|
|
|
out:
|
|
|
|
BN_clear_free(aux);
|
2018-10-03 16:11:13 +00:00
|
|
|
BN_clear_free(d_consttime);
|
|
|
|
BN_clear_free(rsa_dmp1);
|
|
|
|
BN_clear_free(rsa_dmq1);
|
|
|
|
BN_clear_free(rsa_iqmp);
|
2018-05-06 12:24:45 +00:00
|
|
|
BN_CTX_free(ctx);
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2008-07-22 18:58:19 +00:00
|
|
|
/* RSASSA-PKCS1-v1_5 (PKCS #1 v2.0 signature) with SHA1 */
|
|
|
|
int
|
2015-01-05 16:09:55 +00:00
|
|
|
ssh_rsa_sign(const struct sshkey *key, u_char **sigp, size_t *lenp,
|
2016-03-10 20:10:25 +00:00
|
|
|
const u_char *data, size_t datalen, const char *alg_ident)
|
2008-07-22 18:58:19 +00:00
|
|
|
{
|
2018-10-03 16:11:13 +00:00
|
|
|
const BIGNUM *rsa_n;
|
2015-01-05 16:09:55 +00:00
|
|
|
u_char digest[SSH_DIGEST_MAX_LENGTH], *sig = NULL;
|
2018-05-06 12:27:04 +00:00
|
|
|
size_t slen = 0;
|
2015-01-05 16:09:55 +00:00
|
|
|
u_int dlen, len;
|
2016-03-10 20:10:25 +00:00
|
|
|
int nid, hash_alg, ret = SSH_ERR_INTERNAL_ERROR;
|
2015-01-05 16:09:55 +00:00
|
|
|
struct sshbuf *b = NULL;
|
2008-07-22 18:58:19 +00:00
|
|
|
|
2015-01-05 16:09:55 +00:00
|
|
|
if (lenp != NULL)
|
|
|
|
*lenp = 0;
|
|
|
|
if (sigp != NULL)
|
|
|
|
*sigp = NULL;
|
|
|
|
|
2017-01-31 12:33:47 +00:00
|
|
|
if (alg_ident == NULL || strlen(alg_ident) == 0)
|
2016-03-10 20:10:25 +00:00
|
|
|
hash_alg = SSH_DIGEST_SHA1;
|
|
|
|
else
|
2018-08-28 10:47:58 +00:00
|
|
|
hash_alg = rsa_hash_id_from_keyname(alg_ident);
|
2016-03-10 20:10:25 +00:00
|
|
|
if (key == NULL || key->rsa == NULL || hash_alg == -1 ||
|
2018-05-06 12:24:45 +00:00
|
|
|
sshkey_type_plain(key->type) != KEY_RSA)
|
2015-01-05 16:09:55 +00:00
|
|
|
return SSH_ERR_INVALID_ARGUMENT;
|
2018-10-03 16:11:13 +00:00
|
|
|
RSA_get0_key(key->rsa, &rsa_n, NULL, NULL);
|
|
|
|
if (BN_num_bits(rsa_n) < SSH_RSA_MINIMUM_MODULUS_SIZE)
|
2018-05-06 12:24:45 +00:00
|
|
|
return SSH_ERR_KEY_LENGTH;
|
2015-01-05 16:09:55 +00:00
|
|
|
slen = RSA_size(key->rsa);
|
|
|
|
if (slen <= 0 || slen > SSHBUF_MAX_BIGNUM)
|
|
|
|
return SSH_ERR_INVALID_ARGUMENT;
|
2014-01-30 10:56:49 +00:00
|
|
|
|
|
|
|
/* hash the data */
|
2016-03-10 20:10:25 +00:00
|
|
|
nid = rsa_hash_alg_nid(hash_alg);
|
2015-01-05 16:09:55 +00:00
|
|
|
if ((dlen = ssh_digest_bytes(hash_alg)) == 0)
|
|
|
|
return SSH_ERR_INTERNAL_ERROR;
|
|
|
|
if ((ret = ssh_digest_memory(hash_alg, data, datalen,
|
|
|
|
digest, sizeof(digest))) != 0)
|
|
|
|
goto out;
|
2008-07-22 18:58:19 +00:00
|
|
|
|
2015-01-05 16:09:55 +00:00
|
|
|
if ((sig = malloc(slen)) == NULL) {
|
|
|
|
ret = SSH_ERR_ALLOC_FAIL;
|
|
|
|
goto out;
|
|
|
|
}
|
2008-07-22 18:58:19 +00:00
|
|
|
|
2015-01-05 16:09:55 +00:00
|
|
|
if (RSA_sign(nid, digest, dlen, sig, &len, key->rsa) != 1) {
|
|
|
|
ret = SSH_ERR_LIBCRYPTO_ERROR;
|
|
|
|
goto out;
|
2008-07-22 18:58:19 +00:00
|
|
|
}
|
|
|
|
if (len < slen) {
|
2015-01-05 16:09:55 +00:00
|
|
|
size_t diff = slen - len;
|
2008-07-22 18:58:19 +00:00
|
|
|
memmove(sig + diff, sig, len);
|
2014-03-22 15:23:38 +00:00
|
|
|
explicit_bzero(sig, diff);
|
2008-07-22 18:58:19 +00:00
|
|
|
} else if (len > slen) {
|
2015-01-05 16:09:55 +00:00
|
|
|
ret = SSH_ERR_INTERNAL_ERROR;
|
|
|
|
goto out;
|
2008-07-22 18:58:19 +00:00
|
|
|
}
|
|
|
|
/* encode signature */
|
2015-01-05 16:09:55 +00:00
|
|
|
if ((b = sshbuf_new()) == NULL) {
|
|
|
|
ret = SSH_ERR_ALLOC_FAIL;
|
|
|
|
goto out;
|
|
|
|
}
|
2016-03-10 20:10:25 +00:00
|
|
|
if ((ret = sshbuf_put_cstring(b, rsa_hash_alg_ident(hash_alg))) != 0 ||
|
2015-01-05 16:09:55 +00: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);
|
|
|
|
}
|
2008-07-22 18:58:19 +00:00
|
|
|
if (lenp != NULL)
|
|
|
|
*lenp = len;
|
2015-01-05 16:09:55 +00:00
|
|
|
ret = 0;
|
|
|
|
out:
|
|
|
|
explicit_bzero(digest, sizeof(digest));
|
2018-05-06 12:27:04 +00:00
|
|
|
freezero(sig, slen);
|
2016-03-10 20:10:25 +00:00
|
|
|
sshbuf_free(b);
|
2015-07-02 13:18:50 +00:00
|
|
|
return ret;
|
2008-07-22 18:58:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2015-01-05 16:09:55 +00:00
|
|
|
ssh_rsa_verify(const struct sshkey *key,
|
2018-05-06 12:27:04 +00:00
|
|
|
const u_char *sig, size_t siglen, const u_char *data, size_t datalen,
|
|
|
|
const char *alg)
|
2008-07-22 18:58:19 +00:00
|
|
|
{
|
2018-10-03 16:11:13 +00:00
|
|
|
const BIGNUM *rsa_n;
|
2018-05-06 12:27:04 +00:00
|
|
|
char *sigtype = NULL;
|
2018-08-28 10:47:58 +00:00
|
|
|
int hash_alg, want_alg, ret = SSH_ERR_INTERNAL_ERROR;
|
2018-05-06 12:27:04 +00:00
|
|
|
size_t len = 0, diff, modlen, dlen;
|
2015-01-05 16:09:55 +00:00
|
|
|
struct sshbuf *b = NULL;
|
|
|
|
u_char digest[SSH_DIGEST_MAX_LENGTH], *osigblob, *sigblob = NULL;
|
2008-07-22 18:58:19 +00:00
|
|
|
|
2015-01-05 16:09:55 +00:00
|
|
|
if (key == NULL || key->rsa == NULL ||
|
|
|
|
sshkey_type_plain(key->type) != KEY_RSA ||
|
2017-01-31 12:29:48 +00:00
|
|
|
sig == NULL || siglen == 0)
|
2015-01-05 16:09:55 +00:00
|
|
|
return SSH_ERR_INVALID_ARGUMENT;
|
2018-10-03 16:11:13 +00:00
|
|
|
RSA_get0_key(key->rsa, &rsa_n, NULL, NULL);
|
|
|
|
if (BN_num_bits(rsa_n) < SSH_RSA_MINIMUM_MODULUS_SIZE)
|
2018-05-06 12:24:45 +00:00
|
|
|
return SSH_ERR_KEY_LENGTH;
|
2014-01-30 10:56:49 +00:00
|
|
|
|
2016-03-10 20:10:25 +00:00
|
|
|
if ((b = sshbuf_from(sig, siglen)) == NULL)
|
2015-01-05 16:09:55 +00:00
|
|
|
return SSH_ERR_ALLOC_FAIL;
|
2018-05-06 12:27:04 +00:00
|
|
|
if (sshbuf_get_cstring(b, &sigtype, NULL) != 0) {
|
2015-01-05 16:09:55 +00:00
|
|
|
ret = SSH_ERR_INVALID_FORMAT;
|
|
|
|
goto out;
|
2008-07-22 18:58:19 +00:00
|
|
|
}
|
2018-08-28 10:47:58 +00:00
|
|
|
if ((hash_alg = rsa_hash_id_from_ident(sigtype)) == -1) {
|
2015-01-05 16:09:55 +00:00
|
|
|
ret = SSH_ERR_KEY_TYPE_MISMATCH;
|
|
|
|
goto out;
|
2008-07-22 18:58:19 +00:00
|
|
|
}
|
2018-08-28 10:47:58 +00: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;
|
|
|
|
}
|
|
|
|
}
|
2015-01-05 16:09:55 +00: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;
|
2008-07-22 18:58:19 +00:00
|
|
|
}
|
|
|
|
/* RSA_verify expects a signature of RSA_size */
|
|
|
|
modlen = RSA_size(key->rsa);
|
|
|
|
if (len > modlen) {
|
2015-01-05 16:09:55 +00:00
|
|
|
ret = SSH_ERR_KEY_BITS_MISMATCH;
|
|
|
|
goto out;
|
2008-07-22 18:58:19 +00:00
|
|
|
} else if (len < modlen) {
|
2015-01-05 16:09:55 +00: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;
|
|
|
|
}
|
2008-07-22 18:58:19 +00:00
|
|
|
memmove(sigblob + diff, sigblob, len);
|
2014-03-22 15:23:38 +00:00
|
|
|
explicit_bzero(sigblob, diff);
|
2008-07-22 18:58:19 +00:00
|
|
|
len = modlen;
|
|
|
|
}
|
2014-01-30 10:56:49 +00:00
|
|
|
if ((dlen = ssh_digest_bytes(hash_alg)) == 0) {
|
2015-01-05 16:09:55 +00:00
|
|
|
ret = SSH_ERR_INTERNAL_ERROR;
|
|
|
|
goto out;
|
2008-07-22 18:58:19 +00:00
|
|
|
}
|
2015-01-05 16:09:55 +00:00
|
|
|
if ((ret = ssh_digest_memory(hash_alg, data, datalen,
|
|
|
|
digest, sizeof(digest))) != 0)
|
|
|
|
goto out;
|
2008-07-22 18:58:19 +00:00
|
|
|
|
2014-01-30 10:56:49 +00:00
|
|
|
ret = openssh_RSA_verify(hash_alg, digest, dlen, sigblob, len,
|
|
|
|
key->rsa);
|
2015-01-05 16:09:55 +00:00
|
|
|
out:
|
2018-05-06 12:27:04 +00:00
|
|
|
freezero(sigblob, len);
|
|
|
|
free(sigtype);
|
2016-03-10 20:10:25 +00:00
|
|
|
sshbuf_free(b);
|
2014-03-22 15:23:38 +00:00
|
|
|
explicit_bzero(digest, sizeof(digest));
|
2008-07-22 18:58:19 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* See:
|
|
|
|
* http://www.rsasecurity.com/rsalabs/pkcs/pkcs-1/
|
|
|
|
* ftp://ftp.rsasecurity.com/pub/pkcs/pkcs-1/pkcs-1v2-1.asn
|
|
|
|
*/
|
2016-03-10 20:10:25 +00:00
|
|
|
|
2008-07-22 18:58:19 +00:00
|
|
|
/*
|
|
|
|
* id-sha1 OBJECT IDENTIFIER ::= { iso(1) identified-organization(3)
|
|
|
|
* oiw(14) secsig(3) algorithms(2) 26 }
|
|
|
|
*/
|
|
|
|
static const u_char id_sha1[] = {
|
|
|
|
0x30, 0x21, /* type Sequence, length 0x21 (33) */
|
|
|
|
0x30, 0x09, /* type Sequence, length 0x09 */
|
|
|
|
0x06, 0x05, /* type OID, length 0x05 */
|
|
|
|
0x2b, 0x0e, 0x03, 0x02, 0x1a, /* id-sha1 OID */
|
|
|
|
0x05, 0x00, /* NULL */
|
|
|
|
0x04, 0x14 /* Octet string, length 0x14 (20), followed by sha1 hash */
|
|
|
|
};
|
|
|
|
|
2016-03-10 20:10:25 +00:00
|
|
|
/*
|
|
|
|
* See http://csrc.nist.gov/groups/ST/crypto_apps_infra/csor/algorithms.html
|
|
|
|
* id-sha256 OBJECT IDENTIFIER ::= { joint-iso-itu-t(2) country(16) us(840)
|
|
|
|
* organization(1) gov(101) csor(3) nistAlgorithm(4) hashAlgs(2)
|
|
|
|
* id-sha256(1) }
|
|
|
|
*/
|
|
|
|
static const u_char id_sha256[] = {
|
|
|
|
0x30, 0x31, /* type Sequence, length 0x31 (49) */
|
|
|
|
0x30, 0x0d, /* type Sequence, length 0x0d (13) */
|
|
|
|
0x06, 0x09, /* type OID, length 0x09 */
|
|
|
|
0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01, /* id-sha256 */
|
|
|
|
0x05, 0x00, /* NULL */
|
|
|
|
0x04, 0x20 /* Octet string, length 0x20 (32), followed by sha256 hash */
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* See http://csrc.nist.gov/groups/ST/crypto_apps_infra/csor/algorithms.html
|
|
|
|
* id-sha512 OBJECT IDENTIFIER ::= { joint-iso-itu-t(2) country(16) us(840)
|
|
|
|
* organization(1) gov(101) csor(3) nistAlgorithm(4) hashAlgs(2)
|
|
|
|
* id-sha256(3) }
|
|
|
|
*/
|
|
|
|
static const u_char id_sha512[] = {
|
|
|
|
0x30, 0x51, /* type Sequence, length 0x51 (81) */
|
|
|
|
0x30, 0x0d, /* type Sequence, length 0x0d (13) */
|
|
|
|
0x06, 0x09, /* type OID, length 0x09 */
|
|
|
|
0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x03, /* id-sha512 */
|
|
|
|
0x05, 0x00, /* NULL */
|
|
|
|
0x04, 0x40 /* Octet string, length 0x40 (64), followed by sha512 hash */
|
|
|
|
};
|
|
|
|
|
|
|
|
static int
|
|
|
|
rsa_hash_alg_oid(int hash_alg, const u_char **oidp, size_t *oidlenp)
|
|
|
|
{
|
|
|
|
switch (hash_alg) {
|
|
|
|
case SSH_DIGEST_SHA1:
|
|
|
|
*oidp = id_sha1;
|
|
|
|
*oidlenp = sizeof(id_sha1);
|
|
|
|
break;
|
|
|
|
case SSH_DIGEST_SHA256:
|
|
|
|
*oidp = id_sha256;
|
|
|
|
*oidlenp = sizeof(id_sha256);
|
|
|
|
break;
|
|
|
|
case SSH_DIGEST_SHA512:
|
|
|
|
*oidp = id_sha512;
|
|
|
|
*oidlenp = sizeof(id_sha512);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return SSH_ERR_INVALID_ARGUMENT;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-07-22 18:58:19 +00:00
|
|
|
static int
|
2015-01-05 16:09:55 +00:00
|
|
|
openssh_RSA_verify(int hash_alg, u_char *hash, size_t hashlen,
|
|
|
|
u_char *sigbuf, size_t siglen, RSA *rsa)
|
2008-07-22 18:58:19 +00:00
|
|
|
{
|
2016-03-10 20:10:25 +00:00
|
|
|
size_t rsasize = 0, oidlen = 0, hlen = 0;
|
|
|
|
int ret, len, oidmatch, hashmatch;
|
2008-07-22 18:58:19 +00:00
|
|
|
const u_char *oid = NULL;
|
|
|
|
u_char *decrypted = NULL;
|
|
|
|
|
2016-03-10 20:10:25 +00:00
|
|
|
if ((ret = rsa_hash_alg_oid(hash_alg, &oid, &oidlen)) != 0)
|
|
|
|
return ret;
|
2015-01-05 16:09:55 +00:00
|
|
|
ret = SSH_ERR_INTERNAL_ERROR;
|
2016-03-10 20:10:25 +00:00
|
|
|
hlen = ssh_digest_bytes(hash_alg);
|
2008-07-22 18:58:19 +00:00
|
|
|
if (hashlen != hlen) {
|
2015-01-05 16:09:55 +00:00
|
|
|
ret = SSH_ERR_INVALID_ARGUMENT;
|
2008-07-22 18:58:19 +00:00
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
rsasize = RSA_size(rsa);
|
2015-01-05 16:09:55 +00:00
|
|
|
if (rsasize <= 0 || rsasize > SSHBUF_MAX_BIGNUM ||
|
|
|
|
siglen == 0 || siglen > rsasize) {
|
|
|
|
ret = SSH_ERR_INVALID_ARGUMENT;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
if ((decrypted = malloc(rsasize)) == NULL) {
|
|
|
|
ret = SSH_ERR_ALLOC_FAIL;
|
2008-07-22 18:58:19 +00:00
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
if ((len = RSA_public_decrypt(siglen, sigbuf, decrypted, rsa,
|
|
|
|
RSA_PKCS1_PADDING)) < 0) {
|
2015-01-05 16:09:55 +00:00
|
|
|
ret = SSH_ERR_LIBCRYPTO_ERROR;
|
2008-07-22 18:58:19 +00:00
|
|
|
goto done;
|
|
|
|
}
|
2015-01-05 16:09:55 +00:00
|
|
|
if (len < 0 || (size_t)len != hlen + oidlen) {
|
|
|
|
ret = SSH_ERR_INVALID_FORMAT;
|
2008-07-22 18:58:19 +00:00
|
|
|
goto done;
|
|
|
|
}
|
2010-11-08 10:45:44 +00:00
|
|
|
oidmatch = timingsafe_bcmp(decrypted, oid, oidlen) == 0;
|
|
|
|
hashmatch = timingsafe_bcmp(decrypted + oidlen, hash, hlen) == 0;
|
2015-01-05 16:09:55 +00:00
|
|
|
if (!oidmatch || !hashmatch) {
|
|
|
|
ret = SSH_ERR_SIGNATURE_INVALID;
|
2008-07-22 18:58:19 +00:00
|
|
|
goto done;
|
|
|
|
}
|
2015-01-05 16:09:55 +00:00
|
|
|
ret = 0;
|
2008-07-22 18:58:19 +00:00
|
|
|
done:
|
2018-05-06 12:27:04 +00:00
|
|
|
freezero(decrypted, rsasize);
|
2008-07-22 18:58:19 +00:00
|
|
|
return ret;
|
|
|
|
}
|
2015-07-02 13:15:34 +00:00
|
|
|
#endif /* WITH_OPENSSL */
|