From nobody Sun Nov 24 08:06:33 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1725566376; cv=none; d=zohomail.com; s=zohoarc; b=gLMo7P/MxLrg3iQvpo+U2kYtiXXVrGjDLhvbemOvdgjdSvHppQAwJfLybeJC6l3mx0qckmuHBEwh57g7RG0/qKUDe//yIXmO/koAF2Njv0XVPZ1qYqeIw/KZwHD2iI99kyknxeK5HZnrSiSQITY+8FriFJ5uPrbiFu1Qxhzmarg= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1725566376; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=GeAwo6rJ9Ssp7fNXqJixt1EPPvNs6AeUb8bCCXYa7xY=; b=mFVsrYNXhbDFimrvOcTUvBwcM6y1zxq11fSvmdW1DL9l3Lx1pMBbMnAiqSQY/7baCoETYHhh/iiTF5RZy3+hbuU3MemHcItu3mFlN47fNdvFWXNf3ISjjZjOmZJRNAon8bULYG0dxwyUNgiNctUGIR0n6hdrPl0GvMS9wejMnlc= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1725566376043124.85177750931689; Thu, 5 Sep 2024 12:59:36 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1smIcK-0006aZ-0B; Thu, 05 Sep 2024 15:58:08 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1smIcF-0006Gk-5y for qemu-devel@nongnu.org; Thu, 05 Sep 2024 15:58:04 -0400 Received: from mail-pl1-x636.google.com ([2607:f8b0:4864:20::636]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1smIcD-0005QX-Gb for qemu-devel@nongnu.org; Thu, 05 Sep 2024 15:58:02 -0400 Received: by mail-pl1-x636.google.com with SMTP id d9443c01a7336-2068acc8a4fso13378055ad.1 for ; Thu, 05 Sep 2024 12:58:01 -0700 (PDT) Received: from localhost.localdomain ([103.103.35.145]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-206aea5554dsm32031355ad.235.2024.09.05.12.57.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 05 Sep 2024 12:57:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1725566280; x=1726171080; darn=nongnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=GeAwo6rJ9Ssp7fNXqJixt1EPPvNs6AeUb8bCCXYa7xY=; b=acchDRBzTJLX9tcu5FzPzczRpRgyjNkj9QJB1+W0z+RW2BHjJV/bVWzlmNpEpmCl09 OWReFs42bB6isHHc9HNxJgplc62d+keUZYklCEbpFyYAVEVowUeJUmR4rtF964HkHMWh YkkU9zCqKevlxKNQ3hiFoSOfdH91lshZEvhDriqQ2BuK6fRJ68o5cKuZzWvjsQsaB3lp yLj9IWeddXW4jRM0Lu1ckzWKvGKAVXy5aNf1jiBHqoE1VLIngAUpxv2+uRPJJqgvAojY 0p1DhKe/fsZm5PSvUkoxpt+c6xCF/CqKimJzYjTErY0dDm8ELEDbRDevvezYGOd5EfzU 26DQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1725566280; x=1726171080; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=GeAwo6rJ9Ssp7fNXqJixt1EPPvNs6AeUb8bCCXYa7xY=; b=sYNxDVBOOTP8GlRV71O+N8LDx2MBw+LpFcvXNCJTvHSjqB/aJS3IYjBFV3sg9V2/TY Q598XlJu8gPWKjw2bFMLCuwkjjHcGxGTEWGmnxLC1dJAIBu06c8yiFNyqjWHabjIW4Ed Be/vhf4rcIT6Eu00bIgKP1dYcvKo841WjiFP5mG45idOEY1DHYjmXR8MSu32U1OdEqq3 4BcVLirQ6gnJQgNOtb/VONQHH8IxmYWiOnRqgmkqByMlNP+LHYvUOi7LjXSN51RJMWK+ KDoWVv9zV5PVn0i+CJzfZ+gXIIFZrADPNrpSEIt0I/Wae8vYwyn7i9PEHG4cOjvg3ncr wI8w== X-Gm-Message-State: AOJu0YxJi+cjSq+tvLZHS4GzuSKkRuN76XJs6t1L/a/ZUroKwaTxnu9/ 3tmuFLYs3wwqqafjpqCRTWo7qyyPgPQ8lrXO772V6ibNZj04qByZ5dJk4A== X-Google-Smtp-Source: AGHT+IGg0TfIFOSdLL4hFIcd0WGVIetWjVWBsEcowrYrwMNnTjiH+TKyHcB7CrJzD4BiI4UD0EnLpA== X-Received: by 2002:a17:903:2285:b0:205:4e15:54c8 with SMTP id d9443c01a7336-2054e15577fmr203687775ad.61.1725566279731; Thu, 05 Sep 2024 12:57:59 -0700 (PDT) From: Dorjoy Chowdhury To: qemu-devel@nongnu.org Cc: graf@amazon.com, agraf@csgraf.de, stefanha@redhat.com, pbonzini@redhat.com, slp@redhat.com, richard.henderson@linaro.org, eduardo@habkost.net, mst@redhat.com, marcel.apfelbaum@gmail.com, berrange@redhat.com, philmd@linaro.org Subject: [PATCH v6 3/8] crypto: Introduce x509 utils Date: Fri, 6 Sep 2024 01:57:30 +0600 Message-Id: <20240905195735.16911-4-dorjoychy111@gmail.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240905195735.16911-1-dorjoychy111@gmail.com> References: <20240905195735.16911-1-dorjoychy111@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=2607:f8b0:4864:20::636; envelope-from=dorjoychy111@gmail.com; helo=mail-pl1-x636.google.com X-Spam_score_int: 15 X-Spam_score: 1.5 X-Spam_bar: + X-Spam_report: (1.5 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_ENVFROM_END_DIGIT=0.25, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_SBL_CSS=3.335, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=no autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1725566377872116600 Content-Type: text/plain; charset="utf-8" An utility function for getting fingerprint from X.509 certificate has been introduced. Implementation only provided using gnutls. Signed-off-by: Dorjoy Chowdhury Reviewed-by: Philippe Mathieu-Daud=C3=A9 --- crypto/meson.build | 4 ++ crypto/x509-utils.c | 75 +++++++++++++++++++++++++++++++++++++ include/crypto/x509-utils.h | 22 +++++++++++ 3 files changed, 101 insertions(+) create mode 100644 crypto/x509-utils.c create mode 100644 include/crypto/x509-utils.h diff --git a/crypto/meson.build b/crypto/meson.build index c46f9c22a7..735635de1f 100644 --- a/crypto/meson.build +++ b/crypto/meson.build @@ -24,6 +24,10 @@ crypto_ss.add(files( 'rsakey.c', )) =20 +if gnutls.found() + crypto_ss.add(files('x509-utils.c')) +endif + if nettle.found() crypto_ss.add(nettle, files('hash-nettle.c', 'hmac-nettle.c', 'pbkdf-net= tle.c')) if hogweed.found() diff --git a/crypto/x509-utils.c b/crypto/x509-utils.c new file mode 100644 index 0000000000..593eb8968b --- /dev/null +++ b/crypto/x509-utils.c @@ -0,0 +1,75 @@ +/* + * X.509 certificate related helpers + * + * Copyright (c) 2024 Dorjoy Chowdhury + * + * This work is licensed under the terms of the GNU GPL, version 2 or + * (at your option) any later version. See the COPYING file in the + * top-level directory. + */ + +#include "qemu/osdep.h" +#include "qapi/error.h" +#include "crypto/x509-utils.h" +#include +#include +#include + +static const int qcrypto_to_gnutls_hash_alg_map[QCRYPTO_HASH_ALG__MAX] =3D= { + [QCRYPTO_HASH_ALG_MD5] =3D GNUTLS_DIG_MD5, + [QCRYPTO_HASH_ALG_SHA1] =3D GNUTLS_DIG_SHA1, + [QCRYPTO_HASH_ALG_SHA224] =3D GNUTLS_DIG_SHA224, + [QCRYPTO_HASH_ALG_SHA256] =3D GNUTLS_DIG_SHA256, + [QCRYPTO_HASH_ALG_SHA384] =3D GNUTLS_DIG_SHA384, + [QCRYPTO_HASH_ALG_SHA512] =3D GNUTLS_DIG_SHA512, + [QCRYPTO_HASH_ALG_RIPEMD160] =3D GNUTLS_DIG_RMD160, +}; + +int qcrypto_get_x509_cert_fingerprint(uint8_t *cert, size_t size, + QCryptoHashAlgorithm alg, + uint8_t *result, + size_t *resultlen, + Error **errp) +{ + int ret; + gnutls_x509_crt_t crt; + gnutls_datum_t datum =3D {.data =3D cert, .size =3D size}; + + if (alg >=3D G_N_ELEMENTS(qcrypto_to_gnutls_hash_alg_map)) { + error_setg(errp, "Unknown hash algorithm"); + return -1; + } + + if (result =3D=3D NULL) { + error_setg(errp, "No valid buffer given"); + return -1; + } + + gnutls_x509_crt_init(&crt); + + if (gnutls_x509_crt_import(crt, &datum, GNUTLS_X509_FMT_PEM) !=3D 0) { + error_setg(errp, "Failed to import certificate"); + goto cleanup; + } + + ret =3D gnutls_hash_get_len(qcrypto_to_gnutls_hash_alg_map[alg]); + if (*resultlen < ret) { + error_setg(errp, + "Result buffer size %zu is smaller than hash %d", + *resultlen, ret); + goto cleanup; + } + + if (gnutls_x509_crt_get_fingerprint(crt, + qcrypto_to_gnutls_hash_alg_map[alg= ], + result, resultlen) !=3D 0) { + error_setg(errp, "Failed to get fingerprint from certificate"); + goto cleanup; + } + + return 0; + + cleanup: + gnutls_x509_crt_deinit(crt); + return -1; +} diff --git a/include/crypto/x509-utils.h b/include/crypto/x509-utils.h new file mode 100644 index 0000000000..4210dfbcfc --- /dev/null +++ b/include/crypto/x509-utils.h @@ -0,0 +1,22 @@ +/* + * X.509 certificate related helpers + * + * Copyright (c) 2024 Dorjoy Chowdhury + * + * This work is licensed under the terms of the GNU GPL, version 2 or + * (at your option) any later version. See the COPYING file in the + * top-level directory. + */ + +#ifndef QCRYPTO_X509_UTILS_H +#define QCRYPTO_X509_UTILS_H + +#include "crypto/hash.h" + +int qcrypto_get_x509_cert_fingerprint(uint8_t *cert, size_t size, + QCryptoHashAlgorithm hash, + uint8_t *result, + size_t *resultlen, + Error **errp); + +#endif --=20 2.39.2