PublicKey::verify()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 3
eloc 7
c 1
b 0
f 1
nc 3
nop 3
dl 0
loc 14
rs 10
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
11
namespace Jafar\Bundle\GuardedAuthenticationBundle\Api\JWTSigner\Signer\OpenSSL;
12
13
use InvalidArgumentException;
14
use Jafar\Bundle\GuardedAuthenticationBundle\Api\JWTSigner\Signer\SignerInterface;
15
use RuntimeException;
16
17
/**
18
 * Class HS512.
19
 *
20
 * @author Jafar Jabr <[email protected]>
21
 * Class handle sign inputs with the a public key algorithm, after hashing it.
22
 */
23
abstract class PublicKey implements SignerInterface
24
{
25
    /**
26
     * {@inheritdoc}
27
     */
28
    public function sign($input, $key, $password = null)
29
    {
30
        $keyResource = $this->getKeyResource($key, $password);
31
        if (!$this->supportsKey($keyResource)) {
32
            throw new InvalidArgumentException('Invalid key supplied.');
33
        }
34
35
        $signature = null;
36
        openssl_sign($input, $signature, $keyResource);
37
38
        return $signature;
39
    }
40
41
    /**
42
     * {@inheritdoc}
43
     */
44
    public function verify($key, $signature, $input)
45
    {
46
        $keyResource = $this->getKeyResource($key);
47
        if (!$this->supportsKey($keyResource)) {
48
            throw new InvalidArgumentException('Invalid key supplied.');
49
        }
50
51
        $result = openssl_verify($input, $signature, $keyResource);
52
53
        if (-1 === $result) {
54
            throw new RuntimeException('Unknown error during verification.');
55
        }
56
57
        return (bool) $result;
58
    }
59
60
    /**
61
     * Converts a string representation of a key into an OpenSSL resource.
62
     *
63
     * @param string|resource $key
64
     * @param string          $password
65
     *
66
     * @return resource OpenSSL key resource
67
     */
68
    protected function getKeyResource($key, $password = null)
69
    {
70
        if (is_resource($key)) {
71
            return $key;
72
        }
73
74
        $resource = openssl_pkey_get_public($key) ?: openssl_pkey_get_private($key, $password);
75
        if (false === $resource) {
76
            throw new RuntimeException('Could not read key resource: '.openssl_error_string());
77
        }
78
79
        return $resource;
80
    }
81
82
    /**
83
     * Check if the key is supported by this signer.
84
     *
85
     * @param resource $key Public or private key
86
     *
87
     * @return bool
88
     */
89
    protected function supportsKey($key)
90
    {
91
        // OpenSSL 0.9.8+
92
        $keyDetails = openssl_pkey_get_details($key);
93
94
        return isset($keyDetails['type']) ? $this->getSupportedPrivateKeyType() === $keyDetails['type'] : false;
95
    }
96
97
    /**
98
     * Returns the private key type supported in this signer.
99
     *
100
     * @return string
101
     */
102
    abstract protected function getSupportedPrivateKeyType();
103
}
104