|
1 /* |
|
2 * Copyright (c) 2004-2006 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: PnpUtil implementation |
|
15 * |
|
16 */ |
|
17 |
|
18 |
|
19 |
|
20 // INCLUDE FILES |
|
21 #include <e32std.h> |
|
22 #include <e32math.h> |
|
23 #include <mmtsy_names.h> |
|
24 #include <asymmetric.h> // For RInteger |
|
25 #include <3des.h> // For C3DES |
|
26 #include <centralrepository.h> // link against centralrepository.lib |
|
27 |
|
28 #include "PnpUtilImpl.h" |
|
29 #include "PnpUtilLogger.h" |
|
30 #include "PnpUtilPrivateCRKeys.h" // Central repository keys |
|
31 |
|
32 |
|
33 // CONSTANTS |
|
34 |
|
35 const TInt KMaxModulusLength = 300; |
|
36 const TInt KMaxPublicExponentLength = 10; |
|
37 |
|
38 _LIT8(K3DesKey, "111111111111111111111111"); |
|
39 |
|
40 // Default token validity time |
|
41 const TInt KTokenValidityTime = 600; |
|
42 const TInt KTokenLength = 4; |
|
43 // C3DESEncryptor and C3DESDecryptor require the buffer to be more than 4 characters |
|
44 // (8 characters seems to be enough) |
|
45 const TInt KTokenLengthDuringStorage = 8; |
|
46 |
|
47 // ----------------------------------------------------------------------------- |
|
48 // CPnpUtilImpl::ConstructL |
|
49 // ----------------------------------------------------------------------------- |
|
50 // |
|
51 void CPnpUtilImpl::ConstructL() |
|
52 { |
|
53 LOGSTRING( "Enter to CPnpUtilImpl::ConstructL()" ); |
|
54 |
|
55 User::LeaveIfError( iServer.Connect() ); |
|
56 LOGSTRING( "CPnpUtilImpl::ConstructL() 2" ); |
|
57 User::LeaveIfError( iServer.LoadPhoneModule( KMmTsyModuleName ) ); |
|
58 LOGSTRING( "CPnpUtilImpl::ConstructL() 3" ); |
|
59 User::LeaveIfError( iPhone.Open( iServer, KMmTsyPhoneName ) ); |
|
60 LOGSTRING( "CPnpUtilImpl::ConstructL() 4" ); |
|
61 User::LeaveIfError( iPhone.Initialise() ); |
|
62 |
|
63 LOGSTRING( "CPnpUtilImpl::ConstructL() 5" ); |
|
64 |
|
65 iRepository = CRepository::NewL( KCRUidPnpUtil ); |
|
66 |
|
67 #ifndef __WINS__ // calling C3DESEncryptor::NewL crashes emulator |
|
68 iEncryptor = C3DESEncryptor::NewL( K3DesKey ); |
|
69 iDecryptor = C3DESDecryptor::NewL( K3DesKey ); |
|
70 #endif |
|
71 |
|
72 LOGSTRING( "Exit from CPnpUtilImpl::ConstructL()" ); |
|
73 } |
|
74 |
|
75 // ----------------------------------------------------------------------------- |
|
76 // CPnpUtilImpl::NewLC |
|
77 // ----------------------------------------------------------------------------- |
|
78 // |
|
79 EXPORT_C CPnpUtilImpl* CPnpUtilImpl::NewLC() |
|
80 { |
|
81 LOGSTRING("CPnpUtilImpl::NewLC"); |
|
82 CPnpUtilImpl* self = new (ELeave) CPnpUtilImpl(); |
|
83 CleanupStack::PushL( self ); |
|
84 self->ConstructL(); |
|
85 return self; |
|
86 } |
|
87 |
|
88 // ----------------------------------------------------------------------------- |
|
89 // CPnpUtilImpl::CPnpUtilImpl |
|
90 // ----------------------------------------------------------------------------- |
|
91 // |
|
92 CPnpUtilImpl::CPnpUtilImpl() |
|
93 { |
|
94 LOGSTRING("CPnpUtilImpl()"); |
|
95 } |
|
96 |
|
97 // ----------------------------------------------------------------------------- |
|
98 // CPnpUtilImpl::~CPnpUtilImpl |
|
99 // ----------------------------------------------------------------------------- |
|
100 // |
|
101 EXPORT_C CPnpUtilImpl::~CPnpUtilImpl() |
|
102 { |
|
103 LOGSTRING( "~CPnpUtilImpl" ); |
|
104 delete iRepository; |
|
105 iPhone.Close(); |
|
106 iServer.Close(); |
|
107 #ifndef __WINS__ |
|
108 delete iEncryptor; |
|
109 delete iDecryptor; |
|
110 #endif |
|
111 LOGSTRING( "~CPnpUtilImpl - done" ); |
|
112 } |
|
113 |
|
114 // ----------------------------------------------------------------------------- |
|
115 // CPnpUtilImpl::Version |
|
116 // ----------------------------------------------------------------------------- |
|
117 // |
|
118 EXPORT_C TInt CPnpUtilImpl::Version( TDes& aVersion ) |
|
119 { |
|
120 LOGSTRING( "CPnpUtilImpl::Version" ); |
|
121 |
|
122 // There is now a separate dll for HDC so there is no need to |
|
123 // read the version string from a database, we may use |
|
124 // a hard-coded value instead. |
|
125 |
|
126 _LIT( KTempVersion, "NPnP-MSS60-1.01" ); |
|
127 |
|
128 // Max length is KMaxVersionStringLength |
|
129 if( KTempVersion().Length() > aVersion.MaxLength() ) |
|
130 { |
|
131 return KErrArgument; |
|
132 } |
|
133 aVersion.Copy( KTempVersion ); |
|
134 return KErrNone; |
|
135 } |
|
136 |
|
137 |
|
138 // ----------------------------------------------------------------------------- |
|
139 // CPnpUtilImpl::CreateNewToken |
|
140 // ----------------------------------------------------------------------------- |
|
141 // |
|
142 EXPORT_C TInt CPnpUtilImpl::CreateNewToken(TUint32 aTimeout, TInt& aToken) |
|
143 { |
|
144 LOGSTRING("CPnpUtilImpl::CreateNewToken"); |
|
145 TInt value(0); |
|
146 // Get current time |
|
147 TTime t; |
|
148 t.HomeTime(); |
|
149 TInt64 time = t.Int64(); |
|
150 |
|
151 // Create timeout |
|
152 if( aTimeout > 9999 ) |
|
153 { |
|
154 LOGSTRING("too long timeout - KErrArgument"); |
|
155 return KErrArgument; |
|
156 } |
|
157 |
|
158 // Deadline = currenttime + (atimeout * 1000000) |
|
159 TInt64 deadline = time + TInt64((TInt)aTimeout) * TInt64(1000000); |
|
160 TBuf<21> num; // 20 is max length for 64 bit integer (radix 10) |
|
161 num.AppendNum(deadline); |
|
162 LOGSTRING("WriteDeadline"); |
|
163 LOGTEXT(num); |
|
164 |
|
165 // Store deadline |
|
166 TInt result = iRepository->Set( KPnPUtilDeadline, num ); |
|
167 if( result != KErrNone ) |
|
168 { |
|
169 LOGSTRING2("Storing deadline failed: %i", result); |
|
170 return result; |
|
171 } |
|
172 |
|
173 value = Math::Rand(time); |
|
174 value %= (9999 - 1001); |
|
175 value += 1001; |
|
176 |
|
177 LOGSTRING2( "New Token: %i", value ); |
|
178 |
|
179 // KTokenLength is too small for the buffer length because C3DESEncryptor/C3DESDecryptor |
|
180 // cannot handle small buffers |
|
181 TBuf8<KTokenLengthDuringStorage> tokenBuf; |
|
182 tokenBuf.AppendNum( value ); |
|
183 tokenBuf.AppendNum( 1234 ); |
|
184 // Encrypt |
|
185 #ifndef __WINS__ |
|
186 iEncryptor->Transform( tokenBuf ); |
|
187 #endif |
|
188 |
|
189 // Save token |
|
190 result = iRepository->Set( KPnPUtilToken, tokenBuf ); |
|
191 |
|
192 aToken = value; |
|
193 |
|
194 LOGSTRING("CPnpUtilImpl::CreateNewToken - done"); |
|
195 return result; |
|
196 } |
|
197 |
|
198 // ----------------------------------------------------------------------------- |
|
199 // CPnpUtilImpl::GetTokenValidityTime |
|
200 // ----------------------------------------------------------------------------- |
|
201 // |
|
202 EXPORT_C TInt CPnpUtilImpl::GetTokenValidityTime() |
|
203 { |
|
204 LOGSTRING("CPnpUtilImpl::GetTokenValidityTime"); |
|
205 return KTokenValidityTime; |
|
206 } |
|
207 |
|
208 // ----------------------------------------------------------------------------- |
|
209 // CPnpUtilImpl::GetTokenValue |
|
210 // ----------------------------------------------------------------------------- |
|
211 // |
|
212 EXPORT_C TInt CPnpUtilImpl::GetTokenValue(TInt& aToken) |
|
213 { |
|
214 LOGSTRING("Enter CPnpUtilImpl::GetTokenValue"); |
|
215 |
|
216 // Check deadline |
|
217 TBuf<21> num; |
|
218 LOGSTRING("ReadDeadline"); |
|
219 |
|
220 TInt result = iRepository->Get( KPnPUtilDeadline, num ); |
|
221 if( result != KErrNone ) |
|
222 { |
|
223 return KErrCorrupt; |
|
224 } |
|
225 |
|
226 LOGTEXT(num); |
|
227 TLex l(num); |
|
228 TInt64 deadline(0); |
|
229 if( l.Val(deadline)!= KErrNone ) |
|
230 { |
|
231 return KErrCorrupt; |
|
232 } |
|
233 // Check validity time |
|
234 TTime t; |
|
235 t.HomeTime(); |
|
236 if( t > deadline ) |
|
237 { |
|
238 TTimeIntervalSeconds seconds(0); |
|
239 t.SecondsFrom( deadline, seconds ); |
|
240 LOGSTRING2( "Token validity expired: %is ago", seconds.Int() ); |
|
241 return KErrTimedOut; |
|
242 } |
|
243 |
|
244 LOGSTRING("Token not expired"); |
|
245 |
|
246 // Get token |
|
247 // KTokenLength is too small for the buffer length because C3DESEncryptor/C3DESDecryptor |
|
248 // cannot handle small buffers |
|
249 TBuf8<KTokenLengthDuringStorage> tokenBuf; |
|
250 result = iRepository->Get( KPnPUtilToken, tokenBuf ); |
|
251 |
|
252 if( result != KErrNone ) |
|
253 { |
|
254 LOGSTRING2( "Error %i when trying to read token value", result ); |
|
255 return result; |
|
256 } |
|
257 |
|
258 LOGSTRING("Tokenbuf:"); |
|
259 LOGTEXT( tokenBuf ); |
|
260 |
|
261 #ifndef __WINS__ |
|
262 iDecryptor->Transform( tokenBuf ); |
|
263 #endif |
|
264 |
|
265 TInt token(0); |
|
266 TLex8 parser( tokenBuf.Left( KTokenLength ) ); |
|
267 parser.Val( token ); |
|
268 |
|
269 if( token < 1 || token > 9999) |
|
270 { |
|
271 return KErrCorrupt; |
|
272 } |
|
273 |
|
274 LOGSTRING("Token OK"); |
|
275 LOGSTRING2("Token: %i", token); |
|
276 |
|
277 aToken = token; |
|
278 |
|
279 return KErrNone; |
|
280 } |
|
281 |
|
282 |
|
283 // ----------------------------------------------------------------------------- |
|
284 // CPnpUtilImpl::CreateNewNonceL |
|
285 // ----------------------------------------------------------------------------- |
|
286 // |
|
287 EXPORT_C void CPnpUtilImpl::CreateNewNonceL( const TUint /*aTimeOut*/, TDes8& aNonce ) |
|
288 { |
|
289 LOGSTRING("CPnpUtilImpl::CreateNewNonceL"); |
|
290 if( aNonce.MaxLength() < KNonceLength ) |
|
291 { |
|
292 User::Leave( KErrArgument ); |
|
293 } |
|
294 |
|
295 TBuf8<KNonceLength> buffer; |
|
296 |
|
297 TTime time; |
|
298 time.HomeTime(); |
|
299 TInt64 seed = time.Int64(); |
|
300 for( TInt i(0); i < KNonceLength; i++ ) |
|
301 { |
|
302 TChar character( RandomCharacter( seed ) ); |
|
303 buffer.Append( character ); |
|
304 } |
|
305 |
|
306 LOGSTRING("New Nonce:"); |
|
307 LOGTEXT( buffer ); |
|
308 aNonce.Copy( buffer ); |
|
309 |
|
310 // Encrypt |
|
311 #ifndef __WINS__ |
|
312 iEncryptor->Transform( buffer ); |
|
313 #endif |
|
314 |
|
315 // Save the nonce |
|
316 User::LeaveIfError( iRepository->Set( KPnPUtilNonce, buffer ) ); |
|
317 |
|
318 LOGSTRING("CPnpUtilImpl::CreateNewNonceL - done"); |
|
319 } |
|
320 |
|
321 // ----------------------------------------------------------------------------- |
|
322 // CPnpUtilImpl::GetNonceValidityTimeL |
|
323 // ----------------------------------------------------------------------------- |
|
324 // |
|
325 EXPORT_C TInt CPnpUtilImpl::GetNonceValidityTimeL() |
|
326 { |
|
327 // Not used |
|
328 User::Leave( KErrNotSupported ); |
|
329 return KErrNone; |
|
330 } |
|
331 |
|
332 |
|
333 // ----------------------------------------------------------------------------- |
|
334 // CPnpUtilImpl::GetNonceL |
|
335 // ----------------------------------------------------------------------------- |
|
336 // |
|
337 EXPORT_C void CPnpUtilImpl::GetNonceL( TDes8& aNonce ) |
|
338 { |
|
339 LOGSTRING("CPnpUtilImpl::GetNonceL"); |
|
340 |
|
341 if( aNonce.MaxLength() < KNonceLength ) |
|
342 { |
|
343 User::Leave( KErrArgument ); |
|
344 } |
|
345 TBuf8<KNonceLength> buffer; |
|
346 |
|
347 User::LeaveIfError( iRepository->Get( KPnPUtilNonce, buffer) ); |
|
348 |
|
349 // Decrypt |
|
350 #ifndef __WINS__ |
|
351 if( buffer.Length() > 0 ) |
|
352 { |
|
353 LOGTEXT( buffer ); |
|
354 iDecryptor->Transform( buffer ); |
|
355 } |
|
356 #endif |
|
357 |
|
358 LOGSTRING("Nonce:"); |
|
359 LOGTEXT( buffer ); |
|
360 aNonce.Copy( buffer ); |
|
361 |
|
362 LOGSTRING("CPnpUtilImpl::GetNonceL - done"); |
|
363 } |
|
364 |
|
365 |
|
366 // ----------------------------------------------------------------------------- |
|
367 // CPnpUtilImpl::RandomCharacter |
|
368 // ----------------------------------------------------------------------------- |
|
369 // |
|
370 TUint CPnpUtilImpl::RandomCharacter( TInt64& aSeed ) |
|
371 { |
|
372 // Create random number value for token |
|
373 TInt value = Math::Rand( aSeed ); |
|
374 if( value < 0 ) |
|
375 { |
|
376 value = -value; |
|
377 } |
|
378 value %= 61; // [0,61] |
|
379 value += 48; // [48,109] |
|
380 // split the range to ranges [48,57],[65,90] and [97,122] |
|
381 // that is: the ascii codes for numbers from 0 to 9, |
|
382 // capital alphabets from A to Z and alphabets from a to z |
|
383 if( value > 57 ) |
|
384 { |
|
385 value += 8; // [65,90] |
|
386 if( value > 90 ) |
|
387 { |
|
388 value += 6; // [97,122] |
|
389 } |
|
390 } |
|
391 return value; |
|
392 } |
|
393 |
|
394 // ----------------------------------------------------------------------------- |
|
395 // CPnpUtilImpl::GetKeyInfoL |
|
396 // ----------------------------------------------------------------------------- |
|
397 // |
|
398 EXPORT_C void CPnpUtilImpl::GetKeyInfoL( TDes8& aKeyInfo ) |
|
399 { |
|
400 LOGSTRING("CPnpUtilImpl::GetKeyInfoL"); |
|
401 if( aKeyInfo.MaxLength() < KMaxKeyInfoLength ) |
|
402 { |
|
403 User::Leave( KErrArgument ); |
|
404 } |
|
405 // read keyinfo from cenrep |
|
406 TBuf<KMaxKeyInfoLength> buffer; |
|
407 User::LeaveIfError( iRepository->Get( KPnPUtilKeyInfo, buffer) ); |
|
408 aKeyInfo.Copy( buffer ); |
|
409 LOGSTRING("CPnpUtilImpl::GetKeyInfoL - done"); |
|
410 } |
|
411 |
|
412 |
|
413 // ----------------------------------------------------------------------------- |
|
414 // CPnpUtilImpl::VerifySignatureL |
|
415 // ----------------------------------------------------------------------------- |
|
416 // |
|
417 EXPORT_C TBool CPnpUtilImpl::VerifySignatureL( |
|
418 const TDesC8& aDigestValue, const TDesC8& aSignatureValue, |
|
419 const TDesC8& aData, const TDesC8& aNonce ) |
|
420 { |
|
421 LOGSTRING( "CPnpUtilImpl::VerifySignatureL" ); |
|
422 LOGTEXT( aDigestValue ); |
|
423 |
|
424 HBufC8* sendersDigest = DecodeBase64LC( aDigestValue ); |
|
425 LOGSTRING( "senders digest:" ); |
|
426 LogAsASCIIHexL( *sendersDigest ); |
|
427 |
|
428 HBufC8* sendersSignature = DecodeBase64LC( aSignatureValue ); |
|
429 LOGSTRING( "senders signature:" ); |
|
430 LogAsASCIIHexL( *sendersSignature ); |
|
431 |
|
432 |
|
433 // Read Modulus and PublicExponent from Cenrep |
|
434 LOGSTRING( "Read Modulus and PublicExponent from Cenrep" ); |
|
435 TBuf<KMaxModulusLength> bufferMod; |
|
436 TBuf<KMaxPublicExponentLength> bufferExp; |
|
437 User::LeaveIfError( iRepository->Get( KPnPUtilModulus, bufferMod) ); |
|
438 User::LeaveIfError( iRepository->Get( KPnPUtilPublicExponent, bufferExp) ); |
|
439 |
|
440 TBuf8<KMaxModulusLength> bufferMod8; |
|
441 TBuf8<KMaxPublicExponentLength> bufferExp8; |
|
442 bufferMod8.Copy( bufferMod ); |
|
443 bufferExp8.Copy( bufferExp ); |
|
444 |
|
445 LOGSTRING( "Pack modulus and exponent" ); |
|
446 // Look for (lousy/faulty) documentation for RInteger from the 2.1 SDK |
|
447 HBufC8* modulusBuf = PackLC( bufferMod8 ); |
|
448 HBufC8* exponentBuf = PackLC( bufferExp8 ); |
|
449 |
|
450 RInteger modulus = RInteger::NewL( *modulusBuf ); |
|
451 RInteger exponent = RInteger::NewL( *exponentBuf ); |
|
452 |
|
453 CRSAPublicKey* publicKey = CRSAPublicKey::NewLC( modulus, exponent ); |
|
454 CRSAPKCS1v15Verifier* rsaVerifier = CRSAPKCS1v15Verifier::NewLC( *publicKey ); |
|
455 |
|
456 RInteger rsaSignatureInteger = RInteger::NewL( *sendersSignature ); |
|
457 CRSASignature* signature = CRSASignature::NewLC( rsaSignatureInteger ); |
|
458 |
|
459 // Verify the digest against the signature |
|
460 TBool verified = rsaVerifier->VerifyL( *sendersDigest, *signature ); |
|
461 |
|
462 // These are not needed anymore, destroy |
|
463 CleanupStack::PopAndDestroy( signature ); |
|
464 CleanupStack::PopAndDestroy( rsaVerifier ); |
|
465 CleanupStack::PopAndDestroy( publicKey ); |
|
466 CleanupStack::PopAndDestroy( exponentBuf ); |
|
467 CleanupStack::PopAndDestroy( modulusBuf ); |
|
468 CleanupStack::PopAndDestroy( sendersSignature ); |
|
469 |
|
470 |
|
471 if( verified ) // Signature was verified |
|
472 { |
|
473 LOGSTRING( "Signature Verified" ); |
|
474 |
|
475 // ...Yep, verify also digest |
|
476 verified = VerifyDigestL( *sendersDigest, aData, aNonce ); |
|
477 if( verified ) |
|
478 { |
|
479 LOGSTRING( "Digest Verified" ); |
|
480 } |
|
481 else |
|
482 { |
|
483 LOGSTRING( "Digest NOT verified" ); |
|
484 } |
|
485 } |
|
486 else |
|
487 { |
|
488 // ...Nope, do not check the digest, verify already failed anyways |
|
489 LOGSTRING( "Signature NOT verified" ); |
|
490 } |
|
491 |
|
492 CleanupStack::PopAndDestroy( sendersDigest ); |
|
493 |
|
494 LOGSTRING( "CPnpUtilImpl::VerifySignatureL - done" ); |
|
495 return verified; |
|
496 } |
|
497 |
|
498 // ----------------------------------------------------------------------------- |
|
499 // CPnpUtilImpl::VerifyDigestL |
|
500 // ----------------------------------------------------------------------------- |
|
501 // |
|
502 TBool CPnpUtilImpl::VerifyDigestL( |
|
503 const TDesC8& aSendersDigest, const TDesC8& aData, const TDesC8& aNonce ) |
|
504 { |
|
505 LOGSTRING( "VerifyDigestL" ); |
|
506 LOGTEXT( aData ); |
|
507 LOGTEXT( aNonce ); |
|
508 |
|
509 CSHA1* sha1 = CSHA1::NewL(); |
|
510 CleanupStack::PushL( sha1 ); |
|
511 |
|
512 HBufC8* dataToBeHashed = HBufC8::NewLC( aData.Length() + aNonce.Length() + 1 ); |
|
513 TPtr8 dataToBeHashedPtr = dataToBeHashed->Des(); |
|
514 _LIT8( KColon, ":" ); |
|
515 dataToBeHashedPtr.Append( aNonce ); |
|
516 dataToBeHashedPtr.Append( KColon ); |
|
517 dataToBeHashedPtr.Append( aData ); |
|
518 |
|
519 LOGSTRING( "nonce:data" ); |
|
520 LogAsASCIIHexL( *dataToBeHashed ); |
|
521 |
|
522 TPtrC8 hash = sha1->Final( *dataToBeHashed ); |
|
523 LOGSTRING( "computed hash:" ); |
|
524 LOGTEXT( hash ); |
|
525 LogAsASCIIHexL( hash ); |
|
526 |
|
527 LOGSTRING( "senders hash:" ); |
|
528 LogAsASCIIHexL( aSendersDigest ); |
|
529 TBool verified( EFalse ); |
|
530 if( hash.Compare( aSendersDigest ) == 0 ) |
|
531 { |
|
532 verified = ETrue; |
|
533 } |
|
534 CleanupStack::PopAndDestroy( dataToBeHashed ); |
|
535 CleanupStack::PopAndDestroy( sha1 ); |
|
536 return verified; |
|
537 } |
|
538 |
|
539 |
|
540 // ----------------------------------------------------------------------------- |
|
541 // CPnpUtilImpl::LogAsASCIIHexL |
|
542 // ----------------------------------------------------------------------------- |
|
543 // |
|
544 void CPnpUtilImpl::LogAsASCIIHexL( const TDesC8& aDesc ) |
|
545 { |
|
546 HBufC8* asciiHexBuf = HBufC8::NewLC( aDesc.Length() * 2 ); |
|
547 TPtr8 asciiHexBufPtr = asciiHexBuf->Des(); |
|
548 TBuf8<2> asciiHex; |
|
549 // binary to ascii (hex) |
|
550 _LIT8( KAsciiHexFormat, "%02X" ); |
|
551 TUint8 val; |
|
552 for( TInt i=0; i<aDesc.Length(); i++ ) |
|
553 { |
|
554 val = aDesc[i]; |
|
555 asciiHex.Format( KAsciiHexFormat, val ); |
|
556 asciiHexBufPtr.Append( asciiHex ); |
|
557 } |
|
558 |
|
559 #ifdef _DEBUG |
|
560 for( TInt j(0); j < asciiHexBufPtr.Length(); j += 128 ) |
|
561 { |
|
562 TPtrC8 logText = asciiHexBufPtr.Mid(j); // print in 128 byte chunks |
|
563 LOGTEXT( logText ); |
|
564 } |
|
565 #endif |
|
566 |
|
567 CleanupStack::PopAndDestroy( asciiHexBuf ); |
|
568 } |
|
569 |
|
570 // ----------------------------------------------------------------------------- |
|
571 // CPnpUtilImpl::DecodeBase64LC |
|
572 // ----------------------------------------------------------------------------- |
|
573 // |
|
574 HBufC8* CPnpUtilImpl::DecodeBase64LC( const TDesC8& aBase64EncodedDesc ) |
|
575 { |
|
576 TInt lenght = aBase64EncodedDesc.Length(); |
|
577 HBufC8* decoded = HBufC8::NewLC( lenght ); |
|
578 TPtr8 decodedPtr = decoded->Des(); |
|
579 |
|
580 if( lenght % 4 != 0 ) |
|
581 { |
|
582 User::Leave( KErrCorrupt ); |
|
583 } |
|
584 |
|
585 for( TInt i(0); i < lenght - 1; i++ ) |
|
586 { |
|
587 // The last two character may be zero characters ('=') |
|
588 if( aBase64EncodedDesc[i] == '=' ) |
|
589 { |
|
590 if( i < lenght - 2 || aBase64EncodedDesc[i+1] != '=' ) |
|
591 { |
|
592 User::Leave( KErrCorrupt ); |
|
593 } |
|
594 return decoded; |
|
595 } |
|
596 else if( aBase64EncodedDesc[i+1] == '=' ) |
|
597 { |
|
598 if( i < lenght - 2 ) |
|
599 { |
|
600 User::Leave( KErrCorrupt ); |
|
601 } |
|
602 return decoded; |
|
603 } |
|
604 |
|
605 TChar character( DecodeCharL( aBase64EncodedDesc[i] ) ); |
|
606 TChar nextCharacter( DecodeCharL( aBase64EncodedDesc[i + 1] ) ); |
|
607 |
|
608 switch( i % 4 ) |
|
609 { |
|
610 case 0: |
|
611 { |
|
612 TChar shiftedChar( |
|
613 ( character << 2 ) | // bits 0 to 5 of the first character |
|
614 ( ( nextCharacter & 0x30 ) >> 4 ) ); // bits 4 to 5 of the second character |
|
615 decodedPtr.Append( shiftedChar ); |
|
616 } |
|
617 break; |
|
618 case 1: |
|
619 { |
|
620 TChar shiftedChar( |
|
621 ( character << 4 ) | // 0 to 3 |
|
622 ( ( nextCharacter & 0x3C ) >> 2 ) ); // 2 to 5 |
|
623 decodedPtr.Append( shiftedChar ); |
|
624 } |
|
625 break; |
|
626 case 2: |
|
627 { |
|
628 TChar shiftedChar( |
|
629 ( ( character & 0x03 ) << 6 ) | // bits 0 to 1 |
|
630 ( nextCharacter & 0x3F ) ); // bits 0 to 5 |
|
631 decodedPtr.Append( shiftedChar ); |
|
632 } |
|
633 break; |
|
634 default: /* case 3, skip */ |
|
635 break; |
|
636 } |
|
637 } |
|
638 return decoded; |
|
639 } |
|
640 |
|
641 // ----------------------------------------------------------------------------- |
|
642 // CPnpUtilImpl::DecodeCharL |
|
643 // ----------------------------------------------------------------------------- |
|
644 // |
|
645 TChar CPnpUtilImpl::DecodeCharL( const TChar aCharacter ) |
|
646 { |
|
647 TChar decodedChar('A'); |
|
648 |
|
649 if( aCharacter >= 'A' && aCharacter <= 'Z' ) |
|
650 { |
|
651 decodedChar = aCharacter - 'A'; |
|
652 } |
|
653 else if( aCharacter >= 'a' && aCharacter <= 'z' ) |
|
654 { |
|
655 decodedChar = aCharacter - 'a' + 26; |
|
656 } |
|
657 else if( aCharacter >= '0' && aCharacter <= '9' ) |
|
658 { |
|
659 decodedChar = aCharacter - '0' + 52; |
|
660 } |
|
661 else if( aCharacter == '+' ) |
|
662 { |
|
663 decodedChar = 62; |
|
664 } |
|
665 else if( aCharacter == '/' ) |
|
666 { |
|
667 decodedChar = 63; |
|
668 } |
|
669 else if( aCharacter != '=' ) |
|
670 { |
|
671 User::Leave( KErrCorrupt ); |
|
672 } |
|
673 return decodedChar; |
|
674 } |
|
675 |
|
676 // ----------------------------------------------------------------------------- |
|
677 // CPnpUtilImpl::PackLC |
|
678 // ----------------------------------------------------------------------------- |
|
679 // |
|
680 HBufC8* CPnpUtilImpl::PackLC( const TDesC8& aHex ) const |
|
681 { |
|
682 HBufC8* bin = HBufC8::NewLC( aHex.Length() / 2 ); |
|
683 TPtr8 binPtr( bin->Des() ); |
|
684 for( TInt i(0); i < aHex.Length() / 2; i++ ) |
|
685 { |
|
686 TLex8 lex( aHex.Mid( i * 2, 2 ) ); |
|
687 TUint8 byte(0); |
|
688 User::LeaveIfError( lex.Val( byte, EHex ) ); |
|
689 binPtr.Append( TUint8( byte ) ); |
|
690 } |
|
691 return bin; |
|
692 } |
|
693 |
|
694 // ----------------------------------------------------------------------------- |
|
695 // CPnpUtilImpl::Imsi |
|
696 // ----------------------------------------------------------------------------- |
|
697 // |
|
698 EXPORT_C void CPnpUtilImpl::ImsiL(RMobilePhone::TMobilePhoneSubscriberId& aImsi) const |
|
699 { |
|
700 // Get IMSI |
|
701 TRequestStatus status; |
|
702 iPhone.GetSubscriberId( status, aImsi ); |
|
703 User::WaitForRequest( status ); |
|
704 User::LeaveIfError( status.Int() ); |
|
705 } |
|
706 |
|
707 // ----------------------------------------------------------------------------- |
|
708 // CPnpUtilImpl::FetchHomeNetworkInfoL |
|
709 // ----------------------------------------------------------------------------- |
|
710 // |
|
711 EXPORT_C void CPnpUtilImpl::FetchHomeNetworkInfoL() |
|
712 { |
|
713 LOGSTRING( "FetchHomeNetworkInfoL"); |
|
714 |
|
715 // Get home (sim) MCC&MNC info |
|
716 RMobilePhone::TMobilePhoneNetworkInfoV1 info; |
|
717 RMobilePhone::TMobilePhoneNetworkInfoV1Pckg infoPckg( info ); |
|
718 #ifndef __WINS__ |
|
719 TRequestStatus status( KRequestPending ); |
|
720 iPhone.GetHomeNetwork( status, infoPckg ); |
|
721 User::WaitForRequest( status ); |
|
722 User::LeaveIfError( status.Int() ); |
|
723 #else |
|
724 info.iCountryCode.Copy( _L("244") ); |
|
725 info.iNetworkId.Copy( _L("05") ); |
|
726 #endif |
|
727 |
|
728 RMobilePhone::TMobilePhoneNetworkIdentity formattedMnc; |
|
729 FormatMncCodeL( info.iCountryCode, info.iNetworkId, formattedMnc ); |
|
730 SetHomeMncL( formattedMnc ); |
|
731 SetHomeMccL( info.iCountryCode ); |
|
732 |
|
733 LOGSTRING( "FetchHomeNetworkInfoL - done"); |
|
734 } |
|
735 |
|
736 // ----------------------------------------------------------------------------- |
|
737 // CPnpUtilImpl::FetchNetworkInfoL |
|
738 // ----------------------------------------------------------------------------- |
|
739 // |
|
740 EXPORT_C void CPnpUtilImpl::FetchNetworkInfoL() |
|
741 { |
|
742 LOGSTRING( "FetchNetworkInfoL"); |
|
743 |
|
744 // Get current (network) MCC&MNC info |
|
745 TRequestStatus status; |
|
746 LOGSTRING( "FetchNetworkInfoL 1"); |
|
747 RMobilePhone::TMobilePhoneNetworkInfoV1 info; |
|
748 RMobilePhone::TMobilePhoneNetworkInfoV1Pckg infoPckg( info ); |
|
749 LOGSTRING( "FetchNetworkInfoL 2"); |
|
750 status = KErrNone; |
|
751 LOGSTRING( "FetchNetworkInfoL 3"); |
|
752 |
|
753 iPhone.GetCurrentNetwork( status, infoPckg ); |
|
754 |
|
755 LOGSTRING( "FetchNetworkInfoL 4"); |
|
756 User::WaitForRequest( status ); |
|
757 LOGSTRING( "FetchNetworkInfoL 5"); |
|
758 User::LeaveIfError( status.Int() ); |
|
759 LOGSTRING( "FetchNetworkInfoL 6"); |
|
760 |
|
761 RMobilePhone::TMobilePhoneNetworkIdentity formattedMnc; |
|
762 FormatMncCodeL( info.iCountryCode, info.iNetworkId, formattedMnc ); |
|
763 LOGSTRING( "FetchNetworkInfoL 7"); |
|
764 SetNetworkMncL( formattedMnc ); |
|
765 LOGSTRING( "FetchNetworkInfoL 8"); |
|
766 SetNetworkMccL( info.iCountryCode ); |
|
767 |
|
768 LOGSTRING( "FetchNetworkInfoL - done"); |
|
769 } |
|
770 |
|
771 // ----------------------------------------------------------------------------- |
|
772 // CPnpUtilImpl::HomeMccL |
|
773 // ----------------------------------------------------------------------------- |
|
774 // |
|
775 EXPORT_C const RMobilePhone::TMobilePhoneNetworkCountryCode CPnpUtilImpl::HomeMccL() const |
|
776 { |
|
777 RMobilePhone::TMobilePhoneNetworkIdentity mcc; |
|
778 User::LeaveIfError( iRepository->Get( KPnPUtilHomeMcc, mcc ) ); |
|
779 LOGSTRING2( "HomeMccL %S", &mcc ); |
|
780 |
|
781 return mcc; |
|
782 } |
|
783 |
|
784 // ----------------------------------------------------------------------------- |
|
785 // CPnpUtilImpl::SetHomeMccL |
|
786 // ----------------------------------------------------------------------------- |
|
787 // |
|
788 EXPORT_C void CPnpUtilImpl::SetHomeMccL( const RMobilePhone::TMobilePhoneNetworkCountryCode aMcc ) |
|
789 { |
|
790 LOGSTRING2( "SetHomeMccL %S", &aMcc ); |
|
791 User::LeaveIfError( iRepository->Set( KPnPUtilHomeMcc, aMcc ) ); |
|
792 } |
|
793 |
|
794 // ----------------------------------------------------------------------------- |
|
795 // CPnpUtilImpl::HomeMncL |
|
796 // ----------------------------------------------------------------------------- |
|
797 // |
|
798 EXPORT_C const RMobilePhone::TMobilePhoneNetworkIdentity CPnpUtilImpl::HomeMncL() const |
|
799 { |
|
800 RMobilePhone::TMobilePhoneNetworkIdentity mnc; |
|
801 User::LeaveIfError( iRepository->Get( KPnPUtilHomeMnc, mnc ) ); |
|
802 LOGSTRING2( "HomeMncL %S", &mnc ); |
|
803 return mnc; |
|
804 } |
|
805 |
|
806 // ----------------------------------------------------------------------------- |
|
807 // CPnpUtilImpl::SetHomeMncL |
|
808 // ----------------------------------------------------------------------------- |
|
809 // |
|
810 EXPORT_C void CPnpUtilImpl::SetHomeMncL( const RMobilePhone::TMobilePhoneNetworkIdentity aMnc ) |
|
811 { |
|
812 LOGSTRING2( "SetHomeMncL %S", &aMnc ); |
|
813 User::LeaveIfError( iRepository->Set( KPnPUtilHomeMnc, aMnc ) ); |
|
814 } |
|
815 |
|
816 // ----------------------------------------------------------------------------- |
|
817 // CPnpUtilImpl::NetworkMccL |
|
818 // ----------------------------------------------------------------------------- |
|
819 // |
|
820 EXPORT_C const RMobilePhone::TMobilePhoneNetworkCountryCode CPnpUtilImpl::NetworkMccL() const |
|
821 { |
|
822 RMobilePhone::TMobilePhoneNetworkIdentity mcc; |
|
823 User::LeaveIfError( iRepository->Get( KPnPUtilNetworkMcc, mcc ) ); |
|
824 LOGSTRING2( "NetworkMccL %S", &mcc ); |
|
825 return mcc; |
|
826 } |
|
827 |
|
828 EXPORT_C void CPnpUtilImpl::SetNetworkMccL( const RMobilePhone::TMobilePhoneNetworkCountryCode aMcc ) |
|
829 { |
|
830 LOGSTRING2( "SetNetworkMccL %S", &aMcc ); |
|
831 User::LeaveIfError( iRepository->Set( KPnPUtilNetworkMcc, aMcc ) ); |
|
832 } |
|
833 |
|
834 // ----------------------------------------------------------------------------- |
|
835 // CPnpUtilImpl::NetworkMncL |
|
836 // ----------------------------------------------------------------------------- |
|
837 // |
|
838 EXPORT_C const RMobilePhone::TMobilePhoneNetworkIdentity CPnpUtilImpl::NetworkMncL() const |
|
839 { |
|
840 RMobilePhone::TMobilePhoneNetworkIdentity mnc; |
|
841 User::LeaveIfError( iRepository->Get( KPnPUtilNetworkMnc, mnc ) ); |
|
842 LOGSTRING2( "NetworkMncL %S", &mnc ); |
|
843 return mnc; |
|
844 } |
|
845 |
|
846 // ----------------------------------------------------------------------------- |
|
847 // CPnpUtilImpl::NetworkMncL |
|
848 // ----------------------------------------------------------------------------- |
|
849 // |
|
850 EXPORT_C void CPnpUtilImpl::SetNetworkMncL( const RMobilePhone::TMobilePhoneNetworkIdentity aMnc ) |
|
851 { |
|
852 LOGSTRING2( "SetNetworkMncL %S", &aMnc ); |
|
853 User::LeaveIfError( iRepository->Set( KPnPUtilNetworkMnc, aMnc ) ); |
|
854 } |
|
855 |
|
856 // ----------------------------------------------------------------------------- |
|
857 // CPnpUtilImpl::RegisteredInHomeNetworkL |
|
858 // ----------------------------------------------------------------------------- |
|
859 // |
|
860 EXPORT_C TBool CPnpUtilImpl::RegisteredInHomeNetworkL() |
|
861 { |
|
862 // Get registeration status |
|
863 TRequestStatus status; |
|
864 RMobilePhone::TMobilePhoneRegistrationStatus regstatus; |
|
865 iPhone.GetNetworkRegistrationStatus(status, regstatus); |
|
866 User::WaitForRequest( status ); |
|
867 User::LeaveIfError( status.Int() ); |
|
868 return (regstatus == RMobilePhone::ERegisteredOnHomeNetwork); |
|
869 } |
|
870 |
|
871 // ----------------------------------------------------------------------------- |
|
872 // CPnpUtilImpl::OperatorLongNameL |
|
873 // ----------------------------------------------------------------------------- |
|
874 // |
|
875 EXPORT_C TInt CPnpUtilImpl::OperatorLongName(RMobilePhone::TMobilePhoneNetworkLongName& aName) |
|
876 { |
|
877 // Get home (sim) MCC&MNC info |
|
878 TRequestStatus status; |
|
879 RMobilePhone::TMobilePhoneNetworkInfoV1 info; |
|
880 RMobilePhone::TMobilePhoneNetworkInfoV1Pckg infoPckg( info ); |
|
881 status = KErrNone; |
|
882 iPhone.GetHomeNetwork( status, infoPckg ); |
|
883 User::WaitForRequest( status ); |
|
884 aName.Zero(); |
|
885 aName.Append( info.iLongName ); |
|
886 return status.Int(); |
|
887 } |
|
888 |
|
889 // ----------------------------------------------------------------------------- |
|
890 // CPnpUtilImpl::StoreAccessPoint |
|
891 // ----------------------------------------------------------------------------- |
|
892 // |
|
893 EXPORT_C TInt CPnpUtilImpl::StoreAccessPoint(TUint32 /*aAP*/) |
|
894 { |
|
895 // Not used |
|
896 return KErrNotSupported; |
|
897 } |
|
898 |
|
899 // ----------------------------------------------------------------------------- |
|
900 // CPnpUtilImpl::FetchAccessPoint |
|
901 // ----------------------------------------------------------------------------- |
|
902 // |
|
903 TInt CPnpUtilImpl::FetchAccessPoint(TUint32& /*aAP*/) |
|
904 { |
|
905 // Not used |
|
906 return KErrNotSupported; |
|
907 } |
|
908 |
|
909 // ----------------------------------------------------------------------------- |
|
910 // CPnpUtilImpl::FormatMncCodeL |
|
911 // ----------------------------------------------------------------------------- |
|
912 // |
|
913 EXPORT_C void CPnpUtilImpl::FormatMncCodeL( |
|
914 const RMobilePhone::TMobilePhoneNetworkCountryCode aMcc, |
|
915 const RMobilePhone::TMobilePhoneNetworkIdentity aUnformattedMnc, |
|
916 RMobilePhone::TMobilePhoneNetworkIdentity& aFormattedMnc ) const |
|
917 { |
|
918 LOGSTRING("FormatMncCodeL"); |
|
919 LOGTEXT( aMcc ); |
|
920 LOGTEXT( aUnformattedMnc ); |
|
921 |
|
922 RMobilePhone::TMobilePhoneNetworkIdentity formattedMnc; |
|
923 |
|
924 // Check that codes only contain valid characters |
|
925 TInt mncValue(0); |
|
926 TInt mccValue(0); |
|
927 |
|
928 TLex mncParser( aUnformattedMnc ); |
|
929 TInt err = mncParser.Val( mncValue ); |
|
930 if( err != KErrNone ) |
|
931 { |
|
932 User::Leave( KErrCorrupt ); |
|
933 } |
|
934 |
|
935 TLex mccParser( aMcc ); |
|
936 err = mccParser.Val( mccValue ); |
|
937 if( err != KErrNone ) |
|
938 { |
|
939 User::Leave( KErrCorrupt ); |
|
940 } |
|
941 |
|
942 // In N7610 the MNC given by RMobilePhone::GetHomeNetwork is |
|
943 // always three digits |
|
944 // But for example in N6630 the MNC seems to be correctly formatted |
|
945 |
|
946 // The initial value is the given MNC: |
|
947 formattedMnc.Copy( aUnformattedMnc ); |
|
948 |
|
949 // Reformat only if needed |
|
950 if( aUnformattedMnc.Length() == 3 ) |
|
951 { |
|
952 // Assume two digits, exceptions follow |
|
953 formattedMnc.Copy( aUnformattedMnc.Left(2) ); |
|
954 |
|
955 if( aMcc.Compare( _L("302") ) == 0 || |
|
956 aMcc.Compare( _L("346") ) == 0 || |
|
957 aMcc.Compare( _L("348") ) == 0 || |
|
958 aMcc.Compare( _L("356") ) == 0 || |
|
959 aMcc.Compare( _L("365") ) == 0 || |
|
960 aMcc.Compare( _L("376") ) == 0 || |
|
961 aMcc.Compare( _L("467") ) == 0 || |
|
962 aMcc.Compare( _L("732") ) == 0 ) |
|
963 { |
|
964 // MNC should be three digits |
|
965 formattedMnc.Copy( aUnformattedMnc ); |
|
966 } |
|
967 else if( aMcc.Compare( _L("310") ) == 0 ) |
|
968 { |
|
969 // MNC is always three digits in this case |
|
970 // For example: 000, 011, 110, 020, 200, ... |
|
971 formattedMnc.Copy( aUnformattedMnc ); |
|
972 } |
|
973 else if( aMcc.Compare( _L("311") ) == 0 ) |
|
974 { |
|
975 // 3 digit (USA) some exceptions |
|
976 |
|
977 // See previous case for comments |
|
978 formattedMnc.Copy( aUnformattedMnc ); |
|
979 } |
|
980 |
|
981 else if( aMcc.Compare( _L("338") ) == 0 ) |
|
982 { |
|
983 // 2 digit, but 3 digit if 180 (JAM) |
|
984 if( aUnformattedMnc.Compare( _L("180") ) == 0 ) |
|
985 { |
|
986 formattedMnc.Copy( aUnformattedMnc ); |
|
987 } |
|
988 } |
|
989 else if( aMcc.Compare( _L("342") ) == 0 ) |
|
990 { |
|
991 // 2 digit, but 3 digit if larger than 51 |
|
992 if( mncValue >= 510 ) |
|
993 { |
|
994 formattedMnc.Copy( aUnformattedMnc ); |
|
995 } |
|
996 } |
|
997 else if( aMcc.Compare( _L("344") ) == 0 ) |
|
998 { |
|
999 // 2 digit, but 3 digit if larger than 31 |
|
1000 if( mncValue >= 310 ) |
|
1001 { |
|
1002 formattedMnc.Copy( aUnformattedMnc ); |
|
1003 } |
|
1004 } |
|
1005 else if( aMcc.Compare( _L("352") ) == 0 ) |
|
1006 { |
|
1007 // 2 digit, but 3 digit if smaller than 29 |
|
1008 if( mncValue < 300 ) |
|
1009 { |
|
1010 formattedMnc.Copy( aUnformattedMnc ); |
|
1011 } |
|
1012 } |
|
1013 else if( aMcc.Compare( _L("358") ) == 0 ) |
|
1014 { |
|
1015 // 2 digit, but 3 digit if smaller than 20 |
|
1016 if( mncValue < 300 ) |
|
1017 { |
|
1018 formattedMnc.Copy( aUnformattedMnc ); |
|
1019 } |
|
1020 } |
|
1021 else if( aMcc.Compare( _L("360") ) == 0 ) |
|
1022 { |
|
1023 // 2 digit, but 3 digit if 110 |
|
1024 if( aUnformattedMnc.Compare( _L("110") ) == 0 ) |
|
1025 { |
|
1026 formattedMnc.Copy( aUnformattedMnc ); |
|
1027 } |
|
1028 } |
|
1029 else if( aMcc.Compare( _L("362") ) == 0 ) |
|
1030 { |
|
1031 // 2 digit, but 3 digit if larger than 92 |
|
1032 if( mncValue >= 920 ) |
|
1033 { |
|
1034 formattedMnc.Copy( aUnformattedMnc ); |
|
1035 } |
|
1036 } |
|
1037 else if( aMcc.Compare( _L("366") ) == 0 ) |
|
1038 { |
|
1039 // 2 digit, but 3 digit if 110 |
|
1040 if( aUnformattedMnc.Compare( _L("110") ) == 0 ) |
|
1041 { |
|
1042 formattedMnc.Copy( aUnformattedMnc ); |
|
1043 } |
|
1044 } |
|
1045 else if( aMcc.Compare( _L("722") ) == 0 ) |
|
1046 { |
|
1047 // 2 digit, but 3 digit if 310 |
|
1048 if( aUnformattedMnc.Compare( _L("310") ) == 0 ) |
|
1049 { |
|
1050 formattedMnc.Copy( aUnformattedMnc ); |
|
1051 } |
|
1052 } |
|
1053 } |
|
1054 aFormattedMnc.Copy( formattedMnc ); |
|
1055 } |
|
1056 |
|
1057 // ----------------------------------------------------------------------------- |
|
1058 // CPnpUtilImpl::RESERVED_FUNC |
|
1059 // ----------------------------------------------------------------------------- |
|
1060 // |
|
1061 void CPnpUtilImpl::RESERVED_FUNC() |
|
1062 { |
|
1063 LOGSTRING("RESERVED_FUNC") |
|
1064 } |
|
1065 |
|
1066 // End of File |