1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Copyright (c) 2017-present Ganbaro Digital Ltd |
5
|
|
|
* All rights reserved. |
6
|
|
|
* |
7
|
|
|
* Redistribution and use in source and binary forms, with or without |
8
|
|
|
* modification, are permitted provided that the following conditions |
9
|
|
|
* are met: |
10
|
|
|
* |
11
|
|
|
* * Redistributions of source code must retain the above copyright |
12
|
|
|
* notice, this list of conditions and the following disclaimer. |
13
|
|
|
* |
14
|
|
|
* * Redistributions in binary form must reproduce the above copyright |
15
|
|
|
* notice, this list of conditions and the following disclaimer in |
16
|
|
|
* the documentation and/or other materials provided with the |
17
|
|
|
* distribution. |
18
|
|
|
* |
19
|
|
|
* * Neither the names of the copyright holders nor the names of his |
20
|
|
|
* contributors may be used to endorse or promote products derived |
21
|
|
|
* from this software without specific prior written permission. |
22
|
|
|
* |
23
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
24
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
25
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
26
|
|
|
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
27
|
|
|
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
28
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
29
|
|
|
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
30
|
|
|
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
31
|
|
|
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
32
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN |
33
|
|
|
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
34
|
|
|
* POSSIBILITY OF SUCH DAMAGE. |
35
|
|
|
* |
36
|
|
|
* @category Libraries |
37
|
|
|
* @package MessagingPipeline/Checks |
38
|
|
|
* @author Stuart Herbert <[email protected]> |
39
|
|
|
* @copyright 2017-present Ganbaro Digital Ltd www.ganbarodigital.com |
40
|
|
|
* @license http://www.opensource.org/licenses/bsd-license.php BSD License |
41
|
|
|
* @link http://ganbarodigital.github.io/php-mv-messaging-pipeline |
42
|
|
|
*/ |
43
|
|
|
|
44
|
|
|
namespace GanbaroDigital\MessagingPipeline\V1\Checks; |
45
|
|
|
|
46
|
|
|
use GanbaroDigital\MissingBits\Checks\Check; |
47
|
|
|
use GanbaroDigital\MissingBits\Checks\ListCheck; |
48
|
|
|
use GanbaroDigital\MissingBits\Checks\ListCheckHelper; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* do we have an encryption initialisation vector that we can use? |
52
|
|
|
* |
53
|
|
|
* the initialisation vector (IV for short) is often used in configs |
54
|
|
|
* as a 'shared secret' |
55
|
|
|
*/ |
56
|
|
|
class IsValidEncryptionIV implements Check, ListCheck |
57
|
|
|
{ |
58
|
|
|
// saves us having to implement inspectList() ourselves |
59
|
|
|
use ListCheckHelper; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* what kind of encryption are we using? |
63
|
|
|
* @var string |
64
|
|
|
*/ |
65
|
|
|
private $encryptionType; |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* constructor |
69
|
|
|
* |
70
|
|
|
* @param string $encryptionType |
71
|
|
|
* the OpenSSL encryption cipher that we are using |
72
|
|
|
*/ |
73
|
|
|
public function __construct(string $encryptionType) |
74
|
|
|
{ |
75
|
|
|
$this->encryptionType = $encryptionType; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* do we have an encryption initialisation vector that we can use? |
80
|
|
|
* |
81
|
|
|
* the initialisation vector (IV for short) is often used in configs |
82
|
|
|
* as a 'shared secret' |
83
|
|
|
* |
84
|
|
|
* @param string $encryptionType |
85
|
|
|
* what kind of encryption are we using? |
86
|
|
|
* @param string $iv |
87
|
|
|
* what initialisation vector are we checking? |
88
|
|
|
* @return bool |
89
|
|
|
* TRUE if $iv is a valid initialisation vector for the given |
90
|
|
|
* $encryptionType |
91
|
|
|
* FALSE otherwise |
92
|
|
|
*/ |
93
|
|
|
public static function check(string $encryptionType, string $iv) : bool |
94
|
|
|
{ |
95
|
|
|
// deal with a bad cipher |
96
|
|
|
$errorMessage = null; |
97
|
|
|
set_error_handler(function ($errno, $errstr) use (&$errorMessage) { |
98
|
|
|
$errorMessage = $errstr; |
99
|
|
|
}); |
100
|
|
|
$requiredLen = openssl_cipher_iv_length($encryptionType); |
101
|
|
|
restore_error_handler(); |
102
|
|
|
|
103
|
|
|
// was there a problem? |
104
|
|
|
if ($errorMessage || $requiredLen === false) { |
105
|
|
|
return false; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
// if we get here, then we can check that we have enough bytes |
109
|
|
|
return (strlen($iv) === $requiredLen); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* do we have an encryption type that we can use? |
114
|
|
|
* |
115
|
|
|
* @param string $iv |
116
|
|
|
* what initialisation vector are we checking? |
117
|
|
|
* @return bool |
118
|
|
|
* TRUE if $iv is a valid initialisation vector for the given |
119
|
|
|
* $encryptionType |
120
|
|
|
* FALSE otherwise |
121
|
|
|
*/ |
122
|
|
|
public function inspect($iv) |
123
|
|
|
{ |
124
|
|
|
// we are just a wrapper around our stateless check |
125
|
|
|
return static::check($this->encryptionType, $iv); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* do we have an encryption type that we can use? |
130
|
|
|
* |
131
|
|
|
* @param string $encryptionType |
132
|
|
|
* what kind of encryption are we using? |
133
|
|
|
* @param array|Traversable $list |
134
|
|
|
* the list of initialisation vectors to examine |
135
|
|
|
* @return bool |
136
|
|
|
* TRUE if all the items in $list are valid initialisation vectors |
137
|
|
|
* for the given $encryptionType |
138
|
|
|
* FALSE otherwise |
139
|
|
|
*/ |
140
|
|
|
public static function checkList(string $encryptionType, $list) : bool |
141
|
|
|
{ |
142
|
|
|
// we are just a wrapper around our OO list inspector |
143
|
|
|
$inspector = new static($encryptionType); |
144
|
|
|
return $inspector->inspectList($list); |
145
|
|
|
} |
146
|
|
|
} |