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 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552
|
<?php
declare(strict_types=1);
namespace SimpleSAML\Metadata;
use DOMDocument;
use DOMElement;
use RobRichards\XMLSecLibs\XMLSecurityDSig;
use RobRichards\XMLSecLibs\XMLSecurityKey;
use SAML2\Constants;
use SAML2\DOMDocumentFactory;
use SAML2\SignedElementHelper;
use SAML2\XML\Chunk;
use SAML2\XML\ds\X509Certificate;
use SAML2\XML\ds\X509Data;
use SAML2\XML\md\AttributeAuthorityDescriptor;
use SAML2\XML\md\AttributeConsumingService;
use SAML2\XML\md\ContactPerson;
use SAML2\XML\md\EndpointType;
use SAML2\XML\md\EntityDescriptor;
use SAML2\XML\md\EntitiesDescriptor;
use SAML2\XML\md\IDPSSODescriptor;
use SAML2\XML\md\IndexedEndpointType;
use SAML2\XML\md\KeyDescriptor;
use SAML2\XML\md\Organization;
use SAML2\XML\md\RoleDescriptor;
use SAML2\XML\md\SPSSODescriptor;
use SAML2\XML\md\SSODescriptorType;
use SAML2\XML\mdattr\EntityAttributes;
use SAML2\XML\mdrpi\RegistrationInfo;
use SAML2\XML\mdui\DiscoHints;
use SAML2\XML\mdui\Keywords;
use SAML2\XML\mdui\Logo;
use SAML2\XML\mdui\UIInfo;
use SAML2\XML\saml\Attribute;
use SAML2\XML\shibmd\Scope;
use SimpleSAML\Logger;
use SimpleSAML\Utils;
/**
* This is class for parsing of SAML 1.x and SAML 2.0 metadata.
*
* Metadata is loaded by calling the static methods parseFile, parseString or parseElement.
* These functions returns an instance of SAMLParser. To get metadata
* from this object, use the methods getMetadata1xSP or getMetadata20SP.
*
* To parse a file which can contain a collection of EntityDescriptor or EntitiesDescriptor elements, use the
* parseDescriptorsFile, parseDescriptorsString or parseDescriptorsElement methods. These functions will return
* an array of SAMLParser elements where each element represents an EntityDescriptor-element.
*/
class SAMLParser
{
/**
* This is the list of SAML 1.x protocols.
*
* @var string[]
*/
private static $SAML1xProtocols = [
'urn:oasis:names:tc:SAML:1.0:protocol',
'urn:oasis:names:tc:SAML:1.1:protocol',
];
/**
* This is the list with the SAML 2.0 protocol.
*
* @var string[]
*/
private static $SAML20Protocols = [
Constants::NS_SAMLP,
];
/**
* This is the entity id we find in the metadata.
*
* @var string
*/
private $entityId;
/**
* This is an array with the processed SPSSODescriptor elements we have found in this
* metadata file.
* Each element in the array is an associative array with the elements from parseSSODescriptor and:
* - 'AssertionConsumerService': Array with the SP's assertion consumer services.
* Each assertion consumer service is stored as an associative array with the
* elements that parseGenericEndpoint returns.
*
* @var array[]
*/
private $spDescriptors;
/**
* This is an array with the processed IDPSSODescriptor elements we have found.
* Each element in the array is an associative array with the elements from parseSSODescriptor and:
* - 'SingleSignOnService': Array with the IdP's single sign on service endpoints. Each endpoint is stored
* as an associative array with the elements that parseGenericEndpoint returns.
*
* @var array[]
*/
private $idpDescriptors;
/**
* List of attribute authorities we have found.
*
* @var array
*/
private $attributeAuthorityDescriptors = [];
/**
* This is an associative array with the organization name for this entity. The key of
* the associative array is the language code, while the value is a string with the
* organization name.
*
* @var string[]
*/
private $organizationName = [];
/**
* This is an associative array with the organization display name for this entity. The key of
* the associative array is the language code, while the value is a string with the
* organization display name.
*
* @var string[]
*/
private $organizationDisplayName = [];
/**
* This is an associative array with the organization URI for this entity. The key of
* the associative array is the language code, while the value is the URI.
*
* @var string[]
*/
private $organizationURL = [];
/**
* This is an array of the Contact Persons of this entity.
*
* @var array[]
*/
private $contacts = [];
/**
* @var array
*/
private $scopes;
/**
* @var array
*/
private $entityAttributes;
/**
* An associative array of attributes from the RegistrationInfo element.
* @var array
*/
private $registrationInfo;
/**
* @var array
*/
private $tags;
/**
* This is an array of elements that may be used to validate this element.
*
* @var \SAML2\SignedElementHelper[]
*/
private $validators = [];
/**
* The original EntityDescriptor element for this entity, as a base64 encoded string.
*
* @var string
*/
private $entityDescriptor;
/**
* This is the constructor for the SAMLParser class.
*
* @param \SAML2\XML\md\EntityDescriptor $entityElement The EntityDescriptor.
* @param int|null $maxExpireTime The unix timestamp for when this entity should expire, or
* NULL if unknown.
* @param array $validators An array of parent elements that may validate this element.
* @param array $parentExtensions An optional array of extensions from the parent element.
*/
private function __construct(
EntityDescriptor $entityElement,
int $maxExpireTime = null,
array $validators = [],
array $parentExtensions = []
) {
$this->spDescriptors = [];
$this->idpDescriptors = [];
$e = $entityElement->toXML();
$e = $e->ownerDocument->saveXML($e);
$this->entityDescriptor = base64_encode($e);
$this->entityId = $entityElement->getEntityID();
$expireTime = self::getExpireTime($entityElement, $maxExpireTime);
$this->validators = $validators;
$this->validators[] = $entityElement;
// process Extensions element, if it exists
$ext = self::processExtensions($entityElement, $parentExtensions);
$this->scopes = $ext['scope'];
$this->tags = $ext['tags'];
$this->entityAttributes = $ext['EntityAttributes'];
$this->registrationInfo = $ext['RegistrationInfo'];
// look over the RoleDescriptors
foreach ($entityElement->getRoleDescriptor() as $child) {
if ($child instanceof SPSSODescriptor) {
$this->processSPSSODescriptor($child, $expireTime);
} elseif ($child instanceof IDPSSODescriptor) {
$this->processIDPSSODescriptor($child, $expireTime);
} elseif ($child instanceof AttributeAuthorityDescriptor) {
$this->processAttributeAuthorityDescriptor($child, $expireTime);
}
}
$organization = $entityElement->getOrganization();
if ($organization !== null) {
$this->processOrganization($organization);
}
if ($entityElement->getContactPerson() !== []) {
foreach ($entityElement->getContactPerson() as $contact) {
$this->processContactPerson($contact);
}
}
}
/**
* This function parses a file which contains XML encoded metadata.
*
* @param string $file The path to the file which contains the metadata.
*
* @return SAMLParser An instance of this class with the metadata loaded.
* @throws \Exception If the file does not parse as XML.
*/
public static function parseFile($file)
{
/** @var string $data */
$data = Utils\HTTP::fetch($file);
try {
$doc = DOMDocumentFactory::fromString($data);
} catch (\Exception $e) {
throw new \Exception('Failed to read XML from file: ' . $file);
}
return self::parseDocument($doc);
}
/**
* This function parses a string which contains XML encoded metadata.
*
* @param string $metadata A string which contains XML encoded metadata.
*
* @return SAMLParser An instance of this class with the metadata loaded.
* @throws \Exception If the string does not parse as XML.
*/
public static function parseString($metadata)
{
try {
$doc = DOMDocumentFactory::fromString($metadata);
} catch (\Exception $e) {
throw new \Exception('Failed to parse XML string.');
}
return self::parseDocument($doc);
}
/**
* This function parses a \DOMDocument which is assumed to contain a single EntityDescriptor element.
*
* @param \DOMDocument $document The \DOMDocument which contains the EntityDescriptor element.
*
* @return SAMLParser An instance of this class with the metadata loaded.
*/
public static function parseDocument($document)
{
assert($document instanceof DOMDocument);
$entityElement = self::findEntityDescriptor($document);
return self::parseElement($entityElement);
}
/**
* This function parses a \SAML2\XML\md\EntityDescriptor object which represents a EntityDescriptor element.
*
* @param \SAML2\XML\md\EntityDescriptor $entityElement A \SAML2\XML\md\EntityDescriptor object which represents a
* EntityDescriptor element.
*
* @return SAMLParser An instance of this class with the metadata loaded.
*/
public static function parseElement($entityElement)
{
assert($entityElement instanceof EntityDescriptor);
return new SAMLParser($entityElement, null, []);
}
/**
* This function parses a file where the root node is either an EntityDescriptor element or an
* EntitiesDescriptor element. In both cases it will return an associative array of SAMLParser instances. If
* the file contains a single EntityDescriptorElement, then the array will contain a single SAMLParser
* instance.
*
* @param string|null $file The path to the file which contains the EntityDescriptor or EntitiesDescriptor element.
*
* @return SAMLParser[] An array of SAMLParser instances.
* @throws \Exception If the file does not parse as XML.
*/
public static function parseDescriptorsFile($file)
{
if ($file === null) {
throw new \Exception('Cannot open file NULL. File name not specified.');
}
/** @var string $data */
$data = Utils\HTTP::fetch($file);
try {
$doc = DOMDocumentFactory::fromString($data);
} catch (\Exception $e) {
throw new \Exception('Failed to read XML from file: ' . $file);
}
return self::parseDescriptorsElement($doc->documentElement);
}
/**
* This function parses a string with XML data. The root node of the XML data is expected to be either an
* EntityDescriptor element or an EntitiesDescriptor element. It will return an associative array of
* SAMLParser instances.
*
* @param string $string The string with XML data.
*
* @return SAMLParser[] An associative array of SAMLParser instances. The key of the array will
* be the entity id.
* @throws \Exception If the string does not parse as XML.
*/
public static function parseDescriptorsString($string)
{
try {
$doc = DOMDocumentFactory::fromString($string);
} catch (\Exception $e) {
throw new \Exception('Failed to parse XML string.');
}
return self::parseDescriptorsElement($doc->documentElement);
}
/**
* This function parses a DOMElement which represents either an EntityDescriptor element or an
* EntitiesDescriptor element. It will return an associative array of SAMLParser instances in both cases.
*
* @param \DOMElement|NULL $element The DOMElement which contains the EntityDescriptor element or the
* EntitiesDescriptor element.
*
* @return SAMLParser[] An associative array of SAMLParser instances. The key of the array will
* be the entity id.
* @throws \Exception if the document is empty or the root is an unexpected node.
*/
public static function parseDescriptorsElement(DOMElement $element = null)
{
if ($element === null) {
throw new \Exception('Document was empty.');
}
if (Utils\XML::isDOMNodeOfType($element, 'EntityDescriptor', '@md') === true) {
return self::processDescriptorsElement(new EntityDescriptor($element));
} elseif (Utils\XML::isDOMNodeOfType($element, 'EntitiesDescriptor', '@md') === true) {
return self::processDescriptorsElement(new EntitiesDescriptor($element));
} else {
throw new \Exception('Unexpected root node: [' . $element->namespaceURI . ']:' . $element->localName);
}
}
/**
*
* @param \SAML2\XML\md\EntityDescriptor|\SAML2\XML\md\EntitiesDescriptor $element The element we should process.
* @param int|NULL $maxExpireTime The maximum expiration time of the entities.
* @param array $validators The parent-elements that may be signed.
* @param array $parentExtensions An optional array of extensions from the parent element.
*
* @return SAMLParser[] Array of SAMLParser instances.
*/
private static function processDescriptorsElement(
SignedElementHelper $element,
int $maxExpireTime = null,
array $validators = [],
array $parentExtensions = []
): array {
if ($element instanceof EntityDescriptor) {
$ret = new SAMLParser($element, $maxExpireTime, $validators, $parentExtensions);
$ret = [$ret->getEntityId() => $ret];
/** @var SAMLParser[] $ret */
return $ret;
}
assert($element instanceof EntitiesDescriptor);
$extensions = self::processExtensions($element, $parentExtensions);
$expTime = self::getExpireTime($element, $maxExpireTime);
$validators[] = $element;
$ret = [];
foreach ($element->getChildren() as $child) {
$ret += self::processDescriptorsElement($child, $expTime, $validators, $extensions);
}
return $ret;
}
/**
* Determine how long a given element can be cached.
*
* This function looks for the 'validUntil' attribute to determine
* how long a given XML-element is valid. It returns this as a unix timestamp.
*
* @param mixed $element The element we should determine the expiry time of.
* @param int|null $maxExpireTime The maximum expiration time.
*
* @return int|null The unix timestamp for when the element should expire. Will be NULL if no
* limit is set for the element.
*/
private static function getExpireTime($element, int $maxExpireTime = null): ?int
{
// validUntil may be null
$expire = $element->getValidUntil();
if ($maxExpireTime !== null && ($expire === null || $maxExpireTime < $expire)) {
$expire = $maxExpireTime;
}
return $expire;
}
/**
* This function returns the entity id of this parsed entity.
*
* @return string The entity id of this parsed entity.
*/
public function getEntityId()
{
return $this->entityId;
}
/**
* @return array
*/
private function getMetadataCommon(): array
{
$ret = [];
$ret['entityid'] = $this->entityId;
$ret['entityDescriptor'] = $this->entityDescriptor;
// add organizational metadata
if (!empty($this->organizationName)) {
$ret['description'] = $this->organizationName;
$ret['OrganizationName'] = $this->organizationName;
}
if (!empty($this->organizationDisplayName)) {
$ret['name'] = $this->organizationDisplayName;
$ret['OrganizationDisplayName'] = $this->organizationDisplayName;
}
if (!empty($this->organizationURL)) {
$ret['url'] = $this->organizationURL;
$ret['OrganizationURL'] = $this->organizationURL;
}
//add contact metadata
$ret['contacts'] = $this->contacts;
return $ret;
}
/**
* Add data parsed from extensions to metadata.
*
* @param array &$metadata The metadata that should be updated.
* @param array $roleDescriptor The parsed role descriptor.
* @return void
*/
private function addExtensions(array &$metadata, array $roleDescriptor): void
{
assert(array_key_exists('scope', $roleDescriptor));
assert(array_key_exists('tags', $roleDescriptor));
$scopes = array_merge($this->scopes, array_diff($roleDescriptor['scope'], $this->scopes));
if (!empty($scopes)) {
$metadata['scope'] = $scopes;
}
$tags = array_merge($this->tags, array_diff($roleDescriptor['tags'], $this->tags));
if (!empty($tags)) {
$metadata['tags'] = $tags;
}
if (!empty($this->registrationInfo)) {
$metadata['RegistrationInfo'] = $this->registrationInfo;
}
if (!empty($this->entityAttributes)) {
$metadata['EntityAttributes'] = $this->entityAttributes;
// check for entity categories
if (Utils\Config\Metadata::isHiddenFromDiscovery($metadata)) {
$metadata['hide.from.discovery'] = true;
}
}
if (!empty($roleDescriptor['UIInfo'])) {
$metadata['UIInfo'] = $roleDescriptor['UIInfo'];
}
if (!empty($roleDescriptor['DiscoHints'])) {
$metadata['DiscoHints'] = $roleDescriptor['DiscoHints'];
}
}
/**
* This function returns the metadata for SAML 1.x SPs in the format SimpleSAMLphp expects.
* This is an associative array with the following fields:
* - 'entityid': The entity id of the entity described in the metadata.
* - 'AssertionConsumerService': String with the URL of the assertion consumer service which supports
* the browser-post binding.
* - 'certData': X509Certificate for entity (if present).
*
* Metadata must be loaded with one of the parse functions before this function can be called.
*
* @return array|null An associative array with metadata or NULL if we are unable to
* generate metadata for a SAML 1.x SP.
*/
public function getMetadata1xSP()
{
$ret = $this->getMetadataCommon();
$ret['metadata-set'] = 'shib13-sp-remote';
// find SP information which supports one of the SAML 1.x protocols
$spd = $this->getSPDescriptors(self::$SAML1xProtocols);
if (count($spd) === 0) {
return null;
}
// we currently only look at the first SPDescriptor which supports SAML 1.x
$spd = $spd[0];
// add expire time to metadata
if (array_key_exists('expire', $spd)) {
$ret['expire'] = $spd['expire'];
}
// find the assertion consumer service endpoints
$ret['AssertionConsumerService'] = $spd['AssertionConsumerService'];
// add the list of attributes the SP should receive
if (array_key_exists('attributes', $spd)) {
$ret['attributes'] = $spd['attributes'];
}
if (array_key_exists('attributes.required', $spd)) {
$ret['attributes.required'] = $spd['attributes.required'];
}
if (array_key_exists('attributes.NameFormat', $spd)) {
$ret['attributes.NameFormat'] = $spd['attributes.NameFormat'];
}
// add name & description
if (array_key_exists('name', $spd)) {
$ret['name'] = $spd['name'];
}
if (array_key_exists('description', $spd)) {
$ret['description'] = $spd['description'];
}
// add public keys
if (!empty($spd['keys'])) {
$ret['keys'] = $spd['keys'];
}
// add extensions
$this->addExtensions($ret, $spd);
// prioritize mdui:DisplayName as the name if available
if (!empty($ret['UIInfo']['DisplayName'])) {
$ret['name'] = $ret['UIInfo']['DisplayName'];
}
return $ret;
}
/**
* This function returns the metadata for SAML 1.x IdPs in the format SimpleSAMLphp expects.
* This is an associative array with the following fields:
* - 'entityid': The entity id of the entity described in the metadata.
* - 'name': Auto generated name for this entity. Currently set to the entity id.
* - 'SingleSignOnService': String with the URL of the SSO service which supports the redirect binding.
* - 'SingleLogoutService': String with the URL where we should send logout requests/responses.
* - 'certData': X509Certificate for entity (if present).
* - 'certFingerprint': Fingerprint of the X509Certificate from the metadata. (deprecated)
*
* Metadata must be loaded with one of the parse functions before this function can be called.
*
* @return array|null An associative array with metadata or NULL if we are unable to
* generate metadata for a SAML 1.x IdP.
*/
public function getMetadata1xIdP()
{
$ret = $this->getMetadataCommon();
$ret['metadata-set'] = 'shib13-idp-remote';
// find IdP information which supports the SAML 1.x protocol
$idp = $this->getIdPDescriptors(self::$SAML1xProtocols);
if (count($idp) === 0) {
return null;
}
// we currently only look at the first IDP descriptor which supports SAML 1.x
$idp = $idp[0];
// fdd expire time to metadata
if (array_key_exists('expire', $idp)) {
$ret['expire'] = $idp['expire'];
}
// find the SSO service endpoints
$ret['SingleSignOnService'] = $idp['SingleSignOnService'];
// find the ArtifactResolutionService endpoint
$ret['ArtifactResolutionService'] = $idp['ArtifactResolutionService'];
// add public keys
if (!empty($idp['keys'])) {
$ret['keys'] = $idp['keys'];
}
// add extensions
$this->addExtensions($ret, $idp);
// prioritize mdui:DisplayName as the name if available
if (!empty($ret['UIInfo']['DisplayName'])) {
$ret['name'] = $ret['UIInfo']['DisplayName'];
}
return $ret;
}
/**
* This function returns the metadata for SAML 2.0 SPs in the format SimpleSAMLphp expects.
* This is an associative array with the following fields:
* - 'entityid': The entity id of the entity described in the metadata.
* - 'AssertionConsumerService': String with the URL of the assertion consumer service which supports
* the browser-post binding.
* - 'SingleLogoutService': String with the URL where we should send logout requests/responses.
* - 'NameIDFormat': The name ID format this SP expects. This may be unset.
* - 'certData': X509Certificate for entity (if present).
*
* Metadata must be loaded with one of the parse functions before this function can be called.
*
* @return array|null An associative array with metadata or NULL if we are unable to
* generate metadata for a SAML 2.x SP.
*/
public function getMetadata20SP()
{
$ret = $this->getMetadataCommon();
$ret['metadata-set'] = 'saml20-sp-remote';
// find SP information which supports the SAML 2.0 protocol
$spd = $this->getSPDescriptors(self::$SAML20Protocols);
if (count($spd) === 0) {
return null;
}
// we currently only look at the first SPDescriptor which supports SAML 2.0
$spd = $spd[0];
// add expire time to metadata
if (array_key_exists('expire', $spd)) {
$ret['expire'] = $spd['expire'];
}
// find the assertion consumer service endpoints
$ret['AssertionConsumerService'] = $spd['AssertionConsumerService'];
// find the single logout service endpoint
$ret['SingleLogoutService'] = $spd['SingleLogoutService'];
// find the NameIDFormat. This may not exist
if (count($spd['nameIDFormats']) > 0) {
// SimpleSAMLphp currently only supports a single NameIDFormat per SP. We use the first one
$ret['NameIDFormat'] = $spd['nameIDFormats'][0];
}
// add the list of attributes the SP should receive
if (array_key_exists('attributes', $spd)) {
$ret['attributes'] = $spd['attributes'];
}
if (array_key_exists('attributes.required', $spd)) {
$ret['attributes.required'] = $spd['attributes.required'];
}
if (array_key_exists('attributes.NameFormat', $spd)) {
$ret['attributes.NameFormat'] = $spd['attributes.NameFormat'];
}
if (array_key_exists('attributes.index', $spd)) {
$ret['attributes.index'] = $spd['attributes.index'];
}
if (array_key_exists('attributes.isDefault', $spd)) {
$ret['attributes.isDefault'] = $spd['attributes.isDefault'];
}
// add name & description
if (array_key_exists('name', $spd)) {
$ret['name'] = $spd['name'];
}
if (array_key_exists('description', $spd)) {
$ret['description'] = $spd['description'];
}
// add public keys
if (!empty($spd['keys'])) {
$ret['keys'] = $spd['keys'];
}
// add validate.authnrequest
if (array_key_exists('AuthnRequestsSigned', $spd)) {
$ret['validate.authnrequest'] = $spd['AuthnRequestsSigned'];
}
// add saml20.sign.assertion
if (array_key_exists('WantAssertionsSigned', $spd)) {
$ret['saml20.sign.assertion'] = $spd['WantAssertionsSigned'];
}
// add extensions
$this->addExtensions($ret, $spd);
// prioritize mdui:DisplayName as the name if available
if (!empty($ret['UIInfo']['DisplayName'])) {
$ret['name'] = $ret['UIInfo']['DisplayName'];
}
return $ret;
}
/**
* This function returns the metadata for SAML 2.0 IdPs in the format SimpleSAMLphp expects.
* This is an associative array with the following fields:
* - 'entityid': The entity id of the entity described in the metadata.
* - 'name': Auto generated name for this entity. Currently set to the entity id.
* - 'SingleSignOnService': String with the URL of the SSO service which supports the redirect binding.
* - 'SingleLogoutService': String with the URL where we should send logout requests(/responses).
* - 'SingleLogoutServiceResponse': String where we should send logout responses (if this is different from
* the 'SingleLogoutService' endpoint.
* - 'NameIDFormats': The name ID formats this IdP supports.
* - 'certData': X509Certificate for entity (if present).
* - 'certFingerprint': Fingerprint of the X509Certificate from the metadata. (deprecated)
*
* Metadata must be loaded with one of the parse functions before this function can be called.
*
* @return array|null An associative array with metadata or NULL if we are unable to
* generate metadata for a SAML 2.0 IdP.
*/
public function getMetadata20IdP()
{
$ret = $this->getMetadataCommon();
$ret['metadata-set'] = 'saml20-idp-remote';
// find IdP information which supports the SAML 2.0 protocol
$idp = $this->getIdPDescriptors(self::$SAML20Protocols);
if (count($idp) === 0) {
return null;
}
// we currently only look at the first IDP descriptor which supports SAML 2.0
$idp = $idp[0];
// add expire time to metadata
if (array_key_exists('expire', $idp)) {
$ret['expire'] = $idp['expire'];
}
// enable redirect.sign if WantAuthnRequestsSigned is enabled
if ($idp['WantAuthnRequestsSigned']) {
$ret['sign.authnrequest'] = true;
}
// find the SSO service endpoint
$ret['SingleSignOnService'] = $idp['SingleSignOnService'];
// find the single logout service endpoint
$ret['SingleLogoutService'] = $idp['SingleLogoutService'];
// find the ArtifactResolutionService endpoint
$ret['ArtifactResolutionService'] = $idp['ArtifactResolutionService'];
// add supported nameIDFormats
$ret['NameIDFormats'] = $idp['nameIDFormats'];
// add public keys
if (!empty($idp['keys'])) {
$ret['keys'] = $idp['keys'];
}
// add extensions
$this->addExtensions($ret, $idp);
// prioritize mdui:DisplayName as the name if available
if (!empty($ret['UIInfo']['DisplayName'])) {
$ret['name'] = $ret['UIInfo']['DisplayName'];
}
return $ret;
}
/**
* Retrieve AttributeAuthorities from the metadata.
*
* @return array Array of AttributeAuthorityDescriptor entries.
*/
public function getAttributeAuthorities()
{
return $this->attributeAuthorityDescriptors;
}
/**
* Parse a RoleDescriptorType element.
*
* The returned associative array has the following elements:
* - 'protocols': Array with the protocols supported.
* - 'expire': Timestamp for when this descriptor expires.
* - 'keys': Array of associative arrays with the elements from parseKeyDescriptor.
*
* @param \SAML2\XML\md\RoleDescriptor $element The element we should extract metadata from.
* @param int|null $expireTime The unix timestamp for when this element should expire, or
* NULL if unknown.
*
* @return array An associative array with metadata we have extracted from this element.
*/
private static function parseRoleDescriptorType(RoleDescriptor $element, int $expireTime = null): array
{
$ret = [];
$expireTime = self::getExpireTime($element, $expireTime);
if ($expireTime !== null) {
// we got an expired timestamp, either from this element or one of the parent elements
$ret['expire'] = $expireTime;
}
$ret['protocols'] = $element->getProtocolSupportEnumeration();
// process KeyDescriptor elements
$ret['keys'] = [];
foreach ($element->getKeyDescriptor() as $kd) {
$key = self::parseKeyDescriptor($kd);
if ($key !== null) {
$ret['keys'][] = $key;
}
}
$ext = self::processExtensions($element);
$ret['scope'] = $ext['scope'];
$ret['tags'] = $ext['tags'];
$ret['EntityAttributes'] = $ext['EntityAttributes'];
$ret['UIInfo'] = $ext['UIInfo'];
$ret['DiscoHints'] = $ext['DiscoHints'];
return $ret;
}
/**
* This function extracts metadata from a SSODescriptor element.
*
* The returned associative array has the following elements:
* - 'protocols': Array with the protocols this SSODescriptor supports.
* - 'SingleLogoutService': Array with the single logout service endpoints. Each endpoint is stored
* as an associative array with the elements that parseGenericEndpoint returns.
* - 'nameIDFormats': The NameIDFormats supported by this SSODescriptor. This may be an empty array.
* - 'keys': Array of associative arrays with the elements from parseKeyDescriptor:
*
* @param \SAML2\XML\md\SSODescriptorType $element The element we should extract metadata from.
* @param int|NULL $expireTime The unix timestamp for when this element should expire, or
* NULL if unknown.
*
* @return array An associative array with metadata we have extracted from this element.
*/
private static function parseSSODescriptor(SSODescriptorType $element, int $expireTime = null): array
{
$sd = self::parseRoleDescriptorType($element, $expireTime);
// find all SingleLogoutService elements
$sd['SingleLogoutService'] = self::extractEndpoints($element->getSingleLogoutService());
// find all ArtifactResolutionService elements
$sd['ArtifactResolutionService'] = self::extractEndpoints($element->getArtifactResolutionService());
// process NameIDFormat elements
$sd['nameIDFormats'] = $element->getNameIDFormat();
return $sd;
}
/**
* This function extracts metadata from a SPSSODescriptor element.
*
* @param \SAML2\XML\md\SPSSODescriptor $element The element which should be parsed.
* @param int|NULL $expireTime The unix timestamp for when this element should expire, or
* NULL if unknown.
* @return void
*/
private function processSPSSODescriptor(SPSSODescriptor $element, int $expireTime = null): void
{
$sp = self::parseSSODescriptor($element, $expireTime);
// find all AssertionConsumerService elements
$sp['AssertionConsumerService'] = self::extractEndpoints($element->getAssertionConsumerService());
// find all the attributes and SP name...
$attcs = $element->getAttributeConsumingService();
if (count($attcs) > 0) {
self::parseAttributeConsumerService($attcs[0], $sp);
}
// check AuthnRequestsSigned
if ($element->getAuthnRequestsSigned() !== null) {
$sp['AuthnRequestsSigned'] = $element->getAuthnRequestsSigned();
}
// check WantAssertionsSigned
if ($element->wantAssertionsSigned() !== null) {
$sp['WantAssertionsSigned'] = $element->wantAssertionsSigned();
}
$this->spDescriptors[] = $sp;
}
/**
* This function extracts metadata from a IDPSSODescriptor element.
*
* @param \SAML2\XML\md\IDPSSODescriptor $element The element which should be parsed.
* @param int|NULL $expireTime The unix timestamp for when this element should expire, or
* NULL if unknown.
* @return void
*/
private function processIDPSSODescriptor(IDPSSODescriptor $element, int $expireTime = null): void
{
$idp = self::parseSSODescriptor($element, $expireTime);
// find all SingleSignOnService elements
$idp['SingleSignOnService'] = self::extractEndpoints($element->getSingleSignOnService());
if ($element->wantAuthnRequestsSigned()) {
$idp['WantAuthnRequestsSigned'] = true;
} else {
$idp['WantAuthnRequestsSigned'] = false;
}
$this->idpDescriptors[] = $idp;
}
/**
* This function extracts metadata from a AttributeAuthorityDescriptor element.
*
* @param \SAML2\XML\md\AttributeAuthorityDescriptor $element The element which should be parsed.
* @param int|NULL $expireTime The unix timestamp for when this element should
* expire, or NULL if unknown.
* @return void
*/
private function processAttributeAuthorityDescriptor(
AttributeAuthorityDescriptor $element,
$expireTime
): void {
assert($expireTime === null || is_int($expireTime));
$aad = self::parseRoleDescriptorType($element, $expireTime);
$aad['entityid'] = $this->getEntityId();
$aad['metadata-set'] = 'attributeauthority-remote';
$aad['AttributeService'] = self::extractEndpoints($element->getAttributeService());
$aad['AssertionIDRequestService'] = self::extractEndpoints($element->getAssertionIDRequestService());
$aad['NameIDFormat'] = $element->getNameIDFormat();
$this->attributeAuthorityDescriptors[] = $aad;
}
/**
* Parse an Extensions element. Extensions may appear in multiple elements and certain extension may get inherited
* from a parent element.
*
* @param mixed $element The element which contains the Extensions element.
* @param array $parentExtensions An optional array of extensions from the parent element.
*
* @return array An associative array with the extensions parsed.
*/
private static function processExtensions($element, array $parentExtensions = []): array
{
$ret = [
'scope' => [],
'tags' => [],
'EntityAttributes' => [],
'RegistrationInfo' => [],
'UIInfo' => [],
'DiscoHints' => [],
];
// Some extensions may get inherited from a parent element
if (
($element instanceof EntityDescriptor
|| $element instanceof EntitiesDescriptor)
&& !empty($parentExtensions['RegistrationInfo'])
) {
$ret['RegistrationInfo'] = $parentExtensions['RegistrationInfo'];
}
foreach ($element->getExtensions() as $e) {
if ($e instanceof Scope) {
$ret['scope'][] = $e->getScope();
continue;
}
// Entity Attributes are only allowed at entity level extensions and not at RoleDescriptor level
if (
$element instanceof EntityDescriptor
|| $element instanceof EntitiesDescriptor
) {
if ($e instanceof RegistrationInfo) {
// Registration Authority cannot be overridden (warn only if override attempts to change the value)
if (
isset($ret['RegistrationInfo']['registrationAuthority'])
&& $ret['RegistrationInfo']['registrationAuthority'] !== $e->getRegistrationAuthority()
) {
Logger::warning(
'Invalid attempt to override registrationAuthority \''
. $ret['RegistrationInfo']['registrationAuthority']
. "' with '{$e->getRegistrationAuthority()}'"
);
} else {
$ret['RegistrationInfo']['registrationAuthority'] = $e->getRegistrationAuthority();
}
}
if ($e instanceof EntityAttributes && !empty($e->getChildren())) {
foreach ($e->getChildren() as $attr) {
// only saml:Attribute are currently supported here. The specifications also allows
// saml:Assertions, which more complex processing
if ($attr instanceof Attribute) {
/** @psalm-var string|null $attrName Remove for SSP 2.0 */
$attrName = $attr->getName();
$attrNameFormat = $attr->getNameFormat();
$attrValue = $attr->getAttributeValue();
if ($attrName === null || $attrValue === []) {
continue;
}
// attribute names that is not URI is prefixed as this: '{nameformat}name'
$name = $attrName;
if ($attrNameFormat === null) {
$name = '{' . Constants::NAMEFORMAT_UNSPECIFIED . '}' . $attrName;
} elseif ($attrNameFormat !== Constants::NAMEFORMAT_URI) {
$name = '{' . $attrNameFormat . '}' . $attrName;
}
$values = [];
foreach ($attrValue as $attrval) {
$values[] = $attrval->getString();
}
$ret['EntityAttributes'][$name] = $values;
}
}
}
}
// UIInfo elements are only allowed at RoleDescriptor level extensions
if ($element instanceof RoleDescriptor) {
if ($e instanceof UIInfo) {
$ret['UIInfo']['DisplayName'] = $e->getDisplayName();
$ret['UIInfo']['Description'] = $e->getDescription();
$ret['UIInfo']['InformationURL'] = $e->getInformationURL();
$ret['UIInfo']['PrivacyStatementURL'] = $e->getPrivacyStatementURL();
foreach ($e->getKeywords() as $uiItem) {
$keywords = $uiItem->getKeywords();
/** @psalm-var string|null $language */
$language = $uiItem->getLanguage();
if (($keywords === []) || ($language === null)) {
continue;
}
$ret['UIInfo']['Keywords'][$language] = $keywords;
}
foreach ($e->getLogo() as $uiItem) {
/** @psalm-suppress TypeDoesNotContainNull Remove in SSP 2.0 */
if (
!($uiItem instanceof Logo)
|| ($uiItem->getUrl() === null)
|| ($uiItem->getHeight() === null)
|| ($uiItem->getWidth() === null)
) {
continue;
}
$logo = [
'url' => $uiItem->getUrl(),
'height' => $uiItem->getHeight(),
'width' => $uiItem->getWidth(),
];
if ($uiItem->getLanguage() !== null) {
$logo['lang'] = $uiItem->getLanguage();
}
$ret['UIInfo']['Logo'][] = $logo;
}
}
}
// DiscoHints elements are only allowed at IDPSSODescriptor level extensions
if ($element instanceof IDPSSODescriptor) {
if ($e instanceof DiscoHints) {
$ret['DiscoHints']['IPHint'] = $e->getIPHint();
$ret['DiscoHints']['DomainHint'] = $e->getDomainHint();
$ret['DiscoHints']['GeolocationHint'] = $e->getGeolocationHint();
}
}
if (!($e instanceof Chunk)) {
continue;
}
if ($e->getLocalName() === 'Attribute' && $e->getNamespaceURI() === Constants::NS_SAML) {
$attribute = $e->getXML();
$name = $attribute->getAttribute('Name');
$values = array_map(
'\SimpleSAML\Utils\XML::getDOMText',
Utils\XML::getDOMChildren($attribute, 'AttributeValue', '@saml2')
);
if ($name === 'tags') {
foreach ($values as $tagname) {
if (!empty($tagname)) {
$ret['tags'][] = $tagname;
}
}
}
}
}
return $ret;
}
/**
* Parse and process a Organization element.
*
* @param \SAML2\XML\md\Organization $element The Organization element.
* @return void
*/
private function processOrganization(Organization $element): void
{
$this->organizationName = $element->getOrganizationName();
$this->organizationDisplayName = $element->getOrganizationDisplayName();
$this->organizationURL = $element->getOrganizationURL();
}
/**
* Parse and process a ContactPerson element.
*
* @param \SAML2\XML\md\ContactPerson $element The ContactPerson element.
* @return void
*/
private function processContactPerson(ContactPerson $element): void
{
$contactPerson = [];
if ($element->getContactType() !== '') {
$contactPerson['contactType'] = $element->getContactType();
}
if ($element->getCompany() !== null) {
$contactPerson['company'] = $element->getCompany();
}
if ($element->getGivenName() !== null) {
$contactPerson['givenName'] = $element->getGivenName();
}
if ($element->getSurName() !== null) {
$contactPerson['surName'] = $element->getSurName();
}
if ($element->getEmailAddress() !== []) {
$contactPerson['emailAddress'] = $element->getEmailAddress();
}
if ($element->getTelephoneNumber() !== []) {
$contactPerson['telephoneNumber'] = $element->getTelephoneNumber();
}
if (!empty($contactPerson)) {
$this->contacts[] = $contactPerson;
}
}
/**
* This function parses AttributeConsumerService elements.
*
* @param \SAML2\XML\md\AttributeConsumingService $element The AttributeConsumingService to parse.
* @param array $sp The array with the SP's metadata.
* @return void
*/
private static function parseAttributeConsumerService(AttributeConsumingService $element, array &$sp): void
{
$sp['name'] = $element->getServiceName();
$sp['description'] = $element->getServiceDescription();
$format = null;
$sp['attributes'] = [];
$sp['attributes.required'] = [];
foreach ($element->getRequestedAttribute() as $child) {
$attrname = $child->getName();
$sp['attributes'][] = $attrname;
if ($child->getIsRequired() === true) {
$sp['attributes.required'][] = $attrname;
}
if ($child->getNameFormat() !== null) {
$attrformat = $child->getNameFormat();
} else {
$attrformat = Constants::NAMEFORMAT_UNSPECIFIED;
}
if ($format === null) {
$format = $attrformat;
} elseif ($format !== $attrformat) {
$format = Constants::NAMEFORMAT_UNSPECIFIED;
}
}
if (empty($sp['attributes'])) {
// a really invalid configuration: all AttributeConsumingServices should have one or more attributes
unset($sp['attributes']);
}
if (empty($sp['attributes.required'])) {
unset($sp['attributes.required']);
}
if ($format !== Constants::NAMEFORMAT_UNSPECIFIED && $format !== null) {
$sp['attributes.NameFormat'] = $format;
}
}
/**
* This function is a generic endpoint element parser.
*
* The returned associative array has the following elements:
* - 'Binding': The binding this endpoint uses.
* - 'Location': The URL to this endpoint.
* - 'ResponseLocation': The URL where responses should be sent. This may not exist.
* - 'index': The index of this endpoint. This attribute is only for indexed endpoints.
* - 'isDefault': Whether this endpoint is the default endpoint for this type. This attribute may not exist.
*
* @param \SAML2\XML\md\EndpointType $element The element which should be parsed.
*
* @return array An associative array with the data we have extracted from the element.
*/
private static function parseGenericEndpoint(EndpointType $element): array
{
$ep = [];
$ep['Binding'] = $element->getBinding();
$ep['Location'] = $element->getLocation();
if ($element->getResponseLocation() !== null) {
$ep['ResponseLocation'] = $element->getResponseLocation();
}
if ($element instanceof IndexedEndpointType) {
$ep['index'] = $element->getIndex();
if ($element->getIsDefault() !== null) {
$ep['isDefault'] = $element->getIsDefault();
}
}
return $ep;
}
/**
* Extract generic endpoints.
*
* @param array $endpoints The endpoints we should parse.
*
* @return array Array of parsed endpoints.
*/
private static function extractEndpoints(array $endpoints): array
{
return array_map('self::parseGenericEndpoint', $endpoints);
}
/**
* This function parses a KeyDescriptor element. It currently only supports keys with a single
* X509 certificate.
*
* The associative array for a key can contain:
* - 'encryption': Indicates whether this key can be used for encryption.
* - 'signing': Indicates whether this key can be used for signing.
* - 'type: The type of the key. 'X509Certificate' is the only key type we support.
* - 'X509Certificate': The contents of the first X509Certificate element (if the type is 'X509Certificate ').
*
* @param \SAML2\XML\md\KeyDescriptor $kd The KeyDescriptor element.
*
* @return array|null An associative array describing the key, or null if this is an unsupported key.
*/
private static function parseKeyDescriptor(KeyDescriptor $kd): ?array
{
$r = [];
if ($kd->getUse() === 'encryption') {
$r['encryption'] = true;
$r['signing'] = false;
} elseif ($kd->getUse() === 'signing') {
$r['encryption'] = false;
$r['signing'] = true;
} else {
$r['encryption'] = true;
$r['signing'] = true;
}
$keyInfo = $kd->getKeyInfo();
/** @psalm-suppress PossiblyNullReference This will be fixed in saml2 5.0 */
foreach ($keyInfo->getInfo() as $i) {
if ($i instanceof X509Data) {
foreach ($i->getData() as $d) {
if ($d instanceof X509Certificate) {
$r['type'] = 'X509Certificate';
$r['X509Certificate'] = $d->getCertificate();
return $r;
}
}
}
}
return null;
}
/**
* This function finds SP descriptors which supports one of the given protocols.
*
* @param array $protocols Array with the protocols we accept.
*
* @return array with SP descriptors which supports one of the given protocols.
*/
private function getSPDescriptors(array $protocols): array
{
$ret = [];
foreach ($this->spDescriptors as $spd) {
$sharedProtocols = array_intersect($protocols, $spd['protocols']);
if (count($sharedProtocols) > 0) {
$ret[] = $spd;
}
}
return $ret;
}
/**
* This function finds IdP descriptors which supports one of the given protocols.
*
* @param array $protocols Array with the protocols we accept.
*
* @return array with IdP descriptors which supports one of the given protocols.
*/
private function getIdPDescriptors(array $protocols): array
{
$ret = [];
foreach ($this->idpDescriptors as $idpd) {
$sharedProtocols = array_intersect($protocols, $idpd['protocols']);
if (count($sharedProtocols) > 0) {
$ret[] = $idpd;
}
}
return $ret;
}
/**
* This function locates the EntityDescriptor node in a DOMDocument. This node should
* be the first (and only) node in the document.
*
* This function will throw an exception if it is unable to locate the node.
*
* @param \DOMDocument $doc The \DOMDocument where we should find the EntityDescriptor node.
*
* @return \SAML2\XML\md\EntityDescriptor The \DOMEntity which represents the EntityDescriptor.
* @throws \Exception If the document is empty or the first element is not an EntityDescriptor element.
*/
private static function findEntityDescriptor(DOMDocument $doc): EntityDescriptor
{
// find the EntityDescriptor DOMElement. This should be the first (and only) child of the DOMDocument
$ed = $doc->documentElement;
if (Utils\XML::isDOMNodeOfType($ed, 'EntityDescriptor', '@md') === false) {
throw new \Exception('Expected first element in the metadata document to be an EntityDescriptor element.');
}
return new EntityDescriptor($ed);
}
/**
* If this EntityDescriptor was signed this function use the public key to check the signature.
*
* @param array $certificates One ore more certificates with the public key. This makes it possible
* to do a key rollover.
*
* @return boolean True if it is possible to check the signature with the certificate, false otherwise.
* @throws \Exception If the certificate file cannot be found.
*/
public function validateSignature($certificates)
{
foreach ($certificates as $cert) {
assert(is_string($cert));
$certFile = Utils\Config::getCertPath($cert);
if (!file_exists($certFile)) {
throw new \Exception(
'Could not find certificate file [' . $certFile . '], which is needed to validate signature'
);
}
$certData = file_get_contents($certFile);
foreach ($this->validators as $validator) {
$key = new XMLSecurityKey(XMLSecurityKey::RSA_SHA256, ['type' => 'public']);
$key->loadKey($certData);
try {
if ($validator->validate($key)) {
return true;
}
} catch (\Exception $e) {
// this certificate did not sign this element, skip
}
}
}
Logger::debug('Could not validate signature');
return false;
}
/**
* @param string $algorithm
* @param string $data
* @throws \UnexpectedValueException
* @return string
*/
private function computeFingerprint(string $algorithm, string $data): string
{
switch ($algorithm) {
case XMLSecurityDSig::SHA1:
$algo = 'SHA1';
break;
case XMLSecurityDSig::SHA256:
$algo = 'SHA256';
break;
case XMLSecurityDSig::SHA384:
$algo = 'SHA384';
break;
case XMLSecurityDSig::SHA512:
$algo = 'SHA512';
break;
default:
$known_opts = implode(", ", [
XMLSecurityDSig::SHA1,
XMLSecurityDSig::SHA256,
XMLSecurityDSig::SHA384,
XMLSecurityDSig::SHA512,
]);
throw new \UnexpectedValueException(
"Unsupported hashing function {$algorithm}. " .
"Known options: [{$known_opts}]"
);
}
return hash($algo, $data);
}
/**
* This function checks if this EntityDescriptor was signed with a certificate with the
* given fingerprint.
*
* @param string $fingerprint Fingerprint of the certificate which should have been used to sign this
* EntityDescriptor.
* @param string $algorithm Algorithm used to compute the fingerprint of the signing certicate.
*
* @return boolean True if it was signed with the certificate with the given fingerprint, false otherwise.
*/
public function validateFingerprint($fingerprint, $algorithm)
{
assert(is_string($fingerprint));
$fingerprint = strtolower(str_replace(":", "", $fingerprint));
$candidates = [];
foreach ($this->validators as $validator) {
foreach ($validator->getValidatingCertificates() as $cert) {
$decoded_cert = base64_decode($cert);
$fp = $this->computeFingerprint($algorithm, $decoded_cert);
$candidates[] = $fp;
if ($fp === $fingerprint) {
return true;
}
}
}
Logger::debug('Fingerprint was [' . $fingerprint . '] not one of [' . join(', ', $candidates) . ']');
return false;
}
}
|