/
Key.php
193 lines (175 loc) · 3.81 KB
/
Key.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
<?php
declare(strict_types=1);
namespace ParagonIE\Halite;
use ParagonIE\Halite\Alerts\{
CannotCloneKey,
CannotPerformOperation,
CannotSerializeKey,
InvalidType
};
/**
* Class Key
*
* Base class for all cryptography secrets
*
* This library makes heavy use of return-type declarations,
* which are a PHP 7 only feature. Read more about them here:
*
* @ref http://php.net/manual/en/functions.returning-values.php#functions.returning-values.type-declaration
*
* @package ParagonIE\Halite
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
class Key
{
/**
* @var bool
*/
protected $isPublicKey = false;
/**
* @var bool
*/
protected $isSigningKey = false;
/**
* @var bool
*/
protected $isAsymmetricKey = false;
/**
* @var string
*/
private $keyMaterial = '';
/**
* Don't let this ever succeed
*
* @throws CannotCloneKey
* @codeCoverageIgnore
*/
public function __clone()
{
throw new CannotCloneKey;
}
/**
* You probably should not be using this directly.
*
* @param HiddenString $keyMaterial - The actual key data
* @throws \TypeError
*/
public function __construct(HiddenString $keyMaterial)
{
$this->keyMaterial = Util::safeStrcpy($keyMaterial->getString());
}
/**
* Hide this from var_dump(), etc.
*
* @return array
* @codeCoverageIgnore
*/
public function __debugInfo()
{
// We exclude $this->keyMaterial
return [
'isAsymmetricKey' => $this->isAsymmetricKey,
'isPublicKey' => $this->isPublicKey,
'isSigningKey' => $this->isSigningKey
];
}
/**
* Make sure you wipe the key from memory on destruction
*/
public function __destruct()
{
if (!$this->isPublicKey) {
\sodium_memzero($this->keyMaterial);
$this->keyMaterial = '';
}
}
/**
* Don't allow this object to ever be serialized
* @throws CannotSerializeKey
* @codeCoverageIgnore
*/
public function __sleep()
{
throw new CannotSerializeKey;
}
/**
* Don't allow this object to ever be unserialized
* @throws CannotSerializeKey
* @codeCoverageIgnore
*/
public function __wakeup()
{
throw new CannotSerializeKey;
}
/**
* Get public keys
*
* @return string
* @codeCoverageIgnore
*/
public function __toString()
{
if ($this->isPublicKey) {
return $this->keyMaterial;
}
return '';
}
/**
* Get the actual key material
*
* @return string
* @throws \TypeError
*/
public function getRawKeyMaterial(): string
{
return Util::safeStrcpy($this->keyMaterial);
}
/**
* Is this a part of a key pair?
*
* @return bool
*/
public function isAsymmetricKey(): bool
{
return $this->isAsymmetricKey;
}
/**
* Is this a signing key?
*
* @return bool
*/
public function isEncryptionKey(): bool
{
return !$this->isSigningKey;
}
/**
* Is this a public key?
*
* @return bool
*/
public function isPublicKey(): bool
{
return $this->isPublicKey;
}
/**
* Is this a secret key?
*
* @return bool
*/
public function isSecretKey(): bool
{
return !$this->isPublicKey;
}
/**
* Is this a signing key?
*
* @return bool
*/
public function isSigningKey(): bool
{
return $this->isSigningKey;
}
}