|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace LE_ACME2; |
|
4
|
|
|
|
|
5
|
|
|
use LE_ACME2\Connector\Connector; |
|
6
|
|
|
|
|
7
|
|
|
abstract class AbstractKeyValuable { |
|
8
|
|
|
|
|
9
|
|
|
const KEY_TYPE_RSA = "RSA"; |
|
10
|
|
|
const KEY_TYPE_EC = "EC"; |
|
11
|
|
|
|
|
12
|
|
|
protected $_identifier; |
|
13
|
|
|
|
|
14
|
|
|
protected static $_directoryPath = null; |
|
15
|
|
|
|
|
16
|
|
|
public static function setCommonKeyDirectoryPath(string $directoryPath) { |
|
17
|
|
|
|
|
18
|
|
|
if(!file_exists($directoryPath)) { |
|
19
|
|
|
throw new \RuntimeException('Common Key Directory Path does not exist'); |
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
|
|
self::$_directoryPath = realpath($directoryPath) . DIRECTORY_SEPARATOR; |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
public static function getCommonKeyDirectoryPath() : ?string { |
|
26
|
|
|
return self::$_directoryPath; |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
protected function _getKeyDirectoryPath(string $appendix = '') : string { |
|
30
|
|
|
|
|
31
|
|
|
if(self::$_directoryPath === null) { |
|
32
|
|
|
throw new \RuntimeException('Common Key Directory Path is not set'); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
return self::$_directoryPath . $this->_identifier . $appendix . DIRECTORY_SEPARATOR; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
public function getKeyDirectoryPath() : string { |
|
39
|
|
|
|
|
40
|
|
|
return $this->_getKeyDirectoryPath(''); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
protected function _initKeyDirectory(string $keyType = self::KEY_TYPE_RSA, bool $ignoreIfKeysExist = false) { |
|
44
|
|
|
|
|
45
|
|
|
if(!file_exists($this->getKeyDirectoryPath())) { |
|
46
|
|
|
|
|
47
|
|
|
mkdir($this->getKeyDirectoryPath()); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
if(!$ignoreIfKeysExist && ( |
|
51
|
|
|
file_exists($this->getKeyDirectoryPath() . 'private.pem') || |
|
52
|
|
|
file_exists($this->getKeyDirectoryPath() . 'public.pem') |
|
53
|
|
|
) |
|
54
|
|
|
) { |
|
55
|
|
|
|
|
56
|
|
|
throw new \RuntimeException( |
|
57
|
|
|
'Keys exist already. Exists the ' . get_class($this) . ' already?' . PHP_EOL . |
|
58
|
|
|
'Path: ' . $this->getKeyDirectoryPath() |
|
59
|
|
|
); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
if($keyType == self::KEY_TYPE_RSA) { |
|
63
|
|
|
|
|
64
|
|
|
Utilities\KeyGenerator::RSA( |
|
65
|
|
|
$this->getKeyDirectoryPath(), |
|
66
|
|
|
'private.pem', |
|
67
|
|
|
'public.pem' |
|
68
|
|
|
); |
|
69
|
|
|
} else if($keyType == self::KEY_TYPE_EC) { |
|
70
|
|
|
|
|
71
|
|
|
Utilities\KeyGenerator::EC( |
|
72
|
|
|
$this->getKeyDirectoryPath(), |
|
73
|
|
|
'private.pem', |
|
74
|
|
|
'public.pem' |
|
75
|
|
|
); |
|
76
|
|
|
} else { |
|
77
|
|
|
|
|
78
|
|
|
throw new \RuntimeException('Key type "' . $keyType . '" not supported.'); |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
protected function _clearKeyDirectory() { |
|
83
|
|
|
|
|
84
|
|
|
if(file_exists($this->getKeyDirectoryPath() . 'private.pem')) { |
|
85
|
|
|
unlink($this->getKeyDirectoryPath() . 'private.pem'); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
if(file_exists($this->getKeyDirectoryPath() . 'public.pem')) { |
|
89
|
|
|
unlink($this->getKeyDirectoryPath() . 'public.pem'); |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
protected function _getAccountIdentifier(Account $account) : string { |
|
94
|
|
|
|
|
95
|
|
|
$staging = Connector::getInstance()->isUsingStagingServer(); |
|
|
|
|
|
|
96
|
|
|
|
|
97
|
|
|
return 'account_' . ($staging ? 'staging_' : 'live_') . $account->getEmail(); |
|
98
|
|
|
} |
|
99
|
|
|
} |