@@ -31,7 +31,7 @@ class JWT
3131 private const ASN1_SEQUENCE = 0x10 ;
3232 private const ASN1_BIT_STRING = 0x03 ;
3333
34- private const RSA_KEY_MIN_LENGTH = 2048 ;
34+ private const RSA_KEY_MIN_LENGTH = 2048 ;
3535
3636 /**
3737 * When checking nbf, iat or expiration times,
@@ -284,20 +284,8 @@ public static function sign(
284284 }
285285 return $ signature ;
286286 case 'sodium_crypto ' :
287- if (!\function_exists ('sodium_crypto_sign_detached ' )) {
288- throw new DomainException ('libsodium is not available ' );
289- }
290- if (!\is_string ($ key )) {
291- throw new InvalidArgumentException ('key must be a string when using EdDSA ' );
292- }
293287 try {
294- // The last non-empty line is used as the key.
295- $ lines = array_filter (explode ("\n" , $ key ));
296- $ key = base64_decode ((string ) end ($ lines ));
297- if (\strlen ($ key ) === 0 ) {
298- throw new DomainException ('Key cannot be empty string ' );
299- }
300- return sodium_crypto_sign_detached ($ msg , $ key );
288+ return sodium_crypto_sign_detached ($ msg , self ::validateEdDSAKey ($ key ));
301289 } catch (Exception $ e ) {
302290 throw new DomainException ($ e ->getMessage (), 0 , $ e );
303291 }
@@ -352,19 +340,8 @@ private static function verify(
352340 'OpenSSL error: ' . \openssl_error_string ()
353341 );
354342 case 'sodium_crypto ' :
355- if (!\function_exists ('sodium_crypto_sign_verify_detached ' )) {
356- throw new DomainException ('libsodium is not available ' );
357- }
358- if (!\is_string ($ keyMaterial )) {
359- throw new InvalidArgumentException ('key must be a string when using EdDSA ' );
360- }
361343 try {
362- // The last non-empty line is used as the key.
363- $ lines = array_filter (explode ("\n" , $ keyMaterial ));
364- $ key = base64_decode ((string ) end ($ lines ));
365- if (\strlen ($ key ) === 0 ) {
366- throw new DomainException ('Key cannot be empty string ' );
367- }
344+ $ key = self ::validateEdDSAKey ($ keyMaterial );
368345 if (\strlen ($ signature ) === 0 ) {
369346 throw new DomainException ('Signature cannot be empty string ' );
370347 }
@@ -473,7 +450,6 @@ public static function urlsafeB64Encode(string $input): string
473450 return \str_replace ('= ' , '' , \strtr (\base64_encode ($ input ), '+/ ' , '-_ ' ));
474451 }
475452
476-
477453 /**
478454 * Determine if an algorithm has been provided for each Key
479455 *
@@ -745,4 +721,25 @@ private static function validateEcKeyLength(
745721 throw new DomainException ('Provided key is too short ' );
746722 }
747723 }
724+
725+ /**
726+ * @param string|OpenSSLAsymmetricKey|OpenSSLCertificate $keyMaterial
727+ * @return non-empty-string
728+ */
729+ private static function validateEdDSAKey (#[\SensitiveParameter] $ keyMaterial ): string
730+ {
731+ if (!\function_exists ('sodium_crypto_sign_verify_detached ' )) {
732+ throw new DomainException ('libsodium is not available ' );
733+ }
734+ if (!\is_string ($ keyMaterial )) {
735+ throw new InvalidArgumentException ('key must be a string when using EdDSA ' );
736+ }
737+ // The last non-empty line is used as the key.
738+ $ lines = array_filter (explode ("\n" , $ keyMaterial ));
739+ $ key = self ::urlsafeB64Decode ((string ) end ($ lines ));
740+ if (\strlen ($ key ) === 0 ) {
741+ throw new DomainException ('Key cannot be empty string ' );
742+ }
743+ return $ key ;
744+ }
748745}
0 commit comments