Harden identifyproof openssl code

Signed-off-by: Roeland Jago Douma <roeland@famdouma.nl>
This commit is contained in:
Roeland Jago Douma 2019-09-09 20:39:19 +02:00
parent 89d3b2cdd3
commit 2b98eea129
No known key found for this signature in database
GPG Key ID: F941078878347C0C
3 changed files with 42 additions and 22 deletions

View File

@ -29,6 +29,7 @@ namespace OC\Security\IdentityProof;
use OC\Files\AppData\Factory; use OC\Files\AppData\Factory;
use OCP\Files\IAppData; use OCP\Files\IAppData;
use OCP\IConfig; use OCP\IConfig;
use OCP\ILogger;
use OCP\IUser; use OCP\IUser;
use OCP\Security\ICrypto; use OCP\Security\ICrypto;
@ -39,19 +40,18 @@ class Manager {
private $crypto; private $crypto;
/** @var IConfig */ /** @var IConfig */
private $config; private $config;
/** @var ILogger */
private $logger;
/**
* @param Factory $appDataFactory
* @param ICrypto $crypto
* @param IConfig $config
*/
public function __construct(Factory $appDataFactory, public function __construct(Factory $appDataFactory,
ICrypto $crypto, ICrypto $crypto,
IConfig $config IConfig $config,
ILogger $logger
) { ) {
$this->appData = $appDataFactory->get('identityproof'); $this->appData = $appDataFactory->get('identityproof');
$this->crypto = $crypto; $this->crypto = $crypto;
$this->config = $config; $this->config = $config;
$this->logger = $logger;
} }
/** /**
@ -59,6 +59,7 @@ class Manager {
* In a separate function for unit testing purposes. * In a separate function for unit testing purposes.
* *
* @return array [$publicKey, $privateKey] * @return array [$publicKey, $privateKey]
* @throws \RuntimeException
*/ */
protected function generateKeyPair(): array { protected function generateKeyPair(): array {
$config = [ $config = [
@ -68,7 +69,16 @@ class Manager {
// Generate new key // Generate new key
$res = openssl_pkey_new($config); $res = openssl_pkey_new($config);
openssl_pkey_export($res, $privateKey);
if ($res === false) {
$this->logOpensslError();
throw new \RuntimeException('OpenSSL reported a problem');
}
if (openssl_pkey_export($res, $privateKey, null, $config) === false) {
$this->logOpensslError();
throw new \RuntimeException('OpenSSL reported a problem');
}
// Extract the public key from $res to $pubKey // Extract the public key from $res to $pubKey
$publicKey = openssl_pkey_get_details($res); $publicKey = openssl_pkey_get_details($res);
@ -83,6 +93,7 @@ class Manager {
* *
* @param string $id key id * @param string $id key id
* @return Key * @return Key
* @throws \RuntimeException
*/ */
protected function generateKey(string $id): Key { protected function generateKey(string $id): Key {
list($publicKey, $privateKey) = $this->generateKeyPair(); list($publicKey, $privateKey) = $this->generateKeyPair();
@ -105,6 +116,7 @@ class Manager {
* *
* @param string $id * @param string $id
* @return Key * @return Key
* @throws \RuntimeException
*/ */
protected function retrieveKey(string $id): Key { protected function retrieveKey(string $id): Key {
try { try {
@ -124,6 +136,7 @@ class Manager {
* *
* @param IUser $user * @param IUser $user
* @return Key * @return Key
* @throws \RuntimeException
*/ */
public function getKey(IUser $user): Key { public function getKey(IUser $user): Key {
$uid = $user->getUID(); $uid = $user->getUID();
@ -144,5 +157,13 @@ class Manager {
return $this->retrieveKey('system-' . $instanceId); return $this->retrieveKey('system-' . $instanceId);
} }
private function logOpensslError(): void {
$errors = [];
while ($error = openssl_error_string()) {
$errors[] = $error;
}
$this->logger->critical('Something is wrong with your openssl setup: ' . implode(', ', $errors));
}
} }

View File

@ -1186,14 +1186,6 @@ class Server extends ServerContainer implements IServerContainer {
$this->registerAlias(IDashboardManager::class, DashboardManager::class); $this->registerAlias(IDashboardManager::class, DashboardManager::class);
$this->registerAlias(IFullTextSearchManager::class, FullTextSearchManager::class); $this->registerAlias(IFullTextSearchManager::class, FullTextSearchManager::class);
$this->registerService(\OC\Security\IdentityProof\Manager::class, function (Server $c) {
return new \OC\Security\IdentityProof\Manager(
$c->query(\OC\Files\AppData\Factory::class),
$c->getCrypto(),
$c->getConfig()
);
});
$this->registerAlias(ISubAdmin::class, SubAdmin::class); $this->registerAlias(ISubAdmin::class, SubAdmin::class);
$this->registerAlias(IInitialStateService::class, InitialStateService::class); $this->registerAlias(IInitialStateService::class, InitialStateService::class);

View File

@ -29,22 +29,26 @@ use OCP\Files\IAppData;
use OCP\Files\SimpleFS\ISimpleFile; use OCP\Files\SimpleFS\ISimpleFile;
use OCP\Files\SimpleFS\ISimpleFolder; use OCP\Files\SimpleFS\ISimpleFolder;
use OCP\IConfig; use OCP\IConfig;
use OCP\ILogger;
use OCP\IUser; use OCP\IUser;
use OCP\Security\ICrypto; use OCP\Security\ICrypto;
use PHPUnit\Framework\MockObject\MockObject;
use SebastianBergmann\Comparator\MockObjectComparator; use SebastianBergmann\Comparator\MockObjectComparator;
use Test\TestCase; use Test\TestCase;
class ManagerTest extends TestCase { class ManagerTest extends TestCase {
/** @var Factory|\PHPUnit_Framework_MockObject_MockObject */ /** @var Factory|MockObject */
private $factory; private $factory;
/** @var IAppData|\PHPUnit_Framework_MockObject_MockObject */ /** @var IAppData|MockObject */
private $appData; private $appData;
/** @var ICrypto|\PHPUnit_Framework_MockObject_MockObject */ /** @var ICrypto|MockObject */
private $crypto; private $crypto;
/** @var Manager|\PHPUnit_Framework_MockObject_MockObject */ /** @var Manager|MockObject */
private $manager; private $manager;
/** @var IConfig|\PHPUnit_Framework_MockObject_MockObject */ /** @var IConfig|MockObject */
private $config; private $config;
/** @var ILogger|MockObject */
private $logger;
public function setUp() { public function setUp() {
parent::setUp(); parent::setUp();
@ -57,6 +61,7 @@ class ManagerTest extends TestCase {
->method('get') ->method('get')
->with('identityproof') ->with('identityproof')
->willReturn($this->appData); ->willReturn($this->appData);
$this->logger = $this->createMock(ILogger::class);
$this->crypto = $this->createMock(ICrypto::class); $this->crypto = $this->createMock(ICrypto::class);
$this->manager = $this->getManager(['generateKeyPair']); $this->manager = $this->getManager(['generateKeyPair']);
@ -73,14 +78,16 @@ class ManagerTest extends TestCase {
return new Manager( return new Manager(
$this->factory, $this->factory,
$this->crypto, $this->crypto,
$this->config $this->config,
$this->logger
); );
} else { } else {
return $this->getMockBuilder(Manager::class) return $this->getMockBuilder(Manager::class)
->setConstructorArgs([ ->setConstructorArgs([
$this->factory, $this->factory,
$this->crypto, $this->crypto,
$this->config $this->config,
$this->logger
])->setMethods($setMethods)->getMock(); ])->setMethods($setMethods)->getMock();
} }
} }