Completed
Pull Request — master (#237)
by
unknown
03:40
created

OpenSSL::createEncryptionOpenSSL()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 4
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 7
rs 10
ccs 4
cts 4
cp 1
crap 1
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()) {
0 ignored issues
show
Bug introduced by
The method isUsingWeakAlgorithm() does not exist on phpbu\App\Cli\Executable. It seems like you code against a sub-type of phpbu\App\Cli\Executable such as phpbu\App\Cli\Executable\OpenSSL. ( Ignorable by Annotation )

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

64
        if ($this->getExecutable($target)->/** @scrutinizer ignore-call */ isUsingWeakAlgorithm()) {
Loading history...
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);
0 ignored issues
show
Bug introduced by
Are you sure the usage of parent::crypt($target, $result) targeting phpbu\App\Backup\Crypter\Abstraction::crypt() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
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);
0 ignored issues
show
Bug introduced by
Are you sure the usage of parent::simulate($target, $result) targeting phpbu\App\Backup\Crypter\Abstraction::simulate() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
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
0 ignored issues
show
Unused Code introduced by
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

187
    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...
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