|
1 /* |
|
2 * Copyright (c) 2008 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 "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: Provides functionality for verifying the server certificate |
|
15 * |
|
16 */ |
|
17 |
|
18 |
|
19 #include "nativecertificatemanager.h" |
|
20 |
|
21 #include <x509keys.h> |
|
22 #include <asn1dec.h> |
|
23 #include <x509cert.h> |
|
24 #include <unifiedcertstore.h> |
|
25 #include <unifiedkeystore.h> |
|
26 #include <mctwritablecertstore.h> |
|
27 #include <charconv.h> |
|
28 #include <pkcs10.h> |
|
29 #include <secdlg.h> |
|
30 #include <PKIDlg.h> |
|
31 #include <securityerr.h> |
|
32 #include <pkixcertchain.h> |
|
33 #include "logger.h" |
|
34 |
|
35 #ifndef SYMBIAN_ENABLE_SPLIT_HEADERS |
|
36 #include <ssl.h> |
|
37 #else |
|
38 #include <ssl_internal.h> |
|
39 #endif |
|
40 |
|
41 using namespace java::util; |
|
42 //using namespace java::certificatemanagement; |
|
43 |
|
44 // CONSTANTS |
|
45 //_LIT( KSTSSymbianKeyStoreLabel, "Software key store" ); |
|
46 //_LIT(KSTSSymbianCertificateStoreLabel, "Software certificate store"); |
|
47 //_LIT(KSTSSymbianTrustCertStoreLabel, "trust server certstore"); |
|
48 |
|
49 NativeCertificateManager* NativeCertificateManager::NewL(X509* aCert) |
|
50 { |
|
51 JELOG2(ESOCKET); |
|
52 NativeCertificateManager* self = new(ELeave) NativeCertificateManager; |
|
53 CallMethod(self, &NativeCertificateManager::AddToScheduler, self); |
|
54 CallMethodL(self, &NativeCertificateManager::ConstructL, aCert, self); |
|
55 return self; |
|
56 } |
|
57 |
|
58 void NativeCertificateManager::AddToScheduler() |
|
59 { |
|
60 CActiveScheduler::Add(this); |
|
61 } |
|
62 |
|
63 void NativeCertificateManager::doClose() |
|
64 { |
|
65 iFileServer.Close(); |
|
66 } |
|
67 |
|
68 // Destructor |
|
69 NativeCertificateManager::~NativeCertificateManager() |
|
70 { |
|
71 CallMethod(this, &NativeCertificateManager::doClose, this); |
|
72 //stopServer(); |
|
73 |
|
74 } |
|
75 |
|
76 int NativeCertificateManager::doValidationL() |
|
77 { |
|
78 JELOG2(ESOCKET); |
|
79 unsigned char *der, *data; |
|
80 |
|
81 int len = i2d_X509(iCert, NULL); |
|
82 der = (unsigned char *) OPENSSL_malloc(len); |
|
83 if (der == NULL) |
|
84 User::Leave(-1); |
|
85 data = der; |
|
86 int ret = i2d_X509(iCert, &data); |
|
87 HBufC8* certData = HBufC8::NewL(len); |
|
88 TPtr8 tmpPtr(certData->Des()); // pointer to hbuf |
|
89 tmpPtr.Copy(der, ret); // copy the der format data to hbuf |
|
90 |
|
91 TTime ValidationTime; |
|
92 ValidationTime.UniversalTime(); |
|
93 CPKIXValidationResult *certVerificationResult = |
|
94 CPKIXValidationResult::NewL(); |
|
95 CPKIXCertChain *serverCertsChain = CPKIXCertChain::NewL(iFileServer, |
|
96 certData->Des(), TUid::Uid(KUidUnicodeSSLProtocolModule)); |
|
97 serverCertsChain->ValidateL(*certVerificationResult, ValidationTime, |
|
98 iStatus); |
|
99 |
|
100 OPENSSL_free(der); |
|
101 delete certData; |
|
102 return 0; |
|
103 } |
|
104 |
|
105 // NativeCertificateManager::RunL |
|
106 // CActive callback |
|
107 // ----------------------------------------------------------------------------- |
|
108 // |
|
109 void NativeCertificateManager::RunL() |
|
110 { |
|
111 ILOG2(ESOCKET, "++NativeCertificateManager::RunL iState %d, iStatus = %d", iState, iStatus.Int()); |
|
112 if (iStatus != KErrNone) |
|
113 { |
|
114 // Error has occured; inform java |
|
115 Complete(iStatus.Int()); |
|
116 } |
|
117 else |
|
118 { |
|
119 switch (iState) |
|
120 { |
|
121 case EValidating: |
|
122 Complete(KErrNone); |
|
123 break; |
|
124 default: |
|
125 { |
|
126 Complete(KErrGeneral); |
|
127 } |
|
128 } |
|
129 } |
|
130 } |
|
131 |
|
132 // ----------------------------------------------------------------------------- |
|
133 // NativeCertificateManager::RunError |
|
134 // CActive error callback |
|
135 // ----------------------------------------------------------------------------- |
|
136 // |
|
137 TInt NativeCertificateManager::RunError(TInt /*aError*/) |
|
138 { |
|
139 // Complete( aError ); |
|
140 return KErrNone; |
|
141 } |
|
142 |
|
143 // ----------------------------------------------------------------------------- |
|
144 // NativeCertificateManager::NativeCertificateManager |
|
145 // C++ default constructor can NOT contain any code, that |
|
146 // might leave. |
|
147 // ----------------------------------------------------------------------------- |
|
148 // |
|
149 NativeCertificateManager::NativeCertificateManager() : |
|
150 CActive(EPriorityStandard), java::util::FunctionServer("Myserver"), iState( |
|
151 EValidating), iCertVerErrCode(0) |
|
152 { |
|
153 createServerToNewThread(); |
|
154 } |
|
155 |
|
156 void NativeCertificateManager::DoCancel() |
|
157 { |
|
158 |
|
159 } |
|
160 |
|
161 int NativeCertificateManager::doValidateX509Certificate() |
|
162 { |
|
163 TRAPD(err, doValidationL()); |
|
164 if (err < KErrNone) |
|
165 { |
|
166 iCertVerErrCode = err; |
|
167 ELOG1(ESOCKET, "Error in X509 certificate validation: %d", err); |
|
168 } |
|
169 else |
|
170 { |
|
171 WaitForCompletion(); |
|
172 |
|
173 } |
|
174 return iCertVerErrCode; |
|
175 } |
|
176 |
|
177 int NativeCertificateManager::validation() |
|
178 { |
|
179 int ret = 0; |
|
180 CallMethod(ret, this, &NativeCertificateManager::doValidateX509Certificate, |
|
181 this); |
|
182 return ret; |
|
183 |
|
184 } |
|
185 |
|
186 int NativeCertificateManager::validateX509Certificate(X509 *aCert) |
|
187 { |
|
188 JELOG2(ESOCKET); |
|
189 int ret = 0; |
|
190 NativeCertificateManager *obj = NULL; |
|
191 TRAPD(err, obj = NativeCertificateManager::NewL(aCert)); |
|
192 if (err < 0) |
|
193 { |
|
194 return err; |
|
195 } |
|
196 ret = obj->validation(); |
|
197 // obj->Dispose(); |
|
198 delete obj; |
|
199 return ret; |
|
200 |
|
201 } |
|
202 |
|
203 // ----------------------------------------------------------------------------- |
|
204 // NativeCertificateManager::ConstructL |
|
205 // Symbian 2nd phase constructor can leave. |
|
206 // ----------------------------------------------------------------------------- |
|
207 // |
|
208 void NativeCertificateManager::ConstructL(X509* aCert) |
|
209 { |
|
210 iCert = aCert; |
|
211 User::LeaveIfError(iFileServer.Connect()); |
|
212 iWait = new(ELeave) CActiveSchedulerWait; |
|
213 iState = EValidating; |
|
214 } |
|
215 |
|
216 // ----------------------------------------------------------------------------- |
|
217 // NativeCertificateManager::Complete |
|
218 // Completes the operation |
|
219 // ----------------------------------------------------------------------------- |
|
220 // |
|
221 void NativeCertificateManager::Complete(TInt aError) |
|
222 { |
|
223 JELOG2(ESOCKET); |
|
224 if (KErrNone == aError) |
|
225 { |
|
226 iCertVerErrCode = KErrNone; |
|
227 } |
|
228 else |
|
229 { |
|
230 iCertVerErrCode = aError; |
|
231 } |
|
232 iWait->AsyncStop(); |
|
233 } |
|
234 |
|
235 // ----------------------------------------------------------------------------- |
|
236 // NativeCertificateManager::WaitForCompletion() |
|
237 // Wait for completion, leave on error |
|
238 // ----------------------------------------------------------------------------- |
|
239 // |
|
240 void NativeCertificateManager::WaitForCompletion() |
|
241 { |
|
242 SetActive(); |
|
243 iWait->Start(); // will return at the end of complete |
|
244 } |
|
245 |
|
246 void NativeCertificateManager::vmAttached() |
|
247 { |
|
248 |
|
249 } |
|
250 |
|
251 void NativeCertificateManager::doServerSideInit() |
|
252 { |
|
253 |
|
254 FunctionServer::doServerSideInit(); |
|
255 |
|
256 } |