Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[stable20] Explicitly check hex2bin input #26693

Merged
merged 1 commit into from
Apr 22, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 19 additions & 3 deletions lib/private/Security/Crypto.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,14 +122,14 @@ public function decrypt(string $authenticatedCiphertext, string $password = ''):
throw new \Exception('Authenticated ciphertext could not be decoded.');
}

$ciphertext = hex2bin($parts[0]);
$ciphertext = $this->hex2bin($parts[0]);
$iv = $parts[1];
$hmac = hex2bin($parts[2]);
$hmac = $this->hex2bin($parts[2]);

if ($partCount === 4) {
$version = $parts[3];
if ($version === '2') {
$iv = hex2bin($iv);
$iv = $this->hex2bin($iv);
}
}

Expand All @@ -146,4 +146,20 @@ public function decrypt(string $authenticatedCiphertext, string $password = ''):

return $result;
}

private function hex2bin(string $hex): string {
if (!ctype_xdigit($hex)) {
throw new \RuntimeException('String contains non hex chars: ' . $hex);
}
if (strlen($hex) % 2 !== 0) {
throw new \RuntimeException('Hex string is not of even length: ' . $hex);
}
$result = hex2bin($hex);

if ($result === false) {
throw new \RuntimeException('Hex to bin conversion failed: ' . $hex);
}

return $result;
}
}