os/security/cryptoplugins/cryptospiplugins/source/softwarecrypto/rsasignerimpl.cpp
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
sl@0
     1
/*
sl@0
     2
* Copyright (c) 2006-2009 Nokia Corporation and/or its subsidiary(-ies).
sl@0
     3
* All rights reserved.
sl@0
     4
* This component and the accompanying materials are made available
sl@0
     5
* under the terms of the License "Eclipse Public License v1.0"
sl@0
     6
* which accompanies this distribution, and is available
sl@0
     7
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
sl@0
     8
*
sl@0
     9
* Initial Contributors:
sl@0
    10
* Nokia Corporation - initial contribution.
sl@0
    11
*
sl@0
    12
* Contributors:
sl@0
    13
*
sl@0
    14
* Description: 
sl@0
    15
*
sl@0
    16
*/
sl@0
    17
sl@0
    18
sl@0
    19
#include "rsasignerimpl.h"
sl@0
    20
#include "pluginconfig.h"
sl@0
    21
#include "rsafunction.h"
sl@0
    22
sl@0
    23
using namespace SoftwareCrypto;
sl@0
    24
sl@0
    25
// Implementation of CRSASignerImpl 
sl@0
    26
CRSASignerImpl* CRSASignerImpl::NewL(const CKey& aKey, TUid aPaddingMode)
sl@0
    27
	{
sl@0
    28
	CRSASignerImpl* self = CRSASignerImpl::NewLC(aKey, aPaddingMode);
sl@0
    29
	CleanupStack::Pop(self);
sl@0
    30
	return self;
sl@0
    31
	}
sl@0
    32
sl@0
    33
CRSASignerImpl* CRSASignerImpl::NewLC(const CKey& aKey, TUid aPaddingMode)
sl@0
    34
	{
sl@0
    35
	CRSASignerImpl* self = new(ELeave) CRSASignerImpl(aPaddingMode);
sl@0
    36
	CleanupStack::PushL(self);
sl@0
    37
	self->ConstructL(aKey);
sl@0
    38
	return self;
sl@0
    39
	}
sl@0
    40
sl@0
    41
CRSASignerImpl::CRSASignerImpl(TUid aPaddingMode) 
sl@0
    42
	: iPaddingMode(aPaddingMode)
sl@0
    43
	{
sl@0
    44
	}
sl@0
    45
sl@0
    46
CRSASignerImpl::~CRSASignerImpl()
sl@0
    47
	{
sl@0
    48
	delete iPadding;
sl@0
    49
	}
sl@0
    50
sl@0
    51
void CRSASignerImpl::ConstructL(const CKey& aKey)
sl@0
    52
	{
sl@0
    53
	CSignerImpl::ConstructL(aKey);
sl@0
    54
	SetPaddingModeL(iPaddingMode);
sl@0
    55
	}
sl@0
    56
sl@0
    57
CExtendedCharacteristics* CRSASignerImpl::CreateExtendedCharacteristicsL()
sl@0
    58
	{
sl@0
    59
	// All Symbian software plug-ins have unlimited concurrency, cannot be reserved
sl@0
    60
	// for exclusive use and are not CERTIFIED to be standards compliant.
sl@0
    61
	return CExtendedCharacteristics::NewL(KMaxTInt, EFalse);
sl@0
    62
	}
sl@0
    63
sl@0
    64
const CExtendedCharacteristics* CRSASignerImpl::GetExtendedCharacteristicsL()
sl@0
    65
	{
sl@0
    66
	return CRSASignerImpl::CreateExtendedCharacteristicsL();
sl@0
    67
	}
sl@0
    68
sl@0
    69
TUid CRSASignerImpl::ImplementationUid() const
sl@0
    70
	{
sl@0
    71
	return KCryptoPluginRsaSignerUid;
sl@0
    72
	}
sl@0
    73
sl@0
    74
void CRSASignerImpl::SetKeyL(const CKey& aPrivateKey) 
sl@0
    75
	{
sl@0
    76
	DoSetKeyL(aPrivateKey);
sl@0
    77
	Reset();
sl@0
    78
	}
sl@0
    79
sl@0
    80
void CRSASignerImpl::SetPaddingModeL(TUid aPaddingMode)
sl@0
    81
	{
sl@0
    82
	CPadding* padding(0);
sl@0
    83
	switch (aPaddingMode.iUid)
sl@0
    84
		{
sl@0
    85
		case KPaddingModeNone:
sl@0
    86
			padding = CPaddingNone::NewL(GetMaximumOutputLengthL());
sl@0
    87
			break;
sl@0
    88
		case KPaddingModePkcs1_v1_5_Signature:
sl@0
    89
			padding = CPaddingPKCS1Signature::NewL(GetMaximumOutputLengthL());
sl@0
    90
			break;
sl@0
    91
		default:
sl@0
    92
			User::Leave(KErrNotSupported);
sl@0
    93
		}
sl@0
    94
		
sl@0
    95
	delete iPadding;
sl@0
    96
	iPadding = padding;
sl@0
    97
	iPaddingMode = aPaddingMode;
sl@0
    98
	Reset();
sl@0
    99
	}
sl@0
   100
sl@0
   101
TInt CRSASignerImpl::GetMaximumInputLengthL() const
sl@0
   102
	{
sl@0
   103
	return GetMaximumOutputLengthL() - iPadding->MinPaddingLength();	
sl@0
   104
	}
sl@0
   105
sl@0
   106
TInt CRSASignerImpl::GetMaximumOutputLengthL() const
sl@0
   107
	{
sl@0
   108
	const TInteger& paramN = iKey->GetBigIntL(KRsaKeyParameterNUid);
sl@0
   109
	return paramN.ByteCount();	
sl@0
   110
	}
sl@0
   111
sl@0
   112
void CRSASignerImpl::SignL(const TDesC8& aInput, CCryptoParams& aSignature) 
sl@0
   113
	{
sl@0
   114
	HBufC8* buf = HBufC8::NewLC(GetMaximumOutputLengthL());
sl@0
   115
	TPtr8 ptr = buf->Des();
sl@0
   116
	
sl@0
   117
	//The following will panic if aInput is larger than MaxOutputLength() It is
sl@0
   118
	//likely that the caller has passed in something that has not been hashed.
sl@0
   119
	//This is a programming, and likely a security error, in client code, not a
sl@0
   120
	//problem here.
sl@0
   121
	iPadding->PadL(aInput, ptr);
sl@0
   122
sl@0
   123
	RInteger input = RInteger::NewL(ptr);
sl@0
   124
	CleanupClosePushL(input);
sl@0
   125
	RInteger output;
sl@0
   126
sl@0
   127
	RSAFunction::SignL(*iKey, input, output);
sl@0
   128
	CleanupClosePushL(output);
sl@0
   129
sl@0
   130
	aSignature.AddL(output, KRsaSignatureParameterSUid);
sl@0
   131
	CleanupStack::PopAndDestroy(3, buf); //input, buf
sl@0
   132
	}