This repository has been archived by the owner on Jul 28, 2022. It is now read-only.
forked from ezimuel/PHP-Secure-Session
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSecureSession.php
215 lines (213 loc) · 5.78 KB
/
SecureSession.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
<?php
/**
* ------------------------------------------------
* Encrypt PHP session data using files
* ------------------------------------------------
* The encryption is built using mcrypt extension
* and the randomness is managed by openssl
* The default encryption algorithm is AES (Rijndael-128)
* and we use CBC+HMAC (Encrypt-then-mac) with SHA-256
*
* @author Enrico Zimuel (enrico@zimuel.it)
* @copyright GNU General Public License
*/
class SecureSession {
/**
* Encryption algorithm
*
* @var string
*/
protected $_algo= MCRYPT_RIJNDAEL_128;
/**
* Key for encryption/decryption
*
* @var string
*/
protected $_key;
/**
* Key for HMAC authentication
*
* @var string
*/
protected $_auth;
/**
* Path of the session file
*
* @var string
*/
protected $_path;
/**
* Session name (optional)
*
* @var string
*/
protected $_name;
/**
* Size of the IV vector for encryption
*
* @var integer
*/
protected $_ivSize;
/**
* Cookie variable name of the encryption + auth key
*
* @var string
*/
protected $_keyName;
/**
* Generate a random key using openssl
* fallback to mcrypt_create_iv
*
* @param integer $length
* @return string
*/
protected function _randomKey($length=32) {
if(function_exists('openssl_random_pseudo_bytes')) {
$rnd = openssl_random_pseudo_bytes($length, $strong);
if ($strong === true) {
return $rnd;
}
}
if (defined(MCRYPT_DEV_URANDOM)) {
return mcrypt_create_iv($length, MCRYPT_DEV_URANDOM);
} else {
throw new Exception("I cannot generate a secure pseudo-random key. Please install OpenSSL or Mcrypt extension");
}
}
/**
* Constructor
*/
public function __construct()
{
session_set_save_handler(
array($this, "open"),
array($this, "close"),
array($this, "read"),
array($this, "write"),
array($this, "destroy"),
array($this, "gc")
);
}
/**
* Open the session
*
* @param string $save_path
* @param string $session_name
* @return bool
*/
public function open($save_path, $session_name)
{
$this->_path = $save_path . DIRECTORY_SEPARATOR;
$this->_name = $session_name;
$this->_keyName = "KEY_$session_name";
$this->_ivSize = mcrypt_get_iv_size($this->_algo, MCRYPT_MODE_CBC);
if (empty($_COOKIE[$this->_keyName]) || strpos($_COOKIE[$this->_keyName],':')===false) {
$keyLength = mcrypt_get_key_size($this->_algo, MCRYPT_MODE_CBC);
$this->_key = self::_randomKey($keyLength);
$this->_auth = self::_randomKey(32);
$cookie_param = session_get_cookie_params();
setcookie(
$this->_keyName,
base64_encode($this->_key) . ':' . base64_encode($this->_auth),
time() + $cookie_param['lifetime'],
$cookie_param['path'],
$cookie_param['domain'],
$cookie_param['secure'],
$cookie_param['httponly']
);
} else {
list ($this->_key, $this->_auth) = explode (':',$_COOKIE[$this->_keyName]);
$this->_key = base64_decode($this->_key);
$this->_auth = base64_decode($this->_auth);
}
return true;
}
/**
* Close the session
*
* @return bool
*/
public function close()
{
return true;
}
/**
* Read and decrypt the session
*
* @param integer $id
* @return string
*/
public function read($id)
{
$sess_file = $this->_path.$this->_name."_$id";
if (!file_exists($sess_file)) {
return false;
}
$data = file_get_contents($sess_file);
list($hmac, $iv, $encrypted)= explode(':',$data);
$iv = base64_decode($iv);
$encrypted = base64_decode($encrypted);
$newHmac = hash_hmac('sha256', $iv . $this->_algo . $encrypted, $this->_auth);
if ($hmac !== $newHmac) {
return false;
}
$decrypt = mcrypt_decrypt(
$this->_algo,
$this->_key,
$encrypted,
MCRYPT_MODE_CBC,
$iv
);
return rtrim($decrypt, "\0");
}
/**
* Encrypt and write the session
*
* @param integer $id
* @param string $data
* @return bool
*/
public function write($id, $data)
{
$sess_file = $this->_path . $this->_name . "_$id";
$iv = mcrypt_create_iv($this->_ivSize, MCRYPT_DEV_URANDOM);
$encrypted = mcrypt_encrypt(
$this->_algo,
$this->_key,
$data,
MCRYPT_MODE_CBC,
$iv
);
$hmac = hash_hmac('sha256', $iv . $this->_algo . $encrypted, $this->_auth);
$bytes = file_put_contents($sess_file, $hmac . ':' . base64_encode($iv) . ':' . base64_encode($encrypted), LOCK_EX);
return ($bytes !== false);
}
/**
* Destoroy the session
*
* @param int $id
* @return bool
*/
public function destroy($id)
{
$sess_file = $this->_path . $this->_name . "_$id";
setcookie ($this->_keyName, '', time() - 3600);
return(@unlink($sess_file));
}
/**
* Garbage Collector
*
* @param int $max
* @return bool
*/
public function gc($max)
{
foreach (glob($this->_path . $this->_name . '_*') as $filename) {
if (filemtime($filename) + $max < time()) {
@unlink($filename);
}
}
return true;
}
}
new SecureSession();