@@ -11,21 +11,21 @@ |
||
11 | 11 | */ |
12 | 12 | class RSAESOAEPAlgorithm extends RSAESKeyAlgorithm |
13 | 13 | { |
14 | - /** |
|
15 | - * |
|
16 | - * {@inheritdoc} |
|
17 | - */ |
|
18 | - protected function _paddingScheme() |
|
19 | - { |
|
20 | - return OPENSSL_PKCS1_OAEP_PADDING; |
|
21 | - } |
|
14 | + /** |
|
15 | + * |
|
16 | + * {@inheritdoc} |
|
17 | + */ |
|
18 | + protected function _paddingScheme() |
|
19 | + { |
|
20 | + return OPENSSL_PKCS1_OAEP_PADDING; |
|
21 | + } |
|
22 | 22 | |
23 | - /** |
|
24 | - * |
|
25 | - * {@inheritdoc} |
|
26 | - */ |
|
27 | - public function algorithmParamValue() |
|
28 | - { |
|
29 | - return JWA::ALGO_RSA_OAEP; |
|
30 | - } |
|
23 | + /** |
|
24 | + * |
|
25 | + * {@inheritdoc} |
|
26 | + */ |
|
27 | + public function algorithmParamValue() |
|
28 | + { |
|
29 | + return JWA::ALGO_RSA_OAEP; |
|
30 | + } |
|
31 | 31 | } |
@@ -9,43 +9,43 @@ |
||
9 | 9 | * Interface for algorithms that may be used to encrypt and decrypt JWE payload. |
10 | 10 | */ |
11 | 11 | interface ContentEncryptionAlgorithm extends |
12 | - EncryptionAlgorithmParameterValue, |
|
13 | - HeaderParameters |
|
12 | + EncryptionAlgorithmParameterValue, |
|
13 | + HeaderParameters |
|
14 | 14 | { |
15 | - /** |
|
16 | - * Encrypt plaintext. |
|
17 | - * |
|
18 | - * @param string $plaintext Data to encrypt |
|
19 | - * @param string $key Encryption key |
|
20 | - * @param string $iv Initialization vector |
|
21 | - * @param string $aad Additional authenticated data |
|
22 | - * @return array Tuple of ciphertext and authentication tag |
|
23 | - */ |
|
24 | - public function encrypt($plaintext, $key, $iv, $aad); |
|
15 | + /** |
|
16 | + * Encrypt plaintext. |
|
17 | + * |
|
18 | + * @param string $plaintext Data to encrypt |
|
19 | + * @param string $key Encryption key |
|
20 | + * @param string $iv Initialization vector |
|
21 | + * @param string $aad Additional authenticated data |
|
22 | + * @return array Tuple of ciphertext and authentication tag |
|
23 | + */ |
|
24 | + public function encrypt($plaintext, $key, $iv, $aad); |
|
25 | 25 | |
26 | - /** |
|
27 | - * Decrypt ciphertext. |
|
28 | - * |
|
29 | - * @param string $ciphertext Data to decrypt |
|
30 | - * @param string $key Encryption key |
|
31 | - * @param string $iv Initialization vector |
|
32 | - * @param string $aad Additional authenticated data |
|
33 | - * @param string $auth_tag Authentication tag to compare |
|
34 | - * @return string Plaintext |
|
35 | - */ |
|
36 | - public function decrypt($ciphertext, $key, $iv, $aad, $auth_tag); |
|
26 | + /** |
|
27 | + * Decrypt ciphertext. |
|
28 | + * |
|
29 | + * @param string $ciphertext Data to decrypt |
|
30 | + * @param string $key Encryption key |
|
31 | + * @param string $iv Initialization vector |
|
32 | + * @param string $aad Additional authenticated data |
|
33 | + * @param string $auth_tag Authentication tag to compare |
|
34 | + * @return string Plaintext |
|
35 | + */ |
|
36 | + public function decrypt($ciphertext, $key, $iv, $aad, $auth_tag); |
|
37 | 37 | |
38 | - /** |
|
39 | - * Get the required key size in bytes. |
|
40 | - * |
|
41 | - * @return int |
|
42 | - */ |
|
43 | - public function keySize(); |
|
38 | + /** |
|
39 | + * Get the required key size in bytes. |
|
40 | + * |
|
41 | + * @return int |
|
42 | + */ |
|
43 | + public function keySize(); |
|
44 | 44 | |
45 | - /** |
|
46 | - * Get the required IV size in bytes. |
|
47 | - * |
|
48 | - * @return int |
|
49 | - */ |
|
50 | - public function ivSize(); |
|
45 | + /** |
|
46 | + * Get the required IV size in bytes. |
|
47 | + * |
|
48 | + * @return int |
|
49 | + */ |
|
50 | + public function ivSize(); |
|
51 | 51 | } |
@@ -14,122 +14,122 @@ |
||
14 | 14 | * algorithms. |
15 | 15 | */ |
16 | 16 | abstract class KeyManagementAlgorithm implements |
17 | - AlgorithmParameterValue, |
|
18 | - HeaderParameters |
|
17 | + AlgorithmParameterValue, |
|
18 | + HeaderParameters |
|
19 | 19 | { |
20 | - /** |
|
21 | - * ID of the key used by the algorithm. |
|
22 | - * |
|
23 | - * If set, KeyID parameter shall be automatically inserted into JWE's |
|
24 | - * header. |
|
25 | - * |
|
26 | - * @var string|null $_keyID |
|
27 | - */ |
|
28 | - protected $_keyID; |
|
20 | + /** |
|
21 | + * ID of the key used by the algorithm. |
|
22 | + * |
|
23 | + * If set, KeyID parameter shall be automatically inserted into JWE's |
|
24 | + * header. |
|
25 | + * |
|
26 | + * @var string|null $_keyID |
|
27 | + */ |
|
28 | + protected $_keyID; |
|
29 | 29 | |
30 | - /** |
|
31 | - * Encrypt a key. |
|
32 | - * |
|
33 | - * @param string $key Key to be encrypted |
|
34 | - * @param Header $header Reference to the Header variable, that shall |
|
35 | - * be updated to contain parameters specific to the encryption |
|
36 | - * @return string Ciphertext |
|
37 | - */ |
|
38 | - abstract protected function _encryptKey($key, Header &$header); |
|
30 | + /** |
|
31 | + * Encrypt a key. |
|
32 | + * |
|
33 | + * @param string $key Key to be encrypted |
|
34 | + * @param Header $header Reference to the Header variable, that shall |
|
35 | + * be updated to contain parameters specific to the encryption |
|
36 | + * @return string Ciphertext |
|
37 | + */ |
|
38 | + abstract protected function _encryptKey($key, Header &$header); |
|
39 | 39 | |
40 | - /** |
|
41 | - * Decrypt a key. |
|
42 | - * |
|
43 | - * @param string $ciphertext Ciphertext of the encrypted key |
|
44 | - * @param Header $header Header possibly containing encoding specific |
|
45 | - * parameters |
|
46 | - * @return string Plaintext key |
|
47 | - */ |
|
48 | - abstract protected function _decryptKey($ciphertext, Header $header); |
|
40 | + /** |
|
41 | + * Decrypt a key. |
|
42 | + * |
|
43 | + * @param string $ciphertext Ciphertext of the encrypted key |
|
44 | + * @param Header $header Header possibly containing encoding specific |
|
45 | + * parameters |
|
46 | + * @return string Plaintext key |
|
47 | + */ |
|
48 | + abstract protected function _decryptKey($ciphertext, Header $header); |
|
49 | 49 | |
50 | - /** |
|
51 | - * Encrypt a key to be inserted into JWE header. |
|
52 | - * |
|
53 | - * @param string $cek Content encryption key |
|
54 | - * @param Header|null $header Optional reference to the Header variable, |
|
55 | - * which may be updated to contain parameters specific to this |
|
56 | - * encrypt invocation. If the variable is referenced, but is a null, |
|
57 | - * it shall be initialized to an empty Header. |
|
58 | - * @throws \RuntimeException For generic errors |
|
59 | - * @return string Encrypted key |
|
60 | - */ |
|
61 | - final public function encrypt($cek, Header &$header = null) |
|
62 | - { |
|
63 | - if (!isset($header)) { |
|
64 | - $header = new Header(); |
|
65 | - } |
|
66 | - return $this->_encryptKey($cek, $header); |
|
67 | - } |
|
50 | + /** |
|
51 | + * Encrypt a key to be inserted into JWE header. |
|
52 | + * |
|
53 | + * @param string $cek Content encryption key |
|
54 | + * @param Header|null $header Optional reference to the Header variable, |
|
55 | + * which may be updated to contain parameters specific to this |
|
56 | + * encrypt invocation. If the variable is referenced, but is a null, |
|
57 | + * it shall be initialized to an empty Header. |
|
58 | + * @throws \RuntimeException For generic errors |
|
59 | + * @return string Encrypted key |
|
60 | + */ |
|
61 | + final public function encrypt($cek, Header &$header = null) |
|
62 | + { |
|
63 | + if (!isset($header)) { |
|
64 | + $header = new Header(); |
|
65 | + } |
|
66 | + return $this->_encryptKey($cek, $header); |
|
67 | + } |
|
68 | 68 | |
69 | - /** |
|
70 | - * Decrypt a CEK from the encrypted data. |
|
71 | - * |
|
72 | - * @param string $data Encrypted key |
|
73 | - * @param Header|null Optional header containing parameters required to |
|
74 | - * decrypt the key. |
|
75 | - * @throws \RuntimeException For generic errors |
|
76 | - * @return string Content encryption key |
|
77 | - */ |
|
78 | - final public function decrypt($data, Header $header = null) |
|
79 | - { |
|
80 | - if (!isset($header)) { |
|
81 | - $header = new Header(); |
|
82 | - } |
|
83 | - return $this->_decryptKey($data, $header); |
|
84 | - } |
|
69 | + /** |
|
70 | + * Decrypt a CEK from the encrypted data. |
|
71 | + * |
|
72 | + * @param string $data Encrypted key |
|
73 | + * @param Header|null Optional header containing parameters required to |
|
74 | + * decrypt the key. |
|
75 | + * @throws \RuntimeException For generic errors |
|
76 | + * @return string Content encryption key |
|
77 | + */ |
|
78 | + final public function decrypt($data, Header $header = null) |
|
79 | + { |
|
80 | + if (!isset($header)) { |
|
81 | + $header = new Header(); |
|
82 | + } |
|
83 | + return $this->_decryptKey($data, $header); |
|
84 | + } |
|
85 | 85 | |
86 | - /** |
|
87 | - * Get content encryption key for the encryption. |
|
88 | - * |
|
89 | - * Returned key may be random depending on the key management algorithm. |
|
90 | - * |
|
91 | - * @param int $length Required key size in bytes |
|
92 | - * @return string |
|
93 | - */ |
|
94 | - abstract public function cekForEncryption($length); |
|
86 | + /** |
|
87 | + * Get content encryption key for the encryption. |
|
88 | + * |
|
89 | + * Returned key may be random depending on the key management algorithm. |
|
90 | + * |
|
91 | + * @param int $length Required key size in bytes |
|
92 | + * @return string |
|
93 | + */ |
|
94 | + abstract public function cekForEncryption($length); |
|
95 | 95 | |
96 | - /** |
|
97 | - * Initialize key management algorithm from a JWK and a header. |
|
98 | - * |
|
99 | - * @param JWK $jwk |
|
100 | - * @param Header $header |
|
101 | - * @return KeyManagementAlgorithm |
|
102 | - */ |
|
103 | - public static function fromJWK(JWK $jwk, Header $header) |
|
104 | - { |
|
105 | - $factory = new KeyAlgorithmFactory($header); |
|
106 | - return $factory->algoByKey($jwk); |
|
107 | - } |
|
96 | + /** |
|
97 | + * Initialize key management algorithm from a JWK and a header. |
|
98 | + * |
|
99 | + * @param JWK $jwk |
|
100 | + * @param Header $header |
|
101 | + * @return KeyManagementAlgorithm |
|
102 | + */ |
|
103 | + public static function fromJWK(JWK $jwk, Header $header) |
|
104 | + { |
|
105 | + $factory = new KeyAlgorithmFactory($header); |
|
106 | + return $factory->algoByKey($jwk); |
|
107 | + } |
|
108 | 108 | |
109 | - /** |
|
110 | - * Get self with key ID. |
|
111 | - * |
|
112 | - * @param string|null $id Key ID or null to remove |
|
113 | - * @return self |
|
114 | - */ |
|
115 | - public function withKeyID($id) |
|
116 | - { |
|
117 | - $obj = clone $this; |
|
118 | - $obj->_keyID = $id; |
|
119 | - return $obj; |
|
120 | - } |
|
109 | + /** |
|
110 | + * Get self with key ID. |
|
111 | + * |
|
112 | + * @param string|null $id Key ID or null to remove |
|
113 | + * @return self |
|
114 | + */ |
|
115 | + public function withKeyID($id) |
|
116 | + { |
|
117 | + $obj = clone $this; |
|
118 | + $obj->_keyID = $id; |
|
119 | + return $obj; |
|
120 | + } |
|
121 | 121 | |
122 | - /** |
|
123 | - * |
|
124 | - * @see \JWX\JWT\Header\HeaderParameters::headerParameters() |
|
125 | - * @return \JWX\JWT\Parameter\JWTParameter[] |
|
126 | - */ |
|
127 | - public function headerParameters() |
|
128 | - { |
|
129 | - $params = array(); |
|
130 | - if (isset($this->_keyID)) { |
|
131 | - $params[] = new KeyIDParameter($this->_keyID); |
|
132 | - } |
|
133 | - return $params; |
|
134 | - } |
|
122 | + /** |
|
123 | + * |
|
124 | + * @see \JWX\JWT\Header\HeaderParameters::headerParameters() |
|
125 | + * @return \JWX\JWT\Parameter\JWTParameter[] |
|
126 | + */ |
|
127 | + public function headerParameters() |
|
128 | + { |
|
129 | + $params = array(); |
|
130 | + if (isset($this->_keyID)) { |
|
131 | + $params[] = new KeyIDParameter($this->_keyID); |
|
132 | + } |
|
133 | + return $params; |
|
134 | + } |
|
135 | 135 | } |
@@ -35,7 +35,7 @@ discard block |
||
35 | 35 | * be updated to contain parameters specific to the encryption |
36 | 36 | * @return string Ciphertext |
37 | 37 | */ |
38 | - abstract protected function _encryptKey($key, Header &$header); |
|
38 | + abstract protected function _encryptKey($key, Header&$header); |
|
39 | 39 | |
40 | 40 | /** |
41 | 41 | * Decrypt a key. |
@@ -58,7 +58,7 @@ discard block |
||
58 | 58 | * @throws \RuntimeException For generic errors |
59 | 59 | * @return string Encrypted key |
60 | 60 | */ |
61 | - final public function encrypt($cek, Header &$header = null) |
|
61 | + final public function encrypt($cek, Header&$header = null) |
|
62 | 62 | { |
63 | 63 | if (!isset($header)) { |
64 | 64 | $header = new Header(); |
@@ -20,79 +20,79 @@ |
||
20 | 20 | */ |
21 | 21 | class RSAPublicKeyJWK extends PublicKeyJWK |
22 | 22 | { |
23 | - /** |
|
24 | - * Parameter names managed by this class. |
|
25 | - * |
|
26 | - * @internal |
|
27 | - * |
|
28 | - * @var string[] |
|
29 | - */ |
|
30 | - const MANAGED_PARAMS = array( |
|
31 | - /* @formatter:off */ |
|
32 | - JWKParameter::PARAM_KEY_TYPE, |
|
33 | - JWKParameter::PARAM_MODULUS, |
|
34 | - JWKParameter::PARAM_EXPONENT |
|
35 | - /* @formatter:on */ |
|
36 | - ); |
|
23 | + /** |
|
24 | + * Parameter names managed by this class. |
|
25 | + * |
|
26 | + * @internal |
|
27 | + * |
|
28 | + * @var string[] |
|
29 | + */ |
|
30 | + const MANAGED_PARAMS = array( |
|
31 | + /* @formatter:off */ |
|
32 | + JWKParameter::PARAM_KEY_TYPE, |
|
33 | + JWKParameter::PARAM_MODULUS, |
|
34 | + JWKParameter::PARAM_EXPONENT |
|
35 | + /* @formatter:on */ |
|
36 | + ); |
|
37 | 37 | |
38 | - /** |
|
39 | - * Constructor. |
|
40 | - * |
|
41 | - * @param JWKParameter ...$params |
|
42 | - * @throws \UnexpectedValueException If missing required parameter |
|
43 | - */ |
|
44 | - public function __construct(JWKParameter ...$params) |
|
45 | - { |
|
46 | - parent::__construct(...$params); |
|
47 | - foreach (self::MANAGED_PARAMS as $name) { |
|
48 | - if (!$this->has($name)) { |
|
49 | - throw new \UnexpectedValueException("Missing '$name' parameter."); |
|
50 | - } |
|
51 | - } |
|
52 | - if ($this->keyTypeParameter()->value() != KeyTypeParameter::TYPE_RSA) { |
|
53 | - throw new \UnexpectedValueException("Invalid key type."); |
|
54 | - } |
|
55 | - } |
|
38 | + /** |
|
39 | + * Constructor. |
|
40 | + * |
|
41 | + * @param JWKParameter ...$params |
|
42 | + * @throws \UnexpectedValueException If missing required parameter |
|
43 | + */ |
|
44 | + public function __construct(JWKParameter ...$params) |
|
45 | + { |
|
46 | + parent::__construct(...$params); |
|
47 | + foreach (self::MANAGED_PARAMS as $name) { |
|
48 | + if (!$this->has($name)) { |
|
49 | + throw new \UnexpectedValueException("Missing '$name' parameter."); |
|
50 | + } |
|
51 | + } |
|
52 | + if ($this->keyTypeParameter()->value() != KeyTypeParameter::TYPE_RSA) { |
|
53 | + throw new \UnexpectedValueException("Invalid key type."); |
|
54 | + } |
|
55 | + } |
|
56 | 56 | |
57 | - /** |
|
58 | - * Initialize from RSAPublicKey. |
|
59 | - * |
|
60 | - * @param RSAPublicKey $pk |
|
61 | - * @return self |
|
62 | - */ |
|
63 | - public static function fromRSAPublicKey(RSAPublicKey $pk) |
|
64 | - { |
|
65 | - $n = ModulusParameter::fromNumber($pk->modulus()); |
|
66 | - $e = ExponentParameter::fromNumber($pk->publicExponent()); |
|
67 | - $key_type = new KeyTypeParameter(KeyTypeParameter::TYPE_RSA); |
|
68 | - return new self($key_type, $n, $e); |
|
69 | - } |
|
57 | + /** |
|
58 | + * Initialize from RSAPublicKey. |
|
59 | + * |
|
60 | + * @param RSAPublicKey $pk |
|
61 | + * @return self |
|
62 | + */ |
|
63 | + public static function fromRSAPublicKey(RSAPublicKey $pk) |
|
64 | + { |
|
65 | + $n = ModulusParameter::fromNumber($pk->modulus()); |
|
66 | + $e = ExponentParameter::fromNumber($pk->publicExponent()); |
|
67 | + $key_type = new KeyTypeParameter(KeyTypeParameter::TYPE_RSA); |
|
68 | + return new self($key_type, $n, $e); |
|
69 | + } |
|
70 | 70 | |
71 | - /** |
|
72 | - * Initialize from PEM. |
|
73 | - * |
|
74 | - * @param PEM $pem |
|
75 | - * @return self |
|
76 | - */ |
|
77 | - public static function fromPEM(PEM $pem) |
|
78 | - { |
|
79 | - return self::fromRSAPublicKey(RSAPublicKey::fromPEM($pem)); |
|
80 | - } |
|
71 | + /** |
|
72 | + * Initialize from PEM. |
|
73 | + * |
|
74 | + * @param PEM $pem |
|
75 | + * @return self |
|
76 | + */ |
|
77 | + public static function fromPEM(PEM $pem) |
|
78 | + { |
|
79 | + return self::fromRSAPublicKey(RSAPublicKey::fromPEM($pem)); |
|
80 | + } |
|
81 | 81 | |
82 | - /** |
|
83 | - * Convert JWK to PEM. |
|
84 | - * |
|
85 | - * @return PEM |
|
86 | - */ |
|
87 | - public function toPEM() |
|
88 | - { |
|
89 | - $n = $this->modulusParameter() |
|
90 | - ->number() |
|
91 | - ->base10(); |
|
92 | - $e = $this->exponentParameter() |
|
93 | - ->number() |
|
94 | - ->base10(); |
|
95 | - $pk = new RSAPublicKey($n, $e); |
|
96 | - return PublicKeyInfo::fromPublicKey($pk)->toPEM(); |
|
97 | - } |
|
82 | + /** |
|
83 | + * Convert JWK to PEM. |
|
84 | + * |
|
85 | + * @return PEM |
|
86 | + */ |
|
87 | + public function toPEM() |
|
88 | + { |
|
89 | + $n = $this->modulusParameter() |
|
90 | + ->number() |
|
91 | + ->base10(); |
|
92 | + $e = $this->exponentParameter() |
|
93 | + ->number() |
|
94 | + ->base10(); |
|
95 | + $pk = new RSAPublicKey($n, $e); |
|
96 | + return PublicKeyInfo::fromPublicKey($pk)->toPEM(); |
|
97 | + } |
|
98 | 98 | } |
@@ -26,126 +26,126 @@ |
||
26 | 26 | */ |
27 | 27 | class RSAPrivateKeyJWK extends PrivateKeyJWK |
28 | 28 | { |
29 | - /** |
|
30 | - * Parameter names managed by this class. |
|
31 | - * |
|
32 | - * @internal |
|
33 | - * |
|
34 | - * @var string[] |
|
35 | - */ |
|
36 | - const MANAGED_PARAMS = array( |
|
37 | - /* @formatter:off */ |
|
38 | - JWKParameter::PARAM_KEY_TYPE, |
|
39 | - JWKParameter::PARAM_MODULUS, |
|
40 | - JWKParameter::PARAM_EXPONENT, |
|
41 | - JWKParameter::PARAM_PRIVATE_EXPONENT, |
|
42 | - JWKParameter::PARAM_FIRST_PRIME_FACTOR, |
|
43 | - JWKParameter::PARAM_SECOND_PRIME_FACTOR, |
|
44 | - JWKParameter::PARAM_FIRST_FACTOR_CRT_EXPONENT, |
|
45 | - JWKParameter::PARAM_SECOND_FACTOR_CRT_EXPONENT, |
|
46 | - JWKParameter::PARAM_FIRST_CRT_COEFFICIENT |
|
47 | - /* @formatter:on */ |
|
48 | - ); |
|
29 | + /** |
|
30 | + * Parameter names managed by this class. |
|
31 | + * |
|
32 | + * @internal |
|
33 | + * |
|
34 | + * @var string[] |
|
35 | + */ |
|
36 | + const MANAGED_PARAMS = array( |
|
37 | + /* @formatter:off */ |
|
38 | + JWKParameter::PARAM_KEY_TYPE, |
|
39 | + JWKParameter::PARAM_MODULUS, |
|
40 | + JWKParameter::PARAM_EXPONENT, |
|
41 | + JWKParameter::PARAM_PRIVATE_EXPONENT, |
|
42 | + JWKParameter::PARAM_FIRST_PRIME_FACTOR, |
|
43 | + JWKParameter::PARAM_SECOND_PRIME_FACTOR, |
|
44 | + JWKParameter::PARAM_FIRST_FACTOR_CRT_EXPONENT, |
|
45 | + JWKParameter::PARAM_SECOND_FACTOR_CRT_EXPONENT, |
|
46 | + JWKParameter::PARAM_FIRST_CRT_COEFFICIENT |
|
47 | + /* @formatter:on */ |
|
48 | + ); |
|
49 | 49 | |
50 | - /** |
|
51 | - * Constructor. |
|
52 | - * |
|
53 | - * @param JWKParameter ...$params |
|
54 | - * @throws \UnexpectedValueException If missing required parameter |
|
55 | - */ |
|
56 | - public function __construct(JWKParameter ...$params) |
|
57 | - { |
|
58 | - parent::__construct(...$params); |
|
59 | - foreach (self::MANAGED_PARAMS as $name) { |
|
60 | - if (!$this->has($name)) { |
|
61 | - throw new \UnexpectedValueException("Missing '$name' parameter."); |
|
62 | - } |
|
63 | - } |
|
64 | - if ($this->keyTypeParameter()->value() != KeyTypeParameter::TYPE_RSA) { |
|
65 | - throw new \UnexpectedValueException("Invalid key type."); |
|
66 | - } |
|
67 | - // cast private exponent to correct class |
|
68 | - $key = JWKParameter::PARAM_PRIVATE_EXPONENT; |
|
69 | - $this->_parameters[$key] = new PrivateExponentParameter( |
|
70 | - $this->_parameters[$key]->value()); |
|
71 | - } |
|
50 | + /** |
|
51 | + * Constructor. |
|
52 | + * |
|
53 | + * @param JWKParameter ...$params |
|
54 | + * @throws \UnexpectedValueException If missing required parameter |
|
55 | + */ |
|
56 | + public function __construct(JWKParameter ...$params) |
|
57 | + { |
|
58 | + parent::__construct(...$params); |
|
59 | + foreach (self::MANAGED_PARAMS as $name) { |
|
60 | + if (!$this->has($name)) { |
|
61 | + throw new \UnexpectedValueException("Missing '$name' parameter."); |
|
62 | + } |
|
63 | + } |
|
64 | + if ($this->keyTypeParameter()->value() != KeyTypeParameter::TYPE_RSA) { |
|
65 | + throw new \UnexpectedValueException("Invalid key type."); |
|
66 | + } |
|
67 | + // cast private exponent to correct class |
|
68 | + $key = JWKParameter::PARAM_PRIVATE_EXPONENT; |
|
69 | + $this->_parameters[$key] = new PrivateExponentParameter( |
|
70 | + $this->_parameters[$key]->value()); |
|
71 | + } |
|
72 | 72 | |
73 | - /** |
|
74 | - * Initialize from RSAPrivateKey. |
|
75 | - * |
|
76 | - * @param RSAPrivateKey $pk |
|
77 | - * @return self |
|
78 | - */ |
|
79 | - public static function fromRSAPrivateKey(RSAPrivateKey $pk) |
|
80 | - { |
|
81 | - $n = ModulusParameter::fromNumber($pk->modulus()); |
|
82 | - $e = ExponentParameter::fromNumber($pk->publicExponent()); |
|
83 | - $d = PrivateExponentParameter::fromNumber($pk->privateExponent()); |
|
84 | - $p = FirstPrimeFactorParameter::fromNumber($pk->prime1()); |
|
85 | - $q = SecondPrimeFactorParameter::fromNumber($pk->prime2()); |
|
86 | - $dp = FirstFactorCRTExponentParameter::fromNumber($pk->exponent1()); |
|
87 | - $dq = SecondFactorCRTExponentParameter::fromNumber($pk->exponent2()); |
|
88 | - $qi = FirstCRTCoefficientParameter::fromNumber($pk->coefficient()); |
|
89 | - $key_type = new KeyTypeParameter(KeyTypeParameter::TYPE_RSA); |
|
90 | - return new self($key_type, $n, $e, $d, $p, $q, $dp, $dq, $qi); |
|
91 | - } |
|
73 | + /** |
|
74 | + * Initialize from RSAPrivateKey. |
|
75 | + * |
|
76 | + * @param RSAPrivateKey $pk |
|
77 | + * @return self |
|
78 | + */ |
|
79 | + public static function fromRSAPrivateKey(RSAPrivateKey $pk) |
|
80 | + { |
|
81 | + $n = ModulusParameter::fromNumber($pk->modulus()); |
|
82 | + $e = ExponentParameter::fromNumber($pk->publicExponent()); |
|
83 | + $d = PrivateExponentParameter::fromNumber($pk->privateExponent()); |
|
84 | + $p = FirstPrimeFactorParameter::fromNumber($pk->prime1()); |
|
85 | + $q = SecondPrimeFactorParameter::fromNumber($pk->prime2()); |
|
86 | + $dp = FirstFactorCRTExponentParameter::fromNumber($pk->exponent1()); |
|
87 | + $dq = SecondFactorCRTExponentParameter::fromNumber($pk->exponent2()); |
|
88 | + $qi = FirstCRTCoefficientParameter::fromNumber($pk->coefficient()); |
|
89 | + $key_type = new KeyTypeParameter(KeyTypeParameter::TYPE_RSA); |
|
90 | + return new self($key_type, $n, $e, $d, $p, $q, $dp, $dq, $qi); |
|
91 | + } |
|
92 | 92 | |
93 | - /** |
|
94 | - * Initialize from PEM. |
|
95 | - * |
|
96 | - * @param PEM $pem |
|
97 | - * @return self |
|
98 | - */ |
|
99 | - public static function fromPEM(PEM $pem) |
|
100 | - { |
|
101 | - return self::fromRSAPrivateKey(RSAPrivateKey::fromPEM($pem)); |
|
102 | - } |
|
93 | + /** |
|
94 | + * Initialize from PEM. |
|
95 | + * |
|
96 | + * @param PEM $pem |
|
97 | + * @return self |
|
98 | + */ |
|
99 | + public static function fromPEM(PEM $pem) |
|
100 | + { |
|
101 | + return self::fromRSAPrivateKey(RSAPrivateKey::fromPEM($pem)); |
|
102 | + } |
|
103 | 103 | |
104 | - /** |
|
105 | - * Get public key component. |
|
106 | - * |
|
107 | - * @return RSAPublicKeyJWK |
|
108 | - */ |
|
109 | - public function publicKey() |
|
110 | - { |
|
111 | - $kty = $this->keyTypeParameter(); |
|
112 | - $n = $this->modulusParameter(); |
|
113 | - $e = $this->exponentParameter(); |
|
114 | - return new RSAPublicKeyJWK($kty, $n, $e); |
|
115 | - } |
|
104 | + /** |
|
105 | + * Get public key component. |
|
106 | + * |
|
107 | + * @return RSAPublicKeyJWK |
|
108 | + */ |
|
109 | + public function publicKey() |
|
110 | + { |
|
111 | + $kty = $this->keyTypeParameter(); |
|
112 | + $n = $this->modulusParameter(); |
|
113 | + $e = $this->exponentParameter(); |
|
114 | + return new RSAPublicKeyJWK($kty, $n, $e); |
|
115 | + } |
|
116 | 116 | |
117 | - /** |
|
118 | - * Convert JWK to PEM. |
|
119 | - * |
|
120 | - * @return PEM |
|
121 | - */ |
|
122 | - public function toPEM() |
|
123 | - { |
|
124 | - $n = $this->modulusParameter() |
|
125 | - ->number() |
|
126 | - ->base10(); |
|
127 | - $e = $this->exponentParameter() |
|
128 | - ->number() |
|
129 | - ->base10(); |
|
130 | - $d = $this->privateExponentParameter() |
|
131 | - ->number() |
|
132 | - ->base10(); |
|
133 | - $p = $this->firstPrimeFactorParameter() |
|
134 | - ->number() |
|
135 | - ->base10(); |
|
136 | - $q = $this->secondPrimeFactorParameter() |
|
137 | - ->number() |
|
138 | - ->base10(); |
|
139 | - $dp = $this->firstFactorCRTExponentParameter() |
|
140 | - ->number() |
|
141 | - ->base10(); |
|
142 | - $dq = $this->secondFactorCRTExponentParameter() |
|
143 | - ->number() |
|
144 | - ->base10(); |
|
145 | - $qi = $this->firstCRTCoefficientParameter() |
|
146 | - ->number() |
|
147 | - ->base10(); |
|
148 | - $pk = new RSAPrivateKey($n, $e, $d, $p, $q, $dp, $dq, $qi); |
|
149 | - return PrivateKeyInfo::fromPrivateKey($pk)->toPEM(); |
|
150 | - } |
|
117 | + /** |
|
118 | + * Convert JWK to PEM. |
|
119 | + * |
|
120 | + * @return PEM |
|
121 | + */ |
|
122 | + public function toPEM() |
|
123 | + { |
|
124 | + $n = $this->modulusParameter() |
|
125 | + ->number() |
|
126 | + ->base10(); |
|
127 | + $e = $this->exponentParameter() |
|
128 | + ->number() |
|
129 | + ->base10(); |
|
130 | + $d = $this->privateExponentParameter() |
|
131 | + ->number() |
|
132 | + ->base10(); |
|
133 | + $p = $this->firstPrimeFactorParameter() |
|
134 | + ->number() |
|
135 | + ->base10(); |
|
136 | + $q = $this->secondPrimeFactorParameter() |
|
137 | + ->number() |
|
138 | + ->base10(); |
|
139 | + $dp = $this->firstFactorCRTExponentParameter() |
|
140 | + ->number() |
|
141 | + ->base10(); |
|
142 | + $dq = $this->secondFactorCRTExponentParameter() |
|
143 | + ->number() |
|
144 | + ->base10(); |
|
145 | + $qi = $this->firstCRTCoefficientParameter() |
|
146 | + ->number() |
|
147 | + ->base10(); |
|
148 | + $pk = new RSAPrivateKey($n, $e, $d, $p, $q, $dp, $dq, $qi); |
|
149 | + return PrivateKeyInfo::fromPrivateKey($pk)->toPEM(); |
|
150 | + } |
|
151 | 151 | } |
@@ -12,187 +12,187 @@ |
||
12 | 12 | */ |
13 | 13 | class JWK implements \Countable, \IteratorAggregate |
14 | 14 | { |
15 | - use TypedJWK; |
|
15 | + use TypedJWK; |
|
16 | 16 | |
17 | - /** |
|
18 | - * Parameters. |
|
19 | - * |
|
20 | - * @var JWKParameter[] $_parameters |
|
21 | - */ |
|
22 | - protected $_parameters; |
|
17 | + /** |
|
18 | + * Parameters. |
|
19 | + * |
|
20 | + * @var JWKParameter[] $_parameters |
|
21 | + */ |
|
22 | + protected $_parameters; |
|
23 | 23 | |
24 | - /** |
|
25 | - * Constructor. |
|
26 | - * |
|
27 | - * @param JWKParameter ...$params |
|
28 | - */ |
|
29 | - public function __construct(JWKParameter ...$params) |
|
30 | - { |
|
31 | - $this->_parameters = array(); |
|
32 | - foreach ($params as $param) { |
|
33 | - $this->_parameters[$param->name()] = $param; |
|
34 | - } |
|
35 | - } |
|
24 | + /** |
|
25 | + * Constructor. |
|
26 | + * |
|
27 | + * @param JWKParameter ...$params |
|
28 | + */ |
|
29 | + public function __construct(JWKParameter ...$params) |
|
30 | + { |
|
31 | + $this->_parameters = array(); |
|
32 | + foreach ($params as $param) { |
|
33 | + $this->_parameters[$param->name()] = $param; |
|
34 | + } |
|
35 | + } |
|
36 | 36 | |
37 | - /** |
|
38 | - * Initialize from an array representing a JSON object. |
|
39 | - * |
|
40 | - * @param array $members |
|
41 | - * @return self |
|
42 | - */ |
|
43 | - public static function fromArray(array $members) |
|
44 | - { |
|
45 | - $params = array(); |
|
46 | - foreach ($members as $name => $value) { |
|
47 | - $params[] = JWKParameter::fromNameAndValue($name, $value); |
|
48 | - } |
|
49 | - return new static(...$params); |
|
50 | - } |
|
37 | + /** |
|
38 | + * Initialize from an array representing a JSON object. |
|
39 | + * |
|
40 | + * @param array $members |
|
41 | + * @return self |
|
42 | + */ |
|
43 | + public static function fromArray(array $members) |
|
44 | + { |
|
45 | + $params = array(); |
|
46 | + foreach ($members as $name => $value) { |
|
47 | + $params[] = JWKParameter::fromNameAndValue($name, $value); |
|
48 | + } |
|
49 | + return new static(...$params); |
|
50 | + } |
|
51 | 51 | |
52 | - /** |
|
53 | - * Initialize from a JSON string. |
|
54 | - * |
|
55 | - * @param string $json |
|
56 | - * @throws \UnexpectedValueException |
|
57 | - * @return self |
|
58 | - */ |
|
59 | - public static function fromJSON($json) |
|
60 | - { |
|
61 | - $members = json_decode($json, true, 32, JSON_BIGINT_AS_STRING); |
|
62 | - if (!is_array($members)) { |
|
63 | - throw new \UnexpectedValueException("Invalid JSON."); |
|
64 | - } |
|
65 | - return static::fromArray($members); |
|
66 | - } |
|
52 | + /** |
|
53 | + * Initialize from a JSON string. |
|
54 | + * |
|
55 | + * @param string $json |
|
56 | + * @throws \UnexpectedValueException |
|
57 | + * @return self |
|
58 | + */ |
|
59 | + public static function fromJSON($json) |
|
60 | + { |
|
61 | + $members = json_decode($json, true, 32, JSON_BIGINT_AS_STRING); |
|
62 | + if (!is_array($members)) { |
|
63 | + throw new \UnexpectedValueException("Invalid JSON."); |
|
64 | + } |
|
65 | + return static::fromArray($members); |
|
66 | + } |
|
67 | 67 | |
68 | - /** |
|
69 | - * Initialize from another JWK. |
|
70 | - * |
|
71 | - * Allows casting to subclass by late static binding. |
|
72 | - * |
|
73 | - * @param JWK $jwk |
|
74 | - * @return self |
|
75 | - */ |
|
76 | - public static function fromJWK(JWK $jwk) |
|
77 | - { |
|
78 | - return new static(...array_values($jwk->_parameters)); |
|
79 | - } |
|
68 | + /** |
|
69 | + * Initialize from another JWK. |
|
70 | + * |
|
71 | + * Allows casting to subclass by late static binding. |
|
72 | + * |
|
73 | + * @param JWK $jwk |
|
74 | + * @return self |
|
75 | + */ |
|
76 | + public static function fromJWK(JWK $jwk) |
|
77 | + { |
|
78 | + return new static(...array_values($jwk->_parameters)); |
|
79 | + } |
|
80 | 80 | |
81 | - /** |
|
82 | - * Get self with parameters added. |
|
83 | - * |
|
84 | - * @param JWKParameter ...$params |
|
85 | - * @return self |
|
86 | - */ |
|
87 | - public function withParameters(JWKParameter ...$params) |
|
88 | - { |
|
89 | - $obj = clone $this; |
|
90 | - foreach ($params as $param) { |
|
91 | - $obj->_parameters[$param->name()] = $param; |
|
92 | - } |
|
93 | - return $obj; |
|
94 | - } |
|
81 | + /** |
|
82 | + * Get self with parameters added. |
|
83 | + * |
|
84 | + * @param JWKParameter ...$params |
|
85 | + * @return self |
|
86 | + */ |
|
87 | + public function withParameters(JWKParameter ...$params) |
|
88 | + { |
|
89 | + $obj = clone $this; |
|
90 | + foreach ($params as $param) { |
|
91 | + $obj->_parameters[$param->name()] = $param; |
|
92 | + } |
|
93 | + return $obj; |
|
94 | + } |
|
95 | 95 | |
96 | - /** |
|
97 | - * Get all parameters. |
|
98 | - * |
|
99 | - * @return JWKParameter[] |
|
100 | - */ |
|
101 | - public function parameters() |
|
102 | - { |
|
103 | - return $this->_parameters; |
|
104 | - } |
|
96 | + /** |
|
97 | + * Get all parameters. |
|
98 | + * |
|
99 | + * @return JWKParameter[] |
|
100 | + */ |
|
101 | + public function parameters() |
|
102 | + { |
|
103 | + return $this->_parameters; |
|
104 | + } |
|
105 | 105 | |
106 | - /** |
|
107 | - * Get self with given key ID added to parameters. |
|
108 | - * |
|
109 | - * @param string $id Key ID as a string |
|
110 | - * @return self |
|
111 | - */ |
|
112 | - public function withKeyID($id) |
|
113 | - { |
|
114 | - return $this->withParameters(new KeyIDParameter($id)); |
|
115 | - } |
|
106 | + /** |
|
107 | + * Get self with given key ID added to parameters. |
|
108 | + * |
|
109 | + * @param string $id Key ID as a string |
|
110 | + * @return self |
|
111 | + */ |
|
112 | + public function withKeyID($id) |
|
113 | + { |
|
114 | + return $this->withParameters(new KeyIDParameter($id)); |
|
115 | + } |
|
116 | 116 | |
117 | - /** |
|
118 | - * Whether parameters are present. |
|
119 | - * |
|
120 | - * Returns false if any of the given parameters is not set. |
|
121 | - * |
|
122 | - * @param string ...$names Parameter names |
|
123 | - * @return bool |
|
124 | - */ |
|
125 | - public function has(...$names) |
|
126 | - { |
|
127 | - foreach ($names as $name) { |
|
128 | - if (!isset($this->_parameters[$name])) { |
|
129 | - return false; |
|
130 | - } |
|
131 | - } |
|
132 | - return true; |
|
133 | - } |
|
117 | + /** |
|
118 | + * Whether parameters are present. |
|
119 | + * |
|
120 | + * Returns false if any of the given parameters is not set. |
|
121 | + * |
|
122 | + * @param string ...$names Parameter names |
|
123 | + * @return bool |
|
124 | + */ |
|
125 | + public function has(...$names) |
|
126 | + { |
|
127 | + foreach ($names as $name) { |
|
128 | + if (!isset($this->_parameters[$name])) { |
|
129 | + return false; |
|
130 | + } |
|
131 | + } |
|
132 | + return true; |
|
133 | + } |
|
134 | 134 | |
135 | - /** |
|
136 | - * Get a parameter. |
|
137 | - * |
|
138 | - * @param string $name Parameter name |
|
139 | - * @throws \LogicException |
|
140 | - * @return JWKParameter |
|
141 | - */ |
|
142 | - public function get($name) |
|
143 | - { |
|
144 | - if (!$this->has($name)) { |
|
145 | - throw new \LogicException("Parameter $name doesn't exists."); |
|
146 | - } |
|
147 | - return $this->_parameters[$name]; |
|
148 | - } |
|
135 | + /** |
|
136 | + * Get a parameter. |
|
137 | + * |
|
138 | + * @param string $name Parameter name |
|
139 | + * @throws \LogicException |
|
140 | + * @return JWKParameter |
|
141 | + */ |
|
142 | + public function get($name) |
|
143 | + { |
|
144 | + if (!$this->has($name)) { |
|
145 | + throw new \LogicException("Parameter $name doesn't exists."); |
|
146 | + } |
|
147 | + return $this->_parameters[$name]; |
|
148 | + } |
|
149 | 149 | |
150 | - /** |
|
151 | - * Convert to array. |
|
152 | - * |
|
153 | - * @return array Parameter values keyed by parameter names |
|
154 | - */ |
|
155 | - public function toArray() |
|
156 | - { |
|
157 | - $a = array(); |
|
158 | - foreach ($this->_parameters as $param) { |
|
159 | - $a[$param->name()] = $param->value(); |
|
160 | - } |
|
161 | - return $a; |
|
162 | - } |
|
150 | + /** |
|
151 | + * Convert to array. |
|
152 | + * |
|
153 | + * @return array Parameter values keyed by parameter names |
|
154 | + */ |
|
155 | + public function toArray() |
|
156 | + { |
|
157 | + $a = array(); |
|
158 | + foreach ($this->_parameters as $param) { |
|
159 | + $a[$param->name()] = $param->value(); |
|
160 | + } |
|
161 | + return $a; |
|
162 | + } |
|
163 | 163 | |
164 | - /** |
|
165 | - * Convert to JSON. |
|
166 | - * |
|
167 | - * @return string |
|
168 | - */ |
|
169 | - public function toJSON() |
|
170 | - { |
|
171 | - $data = $this->toArray(); |
|
172 | - if (empty($data)) { |
|
173 | - return ""; |
|
174 | - } |
|
175 | - return json_encode((object) $data, JSON_UNESCAPED_SLASHES); |
|
176 | - } |
|
164 | + /** |
|
165 | + * Convert to JSON. |
|
166 | + * |
|
167 | + * @return string |
|
168 | + */ |
|
169 | + public function toJSON() |
|
170 | + { |
|
171 | + $data = $this->toArray(); |
|
172 | + if (empty($data)) { |
|
173 | + return ""; |
|
174 | + } |
|
175 | + return json_encode((object) $data, JSON_UNESCAPED_SLASHES); |
|
176 | + } |
|
177 | 177 | |
178 | - /** |
|
179 | - * Get the number of parameters. |
|
180 | - * |
|
181 | - * @see \Countable::count() |
|
182 | - */ |
|
183 | - public function count() |
|
184 | - { |
|
185 | - return count($this->_parameters); |
|
186 | - } |
|
178 | + /** |
|
179 | + * Get the number of parameters. |
|
180 | + * |
|
181 | + * @see \Countable::count() |
|
182 | + */ |
|
183 | + public function count() |
|
184 | + { |
|
185 | + return count($this->_parameters); |
|
186 | + } |
|
187 | 187 | |
188 | - /** |
|
189 | - * Get iterator for the parameters. |
|
190 | - * |
|
191 | - * @see \IteratorAggregate::getIterator() |
|
192 | - * @return \ArrayIterator |
|
193 | - */ |
|
194 | - public function getIterator() |
|
195 | - { |
|
196 | - return new \ArrayIterator($this->_parameters); |
|
197 | - } |
|
188 | + /** |
|
189 | + * Get iterator for the parameters. |
|
190 | + * |
|
191 | + * @see \IteratorAggregate::getIterator() |
|
192 | + * @return \ArrayIterator |
|
193 | + */ |
|
194 | + public function getIterator() |
|
195 | + { |
|
196 | + return new \ArrayIterator($this->_parameters); |
|
197 | + } |
|
198 | 198 | } |
@@ -23,107 +23,107 @@ |
||
23 | 23 | */ |
24 | 24 | class ECPrivateKeyJWK extends PrivateKeyJWK |
25 | 25 | { |
26 | - /** |
|
27 | - * Parameter names managed by this class. |
|
28 | - * |
|
29 | - * @internal |
|
30 | - * |
|
31 | - * @var string[] |
|
32 | - */ |
|
33 | - const MANAGED_PARAMS = array( |
|
34 | - /* @formatter:off */ |
|
35 | - JWKParameter::PARAM_KEY_TYPE, |
|
36 | - JWKParameter::PARAM_CURVE, |
|
37 | - JWKParameter::PARAM_X_COORDINATE, |
|
38 | - JWKParameter::PARAM_ECC_PRIVATE_KEY |
|
39 | - /* @formatter:on */ |
|
40 | - ); |
|
26 | + /** |
|
27 | + * Parameter names managed by this class. |
|
28 | + * |
|
29 | + * @internal |
|
30 | + * |
|
31 | + * @var string[] |
|
32 | + */ |
|
33 | + const MANAGED_PARAMS = array( |
|
34 | + /* @formatter:off */ |
|
35 | + JWKParameter::PARAM_KEY_TYPE, |
|
36 | + JWKParameter::PARAM_CURVE, |
|
37 | + JWKParameter::PARAM_X_COORDINATE, |
|
38 | + JWKParameter::PARAM_ECC_PRIVATE_KEY |
|
39 | + /* @formatter:on */ |
|
40 | + ); |
|
41 | 41 | |
42 | - /** |
|
43 | - * Constructor. |
|
44 | - * |
|
45 | - * @param JWKParameter ...$params |
|
46 | - * @throws \UnexpectedValueException If missing required parameter |
|
47 | - */ |
|
48 | - public function __construct(JWKParameter ...$params) |
|
49 | - { |
|
50 | - parent::__construct(...$params); |
|
51 | - foreach (self::MANAGED_PARAMS as $name) { |
|
52 | - if (!$this->has($name)) { |
|
53 | - throw new \UnexpectedValueException("Missing '$name' parameter."); |
|
54 | - } |
|
55 | - } |
|
56 | - if ($this->keyTypeParameter()->value() != KeyTypeParameter::TYPE_EC) { |
|
57 | - throw new \UnexpectedValueException("Invalid key type."); |
|
58 | - } |
|
59 | - // cast ECC private key parameter to correct class |
|
60 | - $key = JWKParameter::PARAM_ECC_PRIVATE_KEY; |
|
61 | - $this->_parameters[$key] = new ECCPrivateKeyParameter( |
|
62 | - $this->_parameters[$key]->value()); |
|
63 | - } |
|
42 | + /** |
|
43 | + * Constructor. |
|
44 | + * |
|
45 | + * @param JWKParameter ...$params |
|
46 | + * @throws \UnexpectedValueException If missing required parameter |
|
47 | + */ |
|
48 | + public function __construct(JWKParameter ...$params) |
|
49 | + { |
|
50 | + parent::__construct(...$params); |
|
51 | + foreach (self::MANAGED_PARAMS as $name) { |
|
52 | + if (!$this->has($name)) { |
|
53 | + throw new \UnexpectedValueException("Missing '$name' parameter."); |
|
54 | + } |
|
55 | + } |
|
56 | + if ($this->keyTypeParameter()->value() != KeyTypeParameter::TYPE_EC) { |
|
57 | + throw new \UnexpectedValueException("Invalid key type."); |
|
58 | + } |
|
59 | + // cast ECC private key parameter to correct class |
|
60 | + $key = JWKParameter::PARAM_ECC_PRIVATE_KEY; |
|
61 | + $this->_parameters[$key] = new ECCPrivateKeyParameter( |
|
62 | + $this->_parameters[$key]->value()); |
|
63 | + } |
|
64 | 64 | |
65 | - /** |
|
66 | - * Initialize from ECPrivateKey. |
|
67 | - * |
|
68 | - * @param ECPrivateKey $pk |
|
69 | - * @throws \UnexpectedValueException |
|
70 | - * @return self |
|
71 | - */ |
|
72 | - public static function fromECPrivateKey(ECPrivateKey $pk) |
|
73 | - { |
|
74 | - if (!$pk->hasNamedCurve()) { |
|
75 | - throw new \UnexpectedValueException("No curve name."); |
|
76 | - } |
|
77 | - $curve = CurveParameter::fromOID($pk->namedCurve()); |
|
78 | - $pubkey = $pk->publicKey(); |
|
79 | - list($x, $y) = $pubkey->curvePointOctets(); |
|
80 | - $xcoord = XCoordinateParameter::fromString($x); |
|
81 | - $ycoord = YCoordinateParameter::fromString($y); |
|
82 | - $priv = ECCPrivateKeyParameter::fromString($pk->privateKeyOctets()); |
|
83 | - $key_type = new KeyTypeParameter(KeyTypeParameter::TYPE_EC); |
|
84 | - return new self($key_type, $curve, $xcoord, $ycoord, $priv); |
|
85 | - } |
|
65 | + /** |
|
66 | + * Initialize from ECPrivateKey. |
|
67 | + * |
|
68 | + * @param ECPrivateKey $pk |
|
69 | + * @throws \UnexpectedValueException |
|
70 | + * @return self |
|
71 | + */ |
|
72 | + public static function fromECPrivateKey(ECPrivateKey $pk) |
|
73 | + { |
|
74 | + if (!$pk->hasNamedCurve()) { |
|
75 | + throw new \UnexpectedValueException("No curve name."); |
|
76 | + } |
|
77 | + $curve = CurveParameter::fromOID($pk->namedCurve()); |
|
78 | + $pubkey = $pk->publicKey(); |
|
79 | + list($x, $y) = $pubkey->curvePointOctets(); |
|
80 | + $xcoord = XCoordinateParameter::fromString($x); |
|
81 | + $ycoord = YCoordinateParameter::fromString($y); |
|
82 | + $priv = ECCPrivateKeyParameter::fromString($pk->privateKeyOctets()); |
|
83 | + $key_type = new KeyTypeParameter(KeyTypeParameter::TYPE_EC); |
|
84 | + return new self($key_type, $curve, $xcoord, $ycoord, $priv); |
|
85 | + } |
|
86 | 86 | |
87 | - /** |
|
88 | - * Initialize from PEM. |
|
89 | - * |
|
90 | - * @param PEM $pem |
|
91 | - * @return self |
|
92 | - */ |
|
93 | - public static function fromPEM(PEM $pem) |
|
94 | - { |
|
95 | - return self::fromECPrivateKey(ECPrivateKey::fromPEM($pem)); |
|
96 | - } |
|
87 | + /** |
|
88 | + * Initialize from PEM. |
|
89 | + * |
|
90 | + * @param PEM $pem |
|
91 | + * @return self |
|
92 | + */ |
|
93 | + public static function fromPEM(PEM $pem) |
|
94 | + { |
|
95 | + return self::fromECPrivateKey(ECPrivateKey::fromPEM($pem)); |
|
96 | + } |
|
97 | 97 | |
98 | - /** |
|
99 | - * Get the public key component of the EC private key. |
|
100 | - * |
|
101 | - * @return ECPublicKeyJWK |
|
102 | - */ |
|
103 | - public function publicKey() |
|
104 | - { |
|
105 | - $kty = $this->keyTypeParameter(); |
|
106 | - $curve = $this->curveParameter(); |
|
107 | - $xcoord = $this->XCoordinateParameter(); |
|
108 | - $ycoord = $this->YCoordinateParameter(); |
|
109 | - return new ECPublicKeyJWK($kty, $curve, $xcoord, $ycoord); |
|
110 | - } |
|
98 | + /** |
|
99 | + * Get the public key component of the EC private key. |
|
100 | + * |
|
101 | + * @return ECPublicKeyJWK |
|
102 | + */ |
|
103 | + public function publicKey() |
|
104 | + { |
|
105 | + $kty = $this->keyTypeParameter(); |
|
106 | + $curve = $this->curveParameter(); |
|
107 | + $xcoord = $this->XCoordinateParameter(); |
|
108 | + $ycoord = $this->YCoordinateParameter(); |
|
109 | + return new ECPublicKeyJWK($kty, $curve, $xcoord, $ycoord); |
|
110 | + } |
|
111 | 111 | |
112 | - /** |
|
113 | - * Convert EC private key to PEM. |
|
114 | - * |
|
115 | - * @return PEM |
|
116 | - */ |
|
117 | - public function toPEM() |
|
118 | - { |
|
119 | - $curve_oid = CurveParameter::nameToOID($this->curveParameter()->value()); |
|
120 | - $x = ECConversion::octetsToNumber( |
|
121 | - $this->XCoordinateParameter()->coordinateOctets()); |
|
122 | - $y = ECConversion::octetsToNumber( |
|
123 | - $this->YCoordinateParameter()->coordinateOctets()); |
|
124 | - $pubkey = ECPublicKey::fromCoordinates($x, $y, $curve_oid); |
|
125 | - $priv = $this->ECCPrivateKeyParameter()->privateKeyOctets(); |
|
126 | - $ec = new ECPrivateKey($priv, $curve_oid, $pubkey->ECPoint()); |
|
127 | - return $ec->privateKeyInfo()->toPEM(); |
|
128 | - } |
|
112 | + /** |
|
113 | + * Convert EC private key to PEM. |
|
114 | + * |
|
115 | + * @return PEM |
|
116 | + */ |
|
117 | + public function toPEM() |
|
118 | + { |
|
119 | + $curve_oid = CurveParameter::nameToOID($this->curveParameter()->value()); |
|
120 | + $x = ECConversion::octetsToNumber( |
|
121 | + $this->XCoordinateParameter()->coordinateOctets()); |
|
122 | + $y = ECConversion::octetsToNumber( |
|
123 | + $this->YCoordinateParameter()->coordinateOctets()); |
|
124 | + $pubkey = ECPublicKey::fromCoordinates($x, $y, $curve_oid); |
|
125 | + $priv = $this->ECCPrivateKeyParameter()->privateKeyOctets(); |
|
126 | + $ec = new ECPrivateKey($priv, $curve_oid, $pubkey->ECPoint()); |
|
127 | + return $ec->privateKeyInfo()->toPEM(); |
|
128 | + } |
|
129 | 129 | } |
@@ -21,82 +21,82 @@ |
||
21 | 21 | */ |
22 | 22 | class ECPublicKeyJWK extends PublicKeyJWK |
23 | 23 | { |
24 | - /** |
|
25 | - * Parameter names managed by this class. |
|
26 | - * |
|
27 | - * @var string[] |
|
28 | - */ |
|
29 | - const MANAGED_PARAMS = array( |
|
30 | - /* @formatter:off */ |
|
31 | - JWKParameter::PARAM_KEY_TYPE, |
|
32 | - JWKParameter::PARAM_CURVE, |
|
33 | - JWKParameter::PARAM_X_COORDINATE |
|
34 | - /* @formatter:on */ |
|
35 | - ); |
|
24 | + /** |
|
25 | + * Parameter names managed by this class. |
|
26 | + * |
|
27 | + * @var string[] |
|
28 | + */ |
|
29 | + const MANAGED_PARAMS = array( |
|
30 | + /* @formatter:off */ |
|
31 | + JWKParameter::PARAM_KEY_TYPE, |
|
32 | + JWKParameter::PARAM_CURVE, |
|
33 | + JWKParameter::PARAM_X_COORDINATE |
|
34 | + /* @formatter:on */ |
|
35 | + ); |
|
36 | 36 | |
37 | - /** |
|
38 | - * Constructor. |
|
39 | - * |
|
40 | - * @param JWKParameter ...$params |
|
41 | - * @throws \UnexpectedValueException If missing required parameter |
|
42 | - */ |
|
43 | - public function __construct(JWKParameter ...$params) |
|
44 | - { |
|
45 | - parent::__construct(...$params); |
|
46 | - foreach (self::MANAGED_PARAMS as $name) { |
|
47 | - if (!$this->has($name)) { |
|
48 | - throw new \UnexpectedValueException("Missing '$name' parameter."); |
|
49 | - } |
|
50 | - } |
|
51 | - if ($this->keyTypeParameter()->value() != KeyTypeParameter::TYPE_EC) { |
|
52 | - throw new \UnexpectedValueException("Invalid key type."); |
|
53 | - } |
|
54 | - } |
|
37 | + /** |
|
38 | + * Constructor. |
|
39 | + * |
|
40 | + * @param JWKParameter ...$params |
|
41 | + * @throws \UnexpectedValueException If missing required parameter |
|
42 | + */ |
|
43 | + public function __construct(JWKParameter ...$params) |
|
44 | + { |
|
45 | + parent::__construct(...$params); |
|
46 | + foreach (self::MANAGED_PARAMS as $name) { |
|
47 | + if (!$this->has($name)) { |
|
48 | + throw new \UnexpectedValueException("Missing '$name' parameter."); |
|
49 | + } |
|
50 | + } |
|
51 | + if ($this->keyTypeParameter()->value() != KeyTypeParameter::TYPE_EC) { |
|
52 | + throw new \UnexpectedValueException("Invalid key type."); |
|
53 | + } |
|
54 | + } |
|
55 | 55 | |
56 | - /** |
|
57 | - * Initialize from ECPublicKey. |
|
58 | - * |
|
59 | - * @param ECPublicKey $pk |
|
60 | - * @throws \UnexpectedValueException |
|
61 | - * @return self |
|
62 | - */ |
|
63 | - public static function fromECPublicKey(ECPublicKey $pk) |
|
64 | - { |
|
65 | - if (!$pk->hasNamedCurve()) { |
|
66 | - throw new \UnexpectedValueException("No curve name."); |
|
67 | - } |
|
68 | - $curve = CurveParameter::fromOID($pk->namedCurve()); |
|
69 | - list($x, $y) = $pk->curvePointOctets(); |
|
70 | - $xcoord = XCoordinateParameter::fromString($x); |
|
71 | - $ycoord = YCoordinateParameter::fromString($y); |
|
72 | - $key_type = new KeyTypeParameter(KeyTypeParameter::TYPE_EC); |
|
73 | - return new self($key_type, $curve, $xcoord, $ycoord); |
|
74 | - } |
|
56 | + /** |
|
57 | + * Initialize from ECPublicKey. |
|
58 | + * |
|
59 | + * @param ECPublicKey $pk |
|
60 | + * @throws \UnexpectedValueException |
|
61 | + * @return self |
|
62 | + */ |
|
63 | + public static function fromECPublicKey(ECPublicKey $pk) |
|
64 | + { |
|
65 | + if (!$pk->hasNamedCurve()) { |
|
66 | + throw new \UnexpectedValueException("No curve name."); |
|
67 | + } |
|
68 | + $curve = CurveParameter::fromOID($pk->namedCurve()); |
|
69 | + list($x, $y) = $pk->curvePointOctets(); |
|
70 | + $xcoord = XCoordinateParameter::fromString($x); |
|
71 | + $ycoord = YCoordinateParameter::fromString($y); |
|
72 | + $key_type = new KeyTypeParameter(KeyTypeParameter::TYPE_EC); |
|
73 | + return new self($key_type, $curve, $xcoord, $ycoord); |
|
74 | + } |
|
75 | 75 | |
76 | - /** |
|
77 | - * Initialize from PEM. |
|
78 | - * |
|
79 | - * @param PEM $pem |
|
80 | - * @return self |
|
81 | - */ |
|
82 | - public static function fromPEM(PEM $pem) |
|
83 | - { |
|
84 | - return self::fromECPublicKey(ECPublicKey::fromPEM($pem)); |
|
85 | - } |
|
76 | + /** |
|
77 | + * Initialize from PEM. |
|
78 | + * |
|
79 | + * @param PEM $pem |
|
80 | + * @return self |
|
81 | + */ |
|
82 | + public static function fromPEM(PEM $pem) |
|
83 | + { |
|
84 | + return self::fromECPublicKey(ECPublicKey::fromPEM($pem)); |
|
85 | + } |
|
86 | 86 | |
87 | - /** |
|
88 | - * Convert EC public key to PEM. |
|
89 | - * |
|
90 | - * @return PEM |
|
91 | - */ |
|
92 | - public function toPEM() |
|
93 | - { |
|
94 | - $curve_oid = CurveParameter::nameToOID($this->curveParameter()->value()); |
|
95 | - $x = ECConversion::octetsToNumber( |
|
96 | - $this->XCoordinateParameter()->coordinateOctets()); |
|
97 | - $y = ECConversion::octetsToNumber( |
|
98 | - $this->YCoordinateParameter()->coordinateOctets()); |
|
99 | - $ec = ECPublicKey::fromCoordinates($x, $y, $curve_oid); |
|
100 | - return $ec->publicKeyInfo()->toPEM(); |
|
101 | - } |
|
87 | + /** |
|
88 | + * Convert EC public key to PEM. |
|
89 | + * |
|
90 | + * @return PEM |
|
91 | + */ |
|
92 | + public function toPEM() |
|
93 | + { |
|
94 | + $curve_oid = CurveParameter::nameToOID($this->curveParameter()->value()); |
|
95 | + $x = ECConversion::octetsToNumber( |
|
96 | + $this->XCoordinateParameter()->coordinateOctets()); |
|
97 | + $y = ECConversion::octetsToNumber( |
|
98 | + $this->YCoordinateParameter()->coordinateOctets()); |
|
99 | + $ec = ECPublicKey::fromCoordinates($x, $y, $curve_oid); |
|
100 | + return $ec->publicKeyInfo()->toPEM(); |
|
101 | + } |
|
102 | 102 | } |
@@ -11,227 +11,227 @@ |
||
11 | 11 | */ |
12 | 12 | class JWKSet implements \Countable, \IteratorAggregate |
13 | 13 | { |
14 | - /** |
|
15 | - * JWK objects. |
|
16 | - * |
|
17 | - * @var JWK[] $_jwks |
|
18 | - */ |
|
19 | - protected $_jwks; |
|
20 | - |
|
21 | - /** |
|
22 | - * Additional members. |
|
23 | - * |
|
24 | - * @var array $_additional |
|
25 | - */ |
|
26 | - protected $_additional; |
|
27 | - |
|
28 | - /** |
|
29 | - * JWK mappings. |
|
30 | - * |
|
31 | - * @var array |
|
32 | - */ |
|
33 | - private $_mappings = array(); |
|
34 | - |
|
35 | - /** |
|
36 | - * Constructor. |
|
37 | - * |
|
38 | - * @param JWK ...$jwks |
|
39 | - */ |
|
40 | - public function __construct(JWK ...$jwks) |
|
41 | - { |
|
42 | - $this->_jwks = $jwks; |
|
43 | - $this->_additional = array(); |
|
44 | - } |
|
45 | - |
|
46 | - /** |
|
47 | - * Reset internal cache variables on clone. |
|
48 | - */ |
|
49 | - public function __clone() |
|
50 | - { |
|
51 | - $this->_mappings = array(); |
|
52 | - } |
|
53 | - |
|
54 | - /** |
|
55 | - * Initialize from an array representing a JSON object. |
|
56 | - * |
|
57 | - * @param array $members |
|
58 | - * @throws \UnexpectedValueException |
|
59 | - * @return self |
|
60 | - */ |
|
61 | - public static function fromArray(array $members) |
|
62 | - { |
|
63 | - if (!isset($members["keys"]) || !is_array($members["keys"])) { |
|
64 | - throw new \UnexpectedValueException( |
|
65 | - "JWK Set must have a 'keys' member."); |
|
66 | - } |
|
67 | - $jwks = array_map( |
|
68 | - function ($jwkdata) { |
|
69 | - return JWK::fromArray($jwkdata); |
|
70 | - }, $members["keys"]); |
|
71 | - unset($members["keys"]); |
|
72 | - $obj = new self(...$jwks); |
|
73 | - $obj->_additional = $members; |
|
74 | - return $obj; |
|
75 | - } |
|
76 | - |
|
77 | - /** |
|
78 | - * Initialize from a JSON string. |
|
79 | - * |
|
80 | - * @param string $json |
|
81 | - * @throws \UnexpectedValueException |
|
82 | - * @return self |
|
83 | - */ |
|
84 | - public static function fromJSON($json) |
|
85 | - { |
|
86 | - $members = json_decode($json, true, 32, JSON_BIGINT_AS_STRING); |
|
87 | - if (!is_array($members)) { |
|
88 | - throw new \UnexpectedValueException("Invalid JSON."); |
|
89 | - } |
|
90 | - return self::fromArray($members); |
|
91 | - } |
|
92 | - |
|
93 | - /** |
|
94 | - * Get self with keys added. |
|
95 | - * |
|
96 | - * @param JWK ...$keys JWK objects |
|
97 | - * @return self |
|
98 | - */ |
|
99 | - public function withKeys(JWK ...$keys) |
|
100 | - { |
|
101 | - $obj = clone $this; |
|
102 | - $obj->_jwks = array_merge($obj->_jwks, $keys); |
|
103 | - return $obj; |
|
104 | - } |
|
105 | - |
|
106 | - /** |
|
107 | - * Get all JWK's in a set. |
|
108 | - * |
|
109 | - * @return JWK[] |
|
110 | - */ |
|
111 | - public function keys() |
|
112 | - { |
|
113 | - return $this->_jwks; |
|
114 | - } |
|
115 | - |
|
116 | - /** |
|
117 | - * Get the first JWK in the set. |
|
118 | - * |
|
119 | - * @throws \LogicException |
|
120 | - * @return JWK |
|
121 | - */ |
|
122 | - public function first() |
|
123 | - { |
|
124 | - if (!count($this->_jwks)) { |
|
125 | - throw new \LogicException("No keys."); |
|
126 | - } |
|
127 | - return $this->_jwks[0]; |
|
128 | - } |
|
129 | - |
|
130 | - /** |
|
131 | - * Get JWK by key ID. |
|
132 | - * |
|
133 | - * @param string $id |
|
134 | - * @return JWK|null Null if not found |
|
135 | - */ |
|
136 | - protected function _getKeyByID($id) |
|
137 | - { |
|
138 | - $map = $this->_getMapping(JWKParameter::PARAM_KEY_ID); |
|
139 | - return isset($map[$id]) ? $map[$id] : null; |
|
140 | - } |
|
141 | - |
|
142 | - /** |
|
143 | - * Check whether set has a JWK with a given key ID. |
|
144 | - * |
|
145 | - * @param string $id |
|
146 | - * @return bool |
|
147 | - */ |
|
148 | - public function hasKeyID($id) |
|
149 | - { |
|
150 | - return $this->_getKeyByID($id) !== null; |
|
151 | - } |
|
152 | - |
|
153 | - /** |
|
154 | - * Get a JWK by a key ID. |
|
155 | - * |
|
156 | - * @param string $id |
|
157 | - * @throws \LogicException |
|
158 | - * @return JWK |
|
159 | - */ |
|
160 | - public function keyByID($id) |
|
161 | - { |
|
162 | - $jwk = $this->_getKeyByID($id); |
|
163 | - if (!$jwk) { |
|
164 | - throw new \LogicException("No key ID $id."); |
|
165 | - } |
|
166 | - return $jwk; |
|
167 | - } |
|
168 | - |
|
169 | - /** |
|
170 | - * Get mapping from parameter values of given parameter name to JWK. |
|
171 | - * |
|
172 | - * Later duplicate value shall override earlier JWK. |
|
173 | - * |
|
174 | - * @param string $name Parameter name |
|
175 | - * @return array |
|
176 | - */ |
|
177 | - protected function _getMapping($name) |
|
178 | - { |
|
179 | - if (!isset($this->_mappings[$name])) { |
|
180 | - $mapping = array(); |
|
181 | - foreach ($this->_jwks as $jwk) { |
|
182 | - if ($jwk->has($name)) { |
|
183 | - $key = (string) $jwk->get($name)->value(); |
|
184 | - $mapping[$key] = $jwk; |
|
185 | - } |
|
186 | - } |
|
187 | - $this->_mappings[$name] = $mapping; |
|
188 | - } |
|
189 | - return $this->_mappings[$name]; |
|
190 | - } |
|
191 | - |
|
192 | - /** |
|
193 | - * Convert to array. |
|
194 | - * |
|
195 | - * @return array |
|
196 | - */ |
|
197 | - public function toArray() |
|
198 | - { |
|
199 | - $data = $this->_additional; |
|
200 | - $data["keys"] = array_map( |
|
201 | - function (JWK $jwk) { |
|
202 | - return $jwk->toArray(); |
|
203 | - }, $this->_jwks); |
|
204 | - return $data; |
|
205 | - } |
|
206 | - |
|
207 | - /** |
|
208 | - * Convert to JSON. |
|
209 | - * |
|
210 | - * @return string |
|
211 | - */ |
|
212 | - public function toJSON() |
|
213 | - { |
|
214 | - return json_encode((object) $this->toArray(), JSON_UNESCAPED_SLASHES); |
|
215 | - } |
|
216 | - |
|
217 | - /** |
|
218 | - * Get the number of keys. |
|
219 | - * |
|
220 | - * @see \Countable::count() |
|
221 | - */ |
|
222 | - public function count() |
|
223 | - { |
|
224 | - return count($this->_jwks); |
|
225 | - } |
|
226 | - |
|
227 | - /** |
|
228 | - * Get iterator for JWK objects. |
|
229 | - * |
|
230 | - * @see \IteratorAggregate::getIterator() |
|
231 | - * @return \ArrayIterator |
|
232 | - */ |
|
233 | - public function getIterator() |
|
234 | - { |
|
235 | - return new \ArrayIterator($this->_jwks); |
|
236 | - } |
|
14 | + /** |
|
15 | + * JWK objects. |
|
16 | + * |
|
17 | + * @var JWK[] $_jwks |
|
18 | + */ |
|
19 | + protected $_jwks; |
|
20 | + |
|
21 | + /** |
|
22 | + * Additional members. |
|
23 | + * |
|
24 | + * @var array $_additional |
|
25 | + */ |
|
26 | + protected $_additional; |
|
27 | + |
|
28 | + /** |
|
29 | + * JWK mappings. |
|
30 | + * |
|
31 | + * @var array |
|
32 | + */ |
|
33 | + private $_mappings = array(); |
|
34 | + |
|
35 | + /** |
|
36 | + * Constructor. |
|
37 | + * |
|
38 | + * @param JWK ...$jwks |
|
39 | + */ |
|
40 | + public function __construct(JWK ...$jwks) |
|
41 | + { |
|
42 | + $this->_jwks = $jwks; |
|
43 | + $this->_additional = array(); |
|
44 | + } |
|
45 | + |
|
46 | + /** |
|
47 | + * Reset internal cache variables on clone. |
|
48 | + */ |
|
49 | + public function __clone() |
|
50 | + { |
|
51 | + $this->_mappings = array(); |
|
52 | + } |
|
53 | + |
|
54 | + /** |
|
55 | + * Initialize from an array representing a JSON object. |
|
56 | + * |
|
57 | + * @param array $members |
|
58 | + * @throws \UnexpectedValueException |
|
59 | + * @return self |
|
60 | + */ |
|
61 | + public static function fromArray(array $members) |
|
62 | + { |
|
63 | + if (!isset($members["keys"]) || !is_array($members["keys"])) { |
|
64 | + throw new \UnexpectedValueException( |
|
65 | + "JWK Set must have a 'keys' member."); |
|
66 | + } |
|
67 | + $jwks = array_map( |
|
68 | + function ($jwkdata) { |
|
69 | + return JWK::fromArray($jwkdata); |
|
70 | + }, $members["keys"]); |
|
71 | + unset($members["keys"]); |
|
72 | + $obj = new self(...$jwks); |
|
73 | + $obj->_additional = $members; |
|
74 | + return $obj; |
|
75 | + } |
|
76 | + |
|
77 | + /** |
|
78 | + * Initialize from a JSON string. |
|
79 | + * |
|
80 | + * @param string $json |
|
81 | + * @throws \UnexpectedValueException |
|
82 | + * @return self |
|
83 | + */ |
|
84 | + public static function fromJSON($json) |
|
85 | + { |
|
86 | + $members = json_decode($json, true, 32, JSON_BIGINT_AS_STRING); |
|
87 | + if (!is_array($members)) { |
|
88 | + throw new \UnexpectedValueException("Invalid JSON."); |
|
89 | + } |
|
90 | + return self::fromArray($members); |
|
91 | + } |
|
92 | + |
|
93 | + /** |
|
94 | + * Get self with keys added. |
|
95 | + * |
|
96 | + * @param JWK ...$keys JWK objects |
|
97 | + * @return self |
|
98 | + */ |
|
99 | + public function withKeys(JWK ...$keys) |
|
100 | + { |
|
101 | + $obj = clone $this; |
|
102 | + $obj->_jwks = array_merge($obj->_jwks, $keys); |
|
103 | + return $obj; |
|
104 | + } |
|
105 | + |
|
106 | + /** |
|
107 | + * Get all JWK's in a set. |
|
108 | + * |
|
109 | + * @return JWK[] |
|
110 | + */ |
|
111 | + public function keys() |
|
112 | + { |
|
113 | + return $this->_jwks; |
|
114 | + } |
|
115 | + |
|
116 | + /** |
|
117 | + * Get the first JWK in the set. |
|
118 | + * |
|
119 | + * @throws \LogicException |
|
120 | + * @return JWK |
|
121 | + */ |
|
122 | + public function first() |
|
123 | + { |
|
124 | + if (!count($this->_jwks)) { |
|
125 | + throw new \LogicException("No keys."); |
|
126 | + } |
|
127 | + return $this->_jwks[0]; |
|
128 | + } |
|
129 | + |
|
130 | + /** |
|
131 | + * Get JWK by key ID. |
|
132 | + * |
|
133 | + * @param string $id |
|
134 | + * @return JWK|null Null if not found |
|
135 | + */ |
|
136 | + protected function _getKeyByID($id) |
|
137 | + { |
|
138 | + $map = $this->_getMapping(JWKParameter::PARAM_KEY_ID); |
|
139 | + return isset($map[$id]) ? $map[$id] : null; |
|
140 | + } |
|
141 | + |
|
142 | + /** |
|
143 | + * Check whether set has a JWK with a given key ID. |
|
144 | + * |
|
145 | + * @param string $id |
|
146 | + * @return bool |
|
147 | + */ |
|
148 | + public function hasKeyID($id) |
|
149 | + { |
|
150 | + return $this->_getKeyByID($id) !== null; |
|
151 | + } |
|
152 | + |
|
153 | + /** |
|
154 | + * Get a JWK by a key ID. |
|
155 | + * |
|
156 | + * @param string $id |
|
157 | + * @throws \LogicException |
|
158 | + * @return JWK |
|
159 | + */ |
|
160 | + public function keyByID($id) |
|
161 | + { |
|
162 | + $jwk = $this->_getKeyByID($id); |
|
163 | + if (!$jwk) { |
|
164 | + throw new \LogicException("No key ID $id."); |
|
165 | + } |
|
166 | + return $jwk; |
|
167 | + } |
|
168 | + |
|
169 | + /** |
|
170 | + * Get mapping from parameter values of given parameter name to JWK. |
|
171 | + * |
|
172 | + * Later duplicate value shall override earlier JWK. |
|
173 | + * |
|
174 | + * @param string $name Parameter name |
|
175 | + * @return array |
|
176 | + */ |
|
177 | + protected function _getMapping($name) |
|
178 | + { |
|
179 | + if (!isset($this->_mappings[$name])) { |
|
180 | + $mapping = array(); |
|
181 | + foreach ($this->_jwks as $jwk) { |
|
182 | + if ($jwk->has($name)) { |
|
183 | + $key = (string) $jwk->get($name)->value(); |
|
184 | + $mapping[$key] = $jwk; |
|
185 | + } |
|
186 | + } |
|
187 | + $this->_mappings[$name] = $mapping; |
|
188 | + } |
|
189 | + return $this->_mappings[$name]; |
|
190 | + } |
|
191 | + |
|
192 | + /** |
|
193 | + * Convert to array. |
|
194 | + * |
|
195 | + * @return array |
|
196 | + */ |
|
197 | + public function toArray() |
|
198 | + { |
|
199 | + $data = $this->_additional; |
|
200 | + $data["keys"] = array_map( |
|
201 | + function (JWK $jwk) { |
|
202 | + return $jwk->toArray(); |
|
203 | + }, $this->_jwks); |
|
204 | + return $data; |
|
205 | + } |
|
206 | + |
|
207 | + /** |
|
208 | + * Convert to JSON. |
|
209 | + * |
|
210 | + * @return string |
|
211 | + */ |
|
212 | + public function toJSON() |
|
213 | + { |
|
214 | + return json_encode((object) $this->toArray(), JSON_UNESCAPED_SLASHES); |
|
215 | + } |
|
216 | + |
|
217 | + /** |
|
218 | + * Get the number of keys. |
|
219 | + * |
|
220 | + * @see \Countable::count() |
|
221 | + */ |
|
222 | + public function count() |
|
223 | + { |
|
224 | + return count($this->_jwks); |
|
225 | + } |
|
226 | + |
|
227 | + /** |
|
228 | + * Get iterator for JWK objects. |
|
229 | + * |
|
230 | + * @see \IteratorAggregate::getIterator() |
|
231 | + * @return \ArrayIterator |
|
232 | + */ |
|
233 | + public function getIterator() |
|
234 | + { |
|
235 | + return new \ArrayIterator($this->_jwks); |
|
236 | + } |
|
237 | 237 | } |
@@ -65,7 +65,7 @@ discard block |
||
65 | 65 | "JWK Set must have a 'keys' member."); |
66 | 66 | } |
67 | 67 | $jwks = array_map( |
68 | - function ($jwkdata) { |
|
68 | + function($jwkdata) { |
|
69 | 69 | return JWK::fromArray($jwkdata); |
70 | 70 | }, $members["keys"]); |
71 | 71 | unset($members["keys"]); |
@@ -198,7 +198,7 @@ discard block |
||
198 | 198 | { |
199 | 199 | $data = $this->_additional; |
200 | 200 | $data["keys"] = array_map( |
201 | - function (JWK $jwk) { |
|
201 | + function(JWK $jwk) { |
|
202 | 202 | return $jwk->toArray(); |
203 | 203 | }, $this->_jwks); |
204 | 204 | return $data; |