From b8de3f4586bf22518ab3718ded300020e4e0a9f7 Mon Sep 17 00:00:00 2001 From: Spomky Date: Mon, 2 Sep 2019 22:01:14 +0200 Subject: [PATCH] Fix invalid signature length --- src/cose/src/Algorithm/Signature/ECDSA/ES256.php | 6 +++++- src/cose/src/Algorithm/Signature/ECDSA/ES256K.php | 6 +++++- src/cose/src/Algorithm/Signature/ECDSA/ES384.php | 6 +++++- src/cose/src/Algorithm/Signature/ECDSA/ES512.php | 6 +++++- src/webauthn/src/Util/CoseSignatureFixer.php | 12 ++++++++++++ 5 files changed, 32 insertions(+), 4 deletions(-) diff --git a/src/cose/src/Algorithm/Signature/ECDSA/ES256.php b/src/cose/src/Algorithm/Signature/ECDSA/ES256.php index d0517febb..3378b6fd1 100644 --- a/src/cose/src/Algorithm/Signature/ECDSA/ES256.php +++ b/src/cose/src/Algorithm/Signature/ECDSA/ES256.php @@ -34,7 +34,11 @@ public function sign(string $data, Key $key): string public function verify(string $data, Key $key, string $signature): bool { - $signature = ECSignature::toAsn1($signature, $this->getSignaturePartLength()); + if (mb_strlen($signature, '8bit') !== $this->getSignaturePartLength()) { + @trigger_error('Since v2.1, the method "verify" will only accept raw ECDSA signature in v3.0 and ASN.1 structures will be rejected', E_USER_DEPRECATED); + } else { + $signature = ECSignature::toAsn1($signature, $this->getSignaturePartLength()); + } return parent::verify($data, $key, $signature); } diff --git a/src/cose/src/Algorithm/Signature/ECDSA/ES256K.php b/src/cose/src/Algorithm/Signature/ECDSA/ES256K.php index 7e9d9f85d..206162af8 100644 --- a/src/cose/src/Algorithm/Signature/ECDSA/ES256K.php +++ b/src/cose/src/Algorithm/Signature/ECDSA/ES256K.php @@ -34,7 +34,11 @@ public function sign(string $data, Key $key): string public function verify(string $data, Key $key, string $signature): bool { - $signature = ECSignature::toAsn1($signature, $this->getSignaturePartLength()); + if (mb_strlen($signature, '8bit') !== $this->getSignaturePartLength()) { + @trigger_error('Since v2.1, the method "verify" will only accept raw ECDSA signature in v3.0 and ASN.1 structures will be rejected', E_USER_DEPRECATED); + } else { + $signature = ECSignature::toAsn1($signature, $this->getSignaturePartLength()); + } return parent::verify($data, $key, $signature); } diff --git a/src/cose/src/Algorithm/Signature/ECDSA/ES384.php b/src/cose/src/Algorithm/Signature/ECDSA/ES384.php index bbdc67c29..b70dc313f 100644 --- a/src/cose/src/Algorithm/Signature/ECDSA/ES384.php +++ b/src/cose/src/Algorithm/Signature/ECDSA/ES384.php @@ -34,7 +34,11 @@ public function sign(string $data, Key $key): string public function verify(string $data, Key $key, string $signature): bool { - $signature = ECSignature::toAsn1($signature, $this->getSignaturePartLength()); + if (mb_strlen($signature, '8bit') !== $this->getSignaturePartLength()) { + @trigger_error('Since v2.1, the method "verify" will only accept raw ECDSA signature in v3.0 and ASN.1 structures will be rejected', E_USER_DEPRECATED); + } else { + $signature = ECSignature::toAsn1($signature, $this->getSignaturePartLength()); + } return parent::verify($data, $key, $signature); } diff --git a/src/cose/src/Algorithm/Signature/ECDSA/ES512.php b/src/cose/src/Algorithm/Signature/ECDSA/ES512.php index 7d20d30d6..b88f2c32f 100644 --- a/src/cose/src/Algorithm/Signature/ECDSA/ES512.php +++ b/src/cose/src/Algorithm/Signature/ECDSA/ES512.php @@ -34,7 +34,11 @@ public function sign(string $data, Key $key): string public function verify(string $data, Key $key, string $signature): bool { - $signature = ECSignature::toAsn1($signature, $this->getSignaturePartLength()); + if (mb_strlen($signature, '8bit') !== $this->getSignaturePartLength()) { + @trigger_error('Since v2.1, the method "verify" accepts ASN.1 structures and raw ECDSA signature. In v3.0 and ASN.1 structures will be rejected', E_USER_DEPRECATED); + } else { + $signature = ECSignature::toAsn1($signature, $this->getSignaturePartLength()); + } return parent::verify($data, $key, $signature); } diff --git a/src/webauthn/src/Util/CoseSignatureFixer.php b/src/webauthn/src/Util/CoseSignatureFixer.php index f783abfaf..5418a87d7 100644 --- a/src/webauthn/src/Util/CoseSignatureFixer.php +++ b/src/webauthn/src/Util/CoseSignatureFixer.php @@ -30,10 +30,22 @@ public static function fix(string $signature, Signature $algorithm): string switch ($algorithm::identifier()) { case ECDSA\ES256K::ID: case ECDSA\ES256::ID: + if (64 === mb_strlen($signature, '8bit')) { + return $signature; + } + return ECDSA\ECSignature::fromAsn1($signature, 64); //TODO: fix this hardcoded value by adding a dedicated method for the algorithms case ECDSA\ES384::ID: + if (96 === mb_strlen($signature, '8bit')) { + return $signature; + } + return ECDSA\ECSignature::fromAsn1($signature, 96); case ECDSA\ES512::ID: + if (132 === mb_strlen($signature, '8bit')) { + return $signature; + } + return ECDSA\ECSignature::fromAsn1($signature, 132); }