|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types = 1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Sop\JWX\JWK\Asymmetric; |
|
6
|
|
|
|
|
7
|
|
|
use Sop\CryptoEncoding\PEM; |
|
8
|
|
|
use Sop\CryptoTypes\Asymmetric\EC\ECPrivateKey; |
|
9
|
|
|
use Sop\CryptoTypes\Asymmetric\PrivateKey; |
|
10
|
|
|
use Sop\CryptoTypes\Asymmetric\PrivateKeyInfo; |
|
11
|
|
|
use Sop\CryptoTypes\Asymmetric\RSA\RSAPrivateKey; |
|
12
|
|
|
use Sop\JWX\JWK\EC\ECPrivateKeyJWK; |
|
13
|
|
|
use Sop\JWX\JWK\JWK; |
|
14
|
|
|
use Sop\JWX\JWK\RSA\RSAPrivateKeyJWK; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Base class for JWK private keys of an asymmetric key pairs. |
|
18
|
|
|
*/ |
|
19
|
|
|
abstract class PrivateKeyJWK extends JWK |
|
20
|
|
|
{ |
|
21
|
|
|
/** |
|
22
|
|
|
* Get the public key component of the asymmetric key pair. |
|
23
|
|
|
* |
|
24
|
|
|
* @return PublicKeyJWK |
|
25
|
|
|
*/ |
|
26
|
|
|
abstract public function publicKey(): PublicKeyJWK; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Convert private key to PEM. |
|
30
|
|
|
* |
|
31
|
|
|
* @return PEM |
|
32
|
|
|
*/ |
|
33
|
|
|
abstract public function toPEM(): PEM; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Initialize from a PrivateKey object. |
|
37
|
|
|
* |
|
38
|
|
|
* @param PrivateKey $priv_key Private key |
|
39
|
|
|
* |
|
40
|
|
|
* @throws \UnexpectedValueException |
|
41
|
|
|
* |
|
42
|
|
|
* @return self |
|
43
|
|
|
*/ |
|
44
|
3 |
|
public static function fromPrivateKey(PrivateKey $priv_key): PrivateKeyJWK |
|
45
|
|
|
{ |
|
46
|
3 |
|
if ($priv_key instanceof RSAPrivateKey) { |
|
47
|
1 |
|
return RSAPrivateKeyJWK::fromRSAPrivateKey($priv_key); |
|
48
|
|
|
} |
|
49
|
2 |
|
if ($priv_key instanceof ECPrivateKey) { |
|
50
|
1 |
|
return ECPrivateKeyJWK::fromECPrivateKey($priv_key); |
|
51
|
|
|
} |
|
52
|
1 |
|
throw new \UnexpectedValueException('Unsupported private key.'); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Initialize from a PrivateKeyInfo object. |
|
57
|
|
|
* |
|
58
|
|
|
* @param PrivateKeyInfo $pki PrivateKeyInfo |
|
59
|
|
|
* |
|
60
|
|
|
* @return self |
|
61
|
|
|
*/ |
|
62
|
2 |
|
public static function fromPrivateKeyInfo(PrivateKeyInfo $pki): PrivateKeyJWK |
|
63
|
|
|
{ |
|
64
|
2 |
|
return self::fromPrivateKey($pki->privateKey()); |
|
65
|
|
|
} |
|
66
|
|
|
} |
|
67
|
|
|
|