Received: by 2002:a05:6358:d09b:b0:dc:cd0c:909e with SMTP id jc27csp8921321rwb; Thu, 24 Nov 2022 06:02:08 -0800 (PST) X-Google-Smtp-Source: AA0mqf61Ju/IEI6V0UnceNRYnmy+i3akT7pHqSpcGOh4Mw2aVScKdqMB+cqWFZQG+4T6TeGNv3Sm X-Received: by 2002:a62:3306:0:b0:56b:8181:fe3e with SMTP id z6-20020a623306000000b0056b8181fe3emr35622907pfz.57.1669298528211; Thu, 24 Nov 2022 06:02:08 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1669298528; cv=none; d=google.com; s=arc-20160816; b=NE96OnqA/iYc7fFVnYFG4xCRhWrtzfRI4F6kQX5npZUFpjM7BGqjnL7tSTG/6Y1r0R i+n/uBHBki9PvYkMsuVD0o2+QT6OHtR0GLZzRHbMUXS4+4OOyvi7RvKthrrSFvRxQGT7 dhJrCTYtB2AaxUpf0cFvwJsi7PHZ/2Tggz6epQj6hH1LG/HXrFCcGG2/WTwy1fwCJWjp veAflPDh/wgi89Om3SyStKp2X4si77ys7veuGOkoT57UHoGvu7GJWHjjRqt9CQFeQGMT WyOIJuB8Tw1p1R0ZHW0rn86kiWigCypZi7MwpdO2jpdvjR8wbMOQf1MFo+050XXWHPqS Jx2w== 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=MOV0/JHYEg81m6+HxEgpndLefaJ+Ca9O4oLBIpfzJQg=; b=Lkn3ppJ8Oks1/ec1QTQT+cs6WLEjUOT2BZSaCBh0YjHn1/eSqQNGsElR00h1BsAhQC qKVIuYnbxuKRjiJcoTniBZAb3XRUqhvsqNjv9dkeuNGYQkhn6re49P6h3GegyQIOIauJ m4O/FhQBbRF+noVAiY4vS+yfLI8mMkv4CtLuK9KtmQtIod4jR/oBsf3W/7l3HhtgPLOr UTqFM0iEzW+XKT/Z/d2MKPvoDu3PIdXXU3gvfM4fJ4QUcr50avHITqAM0dLeCQkoc+P0 1yrooxTdVVFDm2pqDnhJ5Jli2HYtnq2Qtg3x/H3sS2pxHimAtAzanUSHqPhSTKac+hdJ BsMw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@cloudflare.com header.s=google header.b=GLYA1v5e; 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=pass (p=REJECT sp=REJECT dis=NONE) header.from=cloudflare.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id il8-20020a17090b164800b00215e0e55a9esi1539597pjb.38.2022.11.24.06.01.54; Thu, 24 Nov 2022 06:02:08 -0800 (PST) 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=@cloudflare.com header.s=google header.b=GLYA1v5e; 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=pass (p=REJECT sp=REJECT dis=NONE) header.from=cloudflare.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230223AbiKXOBA (ORCPT + 99 others); Thu, 24 Nov 2022 09:01:00 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51380 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230320AbiKXOAj (ORCPT ); Thu, 24 Nov 2022 09:00:39 -0500 Received: from mail-wm1-x32c.google.com (mail-wm1-x32c.google.com [IPv6:2a00:1450:4864:20::32c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D0C9E1448E1 for ; Thu, 24 Nov 2022 05:58:36 -0800 (PST) Received: by mail-wm1-x32c.google.com with SMTP id j5-20020a05600c410500b003cfa9c0ea76so1301487wmi.3 for ; Thu, 24 Nov 2022 05:58:36 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cloudflare.com; s=google; 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=MOV0/JHYEg81m6+HxEgpndLefaJ+Ca9O4oLBIpfzJQg=; b=GLYA1v5eNATQO1vvCsTT7oynkA2oTGIWJuo/nJ3SRsduvSWWv1gi2qbfBprG4o33Sw pu/YCPsZ531MyXFGwDXmAqU3aIUuyXibH+njYOHGDEDztc/3xMCcGEdFRKb4ut3KKXAx PkmPtnjn4r5dE7R+FXG0ngW2YTigncjLXWcfk= 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:message-id:reply-to; bh=MOV0/JHYEg81m6+HxEgpndLefaJ+Ca9O4oLBIpfzJQg=; b=xwgu4BbCem5+PD/Qornh0ts1djX9VtUjJrV75owUKb7Rcz6YP5jeUiFIB4wFhDTh2r Vwb0zixIjL9LqWR8B8Z78fqtL1rsIW+2ztmvLt+MQ2+E5QKNX+HKu+vV06S7opQnhsH4 vvUKC6mGSbI4QdwUy0O0zt/2vp5mUtXuBwiVD6vz6g0B2AS6COiJPEiR2CMrfEM+66J0 cmu43uqj0rEhcJJG9CgOktEKw2N+7/n34iIsONMjudD7sx0Xa7mwjxiwF4Y/Lq1+CMU+ ZQkFSerad6u8TooCC+mCYWerR6j/LGGF59AFhr+8irOMUtbczGHXw2rql4uujVw3NPTp 5U1Q== X-Gm-Message-State: ANoB5pk6hGwIuqG9bKfCCJ3flwwauV+EDCLO370n6OUWWSKidgY9C9si BsXM7BeGpROfbtKeRkbTuHCsVg== X-Received: by 2002:a05:600c:3542:b0:3cf:6c2f:950c with SMTP id i2-20020a05600c354200b003cf6c2f950cmr16568410wmq.146.1669298315035; Thu, 24 Nov 2022 05:58:35 -0800 (PST) Received: from localhost.localdomain ([2a09:bac1:28c0:140::15:1b6]) by smtp.gmail.com with ESMTPSA id n14-20020a5d660e000000b00241bee11825sm1371440wru.103.2022.11.24.05.58.34 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Thu, 24 Nov 2022 05:58:34 -0800 (PST) From: Ignat Korchagin To: Herbert Xu , "David S . Miller" , linux-crypto@vger.kernel.org, David Howells , keyrings@vger.kernel.org Cc: kernel-team@cloudflare.com, lei he , Ignat Korchagin Subject: [RESEND PATCH v2 3/4] crypto: pkcs8 parser support ECDSA private keys Date: Thu, 24 Nov 2022 13:58:11 +0000 Message-Id: <20221124135812.26999-4-ignat@cloudflare.com> X-Mailer: git-send-email 2.37.1 (Apple Git-137.1) In-Reply-To: <20221124135812.26999-1-ignat@cloudflare.com> References: <20221124135812.26999-1-ignat@cloudflare.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_NONE autolearn=unavailable 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 Signed-off-by: Ignat Korchagin --- crypto/asymmetric_keys/pkcs8.asn1 | 2 +- crypto/asymmetric_keys/pkcs8_parser.c | 45 +++++++++++++++++++++++++-- 2 files changed, 43 insertions(+), 4 deletions(-) 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.30.2