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.

ECPrivateKeyJWK::fromECPrivateKey()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 10
dl 0
loc 13
ccs 11
cts 11
cp 1
rs 9.9332
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 2
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace Sop\JWX\JWK\EC;
6
7
use Sop\CryptoEncoding\PEM;
8
use Sop\CryptoTypes\Asymmetric\EC\ECConversion;
9
use Sop\CryptoTypes\Asymmetric\EC\ECPrivateKey;
10
use Sop\CryptoTypes\Asymmetric\EC\ECPublicKey;
11
use Sop\JWX\JWK\Asymmetric\PrivateKeyJWK;
12
use Sop\JWX\JWK\Asymmetric\PublicKeyJWK;
13
use Sop\JWX\JWK\Parameter\CurveParameter;
14
use Sop\JWX\JWK\Parameter\ECCPrivateKeyParameter;
15
use Sop\JWX\JWK\Parameter\JWKParameter;
16
use Sop\JWX\JWK\Parameter\KeyTypeParameter;
17
use Sop\JWX\JWK\Parameter\XCoordinateParameter;
18
use Sop\JWX\JWK\Parameter\YCoordinateParameter;
19
20
/**
21
 * Class representing elliptic curve private key as a JWK.
22
 *
23
 * @see https://tools.ietf.org/html/rfc7517#section-4
24
 * @see https://tools.ietf.org/html/rfc7518#section-6.2
25
 * @see https://tools.ietf.org/html/rfc7518#section-6.2.2
26
 */
27
class ECPrivateKeyJWK extends PrivateKeyJWK
28
{
29
    /**
30
     * Parameter names managed by this class.
31
     *
32
     * @internal
33
     *
34
     * @var string[]
35
     */
36
    public const MANAGED_PARAMS = [
37
        JWKParameter::PARAM_KEY_TYPE,
38
        JWKParameter::PARAM_CURVE,
39
        JWKParameter::PARAM_X_COORDINATE,
40
        JWKParameter::PARAM_ECC_PRIVATE_KEY,
41
    ];
42
43
    /**
44
     * Constructor.
45
     *
46
     * @param JWKParameter ...$params
47
     *
48
     * @throws \UnexpectedValueException If missing required parameter
49
     */
50 10
    public function __construct(JWKParameter ...$params)
51
    {
52 10
        parent::__construct(...$params);
53 10
        foreach (self::MANAGED_PARAMS as $name) {
54 10
            if (!$this->has($name)) {
55 10
                throw new \UnexpectedValueException("Missing '{$name}' parameter.");
56
            }
57
        }
58 9
        if (KeyTypeParameter::TYPE_EC !== $this->keyTypeParameter()->value()) {
59 1
            throw new \UnexpectedValueException('Invalid key type.');
60
        }
61
        // cast ECC private key parameter to correct class
62 8
        $key = JWKParameter::PARAM_ECC_PRIVATE_KEY;
63 8
        $this->_parameters[$key] = new ECCPrivateKeyParameter(
64 8
            $this->_parameters[$key]->value());
65 8
    }
66
67
    /**
68
     * Initialize from ECPrivateKey.
69
     *
70
     * @throws \UnexpectedValueException
71
     */
72 4
    public static function fromECPrivateKey(ECPrivateKey $pk): self
73
    {
74 4
        if (!$pk->hasNamedCurve()) {
75 1
            throw new \UnexpectedValueException('No curve name.');
76
        }
77 3
        $curve = CurveParameter::fromOID($pk->namedCurve());
78 3
        $pubkey = $pk->publicKey();
79 3
        [$x, $y] = $pubkey->curvePointOctets();
80 3
        $xcoord = XCoordinateParameter::fromString($x);
81 3
        $ycoord = YCoordinateParameter::fromString($y);
82 3
        $priv = ECCPrivateKeyParameter::fromString($pk->privateKeyOctets());
83 3
        $key_type = new KeyTypeParameter(KeyTypeParameter::TYPE_EC);
84 3
        return new self($key_type, $curve, $xcoord, $ycoord, $priv);
85
    }
86
87
    /**
88
     * Initialize from PEM.
89
     */
90 2
    public static function fromPEM(PEM $pem): self
91
    {
92 2
        return self::fromECPrivateKey(ECPrivateKey::fromPEM($pem));
93
    }
94
95
    /**
96
     * Get the public key component of the EC private key.
97
     *
98
     * @return ECPublicKeyJWK
99
     */
100 7
    public function publicKey(): PublicKeyJWK
101
    {
102 7
        $kty = $this->keyTypeParameter();
103 7
        $curve = $this->curveParameter();
104 7
        $xcoord = $this->XCoordinateParameter();
105 7
        $ycoord = $this->YCoordinateParameter();
106 7
        return new ECPublicKeyJWK($kty, $curve, $xcoord, $ycoord);
107
    }
108
109
    /**
110
     * Convert EC private key to PEM.
111
     */
112 5
    public function toPEM(): PEM
113
    {
114 5
        $curve_oid = CurveParameter::nameToOID($this->curveParameter()->value());
115 5
        $x = ECConversion::octetsToNumber(
116 5
            $this->XCoordinateParameter()->coordinateOctets());
117 5
        $y = ECConversion::octetsToNumber(
118 5
            $this->YCoordinateParameter()->coordinateOctets());
119 5
        $pubkey = ECPublicKey::fromCoordinates($x, $y, $curve_oid);
120 5
        $priv = $this->ECCPrivateKeyParameter()->privateKeyOctets();
121 5
        $ec = new ECPrivateKey($priv, $curve_oid, $pubkey->ECPoint());
122 5
        return $ec->privateKeyInfo()->toPEM();
123
    }
124
}
125