Received: by 2002:a6b:fb09:0:0:0:0:0 with SMTP id h9csp5530744iog; Wed, 22 Jun 2022 23:18:10 -0700 (PDT) X-Google-Smtp-Source: AGRyM1swAwCNC6rwBrYKfrnSAEl6oYY5LAPwZTsQtYWyCqaJ3B1/6lx+lMuDKXbWL82+1e9PuPur X-Received: by 2002:a17:902:cccf:b0:168:c4c3:e8ca with SMTP id z15-20020a170902cccf00b00168c4c3e8camr37602155ple.40.1655965089953; Wed, 22 Jun 2022 23:18:09 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1655965089; cv=none; d=google.com; s=arc-20160816; b=bvJyyQvsaqpjzhbQPLsdZAkClzT0PeK4FvGJfeEguHfjcJR99jpNtThp0Gqwl7zkfV AutWvHRyzkXQcq7E2b1rZUdWMk4edmTeP/gMhp/PcGYDnMXXpqvsrPVYhu6icKPHNO5H /5gtVMM+eh+wt0uRGW7AAgsfzBe9kXN4idaK+RTUH8PL4e1yhpjsVkbytbILnqyrwGWD XroX5YxdFzPbqIMmJHtFFsL9Jp2SoiYJn12RCE7PfzI3J6LJym6mgWZhphuW2ErZR7YX nC8NMzLJgyDzGkI6TUTDowcX9W/SxcEy8RQ/MF8cXHa/WasbMn9juLAjmOFz/zs81xE2 vqVw== 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=N30PFcUJK9qDKb9H5QKueknv/eODLawyoRdpjbtMyggrXKr44j8nqfG8g79Eba8V3+ HKJjWTCxGZvYwNPUfEvNzF9GOxPrYRv/ibzlOTSNY2r+CXbqwycFTm6ZzvVbl3y1h5S6 WytgQMljypndT1OwFrMaqD6DHarYyTSMzJVXLEOO9od6sIiTWb1LZ7FGShYA7fSoNW0q NHf9rQTPJFEIRAOY+0xRp87Bzh8Sz642n7IEDQ+EpFW5EYAR1NtULkcPjrlSoTIXWJdd Do58LCXxPd5jm+qa/HsCOq5dArOROu1dWVMjweUd1S7U4/BWBaASx9B7CyeVSQgi0fki LjLQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@bytedance-com.20210112.gappssmtp.com header.s=20210112 header.b=TumBSXa1; 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 a38-20020a056a001d2600b0051c4c9292e5si2498721pfx.308.2022.06.22.23.17.57; Wed, 22 Jun 2022 23:18:09 -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=TumBSXa1; 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 S229795AbiFWGPX (ORCPT + 99 others); Thu, 23 Jun 2022 02:15:23 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49798 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229692AbiFWGPX (ORCPT ); Thu, 23 Jun 2022 02:15:23 -0400 Received: from mail-pg1-x52e.google.com (mail-pg1-x52e.google.com [IPv6:2607:f8b0:4864:20::52e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9B5363A718 for ; Wed, 22 Jun 2022 23:15:21 -0700 (PDT) Received: by mail-pg1-x52e.google.com with SMTP id l4so18173839pgh.13 for ; Wed, 22 Jun 2022 23:15:21 -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=TumBSXa1A7/fh9+YVChoVb5zwgYdtT1Ure/4zv0RKBmw9qzxU7YkfM33UNSLdJoBkl a3A+zTdHJzTCeM2m0hNmwk5erykvVxCbmGX57AnAU+G5e7MXy8JzzGE4SzwuaII3iZIr +G2p2xOeYrnkv6NzGSohlRaaVIq7lRscrLUDxR3lRZy7bq35PmMghko1bQRN74cdVe98 wrH5ZE0o0k61aigR+MnJNJr9nGOmzfbvvzAmBVB14zTrGgsaGGMBS7/pX6CdL/2voTVQ dSbTfG2ss+QJ2eXoIfHjD6OP6dprU0BtpnCQYwXG/xSMyAgGmiGDGyG0w4x5PLKVsnvs 4Hyw== 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=3GQlFeNOSJz+nQG8gt+cVUKCOFcI96rNTNXt/cBBvQXQDGp7S4fZHAc8VT93MGP+Fo KweB5mOg8ZPCR1pnHvae4OJRh0HQWDRO2MCOgOzfwlSQTe0WHyEYnrAfmymWlB9M6Biv 6E3nnmh7v3umSnZhfPSHUgj4Eu3/9v7tm1qdlLDUeOIqXRAHnsJ5bF9Zq9jETtvD+L2t kIpI9RulquwmmW8Ja1A2Tt4dT2Cav6wUYI2l4g227/4woJRL3PTcc2uFu10IWxuTg6+R 9gt03lUJZymu/fW+hvLsWZhEp6pI52QmiBuZD0ZJSOO1hFBj9VB6U8ZnPOs/tyArzzkG 1M3w== X-Gm-Message-State: AJIora/Ed+qzMF7zd/zfElXMSDs0TdBb5uNA68W2Wf0Vrg30ckSqDTGY UR6DvSoGYXaOWXWdqkfzCCS6WQ== X-Received: by 2002:aa7:838c:0:b0:525:3816:2340 with SMTP id u12-20020aa7838c000000b0052538162340mr11811178pfm.35.1655964921134; Wed, 22 Jun 2022 23:15:21 -0700 (PDT) Received: from FVFDK26JP3YV.bytedance.net ([139.177.225.234]) by smtp.gmail.com with ESMTPSA id y22-20020a17090264d600b0015e8d4eb1b6sm350992pli.0.2022.06.22.23.15.17 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Wed, 22 Jun 2022 23:15:20 -0700 (PDT) From: Lei He To: davem@davemloft.net, herbert@gondor.apana.org.au, dhowells@redhat.com Cc: mst@redhat.com, linux-crypto@vger.kernel.org, linux-kernel@vger.kernel.org, berrange@redhat.com, pizhenwei@bytedance.com, lei he Subject: [PATCH v2 2/4] crypto: pkcs8 parser support ECDSA private keys Date: Thu, 23 Jun 2022 14:14:58 +0800 Message-Id: <20220623061500.78331-2-helei.sig11@bytedance.com> X-Mailer: git-send-email 2.29.2 In-Reply-To: <20220623061500.78331-1-helei.sig11@bytedance.com> References: <20220623061500.78331-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=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 --- 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