wim/WimPlugin/src/WimRSASigner.cpp
changeset 0 164170e6151a
equal deleted inserted replaced
-1:000000000000 0:164170e6151a
       
     1 /*
       
     2 * Copyright (c) 2002 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:  Implementation of Wim RSA signer interface
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include "WimKeyStore.h"
       
    20 #include "WimRSASigner.h"
       
    21 #include <hash.h>
       
    22 #include <mctkeystoreuids.h>            // KRSARepudiableSignerUID
       
    23 
       
    24 
       
    25 // ============================ MEMBER FUNCTIONS ===============================
       
    26 
       
    27 // -----------------------------------------------------------------------------
       
    28 // CWimRSASigner::CWimRSASigner()
       
    29 //
       
    30 // -----------------------------------------------------------------------------
       
    31 //
       
    32 CWimRSASigner::CWimRSASigner( CWimKeyStore& aClient )
       
    33 		: MCTSigner<CRSASignature*>( aClient.Token() ),iClient( aClient )
       
    34 	{                               
       
    35 	iHandle.iTokenHandle = aClient.Token().Handle();
       
    36 	}
       
    37 
       
    38 // -----------------------------------------------------------------------------
       
    39 // CWimRSASigner* CWimRSASigner::NewL()
       
    40 // Creates new Instance of CWimRSASigner
       
    41 // -----------------------------------------------------------------------------
       
    42 //
       
    43 CWimRSASigner* CWimRSASigner::NewL( CWimKeyStore& aClient )
       
    44     {
       
    45     CWimRSASigner* self = new( ELeave ) CWimRSASigner( aClient );
       
    46     return self;
       
    47     }
       
    48 
       
    49 
       
    50 // -----------------------------------------------------------------------------
       
    51 // CWimRSASigner::~CWimRSASigner()
       
    52 // Destructor
       
    53 // -----------------------------------------------------------------------------
       
    54 //
       
    55 CWimRSASigner::~CWimRSASigner()
       
    56 	{
       
    57     delete iLabel;
       
    58 	delete iDigest;
       
    59         delete iDigestBuf;
       
    60 	}
       
    61 
       
    62 // -----------------------------------------------------------------------------
       
    63 // CWimRSASigner::SetLabelL()
       
    64 // Sets label for signin key object
       
    65 // -----------------------------------------------------------------------------
       
    66 //
       
    67 void CWimRSASigner::SetLabelL( const TDesC& aLabel )
       
    68     {
       
    69     iLabel = aLabel.AllocL();
       
    70     }
       
    71 
       
    72 // -----------------------------------------------------------------------------
       
    73 // CWimRSASigner::SetObjectId()
       
    74 // Sets object id -points to a certain key
       
    75 // -----------------------------------------------------------------------------
       
    76 //
       
    77 void CWimRSASigner::SetObjectId( const TInt aObjectId )
       
    78     {
       
    79     iHandle.iObjectId = aObjectId;
       
    80     }
       
    81 
       
    82 // -----------------------------------------------------------------------------
       
    83 // CWimRSASigner::Release()
       
    84 // Release object
       
    85 // -----------------------------------------------------------------------------
       
    86 //
       
    87 void CWimRSASigner::Release()
       
    88 	{
       
    89 	MCTTokenObject::Release();
       
    90 	}
       
    91 
       
    92 // -----------------------------------------------------------------------------
       
    93 // CWimRSASigner::Label() const
       
    94 // Returns the object's human-readable label
       
    95 // -----------------------------------------------------------------------------
       
    96 //
       
    97 const TDesC& CWimRSASigner::Label() const
       
    98 	{
       
    99 	return *iLabel;
       
   100 	}
       
   101 
       
   102 // -----------------------------------------------------------------------------
       
   103 // CWimRSASigner::Token()
       
   104 // Returns a reference to the associated token
       
   105 // -----------------------------------------------------------------------------
       
   106 //
       
   107 MCTToken& CWimRSASigner::Token() const
       
   108 	{
       
   109 	return iClient.Token();
       
   110 	}
       
   111 
       
   112 // -----------------------------------------------------------------------------
       
   113 // CWimRSASigner::Type()
       
   114 // Returns a UID representing the type of the token object. The
       
   115 // meanings of possible UIDs should be documented in the
       
   116 // documentation for the interface that returns them.
       
   117 // -----------------------------------------------------------------------------
       
   118 //
       
   119 TUid CWimRSASigner::Type() const
       
   120 	{
       
   121 	return KRSARepudiableSignerUID;
       
   122 	}
       
   123 
       
   124 // -----------------------------------------------------------------------------
       
   125 // CWimRSASigner::Handle()
       
   126 // Returns a handle for the object. The primary purpose of the
       
   127 // handle is to allow token objects to be 'passed' between
       
   128 // processes. See TCTTokenObjectHandle for more details.
       
   129 // -----------------------------------------------------------------------------
       
   130 //
       
   131 TCTTokenObjectHandle CWimRSASigner::Handle() const
       
   132 	{
       
   133 	return iHandle;
       
   134 	}
       
   135 
       
   136 // -----------------------------------------------------------------------------
       
   137 // CWimRSASigner::SignMessage()
       
   138 // Sign some data. The data is hashed before the signature is created using 
       
   139 // the SHA-1 algorithm.
       
   140 // -----------------------------------------------------------------------------
       
   141 //
       
   142 void CWimRSASigner::SignMessage( const TDesC8& aPlaintext, 
       
   143   				                 CRSASignature*& aSignature, 
       
   144   				                 TRequestStatus& aStatus )
       
   145 	{
       
   146     if( iDigest )
       
   147         {
       
   148         delete iDigest;
       
   149         iDigest = NULL;
       
   150         }
       
   151     if( iDigestBuf )
       
   152         {
       
   153         delete iDigestBuf;
       
   154         iDigest = NULL;
       
   155         }
       
   156 
       
   157 	// Hash the data on the client side
       
   158 	TRAPD( err, iDigest = CSHA1::NewL() );
       
   159 
       
   160 	if ( err == KErrNone )
       
   161 		{
       
   162 	    iDigest->Update( aPlaintext );
       
   163 	    TRAP( err, iDigestBuf = HBufC8::NewL( iDigest->HashSize() ) );
       
   164 	    
       
   165 	    if (err != KErrNone )
       
   166 	       {
       
   167 	       TRequestStatus* status = &aStatus;
       
   168 		     User::RequestComplete( status, err );
       
   169 	       return;
       
   170 	    	 }
       
   171 	    
       
   172 	    TPtr8 tempPtr = iDigestBuf->Des();
       
   173 	    tempPtr.Copy( iDigest->Final() );	
       
   174 	    Sign( *iDigestBuf, aSignature, aStatus );
       
   175 		}
       
   176     else
       
   177         {
       
   178 		TRequestStatus* status = &aStatus;
       
   179 		User::RequestComplete( status, err );
       
   180         }
       
   181 	}
       
   182 
       
   183 // -----------------------------------------------------------------------------
       
   184 // CWimRSASigner::Sign()
       
   185 // Perform a raw signing operation.
       
   186 // -----------------------------------------------------------------------------
       
   187 //
       
   188 void CWimRSASigner::Sign( const TDesC8& aPlaintext, 
       
   189   				          CRSASignature*& aSignature, 
       
   190   				          TRequestStatus& aStatus )
       
   191 	{
       
   192 	iClient.NonRepudiableRSASign( Handle(), aPlaintext, aSignature, aStatus );
       
   193 	}
       
   194 
       
   195 // -----------------------------------------------------------------------------
       
   196 // CWimRSASigner::CancelSign()
       
   197 // Cancel an ongoing sign
       
   198 // -----------------------------------------------------------------------------
       
   199 //
       
   200 void CWimRSASigner::CancelSign()
       
   201 	{
       
   202     iClient.Cancel();
       
   203 	}
       
   204 
       
   205 // -----------------------------------------------------------------------------
       
   206 // CWimRSASigner::DoRelease()
       
   207 // Releases this object.
       
   208 // -----------------------------------------------------------------------------
       
   209 //
       
   210 void CWimRSASigner::DoRelease()
       
   211 	{
       
   212 	delete this;
       
   213 	}