os/security/cryptoplugins/cryptospiplugins/source/softwarecrypto/rsasignerimpl.cpp
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/security/cryptoplugins/cryptospiplugins/source/softwarecrypto/rsasignerimpl.cpp Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,132 @@
1.4 +/*
1.5 +* Copyright (c) 2006-2009 Nokia Corporation and/or its subsidiary(-ies).
1.6 +* All rights reserved.
1.7 +* This component and the accompanying materials are made available
1.8 +* under the terms of the License "Eclipse Public License v1.0"
1.9 +* which accompanies this distribution, and is available
1.10 +* at the URL "http://www.eclipse.org/legal/epl-v10.html".
1.11 +*
1.12 +* Initial Contributors:
1.13 +* Nokia Corporation - initial contribution.
1.14 +*
1.15 +* Contributors:
1.16 +*
1.17 +* Description:
1.18 +*
1.19 +*/
1.20 +
1.21 +
1.22 +#include "rsasignerimpl.h"
1.23 +#include "pluginconfig.h"
1.24 +#include "rsafunction.h"
1.25 +
1.26 +using namespace SoftwareCrypto;
1.27 +
1.28 +// Implementation of CRSASignerImpl
1.29 +CRSASignerImpl* CRSASignerImpl::NewL(const CKey& aKey, TUid aPaddingMode)
1.30 + {
1.31 + CRSASignerImpl* self = CRSASignerImpl::NewLC(aKey, aPaddingMode);
1.32 + CleanupStack::Pop(self);
1.33 + return self;
1.34 + }
1.35 +
1.36 +CRSASignerImpl* CRSASignerImpl::NewLC(const CKey& aKey, TUid aPaddingMode)
1.37 + {
1.38 + CRSASignerImpl* self = new(ELeave) CRSASignerImpl(aPaddingMode);
1.39 + CleanupStack::PushL(self);
1.40 + self->ConstructL(aKey);
1.41 + return self;
1.42 + }
1.43 +
1.44 +CRSASignerImpl::CRSASignerImpl(TUid aPaddingMode)
1.45 + : iPaddingMode(aPaddingMode)
1.46 + {
1.47 + }
1.48 +
1.49 +CRSASignerImpl::~CRSASignerImpl()
1.50 + {
1.51 + delete iPadding;
1.52 + }
1.53 +
1.54 +void CRSASignerImpl::ConstructL(const CKey& aKey)
1.55 + {
1.56 + CSignerImpl::ConstructL(aKey);
1.57 + SetPaddingModeL(iPaddingMode);
1.58 + }
1.59 +
1.60 +CExtendedCharacteristics* CRSASignerImpl::CreateExtendedCharacteristicsL()
1.61 + {
1.62 + // All Symbian software plug-ins have unlimited concurrency, cannot be reserved
1.63 + // for exclusive use and are not CERTIFIED to be standards compliant.
1.64 + return CExtendedCharacteristics::NewL(KMaxTInt, EFalse);
1.65 + }
1.66 +
1.67 +const CExtendedCharacteristics* CRSASignerImpl::GetExtendedCharacteristicsL()
1.68 + {
1.69 + return CRSASignerImpl::CreateExtendedCharacteristicsL();
1.70 + }
1.71 +
1.72 +TUid CRSASignerImpl::ImplementationUid() const
1.73 + {
1.74 + return KCryptoPluginRsaSignerUid;
1.75 + }
1.76 +
1.77 +void CRSASignerImpl::SetKeyL(const CKey& aPrivateKey)
1.78 + {
1.79 + DoSetKeyL(aPrivateKey);
1.80 + Reset();
1.81 + }
1.82 +
1.83 +void CRSASignerImpl::SetPaddingModeL(TUid aPaddingMode)
1.84 + {
1.85 + CPadding* padding(0);
1.86 + switch (aPaddingMode.iUid)
1.87 + {
1.88 + case KPaddingModeNone:
1.89 + padding = CPaddingNone::NewL(GetMaximumOutputLengthL());
1.90 + break;
1.91 + case KPaddingModePkcs1_v1_5_Signature:
1.92 + padding = CPaddingPKCS1Signature::NewL(GetMaximumOutputLengthL());
1.93 + break;
1.94 + default:
1.95 + User::Leave(KErrNotSupported);
1.96 + }
1.97 +
1.98 + delete iPadding;
1.99 + iPadding = padding;
1.100 + iPaddingMode = aPaddingMode;
1.101 + Reset();
1.102 + }
1.103 +
1.104 +TInt CRSASignerImpl::GetMaximumInputLengthL() const
1.105 + {
1.106 + return GetMaximumOutputLengthL() - iPadding->MinPaddingLength();
1.107 + }
1.108 +
1.109 +TInt CRSASignerImpl::GetMaximumOutputLengthL() const
1.110 + {
1.111 + const TInteger& paramN = iKey->GetBigIntL(KRsaKeyParameterNUid);
1.112 + return paramN.ByteCount();
1.113 + }
1.114 +
1.115 +void CRSASignerImpl::SignL(const TDesC8& aInput, CCryptoParams& aSignature)
1.116 + {
1.117 + HBufC8* buf = HBufC8::NewLC(GetMaximumOutputLengthL());
1.118 + TPtr8 ptr = buf->Des();
1.119 +
1.120 + //The following will panic if aInput is larger than MaxOutputLength() It is
1.121 + //likely that the caller has passed in something that has not been hashed.
1.122 + //This is a programming, and likely a security error, in client code, not a
1.123 + //problem here.
1.124 + iPadding->PadL(aInput, ptr);
1.125 +
1.126 + RInteger input = RInteger::NewL(ptr);
1.127 + CleanupClosePushL(input);
1.128 + RInteger output;
1.129 +
1.130 + RSAFunction::SignL(*iKey, input, output);
1.131 + CleanupClosePushL(output);
1.132 +
1.133 + aSignature.AddL(output, KRsaSignatureParameterSUid);
1.134 + CleanupStack::PopAndDestroy(3, buf); //input, buf
1.135 + }