Skip to content

Commit 9e03890

Browse files
committed
feat(CertificateManager): Add option to specify the default certificates bundle path
Signed-off-by: provokateurin <[email protected]>
1 parent 1dd5417 commit 9e03890

File tree

7 files changed

+37
-8
lines changed

7 files changed

+37
-8
lines changed

config/config.sample.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2572,4 +2572,13 @@
25722572
* Defaults to ``true``
25732573
*/
25742574
'enable_non-accessible_features' => true,
2575+
2576+
/**
2577+
* Change the default certificates bundle used for trusting certificates.
2578+
*
2579+
* Nextcloud ships its own up-to-date certificates bundle, but in certain cases admins may wish to specify a different bundle, for example the one shipped by their distro.
2580+
*
2581+
* Defaults to `\OC::$SERVERROOT . '/resources/config/ca-bundle.crt'`.
2582+
*/
2583+
'default_certificates_bundle_path' => \OC::$SERVERROOT . '/resources/config/ca-bundle.crt',
25752584
];

lib/private/Files/ObjectStore/S3ConnectionTrait.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -213,13 +213,13 @@ protected function paramCredentialProvider(): callable {
213213

214214
protected function getCertificateBundlePath(): ?string {
215215
if ((int) ($this->params['use_nextcloud_bundle'] ?? '0')) {
216+
/** @var ICertificateManager $certManager */
217+
$certManager = Server::get(ICertificateManager::class);
216218
// since we store the certificate bundles on the primary storage, we can't get the bundle while setting up the primary storage
217219
if (!isset($this->params['primary_storage'])) {
218-
/** @var ICertificateManager $certManager */
219-
$certManager = Server::get(ICertificateManager::class);
220220
return $certManager->getAbsoluteBundlePath();
221221
} else {
222-
return \OC::$SERVERROOT . '/resources/config/ca-bundle.crt';
222+
return $certManager->getDefaultCertificatesBundlePath();
223223
}
224224
} else {
225225
return null;

lib/private/Http/Client/Client.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ private function getCertBundle(): string {
102102
// $this->certificateManager->getAbsoluteBundlePath() tries to instantiate
103103
// a view
104104
if (!$this->config->getSystemValueBool('installed', false)) {
105-
return \OC::$SERVERROOT . '/resources/config/ca-bundle.crt';
105+
return $this->certificateManager->getDefaultCertificatesBundlePath();
106106
}
107107

108108
return $this->certificateManager->getAbsoluteBundlePath();

lib/private/Security/CertificateManager.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ public function createCertificateBundle(): void {
100100
$this->view->mkdir($path);
101101
}
102102

103-
$defaultCertificates = file_get_contents(\OC::$SERVERROOT . '/resources/config/ca-bundle.crt');
103+
$defaultCertificates = file_get_contents($this->getDefaultCertificatesBundlePath());
104104
if (strlen($defaultCertificates) < 1024) { // sanity check to verify that we have some content for our bundle
105105
// log as exception so we have a stacktrace
106106
$e = new \Exception('Shipped ca-bundle is empty, refusing to create certificate bundle');
@@ -204,7 +204,7 @@ public function getAbsoluteBundlePath(): string {
204204
try {
205205
if ($this->bundlePath === null) {
206206
if (!$this->hasCertificates()) {
207-
$this->bundlePath = \OC::$SERVERROOT . '/resources/config/ca-bundle.crt';
207+
$this->bundlePath = $this->getDefaultCertificatesBundlePath();
208208
} else {
209209
if ($this->needsRebundling()) {
210210
$this->createCertificateBundle();
@@ -221,7 +221,7 @@ public function getAbsoluteBundlePath(): string {
221221
return $this->bundlePath;
222222
} catch (\Exception $e) {
223223
$this->logger->error('Failed to get absolute bundle path. Fallback to default ca-bundle.crt', ['exception' => $e]);
224-
return \OC::$SERVERROOT . '/resources/config/ca-bundle.crt';
224+
return $this->getDefaultCertificatesBundlePath();
225225
}
226226
}
227227

@@ -246,6 +246,10 @@ private function needsRebundling(): bool {
246246
* get mtime of ca-bundle shipped by Nextcloud
247247
*/
248248
protected function getFilemtimeOfCaBundle(): int {
249-
return filemtime(\OC::$SERVERROOT . '/resources/config/ca-bundle.crt');
249+
return filemtime($this->getDefaultCertificatesBundlePath());
250+
}
251+
252+
public function getDefaultCertificatesBundlePath(): string {
253+
return $this->config->getSystemValueString('default_certificates_bundle_path', \OC::$SERVERROOT . '/resources/config/ca-bundle.crt');
250254
}
251255
}

lib/public/ICertificateManager.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,11 @@ public function getCertificateBundle(): string;
5252
* @since 9.0.0
5353
*/
5454
public function getAbsoluteBundlePath(): string;
55+
56+
/**
57+
* Get the path of the default certificates bundle.
58+
*
59+
* @since 33.0.0
60+
*/
61+
public function getDefaultCertificatesBundlePath(): string;
5562
}

tests/lib/Http/Client/ClientTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -471,6 +471,10 @@ public function testSetDefaultOptionsWithNotInstalled(): void {
471471
$this->certificateManager
472472
->expects($this->never())
473473
->method('listCertificates');
474+
$this->certificateManager
475+
->expects($this->once())
476+
->method('getDefaultCertificatesBundlePath')
477+
->willReturn(\OC::$SERVERROOT . '/resources/config/ca-bundle.crt');
474478

475479
$this->assertEquals([
476480
'verify' => \OC::$SERVERROOT . '/resources/config/ca-bundle.crt',

tests/lib/Security/CertificateManagerTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,11 @@ protected function setUp(): void {
4848
$config = $this->createMock(IConfig::class);
4949
$config->expects($this->any())->method('getSystemValueBool')
5050
->with('installed', false)->willReturn(true);
51+
$config
52+
->expects($this->any())
53+
->method('getSystemValueString')
54+
->with('default_certificates_bundle_path', \OC::$SERVERROOT . '/resources/config/ca-bundle.crt')
55+
->willReturn(\OC::$SERVERROOT . '/resources/config/ca-bundle.crt');
5156

5257
$this->random = $this->createMock(ISecureRandom::class);
5358
$this->random->method('generate')

0 commit comments

Comments
 (0)