Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-14.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER, INCLUDES_PATCH,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 354B0C47087 for ; Tue, 25 May 2021 22:34:50 +0000 (UTC) Received: by mail.kernel.org (Postfix) id 01E2B61417; Tue, 25 May 2021 22:34:50 +0000 (UTC) Received: from out2.migadu.com (out2.migadu.com [188.165.223.204]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 75F076140F for ; Tue, 25 May 2021 22:34:49 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 75F076140F Authentication-Results: mail.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=konstantin.ryabitsev@linux.dev X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1621982088; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=VUJXpasI524Md5AA9lvGo8JNRVoqDxhIWaV8TqZ050Y=; b=syKU9sqmxxzDA3aAzzpjoJ6EKBBeLdiZECLYmjzK/C0o37l+x0EEIIMiByNt3ei6sNfyED q0Rt6dNfJ+l3WHU5JKDFC1ep528HVWMaQFwBBLPchhlsiP0B1djs7ujVAelbOoGu+PwYVc hU3Bf8puhNWLw3OHLMaSxPltU9cIYck= From: Konstantin Ryabitsev List-Id: To: signatures@kernel.org Subject: [PATCH 2/3] Perform bh validation first Date: Tue, 25 May 2021 18:34:45 -0400 Message-Id: In-Reply-To: References: MIME-Version: 1.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=1426; i=konstantin.ryabitsev@linux.dev; h=from:subject:message-id; bh=Zm4Twag0aj+XnDadUGb17suvIFrh9HLjPdAvnT9dHp4=; b=owGbwMvMwCG27YjM47CUmTmMp9WSGBLWVrfFXc1Qt9h+tGr5Haad8vqfzBnfh27fZHJWbZnmq+j5 X2Jnd5SyMIhxMMiKKbKU7YvdFFT40EMuvccUZg4rE8gQBi5OAZiIqx0jw28JDcO5/1a+kbrUlL1IMN c0/Ofb4D9h/ml/el/udJXcqcTI8CPqi0jnhzXvkpZaObSEdxxi/uaoILctwal8XmXRAid2bgA= X-Developer-Key: i=konstantin.ryabitsev@linux.dev; a=openpgp; fpr=DE0E66E32F1FDD0902666B96E63EDCA9329DD07E Content-Transfer-Encoding: 8bit X-Migadu-Flow: FLOW_OUT X-Migadu-Auth-User: konstantin.ryabitsev@linux.dev From: Konstantin Ryabitsev Instead of doing bh validation as the last step, do it at the very start. This way we can recognize body content modifications early on before needing to do any signature validation at all. Signed-off-by: Konstantin Ryabitsev --- patatt/__init__.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/patatt/__init__.py b/patatt/__init__.py index 8365a4d..042a747 100644 --- a/patatt/__init__.py +++ b/patatt/__init__.py @@ -188,6 +188,10 @@ class DevsigHeader: def validate(self, keyinfo: Union[str, bytes, None]) -> str: self.sanity_check() + # Start by validating the body hash. If it fails to match, we can + # bail early, before needing to do any signature validation. + if self.get_field('bh') != self._body_hash: + raise BodyValidationError('Body content validation failed') # Check that we have a b= field if not self.get_field('b'): raise RuntimeError('Missing "b=" value') @@ -214,9 +218,6 @@ class DevsigHeader: vdigest = hashed.digest() if sdigest != vdigest: raise ValidationError('Header validation failed') - # Now validate body hash - if self.get_field('bh') != self._body_hash: - raise BodyValidationError('Body content validation failed') return signtime -- 2.31.1