GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Passed
Branch php72 (880eb0)
by Joni
05:58
created

OpenSSLSignatureAlgorithm::computeSignature()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 21
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 4

Importance

Changes 0
Metric Value
eloc 12
dl 0
loc 21
ccs 12
cts 12
cp 1
rs 9.8666
c 0
b 0
f 0
cc 4
nc 4
nop 1
crap 4
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace Sop\JWX\JWS\Algorithm;
6
7
use Sop\JWX\JWK\Asymmetric\PrivateKeyJWK;
8
use Sop\JWX\JWK\Asymmetric\PublicKeyJWK;
9
use Sop\JWX\JWS\SignatureAlgorithm;
10
11
/**
12
 * Base class for algorithms employing asymmetric signature computation
13
 * using OpenSSL extension.
14
 */
15
abstract class OpenSSLSignatureAlgorithm extends SignatureAlgorithm
16
{
17
    /**
18
     * Public key.
19
     *
20
     * @var PublicKeyJWK
21
     */
22
    protected $_publicKey;
23
24
    /**
25
     * Private key.
26
     *
27
     * @var null|PrivateKeyJWK
28
     */
29
    protected $_privateKey;
30
31
    /**
32
     * {@inheritdoc}
33
     *
34
     * @throws \LogicException   If private key was not provided
35
     * @throws \RuntimeException For generic errors
36
     */
37 12
    public function computeSignature(string $data): string
38
    {
39
        /*
40
         * NOTE: OpenSSL uses PKCS #1 v1.5 padding by default, so no explicit
41
         * padding is required by sign and verify operations.
42
         */
43 12
        if (!isset($this->_privateKey)) {
44 1
            throw new \LogicException('Private key not set.');
45
        }
46 11
        $key = openssl_pkey_get_private($this->_privateKey->toPEM()->string());
1 ignored issue
show
Bug introduced by
The method toPEM() does not exist on null. ( Ignorable by Annotation )

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

46
        $key = openssl_pkey_get_private($this->_privateKey->/** @scrutinizer ignore-call */ toPEM()->string());

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
47 11
        if (false === $key) {
48 1
            throw new \RuntimeException(
49
                'openssl_pkey_get_private() failed: ' .
50 1
                     $this->_getLastOpenSSLError());
51
        }
52 10
        $result = @openssl_sign($data, $signature, $key, $this->_mdMethod());
53 10
        if (!$result) {
54 1
            throw new \RuntimeException(
55 1
                'openssl_sign() failed: ' . $this->_getLastOpenSSLError());
56
        }
57 9
        return $signature;
58
    }
59
60
    /**
61
     * {@inheritdoc}
62
     *
63
     * @throws \RuntimeException For generic errors
64
     */
65 10
    public function validateSignature(string $data, string $signature): bool
66
    {
67 10
        $key = openssl_pkey_get_public($this->_publicKey->toPEM()->string());
68 10
        if (false === $key) {
69 1
            throw new \RuntimeException(
70
                'openssl_pkey_get_public() failed: ' .
71 1
                     $this->_getLastOpenSSLError());
72
        }
73 9
        $result = @openssl_verify($data, $signature, $key, $this->_mdMethod());
74 9
        if (false === $result || -1 == $result) {
75 1
            throw new \RuntimeException(
76 1
                'openssl_verify() failed: ' . $this->_getLastOpenSSLError());
77
        }
78 8
        return 1 == $result;
79
    }
80
81
    /**
82
     * Get the signature algorithm identifier supported by OpenSSL.
83
     *
84
     * @return int
85
     */
86
    abstract protected function _mdMethod(): int;
87
88
    /**
89
     * Get the last OpenSSL error message.
90
     *
91
     * @return null|string
92
     */
93 4
    protected function _getLastOpenSSLError(): ?string
94
    {
95 4
        $msg = null;
96 4
        while (false !== ($err = openssl_error_string())) {
97 4
            $msg = $err;
98
        }
99 4
        return $msg;
100
    }
101
}
102