Passed
Push — master ( 90db0f...396226 )
by Jafar
03:16
created

PublicKey   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 80
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 80
rs 10
c 0
b 0
f 0
wmc 11

4 Methods

Rating   Name   Duplication   Size   Complexity  
A verify() 0 14 3
A sign() 0 11 2
A getKeyResource() 0 12 4
A supportsKey() 0 6 2
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 ($result === -1) {
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