Skip to content

Commit a556263

Browse files
tm1000taylorotwell
andauthored
[8.x] Throw if tag is passed but is not supported (#41479)
* Throw if tag is passed but is not supported * Fix Styling * formatting Co-authored-by: Taylor Otwell <[email protected]>
1 parent 03f3f31 commit a556263

File tree

1 file changed

+20
-5
lines changed

1 file changed

+20
-5
lines changed

src/Illuminate/Encryption/Encrypter.php

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -159,11 +159,9 @@ public function decrypt($payload, $unserialize = true)
159159

160160
$iv = base64_decode($payload['iv']);
161161

162-
$tag = empty($payload['tag']) ? null : base64_decode($payload['tag']);
163-
164-
if (self::$supportedCiphers[strtolower($this->cipher)]['aead'] && strlen($tag) !== 16) {
165-
throw new DecryptException('Could not decrypt the data.');
166-
}
162+
$this->ensureTagIsValid(
163+
$tag = empty($payload['tag']) ? null : base64_decode($payload['tag'])
164+
);
167165

168166
// Here we will decrypt the value. If we are able to successfully decrypt it
169167
// we will then unserialize it and return it out to the caller. If we are
@@ -255,6 +253,23 @@ protected function validMac(array $payload)
255253
);
256254
}
257255

256+
/**
257+
* Ensure the given tag is a valid tag given the selected cipher.
258+
*
259+
* @param string $tag
260+
* @return void
261+
*/
262+
protected function ensureTagIsValid($tag)
263+
{
264+
if (self::$supportedCiphers[strtolower($this->cipher)]['aead'] && strlen($tag) !== 16) {
265+
throw new DecryptException('Could not decrypt the data.');
266+
}
267+
268+
if (! self::$supportedCiphers[strtolower($this->cipher)]['aead'] && is_string($tag)) {
269+
throw new DecryptException('Unable to use tag because the cipher algorithm does not support AEAD.');
270+
}
271+
}
272+
258273
/**
259274
* Get the encryption key.
260275
*

0 commit comments

Comments
 (0)