Skip to content

Commit

Permalink
[8.x] Throw if tag is passed but is not supported (#41479)
Browse files Browse the repository at this point in the history
* Throw if tag is passed but is not supported

* Fix Styling

* formatting

Co-authored-by: Taylor Otwell <taylor@laravel.com>
  • Loading branch information
tm1000 and taylorotwell committed Mar 14, 2022
1 parent 03f3f31 commit a556263
Showing 1 changed file with 20 additions and 5 deletions.
25 changes: 20 additions & 5 deletions src/Illuminate/Encryption/Encrypter.php
Expand Up @@ -159,11 +159,9 @@ public function decrypt($payload, $unserialize = true)

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

$tag = empty($payload['tag']) ? null : base64_decode($payload['tag']);

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

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

/**
* Ensure the given tag is a valid tag given the selected cipher.
*
* @param string $tag
* @return void
*/
protected function ensureTagIsValid($tag)
{
if (self::$supportedCiphers[strtolower($this->cipher)]['aead'] && strlen($tag) !== 16) {
throw new DecryptException('Could not decrypt the data.');
}

if (! self::$supportedCiphers[strtolower($this->cipher)]['aead'] && is_string($tag)) {
throw new DecryptException('Unable to use tag because the cipher algorithm does not support AEAD.');
}
}

/**
* Get the encryption key.
*
Expand Down

0 comments on commit a556263

Please sign in to comment.