|
1 // Copyright (c) 2002-2009 Nokia Corporation and/or its subsidiary(-ies). |
|
2 // All rights reserved. |
|
3 // This component and the accompanying materials are made available |
|
4 // under the terms of "Eclipse Public License v1.0" |
|
5 // which accompanies this distribution, and is available |
|
6 // at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
7 // |
|
8 // Initial Contributors: |
|
9 // Nokia Corporation - initial contribution. |
|
10 // |
|
11 // Contributors: |
|
12 // |
|
13 // Description: |
|
14 // |
|
15 |
|
16 #include "smtpauthhelpers.h" |
|
17 #include "SMTSUTIL.H" // forward declarations for utility fns |
|
18 |
|
19 #include <e32des8.h> |
|
20 |
|
21 _LIT8(KPlainMechanism, "PLAIN "); //Space intentional, may find a better way of doing this. |
|
22 _LIT8(KSmtpAuthLoginCommand, "AUTH LOGIN\r\n"); |
|
23 _LIT8(KCramMD5Mechanism, "CRAM-MD5"); |
|
24 |
|
25 _LIT8(KSmtpAuthBase64StringUsername,"username*"); // To match a folded 'username:' or 'username' |
|
26 _LIT8(KSmtpAuthBase64StringPassword,"password*"); // To match a folded 'password:' or 'password' |
|
27 |
|
28 const TInt KMaxLengthOfPlainMessageComponent = 255; |
|
29 const TInt KMd5BlockLength = 64; |
|
30 |
|
31 #define BASE64LEN(x) ((x*4)/3) // Every 3 bytes will be turned into 4 bytes |
|
32 |
|
33 const TInt KPreambleLength = 18; //"AUTH LOGIN\r\nPLAIN " |
|
34 const TInt KMaxLengthOfPlainMessage= KPreambleLength + 1/*NUL*/ + KMaxLengthOfPlainMessageComponent/*Username*/ + 1/*NUL*/ + KMaxLengthOfPlainMessageComponent/*Password*/ + 2/* /r/n */; |
|
35 const TInt KMaxLengthOfPlainMessageBase64= BASE64LEN(KMaxLengthOfPlainMessage); |
|
36 |
|
37 |
|
38 |
|
39 // |
|
40 //CSmtpAuthMechanismHelper |
|
41 // |
|
42 |
|
43 CSmtpAuthMechanismHelper::CSmtpAuthMechanismHelper(const CSmtpSettings& aSettings) : iSettings(aSettings) |
|
44 { |
|
45 } |
|
46 |
|
47 // |
|
48 //CSmtpAuthPlainMechanismHelper |
|
49 // |
|
50 |
|
51 |
|
52 CSmtpAuthPlainMechanismHelper::CSmtpAuthPlainMechanismHelper(const CSmtpSettings& aSettings) : CSmtpAuthMechanismHelper(aSettings) |
|
53 { |
|
54 } |
|
55 |
|
56 void CSmtpAuthPlainMechanismHelper::GetNextClientMessageL(TDes8& aNextMessage) |
|
57 { |
|
58 // Build up the un-encoded authorisation string in parts |
|
59 HBufC8* authString = HBufC8::NewMaxLC(KMaxLengthOfPlainMessage); |
|
60 TPtr8 authStringPtr = authString->Des(); |
|
61 |
|
62 TBuf8<1> nul; |
|
63 nul.SetLength(1); |
|
64 nul[0] = 0; |
|
65 authStringPtr = nul; // Start with the NUL |
|
66 |
|
67 TPtrC8 loginName = iSettings.LoginName(); |
|
68 TInt length= Min(loginName.Length(),KMaxLengthOfPlainMessageComponent); |
|
69 authStringPtr.Append(loginName.Left(length)); |
|
70 |
|
71 authStringPtr.Append(nul); // Separator between login and password |
|
72 |
|
73 length= Min(iSettings.Password().Length(), KMaxLengthOfPlainMessageComponent); |
|
74 authStringPtr.Append(iSettings.Password().Left(length)); |
|
75 |
|
76 // Auth string is now built, encode it |
|
77 HBufC8* encodedBuf = HBufC8::NewMaxLC(KMaxLengthOfPlainMessageBase64); |
|
78 TPtr8 encodedBufPtr = encodedBuf->Des(); |
|
79 |
|
80 |
|
81 iEncoder.Encode(authStringPtr,encodedBufPtr); |
|
82 |
|
83 |
|
84 // Now build the message |
|
85 iNextClientMessage.Zero(); |
|
86 iNextClientMessage.Append(KSmtpAuthCommand); |
|
87 iNextClientMessage.Append(KPlainMechanism); |
|
88 iNextClientMessage.Append(encodedBufPtr); |
|
89 iNextClientMessage.Append(KSmtpCrLf); |
|
90 |
|
91 aNextMessage = iNextClientMessage; |
|
92 |
|
93 CleanupStack::PopAndDestroy(encodedBuf); |
|
94 CleanupStack::PopAndDestroy(authString); |
|
95 } |
|
96 |
|
97 void CSmtpAuthPlainMechanismHelper::SetLastServerMessageL(const TDesC8& /*aLastMessage*/, TBool /*aIsMultiLineResponse*/) |
|
98 { |
|
99 //iLastServerMessage.Set(aLastMessage); |
|
100 } |
|
101 |
|
102 |
|
103 // |
|
104 //CSmtpAuthLoginMechanismHelper |
|
105 // |
|
106 |
|
107 CSmtpAuthLoginMechanismHelper* CSmtpAuthLoginMechanismHelper::NewL(const CSmtpSettings& aSettings) |
|
108 { |
|
109 CSmtpAuthLoginMechanismHelper* self= new (ELeave) CSmtpAuthLoginMechanismHelper(aSettings); |
|
110 CleanupStack::PushL(self); |
|
111 self->ConstructL(); |
|
112 CleanupStack::Pop(); |
|
113 return self; |
|
114 } |
|
115 |
|
116 CSmtpAuthLoginMechanismHelper::CSmtpAuthLoginMechanismHelper(const CSmtpSettings& aSettings) : CSmtpAuthMechanismHelper(aSettings) |
|
117 { |
|
118 } |
|
119 |
|
120 CSmtpAuthLoginMechanismHelper::~CSmtpAuthLoginMechanismHelper() |
|
121 { |
|
122 delete iBase64LoginName; |
|
123 delete iBase64Password; |
|
124 } |
|
125 |
|
126 void CSmtpAuthLoginMechanismHelper::ConstructL() |
|
127 { |
|
128 TInt len=(((iSettings.LoginName().Length()/3)+1)*4)+2; // length of LoginName in base 64 + 2 for CRLF |
|
129 iBase64LoginName=HBufC8::NewL(len); |
|
130 TPtr8 authString(iBase64LoginName->Des()); |
|
131 iEncoder.Encode(iSettings.LoginName(),authString); |
|
132 authString.Append(KSmtpCrLf); |
|
133 len=(((iSettings.Password().Length()/3)+1)*4)+2; // length of Password in base 64 + 2 for CRLF |
|
134 iBase64Password=HBufC8::NewL(len); |
|
135 authString.Set(iBase64Password->Des()); |
|
136 iEncoder.Encode(iSettings.Password(),authString); |
|
137 authString.Append(KSmtpCrLf); |
|
138 } |
|
139 |
|
140 void CSmtpAuthLoginMechanismHelper::GetNextClientMessageL(TDes8& aNextMessage) |
|
141 { |
|
142 iNextClientMessage.Zero(); |
|
143 switch (iState) |
|
144 { |
|
145 case ESendingAuth: |
|
146 iNextClientMessage=KSmtpAuthLoginCommand; |
|
147 break; |
|
148 case ESendingLoginName: |
|
149 iNextClientMessage=*iBase64LoginName; |
|
150 break; |
|
151 case ESendingPassword: |
|
152 iNextClientMessage=*iBase64Password; |
|
153 break; |
|
154 default: |
|
155 gPanic(EImsmSmtpAuthLoginBadState); |
|
156 break; |
|
157 } |
|
158 aNextMessage=iNextClientMessage; |
|
159 } |
|
160 |
|
161 void CSmtpAuthLoginMechanismHelper::SetLastServerMessageL(const TDesC8& aLastMessage, TBool /*aIsMultiLineResponse*/) |
|
162 { |
|
163 HBufC8* decodedMessage = NULL; |
|
164 TInt lastMessageLength = aLastMessage.Length(); |
|
165 // Decode the SMTP response from the Base64 original |
|
166 if (lastMessageLength > 0) |
|
167 { |
|
168 decodedMessage = HBufC8::NewLC(lastMessageLength); // already on stack |
|
169 // Get pointer to response minus the "334 " at the start |
|
170 TPtrC8 response = aLastMessage.Right(lastMessageLength - 4); |
|
171 TPtr8 destination = decodedMessage->Des(); |
|
172 iEncoder.Decode(response, destination); |
|
173 } |
|
174 switch (iState) |
|
175 { |
|
176 case ESendingAuth: |
|
177 if (decodedMessage) |
|
178 { |
|
179 // Match against 'username*' |
|
180 if (decodedMessage->Des().MatchF(KSmtpAuthBase64StringUsername) == KErrNotFound) |
|
181 { |
|
182 User::Leave(KErrNotSupported); |
|
183 } |
|
184 //Some SMTP server don't follow the RFC 2554 completely, so at the end of 334 replies it appends information |
|
185 //string which is not BASE64 encoded. In such cases, after decoding the first 334 reply, it sometimes |
|
186 //leaves BASE64 decoder in incorrect state. So the second reply isn't decoded properly, to avoid |
|
187 //this we need to initialise the encoder |
|
188 iEncoder.Initialise(); |
|
189 iState = ESendingLoginName; |
|
190 } |
|
191 else |
|
192 { |
|
193 User::Leave(KErrNotSupported); |
|
194 } |
|
195 break; |
|
196 case ESendingLoginName: |
|
197 if (decodedMessage) |
|
198 { |
|
199 // Match against 'password*' |
|
200 if (decodedMessage->Des().MatchF(KSmtpAuthBase64StringPassword) == KErrNotFound) |
|
201 { |
|
202 User::Leave(KErrNotSupported); |
|
203 } |
|
204 //Some SMTP server don't follow the RFC 2554 completely, so at the end of 334 replies it appends information |
|
205 //string which is not BASE64 encoded. In such cases, after decoding the first 334 reply, it sometimes |
|
206 //leaves BASE64 decoder in incorrect state. So the second reply isn't decoded properly, to avoid |
|
207 //this we need to initialise the encoder |
|
208 iEncoder.Initialise(); |
|
209 iState = ESendingPassword; |
|
210 } |
|
211 else |
|
212 { |
|
213 User::Leave(KErrNotSupported); |
|
214 } |
|
215 break; |
|
216 case ESendingPassword: // the helper shouldn't be handling the 235 response |
|
217 default: |
|
218 User::Leave(KErrNotSupported); |
|
219 break; |
|
220 } |
|
221 if (decodedMessage) |
|
222 { |
|
223 CleanupStack::PopAndDestroy(decodedMessage); |
|
224 } |
|
225 } |
|
226 |
|
227 // |
|
228 //CSmtpAuthCramMd5MechanismHelper |
|
229 // |
|
230 |
|
231 CSmtpAuthCramMd5MechanismHelper* CSmtpAuthCramMd5MechanismHelper::NewL(const CSmtpSettings& aSettings) |
|
232 { |
|
233 CSmtpAuthCramMd5MechanismHelper* self= new (ELeave) CSmtpAuthCramMd5MechanismHelper(aSettings); |
|
234 CleanupStack::PushL(self); |
|
235 self->ConstructL(); |
|
236 CleanupStack::Pop(); |
|
237 return self; |
|
238 } |
|
239 |
|
240 CSmtpAuthCramMd5MechanismHelper::CSmtpAuthCramMd5MechanismHelper(const CSmtpSettings& aSettings) : CSmtpAuthMechanismHelper(aSettings) |
|
241 { |
|
242 } |
|
243 |
|
244 |
|
245 void CSmtpAuthCramMd5MechanismHelper::ConstructL() |
|
246 { |
|
247 iMd5Hash = CMD5::NewL(); |
|
248 } |
|
249 |
|
250 CSmtpAuthCramMd5MechanismHelper::~CSmtpAuthCramMd5MechanismHelper() |
|
251 { |
|
252 delete iMd5Hash; |
|
253 } |
|
254 |
|
255 |
|
256 void CSmtpAuthCramMd5MechanismHelper::GetNextClientMessageL(TDes8& aNextMessage) |
|
257 { |
|
258 iNextClientMessage.Zero(); |
|
259 |
|
260 if(iInProgress) |
|
261 { |
|
262 HBufC8* authbuffer = HBufC8::NewMaxLC(KImMailMaxBufferSize);//? |
|
263 TPtr8 authbufferptr = authbuffer->Des(); |
|
264 HBufC8* authbuffer2 = HBufC8::NewMaxLC(KImMailMaxBufferSize);//? |
|
265 TPtr8 authbufferptr2 = authbuffer2->Des(); |
|
266 HBufC8* authbuffer3 = HBufC8::NewMaxLC(KImMailMaxBufferSize);//? |
|
267 TPtr8 authbufferptr3 = authbuffer3->Des(); |
|
268 |
|
269 authbufferptr = iLastServerMessage; |
|
270 authbufferptr.Delete(0,4); //remove 334 from the begining of server response |
|
271 iEncoder.Decode(authbufferptr,authbufferptr2); |
|
272 //timestamp info now in authbufferptr2 |
|
273 |
|
274 authbufferptr.Zero(); |
|
275 authbufferptr3.Zero(); |
|
276 authbufferptr = iSettings.Password(); |
|
277 authbufferptr3 = FormSharedSecret(authbufferptr); |
|
278 authbufferptr.Zero(); |
|
279 authbufferptr = authbufferptr3; //now authbufferptr and authbufferptr3 contain the shared secret null padded to 64 bytes. |
|
280 TInt i=0; |
|
281 for (i=0; i<KMd5BlockLength; i++) |
|
282 { |
|
283 authbufferptr[i] ^= 0x36; //ipad |
|
284 authbufferptr3[i] ^= 0x5c; //opad |
|
285 } |
|
286 |
|
287 authbufferptr.Append(authbufferptr2); |
|
288 |
|
289 iMd5Hash->Reset(); ; |
|
290 authbufferptr2 = iMd5Hash->Hash(authbufferptr); |
|
291 |
|
292 authbufferptr3.Append(authbufferptr2); |
|
293 |
|
294 authbufferptr.Zero(); |
|
295 iMd5Hash->Reset(); |
|
296 authbufferptr = iMd5Hash->Hash(authbufferptr3); |
|
297 |
|
298 // MD5 algorithm ALWAYS returns 16 bytes of data - which will be converted into |
|
299 // 32 characters; each byte represented by a 2 character hex representation, |
|
300 // eg 255="ff" |
|
301 TBuf<32> hexHash; |
|
302 for (i=0;i<16;i++) |
|
303 { |
|
304 hexHash.AppendNumFixedWidth(authbufferptr[i],EHex,2); |
|
305 } |
|
306 |
|
307 authbufferptr3.Zero(); |
|
308 authbufferptr3.Append(iSettings.LoginName()); |
|
309 authbufferptr3.Append(_L8(" ")); |
|
310 authbufferptr3.Append(hexHash); |
|
311 |
|
312 iEncoder.Encode(authbufferptr3, iNextClientMessage); |
|
313 iNextClientMessage.Append(KSmtpCrLf); |
|
314 CleanupStack::PopAndDestroy(3); // authbufferptr3 ,authbufferptr2, authbufferptr |
|
315 aNextMessage = iNextClientMessage; |
|
316 } |
|
317 else |
|
318 { |
|
319 iNextClientMessage.Append(KSmtpAuthCommand); |
|
320 iNextClientMessage.Append(KCramMD5Mechanism); |
|
321 iNextClientMessage.Append(KSmtpCrLf); |
|
322 iInProgress=ETrue; |
|
323 aNextMessage = iNextClientMessage; |
|
324 } |
|
325 } |
|
326 |
|
327 TPtr8 CSmtpAuthCramMd5MechanismHelper::FormSharedSecret(TPtr8 aPassword) |
|
328 { |
|
329 TPtr8 secret = aPassword; |
|
330 |
|
331 if (aPassword.Length() <= KMd5BlockLength) |
|
332 { |
|
333 //pad up to 64 bytes with zeros |
|
334 secret.SetLength(64); |
|
335 TInt origLen = aPassword.Length(); |
|
336 TInt lengthToFill = KMd5BlockLength-aPassword.Length(); |
|
337 for (TInt i = 0; i<lengthToFill; i++) |
|
338 { |
|
339 secret[origLen+i]=0x00; |
|
340 } |
|
341 } |
|
342 else |
|
343 { |
|
344 //make shared secret the Digest of the password |
|
345 secret.Zero(); |
|
346 iMd5Hash->Reset(); |
|
347 secret = (iMd5Hash->Hash(aPassword)); |
|
348 //do we need to call this function again now to pad out the 16 byte result? |
|
349 } |
|
350 |
|
351 return secret; |
|
352 } |
|
353 |
|
354 void CSmtpAuthCramMd5MechanismHelper::SetLastServerMessageL(const TDesC8& aLastMessage, TBool /*aIsMultiLineResponse*/) |
|
355 { |
|
356 iLastServerMessage.Set(aLastMessage); |
|
357 } |
|
358 |
|
359 |
|
360 // |
|
361 //CSmtpAuthMechanismHelper factory function |
|
362 // |
|
363 GLDEF_C CSmtpAuthMechanismHelper* CreateSMTPAuthHelperL(CSmtpAuthMechanismHelper::TSmtpAuthProfileFlag aType, const CSmtpSettings& aSettings) |
|
364 { |
|
365 //Factory function that returns relevant Helper corresponding to aType |
|
366 switch(aType) |
|
367 { |
|
368 case CSmtpAuthMechanismHelper::EPlain: |
|
369 { |
|
370 CSmtpAuthMechanismHelper* helper; |
|
371 helper = new (ELeave) CSmtpAuthPlainMechanismHelper(aSettings); |
|
372 return helper; |
|
373 } |
|
374 case CSmtpAuthMechanismHelper::ELogin: |
|
375 { |
|
376 CSmtpAuthLoginMechanismHelper* smtpAuthLogin = CSmtpAuthLoginMechanismHelper::NewL(aSettings); |
|
377 return smtpAuthLogin; |
|
378 } |
|
379 case CSmtpAuthMechanismHelper::ECramMD5: |
|
380 { |
|
381 CSmtpAuthCramMd5MechanismHelper* smtpAuthCramMd5 = CSmtpAuthCramMd5MechanismHelper::NewL(aSettings); |
|
382 return smtpAuthCramMd5; |
|
383 } |
|
384 default: |
|
385 User::Leave(KErrNotSupported); //gPanic(EImsmBadSmtpAuthProfile2); |
|
386 return NULL; |
|
387 } |
|
388 } |