SimpleSAML\Utils\Crypto::loadPrivateKey PHP Method

loadPrivateKey() public static method

This function loads a private key from a metadata array. It looks for the following elements: - 'privatekey': Name of a private key file in the cert-directory. - 'privatekey_pass': Password for the private key. It returns and array with the following elements: - 'PEM': Data for the private key, in PEM-format. - 'password': Password for the private key.
Author: Andreas Solberg, UNINETT AS ([email protected])
Author: Olav Morken, UNINETT AS ([email protected])
public static loadPrivateKey ( SimpleSAML_Configuration $metadata, boolean $required = false, string $prefix = '' ) : array | null
$metadata SimpleSAML_Configuration The metadata array the private key should be loaded from.
$required boolean Whether the private key is required. If this is true, a missing key will cause an exception. Defaults to false.
$prefix string The prefix which should be used when reading from the metadata array. Defaults to ''.
return array | null Extracted private key, or NULL if no private key is present.
    public static function loadPrivateKey(\SimpleSAML_Configuration $metadata, $required = false, $prefix = '')
    {
        if (!is_bool($required) || !is_string($prefix)) {
            throw new \InvalidArgumentException('Invalid input parameters.');
        }
        $file = $metadata->getString($prefix . 'privatekey', null);
        if ($file === null) {
            // no private key found
            if ($required) {
                throw new \SimpleSAML_Error_Exception('No private key found in metadata.');
            } else {
                return null;
            }
        }
        $file = Config::getCertPath($file);
        $data = @file_get_contents($file);
        if ($data === false) {
            throw new \SimpleSAML_Error_Exception('Unable to load private key from file "' . $file . '"');
        }
        $ret = array('PEM' => $data);
        if ($metadata->hasValue($prefix . 'privatekey_pass')) {
            $ret['password'] = $metadata->getString($prefix . 'privatekey_pass');
        }
        return $ret;
    }