Subversion Repositories oidplus

Rev

Rev 426 | 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!
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) {
6 daniel-mar 127
                $iv = random_bytes(16); // AES block size in CBC mode
128
                // Encryption
129
                $ciphertext = openssl_encrypt(
130
                        $data,
131
                        'AES-256-CBC',
132
                        mb_substr($key, 0, 32, '8bit'),
133
                        OPENSSL_RAW_DATA,
134
                        $iv
135
                );
136
                // Authentication
137
                $hmac = hash_hmac(
138
                        'SHA256',
139
                        $iv . $ciphertext,
140
                        mb_substr($key, 32, null, '8bit'),
141
                        true
142
                );
143
                return $hmac . $iv . $ciphertext;
144
        }
2 daniel-mar 145
 
6 daniel-mar 146
        protected static function decrypt($data, $key) {
147
                $hmac       = mb_substr($data, 0, 32, '8bit');
148
                $iv         = mb_substr($data, 32, 16, '8bit');
149
                $ciphertext = mb_substr($data, 48, null, '8bit');
150
                // Authentication
151
                $hmacNew = hash_hmac(
152
                        'SHA256',
153
                        $iv . $ciphertext,
154
                        mb_substr($key, 32, null, '8bit'),
155
                        true
156
                );
157
                if (!hash_equals($hmac, $hmacNew)) {
360 daniel-mar 158
                        throw new OIDplusException(_L('Authentication failed'));
6 daniel-mar 159
                }
160
                // Decryption
161
                return openssl_decrypt(
162
                        $ciphertext,
163
                        'AES-256-CBC',
164
                        mb_substr($key, 0, 32, '8bit'),
165
                        OPENSSL_RAW_DATA,
166
                        $iv
167
                );
168
        }
424 daniel-mar 169
}