Skip to content

Commit 393309b

Browse files
authored
Merge pull request #25714 from nextcloud/fix/23197/explicitly_check_hex2bin_input
Explicitly check hex2bin input
2 parents 634b6b8 + 16652ac commit 393309b

File tree

1 file changed

+19
-3
lines changed

1 file changed

+19
-3
lines changed

lib/private/Security/Crypto.php

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

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

131131
if ($partCount === 4) {
132132
$version = $parts[3];
133133
if ($version >= '2') {
134-
$iv = hex2bin($iv);
134+
$iv = $this->hex2bin($iv);
135135
}
136136

137137
if ($version === '3') {
@@ -154,4 +154,20 @@ public function decrypt(string $authenticatedCiphertext, string $password = ''):
154154

155155
return $result;
156156
}
157+
158+
private function hex2bin(string $hex): string {
159+
if (!ctype_xdigit($hex)) {
160+
throw new \RuntimeException('String contains non hex chars: ' . $hex);
161+
}
162+
if (strlen($hex) % 2 !== 0) {
163+
throw new \RuntimeException('Hex string is not of even length: ' . $hex);
164+
}
165+
$result = hex2bin($hex);
166+
167+
if ($result === false) {
168+
throw new \RuntimeException('Hex to bin conversion failed: ' . $hex);
169+
}
170+
171+
return $result;
172+
}
157173
}

0 commit comments

Comments
 (0)