@@ -13,30 +13,30 @@ |
||
| 13 | 13 | */ |
| 14 | 14 | class KeyTypeParameter extends JWKParameter |
| 15 | 15 | { |
| 16 | - use StringParameterValue; |
|
| 17 | - |
|
| 18 | - /** |
|
| 19 | - * Octet sequence key type. |
|
| 20 | - */ |
|
| 21 | - const TYPE_OCT = 'oct'; |
|
| 22 | - |
|
| 23 | - /** |
|
| 24 | - * RSA key type. |
|
| 25 | - */ |
|
| 26 | - const TYPE_RSA = 'RSA'; |
|
| 27 | - |
|
| 28 | - /** |
|
| 29 | - * Elliptic curve key type. |
|
| 30 | - */ |
|
| 31 | - const TYPE_EC = 'EC'; |
|
| 32 | - |
|
| 33 | - /** |
|
| 34 | - * Constructor. |
|
| 35 | - * |
|
| 36 | - * @param string $type Key type |
|
| 37 | - */ |
|
| 38 | - public function __construct(string $type) |
|
| 39 | - { |
|
| 40 | - parent::__construct(self::PARAM_KEY_TYPE, $type); |
|
| 41 | - } |
|
| 16 | + use StringParameterValue; |
|
| 17 | + |
|
| 18 | + /** |
|
| 19 | + * Octet sequence key type. |
|
| 20 | + */ |
|
| 21 | + const TYPE_OCT = 'oct'; |
|
| 22 | + |
|
| 23 | + /** |
|
| 24 | + * RSA key type. |
|
| 25 | + */ |
|
| 26 | + const TYPE_RSA = 'RSA'; |
|
| 27 | + |
|
| 28 | + /** |
|
| 29 | + * Elliptic curve key type. |
|
| 30 | + */ |
|
| 31 | + const TYPE_EC = 'EC'; |
|
| 32 | + |
|
| 33 | + /** |
|
| 34 | + * Constructor. |
|
| 35 | + * |
|
| 36 | + * @param string $type Key type |
|
| 37 | + */ |
|
| 38 | + public function __construct(string $type) |
|
| 39 | + { |
|
| 40 | + parent::__construct(self::PARAM_KEY_TYPE, $type); |
|
| 41 | + } |
|
| 42 | 42 | } |
@@ -1,6 +1,6 @@ |
||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | -declare(strict_types = 1); |
|
| 3 | +declare(strict_types=1); |
|
| 4 | 4 | |
| 5 | 5 | namespace Sop\JWX\JWK\Parameter; |
| 6 | 6 | |
@@ -14,131 +14,131 @@ |
||
| 14 | 14 | */ |
| 15 | 15 | class JWKParameter extends Parameter |
| 16 | 16 | { |
| 17 | - // registered parameter names |
|
| 18 | - const PARAM_KEY_TYPE = 'kty'; |
|
| 19 | - const PARAM_PUBLIC_KEY_USE = 'use'; |
|
| 20 | - const PARAM_KEY_OPERATIONS = 'key_ops'; |
|
| 21 | - const PARAM_ALGORITHM = 'alg'; |
|
| 22 | - const PARAM_KEY_ID = 'kid'; |
|
| 23 | - const PARAM_X509_URL = 'x5u'; |
|
| 24 | - const PARAM_X509_CERTIFICATE_CHAIN = 'x5c'; |
|
| 25 | - const PARAM_X509_CERTIFICATE_SHA1_THUMBPRINT = 'x5t'; |
|
| 26 | - const PARAM_X509_CERTIFICATE_SHA256_THUMBPRINT = 'x5t#S256'; |
|
| 27 | - const PARAM_CURVE = 'crv'; |
|
| 28 | - const PARAM_X_COORDINATE = 'x'; |
|
| 29 | - const PARAM_Y_COORDINATE = 'y'; |
|
| 30 | - const PARAM_ECC_PRIVATE_KEY = 'd'; |
|
| 31 | - const PARAM_MODULUS = 'n'; |
|
| 32 | - const PARAM_EXPONENT = 'e'; |
|
| 33 | - const PARAM_PRIVATE_EXPONENT = 'd'; |
|
| 34 | - const PARAM_FIRST_PRIME_FACTOR = 'p'; |
|
| 35 | - const PARAM_SECOND_PRIME_FACTOR = 'q'; |
|
| 36 | - const PARAM_FIRST_FACTOR_CRT_EXPONENT = 'dp'; |
|
| 37 | - const PARAM_SECOND_FACTOR_CRT_EXPONENT = 'dq'; |
|
| 38 | - const PARAM_FIRST_CRT_COEFFICIENT = 'qi'; |
|
| 39 | - const PARAM_OTHER_PRIMES_INFO = 'oth'; |
|
| 40 | - const PARAM_KEY_VALUE = 'k'; |
|
| 17 | + // registered parameter names |
|
| 18 | + const PARAM_KEY_TYPE = 'kty'; |
|
| 19 | + const PARAM_PUBLIC_KEY_USE = 'use'; |
|
| 20 | + const PARAM_KEY_OPERATIONS = 'key_ops'; |
|
| 21 | + const PARAM_ALGORITHM = 'alg'; |
|
| 22 | + const PARAM_KEY_ID = 'kid'; |
|
| 23 | + const PARAM_X509_URL = 'x5u'; |
|
| 24 | + const PARAM_X509_CERTIFICATE_CHAIN = 'x5c'; |
|
| 25 | + const PARAM_X509_CERTIFICATE_SHA1_THUMBPRINT = 'x5t'; |
|
| 26 | + const PARAM_X509_CERTIFICATE_SHA256_THUMBPRINT = 'x5t#S256'; |
|
| 27 | + const PARAM_CURVE = 'crv'; |
|
| 28 | + const PARAM_X_COORDINATE = 'x'; |
|
| 29 | + const PARAM_Y_COORDINATE = 'y'; |
|
| 30 | + const PARAM_ECC_PRIVATE_KEY = 'd'; |
|
| 31 | + const PARAM_MODULUS = 'n'; |
|
| 32 | + const PARAM_EXPONENT = 'e'; |
|
| 33 | + const PARAM_PRIVATE_EXPONENT = 'd'; |
|
| 34 | + const PARAM_FIRST_PRIME_FACTOR = 'p'; |
|
| 35 | + const PARAM_SECOND_PRIME_FACTOR = 'q'; |
|
| 36 | + const PARAM_FIRST_FACTOR_CRT_EXPONENT = 'dp'; |
|
| 37 | + const PARAM_SECOND_FACTOR_CRT_EXPONENT = 'dq'; |
|
| 38 | + const PARAM_FIRST_CRT_COEFFICIENT = 'qi'; |
|
| 39 | + const PARAM_OTHER_PRIMES_INFO = 'oth'; |
|
| 40 | + const PARAM_KEY_VALUE = 'k'; |
|
| 41 | 41 | |
| 42 | - // shorthand aliases for parameter names |
|
| 43 | - const P_KTY = self::PARAM_KEY_TYPE; |
|
| 44 | - const P_USE = self::PARAM_PUBLIC_KEY_USE; |
|
| 45 | - const P_KEY_OPS = self::PARAM_KEY_OPERATIONS; |
|
| 46 | - const P_ALG = self::PARAM_ALGORITHM; |
|
| 47 | - const P_KID = self::PARAM_KEY_ID; |
|
| 48 | - const P_X5U = self::PARAM_X509_URL; |
|
| 49 | - const P_X5C = self::PARAM_X509_CERTIFICATE_CHAIN; |
|
| 50 | - const P_X5T = self::PARAM_X509_CERTIFICATE_SHA1_THUMBPRINT; |
|
| 51 | - const P_X5TS256 = self::PARAM_X509_CERTIFICATE_SHA256_THUMBPRINT; |
|
| 52 | - const P_CRV = self::PARAM_CURVE; |
|
| 53 | - const P_X = self::PARAM_X_COORDINATE; |
|
| 54 | - const P_Y = self::PARAM_Y_COORDINATE; |
|
| 55 | - const P_ECC_D = self::PARAM_ECC_PRIVATE_KEY; |
|
| 56 | - const P_N = self::PARAM_MODULUS; |
|
| 57 | - const P_E = self::PARAM_EXPONENT; |
|
| 58 | - const P_RSA_D = self::PARAM_PRIVATE_EXPONENT; |
|
| 59 | - const P_P = self::PARAM_FIRST_PRIME_FACTOR; |
|
| 60 | - const P_Q = self::PARAM_SECOND_PRIME_FACTOR; |
|
| 61 | - const P_DP = self::PARAM_FIRST_FACTOR_CRT_EXPONENT; |
|
| 62 | - const P_DQ = self::PARAM_SECOND_FACTOR_CRT_EXPONENT; |
|
| 63 | - const P_QI = self::PARAM_FIRST_CRT_COEFFICIENT; |
|
| 64 | - const P_OTH = self::PARAM_OTHER_PRIMES_INFO; |
|
| 65 | - const P_K = self::PARAM_KEY_VALUE; |
|
| 42 | + // shorthand aliases for parameter names |
|
| 43 | + const P_KTY = self::PARAM_KEY_TYPE; |
|
| 44 | + const P_USE = self::PARAM_PUBLIC_KEY_USE; |
|
| 45 | + const P_KEY_OPS = self::PARAM_KEY_OPERATIONS; |
|
| 46 | + const P_ALG = self::PARAM_ALGORITHM; |
|
| 47 | + const P_KID = self::PARAM_KEY_ID; |
|
| 48 | + const P_X5U = self::PARAM_X509_URL; |
|
| 49 | + const P_X5C = self::PARAM_X509_CERTIFICATE_CHAIN; |
|
| 50 | + const P_X5T = self::PARAM_X509_CERTIFICATE_SHA1_THUMBPRINT; |
|
| 51 | + const P_X5TS256 = self::PARAM_X509_CERTIFICATE_SHA256_THUMBPRINT; |
|
| 52 | + const P_CRV = self::PARAM_CURVE; |
|
| 53 | + const P_X = self::PARAM_X_COORDINATE; |
|
| 54 | + const P_Y = self::PARAM_Y_COORDINATE; |
|
| 55 | + const P_ECC_D = self::PARAM_ECC_PRIVATE_KEY; |
|
| 56 | + const P_N = self::PARAM_MODULUS; |
|
| 57 | + const P_E = self::PARAM_EXPONENT; |
|
| 58 | + const P_RSA_D = self::PARAM_PRIVATE_EXPONENT; |
|
| 59 | + const P_P = self::PARAM_FIRST_PRIME_FACTOR; |
|
| 60 | + const P_Q = self::PARAM_SECOND_PRIME_FACTOR; |
|
| 61 | + const P_DP = self::PARAM_FIRST_FACTOR_CRT_EXPONENT; |
|
| 62 | + const P_DQ = self::PARAM_SECOND_FACTOR_CRT_EXPONENT; |
|
| 63 | + const P_QI = self::PARAM_FIRST_CRT_COEFFICIENT; |
|
| 64 | + const P_OTH = self::PARAM_OTHER_PRIMES_INFO; |
|
| 65 | + const P_K = self::PARAM_KEY_VALUE; |
|
| 66 | 66 | |
| 67 | - /** |
|
| 68 | - * Mapping from registered JWK parameter name to class name. |
|
| 69 | - * |
|
| 70 | - * Note that ECC private key and RSA private key cannot be mapped since |
|
| 71 | - * they share the same parameter name 'd'. |
|
| 72 | - * |
|
| 73 | - * @internal |
|
| 74 | - * |
|
| 75 | - * @var array |
|
| 76 | - */ |
|
| 77 | - const MAP_NAME_TO_CLASS = [ |
|
| 78 | - self::P_KTY => KeyTypeParameter::class, |
|
| 79 | - self::P_USE => PublicKeyUseParameter::class, |
|
| 80 | - self::P_KEY_OPS => KeyOperationsParameter::class, |
|
| 81 | - self::P_ALG => AlgorithmParameter::class, |
|
| 82 | - self::P_KID => KeyIDParameter::class, |
|
| 83 | - self::P_X5U => X509URLParameter::class, |
|
| 84 | - self::P_X5C => X509CertificateChainParameter::class, |
|
| 85 | - self::P_X5T => X509CertificateSHA1ThumbprintParameter::class, |
|
| 86 | - self::P_X5TS256 => X509CertificateSHA256ThumbprintParameter::class, |
|
| 87 | - self::P_CRV => CurveParameter::class, |
|
| 88 | - self::P_X => XCoordinateParameter::class, |
|
| 89 | - self::P_Y => YCoordinateParameter::class, |
|
| 90 | - self::P_N => ModulusParameter::class, |
|
| 91 | - self::P_E => ExponentParameter::class, |
|
| 92 | - self::P_P => FirstPrimeFactorParameter::class, |
|
| 93 | - self::P_Q => SecondPrimeFactorParameter::class, |
|
| 94 | - self::P_DP => FirstFactorCRTExponentParameter::class, |
|
| 95 | - self::P_DQ => SecondFactorCRTExponentParameter::class, |
|
| 96 | - self::P_QI => FirstCRTCoefficientParameter::class, |
|
| 97 | - self::P_OTH => OtherPrimesInfoParameter::class, |
|
| 98 | - self::P_K => KeyValueParameter::class, |
|
| 99 | - ]; |
|
| 67 | + /** |
|
| 68 | + * Mapping from registered JWK parameter name to class name. |
|
| 69 | + * |
|
| 70 | + * Note that ECC private key and RSA private key cannot be mapped since |
|
| 71 | + * they share the same parameter name 'd'. |
|
| 72 | + * |
|
| 73 | + * @internal |
|
| 74 | + * |
|
| 75 | + * @var array |
|
| 76 | + */ |
|
| 77 | + const MAP_NAME_TO_CLASS = [ |
|
| 78 | + self::P_KTY => KeyTypeParameter::class, |
|
| 79 | + self::P_USE => PublicKeyUseParameter::class, |
|
| 80 | + self::P_KEY_OPS => KeyOperationsParameter::class, |
|
| 81 | + self::P_ALG => AlgorithmParameter::class, |
|
| 82 | + self::P_KID => KeyIDParameter::class, |
|
| 83 | + self::P_X5U => X509URLParameter::class, |
|
| 84 | + self::P_X5C => X509CertificateChainParameter::class, |
|
| 85 | + self::P_X5T => X509CertificateSHA1ThumbprintParameter::class, |
|
| 86 | + self::P_X5TS256 => X509CertificateSHA256ThumbprintParameter::class, |
|
| 87 | + self::P_CRV => CurveParameter::class, |
|
| 88 | + self::P_X => XCoordinateParameter::class, |
|
| 89 | + self::P_Y => YCoordinateParameter::class, |
|
| 90 | + self::P_N => ModulusParameter::class, |
|
| 91 | + self::P_E => ExponentParameter::class, |
|
| 92 | + self::P_P => FirstPrimeFactorParameter::class, |
|
| 93 | + self::P_Q => SecondPrimeFactorParameter::class, |
|
| 94 | + self::P_DP => FirstFactorCRTExponentParameter::class, |
|
| 95 | + self::P_DQ => SecondFactorCRTExponentParameter::class, |
|
| 96 | + self::P_QI => FirstCRTCoefficientParameter::class, |
|
| 97 | + self::P_OTH => OtherPrimesInfoParameter::class, |
|
| 98 | + self::P_K => KeyValueParameter::class, |
|
| 99 | + ]; |
|
| 100 | 100 | |
| 101 | - /** |
|
| 102 | - * Constructor. |
|
| 103 | - * |
|
| 104 | - * @param string $name Parameter name |
|
| 105 | - * @param mixed $value Parameter value |
|
| 106 | - */ |
|
| 107 | - public function __construct(string $name, $value) |
|
| 108 | - { |
|
| 109 | - $this->_name = $name; |
|
| 110 | - $this->_value = $value; |
|
| 111 | - } |
|
| 101 | + /** |
|
| 102 | + * Constructor. |
|
| 103 | + * |
|
| 104 | + * @param string $name Parameter name |
|
| 105 | + * @param mixed $value Parameter value |
|
| 106 | + */ |
|
| 107 | + public function __construct(string $name, $value) |
|
| 108 | + { |
|
| 109 | + $this->_name = $name; |
|
| 110 | + $this->_value = $value; |
|
| 111 | + } |
|
| 112 | 112 | |
| 113 | - /** |
|
| 114 | - * Initialize from a name and a value. |
|
| 115 | - * |
|
| 116 | - * Returns a parameter specific object if one is implemented. |
|
| 117 | - * |
|
| 118 | - * @param string $name Parameter name |
|
| 119 | - * @param mixed $value Parameter value |
|
| 120 | - * |
|
| 121 | - * @return self |
|
| 122 | - */ |
|
| 123 | - public static function fromNameAndValue(string $name, $value): self |
|
| 124 | - { |
|
| 125 | - if (array_key_exists($name, self::MAP_NAME_TO_CLASS)) { |
|
| 126 | - $cls = self::MAP_NAME_TO_CLASS[$name]; |
|
| 127 | - return $cls::fromJSONValue($value); |
|
| 128 | - } |
|
| 129 | - return new self($name, $value); |
|
| 130 | - } |
|
| 113 | + /** |
|
| 114 | + * Initialize from a name and a value. |
|
| 115 | + * |
|
| 116 | + * Returns a parameter specific object if one is implemented. |
|
| 117 | + * |
|
| 118 | + * @param string $name Parameter name |
|
| 119 | + * @param mixed $value Parameter value |
|
| 120 | + * |
|
| 121 | + * @return self |
|
| 122 | + */ |
|
| 123 | + public static function fromNameAndValue(string $name, $value): self |
|
| 124 | + { |
|
| 125 | + if (array_key_exists($name, self::MAP_NAME_TO_CLASS)) { |
|
| 126 | + $cls = self::MAP_NAME_TO_CLASS[$name]; |
|
| 127 | + return $cls::fromJSONValue($value); |
|
| 128 | + } |
|
| 129 | + return new self($name, $value); |
|
| 130 | + } |
|
| 131 | 131 | |
| 132 | - /** |
|
| 133 | - * Initialize from a JSON value. |
|
| 134 | - * |
|
| 135 | - * @param mixed $value |
|
| 136 | - * |
|
| 137 | - * @return JWKParameter |
|
| 138 | - */ |
|
| 139 | - public static function fromJSONValue($value): Parameter |
|
| 140 | - { |
|
| 141 | - throw new \BadMethodCallException( |
|
| 142 | - __FUNCTION__ . ' must be implemented in a derived class.'); |
|
| 143 | - } |
|
| 132 | + /** |
|
| 133 | + * Initialize from a JSON value. |
|
| 134 | + * |
|
| 135 | + * @param mixed $value |
|
| 136 | + * |
|
| 137 | + * @return JWKParameter |
|
| 138 | + */ |
|
| 139 | + public static function fromJSONValue($value): Parameter |
|
| 140 | + { |
|
| 141 | + throw new \BadMethodCallException( |
|
| 142 | + __FUNCTION__ . ' must be implemented in a derived class.'); |
|
| 143 | + } |
|
| 144 | 144 | } |
@@ -1,6 +1,6 @@ |
||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | -declare(strict_types = 1); |
|
| 3 | +declare(strict_types=1); |
|
| 4 | 4 | |
| 5 | 5 | namespace Sop\JWX\JWK\Parameter; |
| 6 | 6 | |
@@ -13,117 +13,117 @@ |
||
| 13 | 13 | */ |
| 14 | 14 | class CurveParameter extends JWKParameter |
| 15 | 15 | { |
| 16 | - use StringParameterValue; |
|
| 16 | + use StringParameterValue; |
|
| 17 | 17 | |
| 18 | - /** |
|
| 19 | - * P-256 Curve. |
|
| 20 | - * |
|
| 21 | - * @var string |
|
| 22 | - */ |
|
| 23 | - const CURVE_P256 = 'P-256'; |
|
| 18 | + /** |
|
| 19 | + * P-256 Curve. |
|
| 20 | + * |
|
| 21 | + * @var string |
|
| 22 | + */ |
|
| 23 | + const CURVE_P256 = 'P-256'; |
|
| 24 | 24 | |
| 25 | - /** |
|
| 26 | - * P-384 Curve. |
|
| 27 | - * |
|
| 28 | - * @var string |
|
| 29 | - */ |
|
| 30 | - const CURVE_P384 = 'P-384'; |
|
| 25 | + /** |
|
| 26 | + * P-384 Curve. |
|
| 27 | + * |
|
| 28 | + * @var string |
|
| 29 | + */ |
|
| 30 | + const CURVE_P384 = 'P-384'; |
|
| 31 | 31 | |
| 32 | - /** |
|
| 33 | - * P-521 Curve. |
|
| 34 | - * |
|
| 35 | - * @var string |
|
| 36 | - */ |
|
| 37 | - const CURVE_P521 = 'P-521'; |
|
| 32 | + /** |
|
| 33 | + * P-521 Curve. |
|
| 34 | + * |
|
| 35 | + * @var string |
|
| 36 | + */ |
|
| 37 | + const CURVE_P521 = 'P-521'; |
|
| 38 | 38 | |
| 39 | - /** |
|
| 40 | - * Mapping from curve OID to curve name. |
|
| 41 | - * |
|
| 42 | - * @internal |
|
| 43 | - * |
|
| 44 | - * @var array |
|
| 45 | - */ |
|
| 46 | - const MAP_OID_TO_CURVE = [ |
|
| 47 | - '1.2.840.10045.3.1.7' => self::CURVE_P256, |
|
| 48 | - '1.3.132.0.34' => self::CURVE_P384, |
|
| 49 | - '1.3.132.0.35' => self::CURVE_P521, |
|
| 50 | - ]; |
|
| 39 | + /** |
|
| 40 | + * Mapping from curve OID to curve name. |
|
| 41 | + * |
|
| 42 | + * @internal |
|
| 43 | + * |
|
| 44 | + * @var array |
|
| 45 | + */ |
|
| 46 | + const MAP_OID_TO_CURVE = [ |
|
| 47 | + '1.2.840.10045.3.1.7' => self::CURVE_P256, |
|
| 48 | + '1.3.132.0.34' => self::CURVE_P384, |
|
| 49 | + '1.3.132.0.35' => self::CURVE_P521, |
|
| 50 | + ]; |
|
| 51 | 51 | |
| 52 | - /** |
|
| 53 | - * Mapping from curve name to bit size. |
|
| 54 | - * |
|
| 55 | - * @internal |
|
| 56 | - * |
|
| 57 | - * @var array |
|
| 58 | - */ |
|
| 59 | - const MAP_CURVE_TO_SIZE = [ |
|
| 60 | - self::CURVE_P256 => 256, |
|
| 61 | - self::CURVE_P384 => 384, |
|
| 62 | - self::CURVE_P521 => 521, |
|
| 63 | - ]; |
|
| 52 | + /** |
|
| 53 | + * Mapping from curve name to bit size. |
|
| 54 | + * |
|
| 55 | + * @internal |
|
| 56 | + * |
|
| 57 | + * @var array |
|
| 58 | + */ |
|
| 59 | + const MAP_CURVE_TO_SIZE = [ |
|
| 60 | + self::CURVE_P256 => 256, |
|
| 61 | + self::CURVE_P384 => 384, |
|
| 62 | + self::CURVE_P521 => 521, |
|
| 63 | + ]; |
|
| 64 | 64 | |
| 65 | - /** |
|
| 66 | - * Constructor. |
|
| 67 | - * |
|
| 68 | - * @param string $curve Curve name |
|
| 69 | - */ |
|
| 70 | - public function __construct(string $curve) |
|
| 71 | - { |
|
| 72 | - parent::__construct(self::PARAM_CURVE, $curve); |
|
| 73 | - } |
|
| 65 | + /** |
|
| 66 | + * Constructor. |
|
| 67 | + * |
|
| 68 | + * @param string $curve Curve name |
|
| 69 | + */ |
|
| 70 | + public function __construct(string $curve) |
|
| 71 | + { |
|
| 72 | + parent::__construct(self::PARAM_CURVE, $curve); |
|
| 73 | + } |
|
| 74 | 74 | |
| 75 | - /** |
|
| 76 | - * Initialize from curve OID. |
|
| 77 | - * |
|
| 78 | - * @param string $oid Object identifier in dotted format |
|
| 79 | - * |
|
| 80 | - * @throws \UnexpectedValueException If the curve is not supported |
|
| 81 | - * |
|
| 82 | - * @return self |
|
| 83 | - */ |
|
| 84 | - public static function fromOID(string $oid): self |
|
| 85 | - { |
|
| 86 | - if (!array_key_exists($oid, self::MAP_OID_TO_CURVE)) { |
|
| 87 | - throw new \UnexpectedValueException("OID {$oid} not supported."); |
|
| 88 | - } |
|
| 89 | - $curve = self::MAP_OID_TO_CURVE[$oid]; |
|
| 90 | - return new self($curve); |
|
| 91 | - } |
|
| 75 | + /** |
|
| 76 | + * Initialize from curve OID. |
|
| 77 | + * |
|
| 78 | + * @param string $oid Object identifier in dotted format |
|
| 79 | + * |
|
| 80 | + * @throws \UnexpectedValueException If the curve is not supported |
|
| 81 | + * |
|
| 82 | + * @return self |
|
| 83 | + */ |
|
| 84 | + public static function fromOID(string $oid): self |
|
| 85 | + { |
|
| 86 | + if (!array_key_exists($oid, self::MAP_OID_TO_CURVE)) { |
|
| 87 | + throw new \UnexpectedValueException("OID {$oid} not supported."); |
|
| 88 | + } |
|
| 89 | + $curve = self::MAP_OID_TO_CURVE[$oid]; |
|
| 90 | + return new self($curve); |
|
| 91 | + } |
|
| 92 | 92 | |
| 93 | - /** |
|
| 94 | - * Get key size in bits for the curve. |
|
| 95 | - * |
|
| 96 | - * @throws \UnexpectedValueException |
|
| 97 | - * |
|
| 98 | - * @return int |
|
| 99 | - */ |
|
| 100 | - public function keySizeBits(): int |
|
| 101 | - { |
|
| 102 | - if (!array_key_exists($this->_value, self::MAP_CURVE_TO_SIZE)) { |
|
| 103 | - throw new \UnexpectedValueException( |
|
| 104 | - 'Curve ' . $this->_value . ' not supported.'); |
|
| 105 | - } |
|
| 106 | - return self::MAP_CURVE_TO_SIZE[$this->_value]; |
|
| 107 | - } |
|
| 93 | + /** |
|
| 94 | + * Get key size in bits for the curve. |
|
| 95 | + * |
|
| 96 | + * @throws \UnexpectedValueException |
|
| 97 | + * |
|
| 98 | + * @return int |
|
| 99 | + */ |
|
| 100 | + public function keySizeBits(): int |
|
| 101 | + { |
|
| 102 | + if (!array_key_exists($this->_value, self::MAP_CURVE_TO_SIZE)) { |
|
| 103 | + throw new \UnexpectedValueException( |
|
| 104 | + 'Curve ' . $this->_value . ' not supported.'); |
|
| 105 | + } |
|
| 106 | + return self::MAP_CURVE_TO_SIZE[$this->_value]; |
|
| 107 | + } |
|
| 108 | 108 | |
| 109 | - /** |
|
| 110 | - * Get the curve OID by curve name. |
|
| 111 | - * |
|
| 112 | - * @param string $name Curve parameter name |
|
| 113 | - * |
|
| 114 | - * @throws \UnexpectedValueException If the curve is not supported |
|
| 115 | - * |
|
| 116 | - * @return string OID in dotted format |
|
| 117 | - */ |
|
| 118 | - public static function nameToOID(string $name): string |
|
| 119 | - { |
|
| 120 | - static $reverseMap; |
|
| 121 | - if (!isset($reverseMap)) { |
|
| 122 | - $reverseMap = array_flip(self::MAP_OID_TO_CURVE); |
|
| 123 | - } |
|
| 124 | - if (!isset($reverseMap[$name])) { |
|
| 125 | - throw new \UnexpectedValueException("Curve {$name} not supported."); |
|
| 126 | - } |
|
| 127 | - return $reverseMap[$name]; |
|
| 128 | - } |
|
| 109 | + /** |
|
| 110 | + * Get the curve OID by curve name. |
|
| 111 | + * |
|
| 112 | + * @param string $name Curve parameter name |
|
| 113 | + * |
|
| 114 | + * @throws \UnexpectedValueException If the curve is not supported |
|
| 115 | + * |
|
| 116 | + * @return string OID in dotted format |
|
| 117 | + */ |
|
| 118 | + public static function nameToOID(string $name): string |
|
| 119 | + { |
|
| 120 | + static $reverseMap; |
|
| 121 | + if (!isset($reverseMap)) { |
|
| 122 | + $reverseMap = array_flip(self::MAP_OID_TO_CURVE); |
|
| 123 | + } |
|
| 124 | + if (!isset($reverseMap[$name])) { |
|
| 125 | + throw new \UnexpectedValueException("Curve {$name} not supported."); |
|
| 126 | + } |
|
| 127 | + return $reverseMap[$name]; |
|
| 128 | + } |
|
| 129 | 129 | } |
@@ -1,6 +1,6 @@ |
||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | -declare(strict_types = 1); |
|
| 3 | +declare(strict_types=1); |
|
| 4 | 4 | |
| 5 | 5 | namespace Sop\JWX\JWK\Parameter; |
| 6 | 6 | |
@@ -11,594 +11,594 @@ |
||
| 11 | 11 | */ |
| 12 | 12 | trait TypedJWK |
| 13 | 13 | { |
| 14 | - /** |
|
| 15 | - * Whether parameters are present. |
|
| 16 | - * |
|
| 17 | - * @param string ...$names Parameter names |
|
| 18 | - * |
|
| 19 | - * @return bool |
|
| 20 | - */ |
|
| 21 | - abstract public function has(string ...$names): bool; |
|
| 22 | - |
|
| 23 | - /** |
|
| 24 | - * Get a parameter. |
|
| 25 | - * |
|
| 26 | - * @param string $name Parameter name |
|
| 27 | - * |
|
| 28 | - * @return JWKParameter |
|
| 29 | - */ |
|
| 30 | - abstract public function get(string $name): JWKParameter; |
|
| 31 | - |
|
| 32 | - /** |
|
| 33 | - * Check whether the algorithm parameter is present. |
|
| 34 | - * |
|
| 35 | - * @return bool |
|
| 36 | - */ |
|
| 37 | - public function hasAlgorithmParameter(): bool |
|
| 38 | - { |
|
| 39 | - return $this->has(Parameter\JWKParameter::P_ALG); |
|
| 40 | - } |
|
| 41 | - |
|
| 42 | - /** |
|
| 43 | - * Get the algorithm parameter. |
|
| 44 | - * |
|
| 45 | - * @throws \UnexpectedValueException If the parameter has a wrong class |
|
| 46 | - * @throws \LogicException If the parameter is not present |
|
| 47 | - * |
|
| 48 | - * @return Parameter\AlgorithmParameter |
|
| 49 | - */ |
|
| 50 | - public function algorithmParameter(): Parameter\AlgorithmParameter |
|
| 51 | - { |
|
| 52 | - return self::_checkType($this->get(Parameter\JWKParameter::P_ALG), |
|
| 53 | - Parameter\AlgorithmParameter::class); |
|
| 54 | - } |
|
| 55 | - |
|
| 56 | - /** |
|
| 57 | - * Check whether the curve parameter is present. |
|
| 58 | - * |
|
| 59 | - * @return bool |
|
| 60 | - */ |
|
| 61 | - public function hasCurveParameter(): bool |
|
| 62 | - { |
|
| 63 | - return $this->has(Parameter\JWKParameter::P_CRV); |
|
| 64 | - } |
|
| 65 | - |
|
| 66 | - /** |
|
| 67 | - * Get the curve parameter. |
|
| 68 | - * |
|
| 69 | - * @throws \UnexpectedValueException If the parameter has a wrong class |
|
| 70 | - * @throws \LogicException If the parameter is not present |
|
| 71 | - * |
|
| 72 | - * @return Parameter\CurveParameter |
|
| 73 | - */ |
|
| 74 | - public function curveParameter(): Parameter\CurveParameter |
|
| 75 | - { |
|
| 76 | - return self::_checkType($this->get(Parameter\JWKParameter::P_CRV), |
|
| 77 | - Parameter\CurveParameter::class); |
|
| 78 | - } |
|
| 79 | - |
|
| 80 | - /** |
|
| 81 | - * Check whether the ECC private key parameter is present. |
|
| 82 | - * |
|
| 83 | - * @return bool |
|
| 84 | - */ |
|
| 85 | - public function hasECCPrivateKeyParameter(): bool |
|
| 86 | - { |
|
| 87 | - return $this->has(Parameter\JWKParameter::P_ECC_D); |
|
| 88 | - } |
|
| 89 | - |
|
| 90 | - /** |
|
| 91 | - * Get the ECC private key parameter. |
|
| 92 | - * |
|
| 93 | - * @throws \UnexpectedValueException If the parameter has a wrong class |
|
| 94 | - * @throws \LogicException If the parameter is not present |
|
| 95 | - * |
|
| 96 | - * @return Parameter\ECCPrivateKeyParameter |
|
| 97 | - */ |
|
| 98 | - public function ECCPrivateKeyParameter(): Parameter\ECCPrivateKeyParameter |
|
| 99 | - { |
|
| 100 | - return self::_checkType($this->get(Parameter\JWKParameter::P_ECC_D), |
|
| 101 | - Parameter\ECCPrivateKeyParameter::class); |
|
| 102 | - } |
|
| 103 | - |
|
| 104 | - /** |
|
| 105 | - * Check whether the exponent parameter is present. |
|
| 106 | - * |
|
| 107 | - * @return bool |
|
| 108 | - */ |
|
| 109 | - public function hasExponentParameter(): bool |
|
| 110 | - { |
|
| 111 | - return $this->has(Parameter\JWKParameter::P_E); |
|
| 112 | - } |
|
| 113 | - |
|
| 114 | - /** |
|
| 115 | - * Get the exponent parameter. |
|
| 116 | - * |
|
| 117 | - * @throws \UnexpectedValueException If the parameter has a wrong class |
|
| 118 | - * @throws \LogicException If the parameter is not present |
|
| 119 | - * |
|
| 120 | - * @return Parameter\ExponentParameter |
|
| 121 | - */ |
|
| 122 | - public function exponentParameter(): Parameter\ExponentParameter |
|
| 123 | - { |
|
| 124 | - return self::_checkType($this->get(Parameter\JWKParameter::P_E), |
|
| 125 | - Parameter\ExponentParameter::class); |
|
| 126 | - } |
|
| 127 | - |
|
| 128 | - /** |
|
| 129 | - * Check whether the first CRT coefficient parameter is present. |
|
| 130 | - * |
|
| 131 | - * @return bool |
|
| 132 | - */ |
|
| 133 | - public function hasFirstCRTCoefficientParameter(): bool |
|
| 134 | - { |
|
| 135 | - return $this->has(Parameter\JWKParameter::P_QI); |
|
| 136 | - } |
|
| 137 | - |
|
| 138 | - /** |
|
| 139 | - * Get the first CRT coefficient parameter. |
|
| 140 | - * |
|
| 141 | - * @throws \UnexpectedValueException If the parameter has a wrong class |
|
| 142 | - * @throws \LogicException If the parameter is not present |
|
| 143 | - * |
|
| 144 | - * @return Parameter\FirstCRTCoefficientParameter |
|
| 145 | - */ |
|
| 146 | - public function firstCRTCoefficientParameter(): Parameter\FirstCRTCoefficientParameter |
|
| 147 | - { |
|
| 148 | - return self::_checkType($this->get(Parameter\JWKParameter::P_QI), |
|
| 149 | - Parameter\FirstCRTCoefficientParameter::class); |
|
| 150 | - } |
|
| 151 | - |
|
| 152 | - /** |
|
| 153 | - * Check whether the first factor CRT exponent parameter is present. |
|
| 154 | - * |
|
| 155 | - * @return bool |
|
| 156 | - */ |
|
| 157 | - public function hasFirstFactorCRTExponentParameter(): bool |
|
| 158 | - { |
|
| 159 | - return $this->has(Parameter\JWKParameter::P_DP); |
|
| 160 | - } |
|
| 161 | - |
|
| 162 | - /** |
|
| 163 | - * Get the first factor CRT exponent parameter. |
|
| 164 | - * |
|
| 165 | - * @throws \UnexpectedValueException If the parameter has a wrong class |
|
| 166 | - * @throws \LogicException If the parameter is not present |
|
| 167 | - * |
|
| 168 | - * @return Parameter\FirstFactorCRTExponentParameter |
|
| 169 | - */ |
|
| 170 | - public function firstFactorCRTExponentParameter(): Parameter\FirstFactorCRTExponentParameter |
|
| 171 | - { |
|
| 172 | - return self::_checkType($this->get(Parameter\JWKParameter::P_DP), |
|
| 173 | - Parameter\FirstFactorCRTExponentParameter::class); |
|
| 174 | - } |
|
| 175 | - |
|
| 176 | - /** |
|
| 177 | - * Check whether the first prime factor parameter is present. |
|
| 178 | - * |
|
| 179 | - * @return bool |
|
| 180 | - */ |
|
| 181 | - public function hasFirstPrimeFactorParameter(): bool |
|
| 182 | - { |
|
| 183 | - return $this->has(Parameter\JWKParameter::P_P); |
|
| 184 | - } |
|
| 185 | - |
|
| 186 | - /** |
|
| 187 | - * Get the first prime factor parameter. |
|
| 188 | - * |
|
| 189 | - * @throws \UnexpectedValueException If the parameter has a wrong class |
|
| 190 | - * @throws \LogicException If the parameter is not present |
|
| 191 | - * |
|
| 192 | - * @return Parameter\FirstPrimeFactorParameter |
|
| 193 | - */ |
|
| 194 | - public function firstPrimeFactorParameter(): Parameter\FirstPrimeFactorParameter |
|
| 195 | - { |
|
| 196 | - return self::_checkType($this->get(Parameter\JWKParameter::P_P), |
|
| 197 | - Parameter\FirstPrimeFactorParameter::class); |
|
| 198 | - } |
|
| 199 | - |
|
| 200 | - /** |
|
| 201 | - * Check whether the key ID parameter is present. |
|
| 202 | - * |
|
| 203 | - * @return bool |
|
| 204 | - */ |
|
| 205 | - public function hasKeyIDParameter(): bool |
|
| 206 | - { |
|
| 207 | - return $this->has(Parameter\JWKParameter::P_KID); |
|
| 208 | - } |
|
| 209 | - |
|
| 210 | - /** |
|
| 211 | - * Get the key ID parameter. |
|
| 212 | - * |
|
| 213 | - * @throws \UnexpectedValueException If the parameter has a wrong class |
|
| 214 | - * @throws \LogicException If the parameter is not present |
|
| 215 | - * |
|
| 216 | - * @return Parameter\KeyIDParameter |
|
| 217 | - */ |
|
| 218 | - public function keyIDParameter(): Parameter\KeyIDParameter |
|
| 219 | - { |
|
| 220 | - return self::_checkType($this->get(Parameter\JWKParameter::P_KID), |
|
| 221 | - Parameter\KeyIDParameter::class); |
|
| 222 | - } |
|
| 223 | - |
|
| 224 | - /** |
|
| 225 | - * Check whether the key operations parameter is present. |
|
| 226 | - * |
|
| 227 | - * @return bool |
|
| 228 | - */ |
|
| 229 | - public function hasKeyOperationsParameter(): bool |
|
| 230 | - { |
|
| 231 | - return $this->has(Parameter\JWKParameter::P_KEY_OPS); |
|
| 232 | - } |
|
| 233 | - |
|
| 234 | - /** |
|
| 235 | - * Get the key operations parameter. |
|
| 236 | - * |
|
| 237 | - * @throws \UnexpectedValueException If the parameter has a wrong class |
|
| 238 | - * @throws \LogicException If the parameter is not present |
|
| 239 | - * |
|
| 240 | - * @return Parameter\KeyOperationsParameter |
|
| 241 | - */ |
|
| 242 | - public function keyOperationsParameter(): Parameter\KeyOperationsParameter |
|
| 243 | - { |
|
| 244 | - return self::_checkType($this->get(Parameter\JWKParameter::P_KEY_OPS), |
|
| 245 | - Parameter\KeyOperationsParameter::class); |
|
| 246 | - } |
|
| 247 | - |
|
| 248 | - /** |
|
| 249 | - * Check whether the key type parameter is present. |
|
| 250 | - * |
|
| 251 | - * @return bool |
|
| 252 | - */ |
|
| 253 | - public function hasKeyTypeParameter(): bool |
|
| 254 | - { |
|
| 255 | - return $this->has(Parameter\JWKParameter::P_KTY); |
|
| 256 | - } |
|
| 257 | - |
|
| 258 | - /** |
|
| 259 | - * Get the key type parameter. |
|
| 260 | - * |
|
| 261 | - * @throws \UnexpectedValueException If the parameter has a wrong class |
|
| 262 | - * @throws \LogicException If the parameter is not present |
|
| 263 | - * |
|
| 264 | - * @return Parameter\KeyTypeParameter |
|
| 265 | - */ |
|
| 266 | - public function keyTypeParameter(): Parameter\KeyTypeParameter |
|
| 267 | - { |
|
| 268 | - return self::_checkType($this->get(Parameter\JWKParameter::P_KTY), |
|
| 269 | - Parameter\KeyTypeParameter::class); |
|
| 270 | - } |
|
| 271 | - |
|
| 272 | - /** |
|
| 273 | - * Check whether the key value parameter is present. |
|
| 274 | - * |
|
| 275 | - * @return bool |
|
| 276 | - */ |
|
| 277 | - public function hasKeyValueParameter(): bool |
|
| 278 | - { |
|
| 279 | - return $this->has(Parameter\JWKParameter::P_K); |
|
| 280 | - } |
|
| 281 | - |
|
| 282 | - /** |
|
| 283 | - * Get the key value parameter. |
|
| 284 | - * |
|
| 285 | - * @throws \UnexpectedValueException If the parameter has a wrong class |
|
| 286 | - * @throws \LogicException If the parameter is not present |
|
| 287 | - * |
|
| 288 | - * @return Parameter\KeyValueParameter |
|
| 289 | - */ |
|
| 290 | - public function keyValueParameter(): Parameter\KeyValueParameter |
|
| 291 | - { |
|
| 292 | - return self::_checkType($this->get(Parameter\JWKParameter::P_K), |
|
| 293 | - Parameter\KeyValueParameter::class); |
|
| 294 | - } |
|
| 295 | - |
|
| 296 | - /** |
|
| 297 | - * Check whether the modulus parameter is present. |
|
| 298 | - * |
|
| 299 | - * @return bool |
|
| 300 | - */ |
|
| 301 | - public function hasModulusParameter(): bool |
|
| 302 | - { |
|
| 303 | - return $this->has(Parameter\JWKParameter::P_N); |
|
| 304 | - } |
|
| 305 | - |
|
| 306 | - /** |
|
| 307 | - * Get the modulus parameter. |
|
| 308 | - * |
|
| 309 | - * @throws \UnexpectedValueException If the parameter has a wrong class |
|
| 310 | - * @throws \LogicException If the parameter is not present |
|
| 311 | - * |
|
| 312 | - * @return Parameter\ModulusParameter |
|
| 313 | - */ |
|
| 314 | - public function modulusParameter(): Parameter\ModulusParameter |
|
| 315 | - { |
|
| 316 | - return self::_checkType($this->get(Parameter\JWKParameter::P_N), |
|
| 317 | - Parameter\ModulusParameter::class); |
|
| 318 | - } |
|
| 319 | - |
|
| 320 | - /** |
|
| 321 | - * Check whether the other primes info parameter is present. |
|
| 322 | - * |
|
| 323 | - * @return bool |
|
| 324 | - */ |
|
| 325 | - public function hasOtherPrimesInfoParameter(): bool |
|
| 326 | - { |
|
| 327 | - return $this->has(Parameter\JWKParameter::P_OTH); |
|
| 328 | - } |
|
| 329 | - |
|
| 330 | - /** |
|
| 331 | - * Get the other primes info parameter. |
|
| 332 | - * |
|
| 333 | - * @throws \UnexpectedValueException If the parameter has a wrong class |
|
| 334 | - * @throws \LogicException If the parameter is not present |
|
| 335 | - * |
|
| 336 | - * @return Parameter\OtherPrimesInfoParameter |
|
| 337 | - */ |
|
| 338 | - public function otherPrimesInfoParameter(): Parameter\OtherPrimesInfoParameter |
|
| 339 | - { |
|
| 340 | - return self::_checkType($this->get(Parameter\JWKParameter::P_OTH), |
|
| 341 | - Parameter\OtherPrimesInfoParameter::class); |
|
| 342 | - } |
|
| 343 | - |
|
| 344 | - /** |
|
| 345 | - * Check whether the private exponent parameter is present. |
|
| 346 | - * |
|
| 347 | - * @return bool |
|
| 348 | - */ |
|
| 349 | - public function hasPrivateExponentParameter(): bool |
|
| 350 | - { |
|
| 351 | - return $this->has(Parameter\JWKParameter::P_RSA_D); |
|
| 352 | - } |
|
| 353 | - |
|
| 354 | - /** |
|
| 355 | - * Get the private exponent parameter. |
|
| 356 | - * |
|
| 357 | - * @throws \UnexpectedValueException If the parameter has a wrong class |
|
| 358 | - * @throws \LogicException If the parameter is not present |
|
| 359 | - * |
|
| 360 | - * @return Parameter\PrivateExponentParameter |
|
| 361 | - */ |
|
| 362 | - public function privateExponentParameter(): Parameter\PrivateExponentParameter |
|
| 363 | - { |
|
| 364 | - return self::_checkType($this->get(Parameter\JWKParameter::P_RSA_D), |
|
| 365 | - Parameter\PrivateExponentParameter::class); |
|
| 366 | - } |
|
| 367 | - |
|
| 368 | - /** |
|
| 369 | - * Check whether the public key use parameter is present. |
|
| 370 | - * |
|
| 371 | - * @return bool |
|
| 372 | - */ |
|
| 373 | - public function hasPublicKeyUseParameter(): bool |
|
| 374 | - { |
|
| 375 | - return $this->has(Parameter\JWKParameter::P_USE); |
|
| 376 | - } |
|
| 377 | - |
|
| 378 | - /** |
|
| 379 | - * Get the public key use parameter. |
|
| 380 | - * |
|
| 381 | - * @throws \UnexpectedValueException If the parameter has a wrong class |
|
| 382 | - * @throws \LogicException If the parameter is not present |
|
| 383 | - * |
|
| 384 | - * @return Parameter\PublicKeyUseParameter |
|
| 385 | - */ |
|
| 386 | - public function publicKeyUseParameter(): Parameter\PublicKeyUseParameter |
|
| 387 | - { |
|
| 388 | - return self::_checkType($this->get(Parameter\JWKParameter::P_USE), |
|
| 389 | - Parameter\PublicKeyUseParameter::class); |
|
| 390 | - } |
|
| 391 | - |
|
| 392 | - /** |
|
| 393 | - * Check whether the second factor CRT exponent parameter is present. |
|
| 394 | - * |
|
| 395 | - * @return bool |
|
| 396 | - */ |
|
| 397 | - public function hasSecondFactorCRTExponentParameter(): bool |
|
| 398 | - { |
|
| 399 | - return $this->has(Parameter\JWKParameter::P_DQ); |
|
| 400 | - } |
|
| 401 | - |
|
| 402 | - /** |
|
| 403 | - * Get the second factor CRT exponent parameter. |
|
| 404 | - * |
|
| 405 | - * @throws \UnexpectedValueException If the parameter has a wrong class |
|
| 406 | - * @throws \LogicException If the parameter is not present |
|
| 407 | - * |
|
| 408 | - * @return Parameter\SecondFactorCRTExponentParameter |
|
| 409 | - */ |
|
| 410 | - public function secondFactorCRTExponentParameter(): Parameter\SecondFactorCRTExponentParameter |
|
| 411 | - { |
|
| 412 | - return self::_checkType($this->get(Parameter\JWKParameter::P_DQ), |
|
| 413 | - Parameter\SecondFactorCRTExponentParameter::class); |
|
| 414 | - } |
|
| 415 | - |
|
| 416 | - /** |
|
| 417 | - * Check whether the second prime factor parameter is present. |
|
| 418 | - * |
|
| 419 | - * @return bool |
|
| 420 | - */ |
|
| 421 | - public function hasSecondPrimeFactorParameter(): bool |
|
| 422 | - { |
|
| 423 | - return $this->has(Parameter\JWKParameter::P_Q); |
|
| 424 | - } |
|
| 425 | - |
|
| 426 | - /** |
|
| 427 | - * Get the second prime factor parameter. |
|
| 428 | - * |
|
| 429 | - * @throws \UnexpectedValueException If the parameter has a wrong class |
|
| 430 | - * @throws \LogicException If the parameter is not present |
|
| 431 | - * |
|
| 432 | - * @return Parameter\SecondPrimeFactorParameter |
|
| 433 | - */ |
|
| 434 | - public function secondPrimeFactorParameter(): Parameter\SecondPrimeFactorParameter |
|
| 435 | - { |
|
| 436 | - return self::_checkType($this->get(Parameter\JWKParameter::P_Q), |
|
| 437 | - Parameter\SecondPrimeFactorParameter::class); |
|
| 438 | - } |
|
| 439 | - |
|
| 440 | - /** |
|
| 441 | - * Check whether the X.509 certificate chain parameter is present. |
|
| 442 | - * |
|
| 443 | - * @return bool |
|
| 444 | - */ |
|
| 445 | - public function hasX509CertificateChainParameter(): bool |
|
| 446 | - { |
|
| 447 | - return $this->has(Parameter\JWKParameter::P_X5C); |
|
| 448 | - } |
|
| 449 | - |
|
| 450 | - /** |
|
| 451 | - * Get the X.509 certificate chain parameter. |
|
| 452 | - * |
|
| 453 | - * @throws \UnexpectedValueException If the parameter has a wrong class |
|
| 454 | - * @throws \LogicException If the parameter is not present |
|
| 455 | - * |
|
| 456 | - * @return Parameter\X509CertificateChainParameter |
|
| 457 | - */ |
|
| 458 | - public function X509CertificateChainParameter(): Parameter\X509CertificateChainParameter |
|
| 459 | - { |
|
| 460 | - return self::_checkType($this->get(Parameter\JWKParameter::P_X5C), |
|
| 461 | - Parameter\X509CertificateChainParameter::class); |
|
| 462 | - } |
|
| 463 | - |
|
| 464 | - /** |
|
| 465 | - * Check whether the X.509 certificate SHA-1 thumbprint parameter is |
|
| 466 | - * present. |
|
| 467 | - * |
|
| 468 | - * @return bool |
|
| 469 | - */ |
|
| 470 | - public function hasX509CertificateSHA1ThumbprintParameter(): bool |
|
| 471 | - { |
|
| 472 | - return $this->has(Parameter\JWKParameter::P_X5T); |
|
| 473 | - } |
|
| 474 | - |
|
| 475 | - /** |
|
| 476 | - * Get the X.509 certificate SHA-1 thumbprint parameter. |
|
| 477 | - * |
|
| 478 | - * @throws \UnexpectedValueException If the parameter has a wrong class |
|
| 479 | - * @throws \LogicException If the parameter is not present |
|
| 480 | - * |
|
| 481 | - * @return Parameter\X509CertificateSHA1ThumbprintParameter |
|
| 482 | - */ |
|
| 483 | - public function X509CertificateSHA1ThumbprintParameter(): Parameter\X509CertificateSHA1ThumbprintParameter |
|
| 484 | - { |
|
| 485 | - return self::_checkType($this->get(Parameter\JWKParameter::P_X5T), |
|
| 486 | - Parameter\X509CertificateSHA1ThumbprintParameter::class); |
|
| 487 | - } |
|
| 488 | - |
|
| 489 | - /** |
|
| 490 | - * Check whether the X.509 certificate SHA-256 thumbprint parameter is |
|
| 491 | - * present. |
|
| 492 | - * |
|
| 493 | - * @return bool |
|
| 494 | - */ |
|
| 495 | - public function hasX509CertificateSHA256ThumbprintParameter(): bool |
|
| 496 | - { |
|
| 497 | - return $this->has(Parameter\JWKParameter::P_X5TS256); |
|
| 498 | - } |
|
| 499 | - |
|
| 500 | - /** |
|
| 501 | - * Get the X.509 certificate SHA-256 thumbprint parameter. |
|
| 502 | - * |
|
| 503 | - * @throws \UnexpectedValueException If the parameter has a wrong class |
|
| 504 | - * @throws \LogicException If the parameter is not present |
|
| 505 | - * |
|
| 506 | - * @return Parameter\X509CertificateSHA256ThumbprintParameter |
|
| 507 | - */ |
|
| 508 | - public function X509CertificateSHA256ThumbprintParameter(): Parameter\X509CertificateSHA256ThumbprintParameter |
|
| 509 | - { |
|
| 510 | - return self::_checkType($this->get(Parameter\JWKParameter::P_X5TS256), |
|
| 511 | - Parameter\X509CertificateSHA256ThumbprintParameter::class); |
|
| 512 | - } |
|
| 513 | - |
|
| 514 | - /** |
|
| 515 | - * Check whether the X.509 URL parameter is present. |
|
| 516 | - * |
|
| 517 | - * @return bool |
|
| 518 | - */ |
|
| 519 | - public function hasX509URLParameter(): bool |
|
| 520 | - { |
|
| 521 | - return $this->has(Parameter\JWKParameter::P_X5U); |
|
| 522 | - } |
|
| 523 | - |
|
| 524 | - /** |
|
| 525 | - * Get the X.509 URL parameter. |
|
| 526 | - * |
|
| 527 | - * @throws \UnexpectedValueException If the parameter has a wrong class |
|
| 528 | - * @throws \LogicException If the parameter is not present |
|
| 529 | - * |
|
| 530 | - * @return Parameter\X509URLParameter |
|
| 531 | - */ |
|
| 532 | - public function X509URLParameter(): Parameter\X509URLParameter |
|
| 533 | - { |
|
| 534 | - return self::_checkType($this->get(Parameter\JWKParameter::P_X5U), |
|
| 535 | - Parameter\X509URLParameter::class); |
|
| 536 | - } |
|
| 537 | - |
|
| 538 | - /** |
|
| 539 | - * Check whether the X coordinate parameter is present. |
|
| 540 | - * |
|
| 541 | - * @return bool |
|
| 542 | - */ |
|
| 543 | - public function hasXCoordinateParameter(): bool |
|
| 544 | - { |
|
| 545 | - return $this->has(Parameter\JWKParameter::P_X); |
|
| 546 | - } |
|
| 547 | - |
|
| 548 | - /** |
|
| 549 | - * Get the X coordinate parameter. |
|
| 550 | - * |
|
| 551 | - * @throws \UnexpectedValueException If the parameter has a wrong class |
|
| 552 | - * @throws \LogicException If the parameter is not present |
|
| 553 | - * |
|
| 554 | - * @return Parameter\XCoordinateParameter |
|
| 555 | - */ |
|
| 556 | - public function XCoordinateParameter(): Parameter\XCoordinateParameter |
|
| 557 | - { |
|
| 558 | - return self::_checkType($this->get(Parameter\JWKParameter::P_X), |
|
| 559 | - Parameter\XCoordinateParameter::class); |
|
| 560 | - } |
|
| 561 | - |
|
| 562 | - /** |
|
| 563 | - * Check whether the Y coordinate parameter is present. |
|
| 564 | - * |
|
| 565 | - * @return bool |
|
| 566 | - */ |
|
| 567 | - public function hasYCoordinateParameter(): bool |
|
| 568 | - { |
|
| 569 | - return $this->has(Parameter\JWKParameter::P_Y); |
|
| 570 | - } |
|
| 571 | - |
|
| 572 | - /** |
|
| 573 | - * Get the Y coordinate parameter. |
|
| 574 | - * |
|
| 575 | - * @throws \UnexpectedValueException If the parameter has a wrong class |
|
| 576 | - * @throws \LogicException If the parameter is not present |
|
| 577 | - * |
|
| 578 | - * @return Parameter\YCoordinateParameter |
|
| 579 | - */ |
|
| 580 | - public function YCoordinateParameter(): Parameter\YCoordinateParameter |
|
| 581 | - { |
|
| 582 | - return self::_checkType($this->get(Parameter\JWKParameter::P_Y), |
|
| 583 | - Parameter\YCoordinateParameter::class); |
|
| 584 | - } |
|
| 585 | - |
|
| 586 | - /** |
|
| 587 | - * Check that the parameter is an instance of the given class. |
|
| 588 | - * |
|
| 589 | - * @param Parameter\JWKParameter $param Parameter |
|
| 590 | - * @param string $cls Class name |
|
| 591 | - * |
|
| 592 | - * @throws \UnexpectedValueException |
|
| 593 | - * |
|
| 594 | - * @return Parameter\JWKParameter |
|
| 595 | - */ |
|
| 596 | - private static function _checkType(Parameter\JWKParameter $param, string $cls): Parameter\JWKParameter |
|
| 597 | - { |
|
| 598 | - if (!$param instanceof $cls) { |
|
| 599 | - throw new \UnexpectedValueException( |
|
| 600 | - "{$cls} expected, got " . get_class($param)); |
|
| 601 | - } |
|
| 602 | - return $param; |
|
| 603 | - } |
|
| 14 | + /** |
|
| 15 | + * Whether parameters are present. |
|
| 16 | + * |
|
| 17 | + * @param string ...$names Parameter names |
|
| 18 | + * |
|
| 19 | + * @return bool |
|
| 20 | + */ |
|
| 21 | + abstract public function has(string ...$names): bool; |
|
| 22 | + |
|
| 23 | + /** |
|
| 24 | + * Get a parameter. |
|
| 25 | + * |
|
| 26 | + * @param string $name Parameter name |
|
| 27 | + * |
|
| 28 | + * @return JWKParameter |
|
| 29 | + */ |
|
| 30 | + abstract public function get(string $name): JWKParameter; |
|
| 31 | + |
|
| 32 | + /** |
|
| 33 | + * Check whether the algorithm parameter is present. |
|
| 34 | + * |
|
| 35 | + * @return bool |
|
| 36 | + */ |
|
| 37 | + public function hasAlgorithmParameter(): bool |
|
| 38 | + { |
|
| 39 | + return $this->has(Parameter\JWKParameter::P_ALG); |
|
| 40 | + } |
|
| 41 | + |
|
| 42 | + /** |
|
| 43 | + * Get the algorithm parameter. |
|
| 44 | + * |
|
| 45 | + * @throws \UnexpectedValueException If the parameter has a wrong class |
|
| 46 | + * @throws \LogicException If the parameter is not present |
|
| 47 | + * |
|
| 48 | + * @return Parameter\AlgorithmParameter |
|
| 49 | + */ |
|
| 50 | + public function algorithmParameter(): Parameter\AlgorithmParameter |
|
| 51 | + { |
|
| 52 | + return self::_checkType($this->get(Parameter\JWKParameter::P_ALG), |
|
| 53 | + Parameter\AlgorithmParameter::class); |
|
| 54 | + } |
|
| 55 | + |
|
| 56 | + /** |
|
| 57 | + * Check whether the curve parameter is present. |
|
| 58 | + * |
|
| 59 | + * @return bool |
|
| 60 | + */ |
|
| 61 | + public function hasCurveParameter(): bool |
|
| 62 | + { |
|
| 63 | + return $this->has(Parameter\JWKParameter::P_CRV); |
|
| 64 | + } |
|
| 65 | + |
|
| 66 | + /** |
|
| 67 | + * Get the curve parameter. |
|
| 68 | + * |
|
| 69 | + * @throws \UnexpectedValueException If the parameter has a wrong class |
|
| 70 | + * @throws \LogicException If the parameter is not present |
|
| 71 | + * |
|
| 72 | + * @return Parameter\CurveParameter |
|
| 73 | + */ |
|
| 74 | + public function curveParameter(): Parameter\CurveParameter |
|
| 75 | + { |
|
| 76 | + return self::_checkType($this->get(Parameter\JWKParameter::P_CRV), |
|
| 77 | + Parameter\CurveParameter::class); |
|
| 78 | + } |
|
| 79 | + |
|
| 80 | + /** |
|
| 81 | + * Check whether the ECC private key parameter is present. |
|
| 82 | + * |
|
| 83 | + * @return bool |
|
| 84 | + */ |
|
| 85 | + public function hasECCPrivateKeyParameter(): bool |
|
| 86 | + { |
|
| 87 | + return $this->has(Parameter\JWKParameter::P_ECC_D); |
|
| 88 | + } |
|
| 89 | + |
|
| 90 | + /** |
|
| 91 | + * Get the ECC private key parameter. |
|
| 92 | + * |
|
| 93 | + * @throws \UnexpectedValueException If the parameter has a wrong class |
|
| 94 | + * @throws \LogicException If the parameter is not present |
|
| 95 | + * |
|
| 96 | + * @return Parameter\ECCPrivateKeyParameter |
|
| 97 | + */ |
|
| 98 | + public function ECCPrivateKeyParameter(): Parameter\ECCPrivateKeyParameter |
|
| 99 | + { |
|
| 100 | + return self::_checkType($this->get(Parameter\JWKParameter::P_ECC_D), |
|
| 101 | + Parameter\ECCPrivateKeyParameter::class); |
|
| 102 | + } |
|
| 103 | + |
|
| 104 | + /** |
|
| 105 | + * Check whether the exponent parameter is present. |
|
| 106 | + * |
|
| 107 | + * @return bool |
|
| 108 | + */ |
|
| 109 | + public function hasExponentParameter(): bool |
|
| 110 | + { |
|
| 111 | + return $this->has(Parameter\JWKParameter::P_E); |
|
| 112 | + } |
|
| 113 | + |
|
| 114 | + /** |
|
| 115 | + * Get the exponent parameter. |
|
| 116 | + * |
|
| 117 | + * @throws \UnexpectedValueException If the parameter has a wrong class |
|
| 118 | + * @throws \LogicException If the parameter is not present |
|
| 119 | + * |
|
| 120 | + * @return Parameter\ExponentParameter |
|
| 121 | + */ |
|
| 122 | + public function exponentParameter(): Parameter\ExponentParameter |
|
| 123 | + { |
|
| 124 | + return self::_checkType($this->get(Parameter\JWKParameter::P_E), |
|
| 125 | + Parameter\ExponentParameter::class); |
|
| 126 | + } |
|
| 127 | + |
|
| 128 | + /** |
|
| 129 | + * Check whether the first CRT coefficient parameter is present. |
|
| 130 | + * |
|
| 131 | + * @return bool |
|
| 132 | + */ |
|
| 133 | + public function hasFirstCRTCoefficientParameter(): bool |
|
| 134 | + { |
|
| 135 | + return $this->has(Parameter\JWKParameter::P_QI); |
|
| 136 | + } |
|
| 137 | + |
|
| 138 | + /** |
|
| 139 | + * Get the first CRT coefficient parameter. |
|
| 140 | + * |
|
| 141 | + * @throws \UnexpectedValueException If the parameter has a wrong class |
|
| 142 | + * @throws \LogicException If the parameter is not present |
|
| 143 | + * |
|
| 144 | + * @return Parameter\FirstCRTCoefficientParameter |
|
| 145 | + */ |
|
| 146 | + public function firstCRTCoefficientParameter(): Parameter\FirstCRTCoefficientParameter |
|
| 147 | + { |
|
| 148 | + return self::_checkType($this->get(Parameter\JWKParameter::P_QI), |
|
| 149 | + Parameter\FirstCRTCoefficientParameter::class); |
|
| 150 | + } |
|
| 151 | + |
|
| 152 | + /** |
|
| 153 | + * Check whether the first factor CRT exponent parameter is present. |
|
| 154 | + * |
|
| 155 | + * @return bool |
|
| 156 | + */ |
|
| 157 | + public function hasFirstFactorCRTExponentParameter(): bool |
|
| 158 | + { |
|
| 159 | + return $this->has(Parameter\JWKParameter::P_DP); |
|
| 160 | + } |
|
| 161 | + |
|
| 162 | + /** |
|
| 163 | + * Get the first factor CRT exponent parameter. |
|
| 164 | + * |
|
| 165 | + * @throws \UnexpectedValueException If the parameter has a wrong class |
|
| 166 | + * @throws \LogicException If the parameter is not present |
|
| 167 | + * |
|
| 168 | + * @return Parameter\FirstFactorCRTExponentParameter |
|
| 169 | + */ |
|
| 170 | + public function firstFactorCRTExponentParameter(): Parameter\FirstFactorCRTExponentParameter |
|
| 171 | + { |
|
| 172 | + return self::_checkType($this->get(Parameter\JWKParameter::P_DP), |
|
| 173 | + Parameter\FirstFactorCRTExponentParameter::class); |
|
| 174 | + } |
|
| 175 | + |
|
| 176 | + /** |
|
| 177 | + * Check whether the first prime factor parameter is present. |
|
| 178 | + * |
|
| 179 | + * @return bool |
|
| 180 | + */ |
|
| 181 | + public function hasFirstPrimeFactorParameter(): bool |
|
| 182 | + { |
|
| 183 | + return $this->has(Parameter\JWKParameter::P_P); |
|
| 184 | + } |
|
| 185 | + |
|
| 186 | + /** |
|
| 187 | + * Get the first prime factor parameter. |
|
| 188 | + * |
|
| 189 | + * @throws \UnexpectedValueException If the parameter has a wrong class |
|
| 190 | + * @throws \LogicException If the parameter is not present |
|
| 191 | + * |
|
| 192 | + * @return Parameter\FirstPrimeFactorParameter |
|
| 193 | + */ |
|
| 194 | + public function firstPrimeFactorParameter(): Parameter\FirstPrimeFactorParameter |
|
| 195 | + { |
|
| 196 | + return self::_checkType($this->get(Parameter\JWKParameter::P_P), |
|
| 197 | + Parameter\FirstPrimeFactorParameter::class); |
|
| 198 | + } |
|
| 199 | + |
|
| 200 | + /** |
|
| 201 | + * Check whether the key ID parameter is present. |
|
| 202 | + * |
|
| 203 | + * @return bool |
|
| 204 | + */ |
|
| 205 | + public function hasKeyIDParameter(): bool |
|
| 206 | + { |
|
| 207 | + return $this->has(Parameter\JWKParameter::P_KID); |
|
| 208 | + } |
|
| 209 | + |
|
| 210 | + /** |
|
| 211 | + * Get the key ID parameter. |
|
| 212 | + * |
|
| 213 | + * @throws \UnexpectedValueException If the parameter has a wrong class |
|
| 214 | + * @throws \LogicException If the parameter is not present |
|
| 215 | + * |
|
| 216 | + * @return Parameter\KeyIDParameter |
|
| 217 | + */ |
|
| 218 | + public function keyIDParameter(): Parameter\KeyIDParameter |
|
| 219 | + { |
|
| 220 | + return self::_checkType($this->get(Parameter\JWKParameter::P_KID), |
|
| 221 | + Parameter\KeyIDParameter::class); |
|
| 222 | + } |
|
| 223 | + |
|
| 224 | + /** |
|
| 225 | + * Check whether the key operations parameter is present. |
|
| 226 | + * |
|
| 227 | + * @return bool |
|
| 228 | + */ |
|
| 229 | + public function hasKeyOperationsParameter(): bool |
|
| 230 | + { |
|
| 231 | + return $this->has(Parameter\JWKParameter::P_KEY_OPS); |
|
| 232 | + } |
|
| 233 | + |
|
| 234 | + /** |
|
| 235 | + * Get the key operations parameter. |
|
| 236 | + * |
|
| 237 | + * @throws \UnexpectedValueException If the parameter has a wrong class |
|
| 238 | + * @throws \LogicException If the parameter is not present |
|
| 239 | + * |
|
| 240 | + * @return Parameter\KeyOperationsParameter |
|
| 241 | + */ |
|
| 242 | + public function keyOperationsParameter(): Parameter\KeyOperationsParameter |
|
| 243 | + { |
|
| 244 | + return self::_checkType($this->get(Parameter\JWKParameter::P_KEY_OPS), |
|
| 245 | + Parameter\KeyOperationsParameter::class); |
|
| 246 | + } |
|
| 247 | + |
|
| 248 | + /** |
|
| 249 | + * Check whether the key type parameter is present. |
|
| 250 | + * |
|
| 251 | + * @return bool |
|
| 252 | + */ |
|
| 253 | + public function hasKeyTypeParameter(): bool |
|
| 254 | + { |
|
| 255 | + return $this->has(Parameter\JWKParameter::P_KTY); |
|
| 256 | + } |
|
| 257 | + |
|
| 258 | + /** |
|
| 259 | + * Get the key type parameter. |
|
| 260 | + * |
|
| 261 | + * @throws \UnexpectedValueException If the parameter has a wrong class |
|
| 262 | + * @throws \LogicException If the parameter is not present |
|
| 263 | + * |
|
| 264 | + * @return Parameter\KeyTypeParameter |
|
| 265 | + */ |
|
| 266 | + public function keyTypeParameter(): Parameter\KeyTypeParameter |
|
| 267 | + { |
|
| 268 | + return self::_checkType($this->get(Parameter\JWKParameter::P_KTY), |
|
| 269 | + Parameter\KeyTypeParameter::class); |
|
| 270 | + } |
|
| 271 | + |
|
| 272 | + /** |
|
| 273 | + * Check whether the key value parameter is present. |
|
| 274 | + * |
|
| 275 | + * @return bool |
|
| 276 | + */ |
|
| 277 | + public function hasKeyValueParameter(): bool |
|
| 278 | + { |
|
| 279 | + return $this->has(Parameter\JWKParameter::P_K); |
|
| 280 | + } |
|
| 281 | + |
|
| 282 | + /** |
|
| 283 | + * Get the key value parameter. |
|
| 284 | + * |
|
| 285 | + * @throws \UnexpectedValueException If the parameter has a wrong class |
|
| 286 | + * @throws \LogicException If the parameter is not present |
|
| 287 | + * |
|
| 288 | + * @return Parameter\KeyValueParameter |
|
| 289 | + */ |
|
| 290 | + public function keyValueParameter(): Parameter\KeyValueParameter |
|
| 291 | + { |
|
| 292 | + return self::_checkType($this->get(Parameter\JWKParameter::P_K), |
|
| 293 | + Parameter\KeyValueParameter::class); |
|
| 294 | + } |
|
| 295 | + |
|
| 296 | + /** |
|
| 297 | + * Check whether the modulus parameter is present. |
|
| 298 | + * |
|
| 299 | + * @return bool |
|
| 300 | + */ |
|
| 301 | + public function hasModulusParameter(): bool |
|
| 302 | + { |
|
| 303 | + return $this->has(Parameter\JWKParameter::P_N); |
|
| 304 | + } |
|
| 305 | + |
|
| 306 | + /** |
|
| 307 | + * Get the modulus parameter. |
|
| 308 | + * |
|
| 309 | + * @throws \UnexpectedValueException If the parameter has a wrong class |
|
| 310 | + * @throws \LogicException If the parameter is not present |
|
| 311 | + * |
|
| 312 | + * @return Parameter\ModulusParameter |
|
| 313 | + */ |
|
| 314 | + public function modulusParameter(): Parameter\ModulusParameter |
|
| 315 | + { |
|
| 316 | + return self::_checkType($this->get(Parameter\JWKParameter::P_N), |
|
| 317 | + Parameter\ModulusParameter::class); |
|
| 318 | + } |
|
| 319 | + |
|
| 320 | + /** |
|
| 321 | + * Check whether the other primes info parameter is present. |
|
| 322 | + * |
|
| 323 | + * @return bool |
|
| 324 | + */ |
|
| 325 | + public function hasOtherPrimesInfoParameter(): bool |
|
| 326 | + { |
|
| 327 | + return $this->has(Parameter\JWKParameter::P_OTH); |
|
| 328 | + } |
|
| 329 | + |
|
| 330 | + /** |
|
| 331 | + * Get the other primes info parameter. |
|
| 332 | + * |
|
| 333 | + * @throws \UnexpectedValueException If the parameter has a wrong class |
|
| 334 | + * @throws \LogicException If the parameter is not present |
|
| 335 | + * |
|
| 336 | + * @return Parameter\OtherPrimesInfoParameter |
|
| 337 | + */ |
|
| 338 | + public function otherPrimesInfoParameter(): Parameter\OtherPrimesInfoParameter |
|
| 339 | + { |
|
| 340 | + return self::_checkType($this->get(Parameter\JWKParameter::P_OTH), |
|
| 341 | + Parameter\OtherPrimesInfoParameter::class); |
|
| 342 | + } |
|
| 343 | + |
|
| 344 | + /** |
|
| 345 | + * Check whether the private exponent parameter is present. |
|
| 346 | + * |
|
| 347 | + * @return bool |
|
| 348 | + */ |
|
| 349 | + public function hasPrivateExponentParameter(): bool |
|
| 350 | + { |
|
| 351 | + return $this->has(Parameter\JWKParameter::P_RSA_D); |
|
| 352 | + } |
|
| 353 | + |
|
| 354 | + /** |
|
| 355 | + * Get the private exponent parameter. |
|
| 356 | + * |
|
| 357 | + * @throws \UnexpectedValueException If the parameter has a wrong class |
|
| 358 | + * @throws \LogicException If the parameter is not present |
|
| 359 | + * |
|
| 360 | + * @return Parameter\PrivateExponentParameter |
|
| 361 | + */ |
|
| 362 | + public function privateExponentParameter(): Parameter\PrivateExponentParameter |
|
| 363 | + { |
|
| 364 | + return self::_checkType($this->get(Parameter\JWKParameter::P_RSA_D), |
|
| 365 | + Parameter\PrivateExponentParameter::class); |
|
| 366 | + } |
|
| 367 | + |
|
| 368 | + /** |
|
| 369 | + * Check whether the public key use parameter is present. |
|
| 370 | + * |
|
| 371 | + * @return bool |
|
| 372 | + */ |
|
| 373 | + public function hasPublicKeyUseParameter(): bool |
|
| 374 | + { |
|
| 375 | + return $this->has(Parameter\JWKParameter::P_USE); |
|
| 376 | + } |
|
| 377 | + |
|
| 378 | + /** |
|
| 379 | + * Get the public key use parameter. |
|
| 380 | + * |
|
| 381 | + * @throws \UnexpectedValueException If the parameter has a wrong class |
|
| 382 | + * @throws \LogicException If the parameter is not present |
|
| 383 | + * |
|
| 384 | + * @return Parameter\PublicKeyUseParameter |
|
| 385 | + */ |
|
| 386 | + public function publicKeyUseParameter(): Parameter\PublicKeyUseParameter |
|
| 387 | + { |
|
| 388 | + return self::_checkType($this->get(Parameter\JWKParameter::P_USE), |
|
| 389 | + Parameter\PublicKeyUseParameter::class); |
|
| 390 | + } |
|
| 391 | + |
|
| 392 | + /** |
|
| 393 | + * Check whether the second factor CRT exponent parameter is present. |
|
| 394 | + * |
|
| 395 | + * @return bool |
|
| 396 | + */ |
|
| 397 | + public function hasSecondFactorCRTExponentParameter(): bool |
|
| 398 | + { |
|
| 399 | + return $this->has(Parameter\JWKParameter::P_DQ); |
|
| 400 | + } |
|
| 401 | + |
|
| 402 | + /** |
|
| 403 | + * Get the second factor CRT exponent parameter. |
|
| 404 | + * |
|
| 405 | + * @throws \UnexpectedValueException If the parameter has a wrong class |
|
| 406 | + * @throws \LogicException If the parameter is not present |
|
| 407 | + * |
|
| 408 | + * @return Parameter\SecondFactorCRTExponentParameter |
|
| 409 | + */ |
|
| 410 | + public function secondFactorCRTExponentParameter(): Parameter\SecondFactorCRTExponentParameter |
|
| 411 | + { |
|
| 412 | + return self::_checkType($this->get(Parameter\JWKParameter::P_DQ), |
|
| 413 | + Parameter\SecondFactorCRTExponentParameter::class); |
|
| 414 | + } |
|
| 415 | + |
|
| 416 | + /** |
|
| 417 | + * Check whether the second prime factor parameter is present. |
|
| 418 | + * |
|
| 419 | + * @return bool |
|
| 420 | + */ |
|
| 421 | + public function hasSecondPrimeFactorParameter(): bool |
|
| 422 | + { |
|
| 423 | + return $this->has(Parameter\JWKParameter::P_Q); |
|
| 424 | + } |
|
| 425 | + |
|
| 426 | + /** |
|
| 427 | + * Get the second prime factor parameter. |
|
| 428 | + * |
|
| 429 | + * @throws \UnexpectedValueException If the parameter has a wrong class |
|
| 430 | + * @throws \LogicException If the parameter is not present |
|
| 431 | + * |
|
| 432 | + * @return Parameter\SecondPrimeFactorParameter |
|
| 433 | + */ |
|
| 434 | + public function secondPrimeFactorParameter(): Parameter\SecondPrimeFactorParameter |
|
| 435 | + { |
|
| 436 | + return self::_checkType($this->get(Parameter\JWKParameter::P_Q), |
|
| 437 | + Parameter\SecondPrimeFactorParameter::class); |
|
| 438 | + } |
|
| 439 | + |
|
| 440 | + /** |
|
| 441 | + * Check whether the X.509 certificate chain parameter is present. |
|
| 442 | + * |
|
| 443 | + * @return bool |
|
| 444 | + */ |
|
| 445 | + public function hasX509CertificateChainParameter(): bool |
|
| 446 | + { |
|
| 447 | + return $this->has(Parameter\JWKParameter::P_X5C); |
|
| 448 | + } |
|
| 449 | + |
|
| 450 | + /** |
|
| 451 | + * Get the X.509 certificate chain parameter. |
|
| 452 | + * |
|
| 453 | + * @throws \UnexpectedValueException If the parameter has a wrong class |
|
| 454 | + * @throws \LogicException If the parameter is not present |
|
| 455 | + * |
|
| 456 | + * @return Parameter\X509CertificateChainParameter |
|
| 457 | + */ |
|
| 458 | + public function X509CertificateChainParameter(): Parameter\X509CertificateChainParameter |
|
| 459 | + { |
|
| 460 | + return self::_checkType($this->get(Parameter\JWKParameter::P_X5C), |
|
| 461 | + Parameter\X509CertificateChainParameter::class); |
|
| 462 | + } |
|
| 463 | + |
|
| 464 | + /** |
|
| 465 | + * Check whether the X.509 certificate SHA-1 thumbprint parameter is |
|
| 466 | + * present. |
|
| 467 | + * |
|
| 468 | + * @return bool |
|
| 469 | + */ |
|
| 470 | + public function hasX509CertificateSHA1ThumbprintParameter(): bool |
|
| 471 | + { |
|
| 472 | + return $this->has(Parameter\JWKParameter::P_X5T); |
|
| 473 | + } |
|
| 474 | + |
|
| 475 | + /** |
|
| 476 | + * Get the X.509 certificate SHA-1 thumbprint parameter. |
|
| 477 | + * |
|
| 478 | + * @throws \UnexpectedValueException If the parameter has a wrong class |
|
| 479 | + * @throws \LogicException If the parameter is not present |
|
| 480 | + * |
|
| 481 | + * @return Parameter\X509CertificateSHA1ThumbprintParameter |
|
| 482 | + */ |
|
| 483 | + public function X509CertificateSHA1ThumbprintParameter(): Parameter\X509CertificateSHA1ThumbprintParameter |
|
| 484 | + { |
|
| 485 | + return self::_checkType($this->get(Parameter\JWKParameter::P_X5T), |
|
| 486 | + Parameter\X509CertificateSHA1ThumbprintParameter::class); |
|
| 487 | + } |
|
| 488 | + |
|
| 489 | + /** |
|
| 490 | + * Check whether the X.509 certificate SHA-256 thumbprint parameter is |
|
| 491 | + * present. |
|
| 492 | + * |
|
| 493 | + * @return bool |
|
| 494 | + */ |
|
| 495 | + public function hasX509CertificateSHA256ThumbprintParameter(): bool |
|
| 496 | + { |
|
| 497 | + return $this->has(Parameter\JWKParameter::P_X5TS256); |
|
| 498 | + } |
|
| 499 | + |
|
| 500 | + /** |
|
| 501 | + * Get the X.509 certificate SHA-256 thumbprint parameter. |
|
| 502 | + * |
|
| 503 | + * @throws \UnexpectedValueException If the parameter has a wrong class |
|
| 504 | + * @throws \LogicException If the parameter is not present |
|
| 505 | + * |
|
| 506 | + * @return Parameter\X509CertificateSHA256ThumbprintParameter |
|
| 507 | + */ |
|
| 508 | + public function X509CertificateSHA256ThumbprintParameter(): Parameter\X509CertificateSHA256ThumbprintParameter |
|
| 509 | + { |
|
| 510 | + return self::_checkType($this->get(Parameter\JWKParameter::P_X5TS256), |
|
| 511 | + Parameter\X509CertificateSHA256ThumbprintParameter::class); |
|
| 512 | + } |
|
| 513 | + |
|
| 514 | + /** |
|
| 515 | + * Check whether the X.509 URL parameter is present. |
|
| 516 | + * |
|
| 517 | + * @return bool |
|
| 518 | + */ |
|
| 519 | + public function hasX509URLParameter(): bool |
|
| 520 | + { |
|
| 521 | + return $this->has(Parameter\JWKParameter::P_X5U); |
|
| 522 | + } |
|
| 523 | + |
|
| 524 | + /** |
|
| 525 | + * Get the X.509 URL parameter. |
|
| 526 | + * |
|
| 527 | + * @throws \UnexpectedValueException If the parameter has a wrong class |
|
| 528 | + * @throws \LogicException If the parameter is not present |
|
| 529 | + * |
|
| 530 | + * @return Parameter\X509URLParameter |
|
| 531 | + */ |
|
| 532 | + public function X509URLParameter(): Parameter\X509URLParameter |
|
| 533 | + { |
|
| 534 | + return self::_checkType($this->get(Parameter\JWKParameter::P_X5U), |
|
| 535 | + Parameter\X509URLParameter::class); |
|
| 536 | + } |
|
| 537 | + |
|
| 538 | + /** |
|
| 539 | + * Check whether the X coordinate parameter is present. |
|
| 540 | + * |
|
| 541 | + * @return bool |
|
| 542 | + */ |
|
| 543 | + public function hasXCoordinateParameter(): bool |
|
| 544 | + { |
|
| 545 | + return $this->has(Parameter\JWKParameter::P_X); |
|
| 546 | + } |
|
| 547 | + |
|
| 548 | + /** |
|
| 549 | + * Get the X coordinate parameter. |
|
| 550 | + * |
|
| 551 | + * @throws \UnexpectedValueException If the parameter has a wrong class |
|
| 552 | + * @throws \LogicException If the parameter is not present |
|
| 553 | + * |
|
| 554 | + * @return Parameter\XCoordinateParameter |
|
| 555 | + */ |
|
| 556 | + public function XCoordinateParameter(): Parameter\XCoordinateParameter |
|
| 557 | + { |
|
| 558 | + return self::_checkType($this->get(Parameter\JWKParameter::P_X), |
|
| 559 | + Parameter\XCoordinateParameter::class); |
|
| 560 | + } |
|
| 561 | + |
|
| 562 | + /** |
|
| 563 | + * Check whether the Y coordinate parameter is present. |
|
| 564 | + * |
|
| 565 | + * @return bool |
|
| 566 | + */ |
|
| 567 | + public function hasYCoordinateParameter(): bool |
|
| 568 | + { |
|
| 569 | + return $this->has(Parameter\JWKParameter::P_Y); |
|
| 570 | + } |
|
| 571 | + |
|
| 572 | + /** |
|
| 573 | + * Get the Y coordinate parameter. |
|
| 574 | + * |
|
| 575 | + * @throws \UnexpectedValueException If the parameter has a wrong class |
|
| 576 | + * @throws \LogicException If the parameter is not present |
|
| 577 | + * |
|
| 578 | + * @return Parameter\YCoordinateParameter |
|
| 579 | + */ |
|
| 580 | + public function YCoordinateParameter(): Parameter\YCoordinateParameter |
|
| 581 | + { |
|
| 582 | + return self::_checkType($this->get(Parameter\JWKParameter::P_Y), |
|
| 583 | + Parameter\YCoordinateParameter::class); |
|
| 584 | + } |
|
| 585 | + |
|
| 586 | + /** |
|
| 587 | + * Check that the parameter is an instance of the given class. |
|
| 588 | + * |
|
| 589 | + * @param Parameter\JWKParameter $param Parameter |
|
| 590 | + * @param string $cls Class name |
|
| 591 | + * |
|
| 592 | + * @throws \UnexpectedValueException |
|
| 593 | + * |
|
| 594 | + * @return Parameter\JWKParameter |
|
| 595 | + */ |
|
| 596 | + private static function _checkType(Parameter\JWKParameter $param, string $cls): Parameter\JWKParameter |
|
| 597 | + { |
|
| 598 | + if (!$param instanceof $cls) { |
|
| 599 | + throw new \UnexpectedValueException( |
|
| 600 | + "{$cls} expected, got " . get_class($param)); |
|
| 601 | + } |
|
| 602 | + return $param; |
|
| 603 | + } |
|
| 604 | 604 | } |
@@ -1,6 +1,6 @@ |
||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | -declare(strict_types = 1); |
|
| 3 | +declare(strict_types=1); |
|
| 4 | 4 | |
| 5 | 5 | namespace Sop\JWX\JWK; |
| 6 | 6 | |
@@ -14,197 +14,197 @@ |
||
| 14 | 14 | */ |
| 15 | 15 | class JWK implements \Countable, \IteratorAggregate |
| 16 | 16 | { |
| 17 | - use TypedJWK; |
|
| 18 | - |
|
| 19 | - /** |
|
| 20 | - * Parameters. |
|
| 21 | - * |
|
| 22 | - * @var JWKParameter[] |
|
| 23 | - */ |
|
| 24 | - protected $_parameters; |
|
| 25 | - |
|
| 26 | - /** |
|
| 27 | - * Constructor. |
|
| 28 | - * |
|
| 29 | - * @param JWKParameter ...$params |
|
| 30 | - */ |
|
| 31 | - public function __construct(JWKParameter ...$params) |
|
| 32 | - { |
|
| 33 | - $this->_parameters = []; |
|
| 34 | - foreach ($params as $param) { |
|
| 35 | - $this->_parameters[$param->name()] = $param; |
|
| 36 | - } |
|
| 37 | - } |
|
| 38 | - |
|
| 39 | - /** |
|
| 40 | - * Initialize from an array representing a JSON object. |
|
| 41 | - * |
|
| 42 | - * @param array $members |
|
| 43 | - * |
|
| 44 | - * @return self |
|
| 45 | - */ |
|
| 46 | - public static function fromArray(array $members): self |
|
| 47 | - { |
|
| 48 | - $params = []; |
|
| 49 | - foreach ($members as $name => $value) { |
|
| 50 | - $params[] = JWKParameter::fromNameAndValue($name, $value); |
|
| 51 | - } |
|
| 52 | - return new static(...$params); |
|
| 53 | - } |
|
| 54 | - |
|
| 55 | - /** |
|
| 56 | - * Initialize from a JSON string. |
|
| 57 | - * |
|
| 58 | - * @param string $json |
|
| 59 | - * |
|
| 60 | - * @throws \UnexpectedValueException |
|
| 61 | - * |
|
| 62 | - * @return self |
|
| 63 | - */ |
|
| 64 | - public static function fromJSON(string $json): self |
|
| 65 | - { |
|
| 66 | - $members = json_decode($json, true, 32, JSON_BIGINT_AS_STRING); |
|
| 67 | - if (!is_array($members)) { |
|
| 68 | - throw new \UnexpectedValueException('Invalid JSON.'); |
|
| 69 | - } |
|
| 70 | - return static::fromArray($members); |
|
| 71 | - } |
|
| 72 | - |
|
| 73 | - /** |
|
| 74 | - * Initialize from another JWK. |
|
| 75 | - * |
|
| 76 | - * Allows casting to subclass by late static binding. |
|
| 77 | - * |
|
| 78 | - * @param JWK $jwk |
|
| 79 | - * |
|
| 80 | - * @return self |
|
| 81 | - */ |
|
| 82 | - public static function fromJWK(JWK $jwk): self |
|
| 83 | - { |
|
| 84 | - return new static(...array_values($jwk->_parameters)); |
|
| 85 | - } |
|
| 86 | - |
|
| 87 | - /** |
|
| 88 | - * Get self with parameters added. |
|
| 89 | - * |
|
| 90 | - * @param JWKParameter ...$params |
|
| 91 | - * |
|
| 92 | - * @return self |
|
| 93 | - */ |
|
| 94 | - public function withParameters(JWKParameter ...$params): self |
|
| 95 | - { |
|
| 96 | - $obj = clone $this; |
|
| 97 | - foreach ($params as $param) { |
|
| 98 | - $obj->_parameters[$param->name()] = $param; |
|
| 99 | - } |
|
| 100 | - return $obj; |
|
| 101 | - } |
|
| 102 | - |
|
| 103 | - /** |
|
| 104 | - * Get all parameters. |
|
| 105 | - * |
|
| 106 | - * @return JWKParameter[] |
|
| 107 | - */ |
|
| 108 | - public function parameters(): array |
|
| 109 | - { |
|
| 110 | - return array_values($this->_parameters); |
|
| 111 | - } |
|
| 112 | - |
|
| 113 | - /** |
|
| 114 | - * Get self with given key ID added to parameters. |
|
| 115 | - * |
|
| 116 | - * @param string $id Key ID as a string |
|
| 117 | - * |
|
| 118 | - * @return self |
|
| 119 | - */ |
|
| 120 | - public function withKeyID(string $id): self |
|
| 121 | - { |
|
| 122 | - return $this->withParameters(new KeyIDParameter($id)); |
|
| 123 | - } |
|
| 124 | - |
|
| 125 | - /** |
|
| 126 | - * Whether parameters are present. |
|
| 127 | - * |
|
| 128 | - * Returns false if any of the given parameters is not set. |
|
| 129 | - * |
|
| 130 | - * @param string ...$names Parameter names |
|
| 131 | - * |
|
| 132 | - * @return bool |
|
| 133 | - */ |
|
| 134 | - public function has(string ...$names): bool |
|
| 135 | - { |
|
| 136 | - foreach ($names as $name) { |
|
| 137 | - if (!isset($this->_parameters[$name])) { |
|
| 138 | - return false; |
|
| 139 | - } |
|
| 140 | - } |
|
| 141 | - return true; |
|
| 142 | - } |
|
| 143 | - |
|
| 144 | - /** |
|
| 145 | - * Get a parameter. |
|
| 146 | - * |
|
| 147 | - * @param string $name Parameter name |
|
| 148 | - * |
|
| 149 | - * @throws \LogicException |
|
| 150 | - * |
|
| 151 | - * @return JWKParameter |
|
| 152 | - */ |
|
| 153 | - public function get(string $name): JWKParameter |
|
| 154 | - { |
|
| 155 | - if (!$this->has($name)) { |
|
| 156 | - throw new \LogicException("Parameter {$name} doesn't exists."); |
|
| 157 | - } |
|
| 158 | - return $this->_parameters[$name]; |
|
| 159 | - } |
|
| 160 | - |
|
| 161 | - /** |
|
| 162 | - * Convert to array. |
|
| 163 | - * |
|
| 164 | - * @return array Parameter values keyed by parameter names |
|
| 165 | - */ |
|
| 166 | - public function toArray(): array |
|
| 167 | - { |
|
| 168 | - $a = []; |
|
| 169 | - foreach ($this->_parameters as $param) { |
|
| 170 | - $a[$param->name()] = $param->value(); |
|
| 171 | - } |
|
| 172 | - return $a; |
|
| 173 | - } |
|
| 174 | - |
|
| 175 | - /** |
|
| 176 | - * Convert to JSON. |
|
| 177 | - * |
|
| 178 | - * @return string |
|
| 179 | - */ |
|
| 180 | - public function toJSON(): string |
|
| 181 | - { |
|
| 182 | - $data = $this->toArray(); |
|
| 183 | - if (empty($data)) { |
|
| 184 | - return ''; |
|
| 185 | - } |
|
| 186 | - return json_encode((object) $data, JSON_UNESCAPED_SLASHES); |
|
| 187 | - } |
|
| 188 | - |
|
| 189 | - /** |
|
| 190 | - * Get the number of parameters. |
|
| 191 | - * |
|
| 192 | - * @see \Countable::count() |
|
| 193 | - */ |
|
| 194 | - public function count(): int |
|
| 195 | - { |
|
| 196 | - return count($this->_parameters); |
|
| 197 | - } |
|
| 198 | - |
|
| 199 | - /** |
|
| 200 | - * Get iterator for the parameters. |
|
| 201 | - * |
|
| 202 | - * @see \IteratorAggregate::getIterator() |
|
| 203 | - * |
|
| 204 | - * @return \ArrayIterator |
|
| 205 | - */ |
|
| 206 | - public function getIterator(): \ArrayIterator |
|
| 207 | - { |
|
| 208 | - return new \ArrayIterator($this->_parameters); |
|
| 209 | - } |
|
| 17 | + use TypedJWK; |
|
| 18 | + |
|
| 19 | + /** |
|
| 20 | + * Parameters. |
|
| 21 | + * |
|
| 22 | + * @var JWKParameter[] |
|
| 23 | + */ |
|
| 24 | + protected $_parameters; |
|
| 25 | + |
|
| 26 | + /** |
|
| 27 | + * Constructor. |
|
| 28 | + * |
|
| 29 | + * @param JWKParameter ...$params |
|
| 30 | + */ |
|
| 31 | + public function __construct(JWKParameter ...$params) |
|
| 32 | + { |
|
| 33 | + $this->_parameters = []; |
|
| 34 | + foreach ($params as $param) { |
|
| 35 | + $this->_parameters[$param->name()] = $param; |
|
| 36 | + } |
|
| 37 | + } |
|
| 38 | + |
|
| 39 | + /** |
|
| 40 | + * Initialize from an array representing a JSON object. |
|
| 41 | + * |
|
| 42 | + * @param array $members |
|
| 43 | + * |
|
| 44 | + * @return self |
|
| 45 | + */ |
|
| 46 | + public static function fromArray(array $members): self |
|
| 47 | + { |
|
| 48 | + $params = []; |
|
| 49 | + foreach ($members as $name => $value) { |
|
| 50 | + $params[] = JWKParameter::fromNameAndValue($name, $value); |
|
| 51 | + } |
|
| 52 | + return new static(...$params); |
|
| 53 | + } |
|
| 54 | + |
|
| 55 | + /** |
|
| 56 | + * Initialize from a JSON string. |
|
| 57 | + * |
|
| 58 | + * @param string $json |
|
| 59 | + * |
|
| 60 | + * @throws \UnexpectedValueException |
|
| 61 | + * |
|
| 62 | + * @return self |
|
| 63 | + */ |
|
| 64 | + public static function fromJSON(string $json): self |
|
| 65 | + { |
|
| 66 | + $members = json_decode($json, true, 32, JSON_BIGINT_AS_STRING); |
|
| 67 | + if (!is_array($members)) { |
|
| 68 | + throw new \UnexpectedValueException('Invalid JSON.'); |
|
| 69 | + } |
|
| 70 | + return static::fromArray($members); |
|
| 71 | + } |
|
| 72 | + |
|
| 73 | + /** |
|
| 74 | + * Initialize from another JWK. |
|
| 75 | + * |
|
| 76 | + * Allows casting to subclass by late static binding. |
|
| 77 | + * |
|
| 78 | + * @param JWK $jwk |
|
| 79 | + * |
|
| 80 | + * @return self |
|
| 81 | + */ |
|
| 82 | + public static function fromJWK(JWK $jwk): self |
|
| 83 | + { |
|
| 84 | + return new static(...array_values($jwk->_parameters)); |
|
| 85 | + } |
|
| 86 | + |
|
| 87 | + /** |
|
| 88 | + * Get self with parameters added. |
|
| 89 | + * |
|
| 90 | + * @param JWKParameter ...$params |
|
| 91 | + * |
|
| 92 | + * @return self |
|
| 93 | + */ |
|
| 94 | + public function withParameters(JWKParameter ...$params): self |
|
| 95 | + { |
|
| 96 | + $obj = clone $this; |
|
| 97 | + foreach ($params as $param) { |
|
| 98 | + $obj->_parameters[$param->name()] = $param; |
|
| 99 | + } |
|
| 100 | + return $obj; |
|
| 101 | + } |
|
| 102 | + |
|
| 103 | + /** |
|
| 104 | + * Get all parameters. |
|
| 105 | + * |
|
| 106 | + * @return JWKParameter[] |
|
| 107 | + */ |
|
| 108 | + public function parameters(): array |
|
| 109 | + { |
|
| 110 | + return array_values($this->_parameters); |
|
| 111 | + } |
|
| 112 | + |
|
| 113 | + /** |
|
| 114 | + * Get self with given key ID added to parameters. |
|
| 115 | + * |
|
| 116 | + * @param string $id Key ID as a string |
|
| 117 | + * |
|
| 118 | + * @return self |
|
| 119 | + */ |
|
| 120 | + public function withKeyID(string $id): self |
|
| 121 | + { |
|
| 122 | + return $this->withParameters(new KeyIDParameter($id)); |
|
| 123 | + } |
|
| 124 | + |
|
| 125 | + /** |
|
| 126 | + * Whether parameters are present. |
|
| 127 | + * |
|
| 128 | + * Returns false if any of the given parameters is not set. |
|
| 129 | + * |
|
| 130 | + * @param string ...$names Parameter names |
|
| 131 | + * |
|
| 132 | + * @return bool |
|
| 133 | + */ |
|
| 134 | + public function has(string ...$names): bool |
|
| 135 | + { |
|
| 136 | + foreach ($names as $name) { |
|
| 137 | + if (!isset($this->_parameters[$name])) { |
|
| 138 | + return false; |
|
| 139 | + } |
|
| 140 | + } |
|
| 141 | + return true; |
|
| 142 | + } |
|
| 143 | + |
|
| 144 | + /** |
|
| 145 | + * Get a parameter. |
|
| 146 | + * |
|
| 147 | + * @param string $name Parameter name |
|
| 148 | + * |
|
| 149 | + * @throws \LogicException |
|
| 150 | + * |
|
| 151 | + * @return JWKParameter |
|
| 152 | + */ |
|
| 153 | + public function get(string $name): JWKParameter |
|
| 154 | + { |
|
| 155 | + if (!$this->has($name)) { |
|
| 156 | + throw new \LogicException("Parameter {$name} doesn't exists."); |
|
| 157 | + } |
|
| 158 | + return $this->_parameters[$name]; |
|
| 159 | + } |
|
| 160 | + |
|
| 161 | + /** |
|
| 162 | + * Convert to array. |
|
| 163 | + * |
|
| 164 | + * @return array Parameter values keyed by parameter names |
|
| 165 | + */ |
|
| 166 | + public function toArray(): array |
|
| 167 | + { |
|
| 168 | + $a = []; |
|
| 169 | + foreach ($this->_parameters as $param) { |
|
| 170 | + $a[$param->name()] = $param->value(); |
|
| 171 | + } |
|
| 172 | + return $a; |
|
| 173 | + } |
|
| 174 | + |
|
| 175 | + /** |
|
| 176 | + * Convert to JSON. |
|
| 177 | + * |
|
| 178 | + * @return string |
|
| 179 | + */ |
|
| 180 | + public function toJSON(): string |
|
| 181 | + { |
|
| 182 | + $data = $this->toArray(); |
|
| 183 | + if (empty($data)) { |
|
| 184 | + return ''; |
|
| 185 | + } |
|
| 186 | + return json_encode((object) $data, JSON_UNESCAPED_SLASHES); |
|
| 187 | + } |
|
| 188 | + |
|
| 189 | + /** |
|
| 190 | + * Get the number of parameters. |
|
| 191 | + * |
|
| 192 | + * @see \Countable::count() |
|
| 193 | + */ |
|
| 194 | + public function count(): int |
|
| 195 | + { |
|
| 196 | + return count($this->_parameters); |
|
| 197 | + } |
|
| 198 | + |
|
| 199 | + /** |
|
| 200 | + * Get iterator for the parameters. |
|
| 201 | + * |
|
| 202 | + * @see \IteratorAggregate::getIterator() |
|
| 203 | + * |
|
| 204 | + * @return \ArrayIterator |
|
| 205 | + */ |
|
| 206 | + public function getIterator(): \ArrayIterator |
|
| 207 | + { |
|
| 208 | + return new \ArrayIterator($this->_parameters); |
|
| 209 | + } |
|
| 210 | 210 | } |
@@ -1,6 +1,6 @@ |
||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | -declare(strict_types = 1); |
|
| 3 | +declare(strict_types=1); |
|
| 4 | 4 | |
| 5 | 5 | namespace Sop\JWX\JWK; |
| 6 | 6 | |
@@ -18,42 +18,42 @@ |
||
| 18 | 18 | */ |
| 19 | 19 | abstract class PublicKeyJWK extends JWK |
| 20 | 20 | { |
| 21 | - /** |
|
| 22 | - * Convert public key to PEM. |
|
| 23 | - * |
|
| 24 | - * @return PEM |
|
| 25 | - */ |
|
| 26 | - abstract public function toPEM(): PEM; |
|
| 21 | + /** |
|
| 22 | + * Convert public key to PEM. |
|
| 23 | + * |
|
| 24 | + * @return PEM |
|
| 25 | + */ |
|
| 26 | + abstract public function toPEM(): PEM; |
|
| 27 | 27 | |
| 28 | - /** |
|
| 29 | - * Initialize from a PublicKey object. |
|
| 30 | - * |
|
| 31 | - * @param PublicKey $pub_key Public key |
|
| 32 | - * |
|
| 33 | - * @throws \UnexpectedValueException |
|
| 34 | - * |
|
| 35 | - * @return self |
|
| 36 | - */ |
|
| 37 | - public static function fromPublicKey(PublicKey $pub_key): PublicKeyJWK |
|
| 38 | - { |
|
| 39 | - if ($pub_key instanceof RSAPublicKey) { |
|
| 40 | - return RSAPublicKeyJWK::fromRSAPublicKey($pub_key); |
|
| 41 | - } |
|
| 42 | - if ($pub_key instanceof ECPublicKey) { |
|
| 43 | - return ECPublicKeyJWK::fromECPublicKey($pub_key); |
|
| 44 | - } |
|
| 45 | - throw new \UnexpectedValueException('Unsupported public key.'); |
|
| 46 | - } |
|
| 28 | + /** |
|
| 29 | + * Initialize from a PublicKey object. |
|
| 30 | + * |
|
| 31 | + * @param PublicKey $pub_key Public key |
|
| 32 | + * |
|
| 33 | + * @throws \UnexpectedValueException |
|
| 34 | + * |
|
| 35 | + * @return self |
|
| 36 | + */ |
|
| 37 | + public static function fromPublicKey(PublicKey $pub_key): PublicKeyJWK |
|
| 38 | + { |
|
| 39 | + if ($pub_key instanceof RSAPublicKey) { |
|
| 40 | + return RSAPublicKeyJWK::fromRSAPublicKey($pub_key); |
|
| 41 | + } |
|
| 42 | + if ($pub_key instanceof ECPublicKey) { |
|
| 43 | + return ECPublicKeyJWK::fromECPublicKey($pub_key); |
|
| 44 | + } |
|
| 45 | + throw new \UnexpectedValueException('Unsupported public key.'); |
|
| 46 | + } |
|
| 47 | 47 | |
| 48 | - /** |
|
| 49 | - * Initialize from a PublicKeyInfo object. |
|
| 50 | - * |
|
| 51 | - * @param PublicKeyInfo $pki Public key info |
|
| 52 | - * |
|
| 53 | - * @return self |
|
| 54 | - */ |
|
| 55 | - public static function fromPublicKeyInfo(PublicKeyInfo $pki): PublicKeyJWK |
|
| 56 | - { |
|
| 57 | - return self::fromPublicKey($pki->publicKey()); |
|
| 58 | - } |
|
| 48 | + /** |
|
| 49 | + * Initialize from a PublicKeyInfo object. |
|
| 50 | + * |
|
| 51 | + * @param PublicKeyInfo $pki Public key info |
|
| 52 | + * |
|
| 53 | + * @return self |
|
| 54 | + */ |
|
| 55 | + public static function fromPublicKeyInfo(PublicKeyInfo $pki): PublicKeyJWK |
|
| 56 | + { |
|
| 57 | + return self::fromPublicKey($pki->publicKey()); |
|
| 58 | + } |
|
| 59 | 59 | } |
@@ -1,6 +1,6 @@ |
||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | -declare(strict_types = 1); |
|
| 3 | +declare(strict_types=1); |
|
| 4 | 4 | |
| 5 | 5 | namespace Sop\JWX\JWK\Asymmetric; |
| 6 | 6 | |
@@ -18,49 +18,49 @@ |
||
| 18 | 18 | */ |
| 19 | 19 | abstract class PrivateKeyJWK extends JWK |
| 20 | 20 | { |
| 21 | - /** |
|
| 22 | - * Get the public key component of the asymmetric key pair. |
|
| 23 | - * |
|
| 24 | - * @return PublicKeyJWK |
|
| 25 | - */ |
|
| 26 | - abstract public function publicKey(): PublicKeyJWK; |
|
| 21 | + /** |
|
| 22 | + * Get the public key component of the asymmetric key pair. |
|
| 23 | + * |
|
| 24 | + * @return PublicKeyJWK |
|
| 25 | + */ |
|
| 26 | + abstract public function publicKey(): PublicKeyJWK; |
|
| 27 | 27 | |
| 28 | - /** |
|
| 29 | - * Convert private key to PEM. |
|
| 30 | - * |
|
| 31 | - * @return PEM |
|
| 32 | - */ |
|
| 33 | - abstract public function toPEM(): PEM; |
|
| 28 | + /** |
|
| 29 | + * Convert private key to PEM. |
|
| 30 | + * |
|
| 31 | + * @return PEM |
|
| 32 | + */ |
|
| 33 | + abstract public function toPEM(): PEM; |
|
| 34 | 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 | - public static function fromPrivateKey(PrivateKey $priv_key): PrivateKeyJWK |
|
| 45 | - { |
|
| 46 | - if ($priv_key instanceof RSAPrivateKey) { |
|
| 47 | - return RSAPrivateKeyJWK::fromRSAPrivateKey($priv_key); |
|
| 48 | - } |
|
| 49 | - if ($priv_key instanceof ECPrivateKey) { |
|
| 50 | - return ECPrivateKeyJWK::fromECPrivateKey($priv_key); |
|
| 51 | - } |
|
| 52 | - throw new \UnexpectedValueException('Unsupported private key.'); |
|
| 53 | - } |
|
| 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 | + public static function fromPrivateKey(PrivateKey $priv_key): PrivateKeyJWK |
|
| 45 | + { |
|
| 46 | + if ($priv_key instanceof RSAPrivateKey) { |
|
| 47 | + return RSAPrivateKeyJWK::fromRSAPrivateKey($priv_key); |
|
| 48 | + } |
|
| 49 | + if ($priv_key instanceof ECPrivateKey) { |
|
| 50 | + return ECPrivateKeyJWK::fromECPrivateKey($priv_key); |
|
| 51 | + } |
|
| 52 | + throw new \UnexpectedValueException('Unsupported private key.'); |
|
| 53 | + } |
|
| 54 | 54 | |
| 55 | - /** |
|
| 56 | - * Initialize from a PrivateKeyInfo object. |
|
| 57 | - * |
|
| 58 | - * @param PrivateKeyInfo $pki PrivateKeyInfo |
|
| 59 | - * |
|
| 60 | - * @return self |
|
| 61 | - */ |
|
| 62 | - public static function fromPrivateKeyInfo(PrivateKeyInfo $pki): PrivateKeyJWK |
|
| 63 | - { |
|
| 64 | - return self::fromPrivateKey($pki->privateKey()); |
|
| 65 | - } |
|
| 55 | + /** |
|
| 56 | + * Initialize from a PrivateKeyInfo object. |
|
| 57 | + * |
|
| 58 | + * @param PrivateKeyInfo $pki PrivateKeyInfo |
|
| 59 | + * |
|
| 60 | + * @return self |
|
| 61 | + */ |
|
| 62 | + public static function fromPrivateKeyInfo(PrivateKeyInfo $pki): PrivateKeyJWK |
|
| 63 | + { |
|
| 64 | + return self::fromPrivateKey($pki->privateKey()); |
|
| 65 | + } |
|
| 66 | 66 | } |
@@ -1,6 +1,6 @@ |
||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | -declare(strict_types = 1); |
|
| 3 | +declare(strict_types=1); |
|
| 4 | 4 | |
| 5 | 5 | namespace Sop\JWX\JWK\Asymmetric; |
| 6 | 6 | |
@@ -23,85 +23,85 @@ |
||
| 23 | 23 | */ |
| 24 | 24 | class ECPublicKeyJWK extends PublicKeyJWK |
| 25 | 25 | { |
| 26 | - /** |
|
| 27 | - * Parameter names managed by this class. |
|
| 28 | - * |
|
| 29 | - * @var string[] |
|
| 30 | - */ |
|
| 31 | - const MANAGED_PARAMS = [ |
|
| 32 | - JWKParameter::PARAM_KEY_TYPE, |
|
| 33 | - JWKParameter::PARAM_CURVE, |
|
| 34 | - JWKParameter::PARAM_X_COORDINATE, |
|
| 35 | - ]; |
|
| 26 | + /** |
|
| 27 | + * Parameter names managed by this class. |
|
| 28 | + * |
|
| 29 | + * @var string[] |
|
| 30 | + */ |
|
| 31 | + const MANAGED_PARAMS = [ |
|
| 32 | + JWKParameter::PARAM_KEY_TYPE, |
|
| 33 | + JWKParameter::PARAM_CURVE, |
|
| 34 | + JWKParameter::PARAM_X_COORDINATE, |
|
| 35 | + ]; |
|
| 36 | 36 | |
| 37 | - /** |
|
| 38 | - * Constructor. |
|
| 39 | - * |
|
| 40 | - * @param JWKParameter ...$params |
|
| 41 | - * |
|
| 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( |
|
| 50 | - "Missing '{$name}' parameter."); |
|
| 51 | - } |
|
| 52 | - } |
|
| 53 | - if (KeyTypeParameter::TYPE_EC !== $this->keyTypeParameter()->value()) { |
|
| 54 | - throw new \UnexpectedValueException('Invalid key type.'); |
|
| 55 | - } |
|
| 56 | - } |
|
| 37 | + /** |
|
| 38 | + * Constructor. |
|
| 39 | + * |
|
| 40 | + * @param JWKParameter ...$params |
|
| 41 | + * |
|
| 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( |
|
| 50 | + "Missing '{$name}' parameter."); |
|
| 51 | + } |
|
| 52 | + } |
|
| 53 | + if (KeyTypeParameter::TYPE_EC !== $this->keyTypeParameter()->value()) { |
|
| 54 | + throw new \UnexpectedValueException('Invalid key type.'); |
|
| 55 | + } |
|
| 56 | + } |
|
| 57 | 57 | |
| 58 | - /** |
|
| 59 | - * Initialize from ECPublicKey. |
|
| 60 | - * |
|
| 61 | - * @param ECPublicKey $pk |
|
| 62 | - * |
|
| 63 | - * @throws \UnexpectedValueException |
|
| 64 | - * |
|
| 65 | - * @return self |
|
| 66 | - */ |
|
| 67 | - public static function fromECPublicKey(ECPublicKey $pk): self |
|
| 68 | - { |
|
| 69 | - if (!$pk->hasNamedCurve()) { |
|
| 70 | - throw new \UnexpectedValueException('No curve name.'); |
|
| 71 | - } |
|
| 72 | - $curve = CurveParameter::fromOID($pk->namedCurve()); |
|
| 73 | - [$x, $y] = $pk->curvePointOctets(); |
|
| 74 | - $xcoord = XCoordinateParameter::fromString($x); |
|
| 75 | - $ycoord = YCoordinateParameter::fromString($y); |
|
| 76 | - $key_type = new KeyTypeParameter(KeyTypeParameter::TYPE_EC); |
|
| 77 | - return new self($key_type, $curve, $xcoord, $ycoord); |
|
| 78 | - } |
|
| 58 | + /** |
|
| 59 | + * Initialize from ECPublicKey. |
|
| 60 | + * |
|
| 61 | + * @param ECPublicKey $pk |
|
| 62 | + * |
|
| 63 | + * @throws \UnexpectedValueException |
|
| 64 | + * |
|
| 65 | + * @return self |
|
| 66 | + */ |
|
| 67 | + public static function fromECPublicKey(ECPublicKey $pk): self |
|
| 68 | + { |
|
| 69 | + if (!$pk->hasNamedCurve()) { |
|
| 70 | + throw new \UnexpectedValueException('No curve name.'); |
|
| 71 | + } |
|
| 72 | + $curve = CurveParameter::fromOID($pk->namedCurve()); |
|
| 73 | + [$x, $y] = $pk->curvePointOctets(); |
|
| 74 | + $xcoord = XCoordinateParameter::fromString($x); |
|
| 75 | + $ycoord = YCoordinateParameter::fromString($y); |
|
| 76 | + $key_type = new KeyTypeParameter(KeyTypeParameter::TYPE_EC); |
|
| 77 | + return new self($key_type, $curve, $xcoord, $ycoord); |
|
| 78 | + } |
|
| 79 | 79 | |
| 80 | - /** |
|
| 81 | - * Initialize from PEM. |
|
| 82 | - * |
|
| 83 | - * @param PEM $pem |
|
| 84 | - * |
|
| 85 | - * @return self |
|
| 86 | - */ |
|
| 87 | - public static function fromPEM(PEM $pem): self |
|
| 88 | - { |
|
| 89 | - return self::fromECPublicKey(ECPublicKey::fromPEM($pem)); |
|
| 90 | - } |
|
| 80 | + /** |
|
| 81 | + * Initialize from PEM. |
|
| 82 | + * |
|
| 83 | + * @param PEM $pem |
|
| 84 | + * |
|
| 85 | + * @return self |
|
| 86 | + */ |
|
| 87 | + public static function fromPEM(PEM $pem): self |
|
| 88 | + { |
|
| 89 | + return self::fromECPublicKey(ECPublicKey::fromPEM($pem)); |
|
| 90 | + } |
|
| 91 | 91 | |
| 92 | - /** |
|
| 93 | - * Convert EC public key to PEM. |
|
| 94 | - * |
|
| 95 | - * @return PEM |
|
| 96 | - */ |
|
| 97 | - public function toPEM(): PEM |
|
| 98 | - { |
|
| 99 | - $curve_oid = CurveParameter::nameToOID($this->curveParameter()->value()); |
|
| 100 | - $x = ECConversion::octetsToNumber( |
|
| 101 | - $this->XCoordinateParameter()->coordinateOctets()); |
|
| 102 | - $y = ECConversion::octetsToNumber( |
|
| 103 | - $this->YCoordinateParameter()->coordinateOctets()); |
|
| 104 | - $ec = ECPublicKey::fromCoordinates($x, $y, $curve_oid); |
|
| 105 | - return $ec->publicKeyInfo()->toPEM(); |
|
| 106 | - } |
|
| 92 | + /** |
|
| 93 | + * Convert EC public key to PEM. |
|
| 94 | + * |
|
| 95 | + * @return PEM |
|
| 96 | + */ |
|
| 97 | + public function toPEM(): PEM |
|
| 98 | + { |
|
| 99 | + $curve_oid = CurveParameter::nameToOID($this->curveParameter()->value()); |
|
| 100 | + $x = ECConversion::octetsToNumber( |
|
| 101 | + $this->XCoordinateParameter()->coordinateOctets()); |
|
| 102 | + $y = ECConversion::octetsToNumber( |
|
| 103 | + $this->YCoordinateParameter()->coordinateOctets()); |
|
| 104 | + $ec = ECPublicKey::fromCoordinates($x, $y, $curve_oid); |
|
| 105 | + return $ec->publicKeyInfo()->toPEM(); |
|
| 106 | + } |
|
| 107 | 107 | } |
@@ -1,6 +1,6 @@ |
||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | -declare(strict_types = 1); |
|
| 3 | +declare(strict_types=1); |
|
| 4 | 4 | |
| 5 | 5 | namespace Sop\JWX\JWK\EC; |
| 6 | 6 | |
@@ -26,109 +26,109 @@ |
||
| 26 | 26 | */ |
| 27 | 27 | class ECPrivateKeyJWK extends PrivateKeyJWK |
| 28 | 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 | - ]; |
|
| 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 | 42 | |
| 43 | - /** |
|
| 44 | - * Constructor. |
|
| 45 | - * |
|
| 46 | - * @param JWKParameter ...$params |
|
| 47 | - * |
|
| 48 | - * @throws \UnexpectedValueException If missing required parameter |
|
| 49 | - */ |
|
| 50 | - public function __construct(JWKParameter ...$params) |
|
| 51 | - { |
|
| 52 | - parent::__construct(...$params); |
|
| 53 | - foreach (self::MANAGED_PARAMS as $name) { |
|
| 54 | - if (!$this->has($name)) { |
|
| 55 | - throw new \UnexpectedValueException("Missing '{$name}' parameter."); |
|
| 56 | - } |
|
| 57 | - } |
|
| 58 | - if (KeyTypeParameter::TYPE_EC !== $this->keyTypeParameter()->value()) { |
|
| 59 | - throw new \UnexpectedValueException('Invalid key type.'); |
|
| 60 | - } |
|
| 61 | - // cast ECC private key parameter to correct class |
|
| 62 | - $key = JWKParameter::PARAM_ECC_PRIVATE_KEY; |
|
| 63 | - $this->_parameters[$key] = new ECCPrivateKeyParameter( |
|
| 64 | - $this->_parameters[$key]->value()); |
|
| 65 | - } |
|
| 43 | + /** |
|
| 44 | + * Constructor. |
|
| 45 | + * |
|
| 46 | + * @param JWKParameter ...$params |
|
| 47 | + * |
|
| 48 | + * @throws \UnexpectedValueException If missing required parameter |
|
| 49 | + */ |
|
| 50 | + public function __construct(JWKParameter ...$params) |
|
| 51 | + { |
|
| 52 | + parent::__construct(...$params); |
|
| 53 | + foreach (self::MANAGED_PARAMS as $name) { |
|
| 54 | + if (!$this->has($name)) { |
|
| 55 | + throw new \UnexpectedValueException("Missing '{$name}' parameter."); |
|
| 56 | + } |
|
| 57 | + } |
|
| 58 | + if (KeyTypeParameter::TYPE_EC !== $this->keyTypeParameter()->value()) { |
|
| 59 | + throw new \UnexpectedValueException('Invalid key type.'); |
|
| 60 | + } |
|
| 61 | + // cast ECC private key parameter to correct class |
|
| 62 | + $key = JWKParameter::PARAM_ECC_PRIVATE_KEY; |
|
| 63 | + $this->_parameters[$key] = new ECCPrivateKeyParameter( |
|
| 64 | + $this->_parameters[$key]->value()); |
|
| 65 | + } |
|
| 66 | 66 | |
| 67 | - /** |
|
| 68 | - * Initialize from ECPrivateKey. |
|
| 69 | - * |
|
| 70 | - * @param ECPrivateKey $pk |
|
| 71 | - * |
|
| 72 | - * @throws \UnexpectedValueException |
|
| 73 | - * |
|
| 74 | - * @return self |
|
| 75 | - */ |
|
| 76 | - public static function fromECPrivateKey(ECPrivateKey $pk): self |
|
| 77 | - { |
|
| 78 | - if (!$pk->hasNamedCurve()) { |
|
| 79 | - throw new \UnexpectedValueException('No curve name.'); |
|
| 80 | - } |
|
| 81 | - $curve = CurveParameter::fromOID($pk->namedCurve()); |
|
| 82 | - $pubkey = $pk->publicKey(); |
|
| 83 | - [$x, $y] = $pubkey->curvePointOctets(); |
|
| 84 | - $xcoord = XCoordinateParameter::fromString($x); |
|
| 85 | - $ycoord = YCoordinateParameter::fromString($y); |
|
| 86 | - $priv = ECCPrivateKeyParameter::fromString($pk->privateKeyOctets()); |
|
| 87 | - $key_type = new KeyTypeParameter(KeyTypeParameter::TYPE_EC); |
|
| 88 | - return new self($key_type, $curve, $xcoord, $ycoord, $priv); |
|
| 89 | - } |
|
| 67 | + /** |
|
| 68 | + * Initialize from ECPrivateKey. |
|
| 69 | + * |
|
| 70 | + * @param ECPrivateKey $pk |
|
| 71 | + * |
|
| 72 | + * @throws \UnexpectedValueException |
|
| 73 | + * |
|
| 74 | + * @return self |
|
| 75 | + */ |
|
| 76 | + public static function fromECPrivateKey(ECPrivateKey $pk): self |
|
| 77 | + { |
|
| 78 | + if (!$pk->hasNamedCurve()) { |
|
| 79 | + throw new \UnexpectedValueException('No curve name.'); |
|
| 80 | + } |
|
| 81 | + $curve = CurveParameter::fromOID($pk->namedCurve()); |
|
| 82 | + $pubkey = $pk->publicKey(); |
|
| 83 | + [$x, $y] = $pubkey->curvePointOctets(); |
|
| 84 | + $xcoord = XCoordinateParameter::fromString($x); |
|
| 85 | + $ycoord = YCoordinateParameter::fromString($y); |
|
| 86 | + $priv = ECCPrivateKeyParameter::fromString($pk->privateKeyOctets()); |
|
| 87 | + $key_type = new KeyTypeParameter(KeyTypeParameter::TYPE_EC); |
|
| 88 | + return new self($key_type, $curve, $xcoord, $ycoord, $priv); |
|
| 89 | + } |
|
| 90 | 90 | |
| 91 | - /** |
|
| 92 | - * Initialize from PEM. |
|
| 93 | - * |
|
| 94 | - * @param PEM $pem |
|
| 95 | - * |
|
| 96 | - * @return self |
|
| 97 | - */ |
|
| 98 | - public static function fromPEM(PEM $pem): self |
|
| 99 | - { |
|
| 100 | - return self::fromECPrivateKey(ECPrivateKey::fromPEM($pem)); |
|
| 101 | - } |
|
| 91 | + /** |
|
| 92 | + * Initialize from PEM. |
|
| 93 | + * |
|
| 94 | + * @param PEM $pem |
|
| 95 | + * |
|
| 96 | + * @return self |
|
| 97 | + */ |
|
| 98 | + public static function fromPEM(PEM $pem): self |
|
| 99 | + { |
|
| 100 | + return self::fromECPrivateKey(ECPrivateKey::fromPEM($pem)); |
|
| 101 | + } |
|
| 102 | 102 | |
| 103 | - /** |
|
| 104 | - * Get the public key component of the EC private key. |
|
| 105 | - * |
|
| 106 | - * @return ECPublicKeyJWK |
|
| 107 | - */ |
|
| 108 | - public function publicKey(): PublicKeyJWK |
|
| 109 | - { |
|
| 110 | - $kty = $this->keyTypeParameter(); |
|
| 111 | - $curve = $this->curveParameter(); |
|
| 112 | - $xcoord = $this->XCoordinateParameter(); |
|
| 113 | - $ycoord = $this->YCoordinateParameter(); |
|
| 114 | - return new ECPublicKeyJWK($kty, $curve, $xcoord, $ycoord); |
|
| 115 | - } |
|
| 103 | + /** |
|
| 104 | + * Get the public key component of the EC private key. |
|
| 105 | + * |
|
| 106 | + * @return ECPublicKeyJWK |
|
| 107 | + */ |
|
| 108 | + public function publicKey(): PublicKeyJWK |
|
| 109 | + { |
|
| 110 | + $kty = $this->keyTypeParameter(); |
|
| 111 | + $curve = $this->curveParameter(); |
|
| 112 | + $xcoord = $this->XCoordinateParameter(); |
|
| 113 | + $ycoord = $this->YCoordinateParameter(); |
|
| 114 | + return new ECPublicKeyJWK($kty, $curve, $xcoord, $ycoord); |
|
| 115 | + } |
|
| 116 | 116 | |
| 117 | - /** |
|
| 118 | - * Convert EC private key to PEM. |
|
| 119 | - * |
|
| 120 | - * @return PEM |
|
| 121 | - */ |
|
| 122 | - public function toPEM(): PEM |
|
| 123 | - { |
|
| 124 | - $curve_oid = CurveParameter::nameToOID($this->curveParameter()->value()); |
|
| 125 | - $x = ECConversion::octetsToNumber( |
|
| 126 | - $this->XCoordinateParameter()->coordinateOctets()); |
|
| 127 | - $y = ECConversion::octetsToNumber( |
|
| 128 | - $this->YCoordinateParameter()->coordinateOctets()); |
|
| 129 | - $pubkey = ECPublicKey::fromCoordinates($x, $y, $curve_oid); |
|
| 130 | - $priv = $this->ECCPrivateKeyParameter()->privateKeyOctets(); |
|
| 131 | - $ec = new ECPrivateKey($priv, $curve_oid, $pubkey->ECPoint()); |
|
| 132 | - return $ec->privateKeyInfo()->toPEM(); |
|
| 133 | - } |
|
| 117 | + /** |
|
| 118 | + * Convert EC private key to PEM. |
|
| 119 | + * |
|
| 120 | + * @return PEM |
|
| 121 | + */ |
|
| 122 | + public function toPEM(): PEM |
|
| 123 | + { |
|
| 124 | + $curve_oid = CurveParameter::nameToOID($this->curveParameter()->value()); |
|
| 125 | + $x = ECConversion::octetsToNumber( |
|
| 126 | + $this->XCoordinateParameter()->coordinateOctets()); |
|
| 127 | + $y = ECConversion::octetsToNumber( |
|
| 128 | + $this->YCoordinateParameter()->coordinateOctets()); |
|
| 129 | + $pubkey = ECPublicKey::fromCoordinates($x, $y, $curve_oid); |
|
| 130 | + $priv = $this->ECCPrivateKeyParameter()->privateKeyOctets(); |
|
| 131 | + $ec = new ECPrivateKey($priv, $curve_oid, $pubkey->ECPoint()); |
|
| 132 | + return $ec->privateKeyInfo()->toPEM(); |
|
| 133 | + } |
|
| 134 | 134 | } |
@@ -1,6 +1,6 @@ |
||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | -declare(strict_types = 1); |
|
| 3 | +declare(strict_types=1); |
|
| 4 | 4 | |
| 5 | 5 | namespace Sop\JWX\JWK\EC; |
| 6 | 6 | |