Received: by 10.192.165.156 with SMTP id m28csp67212imm; Tue, 17 Apr 2018 06:41:25 -0700 (PDT) X-Google-Smtp-Source: AIpwx48N9rYdvXFhbKrFM7rJHYBcNXIcXWIH936/G16mlxsHDfdQlYRUOTwUcnkUWlnGLLfLxNMm X-Received: by 2002:a17:902:d90e:: with SMTP id c14-v6mr2121936plz.352.1523972485776; Tue, 17 Apr 2018 06:41:25 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1523972485; cv=none; d=google.com; s=arc-20160816; b=vDc1c0UBPcIHIEOlkOtTinbHHDVAXmRYrOY9Y+y09ICWsT74LCiJPPRjimh+UL1DGq xbj9lgDV6FLvY7wr2KQTd6iGNpmi9g1MewGyypny+ClJmSp/FFWa5SD4B+yoiwvCt9oV EgUTVyyVAnAtp31APMsoH9AoF7RvofTWs8PCvYfdS1IVoruLfKDUMmzuFCJ8uA7ku1de Xqes0JkjM/2n7SoOVzIaMbhmp61ckJZEyIYzcAC5f9IHJdBHRMj8PNTn9NkLdpD2zqcx hZ6bLseSn7cGLdNiQuOJY1RG7+TbiJKKlw4/aWqsKruuSL1tQW55oWiXFXgunFKtJ9rQ E3uw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding :content-language:mime-version:user-agent:date:message-id:cc:to :subject:from:arc-authentication-results; bh=0UZtyDNlfdlNnoT7P03NiVr2vpOtuHYjfmHfKyMsnBc=; b=wL2FzGzzukW56dRZWow4By2QYzi2qKiSf/g98b96loqAIY21Qdq88qC68LrtX620l9 8Dskx8KNCQ+aMuUIuX6MqgB2i7NiPvwAtsB5JDnnzNHjegKYuCiZ/909pIgvk7G5BIoL QO+BXUGnByCV0zEkxKu0wViAYWQf81Ld9YcKNacNrqaVZ+xAUoy1zDMijqsWuWdhIVds Em5cfWGMKVPR+moICo4/nye3LQLag6eANif6EoSGLS3oPXpUB3N21TN/71d+GljivhZR he9PzaxIWoKQidpZjqVYBEtX8MDIqntAFRlF/4T3LsjIqTqXS3rrElHpwb02pZmN+PtS 5zFg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id p13-v6si14611505pll.416.2018.04.17.06.41.11; Tue, 17 Apr 2018 06:41:25 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753780AbeDQNjs (ORCPT + 99 others); Tue, 17 Apr 2018 09:39:48 -0400 Received: from vps-vb.mhejs.net ([37.28.154.113]:49610 "EHLO vps-vb.mhejs.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753551AbeDQNjm (ORCPT ); Tue, 17 Apr 2018 09:39:42 -0400 Received: by vps-vb.mhejs.net with esmtps (TLSv1.2:ECDHE-RSA-AES128-GCM-SHA256:128) (Exim 4.90_1) (envelope-from ) id 1f8QpZ-0007Zp-QF; Tue, 17 Apr 2018 15:39:33 +0200 From: "Maciej S. Szmigiero" Subject: [PATCH v2] X.509: unpack RSA signatureValue field from BIT STRING To: David Howells Cc: Herbert Xu , "David S. Miller" , Tom Lendacky , Gary Hook , James Morris , "Serge E. Hallyn" , keyrings@vger.kernel.org, linux-security-module@vger.kernel.org, linux-crypto@vger.kernel.org, linux-kernel@vger.kernel.org Message-ID: <6f5cdb8a-809e-6651-0e32-711d415a8b38@maciej.szmigiero.name> Date: Tue, 17 Apr 2018 15:39:33 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.6.0 MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-2 Content-Language: en-US Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org The signatureValue field of a X.509 certificate is encoded as a BIT STRING. For RSA signatures this BIT STRING is of so-called primitive subtype, which contains a u8 prefix indicating a count of unused bits in the encoding. We have to strip this prefix from signature data, just as we already do for key data in x509_extract_key_data() function. This wasn't noticed earlier because this prefix byte is zero for RSA key sizes divisible by 8. Since BIT STRING is a big-endian encoding adding zero prefixes has no bearing on its value. The signature length, however was incorrect, which is a problem for RSA implementations that need it to be exactly correct (like AMD CCP). Signed-off-by: Maciej S. Szmigiero Fixes: c26fd69fa009 ("X.509: Add a crypto key parser for binary (DER) X.509 certificates") Cc: stable@vger.kernel.org --- This is a resend of a patch that was previously submitted in one series with CCP driver changes since this particular patch should go through the security (rather than crypto) tree. Changes from v1: Change '!' to '== 0'. crypto/asymmetric_keys/x509_cert_parser.c | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/crypto/asymmetric_keys/x509_cert_parser.c b/crypto/asymmetric_keys/x509_cert_parser.c index 7d81e6bb461a..b6cabac4b62b 100644 --- a/crypto/asymmetric_keys/x509_cert_parser.c +++ b/crypto/asymmetric_keys/x509_cert_parser.c @@ -249,6 +249,15 @@ int x509_note_signature(void *context, size_t hdrlen, return -EINVAL; } + if (strcmp(ctx->cert->sig->pkey_algo, "rsa") == 0) { + /* Discard the BIT STRING metadata */ + if (vlen < 1 || *(const u8 *)value != 0) + return -EBADMSG; + + value++; + vlen--; + } + ctx->cert->raw_sig = value; ctx->cert->raw_sig_size = vlen; return 0;