@@ -678,6 +678,65 @@ public function provideHmac()
678678 ];
679679 }
680680
681+ /** @dataProvider provideEcKeyInvalidLength */
682+ public function testEcKeyLengthValidationThrowsExceptionEncode (string $ keyFile , string $ alg ): void
683+ {
684+ $ this ->expectException (DomainException::class);
685+ $ this ->expectExceptionMessage ('Provided key is too short ' );
686+
687+ $ tooShortEcKey = file_get_contents (__DIR__ . '/data/ ' . $ keyFile );
688+ $ payload = ['message ' => 'abc ' ];
689+
690+ JWT ::encode ($ payload , $ tooShortEcKey , $ alg );
691+ }
692+
693+ public function testEcKeyLengthValidationThrowsExceptionDecode (): void
694+ {
695+ $ this ->expectException (DomainException::class);
696+ $ this ->expectExceptionMessage ('Provided key is too short ' );
697+
698+ $ payload = ['message ' => 'abc ' ];
699+
700+ $ validEcKeyBytes = file_get_contents (__DIR__ . '/data/ecdsa384-private.pem ' );
701+ $ encoded = JWT ::encode ($ payload , $ validEcKeyBytes , 'ES256 ' );
702+
703+ $ tooShortEcKey = file_get_contents (__DIR__ . '/data/ecdsa192-public.pem ' );
704+ JWT ::decode ($ encoded , new Key ($ tooShortEcKey , 'ES256 ' ));
705+ }
706+
707+ /** @dataProvider provideEcKey */
708+ public function testEcKeyLengthValidationPassesWithCorrectLength (
709+ string $ privateKeyFile ,
710+ string $ publicKeyFile ,
711+ string $ alg
712+ ): void {
713+ $ payload = ['message ' => 'test hmac length ' ];
714+
715+ // Test with a key that is the required length
716+ $ privateKeyBytes = file_get_contents (__DIR__ . '/data/ ' . $ privateKeyFile );
717+ $ encoded48 = JWT ::encode ($ payload , $ privateKeyBytes , $ alg );
718+
719+ $ publicKeyBytes = file_get_contents (__DIR__ . '/data/ ' . $ publicKeyFile );
720+ $ decoded48 = JWT ::decode ($ encoded48 , new Key ($ publicKeyBytes , $ alg ));
721+ $ this ->assertEquals ($ payload ['message ' ], $ decoded48 ->message );
722+ }
723+
724+ public function provideEcKeyInvalidLength ()
725+ {
726+ return [
727+ ['ecdsa192-private.pem ' , 'ES256 ' ],
728+ ['ecdsa-private.pem ' , 'ES384 ' ],
729+ ];
730+ }
731+
732+ public function provideEcKey ()
733+ {
734+ return [
735+ ['ecdsa-private.pem ' , 'ecdsa-public.pem ' , 'ES256 ' ],
736+ ['ecdsa384-private.pem ' , 'ecdsa384-public.pem ' , 'ES384 ' ],
737+ ];
738+ }
739+
681740 private function generateHmac256 (): Key
682741 {
683742 return new Key (random_bytes (32 ), 'HS256 ' );
0 commit comments