credential( "lookup:" . $vault . ":" . $kind . ":" . $normal, ); } public static function resolve( $crypto, $vault, $rows, $dial, $phrase, $now, $password = null, ) { $input = [ "dial" => $dial, "phrase" => $phrase, "password" => Policy::password($password), ]; $lookups = []; $candidates = []; foreach ($input as $kind => $value) { $lookups[$kind] = self::lookup( $crypto, $vault->id, $kind, $value ?? "invalid", ); } foreach ($rows as $row) { if ((int) $row->vault !== (int) $vault->id) { continue; } $match = []; foreach ($input as $kind => $value) { $hash = $row->{$kind . "_hash"} ?? ""; $lookup = $row->{$kind . "_lookup"} ?? ""; $shape = $kind === "password" || (int) $row->{$kind . "_count"} === (int) $vault->{$kind . "_count"}; $match[$kind] = $value !== null && $shape && $hash && ($lookup ? hash_equals($lookup, $lookups[$kind]) : password_verify( $crypto->credential($kind . ":" . $value), $hash, )); } if ( Policy::matches( $vault->mode, $match["dial"], $match["phrase"], $match["password"], ) ) { $candidates[] = [$row, $match]; } } // Credentials from different people never combine. Ambiguous identities fail closed. $chosen = count($candidates) === 1 ? $candidates[0] : null; // The same three slow checks run on indexed success, partial matches and failures. $dummy = '$2y$12$abcdefghijklmnopqrstuuG.nMbFmFWk2ygMYflIJ0U8fJDyLsXa'; $verified = []; foreach ($input as $kind => $value) { $hash = $chosen ? $chosen[0]->{$kind . "_hash"} ?? "" : ""; $slow = password_verify( $crypto->credential($kind . ":" . ($value ?? "invalid")), $hash ?: $dummy, ); $verified[$kind] = $chosen && $chosen[1][$kind] && $slow; } if ( !$chosen || (int) $chosen[0]->revoked || (int) $chosen[0]->expires <= $now || !Policy::matches( $vault->mode, $verified["dial"], $verified["phrase"], $verified["password"], ) ) { return null; } return ["client" => $chosen[0]] + $verified; } }