| @@ 102-125 (lines=24) @@ | ||
| 99 | * @throws \UnexpectedValueException |
|
| 100 | * @return self |
|
| 101 | */ |
|
| 102 | public static function fromJWK(JWK $jwk, $alg = null) { |
|
| 103 | // if algorithm is not explicitly given, consult JWK |
|
| 104 | if (!isset($alg)) { |
|
| 105 | if (!$jwk->hasAlgorithmParameter()) { |
|
| 106 | throw new \UnexpectedValueException( |
|
| 107 | "Missing algorithm parameter."); |
|
| 108 | } |
|
| 109 | $alg = $jwk->algorithmParameter()->value(); |
|
| 110 | } |
|
| 111 | if (!array_key_exists($alg, self::MAP_NAME_TO_CLASS)) { |
|
| 112 | throw new \UnexpectedValueException( |
|
| 113 | "Algorithm '$alg' not supported."); |
|
| 114 | } |
|
| 115 | $cls = self::MAP_NAME_TO_CLASS[$alg]; |
|
| 116 | $params = ECPrivateKeyJWK::MANAGED_PARAMS; |
|
| 117 | if ($jwk->has(...$params)) { |
|
| 118 | return $cls::fromPrivateKey(ECPrivateKeyJWK::fromJWK($jwk)); |
|
| 119 | } |
|
| 120 | $params = ECPublicKeyJWK::MANAGED_PARAMS; |
|
| 121 | if ($jwk->has(...$params)) { |
|
| 122 | return $cls::fromPublicKey(ECPublicKeyJWK::fromJWK($jwk)); |
|
| 123 | } |
|
| 124 | throw new \UnexpectedValueException("Not an EC key."); |
|
| 125 | } |
|
| 126 | ||
| 127 | /** |
|
| 128 | * |
|
| @@ 76-99 (lines=24) @@ | ||
| 73 | * @throws \UnexpectedValueException |
|
| 74 | * @return self |
|
| 75 | */ |
|
| 76 | public static function fromJWK(JWK $jwk, $alg = null) { |
|
| 77 | // if algorithm is not explicitly given, consult JWK |
|
| 78 | if (!isset($alg)) { |
|
| 79 | if (!$jwk->hasAlgorithmParameter()) { |
|
| 80 | throw new \UnexpectedValueException( |
|
| 81 | "Missing algorithm parameter."); |
|
| 82 | } |
|
| 83 | $alg = $jwk->algorithmParameter()->value(); |
|
| 84 | } |
|
| 85 | if (!array_key_exists($alg, self::MAP_NAME_TO_CLASS)) { |
|
| 86 | throw new \UnexpectedValueException( |
|
| 87 | "Algorithm '$alg' not supported."); |
|
| 88 | } |
|
| 89 | $cls = self::MAP_NAME_TO_CLASS[$alg]; |
|
| 90 | $params = RSAPrivateKeyJWK::MANAGED_PARAMS; |
|
| 91 | if ($jwk->has(...$params)) { |
|
| 92 | return $cls::fromPrivateKey(RSAPrivateKeyJWK::fromJWK($jwk)); |
|
| 93 | } |
|
| 94 | $params = RSAPublicKeyJWK::MANAGED_PARAMS; |
|
| 95 | if ($jwk->has(...$params)) { |
|
| 96 | return $cls::fromPublicKey(RSAPublicKeyJWK::fromJWK($jwk)); |
|
| 97 | } |
|
| 98 | throw new \UnexpectedValueException("Not an RSA key."); |
|
| 99 | } |
|
| 100 | ||
| 101 | public function headerParameters() { |
|
| 102 | return array(AlgorithmParameter::fromAlgorithm($this)); |
|