Rev 1090 | Go to most recent revision | Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1088 | daniel-mar | 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 | public function init($html=true) { |
||
29 | // TODO: Let the admin decide about the memory, iterations, and parallelism options |
||
30 | } |
||
31 | |||
32 | private function supportedCryptAlgo($authKey) { |
||
33 | return str_starts_with($authKey, '$argon2i$') || |
||
34 | str_starts_with($authKey, '$argon2id$'); |
||
35 | } |
||
36 | |||
37 | public function verify(OIDplusRAAuthInfo $authInfo, $check_password) { |
||
38 | $authKey = $authInfo->getAuthKey(); |
||
39 | $salt = $authInfo->getSalt(); |
||
40 | |||
41 | if (!$this->supportedCryptAlgo($authKey)) { |
||
42 | // Unsupported algorithm |
||
43 | return false; |
||
44 | } |
||
45 | |||
46 | // $argon2i$v=19$m=1024,t=2,p=2$MEhSZkJLQXUxRzljNE5hMw$33pvelMsxqOn/1VV2pnjmKJUECBhilzOZ2+Gq/FxCP4 |
||
47 | // \_____/ \__/ \____________/ \____________________/ \_________________________________________/ |
||
48 | // Algo Vers Cost options Salt Hash |
||
49 | |||
50 | if ($salt != '') { |
||
51 | throw new OIDplusException(_L('This function does not accept an explicit salt')); |
||
52 | } |
||
53 | |||
54 | return password_verify($check_password, $authKey); |
||
55 | } |
||
56 | |||
57 | private function getBestHashAlgo() { |
||
58 | if ($this->supportsArgon2id()) { |
||
59 | $hashalgo = PASSWORD_ARGON2ID; |
||
60 | } else if ($this->supportsArgon2i()) { |
||
61 | $hashalgo = PASSWORD_ARGON2I; |
||
62 | } else { |
||
63 | $hashalgo = false; |
||
64 | } |
||
65 | return $hashalgo; |
||
66 | } |
||
67 | |||
68 | public function generate($password): OIDplusRAAuthInfo { |
||
69 | $s_salt = ''; // Argon2 automatically generates a salt |
||
70 | $hashalgo = $this->getBestHashAlgo(); |
||
71 | assert($hashalgo !== false); // Should not happen if we called available() before! |
||
72 | $calc_authkey = password_hash($password, $hashalgo); |
||
73 | if (!$calc_authkey) throw new OIDplusException(_L('Error creating password hash')); |
||
74 | assert($this->supportedCryptAlgo($calc_authkey)); |
||
75 | return new OIDplusRAAuthInfo($s_salt, $calc_authkey); |
||
76 | } |
||
77 | |||
78 | private function supportsArgon2i(): bool { |
||
79 | if (version_compare(PHP_VERSION, '7.4.0') >= 0) { |
||
80 | return in_array('argon2i', password_algos()); |
||
81 | } else { |
||
82 | return defined('PASSWORD_ARGON2I'); |
||
83 | } |
||
84 | } |
||
85 | |||
86 | private function supportsArgon2id(): bool { |
||
87 | if (version_compare(PHP_VERSION, '7.4.0') >= 0) { |
||
88 | return in_array('argon2id', password_algos()); |
||
89 | } else { |
||
90 | return defined('PASSWORD_ARGON2ID'); |
||
91 | } |
||
92 | } |
||
93 | |||
94 | public function available(&$reason): bool { |
||
95 | if (!$this->supportsArgon2i() && !$this->supportsArgon2id()) { |
||
96 | $reason = _L('No fitting hash algorithm found'); |
||
97 | return false; |
||
98 | } else { |
||
99 | return true; |
||
100 | } |
||
101 | } |
||
102 | |||
103 | } |