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