1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace SimpleSAML\Module\webauthn\Controller; |
6
|
|
|
|
7
|
|
|
use DateTime; |
8
|
|
|
use Exception; |
9
|
|
|
use SimpleSAML\Auth; |
10
|
|
|
use SimpleSAML\Configuration; |
11
|
|
|
use SimpleSAML\Error; |
12
|
|
|
use SimpleSAML\HTTP\RunnableResponse; |
13
|
|
|
use SimpleSAML\Locale\Translate; |
14
|
|
|
use SimpleSAML\Logger; |
15
|
|
|
use SimpleSAML\Module; |
16
|
|
|
use SimpleSAML\Module\webauthn\WebAuthn\AAGUID; |
17
|
|
|
use SimpleSAML\Module\webauthn\WebAuthn\WebAuthnRegistrationEvent; |
18
|
|
|
use SimpleSAML\Session; |
19
|
|
|
use SimpleSAML\Utils; |
20
|
|
|
use Symfony\Component\HttpFoundation\RedirectResponse; |
21
|
|
|
use Symfony\Component\HttpFoundation\Request; |
22
|
|
|
use Symfony\Component\HttpFoundation\Response; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Controller class for the webauthn module. |
26
|
|
|
* |
27
|
|
|
* This class serves the different views available in the module. |
28
|
|
|
* |
29
|
|
|
* @package SimpleSAML\Module\webauthn |
30
|
|
|
*/ |
31
|
|
|
class RegProcess |
32
|
|
|
{ |
33
|
|
|
/** @var \SimpleSAML\Auth\State|string */ |
34
|
|
|
protected $authState = Auth\State::class; |
35
|
|
|
|
36
|
|
|
/** @var \SimpleSAML\Logger|string */ |
37
|
|
|
protected $logger = Logger::class; |
38
|
|
|
|
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Controller constructor. |
42
|
|
|
* |
43
|
|
|
* It initializes the global configuration and session for the controllers implemented here. |
44
|
|
|
* |
45
|
|
|
* @param \SimpleSAML\Configuration $config The configuration to use by the controllers. |
46
|
|
|
* @param \SimpleSAML\Session $session The session to use by the controllers. |
47
|
|
|
* |
48
|
|
|
* @throws \Exception |
49
|
|
|
*/ |
50
|
|
|
public function __construct( |
51
|
|
|
protected Configuration $config, |
52
|
|
|
protected Session $session, |
53
|
|
|
) { |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Inject the \SimpleSAML\Auth\State dependency. |
59
|
|
|
* |
60
|
|
|
* @param \SimpleSAML\Auth\State $authState |
61
|
|
|
*/ |
62
|
|
|
public function setAuthState(Auth\State $authState): void |
63
|
|
|
{ |
64
|
|
|
$this->authState = $authState; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Inject the \SimpleSAML\Logger dependency. |
70
|
|
|
* |
71
|
|
|
* @param \SimpleSAML\Logger $logger |
72
|
|
|
*/ |
73
|
|
|
public function setLogger(Logger $logger): void |
74
|
|
|
{ |
75
|
|
|
$this->logger = $logger; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @param \Symfony\Component\HttpFoundation\Request $request |
81
|
|
|
* @return \Symfony\Component\HttpFoundation\RedirectResponse|\SimpleSAML\HTTP\RunnableResponse |
82
|
|
|
* A Symfony Response-object. |
83
|
|
|
*/ |
84
|
|
|
public function main(Request $request): Response |
85
|
|
|
{ |
86
|
|
|
$this->logger::info('FIDO2 - Accessing WebAuthn enrollment validation'); |
87
|
|
|
|
88
|
|
|
$stateId = $request->query->get('StateId'); |
89
|
|
|
if ($stateId === null) { |
90
|
|
|
throw new Error\BadRequest('Missing required StateId query parameter.'); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
$moduleConfig = Configuration::getOptionalConfig('module_webauthn.php'); |
94
|
|
|
$debugEnabled = $moduleConfig->getOptionalBoolean('debug', false); |
95
|
|
|
|
96
|
|
|
$state = $this->authState::loadState($stateId, 'webauthn:request'); |
97
|
|
|
|
98
|
|
|
// registering a credential is only allowed for new users or after being authenticated |
99
|
|
|
if (WebAuthn::workflowStateMachine($state) !== WebAuthn::STATE_MGMT) { |
|
|
|
|
100
|
|
|
throw new Exception("Attempt to register new token in unacceptable context."); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
$fido2Scope = ($state['FIDO2Scope'] === null ? $state['FIDO2DerivedScope'] : $state['FIDO2Scope']); |
104
|
|
|
if ($fido2Scope === null) { |
105
|
|
|
throw new Exception("FIDO2Scope cannot be null!"); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
$regObject = new WebAuthnRegistrationEvent( |
109
|
|
|
$request->request->get('type'), |
110
|
|
|
$fido2Scope, |
111
|
|
|
$state['FIDO2SignupChallenge'], |
112
|
|
|
base64_decode($request->request->get('attestation_object')), |
113
|
|
|
$request->request->get('response_id'), |
114
|
|
|
$request->request->get('attestation_client_data_json'), |
115
|
|
|
($request->request->get('passwordless') == "on" ? |
116
|
|
|
$state['authenticatorAcceptabilityPasswordless'] : $state['authenticatorAcceptability2FA']), |
117
|
|
|
$debugEnabled, |
|
|
|
|
118
|
|
|
); |
119
|
|
|
// at this point, we need to talk to the DB |
120
|
|
|
/** |
121
|
|
|
* STEP 19 of the validation procedure in § 7.1 of the spec: see if this credential is already registered |
122
|
|
|
*/ |
123
|
|
|
$store = $state['webauthn:store']; |
124
|
|
|
if ($store->doesCredentialExist(bin2hex($regObject->getCredentialId())) === false) { |
125
|
|
|
// credential does not exist yet in database, good. |
126
|
|
|
} else { |
127
|
|
|
throw new Exception("The credential with ID " . $regObject->getCredentialId() . " already exists."); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
// THAT'S IT. This is a valid credential and can be enrolled to the user. |
131
|
|
|
$friendlyName = $request->request->get('tokenname'); |
132
|
|
|
|
133
|
|
|
// if we know the token model, add it to the name |
134
|
|
|
$model = Translate::noop('unknown model'); |
135
|
|
|
$aaguiddict = AAGUID::getInstance(); |
136
|
|
|
if ($aaguiddict->hasToken($regObject->getAAGUID())) { |
137
|
|
|
$token = $aaguiddict->get($regObject->getAAGUID()); |
138
|
|
|
$model = $token['metadataStatement']['description']; |
139
|
|
|
} |
140
|
|
|
$friendlyName .= " ($model)"; |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* STEP 20 of the validation procedure in § 7.1 of the spec: store credentialId, credential, |
144
|
|
|
* signCount and associate with user |
145
|
|
|
*/ |
146
|
|
|
|
147
|
|
|
/* |
148
|
|
|
* Observed with YubiKey 5: the transaction counter is 0 if the key has NEVER been used, but |
149
|
|
|
* the first transaction is also transaction #0. |
150
|
|
|
* i.e. 0 means "before first transaction OR the very first transaction has already taken place" |
151
|
|
|
* |
152
|
|
|
* The very first use of a key should not trigger the physical object cloning alert, so a |
153
|
|
|
* transaction counter == 0 should be allowed for the first authentication of a new key. |
154
|
|
|
* The best way to do this is to set the current counter value to -1 when registering a key |
155
|
|
|
* with a transaction counter of 0. |
156
|
|
|
*/ |
157
|
|
|
$currentCounterValue = -1; |
158
|
|
|
if ($regObject->getCounter() > 0) { |
159
|
|
|
$currentCounterValue = $regObject->getCounter(); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
// did we get any client extensions? |
163
|
|
|
$isResidentKey = 0; |
164
|
|
|
if ( |
165
|
|
|
strlen($request->request->get('clientext')) > 0 && |
166
|
|
|
count(json_decode($request->request->get('clientext'), true)) > 0 |
167
|
|
|
) { |
168
|
|
|
$extensions = json_decode($request->request->get('clientext'), true); |
169
|
|
|
if (isset($extensions['credProps']['rk']) && $extensions['credProps']['rk'] === true) { |
170
|
|
|
$isResidentKey = 1; |
171
|
|
|
} |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
// we also need to store the hased user_id in case we need to retrieve |
175
|
|
|
// tokens in passwordless mode |
176
|
|
|
// use identical hashing as in JS generation step |
177
|
|
|
$configUtils = new Utils\Config(); |
178
|
|
|
$username = hash('sha512', $state['FIDO2Username'] . '|' . $configUtils->getSecretSalt()); |
179
|
|
|
$store->storeTokenData( |
180
|
|
|
$state['FIDO2Username'], |
181
|
|
|
$regObject->getCredentialId(), |
182
|
|
|
$regObject->getCredential(), |
183
|
|
|
$regObject->getAlgo(), |
184
|
|
|
$regObject->getPresenceLevel(), |
185
|
|
|
// we deny saving resident key properties if Passwordless mode |
186
|
|
|
// was not requested |
187
|
|
|
($request->request->get('passwordless') == "on" ? $isResidentKey : 0), |
188
|
|
|
$currentCounterValue, |
189
|
|
|
$friendlyName, |
190
|
|
|
$username, |
191
|
|
|
$regObject->getAAGUID(), |
192
|
|
|
$regObject->getAttestationLevel(), |
193
|
|
|
); |
194
|
|
|
|
195
|
|
|
// make sure $state gets the news, the token is to be displayed to the user on the next page |
196
|
|
|
$state['FIDO2Tokens'][] = [ |
197
|
|
|
0 => $regObject->getCredentialId(), |
198
|
|
|
1 => $regObject->getCredential(), |
199
|
|
|
2 => $currentCounterValue, |
200
|
|
|
3 => $friendlyName, |
201
|
|
|
4 => $regObject->getAlgo(), |
202
|
|
|
5 => $regObject->getPresenceLevel(), |
203
|
|
|
6 => $isResidentKey, |
204
|
|
|
]; |
205
|
|
|
|
206
|
|
|
$id = $this->authState::saveState($state, 'webauthn:request'); |
|
|
|
|
207
|
|
|
if ($debugEnabled === true) { |
208
|
|
|
$response = new RunnableResponse( |
209
|
|
|
function (WebAuthnRegistrationEvent $regObject, string $id) { |
210
|
|
|
echo $regObject->getDebugBuffer(); |
211
|
|
|
echo $regObject->getValidateBuffer(); |
212
|
|
|
echo "<form id='regform' method='POST' action='" . |
213
|
|
|
Module::getModuleURL('webauthn/webauthn?StateId=' . urlencode($id)) . "'>"; |
214
|
|
|
echo "<button type='submit'>Return to previous page.</button>"; |
215
|
|
|
}, |
216
|
|
|
[$regObject, $id], |
217
|
|
|
); |
218
|
|
|
} elseif (array_key_exists('Registration', $state)) { |
219
|
|
|
$response = new RedirectResponse(Module::getModuleURL('webauthn/webauthn?StateId=' . urlencode($id))); |
220
|
|
|
} else { |
221
|
|
|
$response = new RunnableResponse([Auth\ProcessingChain::class, 'resumeProcessing'], [$state]); |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
$response->setExpires(new DateTime('Thu, 19 Nov 1981 08:52:00 GMT')); |
225
|
|
|
$response->setCache([ |
226
|
|
|
'must_revalidate' => true, |
227
|
|
|
'no_cache' => true, |
228
|
|
|
'no_store' => true, |
229
|
|
|
'no_transform' => false, |
230
|
|
|
'public' => false, |
231
|
|
|
'private' => false, |
232
|
|
|
]); |
233
|
|
|
|
234
|
|
|
return $response; |
235
|
|
|
} |
236
|
|
|
} |
237
|
|
|
|