cryptoplugins/cryptospiplugins/source/softwarecrypto/rsasignerimpl.cpp
changeset 19 cd501b96611d
equal deleted inserted replaced
15:da2ae96f639b 19:cd501b96611d
       
     1 /*
       
     2 * Copyright (c) 2006-2009 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 the License "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: 
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include "rsasignerimpl.h"
       
    20 #include "pluginconfig.h"
       
    21 #include "rsafunction.h"
       
    22 
       
    23 using namespace SoftwareCrypto;
       
    24 
       
    25 // Implementation of CRSASignerImpl 
       
    26 CRSASignerImpl* CRSASignerImpl::NewL(const CKey& aKey, TUid aPaddingMode)
       
    27 	{
       
    28 	CRSASignerImpl* self = CRSASignerImpl::NewLC(aKey, aPaddingMode);
       
    29 	CleanupStack::Pop(self);
       
    30 	return self;
       
    31 	}
       
    32 
       
    33 CRSASignerImpl* CRSASignerImpl::NewLC(const CKey& aKey, TUid aPaddingMode)
       
    34 	{
       
    35 	CRSASignerImpl* self = new(ELeave) CRSASignerImpl(aPaddingMode);
       
    36 	CleanupStack::PushL(self);
       
    37 	self->ConstructL(aKey);
       
    38 	return self;
       
    39 	}
       
    40 
       
    41 CRSASignerImpl::CRSASignerImpl(TUid aPaddingMode) 
       
    42 	: iPaddingMode(aPaddingMode)
       
    43 	{
       
    44 	}
       
    45 
       
    46 CRSASignerImpl::~CRSASignerImpl()
       
    47 	{
       
    48 	delete iPadding;
       
    49 	}
       
    50 
       
    51 void CRSASignerImpl::ConstructL(const CKey& aKey)
       
    52 	{
       
    53 	CSignerImpl::ConstructL(aKey);
       
    54 	SetPaddingModeL(iPaddingMode);
       
    55 	}
       
    56 
       
    57 CExtendedCharacteristics* CRSASignerImpl::CreateExtendedCharacteristicsL()
       
    58 	{
       
    59 	// All Symbian software plug-ins have unlimited concurrency, cannot be reserved
       
    60 	// for exclusive use and are not CERTIFIED to be standards compliant.
       
    61 	return CExtendedCharacteristics::NewL(KMaxTInt, EFalse);
       
    62 	}
       
    63 
       
    64 const CExtendedCharacteristics* CRSASignerImpl::GetExtendedCharacteristicsL()
       
    65 	{
       
    66 	return CRSASignerImpl::CreateExtendedCharacteristicsL();
       
    67 	}
       
    68 
       
    69 TUid CRSASignerImpl::ImplementationUid() const
       
    70 	{
       
    71 	return KCryptoPluginRsaSignerUid;
       
    72 	}
       
    73 
       
    74 void CRSASignerImpl::SetKeyL(const CKey& aPrivateKey) 
       
    75 	{
       
    76 	DoSetKeyL(aPrivateKey);
       
    77 	Reset();
       
    78 	}
       
    79 
       
    80 void CRSASignerImpl::SetPaddingModeL(TUid aPaddingMode)
       
    81 	{
       
    82 	CPadding* padding(0);
       
    83 	switch (aPaddingMode.iUid)
       
    84 		{
       
    85 		case KPaddingModeNone:
       
    86 			padding = CPaddingNone::NewL(GetMaximumOutputLengthL());
       
    87 			break;
       
    88 		case KPaddingModePkcs1_v1_5_Signature:
       
    89 			padding = CPaddingPKCS1Signature::NewL(GetMaximumOutputLengthL());
       
    90 			break;
       
    91 		default:
       
    92 			User::Leave(KErrNotSupported);
       
    93 		}
       
    94 		
       
    95 	delete iPadding;
       
    96 	iPadding = padding;
       
    97 	iPaddingMode = aPaddingMode;
       
    98 	Reset();
       
    99 	}
       
   100 
       
   101 TInt CRSASignerImpl::GetMaximumInputLengthL() const
       
   102 	{
       
   103 	return GetMaximumOutputLengthL() - iPadding->MinPaddingLength();	
       
   104 	}
       
   105 
       
   106 TInt CRSASignerImpl::GetMaximumOutputLengthL() const
       
   107 	{
       
   108 	const TInteger& paramN = iKey->GetBigIntL(KRsaKeyParameterNUid);
       
   109 	return paramN.ByteCount();	
       
   110 	}
       
   111 
       
   112 void CRSASignerImpl::SignL(const TDesC8& aInput, CCryptoParams& aSignature) 
       
   113 	{
       
   114 	HBufC8* buf = HBufC8::NewLC(GetMaximumOutputLengthL());
       
   115 	TPtr8 ptr = buf->Des();
       
   116 	
       
   117 	//The following will panic if aInput is larger than MaxOutputLength() It is
       
   118 	//likely that the caller has passed in something that has not been hashed.
       
   119 	//This is a programming, and likely a security error, in client code, not a
       
   120 	//problem here.
       
   121 	iPadding->PadL(aInput, ptr);
       
   122 
       
   123 	RInteger input = RInteger::NewL(ptr);
       
   124 	CleanupClosePushL(input);
       
   125 	RInteger output;
       
   126 
       
   127 	RSAFunction::SignL(*iKey, input, output);
       
   128 	CleanupClosePushL(output);
       
   129 
       
   130 	aSignature.AddL(output, KRsaSignatureParameterSUid);
       
   131 	CleanupStack::PopAndDestroy(3, buf); //input, buf
       
   132 	}