Skip to content

Commit 48a46d2

Browse files
Merge pull request #237 from moebrowne/feature/weak-cipher-warning
Warn When Using Weak Ciphers
2 parents 38c4532 + 7e6a0f6 commit 48a46d2

File tree

5 files changed

+162
-0
lines changed

5 files changed

+162
-0
lines changed

src/Backup/Crypter/OpenSSL.php

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
use phpbu\App\Backup\Restore\Plan;
55
use phpbu\App\Backup\Target;
66
use phpbu\App\Cli\Executable;
7+
use phpbu\App\Result;
78
use phpbu\App\Util;
89

910
/**
@@ -54,6 +55,79 @@ class OpenSSL extends Abstraction implements Simulator, Restorable
5455
*/
5556
private $keepUncrypted;
5657

58+
private $weakAlgorithms = [
59+
'rc2' => true,
60+
'rc2-40' => true,
61+
'rc2-64' => true,
62+
'rc2-128' => true,
63+
'rc2-40-cbc' => true,
64+
'rc2-64-cbc' => true,
65+
'rc2-cbc' => true,
66+
'rc2-cfb' => true,
67+
'rc2-ecb' => true,
68+
'rc2-ofb' => true,
69+
'rc4' => true,
70+
'rc4-40' => true,
71+
'des' => true,
72+
'des-cbc' => true,
73+
'des-cfb' => true,
74+
'des-ecb' => true,
75+
'des-ede' => true,
76+
'des-ede-cbc' => true,
77+
'des-ede-cfb' => true,
78+
'des-ede-ofb' => true,
79+
'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+
'seed-cbc' => true,
88+
'seed-cfb' => true,
89+
'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+
$name = strtolower(get_class($this));
100+
101+
$result->warn($name . ': The ' . $this->algorithm . ' algorithm is considered weak');
102+
}
103+
104+
return parent::crypt($target, $result);
105+
}
106+
107+
108+
/**
109+
* @inheritDoc
110+
*/
111+
public function simulate(Target $target, Result $result)
112+
{
113+
if ($this->isUsingWeakAlgorithm()) {
114+
$name = strtolower(get_class($this));
115+
116+
$result->warn($name . ': The ' . $this->algorithm . ' algorithm is considered weak');
117+
}
118+
119+
return parent::simulate($target, $result);
120+
}
121+
122+
public function isUsingWeakAlgorithm(): bool
123+
{
124+
if (null === $this->algorithm) {
125+
throw new Exception('algorithm is not set');
126+
}
127+
128+
return isset($this->weakAlgorithms[$this->algorithm]);
129+
}
130+
57131
/**
58132
* Setup
59133
*

src/Event/Warning.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
namespace phpbu\App\Event;
3+
4+
/**
5+
* Debug Event
6+
*
7+
* @package phpbu
8+
* @subpackage Event
9+
* @author MoeBrowne <moebrowne@users.noreply.github.com>
10+
* @license https://opensource.org/licenses/MIT The MIT License (MIT)
11+
* @link http://phpbu.de/
12+
* @since Class available since Release 6.0.0
13+
*/
14+
class Warning
15+
{
16+
/**
17+
* Event name
18+
*/
19+
const NAME = 'phpbu.warning';
20+
21+
/**
22+
* Warning message
23+
*
24+
* @var string
25+
*/
26+
protected $message;
27+
28+
/**
29+
* Constructor.
30+
*
31+
* @param string $message
32+
*/
33+
public function __construct(string $message)
34+
{
35+
$this->message = $message;
36+
}
37+
38+
/**
39+
* Message getter.
40+
*
41+
* @return string
42+
*/
43+
public function getMessage() : string
44+
{
45+
return $this->message;
46+
}
47+
}

src/Result.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -572,4 +572,15 @@ public function debug($msg) : void
572572
$event = new Event\Debug($msg);
573573
$this->eventDispatcher->dispatch(Event\Debug::NAME, $event);
574574
}
575+
576+
/**
577+
* Warning
578+
*
579+
* @param string $msg
580+
*/
581+
public function warn($msg) : void
582+
{
583+
$event = new Event\Warning($msg);
584+
$this->eventDispatcher->dispatch(Event\Warning::NAME, $event);
585+
}
575586
}

src/Result/PrinterCli.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ public static function getSubscribedEvents(): array
110110
{
111111
return [
112112
'phpbu.debug' => 'onDebug',
113+
'phpbu.warning' => 'onWarning',
113114
'phpbu.app_start' => 'onPhpbuStart',
114115
'phpbu.backup_start' => 'onBackupStart',
115116
'phpbu.backup_failed' => 'onBackupFailed',
@@ -406,6 +407,16 @@ public function onDebug(Event\Debug $event)
406407
}
407408
}
408409

410+
/**
411+
* Warnings.
412+
*
413+
* @param \phpbu\App\Event\Warning $event
414+
*/
415+
public function onWarning(Event\Warning $event)
416+
{
417+
$this->writeWithColor('fg-black, bg-yellow', $event->getMessage() . PHP_EOL);
418+
}
419+
409420
/**
410421
* phpbu end event.
411422
*

tests/phpbu/Backup/Crypter/OpenSSLTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,4 +171,23 @@ public function testGetSuffix()
171171
$suffix = $openSSL->getSuffix();
172172
$this->assertEquals('enc', $suffix);
173173
}
174+
175+
/**
176+
* Tests that a warning is emitted for weak algorithms
177+
*/
178+
public function testWeakAlgorithmsCauseWarnings()
179+
{
180+
$runner = $this->getRunnerMock();
181+
$runner->expects($this->once())
182+
->method('run')
183+
->willReturn($this->getRunnerResultMock(0, 'openssl'));
184+
185+
$target = $this->createTargetMock(__FILE__);
186+
$appResult = $this->getAppResultMock();
187+
$appResult->expects($this->once())->method('warn');
188+
189+
$openSSL = new OpenSSL($runner);
190+
$openSSL->setup(['pathToOpenSSL' => PHPBU_TEST_BIN, 'password' => 'fooBarBaz', 'algorithm' => 'des']);
191+
$openSSL->crypt($target, $appResult);
192+
}
174193
}

0 commit comments

Comments
 (0)