Example #1
0
 public static function staticLocateKeyInfo($objBaseKey = null, $node = null)
 {
     if (empty($node) || !$node instanceof DOMNode) {
         return;
     }
     if ($doc = $node->ownerDocument) {
         $xpath = new DOMXPath($doc);
         $xpath->registerNamespace('xmlsecenc', XMLSecEnc::XMLENCNS);
         $xpath->registerNamespace('xmlsecdsig', XMLSecurityDSig::XMLDSIGNS);
         $query = "./xmlsecdsig:KeyInfo";
         $nodeset = $xpath->query($query, $node);
         if ($encmeth = $nodeset->item(0)) {
             foreach ($encmeth->childNodes as $child) {
                 switch ($child->localName) {
                     case 'KeyName':
                         if (!empty($objBaseKey)) {
                             $objBaseKey->name = $child->nodeValue;
                         }
                         break;
                     case 'KeyValue':
                         foreach ($child->childNodes as $keyval) {
                             switch ($keyval->localName) {
                                 case 'DSAKeyValue':
                                     throw new Exception("DSAKeyValue currently not supported");
                                     break;
                                 case 'RSAKeyValue':
                                     $modulus = null;
                                     $exponent = null;
                                     if ($modulusNode = $keyval->getElementsByTagName('Modulus')->item(0)) {
                                         $modulus = base64_decode($modulusNode->nodeValue);
                                     }
                                     if ($exponentNode = $keyval->getElementsByTagName('Exponent')->item(0)) {
                                         $exponent = base64_decode($exponentNode->nodeValue);
                                     }
                                     if (empty($modulus) || empty($exponent)) {
                                         throw new Exception("Missing Modulus or Exponent");
                                     }
                                     $publicKey = XMLSecurityKey::convertRSA($modulus, $exponent);
                                     $objBaseKey->loadKey($publicKey);
                                     break;
                             }
                         }
                         break;
                     case 'RetrievalMethod':
                         /* Not currently supported */
                         break;
                     case 'EncryptedKey':
                         $objenc = new XMLSecEnc();
                         $objenc->setNode($child);
                         if (!($objKey = $objenc->locateKey())) {
                             throw new Exception("Unable to locate algorithm for this Encrypted Key");
                         }
                         $objKey->isEncrypted = true;
                         $objKey->encryptedCtx = $objenc;
                         XMLSecEnc::staticLocateKeyInfo($objKey, $child);
                         return $objKey;
                         break;
                     case 'X509Data':
                         if ($x509certNodes = $child->getElementsByTagName('X509Certificate')) {
                             if ($x509certNodes->length > 0) {
                                 $x509cert = $x509certNodes->item(0)->textContent;
                                 $x509cert = str_replace(array("\r", "\n"), "", $x509cert);
                                 $x509cert = "-----BEGIN CERTIFICATE-----\n" . chunk_split($x509cert, 64, "\n") . "-----END CERTIFICATE-----\n";
                                 $objBaseKey->loadKey($x509cert);
                             }
                         }
                         break;
                 }
             }
         }
         return $objBaseKey;
     }
     return;
 }
Example #2
0
 public function addUserToken($userName, $password = null, $passwordDigest = false)
 {
     if ($passwordDigest && empty($password)) {
         throw new Exception("Cannot calculate the digest without a password");
     }
     $security = $this->locateSecurityHeader();
     $token = $this->soapDoc->createElementNS(WSSESoap::WSSENS, WSSESoap::WSSEPFX . ':UsernameToken');
     $security->insertBefore($token, $security->firstChild);
     $username = $this->soapDoc->createElementNS(WSSESoap::WSSENS, WSSESoap::WSSEPFX . ':Username', $userName);
     $token->appendChild($username);
     /* Generate nonce - create a 256 bit session key to be used */
     $objKey = new XMLSecurityKey(XMLSecurityKey::AES256_CBC);
     $nonce = $objKey->generateSessionKey();
     unset($objKey);
     $createdate = gmdate("Y-m-d\\TH:i:s") . 'Z';
     if ($password) {
         $passType = WSSESoap::WSUNAME . '#PasswordText';
         if ($passwordDigest) {
             $password = base64_encode(sha1($nonce . $createdate . $password, true));
             $passType = WSSESoap::WSUNAME . '#PasswordDigest';
         }
         $passwordNode = $this->soapDoc->createElementNS(WSSESoap::WSSENS, WSSESoap::WSSEPFX . ':Password', $password);
         $token->appendChild($passwordNode);
         $passwordNode->setAttribute('Type', $passType);
     }
     $nonceNode = $this->soapDoc->createElementNS(WSSESoap::WSSENS, WSSESoap::WSSEPFX . ':Nonce', base64_encode($nonce));
     $token->appendChild($nonceNode);
     $created = $this->soapDoc->createElementNS(WSSESoap::WSUNS, WSSESoap::WSUPFX . ':Created', $createdate);
     $token->appendChild($created);
 }
 public static function convertRSA($modulus, $exponent)
 {
     /* make an ASN publicKeyInfo */
     $exponentEncoding = XMLSecurityKey::makeAsnSegment(0x2, $exponent);
     $modulusEncoding = XMLSecurityKey::makeAsnSegment(0x2, $modulus);
     $sequenceEncoding = XMLSecurityKey::makeAsnSegment(0x30, $modulusEncoding . $exponentEncoding);
     $bitstringEncoding = XMLSecurityKey::makeAsnSegment(0x3, $sequenceEncoding);
     $rsaAlgorithmIdentifier = pack("H*", "300D06092A864886F70D0101010500");
     $publicKeyInfo = XMLSecurityKey::makeAsnSegment(0x30, $rsaAlgorithmIdentifier . $bitstringEncoding);
     /* encode the publicKeyInfo in base64 and add PEM brackets */
     $publicKeyInfoBase64 = base64_encode($publicKeyInfo);
     $encoding = "-----BEGIN PUBLIC KEY-----\n";
     $offset = 0;
     while ($segment = substr($publicKeyInfoBase64, $offset, 64)) {
         $encoding = $encoding . $segment . "\n";
         $offset += 64;
     }
     return $encoding . "-----END PUBLIC KEY-----\n";
 }