From nobody Tue Feb 10 06:26:38 2026 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=fail(p=none dis=none) header.from=bytedance.com ARC-Seal: i=1; a=rsa-sha256; t=1663559915; cv=none; d=zohomail.com; s=zohoarc; b=kNtZ3AA8nLvMmc7ghY9WQWG4h7V6fQKOnIZM9bZT+/o5ykgbEZGtdnFEB2Z7MuvPRhKfbmUmiWbhUKCZGaesQNgaJTLtmIcr/9Nb4QSHrgiQG1ZjAWkRhiW7M10EQB0cmpZ72Sv9QPhjxvawYUUDeHaUzKEBseMPq7jTHuPqmXw= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1663559915; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=MMhY9zMQPDm57mz1S/lB0Rr3pQ4kEo+FMv2x4saC10U=; b=HtOM7BUQ9GNx45H/E+Z6qgJGNevAEegNJzmGxFlJeG7RoyvSgivFKVpgcCifBjaUUMmPv5V30+Tl/o/8JIbqgG1TH3EOo0DgyOeUVbCM/9EK3IF/JRYoCv7qd3PGr7hZWIOpy0Ck9OearbP48enzPbxN4/UNm6v2h92Tncu0x1I= 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=fail 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 1663559915471506.15996703644373; Sun, 18 Sep 2022 20:58:35 -0700 (PDT) Received: from localhost ([::1]:35516 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1oa7vW-0007xJ-CF for importer@patchew.org; Sun, 18 Sep 2022 23:58:34 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:59482) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1oa7qs-0001tj-8n for qemu-devel@nongnu.org; Sun, 18 Sep 2022 23:53:46 -0400 Received: from mail-pf1-x42d.google.com ([2607:f8b0:4864:20::42d]:33283) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1oa7qq-0000qg-HZ for qemu-devel@nongnu.org; Sun, 18 Sep 2022 23:53:45 -0400 Received: by mail-pf1-x42d.google.com with SMTP id w2so16588002pfb.0 for ; Sun, 18 Sep 2022 20:53:44 -0700 (PDT) Received: from FVFDK26JP3YV.bytedance.net ([139.177.225.237]) by smtp.gmail.com with ESMTPSA id t19-20020a170902e1d300b0017865059c5dsm9014878pla.161.2022.09.18.20.53.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 18 Sep 2022 20:53:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance-com.20210112.gappssmtp.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date; bh=MMhY9zMQPDm57mz1S/lB0Rr3pQ4kEo+FMv2x4saC10U=; b=xwbujAtV2v03oABRSH05g7ttB3L9hJw2zRvZ8jIO6QnoW+5CKPXlzB6LgtvlJhl5Q/ DrtN+txbPgBjlMiP+qiGgRy4NtZycHBQ3Iofq/o7BoiVy50TyOyN8yHK2/sD/wpbG4Mh 4fCtkNOHzDudd9JFuSYPJzvwMKEg98okEq5ts+Iu9o4j6f7cZBDD4f6c6QW/L+19Bm4W XRPOvWiapNWlevBOj+Yt8kL6M7suAI8vid7o6VXsdbF1wNHci1/P6G4UU8XX11Xt9uco pw+FqHYb8pjdZxLzk4WfASqFC7xXpFgIRP3NNhrytybZr+NzqcVkCy3XKD1Kx6thAat+ e5QA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; 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; bh=MMhY9zMQPDm57mz1S/lB0Rr3pQ4kEo+FMv2x4saC10U=; b=xCIlTa9SI0fULlbsD6cGyBZITIiXJl+9lIc80rKgFqyv5EsD9tmcJjdIhOZSJmqAKA BO5Zi+1hVgYXAgZYCpTC28053MPVf00Hie45SoBH/0xyP6FVdgIDzIKX99mGvOmXsk7L waiat+rwVKE6zgG9wHR17TRGXTQxLo2TdIqwbDdKVs0i29IeY4+zFehvAUe7GVqy1QR6 ViY8EhG02BaI07+cdc0LU5ZZRncPuMiST/TOXmkVJpq/uf6pc6ZoP4Mz1s47/DbQYJeV j9tdZcuI5w4SrLGIHssZvYcjh0q1nbnq/ZstIOnl58zzNpVbXxHIhenBvefcwYHCh2gv mDMQ== X-Gm-Message-State: ACrzQf2SM9pgUJJwFoDpcBQQPZ3ofl4Fi/kXVRdugzuRN9fDASZCNMkT tRWbgfYmDoxyd7pD7nybxAuNbAC/s2/6QA== X-Google-Smtp-Source: AMsMyM68Rtnj5lRK19Fjg7qVQX2vC3RJmDwg0qrAFLdFns3AZ6XED1rDvESG9aezOMwkVd+XbmjnFg== X-Received: by 2002:a05:6a00:1583:b0:543:448b:dbca with SMTP id u3-20020a056a00158300b00543448bdbcamr16207950pfk.38.1663559622966; Sun, 18 Sep 2022 20:53:42 -0700 (PDT) From: Lei He To: qemu-devel@nongnu.org Cc: berrange@redhat.com, arei.gonglei@huawei.com, mst@redhat.com, pizhenwei@bytedance.com, Lei He Subject: [PATCH 3/4] crypto: Support export akcipher to pkcs8 Date: Mon, 19 Sep 2022 11:53:19 +0800 Message-Id: <20220919035320.84467-4-helei.sig11@bytedance.com> X-Mailer: git-send-email 2.29.2 In-Reply-To: <20220919035320.84467-1-helei.sig11@bytedance.com> References: <20220919035320.84467-1-helei.sig11@bytedance.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::42d; envelope-from=helei.sig11@bytedance.com; helo=mail-pf1-x42d.google.com X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham 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" X-ZohoMail-DKIM: pass (identity @bytedance-com.20210112.gappssmtp.com) X-ZM-MESSAGEID: 1663559917156100001 Content-Type: text/plain; charset="utf-8" crypto: support export RSA private keys with PKCS#8 standard. So that users can upload this private key to linux kernel. Signed-off-by: lei he --- crypto/akcipher.c | 17 +++++++++++++++++ crypto/rsakey.c | 42 ++++++++++++++++++++++++++++++++++++++++++ crypto/rsakey.h | 11 ++++++++++- include/crypto/akcipher.h | 21 +++++++++++++++++++++ 4 files changed, 90 insertions(+), 1 deletion(-) diff --git a/crypto/akcipher.c b/crypto/akcipher.c index ad88379c1e..fc9d4b2a6d 100644 --- a/crypto/akcipher.c +++ b/crypto/akcipher.c @@ -22,6 +22,7 @@ #include "qemu/osdep.h" #include "crypto/akcipher.h" #include "akcipherpriv.h" +#include "der.h" =20 #if defined(CONFIG_GCRYPT) #include "akcipher-gcrypt.c.inc" @@ -106,3 +107,19 @@ void qcrypto_akcipher_free(QCryptoAkCipher *akcipher) =20 drv->free(akcipher); } + +int qcrypto_akcipher_export_p8info(const QCryptoAkCipherOptions *opts, + uint8_t *key, size_t keylen, + uint8_t **dst, size_t *dst_len, + Error **errp) +{ + switch (opts->alg) { + case QCRYPTO_AKCIPHER_ALG_RSA: + qcrypto_akcipher_rsakey_export_p8info(key, keylen, dst, dst_len); + return 0; + + default: + error_setg(errp, "Unsupported algorithm: %u", opts->alg); + return -1; + } +} diff --git a/crypto/rsakey.c b/crypto/rsakey.c index cc40e072f0..7d6f273aef 100644 --- a/crypto/rsakey.c +++ b/crypto/rsakey.c @@ -19,6 +19,8 @@ * */ =20 +#include "qemu/osdep.h" +#include "der.h" #include "rsakey.h" =20 void qcrypto_akcipher_rsakey_free(QCryptoAkCipherRSAKey *rsa_key) @@ -37,6 +39,46 @@ void qcrypto_akcipher_rsakey_free(QCryptoAkCipherRSAKey = *rsa_key) g_free(rsa_key); } =20 +/** + * PKCS#8 private key info for RSA + * + * PrivateKeyInfo ::=3D SEQUENCE { + * version INTEGER, + * privateKeyAlgorithm PrivateKeyAlgorithmIdentifier, + * privateKey OCTET STRING, + * attributes [0] IMPLICIT Attributes OPTIONAL + * } + */ +void qcrypto_akcipher_rsakey_export_p8info(const uint8_t *key, + size_t keylen, + uint8_t **dst, + size_t *dlen) +{ + QCryptoEncodeContext *ctx =3D qcrypto_der_encode_ctx_new(); + uint8_t version =3D 0; + + qcrypto_der_encode_seq_begin(ctx); + + /* version */ + qcrypto_der_encode_int(ctx, &version, sizeof(version)); + + /* algorithm identifier */ + qcrypto_der_encode_seq_begin(ctx); + qcrypto_der_encode_oid(ctx, (uint8_t *)QCRYPTO_OID_rsaEncryption, + sizeof(QCRYPTO_OID_rsaEncryption) - 1); + qcrypto_der_encode_null(ctx); + qcrypto_der_encode_seq_end(ctx); + + /* RSA private key */ + qcrypto_der_encode_octet_str(ctx, key, keylen); + + qcrypto_der_encode_seq_end(ctx); + + *dlen =3D qcrypto_der_encode_ctx_buffer_len(ctx); + *dst =3D g_malloc(*dlen); + qcrypto_der_encode_ctx_flush_and_free(ctx, *dst); +} + #if defined(CONFIG_NETTLE) && defined(CONFIG_HOGWEED) #include "rsakey-nettle.c.inc" #else diff --git a/crypto/rsakey.h b/crypto/rsakey.h index 974b76f659..00b3eccec7 100644 --- a/crypto/rsakey.h +++ b/crypto/rsakey.h @@ -22,7 +22,6 @@ #ifndef QCRYPTO_RSAKEY_H #define QCRYPTO_RSAKEY_H =20 -#include "qemu/osdep.h" #include "qemu/host-utils.h" #include "crypto/akcipher.h" =20 @@ -84,6 +83,16 @@ QCryptoAkCipherRSAKey *qcrypto_akcipher_rsakey_parse( QCryptoAkCipherKeyType type, const uint8_t *key, size_t keylen, Error **errp); =20 +/** + * qcrypto_akcipher_rsakey_export_as_p8info: + * + * Export RSA private key to PKCS#8 private key info. + */ +void qcrypto_akcipher_rsakey_export_p8info(const uint8_t *key, + size_t keylen, + uint8_t **dst, + size_t *dlen); + void qcrypto_akcipher_rsakey_free(QCryptoAkCipherRSAKey *key); =20 G_DEFINE_AUTOPTR_CLEANUP_FUNC(QCryptoAkCipherRSAKey, diff --git a/include/crypto/akcipher.h b/include/crypto/akcipher.h index 51f5fa2774..214e58ca47 100644 --- a/include/crypto/akcipher.h +++ b/include/crypto/akcipher.h @@ -153,6 +153,27 @@ int qcrypto_akcipher_max_dgst_len(QCryptoAkCipher *akc= ipher); */ void qcrypto_akcipher_free(QCryptoAkCipher *akcipher); =20 +/** + * qcrypto_akcipher_export_p8info: + * @opts: the options of the akcipher to be exported. + * @key: the original key of the akcipher to be exported. + * @keylen: length of the 'key' + * @dst: output parameter, if export succeed, *dst is set to the + * PKCS#8 encoded private key, caller MUST free this key with + * g_free after use. + * @dst_len: output parameter, indicates the length of PKCS#8 encoded + * key. + * + * Export the akcipher into DER encoded pkcs#8 private key info, expects + * |key| stores a valid asymmetric PRIVATE key. + * + * Returns: 0 for succeed, otherwise -1 is returned. + */ +int qcrypto_akcipher_export_p8info(const QCryptoAkCipherOptions *opts, + uint8_t *key, size_t keylen, + uint8_t **dst, size_t *dst_len, + Error **errp); + G_DEFINE_AUTOPTR_CLEANUP_FUNC(QCryptoAkCipher, qcrypto_akcipher_free) =20 #endif /* QCRYPTO_AKCIPHER_H */ --=20 2.11.0