-
-
Notifications
You must be signed in to change notification settings - Fork 4.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Enhance signature algorithm for code signatures #24727
Closed
Closed
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -37,6 +37,7 @@ | |
use OC\IntegrityCheck\Helpers\AppLocator; | ||
use OC\IntegrityCheck\Helpers\EnvironmentHelper; | ||
use OC\IntegrityCheck\Helpers\FileAccessHelper; | ||
use OC\IntegrityCheck\Helpers\InfoParser; | ||
use OC\IntegrityCheck\Iterator\ExcludeFileByNameFilterIterator; | ||
use OC\IntegrityCheck\Iterator\ExcludeFoldersByPathFilterIterator; | ||
use OCP\App\IAppManager; | ||
|
@@ -76,6 +77,8 @@ class Checker { | |
private $tempManager; | ||
/** @var IMimeTypeDetector */ | ||
private $mimeTypeDetector; | ||
/** @var InfoParser */ | ||
private $infoParser; | ||
|
||
/** | ||
* @param EnvironmentHelper $environmentHelper | ||
|
@@ -89,6 +92,7 @@ class Checker { | |
*/ | ||
public function __construct(EnvironmentHelper $environmentHelper, | ||
FileAccessHelper $fileAccessHelper, | ||
InfoParser $infoParser, | ||
AppLocator $appLocator, | ||
IConfig $config = null, | ||
ICacheFactory $cacheFactory, | ||
|
@@ -103,6 +107,7 @@ public function __construct(EnvironmentHelper $environmentHelper, | |
$this->appManager = $appManager; | ||
$this->tempManager = $tempManager; | ||
$this->mimeTypeDetector = $mimeTypeDetector; | ||
$this->infoParser = $infoParser; | ||
} | ||
|
||
/** | ||
|
@@ -235,13 +240,21 @@ private function createSignatureData(array $hashes, | |
|
||
$privateKey->setSignatureMode(RSA::SIGNATURE_PSS); | ||
$privateKey->setMGFHash('sha512'); | ||
$privateKey->setHash('sha1'); | ||
// See https://tools.ietf.org/html/rfc3447#page-38 | ||
$privateKey->setSaltLength(0); | ||
$signature = $privateKey->sign(json_encode($hashes)); | ||
$sha1signature = $privateKey->sign(json_encode($hashes)); | ||
|
||
$privateKey->setHash('sha512'); | ||
$sha512signature = $privateKey->sign(json_encode($hashes)); | ||
|
||
return [ | ||
'hashes' => $hashes, | ||
'signature' => base64_encode($signature), | ||
'signature' => base64_encode($sha1signature), | ||
'signatures' => [ | ||
'sha1' => base64_encode($sha1signature), | ||
'sha512' => base64_encode($sha512signature), | ||
], | ||
'certificate' => $certificate->saveX509($certificate->currentCert), | ||
]; | ||
} | ||
|
@@ -311,11 +324,12 @@ public function writeCoreSignature(X509 $certificate, | |
* @param string $signaturePath | ||
* @param string $basePath | ||
* @param string $certificateCN | ||
* @param string|null $forceHash | ||
* @return array | ||
* @throws InvalidSignatureException | ||
* @throws \Exception | ||
*/ | ||
private function verify(string $signaturePath, string $basePath, string $certificateCN): array { | ||
private function verify(string $signaturePath, string $basePath, string $certificateCN, ?string $forceHash = null): array { | ||
if (!$this->isCodeCheckEnforced()) { | ||
return []; | ||
} | ||
|
@@ -332,7 +346,6 @@ private function verify(string $signaturePath, string $basePath, string $certifi | |
|
||
$expectedHashes = $signatureData['hashes']; | ||
ksort($expectedHashes); | ||
$signature = base64_decode($signatureData['signature']); | ||
$certificate = $signatureData['certificate']; | ||
|
||
// Check if certificate is signed by Nextcloud Root Authority | ||
|
@@ -357,8 +370,19 @@ private function verify(string $signaturePath, string $basePath, string $certifi | |
$rsa->setMGFHash('sha512'); | ||
// See https://tools.ietf.org/html/rfc3447#page-38 | ||
$rsa->setSaltLength(0); | ||
if (!$rsa->verify(json_encode($expectedHashes), $signature)) { | ||
throw new InvalidSignatureException('Signature could not get verified.'); | ||
|
||
if ($forceHash && isset($signatureData['signatures'][$forceHash])) { | ||
// Check the sha512 hash | ||
$rsa->setHash($forceHash); | ||
if (!$rsa->verify(json_encode($expectedHashes), base64_decode($signatureData['signatures'][$forceHash]))) { | ||
throw new InvalidSignatureException('Signature could not get verified.'); | ||
} | ||
} else { | ||
// The default fallback with the sha1 hash | ||
$rsa->setHash('sha1'); | ||
if (!$rsa->verify(json_encode($expectedHashes), base64_decode($signatureData['signature']))) { | ||
throw new InvalidSignatureException('Signature could not get verified.'); | ||
} | ||
} | ||
|
||
// Fixes for the updater as shipped with ownCloud 9.0.x: The updater is | ||
|
@@ -502,10 +526,18 @@ public function verifyAppSignature(string $appId, string $path = ''): array { | |
if ($path === '') { | ||
$path = $this->appLocator->getAppPath($appId); | ||
} | ||
|
||
$minVersion = $this->infoParser->getMinVersion($path . '/appinfo/info.xml'); | ||
$forceHash = null; | ||
if ($minVersion >= 22) { | ||
$forceHash = 'sha512'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you elaborate the reason for the hardcoding here? :) |
||
} | ||
|
||
$result = $this->verify( | ||
$path . '/appinfo/signature.json', | ||
$path, | ||
$appId | ||
$path . '/appinfo/signature.json', | ||
$path, | ||
$appId, | ||
$forceHash | ||
); | ||
} catch (\Exception $e) { | ||
$result = [ | ||
|
@@ -553,9 +585,10 @@ public function verifyAppSignature(string $appId, string $path = ''): array { | |
public function verifyCoreSignature(): array { | ||
try { | ||
$result = $this->verify( | ||
$this->environmentHelper->getServerRoot() . '/core/signature.json', | ||
$this->environmentHelper->getServerRoot(), | ||
'core' | ||
$this->environmentHelper->getServerRoot() . '/core/signature.json', | ||
$this->environmentHelper->getServerRoot(), | ||
'core', | ||
'sha512' | ||
); | ||
} catch (\Exception $e) { | ||
$result = [ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* @copyright Copyright (c) 2021, Roeland Jago Douma <roeland@famdouma.nl> | ||
* | ||
* @author Roeland Jago Douma <roeland@famdouma.nl> | ||
* | ||
* @license GNU AGPL version 3 or any later version | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License as | ||
* published by the Free Software Foundation, either version 3 of the | ||
* License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
* | ||
*/ | ||
|
||
namespace OC\IntegrityCheck\Helpers; | ||
|
||
use OC\App\InfoParser as RealParser; | ||
|
||
class InfoParser { | ||
public function getMinVersion(string $infoPath): int { | ||
$parser = new RealParser(); | ||
$result = $parser->parse($infoPath); | ||
|
||
if (!isset($result['dependencies']['nextcloud']['@attributes']['min-version'])) { | ||
throw new \Exception('Could not parse nextcloud dependency'); | ||
} | ||
|
||
return (int)$result['dependencies']['nextcloud']['@attributes']['min-version']; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That scares me a bit because it could eventually lead to something like https://auth0.com/blog/critical-vulnerabilities-in-json-web-token-libraries/, in which there is a
null
algorithm or something eventually.Can we instead pass a version identifier (e.g. 2 == SHA512)?