|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Copyright 2022 SURFnet bv |
|
5
|
|
|
* |
|
6
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
7
|
|
|
* you may not use this file except in compliance with the License. |
|
8
|
|
|
* You may obtain a copy of the License at |
|
9
|
|
|
* |
|
10
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
|
11
|
|
|
* |
|
12
|
|
|
* Unless required by applicable law or agreed to in writing, software |
|
13
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
|
14
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
15
|
|
|
* See the License for the specific language governing permissions and |
|
16
|
|
|
* limitations under the License. |
|
17
|
|
|
*/ |
|
|
|
|
|
|
18
|
|
|
|
|
19
|
|
|
namespace Surfnet\StepupGateway\GatewayBundle\Sso2fa\Crypto; |
|
20
|
|
|
|
|
21
|
|
|
use Exception; |
|
22
|
|
|
use ParagonIE\Halite\HiddenString; |
|
23
|
|
|
use ParagonIE\Halite\Symmetric\Crypto; |
|
24
|
|
|
use ParagonIE\Halite\Symmetric\EncryptionKey; |
|
25
|
|
|
use Surfnet\StepupGateway\GatewayBundle\Sso2fa\Exception\DecryptionFailedException; |
|
26
|
|
|
use Surfnet\StepupGateway\GatewayBundle\Sso2fa\Exception\EncryptionFailedException; |
|
27
|
|
|
use Surfnet\StepupGateway\GatewayBundle\Sso2fa\ValueObject\Configuration; |
|
28
|
|
|
use Surfnet\StepupGateway\GatewayBundle\Sso2fa\ValueObject\CookieValue; |
|
29
|
|
|
|
|
30
|
|
|
class HaliteCryptoHelper implements CryptoHelperInterface |
|
|
|
|
|
|
31
|
|
|
{ |
|
32
|
|
|
private $encryptionKey; |
|
|
|
|
|
|
33
|
|
|
|
|
34
|
|
|
public function __construct(Configuration $configuration) |
|
|
|
|
|
|
35
|
|
|
{ |
|
36
|
|
|
// The configured encryption key is used to create a Halite EncryptionKey |
|
37
|
|
|
$this->encryptionKey = new EncryptionKey(new HiddenString($configuration->getEncryptionKey())); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
|
|
|
|
|
41
|
|
|
* Halite always uses authenticated encryption. |
|
42
|
|
|
* See: https://github.com/paragonie/halite/blob/v4.x/doc/Classes/Symmetric/Crypto.md#encrypt |
|
43
|
|
|
* |
|
44
|
|
|
* It uses XSalsa20 for encryption and BLAKE2b for message Authentication (MAC) |
|
45
|
|
|
* The keys used for encryption and message authentication are derived from the secret key using a |
|
46
|
|
|
* HKDF using a salt This means that learning either derived key cannot lead to learning the other |
|
47
|
|
|
* derived key, or the secret key input in the HKDF. Encrypting many messages using the same |
|
48
|
|
|
* secret key is not a problem in this design. |
|
49
|
|
|
*/ |
|
|
|
|
|
|
50
|
|
|
public function encrypt(CookieValue $cookieValue): string |
|
51
|
|
|
{ |
|
52
|
|
|
try { |
|
53
|
|
|
$plainTextCookieValue = new HiddenString($cookieValue->serialize()); |
|
54
|
|
|
// Encryption (we use the default encoding: Halite::ENCODE_BASE64URLSAFE) |
|
55
|
|
|
$encryptedData = Crypto::encrypt( |
|
56
|
|
|
$plainTextCookieValue, |
|
57
|
|
|
$this->encryptionKey |
|
58
|
|
|
); |
|
59
|
|
|
} catch (Exception $e) { |
|
60
|
|
|
throw new EncryptionFailedException( |
|
61
|
|
|
'Encrypting the CookieValue for failed', |
|
62
|
|
|
$e |
|
63
|
|
|
); |
|
64
|
|
|
} |
|
65
|
|
|
return $encryptedData; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
|
|
|
|
|
69
|
|
|
* Decrypt the cookie ciphertext back to plain text. |
|
70
|
|
|
* Again using the encryption key, used to encrypt the data. |
|
71
|
|
|
* The decrypt method will return a deserialized CookieValue value object |
|
72
|
|
|
*/ |
|
|
|
|
|
|
73
|
|
|
public function decrypt(string $cookieData): CookieValue |
|
74
|
|
|
{ |
|
75
|
|
|
try { |
|
76
|
|
|
// Decryption: (we use the default encoding: Halite::DECODE_BASE64URLSAFE) |
|
77
|
|
|
$decryptedData = Crypto::decrypt( |
|
78
|
|
|
$cookieData, |
|
79
|
|
|
$this->encryptionKey |
|
80
|
|
|
); |
|
81
|
|
|
} catch (Exception $e) { |
|
82
|
|
|
throw new DecryptionFailedException( |
|
83
|
|
|
'Decrypting the CookieValue failed, see embedded error message for details', |
|
84
|
|
|
$e |
|
85
|
|
|
); |
|
86
|
|
|
} |
|
87
|
|
|
return CookieValue::deserialize($decryptedData->getString()); |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
|