TpmAttestationStatement::__construct()   B
last analyzed

Complexity

Conditions 8
Paths 6

Size

Total Lines 44
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 20
CRAP Score 8.048

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 8
eloc 28
c 1
b 0
f 0
nc 6
nop 1
dl 0
loc 44
ccs 20
cts 22
cp 0.9091
crap 8.048
rs 8.4444
1
<?php
2
3
namespace MadWizard\WebAuthn\Attestation\Statement;
4
5
use MadWizard\WebAuthn\Attestation\AttestationObject;
6
use MadWizard\WebAuthn\Attestation\Tpm\TpmAttest;
7
use MadWizard\WebAuthn\Attestation\Tpm\TpmPublic;
8
use MadWizard\WebAuthn\Exception\DataValidationException;
9
use MadWizard\WebAuthn\Exception\ParseException;
10
use MadWizard\WebAuthn\Format\ByteBuffer;
11
use MadWizard\WebAuthn\Format\DataValidator;
12
use MadWizard\WebAuthn\Pki\X509Certificate;
13
14
class TpmAttestationStatement extends AbstractAttestationStatement
15
{
16
    public const FORMAT_ID = 'tpm';
17
18
    /**
19
     * @var ByteBuffer
20
     */
21
    private $signature;
22
23
    /**
24
     * @var int
25
     */
26
    private $algorithm;
27
28
    /**
29
     * @var X509Certificate[]|null
30
     */
31
    private $certificates;
32
33
    /**
34
     * @var ByteBuffer|null
35
     */
36
    private $ecdaaKeyId;
37
38
    /**
39
     * @var ByteBuffer
40
     */
41
    private $certInfo;
42
43
    /**
44
     * @var TpmAttest
45
     */
46
    private $attest;
47
48
    /**
49
     * @var TpmPublic
50
     */
51
    private $public;
52
53 4
    public function __construct(AttestationObject $attestationObject)
54
    {
55 4
        parent::__construct($attestationObject, self::FORMAT_ID);
56
57 4
        $statement = $attestationObject->getStatement();
58
59
        try {
60 4
            DataValidator::checkMap(
61 4
                $statement,
62
                [
63 4
                    'ver' => 'string',
64
                    'alg' => 'integer',
65
                    'ecdaaKeyId' => '?' . ByteBuffer::class,
66
                    'x5c' => '?array',
67
                    'sig' => ByteBuffer::class,
68
                    'certInfo' => ByteBuffer::class,
69
                    'pubArea' => ByteBuffer::class,
70
                ]
71
            );
72 1
        } catch (DataValidationException $e) {
73 1
            throw new ParseException('Invalid TPM attestation statement.', 0, $e);
74
        }
75
76 3
        $this->algorithm = $statement->get('alg');
77 3
        $this->signature = $statement->get('sig');
78
79 3
        if ($statement->get('ver') !== '2.0') {
80
            throw new ParseException('Only TPM version 2.0 is supported.');
81
        }
82
83 3
        $this->ecdaaKeyId = $statement->getDefault('ecdaaKeyId', null);
84 3
        $x5c = $statement->getDefault('x5c', null);
85
86 3
        if ($this->ecdaaKeyId === null && $x5c === null) {
87
            throw new ParseException('Either ecdaaKeyId or x5c must be set.');
88
        }
89 3
        if ($this->ecdaaKeyId !== null && $x5c !== null) {
90 1
            throw new ParseException('ecdaaKeyId and x5c cannot both be set.');
91
        }
92 2
        $this->certificates = $x5c === null ? null : $this->buildPEMCertificateArray($x5c);
93
94 2
        $this->attest = new TpmAttest($statement->get('certInfo'));
95 2
        $this->public = new TpmPublic($statement->get('pubArea'));
96 2
        $this->certInfo = $statement->get('certInfo');
97 2
    }
98
99 2
    public function getSignature(): ByteBuffer
100
    {
101 2
        return $this->signature;
102
    }
103
104 2
    public function getAlgorithm(): int
105
    {
106 2
        return $this->algorithm;
107
    }
108
109
    /**
110
     * @return X509Certificate[]|null
111
     */
112 2
    public function getCertificates(): ?array
113
    {
114 2
        return $this->certificates;
115
    }
116
117 1
    public function getEcdaaKeyId(): ?ByteBuffer
118
    {
119 1
        return $this->ecdaaKeyId;
120
    }
121
122 2
    public function getRawCertInfo(): ByteBuffer
123
    {
124 2
        return $this->certInfo;
125
    }
126
127 2
    public function getCertInfo(): TpmAttest
128
    {
129 2
        return $this->attest;
130
    }
131
132 2
    public function getPubArea(): TpmPublic
133
    {
134 2
        return $this->public;
135
    }
136
}
137