Skip to content

Commit 15f2fa4

Browse files
authored
Merge pull request #89 from web-auth/CoseAlgIssue
Fix invalid signature length
2 parents d2f8415 + b8de3f4 commit 15f2fa4

File tree

5 files changed

+32
-4
lines changed

5 files changed

+32
-4
lines changed

src/cose/src/Algorithm/Signature/ECDSA/ES256.php

+5-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,11 @@ public function sign(string $data, Key $key): string
3434

3535
public function verify(string $data, Key $key, string $signature): bool
3636
{
37-
$signature = ECSignature::toAsn1($signature, $this->getSignaturePartLength());
37+
if (mb_strlen($signature, '8bit') !== $this->getSignaturePartLength()) {
38+
@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);
39+
} else {
40+
$signature = ECSignature::toAsn1($signature, $this->getSignaturePartLength());
41+
}
3842

3943
return parent::verify($data, $key, $signature);
4044
}

src/cose/src/Algorithm/Signature/ECDSA/ES256K.php

+5-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,11 @@ public function sign(string $data, Key $key): string
3434

3535
public function verify(string $data, Key $key, string $signature): bool
3636
{
37-
$signature = ECSignature::toAsn1($signature, $this->getSignaturePartLength());
37+
if (mb_strlen($signature, '8bit') !== $this->getSignaturePartLength()) {
38+
@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);
39+
} else {
40+
$signature = ECSignature::toAsn1($signature, $this->getSignaturePartLength());
41+
}
3842

3943
return parent::verify($data, $key, $signature);
4044
}

src/cose/src/Algorithm/Signature/ECDSA/ES384.php

+5-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,11 @@ public function sign(string $data, Key $key): string
3434

3535
public function verify(string $data, Key $key, string $signature): bool
3636
{
37-
$signature = ECSignature::toAsn1($signature, $this->getSignaturePartLength());
37+
if (mb_strlen($signature, '8bit') !== $this->getSignaturePartLength()) {
38+
@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);
39+
} else {
40+
$signature = ECSignature::toAsn1($signature, $this->getSignaturePartLength());
41+
}
3842

3943
return parent::verify($data, $key, $signature);
4044
}

src/cose/src/Algorithm/Signature/ECDSA/ES512.php

+5-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,11 @@ public function sign(string $data, Key $key): string
3434

3535
public function verify(string $data, Key $key, string $signature): bool
3636
{
37-
$signature = ECSignature::toAsn1($signature, $this->getSignaturePartLength());
37+
if (mb_strlen($signature, '8bit') !== $this->getSignaturePartLength()) {
38+
@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);
39+
} else {
40+
$signature = ECSignature::toAsn1($signature, $this->getSignaturePartLength());
41+
}
3842

3943
return parent::verify($data, $key, $signature);
4044
}

src/webauthn/src/Util/CoseSignatureFixer.php

+12
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,22 @@ public static function fix(string $signature, Signature $algorithm): string
3030
switch ($algorithm::identifier()) {
3131
case ECDSA\ES256K::ID:
3232
case ECDSA\ES256::ID:
33+
if (64 === mb_strlen($signature, '8bit')) {
34+
return $signature;
35+
}
36+
3337
return ECDSA\ECSignature::fromAsn1($signature, 64); //TODO: fix this hardcoded value by adding a dedicated method for the algorithms
3438
case ECDSA\ES384::ID:
39+
if (96 === mb_strlen($signature, '8bit')) {
40+
return $signature;
41+
}
42+
3543
return ECDSA\ECSignature::fromAsn1($signature, 96);
3644
case ECDSA\ES512::ID:
45+
if (132 === mb_strlen($signature, '8bit')) {
46+
return $signature;
47+
}
48+
3749
return ECDSA\ECSignature::fromAsn1($signature, 132);
3850
}
3951

0 commit comments

Comments
 (0)