Completed
Push — master ( a70ca1...835655 )
by Jafar
06:00
created

PublicKey::getKeyResource()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 6
nc 3
nop 2
dl 0
loc 11
rs 9.2
c 0
b 0
f 0
1
<?php
2
/*
3
 * This file is part of the Guarded Authentication package.
4
 *
5
 * (c) Jafar Jabr <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
namespace Jafar\Bundle\GuardedAuthenticationBundle\Api\JWTSigner\Signer\OpenSSL;
11
12
use InvalidArgumentException;
13
use Jafar\Bundle\GuardedAuthenticationBundle\Api\JWTSigner\Signer\SignerInterface;
14
use RuntimeException;
15
16
/**
17
 * Class HS512.
18
 *
19
 * @author Jafar Jabr <[email protected]>
20
 * Class handle sign inputs with the a public key algorithm, after hashing it.
21
 */
22
abstract class PublicKey implements SignerInterface
23
{
24
    /**
25
     * {@inheritdoc}
26
     */
27
    public function sign($input, $key, $password = null)
28
    {
29
        $keyResource = $this->getKeyResource($key, $password);
30
        if (!$this->supportsKey($keyResource)) {
31
            throw new InvalidArgumentException('Invalid key supplied.');
32
        }
33
34
        $signature = null;
35
        openssl_sign($input, $signature, $keyResource, $this->getHashingAlgorithm());
0 ignored issues
show
Bug introduced by
$this->getHashingAlgorithm() of type string is incompatible with the type integer expected by parameter $signature_alg of openssl_sign(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

35
        openssl_sign($input, $signature, $keyResource, /** @scrutinizer ignore-type */ $this->getHashingAlgorithm());
Loading history...
36
37
        return $signature;
38
    }
39
40
    /**
41
     * {@inheritdoc}
42
     */
43
    public function verify($key, $signature, $input)
44
    {
45
        $keyResource = $this->getKeyResource($key);
46
        if (!$this->supportsKey($keyResource)) {
47
            throw new InvalidArgumentException('Invalid key supplied.');
48
        }
49
50
        $result = openssl_verify($input, $signature, $keyResource, $this->getHashingAlgorithm());
0 ignored issues
show
Bug introduced by
$this->getHashingAlgorithm() of type string is incompatible with the type integer expected by parameter $signature_alg of openssl_verify(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

50
        $result = openssl_verify($input, $signature, $keyResource, /** @scrutinizer ignore-type */ $this->getHashingAlgorithm());
Loading history...
51
52
        if ($result === -1) {
53
            throw new RuntimeException('Unknown error during verification.');
54
        }
55
56
        return (bool) $result;
57
    }
58
59
    /**
60
     * Converts a string representation of a key into an OpenSSL resource.
61
     *
62
     * @param string|resource $key
63
     * @param string          $password
64
     *
65
     * @return resource OpenSSL key resource
66
     */
67
    protected function getKeyResource($key, $password = null)
68
    {
69
        if (is_resource($key)) {
70
            return $key;
71
        }
72
73
        $resource = openssl_pkey_get_public($key) ?: openssl_pkey_get_private($key, $password);
74
        if ($resource === false) {
75
            throw new RuntimeException('Could not read key resource: ' . openssl_error_string());
76
        }
77
        return $resource;
78
    }
79
80
    /**
81
     * Check if the key is supported by this signer.
82
     *
83
     * @param resource $key Public or private key
84
     *
85
     * @return bool
86
     */
87
    protected function supportsKey($key)
88
    {
89
        // OpenSSL 0.9.8+
90
        $keyDetails = openssl_pkey_get_details($key);
91
92
        return isset($keyDetails['type']) ? $this->getSupportedPrivateKeyType() === $keyDetails['type'] : false;
93
    }
94
95
    /**
96
     * Returns the hashing algorithm used in this signer.
97
     *
98
     * @return string
99
     */
100
    abstract protected function getHashingAlgorithm();
101
102
    /**
103
     * Returns the private key type supported in this signer.
104
     *
105
     * @return string
106
     */
107
    abstract protected function getSupportedPrivateKeyType();
108
}
109