Projet

Général

Profil

Télécharger (1,66 ko) Statistiques
| Branche: | Tag: | Révision:

univnautes / etc / inc / cram_md5_sasl_client.inc @ c650b2f7

1
<?php
2
/*
3
 * cram_md5_sasl_client.php
4
 *
5
 * @(#) $Id: cram_md5_sasl_client.php,v 1.3 2004/11/17 08:00:37 mlemos Exp $
6
 *
7
 */
8

    
9
define("SASL_CRAM_MD5_STATE_START",             0);
10
define("SASL_CRAM_MD5_STATE_RESPOND_CHALLENGE", 1);
11
define("SASL_CRAM_MD5_STATE_DONE",              2);
12

    
13
class cram_md5_sasl_client_class
14
{
15
	var $credentials=array();
16
	var $state=SASL_CRAM_MD5_STATE_START;
17

    
18
	Function Initialize(&$client)
19
	{
20
		return(1);
21
	}
22

    
23
	Function HMACMD5($key,$text)
24
	{
25
		$key=(strlen($key)<64 ? str_pad($key,64,"\0") : substr($key,0,64));
26
		return(md5((str_repeat("\x5c", 64)^$key).pack("H32", md5((str_repeat("\x36", 64)^$key).$text))));
27
	}
28

    
29
	Function Start(&$client, &$message, &$interactions)
30
	{
31
		if($this->state!=SASL_CRAM_MD5_STATE_START)
32
		{
33
			$client->error="CRAM-MD5 authentication state is not at the start";
34
			return(SASL_FAIL);
35
		}
36
		$this->credentials=array(
37
			"user"=>"",
38
			"password"=>""
39
		);
40
		$defaults=array();
41
		$status=$client->GetCredentials($this->credentials,$defaults,$interactions);
42
		if($status==SASL_CONTINUE)
43
			$this->state=SASL_CRAM_MD5_STATE_RESPOND_CHALLENGE;
44
		Unset($message);
45
		return($status);
46
	}
47

    
48
	Function Step(&$client, $response, &$message, &$interactions)
49
	{
50
		switch($this->state)
51
		{
52
			case SASL_CRAM_MD5_STATE_RESPOND_CHALLENGE:
53
				$message=$this->credentials["user"]." ".$this->HMACMD5($this->credentials["password"], $response);
54
				$this->state=SASL_CRAM_MD5_STATE_DONE;
55
				break;
56
			case SASL_CRAM_MD5_STATE_DONE:
57
				$client->error="CRAM-MD5 authentication was finished without success";
58
				return(SASL_FAIL);
59
			default:
60
				$client->error="invalid CRAM-MD5 authentication step state";
61
				return(SASL_FAIL);
62
		}
63
		return(SASL_CONTINUE);
64
	}
65
};
66

    
67
?>
(14-14/68)