KeyHelper::castKey()   A
last analyzed

Complexity

Conditions 5
Paths 5

Size

Total Lines 24

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 5.7283

Importance

Changes 0
Metric Value
dl 0
loc 24
c 0
b 0
f 0
ccs 9
cts 13
cp 0.6923
rs 9.2248
cc 5
nc 5
nop 2
crap 5.7283
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 41
    public static function createPrivateKey($key, $passphrase, $isFile = false, $type = XMLSecurityKey::RSA_SHA1)
28
    {
29 41
        $result = new XMLSecurityKey($type, array('type' => 'private'));
30 41
        $result->passphrase = $passphrase;
31 41
        $result->loadKey($key, $isFile, false);
32
33 41
        return $result;
34
    }
35
36
    /**
37
     * @param X509Certificate $certificate
38
     *
39
     * @return XMLSecurityKey
40
     */
41 30
    public static function createPublicKey(X509Certificate $certificate)
42
    {
43 30
        if (null == $certificate->getSignatureAlgorithm()) {
44
            throw new LightSamlSecurityException('Unrecognized certificate signature algorithm');
45
        }
46 30
        $key = new XMLSecurityKey($certificate->getSignatureAlgorithm(), array('type' => 'public'));
47 30
        $key->loadKey($certificate->toPem(), false, true);
48
49 30
        return $key;
50
    }
51
52
    /**
53
     * @param XMLSecurityKey $key
54
     * @param string         $algorithm
55
     *
56
     * @throws \LightSaml\Error\LightSamlSecurityException
57
     * @throws \InvalidArgumentException
58
     *
59
     * @return XMLSecurityKey
60
     */
61 1
    public static function castKey(XMLSecurityKey $key, $algorithm)
62
    {
63 1
        if (false == is_string($algorithm)) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
64
            throw new \InvalidArgumentException('Algorithm must be string');
65
        }
66
67
        // do nothing if algorithm is already the type of the key
68 1
        if ($key->type === $algorithm) {
69
            return $key;
70
        }
71
72 1
        $keyInfo = openssl_pkey_get_details($key->key);
73 1
        if (false === $keyInfo) {
74
            throw new LightSamlSecurityException('Unable to get key details from XMLSecurityKey.');
75
        }
76 1
        if (false == isset($keyInfo['key'])) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
77
            throw new LightSamlSecurityException('Missing key in public key details.');
78
        }
79
80 1
        $newKey = new XMLSecurityKey($algorithm, array('type' => 'public'));
81 1
        $newKey->loadKey($keyInfo['key']);
82
83 1
        return $newKey;
84
    }
85
}
86