Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/FreeDSx/Sasl/Challenge/CramMD5Challenge.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,12 @@ private function validateClientResponse(
Message $received,
CramMD5Options $options,
): void {
# The client sent a response without us sending a challenge...
if (!$this->context->has('challenge')) {
$this->context->setIsComplete(true);

return;
}
if (!$received->has('username')) {
throw new SaslException('The client response must have a username.');
}
Expand Down
15 changes: 15 additions & 0 deletions tests/unit/Challenge/CramMD5ChallengeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,21 @@ public function testChallengeWithFromServerWithClientCorrectResponse(): void
self::assertTrue($context->isComplete());
}

public function testChallengeFromServerWithNoIssuedChallengeDoesNotAuthenticate(): void
{
$serverChallenge = new CramMD5Challenge(true);
$validate = static fn (string $username, string $challenge): string => hash_hmac('md5', $challenge, 'bar');

// A digest computed offline over an empty challenge, sent before the server issued one.
$context = $serverChallenge->challenge(
'foo ' . hash_hmac('md5', '', 'bar'),
(new CramMD5Options())->setPasswordCallback(Closure::fromCallable($validate)),
);

self::assertFalse($context->isAuthenticated());
self::assertTrue($context->isComplete());
}

public function testPasswordCallableReceivesEncodedChallengeMatchingWhatClientUses(): void
{
$serverChallenge = new CramMD5Challenge(true);
Expand Down
28 changes: 28 additions & 0 deletions tests/unit/Challenge/DigestMD5ChallengeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use FreeDSx\Sasl\Challenge\DigestMD5Challenge;
use FreeDSx\Sasl\Encoder\DigestMD5Encoder;
use FreeDSx\Sasl\Exception\SaslException;
use FreeDSx\Sasl\Message;
use FreeDSx\Sasl\Options\DigestMD5Options;
use FreeDSx\Sasl\SaslContext;
use PHPUnit\Framework\TestCase;
Expand Down Expand Up @@ -119,6 +120,33 @@ public function testGenerateServerChallengeForClientInServerMode(): void
self::assertNotEmpty($challenge->get('nonce'), 'The nonce must be generated.');
}

public function testServerModeWithNoIssuedChallengeDoesNotAuthenticate(): void
{
$serverChallenge = new DigestMD5Challenge(true);

// A client response sent before the server issued its nonce.
$clientResponse = $this->encoder->encode(
new Message([
'username' => 'foo',
'realm' => '',
'nonce' => 'x',
'cnonce' => str_repeat('a', 14),
'nc' => '00000001',
'qop' => 'auth',
'digest-uri' => 'ldap/host',
'response' => str_repeat('a', 32),
]),
new SaslContext(),
);

$context = $serverChallenge->challenge(
$clientResponse,
(new DigestMD5Options())->setPassword('bar'),
);

self::assertFalse($context->isAuthenticated());
}

public function testGenerateServerResponseToClientResponse(): void
{
self::markTestSkipped('Test does not work on newer PHP due to deprecated ciphers it seems. Needs investigation.');
Expand Down
Loading