Subversion Repositories oidplus

Rev

Rev 1099 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

  1. <?php
  2.  
  3. /*
  4.  * OIDplus 2.0
  5.  * Copyright 2019 - 2023 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. namespace ViaThinkSoft\OIDplus;
  21.  
  22. // phpcs:disable PSR1.Files.SideEffects
  23. \defined('INSIDE_OIDPLUS') or die;
  24. // phpcs:enable PSR1.Files.SideEffects
  25.  
  26. class OIDplusAuthPluginArgon2 extends OIDplusAuthPlugin {
  27.  
  28.         /**
  29.          * @param bool $html
  30.          * @return void
  31.          */
  32.         public function init(bool $html=true) {
  33.                 // TODO: Let the admin decide about the memory, iterations, and parallelism options
  34.         }
  35.  
  36.         /**
  37.          * @param string $authKey
  38.          * @return bool
  39.          */
  40.         private function supportedCryptAlgo(string $authKey): bool {
  41.                 return str_starts_with($authKey, '$argon2i$') ||
  42.                        str_starts_with($authKey, '$argon2id$');
  43.         }
  44.  
  45.         /**
  46.          * @param OIDplusRAAuthInfo $authInfo
  47.          * @param string $check_password
  48.          * @return bool
  49.          */
  50.         public function verify(OIDplusRAAuthInfo $authInfo, string $check_password): bool {
  51.                 $authKey = $authInfo->getAuthKey();
  52.  
  53.                 if (!$this->supportedCryptAlgo($authKey)) {
  54.                         // Unsupported algorithm
  55.                         return false;
  56.                 }
  57.  
  58.                 // $argon2i$v=19$m=1024,t=2,p=2$MEhSZkJLQXUxRzljNE5hMw$33pvelMsxqOn/1VV2pnjmKJUECBhilzOZ2+Gq/FxCP4
  59.                 //  \_____/ \__/ \____________/ \____________________/ \_________________________________________/
  60.                 //   Algo   Vers  Cost options   Salt                   Hash
  61.  
  62.                 return password_verify($check_password, $authKey);
  63.         }
  64.  
  65.         /**
  66.          * @return string|int|false
  67.          */
  68.         private function getBestHashAlgo() { /* @phpstan-ignore-line */
  69.                 if ($this->supportsArgon2id()) {
  70.                         $hashalgo = PASSWORD_ARGON2ID;
  71.                 } else if ($this->supportsArgon2i()) {
  72.                         $hashalgo = PASSWORD_ARGON2I;
  73.                 } else {
  74.                         $hashalgo = false;
  75.                 }
  76.                 return $hashalgo;
  77.         }
  78.  
  79.         /**
  80.          * @param string $password
  81.          * @return OIDplusRAAuthInfo
  82.          * @throws OIDplusException
  83.          */
  84.         public function generate(string $password): OIDplusRAAuthInfo {
  85.                 $hashalgo = $this->getBestHashAlgo();
  86.                 assert($hashalgo !== false); // Should not happen if we called available() before!
  87.                 $calc_authkey = password_hash($password, $hashalgo);
  88.                 if (!$calc_authkey) throw new OIDplusException(_L('Error creating password hash'));
  89.                 assert($this->supportedCryptAlgo($calc_authkey));
  90.                 return new OIDplusRAAuthInfo($calc_authkey);
  91.         }
  92.  
  93.         /**
  94.          * @return bool
  95.          */
  96.         private function supportsArgon2i(): bool {
  97.                 if (version_compare(PHP_VERSION, '7.4.0') >= 0) {
  98.                         return in_array('argon2i', password_algos());
  99.                 } else {
  100.                         return defined('PASSWORD_ARGON2I');
  101.                 }
  102.         }
  103.  
  104.         /**
  105.          * @return bool
  106.          */
  107.         private function supportsArgon2id(): bool {
  108.                 if (version_compare(PHP_VERSION, '7.4.0') >= 0) {
  109.                         return in_array('argon2id', password_algos());
  110.                 } else {
  111.                         return defined('PASSWORD_ARGON2ID');
  112.                 }
  113.         }
  114.  
  115.         /**
  116.          * @param string $reason
  117.          * @return bool
  118.          */
  119.         public function availableForHash(string &$reason): bool {
  120.                 if (!$this->supportsArgon2i() && !$this->supportsArgon2id()) {
  121.                         $reason = _L('No fitting hash algorithm found');
  122.                         return false;
  123.                 } else {
  124.                         return true;
  125.                 }
  126.         }
  127.  
  128.         /**
  129.          * @param string $reason
  130.          * @return bool
  131.          */
  132.         public function availableForVerify(string &$reason): bool {
  133.                 return $this->availableForHash($reason);
  134.         }
  135.  
  136. }
  137.