Repository navigation
Expand file tree
/
Copy pathMessage.php
More file actions
1183 lines (1040 loc) · 46.3 KB
/
Copy pathMessage.php
File metadata and controls
1183 lines (1040 loc) · 46.3 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
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
declare(strict_types=1);
namespace SimpleSAML\Module\saml;
use RobRichards\XMLSecLibs\XMLSecurityKey;
use SAML2\Assertion;
use SAML2\AuthnRequest;
use SAML2\EncryptedAssertion;
use SAML2\LogoutRequest;
use SAML2\LogoutResponse;
use SAML2\Message as SAMLMessage;
use SAML2\Response;
use SAML2\SignedElement;
use SAML2\StatusResponse;
use SAML2\XML\ds\KeyInfo;
use SAML2\XML\ds\X509Certificate;
use SAML2\XML\ds\X509Data;
use SAML2\XML\saml\Issuer;
use SimpleSAML\Assert\Assert;
use SimpleSAML\Configuration;
use SimpleSAML\Error as SSP_Error;
use SimpleSAML\Error\ErrorCodes;
use SimpleSAML\Logger;
use SimpleSAML\Module\saml\Error as SAMLError;
use SimpleSAML\SAML2\Constants;
use SimpleSAML\SAML2\XML\samlp\AuthnContextComparisonTypeEnum;
use SimpleSAML\Utils;
use function array_column;
/**
* Common code for building SAML 2 messages based on the available metadata.
*
* @package SimpleSAMLphp
*/
class Message
{
/**
* Add signature key and sender certificate to an element (Message or Assertion).
*
* @param \SimpleSAML\Configuration $srcMetadata The metadata of the sender.
* @param \SimpleSAML\Configuration $dstMetadata The metadata of the recipient.
* @param \SAML2\SignedElement $element The element we should add the data to.
*/
public static function addSign(
Configuration $srcMetadata,
Configuration $dstMetadata,
SignedElement $element,
): void {
$dstPrivateKey = $dstMetadata->getOptionalString('signature.privatekey', null);
$cryptoUtils = new Utils\Crypto();
if ($dstPrivateKey !== null) {
/** @var array $keyArray */
$keyArray = $cryptoUtils->loadPrivateKey($dstMetadata, true, 'signature.');
$certArray = $cryptoUtils->loadPublicKey($dstMetadata, false, 'signature.');
} else {
/** @var array $keyArray */
$keyArray = $cryptoUtils->loadPrivateKey($srcMetadata, true);
$certArray = $cryptoUtils->loadPublicKey($srcMetadata, false);
}
$algo = $dstMetadata->getOptionalString('signature.algorithm', null);
if ($algo === null) {
$algo = $srcMetadata->getOptionalString('signature.algorithm', XMLSecurityKey::RSA_SHA256);
}
$privateKey = new XMLSecurityKey($algo, ['type' => 'private']);
if (array_key_exists('password', $keyArray)) {
$privateKey->passphrase = $keyArray['password'];
}
$privateKey->loadKey($keyArray['PEM'], false);
$element->setSignatureKey($privateKey);
if ($certArray === null) {
// we don't have a certificate to add
return;
}
if (!array_key_exists('PEM', $certArray)) {
// we have a public key with only a fingerprint
return;
}
$element->setCertificates([$certArray['PEM']]);
}
/**
* Add signature key and and senders certificate to message.
*
* @param \SimpleSAML\Configuration $srcMetadata The metadata of the sender.
* @param \SimpleSAML\Configuration $dstMetadata The metadata of the recipient.
* @param \SAML2\Message $message The message we should add the data to.
*/
private static function addRedirectSign(
Configuration $srcMetadata,
Configuration $dstMetadata,
SAMLMessage $message,
): void {
$signingEnabled = null;
if ($message instanceof LogoutRequest || $message instanceof LogoutResponse) {
$signingEnabled = $srcMetadata->getOptionalBoolean('sign.logout', null);
if ($signingEnabled === null) {
$signingEnabled = $dstMetadata->getOptionalBoolean('sign.logout', null);
}
} elseif ($message instanceof AuthnRequest) {
$signingEnabled = $srcMetadata->getOptionalBoolean('sign.authnrequest', null);
if ($signingEnabled === null) {
$signingEnabled = $dstMetadata->getOptionalBoolean('sign.authnrequest', null);
}
}
if ($signingEnabled === null) {
$signingEnabled = $dstMetadata->getOptionalBoolean('redirect.sign', null);
if ($signingEnabled === null) {
$signingEnabled = $srcMetadata->getOptionalBoolean('redirect.sign', false);
}
}
if (!$signingEnabled) {
return;
}
self::addSign($srcMetadata, $dstMetadata, $message);
}
/**
* Check the signature on a SAML2 message or assertion.
*
* @param \SimpleSAML\Configuration $srcMetadata The metadata of the sender.
* @param \SAML2\SignedElement $element Either a \SAML2\Response or a \SAML2\Assertion.
* @return bool True if the signature is correct, false otherwise.
*
* @throws \SimpleSAML\Error\Exception if there is not certificate in the metadata for the entity.
* @throws \Exception if the signature validation fails with an exception.
*/
public static function checkSign(Configuration $srcMetadata, SignedElement $element): bool
{
// find the public key that should verify signatures by this entity
$keys = $srcMetadata->getPublicKeys('signing');
if (!empty($keys)) {
$pemKeys = [];
foreach ($keys as $key) {
switch ($key['type']) {
case 'X509Certificate':
$pemKeys[] = "-----BEGIN CERTIFICATE-----\n" .
chunk_split($key['X509Certificate'], 64) .
"-----END CERTIFICATE-----\n";
break;
default:
Logger::debug('Skipping unknown key type: ' . $key['type']);
}
}
} else {
throw new SSP_Error\Exception(
'Missing certificate in metadata for ' .
var_export($srcMetadata->getString('entityid'), true),
);
}
Logger::debug('Has ' . count($pemKeys) . ' candidate keys for validation.');
$lastException = null;
foreach ($pemKeys as $i => $pem) {
$key = new XMLSecurityKey(XMLSecurityKey::RSA_SHA256, ['type' => 'public']);
$key->loadKey($pem);
try {
// make sure that we have a valid signature on either the response or the assertion
$res = $element->validate($key);
if ($res) {
Logger::debug('Validation with key #' . $i . ' succeeded.');
return true;
}
Logger::debug('Validation with key #' . $i . ' failed without exception.');
} catch (\Exception $e) {
Logger::debug('Check Signature for ' . get_class($element) . ' element');
Logger::debug('EntityID: ' . $srcMetadata->getString('entityid'));
Logger::debug('Validation with key #' . $i . ' failed with exception: ' . $e->getMessage());
$issuerValue = null;
if (method_exists($element, 'getIssuer')) {
$issuer = $element->getIssuer();
if ($issuer instanceof Issuer) {
$issuerValue = $issuer->getValue();
} elseif (is_string($issuer)) {
$issuerValue = $issuer;
}
}
// Clone the exception and improve the message
$lastException = new SSP_Error\Error(
[
ErrorCodes::NOTVALIDCERTSIGNATURE,
'%MESSAGE%' => (new ErrorCodes())->getMessage(ErrorCodes::NOTVALIDCERTSIGNATURE),
'%ELEMENT%' => get_class($element),
'%ISSUER%' => $issuerValue ?? '',
'%ENTITYID%' => $srcMetadata->getString('entityid'),
],
$e->getPrevious(),
);
}
}
// we were unable to validate the signature with any of our keys
if ($lastException !== null) {
throw $lastException;
}
return false;
}
/**
* Check signature on a SAML2 message if enabled.
*
* @param \SimpleSAML\Configuration $srcMetadata The metadata of the sender.
* @param \SimpleSAML\Configuration $dstMetadata The metadata of the recipient.
* @param \SAML2\Message $message The message we should check the signature on.
* @return bool Whether or not the message was validated.
*
* @throws \SimpleSAML\Error\Exception if message validation is enabled, but there is no signature in the message.
*/
public static function validateMessage(
Configuration $srcMetadata,
Configuration $dstMetadata,
SAMLMessage $message,
): bool {
$enabled = null;
if ($message instanceof LogoutRequest || $message instanceof LogoutResponse) {
$enabled = $srcMetadata->getOptionalBoolean('validate.logout', null);
if ($enabled === null) {
$enabled = $dstMetadata->getOptionalBoolean('validate.logout', null);
}
} elseif ($message instanceof AuthnRequest) {
$enabled = $srcMetadata->getOptionalBoolean('validate.authnrequest', null);
if ($enabled === null) {
$enabled = $dstMetadata->getOptionalBoolean('validate.authnrequest', null);
}
}
// If not specifically set to false, the signature must be checked to conform to SAML2INT
if (
(isset($_REQUEST['Signature'])
|| $message->isMessageConstructedWithSignature() === true)
&& ($enabled !== false)
) {
$enabled = true;
} elseif ($enabled === null) {
$enabled = $srcMetadata->getOptionalBoolean('redirect.validate', null);
if ($enabled === null) {
$enabled = $dstMetadata->getOptionalBoolean('redirect.validate', false);
}
}
if (!$enabled) {
return false;
} elseif (!self::checkSign($srcMetadata, $message)) {
throw new SSP_Error\Exception(
'Validation of received messages enabled, but no signature found on message.',
);
}
return true;
}
/**
* Retrieve the decryption keys from metadata.
*
* @param \SimpleSAML\Configuration $srcMetadata The metadata of the sender (IdP).
* @param \SimpleSAML\Configuration $dstMetadata The metadata of the recipient (SP).
* @param string|null $encryptionMethod
* The EncryptionMethod from the assertion.
*
* @return array Array of decryption keys.
*/
public static function getDecryptionKeys(
Configuration $srcMetadata,
Configuration $dstMetadata,
?string $encryptionMethod = null,
): array {
$sharedKey = $srcMetadata->getOptionalString('sharedkey', null);
if ($sharedKey !== null) {
if ($encryptionMethod !== null) {
// @TODO: for saml2v5 replace this line
//$algo = $encryptionMethod->getAlgorithm();
$algo = $encryptionMethod;
} else {
$algo = $srcMetadata->getOptionalString('sharedkey_algorithm', null);
if ($algo === null) {
// If no algorithm is supplied or configured, use a sane default as a last resort
$algo = $dstMetadata->getOptionalString('sharedkey_algorithm', XMLSecurityKey::AES128_GCM);
}
}
$key = new XMLSecurityKey($algo);
$key->loadKey($sharedKey);
return [$key];
}
$keys = [];
$cryptoUtils = new Utils\Crypto();
// load the new private key if it exists
$keyArray = $cryptoUtils->loadPrivateKey($dstMetadata, false, 'new_');
if ($keyArray !== null) {
assert::keyExists($keyArray, 'PEM');
$key = new XMLSecurityKey(XMLSecurityKey::RSA_1_5, ['type' => 'private']);
if (array_key_exists('password', $keyArray)) {
$key->passphrase = $keyArray['password'];
}
$key->loadKey($keyArray['PEM']);
$keys[] = $key;
}
/**
* find the existing private key
*
* @var array $keyArray Because the second param is true
*/
$keyArray = $cryptoUtils->loadPrivateKey($dstMetadata, true);
Assert::keyExists($keyArray, 'PEM');
$key = new XMLSecurityKey(XMLSecurityKey::RSA_1_5, ['type' => 'private']);
if (array_key_exists('password', $keyArray)) {
$key->passphrase = $keyArray['password'];
}
$key->loadKey($keyArray['PEM']);
$keys[] = $key;
return $keys;
}
/**
* Retrieve blacklisted algorithms.
*
* Remote configuration overrides local configuration.
*
* @param \SimpleSAML\Configuration $srcMetadata The metadata of the sender.
* @param \SimpleSAML\Configuration $dstMetadata The metadata of the recipient.
*
* @return array Array of blacklisted algorithms.
*/
public static function getBlacklistedAlgorithms(
Configuration $srcMetadata,
Configuration $dstMetadata,
): array {
$blacklist = $srcMetadata->getOptionalArray('encryption.blacklisted-algorithms', null);
if ($blacklist === null) {
$blacklist = $dstMetadata->getOptionalArray('encryption.blacklisted-algorithms', [XMLSecurityKey::RSA_1_5]);
}
return $blacklist;
}
/**
* Decrypt an assertion.
*
* @param \SimpleSAML\Configuration $srcMetadata The metadata of the sender (IdP).
* @param \SimpleSAML\Configuration $dstMetadata The metadata of the recipient (SP).
* @param \SAML2\Assertion|\SAML2\EncryptedAssertion $assertion The assertion we are decrypting.
*
* @return \SAML2\Assertion The assertion.
*
* @throws \SimpleSAML\Error\Exception if encryption is enabled but the assertion is not encrypted, or if we cannot
* get the decryption keys.
* @throws \Exception if decryption fails for whatever reason.
*/
private static function decryptAssertion(
Configuration $srcMetadata,
Configuration $dstMetadata,
Assertion|EncryptedAssertion $assertion,
): Assertion {
if ($assertion instanceof Assertion) {
$encryptAssertion = $srcMetadata->getOptionalBoolean('assertion.encryption', null);
if ($encryptAssertion === null) {
$encryptAssertion = $dstMetadata->getOptionalBoolean('assertion.encryption', false);
}
if ($encryptAssertion) {
/* The assertion was unencrypted, but we have encryption enabled. */
throw new \Exception('Received unencrypted assertion, but encryption was enabled.');
}
return $assertion;
}
try {
// @todo Enable this code for saml2v5 to automatically determine encryption algorithm
//$encryptionMethod = $assertion->getEncryptedData()->getEncryptionMethod();
//$keys = self::getDecryptionKeys($srcMetadata, $dstMetadata, $encryptionMethod);
$encryptionMethod = null;
$keys = self::getDecryptionKeys($srcMetadata, $dstMetadata, $encryptionMethod);
} catch (\Exception $e) {
throw new SSP_Error\Exception('Error decrypting assertion: ' . $e->getMessage());
}
$blacklist = self::getBlacklistedAlgorithms($srcMetadata, $dstMetadata);
$lastException = null;
foreach ($keys as $i => $key) {
try {
$ret = $assertion->getAssertion($key, $blacklist);
Logger::debug('Decryption with key #' . $i . ' succeeded.');
return $ret;
} catch (\Exception $e) {
Logger::debug('Decryption with key #' . $i . ' failed with exception: ' . $e->getMessage());
$lastException = $e;
}
}
/**
* The annotation below is not working - See vimeo/psalm#1909
* @psalm-suppress InvalidThrow
* @var \Exception $lastException
*/
throw $lastException;
}
/**
* Decrypt any encrypted attributes in an assertion.
*
* @param \SimpleSAML\Configuration $srcMetadata The metadata of the sender (IdP).
* @param \SimpleSAML\Configuration $dstMetadata The metadata of the recipient (SP).
* @param \SAML2\Assertion|\SAML2\Assertion $assertion The assertion containing any possibly encrypted attributes.
*
* @throws \SimpleSAML\Error\Exception if we cannot get the decryption keys or decryption fails.
*/
private static function decryptAttributes(
Configuration $srcMetadata,
Configuration $dstMetadata,
Assertion &$assertion,
): void {
if (!$assertion->hasEncryptedAttributes()) {
return;
}
try {
$keys = self::getDecryptionKeys($srcMetadata, $dstMetadata);
} catch (\Exception $e) {
throw new SSP_Error\Exception('Error decrypting attributes: ' . $e->getMessage());
}
$blacklist = self::getBlacklistedAlgorithms($srcMetadata, $dstMetadata);
$error = true;
foreach ($keys as $i => $key) {
try {
$assertion->decryptAttributes($key, $blacklist);
Logger::debug('Attribute decryption with key #' . $i . ' succeeded.');
$error = false;
break;
} catch (\Exception $e) {
Logger::debug('Attribute decryption failed with exception: ' . $e->getMessage());
}
}
if ($error) {
throw new SSP_Error\Exception('Could not decrypt the attributes');
}
}
/**
* Retrieve the status code of a response as a \SimpleSAML\Module\saml\Error.
*
* @param \SAML2\StatusResponse $response The response.
*
* @return \SimpleSAML\Module\saml\Error The error.
*/
public static function getResponseError(StatusResponse $response): SAMLError
{
$status = $response->getStatus();
return new SAMLError($status['Code'], $status['SubCode'], $status['Message']);
}
/**
* Build an authentication request based on information in the metadata.
*
* @param \SimpleSAML\Configuration $spMetadata The metadata of the service provider.
* @param \SimpleSAML\Configuration $idpMetadata The metadata of the identity provider.
* @return \SAML2\AuthnRequest An authentication request object.
*/
public static function buildAuthnRequest(
Configuration $spMetadata,
Configuration $idpMetadata,
): AuthnRequest {
$ar = new AuthnRequest();
// get the NameIDPolicy to apply. IdP metadata has precedence.
$nameIdPolicy = null;
if ($idpMetadata->hasValue('NameIDPolicy')) {
$nameIdPolicy = $idpMetadata->getValue('NameIDPolicy');
} elseif ($spMetadata->hasValue('NameIDPolicy')) {
$nameIdPolicy = $spMetadata->getValue('NameIDPolicy');
}
$policy = Utils\Config\Metadata::parseNameIdPolicy($nameIdPolicy);
// empty array signals not to set any NameIdPolicy element
if ($policy !== []) {
$ar->setNameIdPolicy($policy);
}
$ar->setForceAuthn($spMetadata->getOptionalBoolean('ForceAuthn', false));
$ar->setIsPassive($spMetadata->getOptionalBoolean('IsPassive', false));
$protbind = $spMetadata->getOptionalValueValidate('ProtocolBinding', [
Constants::BINDING_HTTP_POST,
Constants::BINDING_HOK_SSO,
Constants::BINDING_HTTP_ARTIFACT,
Constants::BINDING_HTTP_REDIRECT,
], Constants::BINDING_HTTP_POST);
// Shoaib: setting the appropriate binding based on parameter in sp-metadata defaults to HTTP_POST
$ar->setProtocolBinding($protbind);
$issuer = new Issuer();
$issuer->setValue($spMetadata->getString('entityID'));
$ar->setIssuer($issuer);
$ar->setAssertionConsumerServiceIndex(
$spMetadata->getOptionalInteger('AssertionConsumerServiceIndex', null),
);
$ar->setAttributeConsumingServiceIndex(
$spMetadata->getOptionalInteger('AttributeConsumingServiceIndex', null),
);
if ($spMetadata->hasValue('AuthnContextClassRef')) {
$accr = $spMetadata->getArrayizeString('AuthnContextClassRef');
$comp = $spMetadata->getOptionalValueValidate(
'AuthnContextComparison',
array_column(AuthnContextComparisonTypeEnum::cases(), 'value'),
AuthnContextComparisonTypeEnum::Exact->value,
);
$ar->setRequestedAuthnContext(['AuthnContextClassRef' => $accr, 'Comparison' => $comp]);
}
self::addRedirectSign($spMetadata, $idpMetadata, $ar);
return $ar;
}
/**
* Build a logout request based on information in the metadata.
*
* @param \SimpleSAML\Configuration $srcMetadata The metadata of the sender.
* @param \SimpleSAML\Configuration $dstMetadata The metadata of the recipient.
* @return \SAML2\LogoutRequest A logout request object.
*/
public static function buildLogoutRequest(
Configuration $srcMetadata,
Configuration $dstMetadata,
): LogoutRequest {
$lr = new LogoutRequest();
$issuer = new Issuer();
$issuer->setValue($srcMetadata->getString('entityid'));
$issuer->setFormat(Constants::NAMEID_ENTITY);
$lr->setIssuer($issuer);
self::addRedirectSign($srcMetadata, $dstMetadata, $lr);
return $lr;
}
/**
* Build a logout response based on information in the metadata.
*
* @param \SimpleSAML\Configuration $srcMetadata The metadata of the sender.
* @param \SimpleSAML\Configuration $dstMetadata The metadata of the recipient.
* @return \SAML2\LogoutResponse A logout response object.
*/
public static function buildLogoutResponse(
Configuration $srcMetadata,
Configuration $dstMetadata,
): LogoutResponse {
$lr = new LogoutResponse();
$issuer = new Issuer();
$issuer->setValue($srcMetadata->getString('entityid'));
$issuer->setFormat(Constants::NAMEID_ENTITY);
$lr->setIssuer($issuer);
self::addRedirectSign($srcMetadata, $dstMetadata, $lr);
return $lr;
}
/**
* Process a response message.
*
* If the response is an error response, we will throw a \SimpleSAML\Module\saml\Error exception with the error.
*
* @param \SimpleSAML\Configuration $spMetadata The metadata of the service provider.
* @param \SimpleSAML\Configuration $idpMetadata The metadata of the identity provider.
* @param \SAML2\Response $response The response.
* @param string|null $expectedRequestId
* The AuthnRequest ID that this Response is expected to correlate with (solicited / SP-initiated flow).
* Caller guarantee: when $isUnsolicited is false, this MUST be the original AuthnRequest ID the SP created.
* @param bool $isUnsolicited
* Whether the Response is processed as unsolicited (IdP-initiated).
* @param string|null $expectedIssuer
* If set, the Response (or Assertion, if Response Issuer is missing) MUST be issued by this entityID.
* This prevents misuse where callers validate a Response using metadata for a different IdP than the one
* the SP state was created for.
*
* @return array Array with \SAML2\Assertion objects, containing valid assertions from the response.
*
* @throws \SimpleSAML\Error\Exception if there are no assertions in the response.
* @throws \Exception if the destination of the response does not match the current URL.
*/
public static function processResponse(
Configuration $spMetadata,
Configuration $idpMetadata,
Response $response,
?string $expectedRequestId = null,
bool $isUnsolicited = false,
?string $expectedIssuer = null,
): array {
if (!$response->isSuccess()) {
throw self::getResponseError($response);
}
// validate Response-element destination
$httpUtils = new Utils\HTTP();
$currentURL = $httpUtils->getSelfURLNoQuery();
$msgDestination = $response->getDestination();
if ($msgDestination !== null && $msgDestination !== $currentURL) {
throw new \Exception(sprintf(
'Destination in response doesn\'t match the current URL. Destination is "%s", current URL is "%s".',
$msgDestination,
$currentURL,
));
}
// Resolve issuer (Response Issuer MAY be missing; fall back to first unencrypted Assertion issuer)
$issuer = $response->getIssuer();
if ($issuer === null) {
foreach ($response->getAssertions() as $a) {
if ($a instanceof Assertion) {
$issuer = $a->getIssuer();
break;
}
}
}
$issuerValue = $issuer?->getValue();
// Expected issuer binding (when provided, issuer must be resolvable and must match)
if ($expectedIssuer !== null) {
if ($issuerValue === null) {
throw new SSP_Error\Exception(sprintf(
'Missing issuer in Response/Assertion; cannot enforce expected issuer %s.',
var_export($expectedIssuer, true),
));
}
if ($issuerValue !== $expectedIssuer) {
throw new SSP_Error\Exception(sprintf(
'Issuer mismatch. Expected %s, got %s.',
var_export($expectedIssuer, true),
var_export($issuerValue, true),
));
}
}
$responseSigned = self::checkSign($idpMetadata, $response);
// Project policy: Response MUST be signed (SAML2Int)
$saml2IntConfig = Configuration::getOptionalConfig('saml2int.conf.php');
$requireSignedResponse = $saml2IntConfig->getOptionalBoolean('response.require_signed', false);
if ($requireSignedResponse && $responseSigned !== true) {
throw new SSP_Error\Exception('Response must be signed.');
}
$responseInResponseTo = $response->getInResponseTo();
// Hard policy:
// - unsolicited: MUST NOT contain Response/@InResponseTo
// - solicited: MUST contain Response/@InResponseTo and MUST match the expected request ID
if ($isUnsolicited) {
if ($responseInResponseTo !== null) {
throw new SSP_Error\Exception(sprintf(
'Unsolicited Response MUST NOT contain InResponseTo. Got: %s',
var_export($responseInResponseTo, true),
));
}
} else {
if ($expectedRequestId === null || $expectedRequestId === '') {
throw new SSP_Error\Exception('Missing expected request ID for solicited Response validation.');
}
if ($responseInResponseTo === null) {
throw new SSP_Error\Exception('Solicited Response must contain InResponseTo.');
}
if ($responseInResponseTo !== $expectedRequestId) {
throw new SSP_Error\Exception(sprintf(
'Response InResponseTo does not match expected request ID. Expected %s, got %s.',
var_export($expectedRequestId, true),
var_export($responseInResponseTo, true),
));
}
}
/*
* When we get this far, the response itself is valid.
* We only need to check signatures and conditions of the response.
*/
$assertion = $response->getAssertions();
if (empty($assertion)) {
throw new SSP_Error\Exception('No assertions found in response from IdP.');
}
$ret = [];
foreach ($assertion as $a) {
$ret[] = self::processAssertion(
$spMetadata,
$idpMetadata,
$response,
$a,
$responseSigned,
$expectedRequestId,
$isUnsolicited,
);
}
return $ret;
}
/**
* Process an assertion in a response.
*
* @param \SimpleSAML\Configuration $spMetadata The metadata of the service provider.
* @param \SimpleSAML\Configuration $idpMetadata The metadata of the identity provider.
* @param \SAML2\Response $response The response containing the assertion.
* @param \SAML2\Assertion|\SAML2\EncryptedAssertion $assertion The assertion.
* @param bool $responseSigned Whether the response is signed.
* @param string|null $expectedRequestId The AuthnRequest ID we expect this response to be correlated with.
* @param bool $isUnsolicited Whether this response is being processed as unsolicited (IdP-initiated).
*
* @return \SAML2\Assertion The assertion, if it is valid.
*
* @throws \SimpleSAML\Error\Exception if an error occurs while trying to validate the assertion, or if a assertion
* is not signed and it should be, or if we are unable to decrypt the NameID due to a local failure (missing or
* invalid decryption key).
* @throws \Exception if we couldn't decrypt the NameID for unexpected reasons.
*/
private static function processAssertion(
Configuration $spMetadata,
Configuration $idpMetadata,
Response $response,
Assertion|EncryptedAssertion $assertion,
bool $responseSigned,
?string $expectedRequestId,
bool $isUnsolicited,
): Assertion {
$assertion = self::decryptAssertion($idpMetadata, $spMetadata, $assertion);
self::decryptAttributes($idpMetadata, $spMetadata, $assertion);
if (!self::checkSign($idpMetadata, $assertion)) {
if (!$responseSigned) {
throw new SSP_Error\Exception('Neither the assertion nor the response was signed.');
}
} // at least one valid signature found
$httpUtils = new Utils\HTTP();
$currentURL = $httpUtils->getSelfURLNoQuery();
$now = time();
// check various properties of the assertion
$config = Configuration::getInstance();
$allowed_clock_skew = $config->getOptionalInteger('assertion.allowed_clock_skew', 180);
$allowed_clock_skew = max(180, min(300, $allowed_clock_skew));
/**
* Validate assertion time constraints and audience restriction.
*
* Uses the following extracted assertion values:
* - Assertion/Conditions/@NotBefore (via {@see \SAML2\Assertion::getNotBefore()})
* - Assertion/Conditions/@NotOnOrAfter (via {@see \SAML2\Assertion::getNotOnOrAfter()})
* - Assertion/AuthnStatement/@SessionNotOnOrAfter (via {@see \SAML2\Assertion::getSessionNotOnOrAfter()})
* - Assertion/Conditions/AudienceRestriction/Audience (via {@see \SAML2\Assertion::getValidAudiences()})
*
* Uses the following local values:
* - $now: current Unix timestamp used as the comparison baseline
* - $allowed_clock_skew: allowed clock skew in seconds
*
* @throws \SimpleSAML\Error\Exception When the assertion is not yet valid, is expired, the session is expired,
* or the SP entityID is not an allowed audience.
*/
self::validateAssertionTimingAndAudience(
$assertion,
$spMetadata,
$now,
$allowed_clock_skew,
);
// is SSO with HoK enabled? IdP remote metadata overwrites SP metadata configuration
$hok = $spMetadata->getOptionalBoolean('saml20.hok.assertion', false);
$found = false;
$lastError = 'No SubjectConfirmation element in Subject.';
foreach ($assertion->getSubjectConfirmation() as $sc) {
try {
self::validateSubjectConfirmation(
$sc,
$hok,
$httpUtils,
$currentURL,
$response,
$now,
$expectedRequestId,
$isUnsolicited,
);
$found = true;
break;
} catch (\SimpleSAML\Error\Exception $e) {
$lastError = $e->getMessage();
continue;
}
}
if (!$found) {
throw new SSP_Error\Exception('Error validating SubjectConfirmation in Assertion: ' . $lastError);
}
// as far as we can tell, the assertion is valid
// decrypt the NameID element if it is encrypted
if ($assertion->isNameIdEncrypted()) {
try {
$keys = self::getDecryptionKeys($idpMetadata, $spMetadata);
} catch (\Exception $e) {
throw new SSP_Error\Exception('Error decrypting NameID: ' . $e->getMessage());
}
$blacklist = self::getBlacklistedAlgorithms($idpMetadata, $spMetadata);
$lastException = null;
foreach ($keys as $i => $key) {
try {
$assertion->decryptNameId($key, $blacklist);
Logger::debug('Decryption with key #' . $i . ' succeeded.');
$lastException = null;
break;
} catch (\Exception $e) {
Logger::debug('Decryption with key #' . $i . ' failed with exception: ' . $e->getMessage());
$lastException = $e;
}
}
if ($lastException !== null) {
throw $lastException;
}
}
return $assertion;
}
/**
* Validates the timing constraints and audience restrictions in the SAML assertion.
*
* @param \SAML2\Assertion $assertion The SAML assertion to validate.
* @param \SimpleSAML\Configuration $spMetadata The metadata of the service provider.
* @param int $now The current timestamp used for validation.
* @param int $allowedClockSkew The maximum allowed clock skew, in seconds, for timing validations.
*
* @throws \SimpleSAML\Error\Exception If the assertion is invalid due to timing constraints or audience issues.
*/
private static function validateAssertionTimingAndAudience(
Assertion $assertion,
Configuration $spMetadata,
int $now,
int $allowedClockSkew,
): void {
$timingChecks = [
[
'value' => $assertion->getNotBefore(),
'invalid' => static fn (int $value): bool => $value > ($now + $allowedClockSkew),
// phpcs:ignore Generic.Files.LineLength
'message' => 'Received an assertion that is valid in the future. Check clock synchronization on IdP and SP.',
],
[
'value' => $assertion->getNotOnOrAfter(),
'invalid' => static fn (int $value): bool => $value <= ($now - $allowedClockSkew),
'message' => 'Received an assertion that has expired. Check clock synchronization on IdP and SP.',
],
[
'value' => $assertion->getSessionNotOnOrAfter(),
'invalid' => static fn (int $value): bool => $value <= ($now - $allowedClockSkew),
// phpcs:ignore Generic.Files.LineLength
'message' => 'Received an assertion with a session that has expired. Check clock synchronization on IdP and SP.',
],
];
foreach ($timingChecks as $check) {
$value = $check['value'];
if ($value === null) {
continue;
}
/** @var \Closure(int):bool $isInvalid */
$isInvalid = $check['invalid'];
if ($isInvalid($value)) {
throw new SSP_Error\Exception($check['message']);
}
}
$validAudiences = $assertion->getValidAudiences();
if ($validAudiences === null) {
return;
}
$spEntityId = $spMetadata->getString('entityid');
if (!in_array($spEntityId, $validAudiences, true)) {
$candidates = '[' . implode('], [', $validAudiences) . ']';
throw new SSP_Error\Exception(sprintf(
'This SP [%s] is not a valid audience for the assertion. Candidates were: %s',
$spEntityId,
$candidates,
));
}
}
/**
* Validates a SubjectConfirmation element in a SAML assertion and checks whether it is valid
* according to the SAML2 specification and binding-specific policies.
*
* This method handles validation of Bearer and Holder-of-Key profiles, ensuring criteria such as:
* - The SubjectConfirmation method is valid for the current SP configuration.
* - Time constraints like NotBefore and NotOnOrAfter are met.
* - The Recipient URL and InResponseTo values are correctly set when applicable.
*
* @param \SAML2\XML\saml\SubjectConfirmation $sc
* The SubjectConfirmation element to validate.
* @param bool $hok
* Whether the Holder-of-Key (HoK) profile is enabled for this SP.
* @param \SimpleSAML\Utils\HTTP $httpUtils
* Utility for accessing HTTP-related data.
* @param string $currentURL
* The current URL used to validate the recipient of the assertion.
* @param \SAML2\Response $response
* The SAML response containing the assertion.
* @param int $now
* The current timestamp for time-based validations.
* @param string|null $expectedRequestId
* The ID of the original AuthnRequest this response is related to, or null for unsolicited responses.
* @param bool $isUnsolicited
* Whether the response was unsolicited (IdP-initiated).
*
* @throws \SimpleSAML\Error\Exception When the SubjectConfirmation is invalid.
*/
private static function validateSubjectConfirmation(
\SAML2\XML\saml\SubjectConfirmation $sc,
bool $hok,
Utils\HTTP $httpUtils,
string $currentURL,
Response $response,
int $now,
?string $expectedRequestId,
bool $isUnsolicited,
): void {
$method = $sc->getMethod();
$scd = $sc->getSubjectConfirmationData();
$validSCMethods = [Constants::CM_BEARER, Constants::CM_HOK, Constants::CM_VOUCHES];
$rejectReason = match (true) {
!in_array($method, $validSCMethods, true)
=> sprintf('Invalid Method on SubjectConfirmation: %s', var_export($method, true)),
$method === Constants::CM_BEARER && $hok
=> 'Bearer SubjectConfirmation received, but Holder-of-Key SubjectConfirmation needed',
$method === Constants::CM_HOK && !$hok
=> 'Holder-of-Key SubjectConfirmation received, but the Holder-of-Key profile is not enabled.',
$scd === null
=> 'No SubjectConfirmationData provided',
default
=> null,
};
if ($rejectReason !== null) {
throw new SSP_Error\Exception($rejectReason);
}
/** @var \SAML2\XML\saml\SubjectConfirmationData $scd */
if ($method === Constants::CM_HOK) {
$hokError = self::validateHolderOfKeySubjectConfirmationData($scd, $httpUtils);
if ($hokError !== null) {
throw new SSP_Error\Exception($hokError);