|
static | aesDecrypt ($ciphertext) |
| Decrypt data using AES-256-CBC and the system-wide secret salt as key. More...
|
|
static | aesEncrypt ($data) |
| Encrypt data using AES-256-CBC and the system-wide secret salt as key. More...
|
|
static | der2pem ($der, $type='CERTIFICATE') |
| Convert data from DER to PEM encoding. More...
|
|
static | loadPrivateKey (\SimpleSAML_Configuration $metadata, $required=false, $prefix='', $full_path=false) |
| Load a private key from metadata. More...
|
|
static | loadPublicKey (\SimpleSAML_Configuration $metadata, $required=false, $prefix='') |
| Get public key or certificate from metadata. More...
|
|
static | pem2der ($pem) |
| Convert from PEM to DER encoding. More...
|
|
static | pwHash ($password, $algorithm, $salt=null) |
| This function hashes a password with a given algorithm. More...
|
|
static | secureCompare ($known, $user) |
| Compare two strings securely. More...
|
|
static | pwValid ($hash, $password) |
| This function checks if a password is valid. More...
|
|
Definition at line 10 of file Crypto.php.
◆ aesDecrypt()
static SimpleSAML\Utils\Crypto::aesDecrypt |
( |
|
$ciphertext | ) |
|
|
static |
◆ aesEncrypt()
static SimpleSAML\Utils\Crypto::aesEncrypt |
( |
|
$data | ) |
|
|
static |
◆ der2pem()
static SimpleSAML\Utils\Crypto::der2pem |
( |
|
$der, |
|
|
|
$type = 'CERTIFICATE' |
|
) |
| |
|
static |
Convert data from DER to PEM encoding.
- Parameters
-
string | $der | Data encoded in DER format. |
string | $type | The type of data we are encoding, as expressed by the PEM header. Defaults to "CERTIFICATE". |
- Returns
- string The same data encoded in PEM format.
- See also
- RFC7648 for known types and PEM format specifics.
Definition at line 160 of file Crypto.php.
Referenced by sspmod_authX509_Auth_Source_X509userCert\authenticate().
162 return "-----BEGIN ".$type.
"-----\n".
163 chunk_split(base64_encode($der), 64,
"\n").
164 "-----END ".$type.
"-----\n";
◆ loadPrivateKey()
static SimpleSAML\Utils\Crypto::loadPrivateKey |
( |
\SimpleSAML_Configuration |
$metadata, |
|
|
|
$required = false , |
|
|
|
$prefix = '' , |
|
|
|
$full_path = false |
|
) |
| |
|
static |
Load a private key from metadata.
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.
- Parameters
-
\SimpleSAML_Configuration | $metadata | The metadata array the private key should be loaded from. |
bool | $required | Whether the private key is required. If this is true, a missing key will cause an exception. Defaults to false. |
string | $prefix | The prefix which should be used when reading from the metadata array. Defaults to ''. |
bool | $full_path | Whether the filename found in the configuration contains the full path to the private key or not. Default to false. |
- Returns
- array|NULL Extracted private key, or NULL if no private key is present.
- Exceptions
-
Definition at line 195 of file Crypto.php.
References $data, $file, $ret, array, SimpleSAML\Utils\Config\getCertPath(), SimpleSAML_Configuration\getString(), and SimpleSAML_Configuration\hasValue().
Referenced by sspmod_saml_Message\addSign(), sspmod_saml_Message\getDecryptionKeys(), SimpleSAML_Utilities\loadPrivateKey(), and SimpleSAML\Bindings\Shib13\HTTPPost\sendResponse().
197 if (!is_bool($required) || !is_string($prefix) || !is_bool($full_path)) {
198 throw new \InvalidArgumentException(
'Invalid input parameters.');
202 if (
$file === null) {
205 throw new \SimpleSAML_Error_Exception(
'No private key found in metadata.');
216 if (
$data ===
false) {
217 throw new \SimpleSAML_Error_Exception(
'Unable to load private key from file "'.
$file.
'"');
224 if (
$metadata->hasValue($prefix.
'privatekey_pass')) {
225 $ret[
'password'] =
$metadata->getString($prefix.
'privatekey_pass');
$metadata['__DYNAMIC:1__']
Create styles array
The data for the language used.
static getCertPath($path)
Resolves a path that may be relative to the cert-directory.
if(!file_exists("$old.txt")) if($old===$new) if(file_exists("$new.txt")) $file
◆ loadPublicKey()
Get public key or certificate from metadata.
This function implements a function to retrieve the public key or certificate from a metadata array.
It will search for the following elements in the metadata:
- 'certData': The certificate as a base64-encoded string.
- 'certificate': A file with a certificate or public key in PEM-format.
- 'certFingerprint': The fingerprint of the certificate. Can be a single fingerprint, or an array of multiple valid fingerprints. (deprecated)
This function will return an array with these elements:
- 'PEM': The public key/certificate in PEM-encoding.
- 'certData': The certificate data, base64 encoded, on a single line. (Only present if this is a certificate.)
- 'certFingerprint': Array of valid certificate fingerprints. (Deprecated. Only present if this is a certificate.)
- Parameters
-
\SimpleSAML_Configuration | $metadata | The metadata. |
bool | $required | Whether the private key is required. If this is TRUE, a missing key will cause an exception. Default is FALSE. |
string | $prefix | The prefix which should be used when reading from the metadata array. Defaults to ''. |
- Returns
- array|NULL Public key or certificate data, or NULL if no public key or certificate was found.
- Exceptions
-
Definition at line 265 of file Crypto.php.
References $key, $keys, array, SimpleSAML_Configuration\getArrayizeString(), SimpleSAML_Configuration\getPublicKeys(), and SimpleSAML_Configuration\hasValue().
Referenced by sspmod_saml_Message\addSign(), SimpleSAML_Utilities\loadPublicKey(), and SimpleSAML\Bindings\Shib13\HTTPPost\sendResponse().
267 if (!is_bool($required) || !is_string($prefix)) {
268 throw new \InvalidArgumentException(
'Invalid input parameters.');
272 if (
$keys !== null) {
274 if ($key[
'type'] !==
'X509Certificate') {
277 if ($key[
'signing'] !==
true) {
280 $certData = $key[
'X509Certificate'];
281 $pem =
"-----BEGIN CERTIFICATE-----\n".
282 chunk_split($certData, 64).
283 "-----END CERTIFICATE-----\n";
284 $certFingerprint = strtolower(sha1(base64_decode($certData)));
287 'certData' => $certData,
289 'certFingerprint' =>
array($certFingerprint),
293 } elseif (
$metadata->hasValue($prefix.
'certFingerprint')) {
295 $fps =
$metadata->getArrayizeString($prefix.
'certFingerprint');
298 foreach ($fps as &$fp) {
299 assert(
'is_string($fp)');
300 $fp = strtolower(str_replace(
':',
'', $fp));
307 return array(
'certFingerprint' => $fps);
312 throw new \SimpleSAML_Error_Exception(
'No public key / certificate found in metadata.');
$metadata['__DYNAMIC:1__']
Create styles array
The data for the language used.
◆ pem2der()
static SimpleSAML\Utils\Crypto::pem2der |
( |
|
$pem | ) |
|
|
static |
Convert from PEM to DER encoding.
- Parameters
-
string | $pem | Data encoded in PEM format. |
- Returns
- string The same data encoded in DER format.
- Exceptions
-
Definition at line 327 of file Crypto.php.
References $end.
330 $begin =
"-----BEGIN ";
332 $lines = explode(
"\n", $pem);
333 $last = count($lines) - 1;
335 if (strpos($lines[0], $begin) !== 0) {
336 throw new \InvalidArgumentException(
"pem2der: input is not encoded in PEM format.");
339 if (strpos($lines[$last],
$end) !== 0) {
340 throw new \InvalidArgumentException(
"pem2der: input is not encoded in PEM format.");
342 unset($lines[$last]);
344 return base64_decode(implode($lines));
◆ pwHash()
static SimpleSAML\Utils\Crypto::pwHash |
( |
|
$password, |
|
|
|
$algorithm, |
|
|
|
$salt = null |
|
) |
| |
|
static |
This function hashes a password with a given algorithm.
- Parameters
-
string | $password | The password to hash. |
string | $algorithm | The hashing algorithm, uppercase, optionally prepended with 'S' (salted). See hash_algos() for a complete list of hashing algorithms. |
string | $salt | An optional salt to use. |
- Returns
- string The hashed password.
- Exceptions
-
Definition at line 365 of file Crypto.php.
References $password, GuzzleHttp\Psr7\hash(), and is.
367 if (!is_string($algorithm) || !is_string(
$password)) {
368 throw new \InvalidArgumentException(
'Invalid input parameters.');
372 if (in_array(strtolower($algorithm), hash_algos(),
true)) {
373 $alg_str =
'{'.str_replace(
'SHA1',
'SHA', $algorithm).
'}';
375 return $alg_str.base64_encode($hash);
379 if ($salt === null) {
381 $bytes = ($algorithm ==
'SSHA1') ? 4 : 8;
382 $salt = openssl_random_pseudo_bytes($bytes);
385 if ($algorithm[0] ==
'S' && in_array(substr(strtolower($algorithm), 1), hash_algos(),
true)) {
386 $alg = substr(strtolower($algorithm), 1);
387 $alg_str =
'{'.str_replace(
'SSHA1',
'SSHA', $algorithm).
'}';
389 return $alg_str.base64_encode($hash.$salt);
392 throw new \SimpleSAML_Error_Exception(
'Hashing algorithm \''.strtolower($algorithm).
'\' is not supported
');
hash(StreamInterface $stream, $algo, $rawOutput=false)
Calculate a hash of a Stream.
◆ pwValid()
static SimpleSAML\Utils\Crypto::pwValid |
( |
|
$hash, |
|
|
|
$password |
|
) |
| |
|
static |
This function checks if a password is valid.
- Parameters
-
string | $hash | The password as it appears in password file, optionally prepended with algorithm. |
string | $password | The password to check in clear. |
- Returns
- boolean True if the hash corresponds with the given password, false otherwise.
- Exceptions
-
Definition at line 440 of file Crypto.php.
442 if (!is_string($hash) || !is_string(
$password)) {
443 throw new \InvalidArgumentException(
'Invalid input parameters.');
447 if (preg_match(
'/^{(.*?)}(.*)$/', $hash, $matches)) {
449 $alg = preg_replace(
'/^(S?SHA)$/',
'${1}1', $matches[1]);
452 if (in_array(strtolower($alg), hash_algos(),
true)) {
453 return self::secureCompare($hash, self::pwHash(
$password, $alg));
457 if ($alg[0] ===
'S' && in_array(substr(strtolower($alg), 1), hash_algos(),
true)) {
458 $php_alg = substr(strtolower($alg), 1);
461 $hash_length = strlen(
hash($php_alg,
'',
true));
462 $salt = substr(base64_decode($matches[2]), $hash_length);
463 return self::secureCompare($hash, self::pwHash(
$password, $alg, $salt));
469 throw new \SimpleSAML_Error_Exception(
'Hashing algorithm \''.strtolower($alg).
'\' is not supported
');
hash(StreamInterface $stream, $algo, $rawOutput=false)
Calculate a hash of a Stream.
◆ secureCompare()
static SimpleSAML\Utils\Crypto::secureCompare |
( |
|
$known, |
|
|
|
$user |
|
) |
| |
|
static |
Compare two strings securely.
This method checks if two strings are equal in constant time, avoiding timing attacks. Use it every time we need to compare a string with a secret that shouldn't be leaked, i.e. when verifying passwords, one-time codes, etc.
- Parameters
-
string | $known | A known string. |
string | $user | A user-provided string to compare with the known string. |
- Returns
- bool True if both strings are equal, false otherwise.
Definition at line 407 of file Crypto.php.
409 if (function_exists(
'hash_equals')) {
411 return hash_equals($known, $user);
415 $len = mb_strlen($known,
'8bit');
416 if ($len !== mb_strlen($user,
'8bit')) {
420 for (
$i = 0;
$i < $len;
$i++) {
421 $diff |= ord($known[
$i]) ^ ord($user[
$i]);
The documentation for this class was generated from the following file:
- libs/composer/vendor/simplesamlphp/simplesamlphp/lib/SimpleSAML/Utils/Crypto.php