1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Sasl library. |
7
|
|
|
* |
8
|
|
|
* Copyright (c) 2002-2003 Richard Heyes, |
9
|
|
|
* 2014-2025 Fabian Grutschus |
10
|
|
|
* All rights reserved. |
11
|
|
|
* |
12
|
|
|
* Redistribution and use in source and binary forms, with or without |
13
|
|
|
* modification, are permitted provided that the following conditions |
14
|
|
|
* are met: |
15
|
|
|
* |
16
|
|
|
* o Redistributions of source code must retain the above copyright |
17
|
|
|
* notice, this list of conditions and the following disclaimer. |
18
|
|
|
* o Redistributions in binary form must reproduce the above copyright |
19
|
|
|
* notice, this list of conditions and the following disclaimer in the |
20
|
|
|
* documentation and/or other materials provided with the distribution.| |
21
|
|
|
* o The names of the authors may not be used to endorse or promote |
22
|
|
|
* products derived from this software without specific prior written |
23
|
|
|
* permission. |
24
|
|
|
* |
25
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
26
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
27
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
28
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
29
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
30
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
31
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
32
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
33
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
34
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
35
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
36
|
|
|
* |
37
|
|
|
* @author Richard Heyes <[email protected]> |
38
|
|
|
*/ |
39
|
|
|
|
40
|
|
|
namespace Fabiang\SASL; |
41
|
|
|
|
42
|
|
|
use Fabiang\SASL\Exception\InvalidArgumentException; |
43
|
|
|
use Fabiang\SASL\Exception\UnsupportedMechanismException; |
44
|
|
|
use Fabiang\SASL\Options\DowngradeProtectionOptions; |
45
|
|
|
use Fabiang\SASL\Authentication\AuthenticationInterface; |
46
|
|
|
use Fabiang\SASL\Options; |
47
|
|
|
use Fabiang\SASL\Authentication; |
48
|
|
|
use Fabiang\SASL\Authentication\SCRAM; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Client implementation of various SASL mechanisms |
52
|
|
|
* |
53
|
|
|
* @author Richard Heyes <[email protected]> |
54
|
|
|
*/ |
55
|
|
|
enum SASL: string |
56
|
|
|
{ |
57
|
|
|
case Anonymous = 'ANONYMOUS'; |
58
|
|
|
case Login = 'LOGIN'; |
59
|
|
|
case Plain = 'PLAIN'; |
60
|
|
|
case External = 'EXTERNAL'; |
61
|
|
|
case CramMD5 = 'CRAMMD5'; |
62
|
|
|
case DigestMD5 = 'DIGESTMD5'; |
63
|
|
|
|
64
|
|
|
case SCRAM_SHA_1 = 'SCRAM-SHA-1'; |
65
|
|
|
case SCRAM_SHA_224 = 'SCRAM-SHA-224'; |
66
|
|
|
case SCRAM_SHA_256 = 'SCRAM-SHA-256'; |
67
|
|
|
case SCRAM_SHA_384 = 'SCRAM-SHA-384'; |
68
|
|
|
case SCRAM_SHA_512 = 'SCRAM-SHA-512'; |
69
|
|
|
case SCRAM_SHA3_224 = 'SCRAM-SHA3-224'; |
70
|
|
|
case SCRAM_SHA3_256 = 'SCRAM-SHA3-256'; |
71
|
|
|
case SCRAM_SHA3_384 = 'SCRAM-SHA3-384'; |
72
|
|
|
case SCRAM_SHA3_512 = 'SCRAM-SHA3-512'; |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @throws UnsupportedMechanismException |
76
|
|
|
*/ |
77
|
|
|
public static function fromString(string $authenticationType): static |
78
|
|
|
{ |
79
|
|
|
$formatedType = strtolower(str_replace('-', '', $authenticationType)); |
80
|
|
|
|
81
|
|
|
return match ($formatedType) { |
82
|
|
|
'anonymous' => static::Anonymous, |
83
|
|
|
'login' => static::Login, |
84
|
|
|
'plain' => static::Plain, |
85
|
|
|
'external' => static::External, |
86
|
|
|
'crammd5' => static::CramMD5, |
87
|
|
|
'digestmd5' => static::DigestMD5, |
88
|
|
|
'scramsha1' => static::SCRAM_SHA_1, |
89
|
|
|
'scramsha224' => static::SCRAM_SHA_224, |
90
|
|
|
'scramsha256' => static::SCRAM_SHA_256, |
91
|
|
|
'scramsha384' => static::SCRAM_SHA_384, |
92
|
|
|
'scramsha512' => static::SCRAM_SHA_512, |
93
|
|
|
'scramsha3224' => static::SCRAM_SHA3_224, |
94
|
|
|
'scramsha3256' => static::SCRAM_SHA3_256, |
95
|
|
|
'scramsha3384' => static::SCRAM_SHA3_384, |
96
|
|
|
'scramsha3512' => static::SCRAM_SHA3_512, |
97
|
|
|
default => throw new UnsupportedMechanismException("Invalid SASL mechanism type '$authenticationType'"), |
98
|
|
|
}; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
public function mechanism(Options|array $options = []): AuthenticationInterface |
102
|
|
|
{ |
103
|
|
|
$options = $this->createOptionsObject($options); |
104
|
|
|
|
105
|
|
|
return match ($this) { |
106
|
|
|
self::Anonymous => new Authentication\Anonymous($options), |
107
|
|
|
self::Login => new Authentication\Login($options), |
108
|
|
|
self::Plain => new Authentication\Plain($options), |
109
|
|
|
self::External => new Authentication\External($options), |
110
|
|
|
self::CramMD5 => new Authentication\CramMD5($options), |
111
|
|
|
self::DigestMD5 => new Authentication\DigestMD5($options), |
112
|
|
|
self::SCRAM_SHA_1 => new SCRAM($options, 'sha1'), |
113
|
|
|
self::SCRAM_SHA_224 => new SCRAM($options, 'sha224'), |
114
|
|
|
self::SCRAM_SHA_256 => new SCRAM($options, 'sha256'), |
115
|
|
|
self::SCRAM_SHA_384 => new SCRAM($options, 'sha384'), |
116
|
|
|
self::SCRAM_SHA_512 => new SCRAM($options, 'sha512'), |
117
|
|
|
self::SCRAM_SHA3_224 => new SCRAM($options, 'sha3-224'), |
118
|
|
|
self::SCRAM_SHA3_256 => new SCRAM($options, 'sha3-256'), |
119
|
|
|
self::SCRAM_SHA3_384 => new SCRAM($options, 'sha3-384'), |
120
|
|
|
self::SCRAM_SHA3_512 => new SCRAM($options, 'sha3-512'), |
121
|
|
|
}; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @throws InvalidArgumentException |
126
|
|
|
*/ |
127
|
|
|
private function createOptionsObject(Options|array $options): Options |
128
|
|
|
{ |
129
|
|
|
if ($options instanceof Options) { |
|
|
|
|
130
|
|
|
return $options; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
$downgradeProtectOptions = null; |
134
|
|
|
if (isset($options['downgrade_protection'])) { |
135
|
|
|
$dpo = $options['downgrade_protection']; |
136
|
|
|
|
137
|
|
|
$allowedMechanisms = $dpo['allowed_mechanisms'] ?? []; |
138
|
|
|
$allowedChannelBindings = $dpo['allowed_channel_bindings'] ?? []; |
139
|
|
|
|
140
|
|
|
$downgradeProtectOptions = new DowngradeProtectionOptions($allowedMechanisms, $allowedChannelBindings); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
return new Options( |
144
|
|
|
$this->checkEmpty($options, 'authcid'), |
145
|
|
|
$this->checkEmpty($options, 'secret'), |
146
|
|
|
$this->checkEmpty($options, 'authzid'), |
147
|
|
|
$this->checkEmpty($options, 'service'), |
148
|
|
|
$this->checkEmpty($options, 'hostname'), |
149
|
|
|
$downgradeProtectOptions |
150
|
|
|
); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
private function checkEmpty(array $array, string $key): mixed |
154
|
|
|
{ |
155
|
|
|
return $array[$key] ?? null; |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
|