Subversion Repositories oidplus

Rev

Rev 465 | 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
 
496 daniel-mar 43
                $path = OIDplus::webpath(null,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!
435 daniel-mar 72
 
73
                                // We don't use $this->destroySession(), because this calls sessionSafeStart() again
74
                                $_SESSION = array();
75
                                session_destroy();
76
                                session_write_close();
77
                                setcookie(session_name(), "", time()-3600, ini_get('session.cookie_path')); // remove cookie, so GDPR people are happy
87 daniel-mar 78
                        }
86 daniel-mar 79
                }
80
        }
81
 
2 daniel-mar 82
        function __destruct() {
83
                session_write_close();
84
        }
85
 
424 daniel-mar 86
        private $cacheSetValues = array(); // Important if you do a setValue() followed by an getValue()
87
 
2 daniel-mar 88
        public function setValue($name, $value) {
426 daniel-mar 89
                $this->cacheSetValues[$name] = self::encrypt($value, $this->secret);
90
                if ($this->simulate) return;
91
 
86 daniel-mar 92
                $this->sessionSafeStart();
261 daniel-mar 93
                setcookie(session_name(),session_id(),time()+$this->sessionLifetime, ini_get('session.cookie_path'));
85 daniel-mar 94
 
2 daniel-mar 95
                $_SESSION[$name] = self::encrypt($value, $this->secret);
96
        }
97
 
98
        public function getValue($name) {
424 daniel-mar 99
                if (isset($this->cacheSetValues[$name])) return self::decrypt($this->cacheSetValues[$name], $this->secret);
426 daniel-mar 100
                if ($this->simulate) return null;
424 daniel-mar 101
 
85 daniel-mar 102
                if (!isset($_COOKIE[session_name()])) return null; // GDPR: Only start a session when we really need one
86 daniel-mar 103
                $this->sessionSafeStart();
261 daniel-mar 104
                setcookie(session_name(),session_id(),time()+$this->sessionLifetime, ini_get('session.cookie_path'));
85 daniel-mar 105
 
2 daniel-mar 106
                if (!isset($_SESSION[$name])) return null;
107
                return self::decrypt($_SESSION[$name], $this->secret);
108
        }
109
 
85 daniel-mar 110
        public function destroySession() {
111
                if (!isset($_COOKIE[session_name()])) return;
112
 
86 daniel-mar 113
                $this->sessionSafeStart();
261 daniel-mar 114
                setcookie(session_name(),session_id(),time()+$this->sessionLifetime, ini_get('session.cookie_path'));
85 daniel-mar 115
 
116
                $_SESSION = array();
117
                session_destroy();
118
                session_write_close();
119
                setcookie(session_name(), "", time()-3600, ini_get('session.cookie_path')); // remove cookie, so GDPR people are happy
120
        }
121
 
83 daniel-mar 122
        public function exists($name) {
123
                return isset($_SESSION[$name]);
124
        }
125
 
2 daniel-mar 126
        protected static function encrypt($data, $key) {
465 daniel-mar 127
                if (function_exists('openssl_encrypt')) {
128
                        $iv = random_bytes(16); // AES block size in CBC mode
129
                        // Encryption
130
                        $ciphertext = openssl_encrypt(
131
                                $data,
132
                                'AES-256-CBC',
133
                                mb_substr($key, 0, 32, '8bit'),
134
                                OPENSSL_RAW_DATA,
135
                                $iv
136
                        );
137
                        // Authentication
138
                        $hmac = hash_hmac(
139
                                'SHA256',
140
                                $iv . $ciphertext,
141
                                mb_substr($key, 32, null, '8bit'),
142
                                true
143
                        );
144
                        return $hmac . $iv . $ciphertext;
145
                } else {
146
                        // When OpenSSL is not available, then we just do a HMAC
147
                        $hmac = hash_hmac(
148
                                'SHA256',
149
                                $data,
150
                                mb_substr($key, 32, null, '8bit'),
151
                                true
152
                        );
153
                        return $hmac . $data;
154
                }
6 daniel-mar 155
        }
2 daniel-mar 156
 
6 daniel-mar 157
        protected static function decrypt($data, $key) {
465 daniel-mar 158
                if (function_exists('openssl_decrypt')) {
159
                        $hmac       = mb_substr($data, 0, 32, '8bit');
160
                        $iv         = mb_substr($data, 32, 16, '8bit');
161
                        $ciphertext = mb_substr($data, 48, null, '8bit');
162
                        // Authentication
163
                        $hmacNew = hash_hmac(
164
                                'SHA256',
165
                                $iv . $ciphertext,
166
                                mb_substr($key, 32, null, '8bit'),
167
                                true
168
                        );
169
                        if (!hash_equals($hmac, $hmacNew)) {
170
                                throw new OIDplusException(_L('Authentication failed'));
171
                        }
172
                        // Decryption
173
                        return openssl_decrypt(
174
                                $ciphertext,
175
                                'AES-256-CBC',
176
                                mb_substr($key, 0, 32, '8bit'),
177
                                OPENSSL_RAW_DATA,
178
                                $iv
179
                        );
180
                } else {
181
                        // When OpenSSL is not available, then we just do a HMAC
182
                        $hmac       = mb_substr($data, 0, 32, '8bit');
183
                        $cleartext  = mb_substr($data, 32, null, '8bit');
184
                        $hmacNew = hash_hmac(
185
                                'SHA256',
186
                                $cleartext,
187
                                mb_substr($key, 32, null, '8bit'),
188
                                true
189
                        );
190
                        if (!hash_equals($hmac, $hmacNew)) {
191
                                throw new OIDplusException(_L('Authentication failed'));
192
                        }
193
                        return $cleartext;
6 daniel-mar 194
                }
195
        }
424 daniel-mar 196
}