-
Notifications
You must be signed in to change notification settings - Fork 78
/
SecureHandler.php
167 lines (158 loc) · 4.32 KB
/
SecureHandler.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
<?php
/**
* Encrypt PHP session data for the internal PHP save handlers
*
* The encryption is built using OpenSSL extension with AES-256-CBC and the
* authentication is provided using HMAC with SHA256.
*
* @author Enrico Zimuel ([email protected])
* @copyright MIT License
*/
namespace PHPSecureSession;
use SessionHandler;
class SecureHandler extends SessionHandler
{
/**
* Encryption and authentication key
* @var string
*/
protected $key;
/**
* Constructor
*/
public function __construct()
{
if (! extension_loaded('openssl')) {
throw new \RuntimeException(sprintf(
"You need the OpenSSL extension to use %s",
__CLASS__
));
}
if (! extension_loaded('mbstring')) {
throw new \RuntimeException(sprintf(
"You need the Multibytes extension to use %s",
__CLASS__
));
}
}
/**
* Open the session
*
* @param string $save_path
* @param string $session_name
* @return bool
*/
public function open($save_path, $session_name)
{
$this->key = $this->getKey('KEY_' . $session_name);
return parent::open($save_path, $session_name);
}
/**
* Read from session and decrypt
*
* @param string $id
*/
public function read($id)
{
$data = parent::read($id);
return empty($data) ? '' : $this->decrypt($data, $this->key);
}
/**
* Encrypt the data and write into the session
*
* @param string $id
* @param string $data
*/
public function write($id, $data)
{
return parent::write($id, $this->encrypt($data, $this->key));
}
/**
* Encrypt and authenticate
*
* @param string $data
* @param string $key
* @return string
*/
protected function encrypt($data, $key)
{
$iv = random_bytes(16); // AES block size in CBC mode
// Encryption
$ciphertext = openssl_encrypt(
$data,
'AES-256-CBC',
mb_substr($key, 0, 32, '8bit'),
OPENSSL_RAW_DATA,
$iv
);
// Authentication
$hmac = hash_hmac(
'SHA256',
$iv . $ciphertext,
mb_substr($key, 32, null, '8bit'),
true
);
return $hmac . $iv . $ciphertext;
}
/**
* Authenticate and decrypt
*
* @param string $data
* @param string $key
* @return string
*/
protected function decrypt($data, $key)
{
$hmac = mb_substr($data, 0, 32, '8bit');
$iv = mb_substr($data, 32, 16, '8bit');
$ciphertext = mb_substr($data, 48, null, '8bit');
// Authentication
$hmacNew = hash_hmac(
'SHA256',
$iv . $ciphertext,
mb_substr($key, 32, null, '8bit'),
true
);
if (! hash_equals($hmac, $hmacNew)) {
throw new Exception\AuthenticationFailedException('Authentication failed');
}
// Decrypt
return openssl_decrypt(
$ciphertext,
'AES-256-CBC',
mb_substr($key, 0, 32, '8bit'),
OPENSSL_RAW_DATA,
$iv
);
}
/**
* Get the encryption and authentication keys from cookie
*
* @param string $name
* @return string
*/
protected function getKey($name)
{
if (empty($_COOKIE[$name])) {
$key = random_bytes(64); // 32 for encryption and 32 for authentication
$cookieParam = session_get_cookie_params();
$encKey = base64_encode($key);
setcookie(
$name,
$encKey,
// if session cookie lifetime > 0 then add to current time
// otherwise leave it as zero, honoring zero's special meaning
// expire at browser close.
($cookieParam['lifetime'] > 0) ? time() + $cookieParam['lifetime'] : 0,
$cookieParam['path'],
$cookieParam['domain'],
$cookieParam['secure'],
$cookieParam['httponly']
);
$_COOKIE[$name] = $encKey;
} else {
$key = base64_decode($_COOKIE[$name]);
}
return $key;
}
}