Subversion Repositories oidplus

Rev

Rev 563 | Go to most recent revision | Blame | Last modification | View Log | RSS feed

  1. <?php
  2.  
  3. /*
  4.  * OIDplus 2.0
  5.  * Copyright 2019 - 2021 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. if (!defined('INSIDE_OIDPLUS')) die();
  21.  
  22. class OIDplusSessionHandler {
  23.  
  24.         private $secret = '';
  25.         protected $sessionLifetime = '';
  26.  
  27.         public function __construct() {
  28.                 $this->sessionLifetime = OIDplus::baseConfig()->getValue('SESSION_LIFETIME', 30*60);
  29.                 $this->secret = OIDplus::baseConfig()->getValue('SERVER_SECRET');
  30.  
  31.                 // **PREVENTING SESSION HIJACKING**
  32.                 // Prevents javascript XSS attacks aimed to steal the session ID
  33.                 @ini_set('session.cookie_httponly', 1);
  34.  
  35.                 // **PREVENTING SESSION FIXATION**
  36.                 // Session ID cannot be passed through URLs
  37.                 @ini_set('session.use_only_cookies', 1);
  38.  
  39.                 @ini_set('session.use_trans_sid', 0);
  40.  
  41.                 // Uses a secure connection (HTTPS) if possible
  42.                 @ini_set('session.cookie_secure', OIDplus::isSslAvailable());
  43.  
  44.                 $path = OIDplus::webpath(null,true);
  45.                 if (empty($path)) $path = '/';
  46.                 @ini_set('session.cookie_path', $path);
  47.  
  48.                 @ini_set('session.cookie_samesite', OIDplus::baseConfig()->getValue('COOKIE_SAMESITE_POLICY', 'Strict'));
  49.  
  50.                 @ini_set('session.use_strict_mode', 1);
  51.  
  52.                 @ini_set('session.gc_maxlifetime', $this->sessionLifetime);
  53.         }
  54.  
  55.         protected function sessionSafeStart() {
  56.                 if (!isset($_SESSION)) {
  57.                         // TODO: session_name() makes some problems. Leave it away for now.
  58.                         //session_name('OIDplus_SESHDLR');
  59.                         if (!session_start()) {
  60.                                 throw new OIDplusException(_L('Session could not be started'));
  61.                         }
  62.                 }
  63.  
  64.                 if (!isset($_SESSION['ip'])) {
  65.                         if (!isset($_SERVER['REMOTE_ADDR'])) return;
  66.  
  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.  
  73.                                 // We don't use $this->destroySession(), because this calls sessionSafeStart() again
  74.                                 $_SESSION = array();
  75.                                 session_destroy();
  76.                                 session_write_close();
  77.                                 OIDplus::cookieUtils()->unsetcookie(session_name()); // remove cookie, so GDPR people are happy
  78.                         }
  79.                 }
  80.         }
  81.  
  82.         function __destruct() {
  83.                 session_write_close();
  84.         }
  85.  
  86.         private $cacheSetValues = array(); // Important if you do a setValue() followed by an getValue()
  87.  
  88.         public function setValue($name, $value) {
  89.                 $this->cacheSetValues[$name] = self::encrypt($value, $this->secret);
  90.  
  91.                 $this->sessionSafeStart();
  92.                 OIDplus::cookieUtils()->setcookie(session_name(),session_id(),time()+$this->sessionLifetime);
  93.  
  94.                 $_SESSION[$name] = self::encrypt($value, $this->secret);
  95.         }
  96.  
  97.         public function getValue($name) {
  98.                 if (isset($this->cacheSetValues[$name])) return self::decrypt($this->cacheSetValues[$name], $this->secret);
  99.  
  100.                 if (!isset($_COOKIE[session_name()])) return null; // GDPR: Only start a session when we really need one
  101.                 $this->sessionSafeStart();
  102.                 OIDplus::cookieUtils()->setcookie(session_name(),session_id(),time()+$this->sessionLifetime);
  103.  
  104.                 if (!isset($_SESSION[$name])) return null;
  105.                 return self::decrypt($_SESSION[$name], $this->secret);
  106.         }
  107.  
  108.         public function destroySession() {
  109.                 if (!isset($_COOKIE[session_name()])) return;
  110.  
  111.                 $this->sessionSafeStart();
  112.                 OIDplus::cookieUtils()->setcookie(session_name(),session_id(),time()+$this->sessionLifetime);
  113.  
  114.                 $_SESSION = array();
  115.                 session_destroy();
  116.                 session_write_close();
  117.                 OIDplus::cookieUtils()->unsetcookie(session_name()); // remove cookie, so GDPR people are happy
  118.         }
  119.  
  120.         public function exists($name) {
  121.                 return isset($_SESSION[$name]);
  122.         }
  123.  
  124.         protected static function encrypt($data, $key) {
  125.                 if (function_exists('openssl_encrypt')) {
  126.                         $iv = random_bytes(16); // AES block size in CBC mode
  127.                         // Encryption
  128.                         $ciphertext = openssl_encrypt(
  129.                                 $data,
  130.                                 'AES-256-CBC',
  131.                                 mb_substr($key, 0, 32, '8bit'),
  132.                                 OPENSSL_RAW_DATA,
  133.                                 $iv
  134.                         );
  135.                         // Authentication
  136.                         $hmac = hash_hmac(
  137.                                 'SHA256',
  138.                                 $iv . $ciphertext,
  139.                                 mb_substr($key, 32, null, '8bit'),
  140.                                 true
  141.                         );
  142.                         return $hmac . $iv . $ciphertext;
  143.                 } else {
  144.                         // When OpenSSL is not available, then we just do a HMAC
  145.                         $hmac = hash_hmac(
  146.                                 'SHA256',
  147.                                 $data,
  148.                                 mb_substr($key, 32, null, '8bit'),
  149.                                 true
  150.                         );
  151.                         return $hmac . $data;
  152.                 }
  153.         }
  154.  
  155.         protected static function decrypt($data, $key) {
  156.                 if (function_exists('openssl_decrypt')) {
  157.                         $hmac       = mb_substr($data, 0, 32, '8bit');
  158.                         $iv         = mb_substr($data, 32, 16, '8bit');
  159.                         $ciphertext = mb_substr($data, 48, null, '8bit');
  160.                         // Authentication
  161.                         $hmacNew = hash_hmac(
  162.                                 'SHA256',
  163.                                 $iv . $ciphertext,
  164.                                 mb_substr($key, 32, null, '8bit'),
  165.                                 true
  166.                         );
  167.                         if (!hash_equals($hmac, $hmacNew)) {
  168.                                 throw new OIDplusException(_L('Authentication failed'));
  169.                         }
  170.                         // Decryption
  171.                         return openssl_decrypt(
  172.                                 $ciphertext,
  173.                                 'AES-256-CBC',
  174.                                 mb_substr($key, 0, 32, '8bit'),
  175.                                 OPENSSL_RAW_DATA,
  176.                                 $iv
  177.                         );
  178.                 } else {
  179.                         // When OpenSSL is not available, then we just do a HMAC
  180.                         $hmac       = mb_substr($data, 0, 32, '8bit');
  181.                         $cleartext  = mb_substr($data, 32, null, '8bit');
  182.                         $hmacNew = hash_hmac(
  183.                                 'SHA256',
  184.                                 $cleartext,
  185.                                 mb_substr($key, 32, null, '8bit'),
  186.                                 true
  187.                         );
  188.                         if (!hash_equals($hmac, $hmacNew)) {
  189.                                 throw new OIDplusException(_L('Authentication failed'));
  190.                         }
  191.                         return $cleartext;
  192.                 }
  193.         }
  194. }
  195.