From: Vitaly Chikunov <vt@altlinux.org>
To: Mimi Zohar <zohar@linux.vnet.ibm.com>,
Dmitry Kasatkin <dmitry.kasatkin@gmail.com>,
linux-integrity@vger.kernel.org
Subject: [PATCH v8 4/9] ima-evm-utils: Convert verify_hash_v2 and find_keyid to EVP_PKEY API
Date: Wed, 3 Jul 2019 18:50:10 +0300 [thread overview]
Message-ID: <20190703155015.14262-5-vt@altlinux.org> (raw)
In-Reply-To: <20190703155015.14262-1-vt@altlinux.org>
Rely on OpenSSL API to verify v2 signatures instead of manual PKCS1
decoding. Also, convert find_keyid() to return EVP_PKEY because
verify_hash_v2() is sole user of it.
Signed-off-by: Vitaly Chikunov <vt@altlinux.org>
---
src/libimaevm.c | 94 +++++++++++++++++++++++++++++++--------------------------
1 file changed, 52 insertions(+), 42 deletions(-)
diff --git a/src/libimaevm.c b/src/libimaevm.c
index 707b2e9..4c98cb0 100644
--- a/src/libimaevm.c
+++ b/src/libimaevm.c
@@ -452,11 +452,11 @@ struct public_key_entry {
struct public_key_entry *next;
uint32_t keyid;
char name[9];
- RSA *key;
+ EVP_PKEY *key;
};
static struct public_key_entry *public_keys = NULL;
-static RSA *find_keyid(uint32_t keyid)
+static EVP_PKEY *find_keyid(uint32_t keyid)
{
struct public_key_entry *entry;
@@ -489,13 +489,13 @@ void init_public_keys(const char *keyfiles)
break;
}
- entry->key = read_pub_key(keyfile, 1);
+ entry->key = read_pub_pkey(keyfile, 1);
if (!entry->key) {
free(entry);
continue;
}
- calc_keyid_v2(&entry->keyid, entry->name, entry->key);
+ calc_pkeyid_v2(&entry->keyid, entry->name, entry->key);
sprintf(entry->name, "%x", __be32_to_cpup(&entry->keyid));
log_info("key %d: %s %s\n", i++, entry->name, keyfile);
entry->next = public_keys;
@@ -503,14 +503,18 @@ void init_public_keys(const char *keyfiles)
}
}
+/*
+ * Return: 0 verification good, 1 verification bad, -1 error.
+ */
int verify_hash_v2(const char *file, const unsigned char *hash, int size,
unsigned char *sig, int siglen, const char *keyfile)
{
- int err, len;
- unsigned char out[1024];
- RSA *key;
+ int ret = -1;
+ EVP_PKEY *pkey, *pkey_free = NULL;
struct signature_v2_hdr *hdr = (struct signature_v2_hdr *)sig;
- const struct RSA_ASN1_template *asn1;
+ EVP_PKEY_CTX *ctx;
+ const EVP_MD *md;
+ const char *st;
if (params.verbose > LOG_INFO) {
log_info("hash: ");
@@ -518,45 +522,51 @@ int verify_hash_v2(const char *file, const unsigned char *hash, int size,
}
if (public_keys) {
- key = find_keyid(hdr->keyid);
- if (!key) {
+ pkey = find_keyid(hdr->keyid);
+ if (!pkey) {
log_err("%s: unknown keyid: %x\n", file,
__be32_to_cpup(&hdr->keyid));
return -1;
}
} else {
- key = read_pub_key(keyfile, 1);
- if (!key)
- return 1;
- }
-
-
- err = RSA_public_decrypt(siglen - sizeof(*hdr), sig + sizeof(*hdr),
- out, key, RSA_PKCS1_PADDING);
- if (err < 0) {
- log_err("%s: RSA_public_decrypt() failed: %d\n", file, err);
- return 1;
- }
-
- len = err;
-
- asn1 = &RSA_ASN1_templates[hdr->hash_algo];
-
- if (len < asn1->size || memcmp(out, asn1->data, asn1->size)) {
- log_err("%s: verification failed: %d (asn1 mismatch)\n",
- file, err);
- return -1;
- }
-
- len -= asn1->size;
-
- if (len != size || memcmp(out + asn1->size, hash, len)) {
- log_err("%s: verification failed: %d (digest mismatch)\n",
- file, err);
- return -1;
- }
-
- return 0;
+ pkey = read_pub_pkey(keyfile, 1);
+ if (!pkey)
+ return -1;
+ pkey_free = pkey;
+ }
+
+ st = "EVP_PKEY_CTX_new";
+ if (!(ctx = EVP_PKEY_CTX_new(pkey, NULL)))
+ goto err;
+ st = "EVP_PKEY_verify_init";
+ if (!EVP_PKEY_verify_init(ctx))
+ goto err;
+ st = "EVP_get_digestbyname";
+ if (!(md = EVP_get_digestbyname(params.hash_algo)))
+ goto err;
+ st = "EVP_PKEY_CTX_set_signature_md";
+ if (!EVP_PKEY_CTX_set_signature_md(ctx, md))
+ goto err;
+ st = "EVP_PKEY_verify";
+ ret = EVP_PKEY_verify(ctx, sig + sizeof(*hdr),
+ siglen - sizeof(*hdr), hash, size);
+ if (ret == 1)
+ ret = 0;
+ else if (ret == 0) {
+ log_err("%s: verification failed: %d (%s)\n",
+ file, ret, ERR_reason_error_string(ERR_get_error()));
+ ret = 1;
+ }
+err:
+ if (ret < 0 || ret > 1) {
+ log_err("%s: verification failed: %d (%s) in %s\n",
+ file, ret, ERR_reason_error_string(ERR_peek_error()),
+ st);
+ ret = -1;
+ }
+ EVP_PKEY_CTX_free(ctx);
+ EVP_PKEY_free(pkey_free);
+ return ret;
}
int get_hash_algo(const char *algo)
--
2.11.0
next prev parent reply other threads:[~2019-07-03 15:50 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2019-07-03 15:50 [PATCH v8 0/9] ima-evm-utils: Convert v2 signatures from RSA to EVP_PKEY API Vitaly Chikunov
2019-07-03 15:50 ` [PATCH v8 1/9] ima-evm-utils: Convert read_pub_key " Vitaly Chikunov
2019-07-03 15:50 ` [PATCH v8 2/9] ima-evm-utils: Convert read_priv_key " Vitaly Chikunov
2019-07-03 15:50 ` [PATCH v8 3/9] ima-evm-utils: Convert cmd_import and calc keyid v2 " Vitaly Chikunov
2019-07-03 15:50 ` Vitaly Chikunov [this message]
2019-07-03 15:50 ` [PATCH v8 5/9] ima-evm-utils: Convert sign_hash_v2 " Vitaly Chikunov
2019-07-03 15:50 ` [PATCH v8 6/9] ima-evm-utils: Replace calc_keyid_v2 with calc_pkeyid_v2 Vitaly Chikunov
2019-07-03 15:50 ` [PATCH v8 7/9] ima-evm-utils: Remove RSA_ASN1_templates Vitaly Chikunov
2019-07-03 15:50 ` [PATCH v8 8/9] ima-evm-utils: Pass status codes from sign and hash functions to the callers Vitaly Chikunov
2019-07-03 15:50 ` [PATCH v8 9/9] ima-evm-utils: Log hash_algo with hash value in verbose mode Vitaly Chikunov
2019-07-08 14:55 ` [PATCH v8 0/9] ima-evm-utils: Convert v2 signatures from RSA to EVP_PKEY API Mimi Zohar
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20190703155015.14262-5-vt@altlinux.org \
--to=vt@altlinux.org \
--cc=dmitry.kasatkin@gmail.com \
--cc=linux-integrity@vger.kernel.org \
--cc=zohar@linux.vnet.ibm.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).