1919use function openssl_sign ;
2020use function openssl_verify ;
2121
22+ use const OPENSSL_KEYTYPE_DH ;
23+ use const OPENSSL_KEYTYPE_DSA ;
24+ use const OPENSSL_KEYTYPE_EC ;
25+ use const OPENSSL_KEYTYPE_RSA ;
2226use const PHP_EOL ;
2327
2428abstract class OpenSSL implements Signer
2529{
30+ protected const KEY_TYPE_MAP = [
31+ OPENSSL_KEYTYPE_RSA => 'RSA ' ,
32+ OPENSSL_KEYTYPE_DSA => 'DSA ' ,
33+ OPENSSL_KEYTYPE_DH => 'DH ' ,
34+ OPENSSL_KEYTYPE_EC => 'EC ' ,
35+ ];
36+
2637 /**
2738 * @throws CannotSignPayload
2839 * @throws InvalidKeyProvided
@@ -47,9 +58,6 @@ final protected function createSignature(
4758 }
4859 }
4960
50- /** @return positive-int */
51- abstract public function minimumBitsLengthForKey (): int ;
52-
5361 /**
5462 * @return resource|OpenSSLAsymmetricKey
5563 *
@@ -105,15 +113,12 @@ private function validateKey($key): void
105113 $ details = openssl_pkey_get_details ($ key );
106114 assert (is_array ($ details ));
107115
108- if (! array_key_exists ('key ' , $ details ) || $ details ['type ' ] !== $ this ->keyType ()) {
109- throw InvalidKeyProvided::incompatibleKey ();
110- }
111-
112116 assert (array_key_exists ('bits ' , $ details ));
113117 assert (is_int ($ details ['bits ' ]));
114- if ($ details ['bits ' ] < $ this ->minimumBitsLengthForKey ()) {
115- throw InvalidKeyProvided::tooShort ($ this ->minimumBitsLengthForKey (), $ details ['bits ' ]);
116- }
118+ assert (array_key_exists ('type ' , $ details ));
119+ assert (is_int ($ details ['type ' ]));
120+
121+ $ this ->guardAgainstIncompatibleKey ($ details ['type ' ], $ details ['bits ' ]);
117122 }
118123
119124 private function fullOpenSSLErrorString (): string
@@ -127,6 +132,9 @@ private function fullOpenSSLErrorString(): string
127132 return $ error ;
128133 }
129134
135+ /** @throws InvalidKeyProvided */
136+ abstract protected function guardAgainstIncompatibleKey (int $ type , int $ lengthInBits ): void ;
137+
130138 /** @param resource|OpenSSLAsymmetricKey $key */
131139 private function freeKey ($ key ): void
132140 {
@@ -137,13 +145,6 @@ private function freeKey($key): void
137145 openssl_free_key ($ key ); // Deprecated and no longer necessary as of PHP >= 8.0
138146 }
139147
140- /**
141- * Returns the type of key to be used to create/verify the signature (using OpenSSL constants)
142- *
143- * @internal
144- */
145- abstract public function keyType (): int ;
146-
147148 /**
148149 * Returns which algorithm to be used to create/verify the signature (using OpenSSL constants)
149150 *
0 commit comments