breakdeps/DRMEngine/DrmStdKeyStorage.cpp
changeset 88 ca165d35976d
child 90 62156f66dbad
equal deleted inserted replaced
87:e9fb2728ea8b 88:ca165d35976d
       
     1 /*
       
     2 * Copyright (c) 2002-2010 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:  OMA DRM 2.0 Key Storage
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // INCLUDE FILES
       
    20 #include <e32std.h>
       
    21 #include <asymmetric.h>
       
    22 #include <symmetric.h>
       
    23 #include <hash.h>
       
    24 #include <asn1dec.h>
       
    25 #include <x509cert.h>
       
    26 #include <etelmm.h>
       
    27 #include <mmtsy_names.h>
       
    28 #include <featmgr.h>
       
    29 
       
    30 #ifdef RD_MULTIPLE_DRIVE
       
    31 #include <driveinfo.h>
       
    32 #endif
       
    33 
       
    34 #include "DrmKeyStorage.h"
       
    35 #include "DrmStdKeyStorage.h"
       
    36 
       
    37 #ifdef _DEBUG
       
    38 #define LOGGING
       
    39 #endif
       
    40 
       
    41 #ifdef LOGGING
       
    42 _LIT(KLogDir, "DRM");
       
    43 _LIT(KLogName, "KeyStorage.log");
       
    44 #include "flogger.h"
       
    45 #define LOG(string) \
       
    46     RFileLogger::Write(KLogDir, KLogName, \
       
    47         EFileLoggingModeAppend, string);
       
    48 #define LOGHEX(buffer) \
       
    49     RFileLogger::HexDump(KLogDir, KLogName, \
       
    50         EFileLoggingModeAppend, _S(""), _S(""), \
       
    51         buffer.Ptr(), buffer.Length());
       
    52 #else
       
    53 #define LOG(string)
       
    54 #define LOGHEX(buffer)
       
    55 #endif
       
    56 
       
    57 #pragma message("Compiling DRM Std KeyStorage..")
       
    58 
       
    59 
       
    60 // LOCAL CONSTANTS AND MACROS
       
    61 
       
    62 const TInt KKeyLength = 128;
       
    63 const TInt KWaitingTime = 2000000; // 2 sec
       
    64 
       
    65 #ifdef RD_MULTIPLE_DRIVE
       
    66 _LIT(KKeyStoragePath, "%c:\\private\\101F51F2\\PKI\\");
       
    67 _LIT(KRomKeyStoragePath, "%c:\\private\\101F51F2\\PKI\\");
       
    68 
       
    69 #else
       
    70 _LIT(KKeyStoragePath, "c:\\private\\101F51F2\\PKI\\");
       
    71 _LIT(KRomKeyStoragePath, "z:\\private\\101F51F2\\PKI\\");
       
    72 #endif
       
    73 
       
    74 _LIT(KDeviceKeyFileName, "DevicePrivateKey.der");
       
    75 _LIT(KDeviceCertFileName, "DeviceCert.der");
       
    76 _LIT(KSingingCertFmt, "SigningCert%02d.der");
       
    77 _LIT(KSingingCertPattern, "SigningCert*");
       
    78 _LIT8(KDefaultKey, "0000000000000000");
       
    79 
       
    80 #ifdef RD_MULTIPLE_DRIVE
       
    81 _LIT(KSerialNumberFile, "%c:\\private\\101F51F2\\rdbserial.dat");
       
    82 _LIT(KUdtCertFileName, "%c:\\private\\101F51F2\\PKI\\UdtCertificate.der");
       
    83 #else
       
    84 _LIT(KSerialNumberFile, "c:\\private\\101F51F2\\rdbserial.dat");
       
    85 _LIT(KUdtCertFileName, "z:\\private\\101F51F2\\PKI\\UdtCertificate.der");
       
    86 #endif
       
    87 
       
    88 _LIT(KCmla, "CMLA");
       
    89 _LIT(KPadding, "\x0");
       
    90 
       
    91 NONSHARABLE_STRUCT( TUnloadModule )
       
    92     {
       
    93     RTelServer* iServer;
       
    94     const TDesC* iName;
       
    95     };
       
    96 
       
    97 
       
    98 // ============================ LOCAL FUNCTIONS ================================
       
    99 LOCAL_C void DoUnloadPhoneModule( TAny* aAny );
       
   100 
       
   101 LOCAL_C void WriteFileL(RFs& aFs, const TDesC& aName, const TDesC8& aData)
       
   102     {
       
   103     RFile file;
       
   104 
       
   105     User::LeaveIfError(file.Replace(aFs, aName, EFileWrite));
       
   106     User::LeaveIfError(file.Write(aData));
       
   107     file.Close();
       
   108     }
       
   109 
       
   110 LOCAL_C void ReadFileL(RFs& aFs, const TDesC& aName, HBufC8*& aContent)
       
   111     {
       
   112     RFile file;
       
   113     TInt size = 0;
       
   114 
       
   115     User::LeaveIfError(file.Open(aFs, aName, EFileRead));
       
   116     CleanupClosePushL(file);
       
   117     User::LeaveIfError(file.Size(size));
       
   118     aContent = HBufC8::NewLC(size);
       
   119     TPtr8 ptr(aContent->Des());
       
   120     User::LeaveIfError(file.Read(ptr, size));
       
   121     CleanupStack::Pop(); //aContent
       
   122     CleanupStack::PopAndDestroy(); // file
       
   123     }
       
   124 
       
   125 HBufC8* I2OSPL(
       
   126     RInteger& aInt, TInt& aKeySize )
       
   127     {
       
   128     HBufC8* integer = aInt.BufferLC();
       
   129     if (integer->Length() < aKeySize)
       
   130         {
       
   131         HBufC8* r = HBufC8::NewLC(aKeySize);
       
   132         TPtr8 ptr(r->Des());
       
   133         for(TInt i = integer->Length(); i < aKeySize; i++)
       
   134             {
       
   135             ptr.Append(KPadding());
       
   136             }
       
   137         ptr.Append(*integer);
       
   138         CleanupStack::Pop(r);
       
   139         CleanupStack::PopAndDestroy(integer);
       
   140         return r;
       
   141         }
       
   142     else
       
   143         {
       
   144         CleanupStack::Pop(integer);
       
   145         return integer;
       
   146         }
       
   147     }
       
   148 
       
   149 RInteger OS2IPL(
       
   150     const TDesC8& aOctetStream)
       
   151     {
       
   152     RInteger r;
       
   153     TInt i;
       
   154 
       
   155     r = RInteger::NewL(0);
       
   156     for (i = 0; i < aOctetStream.Length(); i++)
       
   157         {
       
   158         r *= 256;
       
   159         r += aOctetStream[i];
       
   160         }
       
   161     return r;
       
   162     }
       
   163 
       
   164 void DoUnloadPhoneModule( TAny* aAny )
       
   165     {
       
   166     __ASSERT_DEBUG( aAny, User::Invariant() );
       
   167     TUnloadModule* module = ( TUnloadModule* ) aAny;
       
   168     module->iServer->UnloadPhoneModule( *( module->iName ) );
       
   169     }
       
   170 
       
   171 
       
   172 // ============================ MEMBER FUNCTIONS ===============================
       
   173 
       
   174 // -----------------------------------------------------------------------------
       
   175 // CDrmStdKeyStorage* CDrmStdKeyStorage::NewL
       
   176 //
       
   177 // -----------------------------------------------------------------------------
       
   178 //
       
   179 EXPORT_C CDrmStdKeyStorage* CDrmStdKeyStorage::NewL(RLibrary aLibrary)
       
   180     {
       
   181     CDrmStdKeyStorage* self = new (ELeave) CDrmStdKeyStorage(aLibrary);
       
   182     CleanupStack::PushL(self);
       
   183     self->ConstructL();
       
   184     CleanupStack::Pop();
       
   185     return self;
       
   186     }
       
   187 
       
   188 // -----------------------------------------------------------------------------
       
   189 // CDrmStdKeyStorage::CDrmStdKeyStorage
       
   190 //
       
   191 // -----------------------------------------------------------------------------
       
   192 //
       
   193 CDrmStdKeyStorage::CDrmStdKeyStorage(RLibrary aLibrary):
       
   194     iFileMan(NULL),
       
   195     iRootSelected(EFalse),
       
   196     iKey(NULL),
       
   197     iImei(NULL),
       
   198     iLibrary(aLibrary)
       
   199     {
       
   200     }
       
   201 
       
   202 // -----------------------------------------------------------------------------
       
   203 // CDrmStdKeyStorage::ConstructL
       
   204 //
       
   205 // -----------------------------------------------------------------------------
       
   206 //
       
   207 void CDrmStdKeyStorage::ConstructL()
       
   208     {
       
   209 
       
   210     LOG(_L("CDrmStdKeyStorage::ConstructL ->"));
       
   211     User::LeaveIfError(iFs.Connect());
       
   212     iFileMan = CFileMan::NewL(iFs);
       
   213 
       
   214     FeatureManager::InitializeLibL();
       
   215     
       
   216 #ifdef __DRM_OMA2 
       
   217     if ( FeatureManager::FeatureSupported( KFeatureIdFfOmadrm2Support ) )
       
   218         {
       
   219         TRAP_IGNORE( SelectDefaultRootL() );
       
   220         }
       
   221 #endif
       
   222     
       
   223     FeatureManager::UnInitializeLib();
       
   224     
       
   225     iDeviceSpecificKey.Copy(KDefaultKey);
       
   226 
       
   227     LOG(_L("CDrmStdKeyStorage::ConstructL <-"));
       
   228     }
       
   229 
       
   230 // -----------------------------------------------------------------------------
       
   231 // MDrmKeyStorage::~MDrmKeyStorage
       
   232 //
       
   233 // -----------------------------------------------------------------------------
       
   234 //
       
   235 
       
   236 MDrmKeyStorage::~MDrmKeyStorage()
       
   237     {
       
   238     }
       
   239 // -----------------------------------------------------------------------------
       
   240 // DrmStdKeyStorage::~CDrmStdKeyStorage
       
   241 //
       
   242 // -----------------------------------------------------------------------------
       
   243 //
       
   244 
       
   245 CDrmStdKeyStorage::~CDrmStdKeyStorage()
       
   246     {
       
   247     LOG(_L("CDrmStdKeyStorage::~CDrmStdKeyStorage ->"));
       
   248     delete iFileMan;
       
   249     delete iKey;
       
   250     delete iImei; iImei = NULL;
       
   251     iFs.Close();
       
   252     //iLibrary.Close();
       
   253     LOG(_L("CDrmStdKeyStorage::~CDrmStdKeyStorage <-"));
       
   254     }
       
   255 
       
   256 // -----------------------------------------------------------------------------
       
   257 // DrmStdKeyStorage::ModulusSize
       
   258 //
       
   259 // -----------------------------------------------------------------------------
       
   260 //
       
   261 
       
   262 TInt CDrmStdKeyStorage::ModulusSize()
       
   263     {
       
   264     LOG(_L("CDrmStdKeyStorage::ModulusSize ->"));
       
   265 
       
   266     if(iKey == NULL)
       
   267         {
       
   268         return KErrGeneral;
       
   269         }
       
   270     LOG(_L("CDrmStdKeyStorage::ModulusSize <-"));
       
   271     return iKey->N().BitCount();
       
   272     }
       
   273 
       
   274 // -----------------------------------------------------------------------------
       
   275 // DrmStdKeyStorage::SelectTrustedRootL
       
   276 //
       
   277 // -----------------------------------------------------------------------------
       
   278 //
       
   279 void CDrmStdKeyStorage::SelectTrustedRootL(
       
   280     const TDesC8& aRootKeyHash)
       
   281     {
       
   282     TFileName fileName;
       
   283     TEntry entry;
       
   284     TInt i;
       
   285 
       
   286     LOG(_L("CDrmStdKeyStorage::SelectTrustedRootL ->"));
       
   287     LOG(aRootKeyHash);
       
   288     if (aRootKeyHash.Length() != 0)
       
   289         {
       
   290 
       
   291 #ifndef RD_MULTIPLE_DRIVE
       
   292 
       
   293         fileName.Copy(KKeyStoragePath);
       
   294 
       
   295 #else //RD_MULTIPLE_DRIVE
       
   296 
       
   297         TFileName tempPath;
       
   298         TInt driveNumber( -1 );
       
   299         TChar driveLetter;
       
   300         DriveInfo::GetDefaultDrive( DriveInfo::EDefaultSystem, driveNumber );
       
   301         iFs.DriveToChar( driveNumber, driveLetter );
       
   302 
       
   303         tempPath.Format( KKeyStoragePath, (TUint)driveLetter );
       
   304 
       
   305         fileName.Copy(tempPath);
       
   306 
       
   307 #endif
       
   308 
       
   309         for (i = 0; i < SHA1_HASH; i++)
       
   310             {
       
   311             fileName.AppendNumFixedWidth(aRootKeyHash[i], EHex, 2);
       
   312             }
       
   313         fileName.Append('\\');
       
   314         if (iFs.Entry(fileName, entry) != KErrNone)
       
   315             {
       
   316 
       
   317 #ifndef RD_MULTIPLE_DRIVE
       
   318 
       
   319             fileName.Copy(KRomKeyStoragePath);
       
   320 
       
   321 #else //RD_MULTIPLE_DRIVE
       
   322 
       
   323             DriveInfo::GetDefaultDrive( DriveInfo::EDefaultRom, driveNumber );
       
   324             iFs.DriveToChar( driveNumber, driveLetter );
       
   325 
       
   326             tempPath.Format( KRomKeyStoragePath, (TUint)driveLetter );
       
   327 
       
   328             fileName.Copy(tempPath);
       
   329 
       
   330 #endif
       
   331 
       
   332             for (i = 0; i < SHA1_HASH; i++)
       
   333                 {
       
   334                 fileName.AppendNumFixedWidth(aRootKeyHash[i], EHex, 2);
       
   335                 }
       
   336             fileName.Append('\\');
       
   337             // check that the path exists
       
   338             User::LeaveIfError(iFs.Entry(fileName, entry));
       
   339             }
       
   340         User::LeaveIfError(iFs.SetSessionPath(fileName));
       
   341         InitializeKeyL();
       
   342         CheckRootForCmlaL();
       
   343         iRootSelected = ETrue;
       
   344         }
       
   345     else
       
   346         {
       
   347         SelectDefaultRootL();
       
   348         }
       
   349     LOG(_L("CDrmStdKeyStorage::SelectTrustedRootL <-"));
       
   350     }
       
   351 
       
   352 // -----------------------------------------------------------------------------
       
   353 // DrmStdKeyStorage::SelectDefaultRootL
       
   354 //
       
   355 // -----------------------------------------------------------------------------
       
   356 //
       
   357 void CDrmStdKeyStorage::SelectDefaultRootL()
       
   358     {
       
   359     CDir* dir = NULL;
       
   360     TFileName dirName;
       
   361     TBool found = EFalse;
       
   362 
       
   363     LOG(_L("CDrmStdKeyStorage::SelectDefaultRootL ->"));
       
   364 
       
   365 #ifndef RD_MULTIPLE_DRIVE
       
   366 
       
   367     if (iFs.GetDir(KKeyStoragePath, KEntryAttDir, ESortByName, dir) == KErrNone)
       
   368 
       
   369 #else //RD_MULTIPLE_DRIVE
       
   370 
       
   371     TFileName tempPath;
       
   372     TInt driveNumber( -1 );
       
   373     TChar driveLetter;
       
   374     DriveInfo::GetDefaultDrive( DriveInfo::EDefaultSystem, driveNumber );
       
   375     iFs.DriveToChar( driveNumber, driveLetter );
       
   376 
       
   377     tempPath.Format( KKeyStoragePath, (TUint)driveLetter );
       
   378 
       
   379     if (iFs.GetDir(tempPath, KEntryAttDir, ESortByName, dir) == KErrNone)
       
   380 
       
   381 #endif
       
   382 
       
   383         {
       
   384         __UHEAP_MARK;
       
   385         LOG(_L("  Checking keys on C:"));
       
   386         CleanupStack::PushL(dir);
       
   387         if (dir->Count() >= 1)
       
   388             {
       
   389 
       
   390 #ifndef RD_MULTIPLE_DRIVE
       
   391 
       
   392             dirName.Copy(KKeyStoragePath);
       
   393 
       
   394 #else //RD_MULTIPLE_DRIVE
       
   395 
       
   396             dirName.Copy(tempPath);
       
   397 
       
   398 #endif
       
   399 
       
   400             dirName.Append((*dir)[0].iName);
       
   401             dirName.Append('\\');
       
   402             User::LeaveIfError(iFs.SetSessionPath(dirName));
       
   403             found = ETrue;
       
   404             }
       
   405         CleanupStack::PopAndDestroy(dir);
       
   406         __UHEAP_MARKEND;
       
   407         }
       
   408 
       
   409 #ifndef RD_MULTIPLE_DRIVE
       
   410 
       
   411     if (!found && iFs.GetDir(KRomKeyStoragePath, KEntryAttDir, ESortByName, dir) == KErrNone)
       
   412 
       
   413 #else //RD_MULTIPLE_DRIVE
       
   414 
       
   415     DriveInfo::GetDefaultDrive( DriveInfo::EDefaultRom, driveNumber );
       
   416     iFs.DriveToChar( driveNumber, driveLetter );
       
   417 
       
   418     tempPath.Format( KRomKeyStoragePath, (TUint)driveLetter );
       
   419 
       
   420     if (!found && iFs.GetDir(tempPath, KEntryAttDir, ESortByName, dir) == KErrNone)
       
   421 
       
   422 #endif
       
   423         {
       
   424         LOG(_L("  Checking keys on Z:"));
       
   425         CleanupStack::PushL(dir);
       
   426         if (dir->Count() < 1)
       
   427             {
       
   428             User::Leave(KErrGeneral);
       
   429             }
       
   430 
       
   431 #ifndef RD_MULTIPLE_DRIVE
       
   432 
       
   433         dirName.Copy(KRomKeyStoragePath);
       
   434 
       
   435 #else //RD_MULTIPLE_DRIVE
       
   436 
       
   437         dirName.Copy(tempPath);
       
   438 
       
   439 #endif
       
   440 
       
   441         dirName.Append((*dir)[0].iName);
       
   442         dirName.Append('\\');
       
   443         User::LeaveIfError(iFs.SetSessionPath(dirName));
       
   444         CleanupStack::PopAndDestroy(dir);
       
   445         found = ETrue;
       
   446         }
       
   447     if (!found)
       
   448         {
       
   449         User::Leave(KErrGeneral);
       
   450         }
       
   451     InitializeKeyL();
       
   452     CheckRootForCmlaL();
       
   453     iRootSelected = ETrue;
       
   454     LOG(_L("CDrmStdKeyStorage::SelectDefaultRootL <-"));
       
   455     }
       
   456 
       
   457 TBool CDrmStdKeyStorage::SelectedRootIsCmla()
       
   458     {
       
   459     return iRootIsCmla;
       
   460     }
       
   461 
       
   462 // -----------------------------------------------------------------------------
       
   463 // DrmStdKeyStorage::GetTrustedRootsL
       
   464 //
       
   465 // -----------------------------------------------------------------------------
       
   466 //
       
   467 
       
   468 void CDrmStdKeyStorage::GetTrustedRootsL(
       
   469     RPointerArray<HBufC8>& aRootList)
       
   470     {
       
   471     CDir* dir = NULL;
       
   472     TInt i;
       
   473     TInt j;
       
   474     TBuf8<SHA1_HASH> hash;
       
   475     TEntry entry;
       
   476     TUint8 c;
       
   477     TInt r = KErrNone;
       
   478 
       
   479     LOG(_L("CDrmStdKeyStorage::GetTrustedRootsL ->"));
       
   480     aRootList.ResetAndDestroy();
       
   481 
       
   482 #ifndef RD_MULTIPLE_DRIVE
       
   483 
       
   484     if (iFs.GetDir(KKeyStoragePath, KEntryAttDir, ESortByName, dir) == KErrNone)
       
   485 
       
   486 #else //RD_MULTIPLE_DRIVE
       
   487 
       
   488     TFileName tempPath;
       
   489     TInt driveNumber( -1 );
       
   490     TChar driveLetter;
       
   491     DriveInfo::GetDefaultDrive( DriveInfo::EDefaultSystem, driveNumber );
       
   492     iFs.DriveToChar( driveNumber, driveLetter );
       
   493 
       
   494     tempPath.Format( KKeyStoragePath, (TUint)driveLetter );
       
   495 
       
   496     if (iFs.GetDir(tempPath, KEntryAttDir, ESortByName, dir) == KErrNone)
       
   497 
       
   498 #endif
       
   499         {
       
   500         LOG(_L("  Getting roots on C:"));
       
   501         CleanupStack::PushL(dir);
       
   502         for (i = 0; i < dir->Count(); i++)
       
   503             {
       
   504             entry = (*dir)[i];
       
   505             hash.SetLength(0);
       
   506             LOG(entry.iName);
       
   507             r = KErrNone;
       
   508             for (j = 0; r == KErrNone && j < SHA1_HASH; j++)
       
   509                 {
       
   510                 TLex lex(entry.iName.Mid(j * 2, 2));
       
   511                 r = lex.Val(c, EHex);
       
   512                 hash.Append(c);
       
   513                 }
       
   514             if (r == KErrNone)
       
   515                 {
       
   516                 aRootList.Append(hash.AllocL());
       
   517                 }
       
   518             }
       
   519         CleanupStack::PopAndDestroy(dir);
       
   520         }
       
   521 
       
   522 #ifndef RD_MULTIPLE_DRIVE
       
   523 
       
   524     if (iFs.GetDir(KRomKeyStoragePath, KEntryAttDir, ESortByName, dir) == KErrNone)
       
   525 
       
   526 #else //RD_MULTIPLE_DRIVE
       
   527 
       
   528     DriveInfo::GetDefaultDrive( DriveInfo::EDefaultRom, driveNumber );
       
   529     iFs.DriveToChar( driveNumber, driveLetter );
       
   530 
       
   531     tempPath.Format( KRomKeyStoragePath, (TUint)driveLetter );
       
   532 
       
   533     if (iFs.GetDir(tempPath, KEntryAttDir, ESortByName, dir) == KErrNone)
       
   534 
       
   535 #endif
       
   536         {
       
   537         LOG(_L("  Getting roots on Z:"));
       
   538         CleanupStack::PushL(dir);
       
   539         for (i = 0; i < dir->Count(); i++)
       
   540             {
       
   541             LOG(entry.iName);
       
   542             entry = (*dir)[i];
       
   543             hash.SetLength(0);
       
   544             r = KErrNone;
       
   545             for (j = 0; r == KErrNone && j < SHA1_HASH; j++)
       
   546                 {
       
   547                 TLex lex(entry.iName.Mid(j * 2, 2));
       
   548                 r = lex.Val(c, EHex);
       
   549                 hash.Append(c);
       
   550                 }
       
   551             if (r == KErrNone)
       
   552                 {
       
   553                 aRootList.Append(hash.AllocL());
       
   554                 }
       
   555             }
       
   556         CleanupStack::PopAndDestroy(dir);
       
   557         }
       
   558     LOG(_L("CDrmStdKeyStorage::GetTrustedRootsL <-"));
       
   559     }
       
   560 
       
   561 // -----------------------------------------------------------------------------
       
   562 // DrmStdKeyStorage::GetCertificateChainL
       
   563 //
       
   564 // -----------------------------------------------------------------------------
       
   565 //
       
   566 void CDrmStdKeyStorage::GetCertificateChainL(
       
   567     RPointerArray<HBufC8>& aCertChain)
       
   568     {
       
   569     TFileName fileName;
       
   570     TInt i;
       
   571     CDir* dir = NULL;
       
   572     HBufC8* cert = NULL;
       
   573 
       
   574     LOG(_L("CDrmStdKeyStorage::GetCertificateChainL ->"));
       
   575     if (!iRootSelected)
       
   576         {
       
   577         User::Leave(KErrGeneral);
       
   578         }
       
   579     aCertChain.ResetAndDestroy();
       
   580     ReadFileL(iFs, KDeviceCertFileName, cert);
       
   581     aCertChain.Append(cert);
       
   582     iFs.GetDir(KSingingCertPattern, KEntryAttNormal, ESortByName, dir);
       
   583     CleanupStack::PushL(dir);
       
   584     for (i = 0; i < dir->Count(); i++)
       
   585         {
       
   586         ReadFileL(iFs, (*dir)[i].iName, cert);
       
   587         aCertChain.AppendL(cert);
       
   588         }
       
   589     CleanupStack::PopAndDestroy(); // dir
       
   590     LOG(_L("CDrmStdKeyStorage::GetCertificateChainL <-"));
       
   591     }
       
   592 
       
   593 // -----------------------------------------------------------------------------
       
   594 // CDrmStdKeyStorage::RsaSignL
       
   595 // -----------------------------------------------------------------------------
       
   596 //
       
   597 HBufC8* CDrmStdKeyStorage::RsaSignL(
       
   598     const TDesC8& aInput)
       
   599     {
       
   600     return ModularExponentiateWithKeyL(aInput);
       
   601     }
       
   602 
       
   603 // -----------------------------------------------------------------------------
       
   604 // CDrmStdKeyStorage::RsaDecryptL
       
   605 // -----------------------------------------------------------------------------
       
   606 //
       
   607 HBufC8* CDrmStdKeyStorage::RsaDecryptL(
       
   608     const TDesC8& aInput)
       
   609     {
       
   610     return ModularExponentiateWithKeyL(aInput);
       
   611     }
       
   612 
       
   613 // -----------------------------------------------------------------------------
       
   614 // CDrmStdKeyStorage::ImportDataL
       
   615 // -----------------------------------------------------------------------------
       
   616 //
       
   617 void CDrmStdKeyStorage::ImportDataL(
       
   618     const TDesC8& aPrivateKey,
       
   619     const RArray<TPtrC8>& aCertificateChain)
       
   620     {
       
   621     TInt i;
       
   622     TInt n;
       
   623     HBufC8* publicKey = NULL;
       
   624     CX509Certificate* cert = NULL;
       
   625     CSHA1* hasher = NULL;
       
   626     TBuf8<SHA1_HASH> publicKeyHash;
       
   627     TFileName fileName;
       
   628 
       
   629     LOG(_L("CDrmStdKeyStorage::ImportDataL ->"));
       
   630     n = aCertificateChain.Count();
       
   631     cert = CX509Certificate::NewLC(aCertificateChain[n - 1]);
       
   632     publicKey = cert->DataElementEncoding(
       
   633         CX509Certificate::ESubjectPublicKeyInfo)->AllocL();
       
   634     CleanupStack::PushL(publicKey);
       
   635     hasher = CSHA1::NewL();
       
   636     CleanupStack::PushL(hasher);
       
   637     hasher->Update(*publicKey);
       
   638     publicKeyHash.Copy(hasher->Final());
       
   639 
       
   640 #ifndef RD_MULTIPLE_DRIVE
       
   641 
       
   642     fileName.Copy(KKeyStoragePath);
       
   643 
       
   644 #else //RD_MULTIPLE_DRIVE
       
   645 
       
   646     TInt driveNumber( -1 );
       
   647     TChar driveLetter;
       
   648     DriveInfo::GetDefaultDrive( DriveInfo::EDefaultSystem, driveNumber );
       
   649     iFs.DriveToChar( driveNumber, driveLetter );
       
   650 
       
   651     TFileName keyStorageDir;
       
   652     keyStorageDir.Format( KKeyStoragePath, (TUint)driveLetter );
       
   653 
       
   654     fileName.Copy(keyStorageDir);
       
   655 
       
   656 #endif
       
   657 
       
   658     for (i = 0; i < SHA1_HASH; i++)
       
   659         {
       
   660         fileName.AppendNumFixedWidth(publicKeyHash[i], EHex, 2);
       
   661         }
       
   662     fileName.Append('\\');
       
   663     iFileMan->Delete(fileName, CFileMan::ERecurse);
       
   664     iFs.MkDirAll(fileName);
       
   665     iFs.SetSessionPath(fileName);
       
   666     WriteFileL(iFs, KDeviceKeyFileName, aPrivateKey);
       
   667     fileName.Copy(fileName);
       
   668     WriteFileL(iFs, KDeviceCertFileName, aCertificateChain[0]);
       
   669     for (i = 1; i < n; i++)
       
   670         {
       
   671         fileName.SetLength(0);
       
   672         fileName.AppendFormat(KSingingCertFmt, i - 1);
       
   673         WriteFileL(iFs, fileName, aCertificateChain[i]);
       
   674         }
       
   675     CleanupStack::PopAndDestroy(3); // hasher, publicKey, cert
       
   676     LOG(_L("CDrmStdKeyStorage::ImportDataL <-"));
       
   677     }
       
   678 
       
   679 // -----------------------------------------------------------------------------
       
   680 // CDrmStdKeyStorage::GetDeviceSpecificKeyL
       
   681 // -----------------------------------------------------------------------------
       
   682 //
       
   683 void CDrmStdKeyStorage::GetDeviceSpecificKeyL(
       
   684     TBuf8<KDeviceSpecificKeyLength>& aKey)
       
   685     {
       
   686 
       
   687     HBufC8* key = NULL;
       
   688     TInt n;
       
   689     CSHA1* hasher = NULL;
       
   690     TBuf8<SHA1_HASH> hash;
       
   691 
       
   692     if (iDeviceSpecificKey.Compare(KDefaultKey) == 0)
       
   693         {
       
   694 
       
   695         GetImeiL();
       
   696 
       
   697         HBufC8* buf = HBufC8::NewLC( iImei->Size() + sizeof(VID_DEFAULT) );
       
   698         TPtr8 ptr( buf->Des() );
       
   699         ptr.Copy( *iImei );
       
   700         ptr.Append(VID_DEFAULT);
       
   701 
       
   702         hasher = CSHA1::NewL();
       
   703         CleanupStack::PushL(hasher);
       
   704         hasher->Update(ptr);
       
   705         hash.Copy(hasher->Final());
       
   706         key=hash.AllocL();
       
   707         CleanupStack::PopAndDestroy(2,buf); // hasher,buf;
       
   708 
       
   709         n = Min(key->Length(), KDeviceSpecificKeyLength);
       
   710         iDeviceSpecificKey.Copy(key->Right(n));
       
   711         delete key;
       
   712         n = KDeviceSpecificKeyLength - n;
       
   713         while (n > 0)
       
   714             {
       
   715             iDeviceSpecificKey.Append(0);
       
   716             n--;
       
   717             }
       
   718         }
       
   719 
       
   720     aKey.Copy(iDeviceSpecificKey);
       
   721     }
       
   722 
       
   723 // -----------------------------------------------------------------------------
       
   724 // CDrmStdKeyStorage::InitializeKeyL
       
   725 // -----------------------------------------------------------------------------
       
   726 //
       
   727 void CDrmStdKeyStorage::InitializeKeyL()
       
   728     {
       
   729     HBufC8* key = NULL;
       
   730     TASN1DecInteger encInt;
       
   731     TInt pos = 0;
       
   732 
       
   733     LOG(_L("CDrmStdKeyStorage::InitializeKeyL ->"));
       
   734     delete iKey;
       
   735     iKey = NULL;
       
   736     ReadFileL(iFs, KDeviceKeyFileName, key);
       
   737     CleanupStack::PushL(key);
       
   738     TASN1DecGeneric gen(*key);
       
   739     gen.InitL();
       
   740     pos += gen.LengthDERHeader();
       
   741     if (gen.Tag() != EASN1Sequence)
       
   742         {
       
   743         User::Leave(KErrArgument);
       
   744         }
       
   745     encInt.DecodeDERShortL(*key, pos); // version
       
   746     RInteger modulus = encInt.DecodeDERLongL(*key, pos);
       
   747     CleanupStack::PushL(modulus);
       
   748     RInteger publicExponent = encInt.DecodeDERLongL(*key, pos);
       
   749     CleanupStack::PushL(publicExponent);
       
   750     RInteger privateExponent = encInt.DecodeDERLongL(*key, pos);
       
   751     CleanupStack::PushL(privateExponent);
       
   752     iKey = CRSAPrivateKeyStandard::NewL(modulus, privateExponent);
       
   753     CleanupStack::Pop(); // privateExponent
       
   754     CleanupStack::PopAndDestroy();// publicExponent
       
   755     CleanupStack::Pop(); // modulus
       
   756     CleanupStack::PopAndDestroy(); // key
       
   757     LOG(_L("CDrmStdKeyStorage::InitializeKeyL <-"));
       
   758     }
       
   759 
       
   760 // -----------------------------------------------------------------------------
       
   761 // CDrmStdKeyStorage::ModularExponentiateWithKeyL
       
   762 // -----------------------------------------------------------------------------
       
   763 //
       
   764 HBufC8* CDrmStdKeyStorage::ModularExponentiateWithKeyL(
       
   765     const TDesC8& aInput)
       
   766     {
       
   767     RInteger result;
       
   768     RInteger input;
       
   769     HBufC8* output;
       
   770     TInt keyLength = KKeyLength;
       
   771 
       
   772     LOG(_L("CDrmStdKeyStorage::ModularExponentiateWithKeyL ->"));
       
   773     input = OS2IPL(aInput);
       
   774     CleanupClosePushL(input);
       
   775     result = TInteger::ModularExponentiateL(input,iKey->D(), iKey->N());
       
   776     CleanupClosePushL(result);
       
   777     output = I2OSPL(result,  keyLength);
       
   778     CleanupStack::PopAndDestroy(2); // result, input
       
   779     LOG(_L("CDrmStdKeyStorage::ModularExponentiateWithKeyL <-"));
       
   780     return output;
       
   781     }
       
   782 
       
   783 // -----------------------------------------------------------------------------
       
   784 // CDrmStdKeyStorage::CheckRootForCmlaL
       
   785 // -----------------------------------------------------------------------------
       
   786 //
       
   787 void CDrmStdKeyStorage::CheckRootForCmlaL()
       
   788     {
       
   789     CDir* dir = NULL;
       
   790     HBufC8* buffer = NULL;
       
   791     HBufC* name = NULL;
       
   792     CX509Certificate* cert = NULL;
       
   793 
       
   794     LOG(_L("CDrmStdKeyStorage::CheckRootForCmlaL ->"));
       
   795     __UHEAP_MARK;
       
   796     iFs.GetDir(KSingingCertPattern, KEntryAttNormal, ESortByName, dir);
       
   797     CleanupStack::PushL(dir);
       
   798     ReadFileL(iFs, (*dir)[dir->Count() - 1].iName, buffer);
       
   799     CleanupStack::PushL(buffer);
       
   800     cert = CX509Certificate::NewL(*buffer);
       
   801     CleanupStack::PushL(cert);
       
   802     name = cert->SubjectName().DisplayNameL();
       
   803     CleanupStack::PushL(name);
       
   804     if (name->Find(KCmla) != KErrNotFound)
       
   805         {
       
   806         iRootIsCmla = ETrue;
       
   807         }
       
   808     else
       
   809         {
       
   810         iRootIsCmla = EFalse;
       
   811         }
       
   812     CleanupStack::PopAndDestroy(4); // name, cert, buffer, dir
       
   813     LOG(_L("CDrmStdKeyStorage::CheckRootForCmlaL <-"));
       
   814     __UHEAP_MARKEND;
       
   815     }
       
   816 
       
   817 // -----------------------------------------------------------------------------
       
   818 // CDrmStdKeyStorage::GetRdbSerialNumberL
       
   819 // -----------------------------------------------------------------------------
       
   820 //
       
   821 void CDrmStdKeyStorage::GetRdbSerialNumberL(
       
   822     TBuf8<KRdbSerialNumberLength>& aSerialNumber)
       
   823     {
       
   824     HBufC8* buffer = NULL;
       
   825     TUint att;
       
   826 
       
   827 #ifndef RD_MULTIPLE_DRIVE
       
   828 
       
   829     if (iFs.Att(KSerialNumberFile, att) != KErrNone)
       
   830 
       
   831 #else //RD_MULTIPLE_DRIVE
       
   832 
       
   833     TInt driveNumber( -1 );
       
   834     TChar driveLetter;
       
   835     DriveInfo::GetDefaultDrive( DriveInfo::EDefaultSystem, driveNumber );
       
   836     iFs.DriveToChar( driveNumber, driveLetter );
       
   837 
       
   838     TFileName serialNoFile;
       
   839     serialNoFile.Format( KSerialNumberFile, (TUint)driveLetter );
       
   840 
       
   841     if (iFs.Att(serialNoFile, att) != KErrNone)
       
   842 
       
   843 #endif
       
   844         {
       
   845         GenerateNewRdbSerialNumberL();
       
   846         }
       
   847 
       
   848 #ifndef RD_MULTIPLE_DRIVE
       
   849 
       
   850     ReadFileL(iFs, KSerialNumberFile, buffer);
       
   851 
       
   852 #else //RD_MULTIPLE_DRIVE
       
   853 
       
   854     ReadFileL(iFs, serialNoFile, buffer);
       
   855 
       
   856 #endif
       
   857 
       
   858     aSerialNumber.Copy(*buffer);
       
   859     delete buffer;
       
   860     }
       
   861 
       
   862 // -----------------------------------------------------------------------------
       
   863 // CDrmStdKeyStorage::GenerateNewRdbSerialNumberL
       
   864 // -----------------------------------------------------------------------------
       
   865 //
       
   866 void CDrmStdKeyStorage::GenerateNewRdbSerialNumberL()
       
   867     {
       
   868     TBuf8<KRdbSerialNumberLength> serialNumber;
       
   869     TPtr8 random( const_cast<TUint8*>(serialNumber.Ptr()),
       
   870                   KRdbSerialNumberLength,
       
   871                   KRdbSerialNumberLength );
       
   872 
       
   873     RandomDataGetL(random,KRdbSerialNumberLength);
       
   874 
       
   875 #ifndef RD_MULTIPLE_DRIVE
       
   876 
       
   877     WriteFileL(iFs, KSerialNumberFile, random);
       
   878 
       
   879 #else //RD_MULTIPLE_DRIVE
       
   880 
       
   881     TInt driveNumber( -1 );
       
   882     TChar driveLetter;
       
   883     DriveInfo::GetDefaultDrive( DriveInfo::EDefaultSystem, driveNumber );
       
   884     iFs.DriveToChar( driveNumber, driveLetter );
       
   885 
       
   886     TFileName serialNoFile;
       
   887     serialNoFile.Format( KSerialNumberFile, (TUint)driveLetter );
       
   888 
       
   889     WriteFileL(iFs, serialNoFile, random);
       
   890 
       
   891 #endif
       
   892 
       
   893     }
       
   894 
       
   895 // -----------------------------------------------------------------------------
       
   896 // CDrmStdKeyStorage::UdtEncryptL
       
   897 // -----------------------------------------------------------------------------
       
   898 //
       
   899 HBufC8* CDrmStdKeyStorage::UdtEncryptL(
       
   900     const TDesC8& aInput)
       
   901     {
       
   902     HBufC8* buffer = NULL;
       
   903     HBufC8* output = HBufC8::NewMaxLC( 256 );
       
   904     CX509Certificate* cert = NULL;
       
   905     CRSAPublicKey* key = NULL;
       
   906     TX509KeyFactory factory;
       
   907     CRSAPKCS1v15Encryptor* encryptor = NULL;
       
   908     TPtr8 result(const_cast<TUint8*>(output->Ptr()), 0, 256);
       
   909 
       
   910 #ifndef RD_MULTIPLE_DRIVE
       
   911 
       
   912     ReadFileL(iFs, KUdtCertFileName, buffer);
       
   913 
       
   914 #else //RD_MULTIPLE_DRIVE
       
   915 
       
   916     TInt driveNumber( -1 );
       
   917     TChar driveLetter;
       
   918     DriveInfo::GetDefaultDrive( DriveInfo::EDefaultRom, driveNumber );
       
   919     iFs.DriveToChar( driveNumber, driveLetter );
       
   920 
       
   921     TFileName udtCertFile;
       
   922     udtCertFile.Format( KUdtCertFileName, (TUint)driveLetter );
       
   923 
       
   924     ReadFileL(iFs, udtCertFile, buffer);
       
   925 
       
   926 #endif
       
   927 
       
   928     CleanupStack::PushL(buffer);
       
   929     cert = CX509Certificate::NewL(*buffer);
       
   930     CleanupStack::PushL(cert);
       
   931     key = factory.RSAPublicKeyL(cert->PublicKey().KeyData());
       
   932     CleanupStack::PushL(key);
       
   933 
       
   934     encryptor = CRSAPKCS1v15Encryptor::NewLC(*key);
       
   935     encryptor->EncryptL(aInput, result);
       
   936 
       
   937     CleanupStack::PopAndDestroy(4); // encryptor, key, cert, buffer
       
   938     CleanupStack::Pop();// output
       
   939     return output;
       
   940     };
       
   941 
       
   942 // -----------------------------------------------------------------------------
       
   943 // CDrmStdKeyStorage::GetRootCertificatesL
       
   944 // -----------------------------------------------------------------------------
       
   945 //
       
   946 void CDrmStdKeyStorage::GetRootCertificatesL(
       
   947           RPointerArray<HBufC8>& aRootCerts)
       
   948     {
       
   949     CDir* dir = NULL;
       
   950     CDir* rootCerts = NULL;
       
   951     TFileName dirName;
       
   952     HBufC8* cert = NULL;
       
   953     TInt i = 0;
       
   954     TBuf<256> path;
       
   955 
       
   956     iFs.SessionPath( path );
       
   957 
       
   958 #ifndef RD_MULTIPLE_DRIVE
       
   959 
       
   960     if (iFs.GetDir(KKeyStoragePath, KEntryAttDir, ESortByName, dir) == KErrNone)
       
   961 
       
   962 #else //RD_MULTIPLE_DRIVE
       
   963 
       
   964     TFileName tempPath;
       
   965     TInt driveNumber( -1 );
       
   966     TChar driveLetter;
       
   967     DriveInfo::GetDefaultDrive( DriveInfo::EDefaultSystem, driveNumber );
       
   968     iFs.DriveToChar( driveNumber, driveLetter );
       
   969 
       
   970     tempPath.Format( KKeyStoragePath, (TUint)driveLetter );
       
   971 
       
   972     if (iFs.GetDir(tempPath, KEntryAttDir, ESortByName, dir) == KErrNone)
       
   973 
       
   974 #endif
       
   975         {
       
   976         CleanupStack::PushL(dir);
       
   977         for(i = 0; i < dir->Count(); i++)
       
   978             {
       
   979             if ((*dir)[i].IsDir())
       
   980                 {
       
   981 
       
   982 #ifndef RD_MULTIPLE_DRIVE
       
   983 
       
   984                 dirName.Copy(KKeyStoragePath);
       
   985 
       
   986 #else //RD_MULTIPLE_DRIVE
       
   987 
       
   988                 dirName.Copy(tempPath);
       
   989 
       
   990 #endif
       
   991 
       
   992                 dirName.Append((*dir)[i].iName);
       
   993                 dirName.Append('\\');
       
   994                 User::LeaveIfError(iFs.SetSessionPath(dirName));
       
   995                 User::LeaveIfError(iFs.GetDir(KSingingCertPattern, KEntryAttNormal, ESortByName, rootCerts));
       
   996                 CleanupStack::PushL(rootCerts);
       
   997                 ReadFileL(iFs, (*rootCerts)[rootCerts->Count() - 1].iName, cert);
       
   998                 CleanupStack::PushL(cert);
       
   999                 aRootCerts.AppendL(cert);
       
  1000                 CleanupStack::Pop(cert);
       
  1001                 CleanupStack::PopAndDestroy(); // rootCerts
       
  1002                 }
       
  1003             }
       
  1004         CleanupStack::PopAndDestroy(dir);
       
  1005         }
       
  1006 
       
  1007 #ifndef RD_MULTIPLE_DRIVE
       
  1008 
       
  1009     if (iFs.GetDir(KRomKeyStoragePath, KEntryAttDir, ESortByName, dir) == KErrNone)
       
  1010 
       
  1011 #else //RD_MULTIPLE_DRIVE
       
  1012 
       
  1013     DriveInfo::GetDefaultDrive( DriveInfo::EDefaultRom, driveNumber );
       
  1014     iFs.DriveToChar( driveNumber, driveLetter );
       
  1015 
       
  1016     tempPath.Format( KRomKeyStoragePath, (TUint)driveLetter );
       
  1017 
       
  1018     if (iFs.GetDir(tempPath, KEntryAttDir, ESortByName, dir) == KErrNone)
       
  1019 
       
  1020 #endif
       
  1021         {
       
  1022         CleanupStack::PushL(dir);
       
  1023         for(i = 0; i < dir->Count(); i++)
       
  1024             {
       
  1025             if ((*dir)[i].IsDir())
       
  1026                 {
       
  1027 
       
  1028 #ifndef RD_MULTIPLE_DRIVE
       
  1029 
       
  1030                 dirName.Copy(KRomKeyStoragePath);
       
  1031 
       
  1032 #else //RD_MULTIPLE_DRIVE
       
  1033 
       
  1034                 dirName.Copy(tempPath);
       
  1035 
       
  1036 #endif
       
  1037 
       
  1038                 dirName.Append((*dir)[i].iName);
       
  1039                 dirName.Append('\\');
       
  1040                 User::LeaveIfError(iFs.SetSessionPath(dirName));
       
  1041                 User::LeaveIfError(iFs.GetDir(KSingingCertPattern, KEntryAttNormal, ESortByName, rootCerts));
       
  1042                 CleanupStack::PushL(rootCerts);
       
  1043                 ReadFileL(iFs, (*rootCerts)[rootCerts->Count() - 1].iName, cert);
       
  1044                 CleanupStack::PushL(cert);
       
  1045                 aRootCerts.AppendL(cert);
       
  1046                 CleanupStack::Pop(cert);
       
  1047                 CleanupStack::PopAndDestroy(); // rootCerts
       
  1048                 }
       
  1049             }
       
  1050         CleanupStack::PopAndDestroy(dir);
       
  1051         }
       
  1052     iFs.SetSessionPath( path );
       
  1053     }
       
  1054 
       
  1055 // -----------------------------------------------------------------------------
       
  1056 // CDrmStdKeyStorage::GetIMEIL
       
  1057 // -----------------------------------------------------------------------------
       
  1058 //
       
  1059 const TDesC& CDrmStdKeyStorage::GetImeiL()
       
  1060     {
       
  1061     if ( iImei )
       
  1062         {
       
  1063         return *iImei;
       
  1064         }
       
  1065 
       
  1066 #if (defined __WINS__ || defined WINSCW)
       
  1067     // Default IMEI used for emulator
       
  1068     _LIT( KDefaultSerialNumber, "123456789123456789" );
       
  1069     iImei = KDefaultSerialNumber().AllocL();
       
  1070 
       
  1071     return *iImei;
       
  1072 #else
       
  1073 
       
  1074     TInt error( KErrNone );
       
  1075     TInt count( 0 );
       
  1076     TInt count2( 0 );
       
  1077     TUint32 caps( 0 );
       
  1078     TBool found (EFalse);
       
  1079 
       
  1080     RTelServer etelServer;
       
  1081     RMobilePhone phone;
       
  1082 
       
  1083     TUint KMaxImeiTries = 5;
       
  1084 
       
  1085     for ( TUint8 i = 0; i < KMaxImeiTries; ++i )
       
  1086         {
       
  1087         error = etelServer.Connect();
       
  1088         if ( error )
       
  1089             {
       
  1090             User::After( TTimeIntervalMicroSeconds32( KWaitingTime ) );
       
  1091             }
       
  1092         else
       
  1093             {
       
  1094             break;
       
  1095             }
       
  1096         }
       
  1097 
       
  1098     User::LeaveIfError( error );
       
  1099     CleanupClosePushL( etelServer );
       
  1100 
       
  1101     User::LeaveIfError( etelServer.LoadPhoneModule( KMmTsyModuleName ) );
       
  1102 
       
  1103     TUnloadModule unload;
       
  1104     unload.iServer = &etelServer;
       
  1105     unload.iName = &KMmTsyModuleName;
       
  1106 
       
  1107     TCleanupItem item( DoUnloadPhoneModule, &unload );
       
  1108     CleanupStack::PushL( item );
       
  1109     User::LeaveIfError( etelServer.EnumeratePhones( count ) );
       
  1110 
       
  1111     for ( count2 = 0; count2 < count && !found; ++count2 )
       
  1112         {
       
  1113         RTelServer::TPhoneInfo phoneInfo;
       
  1114         User::LeaveIfError( etelServer.GetTsyName( count2, phoneInfo.iName ) );
       
  1115 
       
  1116         if ( phoneInfo.iName.CompareF(KMmTsyModuleName()) == 0 )
       
  1117            {
       
  1118             User::LeaveIfError( etelServer.GetPhoneInfo( count2, phoneInfo ) );
       
  1119             User::LeaveIfError( phone.Open( etelServer, phoneInfo.iName ) );
       
  1120             CleanupClosePushL( phone );
       
  1121             found = ETrue;
       
  1122             }
       
  1123         }
       
  1124 
       
  1125     if ( !found )
       
  1126         {
       
  1127         // Not found.
       
  1128         User::Leave( KErrNotFound );
       
  1129         }
       
  1130 
       
  1131     User::LeaveIfError( phone.GetIdentityCaps( caps ) );
       
  1132     if (!( caps & RMobilePhone::KCapsGetSerialNumber ))
       
  1133         {
       
  1134          User::Leave( KErrNotFound );
       
  1135         }
       
  1136 
       
  1137     RMobilePhone::TMobilePhoneIdentityV1 id;
       
  1138     TRequestStatus status;
       
  1139 
       
  1140     phone.GetPhoneId( status, id );
       
  1141 
       
  1142     User::WaitForRequest( status );
       
  1143 
       
  1144     User::LeaveIfError( status.Int() );
       
  1145 
       
  1146     iImei = id.iSerialNumber.AllocL();
       
  1147 
       
  1148     CleanupStack::PopAndDestroy( 3 ); // phone, item, etelServer
       
  1149 
       
  1150     HBufC8* buf = HBufC8::NewL( iImei->Size() );
       
  1151     TPtr8 ptr( buf->Des() );
       
  1152     ptr.Copy( *iImei );
       
  1153 
       
  1154     LOG(_L("IMEI:"));
       
  1155     LOGHEX(ptr);
       
  1156     delete buf;
       
  1157 
       
  1158     return *iImei;
       
  1159 #endif /* __WINS__ , WINSCW */
       
  1160 
       
  1161     }
       
  1162 
       
  1163 
       
  1164 // -----------------------------------------------------------------------------
       
  1165 // CDrmStdKeyStorage::RandomDataGetL
       
  1166 // -----------------------------------------------------------------------------
       
  1167 //
       
  1168 
       
  1169 void CDrmStdKeyStorage::RandomDataGetL( TDes8& aData, const TInt aLength )
       
  1170     {
       
  1171     if ( aLength <= 0 )
       
  1172         {
       
  1173          User::Leave(KErrArgument);
       
  1174         }
       
  1175 
       
  1176     TInt size = aData.MaxSize();
       
  1177 
       
  1178     if( size < aLength )
       
  1179         {
       
  1180         User::Leave(KErrOverflow);
       
  1181         }
       
  1182 
       
  1183     TRandom::Random( aData );
       
  1184     }
       
  1185 
       
  1186 // end of file