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 TPMS_RSA_PARMS structure. |
10
|
|
|
*/ |
11
|
|
|
final class TpmRsaParameters implements KeyParametersInterface |
12
|
|
|
{ |
13
|
|
|
public const DEFAULT_EXPONENT = 65537; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @var int |
17
|
|
|
*/ |
18
|
|
|
private $symmetric; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var int |
22
|
|
|
*/ |
23
|
|
|
private $scheme; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var int |
27
|
|
|
*/ |
28
|
|
|
private $keyBits; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var int |
32
|
|
|
*/ |
33
|
|
|
private $exponent; |
34
|
|
|
|
35
|
5 |
|
public function __construct(int $symmetric, int $scheme, int $keyBits, int $exponent) |
36
|
|
|
{ |
37
|
5 |
|
$this->symmetric = $symmetric; |
38
|
5 |
|
$this->scheme = $scheme; |
39
|
5 |
|
$this->keyBits = $keyBits; |
40
|
|
|
// Spec: When zero, indicates that the exponent is the default of 2^16 + 1 |
41
|
5 |
|
$this->exponent = ($exponent === 0 ? self::DEFAULT_EXPONENT : $exponent); |
42
|
5 |
|
} |
43
|
|
|
|
44
|
1 |
|
public function getAlgorithm(): int |
45
|
|
|
{ |
46
|
1 |
|
return TpmPublic::TPM_ALG_RSA; |
47
|
|
|
} |
48
|
|
|
|
49
|
5 |
|
public static function parse(ByteBuffer $buffer, int $offset, ?int &$endOffset): KeyParametersInterface |
50
|
|
|
{ |
51
|
5 |
|
$symmetric = $buffer->getUint16Val($offset); |
52
|
5 |
|
if ($symmetric !== TpmPublic::TPM_ALG_NULL) { |
53
|
|
|
// Values other than TPM_ALG_NULL may be followed by additional fields (in TPMT_SYM_DEF_OBJECT) |
54
|
|
|
// so bail out if symmetric algorithm is not null |
55
|
|
|
throw new UnsupportedException('Only TPM_ALG_NULL supported for symmetric field in TPMS_RSA_PARMS'); |
56
|
|
|
} |
57
|
5 |
|
$scheme = $buffer->getUint16Val($offset + 2); |
58
|
5 |
|
if ($scheme !== TpmPublic::TPM_ALG_NULL) { |
59
|
|
|
// Values other than TPM_ALG_NULL may be followed by additional fields (in TPMT_RSA_SCHEME) |
60
|
|
|
// so bail out if scheme algorithm is not null |
61
|
|
|
throw new UnsupportedException('Only TPM_ALG_NULL supported for scheme field in TPMS_RSA_PARMS'); |
62
|
5 |
|
} $keyBits = $buffer->getUint16Val($offset + 4); |
63
|
5 |
|
$exponent = $buffer->getUint32Val($offset + 6); |
64
|
5 |
|
$endOffset = $offset + 10; |
65
|
5 |
|
return new self($symmetric, $scheme, $keyBits, $exponent); |
66
|
|
|
} |
67
|
|
|
|
68
|
1 |
|
public function getSymmetric(): int |
69
|
|
|
{ |
70
|
1 |
|
return $this->symmetric; |
71
|
|
|
} |
72
|
|
|
|
73
|
1 |
|
public function getScheme(): int |
74
|
|
|
{ |
75
|
1 |
|
return $this->scheme; |
76
|
|
|
} |
77
|
|
|
|
78
|
1 |
|
public function getKeyBits(): int |
79
|
|
|
{ |
80
|
1 |
|
return $this->keyBits; |
81
|
|
|
} |
82
|
|
|
|
83
|
1 |
|
public function getExponent(): int |
84
|
|
|
{ |
85
|
1 |
|
return $this->exponent; |
86
|
|
|
} |
87
|
|
|
|
88
|
1 |
|
public function getExponentAsBuffer(): ByteBuffer |
89
|
|
|
{ |
90
|
1 |
|
$raw = ''; |
91
|
1 |
|
$e = $this->exponent; |
92
|
1 |
|
while ($e > 0) { |
93
|
1 |
|
$raw = chr($e & 0xFF) . $raw; |
94
|
1 |
|
$e >>= 8; |
95
|
|
|
} |
96
|
1 |
|
return new ByteBuffer($raw); |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|