actually use the on-disk site-specific nonce to 'pepper' passwords before hashing...
[authserver.git] / authutils.php-class
CommitLineData
d46a42f1
RK
1<?php
2/* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
4 * You can obtain one at http://mozilla.org/MPL/2.0/. */
5
6class AuthUtils {
7 // KaiRo.at authentication utilities PHP class
8 // This class contains helper functions for the authentication system.
9 //
ac442755
RK
10 // function __construct()
11 // CONSTRUCTOR
be1082a6 12 //
ac442755
RK
13 // private $pwd_cost
14 // The cost parameter for use with PHP password_hash function.
15 //
16 // private $pwd_nonces
17 // The array of nonces to use for "peppering" passwords. For new hashes, the last one of those will be used.
087085d6 18 // Generate a nonce with this command: |openssl rand -base64 48|
ac442755
RK
19 //
20 // function checkPasswordConstraints($new_password, $user_email)
d46a42f1
RK
21 // Check password constraints and return an array of error messages (empty if all constraints are met).
22 //
ac442755 23 // function createSessionKey()
d46a42f1
RK
24 // Return a random session key.
25 //
ac442755 26 // function createVerificationCode()
d46a42f1
RK
27 // Return a random acount/email verification code.
28 //
ac442755 29 // function createTimeCode($session, [$offset], [$validity_minutes])
d46a42f1
RK
30 // Return a time-based code based on the key and ID of the given session.
31 // An offset can be given to create a specific code for verification, otherwise and offset will be generated.
32 // Also, an amount of minutes for the code to stay valid can be handed over, by default 10 minutes will be used.
33 //
ac442755 34 // function verifyTimeCode($timecode_to_verify, $session, [$validity_minutes])
d46a42f1
RK
35 // Verify a given time-based code and return true if it's valid or false if it's not.
36 // See createTimeCode() documentation for the session and validity paramerters.
be1082a6 37 //
ac442755 38 // function pwdHash($new_password)
be1082a6
RK
39 // Return a hash for the given password.
40 //
ac442755 41 // function pwdVerify($password_to_verify, $user)
be1082a6
RK
42 // Return true if the password verifies against the pwdhash field of the user, false if not.
43 //
ac442755 44 // function pwdNeedsRehash($user)
be1082a6
RK
45 // Return true if the pwdhash field of the user uses an outdated standard and needs to be rehashed.
46
ac442755
RK
47 function __construct($settings) {
48 // *** constructor ***
49 if (array_key_exists('pwd_nonces', $settings)) {
50 $this->pwd_nonces = $settings['pwd_nonces'];
51 }
52 if (array_key_exists('pwd_cost', $settings)) {
53 $this->pwd_cost = $settings['pwd_cost'];
54 }
55 }
56
57 private $pwd_cost = 10;
58 private $pwd_nonces = array();
d46a42f1 59
ac442755 60 function checkPasswordConstraints($new_password, $user_email) {
d46a42f1
RK
61 $errors = array();
62 if ($new_password != trim($new_password)) {
63 $errors[] = _('Password must not start or end with a whitespace character like a space.');
64 }
65 if (strlen($new_password) < 8) { $errors[] = sprintf(_('Password too short (min. %s characters).'), 8); }
66 if (strlen($new_password) > 70) { $errors[] = sprintf(_('Password too long (max. %s characters).'), 70); }
67 if ((strtolower($new_password) == strtolower($user_email)) ||
68 in_array(strtolower($new_password), preg_split("/[@\.]+/", strtolower($user_email)))) {
69 $errors[] = _('The passwort can not be equal to your email or any part of it.');
70 }
71 if ((strlen($new_password) < 15) && (preg_match('/^[a-zA-Z]+$/', $new_password))) {
72 $errors[] = sprintf(_('Your password must use characters other than normal letters or contain least %s characters.'), 15);
73 }
74 if (preg_match('/^\d+$/', $new_password)) {
75 $errors[] = sprintf(_('Your password cannot consist only of numbers.'), 15);
76 }
77 if (strlen(count_chars($new_password, 3)) < 5) {
78 $errors[] = sprintf(_('Password does have to contain at least %s different characters.'), 5);
79 }
80 return $errors;
81 }
82
ac442755 83 function createSessionKey() {
d46a42f1
RK
84 return bin2hex(openssl_random_pseudo_bytes(512 / 8)); // Get 512 bits of randomness (128 byte hex string).
85 }
86
ac442755 87 function createVerificationCode() {
d46a42f1
RK
88 return bin2hex(openssl_random_pseudo_bytes(512 / 8)); // Get 512 bits of randomness (128 byte hex string).
89 }
90
ac442755 91 function createTimeCode($session, $offset = null, $validity_minutes = 10) {
d46a42f1
RK
92 // Matches TOTP algorithms, see https://en.wikipedia.org/wiki/Time-based_One-time_Password_Algorithm
93 $valid_seconds = intval($validity_minutes) * 60;
94 if ($valid_seconds < 60) { $valid_seconds = 60; }
95 $code_digits = 8;
96 $time = time();
97 $rest = is_null($offset)?($time % $valid_seconds):intval($offset); // T0, will be sent as part of code to make it valid for the full duration.
98 $counter = floor(($time - $rest) / $valid_seconds);
99 $hmac = mhash(MHASH_SHA1, $counter, $session['id'].$session['sesskey']);
100 $offset = hexdec(substr(bin2hex(substr($hmac, -1)), -1)); // Get the last 4 bits as a number.
101 $totp = hexdec(bin2hex(substr($hmac, $offset, 4))) & 0x7FFFFFFF; // Take 4 bytes at the offset, discard highest bit.
102 $totp_value = sprintf('%0'.$code_digits.'d', substr($totp, -$code_digits));
103 return $rest.'.'.$totp_value;
104 }
105
ac442755 106 function verifyTimeCode($timecode_to_verify, $session, $validity_minutes = 10) {
d46a42f1 107 if (preg_match('/^(\d+)\.\d+$/', $timecode_to_verify, $regs)) {
ac442755 108 return ($timecode_to_verify === $this->createTimeCode($session, $regs[1], $validity_minutes));
d46a42f1
RK
109 }
110 return false;
111 }
be1082a6 112
ac442755
RK
113 function pwdHash($new_password) {
114 $hash_prefix = '';
115 if (count($this->pwd_nonces)) {
116 $new_password .= $this->pwd_nonces[count($this->pwd_nonces) - 1];
117 $hash_prefix = (count($this->pwd_nonces) - 1).'|';
118 }
119 return $hash_prefix.password_hash($new_password, PASSWORD_DEFAULT, array('cost' => $this->pwd_cost));
be1082a6
RK
120 }
121
ac442755 122 function pwdVerify($password_to_verify, $userdata) {
087085d6
RK
123 $pwdhash = $userdata['pwdhash'];
124 if (preg_match('/^(\d+)\|(.+)$/', $userdata['pwdhash'], $regs)) {
ac442755 125 $password_to_verify .= $this->pwd_nonces[$regs[1]];
087085d6 126 $pwdhash = $regs[2];
ac442755 127 }
087085d6 128 return password_verify($password_to_verify, $pwdhash);
be1082a6
RK
129 }
130
ac442755
RK
131 function pwdNeedsRehash($userdata) {
132 $nonceid = -1;
133 $pwdhash = $userdata['pwdhash'];
134 if (preg_match('/^(\d+)\|(.+)$/', $userdata['pwdhash'], $regs)) {
135 $nonceid = $regs[1];
136 $pwdhash = $regs[2];
137 }
138 if ($nonceid == count($this->pwd_nonces) - 1) {
139 return password_needs_rehash($pwdhash, PASSWORD_DEFAULT, array('cost' => $this->pwd_cost));
140 }
141 else {
142 return true;
143 }
be1082a6 144 }
d46a42f1
RK
145}
146?>