@@ -82,17 +82,16 @@ class OpenSSLHandler extends BaseHandler
8282 */
8383 public function encrypt (#[SensitiveParameter] $ data , #[SensitiveParameter] $ params = null )
8484 {
85- // Allow key override
86- if ($ params !== null ) {
87- $ this ->key = is_array ($ params ) && isset ($ params ['key ' ]) ? $ params ['key ' ] : $ params ;
88- }
85+ $ key = $ params !== null
86+ ? (is_array ($ params ) && isset ($ params ['key ' ]) ? $ params ['key ' ] : $ params )
87+ : $ this ->key ;
8988
90- if (empty ($ this -> key )) {
89+ if (empty ($ key )) {
9190 throw EncryptionException::forNeedsStarterKey ();
9291 }
9392
9493 // derive a secret key
95- $ encryptKey = \hash_hkdf ($ this ->digest , $ this -> key , 0 , $ this ->encryptKeyInfo );
94+ $ encryptKey = \hash_hkdf ($ this ->digest , $ key , 0 , $ this ->encryptKeyInfo );
9695
9796 // basic encryption
9897 $ iv = ($ ivSize = \openssl_cipher_iv_length ($ this ->cipher )) ? \openssl_random_pseudo_bytes ($ ivSize ) : null ;
@@ -106,7 +105,7 @@ public function encrypt(#[SensitiveParameter] $data, #[SensitiveParameter] $para
106105 $ result = $ this ->rawData ? $ iv . $ data : base64_encode ($ iv . $ data );
107106
108107 // derive a secret key
109- $ authKey = \hash_hkdf ($ this ->digest , $ this -> key , 0 , $ this ->authKeyInfo );
108+ $ authKey = \hash_hkdf ($ this ->digest , $ key , 0 , $ this ->authKeyInfo );
110109
111110 $ hmacKey = \hash_hmac ($ this ->digest , $ result , $ authKey , $ this ->rawData );
112111
@@ -118,42 +117,49 @@ public function encrypt(#[SensitiveParameter] $data, #[SensitiveParameter] $para
118117 */
119118 public function decrypt ($ data , #[SensitiveParameter] $ params = null )
120119 {
121- // Allow key override
122- if ( $ params !== null ) {
123- $ this -> key = is_array ($ params ) && isset ($ params ['key ' ]) ? $ params ['key ' ] : $ params;
124- }
120+ return $ this -> tryDecryptWithFallback ( $ data , $ params , function ( $ data , $ params ): string {
121+ $ key = $ params !== null
122+ ? ( is_array ($ params ) && isset ($ params ['key ' ]) ? $ params ['key ' ] : $ params)
123+ : $ this -> key ;
125124
126- if (empty ($ this -> key )) {
127- throw EncryptionException::forNeedsStarterKey ();
128- }
125+ if (empty ($ key )) {
126+ throw EncryptionException::forNeedsStarterKey ();
127+ }
129128
130- // derive a secret key
131- $ authKey = \hash_hkdf ($ this ->digest , $ this -> key , 0 , $ this ->authKeyInfo );
129+ // derive a secret key
130+ $ authKey = \hash_hkdf ($ this ->digest , $ key , 0 , $ this ->authKeyInfo );
132131
133- $ hmacLength = $ this ->rawData
134- ? $ this ->digestSize [$ this ->digest ]
135- : $ this ->digestSize [$ this ->digest ] * 2 ;
132+ $ hmacLength = $ this ->rawData
133+ ? $ this ->digestSize [$ this ->digest ]
134+ : $ this ->digestSize [$ this ->digest ] * 2 ;
136135
137- $ hmacKey = self ::substr ($ data , 0 , $ hmacLength );
138- $ data = self ::substr ($ data , $ hmacLength );
139- $ hmacCalc = \hash_hmac ($ this ->digest , $ data , $ authKey , $ this ->rawData );
136+ $ hmacKey = self ::substr ($ data , 0 , $ hmacLength );
137+ $ data = self ::substr ($ data , $ hmacLength );
138+ $ hmacCalc = \hash_hmac ($ this ->digest , $ data , $ authKey , $ this ->rawData );
140139
141- if (! hash_equals ($ hmacKey , $ hmacCalc )) {
142- throw EncryptionException::forAuthenticationFailed ();
143- }
140+ if (! hash_equals ($ hmacKey , $ hmacCalc )) {
141+ throw EncryptionException::forAuthenticationFailed ();
142+ }
144143
145- $ data = $ this ->rawData ? $ data : base64_decode ($ data , true );
144+ $ data = $ this ->rawData ? $ data : base64_decode ($ data , true );
146145
147- if ($ ivSize = \openssl_cipher_iv_length ($ this ->cipher )) {
148- $ iv = self ::substr ($ data , 0 , $ ivSize );
149- $ data = self ::substr ($ data , $ ivSize );
150- } else {
151- $ iv = null ;
152- }
146+ if ($ ivSize = \openssl_cipher_iv_length ($ this ->cipher )) {
147+ $ iv = self ::substr ($ data , 0 , $ ivSize );
148+ $ data = self ::substr ($ data , $ ivSize );
149+ } else {
150+ $ iv = null ;
151+ }
153152
154- // derive a secret key
155- $ encryptKey = \hash_hkdf ($ this ->digest , $ this ->key , 0 , $ this ->encryptKeyInfo );
153+ // derive a secret key
154+ $ encryptKey = \hash_hkdf ($ this ->digest , $ key , 0 , $ this ->encryptKeyInfo );
155+
156+ $ result = \openssl_decrypt ($ data , $ this ->cipher , $ encryptKey , OPENSSL_RAW_DATA , $ iv );
157+
158+ if ($ result === false ) {
159+ throw EncryptionException::forAuthenticationFailed ();
160+ }
156161
157- return \openssl_decrypt ($ data , $ this ->cipher , $ encryptKey , OPENSSL_RAW_DATA , $ iv );
162+ return $ result ;
163+ });
158164 }
159165}
0 commit comments