Projet

Général

Profil

Télécharger (3,72 ko) Statistiques
| Branche: | Tag: | Révision:

univnautes / etc / inc / digest_sasl_client.inc @ 989d361e

1
<?php
2
/*
3
 * digest_sasl_client.php
4
 *
5
 * @(#) $Id: digest_sasl_client.php,v 1.1 2005/10/27 05:24:15 mlemos Exp $
6
 *
7
 */
8

    
9
define('SASL_DIGEST_STATE_START',             0);
10
define('SASL_DIGEST_STATE_RESPOND_CHALLENGE', 1);
11
define('SASL_DIGEST_STATE_DONE',              2);
12

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

    
18
	Function unq($string)
19
	{
20
		return(($string[0]=='"' && $string[strlen($string)-1]=='"') ? substr($string, 1, strlen($string)-2) : $string);
21
	}
22

    
23
	Function H($data)
24
	{
25
		return md5($data);
26
	}
27

    
28
	Function KD($secret, $data)
29
	{
30
		return $this->H($secret.':'.$data);
31
	}
32

    
33
	Function Initialize(&$client)
34
	{
35
		return(1);
36
	}
37

    
38
	Function Start(&$client, &$message, &$interactions)
39
	{
40
		if($this->state!=SASL_DIGEST_STATE_START)
41
		{
42
			$client->error='Digest authentication state is not at the start';
43
			return(SASL_FAIL);
44
		}
45
		$this->credentials=array(
46
			'user'=>'',
47
			'password'=>'',
48
			'uri'=>'',
49
			'method'=>'',
50
			'session'=>''
51
		);
52
		$defaults=array();
53
		$status=$client->GetCredentials($this->credentials,$defaults,$interactions);
54
		if($status==SASL_CONTINUE)
55
			$this->state=SASL_DIGEST_STATE_RESPOND_CHALLENGE;
56
		Unset($message);
57
		return($status);
58
	}
59

    
60
	Function Step(&$client, $response, &$message, &$interactions)
61
	{
62
		switch($this->state)
63
		{
64
			case SASL_DIGEST_STATE_RESPOND_CHALLENGE:
65
				$values=explode(',',$response);
66
				$parameters=array();
67
				for($v=0; $v<count($values); $v++)
68
					$parameters[strtok(trim($values[$v]), '=')]=strtok('');
69

    
70
				$message='username="'.$this->credentials['user'].'"';
71
				if(!IsSet($parameters[$p='realm'])
72
				&& !IsSet($parameters[$p='nonce']))
73
				{
74
					$client->error='Digest authentication parameter '.$p.' is missing from the server response';
75
					return(SASL_FAIL);
76
				}
77
				$message.=', realm='.$parameters['realm'];
78
				$message.=', nonce='.$parameters['nonce'];
79
				$message.=', uri="'.$this->credentials['uri'].'"';
80
				if(IsSet($parameters['algorithm']))
81
				{
82
					$algorithm=$this->unq($parameters['algorithm']);
83
					$message.=', algorithm='.$parameters['algorithm'];
84
				}
85
				else
86
					$algorithm='';
87

    
88
				$realm=$this->unq($parameters['realm']);
89
				$nonce=$this->unq($parameters['nonce']);
90
				if(IsSet($parameters['qop']))
91
				{
92
					switch($qop=$this->unq($parameters['qop']))
93
					{
94
						case "auth":
95
							$cnonce=$this->credentials['session'];
96
							break;
97
						default:
98
							$client->error='Digest authentication quality of protection '.$qop.' is not yet supported';
99
							return(SASL_FAIL);
100
					}
101
				}
102
				$nc_value='00000001';
103
				if(IsSet($parameters['qop'])
104
				&& !strcmp($algorithm, 'MD5-sess'))
105
					$A1=$this->H($this->credentials['user'].':'. $realm.':'. $this->credentials['password']).':'.$nonce.':'.$cnonce;
106
				else
107
					$A1=$this->credentials['user'].':'. $realm.':'. $this->credentials['password'];
108
				$A2=$this->credentials['method'].':'.$this->credentials['uri'];
109
				if(IsSet($parameters['qop']))
110
					$response=$this->KD($this->H($A1), $nonce.':'. $nc_value.':'. $cnonce.':'. $qop.':'. $this->H($A2));
111
				else
112
					$response=$this->KD($this->H($A1), $nonce.':'. $this->H($A2));
113
				$message.=', response="'.$response.'"';
114
				if(IsSet($parameters['opaque']))
115
					$message.=', opaque='.$parameters['opaque'];
116
				if(IsSet($parameters['qop']))
117
					$message.=', qop="'.$qop.'"';
118
				$message.=', nc='.$nc_value;
119
				if(IsSet($parameters['qop']))
120
					$message.=', cnonce="'.$cnonce.'"';
121
				$client->encode_response=0;
122
				$this->state=SASL_DIGEST_STATE_DONE;
123
				break;
124
			case SASL_DIGEST_STATE_DONE:
125
				$client->error='Digest authentication was finished without success';
126
				return(SASL_FAIL);
127
			default:
128
				$client->error='invalid Digest authentication step state';
129
				return(SASL_FAIL);
130
		}
131
		return(SASL_CONTINUE);
132
	}
133
};
134

    
135
?>
(16-16/67)