blob: 2b51ba8b7505dc808862eaef27d6fa76ddd8c9fb [file] [log] [blame]
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +01001<?php
2
3
Matthias Andreas Benkard1ba53812022-12-27 17:32:58 +01004namespace lbuchs\WebAuthn\Attestation\Format;
5use lbuchs\WebAuthn\Attestation\AuthenticatorData;
6use lbuchs\WebAuthn\WebAuthnException;
7use lbuchs\WebAuthn\Binary\ByteBuffer;
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +01008
9class U2f extends FormatBase {
Matthias Andreas Benkarde39c4f82021-01-06 17:59:39 +010010 private $_alg = -7;
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +010011 private $_signature;
12 private $_x5c;
13
Matthias Andreas Benkarde39c4f82021-01-06 17:59:39 +010014 public function __construct($AttestionObject, AuthenticatorData $authenticatorData) {
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +010015 parent::__construct($AttestionObject, $authenticatorData);
16
17 // check u2f data
18 $attStmt = $this->_attestationObject['attStmt'];
19
Matthias Andreas Benkarde39c4f82021-01-06 17:59:39 +010020 if (\array_key_exists('alg', $attStmt) && $attStmt['alg'] !== $this->_alg) {
21 throw new WebAuthnException('u2f only accepts algorithm -7 ("ES256"), but got ' . $attStmt['alg'], WebAuthnException::INVALID_DATA);
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +010022 }
23
24 if (!\array_key_exists('sig', $attStmt) || !\is_object($attStmt['sig']) || !($attStmt['sig'] instanceof ByteBuffer)) {
25 throw new WebAuthnException('no signature found', WebAuthnException::INVALID_DATA);
26 }
27
28 if (!\array_key_exists('x5c', $attStmt) || !\is_array($attStmt['x5c']) || \count($attStmt['x5c']) !== 1) {
29 throw new WebAuthnException('invalid x5c certificate', WebAuthnException::INVALID_DATA);
30 }
31
32 if (!\is_object($attStmt['x5c'][0]) || !($attStmt['x5c'][0] instanceof ByteBuffer)) {
33 throw new WebAuthnException('invalid x5c certificate', WebAuthnException::INVALID_DATA);
34 }
35
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +010036 $this->_signature = $attStmt['sig']->getBinaryString();
37 $this->_x5c = $attStmt['x5c'][0]->getBinaryString();
38 }
39
40
41 /*
42 * returns the key certificate in PEM format
43 * @return string
44 */
45 public function getCertificatePem() {
46 $pem = '-----BEGIN CERTIFICATE-----' . "\n";
47 $pem .= \chunk_split(\base64_encode($this->_x5c), 64, "\n");
48 $pem .= '-----END CERTIFICATE-----' . "\n";
49 return $pem;
50 }
51
52 /**
53 * @param string $clientDataHash
54 */
55 public function validateAttestation($clientDataHash) {
56 $publicKey = \openssl_pkey_get_public($this->getCertificatePem());
57
58 if ($publicKey === false) {
59 throw new WebAuthnException('invalid public key: ' . \openssl_error_string(), WebAuthnException::INVALID_PUBLIC_KEY);
60 }
61
62 // Let verificationData be the concatenation of (0x00 || rpIdHash || clientDataHash || credentialId || publicKeyU2F)
63 $dataToVerify = "\x00";
64 $dataToVerify .= $this->_authenticatorData->getRpIdHash();
65 $dataToVerify .= $clientDataHash;
66 $dataToVerify .= $this->_authenticatorData->getCredentialId();
67 $dataToVerify .= $this->_authenticatorData->getPublicKeyU2F();
68
69 $coseAlgorithm = $this->_getCoseAlgorithm($this->_alg);
70
71 // check certificate
72 return \openssl_verify($dataToVerify, $this->_signature, $publicKey, $coseAlgorithm->openssl) === 1;
73 }
74
75 /**
76 * validates the certificate against root certificates
77 * @param array $rootCas
78 * @return boolean
79 * @throws WebAuthnException
80 */
81 public function validateRootCertificate($rootCas) {
82 $chainC = $this->_createX5cChainFile();
83 if ($chainC) {
84 $rootCas[] = $chainC;
85 }
86
87 $v = \openssl_x509_checkpurpose($this->getCertificatePem(), -1, $rootCas);
88 if ($v === -1) {
89 throw new WebAuthnException('error on validating root certificate: ' . \openssl_error_string(), WebAuthnException::CERTIFICATE_NOT_TRUSTED);
90 }
91 return $v;
92 }
93}