|
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
|
|
|
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
|
|
|
* @param ECPrivateKey $pk |
|
71
|
|
|
* |
|
72
|
|
|
* @throws \UnexpectedValueException |
|
73
|
|
|
* |
|
74
|
|
|
* @return self |
|
75
|
|
|
*/ |
|
76
|
4 |
|
public static function fromECPrivateKey(ECPrivateKey $pk): self |
|
77
|
|
|
{ |
|
78
|
4 |
|
if (!$pk->hasNamedCurve()) { |
|
79
|
1 |
|
throw new \UnexpectedValueException('No curve name.'); |
|
80
|
|
|
} |
|
81
|
3 |
|
$curve = CurveParameter::fromOID($pk->namedCurve()); |
|
82
|
3 |
|
$pubkey = $pk->publicKey(); |
|
83
|
3 |
|
[$x, $y] = $pubkey->curvePointOctets(); |
|
84
|
3 |
|
$xcoord = XCoordinateParameter::fromString($x); |
|
85
|
3 |
|
$ycoord = YCoordinateParameter::fromString($y); |
|
86
|
3 |
|
$priv = ECCPrivateKeyParameter::fromString($pk->privateKeyOctets()); |
|
87
|
3 |
|
$key_type = new KeyTypeParameter(KeyTypeParameter::TYPE_EC); |
|
88
|
3 |
|
return new self($key_type, $curve, $xcoord, $ycoord, $priv); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Initialize from PEM. |
|
93
|
|
|
* |
|
94
|
|
|
* @param PEM $pem |
|
95
|
|
|
* |
|
96
|
|
|
* @return self |
|
97
|
|
|
*/ |
|
98
|
2 |
|
public static function fromPEM(PEM $pem): self |
|
99
|
|
|
{ |
|
100
|
2 |
|
return self::fromECPrivateKey(ECPrivateKey::fromPEM($pem)); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* Get the public key component of the EC private key. |
|
105
|
|
|
* |
|
106
|
|
|
* @return ECPublicKeyJWK |
|
107
|
|
|
*/ |
|
108
|
7 |
|
public function publicKey(): PublicKeyJWK |
|
109
|
|
|
{ |
|
110
|
7 |
|
$kty = $this->keyTypeParameter(); |
|
111
|
7 |
|
$curve = $this->curveParameter(); |
|
112
|
7 |
|
$xcoord = $this->XCoordinateParameter(); |
|
113
|
7 |
|
$ycoord = $this->YCoordinateParameter(); |
|
114
|
7 |
|
return new ECPublicKeyJWK($kty, $curve, $xcoord, $ycoord); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* Convert EC private key to PEM. |
|
119
|
|
|
* |
|
120
|
|
|
* @return PEM |
|
121
|
|
|
*/ |
|
122
|
5 |
|
public function toPEM(): PEM |
|
123
|
|
|
{ |
|
124
|
5 |
|
$curve_oid = CurveParameter::nameToOID($this->curveParameter()->value()); |
|
125
|
5 |
|
$x = ECConversion::octetsToNumber( |
|
126
|
5 |
|
$this->XCoordinateParameter()->coordinateOctets()); |
|
127
|
5 |
|
$y = ECConversion::octetsToNumber( |
|
128
|
5 |
|
$this->YCoordinateParameter()->coordinateOctets()); |
|
129
|
5 |
|
$pubkey = ECPublicKey::fromCoordinates($x, $y, $curve_oid); |
|
130
|
5 |
|
$priv = $this->ECCPrivateKeyParameter()->privateKeyOctets(); |
|
131
|
5 |
|
$ec = new ECPrivateKey($priv, $curve_oid, $pubkey->ECPoint()); |
|
132
|
5 |
|
return $ec->privateKeyInfo()->toPEM(); |
|
133
|
|
|
} |
|
134
|
|
|
} |
|
135
|
|
|
|