javaextensions/satsa/pki/src.s60/cstssignatureservice.cpp
branchRCL_3
changeset 83 26b2b12093af
parent 60 6c158198356e
equal deleted inserted replaced
77:7cee158cb8cd 83:26b2b12093af
    13 *
    13 *
    14 * Description:
    14 * Description:
    15  *
    15  *
    16 */
    16 */
    17 
    17 
       
    18 
       
    19 // INCLUDE FILES
    18 #include "cstssignatureservice.h"
    20 #include "cstssignatureservice.h"
    19 #include "tstsdistinguishednameconverter.h"
    21 #include "tstsdistinguishednameconverter.h"
    20 #include "secdlg.h"
    22 #include "secdlg.h"
    21 #include <unifiedcertstore.h>
    23 #include <unifiedcertstore.h>
    22 #include <asn1enc.h>
    24 #include <asn1enc.h>
    28 #include <asymmetric.h>
    30 #include <asymmetric.h>
    29 #include <CCMSX509Certificate.h>
    31 #include <CCMSX509Certificate.h>
    30 #include <CCMSSignerInfo.h>
    32 #include <CCMSSignerInfo.h>
    31 #include <CCMSEncapsulatedContentInfo.h>
    33 #include <CCMSEncapsulatedContentInfo.h>
    32 #include <CCMSSignedData.h>
    34 #include <CCMSSignedData.h>
    33 
       
    34 #ifndef RD_JAVA_S60_RELEASE_10_1_ONWARDS
       
    35 #include <PKIDlg.h>
    35 #include <PKIDlg.h>
    36 #endif
       
    37 
       
    38 #include "cstsseprompt.h"
    36 #include "cstsseprompt.h"
    39 
    37 
    40 #include <hash.h>
    38 #include <hash.h>
    41 #include "fs_methodcall.h"
    39 #include "fs_methodcall.h"
    42 #include <jutils.h>
    40 #include <jutils.h>
    43 #include "satsajnitools.h"
    41 #include "satsajnitools.h"
    44 #include "logger.h"
    42 #include "logger.h"
    45 #include "jstringutils.h"
    43 #include "jstringutils.h"
    46 
    44 
    47 
    45 // CONSTANTS
    48 const TInt KDefaultGranularity = 1;
    46 const TInt KDefaultGranularity = 1;
    49 
    47 
    50 const TInt KOptionIncludeContent = 1;
    48 const TInt KOptionIncludeContent = 1;
    51 const TInt KOptionIncludeCertificate = 2;
    49 const TInt KOptionIncludeCertificate = 2;
    52 
    50 
    80 {
    78 {
    81     if (iDialog)
    79     if (iDialog)
    82     {
    80     {
    83         iDialog->Release(); // Release deletes the object
    81         iDialog->Release(); // Release deletes the object
    84     }
    82     }
    85 #ifndef RD_JAVA_S60_RELEASE_10_1_ONWARDS	
       
    86     if (iPKIDialog)
    83     if (iPKIDialog)
    87     {
    84     {
    88         iPKIDialog->Release();
    85         iPKIDialog->Release();
    89     }
    86     }
    90 #endif	
       
    91     iCertificateArray.Close();
    87     iCertificateArray.Close();
    92     iCertInfos.Close();
    88     iCertInfos.Close();
    93     delete iFilter;
    89     delete iFilter;
    94     iDERNamesHBufC.ResetAndDestroy();
    90     iDERNamesHBufC.ResetAndDestroy();
    95     iDERNames.Close();
    91     iDERNames.Close();
   128 //
   124 //
   129 HBufC8* CSTSSignatureService::AuthenticateL(const TDesC8& abytesToAuthenticate,
   125 HBufC8* CSTSSignatureService::AuthenticateL(const TDesC8& abytesToAuthenticate,
   130         TInt aOptions, const CDesCArray& aCaNames,
   126         TInt aOptions, const CDesCArray& aCaNames,
   131         const TDesC& asecurityElementPrompt, TBool aShowData)
   127         const TDesC& asecurityElementPrompt, TBool aShowData)
   132 {
   128 {
       
   129     LOG(ESATSA, EInfo, "+ CSTSSignatureService::AuthenticateL");
   133     if (iState != EReady)
   130     if (iState != EReady)
   134     {
   131     {
   135         ELOG(ESATSA, "CSTSSignatureService::AuthenticateL: Not Ready");
   132         ELOG(ESATSA, "CSTSSignatureService::AuthenticateL: Not Ready");
   136         User::Leave(KErrNotReady);
   133         User::Leave(KErrNotReady);
   137     }
   134     }
   138 
   135 
       
   136     LOG(ESATSA, EInfo, "CSTSSignatureService::AuthenticateL:After convertions");
   139     // First we need to convert the data to unicode, if we have to display it
   137     // First we need to convert the data to unicode, if we have to display it
   140     HBufC* textToDisplay = NULL;
   138     HBufC* textToDisplay = NULL;
   141     if (aShowData)
   139     if (aShowData)
   142     {
   140     {
   143         textToDisplay = ConvertUTF8ToUnicodeL(abytesToAuthenticate);
   141         textToDisplay = ConvertUTF8ToUnicodeL(abytesToAuthenticate);
   155     iMessage.Set(abytesToAuthenticate);
   153     iMessage.Set(abytesToAuthenticate);
   156 
   154 
   157     iOptions = aOptions;
   155     iOptions = aOptions;
   158 
   156 
   159     iShowNotes = EFalse;
   157     iShowNotes = EFalse;
       
   158 
       
   159     LOG(ESATSA, EInfo, "CSTSSignatureService::AuthenticateL:Going to call CreateSignatureL");
   160     // get handles to applicable certificates
   160     // get handles to applicable certificates
   161     HBufC8* retVal = CreateSignatureL(aCaNames, EX509UsageDigitalSignature,
   161     HBufC8* retVal = CreateSignatureL(aCaNames, EX509UsageDigitalSignature,
   162                                       asecurityElementPrompt);
   162                                       asecurityElementPrompt);
       
   163     LOG(ESATSA, EInfo, "CSTSSignatureService::AuthenticateL:After call to CreateSignatureL");
       
   164 
       
   165     LOG(ESATSA, EInfo, "-- CSTSSignatureService::AuthenticateL");
   163     return retVal;
   166     return retVal;
   164 }
   167 }
   165 
   168 
   166 TInt CSTSSignatureService::StaticAuthenticateL(JNIEnv* aJniEnv,
   169 TInt CSTSSignatureService::StaticAuthenticateL(JNIEnv* aJniEnv,
   167         CSTSSignatureService* service, jbyteArray aByteArrayToAuthenticate,
   170         CSTSSignatureService* service, jbyteArray aByteArrayToAuthenticate,
   168         jint aOptions, jobjectArray aCaNames, jstring aSecurityElementPrompt,
   171         jint aOptions, jobjectArray aCaNames, jstring aSecurityElementPrompt,
   169         jboolean aShowData, HBufC8** aRetVal)
   172         jboolean aShowData, HBufC8** aRetVal)
   170 {
   173 {
       
   174     LOG(ESATSA, EInfo, "+ CSTSSignatureService::StaticAuthenticateL");
   171     const TInt byteArrayLength = aJniEnv->GetArrayLength(
   175     const TInt byteArrayLength = aJniEnv->GetArrayLength(
   172                                      aByteArrayToAuthenticate);
   176                                      aByteArrayToAuthenticate);
   173     jbyte* bytesToAuthenticate = aJniEnv->GetByteArrayElements(
   177     jbyte* bytesToAuthenticate = aJniEnv->GetByteArrayElements(
   174                                      aByteArrayToAuthenticate, NULL);
   178                                      aByteArrayToAuthenticate, NULL);
   175 
   179 
   183                              byteArrayLength);
   187                              byteArrayLength);
   184 
   188 
   185     CDesCArrayFlat* nativeCaNames = STSCreateNativeStringArrayL(aJniEnv,
   189     CDesCArrayFlat* nativeCaNames = STSCreateNativeStringArrayL(aJniEnv,
   186                                     aCaNames);
   190                                     aCaNames);
   187 
   191 
       
   192     LOG(ESATSA, EInfo,  "CSTSSignatureService::StaticAuthenticateL: prepare se prompt");
   188     const JStringUtils securityElementPrompt(*aJniEnv, aSecurityElementPrompt);
   193     const JStringUtils securityElementPrompt(*aJniEnv, aSecurityElementPrompt);
   189 
   194 
       
   195     LOG(ESATSA, EInfo,  "CSTSSignatureService::StaticAuthenticateL: start convertions to TInt");
   190     TBool ShowData = static_cast<TBool>(aShowData);
   196     TBool ShowData = static_cast<TBool>(aShowData);
   191     const TDesC* sec = static_cast<const TDesC*>(&securityElementPrompt);
   197     const TDesC* sec = static_cast<const TDesC*>(&securityElementPrompt);
   192 
   198 
       
   199     LOG(ESATSA, EInfo, "CSTSSignatureService::StaticAuthenticateL: After convertions,going to call CallMethodL");
   193     TRAPD(err, CallMethodL(*aRetVal, service,
   200     TRAPD(err, CallMethodL(*aRetVal, service,
   194                            &CSTSSignatureService::AuthenticateL, desToAuthenticate, aOptions,
   201                            &CSTSSignatureService::AuthenticateL, desToAuthenticate, aOptions,
   195                            *nativeCaNames, *sec, ShowData, service));
   202                            *nativeCaNames, *sec, ShowData, service));
   196 
   203 
       
   204     LOG(ESATSA, EInfo, "+ CSTSSignatureService::StaticAuthenticateL:After CallmethodL");
       
   205     LOG(ESATSA, EInfo, "-- return CSTSSignatureService::StaticAuthenticateL");
   197     return err;
   206     return err;
   198 
   207 
   199 }
   208 }
   200 
   209 
   201 // -----------------------------------------------------------------------------
   210 // -----------------------------------------------------------------------------
   205 // -----------------------------------------------------------------------------
   214 // -----------------------------------------------------------------------------
   206 
   215 
   207 HBufC8* CSTSSignatureService::SignL(const TDesC8& aBytesToSign, TInt aOptions,
   216 HBufC8* CSTSSignatureService::SignL(const TDesC8& aBytesToSign, TInt aOptions,
   208                                     const CDesCArray& aCaNames, const TDesC& aSecurityElementPrompt)
   217                                     const CDesCArray& aCaNames, const TDesC& aSecurityElementPrompt)
   209 {
   218 {
       
   219     LOG(ESATSA, EInfo, "+ CSTSSignatureService::SignL");
   210     if (iState != EReady)
   220     if (iState != EReady)
   211     {
   221     {
   212         ELOG(ESATSA, "CSTSSignatureService::SignL: Not Ready");
   222         ELOG(ESATSA, "CSTSSignatureService::SignL: Not Ready");
   213         User::Leave(KErrNotReady);
   223         User::Leave(KErrNotReady);
   214     }
   224     }
   215 
   225 
       
   226     LOG(ESATSA, EInfo, "CSTSSignatureService::SignL: start convertions!");
       
   227     LOG(ESATSA, EInfo, "CSTSSignatureService::SignL: After convertions");
   216     // convert text from UTF8
   228     // convert text from UTF8
   217     HBufC* textToDisplay = ConvertUTF8ToUnicodeL(aBytesToSign);
   229     HBufC* textToDisplay = ConvertUTF8ToUnicodeL(aBytesToSign);
   218     delete iTextToDisplay;
   230     delete iTextToDisplay;
   219     iTextToDisplay = textToDisplay;
   231     iTextToDisplay = textToDisplay;
   220 
   232 
   224 
   236 
   225     iOptions = aOptions;
   237     iOptions = aOptions;
   226 
   238 
   227     iShowNotes = ETrue;
   239     iShowNotes = ETrue;
   228 
   240 
       
   241     LOG(ESATSA, EInfo, "CSTSSignatureService::SignL: Before call to CreateSignatureL");
   229     // get handles to applicable certificates
   242     // get handles to applicable certificates
   230     HBufC8* retVal = CreateSignatureL(aCaNames, EX509UsageNonRepudiation,
   243     HBufC8* retVal = CreateSignatureL(aCaNames, EX509UsageNonRepudiation,
   231                                       aSecurityElementPrompt);
   244                                       aSecurityElementPrompt);
   232 
   245 
       
   246     LOG(ESATSA, EInfo, "-- return CSTSSignatureService::SignL");
   233     return retVal;
   247     return retVal;
   234 }
   248 }
   235 
   249 
   236 // -----------------------------------------------------------------------------
   250 // -----------------------------------------------------------------------------
   237 // CSTSSignatureService::StaticSignL
   251 // CSTSSignatureService::StaticSignL
   240 //
   254 //
   241 TInt CSTSSignatureService::StaticSignL(JNIEnv* aJniEnv,
   255 TInt CSTSSignatureService::StaticSignL(JNIEnv* aJniEnv,
   242                                        CSTSSignatureService* service, jbyteArray aBytesToSign, jint aOptions,
   256                                        CSTSSignatureService* service, jbyteArray aBytesToSign, jint aOptions,
   243                                        jobjectArray aCaNames, jstring aSecurityElementPrompt, HBufC8** aRetVal)
   257                                        jobjectArray aCaNames, jstring aSecurityElementPrompt, HBufC8** aRetVal)
   244 {
   258 {
       
   259     LOG(ESATSA, EInfo, "+ CSTSSignatureService::StaticSignL");
   245     const TInt byteArrayLength = aJniEnv->GetArrayLength(aBytesToSign);
   260     const TInt byteArrayLength = aJniEnv->GetArrayLength(aBytesToSign);
   246     jbyte* bytesToSign = aJniEnv->GetByteArrayElements(aBytesToSign, NULL);
   261     jbyte* bytesToSign = aJniEnv->GetByteArrayElements(aBytesToSign, NULL);
   247 
   262 
   248     if (!bytesToSign)
   263     if (!bytesToSign)
   249     {
   264     {
   250         ELOG(ESATSA, "CSTSSignatureService::StaticSignL: No memory");
   265         LOG(ESATSA, EInfo, "CSTSSignatureService::StaticSignL: No memory");
   251         return NULL;
   266         return NULL;
   252     }
   267     }
   253 
   268 
   254     TPtrC8 desToSign(reinterpret_cast<TUint8*>(bytesToSign), byteArrayLength);
   269     TPtrC8 desToSign(reinterpret_cast<TUint8*>(bytesToSign), byteArrayLength);
   255 
   270 
   256     CDesCArrayFlat* nativeCaNames = STSCreateNativeStringArrayL(aJniEnv,
   271     CDesCArrayFlat* nativeCaNames = STSCreateNativeStringArrayL(aJniEnv,
   257                                     aCaNames);
   272                                     aCaNames);
   258 
   273 
   259     const JStringUtils securityElementPrompt(*aJniEnv, aSecurityElementPrompt);
   274     const JStringUtils securityElementPrompt(*aJniEnv, aSecurityElementPrompt);
   260 
   275 
   261     
   276     LOG(ESATSA, EInfo, "CSTSSignatureService::StaticSignL: Start convertions");
       
   277     LOG(ESATSA, EInfo, "CSTSSignatureService::StaticSignL: Before callmethodl");
       
   278 
   262     const TDesC* sec = static_cast<const TDesC*>(&securityElementPrompt);
   279     const TDesC* sec = static_cast<const TDesC*>(&securityElementPrompt);
   263 
   280 
   264     TRAPD(err, CallMethodL(*aRetVal, service, &CSTSSignatureService::SignL,
   281     TRAPD(err, CallMethodL(*aRetVal, service, &CSTSSignatureService::SignL,
   265                            desToSign, aOptions, *nativeCaNames, *sec, service));
   282                            desToSign, aOptions, *nativeCaNames, *sec, service));
   266 
   283 
       
   284     LOG(ESATSA, EInfo, "-- ret CSTSSignatureService::StaticSignL");
   267     return err;
   285     return err;
   268 }
   286 }
   269 
   287 
   270 // -----------------------------------------------------------------------------
   288 // -----------------------------------------------------------------------------
   271 // CSTSSignatureService::RunL
   289 // CSTSSignatureService::RunL
   272 // CActive callback
   290 // CActive callback
   273 // -----------------------------------------------------------------------------
   291 // -----------------------------------------------------------------------------
   274 //
   292 //
   275 void CSTSSignatureService::RunL()
   293 void CSTSSignatureService::RunL()
   276 {
   294 {
       
   295     LOG(ESATSA, EInfo, "+ CSTSSignatureService::RunL");
   277     if (iStatus == KErrCancel)
   296     if (iStatus == KErrCancel)
   278     {
   297     {
       
   298         LOG(ESATSA, EInfo, "CSTSSignatureService::RunL: KErrCancel");
   279         if (iShowNotes)
   299         if (iShowNotes)
   280         {
   300         {
       
   301             LOG(ESATSA, EInfo, "CSTSSignatureService::RunL: ShowNote");
   281             iState = EFinalNote;
   302             iState = EFinalNote;
   282             iStatus = KErrNone;
   303             iStatus = KErrNone;
   283 #ifndef RD_JAVA_S60_RELEASE_10_1_ONWARDS			
       
   284             iPKIDialog->Note(MPKIDialog::ESigningCancelled, iStatus);
   304             iPKIDialog->Note(MPKIDialog::ESigningCancelled, iStatus);
   285 #endif			
       
   286             SetActive();
   305             SetActive();
   287          }
   306             LOG(ESATSA, EInfo, "CSTSSignatureService::RunL: Done ShowNote");
       
   307         }
   288         else
   308         else
   289         {
   309         {
   290              Complete(KErrNone);
   310             LOG(ESATSA, EInfo, "CSTSSignatureService::RunL: Complete");
       
   311             Complete(KErrNone);
   291         }
   312         }
   292     }
   313     }
   293     else if (iStatus != KErrNone)
   314     else if (iStatus != KErrNone)
   294     {
   315     {
   295         ELOG(ESATSA, "CSTSSignatureService::RunL: Error occured");
   316         LOG(ESATSA, EInfo, "CSTSSignatureService::RunL: Error occured");
   296         // Error has occured; inform java
   317         // Error has occured; inform java
   297         Complete(iStatus.Int());
   318         Complete(iStatus.Int());
   298     }
   319     }
   299     else
   320     else
   300     {
   321     {
   301         switch (iState)
   322         switch (iState)
   302         {
   323         {
   303         case EInitialising:
   324         case EInitialising:
   304         {
   325         {
       
   326             LOG(ESATSA, EInfo, "CSTSSignatureService::RunL: case EInitialising");
   305             iState = EKeyStoreInit;
   327             iState = EKeyStoreInit;
   306             iKeyStore->Initialize(iStatus);
   328             iKeyStore->Initialize(iStatus);
   307             SetActive();
   329             SetActive();
       
   330             LOG(ESATSA, EInfo, "CSTSSignatureService::RunL: over case EInitialising");
   308             break;
   331             break;
   309         }
   332         }
   310         case EKeyStoreInit:
   333         case EKeyStoreInit:
   311         {
   334         {
       
   335             LOG(ESATSA, EInfo, "CSTSSignatureService::RunL: case EKeyStoreInit");
   312             Complete(KErrNone);
   336             Complete(KErrNone);
   313             break;
   337             break;
   314         }
   338         }
   315         case EListing:
   339         case EListing:
   316         {
   340         {
       
   341             LOG(ESATSA, EInfo, "CSTSSignatureService::RunL: case EListing");
   317             HandlesFromCertInfosL();
   342             HandlesFromCertInfosL();
       
   343             LOG(ESATSA, EInfo, "CSTSSignatureService::RunL: over case EListing");
   318             break;
   344             break;
   319         }
   345         }
   320         case EDialog:
   346         case EDialog:
   321         {
   347         {
       
   348             LOG(ESATSA, EInfo, "CSTSSignatureService::RunL: case EDialog");
   322             iState = EGetCert;
   349             iState = EGetCert;
   323             if (iCertificate)
   350             if (iCertificate)
   324             {
   351             {
   325                 iCertificate->Release();
   352                 iCertificate->Release();
   326                 iCertificate = NULL;
   353                 iCertificate = NULL;
   331                 iCertificateHandle = iCertificateArray[0];
   358                 iCertificateHandle = iCertificateArray[0];
   332             }
   359             }
   333 
   360 
   334             iCertStore->GetCert(iCertificate, iCertificateHandle, iStatus);
   361             iCertStore->GetCert(iCertificate, iCertificateHandle, iStatus);
   335             SetActive();
   362             SetActive();
       
   363             LOG(ESATSA, EInfo, "CSTSSignatureService::RunL: over case EDialog");
   336             break;
   364             break;
   337         }
   365         }
   338         case EGetCert:
   366         case EGetCert:
   339         {
   367         {
       
   368             LOG(ESATSA, EInfo, "CSTSSignatureService::RunL: case EGetCert");
   340             if (iX509Certificate)
   369             if (iX509Certificate)
   341             {
   370             {
   342                 delete iX509Certificate;
   371                 delete iX509Certificate;
   343                 iX509Certificate = NULL;
   372                 iX509Certificate = NULL;
   344             }
   373             }
   345             iState = ERetrieveCert;
   374             iState = ERetrieveCert;
   346             iCertStore->Retrieve(*iCertificate, iX509Certificate, iStatus);
   375             iCertStore->Retrieve(*iCertificate, iX509Certificate, iStatus);
   347             SetActive();
   376             SetActive();
       
   377             LOG(ESATSA, EInfo, "CSTSSignatureService::RunL: over case EGetCert");
   348             break;
   378             break;
   349         }
   379         }
   350         case ERetrieveCert:
   380         case ERetrieveCert:
   351         {
   381         {
       
   382             LOG(ESATSA, EInfo, "CSTSSignatureService::RunL: case ERetrieveCert");
   352             FindKeyL();
   383             FindKeyL();
   353             break;
   384             break;
   354         }
   385         }
   355         case EFindKey:
   386         case EFindKey:
   356         {
   387         {
       
   388             LOG(ESATSA, EInfo, "CSTSSignatureService::RunL: case EFindKey");
   357             OpenSignerL();
   389             OpenSignerL();
   358             break;
   390             break;
   359         }
   391         }
   360         case EOpenSigner:
   392         case EOpenSigner:
   361         {
   393         {
       
   394             LOG(ESATSA, EInfo, "CSTSSignatureService::RunL: case EOpenSigner");
   362             SignL();
   395             SignL();
   363             break;
   396             break;
   364         }
   397         }
   365         case ESign:
   398         case ESign:
   366         {
   399         {
       
   400             LOG(ESATSA, EInfo, "CSTSSignatureService::RunL: case ESign");
   367             CreateSignedDataL();
   401             CreateSignedDataL();
       
   402             LOG(ESATSA, EInfo, "CSTSSignatureService::RunL: over case ESign");
   368             break;
   403             break;
   369         }
   404         }
   370         case EFinalNote:
   405         case EFinalNote:
   371         {
   406         {
       
   407             LOG(ESATSA, EInfo, "CSTSSignatureService::RunL: case EFinalNote");
   372             Complete(KErrNone);
   408             Complete(KErrNone);
       
   409             LOG(ESATSA, EInfo, "CSTSSignatureService::RunL: over case EFinalNote");
   373             break;
   410             break;
   374         }
   411         }
   375         default:
   412         default:
   376         {
   413         {
       
   414             LOG(ESATSA, EInfo, "CSTSSignatureService::RunL: case default");
   377             Complete(KErrGeneral);
   415             Complete(KErrGeneral);
   378         }
   416         }
   379         }
   417         }
   380     }
   418     }
   381 }
   419 }
   385 // CActive error callback
   423 // CActive error callback
   386 // -----------------------------------------------------------------------------
   424 // -----------------------------------------------------------------------------
   387 //
   425 //
   388 TInt CSTSSignatureService::RunError(TInt aError)
   426 TInt CSTSSignatureService::RunError(TInt aError)
   389 {
   427 {
       
   428     LOG(ESATSA, EInfo, "+ CSTSSignatureService::RunError");
   390     Complete(aError);
   429     Complete(aError);
   391     return KErrNone;
   430     return KErrNone;
   392 }
   431 }
   393 
   432 
   394 // -----------------------------------------------------------------------------
   433 // -----------------------------------------------------------------------------
   483 //
   522 //
   484 void CSTSSignatureService::ConstructL()
   523 void CSTSSignatureService::ConstructL()
   485 {
   524 {
   486     User::LeaveIfError(iFileServer.Connect());
   525     User::LeaveIfError(iFileServer.Connect());
   487     iDialog = SecurityDialogFactory::CreateL();
   526     iDialog = SecurityDialogFactory::CreateL();
   488 #ifndef RD_JAVA_S60_RELEASE_10_1_ONWARDS	
       
   489     iPKIDialog = PKIDialogFactory::CreateNoteL();
   527     iPKIDialog = PKIDialogFactory::CreateNoteL();
   490 #endif
   528 
   491     iFilter = CCertAttributeFilter::NewL();
   529     iFilter = CCertAttributeFilter::NewL();
   492 
   530 
   493     iWait = new(ELeave) CActiveSchedulerWait;
   531     iWait = new(ELeave) CActiveSchedulerWait;
   494     iCertStore = CUnifiedCertStore::NewL(iFileServer, ETrue);
   532     iCertStore = CUnifiedCertStore::NewL(iFileServer, ETrue);
   495     iKeyStore = CUnifiedKeyStore::NewL(iFileServer);
   533     iKeyStore = CUnifiedKeyStore::NewL(iFileServer);
   502 // Completes the operation
   540 // Completes the operation
   503 // -----------------------------------------------------------------------------
   541 // -----------------------------------------------------------------------------
   504 //
   542 //
   505 void CSTSSignatureService::Complete(TInt aError)
   543 void CSTSSignatureService::Complete(TInt aError)
   506 {
   544 {
       
   545     LOG(ESATSA, EInfo, "+ CSTSSignatureService::Complete");
   507     if (KErrNone == aError)
   546     if (KErrNone == aError)
   508     {
   547     {
   509         iState = EReady;
   548         iState = EReady;
   510     }
   549     }
   511     else
   550     else
   512     {
   551     {
   513         iState = EERROR;
   552         iState = EERROR;
   514         iError = aError;
   553         iError = aError;
   515     }
   554     }
   516     iWait->AsyncStop();
   555     iWait->AsyncStop();
   517     
   556     LOG(ESATSA, EInfo, "-- ret CSTSSignatureService::Complete");
   518 }
   557 }
   519 
   558 
   520 // -----------------------------------------------------------------------------
   559 // -----------------------------------------------------------------------------
   521 // CSTSSignatureService::WaitForCompletionL()
   560 // CSTSSignatureService::WaitForCompletionL()
   522 // Wait for completion, leave on error
   561 // Wait for completion, leave on error
   523 // -----------------------------------------------------------------------------
   562 // -----------------------------------------------------------------------------
   524 //
   563 //
   525 void CSTSSignatureService::WaitForCompletionL()
   564 void CSTSSignatureService::WaitForCompletionL()
   526 {
   565 {
       
   566     LOG(ESATSA, EInfo, "+ CSTSSignatureService::WaitForCompletionL");
   527     SetActive();
   567     SetActive();
   528     iWait->Start();
   568     iWait->Start();
   529     if (iState != EReady)
   569     if (iState != EReady)
   530     {
   570     {
   531         // we need to be ready for the next request
   571         // we need to be ready for the next request
   532         iState = EReady;
   572         iState = EReady;
       
   573 
   533         User::Leave(iError);
   574         User::Leave(iError);
   534     }
   575     }
       
   576     LOG(ESATSA, EInfo, "- ret CSTSSignatureService::WaitForCompletionL");
   535 }
   577 }
   536 
   578 
   537 // -----------------------------------------------------------------------------
   579 // -----------------------------------------------------------------------------
   538 // CSTSSignatureService::CreateSignatureL()
   580 // CSTSSignatureService::CreateSignatureL()
   539 // Creates the signature
   581 // Creates the signature
   540 // -----------------------------------------------------------------------------
   582 // -----------------------------------------------------------------------------
   541 //
   583 //
   542 HBufC8* CSTSSignatureService::CreateSignatureL(const CDesCArray& aCaNames,
   584 HBufC8* CSTSSignatureService::CreateSignatureL(const CDesCArray& aCaNames,
   543         const TKeyUsageX509 aUsage, const TDesC& aSecurityElementPrompt)
   585         const TKeyUsageX509 aUsage, const TDesC& aSecurityElementPrompt)
   544 {
   586 {
       
   587     LOG(ESATSA, EInfo, "+ CSTSSignatureService::CreateSignatureL");
   545     HBufC* sePrompt = NULL;
   588     HBufC* sePrompt = NULL;
   546     if (aSecurityElementPrompt != KNullDesC())
   589     if (aSecurityElementPrompt != KNullDesC())
   547     {
   590     {
   548         sePrompt = aSecurityElementPrompt.AllocL();
   591         sePrompt = aSecurityElementPrompt.AllocL();
   549     }
   592     }
   550     delete iSecurityElementPrompt;
   593     delete iSecurityElementPrompt;
   551     iSecurityElementPrompt = sePrompt;
   594     iSecurityElementPrompt = sePrompt;
       
   595     LOG(ESATSA, EInfo, "CSTSSignatureService::CreateSignatureL: call findcertificates");
   552     FindCertificatesL(aCaNames, aUsage);
   596     FindCertificatesL(aCaNames, aUsage);
       
   597     LOG(ESATSA, EInfo, "CSTSSignatureService::CreateSignatureL: After call to findcertificates");
   553     WaitForCompletionL();
   598     WaitForCompletionL();
       
   599     LOG(ESATSA, EInfo, "CSTSSignatureService::CreateSignatureL: call WaitForCompletion");
   554     HBufC8* retVal = iSignature;
   600     HBufC8* retVal = iSignature;
   555     iSignature = NULL;
   601     iSignature = NULL;
       
   602     LOG(ESATSA, EInfo, "-- ret CSTSSignatureService::CreateSignatureL");
   556     return retVal;
   603     return retVal;
   557 }
   604 }
   558 // -----------------------------------------------------------------------------
   605 // -----------------------------------------------------------------------------
   559 // CSTSSignatureService::FindCertificatesL()
   606 // CSTSSignatureService::FindCertificatesL()
   560 // Find applicable certificates
   607 // Find applicable certificates
   562 //
   609 //
   563 void CSTSSignatureService::FindCertificatesL(const CDesCArray& aCaNames,
   610 void CSTSSignatureService::FindCertificatesL(const CDesCArray& aCaNames,
   564         const TKeyUsageX509 aUsage)
   611         const TKeyUsageX509 aUsage)
   565 {
   612 {
   566 
   613 
       
   614     LOG(ESATSA, EInfo, "+ CSTSSignatureService::FindCertificatesL");
   567     iFilter->SetKeyUsage(aUsage);
   615     iFilter->SetKeyUsage(aUsage);
   568     iFilter->SetOwnerType(EUserCertificate);
   616     iFilter->SetOwnerType(EUserCertificate);
   569     iFilter->SetFormat(EX509Certificate);
   617     iFilter->SetFormat(EX509Certificate);
   570 
   618 
   571     iCertInfos.Close();
   619     iCertInfos.Close();
   604         }
   652         }
   605 
   653 
   606         iState = EListing;
   654         iState = EListing;
   607         iCertStore->List(iCertInfos, *iFilter, iDERNames, iStatus);
   655         iCertStore->List(iCertInfos, *iFilter, iDERNames, iStatus);
   608     }
   656     }
   609     
   657     LOG(ESATSA, EInfo, "-- ret CSTSSignatureService::FindCertificatesL");
   610 }
   658 }
   611 
   659 
   612 // -----------------------------------------------------------------------------
   660 // -----------------------------------------------------------------------------
   613 // CSTSSignatureService::HandlesFromCertInfosL()
   661 // CSTSSignatureService::HandlesFromCertInfosL()
   614 // Fills iCertificateArray from iCertInfos.
   662 // Fills iCertificateArray from iCertInfos.
   615 // -----------------------------------------------------------------------------
   663 // -----------------------------------------------------------------------------
   616 //
   664 //
   617 void CSTSSignatureService::HandlesFromCertInfosL()
   665 void CSTSSignatureService::HandlesFromCertInfosL()
   618 {
   666 {
       
   667     LOG(ESATSA, EInfo, "+ CSTSSignatureService::HandlesFromCertInfosL");
   619     TInt certificateCount = iCertInfos.Count();
   668     TInt certificateCount = iCertInfos.Count();
   620     if (certificateCount == 0)
   669     if (certificateCount == 0)
   621     {
   670     {
   622         ELOG(ESATSA, "CSTSSignatureService::HandlesFromCertInfosL, KErrNotFound");
   671         ELOG(ESATSA, "CSTSSignatureService::HandlesFromCertInfosL, KErrNotFound");
   623         User::Leave(KErrNotFound);
   672         User::Leave(KErrNotFound);
   624     }
   673     }
   625 
   674 
   626     iCertificateArray.Reset();
   675     iCertificateArray.Reset();
   627     for (TInt i = 0; i < certificateCount; i++)
   676     for (TInt i = 0; i < certificateCount; i++)
   628     {
   677     {
       
   678         LOG(ESATSA, EInfo, "+ CSTSSignatureService::HandlesFromCertInfosL: No Certificates");
   629         User::LeaveIfError(iCertificateArray.Append(iCertInfos[i]->Handle()));
   679         User::LeaveIfError(iCertificateArray.Append(iCertInfos[i]->Handle()));
   630     }
   680     }
   631 
   681 
   632     // next is a dialog shown depending on the authentication type
   682     // next is a dialog shown depending on the authentication type
   633     switch (iAuthType)
   683     switch (iAuthType)
   634     {
   684     {
   635     case EAuthWithoutText:
   685     case EAuthWithoutText:
   636     {
   686     {
   637 #ifndef RD_JAVA_S60_RELEASE_10_1_ONWARDS        
   687         LOG(ESATSA, EInfo, "CSTSSignatureService::HandlesFromCertInfosL: case EAuthWithoutText");
   638         iPKIDialog->UserAuthentication(iCertificateArray, iCertificateHandle,
   688         iPKIDialog->UserAuthentication(iCertificateArray, iCertificateHandle,
   639                                        iStatus);
   689                                        iStatus);
   640 #endif 
   690         LOG(ESATSA, EInfo, "CSTSSignatureService::HandlesFromCertInfosL: over case EAuthWithoutText");
   641         break;
   691         break;
   642     }
   692     }
   643     case EAuthWithText:
   693     case EAuthWithText:
   644     {
   694     {
   645 #ifndef RD_JAVA_S60_RELEASE_10_1_ONWARDS
   695         LOG(ESATSA, EInfo, "CSTSSignatureService::HandlesFromCertInfosL: case EAuthWithText");
   646         iPKIDialog->UserAuthenticationText(*iTextToDisplay, iCertificateArray,
   696         iPKIDialog->UserAuthenticationText(*iTextToDisplay, iCertificateArray,
   647                                            iCertificateHandle, iStatus);
   697                                            iCertificateHandle, iStatus);
   648 #endif
   698         LOG(ESATSA, EInfo, "CSTSSignatureService::HandlesFromCertInfosL: over case EAuthWithText");
   649         break;
   699         break;
   650     }
   700     }
   651     case ESignWithText:
   701     case ESignWithText:
   652     {
   702     {
   653         iDialog->SignText(*iTextToDisplay, iCertificateArray,
   703         iDialog->SignText(*iTextToDisplay, iCertificateArray,
   669 // Finds the key used in iCertificate.
   719 // Finds the key used in iCertificate.
   670 // -----------------------------------------------------------------------------
   720 // -----------------------------------------------------------------------------
   671 //
   721 //
   672 void CSTSSignatureService::FindKeyL()
   722 void CSTSSignatureService::FindKeyL()
   673 {
   723 {
       
   724     LOG(ESATSA, EInfo, "+ CSTSSignatureService::FindKeyL");
   674     iKeyFilter.iKeyAlgorithm = CKeyInfoBase::EInvalidAlgorithm;
   725     iKeyFilter.iKeyAlgorithm = CKeyInfoBase::EInvalidAlgorithm;
   675     iKeyFilter.iKeyId = iCertificate->SubjectKeyId();
   726     iKeyFilter.iKeyId = iCertificate->SubjectKeyId();
   676 
   727 
   677     iKeyFilter.iUsage = EPKCS15UsageAll;
   728     iKeyFilter.iUsage = EPKCS15UsageAll;
   678     iKeys.Close();
   729     iKeys.Close();
   687 // Opens a signer for the key in iKeys.
   738 // Opens a signer for the key in iKeys.
   688 // -----------------------------------------------------------------------------
   739 // -----------------------------------------------------------------------------
   689 //
   740 //
   690 void CSTSSignatureService::OpenSignerL()
   741 void CSTSSignatureService::OpenSignerL()
   691 {
   742 {
       
   743     LOG(ESATSA, EInfo, "+ CSTSSignatureService::OpenSignerL");
   692     // if key is not found, display security element prompt and search again
   744     // if key is not found, display security element prompt and search again
   693     if (iKeys.Count() == 0)
   745     if (iKeys.Count() == 0)
   694     {
   746     {
   695         if (iSecurityElementPrompt)
   747         if (iSecurityElementPrompt)
   696         {
   748         {
   749 // Signs the message
   801 // Signs the message
   750 // -----------------------------------------------------------------------------
   802 // -----------------------------------------------------------------------------
   751 //
   803 //
   752 void CSTSSignatureService::SignL()
   804 void CSTSSignatureService::SignL()
   753 {
   805 {
       
   806     LOG(ESATSA, EInfo, "+ CSTSSignatureService::SignL");
       
   807 
   754     // first we create a digest of the message
   808     // first we create a digest of the message
   755 
   809 
   756     CSHA1* sha = CSHA1::NewL();
   810     CSHA1* sha = CSHA1::NewL();
   757     CleanupStack::PushL(sha);
   811     CleanupStack::PushL(sha);
   758     TPtrC8 hash = sha->Hash(iMessage);
   812     TPtrC8 hash = sha->Hash(iMessage);
   836 // Creates signed data
   890 // Creates signed data
   837 // -----------------------------------------------------------------------------
   891 // -----------------------------------------------------------------------------
   838 //
   892 //
   839 void CSTSSignatureService::CreateSignedDataL()
   893 void CSTSSignatureService::CreateSignedDataL()
   840 {
   894 {
       
   895     LOG(ESATSA, EInfo, "+ CSTSSignatureService::CreateSignedDataL");
   841     CCTKeyInfo* keyInfo = iKeys[0];
   896     CCTKeyInfo* keyInfo = iKeys[0];
   842 
   897 
   843     HBufC8* signature = NULL;
   898     HBufC8* signature = NULL;
   844     switch (keyInfo->Algorithm())
   899     switch (keyInfo->Algorithm())
   845     {
   900     {
   930 
   985 
   931     CleanupStack::PopAndDestroy(8);
   986     CleanupStack::PopAndDestroy(8);
   932     if (iShowNotes)
   987     if (iShowNotes)
   933     {
   988     {
   934         iState = EFinalNote;
   989         iState = EFinalNote;
   935 #ifndef RD_JAVA_S60_RELEASE_10_1_ONWARDS		
       
   936         iPKIDialog->Note(MPKIDialog::ESignatureDone, iStatus);
   990         iPKIDialog->Note(MPKIDialog::ESignatureDone, iStatus);
   937 #endif		
       
   938         SetActive();
   991         SetActive();
   939     }
   992     }
   940     else
   993     else
   941     {
   994     {
   942         Complete(KErrNone);
   995         Complete(KErrNone);
   943     }
   996     }
   944 
   997     LOG(ESATSA, EInfo, "-- ret CSTSSignatureService::CreateSignedDataL");
   945 }
   998 }
   946 
   999 
   947 // -----------------------------------------------------------------------------
  1000 // -----------------------------------------------------------------------------
   948 // CSTSSignatureService::ConvertUTF8ToUnicodeLC()
  1001 // CSTSSignatureService::ConvertUTF8ToUnicodeLC()
   949 // Converts UTF8 data to Unicode
  1002 // Converts UTF8 data to Unicode