Subversion Repositories oidplus

Rev

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