ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
SAML2\Utils Class Reference
+ Collaboration diagram for SAML2\Utils:

Static Public Member Functions

static castKey (XMLSecurityKey $key, $algorithm, $type='public')
 Helper function to convert a XMLSecurityKey to the correct algorithm. More...
 
static xpQuery (\DOMNode $node, $query)
 Do an XPath query on an XML node. More...
 
static parseBoolean (\DOMElement $node, $attributeName, $default=null)
 Parse a boolean attribute. More...
 
static addNameId (\DOMElement $node, array $nameId)
 Create a NameID element. More...
 
static parseNameId (\DOMElement $xml)
 Parse a NameID element. More...
 
static insertSignature (XMLSecurityKey $key, array $certificates, \DOMElement $root, \DOMNode $insertBefore=null)
 Insert a Signature-node. More...
 
static decryptElement (\DOMElement $encryptedData, XMLSecurityKey $inputKey, array $blacklist=array())
 Decrypt an encrypted element. More...
 
static extractLocalizedStrings (\DOMElement $parent, $namespaceURI, $localName)
 Extract localized strings from a set of nodes. More...
 
static extractStrings (\DOMElement $parent, $namespaceURI, $localName)
 Extract strings from a set of nodes. More...
 
static addString (\DOMElement $parent, $namespace, $name, $value)
 Append string element. More...
 
static addStrings (\DOMElement $parent, $namespace, $name, $localized, array $values)
 Append string elements. More...
 
static createKeyDescriptor ($x509Data)
 Create a KeyDescriptor with the given certificate. More...
 
static xsDateTimeToTimestamp ($time)
 This function converts a SAML2 timestamp on the form yyyy-mm-ddThh:mm:ss(.s+)?Z to a UNIX timestamp. More...
 
static getContainer ()
 

Detailed Description

Definition at line 20 of file Utils.php.

Member Function Documentation

◆ addNameId()

static SAML2\Utils::addNameId ( \DOMElement  $node,
array  $nameId 
)
static

Create a NameID element.

The NameId array can have the following elements: 'Value', 'Format', 'NameQualifier, 'SPNameQualifier' and 'SPProviderID'.

Only the 'Value'-element is required.

Parameters
\DOMElement$nodeThe DOM node we should append the NameId to.
array$nameIdThe name identifier.
Deprecated:
Please use objects instead: $nameId = new (); $nameId->value = $value; ... $nameId->toXML($node);

Definition at line 314 of file Utils.php.

315  {
316  assert(array_key_exists("Value", $nameId));
317 
318  $nid = new XML\saml\NameID();
319 
320  $nid->value = $nameId['Value'];
321 
322  if (array_key_exists('NameQualifier', $nameId) && $nameId['NameQualifier'] !== null) {
323  $nid->NameQualifier = $nameId['NameQualifier'];
324  }
325  if (array_key_exists('SPNameQualifier', $nameId) && $nameId['SPNameQualifier'] !== null) {
326  $nid->SPNameQualifier = $nameId['SPNameQualifier'];
327  }
328  if (array_key_exists('Format', $nameId) && $nameId['Format'] !== null) {
329  $nid->Format = $nameId['Format'];
330  }
331 
332  $nid->toXML($node);
333  }
$nameId
Definition: saml2-acs.php:138

◆ addString()

static SAML2\Utils::addString ( \DOMElement  $parent,
  $namespace,
  $name,
  $value 
)
static

Append string element.

Parameters
\DOMElement$parentThe parent element we should append the new nodes to.
string$namespaceThe namespace of the created element.
string$nameThe name of the created element.
string$valueThe value of the element.
Returns
The generated element.

Definition at line 635 of file Utils.php.

References $n, $name, and $namespace.

Referenced by SAML2\XML\ds\KeyName\toXML(), SAML2\XML\ds\X509Certificate\toXML(), SAML2\XML\md\AdditionalMetadataLocation\toXML(), and SAML2\XML\md\ContactPerson\toXML().

636  {
637  assert(is_string($namespace));
638  assert(is_string($name));
639  assert(is_string($value));
640 
641  $doc = $parent->ownerDocument;
642 
643  $n = $doc->createElementNS($namespace, $name);
644  $n->appendChild($doc->createTextNode($value));
645  $parent->appendChild($n);
646 
647  return $n;
648  }
if($err=$client->getError()) $namespace
$n
Definition: RandomTest.php:85
+ Here is the caller graph for this function:

◆ addStrings()

static SAML2\Utils::addStrings ( \DOMElement  $parent,
  $namespace,
  $name,
  $localized,
array  $values 
)
static

Append string elements.

Parameters
\DOMElement$parentThe parent element we should append the new nodes to.
string$namespaceThe namespace of the created elements
string$nameThe name of the created elements
bool$localizedWhether the strings are localized, and should include the xml:lang attribute.
array$valuesThe values we should create the elements from.

Definition at line 659 of file Utils.php.

References $index, $n, $name, and $namespace.

Referenced by SAML2\XML\mdrpi\RegistrationInfo\toXML(), SAML2\XML\mdui\DiscoHints\toXML(), SAML2\XML\md\AuthnAuthorityDescriptor\toXML(), SAML2\XML\md\PDPDescriptor\toXML(), SAML2\XML\mdrpi\PublicationInfo\toXML(), SAML2\XML\md\Organization\toXML(), SAML2\XML\md\SSODescriptorType\toXML(), SAML2\XML\md\AttributeConsumingService\toXML(), SAML2\XML\md\AttributeAuthorityDescriptor\toXML(), SAML2\XML\mdui\UIInfo\toXML(), SAML2\XML\md\IDPSSODescriptor\toXML(), SAML2\XML\md\AffiliationDescriptor\toXML(), and SAML2\XML\md\ContactPerson\toXML().

660  {
661  assert(is_string($namespace));
662  assert(is_string($name));
663  assert(is_bool($localized));
664 
665  $doc = $parent->ownerDocument;
666 
667  foreach ($values as $index => $value) {
668  $n = $doc->createElementNS($namespace, $name);
669  $n->appendChild($doc->createTextNode($value));
670  if ($localized) {
671  $n->setAttribute('xml:lang', $index);
672  }
673  $parent->appendChild($n);
674  }
675  }
if($err=$client->getError()) $namespace
$index
Definition: metadata.php:60
$values
$n
Definition: RandomTest.php:85
+ Here is the caller graph for this function:

◆ castKey()

static SAML2\Utils::castKey ( XMLSecurityKey  $key,
  $algorithm,
  $type = 'public' 
)
static

Helper function to convert a XMLSecurityKey to the correct algorithm.

Parameters
XMLSecurityKey$keyThe key.
string$algorithmThe desired algorithm.
string$typePublic or private key, defaults to public.
Returns
XMLSecurityKey The new key.
Exceptions

Definition at line 112 of file Utils.php.

References $algo, $info, $key, and $type.

113  {
114  assert(is_string($algorithm));
115  assert($type === "public" || $type === "private");
116 
117  // do nothing if algorithm is already the type of the key
118  if ($key->type === $algorithm) {
119  return $key;
120  }
121 
122  if (!in_array($algorithm, array(
128  ), true)) {
129  throw new \Exception('Unsupported signing algorithm.');
130  }
131 
132  $keyInfo = openssl_pkey_get_details($key->key);
133  if ($keyInfo === false) {
134  throw new \Exception('Unable to get key details from XMLSecurityKey.');
135  }
136  if (!isset($keyInfo['key'])) {
137  throw new \Exception('Missing key in public key details.');
138  }
139 
140  $newKey = new XMLSecurityKey($algorithm, array('type'=>$type));
141  $newKey->loadKey($keyInfo['key']);
142 
143  return $newKey;
144  }
$type
$key
Definition: croninfo.php:18

◆ createKeyDescriptor()

static SAML2\Utils::createKeyDescriptor (   $x509Data)
static

Create a KeyDescriptor with the given certificate.

Parameters
string$x509DataThe certificate, as a base64-encoded DER data.
Returns
The keydescriptor.

Definition at line 683 of file Utils.php.

684  {
685  assert(is_string($x509Data));
686 
687  $x509Certificate = new X509Certificate();
688  $x509Certificate->certificate = $x509Data;
689 
690  $x509Data = new X509Data();
691  $x509Data->data[] = $x509Certificate;
692 
693  $keyInfo = new KeyInfo();
694  $keyInfo->info[] = $x509Data;
695 
696  $keyDescriptor = new KeyDescriptor();
697  $keyDescriptor->KeyInfo = $keyInfo;
698 
699  return $keyDescriptor;
700  }

◆ decryptElement()

static SAML2\Utils::decryptElement ( \DOMElement  $encryptedData,
XMLSecurityKey  $inputKey,
array  $blacklist = array() 
)
static

Decrypt an encrypted element.

Parameters
\DOMElement$encryptedDataThe encrypted data.
XMLSecurityKey$inputKeyThe decryption key.
array$blacklistBlacklisted decryption algorithms.
Returns
The decrypted element.
Exceptions

Definition at line 558 of file Utils.php.

559  {
560  try {
561  return self::doDecryptElement($encryptedData, $inputKey, $blacklist);
562  } catch (\Exception $e) {
563  /*
564  * Something went wrong during decryption, but for security
565  * reasons we cannot tell the user what failed.
566  */
567  Utils::getContainer()->getLogger()->error('Decryption failed: ' . $e->getMessage());
568  throw new \Exception('Failed to decrypt XML element.', 0, $e);
569  }
570  }
static getContainer()
Definition: Utils.php:752

◆ extractLocalizedStrings()

static SAML2\Utils::extractLocalizedStrings ( \DOMElement  $parent,
  $namespaceURI,
  $localName 
)
static

Extract localized strings from a set of nodes.

Parameters
\DOMElement$parentThe element that contains the localized strings.
string$namespaceURIThe namespace URI the localized strings should have.
string$localNameThe localName of the localized strings.
Returns
array Localized strings.

Definition at line 580 of file Utils.php.

References $language, and $ret.

Referenced by SAML2\XML\mdrpi\RegistrationInfo\__construct(), SAML2\XML\md\Organization\__construct(), SAML2\XML\mdrpi\PublicationInfo\__construct(), SAML2\XML\md\AttributeConsumingService\__construct(), and SAML2\XML\mdui\UIInfo\__construct().

581  {
582  assert(is_string($namespaceURI));
583  assert(is_string($localName));
584 
585  $ret = array();
586  for ($node = $parent->firstChild; $node !== null; $node = $node->nextSibling) {
587  if ($node->namespaceURI !== $namespaceURI || $node->localName !== $localName) {
588  continue;
589  }
590 
591  if ($node->hasAttribute('xml:lang')) {
592  $language = $node->getAttribute('xml:lang');
593  } else {
594  $language = 'en';
595  }
596  $ret[$language] = trim($node->textContent);
597  }
598 
599  return $ret;
600  }
$ret
Definition: parser.php:6
+ Here is the caller graph for this function:

◆ extractStrings()

static SAML2\Utils::extractStrings ( \DOMElement  $parent,
  $namespaceURI,
  $localName 
)
static

Extract strings from a set of nodes.

Parameters
\DOMElement$parentThe element that contains the localized strings.
string$namespaceURIThe namespace URI the string elements should have.
string$localNameThe localName of the string elements.
Returns
array The string values of the various nodes.

Definition at line 610 of file Utils.php.

References $ret.

Referenced by SAML2\XML\md\AuthnAuthorityDescriptor\__construct(), SAML2\XML\md\PDPDescriptor\__construct(), SAML2\XML\mdui\DiscoHints\__construct(), SAML2\XML\md\SSODescriptorType\__construct(), SAML2\XML\md\AttributeAuthorityDescriptor\__construct(), SAML2\XML\md\IDPSSODescriptor\__construct(), and SAML2\XML\md\AffiliationDescriptor\__construct().

611  {
612  assert(is_string($namespaceURI));
613  assert(is_string($localName));
614 
615  $ret = array();
616  for ($node = $parent->firstChild; $node !== null; $node = $node->nextSibling) {
617  if ($node->namespaceURI !== $namespaceURI || $node->localName !== $localName) {
618  continue;
619  }
620  $ret[] = trim($node->textContent);
621  }
622 
623  return $ret;
624  }
$ret
Definition: parser.php:6
+ Here is the caller graph for this function:

◆ getContainer()

static SAML2\Utils::getContainer ( )
static
Returns

Definition at line 752 of file Utils.php.

753  {
755  }

◆ insertSignature()

static SAML2\Utils::insertSignature ( XMLSecurityKey  $key,
array  $certificates,
\DOMElement  $root,
\DOMNode  $insertBefore = null 
)
static

Insert a Signature-node.

Parameters
XMLSecurityKey$keyThe key we should use to sign the message.
array$certificatesThe certificates we should add to the signature node.
\DOMElement$rootThe XML node we should sign.
\DOMNode$insertBeforeThe XML element we should insert the signature element before.

Definition at line 364 of file Utils.php.

References $certificate, $type, $xml, RobRichards\XMLSecLibs\XMLSecurityKey\getAlgorithm(), and RobRichards\XMLSecLibs\XMLSecurityKey\getSymmetricKeySize().

369  {
370  $objXMLSecDSig = new XMLSecurityDSig();
371  $objXMLSecDSig->setCanonicalMethod(XMLSecurityDSig::EXC_C14N);
372 
373  switch ($key->type) {
376  break;
379  break;
382  break;
383  default:
385  }
386 
387  $objXMLSecDSig->addReferenceList(
388  array($root),
389  $type,
390  array('http://www.w3.org/2000/09/xmldsig#enveloped-signature', XMLSecurityDSig::EXC_C14N),
391  array('id_name' => 'ID', 'overwrite' => false)
392  );
393 
394  $objXMLSecDSig->sign($key);
395 
396  foreach ($certificates as $certificate) {
397  $objXMLSecDSig->add509Cert($certificate, true);
398  }
399 
400  $objXMLSecDSig->insertSignature($root, $insertBefore);
401  }
$type
$certificates
Definition: metarefresh.php:39
if(@file_exists(dirname(__FILE__).'/lang/eng.php')) $certificate
Definition: example_052.php:77
$root
Definition: sabredav.php:45
$key
Definition: croninfo.php:18
+ Here is the call graph for this function:

◆ parseBoolean()

static SAML2\Utils::parseBoolean ( \DOMElement  $node,
  $attributeName,
  $default = null 
)
static

Parse a boolean attribute.

Parameters
\DOMElement$nodeThe element we should fetch the attribute from.
string$attributeNameThe name of the attribute.
mixed$defaultThe value that should be returned if the attribute doesn't exist.
Returns
bool|mixed The value of the attribute, or $default if the attribute doesn't exist.
Exceptions

Definition at line 276 of file Utils.php.

References $default.

Referenced by SAML2\XML\md\RequestedAttribute\__construct(), SAML2\XML\md\IndexedEndpointType\__construct(), SAML2\XML\shibmd\Scope\__construct(), SAML2\XML\md\SPSSODescriptor\__construct(), SAML2\XML\md\AttributeConsumingService\__construct(), and SAML2\XML\md\IDPSSODescriptor\__construct().

277  {
278  assert(is_string($attributeName));
279 
280  if (!$node->hasAttribute($attributeName)) {
281  return $default;
282  }
283  $value = $node->getAttribute($attributeName);
284  switch (strtolower($value)) {
285  case '0':
286  case 'false':
287  return false;
288  case '1':
289  case 'true':
290  return true;
291  default:
292  throw new \Exception('Invalid value of boolean attribute ' . var_export($attributeName, true) . ': ' . var_export($value, true));
293  }
294  }
$default
Definition: build.php:20
+ Here is the caller graph for this function:

◆ parseNameId()

static SAML2\Utils::parseNameId ( \DOMElement  $xml)
static

Parse a NameID element.

Parameters
\DOMElement$xmlThe DOM element we should parse.
Returns
array The parsed name identifier.
Deprecated:
Please use objects instead: $nameId = new ($xml);

Definition at line 343 of file Utils.php.

References $ret.

344  {
345  $ret = array('Value' => trim($xml->textContent));
346 
347  foreach (array('NameQualifier', 'SPNameQualifier', 'SPProvidedID', 'Format') as $attr) {
348  if ($xml->hasAttribute($attr)) {
349  $ret[$attr] = $xml->getAttribute($attr);
350  }
351  }
352 
353  return $ret;
354  }
$ret
Definition: parser.php:6

◆ xpQuery()

static SAML2\Utils::xpQuery ( \DOMNode  $node,
  $query 
)
static

Do an XPath query on an XML node.

Parameters
\DOMNode$nodeThe XML node.
string$queryThe query.
Returns
[] Array with matching DOM nodes.

Definition at line 191 of file Utils.php.

References $i, $query, $results, and $ret.

Referenced by SAML2\XML\mdattr\EntityAttributes\__construct(), SAML2\XML\saml\SubjectConfirmation\__construct(), SAML2\XML\md\KeyDescriptor\__construct(), SAML2\XML\md\AuthnAuthorityDescriptor\__construct(), SAML2\XML\md\PDPDescriptor\__construct(), SAML2\XML\mdui\DiscoHints\__construct(), SAML2\XML\saml\Attribute\__construct(), SAML2\XML\md\SPSSODescriptor\__construct(), SAML2\XML\md\SSODescriptorType\__construct(), SAML2\XML\md\AttributeConsumingService\__construct(), SAML2\XML\md\EntitiesDescriptor\__construct(), SAML2\XML\md\AttributeAuthorityDescriptor\__construct(), SAML2\XML\mdui\UIInfo\__construct(), SAML2\XML\md\IDPSSODescriptor\__construct(), SAML2\XML\md\AffiliationDescriptor\__construct(), SAML2\XML\md\EntityDescriptor\__construct(), SAML2\XML\md\RoleDescriptor\__construct(), SAML2\XML\samlp\Extensions\getList(), SAML2\XML\md\Extensions\getList(), and SAML2\XML\md\ContactPerson\getStringElements().

192  {
193  assert(is_string($query));
194  static $xpCache = null;
195 
196  if ($node instanceof \DOMDocument) {
197  $doc = $node;
198  } else {
199  $doc = $node->ownerDocument;
200  }
201 
202  if ($xpCache === null || !$xpCache->document->isSameNode($doc)) {
203  $xpCache = new \DOMXPath($doc);
204  $xpCache->registerNamespace('soap-env', Constants::NS_SOAP);
205  $xpCache->registerNamespace('saml_protocol', Constants::NS_SAMLP);
206  $xpCache->registerNamespace('saml_assertion', Constants::NS_SAML);
207  $xpCache->registerNamespace('saml_metadata', Constants::NS_MD);
208  $xpCache->registerNamespace('ds', XMLSecurityDSig::XMLDSIGNS);
209  $xpCache->registerNamespace('xenc', XMLSecEnc::XMLENCNS);
210  }
211 
212  $results = $xpCache->query($query, $node);
213  $ret = array();
214  for ($i = 0; $i < $results->length; $i++) {
215  $ret[$i] = $results->item($i);
216  }
217 
218  return $ret;
219  }
const NS_SOAP
The namespace for the SOAP protocol.
Definition: Constants.php:215
$query
const NS_MD
The namespace for the SAML 2 metadata.
Definition: Constants.php:230
const NS_SAMLP
The namespace for the SAML 2 protocol.
Definition: Constants.php:220
$results
Definition: svg-scanner.php:47
$ret
Definition: parser.php:6
$i
Definition: disco.tpl.php:19
const NS_SAML
The namespace for the SAML 2 assertions.
Definition: Constants.php:225
+ Here is the caller graph for this function:

◆ xsDateTimeToTimestamp()

static SAML2\Utils::xsDateTimeToTimestamp (   $time)
static

This function converts a SAML2 timestamp on the form yyyy-mm-ddThh:mm:ss(.s+)?Z to a UNIX timestamp.

The sub-second part is ignored.

Andreas comments: I got this timestamp from Shibboleth 1.3 IdP: 2008-01-17T11:28:03.577Z Therefore I added to possibility to have microseconds to the format. Added: (.\d{1,3})? to the regex.

Note that we always require a 'Z' timezone for the dateTime to be valid. This is not in the SAML spec but that's considered to be a bug in the spec. See https://github.com/simplesamlphp/saml2/pull/36 for some background.

Parameters
string$timeThe time we should convert.
Returns
int Converted to a unix timestamp.
Exceptions

Definition at line 721 of file Utils.php.

References $time.

Referenced by SAML2\XML\mdrpi\RegistrationInfo\__construct(), SAML2\XML\mdrpi\PublicationInfo\__construct(), SAML2\XML\md\EntitiesDescriptor\__construct(), SAML2\XML\saml\SubjectConfirmationData\__construct(), SAML2\XML\md\AffiliationDescriptor\__construct(), SAML2\XML\md\EntityDescriptor\__construct(), SAML2\XML\md\RoleDescriptor\__construct(), SimpleSAML_Metadata_SAMLBuilder\addExtensions(), SimpleSAML_Utilities\checkDateConditions(), and SimpleSAML\XML\Shib13\AuthnResponse\checkDateConditions().

722  {
723  $matches = array();
724 
725  // We use a very strict regex to parse the timestamp.
726  $regex = '/^(\\d\\d\\d\\d)-(\\d\\d)-(\\d\\d)T(\\d\\d):(\\d\\d):(\\d\\d)(?:\\.\\d{1,9})?Z$/D';
727  if (preg_match($regex, $time, $matches) == 0) {
728  throw new \Exception(
729  'Invalid SAML2 timestamp passed to xsDateTimeToTimestamp: ' . $time
730  );
731  }
732 
733  // Extract the different components of the time from the matches in the regex.
734  // intval will ignore leading zeroes in the string.
735  $year = intval($matches[1]);
736  $month = intval($matches[2]);
737  $day = intval($matches[3]);
738  $hour = intval($matches[4]);
739  $minute = intval($matches[5]);
740  $second = intval($matches[6]);
741 
742  // We use gmmktime because the timestamp will always be given
743  //in UTC.
744  $ts = gmmktime($hour, $minute, $second, $month, $day, $year);
745 
746  return $ts;
747  }
$time
Definition: cron.php:21
+ Here is the caller graph for this function:

The documentation for this class was generated from the following file: