Issues (83)

src/Backup/Crypter/OpenSSL.php (1 issue)

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
    private $weakAlgorithms = [
59
        'rc2'          => true,
60
        'rc2-40'       => true,
61
        'rc2-64'       => true,
62
        'rc2-128'      => true,
63
        'rc2-40-cbc'   => true,
64 10
        'rc2-64-cbc'   => true,
65
        'rc2-cbc'      => true,
66 10
        'rc2-cfb'      => true,
67 1
        'rc2-ecb'      => true,
68
        'rc2-ofb'      => true,
69 9
        'rc4'          => true,
70 9
        'rc4-40'       => true,
71 1
        'des'          => true,
72
        'des-cbc'      => true,
73
        'des-cfb'      => true,
74 8
        'des-ecb'      => true,
75 8
        'des-ede'      => true,
76 8
        'des-ede-cbc'  => true,
77 8
        'des-ede-cfb'  => true,
78 8
        'des-ede-ofb'  => true,
79 8
        'des-ede3'     => true,
80
        'des-ede3-cbc' => true,
81
        'des-ede3-cfb' => true,
82
        'des-ede3-ofb' => true,
83
        'des-ofb'      => true,
84
        'des3'         => true,
85
        'desx'         => true,
86
        'seed'         => true,
87 1
        'seed-cbc'     => true,
88
        'seed-cfb'     => true,
89 1
        'seed-ecb'     => true,
90
        'seed-ofb'     => true,
91
    ];
92
93
    /**
94
     * @inheritDoc
95
     */
96
    public function crypt(Target $target, Result $result)
97
    {
98
        if ($this->isUsingWeakAlgorithm()) {
99 2
            $name = strtolower(get_class($this));
100
101 2
            $result->warn($name . ': The ' . $this->algorithm . ' algorithm is considered weak');
102 2
        }
103 2
        parent::crypt($target, $result);
104
    }
105
106
107
    /**
108
     * @inheritDoc
109
     */
110
    public function simulate(Target $target, Result $result)
111
    {
112 5
        if ($this->isUsingWeakAlgorithm()) {
113
            $name = strtolower(get_class($this));
114 5
115
            $result->warn($name . ': The ' . $this->algorithm . ' algorithm is considered weak');
116
        }
117
        parent::simulate($target, $result);
118
    }
119
120
    /**
121
     * Is the configured cipher secure enough
122
     *
123
     * @return bool
124 5
     * @throws \phpbu\App\Backup\Crypter\Exception
125
     */
126 5
    public function isUsingWeakAlgorithm(): bool
127 5
    {
128 5
        if (null === $this->algorithm) {
129
            throw new Exception('algorithm is not set');
130 5
        }
131
132
        return isset($this->weakAlgorithms[$this->algorithm]);
133
    }
134
135
    /**
136
     * Setup
137
     *
138
     * @see    \phpbu\App\Backup\Crypter
139
     * @param  array $options
140 2
     * @throws Exception
141
     */
142 2
    public function setup(array $options = [])
143 2
    {
144 2
        if (!Util\Arr::isSetAndNotEmptyString($options, 'algorithm')) {
145
            throw new Exception('openssl expects \'algorithm\'');
146 2
        }
147
        if (!Util\Arr::isSetAndNotEmptyString($options, 'password')
148
         && !Util\Arr::isSetAndNotEmptyString($options, 'certFile')) {
149
            throw new Exception('openssl expects \'certFile\' or \'password\'');
150
        }
151
152
        $this->pathToOpenSSL = Util\Arr::getValue($options, 'pathToOpenSSL', '');
153
        $this->keepUncrypted = Util\Str::toBoolean(Util\Arr::getValue($options, 'keepUncrypted', ''), false);
154
        $this->certFile      = $this->toAbsolutePath(Util\Arr::getValue($options, 'certFile', ''));
155
        $this->algorithm     = Util\Arr::getValue($options, 'algorithm', '');
156 7
        $this->password      = Util\Arr::getValue($options, 'password', '');
157
    }
158 7
159
    /**
160 7
     * Return file suffix of encrypted target
161 4
     *
162
     * @see    \phpbu\App\Backup\Crypter
163 3
     * @return string
164 3
     */
165
    public function getSuffix() : string
166 7
    {
167
        return 'enc';
168 7
    }
169
170
    /**
171
     * Decrypt the backup
172
     *
173
     * @param  \phpbu\App\Backup\Target       $target
174
     * @param  \phpbu\App\Backup\Restore\Plan $plan
175
     * @throws \phpbu\App\Exception
176
     */
177
    public function restore(Target $target, Plan $plan)
178
    {
179
        $executable = $this->createDecryptionOpenSSL($target);
180
        $plan->addDecryptionCommand($executable->getCommand());
181
    }
182
183
    /**
184
     * Create the Executable to run the 'mcrypt' command
185
     *
186
     * @param  \phpbu\App\Backup\Target $target
187
     * @return \phpbu\App\Cli\Executable
188
     * @throws \phpbu\App\Exception
189
     */
190
    protected function createExecutable(Target $target) : Executable
191
    {
192
        return $this->createEncryptionOpenSSL($target);
193
    }
194
195
    /**
196
     * Create encryption OpenSSL
197
     *
198
     * @param  \phpbu\App\Backup\Target $target
199
     * @return \phpbu\App\Cli\Executable\OpenSSL
200
     * @throws \phpbu\App\Exception
201
     */
202
    private function createEncryptionOpenSSL(Target $target): Executable\OpenSSL
203
    {
204
        $executable = $this->createOpenSSL($target);
205
        $executable->encryptFile($target->getPathname())
206
                   ->deleteSource(!$this->keepUncrypted);
207
208
        return $executable;
209
    }
210
211
    /**
212
     * Create decryption OpenSSL
213
     *
214
     * @param  \phpbu\App\Backup\Target $target
215
     * @return \phpbu\App\Cli\Executable\OpenSSL
216
     * @throws \phpbu\App\Exception
217
     */
218
    private function createDecryptionOpenSSL(Target $target): Executable\OpenSSL
219
    {
220
        $executable = $this->createOpenSSL($target);
221
        $executable->decryptFile($target->getPathname())
222
                   ->deleteSource(false);
223
224
        return $executable;
225
    }
226
227
    /**
228
     * Setup an OpenSSL executable only thing missing is the decision of en or decryption
229
     *
230
     * @param  \phpbu\App\Backup\Target $target
231
     * @return \phpbu\App\Cli\Executable\OpenSSL
232
     * @throws \phpbu\App\Exception
233
     */
234
    private function createOpenSSL(Target $target): Executable\OpenSSL
0 ignored issues
show
The parameter $target is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

234
    private function createOpenSSL(/** @scrutinizer ignore-unused */ Target $target): Executable\OpenSSL

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
235
    {
236
        $executable = new Executable\OpenSSL($this->pathToOpenSSL);
237
        // use key or password to encrypt
238
        if (!empty($this->certFile)) {
239
            $executable->useSSLCert($this->certFile);
240
        } else {
241
            $executable->usePassword($this->password)
242
                       ->encodeBase64(true);
243
        }
244
        $executable->useAlgorithm($this->algorithm);
245
246
        return $executable;
247
    }
248
}
249