RobRichards\XMLSecLibs\XMLSecurityKey::loadKey PHP Method

loadKey() public method

Loads the given key, or - with isFile set true - the key from the keyfile.
public loadKey ( string $key, boolean $isFile = false, boolean $isCert = false )
$key string
$isFile boolean
$isCert boolean
    public function loadKey($key, $isFile = false, $isCert = false)
    {
        if ($isFile) {
            $this->key = file_get_contents($key);
        } else {
            $this->key = $key;
        }
        if ($isCert) {
            $this->key = openssl_x509_read($this->key);
            openssl_x509_export($this->key, $str_cert);
            $this->x509Certificate = $str_cert;
            $this->key = $str_cert;
        } else {
            $this->x509Certificate = null;
        }
        if ($this->cryptParams['library'] == 'openssl') {
            if ($this->cryptParams['type'] == 'public') {
                if ($isCert) {
                    /* Load the thumbprint if this is an X509 certificate. */
                    $this->X509Thumbprint = self::getRawThumbprint($this->key);
                }
                $this->key = openssl_get_publickey($this->key);
                if (!$this->key) {
                    throw new Exception('Unable to extract public key');
                }
            } else {
                $this->key = openssl_get_privatekey($this->key, $this->passphrase);
            }
        } else {
            if ($this->cryptParams['cipher'] == MCRYPT_RIJNDAEL_128) {
                /* Check key length */
                switch ($this->type) {
                    case self::AES256_CBC:
                        if (strlen($this->key) < 25) {
                            throw new Exception('Key must contain at least 25 characters for this cipher');
                        }
                        break;
                    case self::AES192_CBC:
                        if (strlen($this->key) < 17) {
                            throw new Exception('Key must contain at least 17 characters for this cipher');
                        }
                        break;
                }
            }
        }
    }

Usage Example

 /**
  * @param AbstractSamlModel $object
  * @param XMLSecurityKey    $key
  *
  * @return SerializationContext
  */
 public function encrypt(AbstractSamlModel $object, XMLSecurityKey $key)
 {
     $oldKey = $key;
     $key = new XMLSecurityKey($this->keyTransportEncryption, ['type' => 'public']);
     $key->loadKey($oldKey->key);
     $serializationContext = new SerializationContext();
     $object->serialize($serializationContext->getDocument(), $serializationContext);
     $enc = new XMLSecEnc();
     $enc->setNode($serializationContext->getDocument()->firstChild);
     $enc->type = XMLSecEnc::Element;
     switch ($key->type) {
         case XMLSecurityKey::TRIPLEDES_CBC:
         case XMLSecurityKey::AES128_CBC:
         case XMLSecurityKey::AES192_CBC:
         case XMLSecurityKey::AES256_CBC:
             $symmetricKey = $key;
             break;
         case XMLSecurityKey::RSA_1_5:
         case XMLSecurityKey::RSA_SHA1:
         case XMLSecurityKey::RSA_SHA256:
         case XMLSecurityKey::RSA_SHA384:
         case XMLSecurityKey::RSA_SHA512:
         case XMLSecurityKey::RSA_OAEP_MGF1P:
             $symmetricKey = new XMLSecurityKey($this->blockEncryptionAlgorithm);
             $symmetricKey->generateSessionKey();
             $enc->encryptKey($key, $symmetricKey);
             break;
         default:
             throw new LightSamlException(sprintf('Unknown key type for encryption: "%s"', $key->type));
     }
     $this->encryptedElement = $enc->encryptNode($symmetricKey);
     return $serializationContext;
 }
All Usage Examples Of RobRichards\XMLSecLibs\XMLSecurityKey::loadKey