You are here

public function XMLSecEnc::encryptKey in SAML SP 2.0 Single Sign On (SSO) - SAML Service Provider 8

Encrypt the XMLSecurityKey

Parameters

XMLSecurityKey $srcKey:

XMLSecurityKey $rawKey:

bool $append:

Throws

Exception

File

src/XMLSecurityKey.php, line 2075

Class

XMLSecEnc

Namespace

Drupal\miniorange_saml

Code

public function encryptKey($srcKey, $rawKey, $append = true) {
  if (!$srcKey instanceof XMLSecurityKey || !$rawKey instanceof XMLSecurityKey) {
    throw new Exception('Invalid Key');
  }
  $strEncKey = base64_encode($srcKey
    ->encryptData($rawKey->key));
  $root = $this->encdoc->documentElement;
  $encKey = $this->encdoc
    ->createElementNS(self::XMLENCNS, 'xenc:EncryptedKey');
  if ($append) {
    $keyInfo = $root
      ->insertBefore($this->encdoc
      ->createElementNS('http://www.w3.org/2000/09/xmldsig#', 'dsig:KeyInfo'), $root->firstChild);
    $keyInfo
      ->appendChild($encKey);
  }
  else {
    $this->encKey = $encKey;
  }
  $encMethod = $encKey
    ->appendChild($this->encdoc
    ->createElementNS(self::XMLENCNS, 'xenc:EncryptionMethod'));
  $encMethod
    ->setAttribute('Algorithm', $srcKey
    ->getAlgorith());
  if (!empty($srcKey->name)) {
    $keyInfo = $encKey
      ->appendChild($this->encdoc
      ->createElementNS('http://www.w3.org/2000/09/xmldsig#', 'dsig:KeyInfo'));
    $keyInfo
      ->appendChild($this->encdoc
      ->createElementNS('http://www.w3.org/2000/09/xmldsig#', 'dsig:KeyName', $srcKey->name));
  }
  $cipherData = $encKey
    ->appendChild($this->encdoc
    ->createElementNS(self::XMLENCNS, 'xenc:CipherData'));
  $cipherData
    ->appendChild($this->encdoc
    ->createElementNS(self::XMLENCNS, 'xenc:CipherValue', $strEncKey));
  if (is_array($this->references) && count($this->references) > 0) {
    $refList = $encKey
      ->appendChild($this->encdoc
      ->createElementNS(self::XMLENCNS, 'xenc:ReferenceList'));
    foreach ($this->references as $name => $reference) {
      $refuri = $reference["refuri"];
      $dataRef = $refList
        ->appendChild($this->encdoc
        ->createElementNS(self::XMLENCNS, 'xenc:DataReference'));
      $dataRef
        ->setAttribute("URI", '#' . $refuri);
    }
  }
  return;
}