Subversion Repositories oidplus

Rev

Rev 83 | 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
 
56
        function __destruct() {
57
                session_write_close();
58
        }
59
 
60
        public function setValue($name, $value) {
85 daniel-mar 61
                @session_start();
62
                setcookie(session_name(),session_id(),time()+SESSION_LIFETIME, ini_get('session.cookie_path'));
63
 
2 daniel-mar 64
                $_SESSION[$name] = self::encrypt($value, $this->secret);
65
        }
66
 
67
        public function getValue($name) {
85 daniel-mar 68
                if (!isset($_COOKIE[session_name()])) return null; // GDPR: Only start a session when we really need one
69
 
70
                @session_start();
71
                setcookie(session_name(),session_id(),time()+SESSION_LIFETIME, ini_get('session.cookie_path'));
72
 
2 daniel-mar 73
                if (!isset($_SESSION[$name])) return null;
74
                return self::decrypt($_SESSION[$name], $this->secret);
75
        }
76
 
85 daniel-mar 77
        public function destroySession() {
78
                if (!isset($_COOKIE[session_name()])) return;
79
 
80
                @session_start();
81
                setcookie(session_name(),session_id(),time()+SESSION_LIFETIME, ini_get('session.cookie_path'));
82
 
83
                $_SESSION = array();
84
                session_destroy();
85
                session_write_close();
86
                setcookie(session_name(), "", time()-3600, ini_get('session.cookie_path')); // remove cookie, so GDPR people are happy
87
        }
88
 
83 daniel-mar 89
        public function exists($name) {
90
                return isset($_SESSION[$name]);
91
        }
92
 
2 daniel-mar 93
        protected static function encrypt($data, $key) {
6 daniel-mar 94
                $iv = random_bytes(16); // AES block size in CBC mode
95
                // Encryption
96
                $ciphertext = openssl_encrypt(
97
                        $data,
98
                        'AES-256-CBC',
99
                        mb_substr($key, 0, 32, '8bit'),
100
                        OPENSSL_RAW_DATA,
101
                        $iv
102
                );
103
                // Authentication
104
                $hmac = hash_hmac(
105
                        'SHA256',
106
                        $iv . $ciphertext,
107
                        mb_substr($key, 32, null, '8bit'),
108
                        true
109
                );
110
                return $hmac . $iv . $ciphertext;
111
        }
2 daniel-mar 112
 
6 daniel-mar 113
        protected static function decrypt($data, $key) {
114
                $hmac       = mb_substr($data, 0, 32, '8bit');
115
                $iv         = mb_substr($data, 32, 16, '8bit');
116
                $ciphertext = mb_substr($data, 48, null, '8bit');
117
                // Authentication
118
                $hmacNew = hash_hmac(
119
                        'SHA256',
120
                        $iv . $ciphertext,
121
                        mb_substr($key, 32, null, '8bit'),
122
                        true
123
                );
124
                if (!hash_equals($hmac, $hmacNew)) {
125
                        throw new Exception('Authentication failed');
126
                }
127
                // Decryption
128
                return openssl_decrypt(
129
                        $ciphertext,
130
                        'AES-256-CBC',
131
                        mb_substr($key, 0, 32, '8bit'),
132
                        OPENSSL_RAW_DATA,
133
                        $iv
134
                );
135
        }
2 daniel-mar 136
}