|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Omnipay\Redsys\Message; |
|
4
|
|
|
|
|
5
|
|
|
use Omnipay\Common\Exception\RuntimeException; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Security |
|
9
|
|
|
* |
|
10
|
|
|
* This class provides common encoding, decoding and signing functions. |
|
11
|
|
|
* While all of this code could be called statically, it is left as a |
|
12
|
|
|
* regular class in order to faciliate unit testing. If alternate |
|
13
|
|
|
* encryption methods are provided later, the VERSION const can be |
|
14
|
|
|
* switched to a constructor option (and validated against a whitelist). |
|
15
|
|
|
*/ |
|
16
|
|
|
class Security |
|
17
|
|
|
{ |
|
18
|
|
|
/** @var string */ |
|
19
|
|
|
const VERSION = 'HMAC_SHA256_V1'; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Encode merchant parameters |
|
23
|
|
|
* |
|
24
|
|
|
* @param array $data The parameters to encode |
|
25
|
|
|
* |
|
26
|
|
|
* @return string Encoded data |
|
27
|
|
|
*/ |
|
28
|
1 |
|
public function encodeMerchantParameters($data) |
|
29
|
|
|
{ |
|
30
|
1 |
|
return base64_encode(json_encode($data)); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Decode merchant parameters |
|
35
|
|
|
* |
|
36
|
|
|
* @param string $data The encoded string of parameters |
|
37
|
|
|
* |
|
38
|
|
|
* @return array Decoded data |
|
39
|
|
|
*/ |
|
40
|
9 |
|
public function decodeMerchantParameters($data) |
|
41
|
|
|
{ |
|
42
|
9 |
|
return (array)json_decode(base64_decode(strtr($data, '-_', '+/'))); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Encrypt message with given key and default IV |
|
47
|
|
|
* |
|
48
|
|
|
* @param string $message The message to encrypt |
|
49
|
|
|
* @param string $key The key used to encrypt the message |
|
50
|
|
|
* |
|
51
|
|
|
* @return string Encrypted message |
|
52
|
|
|
* |
|
53
|
|
|
* @throws RuntimeException |
|
54
|
|
|
*/ |
|
55
|
11 |
|
protected function encryptMessage($message, $key) |
|
56
|
|
|
{ |
|
57
|
11 |
|
$iv = implode(array_map('chr', array(0, 0, 0, 0, 0, 0, 0, 0))); |
|
58
|
|
|
|
|
59
|
11 |
|
if ($this->hasValidEncryptionMethod()) { |
|
60
|
10 |
|
$ciphertext = mcrypt_encrypt(MCRYPT_3DES, $key, $message, MCRYPT_MODE_CBC, $iv); |
|
61
|
10 |
|
} else { |
|
62
|
1 |
|
throw new RuntimeException('No valid encryption extension installed'); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
10 |
|
return $ciphertext; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Check if the system has a valid encryption method available |
|
70
|
|
|
* |
|
71
|
|
|
* @todo add or switch to extension_loaded()? |
|
72
|
|
|
* |
|
73
|
|
|
* @return bool |
|
74
|
|
|
*/ |
|
75
|
9 |
|
public function hasValidEncryptionMethod() |
|
76
|
|
|
{ |
|
77
|
9 |
|
return function_exists('mcrypt_encrypt'); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Create signature hash used to verify messages |
|
82
|
|
|
* |
|
83
|
|
|
* @todo Add if-check on algorithm to match against signature version as new param? |
|
84
|
|
|
* |
|
85
|
|
|
* @param string $message The message to encrypt |
|
86
|
|
|
* @param string $salt Unique salt used to generate the ciphertext |
|
87
|
|
|
* @param string $key The key used to encrypt the message |
|
88
|
|
|
* |
|
89
|
|
|
* @return string Generated signature |
|
90
|
|
|
*/ |
|
91
|
9 |
|
public function createSignature($message, $salt, $key) |
|
92
|
|
|
{ |
|
93
|
9 |
|
$ciphertext = $this->encryptMessage($salt, $key); |
|
94
|
9 |
|
return base64_encode(hash_hmac('sha256', $message, $ciphertext, true)); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
8 |
|
public function createReturnSignature($message, $salt, $key) |
|
98
|
|
|
{ |
|
99
|
8 |
|
return strtr($this->createSignature($message, $salt, $key), '+/', '-_'); |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
|