From patchwork Wed Nov 28 20:06:10 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Vitaly Chikunov X-Patchwork-Id: 10703319 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id C4C5E14BD for ; Wed, 28 Nov 2018 20:07:15 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id B438D28AD9 for ; Wed, 28 Nov 2018 20:07:15 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id A53C32DD85; Wed, 28 Nov 2018 20:07:15 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.9 required=2.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 3AB7F28AD9 for ; Wed, 28 Nov 2018 20:07:15 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728654AbeK2HKD (ORCPT ); Thu, 29 Nov 2018 02:10:03 -0500 Received: from vmicros1.altlinux.org ([194.107.17.57]:51560 "EHLO vmicros1.altlinux.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726589AbeK2HKD (ORCPT ); Thu, 29 Nov 2018 02:10:03 -0500 Received: from imap.altlinux.org (imap.altlinux.org [194.107.17.38]) by vmicros1.altlinux.org (Postfix) with ESMTP id 0883B72CC59; Wed, 28 Nov 2018 23:07:13 +0300 (MSK) Received: from beacon.altlinux.org (unknown [185.6.174.98]) by imap.altlinux.org (Postfix) with ESMTPSA id D8B684A4A29; Wed, 28 Nov 2018 23:07:12 +0300 (MSK) From: Vitaly Chikunov To: Mimi Zohar , Dmitry Kasatkin , linux-integrity@vger.kernel.org Cc: Vitaly Chikunov Subject: [PATCH v2 7/7] ima-evm-utils: Try to load digest by its alias Date: Wed, 28 Nov 2018 23:06:10 +0300 Message-Id: <20181128200610.21214-7-vt@altlinux.org> X-Mailer: git-send-email 2.11.0 In-Reply-To: <20181128200610.21214-1-vt@altlinux.org> References: <20181128200610.21214-1-vt@altlinux.org> Sender: linux-integrity-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-integrity@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP For compatibility with older OpenSSL try to load digest by its alias if load by its proper name is failed. This is configured in pkey_hash_algo by mentioning loadable alias first in the comma separated list of algo names. Signed-off-by: Vitaly Chikunov --- Changes since v1: - New patch. src/imaevm.h | 1 + src/libimaevm.c | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++---- 2 files changed, 52 insertions(+), 4 deletions(-) diff --git a/src/imaevm.h b/src/imaevm.h index c81bf21..795966a 100644 --- a/src/imaevm.h +++ b/src/imaevm.h @@ -75,6 +75,7 @@ #define DATA_SIZE 4096 #define SHA1_HASH_LEN 20 +#define MAX_DIGEST_NAME 32 #define MAX_DIGEST_SIZE 64 #define MAX_SIGNATURE_SIZE 1024 diff --git a/src/libimaevm.c b/src/libimaevm.c index cb4721b..7501303 100644 --- a/src/libimaevm.c +++ b/src/libimaevm.c @@ -41,6 +41,7 @@ /* should we use logger instead for library? */ #define USE_FPRINTF +#define _GNU_SOURCE #include #include #include @@ -56,6 +57,7 @@ #include #include #include +#include #include #include "imaevm.h" @@ -70,8 +72,8 @@ const char *const pkey_hash_algo[PKEY_HASH__LAST] = { [PKEY_HASH_SHA384] = "sha384", [PKEY_HASH_SHA512] = "sha512", [PKEY_HASH_SHA224] = "sha224", - [PKEY_HASH_STREEBOG_256] = "streebog256", - [PKEY_HASH_STREEBOG_512] = "streebog512", + [PKEY_HASH_STREEBOG_256] = "md_gost12_256,streebog256", + [PKEY_HASH_STREEBOG_512] = "md_gost12_512,streebog512", }; /* @@ -284,6 +286,35 @@ static int add_dev_hash(struct stat *st, EVP_MD_CTX *ctx) return !EVP_DigestUpdate(ctx, &dev, sizeof(dev)); } +/* Call EVP_get_digestbyname with provided name and with alias, + * which is first name in the comma separated list of names + * in pkey_hash_algo. + */ +static const EVP_MD *get_digestbyname(const char *name) +{ + const EVP_MD *md; + + md = EVP_get_digestbyname(params.hash_algo); + if (!md) { + char alias[MAX_DIGEST_NAME]; + int len; + int algo_id; + const char *algo_alias; + + /* try to get algo by its alias */ + algo_id = get_hash_algo(params.hash_algo); + algo_alias = get_hash_algo_by_id(algo_id); + len = strchrnul(algo_alias, ',') - algo_alias; + if (len < sizeof(alias)) { + memcpy(alias, algo_alias, len); + alias[len] = '\0'; + if (strcmp(params.hash_algo, alias)) + md = EVP_get_digestbyname(alias); + } + } + return md; +} + int ima_calc_hash(const char *file, uint8_t *hash) { const EVP_MD *md; @@ -305,7 +336,7 @@ int ima_calc_hash(const char *file, uint8_t *hash) return err; } - md = EVP_get_digestbyname(params.hash_algo); + md = get_digestbyname(params.hash_algo); if (!md) { log_err("EVP_get_digestbyname(%s) failed\n", params.hash_algo); return 1; @@ -561,6 +592,22 @@ static int algocmp(const char *a, const char *b) return *a || *b; } +static int strmatch(const char *needle, const char *haystack) +{ + int len = strlen(needle); + const char *p = haystack; + const char *t; + + for (t = strchrnul(p, ','); *p; p = t, t = strchrnul(p, ',')) { + if (t - p == len && + !strncmp(needle, p, len)) + return 0; + if (!*t++) + break; + } + return 1; +} + int get_hash_algo(const char *algo) { int i; @@ -568,7 +615,7 @@ int get_hash_algo(const char *algo) /* first iterate over builtin algorithms */ for (i = 0; i < PKEY_HASH__LAST; i++) if (pkey_hash_algo[i] && - !strcmp(algo, pkey_hash_algo[i])) + !strmatch(algo, pkey_hash_algo[i])) return i; /* iterate over algorithms provided by kernel-headers */