Received: by 2002:ac0:a594:0:0:0:0:0 with SMTP id m20-v6csp5586666imm; Sat, 19 May 2018 05:24:32 -0700 (PDT) X-Google-Smtp-Source: AB8JxZrK922232cbrVBfdSOnyKZAiMWxggciiv3O2RGsNZ96VUL0EkWyeudcHqhadULggtTRBAkY X-Received: by 2002:a17:902:6113:: with SMTP id t19-v6mr13179614plj.372.1526732672905; Sat, 19 May 2018 05:24:32 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1526732672; cv=none; d=google.com; s=arc-20160816; b=CcRPXKs/CAAQKiYNQhkfE0O2bIrArecIObiJGgd1KUkn7TOufswP9rnn7+cCWjF53A LNfPmz8pXjl8G1/yPkNDQMfMngBmDKU63y8q5m6/HRFmnzxq4YdRyFo93OZs1+0jsg2r hronXVnsNee9eGttc34HmimQKuopkMU/P6Sn/WaX3sYPK3DLJf/Vqn2KcpZPp5hNHSXZ g3n2vdCcQvSLc4BBCGUHfflIeX+yktb6zLo4+C1AfF/KrYsuGhy7pGsJDUJfOJPpbswg 60OeyLRiymnjFBeYAcThNx+DvSQy7DickB8nw4ztIXJaia6jbtUs++T34q3qPV1YKKwR B44Q== 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:autocrypt :openpgp:cc:to:subject:from:arc-authentication-results; bh=0UZtyDNlfdlNnoT7P03NiVr2vpOtuHYjfmHfKyMsnBc=; b=gsX1v5O9Gt/u7fkyS77uNX2kcSQV2PfPK2wTlWl0eGOTdV/IHDrfWqtAa9zWNNh10I s5qzodiiaUvB1Rn5sLt+PJdgrmsOMi54Tg4dPwZwKDbXpKS5nFBNbfkfIsfAPysUToGj ni8nb2/mZOCquHRlHSbsuZZ3wCUIWH06h+dyHfLXdwdxmN0IqBfcyq72pwgR1ObWOrZo stYWs3hDCrUL31eF8D20pxOLkMLHEQt1FOoCsfnRc414ed2ndwnJaPoRnmnYCKp7KUES Ra8ABu0d1anWK7mN/QH/Hn0UoQJm9gPzlmdmmQtuUbVdlxtV+f1AYGmo0fnHHwHxZiAX T2mA== 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 m19-v6si9420563pff.303.2018.05.19.05.24.18; Sat, 19 May 2018 05:24:32 -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 S1752297AbeESMYG (ORCPT + 99 others); Sat, 19 May 2018 08:24:06 -0400 Received: from vps-vb.mhejs.net ([37.28.154.113]:44662 "EHLO vps-vb.mhejs.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752174AbeESMYE (ORCPT ); Sat, 19 May 2018 08:24:04 -0400 Received: by vps-vb.mhejs.net with esmtps (TLSv1.2:ECDHE-RSA-AES128-GCM-SHA256:128) (Exim 4.90_1) (envelope-from ) id 1fK0tu-0007sR-Rs; Sat, 19 May 2018 14:23:54 +0200 From: "Maciej S. Szmigiero" Subject: [PATCH v2][RESEND] X.509: unpack RSA signatureValue field from BIT STRING To: David Howells , Herbert Xu Cc: "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 Openpgp: preference=signencrypt Autocrypt: addr=mail@maciej.szmigiero.name; prefer-encrypt=mutual; keydata= xsFNBFpGusUBEADXUMM2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DAUJ A8JnAAAKCRCEf143kM4Jd9nXD/9jstJU6L1MLyr/ydKOnY48pSlZYgII9rSnFyLUHzNcW2c/ qw9LPMlDcK13tiVRQgKT4W+RvsET/tZCQcap2OF3Z6vd1naTur7oJvgvVM5lVhUia2O60kEZ XNlMLFwLSmGXhaAXNBySpzN2xStSLCtbK58r7Vf9QS0mR0PGU2v68Cb8fFWcYu2Yzn3RXf0Y dIVWvaQG9whxZq5MdJm5dknfTcCG+MtmbP/DnpQpjAlgVmDgMgYTBW1W9etU36YW0pTqEYuv 6cmRgSAKEDaYHhFLTR1+lLJkp5fFo3Sjm7XqmXzfSv9JGJGMKzoFOMBoLYv+VFnMoLX5UJAs 0JyFqFY2YxGyLd4J103NI/ocqQeU0TVvOZGVkENPSxIESnbxPghsEC0MWEbGsvqA8FwvU7Xf GhZPYzTRf7CndDnezEA69EhwpZXKs4CvxbXo5PDTv0OWzVaAWqq8s8aTMJWWAhvobFozJ63z afYHkuEjMo0Xps3o3uvKg7coooH521nNsv4ci+KeBq3mgMCRAy0g/Ef+Ql7mt900RCBHu4tk tOhPc3J1ep/e2WAJ4ngUqJhilzyCJnzVJ4cT79VK/uPtlfUCZdUz+jTC88TmP1p5wlucS31k Thy/CV4cqDFB8yzEujTSiRzd7neG3sH0vcxBd69uvSxLZPLGID840k0v5sftPA== Message-ID: <04f372f3-bc26-c629-2269-0e5d258f9d8f@maciej.szmigiero.name> Date: Sat, 19 May 2018 14:23:54 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.7.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;