Completed
Push — master ( ba9b17...94004c )
by Lukas
05:19 queued 04:59
created

Manager::generateKey()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 11
nc 2
nop 1
dl 0
loc 15
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * @copyright Copyright (c) 2016 Lukas Reschke <[email protected]>
4
 *
5
 * @license GNU AGPL version 3 or any later version
6
 *
7
 * This program is free software: you can redistribute it and/or modify
8
 * it under the terms of the GNU Affero General Public License as
9
 * published by the Free Software Foundation, either version 3 of the
10
 * License, or (at your option) any later version.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 * GNU Affero General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Affero General Public License
18
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
 *
20
 */
21
22
namespace OC\Security\IdentityProof;
23
24
use OCP\Files\IAppData;
25
use OCP\IUser;
26
use OCP\Security\ICrypto;
27
28
class Manager {
29
	/** @var IAppData */
30
	private $appData;
31
	/** @var ICrypto */
32
	private $crypto;
33
34
	/**
35
	 * @param IAppData $appData
36
	 * @param ICrypto $crypto
37
	 */
38
	public function __construct(IAppData $appData,
39
								ICrypto $crypto) {
40
		$this->appData = $appData;
41
		$this->crypto = $crypto;
42
	}
43
44
	/**
45
	 * Calls the openssl functions to generate a public and private key.
46
	 * In a separate function for unit testing purposes.
47
	 *
48
	 * @return array [$publicKey, $privateKey]
49
	 */
50
	protected function generateKeyPair() {
51
		$config = [
52
			'digest_alg' => 'sha512',
53
			'private_key_bits' => 2048,
54
		];
55
56
		// Generate new key
57
		$res = openssl_pkey_new($config);
58
		openssl_pkey_export($res, $privateKey);
59
60
		// Extract the public key from $res to $pubKey
61
		$publicKey = openssl_pkey_get_details($res);
62
		$publicKey = $publicKey['key'];
63
64
		return [$publicKey, $privateKey];
65
	}
66
67
	/**
68
	 * Generate a key for $user
69
	 * Note: If a key already exists it will be overwritten
70
	 *
71
	 * @param IUser $user
72
	 * @return Key
73
	 */
74
	protected function generateKey(IUser $user) {
75
		list($publicKey, $privateKey) = $this->generateKeyPair();
76
77
		// Write the private and public key to the disk
78
		try {
79
			$this->appData->newFolder($user->getUID());
80
		} catch (\Exception $e) {}
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
81
		$folder = $this->appData->getFolder($user->getUID());
82
		$folder->newFile('private')
83
			->putContent($this->crypto->encrypt($privateKey));
84
		$folder->newFile('public')
85
			->putContent($publicKey);
86
87
		return new Key($publicKey, $privateKey);
88
	}
89
90
	/**
91
	 * Get public and private key for $user
92
	 *
93
	 * @param IUser $user
94
	 * @return Key
95
	 */
96
	public function getKey(IUser $user) {
97
		try {
98
			$folder = $this->appData->getFolder($user->getUID());
99
			$privateKey = $this->crypto->decrypt(
100
				$folder->getFile('private')->getContent()
101
			);
102
			$publicKey = $folder->getFile('public')->getContent();
103
			return new Key($publicKey, $privateKey);
104
		} catch (\Exception $e) {
105
			return $this->generateKey($user);
106
		}
107
	}
108
}
109