|
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\PublicKeyInfo; |
|
9
|
|
|
use Sop\CryptoTypes\Asymmetric\RSA\RSAPublicKey; |
|
10
|
|
|
use Sop\JWX\JWK\Asymmetric\PublicKeyJWK; |
|
11
|
|
|
use Sop\JWX\JWK\Parameter\ExponentParameter; |
|
12
|
|
|
use Sop\JWX\JWK\Parameter\JWKParameter; |
|
13
|
|
|
use Sop\JWX\JWK\Parameter\KeyTypeParameter; |
|
14
|
|
|
use Sop\JWX\JWK\Parameter\ModulusParameter; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Class representing RSA public key as a JWK. |
|
18
|
|
|
* |
|
19
|
|
|
* @see https://tools.ietf.org/html/rfc7517#section-4 |
|
20
|
|
|
* @see https://tools.ietf.org/html/rfc7518#section-6.3 |
|
21
|
|
|
* @see https://tools.ietf.org/html/rfc7518#section-6.3.1 |
|
22
|
|
|
*/ |
|
23
|
|
|
class RSAPublicKeyJWK extends PublicKeyJWK |
|
24
|
|
|
{ |
|
25
|
|
|
/** |
|
26
|
|
|
* Parameter names managed by this class. |
|
27
|
|
|
* |
|
28
|
|
|
* @internal |
|
29
|
|
|
* |
|
30
|
|
|
* @var string[] |
|
31
|
|
|
*/ |
|
32
|
|
|
public const MANAGED_PARAMS = [ |
|
33
|
|
|
JWKParameter::PARAM_KEY_TYPE, |
|
34
|
|
|
JWKParameter::PARAM_MODULUS, |
|
35
|
|
|
JWKParameter::PARAM_EXPONENT, |
|
36
|
|
|
]; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Constructor. |
|
40
|
|
|
* |
|
41
|
|
|
* @param JWKParameter ...$params |
|
42
|
|
|
* |
|
43
|
|
|
* @throws \UnexpectedValueException If missing required parameter |
|
44
|
|
|
*/ |
|
45
|
36 |
|
public function __construct(JWKParameter ...$params) |
|
46
|
|
|
{ |
|
47
|
36 |
|
parent::__construct(...$params); |
|
48
|
36 |
|
foreach (self::MANAGED_PARAMS as $name) { |
|
49
|
36 |
|
if (!$this->has($name)) { |
|
50
|
2 |
|
throw new \UnexpectedValueException( |
|
51
|
36 |
|
"Missing '{$name}' parameter."); |
|
52
|
|
|
} |
|
53
|
|
|
} |
|
54
|
34 |
|
if (KeyTypeParameter::TYPE_RSA !== $this->keyTypeParameter()->value()) { |
|
55
|
1 |
|
throw new \UnexpectedValueException('Invalid key type.'); |
|
56
|
|
|
} |
|
57
|
33 |
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Initialize from RSAPublicKey. |
|
61
|
|
|
*/ |
|
62
|
4 |
|
public static function fromRSAPublicKey(RSAPublicKey $pk): self |
|
63
|
|
|
{ |
|
64
|
4 |
|
$n = ModulusParameter::fromNumber($pk->modulus()); |
|
65
|
4 |
|
$e = ExponentParameter::fromNumber($pk->publicExponent()); |
|
66
|
4 |
|
$key_type = new KeyTypeParameter(KeyTypeParameter::TYPE_RSA); |
|
67
|
4 |
|
return new self($key_type, $n, $e); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Initialize from PEM. |
|
72
|
|
|
*/ |
|
73
|
3 |
|
public static function fromPEM(PEM $pem): self |
|
74
|
|
|
{ |
|
75
|
3 |
|
return self::fromRSAPublicKey(RSAPublicKey::fromPEM($pem)); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Convert JWK to PEM. |
|
80
|
|
|
*/ |
|
81
|
16 |
|
public function toPEM(): PEM |
|
82
|
|
|
{ |
|
83
|
16 |
|
$n = $this->modulusParameter()->number()->base10(); |
|
84
|
16 |
|
$e = $this->exponentParameter()->number()->base10(); |
|
85
|
16 |
|
$pk = new RSAPublicKey($n, $e); |
|
86
|
16 |
|
return PublicKeyInfo::fromPublicKey($pk)->toPEM(); |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
|