Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions lib/private/Security/CertificateManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
use OCP\ICertificateManager;
use OCP\IConfig;
use OCP\ILogger;
use OCP\Security\ISecureRandom;

/**
* Manage trusted certificates for users
Expand All @@ -56,17 +57,26 @@ class CertificateManager implements ICertificateManager {
*/
protected $logger;

/** @var ISecureRandom */
protected $random;

/**
* @param string $uid
* @param \OC\Files\View $view relative to data/
* @param IConfig $config
* @param ILogger $logger
* @param ISecureRandom $random
*/
public function __construct($uid, \OC\Files\View $view, IConfig $config, ILogger $logger) {
public function __construct($uid,
\OC\Files\View $view,
IConfig $config,
ILogger $logger,
ISecureRandom $random) {
$this->uid = $uid;
$this->view = $view;
$this->config = $config;
$this->logger = $logger;
$this->random = $random;
}

/**
Expand Down Expand Up @@ -120,7 +130,8 @@ public function createCertificateBundle() {
}

$certPath = $path . 'rootcerts.crt';
$fhCerts = $this->view->fopen($certPath, 'w');
$tmpPath = $certPath . '.tmp' . $this->random->generate(10, ISecureRandom::CHAR_DIGITS);
$fhCerts = $this->view->fopen($tmpPath, 'w');

// Write user certificates
foreach ($certs as $cert) {
Expand All @@ -143,6 +154,8 @@ public function createCertificateBundle() {
}

fclose($fhCerts);

$this->view->rename($tmpPath, $certPath);
}

/**
Expand Down Expand Up @@ -218,7 +231,7 @@ public function getAbsoluteBundlePath($uid = '') {
}
if ($this->needsRebundling($uid)) {
if (is_null($uid)) {
$manager = new CertificateManager(null, $this->view, $this->config, $this->logger);
$manager = new CertificateManager(null, $this->view, $this->config, $this->logger, $this->random);
$manager->createCertificateBundle();
} else {
$this->createCertificateBundle();
Expand Down
16 changes: 14 additions & 2 deletions lib/private/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -596,7 +596,13 @@ public function __construct($webRoot, \OC\Config $config) {
$uid = $user ? $user : null;
return new ClientService(
$c->getConfig(),
new \OC\Security\CertificateManager($uid, new View(), $c->getConfig(), $c->getLogger())
new \OC\Security\CertificateManager(
$uid,
new View(),
$c->getConfig(),
$c->getLogger(),
$c->getSecureRandom()
)
);
});
$this->registerAlias('HttpClientService', \OCP\Http\Client\IClientService::class);
Expand Down Expand Up @@ -1438,7 +1444,13 @@ public function getCertificateManager($userId = '') {
}
$userId = $user->getUID();
}
return new CertificateManager($userId, new View(), $this->getConfig(), $this->getLogger());
return new CertificateManager(
$userId,
new View(),
$this->getConfig(),
$this->getLogger(),
$this->getSecureRandom()
);
}

/**
Expand Down
17 changes: 15 additions & 2 deletions tests/lib/Security/CertificateManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use \OC\Security\CertificateManager;
use OCP\IConfig;
use OCP\ILogger;
use OCP\Security\ISecureRandom;

/**
* Class CertificateManagerTest
Expand All @@ -26,6 +27,8 @@ class CertificateManagerTest extends \Test\TestCase {
private $certificateManager;
/** @var String */
private $username;
/** @var ISecureRandom */
private $random;

protected function setUp() {
parent::setUp();
Expand All @@ -45,7 +48,17 @@ protected function setUp() {
$config->expects($this->any())->method('getSystemValue')
->with('installed', false)->willReturn(true);

$this->certificateManager = new CertificateManager($this->username, new \OC\Files\View(), $config, $this->createMock(ILogger::class));
$this->random = $this->createMock(ISecureRandom::class);
$this->random->method('generate')
->willReturn('random');

$this->certificateManager = new CertificateManager(
$this->username,
new \OC\Files\View(),
$config,
$this->createMock(ILogger::class),
$this->random
);
}

protected function tearDown() {
Expand Down Expand Up @@ -145,7 +158,7 @@ function testNeedRebundling($uid,

/** @var CertificateManager | \PHPUnit_Framework_MockObject_MockObject $certificateManager */
$certificateManager = $this->getMockBuilder('OC\Security\CertificateManager')
->setConstructorArgs([$uid, $view, $config, $this->createMock(ILogger::class)])
->setConstructorArgs([$uid, $view, $config, $this->createMock(ILogger::class), $this->random])
->setMethods(['getFilemtimeOfCaBundle', 'getCertificateBundle'])
->getMock();

Expand Down