17
|
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 "rsaverifyimpl.h"
|
|
20 |
#include "pluginconfig.h"
|
|
21 |
#include "rsafunction.h"
|
|
22 |
|
|
23 |
using namespace SoftwareCrypto;
|
|
24 |
|
|
25 |
// Implementation of CRSAVerifierImpl
|
|
26 |
CRSAVerifierImpl* CRSAVerifierImpl::NewL(const CKey& aKey, TUid aPaddingMode)
|
|
27 |
{
|
|
28 |
CRSAVerifierImpl* self = CRSAVerifierImpl::NewLC(aKey, aPaddingMode);
|
|
29 |
CleanupStack::Pop(self);
|
|
30 |
return self;
|
|
31 |
}
|
|
32 |
|
|
33 |
CRSAVerifierImpl* CRSAVerifierImpl::NewLC(const CKey& aKey, TUid aPaddingMode)
|
|
34 |
{
|
|
35 |
CRSAVerifierImpl* self = new(ELeave) CRSAVerifierImpl(aPaddingMode);
|
|
36 |
CleanupStack::PushL(self);
|
|
37 |
self->ConstructL(aKey);
|
|
38 |
return self;
|
|
39 |
}
|
|
40 |
|
|
41 |
CRSAVerifierImpl::CRSAVerifierImpl(TUid aPaddingMode)
|
|
42 |
: iPaddingMode(aPaddingMode)
|
|
43 |
{
|
|
44 |
}
|
|
45 |
|
|
46 |
CRSAVerifierImpl::~CRSAVerifierImpl()
|
|
47 |
{
|
|
48 |
delete iPadding;
|
|
49 |
}
|
|
50 |
|
|
51 |
void CRSAVerifierImpl::ConstructL(const CKey& aKey)
|
|
52 |
{
|
|
53 |
CVerifierImpl::ConstructL(aKey);
|
|
54 |
SetPaddingModeL(iPaddingMode);
|
|
55 |
}
|
|
56 |
|
|
57 |
CExtendedCharacteristics* CRSAVerifierImpl::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* CRSAVerifierImpl::GetExtendedCharacteristicsL()
|
|
65 |
{
|
|
66 |
return CRSAVerifierImpl::CreateExtendedCharacteristicsL();
|
|
67 |
}
|
|
68 |
|
|
69 |
TUid CRSAVerifierImpl::ImplementationUid() const
|
|
70 |
{
|
|
71 |
return KCryptoPluginRsaVerifierUid;
|
|
72 |
}
|
|
73 |
|
|
74 |
void CRSAVerifierImpl::SetPaddingModeL(TUid aPaddingMode)
|
|
75 |
{
|
|
76 |
CPadding* padding(0);
|
|
77 |
switch (aPaddingMode.iUid)
|
|
78 |
{
|
|
79 |
case KPaddingModeNone:
|
|
80 |
padding = CPaddingNone::NewL(GetMaximumOutputLengthL());
|
|
81 |
break;
|
|
82 |
case KPaddingModePkcs1_v1_5_Signature:
|
|
83 |
padding = CPaddingPKCS1Signature::NewL(GetMaximumOutputLengthL());
|
|
84 |
break;
|
|
85 |
default:
|
|
86 |
User::Leave(KErrNotSupported);
|
|
87 |
}
|
|
88 |
delete iPadding;
|
|
89 |
iPadding = padding;
|
|
90 |
iPaddingMode = aPaddingMode;
|
|
91 |
Reset();
|
|
92 |
}
|
|
93 |
|
|
94 |
void CRSAVerifierImpl::SetKeyL(const CKey& aPublicKey)
|
|
95 |
{
|
|
96 |
DoSetKeyL(aPublicKey);
|
|
97 |
Reset();
|
|
98 |
}
|
|
99 |
|
|
100 |
TInt CRSAVerifierImpl::GetMaximumInputLengthL() const
|
|
101 |
{
|
|
102 |
return GetMaximumOutputLengthL() - iPadding->MinPaddingLength();
|
|
103 |
}
|
|
104 |
|
|
105 |
TInt CRSAVerifierImpl::GetMaximumOutputLengthL() const
|
|
106 |
{
|
|
107 |
const TInteger& paramN = iKey->GetBigIntL(KRsaKeyParameterNUid);
|
|
108 |
return paramN.ByteCount();
|
|
109 |
}
|
|
110 |
|
|
111 |
void CRSAVerifierImpl::VerifyL(const TDesC8& aInput, const CCryptoParams& aSignature, TBool& aVerificationResult)
|
|
112 |
{
|
|
113 |
HBufC8* output = NULL;
|
|
114 |
InverseSignL(output, aSignature);
|
|
115 |
CleanupStack::PushL(output);
|
|
116 |
|
|
117 |
// is the original hash the same as the hash extracted from the signature
|
|
118 |
aVerificationResult = EFalse;
|
|
119 |
if (!output->Compare(aInput))
|
|
120 |
{
|
|
121 |
aVerificationResult = ETrue;
|
|
122 |
}
|
|
123 |
CleanupStack::PopAndDestroy(output);
|
|
124 |
}
|
|
125 |
|
|
126 |
void CRSAVerifierImpl::InverseSignL(HBufC8*& aOutput, const CCryptoParams& aSignature)
|
|
127 |
{
|
|
128 |
// extract the original hash from the signature
|
|
129 |
const TInteger& signature = aSignature.GetBigIntL(KRsaSignatureParameterSUid);
|
|
130 |
RInteger output;
|
|
131 |
RSAFunction::VerifyL(*iKey, signature, output);
|
|
132 |
CleanupClosePushL(output);
|
|
133 |
|
|
134 |
// format the extracted hash so it can be compared with the original hash
|
|
135 |
HBufC8* paddedHashPtr = output.BufferLC();
|
|
136 |
aOutput = HBufC8::NewLC(GetMaximumOutputLengthL());
|
|
137 |
TPtr8 unpaddedHash = aOutput->Des();
|
|
138 |
|
|
139 |
iPadding->UnPadL(*paddedHashPtr, unpaddedHash);
|
|
140 |
|
|
141 |
CleanupStack::Pop(aOutput);
|
|
142 |
CleanupStack::PopAndDestroy(2, &output);
|
|
143 |
}
|