| 1: | <?php |
| 2: | |
| 3: | |
| 4: | |
| 5: | |
| 6: | |
| 7: | |
| 8: | |
| 9: | |
| 10: | |
| 11: | |
| 12: | |
| 13: | |
| 14: | |
| 15: | |
| 16: | |
| 17: | |
| 18: | |
| 19: | |
| 20: | |
| 21: | |
| 22: | |
| 23: | namespace SimpleID\Auth; |
| 24: | |
| 25: | use Psr\Log\LogLevel; |
| 26: | use SimpleID\Auth\AuthManager; |
| 27: | use SimpleID\Crypt\Random; |
| 28: | use SimpleID\Crypt\SecurityToken; |
| 29: | use SimpleID\Models\User; |
| 30: | use SimpleID\Store\StoreManager; |
| 31: | use SimpleID\Util\Events\UIBuildEvent; |
| 32: | use SimpleID\Util\Forms\FormBuildEvent; |
| 33: | use SimpleID\Util\Forms\FormSubmitEvent; |
| 34: | use SimpleID\Util\UI\Template; |
| 35: | use SimpleJWT\Crypt\AlgorithmFactory; |
| 36: | use SimpleJWT\Keys\KeyFactory; |
| 37: | use SimpleJWT\Keys\KeySet; |
| 38: | use SimpleJWT\Util\Util as SimpleJWTUtil; |
| 39: | |
| 40: | |
| 41: | |
| 42: | |
| 43: | |
| 44: | |
| 45: | class WebAuthnAuthSchemeModule extends AuthSchemeModule { |
| 46: | |
| 47: | static $cose_alg_map = [ |
| 48: | -257 => 'RS256', |
| 49: | -7 => 'ES256' |
| 50: | ]; |
| 51: | |
| 52: | static function init($f3) { |
| 53: | $f3->route('GET|POST /auth/webauthn', 'SimpleID\Auth\WebAuthnAuthSchemeModule->setup'); |
| 54: | $f3->route('POST @webauthn_challenge: /auth/webauthn/challenge [ajax]', 'SimpleID\Auth\WebAuthnAuthSchemeModule->createChallenge'); |
| 55: | $f3->route('GET /auth/webauthn/credentials [ajax]', 'SimpleID\Auth\WebAuthnAuthSchemeModule->listCredentials'); |
| 56: | $f3->map('/auth/webauthn/credentials/@id', 'SimpleID\Auth\WebAuthnAuthSchemeModule'); |
| 57: | } |
| 58: | |
| 59: | |
| 60: | |
| 61: | |
| 62: | |
| 63: | |
| 64: | |
| 65: | public function createChallenge() { |
| 66: | $this->checkHttps('error', true); |
| 67: | |
| 68: | $token = new SecurityToken(); |
| 69: | if (!$this->f3->exists('HEADERS.X-Request-Token') || !$token->verify($this->f3->get('HEADERS.X-Request-Token'), 'webauthn')) { |
| 70: | $this->f3->status(401); |
| 71: | print json_encode([ |
| 72: | 'error' => 'unauthorized', |
| 73: | 'error_description' => $this->f3->get('intl.common.unauthorized') |
| 74: | ]); |
| 75: | return; |
| 76: | } |
| 77: | |
| 78: | $rand = new Random(); |
| 79: | $challenge = SimpleJWTUtil::base64url_encode($rand->bytes(32)); |
| 80: | |
| 81: | |
| 82: | $nonce = $token->generate($challenge, SecurityToken::OPTION_NONCE); |
| 83: | |
| 84: | header('Content-Type: application/json'); |
| 85: | |
| 86: | print json_encode([ |
| 87: | 'challenge' => $challenge, |
| 88: | 'nonce' => $nonce, |
| 89: | 'expires_in' => SIMPLEID_HUMAN_TOKEN_EXPIRES_IN, |
| 90: | ]); |
| 91: | } |
| 92: | |
| 93: | |
| 94: | |
| 95: | |
| 96: | |
| 97: | |
| 98: | |
| 99: | public function listCredentials() { |
| 100: | $this->checkHttps('error', true); |
| 101: | |
| 102: | header('Content-Type: application/json'); |
| 103: | |
| 104: | $token = new SecurityToken(); |
| 105: | if (!$this->f3->exists('HEADERS.X-Request-Token') || !$token->verify($this->f3->get('HEADERS.X-Request-Token'), 'webauthn')) { |
| 106: | $this->f3->status(401); |
| 107: | print json_encode([ |
| 108: | 'error' => 'unauthorized', |
| 109: | 'error_description' => $this->f3->get('intl.common.unauthorized') |
| 110: | ]); |
| 111: | return; |
| 112: | } |
| 113: | |
| 114: | $auth = AuthManager::instance(); |
| 115: | $user = $auth->getUser(); |
| 116: | $results = $this->getSavedCredentials($user, true); |
| 117: | |
| 118: | print json_encode($results); |
| 119: | } |
| 120: | |
| 121: | |
| 122: | |
| 123: | |
| 124: | |
| 125: | |
| 126: | |
| 127: | |
| 128: | public function delete($f3, $params) { |
| 129: | $this->checkHttps('error', true); |
| 130: | parse_str($this->f3->get('BODY'), $delete); |
| 131: | |
| 132: | header('Content-Type: application/json'); |
| 133: | |
| 134: | $token = new SecurityToken(); |
| 135: | if (!$this->f3->exists('HEADERS.X-Request-Token') || !$token->verify($this->f3->get('HEADERS.X-Request-Token'), 'webauthn')) { |
| 136: | $this->f3->status(401); |
| 137: | print json_encode([ |
| 138: | 'error' => 'unauthorized', |
| 139: | 'error_description' => $this->f3->get('intl.common.unauthorized'), |
| 140: | ]); |
| 141: | return; |
| 142: | } |
| 143: | |
| 144: | $auth = AuthManager::instance(); |
| 145: | $user = $auth->getUser(); |
| 146: | |
| 147: | |
| 148: | if (!$user->exists('webauthn.credentials.[' . $params['id'] . ']')) { |
| 149: | $this->f3->status(404); |
| 150: | print json_encode([ |
| 151: | 'error' => 'not_found', |
| 152: | 'error_description' => $this->f3->get('intl.common.not_found') |
| 153: | ]); |
| 154: | return; |
| 155: | } |
| 156: | |
| 157: | $user->unset('webauthn.credentials.[' . $params['id'] . ']'); |
| 158: | |
| 159: | $event = new CredentialEvent($user, CredentialEvent::CREDENTIAL_DELETED_EVENT, self::class, $params['id']); |
| 160: | \Events::instance()->dispatch($event); |
| 161: | |
| 162: | |
| 163: | $store = StoreManager::instance(); |
| 164: | $store->saveUser($user); |
| 165: | |
| 166: | print json_encode([ |
| 167: | 'result' => 'success', |
| 168: | 'result_description' => $this->f3->get('intl.core.auth_webauthn.credential_delete_success') |
| 169: | ]); |
| 170: | } |
| 171: | |
| 172: | |
| 173: | |
| 174: | |
| 175: | |
| 176: | |
| 177: | public function setup() { |
| 178: | $auth = AuthManager::instance(); |
| 179: | $store = StoreManager::instance(); |
| 180: | |
| 181: | $user = $auth->getUser(); |
| 182: | |
| 183: | $tpl = Template::instance(); |
| 184: | $token = new SecurityToken(); |
| 185: | |
| 186: | |
| 187: | $this->checkHttps('redirect', true); |
| 188: | |
| 189: | if (!$auth->isLoggedIn()) { |
| 190: | $this->f3->reroute('/my/dashboard'); |
| 191: | return; |
| 192: | } |
| 193: | |
| 194: | if ($this->f3->exists('POST.result')) { |
| 195: | if (($this->f3->exists('POST.tk') === false) || (!$token->verify($this->f3->get('POST.tk'), 'webauthn'))) { |
| 196: | $this->f3->set('message', $this->f3->get('intl.common.invalid_tk')); |
| 197: | $this->f3->mock('GET /my/dashboard'); |
| 198: | return; |
| 199: | } |
| 200: | |
| 201: | $credential = $this->processNewCredential($this->f3->get('POST.challenge'), $this->f3->get('POST.nonce'), $this->f3->get('POST.result'), $this->f3->get('POST.name')); |
| 202: | |
| 203: | if ($credential == null) { |
| 204: | $this->f3->set('message', $this->f3->get('intl.core.auth_webauthn.credential_add_error')); |
| 205: | } else { |
| 206: | $user->set('webauthn.credentials.' . $credential['id'], $credential); |
| 207: | $store->saveUser($user); |
| 208: | |
| 209: | $event = new CredentialEvent($user, CredentialEvent::CREDENTIAL_ADDED_EVENT, self::class, $credential['id']); |
| 210: | \Events::instance()->dispatch($event); |
| 211: | |
| 212: | $this->f3->set('message', $this->f3->get('intl.core.auth_webauthn.credential_add_success')); |
| 213: | $this->f3->mock('GET /my/dashboard'); |
| 214: | return; |
| 215: | } |
| 216: | } |
| 217: | |
| 218: | $this->f3->set('challenge_url', $this->getCanonicalURL('@webauthn_challenge', '', 'https')); |
| 219: | |
| 220: | $rp_name = ($this->f3->exists('config.site_title')) ? $this->f3->get('config.site_title') : 'SimpleID'; |
| 221: | $options = [ |
| 222: | 'rp' => [ |
| 223: | 'id' => $this->getRpId(), |
| 224: | 'name' => $rp_name |
| 225: | ], |
| 226: | 'user' => [ |
| 227: | 'id' => SimpleJWTUtil::base64url_encode($user->getPairwiseIdentity('webauthn')), |
| 228: | 'name' => ($user->exists('userinfo.nickname')) ? $user->get('userinfo.nickname') : $user['uid'], |
| 229: | 'displayName' => $user->getDisplayName() |
| 230: | ], |
| 231: | 'pubKeyCredParams' => array_map(function ($n) { return [ 'alg' => $n, 'type' => 'public-key' ]; }, array_keys(self::$cose_alg_map)), |
| 232: | 'hints' => [ 'security-key', 'client-device' ], |
| 233: | |
| 234: | 'authenticatorSelection' => [ |
| 235: | 'residentKey' => 'discouraged', |
| 236: | 'userVerification' => 'preferred' |
| 237: | ], |
| 238: | 'timeout' => SIMPLEID_HUMAN_TOKEN_EXPIRES_IN, |
| 239: | 'attestation' => 'none', |
| 240: | ]; |
| 241: | if (isset($user['webauthn']['credentials'])) |
| 242: | $options['excludeCredentials'] = $this->getSavedCredentials($user); |
| 243: | |
| 244: | $this->f3->set('create_options', $options); |
| 245: | |
| 246: | $this->f3->set('otp_recovery_url', 'http://simpleid.org/docs/2/common-problems/#otp'); |
| 247: | |
| 248: | $this->f3->set('js_data.intl.challenge_error', $this->f3->get('intl.core.auth_webauthn.challenge_error')); |
| 249: | $this->f3->set('js_data.intl.browser_error', $this->f3->get('intl.core.auth_webauthn.browser_error')); |
| 250: | |
| 251: | $this->f3->set('tk', $token->generate('webauthn', SecurityToken::OPTION_BIND_SESSION)); |
| 252: | |
| 253: | $this->f3->set('page_class', 'is-dialog-page'); |
| 254: | $this->f3->set('title', $this->f3->get('intl.core.auth_webauthn.webauthn_title')); |
| 255: | $this->f3->set('layout', 'auth_webauthn_setup.html'); |
| 256: | |
| 257: | header('X-Frame-Options: DENY'); |
| 258: | print $tpl->render('page.html'); |
| 259: | } |
| 260: | |
| 261: | |
| 262: | |
| 263: | |
| 264: | |
| 265: | |
| 266: | |
| 267: | |
| 268: | public function onDashboardBlocks(UIBuildEvent $event) { |
| 269: | $tpl = Template::instance(); |
| 270: | |
| 271: | $auth = AuthManager::instance(); |
| 272: | $user = $auth->getUser(); |
| 273: | |
| 274: | $base_path = $this->f3->get('base_path'); |
| 275: | |
| 276: | $token = new SecurityToken(); |
| 277: | $this->f3->set('webauthn_tk', $token->generate('webauthn', SecurityToken::OPTION_BIND_SESSION)); |
| 278: | |
| 279: | $this->f3->set('js_data.intl.credential_confirm_delete', $this->f3->get('intl.core.auth_webauthn.credential_confirm_delete')); |
| 280: | |
| 281: | $event->addBlock('webauthn', $tpl->render('auth_webauthn_dashboard.html', false), 0, [ |
| 282: | 'title' => $this->f3->get('intl.core.auth_webauthn.webauthn_title') |
| 283: | ]); |
| 284: | } |
| 285: | |
| 286: | |
| 287: | |
| 288: | |
| 289: | |
| 290: | public function onLoginFormBuild(FormBuildEvent $event) { |
| 291: | $form_state = $event->getFormState(); |
| 292: | |
| 293: | if ($form_state['mode'] == AuthManager::MODE_VERIFY) { |
| 294: | $auth = AuthManager::instance(); |
| 295: | $store = StoreManager::instance(); |
| 296: | |
| 297: | |
| 298: | $test_user = $store->loadUser($form_state['uid']); |
| 299: | if (!isset($test_user['webauthn'])) return; |
| 300: | |
| 301: | |
| 302: | $uaid = $auth->assignUAID(); |
| 303: | if ($test_user->exists('webauthn.remember') && in_array($uaid, $test_user->get('webauthn.remember'))) return; |
| 304: | |
| 305: | $tpl = Template::instance(); |
| 306: | $token = new SecurityToken(); |
| 307: | |
| 308: | $this->f3->set('challenge_url', $this->getCanonicalURL('@webauthn_challenge', '', 'https')); |
| 309: | $this->f3->set('challenge_tk', $token->generate('webauthn', SecurityToken::OPTION_BIND_SESSION)); |
| 310: | |
| 311: | $options = [ |
| 312: | 'mediation' => 'required', |
| 313: | 'publicKey' => [ |
| 314: | 'userVerification' => 'required', |
| 315: | 'timeout' => 30000, |
| 316: | 'rpId' => $this->getRpId(), |
| 317: | 'allowCredentials' => $this->getSavedCredentials($test_user) |
| 318: | ] |
| 319: | ]; |
| 320: | $this->f3->set('request_options', $options); |
| 321: | |
| 322: | |
| 323: | $this->f3->set('otp_recovery_url', 'http://simpleid.org/docs/2/common_problems/#otp'); |
| 324: | |
| 325: | $this->f3->set('hide_submit_button', true); |
| 326: | |
| 327: | $this->f3->set('js_data.intl.challenge_error', $this->f3->get('intl.core.auth_webauthn.challenge_error')); |
| 328: | $this->f3->set('js_data.intl.browser_error', $this->f3->get('intl.core.auth_webauthn.browser_error')); |
| 329: | |
| 330: | $event->addBlock('auth_webauthn', $tpl->render('auth_webauthn.html', false), 0); |
| 331: | } |
| 332: | } |
| 333: | |
| 334: | |
| 335: | |
| 336: | |
| 337: | |
| 338: | public function onLoginFormSubmit(LoginFormSubmitEvent $event) { |
| 339: | $form_state = $event->getFormState(); |
| 340: | |
| 341: | if ($form_state['mode'] == AuthManager::MODE_VERIFY) { |
| 342: | $store = StoreManager::instance(); |
| 343: | |
| 344: | $uid = $form_state['uid']; |
| 345: | |
| 346: | $test_user = $store->loadUser($form_state['uid']); |
| 347: | $test_credentials = $test_user->get('webauthn.credentials'); |
| 348: | |
| 349: | $result = $this->verifyCredential($this->f3->get('POST.webauthn.challenge'), $this->f3->get('POST.webauthn.nonce'), $test_credentials, $this->f3->get('POST.webauthn.result')); |
| 350: | |
| 351: | if ($result === false) { |
| 352: | $this->f3->set('message', $this->f3->get('intl.core.auth_webauthn.credential_verify_error')); |
| 353: | $event->setInvalid(); |
| 354: | return; |
| 355: | } |
| 356: | |
| 357: | if ($this->f3->get('POST.webauthn.remember') == '1') $form_state['webauthn_remember'] = 1; |
| 358: | |
| 359: | |
| 360: | $prefix = 'webauthn.credentials.[' . $result['credential_id'] . ']'; |
| 361: | $test_user->set($prefix . '.activity.last_time', (new \DateTimeImmutable())->getTimestamp()); |
| 362: | $test_user->set($prefix . '.activity.sign_count', $result['sign_count']); |
| 363: | $store->saveUser($test_user); |
| 364: | |
| 365: | $event->addAuthModuleName(self::class); |
| 366: | $event->setUser($test_user); |
| 367: | $event->setAuthLevel(AuthManager::AUTH_LEVEL_VERIFIED); |
| 368: | } |
| 369: | } |
| 370: | |
| 371: | |
| 372: | |
| 373: | |
| 374: | |
| 375: | public function onLoginEvent(LoginEvent $event) { |
| 376: | $user = $event->getUser(); |
| 377: | $level = $event->getAuthLevel(); |
| 378: | $form_state = $event->getFormState(); |
| 379: | |
| 380: | $auth = AuthManager::instance(); |
| 381: | $store = StoreManager::instance(); |
| 382: | |
| 383: | if (($level >= AuthManager::AUTH_LEVEL_VERIFIED) && isset($form_state['webauthn_remember']) && ($form_state['webauthn_remember'] == 1)) { |
| 384: | $uaid = $auth->assignUAID(); |
| 385: | $remember = $user['webauthn']['remember']; |
| 386: | $remember[] = $uaid; |
| 387: | $user->set('webauthn.remember', array_unique($remember)); |
| 388: | |
| 389: | $store->saveUser($user); |
| 390: | } |
| 391: | } |
| 392: | |
| 393: | |
| 394: | |
| 395: | |
| 396: | public function onLogoutEvent(LogoutEvent $event) { |
| 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: | protected function processNewCredential(string $challenge, string $nonce, string $new_credential_json, string $display_name = null): ?array { |
| 422: | |
| 423: | $token = new SecurityToken(); |
| 424: | if (!$token->verify($nonce, $challenge)) { |
| 425: | return null; |
| 426: | } |
| 427: | |
| 428: | |
| 429: | $new_credential = json_decode($new_credential_json, true); |
| 430: | |
| 431: | |
| 432: | $client_data = json_decode(SimpleJWTUtil::base64url_decode($new_credential['response']['clientDataJSON']), true); |
| 433: | |
| 434: | if ($client_data['type'] != 'webauthn.create') { |
| 435: | $this->logger->log(LogLevel::ERROR, 'Invalid client type: expected webauthn.create, got ' . $client_data['type']); |
| 436: | return null; |
| 437: | } |
| 438: | |
| 439: | if ($client_data['origin'] != $this->getOrigin($this->f3->get('config.canonical_base_path'))) { |
| 440: | $this->logger->log(LogLevel::ERROR, 'Invalid client origin: ' . $client_data['origin']); |
| 441: | return null; |
| 442: | } |
| 443: | |
| 444: | if (!$this->secureCompare($client_data['challenge'], $challenge)) { |
| 445: | $this->logger->log(LogLevel::ERROR, 'Challenge value does not match: expected ' . $challenge . ', got ' . $client_data['challenge']); |
| 446: | return null; |
| 447: | } |
| 448: | |
| 449: | |
| 450: | $authenticator = new WebAuthnAuthenticatorData(SimpleJWTUtil::base64url_decode($new_credential['response']['authenticatorData'])); |
| 451: | $aaguid = $authenticator->getAAGUID(); |
| 452: | |
| 453: | if ($aaguid != null) { |
| 454: | |
| 455: | |
| 456: | } |
| 457: | |
| 458: | |
| 459: | |
| 460: | $pem = wordwrap("-----BEGIN PUBLIC KEY-----\n" . strtr($new_credential['response']['publicKey'], '-_', '+/') . "\n-----END PUBLIC KEY-----\n", 64, "\n", true); |
| 461: | $key = KeyFactory::create($pem, 'pem'); |
| 462: | |
| 463: | |
| 464: | $time = new \DateTimeImmutable(); |
| 465: | if ($display_name == null) $display_name = $time->format(\DateTimeImmutable::ISO8601); |
| 466: | |
| 467: | |
| 468: | $result = [ |
| 469: | 'id' => $new_credential['id'], |
| 470: | 'type' => $new_credential['type'], |
| 471: | |
| 472: | 'display_name' => $display_name, |
| 473: | 'use' => 'verify', |
| 474: | 'authenticator' => [ |
| 475: | 'aaguid' => $aaguid, |
| 476: | 'user_verified' => $authenticator->isUserVerified(), |
| 477: | 'backup_eligible' => $authenticator->isBackupEligible() |
| 478: | ], |
| 479: | 'public_key' => [ |
| 480: | 'jwk' => $key->getKeyData(), |
| 481: | 'alg' => self::$cose_alg_map[$new_credential['response']['publicKeyAlgorithm']], |
| 482: | 'transports' => $new_credential['response']['transports'] |
| 483: | ], |
| 484: | 'activity' => [ |
| 485: | 'first_time' => $time->getTimestamp(), |
| 486: | 'last_time' => $time->getTimestamp(), |
| 487: | 'backed_up' => $authenticator->isBackedUp(), |
| 488: | 'sign_count' => $authenticator->getSignCount() |
| 489: | ] |
| 490: | ]; |
| 491: | |
| 492: | return $result; |
| 493: | } |
| 494: | |
| 495: | |
| 496: | |
| 497: | |
| 498: | |
| 499: | |
| 500: | |
| 501: | |
| 502: | |
| 503: | |
| 504: | |
| 505: | |
| 506: | |
| 507: | |
| 508: | |
| 509: | |
| 510: | |
| 511: | |
| 512: | |
| 513: | |
| 514: | |
| 515: | |
| 516: | protected function verifyCredential(string $challenge, string $nonce, array $stored_credentials, string $credential_json) { |
| 517: | |
| 518: | $token = new SecurityToken(); |
| 519: | if (!$token->verify($nonce, $challenge)) { |
| 520: | return false; |
| 521: | } |
| 522: | |
| 523: | |
| 524: | $credential = json_decode($credential_json, true); |
| 525: | |
| 526: | |
| 527: | if (!array_key_exists($credential['id'], $stored_credentials)) { |
| 528: | return false; |
| 529: | } |
| 530: | |
| 531: | $test_credential = $stored_credentials[$credential['id']]; |
| 532: | |
| 533: | |
| 534: | $client_data_json = SimpleJWTUtil::base64url_decode($credential['response']['clientDataJSON']); |
| 535: | $authenticator_data = SimpleJWTUtil::base64url_decode($credential['response']['authenticatorData']); |
| 536: | if (!$this->verifySignature($credential['response']['signature'], $authenticator_data, $client_data_json, $test_credential['public_key'])) { |
| 537: | return false; |
| 538: | } |
| 539: | |
| 540: | |
| 541: | $client_data = json_decode($client_data_json, true); |
| 542: | |
| 543: | if ($client_data['type'] != 'webauthn.get') { |
| 544: | $this->logger->log(LogLevel::ERROR, 'Invalid client type: expected webauthn.get, got ' . $client_data['type']); |
| 545: | return false; |
| 546: | } |
| 547: | |
| 548: | if ($client_data['origin'] != $this->getOrigin($this->f3->get('config.canonical_base_path'))) { |
| 549: | $this->logger->log(LogLevel::ERROR, 'Invalid client origin: ' . $client_data['origin']); |
| 550: | return false; |
| 551: | } |
| 552: | |
| 553: | if (!$this->secureCompare($client_data['challenge'], $challenge)) { |
| 554: | $this->logger->log(LogLevel::ERROR, 'Challenge value does not match: expected ' . $challenge . ', got ' . $client_data['challenge']); |
| 555: | return false; |
| 556: | } |
| 557: | |
| 558: | |
| 559: | $authenticator = new WebAuthnAuthenticatorData($authenticator_data); |
| 560: | |
| 561: | $rpIdHash = SimpleJWTUtil::base64url_encode(hash('sha256', $this->getRpId(), true)); |
| 562: | if (!$this->secureCompare($authenticator->getRpIdHash(), $rpIdHash)) { |
| 563: | $this->logger->log(LogLevel::ERROR, 'RP ID hash does not match: expected ' . $rpIdHash . ', got ' . $authenticator->getRpIdHash()); |
| 564: | return false; |
| 565: | } |
| 566: | |
| 567: | if (!$authenticator->isUserPresent()) { |
| 568: | $this->logger->log(LogLevel::ERROR, 'User present flag not set in authenticatorData'); |
| 569: | return false; |
| 570: | } |
| 571: | |
| 572: | |
| 573: | |
| 574: | if ($test_credential['authenticator']['user_verified'] && !$authenticator->isUserVerified()) { |
| 575: | $this->logger->log(LogLevel::ERROR, 'User verified flag not set in authenticatorData when flag it was set on creation'); |
| 576: | return false; |
| 577: | } |
| 578: | |
| 579: | $test_sign_count = $test_credential['activity']['sign_count']; |
| 580: | if (($test_sign_count > 0) && ($authenticator->getSignCount() <= $test_sign_count)) { |
| 581: | $this->logger->log(LogLevel::ERROR, 'Sign count too low: expected >' . $test_sign_count . ', got ' . $authenticator->getSignCount()); |
| 582: | return false; |
| 583: | } |
| 584: | |
| 585: | |
| 586: | return [ |
| 587: | 'credential_id' => $credential['id'], |
| 588: | 'user_ppid' => $credential['response']['userHandle'], |
| 589: | 'user_verified' => $authenticator->isUserVerified(), |
| 590: | 'backed_up' => $authenticator->isBackedUp(), |
| 591: | 'sign_count' => $authenticator->getSignCount() |
| 592: | ]; |
| 593: | } |
| 594: | |
| 595: | |
| 596: | |
| 597: | |
| 598: | |
| 599: | |
| 600: | |
| 601: | |
| 602: | |
| 603: | |
| 604: | |
| 605: | |
| 606: | |
| 607: | protected function verifySignature(string $signature, string $authenticator_data, string $client_data_json, array $test_public_key): bool { |
| 608: | $signing_input = $authenticator_data . hash('sha256', $client_data_json, true); |
| 609: | |
| 610: | $set = new KeySet(); |
| 611: | $key = KeyFactory::create($test_public_key['jwk'], 'php'); |
| 612: | $set->add($key); |
| 613: | |
| 614: | if ($key->getKeyType() == \SimpleJWT\Keys\ECKey::KTY) { |
| 615: | |
| 616: | |
| 617: | |
| 618: | |
| 619: | $binary = SimpleJWTUtil::base64url_decode($signature); |
| 620: | |
| 621: | $der = new \SimpleJWT\Util\ASN1\DER(); |
| 622: | $seq = $der->decode($binary); |
| 623: | $r = $seq->getChildAt(0)->getValueAsUIntOctets(); |
| 624: | $s = $seq->getChildAt(1)->getValueAsUIntOctets(); |
| 625: | |
| 626: | |
| 627: | $r = str_pad($r, $key->getSize() / 8, "\x00", STR_PAD_LEFT); |
| 628: | $s = str_pad($s, $key->getSize() / 8, "\x00", STR_PAD_LEFT); |
| 629: | |
| 630: | $signature = SimpleJWTUtil::base64url_encode($r . $s); |
| 631: | } |
| 632: | |
| 633: | |
| 634: | $alg = AlgorithmFactory::create($test_public_key['alg']); |
| 635: | return $alg->verify($signature, $signing_input, $set); |
| 636: | } |
| 637: | |
| 638: | |
| 639: | |
| 640: | |
| 641: | |
| 642: | |
| 643: | |
| 644: | |
| 645: | protected function getRpId(): string { |
| 646: | |
| 647: | $rpId = parse_url($this->f3->get('config.canonical_base_path'), PHP_URL_HOST); |
| 648: | return $rpId; |
| 649: | } |
| 650: | |
| 651: | |
| 652: | |
| 653: | |
| 654: | |
| 655: | |
| 656: | |
| 657: | |
| 658: | |
| 659: | |
| 660: | |
| 661: | |
| 662: | |
| 663: | protected function getSavedCredentials(User $user, bool $include_details = false): array { |
| 664: | if (!$user->exists('webauthn.credentials') || (count($user->get('webauthn.credentials')) == 0)) |
| 665: | return []; |
| 666: | |
| 667: | return array_map(function($credential) use ($include_details) { |
| 668: | $result = [ |
| 669: | 'id' => $credential['id'], |
| 670: | 'type' => $credential['type'] |
| 671: | ]; |
| 672: | if ($include_details) { |
| 673: | $result['display_name'] = $credential['display_name']; |
| 674: | $result['use'] = $credential['use']; |
| 675: | $result['authenticator'] = $credential['authenticator']; |
| 676: | $result['activity'] = $credential['activity']; |
| 677: | } |
| 678: | return $result; |
| 679: | }, array_values($user->get('webauthn.credentials'))); |
| 680: | } |
| 681: | } |
| 682: | ?> |
| 683: | |