|
1 // Copyright (c) 2006-2009 Nokia Corporation and/or its subsidiary(-ies). |
|
2 // All rights reserved. |
|
3 // This component and the accompanying materials are made available |
|
4 // under the terms of "Eclipse Public License v1.0" |
|
5 // which accompanies this distribution, and is available |
|
6 // at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
7 // |
|
8 // Initial Contributors: |
|
9 // Nokia Corporation - initial contribution. |
|
10 // |
|
11 // Contributors: |
|
12 // |
|
13 // Description: |
|
14 // |
|
15 |
|
16 /** |
|
17 @file serverfinishedstep.cpp |
|
18 @internalTechnology |
|
19 */ |
|
20 #include "serverfinishedstep.h" |
|
21 |
|
22 #include <tlsprovinterface.h> |
|
23 |
|
24 CServerFinishedStep::CServerFinishedStep() |
|
25 { |
|
26 SetTestStepName(KServerFinishedStep); |
|
27 } |
|
28 |
|
29 TVerdict CServerFinishedStep::doTestStepPreambleL() |
|
30 { |
|
31 ConstructL(); |
|
32 |
|
33 CTlsCryptoAttributes* atts = Provider()->Attributes(); |
|
34 |
|
35 // Reads PSK values if included in INI file. |
|
36 ReadPskToBeUsedL(); |
|
37 |
|
38 // Reads if NULL ciphers suites are to be allowed from INI file. |
|
39 ReadUseNullCipher(); |
|
40 |
|
41 // read the "server" random |
|
42 HBufC8* random = ServerRandomL(); |
|
43 atts->iMasterSecretInput.iServerRandom.Copy(*random); |
|
44 delete random; |
|
45 |
|
46 // and the client random |
|
47 random = ClientRandomL(); |
|
48 atts->iMasterSecretInput.iClientRandom.Copy(*random); |
|
49 delete random; |
|
50 |
|
51 // we only support null compression... |
|
52 atts->iCompressionMethod = ENullCompression; |
|
53 |
|
54 // read the cipher suite for the test |
|
55 atts->iCurrentCipherSuite = CipherSuiteL(); |
|
56 |
|
57 // read the protocol version |
|
58 TTLSProtocolVersion version = ProtocolVersionL(); |
|
59 atts->iNegotiatedProtocol = version; |
|
60 atts->iProposedProtocol = version; |
|
61 |
|
62 // set the session ID and "server" name (localhost) |
|
63 atts->iSessionNameAndID.iSessionId = SessionId(); |
|
64 atts->iSessionNameAndID.iServerName.iAddress = KLocalHost; |
|
65 atts->iSessionNameAndID.iServerName.iPort = 443; |
|
66 atts->idomainName.Copy(DomainNameL()); |
|
67 |
|
68 // try and read DH params, this section may not exist |
|
69 RInteger gen; |
|
70 CleanupClosePushL(gen); |
|
71 |
|
72 RInteger prime; |
|
73 CleanupClosePushL(prime); |
|
74 |
|
75 // If cipher suite under test is uses PSK (Pre Shared Key) |
|
76 if(UsePsk()) |
|
77 { |
|
78 // Populates values for PSK |
|
79 atts->iPskConfigured = true; |
|
80 atts->iPublicKeyParams->iKeyType = EPsk; |
|
81 atts->iPublicKeyParams->iValue4 = PskIdentity(); |
|
82 atts->iPublicKeyParams->iValue5 = PskKey(); |
|
83 } |
|
84 else |
|
85 { |
|
86 // If cipher suite under test is NOT PSK |
|
87 TRAPD(err, ReadDHParamsL()); |
|
88 if (err == KErrNone) |
|
89 { |
|
90 atts->iPublicKeyParams->iKeyType = EDHE; |
|
91 |
|
92 // The params are: |
|
93 // 1 - Prime |
|
94 // 2 - Generator |
|
95 // 3 - generator ^ random mod prime |
|
96 |
|
97 atts->iPublicKeyParams->iValue1 = Prime().BufferLC(); |
|
98 CleanupStack::Pop(atts->iPublicKeyParams->iValue1); |
|
99 |
|
100 atts->iPublicKeyParams->iValue2 = Generator().BufferLC(); |
|
101 CleanupStack::Pop(atts->iPublicKeyParams->iValue2); |
|
102 |
|
103 atts->iPublicKeyParams->iValue3 = KeyPair()->PublicKey().X().BufferLC(); |
|
104 CleanupStack::Pop(atts->iPublicKeyParams->iValue3); |
|
105 |
|
106 } |
|
107 } |
|
108 |
|
109 CleanupStack::PopAndDestroy(2, &gen); // prime |
|
110 |
|
111 // No client auth, no dialogs |
|
112 atts->iClientAuthenticate = EFalse; |
|
113 atts->iDialogNonAttendedMode = ETrue; |
|
114 |
|
115 if(UseNullCipher()) |
|
116 { |
|
117 // Enables null cipher by setting appropiate parameter |
|
118 atts->iAllowNullCipherSuites = ETrue; |
|
119 } |
|
120 |
|
121 return EPass; |
|
122 } |
|
123 |
|
124 TVerdict CServerFinishedStep::doTestStepL() |
|
125 { |
|
126 INFO_PRINTF1(_L("Calling TLS Provider to fetch cipher suites.")); |
|
127 |
|
128 // first we have to retrieve the available cipher suites |
|
129 TInt err = GetCipherSuitesL(); |
|
130 |
|
131 if (err != KErrNone) |
|
132 { |
|
133 INFO_PRINTF2(_L("Failed! Cannot retrieve supported cipher suites! (Error %d)"), |
|
134 err); |
|
135 SetTestStepResult(EFail); |
|
136 return TestStepResult(); |
|
137 } |
|
138 |
|
139 // verifies certificate if is not a PSK cipher suite |
|
140 if( !UsePsk() ) |
|
141 { |
|
142 // we have to verify the server certificate, to supply the certificate |
|
143 // and its parameters to the TLS provider. |
|
144 |
|
145 INFO_PRINTF1(_L("Calling TLS Provider to verify server certificate.")); |
|
146 |
|
147 CX509Certificate* cert = NULL; |
|
148 |
|
149 err = VerifyServerCertificateL(cert); |
|
150 delete cert; |
|
151 |
|
152 // make sure it completed sucessfully. |
|
153 if (err != KErrNone) |
|
154 { |
|
155 INFO_PRINTF2(_L("Failed! Server Certificate did not verify correctly! (Error %d)"), |
|
156 err); |
|
157 SetTestStepResult(EFail); |
|
158 return TestStepResult(); |
|
159 } |
|
160 |
|
161 } |
|
162 |
|
163 |
|
164 INFO_PRINTF1(_L("Creating TLS Session.")); |
|
165 |
|
166 // now, create a session with the parameters set in the preamble |
|
167 err = CreateSessionL(); |
|
168 |
|
169 // ensure we succeeded |
|
170 if (err != KErrNone) |
|
171 { |
|
172 INFO_PRINTF2(_L("Failed! Create Session failed! (Error %d)"), err); |
|
173 SetTestStepResult(EFail); |
|
174 return TestStepResult(); |
|
175 } |
|
176 |
|
177 INFO_PRINTF1(_L("Calling TLS session key exchange.")); |
|
178 |
|
179 HBufC8* keyExMessage = NULL; |
|
180 err = ClientKeyExchange(keyExMessage); |
|
181 |
|
182 if (err != KErrNone) |
|
183 { |
|
184 INFO_PRINTF2(_L("Failed! Key exchange failed! (Error %d)"), err); |
|
185 delete keyExMessage; |
|
186 SetTestStepResult(EFail); |
|
187 return TestStepResult(); |
|
188 } |
|
189 |
|
190 INFO_PRINTF1(_L("Deriving premaster secret.")); |
|
191 |
|
192 // derive the premaster secret from the key exchange method |
|
193 CleanupStack::PushL(keyExMessage); |
|
194 HBufC8* premaster = DerivePreMasterSecretL(*keyExMessage); |
|
195 CleanupStack::PopAndDestroy(keyExMessage); |
|
196 |
|
197 INFO_PRINTF1(_L("Deriving master secret.")); |
|
198 |
|
199 // compute the master secret from the premaster. |
|
200 CleanupStack::PushL(premaster); |
|
201 HBufC8* master = ComputeMasterSecretL(*premaster); |
|
202 CleanupStack::PopAndDestroy(premaster); |
|
203 CleanupStack::PushL(master); |
|
204 |
|
205 // do the main meat of the test |
|
206 ValidateServerFinishedL(*master); |
|
207 |
|
208 CleanupStack::PopAndDestroy(master); |
|
209 return TestStepResult(); |
|
210 } |
|
211 |
|
212 void CServerFinishedStep::ValidateServerFinishedL(const TDesC8& aMasterSecret) |
|
213 { |
|
214 // create a block of random data to represent our handshake messages, |
|
215 // and create hash objects from it. |
|
216 |
|
217 HBufC8* handshake = HBufC8::NewLC(1024); // totally arbitary length... |
|
218 TPtr8 handshakeBuf = handshake->Des(); |
|
219 handshakeBuf.SetLength(1024); |
|
220 TRandom::RandomL(handshakeBuf); |
|
221 |
|
222 CMessageDigest* handshakeSha = CMessageDigestFactory::NewDigestLC(CMessageDigest::ESHA1); |
|
223 CMessageDigest* handshakeMd = CMessageDigestFactory::NewDigestLC(CMessageDigest::EMD5); |
|
224 |
|
225 handshakeSha->Update(handshakeBuf); |
|
226 handshakeMd->Update(handshakeBuf); |
|
227 |
|
228 INFO_PRINTF1(_L("Computing our test finished message.")); |
|
229 |
|
230 // now, calculate our idea of what the finished message should be. |
|
231 HBufC8* ourFinished = ComputeFinishedMessageL(handshakeSha, handshakeMd, aMasterSecret, EFalse); |
|
232 CleanupStack::PushL(ourFinished); |
|
233 |
|
234 TInt expectedResult = KErrNone; |
|
235 TBool tamper = EFalse; |
|
236 if (GetBoolFromConfig(ConfigSection(), KTampterHandshakeMessages, tamper) && tamper) |
|
237 { |
|
238 INFO_PRINTF1(_L("Simulating man in the middle handshake tampering.")); |
|
239 |
|
240 // we want to simulate a third party tampering with our handshake |
|
241 expectedResult = KErrBadServerFinishedMsg; |
|
242 TRandom::RandomL(handshakeBuf); |
|
243 |
|
244 handshakeSha->Reset(); |
|
245 handshakeMd->Reset();; |
|
246 handshakeSha->Update(handshakeBuf); |
|
247 handshakeMd->Update(handshakeBuf); |
|
248 } |
|
249 |
|
250 INFO_PRINTF1(_L("Calling TLS Session to verify server finished message.")); |
|
251 |
|
252 // ask TLS provider to verify our finished message |
|
253 TInt err = VerifyServerFinishedL(handshakeSha, handshakeMd, *ourFinished); |
|
254 if (err != expectedResult) |
|
255 { |
|
256 INFO_PRINTF3(_L("Failed! Expecting code %d, actual code %d."), expectedResult, err); |
|
257 SetTestStepResult(EFail); |
|
258 } |
|
259 else |
|
260 { |
|
261 INFO_PRINTF1(_L("Test passed.")); |
|
262 SetTestStepResult(EPass); |
|
263 } |
|
264 CleanupStack::PopAndDestroy(4, handshake); // handshakeSha, handshakeMd, ourFinished |
|
265 } |