GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( 59f4b4...e271c1 )
by Joni
03:51
created
lib/JWX/JWE/CompressionAlgorithm.php 1 patch
Indentation   +16 added lines, -16 removed lines patch added patch discarded remove patch
@@ -12,21 +12,21 @@
 block discarded – undo
12 12
  */
13 13
 interface CompressionAlgorithm extends CompressionAlgorithmParameterValue, HeaderParameters
14 14
 {
15
-    /**
16
-     * Compress data.
17
-     *
18
-     * @param string $data Uncompressed data
19
-     *
20
-     * @return string Compressed data
21
-     */
22
-    public function compress(string $data): string;
15
+	/**
16
+	 * Compress data.
17
+	 *
18
+	 * @param string $data Uncompressed data
19
+	 *
20
+	 * @return string Compressed data
21
+	 */
22
+	public function compress(string $data): string;
23 23
 
24
-    /**
25
-     * Decompress data.
26
-     *
27
-     * @param string $data Compressed data
28
-     *
29
-     * @return string Uncompressed data
30
-     */
31
-    public function decompress(string $data): string;
24
+	/**
25
+	 * Decompress data.
26
+	 *
27
+	 * @param string $data Compressed data
28
+	 *
29
+	 * @return string Uncompressed data
30
+	 */
31
+	public function decompress(string $data): string;
32 32
 }
Please login to merge, or discard this patch.
lib/JWX/JWT/Claims.php 1 patch
Indentation   +163 added lines, -163 removed lines patch added patch discarded remove patch
@@ -14,167 +14,167 @@
 block discarded – undo
14 14
  */
15 15
 class Claims implements \Countable, \IteratorAggregate
16 16
 {
17
-    use TypedClaims;
18
-
19
-    /**
20
-     * Claims.
21
-     *
22
-     * @var Claim[]
23
-     */
24
-    protected $_claims;
25
-
26
-    /**
27
-     * Constructor.
28
-     *
29
-     * @param Claim ...$claims Zero or more claims
30
-     */
31
-    public function __construct(Claim ...$claims)
32
-    {
33
-        $this->_claims = [];
34
-        foreach ($claims as $claim) {
35
-            $this->_claims[$claim->name()] = $claim;
36
-        }
37
-    }
38
-
39
-    /**
40
-     * Convert to string.
41
-     *
42
-     * @return string
43
-     */
44
-    public function __toString(): string
45
-    {
46
-        return $this->toJSON();
47
-    }
48
-
49
-    /**
50
-     * Initialize from a JSON string.
51
-     *
52
-     * @param string $json JSON
53
-     *
54
-     * @throws \UnexpectedValueException If JSON is malformed
55
-     *
56
-     * @return self
57
-     */
58
-    public static function fromJSON(string $json): self
59
-    {
60
-        $claims = [];
61
-        $fields = json_decode($json, true, 32, JSON_BIGINT_AS_STRING);
62
-        if (!is_array($fields)) {
63
-            throw new \UnexpectedValueException('Invalid JSON.');
64
-        }
65
-        foreach ($fields as $name => $value) {
66
-            $claims[] = Claim::fromNameAndValue($name, $value);
67
-        }
68
-        return new self(...$claims);
69
-    }
70
-
71
-    /**
72
-     * Get self with Claim objects added.
73
-     *
74
-     * @param Claim ...$claims One or more Claim objects
75
-     *
76
-     * @return self
77
-     */
78
-    public function withClaims(Claim ...$claims): self
79
-    {
80
-        $obj = clone $this;
81
-        foreach ($claims as $claim) {
82
-            $obj->_claims[$claim->name()] = $claim;
83
-        }
84
-        return $obj;
85
-    }
86
-
87
-    /**
88
-     * Get all claims.
89
-     *
90
-     * @return Claim[]
91
-     */
92
-    public function all(): array
93
-    {
94
-        return $this->_claims;
95
-    }
96
-
97
-    /**
98
-     * Check whether claim is present.
99
-     *
100
-     * @param string $name Claim name
101
-     *
102
-     * @return bool
103
-     */
104
-    public function has(string $name): bool
105
-    {
106
-        return isset($this->_claims[$name]);
107
-    }
108
-
109
-    /**
110
-     * Get claim by name.
111
-     *
112
-     * @param string $name Claim name
113
-     *
114
-     * @throws \LogicException If claim is not present
115
-     *
116
-     * @return Claim
117
-     */
118
-    public function get(string $name): Claim
119
-    {
120
-        if (!isset($this->_claims[$name])) {
121
-            throw new \LogicException("Claim {$name} not set.");
122
-        }
123
-        return $this->_claims[$name];
124
-    }
125
-
126
-    /**
127
-     * Convert to a JSON.
128
-     *
129
-     * @return string
130
-     */
131
-    public function toJSON(): string
132
-    {
133
-        $data = [];
134
-        foreach ($this->_claims as $claim) {
135
-            $data[$claim->name()] = $claim->value();
136
-        }
137
-        return json_encode((object) $data, JSON_UNESCAPED_SLASHES);
138
-    }
139
-
140
-    /**
141
-     * Check whether a claims set is valid in the given context.
142
-     *
143
-     * @param ValidationContext $ctx Validation context
144
-     *
145
-     * @return bool
146
-     */
147
-    public function isValid(ValidationContext $ctx): bool
148
-    {
149
-        try {
150
-            $ctx->validate($this);
151
-        } catch (\RuntimeException $e) {
152
-            return false;
153
-        }
154
-        return true;
155
-    }
156
-
157
-    /**
158
-     * Get the number of claims.
159
-     *
160
-     * @see \Countable::count()
161
-     *
162
-     * @return int
163
-     */
164
-    public function count(): int
165
-    {
166
-        return count($this->_claims);
167
-    }
168
-
169
-    /**
170
-     * Get iterator for Claim objects keyed by claim name.
171
-     *
172
-     * @see \IteratorAggregate::getIterator()
173
-     *
174
-     * @return \ArrayIterator
175
-     */
176
-    public function getIterator(): \ArrayIterator
177
-    {
178
-        return new \ArrayIterator($this->_claims);
179
-    }
17
+	use TypedClaims;
18
+
19
+	/**
20
+	 * Claims.
21
+	 *
22
+	 * @var Claim[]
23
+	 */
24
+	protected $_claims;
25
+
26
+	/**
27
+	 * Constructor.
28
+	 *
29
+	 * @param Claim ...$claims Zero or more claims
30
+	 */
31
+	public function __construct(Claim ...$claims)
32
+	{
33
+		$this->_claims = [];
34
+		foreach ($claims as $claim) {
35
+			$this->_claims[$claim->name()] = $claim;
36
+		}
37
+	}
38
+
39
+	/**
40
+	 * Convert to string.
41
+	 *
42
+	 * @return string
43
+	 */
44
+	public function __toString(): string
45
+	{
46
+		return $this->toJSON();
47
+	}
48
+
49
+	/**
50
+	 * Initialize from a JSON string.
51
+	 *
52
+	 * @param string $json JSON
53
+	 *
54
+	 * @throws \UnexpectedValueException If JSON is malformed
55
+	 *
56
+	 * @return self
57
+	 */
58
+	public static function fromJSON(string $json): self
59
+	{
60
+		$claims = [];
61
+		$fields = json_decode($json, true, 32, JSON_BIGINT_AS_STRING);
62
+		if (!is_array($fields)) {
63
+			throw new \UnexpectedValueException('Invalid JSON.');
64
+		}
65
+		foreach ($fields as $name => $value) {
66
+			$claims[] = Claim::fromNameAndValue($name, $value);
67
+		}
68
+		return new self(...$claims);
69
+	}
70
+
71
+	/**
72
+	 * Get self with Claim objects added.
73
+	 *
74
+	 * @param Claim ...$claims One or more Claim objects
75
+	 *
76
+	 * @return self
77
+	 */
78
+	public function withClaims(Claim ...$claims): self
79
+	{
80
+		$obj = clone $this;
81
+		foreach ($claims as $claim) {
82
+			$obj->_claims[$claim->name()] = $claim;
83
+		}
84
+		return $obj;
85
+	}
86
+
87
+	/**
88
+	 * Get all claims.
89
+	 *
90
+	 * @return Claim[]
91
+	 */
92
+	public function all(): array
93
+	{
94
+		return $this->_claims;
95
+	}
96
+
97
+	/**
98
+	 * Check whether claim is present.
99
+	 *
100
+	 * @param string $name Claim name
101
+	 *
102
+	 * @return bool
103
+	 */
104
+	public function has(string $name): bool
105
+	{
106
+		return isset($this->_claims[$name]);
107
+	}
108
+
109
+	/**
110
+	 * Get claim by name.
111
+	 *
112
+	 * @param string $name Claim name
113
+	 *
114
+	 * @throws \LogicException If claim is not present
115
+	 *
116
+	 * @return Claim
117
+	 */
118
+	public function get(string $name): Claim
119
+	{
120
+		if (!isset($this->_claims[$name])) {
121
+			throw new \LogicException("Claim {$name} not set.");
122
+		}
123
+		return $this->_claims[$name];
124
+	}
125
+
126
+	/**
127
+	 * Convert to a JSON.
128
+	 *
129
+	 * @return string
130
+	 */
131
+	public function toJSON(): string
132
+	{
133
+		$data = [];
134
+		foreach ($this->_claims as $claim) {
135
+			$data[$claim->name()] = $claim->value();
136
+		}
137
+		return json_encode((object) $data, JSON_UNESCAPED_SLASHES);
138
+	}
139
+
140
+	/**
141
+	 * Check whether a claims set is valid in the given context.
142
+	 *
143
+	 * @param ValidationContext $ctx Validation context
144
+	 *
145
+	 * @return bool
146
+	 */
147
+	public function isValid(ValidationContext $ctx): bool
148
+	{
149
+		try {
150
+			$ctx->validate($this);
151
+		} catch (\RuntimeException $e) {
152
+			return false;
153
+		}
154
+		return true;
155
+	}
156
+
157
+	/**
158
+	 * Get the number of claims.
159
+	 *
160
+	 * @see \Countable::count()
161
+	 *
162
+	 * @return int
163
+	 */
164
+	public function count(): int
165
+	{
166
+		return count($this->_claims);
167
+	}
168
+
169
+	/**
170
+	 * Get iterator for Claim objects keyed by claim name.
171
+	 *
172
+	 * @see \IteratorAggregate::getIterator()
173
+	 *
174
+	 * @return \ArrayIterator
175
+	 */
176
+	public function getIterator(): \ArrayIterator
177
+	{
178
+		return new \ArrayIterator($this->_claims);
179
+	}
180 180
 }
Please login to merge, or discard this patch.
lib/JWX/Parameter/Feature/Base64UIntValue.php 1 patch
Indentation   +22 added lines, -22 removed lines patch added patch discarded remove patch
@@ -15,28 +15,28 @@
 block discarded – undo
15 15
  */
16 16
 trait Base64UIntValue
17 17
 {
18
-    use Base64URLValue;
18
+	use Base64URLValue;
19 19
 
20
-    /**
21
-     * Initialize parameter from base10 number.
22
-     *
23
-     * @param int|string $number
24
-     *
25
-     * @return self
26
-     */
27
-    public static function fromNumber($number): Parameter
28
-    {
29
-        $data = BigInt::fromBase10($number)->base256();
30
-        return static::fromString($data);
31
-    }
20
+	/**
21
+	 * Initialize parameter from base10 number.
22
+	 *
23
+	 * @param int|string $number
24
+	 *
25
+	 * @return self
26
+	 */
27
+	public static function fromNumber($number): Parameter
28
+	{
29
+		$data = BigInt::fromBase10($number)->base256();
30
+		return static::fromString($data);
31
+	}
32 32
 
33
-    /**
34
-     * Get value as a number.
35
-     *
36
-     * @return BigInt
37
-     */
38
-    public function number(): BigInt
39
-    {
40
-        return BigInt::fromBase256(Base64::urlDecode($this->value()));
41
-    }
33
+	/**
34
+	 * Get value as a number.
35
+	 *
36
+	 * @return BigInt
37
+	 */
38
+	public function number(): BigInt
39
+	{
40
+		return BigInt::fromBase256(Base64::urlDecode($this->value()));
41
+	}
42 42
 }
Please login to merge, or discard this patch.
lib/JWX/JWT/Claim/Feature/NumericDateClaim.php 1 patch
Indentation   +89 added lines, -89 removed lines patch added patch discarded remove patch
@@ -9,99 +9,99 @@
 block discarded – undo
9 9
  */
10 10
 trait NumericDateClaim
11 11
 {
12
-    /**
13
-     * Constructor.
14
-     *
15
-     * @param int $timestamp Unix timestamp
16
-     */
17
-    abstract public function __construct(int $timestamp);
12
+	/**
13
+	 * Constructor.
14
+	 *
15
+	 * @param int $timestamp Unix timestamp
16
+	 */
17
+	abstract public function __construct(int $timestamp);
18 18
 
19
-    /**
20
-     * Get the parameter value.
21
-     *
22
-     * @return string
23
-     */
24
-    abstract public function value();
19
+	/**
20
+	 * Get the parameter value.
21
+	 *
22
+	 * @return string
23
+	 */
24
+	abstract public function value();
25 25
 
26
-    /**
27
-     * Initialize instance from date/time string.
28
-     *
29
-     * @param string $time `strtotime` compatible time string
30
-     * @param string $tz   Default timezone
31
-     *
32
-     * @throws \RuntimeException
33
-     *
34
-     * @return static
35
-     */
36
-    public static function fromString(string $time, string $tz = 'UTC')
37
-    {
38
-        try {
39
-            $dt = new \DateTimeImmutable($time, self::_createTimeZone($tz));
40
-            return new static($dt->getTimestamp());
41
-        } catch (\Exception $e) {
42
-            throw new \RuntimeException(
43
-                'Failed to create DateTime: ' .
44
-                     self::_getLastDateTimeImmutableErrorsStr(), 0, $e);
45
-        }
46
-    }
26
+	/**
27
+	 * Initialize instance from date/time string.
28
+	 *
29
+	 * @param string $time `strtotime` compatible time string
30
+	 * @param string $tz   Default timezone
31
+	 *
32
+	 * @throws \RuntimeException
33
+	 *
34
+	 * @return static
35
+	 */
36
+	public static function fromString(string $time, string $tz = 'UTC')
37
+	{
38
+		try {
39
+			$dt = new \DateTimeImmutable($time, self::_createTimeZone($tz));
40
+			return new static($dt->getTimestamp());
41
+		} catch (\Exception $e) {
42
+			throw new \RuntimeException(
43
+				'Failed to create DateTime: ' .
44
+					 self::_getLastDateTimeImmutableErrorsStr(), 0, $e);
45
+		}
46
+	}
47 47
 
48
-    /**
49
-     * Get date as a unix timestamp.
50
-     *
51
-     * @return int
52
-     */
53
-    public function timestamp(): int
54
-    {
55
-        return (int) $this->value();
56
-    }
48
+	/**
49
+	 * Get date as a unix timestamp.
50
+	 *
51
+	 * @return int
52
+	 */
53
+	public function timestamp(): int
54
+	{
55
+		return (int) $this->value();
56
+	}
57 57
 
58
-    /**
59
-     * Get date as a datetime object.
60
-     *
61
-     * @param string $tz Timezone
62
-     *
63
-     * @throws \RuntimeException
64
-     *
65
-     * @return \DateTimeImmutable
66
-     */
67
-    public function dateTime(string $tz = 'UTC'): \DateTimeImmutable
68
-    {
69
-        $dt = \DateTimeImmutable::createFromFormat('!U', strval($this->value()),
70
-            self::_createTimeZone($tz));
71
-        if (false === $dt) {
72
-            throw new \RuntimeException(
73
-                'Failed to create DateTime: ' .
74
-                     self::_getLastDateTimeImmutableErrorsStr());
75
-        }
76
-        return $dt;
77
-    }
58
+	/**
59
+	 * Get date as a datetime object.
60
+	 *
61
+	 * @param string $tz Timezone
62
+	 *
63
+	 * @throws \RuntimeException
64
+	 *
65
+	 * @return \DateTimeImmutable
66
+	 */
67
+	public function dateTime(string $tz = 'UTC'): \DateTimeImmutable
68
+	{
69
+		$dt = \DateTimeImmutable::createFromFormat('!U', strval($this->value()),
70
+			self::_createTimeZone($tz));
71
+		if (false === $dt) {
72
+			throw new \RuntimeException(
73
+				'Failed to create DateTime: ' .
74
+					 self::_getLastDateTimeImmutableErrorsStr());
75
+		}
76
+		return $dt;
77
+	}
78 78
 
79
-    /**
80
-     * Create DateTimeZone object from string.
81
-     *
82
-     * @param string $tz
83
-     *
84
-     * @throws \UnexpectedValueException
85
-     *
86
-     * @return \DateTimeZone
87
-     */
88
-    private static function _createTimeZone(string $tz): \DateTimeZone
89
-    {
90
-        try {
91
-            return new \DateTimeZone($tz);
92
-        } catch (\Exception $e) {
93
-            throw new \UnexpectedValueException('Invalid timezone.', 0, $e);
94
-        }
95
-    }
79
+	/**
80
+	 * Create DateTimeZone object from string.
81
+	 *
82
+	 * @param string $tz
83
+	 *
84
+	 * @throws \UnexpectedValueException
85
+	 *
86
+	 * @return \DateTimeZone
87
+	 */
88
+	private static function _createTimeZone(string $tz): \DateTimeZone
89
+	{
90
+		try {
91
+			return new \DateTimeZone($tz);
92
+		} catch (\Exception $e) {
93
+			throw new \UnexpectedValueException('Invalid timezone.', 0, $e);
94
+		}
95
+	}
96 96
 
97
-    /**
98
-     * Get last error caused by DateTimeImmutable.
99
-     *
100
-     * @return string
101
-     */
102
-    private static function _getLastDateTimeImmutableErrorsStr(): string
103
-    {
104
-        $errors = \DateTimeImmutable::getLastErrors()['errors'];
105
-        return implode(', ', $errors);
106
-    }
97
+	/**
98
+	 * Get last error caused by DateTimeImmutable.
99
+	 *
100
+	 * @return string
101
+	 */
102
+	private static function _getLastDateTimeImmutableErrorsStr(): string
103
+	{
104
+		$errors = \DateTimeImmutable::getLastErrors()['errors'];
105
+		return implode(', ', $errors);
106
+	}
107 107
 }
Please login to merge, or discard this patch.
lib/JWX/JWE/KeyAlgorithm/RSAESKeyAlgorithm.php 1 patch
Indentation   +198 added lines, -198 removed lines patch added patch discarded remove patch
@@ -21,202 +21,202 @@
 block discarded – undo
21 21
  */
22 22
 abstract class RSAESKeyAlgorithm extends KeyManagementAlgorithm
23 23
 {
24
-    use RandomCEK;
25
-
26
-    /**
27
-     * Mapping from algorithm name to class name.
28
-     *
29
-     * @internal
30
-     *
31
-     * @var array
32
-     */
33
-    const MAP_ALGO_TO_CLASS = [
34
-        JWA::ALGO_RSA1_5 => RSAESPKCS1Algorithm::class,
35
-        JWA::ALGO_RSA_OAEP => RSAESOAEPAlgorithm::class,
36
-    ];
37
-
38
-    /**
39
-     * Public key.
40
-     *
41
-     * @var RSAPublicKeyJWK
42
-     */
43
-    protected $_publicKey;
44
-
45
-    /**
46
-     * Private key.
47
-     *
48
-     * @var null|RSAPrivateKeyJWK
49
-     */
50
-    protected $_privateKey;
51
-
52
-    /**
53
-     * Constructor.
54
-     *
55
-     * Use `fromPublicKey` or `fromPrivateKey` instead!
56
-     *
57
-     * @param RSAPublicKeyJWK  $pub_key  RSA public key
58
-     * @param RSAPrivateKeyJWK $priv_key Optional RSA private key
59
-     */
60
-    protected function __construct(RSAPublicKeyJWK $pub_key,
61
-        ?RSAPrivateKeyJWK $priv_key = null)
62
-    {
63
-        $this->_publicKey = $pub_key;
64
-        $this->_privateKey = $priv_key;
65
-    }
66
-
67
-    /**
68
-     * Initialize from JWK.
69
-     *
70
-     * @param JWK    $jwk
71
-     * @param Header $header
72
-     *
73
-     * @throws \UnexpectedValueException
74
-     *
75
-     * @return self
76
-     */
77
-    public static function fromJWK(JWK $jwk, Header $header): KeyManagementAlgorithm
78
-    {
79
-        $alg = JWA::deriveAlgorithmName($header, $jwk);
80
-        if (!array_key_exists($alg, self::MAP_ALGO_TO_CLASS)) {
81
-            throw new \UnexpectedValueException("Unsupported algorithm '{$alg}'.");
82
-        }
83
-        $cls = self::MAP_ALGO_TO_CLASS[$alg];
84
-        if ($jwk->has(...RSAPrivateKeyJWK::MANAGED_PARAMS)) {
85
-            return $cls::fromPrivateKey(RSAPrivateKeyJWK::fromJWK($jwk));
86
-        }
87
-        return $cls::fromPublicKey(RSAPublicKeyJWK::fromJWK($jwk));
88
-    }
89
-
90
-    /**
91
-     * Initialize from a public key.
92
-     *
93
-     * @param RSAPublicKeyJWK $jwk
94
-     *
95
-     * @return self
96
-     */
97
-    public static function fromPublicKey(RSAPublicKeyJWK $jwk): self
98
-    {
99
-        return new static($jwk);
100
-    }
101
-
102
-    /**
103
-     * Initialize from a private key.
104
-     *
105
-     * @param RSAPrivateKeyJWK $jwk
106
-     *
107
-     * @return self
108
-     */
109
-    public static function fromPrivateKey(RSAPrivateKeyJWK $jwk): self
110
-    {
111
-        return new static($jwk->publicKey(), $jwk);
112
-    }
113
-
114
-    /**
115
-     * Get the public key.
116
-     *
117
-     * @return RSAPublicKeyJWK
118
-     */
119
-    public function publicKey(): RSAPublicKeyJWK
120
-    {
121
-        return $this->_publicKey;
122
-    }
123
-
124
-    /**
125
-     * Check whether the private key is present.
126
-     *
127
-     * @return bool
128
-     */
129
-    public function hasPrivateKey(): bool
130
-    {
131
-        return isset($this->_privateKey);
132
-    }
133
-
134
-    /**
135
-     * Get the private key.
136
-     *
137
-     * @throws \LogicException
138
-     *
139
-     * @return RSAPrivateKeyJWK
140
-     */
141
-    public function privateKey(): RSAPrivateKeyJWK
142
-    {
143
-        if (!$this->hasPrivateKey()) {
144
-            throw new \LogicException('Private key not set.');
145
-        }
146
-        return $this->_privateKey;
147
-    }
148
-
149
-    /**
150
-     * {@inheritdoc}
151
-     */
152
-    public function headerParameters(): array
153
-    {
154
-        return array_merge(parent::headerParameters(),
155
-            [AlgorithmParameter::fromAlgorithm($this)]);
156
-    }
157
-
158
-    /**
159
-     * Get the padding scheme.
160
-     *
161
-     * @return int
162
-     */
163
-    abstract protected function _paddingScheme(): int;
164
-
165
-    /**
166
-     * {@inheritdoc}
167
-     */
168
-    protected function _encryptKey(string $key, Header &$header): string
169
-    {
170
-        $pubkey = openssl_pkey_get_public(
171
-            $this->publicKey()->toPEM()->string());
172
-        if (false === $pubkey) {
173
-            throw new \RuntimeException(
174
-                'openssl_pkey_get_public() failed: ' .
175
-                     $this->_getLastOpenSSLError());
176
-        }
177
-        $result = openssl_public_encrypt($key, $crypted, $pubkey,
178
-            $this->_paddingScheme());
179
-        if (!$result) {
180
-            throw new \RuntimeException(
181
-                'openssl_public_encrypt() failed: ' .
182
-                     $this->_getLastOpenSSLError());
183
-        }
184
-        return $crypted;
185
-    }
186
-
187
-    /**
188
-     * {@inheritdoc}
189
-     */
190
-    protected function _decryptKey(string $ciphertext, Header $header): string
191
-    {
192
-        $privkey = openssl_pkey_get_private(
193
-            $this->privateKey()->toPEM()->string());
194
-        if (false === $privkey) {
195
-            throw new \RuntimeException(
196
-                'openssl_pkey_get_private() failed: ' .
197
-                     $this->_getLastOpenSSLError());
198
-        }
199
-        $result = openssl_private_decrypt($ciphertext, $cek, $privkey,
200
-            $this->_paddingScheme());
201
-        if (!$result) {
202
-            throw new \RuntimeException(
203
-                'openssl_private_decrypt() failed: ' .
204
-                     $this->_getLastOpenSSLError());
205
-        }
206
-        return $cek;
207
-    }
208
-
209
-    /**
210
-     * Get last OpenSSL error message.
211
-     *
212
-     * @return null|string
213
-     */
214
-    protected function _getLastOpenSSLError(): ?string
215
-    {
216
-        $msg = null;
217
-        while (false !== ($err = openssl_error_string())) {
218
-            $msg = $err;
219
-        }
220
-        return $msg;
221
-    }
24
+	use RandomCEK;
25
+
26
+	/**
27
+	 * Mapping from algorithm name to class name.
28
+	 *
29
+	 * @internal
30
+	 *
31
+	 * @var array
32
+	 */
33
+	const MAP_ALGO_TO_CLASS = [
34
+		JWA::ALGO_RSA1_5 => RSAESPKCS1Algorithm::class,
35
+		JWA::ALGO_RSA_OAEP => RSAESOAEPAlgorithm::class,
36
+	];
37
+
38
+	/**
39
+	 * Public key.
40
+	 *
41
+	 * @var RSAPublicKeyJWK
42
+	 */
43
+	protected $_publicKey;
44
+
45
+	/**
46
+	 * Private key.
47
+	 *
48
+	 * @var null|RSAPrivateKeyJWK
49
+	 */
50
+	protected $_privateKey;
51
+
52
+	/**
53
+	 * Constructor.
54
+	 *
55
+	 * Use `fromPublicKey` or `fromPrivateKey` instead!
56
+	 *
57
+	 * @param RSAPublicKeyJWK  $pub_key  RSA public key
58
+	 * @param RSAPrivateKeyJWK $priv_key Optional RSA private key
59
+	 */
60
+	protected function __construct(RSAPublicKeyJWK $pub_key,
61
+		?RSAPrivateKeyJWK $priv_key = null)
62
+	{
63
+		$this->_publicKey = $pub_key;
64
+		$this->_privateKey = $priv_key;
65
+	}
66
+
67
+	/**
68
+	 * Initialize from JWK.
69
+	 *
70
+	 * @param JWK    $jwk
71
+	 * @param Header $header
72
+	 *
73
+	 * @throws \UnexpectedValueException
74
+	 *
75
+	 * @return self
76
+	 */
77
+	public static function fromJWK(JWK $jwk, Header $header): KeyManagementAlgorithm
78
+	{
79
+		$alg = JWA::deriveAlgorithmName($header, $jwk);
80
+		if (!array_key_exists($alg, self::MAP_ALGO_TO_CLASS)) {
81
+			throw new \UnexpectedValueException("Unsupported algorithm '{$alg}'.");
82
+		}
83
+		$cls = self::MAP_ALGO_TO_CLASS[$alg];
84
+		if ($jwk->has(...RSAPrivateKeyJWK::MANAGED_PARAMS)) {
85
+			return $cls::fromPrivateKey(RSAPrivateKeyJWK::fromJWK($jwk));
86
+		}
87
+		return $cls::fromPublicKey(RSAPublicKeyJWK::fromJWK($jwk));
88
+	}
89
+
90
+	/**
91
+	 * Initialize from a public key.
92
+	 *
93
+	 * @param RSAPublicKeyJWK $jwk
94
+	 *
95
+	 * @return self
96
+	 */
97
+	public static function fromPublicKey(RSAPublicKeyJWK $jwk): self
98
+	{
99
+		return new static($jwk);
100
+	}
101
+
102
+	/**
103
+	 * Initialize from a private key.
104
+	 *
105
+	 * @param RSAPrivateKeyJWK $jwk
106
+	 *
107
+	 * @return self
108
+	 */
109
+	public static function fromPrivateKey(RSAPrivateKeyJWK $jwk): self
110
+	{
111
+		return new static($jwk->publicKey(), $jwk);
112
+	}
113
+
114
+	/**
115
+	 * Get the public key.
116
+	 *
117
+	 * @return RSAPublicKeyJWK
118
+	 */
119
+	public function publicKey(): RSAPublicKeyJWK
120
+	{
121
+		return $this->_publicKey;
122
+	}
123
+
124
+	/**
125
+	 * Check whether the private key is present.
126
+	 *
127
+	 * @return bool
128
+	 */
129
+	public function hasPrivateKey(): bool
130
+	{
131
+		return isset($this->_privateKey);
132
+	}
133
+
134
+	/**
135
+	 * Get the private key.
136
+	 *
137
+	 * @throws \LogicException
138
+	 *
139
+	 * @return RSAPrivateKeyJWK
140
+	 */
141
+	public function privateKey(): RSAPrivateKeyJWK
142
+	{
143
+		if (!$this->hasPrivateKey()) {
144
+			throw new \LogicException('Private key not set.');
145
+		}
146
+		return $this->_privateKey;
147
+	}
148
+
149
+	/**
150
+	 * {@inheritdoc}
151
+	 */
152
+	public function headerParameters(): array
153
+	{
154
+		return array_merge(parent::headerParameters(),
155
+			[AlgorithmParameter::fromAlgorithm($this)]);
156
+	}
157
+
158
+	/**
159
+	 * Get the padding scheme.
160
+	 *
161
+	 * @return int
162
+	 */
163
+	abstract protected function _paddingScheme(): int;
164
+
165
+	/**
166
+	 * {@inheritdoc}
167
+	 */
168
+	protected function _encryptKey(string $key, Header &$header): string
169
+	{
170
+		$pubkey = openssl_pkey_get_public(
171
+			$this->publicKey()->toPEM()->string());
172
+		if (false === $pubkey) {
173
+			throw new \RuntimeException(
174
+				'openssl_pkey_get_public() failed: ' .
175
+					 $this->_getLastOpenSSLError());
176
+		}
177
+		$result = openssl_public_encrypt($key, $crypted, $pubkey,
178
+			$this->_paddingScheme());
179
+		if (!$result) {
180
+			throw new \RuntimeException(
181
+				'openssl_public_encrypt() failed: ' .
182
+					 $this->_getLastOpenSSLError());
183
+		}
184
+		return $crypted;
185
+	}
186
+
187
+	/**
188
+	 * {@inheritdoc}
189
+	 */
190
+	protected function _decryptKey(string $ciphertext, Header $header): string
191
+	{
192
+		$privkey = openssl_pkey_get_private(
193
+			$this->privateKey()->toPEM()->string());
194
+		if (false === $privkey) {
195
+			throw new \RuntimeException(
196
+				'openssl_pkey_get_private() failed: ' .
197
+					 $this->_getLastOpenSSLError());
198
+		}
199
+		$result = openssl_private_decrypt($ciphertext, $cek, $privkey,
200
+			$this->_paddingScheme());
201
+		if (!$result) {
202
+			throw new \RuntimeException(
203
+				'openssl_private_decrypt() failed: ' .
204
+					 $this->_getLastOpenSSLError());
205
+		}
206
+		return $cek;
207
+	}
208
+
209
+	/**
210
+	 * Get last OpenSSL error message.
211
+	 *
212
+	 * @return null|string
213
+	 */
214
+	protected function _getLastOpenSSLError(): ?string
215
+	{
216
+		$msg = null;
217
+		while (false !== ($err = openssl_error_string())) {
218
+			$msg = $err;
219
+		}
220
+		return $msg;
221
+	}
222 222
 }
Please login to merge, or discard this patch.
lib/JWX/JWE/EncryptionAlgorithm/A128GCMAlgorithm.php 1 patch
Indentation   +14 added lines, -14 removed lines patch added patch discarded remove patch
@@ -13,19 +13,19 @@
 block discarded – undo
13 13
  */
14 14
 class A128GCMAlgorithm extends AESGCMAlgorithm
15 15
 {
16
-    /**
17
-     * {@inheritdoc}
18
-     */
19
-    public function encryptionAlgorithmParamValue(): string
20
-    {
21
-        return JWA::ALGO_A128GCM;
22
-    }
16
+	/**
17
+	 * {@inheritdoc}
18
+	 */
19
+	public function encryptionAlgorithmParamValue(): string
20
+	{
21
+		return JWA::ALGO_A128GCM;
22
+	}
23 23
 
24
-    /**
25
-     * {@inheritdoc}
26
-     */
27
-    public function keySize(): int
28
-    {
29
-        return 16;
30
-    }
24
+	/**
25
+	 * {@inheritdoc}
26
+	 */
27
+	public function keySize(): int
28
+	{
29
+		return 16;
30
+	}
31 31
 }
Please login to merge, or discard this patch.
lib/JWX/JWE/EncryptionAlgorithm/A192GCMAlgorithm.php 1 patch
Indentation   +14 added lines, -14 removed lines patch added patch discarded remove patch
@@ -13,19 +13,19 @@
 block discarded – undo
13 13
  */
14 14
 class A192GCMAlgorithm extends AESGCMAlgorithm
15 15
 {
16
-    /**
17
-     * {@inheritdoc}
18
-     */
19
-    public function encryptionAlgorithmParamValue(): string
20
-    {
21
-        return JWA::ALGO_A192GCM;
22
-    }
16
+	/**
17
+	 * {@inheritdoc}
18
+	 */
19
+	public function encryptionAlgorithmParamValue(): string
20
+	{
21
+		return JWA::ALGO_A192GCM;
22
+	}
23 23
 
24
-    /**
25
-     * {@inheritdoc}
26
-     */
27
-    public function keySize(): int
28
-    {
29
-        return 24;
30
-    }
24
+	/**
25
+	 * {@inheritdoc}
26
+	 */
27
+	public function keySize(): int
28
+	{
29
+		return 24;
30
+	}
31 31
 }
Please login to merge, or discard this patch.
lib/JWX/JWE/EncryptionAlgorithm/A256GCMAlgorithm.php 1 patch
Indentation   +14 added lines, -14 removed lines patch added patch discarded remove patch
@@ -13,19 +13,19 @@
 block discarded – undo
13 13
  */
14 14
 class A256GCMAlgorithm extends AESGCMAlgorithm
15 15
 {
16
-    /**
17
-     * {@inheritdoc}
18
-     */
19
-    public function encryptionAlgorithmParamValue(): string
20
-    {
21
-        return JWA::ALGO_A256GCM;
22
-    }
16
+	/**
17
+	 * {@inheritdoc}
18
+	 */
19
+	public function encryptionAlgorithmParamValue(): string
20
+	{
21
+		return JWA::ALGO_A256GCM;
22
+	}
23 23
 
24
-    /**
25
-     * {@inheritdoc}
26
-     */
27
-    public function keySize(): int
28
-    {
29
-        return 32;
30
-    }
24
+	/**
25
+	 * {@inheritdoc}
26
+	 */
27
+	public function keySize(): int
28
+	{
29
+		return 32;
30
+	}
31 31
 }
Please login to merge, or discard this patch.
lib/JWX/JWE/EncryptionAlgorithm/AESGCMAlgorithm.php 1 patch
Indentation   +65 added lines, -65 removed lines patch added patch discarded remove patch
@@ -17,74 +17,74 @@
 block discarded – undo
17 17
  */
18 18
 abstract class AESGCMAlgorithm implements ContentEncryptionAlgorithm
19 19
 {
20
-    /**
21
-     * {@inheritdoc}
22
-     */
23
-    public function encrypt(string $plaintext, string $key, string $iv,
24
-        string $aad): array
25
-    {
26
-        $this->_validateKey($key);
27
-        $this->_validateIV($iv);
28
-        return AESGCM::encrypt($plaintext, $aad, $key, $iv, 16);
29
-    }
20
+	/**
21
+	 * {@inheritdoc}
22
+	 */
23
+	public function encrypt(string $plaintext, string $key, string $iv,
24
+		string $aad): array
25
+	{
26
+		$this->_validateKey($key);
27
+		$this->_validateIV($iv);
28
+		return AESGCM::encrypt($plaintext, $aad, $key, $iv, 16);
29
+	}
30 30
 
31
-    /**
32
-     * {@inheritdoc}
33
-     */
34
-    public function decrypt(string $ciphertext, string $key, string $iv,
35
-        string $aad, string $auth_tag): string
36
-    {
37
-        $this->_validateKey($key);
38
-        $this->_validateIV($iv);
39
-        try {
40
-            $plaintext = AESGCM::decrypt($ciphertext, $auth_tag, $aad, $key, $iv);
41
-        } catch (GCMAuthException $e) {
42
-            throw new AuthenticationException('Message authentication failed.');
43
-        }
44
-        return $plaintext;
45
-    }
31
+	/**
32
+	 * {@inheritdoc}
33
+	 */
34
+	public function decrypt(string $ciphertext, string $key, string $iv,
35
+		string $aad, string $auth_tag): string
36
+	{
37
+		$this->_validateKey($key);
38
+		$this->_validateIV($iv);
39
+		try {
40
+			$plaintext = AESGCM::decrypt($ciphertext, $auth_tag, $aad, $key, $iv);
41
+		} catch (GCMAuthException $e) {
42
+			throw new AuthenticationException('Message authentication failed.');
43
+		}
44
+		return $plaintext;
45
+	}
46 46
 
47
-    /**
48
-     * {@inheritdoc}
49
-     */
50
-    public function ivSize(): int
51
-    {
52
-        return 12;
53
-    }
47
+	/**
48
+	 * {@inheritdoc}
49
+	 */
50
+	public function ivSize(): int
51
+	{
52
+		return 12;
53
+	}
54 54
 
55
-    /**
56
-     * {@inheritdoc}
57
-     */
58
-    public function headerParameters(): array
59
-    {
60
-        return [EncryptionAlgorithmParameter::fromAlgorithm($this)];
61
-    }
55
+	/**
56
+	 * {@inheritdoc}
57
+	 */
58
+	public function headerParameters(): array
59
+	{
60
+		return [EncryptionAlgorithmParameter::fromAlgorithm($this)];
61
+	}
62 62
 
63
-    /**
64
-     * Check that key is valid.
65
-     *
66
-     * @param string $key
67
-     *
68
-     * @throws \RuntimeException
69
-     */
70
-    final protected function _validateKey(string $key): void
71
-    {
72
-        if (strlen($key) !== $this->keySize()) {
73
-            throw new \RuntimeException('Invalid key size.');
74
-        }
75
-    }
63
+	/**
64
+	 * Check that key is valid.
65
+	 *
66
+	 * @param string $key
67
+	 *
68
+	 * @throws \RuntimeException
69
+	 */
70
+	final protected function _validateKey(string $key): void
71
+	{
72
+		if (strlen($key) !== $this->keySize()) {
73
+			throw new \RuntimeException('Invalid key size.');
74
+		}
75
+	}
76 76
 
77
-    /**
78
-     * Check that IV is valid.
79
-     *
80
-     * @param string $iv
81
-     *
82
-     * @throws \RuntimeException
83
-     */
84
-    final protected function _validateIV(string $iv): void
85
-    {
86
-        if (strlen($iv) !== $this->ivSize()) {
87
-            throw new \RuntimeException('Invalid IV length.');
88
-        }
89
-    }
77
+	/**
78
+	 * Check that IV is valid.
79
+	 *
80
+	 * @param string $iv
81
+	 *
82
+	 * @throws \RuntimeException
83
+	 */
84
+	final protected function _validateIV(string $iv): void
85
+	{
86
+		if (strlen($iv) !== $this->ivSize()) {
87
+			throw new \RuntimeException('Invalid IV length.');
88
+		}
89
+	}
90 90
 }
Please login to merge, or discard this patch.