Rev 1050 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
2 | daniel-mar | 1 | <?php |
2 | |||
3 | /* |
||
4 | * OIDplus 2.0 |
||
1086 | daniel-mar | 5 | * Copyright 2019 - 2023 Daniel Marschall, ViaThinkSoft |
2 | daniel-mar | 6 | * |
7 | * Licensed under the Apache License, Version 2.0 (the "License"); |
||
8 | * you may not use this file except in compliance with the License. |
||
9 | * You may obtain a copy of the License at |
||
10 | * |
||
11 | * http://www.apache.org/licenses/LICENSE-2.0 |
||
12 | * |
||
13 | * Unless required by applicable law or agreed to in writing, software |
||
14 | * distributed under the License is distributed on an "AS IS" BASIS, |
||
15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||
16 | * See the License for the specific language governing permissions and |
||
17 | * limitations under the License. |
||
18 | */ |
||
19 | |||
1050 | daniel-mar | 20 | namespace ViaThinkSoft\OIDplus; |
511 | daniel-mar | 21 | |
1086 | daniel-mar | 22 | // phpcs:disable PSR1.Files.SideEffects |
23 | \defined('INSIDE_OIDPLUS') or die; |
||
24 | // phpcs:enable PSR1.Files.SideEffects |
||
25 | |||
730 | daniel-mar | 26 | class OIDplusSessionHandler extends OIDplusBaseClass implements OIDplusGetterSetterInterface { |
2 | daniel-mar | 27 | |
263 | daniel-mar | 28 | private $secret = ''; |
847 | daniel-mar | 29 | protected $sessionLifetime = 0; |
2 | daniel-mar | 30 | |
263 | daniel-mar | 31 | public function __construct() { |
261 | daniel-mar | 32 | $this->sessionLifetime = OIDplus::baseConfig()->getValue('SESSION_LIFETIME', 30*60); |
33 | $this->secret = OIDplus::baseConfig()->getValue('SERVER_SECRET'); |
||
34 | |||
2 | daniel-mar | 35 | // **PREVENTING SESSION HIJACKING** |
36 | // Prevents javascript XSS attacks aimed to steal the session ID |
||
592 | daniel-mar | 37 | @ini_set('session.cookie_httponly', '1'); |
2 | daniel-mar | 38 | |
39 | // **PREVENTING SESSION FIXATION** |
||
40 | // Session ID cannot be passed through URLs |
||
592 | daniel-mar | 41 | @ini_set('session.use_only_cookies', '1'); |
2 | daniel-mar | 42 | |
592 | daniel-mar | 43 | @ini_set('session.use_trans_sid', '0'); |
85 | daniel-mar | 44 | |
2 | daniel-mar | 45 | // Uses a secure connection (HTTPS) if possible |
230 | daniel-mar | 46 | @ini_set('session.cookie_secure', OIDplus::isSslAvailable()); |
2 | daniel-mar | 47 | |
801 | daniel-mar | 48 | $path = OIDplus::webpath(null,OIDplus::PATH_RELATIVE); |
555 | daniel-mar | 49 | if (empty($path)) $path = '/'; |
50 | @ini_set('session.cookie_path', $path); |
||
2 | daniel-mar | 51 | |
563 | daniel-mar | 52 | @ini_set('session.cookie_samesite', OIDplus::baseConfig()->getValue('COOKIE_SAMESITE_POLICY', 'Strict')); |
2 | daniel-mar | 53 | |
592 | daniel-mar | 54 | @ini_set('session.use_strict_mode', '1'); |
2 | daniel-mar | 55 | |
261 | daniel-mar | 56 | @ini_set('session.gc_maxlifetime', $this->sessionLifetime); |
2 | daniel-mar | 57 | } |
58 | |||
86 | daniel-mar | 59 | protected function sessionSafeStart() { |
179 | daniel-mar | 60 | if (!isset($_SESSION)) { |
61 | // TODO: session_name() makes some problems. Leave it away for now. |
||
261 | daniel-mar | 62 | //session_name('OIDplus_SESHDLR'); |
179 | daniel-mar | 63 | if (!session_start()) { |
360 | daniel-mar | 64 | throw new OIDplusException(_L('Session could not be started')); |
179 | daniel-mar | 65 | } |
66 | } |
||
86 | daniel-mar | 67 | |
87 | daniel-mar | 68 | if (!isset($_SESSION['ip'])) { |
69 | if (!isset($_SERVER['REMOTE_ADDR'])) return; |
||
86 | daniel-mar | 70 | |
87 | daniel-mar | 71 | // Remember the IP address of the user |
72 | $_SESSION['ip'] = $_SERVER['REMOTE_ADDR']; |
||
73 | } else { |
||
74 | if ($_SERVER['REMOTE_ADDR'] != $_SESSION['ip']) { |
||
75 | // Was the session hijacked?! Get out of here! |
||
435 | daniel-mar | 76 | |
77 | // We don't use $this->destroySession(), because this calls sessionSafeStart() again |
||
78 | $_SESSION = array(); |
||
79 | session_destroy(); |
||
80 | session_write_close(); |
||
557 | daniel-mar | 81 | OIDplus::cookieUtils()->unsetcookie(session_name()); // remove cookie, so GDPR people are happy |
87 | daniel-mar | 82 | } |
86 | daniel-mar | 83 | } |
84 | } |
||
85 | |||
2 | daniel-mar | 86 | function __destruct() { |
87 | session_write_close(); |
||
88 | } |
||
89 | |||
424 | daniel-mar | 90 | private $cacheSetValues = array(); // Important if you do a setValue() followed by an getValue() |
91 | |||
2 | daniel-mar | 92 | public function setValue($name, $value) { |
716 | daniel-mar | 93 | $enc_data = self::encrypt($value, $this->secret); |
426 | daniel-mar | 94 | |
716 | daniel-mar | 95 | $this->cacheSetValues[$name] = $enc_data; |
96 | |||
86 | daniel-mar | 97 | $this->sessionSafeStart(); |
557 | daniel-mar | 98 | OIDplus::cookieUtils()->setcookie(session_name(),session_id(),time()+$this->sessionLifetime); |
85 | daniel-mar | 99 | |
716 | daniel-mar | 100 | $_SESSION[$name] = $enc_data; |
2 | daniel-mar | 101 | } |
102 | |||
569 | daniel-mar | 103 | public function getValue($name, $default = NULL) { |
424 | daniel-mar | 104 | if (isset($this->cacheSetValues[$name])) return self::decrypt($this->cacheSetValues[$name], $this->secret); |
105 | |||
585 | daniel-mar | 106 | if (!$this->isActive()) return $default; // GDPR: Only start a session when we really need one |
86 | daniel-mar | 107 | $this->sessionSafeStart(); |
557 | daniel-mar | 108 | OIDplus::cookieUtils()->setcookie(session_name(),session_id(),time()+$this->sessionLifetime); |
85 | daniel-mar | 109 | |
569 | daniel-mar | 110 | if (!isset($_SESSION[$name])) return $default; |
2 | daniel-mar | 111 | return self::decrypt($_SESSION[$name], $this->secret); |
112 | } |
||
113 | |||
569 | daniel-mar | 114 | public function exists($name) { |
115 | if (isset($this->cacheSetValues[$name])) return true; |
||
116 | |||
585 | daniel-mar | 117 | if (!$this->isActive()) return false; // GDPR: Only start a session when we really need one |
569 | daniel-mar | 118 | $this->sessionSafeStart(); |
119 | OIDplus::cookieUtils()->setcookie(session_name(),session_id(),time()+$this->sessionLifetime); |
||
120 | |||
121 | if (!isset($_SESSION[$name])) return false; |
||
122 | } |
||
123 | |||
124 | public function delete($name) { |
||
125 | if (isset($this->cacheSetValues[$name])) unset($this->cacheSetValues[$name]); |
||
126 | |||
585 | daniel-mar | 127 | if (!$this->isActive()) return; // GDPR: Only start a session when we really need one |
569 | daniel-mar | 128 | $this->sessionSafeStart(); |
129 | OIDplus::cookieUtils()->setcookie(session_name(),session_id(),time()+$this->sessionLifetime); |
||
130 | |||
131 | unset($_SESSION[$name]); |
||
132 | } |
||
133 | |||
85 | daniel-mar | 134 | public function destroySession() { |
585 | daniel-mar | 135 | if (!$this->isActive()) return; |
85 | daniel-mar | 136 | |
86 | daniel-mar | 137 | $this->sessionSafeStart(); |
557 | daniel-mar | 138 | OIDplus::cookieUtils()->setcookie(session_name(),session_id(),time()+$this->sessionLifetime); |
85 | daniel-mar | 139 | |
140 | $_SESSION = array(); |
||
141 | session_destroy(); |
||
142 | session_write_close(); |
||
557 | daniel-mar | 143 | OIDplus::cookieUtils()->unsetcookie(session_name()); // remove cookie, so GDPR people are happy |
85 | daniel-mar | 144 | } |
145 | |||
585 | daniel-mar | 146 | public function isActive() { |
147 | return isset($_COOKIE[session_name()]); |
||
148 | } |
||
149 | |||
2 | daniel-mar | 150 | protected static function encrypt($data, $key) { |
465 | daniel-mar | 151 | if (function_exists('openssl_encrypt')) { |
152 | $iv = random_bytes(16); // AES block size in CBC mode |
||
153 | // Encryption |
||
154 | $ciphertext = openssl_encrypt( |
||
155 | $data, |
||
156 | 'AES-256-CBC', |
||
826 | daniel-mar | 157 | hash_pbkdf2('sha512', $key, '', 10000, 32/*256bit*/, true), |
465 | daniel-mar | 158 | OPENSSL_RAW_DATA, |
159 | $iv |
||
160 | ); |
||
161 | // Authentication |
||
711 | daniel-mar | 162 | $hmac = sha3_512_hmac($iv . $ciphertext, $key, true); |
465 | daniel-mar | 163 | return $hmac . $iv . $ciphertext; |
164 | } else { |
||
165 | // When OpenSSL is not available, then we just do a HMAC |
||
711 | daniel-mar | 166 | $hmac = sha3_512_hmac($data, $key, true); |
465 | daniel-mar | 167 | return $hmac . $data; |
168 | } |
||
6 | daniel-mar | 169 | } |
2 | daniel-mar | 170 | |
6 | daniel-mar | 171 | protected static function decrypt($data, $key) { |
465 | daniel-mar | 172 | if (function_exists('openssl_decrypt')) { |
711 | daniel-mar | 173 | $hmac = mb_substr($data, 0, 64, '8bit'); |
174 | $iv = mb_substr($data, 64, 16, '8bit'); |
||
175 | $ciphertext = mb_substr($data, 80, null, '8bit'); |
||
465 | daniel-mar | 176 | // Authentication |
711 | daniel-mar | 177 | $hmacNew = sha3_512_hmac($iv . $ciphertext, $key, true); |
465 | daniel-mar | 178 | if (!hash_equals($hmac, $hmacNew)) { |
179 | throw new OIDplusException(_L('Authentication failed')); |
||
180 | } |
||
181 | // Decryption |
||
711 | daniel-mar | 182 | $cleartext = openssl_decrypt( |
465 | daniel-mar | 183 | $ciphertext, |
184 | 'AES-256-CBC', |
||
826 | daniel-mar | 185 | hash_pbkdf2('sha512', $key, '', 10000, 32/*256bit*/, true), |
465 | daniel-mar | 186 | OPENSSL_RAW_DATA, |
187 | $iv |
||
188 | ); |
||
711 | daniel-mar | 189 | if ($cleartext === false) { |
190 | throw new OIDplusException(_L('Decryption failed')); |
||
191 | } |
||
192 | return $cleartext; |
||
465 | daniel-mar | 193 | } else { |
194 | // When OpenSSL is not available, then we just do a HMAC |
||
711 | daniel-mar | 195 | $hmac = mb_substr($data, 0, 64, '8bit'); |
196 | $cleartext = mb_substr($data, 64, null, '8bit'); |
||
197 | $hmacNew = sha3_512_hmac($cleartext, $key, true); |
||
465 | daniel-mar | 198 | if (!hash_equals($hmac, $hmacNew)) { |
199 | throw new OIDplusException(_L('Authentication failed')); |
||
200 | } |
||
201 | return $cleartext; |
||
6 | daniel-mar | 202 | } |
203 | } |
||
424 | daniel-mar | 204 | } |