lightsaml2 /
lightSAML
| 1 | <?php |
||
| 2 | |||
| 3 | /* |
||
| 4 | * This file is part of the LightSAML-Core package. |
||
| 5 | * |
||
| 6 | * (c) Milos Tomic <[email protected]> |
||
| 7 | * |
||
| 8 | * This source file is subject to the MIT license that is bundled |
||
| 9 | * with this source code in the file LICENSE. |
||
| 10 | */ |
||
| 11 | |||
| 12 | namespace LightSaml\Credential; |
||
| 13 | |||
| 14 | use LightSaml\Error\LightSamlSecurityException; |
||
| 15 | use RobRichards\XMLSecLibs\XMLSecurityKey; |
||
| 16 | |||
| 17 | class KeyHelper |
||
| 18 | { |
||
| 19 | /** |
||
| 20 | * @param string $key Key content or key filename |
||
| 21 | * @param string $passphrase Passphrase for the private key |
||
| 22 | * @param bool $isFile true if $key is a filename of the key |
||
| 23 | * @param string $type |
||
| 24 | * |
||
| 25 | * @return XMLSecurityKey |
||
| 26 | */ |
||
| 27 | public static function createPrivateKey($key, $passphrase, $isFile = false, $type = XMLSecurityKey::RSA_SHA1) |
||
| 28 | { |
||
| 29 | $result = new XMLSecurityKey($type, ['type' => 'private']); |
||
| 30 | $result->passphrase = $passphrase; |
||
| 31 | $result->loadKey($key, $isFile, false); |
||
| 32 | |||
| 33 | return $result; |
||
| 34 | } |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @return XMLSecurityKey |
||
| 38 | */ |
||
| 39 | public static function createPublicKey(X509Certificate $certificate) |
||
| 40 | { |
||
| 41 | if (null == $certificate->getSignatureAlgorithm()) { |
||
| 42 | throw new LightSamlSecurityException('Unrecognized certificate signature algorithm'); |
||
| 43 | } |
||
| 44 | $key = new XMLSecurityKey($certificate->getSignatureAlgorithm(), ['type' => 'public']); |
||
| 45 | $key->loadKey($certificate->toPem(), false, true); |
||
| 46 | |||
| 47 | return $key; |
||
| 48 | } |
||
| 49 | |||
| 50 | /** |
||
| 51 | * @param string $algorithm |
||
| 52 | * |
||
| 53 | * @throws \LightSaml\Error\LightSamlSecurityException |
||
| 54 | * @throws \InvalidArgumentException |
||
| 55 | * |
||
| 56 | * @return XMLSecurityKey |
||
| 57 | */ |
||
| 58 | public static function castKey(XMLSecurityKey $key, $algorithm) |
||
| 59 | { |
||
| 60 | if (false == is_string($algorithm)) { |
||
|
0 ignored issues
–
show
|
|||
| 61 | throw new \InvalidArgumentException('Algorithm must be string'); |
||
| 62 | } |
||
| 63 | |||
| 64 | // do nothing if algorithm is already the type of the key |
||
| 65 | if ($key->type === $algorithm) { |
||
| 66 | return $key; |
||
| 67 | } |
||
| 68 | |||
| 69 | $keyInfo = openssl_pkey_get_details($key->key); |
||
| 70 | if (false === $keyInfo) { |
||
| 71 | throw new LightSamlSecurityException('Unable to get key details from XMLSecurityKey.'); |
||
| 72 | } |
||
| 73 | if (false == isset($keyInfo['key'])) { |
||
|
0 ignored issues
–
show
|
|||
| 74 | throw new LightSamlSecurityException('Missing key in public key details.'); |
||
| 75 | } |
||
| 76 | |||
| 77 | $newKey = new XMLSecurityKey($algorithm, ['type' => 'public']); |
||
| 78 | $newKey->loadKey($keyInfo['key']); |
||
| 79 | |||
| 80 | return $newKey; |
||
| 81 | } |
||
| 82 | } |
||
| 83 |
When comparing two booleans, it is generally considered safer to use the strict comparison operator.