Rev 424 | 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 |
||
5 | * Copyright 2019 Daniel Marschall, ViaThinkSoft |
||
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 | |||
20 | class OIDplusSessionHandler { |
||
21 | |||
263 | daniel-mar | 22 | private $secret = ''; |
23 | protected $sessionLifetime = ''; |
||
426 | daniel-mar | 24 | public $simulate = false; |
2 | daniel-mar | 25 | |
263 | daniel-mar | 26 | public function __construct() { |
261 | daniel-mar | 27 | $this->sessionLifetime = OIDplus::baseConfig()->getValue('SESSION_LIFETIME', 30*60); |
28 | $this->secret = OIDplus::baseConfig()->getValue('SERVER_SECRET'); |
||
29 | |||
2 | daniel-mar | 30 | // **PREVENTING SESSION HIJACKING** |
31 | // Prevents javascript XSS attacks aimed to steal the session ID |
||
87 | daniel-mar | 32 | @ini_set('session.cookie_httponly', 1); |
2 | daniel-mar | 33 | |
34 | // **PREVENTING SESSION FIXATION** |
||
35 | // Session ID cannot be passed through URLs |
||
87 | daniel-mar | 36 | @ini_set('session.use_only_cookies', 1); |
2 | daniel-mar | 37 | |
87 | daniel-mar | 38 | @ini_set('session.use_trans_sid', 0); |
85 | daniel-mar | 39 | |
2 | daniel-mar | 40 | // Uses a secure connection (HTTPS) if possible |
230 | daniel-mar | 41 | @ini_set('session.cookie_secure', OIDplus::isSslAvailable()); |
2 | daniel-mar | 42 | |
227 | daniel-mar | 43 | $path = OIDplus::getSystemUrl(true); |
83 | daniel-mar | 44 | if (!empty($path)) { |
87 | daniel-mar | 45 | @ini_set('session.cookie_path', $path); |
2 | daniel-mar | 46 | } |
47 | |||
87 | daniel-mar | 48 | @ini_set('session.cookie_samesite', 'Lax'); |
2 | daniel-mar | 49 | |
87 | daniel-mar | 50 | @ini_set('session.use_strict_mode', 1); |
2 | daniel-mar | 51 | |
261 | daniel-mar | 52 | @ini_set('session.gc_maxlifetime', $this->sessionLifetime); |
2 | daniel-mar | 53 | } |
54 | |||
86 | daniel-mar | 55 | protected function sessionSafeStart() { |
179 | daniel-mar | 56 | if (!isset($_SESSION)) { |
57 | // TODO: session_name() makes some problems. Leave it away for now. |
||
261 | daniel-mar | 58 | //session_name('OIDplus_SESHDLR'); |
179 | daniel-mar | 59 | if (!session_start()) { |
360 | daniel-mar | 60 | throw new OIDplusException(_L('Session could not be started')); |
179 | daniel-mar | 61 | } |
62 | } |
||
86 | daniel-mar | 63 | |
87 | daniel-mar | 64 | if (!isset($_SESSION['ip'])) { |
65 | if (!isset($_SERVER['REMOTE_ADDR'])) return; |
||
86 | daniel-mar | 66 | |
87 | daniel-mar | 67 | // Remember the IP address of the user |
68 | $_SESSION['ip'] = $_SERVER['REMOTE_ADDR']; |
||
69 | } else { |
||
70 | if ($_SERVER['REMOTE_ADDR'] != $_SESSION['ip']) { |
||
71 | // Was the session hijacked?! Get out of here! |
||
72 | $this->destroySession(); |
||
73 | } |
||
86 | daniel-mar | 74 | } |
75 | } |
||
76 | |||
2 | daniel-mar | 77 | function __destruct() { |
78 | session_write_close(); |
||
79 | } |
||
80 | |||
424 | daniel-mar | 81 | private $cacheSetValues = array(); // Important if you do a setValue() followed by an getValue() |
82 | |||
2 | daniel-mar | 83 | public function setValue($name, $value) { |
426 | daniel-mar | 84 | $this->cacheSetValues[$name] = self::encrypt($value, $this->secret); |
85 | if ($this->simulate) return; |
||
86 | |||
86 | daniel-mar | 87 | $this->sessionSafeStart(); |
261 | daniel-mar | 88 | setcookie(session_name(),session_id(),time()+$this->sessionLifetime, ini_get('session.cookie_path')); |
85 | daniel-mar | 89 | |
2 | daniel-mar | 90 | $_SESSION[$name] = self::encrypt($value, $this->secret); |
91 | } |
||
92 | |||
93 | public function getValue($name) { |
||
424 | daniel-mar | 94 | if (isset($this->cacheSetValues[$name])) return self::decrypt($this->cacheSetValues[$name], $this->secret); |
426 | daniel-mar | 95 | if ($this->simulate) return null; |
424 | daniel-mar | 96 | |
85 | daniel-mar | 97 | if (!isset($_COOKIE[session_name()])) return null; // GDPR: Only start a session when we really need one |
86 | daniel-mar | 98 | $this->sessionSafeStart(); |
261 | daniel-mar | 99 | setcookie(session_name(),session_id(),time()+$this->sessionLifetime, ini_get('session.cookie_path')); |
85 | daniel-mar | 100 | |
2 | daniel-mar | 101 | if (!isset($_SESSION[$name])) return null; |
102 | return self::decrypt($_SESSION[$name], $this->secret); |
||
103 | } |
||
104 | |||
85 | daniel-mar | 105 | public function destroySession() { |
106 | if (!isset($_COOKIE[session_name()])) return; |
||
107 | |||
86 | daniel-mar | 108 | $this->sessionSafeStart(); |
261 | daniel-mar | 109 | setcookie(session_name(),session_id(),time()+$this->sessionLifetime, ini_get('session.cookie_path')); |
85 | daniel-mar | 110 | |
111 | $_SESSION = array(); |
||
112 | session_destroy(); |
||
113 | session_write_close(); |
||
114 | setcookie(session_name(), "", time()-3600, ini_get('session.cookie_path')); // remove cookie, so GDPR people are happy |
||
115 | } |
||
116 | |||
83 | daniel-mar | 117 | public function exists($name) { |
118 | return isset($_SESSION[$name]); |
||
119 | } |
||
120 | |||
2 | daniel-mar | 121 | protected static function encrypt($data, $key) { |
6 | daniel-mar | 122 | $iv = random_bytes(16); // AES block size in CBC mode |
123 | // Encryption |
||
124 | $ciphertext = openssl_encrypt( |
||
125 | $data, |
||
126 | 'AES-256-CBC', |
||
127 | mb_substr($key, 0, 32, '8bit'), |
||
128 | OPENSSL_RAW_DATA, |
||
129 | $iv |
||
130 | ); |
||
131 | // Authentication |
||
132 | $hmac = hash_hmac( |
||
133 | 'SHA256', |
||
134 | $iv . $ciphertext, |
||
135 | mb_substr($key, 32, null, '8bit'), |
||
136 | true |
||
137 | ); |
||
138 | return $hmac . $iv . $ciphertext; |
||
139 | } |
||
2 | daniel-mar | 140 | |
6 | daniel-mar | 141 | protected static function decrypt($data, $key) { |
142 | $hmac = mb_substr($data, 0, 32, '8bit'); |
||
143 | $iv = mb_substr($data, 32, 16, '8bit'); |
||
144 | $ciphertext = mb_substr($data, 48, null, '8bit'); |
||
145 | // Authentication |
||
146 | $hmacNew = hash_hmac( |
||
147 | 'SHA256', |
||
148 | $iv . $ciphertext, |
||
149 | mb_substr($key, 32, null, '8bit'), |
||
150 | true |
||
151 | ); |
||
152 | if (!hash_equals($hmac, $hmacNew)) { |
||
360 | daniel-mar | 153 | throw new OIDplusException(_L('Authentication failed')); |
6 | daniel-mar | 154 | } |
155 | // Decryption |
||
156 | return openssl_decrypt( |
||
157 | $ciphertext, |
||
158 | 'AES-256-CBC', |
||
159 | mb_substr($key, 0, 32, '8bit'), |
||
160 | OPENSSL_RAW_DATA, |
||
161 | $iv |
||
162 | ); |
||
163 | } |
||
424 | daniel-mar | 164 | } |