Received: by 2002:ac0:a581:0:0:0:0:0 with SMTP id m1-v6csp5316680imm; Tue, 26 Jun 2018 09:12:52 -0700 (PDT) X-Google-Smtp-Source: AAOMgpcSn4v0NeOFelbw3ceWGb1KckSe1Zl/DI5vuMNhATSNDhdxoAXXCcNToat+uARtBT01hdpd X-Received: by 2002:a62:1c43:: with SMTP id c64-v6mr2201202pfc.176.1530029571943; Tue, 26 Jun 2018 09:12:51 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1530029571; cv=none; d=google.com; s=arc-20160816; b=zfoYL/KMFl0R9y7xL9Kxh79j1E2WQmryCB2Xf1LPxjicyhwif/udB8OITlGa9dkpmu ne3IRIAuV98x0bIl5kk9wFUJ3/ZY6DXiRm1KNs06Mu51k9JHqlFqfOuwbtoaXOY6EUrN wOJX6hh/MpiGYGLMgp5ZEKmEW0Nw1Tt24339V5RBDEk+cYjL6CjshWYiNreJvy/+z8kl etUvKUA1fdx9+EDSCc4Wpdkn9tpkh3Hhe9/jT/wOlnlcZb/p1+vuAImvX3ResPSlN/8P DQw5NKF2+kvH+GVqTP8g41Xp8p2ZDrsdv7c4vyXVBxoLjLE8B7hR5aE/xvMInKIx5YWM RN3A== 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:mime-version :user-agent:references:in-reply-to:message-id:date:cc:to:from :subject:organization:arc-authentication-results; bh=0PMNYGo6nfw+xYpFeI/Fk7t9qfAozzFrpLFPe1wIP1k=; b=PH2koOrZq9VEA/VUwUbKbitCqO4UMoJcq2o2iHBR+j31rLmDj2cWTyzQKOMml/uz33 kqX/yGe9L17eCDLxGv7E+/E3+s9WfBgMxahZvRsBk1JaLm3EPCuF8xEIDEHfXeTfzSj+ jBcbzy2m9uezWaDqROLVtRauEYCEUxk50yvEE6T8aFn359/cPHwflkMHAHRXk0Hq7dwV SoU9psCE5jMPgZXTunov7rFXAXGPBbZo0OnE5OBLcABxTeuJsFMVWegEip3jwvnM7MkI +mZqNiY5N+aq/u3O6r/jkAWdCCxT2j9FLUYSQ4Y9DHkcw5yrp7iE5T/EkD1kngn6nts4 5kYw== 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; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id f35-v6si1823758plh.193.2018.06.26.09.12.37; Tue, 26 Jun 2018 09:12:51 -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; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752563AbeFZP7j (ORCPT + 99 others); Tue, 26 Jun 2018 11:59:39 -0400 Received: from mx3-rdu2.redhat.com ([66.187.233.73]:52118 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1752544AbeFZP7g (ORCPT ); Tue, 26 Jun 2018 11:59:36 -0400 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.rdu2.redhat.com [10.11.54.4]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 65E4E402312B; Tue, 26 Jun 2018 15:59:35 +0000 (UTC) Received: from warthog.procyon.org.uk (ovpn-120-238.rdu2.redhat.com [10.10.120.238]) by smtp.corp.redhat.com (Postfix) with ESMTP id 906972026E0E; Tue, 26 Jun 2018 15:59:34 +0000 (UTC) Organization: Red Hat UK Ltd. Registered Address: Red Hat UK Ltd, Amberley Place, 107-111 Peascod Street, Windsor, Berkshire, SI4 1TE, United Kingdom. Registered in England and Wales under Company Registration No. 3798903 Subject: [PATCH 1/3] X.509: unpack RSA signatureValue field from BIT STRING From: David Howells To: jmorris@namei.org Cc: "Maciej S. Szmigiero" , linux-kernel@vger.kernel.org, stable@vger.kernel.org, dhowells@redhat.com, linux-security-module@vger.kernel.org, keyrings@vger.kernel.org Date: Tue, 26 Jun 2018 16:59:33 +0100 Message-ID: <153002877383.31726.1992447630485011119.stgit@warthog.procyon.org.uk> In-Reply-To: <153002876741.31726.8580751276453556884.stgit@warthog.procyon.org.uk> References: <153002876741.31726.8580751276453556884.stgit@warthog.procyon.org.uk> User-Agent: StGit/0.17.1-dirty MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit X-Scanned-By: MIMEDefang 2.78 on 10.11.54.4 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.6]); Tue, 26 Jun 2018 15:59:35 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.6]); Tue, 26 Jun 2018 15:59:35 +0000 (UTC) for IP:'10.11.54.4' DOMAIN:'int-mx04.intmail.prod.int.rdu2.redhat.com' HELO:'smtp.corp.redhat.com' FROM:'dhowells@redhat.com' RCPT:'' Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Maciej S. Szmigiero 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 Signed-off-by: David Howells --- 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;