]> source.dussan.org Git - nextcloud-server.git/commitdiff
Explicitly check hex2bin input 26693/head
authorRoeland Jago Douma <roeland@famdouma.nl>
Thu, 18 Feb 2021 19:12:20 +0000 (20:12 +0100)
committerRoeland Jago Douma <roeland@famdouma.nl>
Thu, 22 Apr 2021 12:01:25 +0000 (14:01 +0200)
For #23197

Signed-off-by: Roeland Jago Douma <roeland@famdouma.nl>
lib/private/Security/Crypto.php

index 154448281b9a35ab00863ec4837fa1ad86335243..99e7e74a3a0dad4584daa1831b45070cad0ad26c 100644 (file)
@@ -122,14 +122,14 @@ class Crypto implements ICrypto {
                        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);
                        }
                }
 
@@ -146,4 +146,20 @@ class Crypto implements ICrypto {
 
                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;
+       }
 }