Received: by 2002:a05:6358:1087:b0:cb:c9d3:cd90 with SMTP id j7csp363023rwi; Fri, 14 Oct 2022 03:11:14 -0700 (PDT) X-Google-Smtp-Source: AMsMyM5+4ZeVsJKc1J8vrZ5NFVKp9fGEeby/rzETQ4o+MQJqU2USgmew324dQcFTb1yP6fe/ef0+ X-Received: by 2002:a63:8549:0:b0:461:47e9:30df with SMTP id u70-20020a638549000000b0046147e930dfmr3959153pgd.376.1665742274384; Fri, 14 Oct 2022 03:11:14 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1665742274; cv=none; d=google.com; s=arc-20160816; b=PkaZ41RrDXCCyxgueG5KQDJ5LO5GQ5Asx1akO8xYXeTIA7psa5n5f0kbO0upp04fNG jeJ5gRLpNMoFfuSzjQCKzdfk9xogHmWIGIXfumFakBdWkgwusa7OtrUROX8ip/B0X69T GVtpyZV5Lpms4GS2QQEEbkTZ6S8rJh7BZ6sqymNs+t8ibHqPbmecXDzXgZZ+XzCKDuy3 5ZRxH78V5xSRDYORi6CfLAv32G++FFKn/B7S2ZyZH9Tu31TDxxevdfhCHBcjCcXDgx7K IMIouxCfF9ZrDFQrS8kjEfHov0j6Irxz2zHFQeBvaZiaOaqLrkuCr+F0x2BGVFO1lSu0 FWAQ== 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=J4ABuYwjNC33Bl/kCFhwk0nhxAGkT+VeM8/yFoM86lOVCrY3t57LuKFrzAH+WBNFnf KDHPKZoEuq77JD3zjxsILU8rPbsg3hSW3i2b1tJv3pWsl9IZrcPtB3T9douQMmKCHhGT QyibBIScJAeQCOkpK7B2TxiqZePB8HNzZI+hh67/q/H6WNaNxD0j6Dm6q4anJwVtbrsr f6k6yB9QhGNYBoA017J7hBgt94BxeMRKbZ93s61Ke1z6FGcYnPVUehS73cQJfvDDchiE Lh/1a8uL+puOS28NPYVDGTm3mVJjuhMja7uw7baHF4bCW5waDlhcGpitkey22iUikCSo clrA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@cloudflare.com header.s=google header.b=RJR6YVOd; 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 u10-20020a17090341ca00b00174a7230e39si2901286ple.329.2022.10.14.03.10.42; Fri, 14 Oct 2022 03:11:14 -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=@cloudflare.com header.s=google header.b=RJR6YVOd; 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 S230053AbiJNKID (ORCPT + 99 others); Fri, 14 Oct 2022 06:08:03 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42998 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229563AbiJNKH7 (ORCPT ); Fri, 14 Oct 2022 06:07:59 -0400 Received: from mail-wr1-x434.google.com (mail-wr1-x434.google.com [IPv6:2a00:1450:4864:20::434]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 95A271C8D42 for ; Fri, 14 Oct 2022 03:07:56 -0700 (PDT) Received: by mail-wr1-x434.google.com with SMTP id r13so6773130wrj.11 for ; Fri, 14 Oct 2022 03:07:56 -0700 (PDT) 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=RJR6YVOdooXE1ewExAo012x6+ETUffZf9JSuN3wIAQMjSS52jgw0YElszm7b2hfOU9 j9/r+7BCB5NuBVtfceRoJR+MG5bC7zDU0dKqpv8sX76TcTT1KDPv6CFtr+I2QgTyFeV5 whm8Y4wRQ4JhWP52lRcrdgX7vlqNx5cmNZPSw= 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=Bsl0dErXxmER/iXbKgcYvda90ED77g+j9+vtN/pRxvLcvgAjde9HPMv9PPZCdsf4md mgHDSJC84AMzGeQEBa6XJsLo2bFGBI+/nx46sqJzJjaq/yPQziTStaVk2f2OBmhoxRjf wirMNdVCjX/22G/ojrL57nzhSLAKDNWWdO3+APojJGjOHZinMOuj8cZVvoaWRWCdbVlk d3g+Wg4qZXKM6ytGTZhnEsKt7+z5hqyHHZpIsS4bZ3ZqNVPKQ9+bKyOkWOkFX0uhRFNH 359brWjYksLHnpDp17MrggOtqdyj5dH5qsVag4cOdhxHUbsA5AOG4W2yLDXAPdcWrjXx yhvQ== X-Gm-Message-State: ACrzQf0OCEb6+P5j0CmjfGDt3upro0a3eBie9SnI/Wq0uqNYNjtRcbIE u/eum5/D+48W5GnYMDs893lUnw== X-Received: by 2002:a5d:584a:0:b0:231:636c:de28 with SMTP id i10-20020a5d584a000000b00231636cde28mr2816767wrf.175.1665742074550; Fri, 14 Oct 2022 03:07:54 -0700 (PDT) Received: from localhost.localdomain ([2a02:c7c:5308:6600:49a0:d6bf:5c1a:f3da]) by smtp.gmail.com with ESMTPSA id c15-20020a5d414f000000b002285f73f11dsm1931008wrq.81.2022.10.14.03.07.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 14 Oct 2022 03:07:54 -0700 (PDT) From: Ignat Korchagin To: David Howells , Herbert Xu , "David S. Miller" , keyrings@vger.kernel.org, linux-crypto@vger.kernel.org, linux-kernel@vger.kernel.org Cc: kernel-team@cloudflare.com, lei he , Ignat Korchagin Subject: [PATCH v2 3/4] crypto: pkcs8 parser support ECDSA private keys Date: Fri, 14 Oct 2022 11:07:36 +0100 Message-Id: <20221014100737.94742-4-ignat@cloudflare.com> X-Mailer: git-send-email 2.37.0 (Apple Git-136) In-Reply-To: <20221014100737.94742-1-ignat@cloudflare.com> References: <20221014100737.94742-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