blob: f996413b920ee0ae481da40b8968c937bce43d95 [file] [log] [blame]
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +01001<?php
2
3
4namespace WebAuthn\Attestation\Format;
5use WebAuthn\WebAuthnException;
6use WebAuthn\Binary\ByteBuffer;
7
8class Packed extends FormatBase {
9 private $_alg;
10 private $_signature;
11 private $_x5c;
12
13 public function __construct($AttestionObject, \WebAuthn\Attestation\AuthenticatorData $authenticatorData) {
14 parent::__construct($AttestionObject, $authenticatorData);
15
16 // check packed data
17 $attStmt = $this->_attestationObject['attStmt'];
18
19 if (!\array_key_exists('alg', $attStmt) || $this->_getCoseAlgorithm($attStmt['alg']) === null) {
20 throw new WebAuthnException('unsupported alg: ' . $attStmt['alg'], WebAuthnException::INVALID_DATA);
21 }
22
23 if (!\array_key_exists('sig', $attStmt) || !\is_object($attStmt['sig']) || !($attStmt['sig'] instanceof ByteBuffer)) {
24 throw new WebAuthnException('no signature found', WebAuthnException::INVALID_DATA);
25 }
26
27 $this->_alg = $attStmt['alg'];
28 $this->_signature = $attStmt['sig']->getBinaryString();
29
30 // certificate for validation
31 if (\array_key_exists('x5c', $attStmt) && \is_array($attStmt['x5c']) && \count($attStmt['x5c']) > 0) {
32
33 // The attestation certificate attestnCert MUST be the first element in the array
34 $attestnCert = array_shift($attStmt['x5c']);
35
36 if (!($attestnCert instanceof ByteBuffer)) {
37 throw new WebAuthnException('invalid x5c certificate', WebAuthnException::INVALID_DATA);
38 }
39
40 $this->_x5c = $attestnCert->getBinaryString();
41
42 // certificate chain
43 foreach ($attStmt['x5c'] as $chain) {
44 if ($chain instanceof ByteBuffer) {
45 $this->_x5c_chain[] = $chain->getBinaryString();
46 }
47 }
48 }
49 }
50
51
52 /*
53 * returns the key certificate in PEM format
54 * @return string|null
55 */
56 public function getCertificatePem() {
57 if (!$this->_x5c) {
58 return null;
59 }
60 return $this->_createCertificatePem($this->_x5c);
61 }
62
63 /**
64 * @param string $clientDataHash
65 */
66 public function validateAttestation($clientDataHash) {
67 if ($this->_x5c) {
68 return $this->_validateOverX5c($clientDataHash);
69 } else {
70 return $this->_validateSelfAttestation($clientDataHash);
71 }
72 }
73
74 /**
75 * validates the certificate against root certificates
76 * @param array $rootCas
77 * @return boolean
78 * @throws WebAuthnException
79 */
80 public function validateRootCertificate($rootCas) {
81 if (!$this->_x5c) {
82 return false;
83 }
84
85 $chainC = $this->_createX5cChainFile();
86 if ($chainC) {
87 $rootCas[] = $chainC;
88 }
89
90 $v = \openssl_x509_checkpurpose($this->getCertificatePem(), -1, $rootCas);
91 if ($v === -1) {
92 throw new WebAuthnException('error on validating root certificate: ' . \openssl_error_string(), WebAuthnException::CERTIFICATE_NOT_TRUSTED);
93 }
94 return $v;
95 }
96
97 /**
98 * validate if x5c is present
99 * @param string $clientDataHash
100 * @return bool
101 * @throws WebAuthnException
102 */
103 protected function _validateOverX5c($clientDataHash) {
104 $publicKey = \openssl_pkey_get_public($this->getCertificatePem());
105
106 if ($publicKey === false) {
107 throw new WebAuthnException('invalid public key: ' . \openssl_error_string(), WebAuthnException::INVALID_PUBLIC_KEY);
108 }
109
110 // Verify that sig is a valid signature over the concatenation of authenticatorData and clientDataHash
111 // using the attestation public key in attestnCert with the algorithm specified in alg.
112 $dataToVerify = $this->_authenticatorData->getBinary();
113 $dataToVerify .= $clientDataHash;
114
115 $coseAlgorithm = $this->_getCoseAlgorithm($this->_alg);
116
117 // check certificate
118 return \openssl_verify($dataToVerify, $this->_signature, $publicKey, $coseAlgorithm->openssl) === 1;
119 }
120
121 /**
122 * validate if self attestation is in use
123 * @param string $clientDataHash
124 * @return bool
125 */
126 protected function _validateSelfAttestation($clientDataHash) {
127 // Verify that sig is a valid signature over the concatenation of authenticatorData and clientDataHash
128 // using the credential public key with alg.
129 $dataToVerify = $this->_authenticatorData->getBinary();
130 $dataToVerify .= $clientDataHash;
131
132 $publicKey = $this->_authenticatorData->getPublicKeyPem();
133
134 // check certificate
135 return \openssl_verify($dataToVerify, $this->_signature, $publicKey, OPENSSL_ALGO_SHA256) === 1;
136 }
137}
138