1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace MadWizard\WebAuthn\Attestation\Tpm; |
4
|
|
|
|
5
|
|
|
use MadWizard\WebAuthn\Exception\UnsupportedException; |
6
|
|
|
use MadWizard\WebAuthn\Format\ByteBuffer; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Represents TPMPS_ECC_PARMS structure. |
10
|
|
|
*/ |
11
|
|
|
final class TpmEccParameters implements KeyParametersInterface |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var int |
15
|
|
|
*/ |
16
|
|
|
private $symmetric; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var int |
20
|
|
|
*/ |
21
|
|
|
private $scheme; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var int |
25
|
|
|
*/ |
26
|
|
|
private $curveId; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var int |
30
|
|
|
*/ |
31
|
|
|
private $kdf; |
32
|
|
|
|
33
|
|
|
public const TPM_ECC_NIST_P256 = 0x0003; |
34
|
|
|
|
35
|
|
|
public const TPM_ECC_BN_P256 = 0x0010; |
36
|
|
|
|
37
|
|
|
public const TPM_ECC_SM2_P256 = 0x0020; |
38
|
|
|
|
39
|
1 |
|
public function __construct(int $symmetric, int $scheme, int $curveId, int $kdf) |
40
|
|
|
{ |
41
|
1 |
|
$this->symmetric = $symmetric; |
42
|
1 |
|
$this->scheme = $scheme; |
43
|
1 |
|
$this->curveId = $curveId; |
44
|
1 |
|
$this->kdf = $kdf; |
45
|
1 |
|
} |
46
|
|
|
|
47
|
1 |
|
public function getAlgorithm(): int |
48
|
|
|
{ |
49
|
1 |
|
return TpmPublic::TPM_ALG_ECC; |
50
|
|
|
} |
51
|
|
|
|
52
|
1 |
|
public static function parse(ByteBuffer $buffer, int $offset, ?int &$endOffset): KeyParametersInterface |
53
|
|
|
{ |
54
|
1 |
|
$symmetric = $buffer->getUint16Val($offset); |
55
|
1 |
|
if ($symmetric !== TpmPublic::TPM_ALG_NULL) { |
56
|
|
|
// Values other than TPM_ALG_NULL may be followed by additional fields (in TPMT_SYM_DEF_OBJECT) |
57
|
|
|
// so bail out if symmetric algorithm is not null |
58
|
|
|
throw new UnsupportedException('Only TPM_ALG_NULL supported for symmetric field in TPMPS_ECC_PARMS'); |
59
|
|
|
} |
60
|
1 |
|
$scheme = $buffer->getUint16Val($offset + 2); |
61
|
1 |
|
if ($scheme !== TpmPublic::TPM_ALG_NULL) { |
62
|
|
|
// Values other than TPM_ALG_NULL may be followed by additional fields (in TPMT_ECC_SCHEME) |
63
|
|
|
// so bail out if scheme algorithm is not null |
64
|
|
|
throw new UnsupportedException('Only TPM_ALG_NULL supported for scheme field in TPMPS_ECC_PARMS'); |
65
|
|
|
} |
66
|
1 |
|
$curveId = $buffer->getUint16Val($offset + 4); |
67
|
1 |
|
$kdf = $buffer->getUint16Val($offset + 6); |
68
|
1 |
|
if ($kdf !== TpmPublic::TPM_ALG_NULL) { |
69
|
|
|
// Values other than TPM_ALG_NULL may be followed by additional fields (in TPMT_KDF_SCHEME+) |
70
|
|
|
// so bail out if kdf algorithm is not null |
71
|
|
|
throw new UnsupportedException('Only TPM_ALG_NULL supported for kdf field in TPMPS_ECC_PARMS'); |
72
|
|
|
} |
73
|
1 |
|
$endOffset = $offset + 8; |
74
|
1 |
|
return new self($symmetric, $scheme, $curveId, $kdf); |
75
|
|
|
} |
76
|
|
|
|
77
|
1 |
|
public function getSymmetric(): int |
78
|
|
|
{ |
79
|
1 |
|
return $this->symmetric; |
80
|
|
|
} |
81
|
|
|
|
82
|
1 |
|
public function getScheme(): int |
83
|
|
|
{ |
84
|
1 |
|
return $this->scheme; |
85
|
|
|
} |
86
|
|
|
|
87
|
1 |
|
public function getCurveId(): int |
88
|
|
|
{ |
89
|
1 |
|
return $this->curveId; |
90
|
|
|
} |
91
|
|
|
|
92
|
1 |
|
public function getKdf(): int |
93
|
|
|
{ |
94
|
1 |
|
return $this->kdf; |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|