From nobody Sun Nov 24 06:05:18 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=1725566327; cv=none; d=zohomail.com; s=zohoarc; b=NrFXyywxcK7tTHdiqCVBpgCkqV/TWqmWxKQLIRiQCZD0S1fa8gEq3jkkhkvTR5KunbodA8UIK4ymSCu12/6UrbfZ9knQV08lhxDMFyvAQ9f3HSJFQWX41hItDipy9SjsYGLJy9rtzwlyxIkfuS1S6o0YSGCkUARj/pTrDN91fqY= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1725566327; h=Content-Type: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=J8mnf11fqMhOJ0HRRJgNLp3sCX2YE0q+64B/TUz3Pt8=; b=ebXK8BwGHazLlSXxn7UX2MTg/jVAyr0dAWGZkBpwUoU68xA77hpfv+o6GvQCVnGwrJLy4FJARm+jeZs5bgWDtP2O7gLm0JebRKwNwQozQOCVdiJeezG4DswqgvrWfSmVYETPYiAlQ4/xTrlpPVD8Wtwmn4JnEgyPypKDTD06qnM= 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 1725566327009793.6547458537325; Thu, 5 Sep 2024 12:58:47 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1smIcJ-0006Z9-HE; Thu, 05 Sep 2024 15:58:07 -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 1smIcD-0006Bc-7s for qemu-devel@nongnu.org; Thu, 05 Sep 2024 15:58:02 -0400 Received: from mail-pl1-x632.google.com ([2607:f8b0:4864:20::632]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1smIc8-0005Q2-Sw for qemu-devel@nongnu.org; Thu, 05 Sep 2024 15:58:00 -0400 Received: by mail-pl1-x632.google.com with SMTP id d9443c01a7336-20570b42f24so16611985ad.1 for ; Thu, 05 Sep 2024 12:57:53 -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.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 05 Sep 2024 12:57:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1725566272; x=1726171072; 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=J8mnf11fqMhOJ0HRRJgNLp3sCX2YE0q+64B/TUz3Pt8=; b=auhXSGLMF2v8csBs6j44cXCeozDbJ8ewJr4PWgWkQcygM8vmAuRAQ+AAIOsuF5qhoO jah7829DMkCCrv4jijvJ8/AlwJ4/Mxsbp3O9tdcvjdQSCwdiAyq8YpR+/Xjn8gjhfVQ1 0gMriHSLCJStvr6JNayGFt3prQWYkHmG5xbvlwas76sSwK4655kUEufJmH+WDpIhBIJn V625HKRej1+0dXY+RT0o8zO66qZHj6StH9Zz/qbjPrR0uHEatASjdiYvLP41qC+a2qJy Df/ordbCGlT9i9+QNhslKeSnIXZlMvRTsIWLRJxoLLP8lMV6wD07xvzDwKecUp1f00ab VTyA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1725566272; x=1726171072; 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=J8mnf11fqMhOJ0HRRJgNLp3sCX2YE0q+64B/TUz3Pt8=; b=GuawkBIA5JnwEsr72wIQ0aQQeMRhQq3Kcx2lUKTDvCkLOkqGT6BUhUqeiCKNPUy4j8 y73sPybe0BYdD3QtUQCGrk6z4cyu9+eiXWtJ3xisRFJjH1Q3jaxO5ki15VuVaxqeZ2IT pBgerBdEJrjNnRgMxtIYXbt0gwBVH1PCzB/QOT4UF9lDuPsoQH4Tc4hSRFoWZNsdz+5/ btrEp6UGAVaELeWNkk0IVdaR11Vg0BptGDeQKGZle53FY8YosTKYEFeN4EmDvdKFypgT a8vyr8+PqdL2/rn7IB8Alsgsf1FxEMWqkrskHREvm9KSmZi+Xkj7rWLZvUNb7fWXXEon DJjA== X-Gm-Message-State: AOJu0YyAaGgZfRFyIpSkLo0bL4v1mDLjA8K5ODqHcLGC15LqwKJ4w3Ev oFBnwGhhGxuc3WZ/wZCQr3ydC9PclJoFn2x7XtSug9p+xviErHymhvBkhA== X-Google-Smtp-Source: AGHT+IFgj0KrRHswL9j3aMxQ3AeikWLoriDiEUYI7hXra1UrJnBFxerhRYkYvgzbJcdGjYh2BGvHdg== X-Received: by 2002:a17:902:e950:b0:205:60d2:106b with SMTP id d9443c01a7336-206f054fe2cmr2493765ad.34.1725566271741; Thu, 05 Sep 2024 12:57:51 -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 1/8] crypto: Define macros for hash algorithm digest lengths Date: Fri, 6 Sep 2024 01:57:28 +0600 Message-Id: <20240905195735.16911-2-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-Type: text/plain; charset="utf-8" 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::632; envelope-from=dorjoychy111@gmail.com; helo=mail-pl1-x632.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: 1725566327652116600 Reviewed-by: Daniel P. Berrang=C3=A9 Signed-off-by: Dorjoy Chowdhury --- crypto/hash.c | 14 +++++++------- include/crypto/hash.h | 8 ++++++++ 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/crypto/hash.c b/crypto/hash.c index b0f8228bdc..8087f5dae6 100644 --- a/crypto/hash.c +++ b/crypto/hash.c @@ -23,13 +23,13 @@ #include "hashpriv.h" =20 static size_t qcrypto_hash_alg_size[QCRYPTO_HASH_ALG__MAX] =3D { - [QCRYPTO_HASH_ALG_MD5] =3D 16, - [QCRYPTO_HASH_ALG_SHA1] =3D 20, - [QCRYPTO_HASH_ALG_SHA224] =3D 28, - [QCRYPTO_HASH_ALG_SHA256] =3D 32, - [QCRYPTO_HASH_ALG_SHA384] =3D 48, - [QCRYPTO_HASH_ALG_SHA512] =3D 64, - [QCRYPTO_HASH_ALG_RIPEMD160] =3D 20, + [QCRYPTO_HASH_ALG_MD5] =3D QCRYPTO_HASH_DIGEST_LEN_MD5, + [QCRYPTO_HASH_ALG_SHA1] =3D QCRYPTO_HASH_DIGEST_LEN_SHA1, + [QCRYPTO_HASH_ALG_SHA224] =3D QCRYPTO_HASH_DIGEST_LEN_SHA224, + [QCRYPTO_HASH_ALG_SHA256] =3D QCRYPTO_HASH_DIGEST_LEN_SHA256, + [QCRYPTO_HASH_ALG_SHA384] =3D QCRYPTO_HASH_DIGEST_LEN_SHA384, + [QCRYPTO_HASH_ALG_SHA512] =3D QCRYPTO_HASH_DIGEST_LEN_SHA512, + [QCRYPTO_HASH_ALG_RIPEMD160] =3D QCRYPTO_HASH_DIGEST_LEN_RIPEMD160, }; =20 size_t qcrypto_hash_digest_len(QCryptoHashAlgorithm alg) diff --git a/include/crypto/hash.h b/include/crypto/hash.h index 54d87aa2a1..a113cc3b04 100644 --- a/include/crypto/hash.h +++ b/include/crypto/hash.h @@ -23,6 +23,14 @@ =20 #include "qapi/qapi-types-crypto.h" =20 +#define QCRYPTO_HASH_DIGEST_LEN_MD5 16 +#define QCRYPTO_HASH_DIGEST_LEN_SHA1 20 +#define QCRYPTO_HASH_DIGEST_LEN_SHA224 28 +#define QCRYPTO_HASH_DIGEST_LEN_SHA256 32 +#define QCRYPTO_HASH_DIGEST_LEN_SHA384 48 +#define QCRYPTO_HASH_DIGEST_LEN_SHA512 64 +#define QCRYPTO_HASH_DIGEST_LEN_RIPEMD160 20 + /* See also "QCryptoHashAlgorithm" defined in qapi/crypto.json */ =20 /** --=20 2.39.2 From nobody Sun Nov 24 06:05:18 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=1725566304; cv=none; d=zohomail.com; s=zohoarc; b=AEtdFz0h2C26YQUUJ31m9LIOZuIH9rIL7JW4QbmHl/OMw4D7eRRBsYlF1EEAUfI0WPdUvEZtw+lJ+I+RLxJq7YQ+KAJgCdsi3B15sgC5oThBs6BrZpXri8+Yft1+R0u56u5QYbbZv/RZ0SC0BpMw1TE7IHl4pZS3ffL1J6+n6U4= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1725566304; h=Content-Type: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=2cUhzbDO1c4TVr/5fHme4bDVdctPUWA406Qmu6O3nZE=; b=epLZwkNWnh+DCADt06QjrIIIrFCfOkBPrgnh4TlJeQ6kEiBt67cVltyX44XHc99ZMDRabDdQzDCEpPxpmOdmtkOSYK4sAeWZSgdYQ0NNlZ1mEWEHxONYt81qoe/qdvo9hc0lGyWb6EhsV0E7/0hytaMEEYXaBFr2X4NU47He4WA= 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 172556630445983.47074429817962; Thu, 5 Sep 2024 12:58:24 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1smIcI-0006PJ-A5; Thu, 05 Sep 2024 15:58:06 -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 1smIcD-0006Bd-EC for qemu-devel@nongnu.org; Thu, 05 Sep 2024 15:58:02 -0400 Received: from mail-pg1-x536.google.com ([2607:f8b0:4864:20::536]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1smIcB-0005QI-Nc for qemu-devel@nongnu.org; Thu, 05 Sep 2024 15:58:01 -0400 Received: by mail-pg1-x536.google.com with SMTP id 41be03b00d2f7-7d666fb3fb9so203012a12.0 for ; Thu, 05 Sep 2024 12:57:57 -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.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 05 Sep 2024 12:57:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1725566276; x=1726171076; 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=2cUhzbDO1c4TVr/5fHme4bDVdctPUWA406Qmu6O3nZE=; b=c8TjuYrJmXx0cnyrqioHdJj8HJhSWLANGgx7IBR/E9CJB08RfgXqqnMASTe4h71wSi F3eYvWvgFGxTJOWC2xndGEu/nWmv6O/fX2PbCGW3nFDkMV0BpiUDgCMB9MzEhDHmfZJM BkdWgO8x5KuXKL16BrtPwHfF6v5sQPRH8kHWYfZIoQilS/lKLy/3AvG4SjWKYhD6hUHX YgfHva6e6uVKl2oXvYDLYigLnm8CBnAsJXeQteE+WGN0bokcfUpvhVbfJwc85y5YfP8o SyduD4fuCImQj1IElSgnDXSVTPlkGB+rj9FcjZygmcjiaHd+bGEvZm+Gv38NViIhJ1el ds7A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1725566276; x=1726171076; 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=2cUhzbDO1c4TVr/5fHme4bDVdctPUWA406Qmu6O3nZE=; b=Zzms6K0x8mQJZrHBbCFMybK7ODLj+rRIeAGgy6nN6rFqKI25oxQ/TOixJkVw40OsGI 3gFoh6mKOu3w4jdRHbFH1ekfYITTGUrmcD4q6EYD7xlxYOg7tbpxgzhfq+PttDDbl7rD kLWO7qJF3bq14Otw2yn8ytZYg+KnysChYV0a2SNe43dIplGzpPrZnXuL9Hv57IG6y94K udubxS018c3pDw2BNZaINPuqwcv3hr+zYSG5ITQCs40fMJ966TzZVY0CCVk7yKdhMTBJ RZqBGTjpK31HnDmAYMxwc0CPdC3p8D71+ZD/ebTldsiSWPFs9sxbbisnXMpt9jakcbBO VnuA== X-Gm-Message-State: AOJu0YwZ45Jp5iDDwCa2CPguhRdMnXVvMaRvCNFwFGyXq6lNB+x473lR AS2ubOJ25Q4H46cSzpO0U8vG6RpmdHkjU5CbuBH2NaEFB/rOvhQpnOr84g== X-Google-Smtp-Source: AGHT+IHPH+NMSopG83F+C7LUKqyL1muBwYkK92W2GGvBm6vcfsSuRQJ6lD3BvXgXHcR0Bdt/NsAZnw== X-Received: by 2002:a17:902:f54d:b0:206:b618:1d96 with SMTP id d9443c01a7336-206ee75720fmr6211435ad.0.1725566275731; Thu, 05 Sep 2024 12:57:55 -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 2/8] crypto: Support SHA384 hash when using glib Date: Fri, 6 Sep 2024 01:57:29 +0600 Message-Id: <20240905195735.16911-3-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-Type: text/plain; charset="utf-8" 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::536; envelope-from=dorjoychy111@gmail.com; helo=mail-pg1-x536.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: 1725566305588116600 QEMU requires minimum glib version 2.66.0 as per the root meson.build file and per glib documentation[1] G_CHECKSUM_SHA384 is available since 2.51. [1] https://docs.gtk.org/glib/enum.ChecksumType.html Reviewed-by: Daniel P. Berrang=C3=A9 Signed-off-by: Dorjoy Chowdhury --- crypto/hash-glib.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crypto/hash-glib.c b/crypto/hash-glib.c index 82de9db705..18e64faa9c 100644 --- a/crypto/hash-glib.c +++ b/crypto/hash-glib.c @@ -29,7 +29,7 @@ static int qcrypto_hash_alg_map[QCRYPTO_HASH_ALG__MAX] = =3D { [QCRYPTO_HASH_ALG_SHA1] =3D G_CHECKSUM_SHA1, [QCRYPTO_HASH_ALG_SHA224] =3D -1, [QCRYPTO_HASH_ALG_SHA256] =3D G_CHECKSUM_SHA256, - [QCRYPTO_HASH_ALG_SHA384] =3D -1, + [QCRYPTO_HASH_ALG_SHA384] =3D G_CHECKSUM_SHA384, [QCRYPTO_HASH_ALG_SHA512] =3D G_CHECKSUM_SHA512, [QCRYPTO_HASH_ALG_RIPEMD160] =3D -1, }; --=20 2.39.2 From nobody Sun Nov 24 06:05:18 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 From nobody Sun Nov 24 06:05:18 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=1725566317; cv=none; d=zohomail.com; s=zohoarc; b=Mm3+zfHvEnOR1j1eehZt3H5KGFonh02tYmHSiV1YI1+PJBZ6yLaYy9TOHNsm0+0Qryu80u+O05un0AdRfl9cW37UaXB8XGQFIfeOdUaaVJXTkzPHcmB4Kin4tQ4iQjP3nSYfVF7rcPvtVPlWZcm2W/014gmOrDZ8xJC5dT62eMY= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1725566317; h=Content-Type: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=+DuOGftFxDY3qyDKXAwk5F1NE2ERbb7vqAY+mBTnz/c=; b=OhByXTdtXy0qn6R0rpFm3GrZsqzjrAdQ9/0Ae3lVFWMv6StQwS+qqADgL2VuR9a2kxY4HTNKrPG+kY18F4QmaKdNYhqRsxVeveM9QaLqOhSaLQJsUngwkpiSRxzG0LRxEQI25Lc/6AG4HvgFOMzA7QDywWGCISy5Jzj+w8guGI0= 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 1725566317644921.1317928556148; Thu, 5 Sep 2024 12:58:37 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1smIcN-0006pD-BE; Thu, 05 Sep 2024 15:58:11 -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 1smIcJ-0006bO-Ts for qemu-devel@nongnu.org; Thu, 05 Sep 2024 15:58:07 -0400 Received: from mail-pl1-x632.google.com ([2607:f8b0:4864:20::632]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1smIcH-0005RA-Jz for qemu-devel@nongnu.org; Thu, 05 Sep 2024 15:58:07 -0400 Received: by mail-pl1-x632.google.com with SMTP id d9443c01a7336-2021c08b95cso19292295ad.0 for ; Thu, 05 Sep 2024 12:58:05 -0700 (PDT) Received: from localhost.localdomain ([103.103.35.145]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-206aea5554dsm32031355ad.235.2024.09.05.12.58.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 05 Sep 2024 12:58:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1725566284; x=1726171084; 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=+DuOGftFxDY3qyDKXAwk5F1NE2ERbb7vqAY+mBTnz/c=; b=SQOqyc1wkgjJEUniUJ95LFIiHPmJEmCuAVnXVbp+eYnw5FwiknoXoLqgTfh0ayk4E7 UCsqgHEobHKQSIF9apZuQCr1tkkxKvHQdnry5yfigwsptgxuhqMiJRmme9xoIQPwkqFh ax3+YPT6/BoqB1B98wl3sNmSpNUMdK/qR8pAZ2Z0aBgN4jSPATbAOSnIezw6bNwjPli3 NJ5FJgb+9NG3mekNehF5T5gIitnUJgJSV+SQxH3ZlUG00FH5k8jiSFuRuCi+DYuJSR6N IYg9nf5KCXRFUHTGrgfKKg9KsOYcq7Qp7WXgXKuEEJNMbOirtEW8JlH+gUoRSMgXJonN ESSQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1725566284; x=1726171084; 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=+DuOGftFxDY3qyDKXAwk5F1NE2ERbb7vqAY+mBTnz/c=; b=N4muBr/HLDXqrYatlfNOxsO59Rp19515hYpEYNA8qYhIhlizGygeNHStBn7zzNyn8t qwLVOevN/DV1tPdVAqgDP5qP1hwP5V9GzmGXMqE3ydh2OGNcw7dNJ8aRWoOzHk4TS0E/ Fo0u4kJjeI5Bi9YeDF2wSLL+HA7xxfmsk/+Ct+NYZgLHlqNFsnvR0K7MwMc4KIqAqQUp JmKBMVz+urp8uWWMdsjiISlfk4DQt3w4ySG8AM9MjJwR3thIwBlgg2+j3uoPMi/cHbCa fWMyBC7Scl30bB6RaA5n3kkkiuUIKjEwxrkjtFIa6MXWmya0dBN0KU+Z6MM0YYTLn32S O6HQ== X-Gm-Message-State: AOJu0YyYS85jFfZScVU/m422xMAvXuX+6CNEQA3VrDT1BgK4WbBfOeWo DlNlZqxdEjzDigBDrxTrS3h/3tJCsEqQL5Y/sBc5glKYcndmu9YcCPZoog== X-Google-Smtp-Source: AGHT+IHQHYrzhROAHuoNY5GhwPg1+QW4LUc6hAypGRADPoyf3+ThbP+M9KlPfIjFAgwks565JRngcg== X-Received: by 2002:a17:902:dac1:b0:202:4bd9:aea5 with SMTP id d9443c01a7336-206b833f304mr126702805ad.14.1725566283762; Thu, 05 Sep 2024 12:58:03 -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 4/8] tests/lcitool: Update libvirt-ci and add libcbor dependency Date: Fri, 6 Sep 2024 01:57:31 +0600 Message-Id: <20240905195735.16911-5-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-Type: text/plain; charset="utf-8" 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::632; envelope-from=dorjoychy111@gmail.com; helo=mail-pl1-x632.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: 1725566320006116600 libcbor dependecy is necessary for adding virtio-nsm and nitro-enclave machine support in the following commits. libvirt-ci has already been updated with the dependency upstream and this commit updates libvirt-ci submodule in QEMU to latest upstream. Also the libcbor dependency has been added to tests/lcitool/projects/qemu.yml. Reviewed-by: Daniel P. Berrang=C3=A9 Signed-off-by: Dorjoy Chowdhury --- .gitlab-ci.d/cirrus/macos-13.vars | 2 +- .gitlab-ci.d/cirrus/macos-14.vars | 2 +- scripts/ci/setup/ubuntu/ubuntu-2204-aarch64.yaml | 1 + scripts/ci/setup/ubuntu/ubuntu-2204-s390x.yaml | 1 + tests/docker/dockerfiles/alpine.docker | 1 + tests/docker/dockerfiles/debian-amd64-cross.docker | 1 + tests/docker/dockerfiles/debian-arm64-cross.docker | 1 + tests/docker/dockerfiles/debian-armel-cross.docker | 1 + tests/docker/dockerfiles/debian-armhf-cross.docker | 1 + tests/docker/dockerfiles/debian-i686-cross.docker | 1 + tests/docker/dockerfiles/debian-mips64el-cross.docker | 1 + tests/docker/dockerfiles/debian-mipsel-cross.docker | 1 + tests/docker/dockerfiles/debian-ppc64el-cross.docker | 1 + tests/docker/dockerfiles/debian-s390x-cross.docker | 1 + tests/docker/dockerfiles/debian.docker | 1 + tests/docker/dockerfiles/fedora.docker | 1 + tests/docker/dockerfiles/opensuse-leap.docker | 3 ++- tests/docker/dockerfiles/ubuntu2204.docker | 1 + tests/lcitool/libvirt-ci | 2 +- tests/lcitool/projects/qemu.yml | 1 + 20 files changed, 21 insertions(+), 4 deletions(-) diff --git a/.gitlab-ci.d/cirrus/macos-13.vars b/.gitlab-ci.d/cirrus/macos-= 13.vars index ac3fa3a847..b10b7f34de 100644 --- a/.gitlab-ci.d/cirrus/macos-13.vars +++ b/.gitlab-ci.d/cirrus/macos-13.vars @@ -11,6 +11,6 @@ MAKE=3D'/opt/homebrew/bin/gmake' NINJA=3D'/opt/homebrew/bin/ninja' PACKAGING_COMMAND=3D'brew' PIP3=3D'/opt/homebrew/bin/pip3' -PKGS=3D'bash bc bison bzip2 capstone ccache cmocka ctags curl dbus diffuti= ls dtc flex gcovr gettext git glib gnu-sed gnutls gtk+3 gtk-vnc jemalloc jp= eg-turbo json-c libepoxy libffi libgcrypt libiscsi libnfs libpng libslirp l= ibssh libtasn1 libusb llvm lzo make meson mtools ncurses nettle ninja pixma= n pkg-config python3 rpm2cpio sdl2 sdl2_image snappy socat sparse spice-pro= tocol swtpm tesseract usbredir vde vte3 xorriso zlib zstd' +PKGS=3D'bash bc bison bzip2 capstone ccache cmocka ctags curl dbus diffuti= ls dtc flex gcovr gettext git glib gnu-sed gnutls gtk+3 gtk-vnc jemalloc jp= eg-turbo json-c libcbor libepoxy libffi libgcrypt libiscsi libnfs libpng li= bslirp libssh libtasn1 libusb llvm lzo make meson mtools ncurses nettle nin= ja pixman pkg-config python3 rpm2cpio sdl2 sdl2_image snappy socat sparse s= pice-protocol swtpm tesseract usbredir vde vte3 xorriso zlib zstd' PYPI_PKGS=3D'PyYAML numpy pillow sphinx sphinx-rtd-theme tomli' PYTHON=3D'/opt/homebrew/bin/python3' diff --git a/.gitlab-ci.d/cirrus/macos-14.vars b/.gitlab-ci.d/cirrus/macos-= 14.vars index 24cfec3b89..2bbab50ca0 100644 --- a/.gitlab-ci.d/cirrus/macos-14.vars +++ b/.gitlab-ci.d/cirrus/macos-14.vars @@ -11,6 +11,6 @@ MAKE=3D'/opt/homebrew/bin/gmake' NINJA=3D'/opt/homebrew/bin/ninja' PACKAGING_COMMAND=3D'brew' PIP3=3D'/opt/homebrew/bin/pip3' -PKGS=3D'bash bc bison bzip2 capstone ccache cmocka ctags curl dbus diffuti= ls dtc flex gcovr gettext git glib gnu-sed gnutls gtk+3 gtk-vnc jemalloc jp= eg-turbo json-c libepoxy libffi libgcrypt libiscsi libnfs libpng libslirp l= ibssh libtasn1 libusb llvm lzo make meson mtools ncurses nettle ninja pixma= n pkg-config python3 rpm2cpio sdl2 sdl2_image snappy socat sparse spice-pro= tocol swtpm tesseract usbredir vde vte3 xorriso zlib zstd' +PKGS=3D'bash bc bison bzip2 capstone ccache cmocka ctags curl dbus diffuti= ls dtc flex gcovr gettext git glib gnu-sed gnutls gtk+3 gtk-vnc jemalloc jp= eg-turbo json-c libcbor libepoxy libffi libgcrypt libiscsi libnfs libpng li= bslirp libssh libtasn1 libusb llvm lzo make meson mtools ncurses nettle nin= ja pixman pkg-config python3 rpm2cpio sdl2 sdl2_image snappy socat sparse s= pice-protocol swtpm tesseract usbredir vde vte3 xorriso zlib zstd' PYPI_PKGS=3D'PyYAML numpy pillow sphinx sphinx-rtd-theme tomli' PYTHON=3D'/opt/homebrew/bin/python3' diff --git a/scripts/ci/setup/ubuntu/ubuntu-2204-aarch64.yaml b/scripts/ci/= setup/ubuntu/ubuntu-2204-aarch64.yaml index 71a0f0c433..4fb3c7555f 100644 --- a/scripts/ci/setup/ubuntu/ubuntu-2204-aarch64.yaml +++ b/scripts/ci/setup/ubuntu/ubuntu-2204-aarch64.yaml @@ -35,6 +35,7 @@ packages: - libcacard-dev - libcap-ng-dev - libcapstone-dev + - libcbor-dev - libcmocka-dev - libcurl4-gnutls-dev - libdaxctl-dev diff --git a/scripts/ci/setup/ubuntu/ubuntu-2204-s390x.yaml b/scripts/ci/se= tup/ubuntu/ubuntu-2204-s390x.yaml index d8de967b18..d79399caf3 100644 --- a/scripts/ci/setup/ubuntu/ubuntu-2204-s390x.yaml +++ b/scripts/ci/setup/ubuntu/ubuntu-2204-s390x.yaml @@ -35,6 +35,7 @@ packages: - libcacard-dev - libcap-ng-dev - libcapstone-dev + - libcbor-dev - libcmocka-dev - libcurl4-gnutls-dev - libdaxctl-dev diff --git a/tests/docker/dockerfiles/alpine.docker b/tests/docker/dockerfi= les/alpine.docker index 54b9721997..a0136181d3 100644 --- a/tests/docker/dockerfiles/alpine.docker +++ b/tests/docker/dockerfiles/alpine.docker @@ -45,6 +45,7 @@ RUN apk update && \ libaio-dev \ libbpf-dev \ libcap-ng-dev \ + libcbor-dev \ libdrm-dev \ libepoxy-dev \ libffi-dev \ diff --git a/tests/docker/dockerfiles/debian-amd64-cross.docker b/tests/doc= ker/dockerfiles/debian-amd64-cross.docker index 136c3a79a1..5c5a753aca 100644 --- a/tests/docker/dockerfiles/debian-amd64-cross.docker +++ b/tests/docker/dockerfiles/debian-amd64-cross.docker @@ -92,6 +92,7 @@ RUN export DEBIAN_FRONTEND=3Dnoninteractive && \ libcacard-dev:amd64 \ libcap-ng-dev:amd64 \ libcapstone-dev:amd64 \ + libcbor-dev:amd64 \ libcmocka-dev:amd64 \ libcurl4-gnutls-dev:amd64 \ libdaxctl-dev:amd64 \ diff --git a/tests/docker/dockerfiles/debian-arm64-cross.docker b/tests/doc= ker/dockerfiles/debian-arm64-cross.docker index 233f6ee1de..7724b5f7fb 100644 --- a/tests/docker/dockerfiles/debian-arm64-cross.docker +++ b/tests/docker/dockerfiles/debian-arm64-cross.docker @@ -92,6 +92,7 @@ RUN export DEBIAN_FRONTEND=3Dnoninteractive && \ libcacard-dev:arm64 \ libcap-ng-dev:arm64 \ libcapstone-dev:arm64 \ + libcbor-dev:arm64 \ libcmocka-dev:arm64 \ libcurl4-gnutls-dev:arm64 \ libdaxctl-dev:arm64 \ diff --git a/tests/docker/dockerfiles/debian-armel-cross.docker b/tests/doc= ker/dockerfiles/debian-armel-cross.docker index 8476fc8cce..5e9c4002db 100644 --- a/tests/docker/dockerfiles/debian-armel-cross.docker +++ b/tests/docker/dockerfiles/debian-armel-cross.docker @@ -95,6 +95,7 @@ RUN export DEBIAN_FRONTEND=3Dnoninteractive && \ libcacard-dev:armel \ libcap-ng-dev:armel \ libcapstone-dev:armel \ + libcbor-dev:armel \ libcmocka-dev:armel \ libcurl4-gnutls-dev:armel \ libdaxctl-dev:armel \ diff --git a/tests/docker/dockerfiles/debian-armhf-cross.docker b/tests/doc= ker/dockerfiles/debian-armhf-cross.docker index f26385e0b9..d64c2a2cbd 100644 --- a/tests/docker/dockerfiles/debian-armhf-cross.docker +++ b/tests/docker/dockerfiles/debian-armhf-cross.docker @@ -92,6 +92,7 @@ RUN export DEBIAN_FRONTEND=3Dnoninteractive && \ libcacard-dev:armhf \ libcap-ng-dev:armhf \ libcapstone-dev:armhf \ + libcbor-dev:armhf \ libcmocka-dev:armhf \ libcurl4-gnutls-dev:armhf \ libdaxctl-dev:armhf \ diff --git a/tests/docker/dockerfiles/debian-i686-cross.docker b/tests/dock= er/dockerfiles/debian-i686-cross.docker index 3fe8ee623d..5b548c764b 100644 --- a/tests/docker/dockerfiles/debian-i686-cross.docker +++ b/tests/docker/dockerfiles/debian-i686-cross.docker @@ -95,6 +95,7 @@ RUN export DEBIAN_FRONTEND=3Dnoninteractive && \ libcacard-dev:i386 \ libcap-ng-dev:i386 \ libcapstone-dev:i386 \ + libcbor-dev:i386 \ libcmocka-dev:i386 \ libcurl4-gnutls-dev:i386 \ libdaxctl-dev:i386 \ diff --git a/tests/docker/dockerfiles/debian-mips64el-cross.docker b/tests/= docker/dockerfiles/debian-mips64el-cross.docker index 2862785692..c375921c2d 100644 --- a/tests/docker/dockerfiles/debian-mips64el-cross.docker +++ b/tests/docker/dockerfiles/debian-mips64el-cross.docker @@ -94,6 +94,7 @@ RUN export DEBIAN_FRONTEND=3Dnoninteractive && \ libcacard-dev:mips64el \ libcap-ng-dev:mips64el \ libcapstone-dev:mips64el \ + libcbor-dev:mips64el \ libcmocka-dev:mips64el \ libcurl4-gnutls-dev:mips64el \ libdaxctl-dev:mips64el \ diff --git a/tests/docker/dockerfiles/debian-mipsel-cross.docker b/tests/do= cker/dockerfiles/debian-mipsel-cross.docker index 0d559ae4ba..370f8d9c90 100644 --- a/tests/docker/dockerfiles/debian-mipsel-cross.docker +++ b/tests/docker/dockerfiles/debian-mipsel-cross.docker @@ -94,6 +94,7 @@ RUN export DEBIAN_FRONTEND=3Dnoninteractive && \ libcacard-dev:mipsel \ libcap-ng-dev:mipsel \ libcapstone-dev:mipsel \ + libcbor-dev:mipsel \ libcmocka-dev:mipsel \ libcurl4-gnutls-dev:mipsel \ libdaxctl-dev:mipsel \ diff --git a/tests/docker/dockerfiles/debian-ppc64el-cross.docker b/tests/d= ocker/dockerfiles/debian-ppc64el-cross.docker index 8c1dcec9cf..393d0300c6 100644 --- a/tests/docker/dockerfiles/debian-ppc64el-cross.docker +++ b/tests/docker/dockerfiles/debian-ppc64el-cross.docker @@ -92,6 +92,7 @@ RUN export DEBIAN_FRONTEND=3Dnoninteractive && \ libcacard-dev:ppc64el \ libcap-ng-dev:ppc64el \ libcapstone-dev:ppc64el \ + libcbor-dev:ppc64el \ libcmocka-dev:ppc64el \ libcurl4-gnutls-dev:ppc64el \ libdaxctl-dev:ppc64el \ diff --git a/tests/docker/dockerfiles/debian-s390x-cross.docker b/tests/doc= ker/dockerfiles/debian-s390x-cross.docker index 72668e0315..cbe590080d 100644 --- a/tests/docker/dockerfiles/debian-s390x-cross.docker +++ b/tests/docker/dockerfiles/debian-s390x-cross.docker @@ -92,6 +92,7 @@ RUN export DEBIAN_FRONTEND=3Dnoninteractive && \ libcacard-dev:s390x \ libcap-ng-dev:s390x \ libcapstone-dev:s390x \ + libcbor-dev:s390x \ libcmocka-dev:s390x \ libcurl4-gnutls-dev:s390x \ libdaxctl-dev:s390x \ diff --git a/tests/docker/dockerfiles/debian.docker b/tests/docker/dockerfi= les/debian.docker index 42bd0067d1..631fe6b671 100644 --- a/tests/docker/dockerfiles/debian.docker +++ b/tests/docker/dockerfiles/debian.docker @@ -41,6 +41,7 @@ RUN export DEBIAN_FRONTEND=3Dnoninteractive && \ libcacard-dev \ libcap-ng-dev \ libcapstone-dev \ + libcbor-dev \ libcmocka-dev \ libcurl4-gnutls-dev \ libdaxctl-dev \ diff --git a/tests/docker/dockerfiles/fedora.docker b/tests/docker/dockerfi= les/fedora.docker index ecdefaff1a..a01c004cfa 100644 --- a/tests/docker/dockerfiles/fedora.docker +++ b/tests/docker/dockerfiles/fedora.docker @@ -61,6 +61,7 @@ exec "$@"\n' > /usr/bin/nosync && \ libbpf-devel \ libcacard-devel \ libcap-ng-devel \ + libcbor-devel \ libcmocka-devel \ libcurl-devel \ libdrm-devel \ diff --git a/tests/docker/dockerfiles/opensuse-leap.docker b/tests/docker/d= ockerfiles/opensuse-leap.docker index 66143621fe..751919c1ec 100644 --- a/tests/docker/dockerfiles/opensuse-leap.docker +++ b/tests/docker/dockerfiles/opensuse-leap.docker @@ -4,7 +4,7 @@ # # https://gitlab.com/libvirt/libvirt-ci =20 -FROM registry.opensuse.org/opensuse/leap:15.5 +FROM registry.opensuse.org/opensuse/leap:15.6 =20 RUN zypper update -y && \ zypper install -y \ @@ -46,6 +46,7 @@ RUN zypper update -y && \ libbz2-devel \ libcacard-devel \ libcap-ng-devel \ + libcbor-devel \ libcmocka-devel \ libcurl-devel \ libdrm-devel \ diff --git a/tests/docker/dockerfiles/ubuntu2204.docker b/tests/docker/dock= erfiles/ubuntu2204.docker index 3a7de6a318..ecd5c4fd08 100644 --- a/tests/docker/dockerfiles/ubuntu2204.docker +++ b/tests/docker/dockerfiles/ubuntu2204.docker @@ -41,6 +41,7 @@ RUN export DEBIAN_FRONTEND=3Dnoninteractive && \ libcacard-dev \ libcap-ng-dev \ libcapstone-dev \ + libcbor-dev \ libcmocka-dev \ libcurl4-gnutls-dev \ libdaxctl-dev \ diff --git a/tests/lcitool/libvirt-ci b/tests/lcitool/libvirt-ci index 789b4601bc..a8253ca565 160000 --- a/tests/lcitool/libvirt-ci +++ b/tests/lcitool/libvirt-ci @@ -1 +1 @@ -Subproject commit 789b4601bce4e01f43fdb6ad4ce5ab4e46674440 +Subproject commit a8253ca5656c34c1d91f2c1170bd0cbf117358f1 diff --git a/tests/lcitool/projects/qemu.yml b/tests/lcitool/projects/qemu.= yml index 252e871f80..35878951b6 100644 --- a/tests/lcitool/projects/qemu.yml +++ b/tests/lcitool/projects/qemu.yml @@ -42,6 +42,7 @@ packages: - libc-static - libcacard - libcap-ng + - libcbor - libcurl - libdrm - libepoxy --=20 2.39.2 From nobody Sun Nov 24 06:05:18 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=1725566315; cv=none; d=zohomail.com; s=zohoarc; b=H/b3q9O4AKno15tvtn6K6wWQmIyax0W9prquMlq3St2+KmERr0OjnqxMzHdrzw1qqx5tXbmNZcS5hOSl348coqvCS3cIX6MuB0IAj98m7A9vRGwm2KLaMhKRkS0wJd74HQwaUaKSI3OPqvECcm/iMmcPhcCPT0474jhMbXy5PGM= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1725566315; h=Content-Type: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=B9GQVkRKCcWFt/FMa+Czonh0ci05P446AxEWOO+S1uE=; b=VZ/XZak+SnJh873Fv91l+bPJDufl9OZsN4ilmqoCJXcvLm2YovpQN0nQt3BCdbIA396qn2QbMSrhiguyPtweBXcnCKHelJoE0ZRpMdzMNgsDLBqT+jb0RwVMhuDlK+otO89VRCmtSGmxYdLAbbALuEWXwUTgm35N03vgH71ANAg= 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 1725566315445436.9973792810674; Thu, 5 Sep 2024 12:58:35 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1smIcS-0007Cs-Sm; Thu, 05 Sep 2024 15:58:16 -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 1smIcR-00077y-LW for qemu-devel@nongnu.org; Thu, 05 Sep 2024 15:58:15 -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 1smIcM-0005RZ-Ms for qemu-devel@nongnu.org; Thu, 05 Sep 2024 15:58:14 -0400 Received: by mail-pl1-x636.google.com with SMTP id d9443c01a7336-206e614953aso6496465ad.1 for ; Thu, 05 Sep 2024 12:58:10 -0700 (PDT) Received: from localhost.localdomain ([103.103.35.145]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-206aea5554dsm32031355ad.235.2024.09.05.12.58.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 05 Sep 2024 12:58:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1725566289; x=1726171089; 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=B9GQVkRKCcWFt/FMa+Czonh0ci05P446AxEWOO+S1uE=; b=GFlqOS9JPqVxmZFTTlIKsa3VhphxDKsu2OH7UT8mh65nf82XF3aMa7MWEizKg/Rj80 kHmYKf8P2j0G5+PjECwmpTvmc15ULFn2X/hPtY85rrv2pUPu9/vivsm6nP9Najci6KVA C8rYWH7hzFbjdHQ3u0ao66P7NkjhXWO3VWerZjD4hug0K70+ZUyIwGI2rATbbtKjvTZT vpFBlmhalxuG4Mic2wvQTuYDm6EjHWckJ8aM2jYZ5N8Vv78fjJ4lDYPAsiA9AiyrWUWM i87Lym9EA/BtLRaBMQgQXQXNIhBPUNhwraNURM8GJtSIAqgr6lZsMjzjGFWDe53cKjFj 6KkQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1725566289; x=1726171089; 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=B9GQVkRKCcWFt/FMa+Czonh0ci05P446AxEWOO+S1uE=; b=hl15R2wSBHn0+hEdxtejAvy4xOcmls0v8oFZBjvMPNqZq/jcGJTm8gkEDRnRHigNls uTG1pJAZcTyrzQrmLhfRZ0kugOT6CCZ8NmWe/iX57MmC8SG68yZR9aPap0syQqNO7RwB VXtJX9lrs67q5NIA5C743B+0DrRnKW2RbsX9/viOvfffZqFUfdx/nSYTJFcL3VfJs+eR DgGa/zjkPfOlAmUY/vNWX/zmvx389v9vKPpH4kf/XlWaFBaNjSv2WW3oUqw2KiI8ogZf XyM1a9fT1Sir+361ghCM3s0Giw/YN40xc5CfgS5imQ/wmsi4mRSyedkzZ/T/zvBgM4nz potg== X-Gm-Message-State: AOJu0YyMtVLjAXV/2CM4ARv4Bw/Dsu9I6YCefqrP+iuOJOJd8Ph2cRrd jtYlje/N6InoJBIEoy5RLoUCa1puSb2XuAU5d9UO4KUUeJjoKFlisG+pTQ== X-Google-Smtp-Source: AGHT+IEBO3qxaC382suQn/SZvolO8GeGu5R9VwAQcT7N4ZgG5Z1oUzOOj/oPK3+SXDH3X+fVQHrclg== X-Received: by 2002:a17:902:e887:b0:205:5f36:ffb1 with SMTP id d9443c01a7336-2055f370174mr168348375ad.37.1725566288343; Thu, 05 Sep 2024 12:58:08 -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 5/8] device/virtio-nsm: Support for Nitro Secure Module device Date: Fri, 6 Sep 2024 01:57:32 +0600 Message-Id: <20240905195735.16911-6-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-Type: text/plain; charset="utf-8" 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: 1725566317734116600 Nitro Secure Module (NSM)[1] device is used in AWS Nitro Enclaves[2] for stripped down TPM functionality like cryptographic attestation. The requests to and responses from NSM device are CBOR[3] encoded. This commit adds support for NSM device in QEMU. Although related to AWS Nitro Enclaves, the virito-nsm device is independent and can be used in other machine types as well. The libcbor[4] library has been used for the CBOR encoding and decoding functionalities. [1] https://lists.oasis-open.org/archives/virtio-comment/202310/msg00387.ht= ml [2] https://docs.aws.amazon.com/enclaves/latest/user/nitro-enclave.html [3] http://cbor.io/ [4] https://libcbor.readthedocs.io/en/latest/ Signed-off-by: Dorjoy Chowdhury --- MAINTAINERS | 10 + hw/virtio/Kconfig | 5 + hw/virtio/cbor-helpers.c | 326 ++++++ hw/virtio/meson.build | 6 + hw/virtio/virtio-nsm-pci.c | 73 ++ hw/virtio/virtio-nsm.c | 1665 ++++++++++++++++++++++++++++++ include/hw/virtio/cbor-helpers.h | 46 + include/hw/virtio/virtio-nsm.h | 59 ++ meson.build | 2 + 9 files changed, 2192 insertions(+) create mode 100644 hw/virtio/cbor-helpers.c create mode 100644 hw/virtio/virtio-nsm-pci.c create mode 100644 hw/virtio/virtio-nsm.c create mode 100644 include/hw/virtio/cbor-helpers.h create mode 100644 include/hw/virtio/virtio-nsm.h diff --git a/MAINTAINERS b/MAINTAINERS index c14ac014e2..b371c24747 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -2342,6 +2342,16 @@ F: include/sysemu/rng*.h F: backends/rng*.c F: tests/qtest/virtio-rng-test.c =20 +virtio-nsm +M: Alexander Graf +M: Dorjoy Chowdhury +S: Maintained +F: hw/virtio/cbor-helpers.c +F: hw/virtio/virtio-nsm.c +F: hw/virtio/virtio-nsm-pci.c +F: include/hw/virtio/cbor-helpers.h +F: include/hw/virtio/virtio-nsm.h + vhost-user-stubs M: Alex Benn=C3=A9e S: Maintained diff --git a/hw/virtio/Kconfig b/hw/virtio/Kconfig index aa63ff7fd4..29fee32035 100644 --- a/hw/virtio/Kconfig +++ b/hw/virtio/Kconfig @@ -6,6 +6,11 @@ config VIRTIO_RNG default y depends on VIRTIO =20 +config VIRTIO_NSM + bool + default y + depends on VIRTIO + config VIRTIO_IOMMU bool default y diff --git a/hw/virtio/cbor-helpers.c b/hw/virtio/cbor-helpers.c new file mode 100644 index 0000000000..a0e58d6862 --- /dev/null +++ b/hw/virtio/cbor-helpers.c @@ -0,0 +1,326 @@ +/* + * QEMU CBOR 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 "hw/virtio/cbor-helpers.h" + +bool qemu_cbor_map_add(cbor_item_t *map, cbor_item_t *key, cbor_item_t *va= lue) +{ + bool success =3D false; + struct cbor_pair pair =3D (struct cbor_pair) { + .key =3D cbor_move(key), + .value =3D cbor_move(value) + }; + + success =3D cbor_map_add(map, pair); + if (!success) { + cbor_incref(pair.key); + cbor_incref(pair.value); + } + + return success; +} + +bool qemu_cbor_array_push(cbor_item_t *array, cbor_item_t *value) +{ + bool success =3D false; + + success =3D cbor_array_push(array, cbor_move(value)); + if (!success) { + cbor_incref(value); + } + + return success; +} + +bool qemu_cbor_add_bool_to_map(cbor_item_t *map, const char *key, bool val= ue) +{ + cbor_item_t *key_cbor =3D NULL; + cbor_item_t *value_cbor =3D NULL; + + key_cbor =3D cbor_build_string(key); + if (!key_cbor) { + goto cleanup; + } + value_cbor =3D cbor_build_bool(value); + if (!value_cbor) { + goto cleanup; + } + if (!qemu_cbor_map_add(map, key_cbor, value_cbor)) { + goto cleanup; + } + + return true; + + cleanup: + if (key_cbor) { + cbor_decref(&key_cbor); + } + if (value_cbor) { + cbor_decref(&value_cbor); + } + return false; +} + +bool qemu_cbor_add_uint8_to_map(cbor_item_t *map, const char *key, + uint8_t value) +{ + cbor_item_t *key_cbor =3D NULL; + cbor_item_t *value_cbor =3D NULL; + + key_cbor =3D cbor_build_string(key); + if (!key_cbor) { + goto cleanup; + } + value_cbor =3D cbor_build_uint8(value); + if (!value_cbor) { + goto cleanup; + } + if (!qemu_cbor_map_add(map, key_cbor, value_cbor)) { + goto cleanup; + } + + return true; + + cleanup: + if (key_cbor) { + cbor_decref(&key_cbor); + } + if (value_cbor) { + cbor_decref(&value_cbor); + } + return false; +} + +bool qemu_cbor_add_map_to_map(cbor_item_t *map, const char *key, + size_t nested_map_size, + cbor_item_t **nested_map) +{ + cbor_item_t *key_cbor =3D NULL; + cbor_item_t *value_cbor =3D NULL; + + key_cbor =3D cbor_build_string(key); + if (!key_cbor) { + goto cleanup; + } + value_cbor =3D cbor_new_definite_map(nested_map_size); + if (!value_cbor) { + goto cleanup; + } + if (!qemu_cbor_map_add(map, key_cbor, value_cbor)) { + goto cleanup; + } + *nested_map =3D value_cbor; + + return true; + + cleanup: + if (key_cbor) { + cbor_decref(&key_cbor); + } + if (value_cbor) { + cbor_decref(&value_cbor); + } + return false; +} + +bool qemu_cbor_add_bytestring_to_map(cbor_item_t *map, const char *key, + uint8_t *arr, size_t len) +{ + cbor_item_t *key_cbor =3D NULL; + cbor_item_t *value_cbor =3D NULL; + + key_cbor =3D cbor_build_string(key); + if (!key_cbor) { + goto cleanup; + } + value_cbor =3D cbor_build_bytestring(arr, len); + if (!value_cbor) { + goto cleanup; + } + if (!qemu_cbor_map_add(map, key_cbor, value_cbor)) { + goto cleanup; + } + + return true; + + cleanup: + if (key_cbor) { + cbor_decref(&key_cbor); + } + if (value_cbor) { + cbor_decref(&value_cbor); + } + return false; +} + +bool qemu_cbor_add_bytestring_or_null_to_map(cbor_item_t *map, const char = *key, + uint8_t *arr, size_t len) +{ + cbor_item_t *key_cbor =3D NULL; + cbor_item_t *value_cbor =3D NULL; + + key_cbor =3D cbor_build_string(key); + if (!key_cbor) { + goto cleanup; + } + if (len) { + value_cbor =3D cbor_build_bytestring(arr, len); + } else { + value_cbor =3D cbor_new_null(); + } + if (!value_cbor) { + goto cleanup; + } + if (!qemu_cbor_map_add(map, key_cbor, value_cbor)) { + goto cleanup; + } + + return true; + + cleanup: + if (key_cbor) { + cbor_decref(&key_cbor); + } + if (value_cbor) { + cbor_decref(&value_cbor); + } + return false; +} + +bool qemu_cbor_add_string_to_map(cbor_item_t *map, const char *key, + const char *value) +{ + cbor_item_t *key_cbor =3D NULL; + cbor_item_t *value_cbor =3D NULL; + + key_cbor =3D cbor_build_string(key); + if (!key_cbor) { + goto cleanup; + } + value_cbor =3D cbor_build_string(value); + if (!value_cbor) { + goto cleanup; + } + if (!qemu_cbor_map_add(map, key_cbor, value_cbor)) { + goto cleanup; + } + + return true; + + cleanup: + if (key_cbor) { + cbor_decref(&key_cbor); + } + if (value_cbor) { + cbor_decref(&value_cbor); + } + return false; +} + +bool qemu_cbor_add_uint8_array_to_map(cbor_item_t *map, const char *key, + uint8_t *arr, size_t len) +{ + cbor_item_t *key_cbor =3D NULL; + cbor_item_t *value_cbor =3D NULL; + + key_cbor =3D cbor_build_string(key); + if (!key_cbor) { + goto cleanup; + } + value_cbor =3D cbor_new_definite_array(len); + if (!value_cbor) { + goto cleanup; + } + + for (int i =3D 0; i < len; ++i) { + cbor_item_t *tmp =3D cbor_build_uint8(arr[i]); + if (!tmp) { + goto cleanup; + } + if (!qemu_cbor_array_push(value_cbor, tmp)) { + cbor_decref(&tmp); + goto cleanup; + } + } + if (!qemu_cbor_map_add(map, key_cbor, value_cbor)) { + goto cleanup; + } + + return true; + + cleanup: + if (key_cbor) { + cbor_decref(&key_cbor); + } + if (value_cbor) { + cbor_decref(&value_cbor); + } + return false; +} + +bool qemu_cbor_add_uint8_key_bytestring_to_map(cbor_item_t *map, uint8_t k= ey, + uint8_t *buf, size_t len) +{ + cbor_item_t *key_cbor =3D NULL; + cbor_item_t *value_cbor =3D NULL; + + key_cbor =3D cbor_build_uint8(key); + if (!key_cbor) { + goto cleanup; + } + value_cbor =3D cbor_build_bytestring(buf, len); + if (!value_cbor) { + goto cleanup; + } + if (!qemu_cbor_map_add(map, key_cbor, value_cbor)) { + goto cleanup; + } + + return true; + + cleanup: + if (key_cbor) { + cbor_decref(&key_cbor); + } + if (value_cbor) { + cbor_decref(&value_cbor); + } + return false; +} + +bool qemu_cbor_add_uint64_to_map(cbor_item_t *map, const char *key, + uint64_t value) +{ + cbor_item_t *key_cbor =3D NULL; + cbor_item_t *value_cbor =3D NULL; + + key_cbor =3D cbor_build_string(key); + if (!key_cbor) { + goto cleanup; + } + value_cbor =3D cbor_build_uint64(value); + if (!value_cbor) { + goto cleanup; + } + if (!qemu_cbor_map_add(map, key_cbor, value_cbor)) { + goto cleanup; + } + + return true; + + cleanup: + if (key_cbor) { + cbor_decref(&key_cbor); + } + if (value_cbor) { + cbor_decref(&value_cbor); + } + return false; +} diff --git a/hw/virtio/meson.build b/hw/virtio/meson.build index 621fc65454..1fe7cb4d72 100644 --- a/hw/virtio/meson.build +++ b/hw/virtio/meson.build @@ -54,6 +54,9 @@ specific_virtio_ss.add(when: 'CONFIG_VIRTIO_PMEM', if_tru= e: files('virtio-pmem.c specific_virtio_ss.add(when: 'CONFIG_VHOST_VSOCK', if_true: files('vhost-v= sock.c')) specific_virtio_ss.add(when: 'CONFIG_VHOST_USER_VSOCK', if_true: files('vh= ost-user-vsock.c')) specific_virtio_ss.add(when: 'CONFIG_VIRTIO_RNG', if_true: files('virtio-r= ng.c')) +if libcbor.found() + specific_virtio_ss.add(when: 'CONFIG_VIRTIO_NSM', if_true: [files('virti= o-nsm.c', 'cbor-helpers.c'), libcbor]) +endif specific_virtio_ss.add(when: 'CONFIG_VIRTIO_MEM', if_true: files('virtio-m= em.c')) specific_virtio_ss.add(when: 'CONFIG_VHOST_USER_SCMI', if_true: files('vho= st-user-scmi.c')) specific_virtio_ss.add(when: ['CONFIG_VIRTIO_PCI', 'CONFIG_VHOST_USER_SCMI= '], if_true: files('vhost-user-scmi-pci.c')) @@ -70,6 +73,9 @@ virtio_pci_ss.add(when: 'CONFIG_VIRTIO_CRYPTO', if_true: = files('virtio-crypto-pc virtio_pci_ss.add(when: 'CONFIG_VIRTIO_INPUT_HOST', if_true: files('virtio= -input-host-pci.c')) virtio_pci_ss.add(when: 'CONFIG_VIRTIO_INPUT', if_true: files('virtio-inpu= t-pci.c')) virtio_pci_ss.add(when: 'CONFIG_VIRTIO_RNG', if_true: files('virtio-rng-pc= i.c')) +if libcbor.found() + virtio_pci_ss.add(when: 'CONFIG_VIRTIO_NSM', if_true: [files('virtio-nsm= -pci.c', 'cbor-helpers.c'), libcbor]) +endif virtio_pci_ss.add(when: 'CONFIG_VIRTIO_BALLOON', if_true: files('virtio-ba= lloon-pci.c')) virtio_pci_ss.add(when: 'CONFIG_VIRTIO_9P', if_true: files('virtio-9p-pci.= c')) virtio_pci_ss.add(when: 'CONFIG_VIRTIO_SCSI', if_true: files('virtio-scsi-= pci.c')) diff --git a/hw/virtio/virtio-nsm-pci.c b/hw/virtio/virtio-nsm-pci.c new file mode 100644 index 0000000000..dca797315a --- /dev/null +++ b/hw/virtio/virtio-nsm-pci.c @@ -0,0 +1,73 @@ +/* + * AWS Nitro Secure Module (NSM) device + * + * 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 "hw/virtio/virtio-pci.h" +#include "hw/virtio/virtio-nsm.h" +#include "hw/qdev-properties.h" +#include "qapi/error.h" +#include "qemu/module.h" +#include "qom/object.h" + +typedef struct VirtIONsmPCI VirtIONsmPCI; + +#define TYPE_VIRTIO_NSM_PCI "virtio-nsm-pci-base" +DECLARE_INSTANCE_CHECKER(VirtIONsmPCI, VIRTIO_NSM_PCI, + TYPE_VIRTIO_NSM_PCI) + +struct VirtIONsmPCI { + VirtIOPCIProxy parent_obj; + VirtIONSM vdev; +}; + +static void virtio_nsm_pci_realize(VirtIOPCIProxy *vpci_dev, Error **errp) +{ + VirtIONsmPCI *vnsm =3D VIRTIO_NSM_PCI(vpci_dev); + DeviceState *vdev =3D DEVICE(&vnsm->vdev); + + virtio_pci_force_virtio_1(vpci_dev); + + if (!qdev_realize(vdev, BUS(&vpci_dev->bus), errp)) { + return; + } +} + +static void virtio_nsm_pci_class_init(ObjectClass *klass, void *data) +{ + DeviceClass *dc =3D DEVICE_CLASS(klass); + VirtioPCIClass *k =3D VIRTIO_PCI_CLASS(klass); + + k->realize =3D virtio_nsm_pci_realize; + set_bit(DEVICE_CATEGORY_MISC, dc->categories); +} + +static void virtio_nsm_initfn(Object *obj) +{ + VirtIONsmPCI *dev =3D VIRTIO_NSM_PCI(obj); + + virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev), + TYPE_VIRTIO_NSM); +} + +static const VirtioPCIDeviceTypeInfo virtio_nsm_pci_info =3D { + .base_name =3D TYPE_VIRTIO_NSM_PCI, + .generic_name =3D "virtio-nsm-pci", + .instance_size =3D sizeof(VirtIONsmPCI), + .instance_init =3D virtio_nsm_initfn, + .class_init =3D virtio_nsm_pci_class_init, +}; + +static void virtio_nsm_pci_register(void) +{ + virtio_pci_types_register(&virtio_nsm_pci_info); +} + +type_init(virtio_nsm_pci_register) diff --git a/hw/virtio/virtio-nsm.c b/hw/virtio/virtio-nsm.c new file mode 100644 index 0000000000..0f4edd92ff --- /dev/null +++ b/hw/virtio/virtio-nsm.c @@ -0,0 +1,1665 @@ +/* + * AWS Nitro Secure Module (NSM) device + * + * 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 "qemu/iov.h" +#include "qemu/guest-random.h" +#include "qapi/error.h" + +#include "crypto/hash.h" +#include "hw/virtio/virtio.h" +#include "hw/virtio/virtio-nsm.h" +#include "hw/virtio/cbor-helpers.h" +#include "standard-headers/linux/virtio_ids.h" + +#define NSM_RESPONSE_BUF_SIZE 0x3000 +#define NSM_PCR_DATA_REQ_MAX_SIZE 512 + +enum NSMResponseTypes { + NSM_SUCCESS =3D 0, + NSM_INVALID_ARGUMENT =3D 1, + NSM_INVALID_INDEX =3D 2, + NSM_READONLY_INDEX =3D 3, + NSM_INVALID_OPERATION =3D 4, + NSM_BUFFER_TOO_SMALL =3D 5, + NSM_INPUT_TOO_LARGE =3D 6, + NSM_INTERNAL_ERROR =3D 7, +}; + +static const char *error_string(enum NSMResponseTypes type) +{ + const char *str; + switch (type) { + case NSM_INVALID_ARGUMENT: + str =3D "InvalidArgument"; + break; + case NSM_INVALID_INDEX: + str =3D "InvalidIndex"; + break; + case NSM_READONLY_INDEX: + str =3D "ReadOnlyIndex"; + break; + case NSM_INVALID_OPERATION: + str =3D "InvalidOperation"; + break; + case NSM_BUFFER_TOO_SMALL: + str =3D "BufferTooSmall"; + break; + case NSM_INPUT_TOO_LARGE: + str =3D "InputTooLarge"; + break; + default: + str =3D "InternalError"; + break; + } + + return str; +} + +/* + * Error response structure: + * + * { + * Map(1) { + * key =3D String("Error"), + * value =3D String(error_name) + * } + * } + * + * where error_name can be one of the following: + * InvalidArgument + * InvalidIndex + * InvalidResponse + * ReadOnlyIndex + * InvalidOperation + * BufferTooSmall + * InputTooLarge + * InternalError + */ + +static bool error_response(struct iovec *response, enum NSMResponseTypes e= rror, + Error **errp) +{ + cbor_item_t *root; + size_t len; + bool r =3D false; + + root =3D cbor_new_definite_map(1); + if (!root) { + goto err; + } + + if (!qemu_cbor_add_string_to_map(root, "Error", error_string(error))) { + goto err; + } + + len =3D cbor_serialize(root, response->iov_base, response->iov_len); + if (len =3D=3D 0) { + error_setg(errp, "Response buffer is small for %s response", + error_string(error)); + goto out; + } + response->iov_len =3D len; + r =3D true; + + out: + if (root) { + cbor_decref(&root); + } + return r; + + err: + error_setg(errp, "Failed to initialize %s response", error_string(erro= r)); + goto out; +} + +/* + * GetRandom response structure: + * + * { + * Map(1) { + * key =3D String("GetRandom"), + * value =3D Map(1) { + * key =3D String("random"), + * value =3D Byte_String() + * } + * } + * } + */ +static bool handle_GetRandom(VirtIONSM *vnsm, struct iovec *request, + struct iovec *response, Error **errp) +{ + cbor_item_t *root, *nested_map; + size_t len; + uint8_t rnd[64]; + bool r =3D false; + + root =3D cbor_new_definite_map(1); + if (!root) { + goto err; + } + + if (!qemu_cbor_add_map_to_map(root, "GetRandom", 1, &nested_map)) { + goto err; + } + + qemu_guest_getrandom_nofail(rnd, 64); + + if (!qemu_cbor_add_bytestring_to_map(nested_map, "random", rnd, 64)) { + goto err; + } + + len =3D cbor_serialize(root, response->iov_base, response->iov_len); + if (len =3D=3D 0) { + if (error_response(response, NSM_BUFFER_TOO_SMALL, errp)) { + r =3D true; + } + goto out; + } + + response->iov_len =3D len; + r =3D true; + + out: + if (root) { + cbor_decref(&root); + } + return r; + + err: + error_setg(errp, "Failed to initialize GetRandom response"); + goto out; +} + +/* + * DescribeNSM response structure: + * + * { + * Map(1) { + * key =3D String("DescribeNSM"), + * value =3D Map(7) { + * key =3D String("digest"), + * value =3D String("SHA384"), + * key =3D String("max_pcrs"), + * value =3D Uint8(32), + * key =3D String("module_id"), + * value =3D String("i-1234-enc5678"), + * key =3D String("locked_pcrs"), + * value =3D Array(), + * key =3D String("version_major"), + * value =3D Uint8(1), + * key =3D String("version_minor"), + * value =3D Uint8(0), + * key =3D String("version_patch"), + * value =3D Uint8(0) + * } + * } + * } + */ +static bool handle_DescribeNSM(VirtIONSM *vnsm, struct iovec *request, + struct iovec *response, Error **errp) +{ + cbor_item_t *root, *nested_map; + uint16_t locked_pcrs_cnt =3D 0; + uint8_t locked_pcrs_ind[NSM_MAX_PCRS]; + size_t len; + bool r =3D false; + + root =3D cbor_new_definite_map(1); + if (!root) { + goto err; + } + + if (!qemu_cbor_add_map_to_map(root, "DescribeNSM", 7, &nested_map)) { + goto err; + } + + if (!qemu_cbor_add_string_to_map(nested_map, "digest", vnsm->digest)) { + goto err; + } + + if (!qemu_cbor_add_uint8_to_map(nested_map, "max_pcrs", vnsm->max_pcrs= )) { + goto err; + } + + if (!qemu_cbor_add_string_to_map(nested_map, "module_id", + vnsm->module_id)) { + goto err; + } + + for (uint8_t i =3D 0; i < NSM_MAX_PCRS; ++i) { + if (vnsm->pcrs[i].locked) { + locked_pcrs_ind[locked_pcrs_cnt++] =3D i; + } + } + if (!qemu_cbor_add_uint8_array_to_map(nested_map, "locked_pcrs", + locked_pcrs_ind, locked_pcrs_cnt= )) { + goto err; + } + + if (!qemu_cbor_add_uint8_to_map(nested_map, "version_major", + vnsm->version_major)) { + goto err; + } + + if (!qemu_cbor_add_uint8_to_map(nested_map, "version_minor", + vnsm->version_minor)) { + goto err; + } + + if (!qemu_cbor_add_uint8_to_map(nested_map, "version_patch", + vnsm->version_patch)) { + goto err; + } + + len =3D cbor_serialize(root, response->iov_base, response->iov_len); + if (len =3D=3D 0) { + if (error_response(response, NSM_BUFFER_TOO_SMALL, errp)) { + r =3D true; + } + goto out; + } + + response->iov_len =3D len; + r =3D true; + + out: + if (root) { + cbor_decref(&root); + } + return r; + + err: + error_setg(errp, "Failed to initialize DescribeNSM response"); + goto out; +} + +/* + * DescribePCR request structure: + * + * { + * Map(1) { + * key =3D String("DescribePCR"), + * value =3D Map(1) { + * key =3D String("index"), + * value =3D Uint8(pcr) + * } + * } + * } + */ +typedef struct NSMDescribePCRReq { + uint8_t index; +} NSMDescribePCRReq; + +static enum NSMResponseTypes get_nsm_describe_pcr_req( + uint8_t *req, size_t len, + NSMDescribePCRReq *nsm_req) +{ + size_t size; + uint8_t *str; + struct cbor_pair *pair; + cbor_item_t *item =3D NULL; + struct cbor_load_result result; + enum NSMResponseTypes r =3D NSM_INVALID_ARGUMENT; + + item =3D cbor_load(req, len, &result); + if (!item || result.error.code !=3D CBOR_ERR_NONE) { + goto cleanup; + } + + pair =3D cbor_map_handle(item); + if (!cbor_isa_map(pair->value) || cbor_map_size(pair->value) !=3D 1) { + goto cleanup; + } + + pair =3D cbor_map_handle(pair->value); + if (!cbor_isa_string(pair->key)) { + goto cleanup; + } + + str =3D cbor_string_handle(pair->key); + size =3D cbor_string_length(pair->key); + if (!str || size !=3D 5 || memcmp(str, "index", 5) !=3D 0) { + goto cleanup; + } + if (!cbor_isa_uint(pair->value) || + cbor_int_get_width(pair->value) !=3D CBOR_INT_8) { + goto cleanup; + } + + nsm_req->index =3D cbor_get_uint8(pair->value); + r =3D NSM_SUCCESS; + goto cleanup; + + cleanup: + if (item) { + cbor_decref(&item); + } + return r; +} + +/* + * DescribePCR response structure: + * + * { + * Map(1) { + * key =3D String("DescribePCR"), + * value =3D Map(2) { + * key =3D String("data"), + * value =3D Byte_String(), + * key =3D String("lock"), + * value =3D Bool() + * } + * } + * } + */ +static bool handle_DescribePCR(VirtIONSM *vnsm, struct iovec *request, + struct iovec *response, Error **errp) +{ + cbor_item_t *root =3D NULL; + cbor_item_t *nested_map; + size_t len; + NSMDescribePCRReq nsm_req; + enum NSMResponseTypes type; + struct PCRInfo *pcr; + bool r =3D false; + + type =3D get_nsm_describe_pcr_req(request->iov_base, request->iov_len, + &nsm_req); + if (type !=3D NSM_SUCCESS) { + if (error_response(response, type, errp)) { + r =3D true; + } + goto out; + } + if (nsm_req.index >=3D vnsm->max_pcrs) { + if (error_response(response, NSM_INVALID_INDEX, errp)) { + r =3D true; + } + goto out; + } + pcr =3D &(vnsm->pcrs[nsm_req.index]); + + root =3D cbor_new_definite_map(1); + if (!root) { + goto err; + } + + if (!qemu_cbor_add_map_to_map(root, "DescribePCR", 2, &nested_map)) { + goto err; + } + + if (!qemu_cbor_add_bytestring_to_map(nested_map, "data", pcr->data, + QCRYPTO_HASH_DIGEST_LEN_SHA384)) { + goto err; + } + + if (!qemu_cbor_add_bool_to_map(nested_map, "lock", pcr->locked)) { + goto err; + } + + len =3D cbor_serialize(root, response->iov_base, response->iov_len); + if (len =3D=3D 0) { + if (error_response(response, NSM_BUFFER_TOO_SMALL, errp)) { + r =3D true; + } + goto out; + } + + response->iov_len =3D len; + r =3D true; + + out: + if (root) { + cbor_decref(&root); + } + return r; + + err: + error_setg(errp, "Failed to initialize DescribePCR response"); + goto out; +} + +/* + * ExtendPCR request structure: + * + * { + * Map(1) { + * key =3D String("ExtendPCR"), + * value =3D Map(2) { + * key =3D String("index"), + * value =3D Uint8(pcr), + * key =3D String("data"), + * value =3D Byte_String(data), + * } + * } + * } + */ +typedef struct NSMExtendPCRReq { + uint8_t index; + uint16_t data_len; + uint8_t data[NSM_PCR_DATA_REQ_MAX_SIZE]; +} NSMExtendPCRReq; + +static bool get_nsm_extend_pcr_req(uint8_t *req, size_t len, + NSMExtendPCRReq *nsm_req) +{ + cbor_item_t *item =3D NULL; + size_t size ; + uint8_t *str; + struct cbor_pair *pair; + struct cbor_load_result result; + enum NSMResponseTypes r =3D NSM_INVALID_ARGUMENT; + + item =3D cbor_load(req, len, &result); + if (!item || result.error.code !=3D CBOR_ERR_NONE) { + goto cleanup; + } + + pair =3D cbor_map_handle(item); + if (!cbor_isa_map(pair->value) || cbor_map_size(pair->value) !=3D 2) { + goto cleanup; + } + pair =3D cbor_map_handle(pair->value); + if (!cbor_isa_string(pair->key)) { + goto cleanup; + } + str =3D cbor_string_handle(pair->key); + size =3D cbor_string_length(pair->key); + if (!str || size !=3D 5 || memcmp(str, "index", 5) !=3D 0) { + goto cleanup; + } + if (!cbor_isa_uint(pair->value) || + cbor_int_get_width(pair->value) !=3D CBOR_INT_8) { + goto cleanup; + } + nsm_req->index =3D cbor_get_uint8(pair->value); + + /* let's move forward to the next key value pair */ + pair++; + if (!cbor_isa_string(pair->key)) { + goto cleanup; + } + str =3D cbor_string_handle(pair->key); + size =3D cbor_string_length(pair->key); + if (!str || size !=3D 4 || memcmp(str, "data", 4) !=3D 0) { + goto cleanup; + } + if (!cbor_isa_bytestring(pair->value)) { + goto cleanup; + } + str =3D cbor_bytestring_handle(pair->value); + size =3D cbor_bytestring_length(pair->value); + if (!str || size =3D=3D 0) { + goto cleanup; + } + if (size > NSM_PCR_DATA_REQ_MAX_SIZE) { + r =3D NSM_INPUT_TOO_LARGE; + goto cleanup; + } + + nsm_req->data_len =3D size; + memcpy(nsm_req->data, str, size); + r =3D NSM_SUCCESS; + goto cleanup; + + cleanup: + if (item) { + cbor_decref(&item); + } + return r; +} + +/* + * ExtendPCR response structure: + * + * { + * Map(1) { + * key =3D String("ExtendPCR"), + * value =3D Map(1) { + * key =3D String("data"), + * value =3D Byte_String() + * } + * } + * } + */ +static bool handle_ExtendPCR(VirtIONSM *vnsm, struct iovec *request, + struct iovec *response, Error **errp) +{ + cbor_item_t *root =3D NULL; + cbor_item_t *nested_map; + size_t len; + NSMExtendPCRReq nsm_req; + enum NSMResponseTypes type; + struct PCRInfo *pcr; + bool r =3D false; + + type =3D get_nsm_extend_pcr_req(request->iov_base, request->iov_len, + &nsm_req); + if (type !=3D NSM_SUCCESS) { + if (error_response(response, type, errp)) { + r =3D true; + } + goto out; + } + if (nsm_req.index >=3D vnsm->max_pcrs) { + if (error_response(response, NSM_INVALID_INDEX, errp)) { + r =3D true; + } + goto out; + } + + pcr =3D &(vnsm->pcrs[nsm_req.index]); + + if (pcr->locked) { + if (error_response(response, NSM_READONLY_INDEX, errp)) { + r =3D true; + } + goto out; + } + + if (!vnsm->extend_pcr(vnsm, nsm_req.index, nsm_req.data, + nsm_req.data_len)) { + if (error_response(response, NSM_INTERNAL_ERROR, errp)) { + r =3D true; + } + goto out; + } + + root =3D cbor_new_definite_map(1); + if (!root) { + goto err; + } + + if (!qemu_cbor_add_map_to_map(root, "ExtendPCR", 1, &nested_map)) { + goto err; + } + + if (!qemu_cbor_add_bytestring_to_map(nested_map, "data", pcr->data, + QCRYPTO_HASH_DIGEST_LEN_SHA384)) { + goto err; + } + + len =3D cbor_serialize(root, response->iov_base, response->iov_len); + if (len =3D=3D 0) { + if (error_response(response, NSM_BUFFER_TOO_SMALL, errp)) { + r =3D true; + } + goto out; + } + + response->iov_len =3D len; + r =3D true; + + out: + if (root) { + cbor_decref(&root); + } + return r; + + err: + error_setg(errp, "Failed to initialize DescribePCR response"); + goto out; +} + +/* + * LockPCR request structure: + * + * { + * Map(1) { + * key =3D String("LockPCR"), + * value =3D Map(1) { + * key =3D String("index"), + * value =3D Uint8(pcr) + * } + * } + * } + */ +typedef struct NSMLockPCRReq { + uint8_t index; +} NSMLockPCRReq; + +static enum NSMResponseTypes get_nsm_lock_pcr_req(uint8_t *req, size_t len, + NSMLockPCRReq *nsm_req) +{ + cbor_item_t *item =3D NULL; + size_t size; + uint8_t *str; + struct cbor_pair *pair; + struct cbor_load_result result; + enum NSMResponseTypes r =3D NSM_INVALID_ARGUMENT; + + item =3D cbor_load(req, len, &result); + if (!item || result.error.code !=3D CBOR_ERR_NONE) { + goto cleanup; + } + + pair =3D cbor_map_handle(item); + if (!cbor_isa_map(pair->value) || cbor_map_size(pair->value) !=3D 1) { + goto cleanup; + } + pair =3D cbor_map_handle(pair->value); + if (!cbor_isa_string(pair->key)) { + goto cleanup; + } + str =3D cbor_string_handle(pair->key); + size =3D cbor_string_length(pair->key); + if (!str || size !=3D 5 || memcmp(str, "index", 5) !=3D 0) { + goto cleanup; + } + if (!cbor_isa_uint(pair->value) || + cbor_int_get_width(pair->value) !=3D CBOR_INT_8) { + goto cleanup; + } + + nsm_req->index =3D cbor_get_uint8(pair->value); + r =3D NSM_SUCCESS; + goto cleanup; + + cleanup: + if (item) { + cbor_decref(&item); + } + return r; +} + +/* + * LockPCR success response structure: + * { + * String("LockPCR") + * } + */ +static bool handle_LockPCR(VirtIONSM *vnsm, struct iovec *request, + struct iovec *response, Error **errp) +{ + cbor_item_t *root =3D NULL; + size_t len; + NSMLockPCRReq nsm_req; + enum NSMResponseTypes type; + struct PCRInfo *pcr; + bool r =3D false; + + type =3D get_nsm_lock_pcr_req(request->iov_base, request->iov_len, &ns= m_req); + if (type !=3D NSM_SUCCESS) { + if (error_response(response, type, errp)) { + r =3D true; + } + goto cleanup; + } + if (nsm_req.index >=3D vnsm->max_pcrs) { + if (error_response(response, NSM_INVALID_INDEX, errp)) { + r =3D true; + } + goto cleanup; + } + + pcr =3D &(vnsm->pcrs[nsm_req.index]); + + if (pcr->locked) { + if (error_response(response, NSM_READONLY_INDEX, errp)) { + r =3D true; + } + goto cleanup; + } + + pcr->locked =3D true; + + root =3D cbor_build_string("LockPCR"); + if (!root) { + goto err; + } + + len =3D cbor_serialize(root, response->iov_base, response->iov_len); + if (len =3D=3D 0) { + if (error_response(response, NSM_BUFFER_TOO_SMALL, errp)) { + r =3D true; + } + goto cleanup; + } + + response->iov_len =3D len; + r =3D true; + goto cleanup; + + err: + error_setg(errp, "Failed to initialize LockPCR response"); + + cleanup: + if (root) { + cbor_decref(&root); + } + return r; +} + +/* + * LockPCRs request structure: + * + * { + * Map(1) { + * key =3D String("LockPCRs"), + * value =3D Map(1) { + * key =3D String("range"), + * value =3D Uint8(pcr) + * } + * } + * } + */ +typedef struct NSMLockPCRsReq { + uint16_t range; +} NSMLockPCRsReq; + +static enum NSMResponseTypes get_nsm_lock_pcrs_req(uint8_t *req, size_t le= n, + NSMLockPCRsReq *nsm_req) +{ + cbor_item_t *item =3D NULL; + size_t size; + uint8_t *str; + struct cbor_pair *pair; + struct cbor_load_result result; + enum NSMResponseTypes r =3D NSM_INVALID_ARGUMENT; + + item =3D cbor_load(req, len, &result); + if (!item || result.error.code !=3D CBOR_ERR_NONE) { + goto cleanup; + } + + pair =3D cbor_map_handle(item); + if (!cbor_isa_map(pair->value) || cbor_map_size(pair->value) !=3D 1) { + goto cleanup; + } + pair =3D cbor_map_handle(pair->value); + if (!cbor_isa_string(pair->key)) { + goto cleanup; + } + str =3D cbor_string_handle(pair->key); + size =3D cbor_string_length(pair->key); + if (!str || size !=3D 5 || memcmp(str, "range", 5) !=3D 0) { + goto cleanup; + } + if (!cbor_isa_uint(pair->value) || + cbor_int_get_width(pair->value) !=3D CBOR_INT_8) { + goto cleanup; + } + + nsm_req->range =3D cbor_get_uint8(pair->value); + r =3D NSM_SUCCESS; + goto cleanup; + + cleanup: + if (item) { + cbor_decref(&item); + } + return r; +} + +/* + * LockPCRs success response structure: + * { + * String("LockPCRs") + * } + */ +static bool handle_LockPCRs(VirtIONSM *vnsm, struct iovec *request, + struct iovec *response, Error **errp) +{ + cbor_item_t *root =3D NULL; + size_t len; + NSMLockPCRsReq nsm_req; + enum NSMResponseTypes type; + bool r =3D false; + + type =3D get_nsm_lock_pcrs_req(request->iov_base, request->iov_len, &n= sm_req); + if (type !=3D NSM_SUCCESS) { + if (error_response(response, type, errp)) { + r =3D true; + } + goto cleanup; + } + if (nsm_req.range >=3D vnsm->max_pcrs) { + if (error_response(response, NSM_INVALID_INDEX, errp)) { + r =3D true; + } + goto cleanup; + } + + for (int i =3D 0; i <=3D nsm_req.range; ++i) { + vnsm->pcrs[i].locked =3D true; + } + + root =3D cbor_build_string("LockPCRs"); + if (!root) { + goto err; + } + + len =3D cbor_serialize(root, response->iov_base, response->iov_len); + if (len =3D=3D 0) { + if (error_response(response, NSM_BUFFER_TOO_SMALL, errp)) { + r =3D true; + } + goto cleanup; + } + + response->iov_len =3D len; + r =3D true; + goto cleanup; + + err: + error_setg(errp, "Failed to initialize response"); + + cleanup: + if (root) { + cbor_decref(&root); + } + return r; +} + +/* + * Attestation request structure: + * + * Map(1) { + * key =3D String("Attestation"), + * value =3D Map(3) { + * key =3D String("user_data"), + * value =3D Byte_String() || null, + * key =3D String("nonce"), + * value =3D Byte_String() || null, + * key =3D String("public_key"), + * value =3D Byte_String() || null, + * } + * } + * } + */ +typedef struct NSMAttestationReq { + uint16_t user_data_len; + uint8_t user_data[NSM_USER_DATA_MAX_SIZE]; + + uint16_t nonce_len; + uint8_t nonce[NSM_NONCE_MAX_SIZE]; + + uint16_t public_key_len; + uint8_t public_key[NSM_PUBLIC_KEY_MAX_SIZE]; +} NSMAttestationReq; + +static enum NSMResponseTypes get_nsm_attestation_req(uint8_t *req, size_t = len, + NSMAttestationReq *ns= m_req) +{ + cbor_item_t *item =3D NULL; + size_t size; + uint8_t *str; + struct cbor_pair *pair; + struct cbor_load_result result; + enum NSMResponseTypes r =3D NSM_INVALID_ARGUMENT; + + item =3D cbor_load(req, len, &result); + if (!item || result.error.code !=3D CBOR_ERR_NONE) { + goto cleanup; + } + + pair =3D cbor_map_handle(item); + if (!cbor_isa_map(pair->value) || cbor_map_size(pair->value) !=3D 3) { + goto cleanup; + } + pair =3D cbor_map_handle(pair->value); + if (!cbor_isa_string(pair->key)) { + goto cleanup; + } + str =3D cbor_string_handle(pair->key); + size =3D cbor_string_length(pair->key); + if (!str || size !=3D 9 || memcmp(str, "user_data", 9) !=3D 0) { + goto cleanup; + } + + if (cbor_isa_bytestring(pair->value)) { + str =3D cbor_bytestring_handle(pair->value); + size =3D cbor_bytestring_length(pair->value); + if (!str || size =3D=3D 0) { + goto cleanup; + } + if (size > NSM_USER_DATA_MAX_SIZE) { + r =3D NSM_INPUT_TOO_LARGE; + goto cleanup; + } + memcpy(nsm_req->user_data, str, size); + nsm_req->user_data_len =3D size; + } else if (cbor_is_null(pair->value)) { + nsm_req->user_data_len =3D 0; + } else { + goto cleanup; + } + + /* let's move forward */ + pair++; + if (!cbor_isa_string(pair->key)) { + goto cleanup; + } + str =3D cbor_string_handle(pair->key); + size =3D cbor_string_length(pair->key); + if (!str || size !=3D 5 || memcmp(str, "nonce", 5) !=3D 0) { + goto cleanup; + } + + if (cbor_isa_bytestring(pair->value)) { + str =3D cbor_bytestring_handle(pair->value); + size =3D cbor_bytestring_length(pair->value); + if (!str || size =3D=3D 0) { + goto cleanup; + } + if (size > NSM_NONCE_MAX_SIZE) { + r =3D NSM_INPUT_TOO_LARGE; + goto cleanup; + } + memcpy(nsm_req->nonce, str, size); + nsm_req->nonce_len =3D size; + } else if (cbor_is_null(pair->value)) { + nsm_req->nonce_len =3D 0; + } else { + goto cleanup; + } + + /* let's move forward */ + pair++; + if (!cbor_isa_string(pair->key)) { + goto cleanup; + } + str =3D cbor_string_handle(pair->key); + size =3D cbor_string_length(pair->key); + if (!str || size !=3D 10 || memcmp(str, "public_key", 10) !=3D 0) { + goto cleanup; + } + + if (cbor_isa_bytestring(pair->value)) { + str =3D cbor_bytestring_handle(pair->value); + size =3D cbor_bytestring_length(pair->value); + if (!str || size =3D=3D 0) { + goto cleanup; + } + if (size > NSM_PUBLIC_KEY_MAX_SIZE) { + r =3D NSM_INPUT_TOO_LARGE; + goto cleanup; + } + memcpy(nsm_req->public_key, str, size); + nsm_req->public_key_len =3D size; + } else if (cbor_is_null(pair->value)) { + nsm_req->public_key_len =3D 0; + } else { + goto cleanup; + } + + r =3D NSM_SUCCESS; + + cleanup: + if (item) { + cbor_decref(&item); + } + return r; +} + +static bool add_protected_header_to_cose(cbor_item_t *cose) +{ + cbor_item_t *map =3D NULL; + cbor_item_t *key =3D NULL; + cbor_item_t *value =3D NULL; + cbor_item_t *bs =3D NULL; + size_t len; + bool r =3D false; + size_t buf_len =3D 4096; + g_autofree uint8_t *buf =3D g_malloc(buf_len); + + map =3D cbor_new_definite_map(1); + if (!map) { + goto cleanup; + } + key =3D cbor_build_uint8(1); + if (!key) { + goto cleanup; + } + value =3D cbor_new_int8(); + if (!value) { + goto cleanup; + } + cbor_mark_negint(value); + /* we don't actually sign the data, so we use -1 as the 'alg' value */ + cbor_set_uint8(value, 0); + + if (!qemu_cbor_map_add(map, key, value)) { + goto cleanup; + } + + len =3D cbor_serialize(map, buf, buf_len); + if (len =3D=3D 0) { + goto cleanup_map; + } + + bs =3D cbor_build_bytestring(buf, len); + if (!bs) { + goto cleanup_map; + } + if (!qemu_cbor_array_push(cose, bs)) { + cbor_decref(&bs); + goto cleanup_map; + } + r =3D true; + goto cleanup_map; + + cleanup: + if (key) { + cbor_decref(&key); + } + if (value) { + cbor_decref(&value); + } + + cleanup_map: + if (map) { + cbor_decref(&map); + } + return r; +} + +static bool add_unprotected_header_to_cose(cbor_item_t *cose) +{ + cbor_item_t *map =3D cbor_new_definite_map(0); + if (!map) { + goto cleanup; + } + if (!qemu_cbor_array_push(cose, map)) { + goto cleanup; + } + + return true; + + cleanup: + if (map) { + cbor_decref(&map); + } + return false; +} + +static bool add_ca_bundle_to_payload(cbor_item_t *map) +{ + cbor_item_t *key_cbor =3D NULL; + cbor_item_t *value_cbor =3D NULL; + cbor_item_t *bs =3D NULL; + uint8_t zero[64] =3D {0}; + + key_cbor =3D cbor_build_string("cabundle"); + if (!key_cbor) { + goto cleanup; + } + value_cbor =3D cbor_new_definite_array(1); + if (!value_cbor) { + goto cleanup; + } + bs =3D cbor_build_bytestring(zero, 64); + if (!bs) { + goto cleanup; + } + if (!qemu_cbor_array_push(value_cbor, bs)) { + cbor_decref(&bs); + goto cleanup; + } + if (!qemu_cbor_map_add(map, key_cbor, value_cbor)) { + goto cleanup; + } + + return true; + + cleanup: + if (key_cbor) { + cbor_decref(&key_cbor); + } + if (value_cbor) { + cbor_decref(&value_cbor); + } + return false; +} + +static bool add_payload_to_cose(cbor_item_t *cose, VirtIONSM *vnsm) +{ + cbor_item_t *root =3D NULL; + cbor_item_t *nested_map; + cbor_item_t *bs =3D NULL; + size_t locked_cnt; + uint8_t ind[NSM_MAX_PCRS]; + size_t payload_map_size =3D 8; + size_t len; + struct PCRInfo *pcr; + uint8_t zero[64] =3D {0}; + bool r =3D false; + size_t buf_len =3D 16384; + g_autofree uint8_t *buf =3D g_malloc(buf_len); + + if (vnsm->public_key_len > 0) { + payload_map_size++; + } + + root =3D cbor_new_definite_map(payload_map_size); + if (!root) { + goto cleanup; + } + if (!qemu_cbor_add_string_to_map(root, "module_id", vnsm->module_id)) { + goto cleanup; + } + if (!qemu_cbor_add_string_to_map(root, "digest", vnsm->digest)) { + goto cleanup; + } + if (!qemu_cbor_add_uint64_to_map(root, "timestamp", + (uint64_t) time(NULL) * 1000)) { + goto cleanup; + } + + locked_cnt =3D 0; + for (uint8_t i =3D 0; i < NSM_MAX_PCRS; ++i) { + if (vnsm->pcrs[i].locked) { + ind[locked_cnt++] =3D i; + } + } + if (!qemu_cbor_add_map_to_map(root, "pcrs", locked_cnt, &nested_map)) { + goto cleanup; + } + for (uint8_t i =3D 0; i < locked_cnt; ++i) { + pcr =3D &(vnsm->pcrs[ind[i]]); + if (!qemu_cbor_add_uint8_key_bytestring_to_map( + nested_map, ind[i], + pcr->data, + QCRYPTO_HASH_DIGEST_LEN_SHA384)) { + goto cleanup; + } + } + if (!qemu_cbor_add_bytestring_to_map(root, "certificate", zero, 64)) { + goto cleanup; + } + if (!add_ca_bundle_to_payload(root)) { + goto cleanup; + } + if (vnsm->public_key_len > 0 && + !qemu_cbor_add_bytestring_to_map(root, "public_key", vnsm->public_= key, + vnsm->public_key_len)) { + goto cleanup; + } + if (!qemu_cbor_add_bytestring_or_null_to_map(root, "user_data", + vnsm->user_data, + vnsm->user_data_len)) { + goto cleanup; + } + if (!qemu_cbor_add_bytestring_or_null_to_map(root, "nonce", vnsm->nonc= e, + vnsm->nonce_len)) { + goto cleanup; + } + len =3D cbor_serialize(root, buf, buf_len); + if (len =3D=3D 0) { + goto cleanup; + } + + bs =3D cbor_build_bytestring(buf, len); + if (!bs) { + goto cleanup; + } + if (!qemu_cbor_array_push(cose, bs)) { + cbor_decref(&bs); + goto cleanup; + } + + r =3D true; + + cleanup: + if (root) { + cbor_decref(&root); + } + return r; +} + +static bool add_signature_to_cose(cbor_item_t *cose) +{ + cbor_item_t *bs =3D NULL; + uint8_t zero[64] =3D {0}; + + /* we don't actually sign the data, so we just put 64 zero bytes */ + bs =3D cbor_build_bytestring(zero, 64); + if (!bs) { + goto cleanup; + } + + if (!qemu_cbor_array_push(cose, bs)) { + goto cleanup; + } + + return true; + + cleanup: + if (bs) { + cbor_decref(&bs); + } + return false; +} + +/* + * Attestation response structure: + * + * { + * Map(1) { + * key =3D String("Attestation"), + * value =3D Map(1) { + * key =3D String("document"), + * value =3D Byte_String() + * } + * } + * } + * + * The document is a serialized COSE sign1 blob of the structure: + * { + * Array(4) { + * [0] { ByteString() }, // serialized protected header + * [1] { Map(0) }, // 0 length map + * [2] { ByteString() }, // serialized payload + * [3] { ByteString() }, // signature + * } + * } + * + * where [0] protected header is a serialized CBOR blob of the structure: + * { + * Map(1) { + * key =3D Uint8(1) // alg + * value =3D NegativeInt8() // Signing algorithm + * } + * } + * + * [2] payload is serialized CBOR blob of the structure: + * { + * Map(9/8/7) { + * [0] { key =3D String("module_id"), value =3D String(module_id) }, + * [1] { key =3D String("digest"), value =3D String("SHA384") }, + * [2] { + * key =3D String("timestamp"), + * value =3D Uint64(unix epoch of when document was created) + * }, + * [3] { + * key =3D String("pcrs"), + * value =3D Map(locked_pcr_cnt) { + * key =3D Uint8(pcr_index), + * value =3D ByteString(pcr_data) + * }, + * }, + * [4] { + * key =3D String("certificate"), + * value =3D ByteString(Signing certificate) + * }, + * [5] { key =3D String("cabundle"), value =3D Array(N) { ByteString()= ... } }, + * [6] { key =3D String("public_key"), value =3D ByteString() }, // op= tional + * [7] { key =3D String("user_data"), value =3D ByteString() }, // op= tional + * [8] { key =3D String("nonce"), value =3D ByteString() }, // op= tional + * } + * } + */ +static bool handle_Attestation(VirtIONSM *vnsm, struct iovec *request, + struct iovec *response, Error **errp) +{ + cbor_item_t *root =3D NULL; + cbor_item_t *cose =3D NULL; + cbor_item_t *nested_map; + size_t len; + NSMAttestationReq nsm_req; + enum NSMResponseTypes type; + bool r =3D false; + size_t buf_len =3D 16384; + g_autofree uint8_t *buf =3D g_malloc(buf_len); + + type =3D get_nsm_attestation_req(request->iov_base, request->iov_len, + &nsm_req); + if (type !=3D NSM_SUCCESS) { + if (error_response(response, type, errp)) { + r =3D true; + } + goto out; + } + + cose =3D cbor_new_definite_array(4); + if (!cose) { + goto err; + } + if (!add_protected_header_to_cose(cose)) { + goto err; + } + if (!add_unprotected_header_to_cose(cose)) { + goto err; + } + + if (nsm_req.public_key_len > 0) { + memcpy(vnsm->public_key, nsm_req.public_key, nsm_req.public_key_le= n); + vnsm->public_key_len =3D nsm_req.public_key_len; + } + if (nsm_req.user_data_len > 0) { + memcpy(vnsm->user_data, nsm_req.user_data, nsm_req.user_data_len); + vnsm->user_data_len =3D nsm_req.user_data_len; + } + if (nsm_req.nonce_len > 0) { + memcpy(vnsm->nonce, nsm_req.nonce, nsm_req.nonce_len); + vnsm->nonce_len =3D nsm_req.nonce_len; + } + + if (!add_payload_to_cose(cose, vnsm)) { + goto err; + } + + if (!add_signature_to_cose(cose)) { + goto err; + } + + len =3D cbor_serialize(cose, buf, buf_len); + if (len =3D=3D 0) { + goto err; + } + + root =3D cbor_new_definite_map(1); + if (!root) { + goto err; + } + if (!qemu_cbor_add_map_to_map(root, "Attestation", 1, &nested_map)) { + goto err; + } + if (!qemu_cbor_add_bytestring_to_map(nested_map, "document", buf, len)= ) { + goto err; + } + + len =3D cbor_serialize(root, response->iov_base, response->iov_len); + if (len =3D=3D 0) { + if (error_response(response, NSM_BUFFER_TOO_SMALL, errp)) { + r =3D true; + } + goto out; + } + + response->iov_len =3D len; + r =3D true; + + out: + if (root) { + cbor_decref(&root); + } + if (cose) { + cbor_decref(&cose); + } + return r; + + err: + error_setg(errp, "Failed to initialize Attestation response"); + goto out; +} + +enum CBOR_ROOT_TYPE { + CBOR_ROOT_TYPE_STRING =3D 0, + CBOR_ROOT_TYPE_MAP =3D 1, +}; + +struct nsm_cmd { + char name[16]; + /* + * There are 2 types of request + * 1) String(); "GetRandom", "DescribeNSM" + * 2) Map(1) { key: String(), value: ... } + */ + enum CBOR_ROOT_TYPE root_type; + bool (*response_fn)(VirtIONSM *vnsm, struct iovec *request, + struct iovec *response, Error **errp); +}; + +const struct nsm_cmd nsm_cmds[] =3D { + { "GetRandom", CBOR_ROOT_TYPE_STRING, handle_GetRandom }, + { "DescribeNSM", CBOR_ROOT_TYPE_STRING, handle_DescribeNSM }, + { "DescribePCR", CBOR_ROOT_TYPE_MAP, handle_DescribePCR }, + { "ExtendPCR", CBOR_ROOT_TYPE_MAP, handle_ExtendPCR }, + { "LockPCR", CBOR_ROOT_TYPE_MAP, handle_LockPCR }, + { "LockPCRs", CBOR_ROOT_TYPE_MAP, handle_LockPCRs }, + { "Attestation", CBOR_ROOT_TYPE_MAP, handle_Attestation }, +}; + +static const struct nsm_cmd *get_nsm_request_cmd(uint8_t *buf, size_t len) +{ + size_t size; + uint8_t *req; + enum CBOR_ROOT_TYPE root_type; + struct cbor_load_result result; + cbor_item_t *item =3D cbor_load(buf, len, &result); + if (!item || result.error.code !=3D CBOR_ERR_NONE) { + goto cleanup; + } + + if (cbor_isa_string(item)) { + size =3D cbor_string_length(item); + req =3D cbor_string_handle(item); + root_type =3D CBOR_ROOT_TYPE_STRING; + } else if (cbor_isa_map(item) && cbor_map_size(item) =3D=3D 1) { + struct cbor_pair *handle =3D cbor_map_handle(item); + if (cbor_isa_string(handle->key)) { + size =3D cbor_string_length(handle->key); + req =3D cbor_string_handle(handle->key); + root_type =3D CBOR_ROOT_TYPE_MAP; + } else { + goto cleanup; + } + } else { + goto cleanup; + } + + if (size =3D=3D 0 || req =3D=3D NULL) { + goto cleanup; + } + + for (int i =3D 0; i < ARRAY_SIZE(nsm_cmds); ++i) { + if (nsm_cmds[i].root_type =3D=3D root_type && + strlen(nsm_cmds[i].name) =3D=3D size && + memcmp(nsm_cmds[i].name, req, size) =3D=3D 0) { + cbor_decref(&item); + return &nsm_cmds[i]; + } + } + + cleanup: + if (item) { + cbor_decref(&item); + } + return NULL; +} + +static bool get_nsm_request_response(VirtIONSM *vnsm, struct iovec *req, + struct iovec *resp, Error **errp) +{ + const struct nsm_cmd *cmd; + + cmd =3D get_nsm_request_cmd(req->iov_base, req->iov_len); + + if (cmd =3D=3D NULL) { + if (error_response(resp, NSM_INVALID_OPERATION, errp)) { + return true; + } + error_setg(errp, "Failed to initialize InvalidOperation response"); + return false; + } + + return cmd->response_fn(vnsm, req, resp, errp); +} + +static void handle_input(VirtIODevice *vdev, VirtQueue *vq) +{ + g_autofree VirtQueueElement *out_elem =3D NULL; + g_autofree VirtQueueElement *in_elem =3D NULL; + VirtIONSM *vnsm =3D VIRTIO_NSM(vdev); + Error *err =3D NULL; + size_t sz; + struct iovec req =3D {.iov_base =3D NULL, .iov_len =3D 0}; + struct iovec res =3D {.iov_base =3D NULL, .iov_len =3D 0}; + + out_elem =3D virtqueue_pop(vq, sizeof(VirtQueueElement)); + if (!out_elem) { + /* nothing in virtqueue */ + return; + } + + sz =3D iov_size(out_elem->out_sg, out_elem->out_num); + if (sz =3D=3D 0) { + virtio_error(vdev, "Expected non-zero sized request buffer in " + "virtqueue"); + goto cleanup; + } + + in_elem =3D virtqueue_pop(vq, sizeof(VirtQueueElement)); + if (!in_elem) { + virtio_error(vdev, "Expected response buffer after request buffer " + "in virtqueue"); + goto cleanup; + } + if (iov_size(in_elem->in_sg, in_elem->in_num) !=3D NSM_RESPONSE_BUF_SI= ZE) { + virtio_error(vdev, "Expected response buffer of length 0x3000"); + goto cleanup; + } + + req.iov_base =3D g_malloc(sz); + req.iov_len =3D iov_to_buf(out_elem->out_sg, out_elem->out_num, 0, + req.iov_base, sz); + if (req.iov_len !=3D sz) { + virtio_error(vdev, "Failed to copy request buffer"); + goto cleanup; + } + + res.iov_base =3D g_malloc(NSM_RESPONSE_BUF_SIZE); + res.iov_len =3D NSM_RESPONSE_BUF_SIZE; + + if (!get_nsm_request_response(vnsm, &req, &res, &err)) { + error_report_err(err); + virtio_error(vdev, "Failed to get NSM request response"); + goto cleanup; + } + + sz =3D iov_from_buf(in_elem->in_sg, in_elem->in_num, 0, res.iov_base, + res.iov_len); + if (sz !=3D res.iov_len) { + virtio_error(vdev, "Failed to copy response buffer"); + goto cleanup; + } + + g_free(req.iov_base); + g_free(res.iov_base); + virtqueue_push(vq, out_elem, 0); + virtqueue_push(vq, in_elem, in_elem->in_sg->iov_len); + virtio_notify(vdev, vq); + return; + + cleanup: + g_free(req.iov_base); + g_free(res.iov_base); + if (out_elem) { + virtqueue_detach_element(vq, out_elem, 0); + } + if (in_elem) { + virtqueue_detach_element(vq, in_elem, 0); + } + return; +} + +static uint64_t get_features(VirtIODevice *vdev, uint64_t f, Error **errp) +{ + return f; +} + +static bool extend_pcr(VirtIONSM *vnsm, int ind, uint8_t *data, uint16_t l= en) +{ + Error *err =3D NULL; + struct PCRInfo *pcr =3D &(vnsm->pcrs[ind]); + size_t digest_len =3D QCRYPTO_HASH_DIGEST_LEN_SHA384; + uint8_t result[QCRYPTO_HASH_DIGEST_LEN_SHA384]; + uint8_t *ptr =3D result; + struct iovec iov[2] =3D { + { .iov_base =3D pcr->data, .iov_len =3D QCRYPTO_HASH_DIGEST_LEN_SH= A384 }, + { .iov_base =3D data, .iov_len =3D len }, + }; + + if (qcrypto_hash_bytesv(QCRYPTO_HASH_ALG_SHA384, iov, 2, &ptr, &digest= _len, + &err) < 0) { + return false; + } + + memcpy(pcr->data, result, QCRYPTO_HASH_DIGEST_LEN_SHA384); + return true; +} + +static void lock_pcr(VirtIONSM *vnsm, int ind) +{ + vnsm->pcrs[ind].locked =3D true; +} + +static void virtio_nsm_device_realize(DeviceState *dev, Error **errp) +{ + VirtIODevice *vdev =3D VIRTIO_DEVICE(dev); + VirtIONSM *vnsm =3D VIRTIO_NSM(dev); + + vnsm->max_pcrs =3D NSM_MAX_PCRS; + vnsm->digest =3D (char *) "SHA384"; + if (vnsm->module_id =3D=3D NULL) { + vnsm->module_id =3D (char *) "i-234-enc5678"; + } + vnsm->version_major =3D 1; + vnsm->version_minor =3D 0; + vnsm->version_patch =3D 0; + vnsm->extend_pcr =3D extend_pcr; + vnsm->lock_pcr =3D lock_pcr; + + virtio_init(vdev, VIRTIO_ID_NITRO_SEC_MOD, 0); + + vnsm->vq =3D virtio_add_queue(vdev, 2, handle_input); +} + +static void virtio_nsm_device_unrealize(DeviceState *dev) +{ + VirtIODevice *vdev =3D VIRTIO_DEVICE(dev); + + virtio_del_queue(vdev, 0); + virtio_cleanup(vdev); +} + +static const VMStateDescription vmstate_virtio_nsm =3D { + .name =3D "virtio-nsm", + .minimum_version_id =3D 1, + .version_id =3D 1, + .fields =3D (const VMStateField[]) { + VMSTATE_VIRTIO_DEVICE, + VMSTATE_END_OF_LIST() + }, +}; + +static Property virtio_nsm_properties[] =3D { + DEFINE_PROP_STRING("module-id", VirtIONSM, module_id), + DEFINE_PROP_END_OF_LIST(), +}; + +static void virtio_nsm_class_init(ObjectClass *klass, void *data) +{ + DeviceClass *dc =3D DEVICE_CLASS(klass); + VirtioDeviceClass *vdc =3D VIRTIO_DEVICE_CLASS(klass); + + device_class_set_props(dc, virtio_nsm_properties); + dc->vmsd =3D &vmstate_virtio_nsm; + set_bit(DEVICE_CATEGORY_MISC, dc->categories); + vdc->realize =3D virtio_nsm_device_realize; + vdc->unrealize =3D virtio_nsm_device_unrealize; + vdc->get_features =3D get_features; +} + +static const TypeInfo virtio_nsm_info =3D { + .name =3D TYPE_VIRTIO_NSM, + .parent =3D TYPE_VIRTIO_DEVICE, + .instance_size =3D sizeof(VirtIONSM), + .class_init =3D virtio_nsm_class_init, +}; + +static void virtio_register_types(void) +{ + type_register_static(&virtio_nsm_info); +} + +type_init(virtio_register_types) diff --git a/include/hw/virtio/cbor-helpers.h b/include/hw/virtio/cbor-help= ers.h new file mode 100644 index 0000000000..fe3a2077fb --- /dev/null +++ b/include/hw/virtio/cbor-helpers.h @@ -0,0 +1,46 @@ +/* + * QEMU CBOR 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 QEMU_VIRTIO_CBOR_HELPERS_H +#define QEMU_VIRTIO_CBOR_HELPERS_H + +#include + +bool qemu_cbor_map_add(cbor_item_t *map, cbor_item_t *key, cbor_item_t *va= lue); + +bool qemu_cbor_array_push(cbor_item_t *array, cbor_item_t *value); + +bool qemu_cbor_add_bool_to_map(cbor_item_t *map, const char *key, bool val= ue); + +bool qemu_cbor_add_uint8_to_map(cbor_item_t *map, const char *key, + uint8_t value); + +bool qemu_cbor_add_map_to_map(cbor_item_t *map, const char *key, + size_t nested_map_size, + cbor_item_t **nested_map); + +bool qemu_cbor_add_bytestring_to_map(cbor_item_t *map, const char *key, + uint8_t *arr, size_t len); + +bool qemu_cbor_add_bytestring_or_null_to_map(cbor_item_t *map, const char = *key, + uint8_t *arr, size_t len); + +bool qemu_cbor_add_string_to_map(cbor_item_t *map, const char *key, + const char *value); + +bool qemu_cbor_add_uint8_array_to_map(cbor_item_t *map, const char *key, + uint8_t *arr, size_t len); + +bool qemu_cbor_add_uint8_key_bytestring_to_map(cbor_item_t *map, uint8_t k= ey, + uint8_t *buf, size_t len); + +bool qemu_cbor_add_uint64_to_map(cbor_item_t *map, const char *key, + uint64_t value); +#endif diff --git a/include/hw/virtio/virtio-nsm.h b/include/hw/virtio/virtio-nsm.h new file mode 100644 index 0000000000..fdeb6fd815 --- /dev/null +++ b/include/hw/virtio/virtio-nsm.h @@ -0,0 +1,59 @@ +/* + * AWS Nitro Secure Module (NSM) device + * + * 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 QEMU_VIRTIO_NSM_H +#define QEMU_VIRTIO_NSM_H + +#include "crypto/hash.h" +#include "hw/virtio/virtio.h" +#include "qom/object.h" + +#define NSM_MAX_PCRS 32 +#define NSM_USER_DATA_MAX_SIZE 512 +#define NSM_NONCE_MAX_SIZE 512 +#define NSM_PUBLIC_KEY_MAX_SIZE 1024 + +#define TYPE_VIRTIO_NSM "virtio-nsm-device" +OBJECT_DECLARE_SIMPLE_TYPE(VirtIONSM, VIRTIO_NSM) +#define VIRTIO_NSM_GET_PARENT_CLASS(obj) \ + OBJECT_GET_PARENT_CLASS(obj, TYPE_VIRTIO_NSM) + +struct PCRInfo { + bool locked; + uint8_t data[QCRYPTO_HASH_DIGEST_LEN_SHA384]; +}; + +struct VirtIONSM { + VirtIODevice parent_obj; + + /* Only one vq - guest puts request and response buffers on it */ + VirtQueue *vq; + + /* NSM State */ + uint16_t max_pcrs; + struct PCRInfo pcrs[NSM_MAX_PCRS]; + char *digest; + char *module_id; + uint8_t version_major; + uint8_t version_minor; + uint8_t version_patch; + + uint16_t public_key_len; + uint8_t public_key[NSM_PUBLIC_KEY_MAX_SIZE]; + uint16_t user_data_len; + uint8_t user_data[NSM_USER_DATA_MAX_SIZE]; + uint16_t nonce_len; + uint8_t nonce[NSM_NONCE_MAX_SIZE]; + + bool (*extend_pcr)(VirtIONSM *vnsm, int ind, uint8_t *data, uint16_t l= en); + void (*lock_pcr)(VirtIONSM *vnsm, int ind); +}; + +#endif diff --git a/meson.build b/meson.build index fbda17c987..b78a833803 100644 --- a/meson.build +++ b/meson.build @@ -1661,6 +1661,8 @@ if (have_system or have_tools) and (virgl.found() or = opengl.found()) endif have_vhost_user_gpu =3D have_vhost_user_gpu and virgl.found() and opengl.f= ound() and gbm.found() =20 +libcbor =3D dependency('libcbor', version: '>=3D0.7.0', required: false) + gnutls =3D not_found gnutls_crypto =3D not_found if get_option('gnutls').enabled() or (get_option('gnutls').auto() and have= _system) --=20 2.39.2 From nobody Sun Nov 24 06:05:18 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=1725566364; cv=none; d=zohomail.com; s=zohoarc; b=DPbYqgqZ8Xmr5e0us/TLCUiZmji7VHwARqwJIaVw4u9ddhvx02+iep94IMfWxcbVxanBG+djFh/t/FaPhAniDBj1feqnG5I2igHKFsTyyqFZaAs7W8/sEGawDopZwdRXhTZJ5zNeVk8Fcqatz+e8zT7RSw75x/c5r0s1uR/9wpA= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1725566364; 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=hhMjTsQKStT+/SjSYAuvmvPjXZgsrUDCW4DUN02r8L0=; b=lvzhsOLjObM/R827BRzrPI5zeFGIMqI256pC67JAkyfzm0nWoXO5VedZ2OzgP8IQMLVx+jEgDRjtsu266kUf4FT0kPGj3E2DdmRbNkeP6oo/Gd6n2payk6HNtorzNEDmoJZ2D2KjE/ETHfLvIfrwhx9jdDPX6HZEoJ0HW1+kDB8= 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 1725566364491328.5176252335766; Thu, 5 Sep 2024 12:59:24 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1smIcV-0007SE-OV; Thu, 05 Sep 2024 15:58:19 -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 1smIcT-0007FE-C5 for qemu-devel@nongnu.org; Thu, 05 Sep 2024 15:58:17 -0400 Received: from mail-pg1-x534.google.com ([2607:f8b0:4864:20::534]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1smIcQ-0005Rm-EI for qemu-devel@nongnu.org; Thu, 05 Sep 2024 15:58:17 -0400 Received: by mail-pg1-x534.google.com with SMTP id 41be03b00d2f7-6c5bcb8e8edso929962a12.2 for ; Thu, 05 Sep 2024 12:58:14 -0700 (PDT) Received: from localhost.localdomain ([103.103.35.145]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-206aea5554dsm32031355ad.235.2024.09.05.12.58.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 05 Sep 2024 12:58:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1725566293; x=1726171093; 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=hhMjTsQKStT+/SjSYAuvmvPjXZgsrUDCW4DUN02r8L0=; b=dJsRXjMAEhHp8XfSKRMBn31IrsBmTT6QePZzCqvlAyC3WMDR7UM6JkHN4qt987qrw8 HVzzuRhcfD8MrsXmLyA8bQYNIMBRhg8zKfrNKVQpV2dXshJI1lwDs0P+lW3X/ooau0q7 +i1h7xG5BrJkfTtlNDB8nWYgpY3eBzqDXmd68gR4mHsm7/aOYCq/a8+6A8HpZQIiMVnb IS6yTwQFG0XOVDk59hnfNJpRLa6/ih34RcGGe2xvPQdfmBYZE2bVi8eoaYjAfm+Y6wNA tuN67QIl/BTDVH0UphAoQ7IuOHF9iYyDXGTCqfmATWptjBzi3eBBawcV2JwV8HLrIU0d aRuA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1725566293; x=1726171093; 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=hhMjTsQKStT+/SjSYAuvmvPjXZgsrUDCW4DUN02r8L0=; b=Dt5f0KxCOk2SqJlRWdchbnb7ELHB93GxUU0VqrVkyu/0Oh1m5wW3iIrNzahr7yLyKK a9wkvnb5dBRgJevSGaY4wBsOz4+9UXrU6Bwgc3fWiDK425d7J+Ty3dB3eKmvRV2CJUYE UZ+kIMt807YVJ/Mlgb+3WJT89o5UfFgDmEklYxQ5RTiA+xpIK4I8L3FPWe9ogU2/qPXH Qyyh7F3+Xbj3nRXRUrfWFq1yFjt2gjXf5nhER1lKL15nNMrBms1hWJ7uC4fKz854jTJx sThetvcTKnRMQfMJlJyR2z7xDVEXL91bdfsA2ggP+KKRdLEg8R0ve64GociT/OX82VHB 7LHQ== X-Gm-Message-State: AOJu0Yz4Z5P29+SvDnpWwPuPrzqiTEyT0t9XJanf3gn48+NstjDRz8Wi C4+4cSUA5GEkMXEHsmdv5CeAR5C9SFurH1wNxlyeFEsNbVzeD+FtlrMPWg== X-Google-Smtp-Source: AGHT+IGQEXDmZXvp872iB16qvAQGzz29Mnt1mlnWDSrPl2Q9d0Xx8qy5hCaTYleLNQHXwOW+ZNfLcw== X-Received: by 2002:a17:903:230a:b0:206:c486:4c33 with SMTP id d9443c01a7336-206f0522330mr2409635ad.30.1725566292496; Thu, 05 Sep 2024 12:58:12 -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 6/8] hw/core: Add Enclave Image Format (EIF) related helpers Date: Fri, 6 Sep 2024 01:57:33 +0600 Message-Id: <20240905195735.16911-7-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::534; envelope-from=dorjoychy111@gmail.com; helo=mail-pg1-x534.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: 1725566366060116600 Content-Type: text/plain; charset="utf-8" An EIF (Enclave Image Format)[1] file is used to boot an AWS nitro enclave[2] virtual machine. The EIF file contains the necessary kernel, cmdline, ramdisk(s) sections to boot. Some helper functions have been introduced for extracting the necessary sections from an EIF file and then writing them to temporary files as well as computing SHA384 hashes from the section data. These will be used in the following commit to add support for nitro-enclave machine type in QEMU. The files added in this commit are not compiled yet but will be added to the hw/core/meson.build file in the following commit where CONFIG_NITRO_ENCLAVE will be introduced. [1] https://github.com/aws/aws-nitro-enclaves-image-format [2] https://docs.aws.amazon.com/enclaves/latest/user/nitro-enclave.html Signed-off-by: Dorjoy Chowdhury --- MAINTAINERS | 7 + hw/core/eif.c | 719 ++++++++++++++++++++++++++++++++++++++++++++++++++ hw/core/eif.h | 22 ++ 3 files changed, 748 insertions(+) create mode 100644 hw/core/eif.c create mode 100644 hw/core/eif.h diff --git a/MAINTAINERS b/MAINTAINERS index b371c24747..ead98e645a 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -1879,6 +1879,13 @@ F: hw/i386/microvm.c F: include/hw/i386/microvm.h F: pc-bios/bios-microvm.bin =20 +nitro-enclave +M: Alexander Graf +M: Dorjoy Chowdhury +S: Maintained +F: hw/core/eif.c +F: hw/core/eif.h + Machine core M: Eduardo Habkost M: Marcel Apfelbaum diff --git a/hw/core/eif.c b/hw/core/eif.c new file mode 100644 index 0000000000..2e0759e18c --- /dev/null +++ b/hw/core/eif.c @@ -0,0 +1,719 @@ +/* + * EIF (Enclave Image Format) 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 "qemu/bswap.h" +#include "qapi/error.h" +#include "crypto/hash.h" +#include "crypto/x509-utils.h" +#include /* for crc32 */ +#include + +#include "hw/core/eif.h" + +#define MAX_SECTIONS 32 + +/* members are ordered according to field order in .eif file */ +typedef struct EifHeader { + uint8_t magic[4]; /* must be .eif in ascii i.e., [46, 101, 105, 102] = */ + uint16_t version; + uint16_t flags; + uint64_t default_memory; + uint64_t default_cpus; + uint16_t reserved; + uint16_t section_cnt; + uint64_t section_offsets[MAX_SECTIONS]; + uint64_t section_sizes[MAX_SECTIONS]; + uint32_t unused; + uint32_t eif_crc32; +} QEMU_PACKED EifHeader; + +/* members are ordered according to field order in .eif file */ +typedef struct EifSectionHeader { + /* + * 0 =3D invalid, 1 =3D kernel, 2 =3D cmdline, 3 =3D ramdisk, 4 =3D si= gnature, + * 5 =3D metadata + */ + uint16_t section_type; + uint16_t flags; + uint64_t section_size; +} QEMU_PACKED EifSectionHeader; + +enum EifSectionTypes { + EIF_SECTION_INVALID =3D 0, + EIF_SECTION_KERNEL =3D 1, + EIF_SECTION_CMDLINE =3D 2, + EIF_SECTION_RAMDISK =3D 3, + EIF_SECTION_SIGNATURE =3D 4, + EIF_SECTION_METADATA =3D 5, + EIF_SECTION_MAX =3D 6, +}; + +static const char *section_type_to_string(uint16_t type) +{ + const char *str; + switch (type) { + case EIF_SECTION_INVALID: + str =3D "invalid"; + break; + case EIF_SECTION_KERNEL: + str =3D "kernel"; + break; + case EIF_SECTION_CMDLINE: + str =3D "cmdline"; + break; + case EIF_SECTION_RAMDISK: + str =3D "ramdisk"; + break; + case EIF_SECTION_SIGNATURE: + str =3D "signature"; + break; + case EIF_SECTION_METADATA: + str =3D "metadata"; + break; + default: + str =3D "unknown"; + break; + } + + return str; +} + +static bool read_eif_header(FILE *f, EifHeader *header, uint32_t *crc, + Error **errp) +{ + size_t got; + size_t header_size =3D sizeof(*header); + + got =3D fread(header, 1, header_size, f); + if (got !=3D header_size) { + error_setg(errp, "Failed to read EIF header"); + return false; + } + + if (memcmp(header->magic, ".eif", 4) !=3D 0) { + error_setg(errp, "Invalid EIF image. Magic mismatch."); + return false; + } + + /* Exclude header->eif_crc32 field from CRC calculation */ + *crc =3D crc32(*crc, (uint8_t *)header, header_size - 4); + + header->version =3D be16_to_cpu(header->version); + header->flags =3D be16_to_cpu(header->flags); + header->default_memory =3D be64_to_cpu(header->default_memory); + header->default_cpus =3D be64_to_cpu(header->default_cpus); + header->reserved =3D be16_to_cpu(header->reserved); + header->section_cnt =3D be16_to_cpu(header->section_cnt); + + for (int i =3D 0; i < MAX_SECTIONS; ++i) { + header->section_offsets[i] =3D be64_to_cpu(header->section_offsets= [i]); + } + + for (int i =3D 0; i < MAX_SECTIONS; ++i) { + header->section_sizes[i] =3D be64_to_cpu(header->section_sizes[i]); + } + + header->unused =3D be32_to_cpu(header->unused); + header->eif_crc32 =3D be32_to_cpu(header->eif_crc32); + return true; +} + +static bool read_eif_section_header(FILE *f, EifSectionHeader *section_hea= der, + uint32_t *crc, Error **errp) +{ + size_t got; + size_t section_header_size =3D sizeof(*section_header); + + got =3D fread(section_header, 1, section_header_size, f); + if (got !=3D section_header_size) { + error_setg(errp, "Failed to read EIF section header"); + return false; + } + + *crc =3D crc32(*crc, (uint8_t *)section_header, section_header_size); + + section_header->section_type =3D be16_to_cpu(section_header->section_t= ype); + section_header->flags =3D be16_to_cpu(section_header->flags); + section_header->section_size =3D be64_to_cpu(section_header->section_s= ize); + return true; +} + +/* + * Upon success, the caller is responsible for unlinking and freeing *tmp_= path. + */ +static bool get_tmp_file(const char *template, char **tmp_path, Error **er= rp) +{ + int tmp_fd; + + *tmp_path =3D NULL; + tmp_fd =3D g_file_open_tmp(template, tmp_path, NULL); + if (tmp_fd < 0 || *tmp_path =3D=3D NULL) { + error_setg(errp, "Failed to create temporary file for template %s", + template); + return false; + } + + close(tmp_fd); + return true; +} + +static void safe_fclose(FILE *f) +{ + if (f) { + fclose(f); + } +} + +static void safe_unlink(char *f) +{ + if (f) { + unlink(f); + } +} + +/* + * Upon success, the caller is reponsible for unlinking and freeing *kerne= l_path + */ +static bool read_eif_kernel(FILE *f, uint64_t size, char **kernel_path, + uint8_t *kernel, uint32_t *crc, Error **errp) +{ + size_t got; + FILE *tmp_file =3D NULL; + + *kernel_path =3D NULL; + if (!get_tmp_file("eif-kernel-XXXXXX", kernel_path, errp)) { + goto cleanup; + } + + tmp_file =3D fopen(*kernel_path, "wb"); + if (tmp_file =3D=3D NULL) { + error_setg_errno(errp, errno, "Failed to open temporary file %s", + *kernel_path); + goto cleanup; + } + + got =3D fread(kernel, 1, size, f); + if ((uint64_t) got !=3D size) { + error_setg(errp, "Failed to read EIF kernel section data"); + goto cleanup; + } + + got =3D fwrite(kernel, 1, size, tmp_file); + if ((uint64_t) got !=3D size) { + error_setg(errp, "Failed to write EIF kernel section data to tempo= rary" + " file"); + goto cleanup; + } + + *crc =3D crc32(*crc, kernel, size); + fclose(tmp_file); + + return true; + + cleanup: + safe_fclose(tmp_file); + + safe_unlink(*kernel_path); + g_free(*kernel_path); + *kernel_path =3D NULL; + + return false; +} + +static bool read_eif_cmdline(FILE *f, uint64_t size, char *cmdline, + uint32_t *crc, Error **errp) +{ + size_t got =3D fread(cmdline, 1, size, f); + if ((uint64_t) got !=3D size) { + error_setg(errp, "Failed to read EIF cmdline section data"); + return false; + } + + *crc =3D crc32(*crc, (uint8_t *)cmdline, size); + return true; +} + +static bool read_eif_ramdisk(FILE *eif, FILE *initrd, uint64_t size, + uint8_t *ramdisk, uint32_t *crc, Error **errp) +{ + size_t got; + + got =3D fread(ramdisk, 1, size, eif); + if ((uint64_t) got !=3D size) { + error_setg(errp, "Failed to read EIF ramdisk section data"); + return false; + } + + got =3D fwrite(ramdisk, 1, size, initrd); + if ((uint64_t) got !=3D size) { + error_setg(errp, "Failed to write EIF ramdisk data to temporary fi= le"); + return false; + } + + *crc =3D crc32(*crc, ramdisk, size); + return true; +} + +static bool get_signature_fingerprint_sha384(FILE *eif, uint64_t size, + uint8_t *sha384, + uint32_t *crc, + Error **errp) +{ + size_t got; + g_autofree uint8_t *sig =3D NULL; + g_autofree uint8_t *cert =3D NULL; + cbor_item_t *item =3D NULL; + cbor_item_t *pcr0 =3D NULL; + size_t len; + size_t hash_len =3D QCRYPTO_HASH_DIGEST_LEN_SHA384; + struct cbor_pair *pair; + struct cbor_load_result result; + bool ret =3D false; + + sig =3D g_malloc(size); + got =3D fread(sig, 1, size, eif); + if ((uint64_t) got !=3D size) { + error_setg(errp, "Failed to read EIF signature section data"); + goto cleanup; + } + + *crc =3D crc32(*crc, sig, size); + + item =3D cbor_load(sig, size, &result); + if (!item || result.error.code !=3D CBOR_ERR_NONE) { + error_setg(errp, "Failed to load signature section data as CBOR"); + goto cleanup; + } + if (!cbor_isa_array(item) || cbor_array_size(item) < 1) { + error_setg(errp, "Invalid signature CBOR"); + goto cleanup; + } + pcr0 =3D cbor_array_get(item, 0); + if (!pcr0) { + error_setg(errp, "Failed to get PCR0 signature"); + goto cleanup; + } + if (!cbor_isa_map(pcr0) || cbor_map_size(pcr0) !=3D 2) { + error_setg(errp, "Invalid signature CBOR"); + goto cleanup; + } + pair =3D cbor_map_handle(pcr0); + if (!cbor_isa_string(pair->key) || cbor_string_length(pair->key) !=3D = 19 || + memcmp(cbor_string_handle(pair->key), "signing_certificate", 19) != =3D 0) { + error_setg(errp, "Invalid signautre CBOR"); + goto cleanup; + } + if (!cbor_isa_array(pair->value)) { + error_setg(errp, "Invalid signature CBOR"); + goto cleanup; + } + len =3D cbor_array_size(pair->value); + if (len =3D=3D 0) { + error_setg(errp, "Invalid signature CBOR"); + goto cleanup; + } + cert =3D g_malloc(len); + for (int i =3D 0; i < len; ++i) { + cbor_item_t *tmp =3D cbor_array_get(pair->value, i); + if (!tmp) { + error_setg(errp, "Invalid signature CBOR"); + goto cleanup; + } + if (!cbor_isa_uint(tmp) || cbor_int_get_width(tmp) !=3D CBOR_INT_8= ) { + cbor_decref(&tmp); + error_setg(errp, "Invalid signature CBOR"); + goto cleanup; + } + cert[i] =3D cbor_get_uint8(tmp); + cbor_decref(&tmp); + } + + if (qcrypto_get_x509_cert_fingerprint(cert, len, QCRYPTO_HASH_ALG_SHA3= 84, + sha384, &hash_len, errp)) { + goto cleanup; + } + + ret =3D true; + + cleanup: + if (pcr0) { + cbor_decref(&pcr0); + } + if (item) { + cbor_decref(&item); + } + return ret; +} + +/* Expects file to have offset 0 before this function is called */ +static long get_file_size(FILE *f, Error **errp) +{ + long size; + + if (fseek(f, 0, SEEK_END) !=3D 0) { + error_setg_errno(errp, errno, "Failed to seek to the end of file"); + return -1; + } + + size =3D ftell(f); + if (size =3D=3D -1) { + error_setg_errno(errp, errno, "Failed to get offset"); + return -1; + } + + if (fseek(f, 0, SEEK_SET) !=3D 0) { + error_setg_errno(errp, errno, "Failed to seek back to the start"); + return -1; + } + + return size; +} + +static bool get_SHA384_digest(GList *list, uint8_t *digest, Error **errp) +{ + size_t digest_len =3D QCRYPTO_HASH_DIGEST_LEN_SHA384; + size_t list_len =3D g_list_length(list); + struct iovec *iovec_list =3D g_new0(struct iovec, list_len); + bool ret =3D true; + GList *l; + int i; + + for (i =3D 0, l =3D list; l !=3D NULL; l =3D l->next, i++) { + iovec_list[i] =3D *(struct iovec *) l->data; + } + + if (qcrypto_hash_bytesv(QCRYPTO_HASH_ALG_SHA384, iovec_list, list_len, + &digest, &digest_len, errp) < 0) { + ret =3D false; + } + + g_free(iovec_list); + return ret; +} + +static void free_iovec(struct iovec *iov) +{ + if (iov) { + g_free(iov->iov_base); + g_free(iov); + } +} + +/* + * Upon success, the caller is reponsible for unlinking and freeing + * *kernel_path, *initrd_path and freeing *cmdline. + */ +bool read_eif_file(const char *eif_path, const char *machine_initrd, + char **kernel_path, char **initrd_path, char **cmdline, + uint8_t *image_sha384, uint8_t *bootstrap_sha384, + uint8_t *app_sha384, uint8_t *fingerprint_sha384, + bool *signature_found, Error **errp) +{ + FILE *f =3D NULL; + FILE *machine_initrd_f =3D NULL; + FILE *initrd_path_f =3D NULL; + long machine_initrd_size; + uint32_t crc =3D 0; + EifHeader eif_header; + bool seen_sections[EIF_SECTION_MAX] =3D {false}; + /* kernel + ramdisks + cmdline sha384 hash */ + GList *iov_PCR0 =3D NULL; + /* kernel + boot ramdisk + cmdline sha384 hash */ + GList *iov_PCR1 =3D NULL; + /* application ramdisk(s) hash */ + GList *iov_PCR2 =3D NULL; + uint8_t *ptr =3D NULL; + struct iovec *iov_ptr =3D NULL; + + *signature_found =3D false; + *kernel_path =3D *initrd_path =3D *cmdline =3D NULL; + + f =3D fopen(eif_path, "rb"); + if (f =3D=3D NULL) { + error_setg_errno(errp, errno, "Failed to open %s", eif_path); + goto cleanup; + } + + if (!read_eif_header(f, &eif_header, &crc, errp)) { + goto cleanup; + } + + if (eif_header.version < 4) { + error_setg(errp, "Expected EIF version 4 or greater"); + goto cleanup; + } + + if (eif_header.flags !=3D 0) { + error_setg(errp, "Expected EIF flags to be 0"); + goto cleanup; + } + + if (eif_header.section_cnt > MAX_SECTIONS) { + error_setg(errp, "EIF header section count must not be greater tha= n " + "%d but found %d", MAX_SECTIONS, eif_header.section_cnt= ); + goto cleanup; + } + + for (int i =3D 0; i < eif_header.section_cnt; ++i) { + EifSectionHeader hdr; + uint16_t section_type; + + if (fseek(f, eif_header.section_offsets[i], SEEK_SET) !=3D 0) { + error_setg_errno(errp, errno, "Failed to offset to %lu in EIF = file", + eif_header.section_offsets[i]); + goto cleanup; + } + + if (!read_eif_section_header(f, &hdr, &crc, errp)) { + goto cleanup; + } + + if (hdr.flags !=3D 0) { + error_setg(errp, "Expected EIF section header flags to be 0"); + goto cleanup; + } + + if (eif_header.section_sizes[i] !=3D hdr.section_size) { + error_setg(errp, "EIF section size mismatch between header and= " + "section header: header %lu, section header %lu", + eif_header.section_sizes[i], + hdr.section_size); + goto cleanup; + } + + section_type =3D hdr.section_type; + + switch (section_type) { + case EIF_SECTION_KERNEL: + if (seen_sections[EIF_SECTION_KERNEL]) { + error_setg(errp, "Invalid EIF image. More than 1 kernel " + "section"); + goto cleanup; + } + + ptr =3D g_malloc(hdr.section_size); + + iov_ptr =3D g_malloc(sizeof(struct iovec)); + iov_ptr->iov_base =3D ptr; + iov_ptr->iov_len =3D hdr.section_size; + + iov_PCR0 =3D g_list_append(iov_PCR0, iov_ptr); + iov_PCR1 =3D g_list_append(iov_PCR1, iov_ptr); + + if (!read_eif_kernel(f, hdr.section_size, kernel_path, ptr, &c= rc, + errp)) { + goto cleanup; + } + + break; + case EIF_SECTION_CMDLINE: + { + uint64_t size; + uint8_t *cmdline_copy; + if (seen_sections[EIF_SECTION_CMDLINE]) { + error_setg(errp, "Invalid EIF image. More than 1 cmdline " + "section"); + goto cleanup; + } + size =3D hdr.section_size; + *cmdline =3D g_malloc(size + 1); + if (!read_eif_cmdline(f, size, *cmdline, &crc, errp)) { + goto cleanup; + } + (*cmdline)[size] =3D '\0'; + + /* + * We make a copy of '*cmdline' for putting it in iovecs so th= at + * we can easily free all the iovec entries later as we cannot + * free '*cmdline' which is used by the caller. + */ + cmdline_copy =3D g_memdup2(*cmdline, size); + + iov_ptr =3D g_malloc(sizeof(struct iovec)); + iov_ptr->iov_base =3D cmdline_copy; + iov_ptr->iov_len =3D size; + + iov_PCR0 =3D g_list_append(iov_PCR0, iov_ptr); + iov_PCR1 =3D g_list_append(iov_PCR1, iov_ptr); + break; + } + case EIF_SECTION_RAMDISK: + { + if (!seen_sections[EIF_SECTION_RAMDISK]) { + /* + * If this is the first time we are seeing a ramdisk secti= on, + * we need to create the initrd temporary file. + */ + if (!get_tmp_file("eif-initrd-XXXXXX", initrd_path, errp))= { + goto cleanup; + } + initrd_path_f =3D fopen(*initrd_path, "wb"); + if (initrd_path_f =3D=3D NULL) { + error_setg_errno(errp, errno, "Failed to open file %s", + *initrd_path); + goto cleanup; + } + } + + ptr =3D g_malloc(hdr.section_size); + + iov_ptr =3D g_malloc(sizeof(struct iovec)); + iov_ptr->iov_base =3D ptr; + iov_ptr->iov_len =3D hdr.section_size; + + iov_PCR0 =3D g_list_append(iov_PCR0, iov_ptr); + /* + * If it's the first ramdisk, we need to hash it into bootstrap + * i.e., iov_PCR1, otherwise we need to hash it into app i.e., + * iov_PCR2. + */ + if (!seen_sections[EIF_SECTION_RAMDISK]) { + iov_PCR1 =3D g_list_append(iov_PCR1, iov_ptr); + } else { + iov_PCR2 =3D g_list_append(iov_PCR2, iov_ptr); + } + + if (!read_eif_ramdisk(f, initrd_path_f, hdr.section_size, ptr, + &crc, errp)) { + goto cleanup; + } + + break; + } + case EIF_SECTION_SIGNATURE: + *signature_found =3D true; + if (!get_signature_fingerprint_sha384(f, hdr.section_size, + fingerprint_sha384, &crc, + errp)) { + goto cleanup; + } + break; + default: + /* other sections including invalid or unknown sections */ + { + uint8_t *buf; + size_t got; + uint64_t size =3D hdr.section_size; + buf =3D g_malloc(size); + got =3D fread(buf, 1, size, f); + if ((uint64_t) got !=3D size) { + g_free(buf); + error_setg(errp, "Failed to read EIF %s section data", + section_type_to_string(section_type)); + goto cleanup; + } + crc =3D crc32(crc, buf, size); + g_free(buf); + break; + } + } + + if (section_type < EIF_SECTION_MAX) { + seen_sections[section_type] =3D true; + } + } + + if (!seen_sections[EIF_SECTION_KERNEL]) { + error_setg(errp, "Invalid EIF image. No kernel section."); + goto cleanup; + } + if (!seen_sections[EIF_SECTION_CMDLINE]) { + error_setg(errp, "Invalid EIF image. No cmdline section."); + goto cleanup; + } + if (!seen_sections[EIF_SECTION_RAMDISK]) { + error_setg(errp, "Invalid EIF image. No ramdisk section."); + goto cleanup; + } + + if (eif_header.eif_crc32 !=3D crc) { + error_setg(errp, "CRC mismatch. Expected %u but header has %u.", + crc, eif_header.eif_crc32); + goto cleanup; + } + + /* + * Let's append the initrd file from "-initrd" option if any. Although + * we pass the crc pointer to read_eif_ramdisk, it is not useful anymo= re. + * We have already done the crc mismatch check above this code. + */ + if (machine_initrd) { + machine_initrd_f =3D fopen(machine_initrd, "rb"); + if (machine_initrd_f =3D=3D NULL) { + error_setg_errno(errp, errno, "Failed to open initrd file %s", + machine_initrd); + goto cleanup; + } + + machine_initrd_size =3D get_file_size(machine_initrd_f, errp); + if (machine_initrd_size =3D=3D -1) { + goto cleanup; + } + + ptr =3D g_malloc(machine_initrd_size); + + iov_ptr =3D g_malloc(sizeof(struct iovec)); + iov_ptr->iov_base =3D ptr; + iov_ptr->iov_len =3D machine_initrd_size; + + iov_PCR0 =3D g_list_append(iov_PCR0, iov_ptr); + iov_PCR2 =3D g_list_append(iov_PCR2, iov_ptr); + + if (!read_eif_ramdisk(machine_initrd_f, initrd_path_f, + machine_initrd_size, ptr, &crc, errp)) { + goto cleanup; + } + } + + if (!get_SHA384_digest(iov_PCR0, image_sha384, errp)) { + goto cleanup; + } + if (!get_SHA384_digest(iov_PCR1, bootstrap_sha384, errp)) { + goto cleanup; + } + if (!get_SHA384_digest(iov_PCR2, app_sha384, errp)) { + goto cleanup; + } + + /* + * We only need to free iov_PCR0 entries because iov_PCR1 and + * iov_PCR2 iovec entries are subsets of iov_PCR0 iovec entries. + */ + g_list_free_full(iov_PCR0, (GDestroyNotify) free_iovec); + g_list_free(iov_PCR1); + g_list_free(iov_PCR2); + fclose(f); + fclose(initrd_path_f); + safe_fclose(machine_initrd_f); + return true; + + cleanup: + g_list_free_full(iov_PCR0, (GDestroyNotify) free_iovec); + g_list_free(iov_PCR1); + g_list_free(iov_PCR2); + + safe_fclose(f); + safe_fclose(initrd_path_f); + safe_fclose(machine_initrd_f); + + safe_unlink(*kernel_path); + g_free(*kernel_path); + *kernel_path =3D NULL; + + safe_unlink(*initrd_path); + g_free(*initrd_path); + *initrd_path =3D NULL; + + g_free(*cmdline); + *cmdline =3D NULL; + + return false; +} diff --git a/hw/core/eif.h b/hw/core/eif.h new file mode 100644 index 0000000000..fed3cb5514 --- /dev/null +++ b/hw/core/eif.h @@ -0,0 +1,22 @@ +/* + * EIF (Enclave Image Format) 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 HW_CORE_EIF_H +#define HW_CORE_EIF_H + +bool read_eif_file(const char *eif_path, const char *machine_initrd, + char **kernel_path, char **initrd_path, + char **kernel_cmdline, uint8_t *image_sha384, + uint8_t *bootstrap_sha384, uint8_t *app_sha384, + uint8_t *fingerprint_sha384, bool *signature_found, + Error **errp); + +#endif + --=20 2.39.2 From nobody Sun Nov 24 06:05:18 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=1725566363; cv=none; d=zohomail.com; s=zohoarc; b=IhGhMi1cafFJtq5tNOEe/dffsaf1EyPbLFFwX9SOGBTImdc0FjBlRvLBKGeGcwJC8QrmF7Zm7PYQi28JL0jaupm+KeII1JlrlyHSSKH5aQ1JvwvFU1D+nF8G5llfPcGB/Qgg/ybBfnvrdWfbLUqhYr1qq7s4OFV+V2nx1nZ5krI= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1725566363; 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=TPD1++3Ru/bEAjnX03jCVbPiPc0WT+1zyJFlql9oWJ8=; b=cFHMv9U9F8rKtlUA/lSH59KZrcThp62h/fPFon9dzeWn+z9UkLAELIrcRm9/CFJPi8OeEE+GSfuzM9xWlaT78pVxRwMaTBtu5aVb/Uv6JePg07SjOLfxHELJ4UWFxj7sgKLqVmgsPk0QTSsaVestTI8yAgOuCTiaTBoP+mUUAqk= 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 1725566363479916.3686555251959; Thu, 5 Sep 2024 12:59:23 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1smIca-00088Y-2n; Thu, 05 Sep 2024 15:58:24 -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 1smIcY-0007no-3e for qemu-devel@nongnu.org; Thu, 05 Sep 2024 15:58:22 -0400 Received: from mail-pl1-x62b.google.com ([2607:f8b0:4864:20::62b]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1smIcU-0005SC-Or for qemu-devel@nongnu.org; Thu, 05 Sep 2024 15:58:21 -0400 Received: by mail-pl1-x62b.google.com with SMTP id d9443c01a7336-20543fdb7acso10319085ad.1 for ; Thu, 05 Sep 2024 12:58:18 -0700 (PDT) Received: from localhost.localdomain ([103.103.35.145]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-206aea5554dsm32031355ad.235.2024.09.05.12.58.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 05 Sep 2024 12:58:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1725566297; x=1726171097; 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=TPD1++3Ru/bEAjnX03jCVbPiPc0WT+1zyJFlql9oWJ8=; b=YzMteVdI/fu/5Fndb7cmMY8UDD4JoVh0zDyyHbDQv1p4pPVDCEaXq5mTLbsgROOUbO w6ocnfgjhMq8OtxVDo4TnBCX80BDjtO4EXmBOawplTu99b9S+hzGaBiGemFdsBgXF62H vkMCSWKRfGzS9u+HqfxpVJL29mF3DEaCEFifmw+ptPQ1h1wZcBbpgHGFJm088VSdbs3r wkHadXrTKsi12C3p12ccZcohb0N11i4CaAJHb4og7sy1w4m5jINtH2E8vLEREAZ3T6KB /apkgjKKbm7mShic0QFFxVm7WPkAzX5Wzrfj+pAlPkn9KEae6VI/MCi1bCZOe/P+ElnU kwEg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1725566297; x=1726171097; 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=TPD1++3Ru/bEAjnX03jCVbPiPc0WT+1zyJFlql9oWJ8=; b=X9HKfAqcMZfjN9i3P5eSV27CCTCcvhLWPj/oFe9PlzcRlIwRU4g0JA0qz4mWgkfu4J mBqjRbtmt1vCHJ6baHlRn9O6Z3wqQw5oN/s/1PIhgSd1ht6FibN6qbhUftKgzB371kEB gk76y3JZ0Ya/ZrfH0J82OaUC4U53nCACwQMc1CSqPUYwdyFZkBv/dIh1/8WoIKaMBEJy bRy+auPINuD5pfr/1bDXy/FN8mC096EgHhj3TsKkpjret/hwKd2xWTPntPKj9hquUTkw HqsJVDToJ9CserhPvkhrHSVTYCgFjX/u1sLk+HIV23ZtP8o2XZfzy6J3QqBhPRp6IT4j 7y0Q== X-Gm-Message-State: AOJu0Yx2O/7yDzJmCfTs1SykqSjpzAGsd1N37yr8vXDAIowJDYqBX24t tbsln//v9gBtffoFFxVj5NaA89OYLSoCf5PMi235r/hnyB/ebVzb7g4ZpA== X-Google-Smtp-Source: AGHT+IEFDS9U9vYY/C2UVOyMw2M83tTkLPzMwFLROOhWB5NttxLPA/t18+6usWkpZaOqDtQ/4/PDkQ== X-Received: by 2002:a17:902:a516:b0:1fb:2bed:6418 with SMTP id d9443c01a7336-206f065a8d7mr1673185ad.57.1725566296660; Thu, 05 Sep 2024 12:58:16 -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 7/8] machine/nitro-enclave: New machine type for AWS Nitro Enclaves Date: Fri, 6 Sep 2024 01:57:34 +0600 Message-Id: <20240905195735.16911-8-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::62b; envelope-from=dorjoychy111@gmail.com; helo=mail-pl1-x62b.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: 1725566364293116600 Content-Type: text/plain; charset="utf-8" AWS nitro enclaves[1] is an Amazon EC2[2] feature that allows creating isolated execution environments, called enclaves, from Amazon EC2 instances which are used for processing highly sensitive data. Enclaves have no persistent storage and no external networking. The enclave VMs are based on the Firecracker microvm with a vhost-vsock device for communication with the parent EC2 instance that spawned it and a Nitro Secure Module (NSM) device for cryptographic attestation. The parent instance VM always has CID 3 while the enclave VM gets a dynamic CID. An EIF (Enclave Image Format)[3] file is used to boot an AWS nitro enclave virtual machine. This commit adds support for AWS nitro enclave emulation using a new machine type option '-M nitro-enclave'. This new machine type is based on the 'microvm' machine type, similar to how real nitro enclave VMs are based on Firecracker microvm. For nitro-enclave to boot from an EIF file, the kernel and ramdisk(s) are extracted into a temporary kernel and a temporary initrd file which are then hooked into the regular x86 boot mechanism along with the extracted cmdline. The EIF file path should be provided using the '-kernel' QEMU option. In QEMU, the vsock emulation for nitro enclave is added using vhost-user- vsock as opposed to vhost-vsock. vhost-vsock doesn't support sibling VM communication which is needed for nitro enclaves. So for the vsock communication to CID 3 to work, another process that does the vsock emulation in userspace must be run, for example, vhost-device-vsock[4] from rust-vmm, with necessary vsock communication support in another guest VM with CID 3. Using vhost-user-vsock also enables the possibility to implement some proxying support in the vhost-user-vsock daemon that will forward all the packets to the host machine instead of CID 3 so that users of nitro-enclave can run the necessary applications in their host machine instead of running another whole VM with CID 3. The following mandatory nitro-enclave machine option has been added related to the vhost-user-vsock device. - 'vsock': The chardev id from the '-chardev' option for the vhost-user-vsock device. AWS Nitro Enclaves have built-in Nitro Secure Module (NSM) device which has been added using the virtio-nsm device added in a previous commit. In Nitro Enclaves, all the PCRs start in a known zero state and the first 16 PCRs are locked from boot and reserved. The PCR0, PCR1, PCR2 and PCR8 contain the SHA384 hashes related to the EIF file used to boot the VM for validation. The following optional nitro-enclave machine options have been added related to the NSM device. - 'id': Enclave identifier, reflected in the module-id of the NSM device. If not provided, a default id will be set. - 'parent-role': Parent instance IAM role ARN, reflected in PCR3 of the NSM device. - 'parent-id': Parent instance identifier, reflected in PCR4 of the NSM device. [1] https://docs.aws.amazon.com/enclaves/latest/user/nitro-enclave.html [2] https://aws.amazon.com/ec2/ [3] https://github.com/aws/aws-nitro-enclaves-image-format [4] https://github.com/rust-vmm/vhost-device/tree/main/vhost-device-vsock Signed-off-by: Dorjoy Chowdhury --- MAINTAINERS | 2 + backends/hostmem-memfd.c | 2 - configs/devices/i386-softmmu/default.mak | 1 + hw/core/machine.c | 71 ++--- hw/core/meson.build | 3 + hw/i386/Kconfig | 6 + hw/i386/meson.build | 3 + hw/i386/microvm.c | 6 +- hw/i386/nitro_enclave.c | 355 +++++++++++++++++++++++ include/hw/boards.h | 2 + include/hw/i386/microvm.h | 2 + include/hw/i386/nitro_enclave.h | 62 ++++ include/sysemu/hostmem.h | 2 + 13 files changed, 481 insertions(+), 36 deletions(-) create mode 100644 hw/i386/nitro_enclave.c create mode 100644 include/hw/i386/nitro_enclave.h diff --git a/MAINTAINERS b/MAINTAINERS index ead98e645a..e06aa4a5d7 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -1885,6 +1885,8 @@ M: Dorjoy Chowdhury S: Maintained F: hw/core/eif.c F: hw/core/eif.h +F: hw/i386/nitro_enclave.c +F: include/hw/i386/nitro_enclave.h =20 Machine core M: Eduardo Habkost diff --git a/backends/hostmem-memfd.c b/backends/hostmem-memfd.c index 6a3c89a12b..9f890a813e 100644 --- a/backends/hostmem-memfd.c +++ b/backends/hostmem-memfd.c @@ -18,8 +18,6 @@ #include "qapi/error.h" #include "qom/object.h" =20 -#define TYPE_MEMORY_BACKEND_MEMFD "memory-backend-memfd" - OBJECT_DECLARE_SIMPLE_TYPE(HostMemoryBackendMemfd, MEMORY_BACKEND_MEMFD) =20 =20 diff --git a/configs/devices/i386-softmmu/default.mak b/configs/devices/i38= 6-softmmu/default.mak index 448e3e3b1b..4faf2f0315 100644 --- a/configs/devices/i386-softmmu/default.mak +++ b/configs/devices/i386-softmmu/default.mak @@ -29,3 +29,4 @@ # CONFIG_I440FX=3Dn # CONFIG_Q35=3Dn # CONFIG_MICROVM=3Dn +# CONFIG_NITRO_ENCLAVE=3Dn diff --git a/hw/core/machine.c b/hw/core/machine.c index 27dcda0248..b4662b2795 100644 --- a/hw/core/machine.c +++ b/hw/core/machine.c @@ -998,6 +998,39 @@ void machine_add_audiodev_property(MachineClass *mc) "Audiodev to use for default mac= hine devices"); } =20 +static bool create_default_memdev(MachineState *ms, const char *path, + Error **errp) +{ + Object *obj; + MachineClass *mc =3D MACHINE_GET_CLASS(ms); + bool r =3D false; + + obj =3D object_new(path ? TYPE_MEMORY_BACKEND_FILE : TYPE_MEMORY_BACKE= ND_RAM); + if (path) { + if (!object_property_set_str(obj, "mem-path", path, errp)) { + goto out; + } + } + if (!object_property_set_int(obj, "size", ms->ram_size, errp)) { + goto out; + } + object_property_add_child(object_get_objects_root(), mc->default_ram_i= d, + obj); + /* Ensure backend's memory region name is equal to mc->default_ram_id = */ + if (!object_property_set_bool(obj, "x-use-canonical-path-for-ramblock-= id", + false, errp)) { + goto out; + } + if (!user_creatable_complete(USER_CREATABLE(obj), errp)) { + goto out; + } + r =3D object_property_set_link(OBJECT(ms), "memory-backend", obj, errp= ); + +out: + object_unref(obj); + return r; +} + static void machine_class_init(ObjectClass *oc, void *data) { MachineClass *mc =3D MACHINE_CLASS(oc); @@ -1017,6 +1050,8 @@ static void machine_class_init(ObjectClass *oc, void = *data) */ mc->numa_mem_align_shift =3D 23; =20 + mc->create_default_memdev =3D create_default_memdev; + object_class_property_add_str(oc, "kernel", machine_get_kernel, machine_set_kernel); object_class_property_set_description(oc, "kernel", @@ -1410,38 +1445,6 @@ MemoryRegion *machine_consume_memdev(MachineState *m= achine, return ret; } =20 -static bool create_default_memdev(MachineState *ms, const char *path, Erro= r **errp) -{ - Object *obj; - MachineClass *mc =3D MACHINE_GET_CLASS(ms); - bool r =3D false; - - obj =3D object_new(path ? TYPE_MEMORY_BACKEND_FILE : TYPE_MEMORY_BACKE= ND_RAM); - if (path) { - if (!object_property_set_str(obj, "mem-path", path, errp)) { - goto out; - } - } - if (!object_property_set_int(obj, "size", ms->ram_size, errp)) { - goto out; - } - object_property_add_child(object_get_objects_root(), mc->default_ram_i= d, - obj); - /* Ensure backend's memory region name is equal to mc->default_ram_id = */ - if (!object_property_set_bool(obj, "x-use-canonical-path-for-ramblock-= id", - false, errp)) { - goto out; - } - if (!user_creatable_complete(USER_CREATABLE(obj), errp)) { - goto out; - } - r =3D object_property_set_link(OBJECT(ms), "memory-backend", obj, errp= ); - -out: - object_unref(obj); - return r; -} - const char *machine_class_default_cpu_type(MachineClass *mc) { if (mc->valid_cpu_types && !mc->valid_cpu_types[1]) { @@ -1545,7 +1548,9 @@ void machine_run_board_init(MachineState *machine, co= nst char *mem_path, Error * machine_class->default_ram_id); return; } - if (!create_default_memdev(current_machine, mem_path, errp)) { + + if (!machine_class->create_default_memdev(current_machine, mem_pat= h, + errp)) { return; } } diff --git a/hw/core/meson.build b/hw/core/meson.build index a3d9bab9f4..5437a94490 100644 --- a/hw/core/meson.build +++ b/hw/core/meson.build @@ -24,6 +24,9 @@ system_ss.add(when: 'CONFIG_REGISTER', if_true: files('re= gister.c')) system_ss.add(when: 'CONFIG_SPLIT_IRQ', if_true: files('split-irq.c')) system_ss.add(when: 'CONFIG_XILINX_AXI', if_true: files('stream.c')) system_ss.add(when: 'CONFIG_PLATFORM_BUS', if_true: files('sysbus-fdt.c')) +if libcbor.found() and gnutls.found() + system_ss.add(when: 'CONFIG_NITRO_ENCLAVE', if_true: [files('eif.c'), zl= ib, libcbor, gnutls]) +endif =20 system_ss.add(files( 'cpu-sysemu.c', diff --git a/hw/i386/Kconfig b/hw/i386/Kconfig index f4a33b6c08..63271bf915 100644 --- a/hw/i386/Kconfig +++ b/hw/i386/Kconfig @@ -129,6 +129,12 @@ config MICROVM select USB_XHCI_SYSBUS select I8254 =20 +config NITRO_ENCLAVE + default y + depends on MICROVM + select VHOST_USER_VSOCK + select VIRTIO_NSM + config X86_IOMMU bool depends on PC diff --git a/hw/i386/meson.build b/hw/i386/meson.build index 03aad10df7..1ddd7a83be 100644 --- a/hw/i386/meson.build +++ b/hw/i386/meson.build @@ -15,6 +15,9 @@ i386_ss.add(when: 'CONFIG_AMD_IOMMU', if_true: files('amd= _iommu.c'), if_false: files('amd_iommu-stub.c')) i386_ss.add(when: 'CONFIG_I440FX', if_true: files('pc_piix.c')) i386_ss.add(when: 'CONFIG_MICROVM', if_true: files('x86-common.c', 'microv= m.c', 'acpi-microvm.c', 'microvm-dt.c')) +if libcbor.found() and gnutls.found() + i386_ss.add(when: 'CONFIG_NITRO_ENCLAVE', if_true: files('nitro_enclave.= c')) +endif i386_ss.add(when: 'CONFIG_Q35', if_true: files('pc_q35.c')) i386_ss.add(when: 'CONFIG_VMMOUSE', if_true: files('vmmouse.c')) i386_ss.add(when: 'CONFIG_VMPORT', if_true: files('vmport.c')) diff --git a/hw/i386/microvm.c b/hw/i386/microvm.c index 40edcee7af..869c177642 100644 --- a/hw/i386/microvm.c +++ b/hw/i386/microvm.c @@ -283,6 +283,7 @@ static void microvm_devices_init(MicrovmMachineState *m= ms) =20 static void microvm_memory_init(MicrovmMachineState *mms) { + MicrovmMachineClass *mmc =3D MICROVM_MACHINE_GET_CLASS(mms); MachineState *machine =3D MACHINE(mms); X86MachineState *x86ms =3D X86_MACHINE(mms); MemoryRegion *ram_below_4g, *ram_above_4g; @@ -328,7 +329,7 @@ static void microvm_memory_init(MicrovmMachineState *mm= s) rom_set_fw(fw_cfg); =20 if (machine->kernel_filename !=3D NULL) { - x86_load_linux(x86ms, fw_cfg, 0, true); + mmc->x86_load_linux(x86ms, fw_cfg, 0, true); } =20 if (mms->option_roms) { @@ -637,9 +638,12 @@ GlobalProperty microvm_properties[] =3D { static void microvm_class_init(ObjectClass *oc, void *data) { X86MachineClass *x86mc =3D X86_MACHINE_CLASS(oc); + MicrovmMachineClass *mmc =3D MICROVM_MACHINE_CLASS(oc); MachineClass *mc =3D MACHINE_CLASS(oc); HotplugHandlerClass *hc =3D HOTPLUG_HANDLER_CLASS(oc); =20 + mmc->x86_load_linux =3D x86_load_linux; + mc->init =3D microvm_machine_state_init; =20 mc->family =3D "microvm_i386"; diff --git a/hw/i386/nitro_enclave.c b/hw/i386/nitro_enclave.c new file mode 100644 index 0000000000..7dbeee530f --- /dev/null +++ b/hw/i386/nitro_enclave.c @@ -0,0 +1,355 @@ +/* + * AWS nitro-enclave machine + * + * 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 "qemu/error-report.h" +#include "qapi/error.h" +#include "qom/object_interfaces.h" + +#include "chardev/char.h" +#include "hw/sysbus.h" +#include "hw/core/eif.h" +#include "hw/i386/x86.h" +#include "hw/i386/microvm.h" +#include "hw/i386/nitro_enclave.h" +#include "hw/virtio/virtio-mmio.h" +#include "hw/virtio/virtio-nsm.h" +#include "hw/virtio/vhost-user-vsock.h" +#include "sysemu/hostmem.h" + +static BusState *find_free_virtio_mmio_bus(void) +{ + BusChild *kid; + BusState *bus =3D sysbus_get_default(); + + QTAILQ_FOREACH(kid, &bus->children, sibling) { + DeviceState *dev =3D kid->child; + if (object_dynamic_cast(OBJECT(dev), TYPE_VIRTIO_MMIO)) { + VirtIOMMIOProxy *mmio =3D VIRTIO_MMIO(OBJECT(dev)); + VirtioBusState *mmio_virtio_bus =3D &mmio->bus; + BusState *mmio_bus =3D &mmio_virtio_bus->parent_obj; + if (QTAILQ_EMPTY(&mmio_bus->children)) { + return mmio_bus; + } + } + } + + return NULL; +} + +static void vhost_user_vsock_init(NitroEnclaveMachineState *nems) +{ + DeviceState *dev =3D qdev_new(TYPE_VHOST_USER_VSOCK); + VHostUserVSock *vsock =3D VHOST_USER_VSOCK(dev); + BusState *bus; + + if (!nems->vsock) { + error_report("A valid chardev id for vhost-user-vsock device must = be " + "provided using the 'vsock' machine option"); + exit(1); + } + + bus =3D find_free_virtio_mmio_bus(); + if (!bus) { + error_report("Failed to find bus for vhost-user-vsock device"); + exit(1); + } + + Chardev *chardev =3D qemu_chr_find(nems->vsock); + if (!chardev) { + error_report("Failed to find chardev with id %s", nems->vsock); + exit(1); + } + + vsock->conf.chardev.chr =3D chardev; + + qdev_realize_and_unref(dev, bus, &error_fatal); +} + +static void virtio_nsm_init(NitroEnclaveMachineState *nems) +{ + DeviceState *dev =3D qdev_new(TYPE_VIRTIO_NSM); + VirtIONSM *vnsm =3D VIRTIO_NSM(dev); + BusState *bus =3D find_free_virtio_mmio_bus(); + + if (!bus) { + error_report("Failed to find bus for virtio-nsm device."); + exit(1); + } + + qdev_prop_set_string(dev, "module-id", nems->id); + + qdev_realize_and_unref(dev, bus, &error_fatal); + nems->vnsm =3D vnsm; +} + +static void nitro_enclave_devices_init(NitroEnclaveMachineState *nems) +{ + vhost_user_vsock_init(nems); + virtio_nsm_init(nems); +} + +static void nitro_enclave_machine_state_init(MachineState *machine) +{ + NitroEnclaveMachineClass *ne_class =3D + NITRO_ENCLAVE_MACHINE_GET_CLASS(machine); + NitroEnclaveMachineState *ne_state =3D NITRO_ENCLAVE_MACHINE(machine); + + ne_class->parent_init(machine); + nitro_enclave_devices_init(ne_state); +} + +static void nitro_enclave_machine_reset(MachineState *machine, + ShutdownCause reason) +{ + NitroEnclaveMachineClass *ne_class =3D + NITRO_ENCLAVE_MACHINE_GET_CLASS(machine); + NitroEnclaveMachineState *ne_state =3D NITRO_ENCLAVE_MACHINE(machine); + + ne_class->parent_reset(machine, reason); + + memset(ne_state->vnsm->pcrs, 0, sizeof(ne_state->vnsm->pcrs)); + + /* PCR0 */ + ne_state->vnsm->extend_pcr(ne_state->vnsm, 0, ne_state->image_sha384, + QCRYPTO_HASH_DIGEST_LEN_SHA384); + /* PCR1 */ + ne_state->vnsm->extend_pcr(ne_state->vnsm, 1, ne_state->bootstrap_sha3= 84, + QCRYPTO_HASH_DIGEST_LEN_SHA384); + /* PCR2 */ + ne_state->vnsm->extend_pcr(ne_state->vnsm, 2, ne_state->app_sha384, + QCRYPTO_HASH_DIGEST_LEN_SHA384); + /* PCR3 */ + if (ne_state->parent_role) { + ne_state->vnsm->extend_pcr(ne_state->vnsm, 3, + (uint8_t *) ne_state->parent_role, + strlen(ne_state->parent_role)); + } + /* PCR4 */ + if (ne_state->parent_id) { + ne_state->vnsm->extend_pcr(ne_state->vnsm, 4, + (uint8_t *) ne_state->parent_id, + strlen(ne_state->parent_id)); + } + /* PCR8 */ + if (ne_state->signature_found) { + ne_state->vnsm->extend_pcr(ne_state->vnsm, 8, + ne_state->fingerprint_sha384, + QCRYPTO_HASH_DIGEST_LEN_SHA384); + } + + /* First 16 PCRs are locked from boot and reserved for nitro enclave */ + for (int i =3D 0; i < 16; ++i) { + ne_state->vnsm->lock_pcr(ne_state->vnsm, i); + } +} + +static void nitro_enclave_machine_initfn(Object *obj) +{ + MicrovmMachineState *mms =3D MICROVM_MACHINE(obj); + X86MachineState *x86ms =3D X86_MACHINE(obj); + NitroEnclaveMachineState *nems =3D NITRO_ENCLAVE_MACHINE(obj); + + nems->id =3D g_strdup("i-234-enc5678"); + + /* AWS nitro enclaves have PCIE and ACPI disabled */ + mms->pcie =3D ON_OFF_AUTO_OFF; + x86ms->acpi =3D ON_OFF_AUTO_OFF; +} + +static void x86_load_eif(X86MachineState *x86ms, FWCfgState *fw_cfg, + int acpi_data_size, bool pvh_enabled) +{ + Error *err =3D NULL; + char *eif_kernel, *eif_initrd, *eif_cmdline; + MachineState *machine =3D MACHINE(x86ms); + NitroEnclaveMachineState *nems =3D NITRO_ENCLAVE_MACHINE(x86ms); + + if (!read_eif_file(machine->kernel_filename, machine->initrd_filename, + &eif_kernel, &eif_initrd, &eif_cmdline, + nems->image_sha384, nems->bootstrap_sha384, + nems->app_sha384, nems->fingerprint_sha384, + &(nems->signature_found), &err)) { + error_report_err(err); + exit(1); + } + + g_free(machine->kernel_filename); + machine->kernel_filename =3D eif_kernel; + g_free(machine->initrd_filename); + machine->initrd_filename =3D eif_initrd; + + /* + * If kernel cmdline argument was provided, let's concatenate it to the + * extracted EIF kernel cmdline. + */ + if (machine->kernel_cmdline !=3D NULL) { + char *cmd =3D g_strdup_printf("%s %s", eif_cmdline, + machine->kernel_cmdline); + g_free(eif_cmdline); + g_free(machine->kernel_cmdline); + machine->kernel_cmdline =3D cmd; + } else { + machine->kernel_cmdline =3D eif_cmdline; + } + + x86_load_linux(x86ms, fw_cfg, 0, true); + + unlink(machine->kernel_filename); + unlink(machine->initrd_filename); + return; +} + +static bool create_memfd_backend(MachineState *ms, const char *path, + Error **errp) +{ + Object *obj; + MachineClass *mc =3D MACHINE_GET_CLASS(ms); + bool r =3D false; + + obj =3D object_new(TYPE_MEMORY_BACKEND_MEMFD); + if (!object_property_set_int(obj, "size", ms->ram_size, errp)) { + goto out; + } + object_property_add_child(object_get_objects_root(), mc->default_ram_i= d, + obj); + + if (!user_creatable_complete(USER_CREATABLE(obj), errp)) { + goto out; + } + r =3D object_property_set_link(OBJECT(ms), "memory-backend", obj, errp= ); + +out: + object_unref(obj); + return r; +} + +static char *nitro_enclave_get_vsock_chardev_id(Object *obj, Error **errp) +{ + NitroEnclaveMachineState *nems =3D NITRO_ENCLAVE_MACHINE(obj); + + return g_strdup(nems->vsock); +} + +static void nitro_enclave_set_vsock_chardev_id(Object *obj, const char *va= lue, + Error **errp) +{ + NitroEnclaveMachineState *nems =3D NITRO_ENCLAVE_MACHINE(obj); + + g_free(nems->vsock); + nems->vsock =3D g_strdup(value); +} + +static char *nitro_enclave_get_id(Object *obj, Error **errp) +{ + NitroEnclaveMachineState *nems =3D NITRO_ENCLAVE_MACHINE(obj); + + return g_strdup(nems->id); +} + +static void nitro_enclave_set_id(Object *obj, const char *value, + Error **errp) +{ + NitroEnclaveMachineState *nems =3D NITRO_ENCLAVE_MACHINE(obj); + + g_free(nems->id); + nems->id =3D g_strdup(value); +} + +static char *nitro_enclave_get_parent_role(Object *obj, Error **errp) +{ + NitroEnclaveMachineState *nems =3D NITRO_ENCLAVE_MACHINE(obj); + + return g_strdup(nems->parent_role); +} + +static void nitro_enclave_set_parent_role(Object *obj, const char *value, + Error **errp) +{ + NitroEnclaveMachineState *nems =3D NITRO_ENCLAVE_MACHINE(obj); + + g_free(nems->parent_role); + nems->parent_role =3D g_strdup(value); +} + +static char *nitro_enclave_get_parent_id(Object *obj, Error **errp) +{ + NitroEnclaveMachineState *nems =3D NITRO_ENCLAVE_MACHINE(obj); + + return g_strdup(nems->parent_id); +} + +static void nitro_enclave_set_parent_id(Object *obj, const char *value, + Error **errp) +{ + NitroEnclaveMachineState *nems =3D NITRO_ENCLAVE_MACHINE(obj); + + g_free(nems->parent_id); + nems->parent_id =3D g_strdup(value); +} + +static void nitro_enclave_class_init(ObjectClass *oc, void *data) +{ + MachineClass *mc =3D MACHINE_CLASS(oc); + MicrovmMachineClass *mmc =3D MICROVM_MACHINE_CLASS(oc); + NitroEnclaveMachineClass *nemc =3D NITRO_ENCLAVE_MACHINE_CLASS(oc); + + mmc->x86_load_linux =3D x86_load_eif; + + mc->family =3D "nitro_enclave_i386"; + mc->desc =3D "AWS Nitro Enclave"; + + nemc->parent_init =3D mc->init; + mc->init =3D nitro_enclave_machine_state_init; + + nemc->parent_reset =3D mc->reset; + mc->reset =3D nitro_enclave_machine_reset; + + mc->create_default_memdev =3D create_memfd_backend; + + object_class_property_add_str(oc, NITRO_ENCLAVE_VSOCK_CHARDEV_ID, + nitro_enclave_get_vsock_chardev_id, + nitro_enclave_set_vsock_chardev_id); + object_class_property_set_description(oc, NITRO_ENCLAVE_VSOCK_CHARDEV_= ID, + "Set chardev id for vhost-user-v= sock " + "device"); + + object_class_property_add_str(oc, NITRO_ENCLAVE_ID, nitro_enclave_get_= id, + nitro_enclave_set_id); + object_class_property_set_description(oc, NITRO_ENCLAVE_ID, + "Set enclave identifier"); + + object_class_property_add_str(oc, NITRO_ENCLAVE_PARENT_ROLE, + nitro_enclave_get_parent_role, + nitro_enclave_set_parent_role); + object_class_property_set_description(oc, NITRO_ENCLAVE_PARENT_ROLE, + "Set parent instance IAM role AR= N"); + + object_class_property_add_str(oc, NITRO_ENCLAVE_PARENT_ID, + nitro_enclave_get_parent_id, + nitro_enclave_set_parent_id); + object_class_property_set_description(oc, NITRO_ENCLAVE_PARENT_ID, + "Set parent instance identifier"= ); +} + +static const TypeInfo nitro_enclave_machine_info =3D { + .name =3D TYPE_NITRO_ENCLAVE_MACHINE, + .parent =3D TYPE_MICROVM_MACHINE, + .instance_size =3D sizeof(NitroEnclaveMachineState), + .instance_init =3D nitro_enclave_machine_initfn, + .class_size =3D sizeof(NitroEnclaveMachineClass), + .class_init =3D nitro_enclave_class_init, +}; + +static void nitro_enclave_machine_init(void) +{ + type_register_static(&nitro_enclave_machine_info); +} +type_init(nitro_enclave_machine_init); diff --git a/include/hw/boards.h b/include/hw/boards.h index 48ff6d8b93..c268e7f005 100644 --- a/include/hw/boards.h +++ b/include/hw/boards.h @@ -308,6 +308,8 @@ struct MachineClass { int64_t (*get_default_cpu_node_id)(const MachineState *ms, int idx); ram_addr_t (*fixup_ram_size)(ram_addr_t size); uint64_t smbios_memory_device_size; + bool (*create_default_memdev)(MachineState *ms, const char *path, + Error **errp); }; =20 /** diff --git a/include/hw/i386/microvm.h b/include/hw/i386/microvm.h index fad97a891d..b9ac34a3ef 100644 --- a/include/hw/i386/microvm.h +++ b/include/hw/i386/microvm.h @@ -78,6 +78,8 @@ struct MicrovmMachineClass { X86MachineClass parent; HotplugHandler *(*orig_hotplug_handler)(MachineState *machine, DeviceState *dev); + void (*x86_load_linux)(X86MachineState *x86ms, FWCfgState *fw_cfg, + int acpi_data_size, bool pvh_enabled); }; =20 struct MicrovmMachineState { diff --git a/include/hw/i386/nitro_enclave.h b/include/hw/i386/nitro_enclav= e.h new file mode 100644 index 0000000000..687c88cb54 --- /dev/null +++ b/include/hw/i386/nitro_enclave.h @@ -0,0 +1,62 @@ +/* + * AWS nitro-enclave machine + * + * 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 HW_I386_NITRO_ENCLAVE_H +#define HW_I386_NITRO_ENCLAVE_H + +#include "crypto/hash.h" +#include "hw/i386/microvm.h" +#include "qom/object.h" +#include "hw/virtio/virtio-nsm.h" + +/* Machine type options */ +#define NITRO_ENCLAVE_VSOCK_CHARDEV_ID "vsock" +#define NITRO_ENCLAVE_ID "id" +#define NITRO_ENCLAVE_PARENT_ROLE "parent-role" +#define NITRO_ENCLAVE_PARENT_ID "parent-id" + +struct NitroEnclaveMachineClass { + MicrovmMachineClass parent; + + void (*parent_init)(MachineState *state); + void (*parent_reset)(MachineState *machine, ShutdownCause reason); +}; + +struct NitroEnclaveMachineState { + MicrovmMachineState parent; + + /* Machine type options */ + char *vsock; + /* Enclave identifier */ + char *id; + /* Parent instance IAM role ARN */ + char *parent_role; + /* Parent instance identifier */ + char *parent_id; + + /* Machine state */ + VirtIONSM *vnsm; + + /* kernel + ramdisks + cmdline sha384 hash */ + uint8_t image_sha384[QCRYPTO_HASH_DIGEST_LEN_SHA384]; + /* kernel + boot ramdisk + cmdline sha384 hash */ + uint8_t bootstrap_sha384[QCRYPTO_HASH_DIGEST_LEN_SHA384]; + /* application ramdisk(s) hash */ + uint8_t app_sha384[QCRYPTO_HASH_DIGEST_LEN_SHA384]; + /* certificate fingerprint hash */ + uint8_t fingerprint_sha384[QCRYPTO_HASH_DIGEST_LEN_SHA384]; + bool signature_found; +}; + +#define TYPE_NITRO_ENCLAVE_MACHINE MACHINE_TYPE_NAME("nitro-enclave") +OBJECT_DECLARE_TYPE(NitroEnclaveMachineState, NitroEnclaveMachineClass, + NITRO_ENCLAVE_MACHINE) + +#endif diff --git a/include/sysemu/hostmem.h b/include/sysemu/hostmem.h index de47ae59e4..67f45abe39 100644 --- a/include/sysemu/hostmem.h +++ b/include/sysemu/hostmem.h @@ -39,6 +39,8 @@ OBJECT_DECLARE_TYPE(HostMemoryBackend, HostMemoryBackendC= lass, */ #define TYPE_MEMORY_BACKEND_FILE "memory-backend-file" =20 +#define TYPE_MEMORY_BACKEND_MEMFD "memory-backend-memfd" + =20 /** * HostMemoryBackendClass: --=20 2.39.2 From nobody Sun Nov 24 06:05:18 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=1725566363; cv=none; d=zohomail.com; s=zohoarc; b=LCCLJurAcVDNhChivOpNmWbwFq/SUc4/Z3UzuUYemYEKfA10Yeaf77MUKQL0F+5AnlYVKmrLNnk4cWd4ZeRlSqPcaMpZ4G6+v6y3K5qWkp7CAHIE+ViN8t3xMo1K9lg3b7dni/on2x0ygsVR1nf8hU5XdZT72ZtFD5CCGSzSikA= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1725566363; 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=M3KKZe7ThyewA7ti6iiemriVuoI4xdMRGufcUwn0J3E=; b=jKVSgMDmDYss1FCvCEVOHn8tw2NW+dHOCofdNyuqkaJDH6UzIjUHBZweBuDmAf5zQj+h3xzmJNpdSTKHDz3c8o+5XBJlIgyHtx4wDDhRWYgK6cbtqTZDBPQjqFYKPouYzk/HYFZpOtNb7lQcTnJoMhIWkzw7UGdDatIXQuEtc08= 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 1725566363150879.9565269771374; Thu, 5 Sep 2024 12:59:23 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1smIcw-0001O9-BZ; Thu, 05 Sep 2024 15:58:46 -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 1smIcs-000158-Sb for qemu-devel@nongnu.org; Thu, 05 Sep 2024 15:58:45 -0400 Received: from mail-pl1-x632.google.com ([2607:f8b0:4864:20::632]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1smIcY-0005SK-8c for qemu-devel@nongnu.org; Thu, 05 Sep 2024 15:58:39 -0400 Received: by mail-pl1-x632.google.com with SMTP id d9443c01a7336-205722ba00cso11931815ad.0 for ; Thu, 05 Sep 2024 12:58:21 -0700 (PDT) Received: from localhost.localdomain ([103.103.35.145]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-206aea5554dsm32031355ad.235.2024.09.05.12.58.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 05 Sep 2024 12:58:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1725566301; x=1726171101; 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=M3KKZe7ThyewA7ti6iiemriVuoI4xdMRGufcUwn0J3E=; b=JSEligg0JNn6qMH70u1JlyUiuI/pNWe2nLpMQLtpbDgLXSVG4yk/A7yQerYmFBsxPq lJnr2L5wO4w5Hkh5boEws9SMOK6IK2baw/5sjaJt55y5mPiF6iAniRL478d0ocHw9RTG 9nt7sFnyKDMGZAXZVXdp4U8FzIb7RHxNdqT0KMjxFdV+LIhRiK8pOPESzqjfKwFxV3yo /hXPW9D+4E6LZYcbJy28ALkNQ4YLW7+1PdLsb+hyxwBWVNkyA+9M8e+oZkvsqMmy7cBi KOAExX3QrXtnpXcbLqdBF6kes5GsItZl8hBeRCB6QK9ln89j1aNzHXXwAiZpIqW/cvKZ +X5A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1725566301; x=1726171101; 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=M3KKZe7ThyewA7ti6iiemriVuoI4xdMRGufcUwn0J3E=; b=vjqUurABc/ySU5Ypy8pzXWS7DgVKuGhSdxbleKwjsMyNbjBeiR2U0PIj7sSYBgaMD/ 6qT/sAkLBLCRYK+ipwFJJxeo0Bbuj7Hl3xRspvQ0Sl1y81GAy++vQlNttfY/MY6J3p91 /VmUZCn7VscxlmFk1GVF2p+jJkPLaXII5AExfWLYBAZ5EmWuA0XqR9T+uQS3Kww2qWMR aIxDrMX5QC/6rVpw+jzyoREjKaDq1rXmTVXC2BivgVZJ06+3s9Dt8f04eSg1a0YxAw7h Hgo5ARMtcCWQpJ+irXfU1FxpNQKt0cd4Zl2rEF93XR3YemakTF/pM4i3js6UusMK6l2H V1Lg== X-Gm-Message-State: AOJu0YzCjv9Y4bGGNIiJTbNY+XIOa698jwD5HJyjjgh9bn0ZHlu9Kep5 dGYP4XVCv/POlmLMQwoU8y8hX0JkdtMSw+OCAQ1Ib3dOzhcpbS6yZgZKgQ== X-Google-Smtp-Source: AGHT+IFasMPIN4ZSMTOJwjkpop5vykF0XcYdbJJC8Dk3d0up5xLEtCXVd+DYOgCXOxndS/BIcmt6gw== X-Received: by 2002:a17:902:ea08:b0:202:43fa:52de with SMTP id d9443c01a7336-206f04f18f1mr2002485ad.13.1725566300686; Thu, 05 Sep 2024 12:58:20 -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 8/8] docs/nitro-enclave: Documentation for nitro-enclave machine type Date: Fri, 6 Sep 2024 01:57:35 +0600 Message-Id: <20240905195735.16911-9-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::632; envelope-from=dorjoychy111@gmail.com; helo=mail-pl1-x632.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: 1725566364238116600 Content-Type: text/plain; charset="utf-8" Signed-off-by: Dorjoy Chowdhury --- MAINTAINERS | 1 + docs/system/i386/nitro-enclave.rst | 78 ++++++++++++++++++++++++++++++ 2 files changed, 79 insertions(+) create mode 100644 docs/system/i386/nitro-enclave.rst diff --git a/MAINTAINERS b/MAINTAINERS index e06aa4a5d7..7bce4979b5 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -1887,6 +1887,7 @@ F: hw/core/eif.c F: hw/core/eif.h F: hw/i386/nitro_enclave.c F: include/hw/i386/nitro_enclave.h +F: docs/system/i386/nitro-enclave.rst =20 Machine core M: Eduardo Habkost diff --git a/docs/system/i386/nitro-enclave.rst b/docs/system/i386/nitro-en= clave.rst new file mode 100644 index 0000000000..3fb9e06893 --- /dev/null +++ b/docs/system/i386/nitro-enclave.rst @@ -0,0 +1,78 @@ +'nitro-enclave' virtual machine (``nitro-enclave``) +=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D + +``nitro-enclave`` is a machine type which emulates an ``AWS nitro enclave`` +virtual machine. `AWS nitro enclaves`_ is an `Amazon EC2`_ feature that al= lows +creating isolated execution environments, called enclaves, from Amazon EC2 +instances which are used for processing highly sensitive data. Enclaves ha= ve +no persistent storage and no external networking. The enclave VMs are based +on Firecracker microvm with a vhost-vsock device for communication with the +parent EC2 instance that spawned it and a Nitro Secure Module (NSM) device +for cryptographic attestation. The parent instance VM always has CID 3 whi= le +the enclave VM gets a dynamic CID. Enclaves use an EIF (`Enclave Image For= mat`_) +file which contains the necessary kernel, cmdline and ramdisk(s) to boot. + +In QEMU, ``nitro-enclave`` is a machine type based on ``microvm`` similar = to how +``AWS nitro enclaves`` are based on ``Firecracker`` microvm. This is usefu= l for +local testing of EIF files using QEMU instead of running real AWS Nitro En= claves +which can be difficult for debugging due to its roots in security. The vso= ck +device emulation is done using vhost-user-vsock which means another proces= s that +can do the userspace emulation, like `vhost-device-vsock`_ from rust-vmm c= rate, +must be run alongside nitro-enclave for the vsock communication to work. + +``libcbor`` and ``gnutls`` are required dependencies for nitro-enclave mac= hine +support to be added when building QEMU from source. + +.. _AWS nitro enlaves: https://docs.aws.amazon.com/enclaves/latest/user/ni= tro-enclave.html +.. _Amazon EC2: https://aws.amazon.com/ec2/ +.. _Enclave Image Format: https://github.com/aws/aws-nitro-enclaves-image-= format +.. _vhost-device-vsock: https://github.com/rust-vmm/vhost-device/tree/main= /vhost-device-vsock + +Using the nitro-enclave machine type +------------------------------ + +Machine-specific options +~~~~~~~~~~~~~~~~~~~~~~~~ + +It supports the following machine-specific options: + +- nitro-enclave.vsock=3Dstring (required) (Id of the chardev from '-charde= v' option that vhost-user-vsock device will use) +- nitro-enclave.id=3Dstring (optional) (Set enclave identifier) +- nitro-enclave.parent-role=3Dstring (optional) (Set parent instance IAM r= ole ARN) +- nitro-enclave.parent-id=3Dstring (optional) (Set parent instance identif= ier) + + +Running a nitro-enclave VM +~~~~~~~~~~~~~~~~~~~~~~~~~~ + +First, run `vhost-device-vsock`_ (or a similar tool that supports vhost-us= er-vsock). +The forward-cid option below with value 1 forwards all connections from th= e enclave +VM to the host machine and the forward-listen (port numbers separated by '= +') is used +for forwarding connections from the host machine to the enclave VM. + + $ vhost-device-vsock \ + --vm guest-cid=3D4,forward-cid=3D1,forward-listen=3D9001+9002,socket= =3D/tmp/vhost4.socket + +Now run the necessary applications on the host machine so that the nitro-e= nclave VM +applications' vsock communication works. For example, the nitro-enclave VM= 's init +process connects to CID 3 and sends a single byte hello heartbeat (0xB7) t= o let the +parent VM know that it booted expecting a heartbeat (0xB7) response. So yo= u must run +a AF_VSOCK server on the host machine that listens on port 9000 and sends = the heartbeat +after it receives the heartbeat for enclave VM to boot successfully. You s= hould run all +the applications on the host machine that would typically be running in th= e parent EC2 +VM for successful communication with the enclave VM. + +Then run the nitro-enclave VM using the following command where ``hello.ei= f`` is +an EIF file you would use to spawn a real AWS nitro enclave virtual machin= e: + + $ qemu-system-x86_64 -M nitro-enclave,vsock=3Dc,id=3Dhello-world \ + -kernel hello-world.eif -nographic -m 4G --enable-kvm -cpu host \ + -chardev socket,id=3Dc,path=3D/tmp/vhost4.socket + +In this example, the nitro-enclave VM has CID 4. If there are applications= that +connect to the enclave VM, run them on the host machine after enclave VM s= tarts. +You need to modify the applications to connect to CID 1 (instead of the en= clave +VM's CID) and use the forward-listen (e.g., 9001+9002) option of vhost-dev= ice-vsock +to forward the ports they connect to. + +.. _vhost-device-vsock: https://github.com/rust-vmm/vhost-device/tree/main= /vhost-device-vsock#using-the-vsock-backend --=20 2.39.2