summaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorCarl Schwan <carl@carlschwan.eu>2022-10-17 16:02:58 +0200
committerGitHub <noreply@github.com>2022-10-17 16:02:58 +0200
commit99191167167dd6c98dc3ae9b0eca947526e7939f (patch)
treef4f9f54791d136d861738f93749238fd0708a705 /lib
parent44d2eb8b4ec838652089be6018ae7240663781df (diff)
parentef31396727771eb771b450e91e7b097b2ca151b9 (diff)
downloadnextcloud-server-99191167167dd6c98dc3ae9b0eca947526e7939f.tar.gz
nextcloud-server-99191167167dd6c98dc3ae9b0eca947526e7939f.zip
Merge pull request #31499 from nextcloud/bugfix/empty-secret
Add fallback routines for empty secret cases
Diffstat (limited to 'lib')
-rw-r--r--lib/private/Authentication/Token/PublicKeyTokenProvider.php29
-rw-r--r--lib/private/Security/Crypto.php17
-rw-r--r--lib/private/Security/Hasher.php9
-rw-r--r--lib/private/Security/VerificationToken/VerificationToken.php9
4 files changed, 55 insertions, 9 deletions
diff --git a/lib/private/Authentication/Token/PublicKeyTokenProvider.php b/lib/private/Authentication/Token/PublicKeyTokenProvider.php
index 0f1767e845b..511aad76211 100644
--- a/lib/private/Authentication/Token/PublicKeyTokenProvider.php
+++ b/lib/private/Authentication/Token/PublicKeyTokenProvider.php
@@ -111,8 +111,14 @@ class PublicKeyTokenProvider implements IProvider {
$token = $this->mapper->getToken($this->hashToken($tokenId));
$this->cache[$token->getToken()] = $token;
} catch (DoesNotExistException $ex) {
- $this->cache[$tokenHash] = $ex;
- throw new InvalidTokenException("Token does not exist: " . $ex->getMessage(), 0, $ex);
+ try {
+ $token = $this->mapper->getToken($this->hashTokenWithEmptySecret($tokenId));
+ $this->cache[$token->getToken()] = $token;
+ $this->rotate($token, $tokenId, $tokenId);
+ } catch (DoesNotExistException $ex2) {
+ $this->cache[$tokenHash] = $ex2;
+ throw new InvalidTokenException("Token does not exist: " . $ex->getMessage(), 0, $ex);
+ }
}
}
@@ -189,6 +195,7 @@ class PublicKeyTokenProvider implements IProvider {
$this->cache->clear();
$this->mapper->invalidate($this->hashToken($token));
+ $this->mapper->invalidate($this->hashTokenWithEmptySecret($token));
}
public function invalidateTokenById(string $uid, int $id) {
@@ -305,9 +312,14 @@ class PublicKeyTokenProvider implements IProvider {
try {
return $this->crypto->decrypt($cipherText, $token . $secret);
} catch (\Exception $ex) {
- // Delete the invalid token
- $this->invalidateToken($token);
- throw new InvalidTokenException("Could not decrypt token password: " . $ex->getMessage(), 0, $ex);
+ // Retry with empty secret as a fallback for instances where the secret might not have been set by accident
+ try {
+ return $this->crypto->decrypt($cipherText, $token);
+ } catch (\Exception $ex2) {
+ // Delete the invalid token
+ $this->invalidateToken($token);
+ throw new InvalidTokenException("Could not decrypt token password: " . $ex->getMessage(), 0, $ex2);
+ }
}
}
@@ -331,6 +343,13 @@ class PublicKeyTokenProvider implements IProvider {
}
/**
+ * @deprecated Fallback for instances where the secret might not have been set by accident
+ */
+ private function hashTokenWithEmptySecret(string $token): string {
+ return hash('sha512', $token);
+ }
+
+ /**
* @throws \RuntimeException when OpenSSL reports a problem
*/
private function newToken(string $token,
diff --git a/lib/private/Security/Crypto.php b/lib/private/Security/Crypto.php
index e9ef4417925..aeeafcc271c 100644
--- a/lib/private/Security/Crypto.php
+++ b/lib/private/Security/Crypto.php
@@ -122,9 +122,22 @@ class Crypto implements ICrypto {
* @throws Exception If the decryption failed
*/
public function decrypt(string $authenticatedCiphertext, string $password = ''): string {
- if ($password === '') {
- $password = $this->config->getSystemValue('secret');
+ $secret = $this->config->getSystemValue('secret');
+ try {
+ if ($password === '') {
+ return $this->decryptWithoutSecret($authenticatedCiphertext, $secret);
+ }
+ return $this->decryptWithoutSecret($authenticatedCiphertext, $password);
+ } catch (Exception $e) {
+ if ($password === '') {
+ // Retry with empty secret as a fallback for instances where the secret might not have been set by accident
+ return $this->decryptWithoutSecret($authenticatedCiphertext, '');
+ }
+ throw $e;
}
+ }
+
+ private function decryptWithoutSecret(string $authenticatedCiphertext, string $password = ''): string {
$hmacKey = $encryptionKey = $password;
$parts = explode('|', $authenticatedCiphertext);
diff --git a/lib/private/Security/Hasher.php b/lib/private/Security/Hasher.php
index 5b3fc2b47a9..4731ba96bd3 100644
--- a/lib/private/Security/Hasher.php
+++ b/lib/private/Security/Hasher.php
@@ -137,6 +137,15 @@ class Hasher implements IHasher {
return true;
}
+ // Verify whether it matches a legacy PHPass or SHA1 string
+ // Retry with empty passwordsalt for cases where it was not set
+ $hashLength = \strlen($hash);
+ if (($hashLength === 60 && password_verify($message, $hash)) ||
+ ($hashLength === 40 && hash_equals($hash, sha1($message)))) {
+ $newHash = $this->hash($message);
+ return true;
+ }
+
return false;
}
diff --git a/lib/private/Security/VerificationToken/VerificationToken.php b/lib/private/Security/VerificationToken/VerificationToken.php
index c85e0e7b5a1..2d3f902b622 100644
--- a/lib/private/Security/VerificationToken/VerificationToken.php
+++ b/lib/private/Security/VerificationToken/VerificationToken.php
@@ -84,10 +84,15 @@ class VerificationToken implements IVerificationToken {
try {
$decryptedToken = $this->crypto->decrypt($encryptedToken, $passwordPrefix.$this->config->getSystemValue('secret'));
} catch (\Exception $e) {
- $this->throwInvalidTokenException(InvalidTokenException::TOKEN_DECRYPTION_ERROR);
+ // Retry with empty secret as a fallback for instances where the secret might not have been set by accident
+ try {
+ $decryptedToken = $this->crypto->decrypt($encryptedToken, $passwordPrefix);
+ } catch (\Exception $e2) {
+ $this->throwInvalidTokenException(InvalidTokenException::TOKEN_DECRYPTION_ERROR);
+ }
}
- $splitToken = explode(':', $decryptedToken ?? '');
+ $splitToken = explode(':', $decryptedToken);
if (count($splitToken) !== 2) {
$this->throwInvalidTokenException(InvalidTokenException::TOKEN_INVALID_FORMAT);
}