Received: by 2002:a6b:fb09:0:0:0:0:0 with SMTP id h9csp139922iog; Fri, 17 Jun 2022 00:09:17 -0700 (PDT) X-Google-Smtp-Source: AGRyM1spJlJ/cMAU0o3c9wxep13L++G9XzioP1WSpa4lexv9kfP7QzQeCCkmYudT7iokwhGiZ5tf X-Received: by 2002:a05:6402:2710:b0:42e:28c9:fa7 with SMTP id y16-20020a056402271000b0042e28c90fa7mr10826973edd.2.1655449757824; Fri, 17 Jun 2022 00:09:17 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1655449757; cv=none; d=google.com; s=arc-20160816; b=DbBWLZB7YZOvZ6tGn47MY5pI8nH/O3Ur528+Q0Q7j+3UeI9qIOEqydPIz3iPpLmnLa l6oaBY56bh0NImxswpUu7SFpqqJx4FVmcVhoUE0ArOjEHVwyo9SALOxsanEj0SSFyVJ4 pI08hp8R+W8bOWpCIC/tLEHV61KPx3A2Q65Hdjk7SGinHL0I6meKSbEbCkYU1Ku8Mv7b FPV8art0W+zWRNFD/1miINxep9bTjy/wECP9Vacg+la6peM1qrZYSsWrXVMIVXUhy+Pg ebXEvw2ONqrA9tNLdOEhO2oY5uUPXx/02rCqoBhwRUPgYwncfnYJxuIpK4Kf90Vlb8Fk b3zg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=rr6W1HmkOzqLYvFXcgRYRczNT4ybnvSLXNzzsMN6g3Q=; b=LkwBwr7sw8perTADh5mLcqg/kJK2wzvaEFvxbC/RceUAKjrgydsPx5wlp0TJu9tc5H baTmhUJzTv8PB3ObB8u+qy7Q4v5x9frgImv49JPEyCxpoHqsOX9ypc7o/+S4yBSagsjP QKIBgTvA9/ZK4lVfraVmUPm9+ZBNd5tSBfSIHuAnrYrtJFDxUMA/OcZ/Fc7bDTApr7hD bwJMeWXZOLiWpgOEnWnOJBCf0as7uhPK2lJly7sumopkHXBz91tqz6RS6g/2Xd3cb7n3 xzpBSYQ73N+WZ7M9Hf50S4ZIfGh7Ky6rdV2LZUtiap/wPgnTnWir0FLZN/zdc7Pw0c6y QoCA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@bytedance-com.20210112.gappssmtp.com header.s=20210112 header.b=JpTFJBT1; spf=pass (google.com: domain of linux-crypto-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-crypto-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=bytedance.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id kz1-20020a17090777c100b00711bd5bbe4bsi913118ejc.585.2022.06.17.00.08.50; Fri, 17 Jun 2022 00:09:17 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-crypto-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@bytedance-com.20210112.gappssmtp.com header.s=20210112 header.b=JpTFJBT1; spf=pass (google.com: domain of linux-crypto-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-crypto-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=bytedance.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1380409AbiFQHIi (ORCPT + 99 others); Fri, 17 Jun 2022 03:08:38 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46296 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1380206AbiFQHIa (ORCPT ); Fri, 17 Jun 2022 03:08:30 -0400 Received: from mail-pg1-x530.google.com (mail-pg1-x530.google.com [IPv6:2607:f8b0:4864:20::530]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5E7EF63BCC for ; Fri, 17 Jun 2022 00:08:28 -0700 (PDT) Received: by mail-pg1-x530.google.com with SMTP id q140so3332717pgq.6 for ; Fri, 17 Jun 2022 00:08:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance-com.20210112.gappssmtp.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=rr6W1HmkOzqLYvFXcgRYRczNT4ybnvSLXNzzsMN6g3Q=; b=JpTFJBT1fX9DRCTHhPWB6z8Us/gWAXimLrXLSCaTPqgaxc04h48CuHFEXRXUtfjR4L 2gy0HQMZJTBJMpRvCZSPnQaPi1HTXO3bDHc5JlhR7yAcaNKchysoIWUBREQaNkCl4aRx TUMdgN0Wdl2/Z/gFVTm/dF6WAuxGVxRdVUJPp4sPa43gNrxCMLHRGak3X6PIK5Gwz5U2 TDrx1JsL/CNO4TSmxUxzfWqPMYY2mmJ/hgMzoPct14DvkDXyuWBnDaqPUcKv79B0XvHG uG4gX7N6X2681ZrdL2mg7JOK4DYTQbf5X0dRFVgW++vzTxDsHn+GnXRjrNhzWxKrHscT 5uyw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=rr6W1HmkOzqLYvFXcgRYRczNT4ybnvSLXNzzsMN6g3Q=; b=hZE/zArwSCt90HEClZyglZSFhDvQdJ2DitQ13/u8yJt5oUaMQ46mOLhP99ohVRZ4MQ U/KrZnlujuZxkTV+efHLPQrxOY9+kkBeiL3VzVf0acMppavCxxNEiJ/tgEkxxOL6N+av eJwH2ro/9KdtMru1aUDamvE9fpp3KBSO03byWj3MvVLHMpdxIPWHF40uZTD+EeQEVGRM NcqEyAKy0XX30Lwp0SWlCRYbQvgvu8KlISP5xJgRtTuqxqAMvYYrbhGO6YOlJJ0OSvj9 L5hyIa6asRxf0U6Qn48CjDbtwmO6v3jDyoUt4Sjv3XGDHmD0W4qa3Vde/Ae3Uaqtr6BQ cMDg== X-Gm-Message-State: AJIora/AXQFrlgkPfo/Ezg63XiVtk+AKvaee0hxaqCL12wPgAcvcve5A p3XaP9MpjtwempFDoHLMLBiPdA== X-Received: by 2002:a05:6a00:21c8:b0:4c4:4bd:dc17 with SMTP id t8-20020a056a0021c800b004c404bddc17mr8683594pfj.57.1655449707841; Fri, 17 Jun 2022 00:08:27 -0700 (PDT) Received: from FVFDK26JP3YV.bytedance.net ([139.177.225.234]) by smtp.gmail.com with ESMTPSA id j1-20020a170903028100b00163d4c3ffabsm2757868plr.304.2022.06.17.00.08.23 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Fri, 17 Jun 2022 00:08:27 -0700 (PDT) From: Lei He To: herbert@gondor.apana.org.au, davem@davemloft.net, dhowells@redhat.com, mst@redhat.com Cc: arei.gonglei@huawei.com, jasowang@redhat.com, linux-crypto@vger.kernel.org, linux-kernel@vger.kernel.org, pizhenwei@bytedance.com, helei.sig11@bytedance.com, f4bug@amsat.org, berrange@redhat.com Subject: [PATCH 2/4] crypto: pkcs8 parser support ECDSA private keys Date: Fri, 17 Jun 2022 15:07:52 +0800 Message-Id: <20220617070754.73667-4-helei.sig11@bytedance.com> X-Mailer: git-send-email 2.29.2 In-Reply-To: <20220617070754.73667-1-helei.sig11@bytedance.com> References: <20220617070754.73667-1-helei.sig11@bytedance.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org From: lei he Make pkcs8_private_key_parser can identify ECDSA private keys. Signed-off-by: lei he --- crypto/akcipher.c | 10 ++++++ crypto/asymmetric_keys/pkcs8.asn1 | 2 +- crypto/asymmetric_keys/pkcs8_parser.c | 45 +++++++++++++++++++++++++-- 3 files changed, 53 insertions(+), 4 deletions(-) diff --git a/crypto/akcipher.c b/crypto/akcipher.c index f866085c8a4a..3adcdc6d48c2 100644 --- a/crypto/akcipher.c +++ b/crypto/akcipher.c @@ -120,6 +120,12 @@ static int akcipher_default_op(struct akcipher_request *req) return -ENOSYS; } +static int akcipher_default_set_key(struct crypto_akcipher *tfm, + const void *key, unsigned int keylen) +{ + return -ENOSYS; +} + int crypto_register_akcipher(struct akcipher_alg *alg) { struct crypto_alg *base = &alg->base; @@ -132,6 +138,10 @@ int crypto_register_akcipher(struct akcipher_alg *alg) alg->encrypt = akcipher_default_op; if (!alg->decrypt) alg->decrypt = akcipher_default_op; + if (!alg->set_pub_key) + alg->set_pub_key = akcipher_default_set_key; + if (!alg->set_priv_key) + alg->set_priv_key = akcipher_default_set_key; akcipher_prepare_alg(alg); return crypto_register_alg(base); diff --git a/crypto/asymmetric_keys/pkcs8.asn1 b/crypto/asymmetric_keys/pkcs8.asn1 index 702c41a3c713..1791ddf4168a 100644 --- a/crypto/asymmetric_keys/pkcs8.asn1 +++ b/crypto/asymmetric_keys/pkcs8.asn1 @@ -20,5 +20,5 @@ Attribute ::= ANY AlgorithmIdentifier ::= SEQUENCE { algorithm OBJECT IDENTIFIER ({ pkcs8_note_OID }), - parameters ANY OPTIONAL + parameters ANY OPTIONAL ({ pkcs8_note_algo_parameter }) } diff --git a/crypto/asymmetric_keys/pkcs8_parser.c b/crypto/asymmetric_keys/pkcs8_parser.c index 105dcce27f71..e507c635ead5 100644 --- a/crypto/asymmetric_keys/pkcs8_parser.c +++ b/crypto/asymmetric_keys/pkcs8_parser.c @@ -24,6 +24,8 @@ struct pkcs8_parse_context { enum OID algo_oid; /* Algorithm OID */ u32 key_size; const void *key; + const void *algo_param; + u32 algo_param_len; }; /* @@ -47,6 +49,17 @@ int pkcs8_note_OID(void *context, size_t hdrlen, return 0; } +int pkcs8_note_algo_parameter(void *context, size_t hdrlen, + unsigned char tag, + const void *value, size_t vlen) +{ + struct pkcs8_parse_context *ctx = context; + + ctx->algo_param = value; + ctx->algo_param_len = vlen; + return 0; +} + /* * Note the version number of the ASN.1 blob. */ @@ -69,11 +82,37 @@ int pkcs8_note_algo(void *context, size_t hdrlen, const void *value, size_t vlen) { struct pkcs8_parse_context *ctx = context; - - if (ctx->last_oid != OID_rsaEncryption) + enum OID curve_id; + + switch (ctx->last_oid) { + case OID_id_ecPublicKey: + if (!ctx->algo_param || ctx->algo_param_len == 0) + return -EBADMSG; + curve_id = look_up_OID(ctx->algo_param, ctx->algo_param_len); + + switch (curve_id) { + case OID_id_prime192v1: + ctx->pub->pkey_algo = "ecdsa-nist-p192"; + break; + case OID_id_prime256v1: + ctx->pub->pkey_algo = "ecdsa-nist-p256"; + break; + case OID_id_ansip384r1: + ctx->pub->pkey_algo = "ecdsa-nist-p384"; + break; + default: + return -ENOPKG; + } + break; + + case OID_rsaEncryption: + ctx->pub->pkey_algo = "rsa"; + break; + + default: return -ENOPKG; + } - ctx->pub->pkey_algo = "rsa"; return 0; } -- 2.20.1