Skip to content

Commit 35189a9

Browse files
authored
Merge pull request #26693 from nextcloud/backport/25714/stable20
[stable20] Explicitly check hex2bin input
2 parents 3c9b923 + 86de5d9 commit 35189a9

1 file changed

Lines changed: 19 additions & 3 deletions

File tree

lib/private/Security/Crypto.php

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -122,14 +122,14 @@ public function decrypt(string $authenticatedCiphertext, string $password = ''):
122122
throw new \Exception('Authenticated ciphertext could not be decoded.');
123123
}
124124

125-
$ciphertext = hex2bin($parts[0]);
125+
$ciphertext = $this->hex2bin($parts[0]);
126126
$iv = $parts[1];
127-
$hmac = hex2bin($parts[2]);
127+
$hmac = $this->hex2bin($parts[2]);
128128

129129
if ($partCount === 4) {
130130
$version = $parts[3];
131131
if ($version === '2') {
132-
$iv = hex2bin($iv);
132+
$iv = $this->hex2bin($iv);
133133
}
134134
}
135135

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

147147
return $result;
148148
}
149+
150+
private function hex2bin(string $hex): string {
151+
if (!ctype_xdigit($hex)) {
152+
throw new \RuntimeException('String contains non hex chars: ' . $hex);
153+
}
154+
if (strlen($hex) % 2 !== 0) {
155+
throw new \RuntimeException('Hex string is not of even length: ' . $hex);
156+
}
157+
$result = hex2bin($hex);
158+
159+
if ($result === false) {
160+
throw new \RuntimeException('Hex to bin conversion failed: ' . $hex);
161+
}
162+
163+
return $result;
164+
}
149165
}

0 commit comments

Comments
 (0)