blob: aa6f1abb4a330cbab0d10ee88eafbd7ea3e625df [file] [log] [blame]
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +01001<?php
2
3namespace WebAuthn\Attestation\Format;
4use WebAuthn\WebAuthnException;
5use WebAuthn\Binary\ByteBuffer;
6
7class AndroidKey extends FormatBase {
8 private $_alg;
9 private $_signature;
10 private $_x5c;
11
12 public function __construct($AttestionObject, \WebAuthn\Attestation\AuthenticatorData $authenticatorData) {
13 parent::__construct($AttestionObject, $authenticatorData);
14
15 // check u2f data
16 $attStmt = $this->_attestationObject['attStmt'];
17
18 if (!\array_key_exists('alg', $attStmt) || $this->_getCoseAlgorithm($attStmt['alg']) === null) {
19 throw new WebAuthnException('unsupported alg: ' . $attStmt['alg'], WebAuthnException::INVALID_DATA);
20 }
21
22 if (!\array_key_exists('sig', $attStmt) || !\is_object($attStmt['sig']) || !($attStmt['sig'] instanceof ByteBuffer)) {
23 throw new WebAuthnException('no signature found', WebAuthnException::INVALID_DATA);
24 }
25
26 if (!\array_key_exists('x5c', $attStmt) || !\is_array($attStmt['x5c']) || \count($attStmt['x5c']) < 1) {
27 throw new WebAuthnException('invalid x5c certificate', WebAuthnException::INVALID_DATA);
28 }
29
30 if (!\is_object($attStmt['x5c'][0]) || !($attStmt['x5c'][0] instanceof ByteBuffer)) {
31 throw new WebAuthnException('invalid x5c certificate', WebAuthnException::INVALID_DATA);
32 }
33
34 $this->_alg = $attStmt['alg'];
35 $this->_signature = $attStmt['sig']->getBinaryString();
36 $this->_x5c = $attStmt['x5c'][0]->getBinaryString();
37
38 if (count($attStmt['x5c']) > 1) {
39 for ($i=1; $i<count($attStmt['x5c']); $i++) {
40 $this->_x5c_chain[] = $attStmt['x5c'][$i]->getBinaryString();
41 }
42 unset ($i);
43 }
44 }
45
46
47 /*
48 * returns the key certificate in PEM format
49 * @return string
50 */
51 public function getCertificatePem() {
52 return $this->_createCertificatePem($this->_x5c);
53 }
54
55 /**
56 * @param string $clientDataHash
57 */
58 public function validateAttestation($clientDataHash) {
59 $publicKey = \openssl_pkey_get_public($this->getCertificatePem());
60
61 if ($publicKey === false) {
62 throw new WebAuthnException('invalid public key: ' . \openssl_error_string(), WebAuthnException::INVALID_PUBLIC_KEY);
63 }
64
65 // Verify that sig is a valid signature over the concatenation of authenticatorData and clientDataHash
66 // using the attestation public key in attestnCert with the algorithm specified in alg.
67 $dataToVerify = $this->_authenticatorData->getBinary();
68 $dataToVerify .= $clientDataHash;
69
70 $coseAlgorithm = $this->_getCoseAlgorithm($this->_alg);
71
72 // check certificate
73 return \openssl_verify($dataToVerify, $this->_signature, $publicKey, $coseAlgorithm->openssl) === 1;
74 }
75
76 /**
77 * validates the certificate against root certificates
78 * @param array $rootCas
79 * @return boolean
80 * @throws WebAuthnException
81 */
82 public function validateRootCertificate($rootCas) {
83 $chainC = $this->_createX5cChainFile();
84 if ($chainC) {
85 $rootCas[] = $chainC;
86 }
87
88 $v = \openssl_x509_checkpurpose($this->getCertificatePem(), -1, $rootCas);
89 if ($v === -1) {
90 throw new WebAuthnException('error on validating root certificate: ' . \openssl_error_string(), WebAuthnException::CERTIFICATE_NOT_TRUSTED);
91 }
92 return $v;
93 }
94}
95