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