File: SAML2.php

package info (click to toggle)
simplesamlphp 1.13.1-2%2Bdeb8u1
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 11,304 kB
  • sloc: php: 65,124; xml: 629; python: 376; sh: 193; perl: 185; makefile: 43
file content (1047 lines) | stat: -rw-r--r-- 36,540 bytes parent folder | download | duplicates (2)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
<?php

/**
 * IdP implementation for SAML 2.0 protocol.
 *
 * @package simpleSAMLphp
 */
class sspmod_saml_IdP_SAML2 {

	/**
	 * Send a response to the SP.
	 *
	 * @param array $state  The authentication state.
	 */
	public static function sendResponse(array $state) {
		assert('isset($state["Attributes"])');
		assert('isset($state["SPMetadata"])');
		assert('isset($state["saml:ConsumerURL"])');
		assert('array_key_exists("saml:RequestId", $state)'); // Can be NULL.
		assert('array_key_exists("saml:RelayState", $state)'); // Can be NULL.

		$spMetadata = $state["SPMetadata"];
		$spEntityId = $spMetadata['entityid'];
		$spMetadata = SimpleSAML_Configuration::loadFromArray($spMetadata,
			'$metadata[' . var_export($spEntityId, TRUE) . ']');

		SimpleSAML_Logger::info('Sending SAML 2.0 Response to ' . var_export($spEntityId, TRUE));

		$requestId = $state['saml:RequestId'];
		$relayState = $state['saml:RelayState'];
		$consumerURL = $state['saml:ConsumerURL'];
		$protocolBinding = $state['saml:Binding'];

		$idp = SimpleSAML_IdP::getByState($state);

		$idpMetadata = $idp->getConfig();

		$assertion = self::buildAssertion($idpMetadata, $spMetadata, $state);
		
		if (isset($state['saml:AuthenticatingAuthority'])) {
			$assertion->setAuthenticatingAuthority($state['saml:AuthenticatingAuthority']);
		}

		/* Create the session association (for logout). */
		$association = array(
			'id' => 'saml:' . $spEntityId,
			'Handler' => 'sspmod_saml_IdP_SAML2',
			'Expires' => $assertion->getSessionNotOnOrAfter(),
			'saml:entityID' => $spEntityId,
			'saml:NameID' => $state['saml:idp:NameID'],
			'saml:SessionIndex' => $assertion->getSessionIndex(),
		);

		/* Maybe encrypt the assertion. */
		$assertion = self::encryptAssertion($idpMetadata, $spMetadata, $assertion);

		/* Create the response. */
		$ar = self::buildResponse($idpMetadata, $spMetadata, $consumerURL);
		$ar->setInResponseTo($requestId);
		$ar->setRelayState($relayState);
		$ar->setAssertions(array($assertion));

		/* Register the session association with the IdP. */
		$idp->addAssociation($association);

		$statsData = array(
			'spEntityID' => $spEntityId,
			'idpEntityID' => $idpMetadata->getString('entityid'),
			'protocol' => 'saml2',
		);
		if (isset($state['saml:AuthnRequestReceivedAt'])) {
			$statsData['logintime'] = microtime(TRUE) - $state['saml:AuthnRequestReceivedAt'];
		}
		SimpleSAML_Stats::log('saml:idp:Response', $statsData);

		/* Send the response. */
		$binding = SAML2_Binding::getBinding($protocolBinding);
		$binding->send($ar);
	}


	/**
	 * Handle authentication error.
	 *
	 * SimpleSAML_Error_Exception $exception  The exception.
	 * @param array $state  The error state.
	 */
	public static function handleAuthError(SimpleSAML_Error_Exception $exception, array $state) {
		assert('isset($state["SPMetadata"])');
		assert('isset($state["saml:ConsumerURL"])');
		assert('array_key_exists("saml:RequestId", $state)'); // Can be NULL.
		assert('array_key_exists("saml:RelayState", $state)'); // Can be NULL.

		$spMetadata = $state["SPMetadata"];
		$spEntityId = $spMetadata['entityid'];
		$spMetadata = SimpleSAML_Configuration::loadFromArray($spMetadata,
			'$metadata[' . var_export($spEntityId, TRUE) . ']');

		$requestId = $state['saml:RequestId'];
		$relayState = $state['saml:RelayState'];
		$consumerURL = $state['saml:ConsumerURL'];
		$protocolBinding = $state['saml:Binding'];

		$idp = SimpleSAML_IdP::getByState($state);

		$idpMetadata = $idp->getConfig();

		$error = sspmod_saml_Error::fromException($exception);

		SimpleSAML_Logger::warning('Returning error to sp: ' . var_export($spEntityId, TRUE));
		$error->logWarning();

		$ar = self::buildResponse($idpMetadata, $spMetadata, $consumerURL);
		$ar->setInResponseTo($requestId);
		$ar->setRelayState($relayState);

		$status = array(
			'Code' => $error->getStatus(),
			'SubCode' => $error->getSubStatus(),
			'Message' => $error->getStatusMessage(),
		);
		$ar->setStatus($status);

		$statsData = array(
			'spEntityID' => $spEntityId,
			'idpEntityID' => $idpMetadata->getString('entityid'),
			'protocol' => 'saml2',
			'error' => $status,
		);
		if (isset($state['saml:AuthnRequestReceivedAt'])) {
			$statsData['logintime'] = microtime(TRUE) - $state['saml:AuthnRequestReceivedAt'];
		}
		SimpleSAML_Stats::log('saml:idp:Response:error', $statsData);

		$binding = SAML2_Binding::getBinding($protocolBinding);
		$binding->send($ar);
	}


	/**
	 * Find SP AssertionConsumerService based on parameter in AuthnRequest.
	 *
	 * @param array $supportedBindings  The bindings we allow for the response.
	 * @param SimpleSAML_Configuration $spMetadata  The metadata for the SP.
	 * @param string|NULL $AssertionConsumerServiceURL  AssertionConsumerServiceURL from request.
	 * @param string|NULL $ProtocolBinding  ProtocolBinding from request.
	 * @param int|NULL $AssertionConsumerServiceIndex  AssertionConsumerServiceIndex from request.
	 * @return array  Array with the Location and Binding we should use for the response.
	 */
	private static function getAssertionConsumerService(array $supportedBindings, SimpleSAML_Configuration $spMetadata,
		$AssertionConsumerServiceURL, $ProtocolBinding, $AssertionConsumerServiceIndex) {
		assert('is_string($AssertionConsumerServiceURL) || is_null($AssertionConsumerServiceURL)');
		assert('is_string($ProtocolBinding) || is_null($ProtocolBinding)');
		assert('is_int($AssertionConsumerServiceIndex) || is_null($AssertionConsumerServiceIndex)');

		/* We want to pick the best matching endpoint in the case where for example
		 * only the ProtocolBinding is given. We therefore pick endpoints with the
		 * following priority:
		 *  1. isDefault="true"
		 *  2. isDefault unset
		 *  3. isDefault="false"
		 */
		$firstNotFalse = NULL;
		$firstFalse = NULL;
		foreach ($spMetadata->getEndpoints('AssertionConsumerService') as $ep) {

			if ($AssertionConsumerServiceURL !== NULL && $ep['Location'] !== $AssertionConsumerServiceURL) {
				continue;
			}
			if ($ProtocolBinding !== NULL && $ep['Binding'] !== $ProtocolBinding) {
				continue;
			}
			if ($AssertionConsumerServiceIndex !== NULL && $ep['index'] !== $AssertionConsumerServiceIndex) {
				continue;
			}

			if (!in_array($ep['Binding'], $supportedBindings, TRUE)) {
				/* The endpoint has an unsupported binding. */
				continue;
			}

			/* We have an endpoint that matches all our requirements. Check if it is the best one. */

			if (array_key_exists('isDefault', $ep)) {
				if ($ep['isDefault'] === TRUE) {
					/* This is the first matching endpoint with isDefault set to TRUE. */
					return $ep;
				}
				/* isDefault is set to FALSE, but the endpoint is still useable. */
				if ($firstFalse === NULL) {
					/* This is the first endpoint that we can use. */
					$firstFalse = $ep;
				}
			} else if ($firstNotFalse === NULL) {
				/* This is the first endpoint without isDefault set. */
				$firstNotFalse = $ep;
			}
		}

		if ($firstNotFalse !== NULL) {
			return $firstNotFalse;
		} elseif ($firstFalse !== NULL) {
			return $firstFalse;
		}

		SimpleSAML_Logger::warning('Authentication request specifies invalid AssertionConsumerService:');
		if ($AssertionConsumerServiceURL !== NULL) {
			SimpleSAML_Logger::warning('AssertionConsumerServiceURL: ' . var_export($AssertionConsumerServiceURL, TRUE));
		}
		if ($ProtocolBinding !== NULL) {
			SimpleSAML_Logger::warning('ProtocolBinding: ' . var_export($ProtocolBinding, TRUE));
		}
		if ($AssertionConsumerServiceIndex !== NULL) {
			SimpleSAML_Logger::warning('AssertionConsumerServiceIndex: ' . var_export($AssertionConsumerServiceIndex, TRUE));
		}

		/* We have no good endpoints. Our last resort is to just use the default endpoint. */
		return $spMetadata->getDefaultEndpoint('AssertionConsumerService', $supportedBindings);
	}


	/**
	 * Receive an authentication request.
	 *
	 * @param SimpleSAML_IdP $idp  The IdP we are receiving it for.
	 */
	public static function receiveAuthnRequest(SimpleSAML_IdP $idp) {

		$metadata = SimpleSAML_Metadata_MetaDataStorageHandler::getMetadataHandler();
		$idpMetadata = $idp->getConfig();

		$supportedBindings = array(SAML2_Const::BINDING_HTTP_POST);
		if ($idpMetadata->getBoolean('saml20.sendartifact', FALSE)) {
			$supportedBindings[] = SAML2_Const::BINDING_HTTP_ARTIFACT;
		}
		if ($idpMetadata->getBoolean('saml20.hok.assertion', FALSE)) {
			$supportedBindings[] = SAML2_Const::BINDING_HOK_SSO;
		}

		if (isset($_REQUEST['spentityid'])) {
			/* IdP initiated authentication. */

			if (isset($_REQUEST['cookieTime'])) {
				$cookieTime = (int)$_REQUEST['cookieTime'];
				if ($cookieTime + 5 > time()) {
					/*
					 * Less than five seconds has passed since we were
					 * here the last time. Cookies are probably disabled.
					 */
					SimpleSAML_Utilities::checkCookie(SimpleSAML_Utilities::selfURL());
				}
			}

			$spEntityId = (string)$_REQUEST['spentityid'];
			$spMetadata = $metadata->getMetaDataConfig($spEntityId, 'saml20-sp-remote');

			if (isset($_REQUEST['RelayState'])) {
				$relayState = (string)$_REQUEST['RelayState'];
			} else {
				$relayState = NULL;
			}

			if (isset($_REQUEST['binding'])){
				$protocolBinding = (string)$_REQUEST['binding'];
			} else {
				$protocolBinding = NULL;
			}

			if (isset($_REQUEST['NameIDFormat'])) {
				$nameIDFormat = (string)$_REQUEST['NameIDFormat'];
			} else {
				$nameIDFormat = NULL;
			}

			$requestId = NULL;
			$IDPList = array();
			$ProxyCount = NULL;
			$RequesterID = NULL;
			$forceAuthn = FALSE;
			$isPassive = FALSE;
			$consumerURL = NULL;
			$consumerIndex = NULL;
			$extensions = NULL;
			$allowCreate = TRUE;

			$idpInit = TRUE;

			SimpleSAML_Logger::info('SAML2.0 - IdP.SSOService: IdP initiated authentication: '. var_export($spEntityId, TRUE));

		} else {

			$binding = SAML2_Binding::getCurrentBinding();
			$request = $binding->receive();

			if (!($request instanceof SAML2_AuthnRequest)) {
				throw new SimpleSAML_Error_BadRequest('Message received on authentication request endpoint wasn\'t an authentication request.');
			}

			$spEntityId = $request->getIssuer();
			if ($spEntityId === NULL) {
				throw new SimpleSAML_Error_BadRequest('Received message on authentication request endpoint without issuer.');
			}
			$spMetadata = $metadata->getMetaDataConfig($spEntityId, 'saml20-sp-remote');

			sspmod_saml_Message::validateMessage($spMetadata, $idpMetadata, $request);

			$relayState = $request->getRelayState();

			$requestId = $request->getId();
			$IDPList = $request->getIDPList();
			$ProxyCount = $request->getProxyCount();
			if ($ProxyCount !== null) $ProxyCount--;
			$RequesterID = $request->getRequesterID();
			$forceAuthn = $request->getForceAuthn();
			$isPassive = $request->getIsPassive();
			$consumerURL = $request->getAssertionConsumerServiceURL();
			$protocolBinding = $request->getProtocolBinding();
			$consumerIndex = $request->getAssertionConsumerServiceIndex();
			$extensions = $request->getExtensions();

			$nameIdPolicy = $request->getNameIdPolicy();
			if (isset($nameIdPolicy['Format'])) {
				$nameIDFormat = $nameIdPolicy['Format'];
			} else {
				$nameIDFormat = NULL;
			}
			if (isset($nameIdPolicy['AllowCreate'])) {
				$allowCreate = $nameIdPolicy['AllowCreate'];
			} else {
				$allowCreate = FALSE;
			}

			$idpInit = FALSE;

			SimpleSAML_Logger::info('SAML2.0 - IdP.SSOService: Incomming Authentication request: '. var_export($spEntityId, TRUE));
		}

		SimpleSAML_Stats::log('saml:idp:AuthnRequest', array(
			'spEntityID' => $spEntityId,
			'idpEntityID' => $idpMetadata->getString('entityid'),
			'forceAuthn' => $forceAuthn,
			'isPassive' => $isPassive,
			'protocol' => 'saml2',
			'idpInit' => $idpInit,
		));

		$acsEndpoint = self::getAssertionConsumerService($supportedBindings, $spMetadata, $consumerURL, $protocolBinding, $consumerIndex);

		$IDPList = array_unique(array_merge($IDPList, $spMetadata->getArrayizeString('IDPList', array())));
		if ($ProxyCount == null) $ProxyCount = $spMetadata->getInteger('ProxyCount', null);

		if (!$forceAuthn) {
			$forceAuthn = $spMetadata->getBoolean('ForceAuthn', FALSE);
		}

		$sessionLostParams = array(
			'spentityid' => $spEntityId,
			'cookieTime' => time(),
		);
		if ($relayState !== NULL) {
			$sessionLostParams['RelayState'] = $relayState;
		}

		$sessionLostURL = SimpleSAML_Utilities::addURLparameter(
			SimpleSAML_Utilities::selfURLNoQuery(),
			$sessionLostParams);

		$state = array(
			'Responder' => array('sspmod_saml_IdP_SAML2', 'sendResponse'),
			SimpleSAML_Auth_State::EXCEPTION_HANDLER_FUNC => array('sspmod_saml_IdP_SAML2', 'handleAuthError'),
			SimpleSAML_Auth_State::RESTART => $sessionLostURL,

			'SPMetadata' => $spMetadata->toArray(),
			'saml:RelayState' => $relayState,
			'saml:RequestId' => $requestId,
			'saml:IDPList' => $IDPList,
			'saml:ProxyCount' => $ProxyCount,
			'saml:RequesterID' => $RequesterID,
			'ForceAuthn' => $forceAuthn,
			'isPassive' => $isPassive,
			'saml:ConsumerURL' => $acsEndpoint['Location'],
			'saml:Binding' => $acsEndpoint['Binding'],
			'saml:NameIDFormat' => $nameIDFormat,
			'saml:AllowCreate' => $allowCreate,
			'saml:Extensions' => $extensions,
			'saml:AuthnRequestReceivedAt' => microtime(TRUE),
		);

		$idp->handleAuthenticationRequest($state);
	}


	/**
	 * Send a logout request to a given association.
	 *
	 * @param SimpleSAML_IdP $idp  The IdP we are sending a logout request from.
	 * @param array $association  The association that should be terminated.
	 * @param string|NULL $relayState  An id that should be carried across the logout.
	 */
	public static function sendLogoutRequest(SimpleSAML_IdP $idp, array $association, $relayState) {
		assert('is_string($relayState) || is_null($relayState)');

		SimpleSAML_Logger::info('Sending SAML 2.0 LogoutRequest to: '. var_export($association['saml:entityID'], TRUE));

		$metadata = SimpleSAML_Metadata_MetaDataStorageHandler::getMetadataHandler();
		$idpMetadata = $idp->getConfig();
		$spMetadata = $metadata->getMetaDataConfig($association['saml:entityID'], 'saml20-sp-remote');

		SimpleSAML_Stats::log('saml:idp:LogoutRequest:sent', array(
			'spEntityID' => $association['saml:entityID'],
			'idpEntityID' => $idpMetadata->getString('entityid'),
		));

		$dst = $spMetadata->getEndpointPrioritizedByBinding('SingleLogoutService', array(
			SAML2_Const::BINDING_HTTP_REDIRECT,
			SAML2_Const::BINDING_HTTP_POST)
		);
		$binding = SAML2_Binding::getBinding($dst['Binding']);
		$lr = self::buildLogoutRequest($idpMetadata, $spMetadata, $association, $relayState);
		$lr->setDestination($dst['Location']);

		$binding->send($lr);
	}


	/**
	 * Send a logout response.
	 *
	 * @param SimpleSAML_IdP $idp  The IdP we are sending a logout request from.
	 * @param array &$state  The logout state array.
	 */
	public static function sendLogoutResponse(SimpleSAML_IdP $idp, array $state) {
		assert('isset($state["saml:SPEntityId"])');
		assert('isset($state["saml:RequestId"])');
		assert('array_key_exists("saml:RelayState", $state)'); // Can be NULL.

		$spEntityId = $state['saml:SPEntityId'];

		$metadata = SimpleSAML_Metadata_MetaDataStorageHandler::getMetadataHandler();
		$idpMetadata = $idp->getConfig();
		$spMetadata = $metadata->getMetaDataConfig($spEntityId, 'saml20-sp-remote');

		$lr = sspmod_saml_Message::buildLogoutResponse($idpMetadata, $spMetadata);
		$lr->setInResponseTo($state['saml:RequestId']);
		$lr->setRelayState($state['saml:RelayState']);

		if (isset($state['core:Failed']) && $state['core:Failed']) {
			$partial = TRUE;
			$lr->setStatus(array(
				'Code' => SAML2_Const::STATUS_SUCCESS,
				'SubCode' => SAML2_Const::STATUS_PARTIAL_LOGOUT,
			));
			SimpleSAML_Logger::info('Sending logout response for partial logout to SP ' . var_export($spEntityId, TRUE));
		} else {
			$partial = FALSE;
			SimpleSAML_Logger::debug('Sending logout response to SP ' . var_export($spEntityId, TRUE));
		}

		SimpleSAML_Stats::log('saml:idp:LogoutResponse:sent', array(
			'spEntityID' => $spEntityId,
			'idpEntityID' => $idpMetadata->getString('entityid'),
			'partial' => $partial
		));
		$dst = $spMetadata->getEndpointPrioritizedByBinding('SingleLogoutService', array(
			SAML2_Const::BINDING_HTTP_REDIRECT,
			SAML2_Const::BINDING_HTTP_POST)
		);
		$binding = SAML2_Binding::getBinding($dst['Binding']);
		if (isset($dst['ResponseLocation'])) {
			$dst = $dst['ResponseLocation'];
		} else {
			$dst = $dst['Location'];
		}
		$lr->setDestination($dst);

		$binding->send($lr);
	}


	/**
	 * Receive a logout message.
	 *
	 * @param SimpleSAML_IdP $idp  The IdP we are receiving it for.
	 */
	public static function receiveLogoutMessage(SimpleSAML_IdP $idp) {

		$binding = SAML2_Binding::getCurrentBinding();
		$message = $binding->receive();

		$spEntityId = $message->getIssuer();
		if ($spEntityId === NULL) {
			/* Without an issuer we have no way to respond to the message. */
			throw new SimpleSAML_Error_BadRequest('Received message on logout endpoint without issuer.');
		}

		$metadata = SimpleSAML_Metadata_MetaDataStorageHandler::getMetadataHandler();
		$idpMetadata = $idp->getConfig();
		$spMetadata = $metadata->getMetaDataConfig($spEntityId, 'saml20-sp-remote');

		sspmod_saml_Message::validateMessage($spMetadata, $idpMetadata, $message);

		if ($message instanceof SAML2_LogoutResponse) {

			SimpleSAML_Logger::info('Received SAML 2.0 LogoutResponse from: '. var_export($spEntityId, TRUE));
			$statsData = array(
				'spEntityID' => $spEntityId,
				'idpEntityID' => $idpMetadata->getString('entityid'),
			);
			if (!$message->isSuccess()) {
				$statsData['error'] = $message->getStatus();
			}
			SimpleSAML_Stats::log('saml:idp:LogoutResponse:recv', $statsData);

			$relayState = $message->getRelayState();

			if (!$message->isSuccess()) {
				$logoutError = sspmod_saml_Message::getResponseError($message);
				SimpleSAML_Logger::warning('Unsuccessful logout. Status was: ' . $logoutError);
			} else {
				$logoutError = NULL;
			}

			$assocId = 'saml:' . $spEntityId;

			$idp->handleLogoutResponse($assocId, $relayState, $logoutError);


		} elseif ($message instanceof SAML2_LogoutRequest) {

			SimpleSAML_Logger::info('Received SAML 2.0 LogoutRequest from: '. var_export($spEntityId, TRUE));
			SimpleSAML_Stats::log('saml:idp:LogoutRequest:recv', array(
				'spEntityID' => $spEntityId,
				'idpEntityID' => $idpMetadata->getString('entityid'),
			));

			$spStatsId = $spMetadata->getString('core:statistics-id', $spEntityId);
			SimpleSAML_Logger::stats('saml20-idp-SLO spinit ' . $spStatsId . ' ' . $idpMetadata->getString('entityid'));

			$state = array(
				'Responder' => array('sspmod_saml_IdP_SAML2', 'sendLogoutResponse'),
				'saml:SPEntityId' => $spEntityId,
				'saml:RelayState' => $message->getRelayState(),
				'saml:RequestId' => $message->getId(),
			);

			$assocId = 'saml:' . $spEntityId;
			$idp->handleLogoutRequest($state, $assocId);

		} else {
			throw new SimpleSAML_Error_BadRequest('Unknown message received on logout endpoint: ' . get_class($message));
		}

	}


	/**
     * Retrieve a logout URL for a given logout association.
	 *
	 * @param SimpleSAML_IdP $idp  The IdP we are sending a logout request from.
	 * @param array $association  The association that should be terminated.
	 * @param string|NULL $relayState  An id that should be carried across the logout.
	 */
	public static function getLogoutURL(SimpleSAML_IdP $idp, array $association, $relayState) {
		assert('is_string($relayState) || is_null($relayState)');

		SimpleSAML_Logger::info('Sending SAML 2.0 LogoutRequest to: '. var_export($association['saml:entityID'], TRUE));

		$metadata = SimpleSAML_Metadata_MetaDataStorageHandler::getMetadataHandler();
		$idpMetadata = $idp->getConfig();
		$spMetadata = $metadata->getMetaDataConfig($association['saml:entityID'], 'saml20-sp-remote');

		$bindings = array(SAML2_Const::BINDING_HTTP_REDIRECT,
						  SAML2_Const::BINDING_HTTP_POST);
		$dst = $spMetadata->getEndpointPrioritizedByBinding('SingleLogoutService', $bindings);

		if ($dst['Binding'] === SAML2_Const::BINDING_HTTP_POST) {
			$params = array('association' => $association['id'], 'idp' => $idp->getId());
			if ($relayState !== NULL) {
				$params['RelayState'] = $relayState;
			}
			return SimpleSAML_Module::getModuleURL('core/idp/logout-iframe-post.php', $params);
		}

		$lr = self::buildLogoutRequest($idpMetadata, $spMetadata, $association, $relayState);
		$lr->setDestination($dst['Location']);

		$binding = new SAML2_HTTPRedirect();
		return $binding->getRedirectURL($lr);
	}


	/**
	 * Retrieve the metadata for the given SP association.
	 *
	 * @param SimpleSAML_IdP $idp  The IdP the association belongs to.
	 * @param array $association  The SP association.
	 * @return SimpleSAML_Configuration  Configuration object for the SP metadata.
	 */
	public static function getAssociationConfig(SimpleSAML_IdP $idp, array $association) {
		$metadata = SimpleSAML_Metadata_MetaDataStorageHandler::getMetadataHandler();
		try {
			return $metadata->getMetaDataConfig($association['saml:entityID'], 'saml20-sp-remote');
		} catch (Exception $e) {
			return SimpleSAML_Configuration::loadFromArray(array(), 'Unknown SAML 2 entity.');
		}
	}


	/**
	 * Calculate the NameID value that should be used.
	 *
	 * @param SimpleSAML_Configuration $idpMetadata  The metadata of the IdP.
	 * @param SimpleSAML_Configuration $dstMetadata  The metadata of the SP.
	 * @param array &$state  The authentication state of the user.
	 * @return string  The NameID value.
	 */
	private static function generateNameIdValue(SimpleSAML_Configuration $idpMetadata,
		SimpleSAML_Configuration $spMetadata, array &$state) {

		$attribute = $spMetadata->getString('simplesaml.nameidattribute', NULL);
		if ($attribute === NULL) {
			$attribute = $idpMetadata->getString('simplesaml.nameidattribute', NULL);
			if ($attribute === NULL) {
				if (!isset($state['UserID'])) {
					SimpleSAML_Logger::error('Unable to generate NameID. Check the userid.attribute option.');
				}
				$attributeValue = $state['UserID'];
				$idpEntityId = $idpMetadata->getString('entityid');
				$spEntityId = $spMetadata->getString('entityid');

				$secretSalt = SimpleSAML_Utilities::getSecretSalt();

				$uidData = 'uidhashbase' . $secretSalt;
				$uidData .= strlen($idpEntityId) . ':' . $idpEntityId;
				$uidData .= strlen($spEntityId) . ':' . $spEntityId;
				$uidData .= strlen($attributeValue) . ':' . $attributeValue;
				$uidData .= $secretSalt;

				return hash('sha1', $uidData);
			}
		}

		$attributes = $state['Attributes'];
		if (!array_key_exists($attribute, $attributes)) {
			SimpleSAML_Logger::error('Unable to add NameID: Missing ' . var_export($attribute, TRUE) .
				' in the attributes of the user.');
			return NULL;
		}

		return $attributes[$attribute][0];
	}


	/**
	 * Helper function for encoding attributes.
	 *
	 * @param SimpleSAML_Configuration $idpMetadata  The metadata of the IdP.
	 * @param SimpleSAML_Configuration $spMetadata  The metadata of the SP.
	 * @param array $attributes  The attributes of the user
	 * @return array  The encoded attributes.
	 */
	private static function encodeAttributes(SimpleSAML_Configuration $idpMetadata,
		SimpleSAML_Configuration $spMetadata, array $attributes) {

		$base64Attributes = $spMetadata->getBoolean('base64attributes', NULL);
		if ($base64Attributes === NULL) {
			$base64Attributes = $idpMetadata->getBoolean('base64attributes', FALSE);
		}

		if ($base64Attributes) {
			$defaultEncoding = 'base64';
		} else {
			$defaultEncoding = 'string';
		}

		$srcEncodings = $idpMetadata->getArray('attributeencodings', array());
		$dstEncodings = $spMetadata->getArray('attributeencodings', array());

		/*
		 * Merge the two encoding arrays. Encodings specified in the target metadata
		 * takes precedence over the source metadata.
		 */
		$encodings = array_merge($srcEncodings, $dstEncodings);

		$ret = array();
		foreach ($attributes as $name => $values) {
			$ret[$name] = array();
			if (array_key_exists($name, $encodings)) {
				$encoding = $encodings[$name];
			} else {
				$encoding = $defaultEncoding;
			}

			foreach ($values as $value) {
                // allow null values
                if ($value === null) {
                    $ret[$name][] = $value;
                    continue;
                }

				switch ($encoding) {
				case 'string':
					$value = (string)$value;
					break;
				case 'base64':
					$value = base64_encode((string)$value);
					break;
				case 'raw':
					if (is_string($value)) {
						$doc = new DOMDocument();
						$doc->loadXML('<root>' . $value . '</root>');
						$value = $doc->firstChild->childNodes;
					}
					assert('$value instanceof DOMNodeList');
					break;
				default:
					throw new SimpleSAML_Error_Exception('Invalid encoding for attribute ' .
						var_export($name, TRUE) . ': ' . var_export($encoding, TRUE));
				}
				$ret[$name][] = $value;
			}
		}

		return $ret;
	}


	/**
	 * Determine which NameFormat we should use for attributes.
	 *
	 * @param SimpleSAML_Configuration $idpMetadata  The metadata of the IdP.
	 * @param SimpleSAML_Configuration $spMetadata  The metadata of the SP.
	 * @return string  The NameFormat.
	 */
	private static function getAttributeNameFormat(SimpleSAML_Configuration $idpMetadata,
		SimpleSAML_Configuration $spMetadata) {

		/* Try SP metadata first. */
		$attributeNameFormat = $spMetadata->getString('attributes.NameFormat', NULL);
		if ($attributeNameFormat !== NULL) {
			return $attributeNameFormat;
		}
		$attributeNameFormat = $spMetadata->getString('AttributeNameFormat', NULL);
		if ($attributeNameFormat !== NULL) {
			return $attributeNameFormat;
		}

		/* Look in IdP metadata. */
		$attributeNameFormat = $idpMetadata->getString('attributes.NameFormat', NULL);
		if ($attributeNameFormat !== NULL) {
			return $attributeNameFormat;
		}
		$attributeNameFormat = $idpMetadata->getString('AttributeNameFormat', NULL);
		if ($attributeNameFormat !== NULL) {
			return $attributeNameFormat;
		}

		/* Default. */
		return 'urn:oasis:names:tc:SAML:2.0:attrname-format:basic';
	}


	/**
	 * Build an assertion based on information in the metadata.
	 *
	 * @param SimpleSAML_Configuration $idpMetadata  The metadata of the IdP.
	 * @param SimpleSAML_Configuration $spMetadata  The metadata of the SP.
	 * @param array &$state  The state array with information about the request.
	 * @return SAML2_Assertion  The assertion.
	 */
	private static function buildAssertion(SimpleSAML_Configuration $idpMetadata,
		SimpleSAML_Configuration $spMetadata, array &$state) {
		assert('isset($state["Attributes"])');
		assert('isset($state["saml:ConsumerURL"])');

		$signAssertion = $spMetadata->getBoolean('saml20.sign.assertion', NULL);
		if ($signAssertion === NULL) {
			$signAssertion = $idpMetadata->getBoolean('saml20.sign.assertion', TRUE);
		}

		$config = SimpleSAML_Configuration::getInstance();

		$a = new SAML2_Assertion();
		if ($signAssertion) {
			sspmod_saml_Message::addSign($idpMetadata, $spMetadata, $a);
		}

		$a->setIssuer($idpMetadata->getString('entityid'));
		$a->setValidAudiences(array($spMetadata->getString('entityid')));

		$a->setNotBefore(time() - 30);

		$assertionLifetime = $spMetadata->getInteger('assertion.lifetime', NULL);
		if ($assertionLifetime === NULL) {
			$assertionLifetime = $idpMetadata->getInteger('assertion.lifetime', 300);
		}
		$a->setNotOnOrAfter(time() + $assertionLifetime);

		if (isset($state['saml:AuthnContextClassRef'])) {
			$a->setAuthnContext($state['saml:AuthnContextClassRef']);
		} else {
			$a->setAuthnContext(SAML2_Const::AC_PASSWORD);
		}

		if (isset($state['AuthnInstant'])) {
			$a->setAuthnInstant($state['AuthnInstant']);
		} else {
			/* For backwards compatibility. Remove in version 1.8. */
			$session = SimpleSAML_Session::getSessionFromRequest();
			$a->setAuthnInstant($session->getAuthnInstant());
		}

		$sessionLifetime = $config->getInteger('session.duration', 8*60*60);
		$a->setSessionNotOnOrAfter(time() + $sessionLifetime);

		$a->setSessionIndex(SimpleSAML_Utilities::generateID());

		$sc = new SAML2_XML_saml_SubjectConfirmation();
		$sc->SubjectConfirmationData = new SAML2_XML_saml_SubjectConfirmationData();
		$sc->SubjectConfirmationData->NotOnOrAfter = time() + $assertionLifetime;
		$sc->SubjectConfirmationData->Recipient = $state['saml:ConsumerURL'];
		$sc->SubjectConfirmationData->InResponseTo = $state['saml:RequestId'];

		/* ProtcolBinding of SP's <AuthnRequest> overwrites IdP hosted metadata configuration. */
		$hokAssertion = NULL;
		if ($state['saml:Binding'] === SAML2_Const::BINDING_HOK_SSO) {
		    $hokAssertion = TRUE;
		}
		if ($hokAssertion === NULL) {
			$hokAssertion = $idpMetadata->getBoolean('saml20.hok.assertion', FALSE);
		}

		if ($hokAssertion) {
			/* Holder-of-Key */
			$sc->Method = SAML2_Const::CM_HOK;
			if (SimpleSAML_Utilities::isHTTPS()) {
				if (isset($_SERVER['SSL_CLIENT_CERT']) && !empty($_SERVER['SSL_CLIENT_CERT'])) {
					/* Extract certificate data (if this is a certificate). */
					$clientCert = $_SERVER['SSL_CLIENT_CERT'];
					$pattern = '/^-----BEGIN CERTIFICATE-----([^-]*)^-----END CERTIFICATE-----/m';
					if (preg_match($pattern, $clientCert, $matches)) {
						/* We have a client certificate from the browser which we add to the HoK assertion. */
						$x509Certificate = new SAML2_XML_ds_X509Certificate();
						$x509Certificate->certificate = str_replace(array("\r", "\n", " "), '', $matches[1]);

						$x509Data = new SAML2_XML_ds_X509Data();
						$x509Data->data[] = $x509Certificate;

						$keyInfo = new SAML2_XML_ds_KeyInfo();
						$keyInfo->info[] = $x509Data;

						$sc->SubjectConfirmationData->info[] = $keyInfo;
					} else throw new SimpleSAML_Error_Exception('Error creating HoK assertion: No valid client certificate provided during TLS handshake with IdP');
				} else throw new SimpleSAML_Error_Exception('Error creating HoK assertion: No client certificate provided during TLS handshake with IdP');
			} else throw new SimpleSAML_Error_Exception('Error creating HoK assertion: No HTTPS connection to IdP, but required for Holder-of-Key SSO');
		} else {
			/* Bearer */
			$sc->Method = SAML2_Const::CM_BEARER;
		}
		$a->setSubjectConfirmation(array($sc));

		/* Add attributes. */

		if ($spMetadata->getBoolean('simplesaml.attributes', TRUE)) {
			$attributeNameFormat = self::getAttributeNameFormat($idpMetadata, $spMetadata);
			$a->setAttributeNameFormat($attributeNameFormat);
			$attributes = self::encodeAttributes($idpMetadata, $spMetadata, $state['Attributes']);
			$a->setAttributes($attributes);
		}


		/* Generate the NameID for the assertion. */

		if (isset($state['saml:NameIDFormat'])) {
			$nameIdFormat = $state['saml:NameIDFormat'];
		} else {
			$nameIdFormat = NULL;
		}

		if ($nameIdFormat === NULL || !isset($state['saml:NameID'][$nameIdFormat])) {
			/* Either not set in request, or not set to a format we supply. Fall back to old generation method. */
			$nameIdFormat = $spMetadata->getString('NameIDFormat', NULL);
			if ($nameIdFormat === NULL) {
				$nameIdFormat = $idpMetadata->getString('NameIDFormat', SAML2_Const::NAMEID_TRANSIENT);
			}
		}

		if (isset($state['saml:NameID'][$nameIdFormat])) {
			$nameId = $state['saml:NameID'][$nameIdFormat];
			$nameId['Format'] = $nameIdFormat;
		} else {
			$spNameQualifier = $spMetadata->getString('SPNameQualifier', NULL);
			if ($spNameQualifier === NULL) {
				$spNameQualifier = $spMetadata->getString('entityid');
			}

			if ($nameIdFormat === SAML2_Const::NAMEID_TRANSIENT) {
				/* generate a random id */
				$nameIdValue = SimpleSAML_Utilities::generateID();
			} else {
				/* this code will end up generating either a fixed assigned id (via nameid.attribute)
				   or random id if not assigned/configured */
				$nameIdValue = self::generateNameIdValue($idpMetadata, $spMetadata, $state);
				if ($nameIdValue === NULL) {
					SimpleSAML_Logger::warning('Falling back to transient NameID.');
					$nameIdFormat = SAML2_Const::NAMEID_TRANSIENT;
					$nameIdValue = SimpleSAML_Utilities::generateID();
				}
			}

			$nameId = array(
				'Format' => $nameIdFormat,
				'Value' => $nameIdValue,
				'SPNameQualifier' => $spNameQualifier,
			);
		}

		$state['saml:idp:NameID'] = $nameId;

		$a->setNameId($nameId);

		$encryptNameId = $spMetadata->getBoolean('nameid.encryption', NULL);
		if ($encryptNameId === NULL) {
			$encryptNameId = $idpMetadata->getBoolean('nameid.encryption', FALSE);
		}
		if ($encryptNameId) {
			$a->encryptNameId(sspmod_saml_Message::getEncryptionKey($spMetadata));
		}

		return $a;
	}


	/**
	 * Encrypt an assertion.
	 *
	 * This function takes in a SAML2_Assertion and encrypts it if encryption of
	 * assertions are enabled in the metadata.
	 *
	 * @param SimpleSAML_Configuration $idpMetadata  The metadata of the IdP.
	 * @param SimpleSAML_Configuration $spMetadata  The metadata of the SP.
	 * @param SAML2_Assertion $assertion  The assertion we are encrypting.
	 * @return SAML2_Assertion|SAML2_EncryptedAssertion  The assertion.
	 */
	private static function encryptAssertion(SimpleSAML_Configuration $idpMetadata,
		SimpleSAML_Configuration $spMetadata, SAML2_Assertion $assertion) {

		$encryptAssertion = $spMetadata->getBoolean('assertion.encryption', NULL);
		if ($encryptAssertion === NULL) {
			$encryptAssertion = $idpMetadata->getBoolean('assertion.encryption', FALSE);
		}
		if (!$encryptAssertion) {
			/* We are _not_ encrypting this assertion, and are therefore done. */
			return $assertion;
		}


		$sharedKey = $spMetadata->getString('sharedkey', NULL);
		if ($sharedKey !== NULL) {
			$key = new XMLSecurityKey(XMLSecurityKey::AES128_CBC);
			$key->loadKey($sharedKey);
		} else {
			$keys = $spMetadata->getPublicKeys('encryption', TRUE);
			$key = $keys[0];
			switch ($key['type']) {
			case 'X509Certificate':
				$pemKey = "-----BEGIN CERTIFICATE-----\n" .
					chunk_split($key['X509Certificate'], 64) .
					"-----END CERTIFICATE-----\n";
				break;
			default:
				throw new SimpleSAML_Error_Exception('Unsupported encryption key type: ' . $key['type']);
			}

			/* Extract the public key from the certificate for encryption. */
			$key = new XMLSecurityKey(XMLSecurityKey::RSA_OAEP_MGF1P, array('type'=>'public'));
			$key->loadKey($pemKey);
		}

		$ea = new SAML2_EncryptedAssertion();
		$ea->setAssertion($assertion, $key);
		return $ea;
	}


	/**
	 * Build a logout request based on information in the metadata.
	 *
	 * @param SimpleSAML_Configuration idpMetadata  The metadata of the IdP.
	 * @param SimpleSAML_Configuration spMetadata  The metadata of the SP.
	 * @param array $association  The SP association.
	 * @param string|NULL $relayState  An id that should be carried across the logout.
	 */
	private static function buildLogoutRequest(SimpleSAML_Configuration $idpMetadata,
		SimpleSAML_Configuration $spMetadata, array $association, $relayState) {

		$lr = sspmod_saml_Message::buildLogoutRequest($idpMetadata, $spMetadata);
		$lr->setRelayState($relayState);
		$lr->setSessionIndex($association['saml:SessionIndex']);
		$lr->setNameId($association['saml:NameID']);

		$assertionLifetime = $spMetadata->getInteger('assertion.lifetime', NULL);
		if ($assertionLifetime === NULL) {
			$assertionLifetime = $idpMetadata->getInteger('assertion.lifetime', 300);
		}
		$lr->setNotOnOrAfter(time() + $assertionLifetime);

		$encryptNameId = $spMetadata->getBoolean('nameid.encryption', NULL);
		if ($encryptNameId === NULL) {
			$encryptNameId = $idpMetadata->getBoolean('nameid.encryption', FALSE);
		}
		if ($encryptNameId) {
			$lr->encryptNameId(sspmod_saml_Message::getEncryptionKey($spMetadata));
		}

		return $lr;
	}


	/**
	 * Build a authentication response based on information in the metadata.
	 *
	 * @param SimpleSAML_Configuration $idpMetadata  The metadata of the IdP.
	 * @param SimpleSAML_Configuration $spMetadata  The metadata of the SP.
	 * @param string $consumerURL  The Destination URL of the response.
	 */
	private static function buildResponse(SimpleSAML_Configuration $idpMetadata,
		SimpleSAML_Configuration $spMetadata, $consumerURL) {

		$signResponse = $spMetadata->getBoolean('saml20.sign.response', NULL);
		if ($signResponse === NULL) {
			$signResponse = $idpMetadata->getBoolean('saml20.sign.response', TRUE);
		}

		$r = new SAML2_Response();

		$r->setIssuer($idpMetadata->getString('entityid'));
		$r->setDestination($consumerURL);

		if ($signResponse) {
			sspmod_saml_Message::addSign($idpMetadata, $spMetadata, $r);
		}

		return $r;
	}

}