This repository has been archived by the owner on Mar 25, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Server.php
513 lines (476 loc) · 18.5 KB
/
Server.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
<?php
declare(strict_types=1);
namespace Firehed\U2F;
use BadMethodCallException;
use Firehed\U2F\SecurityException as SE;
use RuntimeException;
class Server
{
use AppIdTrait;
/**
* Holds a list of paths to PEM-formatted CA certificates. Unless
* verification has been explicitly disabled with `disableCAVerification()`,
* the Attestation Certificate in the `RegistrationResponseInterface` will
* be validated against the provided CAs.
*
* This means that you *must* either a) provide a list of trusted
* certificates, or b) explicitly disable verifiation. By default, it will
* attempt to validate against an empty list which will always fail. This
* is by design.
*
* @var string[]
*/
private $trustedCAs = [];
/**
* Indicates whether to verify against `$trustedCAs`. Must be explicitly
* disabled with `disableCAVerification()`.
*
* @var bool
*/
private $verifyCA = true;
/**
* Holds a RegisterRequest used by `register()`, which contains the
* challenge in the signature.
*
* @deprecated
*
* @var ?RegisterRequest
*/
private $registerRequest;
/**
* Holds Registrations that were previously established by the
* authenticating party during `authenticate()`
*
* @deprecated
*
* @var RegistrationInterface[]
*/
private $registrations = [];
/**
* Holds SignRequests used by `authenticate` which contain the challenge
* that's part of the signed response.
*
* @deprecated
*
* @var SignRequest[]
*/
private $signRequests = [];
public function __construct(string $appId = '')
{
$this->appId = $appId;
$overload = ini_get('mbstring.func_overload');
// @codeCoverageIgnoreStart
if ($overload > 0) {
throw new RuntimeException(
'The deprecated "mbstring.func_overload" directive must be disabled'
);
}
// @codeCoverageIgnoreEnd
}
/**
* This method authenticates a `LoginResponseInterface` against outstanding
* registrations and a known challenge. If the response's signature
* validates and the counter hasn't done anything strange, the registration
* will be returned with an updated counter value, which *must* be
* persisted for the next authentication. If any verification component
* fails, a `SE` will be thrown.
*
* @param RegistrationInterface[] $registrations
* @return RegistrationInterface if authentication succeeds
* @throws SE if authentication fails
* @throws BadMethodCallException if a precondition is not met
*/
public function validateLogin(
ChallengeProviderInterface $challenge,
LoginResponseInterface $response,
array $registrations
): RegistrationInterface {
// Search for the registration to use based on the Key Handle
$registration = $this->findObjectWithKeyHandle(
$registrations,
$response->getKeyHandleBinary()
);
if ($registration === null) {
// This would suggest either some sort of forgery attempt or
// a hilariously-broken token responding to handles it doesn't
// support and not returning a DEVICE_INELIGIBLE client error.
throw new SE(SE::KEY_HANDLE_UNRECOGNIZED);
}
// If the challenge in the (signed) response ClientData doesn't
// match the one in the signing request, the client signed the
// wrong thing. This could possibly be an attempt at a replay
// attack.
$this->validateChallenge($challenge, $response);
$pem = $registration->getPublicKey()->getPemFormatted();
$toVerify = $response->getSignedData();
// Signature must validate against
$sig_check = openssl_verify(
$toVerify,
$response->getSignature(),
$pem,
\OPENSSL_ALGO_SHA256
);
if ($sig_check !== 1) {
// We could not validate the signature using the
// previously-verified public key on file for this registration.
// This is most likely malicious, since there's either
// a non-spec-compliant device or the device doesn't have access to
// the embedded private key that was used to sign the original
// registration request.
throw new SE(SE::SIGNATURE_INVALID);
}
if ($response->getCounter() <= $registration->getCounter()) {
// Tokens are required to keep a counter of authentications
// performed, and this value is included in the signed response.
// Entering this block means one of two things:
// 1) The device counter rolled over its integer limit (very, very
// unlikely), or
// 2) A message was compromised and this is an attempt at a replay
// attack, or
// 3) The private key on the device was somehow compromised, but
// the counter is unknown to the attacker.
// 3a) The attacker started low and we caught them, or
// 3b) The attacker started high, got in, updated the counter on
// file, and the device owner just tried to reauthenticate
// In either case, this indicates the device was somehow
// compromised. The user should be alerted and the device should
// no longer be trusted for authentication. However, the
// registration assicated with the device should only be
// disabled and not deleted, since it should not be allowed to
// be re-added to the user's account.
throw new SE(SE::COUNTER_USED);
}
// It's reasonable to check that the gap between these values is
// relatively small, to handle the case where an attacker is able to
// compromise a token's private key and performs a single
// authentication with an arbitrarily-high counter to avoid this
// rollback detection. Such a scenario would still trigger the above
// error when the legitimate token-holder attempts to use their token
// again. There's no perfect way to handle this since
return (new Registration())
->setAttestationCertificate($registration->getAttestationCertificate())
->setKeyHandle($registration->getKeyHandleBinary())
->setPublicKey($registration->getPublicKey())
->setCounter($response->getCounter());
}
/**
* @deprecated This is being replaced by validateLogin
*
* This method authenticates a `LoginResponseInterface` against outstanding
* registrations and their corresponding `SignRequest`s. If the response's
* signature validates and the counter hasn't done anything strange, the
* registration will be returned with an updated counter value, which *must*
* be persisted for the next authentication. If any verification component
* fails, a `SE` will be thrown.
*
* @param LoginResponseInterface $response the parsed response from the user
* @return RegistrationInterface if authentication succeeds
* @throws SE if authentication fails
* @throws BadMethodCallException if a precondition is not met
*/
public function authenticate(LoginResponseInterface $response): RegistrationInterface
{
if (!$this->registrations) {
throw new BadMethodCallException(
'Before calling authenticate(), provide objects implementing'.
'RegistrationInterface with setRegistrations()'
);
}
if (!$this->signRequests) {
throw new BadMethodCallException(
'Before calling authenticate(), provide `SignRequest`s with '.
'setSignRequests()'
);
}
// Search for the Signing Request to use based on the Key Handle
$request = $this->findObjectWithKeyHandle(
$this->signRequests,
$response->getKeyHandleBinary()
);
if (!$request) {
// Similar to above, there is a bizarre mismatch between the known
// possible sign requests and the key handle determined above. This
// would probably be caused by a logic error causing bogus sign
// requests to be passed to this method.
throw new SE(SE::KEY_HANDLE_UNRECOGNIZED);
}
return $this->validateLogin($request, $response, $this->registrations);
}
/**
* This method authenticates a RegistrationResponseInterface against its
* corresponding RegisterRequest by verifying the certificate and signature.
* If valid, it returns a registration; if not, a SE will be thrown and
* attempt to register the key must be aborted.
*
* @param RegistrationResponseInterface $response The response to verify
* @return RegistrationInterface if the response is proven authentic
* @throws SE if the response cannot be proven authentic
* @throws BadMethodCallException if a precondition is not met
*/
public function validateRegistration(
ChallengeProviderInterface $request,
RegistrationResponseInterface $response
): RegistrationInterface {
$this->validateChallenge($request, $response);
// Check the Application Parameter
$this->validateRelyingParty($response->getRpIdHash());
if ($this->verifyCA) {
$this->verifyAttestationCertAgainstTrustedCAs($response);
}
// Signature must validate against device issuer's public key
$pem = $response->getAttestationCertificate()->getPemFormatted();
$sig_check = openssl_verify(
$response->getSignedData(),
$response->getSignature(),
$pem,
\OPENSSL_ALGO_SHA256
);
if ($sig_check !== 1) {
throw new SE(SE::SIGNATURE_INVALID);
}
return (new Registration())
->setAttestationCertificate($response->getAttestationCertificate())
->setCounter(0) // The response does not include this
->setKeyHandle($response->getKeyHandleBinary())
->setPublicKey($response->getPublicKey());
}
/**
* @deprecated This is being replaced with validateRegistration()
*
* This method authenticates a RegistrationResponseInterface against its
* corresponding RegisterRequest by verifying the certificate and signature.
* If valid, it returns a registration; if not, a SE will be thrown and
* attempt to register the key must be aborted.
*
* @param RegistrationResponseInterface $response The response to verify
* @return RegistrationInterface if the response is proven authentic
* @throws SE if the response cannot be proven authentic
* @throws BadMethodCallException if a precondition is not met
*/
public function register(RegistrationResponseInterface $response): RegistrationInterface
{
if ($this->registerRequest === null) {
throw new BadMethodCallException(
'Before calling register(), provide a RegisterRequest '.
'with setRegisterRequest()'
);
}
return $this->validateRegistration($this->registerRequest, $response);
}
/**
* Disables verification of the Attestation Certificate against the list of
* CA certificates. This lowers overall security, at the benefit of being
* able to use devices that haven't been explicitly whitelisted.
*
* This method or setTrustedCAs() must be called before register() or
* a SecurityException will always be thrown.
*
* @return self
*/
public function disableCAVerification(): self
{
$this->verifyCA = false;
return $this;
}
/**
* Provides a list of CA certificates for device issuer verification during
* registration.
*
* This method or disableCAVerification must be called before register() or
* a SecurityException will always be thrown.
*
* @param string[] $CAs A list of file paths to device issuer CA certs
* @return self
*/
public function setTrustedCAs(array $CAs): self
{
$this->verifyCA = true;
$this->trustedCAs = $CAs;
return $this;
}
/**
* @deprecated
*
* Provide the previously-generated RegisterRequest to be used when
* verifying a RegisterResponse during register()
*
* @param RegisterRequest $request
* @return self
*/
public function setRegisterRequest(RegisterRequest $request): self
{
$this->registerRequest = $request;
return $this;
}
/**
* @deprecated
*
* Provide a user's existing registration to be used during
* authentication
*
* @param RegistrationInterface[] $registrations
* @return self
*/
public function setRegistrations(array $registrations): self
{
array_map(function (RegistrationInterface $r) {
}, $registrations); // type check
$this->registrations = $registrations;
return $this;
}
/**
* @deprecated
*
* Provide the previously-generated SignRequests, corresponing to the
* existing Registrations, of of which should be signed and will be
* verified during authenticate()
*
* @param SignRequest[] $signRequests
* @return self
*/
public function setSignRequests(array $signRequests): self
{
array_map(function (SignRequest $s) {
}, $signRequests); // type check
$this->signRequests = $signRequests;
return $this;
}
/**
* Creates a new RegisterRequest to be sent to the authenticated user to be
* used by the `u2f.register` API.
*
* @deprecated
*
* @return RegisterRequest
*/
public function generateRegisterRequest(): RegisterRequest
{
return (new RegisterRequest())
->setAppId($this->getAppId())
->setChallenge($this->generateChallenge()->getChallenge());
}
/**
* Creates a new SignRequest for an existing registration for an
* authenticating user, used by the `u2f.sign` API.
*
* @deprecated
*
* @param RegistrationInterface $reg one of the user's existing Registrations
* @return SignRequest
*/
public function generateSignRequest(RegistrationInterface $reg): SignRequest
{
return (new SignRequest())
->setAppId($this->getAppId())
->setChallenge($this->generateChallenge()->getChallenge())
->setKeyHandle($reg->getKeyHandleBinary());
}
/**
* Wraps generateSignRequest for multiple registrations. Using this API
* ensures that all sign requests share a single challenge, which greatly
* simplifies compatibility with WebAuthn
*
* @deprecated
*
* @param RegistrationInterface[] $registrations
* @return SignRequest[]
*/
public function generateSignRequests(array $registrations): array
{
$challenge = $this->generateChallenge()->getChallenge();
$requests = array_map([$this, 'generateSignRequest'], $registrations);
$requestsWithSameChallenge = array_map(function (SignRequest $req) use ($challenge) {
return $req->setChallenge($challenge);
}, $requests);
return array_values($requestsWithSameChallenge);
}
/**
* @deprecated
*
* Re-implements the trait's version solely for deprecation warnings
*/
public function setAppId(string $appId): self
{
$this->appId = $appId;
return $this;
}
/**
* Searches through the provided array of objects, and looks for a matching
* key handle value. If one is found, it is returned; if not, this returns
* null.
*
* @template T of KeyHandleInterface
*
* @param T[] $objects haystack to search
* @param string $keyHandle key handle to find in haystack
*
* @return ?T element from haystack if match found, otherwise null
*/
private function findObjectWithKeyHandle(
array $objects,
string $keyHandle
) {
foreach ($objects as $object) {
if (hash_equals($object->getKeyHandleBinary(), $keyHandle)) {
return $object;
}
}
return null;
}
/**
* Generates a random challenge and returns it base64-web-encoded
*/
public function generateChallenge(): ChallengeProviderInterface
{
// FIDO Alliance spec suggests a minimum of 8 random bytes
return new Challenge(toBase64Web(\random_bytes(16)));
}
private function validateRelyingParty(string $rpIdHash): void
{
// Note: this is a bit delicate at the moment, since different
// protocols have different rules around the handling of Relying Party
// verification. Expect this to be revised.
if (!hash_equals($this->getRpIdHash(), $rpIdHash)) {
throw new SE(SE::WRONG_RELYING_PARTY);
}
}
/**
* Compares the Challenge value from a known source against the
* user-provided value. A mismatch will throw a SE. Future
* versions may also enforce a timing window.
*
* @param ChallengeProviderInterface $from source of known challenge
* @param ChallengeProviderInterface $to user-provided value
* @throws SE on failure
*/
private function validateChallenge(ChallengeProviderInterface $from, ChallengeProviderInterface $to): void
{
// Note: strictly speaking, this shouldn't even be targetable as
// a timing attack. However, this opts to be proactive, and also
// ensures that no weird PHP-isms in string handling cause mismatched
// values to validate.
if (!hash_equals($from->getChallenge(), $to->getChallenge())) {
throw new SE(SE::CHALLENGE_MISMATCH);
}
}
/**
* Asserts that the attestation cert provided by the registration is issued
* by the set of trusted CAs.
*
* @param RegistrationResponseInterface $response The response to validate
* @throws SecurityException upon failure
* @return void
*/
private function verifyAttestationCertAgainstTrustedCAs(RegistrationResponseInterface $response): void
{
$pem = $response->getAttestationCertificate()->getPemFormatted();
$result = openssl_x509_checkpurpose(
$pem,
\X509_PURPOSE_ANY,
$this->trustedCAs
);
if ($result !== true) {
throw new SE(SE::NO_TRUSTED_CA);
}
}
}