-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathJWE.php
218 lines (190 loc) · 5.33 KB
/
JWE.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
<?php
declare(strict_types=1);
namespace Jose\Component\Encryption;
use function array_key_exists;
use function count;
use InvalidArgumentException;
use Jose\Component\Core\JWT;
class JWE implements JWT
{
private ?string $payload = null;
public function __construct(
private readonly ?string $ciphertext,
private readonly string $iv,
private readonly string $tag,
private readonly ?string $aad = null,
private readonly array $sharedHeader = [],
private readonly array $sharedProtectedHeader = [],
private readonly ?string $encodedSharedProtectedHeader = null,
private readonly array $recipients = []
) {
}
public function getPayload(): ?string
{
return $this->payload;
}
/**
* Set the payload. This method is immutable and a new object will be returned.
*/
public function withPayload(string $payload): self
{
$clone = clone $this;
$clone->payload = $payload;
return $clone;
}
/**
* Returns the number of recipients associated with the JWS.
*/
public function countRecipients(): int
{
return count($this->recipients);
}
/**
* Returns true is the JWE has already been encrypted.
*/
public function isEncrypted(): bool
{
return $this->getCiphertext() !== null;
}
/**
* Returns the recipients associated with the JWS.
*
* @return Recipient[]
*/
public function getRecipients(): array
{
return $this->recipients;
}
/**
* Returns the recipient object at the given index.
*/
public function getRecipient(int $id): Recipient
{
if (! isset($this->recipients[$id])) {
throw new InvalidArgumentException('The recipient does not exist.');
}
return $this->recipients[$id];
}
/**
* Returns the ciphertext. This method will return null is the JWE has not yet been encrypted.
*
* @return string|null The cyphertext
*/
public function getCiphertext(): ?string
{
return $this->ciphertext;
}
/**
* Returns the Additional Authentication Data if available.
*/
public function getAAD(): ?string
{
return $this->aad;
}
/**
* Returns the Initialization Vector if available.
*/
public function getIV(): ?string
{
return $this->iv;
}
/**
* Returns the tag if available.
*/
public function getTag(): ?string
{
return $this->tag;
}
/**
* Returns the encoded shared protected header.
*/
public function getEncodedSharedProtectedHeader(): string
{
return $this->encodedSharedProtectedHeader ?? '';
}
/**
* Returns the shared protected header.
*/
public function getSharedProtectedHeader(): array
{
return $this->sharedProtectedHeader;
}
/**
* Returns the shared protected header parameter identified by the given key. Throws an exception is the the
* parameter is not available.
*
* @param string $key The key
*
* @return mixed|null
*/
public function getSharedProtectedHeaderParameter(string $key)
{
if (! $this->hasSharedProtectedHeaderParameter($key)) {
throw new InvalidArgumentException(sprintf('The shared protected header "%s" does not exist.', $key));
}
return $this->sharedProtectedHeader[$key];
}
/**
* Returns true if the shared protected header has the parameter identified by the given key.
*
* @param string $key The key
*/
public function hasSharedProtectedHeaderParameter(string $key): bool
{
return array_key_exists($key, $this->sharedProtectedHeader);
}
/**
* Returns the shared header.
*/
public function getSharedHeader(): array
{
return $this->sharedHeader;
}
/**
* Returns the shared header parameter identified by the given key. Throws an exception is the the parameter is not
* available.
*
* @param string $key The key
*
* @return mixed|null
*/
public function getSharedHeaderParameter(string $key)
{
if (! $this->hasSharedHeaderParameter($key)) {
throw new InvalidArgumentException(sprintf('The shared header "%s" does not exist.', $key));
}
return $this->sharedHeader[$key];
}
/**
* Returns true if the shared header has the parameter identified by the given key.
*
* @param string $key The key
*/
public function hasSharedHeaderParameter(string $key): bool
{
return array_key_exists($key, $this->sharedHeader);
}
/**
* This method splits the JWE into a list of JWEs. It is only useful when the JWE contains more than one recipient
* (JSON General Serialization).
*
* @return JWE[]
*/
public function split(): array
{
$result = [];
foreach ($this->recipients as $recipient) {
$result[] = new self(
$this->ciphertext,
$this->iv,
$this->tag,
$this->aad,
$this->sharedHeader,
$this->sharedProtectedHeader,
$this->encodedSharedProtectedHeader,
[$recipient]
);
}
return $result;
}
}