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 "keyexchangesyncstep.h"
|
sl@0
|
25 |
|
sl@0
|
26 |
#include <cryptospi/keypair.h>
|
sl@0
|
27 |
#include <cryptospi/cryptokeypairgeneratorapi.h>
|
sl@0
|
28 |
#include <cryptospi/cryptokeyagreementapi.h>
|
sl@0
|
29 |
#include <bigint.h>
|
sl@0
|
30 |
|
sl@0
|
31 |
using namespace CryptoSpi;
|
sl@0
|
32 |
|
sl@0
|
33 |
CKeyExchangeSyncStep::~CKeyExchangeSyncStep()
|
sl@0
|
34 |
{
|
sl@0
|
35 |
}
|
sl@0
|
36 |
|
sl@0
|
37 |
CKeyExchangeSyncStep::CKeyExchangeSyncStep()
|
sl@0
|
38 |
{
|
sl@0
|
39 |
SetTestStepName(KKeyExchangeSyncStep);
|
sl@0
|
40 |
}
|
sl@0
|
41 |
|
sl@0
|
42 |
TVerdict CKeyExchangeSyncStep::doTestStepPreambleL()
|
sl@0
|
43 |
{
|
sl@0
|
44 |
SetTestStepResult(EPass);
|
sl@0
|
45 |
return TestStepResult();
|
sl@0
|
46 |
}
|
sl@0
|
47 |
|
sl@0
|
48 |
TVerdict CKeyExchangeSyncStep::doTestStepL()
|
sl@0
|
49 |
{
|
sl@0
|
50 |
INFO_PRINTF1(_L("*** Key Exchange - DH Key Agreement ***"));
|
sl@0
|
51 |
INFO_PRINTF2(_L("HEAP CELLS: %d"), User::CountAllocCells());
|
sl@0
|
52 |
|
sl@0
|
53 |
if (TestStepResult()==EPass)
|
sl@0
|
54 |
{
|
sl@0
|
55 |
//Assume faliure, unless all is successful
|
sl@0
|
56 |
SetTestStepResult(EFail);
|
sl@0
|
57 |
|
sl@0
|
58 |
TVariantPtrC testVariant;
|
sl@0
|
59 |
TVariantPtrC dhnVariant;
|
sl@0
|
60 |
TVariantPtrC dhgVariant;
|
sl@0
|
61 |
|
sl@0
|
62 |
if( !GetStringFromConfig(ConfigSection(),KConfigExchangeKey, dhnVariant) ||
|
sl@0
|
63 |
!GetStringFromConfig(ConfigSection(),KConfigExchangeKey, dhgVariant )
|
sl@0
|
64 |
)
|
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 |
/*
|
sl@0
|
72 |
* both DH keys (ie our private and their public keys) must use the same N and G parameters
|
sl@0
|
73 |
*/
|
sl@0
|
74 |
INFO_PRINTF1(_L("Creating Primes and Base Integers..."));
|
sl@0
|
75 |
|
sl@0
|
76 |
RInteger DH_N = RInteger::NewPrimeL(1024); // from ini file
|
sl@0
|
77 |
CleanupClosePushL(DH_N);
|
sl@0
|
78 |
RInteger DH_N_MinusTwo = RInteger::NewL(DH_N);
|
sl@0
|
79 |
CleanupClosePushL(DH_N_MinusTwo);
|
sl@0
|
80 |
DH_N_MinusTwo-=2;
|
sl@0
|
81 |
|
sl@0
|
82 |
RInteger DH_G = RInteger::NewRandomL(TInteger::Two(), DH_N_MinusTwo);
|
sl@0
|
83 |
CleanupClosePushL(DH_G);
|
sl@0
|
84 |
|
sl@0
|
85 |
INFO_PRINTF1(_L("Creating Key Pair Generator..."));
|
sl@0
|
86 |
|
sl@0
|
87 |
// create a DH key pair generator interface for creating the 2 key pairs
|
sl@0
|
88 |
CKeyPairGenerator* keyPairGeneratorImpl = NULL;
|
sl@0
|
89 |
|
sl@0
|
90 |
TRAPD_LOG(err,CKeyPairGeneratorFactory::CreateKeyPairGeneratorL(keyPairGeneratorImpl,
|
sl@0
|
91 |
KDHKeyPairGeneratorUid,
|
sl@0
|
92 |
NULL));
|
sl@0
|
93 |
|
sl@0
|
94 |
if(keyPairGeneratorImpl && (err==KErrNone))
|
sl@0
|
95 |
{
|
sl@0
|
96 |
|
sl@0
|
97 |
CleanupStack::PushL(keyPairGeneratorImpl);
|
sl@0
|
98 |
|
sl@0
|
99 |
// package up the common parameters N and G for use through the rest of this method
|
sl@0
|
100 |
CCryptoParams* keyParameters = CCryptoParams::NewLC();
|
sl@0
|
101 |
keyParameters->AddL(DH_N, KDhKeyParameterNUid);
|
sl@0
|
102 |
keyParameters->AddL(DH_G, KDhKeyParameterGUid);
|
sl@0
|
103 |
|
sl@0
|
104 |
/*
|
sl@0
|
105 |
* call the api to create a DH key pair for alice
|
sl@0
|
106 |
*/
|
sl@0
|
107 |
INFO_PRINTF1(_L("Generating DH ALICE Key Pair..."));
|
sl@0
|
108 |
CKeyPair* keyAlice = NULL;
|
sl@0
|
109 |
|
sl@0
|
110 |
TRAP_LOG(err,keyPairGeneratorImpl->GenerateKeyPairL(NULL,
|
sl@0
|
111 |
*keyParameters,
|
sl@0
|
112 |
keyAlice));
|
sl@0
|
113 |
CleanupStack::PushL(keyAlice);
|
sl@0
|
114 |
|
sl@0
|
115 |
/*
|
sl@0
|
116 |
* call the api to create a DH key pair for bob
|
sl@0
|
117 |
*/
|
sl@0
|
118 |
INFO_PRINTF1(_L("Generating DH BOB Key Pair..."));
|
sl@0
|
119 |
CKeyPair* keyBob = NULL;
|
sl@0
|
120 |
TRAP_LOG(err,keyPairGeneratorImpl->GenerateKeyPairL(NULL,
|
sl@0
|
121 |
*keyParameters,
|
sl@0
|
122 |
keyBob));
|
sl@0
|
123 |
CleanupStack::PushL(keyBob);
|
sl@0
|
124 |
|
sl@0
|
125 |
/*
|
sl@0
|
126 |
* get DH key agreement interfaces
|
sl@0
|
127 |
*/
|
sl@0
|
128 |
INFO_PRINTF1(_L("Generating ALICE & BOB Key Agreement Interfaces..."));
|
sl@0
|
129 |
CKeyAgreement* keyAgreementAliceImpl = NULL;
|
sl@0
|
130 |
|
sl@0
|
131 |
TRAP_LOG(err,CKeyAgreementFactory::CreateKeyAgreementL(keyAgreementAliceImpl,
|
sl@0
|
132 |
KDHAgreementUid,
|
sl@0
|
133 |
keyAlice->PrivateKey(),
|
sl@0
|
134 |
keyParameters));
|
sl@0
|
135 |
CleanupStack::PushL(keyAgreementAliceImpl);
|
sl@0
|
136 |
|
sl@0
|
137 |
CKeyAgreement* keyAgreementBobImpl = NULL;
|
sl@0
|
138 |
|
sl@0
|
139 |
TRAP_LOG(err,CKeyAgreementFactory::CreateKeyAgreementL(keyAgreementBobImpl,
|
sl@0
|
140 |
KDHAgreementUid,
|
sl@0
|
141 |
keyBob->PrivateKey(),
|
sl@0
|
142 |
keyParameters));
|
sl@0
|
143 |
CleanupStack::PushL(keyAgreementBobImpl);
|
sl@0
|
144 |
|
sl@0
|
145 |
/*
|
sl@0
|
146 |
* call the api to get a DH agreed keys
|
sl@0
|
147 |
*/
|
sl@0
|
148 |
INFO_PRINTF1(_L("Generating Agreed Keys..."));
|
sl@0
|
149 |
|
sl@0
|
150 |
CKey* agreedKeyAlice = keyAgreementAliceImpl->AgreeL(keyBob->PublicKey(), keyParameters);
|
sl@0
|
151 |
CleanupStack::PushL(agreedKeyAlice);
|
sl@0
|
152 |
|
sl@0
|
153 |
CKey* agreedKeyBob = keyAgreementBobImpl->AgreeL(keyAlice->PublicKey(), keyParameters);
|
sl@0
|
154 |
CleanupStack::PushL(agreedKeyBob);
|
sl@0
|
155 |
|
sl@0
|
156 |
/*
|
sl@0
|
157 |
* compare the agreed keys
|
sl@0
|
158 |
*/
|
sl@0
|
159 |
const TInteger& agreedKeyDataAlice = agreedKeyAlice->GetBigIntL(KSymmetricKeyParameterUid);
|
sl@0
|
160 |
const TInteger& agreedKeyDataBob = agreedKeyBob->GetBigIntL(KSymmetricKeyParameterUid);
|
sl@0
|
161 |
|
sl@0
|
162 |
if (agreedKeyDataAlice == agreedKeyDataBob)
|
sl@0
|
163 |
{
|
sl@0
|
164 |
INFO_PRINTF1(_L("*** PASS : Key Agreement Success ***"));
|
sl@0
|
165 |
SetTestStepResult(EPass);
|
sl@0
|
166 |
}
|
sl@0
|
167 |
else
|
sl@0
|
168 |
{
|
sl@0
|
169 |
INFO_PRINTF1(_L("*** FAIL : Agreed Keys Mismatch ***"));
|
sl@0
|
170 |
SetTestStepResult(EFail);
|
sl@0
|
171 |
}
|
sl@0
|
172 |
|
sl@0
|
173 |
// Set the private keys to check the new agreements.
|
sl@0
|
174 |
keyAgreementAliceImpl->SetKeyL(keyAlice->PrivateKey(), keyParameters);
|
sl@0
|
175 |
keyAgreementAliceImpl->SetKeyL(keyBob->PrivateKey(), keyParameters);
|
sl@0
|
176 |
|
sl@0
|
177 |
/*
|
sl@0
|
178 |
* call the api to get a DH agreed keys
|
sl@0
|
179 |
*/
|
sl@0
|
180 |
INFO_PRINTF1(_L("Generating Agreed Keys second time..."));
|
sl@0
|
181 |
|
sl@0
|
182 |
CKey* agreedKeyAlice1 = keyAgreementAliceImpl->AgreeL(keyBob->PublicKey(), keyParameters);
|
sl@0
|
183 |
CleanupStack::PushL(agreedKeyAlice1);
|
sl@0
|
184 |
|
sl@0
|
185 |
CKey* agreedKeyBob1 = keyAgreementBobImpl->AgreeL(keyAlice->PublicKey(), keyParameters);
|
sl@0
|
186 |
CleanupStack::PushL(agreedKeyBob1);
|
sl@0
|
187 |
|
sl@0
|
188 |
/*
|
sl@0
|
189 |
* compare the agreed keys
|
sl@0
|
190 |
*/
|
sl@0
|
191 |
const TInteger& agreedKeyDataAlice1 = agreedKeyAlice->GetBigIntL(KSymmetricKeyParameterUid);
|
sl@0
|
192 |
const TInteger& agreedKeyDataBob1 = agreedKeyBob->GetBigIntL(KSymmetricKeyParameterUid);
|
sl@0
|
193 |
|
sl@0
|
194 |
if (agreedKeyDataAlice1 != agreedKeyDataBob1)
|
sl@0
|
195 |
{
|
sl@0
|
196 |
INFO_PRINTF1(_L("*** FAIL : Second Agreed Keys Mismatch ***"));
|
sl@0
|
197 |
SetTestStepResult(EFail);
|
sl@0
|
198 |
}
|
sl@0
|
199 |
|
sl@0
|
200 |
const CCryptoParams& cryptoParams1 = agreedKeyAlice1->KeyParameters();
|
sl@0
|
201 |
const CCryptoParams& cryptoParams2 = agreedKeyBob1->KeyParameters();
|
sl@0
|
202 |
if (cryptoParams1.Count() != cryptoParams2.GetParams().Count())
|
sl@0
|
203 |
{
|
sl@0
|
204 |
INFO_PRINTF1(_L("*** FAIL : Key Parameters' Count Mismatch ***"));
|
sl@0
|
205 |
SetTestStepResult(EFail);
|
sl@0
|
206 |
}
|
sl@0
|
207 |
|
sl@0
|
208 |
TInt paramLength = 10;
|
sl@0
|
209 |
HBufC16* buf = HBufC16::NewLC(paramLength);
|
sl@0
|
210 |
TPtr16 ptr = buf->Des();
|
sl@0
|
211 |
ptr.Copy(_L("DH_N"));
|
sl@0
|
212 |
CCryptoParams* params = CCryptoParams::NewL();
|
sl@0
|
213 |
params->AddL(*buf, KDhKeyParameterNUid);
|
sl@0
|
214 |
if(!params->Count())
|
sl@0
|
215 |
{
|
sl@0
|
216 |
INFO_PRINTF1(_L("*** FAIL : Parameter construction with descriptor failed ***"));
|
sl@0
|
217 |
SetTestStepResult(EFail);
|
sl@0
|
218 |
}
|
sl@0
|
219 |
|
sl@0
|
220 |
if (agreedKeyAlice1->IsPresent(KSymmetricKeyParameterUid))
|
sl@0
|
221 |
{
|
sl@0
|
222 |
TRAPD(err, agreedKeyAlice1->GetTIntL(KSymmetricKeyParameterUid));
|
sl@0
|
223 |
if(err == KErrNone)
|
sl@0
|
224 |
{
|
sl@0
|
225 |
INFO_PRINTF1(_L("*** FAIL : Expected Key Parameter Int Value Mismatch ***"));
|
sl@0
|
226 |
SetTestStepResult(EFail);
|
sl@0
|
227 |
}
|
sl@0
|
228 |
}
|
sl@0
|
229 |
|
sl@0
|
230 |
// Clear the second key agreement elements.
|
sl@0
|
231 |
CleanupStack::PopAndDestroy(3, agreedKeyAlice1);
|
sl@0
|
232 |
|
sl@0
|
233 |
/*
|
sl@0
|
234 |
* cleanup stack - it should contain privateKey, keyAgreementImpl, publicKey, keyParameters and agreedKey
|
sl@0
|
235 |
*/
|
sl@0
|
236 |
CleanupStack::PopAndDestroy(agreedKeyBob);
|
sl@0
|
237 |
CleanupStack::PopAndDestroy(agreedKeyAlice);
|
sl@0
|
238 |
CleanupStack::PopAndDestroy(keyAgreementBobImpl);
|
sl@0
|
239 |
CleanupStack::PopAndDestroy(keyAgreementAliceImpl);
|
sl@0
|
240 |
CleanupStack::PopAndDestroy(keyBob);
|
sl@0
|
241 |
CleanupStack::PopAndDestroy(keyAlice);
|
sl@0
|
242 |
CleanupStack::PopAndDestroy(keyParameters);
|
sl@0
|
243 |
CleanupStack::PopAndDestroy(keyPairGeneratorImpl);
|
sl@0
|
244 |
}
|
sl@0
|
245 |
|
sl@0
|
246 |
CleanupStack::PopAndDestroy(&DH_G);
|
sl@0
|
247 |
CleanupStack::PopAndDestroy(&DH_N_MinusTwo);
|
sl@0
|
248 |
CleanupStack::PopAndDestroy(&DH_N);
|
sl@0
|
249 |
}
|
sl@0
|
250 |
|
sl@0
|
251 |
INFO_PRINTF2(_L("HEAP CELLS: %d"), User::CountAllocCells());
|
sl@0
|
252 |
|
sl@0
|
253 |
}
|
sl@0
|
254 |
return TestStepResult();
|
sl@0
|
255 |
}
|
sl@0
|
256 |
|
sl@0
|
257 |
TVerdict CKeyExchangeSyncStep::doTestStepPostambleL()
|
sl@0
|
258 |
{
|
sl@0
|
259 |
return TestStepResult();
|
sl@0
|
260 |
}
|