1
|
|
|
<?php |
2
|
|
|
namespace phpbu\App\Backup\Crypter; |
3
|
|
|
|
4
|
|
|
use phpbu\App\Backup\Restore\Plan; |
5
|
|
|
use phpbu\App\Backup\Target; |
6
|
|
|
use phpbu\App\Cli\Executable; |
7
|
|
|
use phpbu\App\Result; |
8
|
|
|
use phpbu\App\Util; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* OpenSSL crypter class |
12
|
|
|
* |
13
|
|
|
* @package phpbu |
14
|
|
|
* @subpackage Backup |
15
|
|
|
* @author Sebastian Feldmann <[email protected]> |
16
|
|
|
* @copyright Sebastian Feldmann <[email protected]> |
17
|
|
|
* @license https://opensource.org/licenses/MIT The MIT License (MIT) |
18
|
|
|
* @link https://phpbu.de/ |
19
|
|
|
* @since Class available since Release 2.1.6 |
20
|
|
|
*/ |
21
|
|
|
class OpenSSL extends Abstraction implements Simulator, Restorable |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* Path to mcrypt command |
25
|
|
|
* |
26
|
|
|
* @var string |
27
|
|
|
*/ |
28
|
|
|
private $pathToOpenSSL; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Key file |
32
|
|
|
* |
33
|
|
|
* @var string |
34
|
|
|
*/ |
35
|
|
|
private $certFile; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Algorithm to use |
39
|
|
|
* |
40
|
|
|
* @var string |
41
|
|
|
*/ |
42
|
|
|
private $algorithm; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Password to use |
46
|
|
|
* |
47
|
|
|
* @var string |
48
|
|
|
*/ |
49
|
|
|
private $password; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Keep the not encrypted file |
53
|
|
|
* |
54
|
|
|
* @var boolean |
55
|
|
|
*/ |
56
|
|
|
private $keepUncrypted; |
57
|
|
|
|
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @inheritDoc |
61
|
|
|
*/ |
62
|
|
|
public function crypt(Target $target, Result $result) |
63
|
|
|
{ |
64
|
10 |
|
if ($this->getExecutable($target)->isUsingWeakAlgorithm()) { |
|
|
|
|
65
|
|
|
$name = strtolower(get_class($this)); |
66
|
10 |
|
|
67
|
1 |
|
$result->warn($name . ': The ' . $this->algorithm . ' algorithm is considered weak'); |
68
|
|
|
} |
69
|
9 |
|
|
70
|
9 |
|
return parent::crypt($target, $result); |
|
|
|
|
71
|
1 |
|
} |
72
|
|
|
|
73
|
|
|
|
74
|
8 |
|
/** |
75
|
8 |
|
* @inheritDoc |
76
|
8 |
|
*/ |
77
|
8 |
|
public function simulate(Target $target, Result $result) |
78
|
8 |
|
{ |
79
|
8 |
|
if ($this->getExecutable($target)->isUsingWeakAlgorithm()) { |
80
|
|
|
$name = strtolower(get_class($this)); |
81
|
|
|
|
82
|
|
|
$result->warn($name . ': The ' . $this->algorithm . ' algorithm is considered weak'); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
return parent::simulate($target, $result); |
|
|
|
|
86
|
|
|
} |
87
|
1 |
|
|
88
|
|
|
/** |
89
|
1 |
|
* Setup |
90
|
|
|
* |
91
|
|
|
* @see \phpbu\App\Backup\Crypter |
92
|
|
|
* @param array $options |
93
|
|
|
* @throws Exception |
94
|
|
|
*/ |
95
|
|
|
public function setup(array $options = []) |
96
|
|
|
{ |
97
|
|
|
if (!Util\Arr::isSetAndNotEmptyString($options, 'algorithm')) { |
98
|
|
|
throw new Exception('openssl expects \'algorithm\''); |
99
|
2 |
|
} |
100
|
|
|
if (!Util\Arr::isSetAndNotEmptyString($options, 'password') |
101
|
2 |
|
&& !Util\Arr::isSetAndNotEmptyString($options, 'certFile')) { |
102
|
2 |
|
throw new Exception('openssl expects \'certFile\' or \'password\''); |
103
|
2 |
|
} |
104
|
|
|
|
105
|
|
|
$this->pathToOpenSSL = Util\Arr::getValue($options, 'pathToOpenSSL', ''); |
106
|
|
|
$this->keepUncrypted = Util\Str::toBoolean(Util\Arr::getValue($options, 'keepUncrypted', ''), false); |
107
|
|
|
$this->certFile = $this->toAbsolutePath(Util\Arr::getValue($options, 'certFile', '')); |
108
|
|
|
$this->algorithm = Util\Arr::getValue($options, 'algorithm', ''); |
109
|
|
|
$this->password = Util\Arr::getValue($options, 'password', ''); |
110
|
|
|
} |
111
|
|
|
|
112
|
5 |
|
/** |
113
|
|
|
* Return file suffix of encrypted target |
114
|
5 |
|
* |
115
|
|
|
* @see \phpbu\App\Backup\Crypter |
116
|
|
|
* @return string |
117
|
|
|
*/ |
118
|
|
|
public function getSuffix() : string |
119
|
|
|
{ |
120
|
|
|
return 'enc'; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
5 |
|
* Decrypt the backup |
125
|
|
|
* |
126
|
5 |
|
* @param \phpbu\App\Backup\Target $target |
127
|
5 |
|
* @param \phpbu\App\Backup\Restore\Plan $plan |
128
|
5 |
|
* @throws \phpbu\App\Exception |
129
|
|
|
*/ |
130
|
5 |
|
public function restore(Target $target, Plan $plan) |
131
|
|
|
{ |
132
|
|
|
$executable = $this->createDecryptionOpenSSL($target); |
133
|
|
|
$plan->addDecryptionCommand($executable->getCommand()); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Create the Executable to run the 'mcrypt' command |
138
|
|
|
* |
139
|
|
|
* @param \phpbu\App\Backup\Target $target |
140
|
2 |
|
* @return \phpbu\App\Cli\Executable |
141
|
|
|
* @throws \phpbu\App\Exception |
142
|
2 |
|
*/ |
143
|
2 |
|
protected function createExecutable(Target $target) : Executable |
144
|
2 |
|
{ |
145
|
|
|
return $this->createEncryptionOpenSSL($target); |
146
|
2 |
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Create encryption OpenSSL |
150
|
|
|
* |
151
|
|
|
* @param \phpbu\App\Backup\Target $target |
152
|
|
|
* @return \phpbu\App\Cli\Executable\OpenSSL |
153
|
|
|
* @throws \phpbu\App\Exception |
154
|
|
|
*/ |
155
|
|
|
private function createEncryptionOpenSSL(Target $target): Executable\OpenSSL |
156
|
7 |
|
{ |
157
|
|
|
$executable = $this->createOpenSSL($target); |
158
|
7 |
|
$executable->encryptFile($target->getPathname()) |
159
|
|
|
->deleteSource(!$this->keepUncrypted); |
160
|
7 |
|
|
161
|
4 |
|
return $executable; |
162
|
|
|
} |
163
|
3 |
|
|
164
|
3 |
|
/** |
165
|
|
|
* Create decryption OpenSSL |
166
|
7 |
|
* |
167
|
|
|
* @param \phpbu\App\Backup\Target $target |
168
|
7 |
|
* @return \phpbu\App\Cli\Executable\OpenSSL |
169
|
|
|
* @throws \phpbu\App\Exception |
170
|
|
|
*/ |
171
|
|
|
private function createDecryptionOpenSSL(Target $target): Executable\OpenSSL |
172
|
|
|
{ |
173
|
|
|
$executable = $this->createOpenSSL($target); |
174
|
|
|
$executable->decryptFile($target->getPathname()) |
175
|
|
|
->deleteSource(false); |
176
|
|
|
|
177
|
|
|
return $executable; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* Setup an OpenSSL executable only thing missing is the decision of en or decryption |
182
|
|
|
* |
183
|
|
|
* @param \phpbu\App\Backup\Target $target |
184
|
|
|
* @return \phpbu\App\Cli\Executable\OpenSSL |
185
|
|
|
* @throws \phpbu\App\Exception |
186
|
|
|
*/ |
187
|
|
|
private function createOpenSSL(Target $target): Executable\OpenSSL |
|
|
|
|
188
|
|
|
{ |
189
|
|
|
$executable = new Executable\OpenSSL($this->pathToOpenSSL); |
190
|
|
|
// use key or password to encrypt |
191
|
|
|
if (!empty($this->certFile)) { |
192
|
|
|
$executable->useSSLCert($this->certFile); |
193
|
|
|
} else { |
194
|
|
|
$executable->usePassword($this->password) |
195
|
|
|
->encodeBase64(true); |
196
|
|
|
} |
197
|
|
|
$executable->useAlgorithm($this->algorithm); |
198
|
|
|
|
199
|
|
|
return $executable; |
200
|
|
|
} |
201
|
|
|
} |
202
|
|
|
|