-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTrustChain.php
More file actions
480 lines (411 loc) · 16.9 KB
/
Copy pathTrustChain.php
File metadata and controls
480 lines (411 loc) · 16.9 KB
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
<?php
declare(strict_types=1);
namespace SimpleSAML\OpenID\Federation;
use JsonSerializable;
use SimpleSAML\OpenID\Codebooks\ClaimsEnum;
use SimpleSAML\OpenID\Codebooks\EntityTypesEnum;
use SimpleSAML\OpenID\Decorators\DateIntervalDecorator;
use SimpleSAML\OpenID\Exceptions\EntityStatementException;
use SimpleSAML\OpenID\Exceptions\TrustChainException;
use SimpleSAML\OpenID\Helpers;
class TrustChain implements JsonSerializable
{
/** @var \SimpleSAML\OpenID\Federation\EntityStatement[] Entities which belong to the trust chain. */
protected array $entities = [];
/** @var bool Indication if the trust chain is resolved up to the trust anchor. */
protected bool $isResolved = false;
/** @var ?int Expiration time (exp) of the trust chain based on the minimum exp from each entity statement. Null
* if entity statements have not been added to chain.
*/
protected ?int $expirationTime = null;
/**
* Critical metadata policy operators gathered from subordinate statements (claim metadata_policy_crit).
*
* @var string[]
*/
protected array $criticalMetadataPolicyOperators = [];
/**
* Metadata policy entries (claim metadata_policy) gathered from subordinate statements. It begins with the one
* issued by the most Superior Entity and ends with the Subordinate Statement issued by the Immediate Superior
* of the Trust Chain subject.
*
* @var array<array<string,array<string,array<string,mixed>>>>
*/
protected array $metadataPolicies = [];
/**
* Resolved metadata policy per entity type.
*
* @var array<string,array<string,array<string,mixed>>>
*/
protected array $resolvedMetadataPolicy = [];
/**
* Resolved metadata (after applying resolved policy) per entity type.
*
* @var array<string,null|array<string,mixed>>
*/
protected array $resolvedMetadata = [];
public function __construct(
protected readonly DateIntervalDecorator $timestampValidationLeewayDecorator,
protected readonly MetadataPolicyResolver $metadataPolicyResolver,
protected readonly MetadataPolicyApplicator $metadataPolicyApplicator,
protected readonly Helpers $helpers,
) {
}
/**
* Check if the trust chain is (currently) empty, meaning there are no entity statements present in the chain.
*/
public function isEmpty(): bool
{
return $this->entities === [];
}
/**
* @throws \SimpleSAML\OpenID\Exceptions\TrustChainException
*/
public function getResolvedExpirationTime(): int
{
$this->validateIsResolved();
$this->validateExpirationTime();
return $this->expirationTime ?? throw new TrustChainException('Empty expiration time encountered.');
}
/**
* @throws \SimpleSAML\OpenID\Exceptions\TrustChainException
*/
public function getResolvedLeaf(): EntityStatement
{
$this->validateIsResolved();
($leaf = reset($this->entities)) ||
throw new TrustChainException('Empty leaf statement encountered.');
return $leaf;
}
/**
* @throws \SimpleSAML\OpenID\Exceptions\TrustChainException
*/
public function getResolvedImmediateSuperior(): ?EntityStatement
{
$this->validateIsResolved();
return $this->entities[1] ?? null;
}
/**
* @throws \SimpleSAML\OpenID\Exceptions\TrustChainException
*/
public function getResolvedTrustAnchor(): EntityStatement
{
$this->validateIsResolved();
($trustAnchor = end($this->entities)) ||
throw new TrustChainException('Empty entity statement encountered.');
reset($this->entities);
return $trustAnchor;
}
/**
* @return ?array<string,mixed>
* @throws \SimpleSAML\OpenID\Exceptions\TrustChainException
* @throws \SimpleSAML\OpenID\Exceptions\JwsException
* @throws \SimpleSAML\OpenID\Exceptions\OpenIdException
*/
public function getResolvedMetadata(EntityTypesEnum $entityTypeEnum): ?array
{
$this->validateIsResolved();
// If we have already resolved the metadata for the given entity type, return it.
if (array_key_exists($entityTypeEnum->value, $this->resolvedMetadata)) {
return $this->resolvedMetadata[$entityTypeEnum->value];
}
$this->resolveMetadataFor($entityTypeEnum);
return $this->resolvedMetadata[$entityTypeEnum->value] ?? null;
}
/**
* Get resolved chain length.
*
* @throws \SimpleSAML\OpenID\Exceptions\TrustChainException
*/
public function getResolvedLength(): int
{
$this->validateIsResolved();
return count($this->entities);
}
/**
* @throws \SimpleSAML\OpenID\Exceptions\TrustChainException
* @throws \SimpleSAML\OpenID\Exceptions\JwsException
*
* @internal
*/
public function addLeaf(EntityStatement $entityStatement): void
{
$this->validateIsNotResolved();
$this->validateIsEmpty();
$this->validateConfigurationStatement($entityStatement);
$this->entities[] = $entityStatement;
$this->updateExpirationTime($entityStatement);
}
/**
* @throws \SimpleSAML\OpenID\Exceptions\TrustChainException
* @throws \SimpleSAML\OpenID\Exceptions\JwsException
*
* @internal
*/
public function addSubordinate(EntityStatement $entityStatement): void
{
$this->validateIsNotResolved();
$this->validateIsNotEmpty();
$this->validateSubordinateStatement($entityStatement);
$this->entities[] = $entityStatement;
$this->updateExpirationTime($entityStatement);
$this->gatherCriticalMetadataPolicyOperators($entityStatement);
$this->gatherMetadataPolicies($entityStatement);
}
/**
* @throws \SimpleSAML\OpenID\Exceptions\TrustChainException
* @throws \SimpleSAML\OpenID\Exceptions\JwsException
*
* @internal
*/
public function addTrustAnchor(EntityStatement $entityStatement): void
{
$this->validateIsNotResolved();
$this->validateIsNotEmpty();
$this->validateAtLeastNumberOfEntities(2);
$this->validateConfigurationStatement($entityStatement);
$this->entities[] = $entityStatement;
$this->updateExpirationTime($entityStatement);
$this->isResolved = true;
}
/**
* Add a Trust Anchor Entity Configuration to create a single entity statement chain. This accommodates a special
* case for the Trust Chain of the Trust Anchor itself.
*
* @throws \SimpleSAML\OpenID\Exceptions\TrustChainException
* @throws \SimpleSAML\OpenID\Exceptions\JwsException
*
* @internal
*/
public function addForTrustAnchorOnly(EntityStatement $entityStatement): void
{
$this->validateIsNotResolved();
$this->validateIsEmpty();
$this->validateConfigurationStatement($entityStatement);
$this->entities[] = $entityStatement;
$this->updateExpirationTime($entityStatement);
$this->isResolved = true;
}
/**
* @throws \SimpleSAML\OpenID\Exceptions\EntityStatementException
* @throws \SimpleSAML\OpenID\Exceptions\JwsException
* @throws \SimpleSAML\OpenID\Exceptions\TrustChainException
*/
protected function updateExpirationTime(EntityStatement $entityStatement): void
{
$newExpirationTime = $entityStatement->getExpirationTime();
// If we have already updated expiration time previously, take the minimum value.
if (!is_null($this->expirationTime)) {
$newExpirationTime = min($newExpirationTime, $this->expirationTime);
}
$this->expirationTime = $newExpirationTime;
$this->validateExpirationTime();
}
/**
* @throws \SimpleSAML\OpenID\Exceptions\TrustChainException
*/
public function validateExpirationTime(): void
{
if (is_null($this->expirationTime)) {
return;
}
if ($this->expirationTime + $this->timestampValidationLeewayDecorator->getInSeconds() < time()) {
throw new TrustChainException(
sprintf('Trust Chain expiration time (%d) is lesser than current time.', $this->expirationTime),
);
}
}
/**
* @throws \SimpleSAML\OpenID\Exceptions\TrustChainException
*/
protected function validateIsResolved(): void
{
if (!$this->isResolved) {
throw new TrustChainException('Trust Chain is expected to be resolved at this point.');
}
}
/**
* @throws \SimpleSAML\OpenID\Exceptions\TrustChainException
*/
protected function validateIsNotResolved(): void
{
if ($this->isResolved) {
throw new TrustChainException('Trust Chain is expected to not be resolved at this point.');
}
}
/**
* @throws \SimpleSAML\OpenID\Exceptions\TrustChainException
*/
protected function validateIsEmpty(): void
{
if ($this->entities !== []) {
throw new TrustChainException('Trust Chain is expected to be empty at this point.');
}
}
/**
* @throws \SimpleSAML\OpenID\Exceptions\TrustChainException
*/
protected function validateIsNotEmpty(): void
{
if ($this->entities === []) {
throw new TrustChainException('Trust Chain is expected to be non-empty at this point.');
}
}
/**
* @throws \SimpleSAML\OpenID\Exceptions\TrustChainException
*/
protected function validateAtLeastNumberOfEntities(int $count): void
{
if (count($this->entities) < $count) {
throw new TrustChainException(sprintf(
'Trust Chain is expected to have at least %d entity/ies at this point.',
$count,
));
}
}
/**
* @throws \SimpleSAML\OpenID\Exceptions\JwsException
*/
protected function validateConfigurationStatement(EntityStatement $entityStatement): void
{
// This must be entity configuration (statement about itself).
if (!$entityStatement->isConfiguration()) {
throw new EntityStatementException('Configuration statement issuer is expected to match subject.');
}
// Verify with own keys from configuration.
$entityStatement->verifyWithKeySet();
}
/**
* @throws \SimpleSAML\OpenID\Exceptions\EntityStatementException
* @throws \SimpleSAML\OpenID\Exceptions\JwsException
*/
protected function validateSubordinateStatement(EntityStatement $entityStatement): void
{
// This must not be configuration
if ($entityStatement->isConfiguration()) {
throw new EntityStatementException('Subordinate statement issuer is not expected to match subject.');
}
// Check if the subject is the issuer of the last statement in the chain.
$previousStatement = end($this->entities);
if (!($previousStatement instanceof EntityStatement)) {
throw new EntityStatementException('Unexpected statement value');
}
reset($this->entities);
if ($entityStatement->getSubject() !== $previousStatement->getIssuer()) {
throw new EntityStatementException(
'Subordinate statement subject does not match issuer in previous statement.',
);
}
// Verify previous statement using the keys in subordinate statement.
$previousStatement->verifyWithKeySet($entityStatement->getJwks()->getValue());
}
/**
* @throws \SimpleSAML\OpenID\Exceptions\TrustChainException
*
* @return string[]
*/
public function jsonSerialize(): array
{
$this->validateIsResolved();
return array_map(
fn(EntityStatement $entityStatement): string => $entityStatement->getToken(),
$this->entities,
);
}
/**
* @throws \SimpleSAML\OpenID\Exceptions\JwsException
*/
protected function gatherCriticalMetadataPolicyOperators(EntityStatement $entityStatement): void
{
$operators = (array)($entityStatement->getPayloadClaim(ClaimsEnum::MetadataPolicyCrit->value) ?? []);
// Make sure we have strings only
$operators = $this->helpers->type()->ensureArrayWithValuesAsStrings($operators);
$this->criticalMetadataPolicyOperators = array_merge($this->criticalMetadataPolicyOperators, $operators);
}
/**
* @throws \SimpleSAML\OpenID\Exceptions\JwsException
* @throws \SimpleSAML\OpenID\Exceptions\MetadataPolicyException
*/
protected function gatherMetadataPolicies(EntityStatement $entityStatement): void
{
$policy = $this->metadataPolicyResolver->ensureFormat(
$entityStatement->getMetadataPolicy() ?? [],
);
if ($policy !== []) {
array_unshift($this->metadataPolicies, $policy);
}
}
/**
* @throws \SimpleSAML\OpenID\Exceptions\MetadataPolicyException
* @throws \SimpleSAML\OpenID\Exceptions\TrustChainException
* @throws \SimpleSAML\OpenID\Exceptions\JwsException
* @throws \SimpleSAML\OpenID\Exceptions\OpenIdException
*/
protected function resolveMetadataFor(EntityTypesEnum $entityTypeEnum): void
{
// In order to be able to resolve metadata, we need to have resolved metadata policy.
if (!array_key_exists($entityTypeEnum->value, $this->resolvedMetadataPolicy)) {
$this->resolvedMetadataPolicy[$entityTypeEnum->value] = $this->metadataPolicyResolver->for(
$entityTypeEnum,
$this->metadataPolicies,
$this->criticalMetadataPolicyOperators,
);
}
// When an Entity participates in a federation or federations with one or more Entity Types, its Entity
// Configuration MUST contain a metadata claim with JSON object values for each of the corresponding
// Entity Type Identifiers, even if the values are the empty JSON object {} (when the Entity Type
// has no associated metadata or Immediate Superiors supply any necessary metadata).
$leafMetadata = $this->getResolvedLeaf()->getMetadata();
if (
(!is_array($leafMetadata)) || // Claim 'metadata' is optional.
(!isset($leafMetadata[$entityTypeEnum->value])) || // If no metadata for given entity type
(!is_array($leafMetadata[$entityTypeEnum->value])) // Unexpected value
) {
$this->resolvedMetadata[$entityTypeEnum->value] = null;
return;
}
$leafMetadataEntityType = $leafMetadata[$entityTypeEnum->value];
// An Immediate Superior MAY provide selected or all metadata parameters for an Immediate Subordinate, by using
// the metadata claim in a Subordinate Statement. When metadata is used in a Subordinate Statement, it applies
// only to those Entity Types that are present in the subject's Entity Configuration. Furthermore, the
// metadata applies only to the subject of the Subordinate Statement and has no effect on the
// subject's Subordinates. Metadata parameters in a Subordinate Statement have precedence
// and override identically named parameters under the same Entity Type in the
// subject's Entity Configuration. If both metadata and metadata_policy
// appear in a Subordinate Statement, then the stated metadata MUST
// be applied before the metadata_policy.
$immediateSuperiorMetadata = $this->getResolvedImmediateSuperior()?->getMetadata();
if (
is_array($immediateSuperiorMetadata) &&
isset($immediateSuperiorMetadata[$entityTypeEnum->value]) &&
is_array($immediateSuperiorMetadata[$entityTypeEnum->value])
) {
$leafMetadataEntityType = array_merge(
$leafMetadataEntityType,
$immediateSuperiorMetadata[$entityTypeEnum->value],
);
}
// If the process described in Section 6.1.4.1 found no Subordinate Statements in the Trust Chain with a
// metadata_policy claim, the metadata of the Trust Chain subject resolves simply to the metadata found
// in its Entity Configuration, with any metadata parameters provided by the Immediate Superior applied
// to it.
if (empty($this->resolvedMetadataPolicy[$entityTypeEnum->value])) {
/** @var array<string,mixed> $leafMetadataEntityType */
$this->resolvedMetadata[$entityTypeEnum->value] = $leafMetadataEntityType;
return;
}
// Policy application to leaf metadata.
/** @var array<string,mixed> $leafMetadataEntityType */
$this->resolvedMetadata[$entityTypeEnum->value] = $this->metadataPolicyApplicator->for(
$this->resolvedMetadataPolicy[$entityTypeEnum->value],
$leafMetadataEntityType,
);
}
/**
* @return \SimpleSAML\OpenID\Federation\EntityStatement[]
*/
public function getEntities(): array
{
return $this->entities;
}
}