Passed
Push — master ( 35fc37...405876 )
by Andrew
01:36
created

CryptKey::getPassPhrase()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
/**
3
 * Cryptography key holder.
4
 *
5
 * @author      Julián Gutiérrez <[email protected]>
6
 * @copyright   Copyright (c) Alex Bilbie
7
 * @license     http://mit-license.org/
8
 *
9
 * @link        https://github.com/thephpleague/oauth2-server
10
 */
11
12
namespace League\OAuth2\Server;
13
14
use LogicException;
15
16
class CryptKey
17
{
18
    /** @deprecated left for backward compatibility check */
19
    const RSA_KEY_PATTERN =
20
        '/^(-----BEGIN (RSA )?(PUBLIC|PRIVATE) KEY-----)\R.*(-----END (RSA )?(PUBLIC|PRIVATE) KEY-----)\R?$/s';
21
22
    private const FILE_PREFIX = 'file://';
23
24
    /**
25
     * @var string Key contents
26
     */
27
    protected $keyContents;
28
29
    /**
30
     * @var string
31
     */
32
    protected $keyPath;
33
34
    /**
35
     * @var null|string
36
     */
37
    protected $passPhrase;
38
39
    /**
40
     * @param string      $keyPath
41
     * @param null|string $passPhrase
42
     * @param bool        $keyPermissionsCheck
43
     */
44 60
    public function __construct($keyPath, $passPhrase = null, $keyPermissionsCheck = true)
45
    {
46 60
        $this->passPhrase = $passPhrase;
47
48 60
        if (\strpos($keyPath, self::FILE_PREFIX) !== 0 && $this->isValidKey($keyPath, $this->passPhrase ?? '')) {
49 3
            $this->keyContents = $keyPath;
50 3
            $this->keyPath = '';
51
            // There's no file, so no need for permission check.
52 3
            $keyPermissionsCheck = false;
53 57
        } elseif (\is_file($keyPath)) {
54 55
            if (\strpos($keyPath, self::FILE_PREFIX) !== 0) {
55 1
                $keyPath = self::FILE_PREFIX . $keyPath;
56
            }
57
58 55
            if (!\is_readable($keyPath)) {
59
                throw new LogicException(\sprintf('Key path "%s" does not exist or is not readable', $keyPath));
60
            }
61 55
            $this->keyContents = \file_get_contents($keyPath);
62 55
            $this->keyPath = $keyPath;
63 55
            if (!$this->isValidKey($this->keyContents, $this->passPhrase ?? '')) {
64 55
                throw new LogicException('Unable to read key from file ' . $keyPath);
65
            }
66
        } else {
67 2
            throw new LogicException('Unable to read key from file ' . $keyPath);
68
        }
69
70 58
        if ($keyPermissionsCheck === true) {
71
            // Verify the permissions of the key
72 55
            $keyPathPerms = \decoct(\fileperms($this->keyPath) & 0777);
73 55
            if (\in_array($keyPathPerms, ['400', '440', '600', '640', '660'], true) === false) {
74
                \trigger_error(
75
                    \sprintf(
76
                        'Key file "%s" permissions are not correct, recommend changing to 600 or 660 instead of %s',
77
                        $this->keyPath,
78
                        $keyPathPerms
79
                    ),
80
                    E_USER_NOTICE
81
                );
82
            }
83
        }
84 58
    }
85
86
    /**
87
     * Get key contents
88
     *
89
     * @return string Key contents
90
     */
91 16
    public function getKeyContents(): string
92
    {
93 16
        return $this->keyContents;
94
    }
95
96
    /**
97
     * Validate key contents.
98
     *
99
     * @param string $contents
100
     * @param string $passPhrase
101
     *
102
     * @return bool
103
     */
104 60
    private function isValidKey($contents, $passPhrase)
105
    {
106 60
        $pkey = \openssl_pkey_get_private($contents, $passPhrase) ?: \openssl_pkey_get_public($contents);
107 60
        if ($pkey === false) {
0 ignored issues
show
introduced by
The condition $pkey === false is always false.
Loading history...
108 2
            return false;
109
        }
110 59
        $details = \openssl_pkey_get_details($pkey);
111
112 59
        return $details !== false && \in_array(
113 59
            $details['type'] ?? -1,
114 59
            [OPENSSL_KEYTYPE_RSA, OPENSSL_KEYTYPE_EC],
115 59
            true
116
        );
117
    }
118
119
    /**
120
     * Retrieve key path.
121
     *
122
     * @return string
123
     */
124 5
    public function getKeyPath()
125
    {
126 5
        return $this->keyPath;
127
    }
128
129
    /**
130
     * Retrieve key pass phrase.
131
     *
132
     * @return null|string
133
     */
134 18
    public function getPassPhrase()
135
    {
136 18
        return $this->passPhrase;
137
    }
138
}
139