Skip to content

Commit a2e9eff

Browse files
authored
Merge pull request #62806 from nextcloud/sftp-key-remove-password
fix: clear key password after loading key
2 parents 9d28063 + f6b6541 commit a2e9eff

2 files changed

Lines changed: 16 additions & 4 deletions

File tree

apps/files_external/lib/Lib/Auth/PublicKey/RSA.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use OCP\IConfig;
1515
use OCP\IL10N;
1616
use OCP\IUser;
17+
use phpseclib3\Crypt\PublicKeyLoader;
1718
use phpseclib3\Crypt\RSA as RSACrypt;
1819

1920
/**
@@ -46,15 +47,19 @@ public function __construct(
4647
#[\Override]
4748
public function manipulateStorageConfig(StorageConfig &$storage, ?IUser $user = null) {
4849
try {
49-
$auth = RSACrypt::loadPrivateKey(
50+
$auth = PublicKeyLoader::load(
5051
$storage->getBackendOption('private_key'),
5152
$this->config->getSystemValue('secret', '')
5253
);
5354
} catch (\Throwable) {
5455
// Add fallback routine for a time where secret was not enforced to be exists
55-
$auth = RSACrypt::loadPrivateKey($storage->getBackendOption('private_key'));
56+
$auth = PublicKeyLoader::load($storage->getBackendOption('private_key'));
5657
}
5758

59+
if (!$auth instanceof RSACrypt\PrivateKey) {
60+
throw new \RuntimeException('Loaded key is not a private key');
61+
}
62+
$auth = $auth->withPassword('');
5863
$storage->setBackendOption('public_key_auth', $auth);
5964
}
6065

apps/files_external/lib/Lib/Auth/PublicKey/RSAPrivateKey.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use OCP\IConfig;
1414
use OCP\IL10N;
1515
use OCP\IUser;
16+
use phpseclib3\Crypt\PublicKeyLoader;
1617
use phpseclib3\Crypt\RSA;
1718
use phpseclib3\Exception\NoKeyLoadedException;
1819

@@ -46,17 +47,23 @@ public function __construct(
4647
public function manipulateStorageConfig(StorageConfig &$storage, ?IUser $user = null) {
4748

4849
try {
49-
$auth = RSA\PrivateKey::loadPrivateKey(
50+
$auth = PublicKeyLoader::load(
5051
$storage->getBackendOption('private_key'),
5152
$this->config->getSystemValue('secret', ''),
5253
);
5354
} catch (NoKeyLoadedException) {
5455
// Add fallback routine for a time where secret was not enforced to be exists
55-
$auth = RSA\PrivateKey::loadPrivateKey(
56+
$auth = PublicKeyLoader::load(
5657
$storage->getBackendOption('private_key'),
5758
'',
5859
);
5960
}
61+
62+
if (!$auth instanceof RSA\PrivateKey) {
63+
throw new \RuntimeException('Loaded key is not a private key');
64+
}
65+
66+
$auth = $auth->withPassword('');
6067
$storage->setBackendOption('public_key_auth', $auth);
6168
}
6269
}

0 commit comments

Comments
 (0)