|
Jakub Jelen |
751cd9 |
From ed7ec0cdf577ffbb0b15145340cf51596ca3eb89 Mon Sep 17 00:00:00 2001
|
|
Jakub Jelen |
751cd9 |
From: Jakub Jelen <jjelen@redhat.com>
|
|
Jakub Jelen |
751cd9 |
Date: Tue, 14 May 2019 10:45:45 +0200
|
|
Jakub Jelen |
751cd9 |
Subject: [PATCH] Use high-level OpenSSL API for signatures
|
|
Jakub Jelen |
751cd9 |
|
|
Jakub Jelen |
751cd9 |
---
|
|
Jakub Jelen |
751cd9 |
digest-openssl.c | 16 ++++
|
|
Jakub Jelen |
751cd9 |
digest.h | 6 ++
|
|
Jakub Jelen |
751cd9 |
ssh-dss.c | 65 ++++++++++------
|
|
Jakub Jelen |
751cd9 |
ssh-ecdsa.c | 69 ++++++++++-------
|
|
Jakub Jelen |
751cd9 |
ssh-rsa.c | 193 +++++++++--------------------------------------
|
|
Jakub Jelen |
751cd9 |
sshkey.c | 77 +++++++++++++++++++
|
|
Jakub Jelen |
751cd9 |
sshkey.h | 4 +
|
|
Jakub Jelen |
751cd9 |
7 files changed, 221 insertions(+), 209 deletions(-)
|
|
Jakub Jelen |
751cd9 |
|
|
Jakub Jelen |
751cd9 |
diff --git a/digest-openssl.c b/digest-openssl.c
|
|
Jakub Jelen |
751cd9 |
index da7ed72bc..6a21d8adb 100644
|
|
Jakub Jelen |
751cd9 |
--- a/digest-openssl.c
|
|
Jakub Jelen |
751cd9 |
+++ b/digest-openssl.c
|
|
Jakub Jelen |
751cd9 |
@@ -63,6 +63,22 @@ const struct ssh_digest digests[] = {
|
|
Jakub Jelen |
751cd9 |
{ -1, NULL, 0, NULL },
|
|
Jakub Jelen |
751cd9 |
};
|
|
Jakub Jelen |
751cd9 |
|
|
Jakub Jelen |
751cd9 |
+const EVP_MD *
|
|
Jakub Jelen |
751cd9 |
+ssh_digest_to_md(int digest_type)
|
|
Jakub Jelen |
751cd9 |
+{
|
|
Jakub Jelen |
751cd9 |
+ switch (digest_type) {
|
|
Jakub Jelen |
751cd9 |
+ case SSH_DIGEST_SHA1:
|
|
Jakub Jelen |
751cd9 |
+ return EVP_sha1();
|
|
Jakub Jelen |
751cd9 |
+ case SSH_DIGEST_SHA256:
|
|
Jakub Jelen |
751cd9 |
+ return EVP_sha256();
|
|
Jakub Jelen |
751cd9 |
+ case SSH_DIGEST_SHA384:
|
|
Jakub Jelen |
751cd9 |
+ return EVP_sha384();
|
|
Jakub Jelen |
751cd9 |
+ case SSH_DIGEST_SHA512:
|
|
Jakub Jelen |
751cd9 |
+ return EVP_sha512();
|
|
Jakub Jelen |
751cd9 |
+ }
|
|
Jakub Jelen |
751cd9 |
+ return NULL;
|
|
Jakub Jelen |
751cd9 |
+}
|
|
Jakub Jelen |
751cd9 |
+
|
|
Jakub Jelen |
751cd9 |
static const struct ssh_digest *
|
|
Jakub Jelen |
751cd9 |
ssh_digest_by_alg(int alg)
|
|
Jakub Jelen |
751cd9 |
{
|
|
Jakub Jelen |
751cd9 |
diff --git a/digest.h b/digest.h
|
|
Jakub Jelen |
751cd9 |
index 274574d0e..c7ceeb36f 100644
|
|
Jakub Jelen |
751cd9 |
--- a/digest.h
|
|
Jakub Jelen |
751cd9 |
+++ b/digest.h
|
|
Jakub Jelen |
751cd9 |
@@ -32,6 +32,12 @@
|
|
Jakub Jelen |
751cd9 |
struct sshbuf;
|
|
Jakub Jelen |
751cd9 |
struct ssh_digest_ctx;
|
|
Jakub Jelen |
751cd9 |
|
|
Jakub Jelen |
751cd9 |
+#ifdef WITH_OPENSSL
|
|
Jakub Jelen |
751cd9 |
+#include <openssl/evp.h>
|
|
Jakub Jelen |
751cd9 |
+/* Converts internal digest representation to the OpenSSL one */
|
|
Jakub Jelen |
751cd9 |
+const EVP_MD *ssh_digest_to_md(int digest_type);
|
|
Jakub Jelen |
751cd9 |
+#endif
|
|
Jakub Jelen |
751cd9 |
+
|
|
Jakub Jelen |
751cd9 |
/* Looks up a digest algorithm by name */
|
|
Jakub Jelen |
751cd9 |
int ssh_digest_alg_by_name(const char *name);
|
|
Jakub Jelen |
751cd9 |
|
|
Jakub Jelen |
751cd9 |
diff --git a/ssh-dss.c b/ssh-dss.c
|
|
Jakub Jelen |
751cd9 |
index a23c383dc..ea45e7275 100644
|
|
Jakub Jelen |
751cd9 |
--- a/ssh-dss.c
|
|
Jakub Jelen |
751cd9 |
+++ b/ssh-dss.c
|
|
Jakub Jelen |
751cd9 |
@@ -52,11 +52,15 @@ int
|
|
Dmitry Belyavskiy |
f561c6 |
const u_char *data, size_t datalen,
|
|
Dmitry Belyavskiy |
f561c6 |
const char *alg, const char *sk_provider, const char *sk_pin, u_int compat)
|
|
Jakub Jelen |
751cd9 |
{
|
|
Jakub Jelen |
751cd9 |
+ EVP_PKEY *pkey = NULL;
|
|
Jakub Jelen |
751cd9 |
DSA_SIG *sig = NULL;
|
|
Jakub Jelen |
751cd9 |
const BIGNUM *sig_r, *sig_s;
|
|
Jakub Jelen |
751cd9 |
- u_char digest[SSH_DIGEST_MAX_LENGTH], sigblob[SIGBLOB_LEN];
|
|
Jakub Jelen |
751cd9 |
- size_t rlen, slen, len, dlen = ssh_digest_bytes(SSH_DIGEST_SHA1);
|
|
Jakub Jelen |
751cd9 |
+ u_char sigblob[SIGBLOB_LEN];
|
|
Jakub Jelen |
751cd9 |
+ size_t rlen, slen;
|
|
Jakub Jelen |
751cd9 |
+ int len;
|
|
Jakub Jelen |
751cd9 |
struct sshbuf *b = NULL;
|
|
Jakub Jelen |
751cd9 |
+ u_char *sigb = NULL;
|
|
Jakub Jelen |
751cd9 |
+ const u_char *psig = NULL;
|
|
Jakub Jelen |
751cd9 |
int ret = SSH_ERR_INVALID_ARGUMENT;
|
|
Jakub Jelen |
751cd9 |
|
|
Jakub Jelen |
751cd9 |
if (lenp != NULL)
|
|
Jakub Jelen |
751cd9 |
@@ -67,17 +71,24 @@ ssh_dss_sign(const struct sshkey *key, u_char **sigp, size_t *lenp,
|
|
Jakub Jelen |
751cd9 |
if (key == NULL || key->dsa == NULL ||
|
|
Jakub Jelen |
751cd9 |
sshkey_type_plain(key->type) != KEY_DSA)
|
|
Jakub Jelen |
751cd9 |
return SSH_ERR_INVALID_ARGUMENT;
|
|
Jakub Jelen |
751cd9 |
- if (dlen == 0)
|
|
Jakub Jelen |
751cd9 |
- return SSH_ERR_INTERNAL_ERROR;
|
|
Jakub Jelen |
751cd9 |
|
|
Jakub Jelen |
751cd9 |
- if ((ret = ssh_digest_memory(SSH_DIGEST_SHA1, data, datalen,
|
|
Jakub Jelen |
751cd9 |
- digest, sizeof(digest))) != 0)
|
|
Jakub Jelen |
751cd9 |
+ if ((pkey = EVP_PKEY_new()) == NULL ||
|
|
Jakub Jelen |
751cd9 |
+ EVP_PKEY_set1_DSA(pkey, key->dsa) != 1)
|
|
Jakub Jelen |
751cd9 |
+ return SSH_ERR_ALLOC_FAIL;
|
|
Jakub Jelen |
751cd9 |
+ ret = sshkey_calculate_signature(pkey, SSH_DIGEST_SHA1, &sigb, &len,
|
|
Jakub Jelen |
751cd9 |
+ data, datalen);
|
|
Jakub Jelen |
751cd9 |
+ EVP_PKEY_free(pkey);
|
|
Jakub Jelen |
751cd9 |
+ if (ret < 0) {
|
|
Jakub Jelen |
751cd9 |
goto out;
|
|
Jakub Jelen |
751cd9 |
+ }
|
|
Jakub Jelen |
751cd9 |
|
|
Jakub Jelen |
751cd9 |
- if ((sig = DSA_do_sign(digest, dlen, key->dsa)) == NULL) {
|
|
Jakub Jelen |
751cd9 |
+ psig = sigb;
|
|
Jakub Jelen |
751cd9 |
+ if ((sig = d2i_DSA_SIG(NULL, &psig, len)) == NULL) {
|
|
Jakub Jelen |
751cd9 |
ret = SSH_ERR_LIBCRYPTO_ERROR;
|
|
Jakub Jelen |
751cd9 |
goto out;
|
|
Jakub Jelen |
751cd9 |
}
|
|
Jakub Jelen |
751cd9 |
+ free(sigb);
|
|
Jakub Jelen |
751cd9 |
+ sigb = NULL;
|
|
Jakub Jelen |
751cd9 |
|
|
Jakub Jelen |
751cd9 |
DSA_SIG_get0(sig, &sig_r, &sig_s);
|
|
Jakub Jelen |
751cd9 |
rlen = BN_num_bytes(sig_r);
|
|
Jakub Jelen |
751cd9 |
@@ -110,7 +121,7 @@ ssh_dss_sign(const struct sshkey *key, u_char **sigp, size_t *lenp,
|
|
Jakub Jelen |
751cd9 |
*lenp = len;
|
|
Jakub Jelen |
751cd9 |
ret = 0;
|
|
Jakub Jelen |
751cd9 |
out:
|
|
Jakub Jelen |
751cd9 |
- explicit_bzero(digest, sizeof(digest));
|
|
Jakub Jelen |
751cd9 |
+ free(sigb);
|
|
Jakub Jelen |
751cd9 |
DSA_SIG_free(sig);
|
|
Jakub Jelen |
751cd9 |
sshbuf_free(b);
|
|
Jakub Jelen |
751cd9 |
return ret;
|
|
Jakub Jelen |
751cd9 |
@@ -121,20 +132,20 @@ ssh_dss_verify(const struct sshkey *key,
|
|
Dmitry Belyavskiy |
f561c6 |
const u_char *data, size_t dlen, const char *alg, u_int compat,
|
|
Dmitry Belyavskiy |
f561c6 |
struct sshkey_sig_details **detailsp)
|
|
Jakub Jelen |
751cd9 |
{
|
|
Jakub Jelen |
751cd9 |
+ EVP_PKEY *pkey = NULL;
|
|
Dmitry Belyavskiy |
f561c6 |
DSA_SIG *dsig = NULL;
|
|
Jakub Jelen |
751cd9 |
BIGNUM *sig_r = NULL, *sig_s = NULL;
|
|
Jakub Jelen |
751cd9 |
- u_char digest[SSH_DIGEST_MAX_LENGTH], *sigblob = NULL;
|
|
Dmitry Belyavskiy |
f561c6 |
- size_t len, hlen = ssh_digest_bytes(SSH_DIGEST_SHA1);
|
|
Jakub Jelen |
751cd9 |
+ u_char *sigblob = NULL;
|
|
Jakub Jelen |
751cd9 |
+ size_t len, slen;
|
|
Jakub Jelen |
751cd9 |
int ret = SSH_ERR_INTERNAL_ERROR;
|
|
Jakub Jelen |
751cd9 |
struct sshbuf *b = NULL;
|
|
Jakub Jelen |
751cd9 |
char *ktype = NULL;
|
|
Jakub Jelen |
751cd9 |
+ u_char *sigb = NULL, *psig = NULL;
|
|
Jakub Jelen |
751cd9 |
|
|
Jakub Jelen |
751cd9 |
if (key == NULL || key->dsa == NULL ||
|
|
Jakub Jelen |
751cd9 |
sshkey_type_plain(key->type) != KEY_DSA ||
|
|
Dmitry Belyavskiy |
f561c6 |
sig == NULL || siglen == 0)
|
|
Jakub Jelen |
751cd9 |
return SSH_ERR_INVALID_ARGUMENT;
|
|
Dmitry Belyavskiy |
f561c6 |
- if (hlen == 0)
|
|
Jakub Jelen |
751cd9 |
- return SSH_ERR_INTERNAL_ERROR;
|
|
Jakub Jelen |
751cd9 |
|
|
Jakub Jelen |
751cd9 |
/* fetch signature */
|
|
Dmitry Belyavskiy |
f561c6 |
if ((b = sshbuf_from(sig, siglen)) == NULL)
|
|
Jakub Jelen |
751cd9 |
@@ -176,25 +187,31 @@ ssh_dss_verify(const struct sshkey *key,
|
|
Jakub Jelen |
751cd9 |
}
|
|
Jakub Jelen |
751cd9 |
sig_r = sig_s = NULL; /* transferred */
|
|
Jakub Jelen |
751cd9 |
|
|
Jakub Jelen |
751cd9 |
- /* sha1 the data */
|
|
Dmitry Belyavskiy |
f561c6 |
- if ((ret = ssh_digest_memory(SSH_DIGEST_SHA1, data, dlen,
|
|
Jakub Jelen |
751cd9 |
- digest, sizeof(digest))) != 0)
|
|
Jakub Jelen |
751cd9 |
+ if ((slen = i2d_DSA_SIG(sig, NULL)) == 0) {
|
|
Jakub Jelen |
751cd9 |
+ ret = SSH_ERR_LIBCRYPTO_ERROR;
|
|
Jakub Jelen |
751cd9 |
goto out;
|
|
Jakub Jelen |
751cd9 |
-
|
|
Dmitry Belyavskiy |
f561c6 |
- switch (DSA_do_verify(digest, hlen, dsig, key->dsa)) {
|
|
Jakub Jelen |
751cd9 |
- case 1:
|
|
Jakub Jelen |
751cd9 |
- ret = 0;
|
|
Jakub Jelen |
751cd9 |
- break;
|
|
Jakub Jelen |
751cd9 |
- case 0:
|
|
Jakub Jelen |
751cd9 |
- ret = SSH_ERR_SIGNATURE_INVALID;
|
|
Jakub Jelen |
751cd9 |
+ }
|
|
Jakub Jelen |
751cd9 |
+ if ((sigb = malloc(slen)) == NULL) {
|
|
Jakub Jelen |
751cd9 |
+ ret = SSH_ERR_ALLOC_FAIL;
|
|
Jakub Jelen |
751cd9 |
goto out;
|
|
Jakub Jelen |
751cd9 |
- default:
|
|
Jakub Jelen |
751cd9 |
+ }
|
|
Jakub Jelen |
751cd9 |
+ psig = sigb;
|
|
Jakub Jelen |
751cd9 |
+ if ((slen = i2d_DSA_SIG(sig, &psig)) == 0) {
|
|
Jakub Jelen |
751cd9 |
ret = SSH_ERR_LIBCRYPTO_ERROR;
|
|
Jakub Jelen |
751cd9 |
goto out;
|
|
Jakub Jelen |
751cd9 |
}
|
|
Jakub Jelen |
751cd9 |
|
|
Jakub Jelen |
751cd9 |
+ if ((pkey = EVP_PKEY_new()) == NULL ||
|
|
Jakub Jelen |
751cd9 |
+ EVP_PKEY_set1_DSA(pkey, key->dsa) != 1) {
|
|
Jakub Jelen |
751cd9 |
+ ret = SSH_ERR_ALLOC_FAIL;
|
|
Jakub Jelen |
751cd9 |
+ goto out;
|
|
Jakub Jelen |
751cd9 |
+ }
|
|
Dmitry Belyavskiy |
f561c6 |
+ ret = sshkey_verify_signature(pkey, SSH_DIGEST_SHA1, data, dlen,
|
|
Jakub Jelen |
751cd9 |
+ sigb, slen);
|
|
Jakub Jelen |
751cd9 |
+ EVP_PKEY_free(pkey);
|
|
Jakub Jelen |
751cd9 |
+
|
|
Jakub Jelen |
751cd9 |
out:
|
|
Jakub Jelen |
751cd9 |
- explicit_bzero(digest, sizeof(digest));
|
|
Jakub Jelen |
751cd9 |
+ free(sigb);
|
|
Dmitry Belyavskiy |
f561c6 |
DSA_SIG_free(dsig);
|
|
Jakub Jelen |
751cd9 |
BN_clear_free(sig_r);
|
|
Jakub Jelen |
751cd9 |
BN_clear_free(sig_s);
|
|
Jakub Jelen |
751cd9 |
diff --git a/ssh-ecdsa.c b/ssh-ecdsa.c
|
|
Jakub Jelen |
751cd9 |
index 599c7199d..b036796e8 100644
|
|
Jakub Jelen |
751cd9 |
--- a/ssh-ecdsa.c
|
|
Jakub Jelen |
751cd9 |
+++ b/ssh-ecdsa.c
|
|
Jakub Jelen |
751cd9 |
@@ -50,11 +50,13 @@ int
|
|
Dmitry Belyavskiy |
f561c6 |
const u_char *data, size_t dlen,
|
|
Dmitry Belyavskiy |
f561c6 |
const char *alg, const char *sk_provider, const char *sk_pin, u_int compat)
|
|
Jakub Jelen |
751cd9 |
{
|
|
Jakub Jelen |
751cd9 |
+ EVP_PKEY *pkey = NULL;
|
|
Dmitry Belyavskiy |
f561c6 |
ECDSA_SIG *esig = NULL;
|
|
Jakub Jelen |
751cd9 |
+ unsigned char *sigb = NULL;
|
|
Jakub Jelen |
751cd9 |
+ const unsigned char *psig;
|
|
Jakub Jelen |
751cd9 |
const BIGNUM *sig_r, *sig_s;
|
|
Jakub Jelen |
751cd9 |
int hash_alg;
|
|
Jakub Jelen |
751cd9 |
- u_char digest[SSH_DIGEST_MAX_LENGTH];
|
|
Dmitry Belyavskiy |
f561c6 |
- size_t len, hlen;
|
|
Jakub Jelen |
751cd9 |
+ int len;
|
|
Jakub Jelen |
751cd9 |
struct sshbuf *b = NULL, *bb = NULL;
|
|
Jakub Jelen |
751cd9 |
int ret = SSH_ERR_INTERNAL_ERROR;
|
|
Jakub Jelen |
751cd9 |
|
|
Jakub Jelen |
751cd9 |
@@ -67,18 +69,24 @@ ssh_ecdsa_sign(const struct sshkey *key, u_char **sigp, size_t *lenp,
|
|
Jakub Jelen |
751cd9 |
sshkey_type_plain(key->type) != KEY_ECDSA)
|
|
Jakub Jelen |
751cd9 |
return SSH_ERR_INVALID_ARGUMENT;
|
|
Jakub Jelen |
751cd9 |
|
|
Jakub Jelen |
751cd9 |
- if ((hash_alg = sshkey_ec_nid_to_hash_alg(key->ecdsa_nid)) == -1 ||
|
|
Dmitry Belyavskiy |
f561c6 |
- (hlen = ssh_digest_bytes(hash_alg)) == 0)
|
|
Jakub Jelen |
751cd9 |
+ if ((hash_alg = sshkey_ec_nid_to_hash_alg(key->ecdsa_nid)) == -1)
|
|
Jakub Jelen |
751cd9 |
return SSH_ERR_INTERNAL_ERROR;
|
|
Dmitry Belyavskiy |
f561c6 |
- if ((ret = ssh_digest_memory(hash_alg, data, dlen,
|
|
Jakub Jelen |
751cd9 |
- digest, sizeof(digest))) != 0)
|
|
Jakub Jelen |
751cd9 |
+
|
|
Jakub Jelen |
751cd9 |
+ if ((pkey = EVP_PKEY_new()) == NULL ||
|
|
Jakub Jelen |
751cd9 |
+ EVP_PKEY_set1_EC_KEY(pkey, key->ecdsa) != 1)
|
|
Jakub Jelen |
751cd9 |
+ return SSH_ERR_ALLOC_FAIL;
|
|
Jakub Jelen |
751cd9 |
+ ret = sshkey_calculate_signature(pkey, hash_alg, &sigb, &len, data,
|
|
Dmitry Belyavskiy |
f561c6 |
+ dlen);
|
|
Jakub Jelen |
751cd9 |
+ EVP_PKEY_free(pkey);
|
|
Jakub Jelen |
751cd9 |
+ if (ret < 0) {
|
|
Jakub Jelen |
751cd9 |
goto out;
|
|
Jakub Jelen |
751cd9 |
+ }
|
|
Jakub Jelen |
751cd9 |
|
|
Dmitry Belyavskiy |
f561c6 |
- if ((esig = ECDSA_do_sign(digest, hlen, key->ecdsa)) == NULL) {
|
|
Jakub Jelen |
751cd9 |
+ psig = sigb;
|
|
Dmitry Belyavskiy |
f561c6 |
+ if (d2i_ECDSA_SIG(&esig, &psig, len) == NULL) {
|
|
Jakub Jelen |
751cd9 |
ret = SSH_ERR_LIBCRYPTO_ERROR;
|
|
Jakub Jelen |
751cd9 |
goto out;
|
|
Jakub Jelen |
751cd9 |
}
|
|
Jakub Jelen |
751cd9 |
-
|
|
Jakub Jelen |
751cd9 |
if ((bb = sshbuf_new()) == NULL || (b = sshbuf_new()) == NULL) {
|
|
Jakub Jelen |
751cd9 |
ret = SSH_ERR_ALLOC_FAIL;
|
|
Jakub Jelen |
751cd9 |
goto out;
|
|
Jakub Jelen |
751cd9 |
@@ -102,7 +110,7 @@ ssh_ecdsa_sign(const struct sshkey *key, u_char **sigp, size_t *lenp,
|
|
Jakub Jelen |
751cd9 |
*lenp = len;
|
|
Jakub Jelen |
751cd9 |
ret = 0;
|
|
Jakub Jelen |
751cd9 |
out:
|
|
Jakub Jelen |
751cd9 |
- explicit_bzero(digest, sizeof(digest));
|
|
Jakub Jelen |
751cd9 |
+ free(sigb);
|
|
Jakub Jelen |
751cd9 |
sshbuf_free(b);
|
|
Jakub Jelen |
751cd9 |
sshbuf_free(bb);
|
|
Dmitry Belyavskiy |
f561c6 |
ECDSA_SIG_free(esig);
|
|
Jakub Jelen |
751cd9 |
@@ -115,22 +123,21 @@ ssh_ecdsa_verify(const struct sshkey *key,
|
|
Dmitry Belyavskiy |
f561c6 |
const u_char *data, size_t dlen, const char *alg, u_int compat,
|
|
Dmitry Belyavskiy |
f561c6 |
struct sshkey_sig_details **detailsp)
|
|
Jakub Jelen |
751cd9 |
{
|
|
Jakub Jelen |
751cd9 |
+ EVP_PKEY *pkey = NULL;
|
|
Dmitry Belyavskiy |
f561c6 |
ECDSA_SIG *esig = NULL;
|
|
Jakub Jelen |
751cd9 |
BIGNUM *sig_r = NULL, *sig_s = NULL;
|
|
Jakub Jelen |
751cd9 |
- int hash_alg;
|
|
Jakub Jelen |
751cd9 |
- u_char digest[SSH_DIGEST_MAX_LENGTH];
|
|
Dmitry Belyavskiy |
f561c6 |
- size_t hlen;
|
|
Jakub Jelen |
751cd9 |
+ int hash_alg, len;
|
|
Jakub Jelen |
751cd9 |
int ret = SSH_ERR_INTERNAL_ERROR;
|
|
Jakub Jelen |
751cd9 |
struct sshbuf *b = NULL, *sigbuf = NULL;
|
|
Jakub Jelen |
751cd9 |
char *ktype = NULL;
|
|
Jakub Jelen |
751cd9 |
+ unsigned char *sigb = NULL, *psig = NULL;
|
|
Jakub Jelen |
751cd9 |
|
|
Jakub Jelen |
751cd9 |
if (key == NULL || key->ecdsa == NULL ||
|
|
Jakub Jelen |
751cd9 |
sshkey_type_plain(key->type) != KEY_ECDSA ||
|
|
Dmitry Belyavskiy |
f561c6 |
sig == NULL || siglen == 0)
|
|
Jakub Jelen |
751cd9 |
return SSH_ERR_INVALID_ARGUMENT;
|
|
Jakub Jelen |
751cd9 |
|
|
Jakub Jelen |
751cd9 |
- if ((hash_alg = sshkey_ec_nid_to_hash_alg(key->ecdsa_nid)) == -1 ||
|
|
Dmitry Belyavskiy |
f561c6 |
- (hlen = ssh_digest_bytes(hash_alg)) == 0)
|
|
Jakub Jelen |
751cd9 |
+ if ((hash_alg = sshkey_ec_nid_to_hash_alg(key->ecdsa_nid)) == -1)
|
|
Jakub Jelen |
751cd9 |
return SSH_ERR_INTERNAL_ERROR;
|
|
Jakub Jelen |
751cd9 |
|
|
Jakub Jelen |
751cd9 |
/* fetch signature */
|
|
Jakub Jelen |
751cd9 |
@@ -166,28 +173,36 @@ ssh_ecdsa_verify(const struct sshkey *key,
|
|
Jakub Jelen |
751cd9 |
}
|
|
Jakub Jelen |
751cd9 |
sig_r = sig_s = NULL; /* transferred */
|
|
Jakub Jelen |
751cd9 |
|
|
Jakub Jelen |
751cd9 |
- if (sshbuf_len(sigbuf) != 0) {
|
|
Jakub Jelen |
751cd9 |
- ret = SSH_ERR_UNEXPECTED_TRAILING_DATA;
|
|
Jakub Jelen |
751cd9 |
+ /* Figure out the length */
|
|
Dmitry Belyavskiy |
f561c6 |
+ if ((len = i2d_ECDSA_SIG(esig, NULL)) == 0) {
|
|
Jakub Jelen |
751cd9 |
+ ret = SSH_ERR_LIBCRYPTO_ERROR;
|
|
Jakub Jelen |
751cd9 |
+ goto out;
|
|
Jakub Jelen |
751cd9 |
+ }
|
|
Jakub Jelen |
751cd9 |
+ if ((sigb = malloc(len)) == NULL) {
|
|
Jakub Jelen |
751cd9 |
+ ret = SSH_ERR_ALLOC_FAIL;
|
|
Jakub Jelen |
751cd9 |
goto out;
|
|
Jakub Jelen |
751cd9 |
}
|
|
Dmitry Belyavskiy |
f561c6 |
- if ((ret = ssh_digest_memory(hash_alg, data, dlen,
|
|
Jakub Jelen |
751cd9 |
- digest, sizeof(digest))) != 0)
|
|
Jakub Jelen |
751cd9 |
+ psig = sigb;
|
|
Dmitry Belyavskiy |
f561c6 |
+ if ((len = i2d_ECDSA_SIG(esig, &psig)) == 0) {
|
|
Jakub Jelen |
751cd9 |
+ ret = SSH_ERR_LIBCRYPTO_ERROR;
|
|
Jakub Jelen |
751cd9 |
goto out;
|
|
Jakub Jelen |
751cd9 |
+ }
|
|
Jakub Jelen |
751cd9 |
|
|
Dmitry Belyavskiy |
f561c6 |
- switch (ECDSA_do_verify(digest, hlen, esig, key->ecdsa)) {
|
|
Jakub Jelen |
751cd9 |
- case 1:
|
|
Jakub Jelen |
751cd9 |
- ret = 0;
|
|
Jakub Jelen |
751cd9 |
- break;
|
|
Jakub Jelen |
751cd9 |
- case 0:
|
|
Jakub Jelen |
751cd9 |
- ret = SSH_ERR_SIGNATURE_INVALID;
|
|
Jakub Jelen |
751cd9 |
+ if (sshbuf_len(sigbuf) != 0) {
|
|
Jakub Jelen |
751cd9 |
+ ret = SSH_ERR_UNEXPECTED_TRAILING_DATA;
|
|
Jakub Jelen |
751cd9 |
goto out;
|
|
Jakub Jelen |
751cd9 |
- default:
|
|
Jakub Jelen |
751cd9 |
- ret = SSH_ERR_LIBCRYPTO_ERROR;
|
|
Jakub Jelen |
751cd9 |
+ }
|
|
Jakub Jelen |
751cd9 |
+
|
|
Jakub Jelen |
751cd9 |
+ if ((pkey = EVP_PKEY_new()) == NULL ||
|
|
Jakub Jelen |
751cd9 |
+ EVP_PKEY_set1_EC_KEY(pkey, key->ecdsa) != 1) {
|
|
Jakub Jelen |
751cd9 |
+ ret = SSH_ERR_ALLOC_FAIL;
|
|
Jakub Jelen |
751cd9 |
goto out;
|
|
Jakub Jelen |
751cd9 |
}
|
|
Dmitry Belyavskiy |
f561c6 |
+ ret = sshkey_verify_signature(pkey, hash_alg, data, dlen, sigb, len);
|
|
Jakub Jelen |
751cd9 |
+ EVP_PKEY_free(pkey);
|
|
Jakub Jelen |
751cd9 |
|
|
Jakub Jelen |
751cd9 |
out:
|
|
Jakub Jelen |
751cd9 |
- explicit_bzero(digest, sizeof(digest));
|
|
Jakub Jelen |
751cd9 |
+ free(sigb);
|
|
Jakub Jelen |
751cd9 |
sshbuf_free(sigbuf);
|
|
Jakub Jelen |
751cd9 |
sshbuf_free(b);
|
|
Dmitry Belyavskiy |
f561c6 |
ECDSA_SIG_free(esig);
|
|
Jakub Jelen |
751cd9 |
diff --git a/ssh-rsa.c b/ssh-rsa.c
|
|
Jakub Jelen |
751cd9 |
index 9b14f9a9a..8ef3a6aca 100644
|
|
Jakub Jelen |
751cd9 |
--- a/ssh-rsa.c
|
|
Jakub Jelen |
751cd9 |
+++ b/ssh-rsa.c
|
|
Jakub Jelen |
751cd9 |
@@ -37,7 +37,7 @@
|
|
Jakub Jelen |
751cd9 |
|
|
Jakub Jelen |
751cd9 |
#include "openbsd-compat/openssl-compat.h"
|
|
Jakub Jelen |
751cd9 |
|
|
Jakub Jelen |
751cd9 |
-static int openssh_RSA_verify(int, u_char *, size_t, u_char *, size_t, RSA *);
|
|
Jakub Jelen |
751cd9 |
+static int openssh_RSA_verify(int, const u_char *, size_t, u_char *, size_t, EVP_PKEY *);
|
|
Jakub Jelen |
751cd9 |
|
|
Dmitry Belyavskiy |
f561c6 |
static u_int
|
|
Dmitry Belyavskiy |
f561c6 |
ssh_rsa_size(const struct sshkey *key)
|
|
Jakub Jelen |
751cd9 |
@@ -90,21 +90,6 @@ rsa_hash_id_from_keyname(const char *alg)
|
|
Jakub Jelen |
751cd9 |
return -1;
|
|
Jakub Jelen |
751cd9 |
}
|
|
Jakub Jelen |
751cd9 |
|
|
Jakub Jelen |
751cd9 |
-static int
|
|
Jakub Jelen |
751cd9 |
-rsa_hash_alg_nid(int type)
|
|
Jakub Jelen |
751cd9 |
-{
|
|
Jakub Jelen |
751cd9 |
- switch (type) {
|
|
Jakub Jelen |
751cd9 |
- case SSH_DIGEST_SHA1:
|
|
Jakub Jelen |
751cd9 |
- return NID_sha1;
|
|
Jakub Jelen |
751cd9 |
- case SSH_DIGEST_SHA256:
|
|
Jakub Jelen |
751cd9 |
- return NID_sha256;
|
|
Jakub Jelen |
751cd9 |
- case SSH_DIGEST_SHA512:
|
|
Jakub Jelen |
751cd9 |
- return NID_sha512;
|
|
Jakub Jelen |
751cd9 |
- default:
|
|
Jakub Jelen |
751cd9 |
- return -1;
|
|
Jakub Jelen |
751cd9 |
- }
|
|
Jakub Jelen |
751cd9 |
-}
|
|
Jakub Jelen |
751cd9 |
-
|
|
Jakub Jelen |
751cd9 |
int
|
|
Jakub Jelen |
751cd9 |
ssh_rsa_complete_crt_parameters(struct sshkey *key, const BIGNUM *iqmp)
|
|
Jakub Jelen |
751cd9 |
{
|
|
Jakub Jelen |
751cd9 |
@@ -164,11 +149,10 @@ int
|
|
Dmitry Belyavskiy |
f561c6 |
const u_char *data, size_t datalen,
|
|
Dmitry Belyavskiy |
f561c6 |
const char *alg, const char *sk_provider, const char *sk_pin, u_int compat)
|
|
Jakub Jelen |
751cd9 |
{
|
|
Jakub Jelen |
751cd9 |
- const BIGNUM *rsa_n;
|
|
Jakub Jelen |
751cd9 |
- u_char digest[SSH_DIGEST_MAX_LENGTH], *sig = NULL;
|
|
Jakub Jelen |
751cd9 |
- size_t slen = 0;
|
|
Dmitry Belyavskiy |
f561c6 |
- u_int hlen, len;
|
|
Jakub Jelen |
751cd9 |
- int nid, hash_alg, ret = SSH_ERR_INTERNAL_ERROR;
|
|
Jakub Jelen |
751cd9 |
+ EVP_PKEY *pkey = NULL;
|
|
Jakub Jelen |
751cd9 |
+ u_char *sig = NULL;
|
|
Jakub Jelen |
751cd9 |
+ int len, slen = 0;
|
|
Jakub Jelen |
751cd9 |
+ int hash_alg, ret = SSH_ERR_INTERNAL_ERROR;
|
|
Jakub Jelen |
751cd9 |
struct sshbuf *b = NULL;
|
|
Jakub Jelen |
751cd9 |
|
|
Jakub Jelen |
751cd9 |
if (lenp != NULL)
|
|
Jakub Jelen |
751cd9 |
@@ -180,33 +164,24 @@ ssh_rsa_sign(const struct sshkey *key, u_char **sigp, size_t *lenp,
|
|
Jakub Jelen |
751cd9 |
hash_alg = SSH_DIGEST_SHA1;
|
|
Jakub Jelen |
751cd9 |
else
|
|
Dmitry Belyavskiy |
f561c6 |
hash_alg = rsa_hash_id_from_keyname(alg);
|
|
Jakub Jelen |
751cd9 |
+
|
|
Jakub Jelen |
751cd9 |
if (key == NULL || key->rsa == NULL || hash_alg == -1 ||
|
|
Jakub Jelen |
751cd9 |
sshkey_type_plain(key->type) != KEY_RSA)
|
|
Jakub Jelen |
751cd9 |
return SSH_ERR_INVALID_ARGUMENT;
|
|
Jakub Jelen |
751cd9 |
- RSA_get0_key(key->rsa, &rsa_n, NULL, NULL);
|
|
Jakub Jelen |
751cd9 |
- if (BN_num_bits(rsa_n) < SSH_RSA_MINIMUM_MODULUS_SIZE)
|
|
Jakub Jelen |
751cd9 |
- return SSH_ERR_KEY_LENGTH;
|
|
Jakub Jelen |
751cd9 |
slen = RSA_size(key->rsa);
|
|
Jakub Jelen |
751cd9 |
- if (slen <= 0 || slen > SSHBUF_MAX_BIGNUM)
|
|
Jakub Jelen |
751cd9 |
- return SSH_ERR_INVALID_ARGUMENT;
|
|
Jakub Jelen |
751cd9 |
-
|
|
Jakub Jelen |
751cd9 |
- /* hash the data */
|
|
Jakub Jelen |
751cd9 |
- nid = rsa_hash_alg_nid(hash_alg);
|
|
Dmitry Belyavskiy |
f561c6 |
- if ((hlen = ssh_digest_bytes(hash_alg)) == 0)
|
|
Jakub Jelen |
751cd9 |
- return SSH_ERR_INTERNAL_ERROR;
|
|
Jakub Jelen |
751cd9 |
- if ((ret = ssh_digest_memory(hash_alg, data, datalen,
|
|
Jakub Jelen |
751cd9 |
- digest, sizeof(digest))) != 0)
|
|
Jakub Jelen |
751cd9 |
- goto out;
|
|
Jakub Jelen |
751cd9 |
+ if (RSA_bits(key->rsa) < SSH_RSA_MINIMUM_MODULUS_SIZE)
|
|
Jakub Jelen |
751cd9 |
+ return SSH_ERR_KEY_LENGTH;
|
|
Jakub Jelen |
751cd9 |
|
|
Jakub Jelen |
751cd9 |
- if ((sig = malloc(slen)) == NULL) {
|
|
Jakub Jelen |
751cd9 |
- ret = SSH_ERR_ALLOC_FAIL;
|
|
Jakub Jelen |
751cd9 |
+ if ((pkey = EVP_PKEY_new()) == NULL ||
|
|
Jakub Jelen |
751cd9 |
+ EVP_PKEY_set1_RSA(pkey, key->rsa) != 1)
|
|
Jakub Jelen |
751cd9 |
+ return SSH_ERR_ALLOC_FAIL;
|
|
Jakub Jelen |
751cd9 |
+ ret = sshkey_calculate_signature(pkey, hash_alg, &sig, &len, data,
|
|
Jakub Jelen |
751cd9 |
+ datalen);
|
|
Jakub Jelen |
751cd9 |
+ EVP_PKEY_free(pkey);
|
|
Jakub Jelen |
751cd9 |
+ if (ret < 0) {
|
|
Jakub Jelen |
751cd9 |
goto out;
|
|
Jakub Jelen |
751cd9 |
}
|
|
Jakub Jelen |
751cd9 |
|
|
Dmitry Belyavskiy |
f561c6 |
- if (RSA_sign(nid, digest, hlen, sig, &len, key->rsa) != 1) {
|
|
Jakub Jelen |
751cd9 |
- ret = SSH_ERR_LIBCRYPTO_ERROR;
|
|
Jakub Jelen |
751cd9 |
- goto out;
|
|
Jakub Jelen |
751cd9 |
- }
|
|
Jakub Jelen |
751cd9 |
if (len < slen) {
|
|
Jakub Jelen |
751cd9 |
size_t diff = slen - len;
|
|
Jakub Jelen |
751cd9 |
memmove(sig + diff, sig, len);
|
|
Jakub Jelen |
751cd9 |
@@ -215,6 +190,7 @@ ssh_rsa_sign(const struct sshkey *key, u_char **sigp, size_t *lenp,
|
|
Jakub Jelen |
751cd9 |
ret = SSH_ERR_INTERNAL_ERROR;
|
|
Jakub Jelen |
751cd9 |
goto out;
|
|
Jakub Jelen |
751cd9 |
}
|
|
Jakub Jelen |
751cd9 |
+
|
|
Jakub Jelen |
751cd9 |
/* encode signature */
|
|
Jakub Jelen |
751cd9 |
if ((b = sshbuf_new()) == NULL) {
|
|
Jakub Jelen |
751cd9 |
ret = SSH_ERR_ALLOC_FAIL;
|
|
Jakub Jelen |
751cd9 |
@@ -235,7 +211,6 @@ ssh_rsa_sign(const struct sshkey *key, u_char **sigp, size_t *lenp,
|
|
Jakub Jelen |
751cd9 |
*lenp = len;
|
|
Jakub Jelen |
751cd9 |
ret = 0;
|
|
Jakub Jelen |
751cd9 |
out:
|
|
Jakub Jelen |
751cd9 |
- explicit_bzero(digest, sizeof(digest));
|
|
Jakub Jelen |
751cd9 |
freezero(sig, slen);
|
|
Jakub Jelen |
751cd9 |
sshbuf_free(b);
|
|
Jakub Jelen |
751cd9 |
return ret;
|
|
Jakub Jelen |
751cd9 |
@@ -246,10 +221,10 @@ ssh_rsa_verify(const struct sshkey *key,
|
|
Dmitry Belyavskiy |
f561c6 |
const u_char *data, size_t dlen, const char *alg, u_int compat,
|
|
Dmitry Belyavskiy |
f561c6 |
struct sshkey_sig_details **detailsp)
|
|
Jakub Jelen |
751cd9 |
{
|
|
Jakub Jelen |
751cd9 |
- const BIGNUM *rsa_n;
|
|
Jakub Jelen |
751cd9 |
+ EVP_PKEY *pkey = NULL;
|
|
Jakub Jelen |
751cd9 |
char *sigtype = NULL;
|
|
Jakub Jelen |
751cd9 |
int hash_alg, want_alg, ret = SSH_ERR_INTERNAL_ERROR;
|
|
Dmitry Belyavskiy |
f561c6 |
- size_t len = 0, diff, modlen, hlen;
|
|
Jakub Jelen |
751cd9 |
+ size_t len = 0, diff, modlen;
|
|
Jakub Jelen |
751cd9 |
struct sshbuf *b = NULL;
|
|
Jakub Jelen |
751cd9 |
u_char digest[SSH_DIGEST_MAX_LENGTH], *osigblob, *sigblob = NULL;
|
|
Jakub Jelen |
751cd9 |
|
|
Jakub Jelen |
751cd9 |
@@ -257,8 +232,7 @@ ssh_rsa_verify(const struct sshkey *key,
|
|
Jakub Jelen |
751cd9 |
sshkey_type_plain(key->type) != KEY_RSA ||
|
|
Jakub Jelen |
751cd9 |
sig == NULL || siglen == 0)
|
|
Jakub Jelen |
751cd9 |
return SSH_ERR_INVALID_ARGUMENT;
|
|
Jakub Jelen |
751cd9 |
- RSA_get0_key(key->rsa, &rsa_n, NULL, NULL);
|
|
Jakub Jelen |
751cd9 |
- if (BN_num_bits(rsa_n) < SSH_RSA_MINIMUM_MODULUS_SIZE)
|
|
Jakub Jelen |
751cd9 |
+ if (RSA_bits(key->rsa) < SSH_RSA_MINIMUM_MODULUS_SIZE)
|
|
Jakub Jelen |
751cd9 |
return SSH_ERR_KEY_LENGTH;
|
|
Jakub Jelen |
751cd9 |
|
|
Jakub Jelen |
751cd9 |
if ((b = sshbuf_from(sig, siglen)) == NULL)
|
|
Jakub Jelen |
751cd9 |
@@ -310,16 +284,15 @@ ssh_rsa_verify(const struct sshkey *key,
|
|
Jakub Jelen |
751cd9 |
explicit_bzero(sigblob, diff);
|
|
Jakub Jelen |
751cd9 |
len = modlen;
|
|
Jakub Jelen |
751cd9 |
}
|
|
Dmitry Belyavskiy |
f561c6 |
- if ((hlen = ssh_digest_bytes(hash_alg)) == 0) {
|
|
Jakub Jelen |
751cd9 |
- ret = SSH_ERR_INTERNAL_ERROR;
|
|
Jakub Jelen |
751cd9 |
+
|
|
Jakub Jelen |
751cd9 |
+ if ((pkey = EVP_PKEY_new()) == NULL ||
|
|
Jakub Jelen |
751cd9 |
+ EVP_PKEY_set1_RSA(pkey, key->rsa) != 1) {
|
|
Jakub Jelen |
751cd9 |
+ ret = SSH_ERR_ALLOC_FAIL;
|
|
Jakub Jelen |
751cd9 |
goto out;
|
|
Jakub Jelen |
751cd9 |
}
|
|
Dmitry Belyavskiy |
f561c6 |
- if ((ret = ssh_digest_memory(hash_alg, data, dlen,
|
|
Jakub Jelen |
751cd9 |
- digest, sizeof(digest))) != 0)
|
|
Jakub Jelen |
751cd9 |
- goto out;
|
|
Dmitry Belyavskiy |
f561c6 |
+ ret = openssh_RSA_verify(hash_alg, data, dlen, sigblob, len, pkey);
|
|
Jakub Jelen |
751cd9 |
+ EVP_PKEY_free(pkey);
|
|
Jakub Jelen |
751cd9 |
|
|
Dmitry Belyavskiy |
f561c6 |
- ret = openssh_RSA_verify(hash_alg, digest, hlen, sigblob, len,
|
|
Jakub Jelen |
751cd9 |
- key->rsa);
|
|
Jakub Jelen |
751cd9 |
out:
|
|
Jakub Jelen |
751cd9 |
freezero(sigblob, len);
|
|
Jakub Jelen |
751cd9 |
free(sigtype);
|
|
Jakub Jelen |
751cd9 |
@@ -328,122 +301,26 @@ ssh_rsa_verify(const struct sshkey *key,
|
|
Jakub Jelen |
751cd9 |
return ret;
|
|
Jakub Jelen |
751cd9 |
}
|
|
Jakub Jelen |
751cd9 |
|
|
Jakub Jelen |
751cd9 |
-/*
|
|
Jakub Jelen |
751cd9 |
- * See:
|
|
Jakub Jelen |
751cd9 |
- * http://www.rsasecurity.com/rsalabs/pkcs/pkcs-1/
|
|
Jakub Jelen |
751cd9 |
- * ftp://ftp.rsasecurity.com/pub/pkcs/pkcs-1/pkcs-1v2-1.asn
|
|
Jakub Jelen |
751cd9 |
- */
|
|
Jakub Jelen |
751cd9 |
-
|
|
Jakub Jelen |
751cd9 |
-/*
|
|
Jakub Jelen |
751cd9 |
- * id-sha1 OBJECT IDENTIFIER ::= { iso(1) identified-organization(3)
|
|
Jakub Jelen |
751cd9 |
- * oiw(14) secsig(3) algorithms(2) 26 }
|
|
Jakub Jelen |
751cd9 |
- */
|
|
Jakub Jelen |
751cd9 |
-static const u_char id_sha1[] = {
|
|
Jakub Jelen |
751cd9 |
- 0x30, 0x21, /* type Sequence, length 0x21 (33) */
|
|
Jakub Jelen |
751cd9 |
- 0x30, 0x09, /* type Sequence, length 0x09 */
|
|
Jakub Jelen |
751cd9 |
- 0x06, 0x05, /* type OID, length 0x05 */
|
|
Jakub Jelen |
751cd9 |
- 0x2b, 0x0e, 0x03, 0x02, 0x1a, /* id-sha1 OID */
|
|
Jakub Jelen |
751cd9 |
- 0x05, 0x00, /* NULL */
|
|
Jakub Jelen |
751cd9 |
- 0x04, 0x14 /* Octet string, length 0x14 (20), followed by sha1 hash */
|
|
Jakub Jelen |
751cd9 |
-};
|
|
Jakub Jelen |
751cd9 |
-
|
|
Jakub Jelen |
751cd9 |
-/*
|
|
Jakub Jelen |
751cd9 |
- * See http://csrc.nist.gov/groups/ST/crypto_apps_infra/csor/algorithms.html
|
|
Jakub Jelen |
751cd9 |
- * id-sha256 OBJECT IDENTIFIER ::= { joint-iso-itu-t(2) country(16) us(840)
|
|
Jakub Jelen |
751cd9 |
- * organization(1) gov(101) csor(3) nistAlgorithm(4) hashAlgs(2)
|
|
Jakub Jelen |
751cd9 |
- * id-sha256(1) }
|
|
Jakub Jelen |
751cd9 |
- */
|
|
Jakub Jelen |
751cd9 |
-static const u_char id_sha256[] = {
|
|
Jakub Jelen |
751cd9 |
- 0x30, 0x31, /* type Sequence, length 0x31 (49) */
|
|
Jakub Jelen |
751cd9 |
- 0x30, 0x0d, /* type Sequence, length 0x0d (13) */
|
|
Jakub Jelen |
751cd9 |
- 0x06, 0x09, /* type OID, length 0x09 */
|
|
Jakub Jelen |
751cd9 |
- 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01, /* id-sha256 */
|
|
Jakub Jelen |
751cd9 |
- 0x05, 0x00, /* NULL */
|
|
Jakub Jelen |
751cd9 |
- 0x04, 0x20 /* Octet string, length 0x20 (32), followed by sha256 hash */
|
|
Jakub Jelen |
751cd9 |
-};
|
|
Jakub Jelen |
751cd9 |
-
|
|
Jakub Jelen |
751cd9 |
-/*
|
|
Jakub Jelen |
751cd9 |
- * See http://csrc.nist.gov/groups/ST/crypto_apps_infra/csor/algorithms.html
|
|
Jakub Jelen |
751cd9 |
- * id-sha512 OBJECT IDENTIFIER ::= { joint-iso-itu-t(2) country(16) us(840)
|
|
Jakub Jelen |
751cd9 |
- * organization(1) gov(101) csor(3) nistAlgorithm(4) hashAlgs(2)
|
|
Jakub Jelen |
751cd9 |
- * id-sha256(3) }
|
|
Jakub Jelen |
751cd9 |
- */
|
|
Jakub Jelen |
751cd9 |
-static const u_char id_sha512[] = {
|
|
Jakub Jelen |
751cd9 |
- 0x30, 0x51, /* type Sequence, length 0x51 (81) */
|
|
Jakub Jelen |
751cd9 |
- 0x30, 0x0d, /* type Sequence, length 0x0d (13) */
|
|
Jakub Jelen |
751cd9 |
- 0x06, 0x09, /* type OID, length 0x09 */
|
|
Jakub Jelen |
751cd9 |
- 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x03, /* id-sha512 */
|
|
Jakub Jelen |
751cd9 |
- 0x05, 0x00, /* NULL */
|
|
Jakub Jelen |
751cd9 |
- 0x04, 0x40 /* Octet string, length 0x40 (64), followed by sha512 hash */
|
|
Jakub Jelen |
751cd9 |
-};
|
|
Jakub Jelen |
751cd9 |
-
|
|
Jakub Jelen |
751cd9 |
static int
|
|
Jakub Jelen |
751cd9 |
-rsa_hash_alg_oid(int hash_alg, const u_char **oidp, size_t *oidlenp)
|
|
Jakub Jelen |
751cd9 |
+openssh_RSA_verify(int hash_alg, const u_char *data, size_t datalen,
|
|
Jakub Jelen |
751cd9 |
+ u_char *sigbuf, size_t siglen, EVP_PKEY *pkey)
|
|
Jakub Jelen |
751cd9 |
{
|
|
Jakub Jelen |
751cd9 |
- switch (hash_alg) {
|
|
Jakub Jelen |
751cd9 |
- case SSH_DIGEST_SHA1:
|
|
Jakub Jelen |
751cd9 |
- *oidp = id_sha1;
|
|
Jakub Jelen |
751cd9 |
- *oidlenp = sizeof(id_sha1);
|
|
Jakub Jelen |
751cd9 |
- break;
|
|
Jakub Jelen |
751cd9 |
- case SSH_DIGEST_SHA256:
|
|
Jakub Jelen |
751cd9 |
- *oidp = id_sha256;
|
|
Jakub Jelen |
751cd9 |
- *oidlenp = sizeof(id_sha256);
|
|
Jakub Jelen |
751cd9 |
- break;
|
|
Jakub Jelen |
751cd9 |
- case SSH_DIGEST_SHA512:
|
|
Jakub Jelen |
751cd9 |
- *oidp = id_sha512;
|
|
Jakub Jelen |
751cd9 |
- *oidlenp = sizeof(id_sha512);
|
|
Jakub Jelen |
751cd9 |
- break;
|
|
Jakub Jelen |
751cd9 |
- default:
|
|
Jakub Jelen |
751cd9 |
- return SSH_ERR_INVALID_ARGUMENT;
|
|
Jakub Jelen |
751cd9 |
- }
|
|
Jakub Jelen |
751cd9 |
- return 0;
|
|
Jakub Jelen |
751cd9 |
-}
|
|
Jakub Jelen |
751cd9 |
+ size_t rsasize = 0;
|
|
Jakub Jelen |
751cd9 |
+ const RSA *rsa;
|
|
Jakub Jelen |
751cd9 |
+ int ret;
|
|
Jakub Jelen |
751cd9 |
|
|
Jakub Jelen |
751cd9 |
-static int
|
|
Jakub Jelen |
751cd9 |
-openssh_RSA_verify(int hash_alg, u_char *hash, size_t hashlen,
|
|
Jakub Jelen |
751cd9 |
- u_char *sigbuf, size_t siglen, RSA *rsa)
|
|
Jakub Jelen |
751cd9 |
-{
|
|
Jakub Jelen |
751cd9 |
- size_t rsasize = 0, oidlen = 0, hlen = 0;
|
|
Jakub Jelen |
751cd9 |
- int ret, len, oidmatch, hashmatch;
|
|
Jakub Jelen |
751cd9 |
- const u_char *oid = NULL;
|
|
Jakub Jelen |
751cd9 |
- u_char *decrypted = NULL;
|
|
Jakub Jelen |
751cd9 |
-
|
|
Jakub Jelen |
751cd9 |
- if ((ret = rsa_hash_alg_oid(hash_alg, &oid, &oidlen)) != 0)
|
|
Jakub Jelen |
751cd9 |
- return ret;
|
|
Jakub Jelen |
751cd9 |
- ret = SSH_ERR_INTERNAL_ERROR;
|
|
Jakub Jelen |
751cd9 |
- hlen = ssh_digest_bytes(hash_alg);
|
|
Jakub Jelen |
751cd9 |
- if (hashlen != hlen) {
|
|
Jakub Jelen |
751cd9 |
- ret = SSH_ERR_INVALID_ARGUMENT;
|
|
Jakub Jelen |
751cd9 |
- goto done;
|
|
Jakub Jelen |
751cd9 |
- }
|
|
Jakub Jelen |
751cd9 |
+ rsa = EVP_PKEY_get0_RSA(pkey);
|
|
Jakub Jelen |
751cd9 |
rsasize = RSA_size(rsa);
|
|
Jakub Jelen |
751cd9 |
if (rsasize <= 0 || rsasize > SSHBUF_MAX_BIGNUM ||
|
|
Jakub Jelen |
751cd9 |
siglen == 0 || siglen > rsasize) {
|
|
Jakub Jelen |
751cd9 |
ret = SSH_ERR_INVALID_ARGUMENT;
|
|
Jakub Jelen |
751cd9 |
goto done;
|
|
Jakub Jelen |
751cd9 |
}
|
|
Jakub Jelen |
751cd9 |
- if ((decrypted = malloc(rsasize)) == NULL) {
|
|
Jakub Jelen |
751cd9 |
- ret = SSH_ERR_ALLOC_FAIL;
|
|
Jakub Jelen |
751cd9 |
- goto done;
|
|
Jakub Jelen |
751cd9 |
- }
|
|
Jakub Jelen |
751cd9 |
- if ((len = RSA_public_decrypt(siglen, sigbuf, decrypted, rsa,
|
|
Jakub Jelen |
751cd9 |
- RSA_PKCS1_PADDING)) < 0) {
|
|
Jakub Jelen |
751cd9 |
- ret = SSH_ERR_LIBCRYPTO_ERROR;
|
|
Jakub Jelen |
751cd9 |
- goto done;
|
|
Jakub Jelen |
751cd9 |
- }
|
|
Jakub Jelen |
751cd9 |
- if (len < 0 || (size_t)len != hlen + oidlen) {
|
|
Jakub Jelen |
751cd9 |
- ret = SSH_ERR_INVALID_FORMAT;
|
|
Jakub Jelen |
751cd9 |
- goto done;
|
|
Jakub Jelen |
751cd9 |
- }
|
|
Jakub Jelen |
751cd9 |
- oidmatch = timingsafe_bcmp(decrypted, oid, oidlen) == 0;
|
|
Jakub Jelen |
751cd9 |
- hashmatch = timingsafe_bcmp(decrypted + oidlen, hash, hlen) == 0;
|
|
Jakub Jelen |
751cd9 |
- if (!oidmatch || !hashmatch) {
|
|
Jakub Jelen |
751cd9 |
- ret = SSH_ERR_SIGNATURE_INVALID;
|
|
Jakub Jelen |
751cd9 |
- goto done;
|
|
Jakub Jelen |
751cd9 |
- }
|
|
Jakub Jelen |
751cd9 |
- ret = 0;
|
|
Jakub Jelen |
751cd9 |
+
|
|
Jakub Jelen |
751cd9 |
+ ret = sshkey_verify_signature(pkey, hash_alg, data, datalen,
|
|
Jakub Jelen |
751cd9 |
+ sigbuf, siglen);
|
|
Jakub Jelen |
751cd9 |
+
|
|
Jakub Jelen |
751cd9 |
done:
|
|
Jakub Jelen |
751cd9 |
- freezero(decrypted, rsasize);
|
|
Jakub Jelen |
751cd9 |
return ret;
|
|
Jakub Jelen |
751cd9 |
}
|
|
Dmitry Belyavskiy |
f561c6 |
|
|
Jakub Jelen |
751cd9 |
diff --git a/sshkey.c b/sshkey.c
|
|
Jakub Jelen |
751cd9 |
index ad1957762..b95ed0b10 100644
|
|
Jakub Jelen |
751cd9 |
--- a/sshkey.c
|
|
Jakub Jelen |
751cd9 |
+++ b/sshkey.c
|
|
Jakub Jelen |
751cd9 |
@@ -358,6 +358,83 @@ sshkey_type_plain(int type)
|
|
Jakub Jelen |
751cd9 |
}
|
|
Jakub Jelen |
751cd9 |
|
|
Jakub Jelen |
751cd9 |
#ifdef WITH_OPENSSL
|
|
Jakub Jelen |
751cd9 |
+int
|
|
Jakub Jelen |
751cd9 |
+sshkey_calculate_signature(EVP_PKEY *pkey, int hash_alg, u_char **sigp,
|
|
Jakub Jelen |
751cd9 |
+ int *lenp, const u_char *data, size_t datalen)
|
|
Jakub Jelen |
751cd9 |
+{
|
|
Jakub Jelen |
751cd9 |
+ EVP_MD_CTX *ctx = NULL;
|
|
Jakub Jelen |
751cd9 |
+ u_char *sig = NULL;
|
|
Jakub Jelen |
751cd9 |
+ int ret, slen, len;
|
|
Jakub Jelen |
751cd9 |
+
|
|
Jakub Jelen |
751cd9 |
+ if (sigp == NULL || lenp == NULL) {
|
|
Jakub Jelen |
751cd9 |
+ return SSH_ERR_INVALID_ARGUMENT;
|
|
Jakub Jelen |
751cd9 |
+ }
|
|
Jakub Jelen |
751cd9 |
+
|
|
Jakub Jelen |
751cd9 |
+ slen = EVP_PKEY_size(pkey);
|
|
Jakub Jelen |
751cd9 |
+ if (slen <= 0 || slen > SSHBUF_MAX_BIGNUM)
|
|
Jakub Jelen |
751cd9 |
+ return SSH_ERR_INVALID_ARGUMENT;
|
|
Jakub Jelen |
751cd9 |
+
|
|
Jakub Jelen |
751cd9 |
+ len = slen;
|
|
Jakub Jelen |
751cd9 |
+ if ((sig = malloc(slen)) == NULL) {
|
|
Jakub Jelen |
751cd9 |
+ return SSH_ERR_ALLOC_FAIL;
|
|
Jakub Jelen |
751cd9 |
+ }
|
|
Jakub Jelen |
751cd9 |
+
|
|
Jakub Jelen |
751cd9 |
+ if ((ctx = EVP_MD_CTX_new()) == NULL) {
|
|
Jakub Jelen |
751cd9 |
+ ret = SSH_ERR_ALLOC_FAIL;
|
|
Jakub Jelen |
751cd9 |
+ goto error;
|
|
Jakub Jelen |
751cd9 |
+ }
|
|
Jakub Jelen |
751cd9 |
+ if (EVP_SignInit_ex(ctx, ssh_digest_to_md(hash_alg), NULL) <= 0 ||
|
|
Jakub Jelen |
751cd9 |
+ EVP_SignUpdate(ctx, data, datalen) <= 0 ||
|
|
Jakub Jelen |
751cd9 |
+ EVP_SignFinal(ctx, sig, &len, pkey) <= 0) {
|
|
Jakub Jelen |
751cd9 |
+ ret = SSH_ERR_LIBCRYPTO_ERROR;
|
|
Jakub Jelen |
751cd9 |
+ goto error;
|
|
Jakub Jelen |
751cd9 |
+ }
|
|
Jakub Jelen |
751cd9 |
+
|
|
Jakub Jelen |
751cd9 |
+ *sigp = sig;
|
|
Jakub Jelen |
751cd9 |
+ *lenp = len;
|
|
Jakub Jelen |
751cd9 |
+ /* Now owned by the caller */
|
|
Jakub Jelen |
751cd9 |
+ sig = NULL;
|
|
Jakub Jelen |
751cd9 |
+ ret = 0;
|
|
Jakub Jelen |
751cd9 |
+
|
|
Jakub Jelen |
751cd9 |
+error:
|
|
Jakub Jelen |
751cd9 |
+ EVP_MD_CTX_free(ctx);
|
|
Jakub Jelen |
751cd9 |
+ free(sig);
|
|
Jakub Jelen |
751cd9 |
+ return ret;
|
|
Jakub Jelen |
751cd9 |
+}
|
|
Jakub Jelen |
751cd9 |
+
|
|
Jakub Jelen |
751cd9 |
+int
|
|
Jakub Jelen |
751cd9 |
+sshkey_verify_signature(EVP_PKEY *pkey, int hash_alg, const u_char *data,
|
|
Jakub Jelen |
751cd9 |
+ size_t datalen, u_char *sigbuf, int siglen)
|
|
Jakub Jelen |
751cd9 |
+{
|
|
Jakub Jelen |
751cd9 |
+ EVP_MD_CTX *ctx = NULL;
|
|
Jakub Jelen |
751cd9 |
+ int ret;
|
|
Jakub Jelen |
751cd9 |
+
|
|
Jakub Jelen |
751cd9 |
+ if ((ctx = EVP_MD_CTX_new()) == NULL) {
|
|
Jakub Jelen |
751cd9 |
+ return SSH_ERR_ALLOC_FAIL;
|
|
Jakub Jelen |
751cd9 |
+ }
|
|
Jakub Jelen |
751cd9 |
+ if (EVP_VerifyInit_ex(ctx, ssh_digest_to_md(hash_alg), NULL) <= 0 ||
|
|
Jakub Jelen |
751cd9 |
+ EVP_VerifyUpdate(ctx, data, datalen) <= 0) {
|
|
Jakub Jelen |
751cd9 |
+ ret = SSH_ERR_LIBCRYPTO_ERROR;
|
|
Jakub Jelen |
751cd9 |
+ goto done;
|
|
Jakub Jelen |
751cd9 |
+ }
|
|
Jakub Jelen |
751cd9 |
+ ret = EVP_VerifyFinal(ctx, sigbuf, siglen, pkey);
|
|
Jakub Jelen |
751cd9 |
+ switch (ret) {
|
|
Jakub Jelen |
751cd9 |
+ case 1:
|
|
Jakub Jelen |
751cd9 |
+ ret = 0;
|
|
Jakub Jelen |
751cd9 |
+ break;
|
|
Jakub Jelen |
751cd9 |
+ case 0:
|
|
Jakub Jelen |
751cd9 |
+ ret = SSH_ERR_SIGNATURE_INVALID;
|
|
Jakub Jelen |
751cd9 |
+ break;
|
|
Jakub Jelen |
751cd9 |
+ default:
|
|
Jakub Jelen |
751cd9 |
+ ret = SSH_ERR_LIBCRYPTO_ERROR;
|
|
Jakub Jelen |
751cd9 |
+ break;
|
|
Jakub Jelen |
751cd9 |
+ }
|
|
Jakub Jelen |
751cd9 |
+
|
|
Jakub Jelen |
751cd9 |
+done:
|
|
Jakub Jelen |
751cd9 |
+ EVP_MD_CTX_free(ctx);
|
|
Jakub Jelen |
751cd9 |
+ return ret;
|
|
Jakub Jelen |
751cd9 |
+}
|
|
Jakub Jelen |
751cd9 |
+
|
|
Jakub Jelen |
751cd9 |
/* XXX: these are really begging for a table-driven approach */
|
|
Jakub Jelen |
751cd9 |
int
|
|
Jakub Jelen |
751cd9 |
sshkey_curve_name_to_nid(const char *name)
|
|
Jakub Jelen |
751cd9 |
diff --git a/sshkey.h b/sshkey.h
|
|
Jakub Jelen |
751cd9 |
index a91e60436..270901a87 100644
|
|
Jakub Jelen |
751cd9 |
--- a/sshkey.h
|
|
Jakub Jelen |
751cd9 |
+++ b/sshkey.h
|
|
Jakub Jelen |
751cd9 |
@@ -179,6 +179,10 @@ const char *sshkey_ssh_name(const struct sshkey *);
|
|
Jakub Jelen |
751cd9 |
const char *sshkey_ssh_name_plain(const struct sshkey *);
|
|
Jakub Jelen |
751cd9 |
int sshkey_names_valid2(const char *, int);
|
|
Jakub Jelen |
751cd9 |
char *sshkey_alg_list(int, int, int, char);
|
|
Jakub Jelen |
751cd9 |
+int sshkey_calculate_signature(EVP_PKEY*, int, u_char **,
|
|
Jakub Jelen |
751cd9 |
+ int *, const u_char *, size_t);
|
|
Jakub Jelen |
751cd9 |
+int sshkey_verify_signature(EVP_PKEY *, int, const u_char *,
|
|
Jakub Jelen |
751cd9 |
+ size_t, u_char *, int);
|
|
Jakub Jelen |
751cd9 |
|
|
Jakub Jelen |
751cd9 |
int sshkey_from_blob(const u_char *, size_t, struct sshkey **);
|
|
Jakub Jelen |
751cd9 |
int sshkey_fromb(struct sshbuf *, struct sshkey **);
|
|
Jakub Jelen |
751cd9 |
|