1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Sasl library. |
5
|
|
|
* |
6
|
|
|
* Copyright (c) 2002-2003 Richard Heyes, |
7
|
|
|
* 2014-2023 Fabian Grutschus |
8
|
|
|
* All rights reserved. |
9
|
|
|
* |
10
|
|
|
* Redistribution and use in source and binary forms, with or without |
11
|
|
|
* modification, are permitted provided that the following conditions |
12
|
|
|
* are met: |
13
|
|
|
* |
14
|
|
|
* o Redistributions of source code must retain the above copyright |
15
|
|
|
* notice, this list of conditions and the following disclaimer. |
16
|
|
|
* o Redistributions in binary form must reproduce the above copyright |
17
|
|
|
* notice, this list of conditions and the following disclaimer in the |
18
|
|
|
* documentation and/or other materials provided with the distribution.| |
19
|
|
|
* o The names of the authors may not be used to endorse or promote |
20
|
|
|
* products derived from this software without specific prior written |
21
|
|
|
* 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 FOR |
26
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
27
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
28
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
29
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
30
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
31
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
32
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
33
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
34
|
|
|
* |
35
|
|
|
* @author Richard Heyes <[email protected]> |
36
|
|
|
*/ |
37
|
|
|
|
38
|
|
|
namespace Fabiang\Sasl\Authentication; |
39
|
|
|
|
40
|
|
|
use Fabiang\Sasl\Options; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Common functionality to SASL mechanisms |
44
|
|
|
* |
45
|
|
|
* @author Richard Heyes <[email protected]> |
46
|
|
|
*/ |
47
|
|
|
abstract class AbstractAuthentication |
48
|
|
|
{ |
49
|
|
|
/** |
50
|
|
|
* Use random devices. |
51
|
|
|
* |
52
|
|
|
* @var bool |
53
|
|
|
*/ |
54
|
|
|
public static $useDevRandom = true; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Options object. |
58
|
|
|
* |
59
|
|
|
* @var Options |
60
|
|
|
*/ |
61
|
|
|
protected $options; |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* |
65
|
|
|
* @param Options $options |
66
|
|
|
*/ |
67
|
20 |
|
public function __construct(Options $options) |
68
|
|
|
{ |
69
|
20 |
|
$this->options = $options; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Get options object. |
74
|
|
|
* |
75
|
|
|
* @return Options |
76
|
|
|
*/ |
77
|
20 |
|
public function getOptions() |
78
|
|
|
{ |
79
|
20 |
|
return $this->options; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Creates the client nonce for the response |
84
|
|
|
* |
85
|
|
|
* @return string The cnonce value |
86
|
|
|
*/ |
87
|
10 |
|
protected function generateCnonce() |
88
|
|
|
{ |
89
|
10 |
|
foreach (array('/dev/urandom', '/dev/random') as $file) { |
90
|
10 |
|
if (true === static::$useDevRandom && is_readable($file)) { |
91
|
8 |
|
return base64_encode(file_get_contents($file, false, null, 0, 32)); |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|
95
|
2 |
|
$cnonce = ''; |
96
|
2 |
|
for ($i = 0; $i < 32; $i++) { |
97
|
2 |
|
$cnonce .= chr(mt_rand(0, 255)); |
98
|
|
|
} |
99
|
|
|
|
100
|
2 |
|
return base64_encode($cnonce); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Generate downgrade protection string |
105
|
|
|
* |
106
|
|
|
* @return string |
107
|
|
|
*/ |
108
|
|
|
protected function generateDowngradeProtectionVerification() |
109
|
|
|
{ |
110
|
|
|
$downgradeProtectionOptions = $this->options->getDowngradeProtection(); |
111
|
|
|
|
112
|
|
|
$allowedMechanisms = $downgradeProtectionOptions->getAllowedMechanisms(); |
113
|
|
|
$allowedChannelBindings = $downgradeProtectionOptions->getAllowedChannelBindings(); |
114
|
|
|
|
115
|
|
|
if (count($allowedMechanisms) === 0 && count($allowedChannelBindings) === 0) { |
116
|
|
|
return ''; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
usort($allowedMechanisms, array($this, 'sortOctetCollation')); |
120
|
|
|
usort($allowedChannelBindings, array($this, 'sortOctetCollation')); |
121
|
|
|
|
122
|
|
|
$protect = implode(',', $allowedMechanisms); |
123
|
|
|
if (count($allowedChannelBindings) > 0) { |
124
|
|
|
$protect .= '|' . implode(',', $allowedChannelBindings); |
125
|
|
|
} |
126
|
|
|
return $protect; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @param string $a |
131
|
|
|
* @param string $b |
132
|
|
|
* @return int |
133
|
|
|
* @link https://datatracker.ietf.org/doc/html/rfc4790#page-22 |
134
|
|
|
*/ |
135
|
|
|
private function sortOctetCollation($a, $b) |
136
|
|
|
{ |
137
|
|
|
if ($a == $b) { |
138
|
|
|
return 0; |
139
|
|
|
} |
140
|
|
|
return ($a < $b) ? -1 : 1; |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|