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

RSAPrivateKeyJWK::toPEM()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 29
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 27
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 29
ccs 27
cts 27
cp 1
rs 8.8571
c 0
b 0
f 0
cc 1
eloc 27
nc 1
nop 0
crap 1

1 Method

Rating   Name   Duplication   Size   Complexity  
A 0 12 1
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace Sop\JWX\JWK\RSA;
6
7
use Sop\CryptoEncoding\PEM;
8
use Sop\CryptoTypes\Asymmetric\PrivateKeyInfo;
9
use Sop\CryptoTypes\Asymmetric\RSA\RSAPrivateKey;
10
use Sop\JWX\JWK\Asymmetric\PrivateKeyJWK;
11
use Sop\JWX\JWK\Asymmetric\PublicKeyJWK;
12
use Sop\JWX\JWK\Parameter\ExponentParameter;
13
use Sop\JWX\JWK\Parameter\FirstCRTCoefficientParameter;
14
use Sop\JWX\JWK\Parameter\FirstFactorCRTExponentParameter;
15
use Sop\JWX\JWK\Parameter\FirstPrimeFactorParameter;
16
use Sop\JWX\JWK\Parameter\JWKParameter;
17
use Sop\JWX\JWK\Parameter\KeyTypeParameter;
18
use Sop\JWX\JWK\Parameter\ModulusParameter;
19
use Sop\JWX\JWK\Parameter\PrivateExponentParameter;
20
use Sop\JWX\JWK\Parameter\SecondFactorCRTExponentParameter;
21
use Sop\JWX\JWK\Parameter\SecondPrimeFactorParameter;
22
23
/**
24
 * Class representing RSA private key as a JWK.
25
 *
26
 * @see https://tools.ietf.org/html/rfc7517#section-4
27
 * @see https://tools.ietf.org/html/rfc7518#section-6.3
28
 * @see https://tools.ietf.org/html/rfc7518#section-6.3.2
29
 */
30
class RSAPrivateKeyJWK extends PrivateKeyJWK
31
{
32
    /**
33
     * Parameter names managed by this class.
34
     *
35
     * @internal
36
     *
37
     * @var string[]
38
     */
39
    const MANAGED_PARAMS = [
40
        JWKParameter::PARAM_KEY_TYPE,
41
        JWKParameter::PARAM_MODULUS,
42
        JWKParameter::PARAM_EXPONENT,
43
        JWKParameter::PARAM_PRIVATE_EXPONENT,
44
        JWKParameter::PARAM_FIRST_PRIME_FACTOR,
45
        JWKParameter::PARAM_SECOND_PRIME_FACTOR,
46
        JWKParameter::PARAM_FIRST_FACTOR_CRT_EXPONENT,
47
        JWKParameter::PARAM_SECOND_FACTOR_CRT_EXPONENT,
48
        JWKParameter::PARAM_FIRST_CRT_COEFFICIENT,
49
    ];
50
51
    /**
52
     * Constructor.
53
     *
54
     * @param JWKParameter ...$params
55
     *
56
     * @throws \UnexpectedValueException If missing required parameter
57
     */
58 13
    public function __construct(JWKParameter ...$params)
59
    {
60 13
        parent::__construct(...$params);
61 13
        foreach (self::MANAGED_PARAMS as $name) {
62 13
            if (!$this->has($name)) {
63 1
                throw new \UnexpectedValueException(
64 13
                    "Missing '{$name}' parameter.");
65
            }
66
        }
67 12
        if (KeyTypeParameter::TYPE_RSA !== $this->keyTypeParameter()->value()) {
68 1
            throw new \UnexpectedValueException('Invalid key type.');
69
        }
70
        // cast private exponent to correct class
71 11
        $key = JWKParameter::PARAM_PRIVATE_EXPONENT;
72 11
        $this->_parameters[$key] = new PrivateExponentParameter(
73 11
            $this->_parameters[$key]->value());
74 11
    }
75
76
    /**
77
     * Initialize from RSAPrivateKey.
78
     *
79
     * @param RSAPrivateKey $pk
80
     *
81
     * @return self
82
     */
83 3
    public static function fromRSAPrivateKey(RSAPrivateKey $pk): self
84
    {
85 3
        $n = ModulusParameter::fromNumber($pk->modulus());
86 3
        $e = ExponentParameter::fromNumber($pk->publicExponent());
87 3
        $d = PrivateExponentParameter::fromNumber($pk->privateExponent());
88 3
        $p = FirstPrimeFactorParameter::fromNumber($pk->prime1());
89 3
        $q = SecondPrimeFactorParameter::fromNumber($pk->prime2());
90 3
        $dp = FirstFactorCRTExponentParameter::fromNumber($pk->exponent1());
91 3
        $dq = SecondFactorCRTExponentParameter::fromNumber($pk->exponent2());
92 3
        $qi = FirstCRTCoefficientParameter::fromNumber($pk->coefficient());
93 3
        $key_type = new KeyTypeParameter(KeyTypeParameter::TYPE_RSA);
94 3
        return new self($key_type, $n, $e, $d, $p, $q, $dp, $dq, $qi);
95
    }
96
97
    /**
98
     * Initialize from PEM.
99
     *
100
     * @param PEM $pem
101
     *
102
     * @return self
103
     */
104 1
    public static function fromPEM(PEM $pem): self
105
    {
106 1
        return self::fromRSAPrivateKey(RSAPrivateKey::fromPEM($pem));
107
    }
108
109
    /**
110
     * Get public key component.
111
     *
112
     * @return RSAPublicKeyJWK
113
     */
114 26
    public function publicKey(): PublicKeyJWK
115
    {
116 26
        $kty = $this->keyTypeParameter();
117 26
        $n = $this->modulusParameter();
118 26
        $e = $this->exponentParameter();
119 26
        return new RSAPublicKeyJWK($kty, $n, $e);
120
    }
121
122
    /**
123
     * Convert JWK to PEM.
124
     *
125
     * @return PEM
126
     */
127 16
    public function toPEM(): PEM
128
    {
129 16
        $n = $this->modulusParameter()->number()->base10();
130 16
        $e = $this->exponentParameter()->number()->base10();
131 16
        $d = $this->privateExponentParameter()->number()->base10();
132 16
        $p = $this->firstPrimeFactorParameter()->number()->base10();
133 16
        $q = $this->secondPrimeFactorParameter()->number()->base10();
134 16
        $dp = $this->firstFactorCRTExponentParameter()->number()->base10();
135 16
        $dq = $this->secondFactorCRTExponentParameter()->number()->base10();
136 16
        $qi = $this->firstCRTCoefficientParameter()->number()->base10();
137 16
        $pk = new RSAPrivateKey($n, $e, $d, $p, $q, $dp, $dq, $qi);
138 16
        return PrivateKeyInfo::fromPrivateKey($pk)->toPEM();
139
    }
140
}
141