sl@0
|
1 |
/*
|
sl@0
|
2 |
* Copyright (c) 2007-2010 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 |
* Example CTestStep derived implementation
|
sl@0
|
16 |
*
|
sl@0
|
17 |
*/
|
sl@0
|
18 |
|
sl@0
|
19 |
|
sl@0
|
20 |
/**
|
sl@0
|
21 |
@file
|
sl@0
|
22 |
@internalTechnology
|
sl@0
|
23 |
*/
|
sl@0
|
24 |
#include "signerverifierstep.h"
|
sl@0
|
25 |
|
sl@0
|
26 |
#include <cryptospi/keypair.h>
|
sl@0
|
27 |
#include <cryptospi/cryptosignatureapi.h>
|
sl@0
|
28 |
#include <cryptospi/cryptokeypairgeneratorapi.h>
|
sl@0
|
29 |
|
sl@0
|
30 |
using namespace CryptoSpi;
|
sl@0
|
31 |
|
sl@0
|
32 |
|
sl@0
|
33 |
CSignerVerifierStep::~CSignerVerifierStep()
|
sl@0
|
34 |
{
|
sl@0
|
35 |
}
|
sl@0
|
36 |
|
sl@0
|
37 |
CSignerVerifierStep::CSignerVerifierStep()
|
sl@0
|
38 |
{
|
sl@0
|
39 |
SetTestStepName(KSignerVerifierStep);
|
sl@0
|
40 |
}
|
sl@0
|
41 |
|
sl@0
|
42 |
TVerdict CSignerVerifierStep::doTestStepPreambleL()
|
sl@0
|
43 |
{
|
sl@0
|
44 |
SetTestStepResult(EPass);
|
sl@0
|
45 |
return TestStepResult();
|
sl@0
|
46 |
}
|
sl@0
|
47 |
|
sl@0
|
48 |
|
sl@0
|
49 |
TVerdict CSignerVerifierStep::doTestStepL()
|
sl@0
|
50 |
{
|
sl@0
|
51 |
INFO_PRINTF1(_L("*** Signer/Verifier - Signature and Verification ***"));
|
sl@0
|
52 |
INFO_PRINTF2(_L("HEAP CELLS: %d"), User::CountAllocCells());
|
sl@0
|
53 |
|
sl@0
|
54 |
if (TestStepResult()==EPass)
|
sl@0
|
55 |
{
|
sl@0
|
56 |
//Assume faliure, unless all is successful
|
sl@0
|
57 |
SetTestStepResult(EFail);
|
sl@0
|
58 |
|
sl@0
|
59 |
TVariantPtrC testVariant;
|
sl@0
|
60 |
TVariantPtrC typeVariant;
|
sl@0
|
61 |
TVariantPtrC dataVariant;
|
sl@0
|
62 |
|
sl@0
|
63 |
if( !GetStringFromConfig(ConfigSection(),KConfigSignVerifyType, typeVariant)
|
sl@0
|
64 |
|| !GetStringFromConfig(ConfigSection(),KConfigSignVerifyData, dataVariant))
|
sl@0
|
65 |
{
|
sl@0
|
66 |
// Leave if there's any error.
|
sl@0
|
67 |
User::Leave(KErrNotFound);
|
sl@0
|
68 |
}
|
sl@0
|
69 |
else
|
sl@0
|
70 |
{
|
sl@0
|
71 |
TUid sigType = typeVariant;
|
sl@0
|
72 |
|
sl@0
|
73 |
//Construct a Crypto Parameters object to store the necessary key pair generator parameters
|
sl@0
|
74 |
CCryptoParams* keyParams = CCryptoParams::NewL();
|
sl@0
|
75 |
CleanupStack::PushL(keyParams);
|
sl@0
|
76 |
|
sl@0
|
77 |
//Create Key Pair Generator Object
|
sl@0
|
78 |
CKeyPairGenerator * keypairImpl = NULL;
|
sl@0
|
79 |
|
sl@0
|
80 |
//Define error code as first TRAP is beyond scope
|
sl@0
|
81 |
TInt err = 0;
|
sl@0
|
82 |
|
sl@0
|
83 |
if (sigType == KRsaSignerUid)
|
sl@0
|
84 |
{
|
sl@0
|
85 |
INFO_PRINTF1(_L("Creating Key Pair Generator (RSA)..."));
|
sl@0
|
86 |
|
sl@0
|
87 |
keyParams->AddL(KKeyExponent, KRsaKeyParameterEUid);
|
sl@0
|
88 |
keyParams->AddL(KRsaPrivateKeyStandard, KRsaKeyTypeUid);
|
sl@0
|
89 |
|
sl@0
|
90 |
//Construct an 'RSA' Key Pair Generator Implementation Object
|
sl@0
|
91 |
TRAP_LOG(err,CKeyPairGeneratorFactory::CreateKeyPairGeneratorL(keypairImpl,
|
sl@0
|
92 |
KRSAKeyPairGeneratorUid,
|
sl@0
|
93 |
keyParams));
|
sl@0
|
94 |
}
|
sl@0
|
95 |
else
|
sl@0
|
96 |
{
|
sl@0
|
97 |
INFO_PRINTF1(_L("Creating Key Pair Generator (DSA)..."));
|
sl@0
|
98 |
|
sl@0
|
99 |
//Construct a 'DSA' Key Pair Generator Implementation Object
|
sl@0
|
100 |
TRAP_LOG(err,CKeyPairGeneratorFactory::CreateKeyPairGeneratorL(keypairImpl,
|
sl@0
|
101 |
KDSAKeyPairGeneratorUid,
|
sl@0
|
102 |
NULL));
|
sl@0
|
103 |
}
|
sl@0
|
104 |
|
sl@0
|
105 |
CleanupStack::PushL(keypairImpl);
|
sl@0
|
106 |
|
sl@0
|
107 |
// Generate a Key Pair
|
sl@0
|
108 |
INFO_PRINTF1(_L("Generating Key Pair..."));
|
sl@0
|
109 |
|
sl@0
|
110 |
CKeyPair* keyPair = NULL;
|
sl@0
|
111 |
|
sl@0
|
112 |
TRAP_LOG(err,keypairImpl->GenerateKeyPairL(1024,
|
sl@0
|
113 |
*keyParams,
|
sl@0
|
114 |
keyPair));
|
sl@0
|
115 |
|
sl@0
|
116 |
CleanupStack::PushL(keyPair);
|
sl@0
|
117 |
|
sl@0
|
118 |
|
sl@0
|
119 |
// Create a Signer Object
|
sl@0
|
120 |
INFO_PRINTF1(_L("Creating Signer Object..."));
|
sl@0
|
121 |
CCryptoParams* svParams = CCryptoParams::NewL();
|
sl@0
|
122 |
CleanupStack::PushL(svParams);
|
sl@0
|
123 |
|
sl@0
|
124 |
CSigner * implsig = NULL;
|
sl@0
|
125 |
TRAP_LOG(err,CSignatureFactory::CreateSignerL(implsig,
|
sl@0
|
126 |
sigType,
|
sl@0
|
127 |
keyPair->PublicKey(),
|
sl@0
|
128 |
KPaddingModePkcs1_v1_5_SignatureUid,
|
sl@0
|
129 |
svParams));
|
sl@0
|
130 |
|
sl@0
|
131 |
CleanupStack::PushL(implsig);
|
sl@0
|
132 |
|
sl@0
|
133 |
|
sl@0
|
134 |
// Create a Verifier
|
sl@0
|
135 |
INFO_PRINTF1(_L("Creating Verifier Object..."));
|
sl@0
|
136 |
|
sl@0
|
137 |
CVerifier * implver = NULL;
|
sl@0
|
138 |
TRAP_LOG(err,CSignatureFactory::CreateVerifierL(implver,
|
sl@0
|
139 |
sigType == KRsaSignerUid ? KRsaVerifierUid : KDsaVerifierUid,
|
sl@0
|
140 |
keyPair->PrivateKey(),
|
sl@0
|
141 |
KPaddingModePkcs1_v1_5_SignatureUid,
|
sl@0
|
142 |
svParams));
|
sl@0
|
143 |
|
sl@0
|
144 |
CleanupStack::PushL(implver);
|
sl@0
|
145 |
|
sl@0
|
146 |
//Define a new signature object
|
sl@0
|
147 |
CCryptoParams *signature = CCryptoParams::NewL();
|
sl@0
|
148 |
CleanupStack::PushL(signature);
|
sl@0
|
149 |
|
sl@0
|
150 |
//Convert the source data to an 8 Bit Descriptor
|
sl@0
|
151 |
HBufC8* sourceData = HBufC8::NewLC(dataVariant.Length());
|
sl@0
|
152 |
TPtr8 sourceDataPtr = sourceData->Des();
|
sl@0
|
153 |
|
sl@0
|
154 |
sourceDataPtr.Copy(dataVariant);
|
sl@0
|
155 |
|
sl@0
|
156 |
//Set the signer object to use the 'Private' Key of the key pair
|
sl@0
|
157 |
INFO_PRINTF1(_L("Setting Signer Private Key..."));
|
sl@0
|
158 |
TRAP_LOG(err,implsig->SetKeyL(keyPair->PrivateKey()));
|
sl@0
|
159 |
|
sl@0
|
160 |
//Generate a signature for the source data
|
sl@0
|
161 |
INFO_PRINTF1(_L("Signing..."));
|
sl@0
|
162 |
TRAP_LOG(err,implsig->SignL(*sourceData, *signature));
|
sl@0
|
163 |
|
sl@0
|
164 |
TBool bResult = EFalse;
|
sl@0
|
165 |
|
sl@0
|
166 |
//Set the verifier object to use the 'Public' Key of the key pair
|
sl@0
|
167 |
INFO_PRINTF1(_L("Setting Verifier Public Key..."));
|
sl@0
|
168 |
TRAP_LOG(err,implver->SetKeyL(keyPair->PublicKey()));
|
sl@0
|
169 |
|
sl@0
|
170 |
//Verify that the generated signature is valid for the given data
|
sl@0
|
171 |
INFO_PRINTF1(_L("Verifying..."));
|
sl@0
|
172 |
TRAP_LOG(err,implver->VerifyL(*sourceData, *signature, bResult));
|
sl@0
|
173 |
|
sl@0
|
174 |
if (bResult)
|
sl@0
|
175 |
{
|
sl@0
|
176 |
// verifier agrees signature is valid
|
sl@0
|
177 |
INFO_PRINTF1(_L("PASS: Verification of Generated Signature Successful"));
|
sl@0
|
178 |
SetTestStepResult(EPass);
|
sl@0
|
179 |
}
|
sl@0
|
180 |
else
|
sl@0
|
181 |
{
|
sl@0
|
182 |
//verification failure, signature is invalid
|
sl@0
|
183 |
ERR_PRINTF2(_L("FAIL: Verification Failure of Generated Signature - %d"), err);
|
sl@0
|
184 |
SetTestStepResult(EFail);
|
sl@0
|
185 |
}
|
sl@0
|
186 |
|
sl@0
|
187 |
CleanupStack::PopAndDestroy(sourceData);
|
sl@0
|
188 |
CleanupStack::PopAndDestroy(signature);
|
sl@0
|
189 |
CleanupStack::PopAndDestroy(implver);
|
sl@0
|
190 |
CleanupStack::PopAndDestroy(implsig);
|
sl@0
|
191 |
CleanupStack::PopAndDestroy(svParams);
|
sl@0
|
192 |
|
sl@0
|
193 |
CleanupStack::PopAndDestroy(keyPair);
|
sl@0
|
194 |
CleanupStack::PopAndDestroy(keypairImpl);
|
sl@0
|
195 |
CleanupStack::PopAndDestroy(keyParams);
|
sl@0
|
196 |
}
|
sl@0
|
197 |
|
sl@0
|
198 |
INFO_PRINTF2(_L("HEAP CELLS: %d"), User::CountAllocCells());
|
sl@0
|
199 |
|
sl@0
|
200 |
}
|
sl@0
|
201 |
return TestStepResult();
|
sl@0
|
202 |
}
|
sl@0
|
203 |
|
sl@0
|
204 |
|
sl@0
|
205 |
|
sl@0
|
206 |
TVerdict CSignerVerifierStep::doTestStepPostambleL()
|
sl@0
|
207 |
{
|
sl@0
|
208 |
return TestStepResult();
|
sl@0
|
209 |
}
|