|
1 /* |
|
2 * Copyright (c) 2005-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 * Test program for PKCS8 Encoding |
|
16 * |
|
17 */ |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 /** |
|
24 @file |
|
25 */ |
|
26 |
|
27 #include <e32test.h> |
|
28 #include <e32std.h> |
|
29 #include <U32STD.H> |
|
30 #include <asn1enc.h> |
|
31 #include <asnpkcs.h> |
|
32 #include <pbe.h> |
|
33 #include <pbebase.h> |
|
34 #include <pbedata.h> |
|
35 #include <random.h> |
|
36 #include <asymmetrickeys.h> |
|
37 |
|
38 #include <bigint.h> |
|
39 |
|
40 #include "pkcs8tester.h" |
|
41 |
|
42 const TInt KErrEmptyKeyFile = -99990; |
|
43 |
|
44 /** |
|
45 * Extracts the nPos command line argument. |
|
46 */ |
|
47 HBufC* Pkcs8Utils::GetArgumentL(TInt nPos) |
|
48 { |
|
49 HBufC *argv = HBufC::NewLC(User::CommandLineLength()); |
|
50 TPtr cmd(argv->Des()); |
|
51 User::CommandLine(cmd); |
|
52 TLex arguments(cmd); |
|
53 |
|
54 // finds nth parameter |
|
55 while(nPos && !arguments.Eos()) |
|
56 { |
|
57 TPtrC token = arguments.NextToken(); |
|
58 if(token.Length() > 0) |
|
59 nPos--; |
|
60 } |
|
61 |
|
62 HBufC* result = NULL; |
|
63 if(!arguments.Eos()) |
|
64 { |
|
65 TPtrC testfile(arguments.NextToken()); |
|
66 |
|
67 if(testfile.Length() > 0) |
|
68 result = testfile.AllocL(); |
|
69 }; |
|
70 |
|
71 // no parameter found, but must return something so.. |
|
72 if(!result) |
|
73 result = HBufC::NewL(0); |
|
74 |
|
75 CleanupStack::PopAndDestroy(argv); |
|
76 |
|
77 return result; |
|
78 } |
|
79 |
|
80 CDecPKCS8Data* Pkcs8Utils::getPkcsDataL(TDesC& fileName) |
|
81 { |
|
82 // First import a PKCS8 private key from file |
|
83 RFs fs; |
|
84 CleanupClosePushL(fs); |
|
85 User::LeaveIfError(fs.Connect()); |
|
86 |
|
87 RFile file; |
|
88 CleanupClosePushL(file); |
|
89 TInt r = file.Open(fs, fileName, EFileRead); |
|
90 User::LeaveIfError(r); |
|
91 |
|
92 TInt fileSize = 0; |
|
93 User::LeaveIfError(file.Size(fileSize)); |
|
94 |
|
95 if (fileSize <= 0) |
|
96 { |
|
97 User::Leave(KErrEmptyKeyFile); |
|
98 } |
|
99 |
|
100 HBufC8* buf = HBufC8::NewMaxLC(fileSize); |
|
101 TPtr8 data(buf->Des()); |
|
102 data.FillZ(); |
|
103 User::LeaveIfError(file.Read(data, fileSize)); |
|
104 |
|
105 CDecPKCS8Data* decoded = TASN1DecPKCS8::DecodeDERL(buf->Des()); |
|
106 |
|
107 CleanupStack::PopAndDestroy(buf); |
|
108 CleanupStack::PopAndDestroy(&file); |
|
109 CleanupStack::PopAndDestroy(&fs); |
|
110 //CleanupStack::PopAndDestroy(fileName); |
|
111 |
|
112 return decoded; |
|
113 } |
|
114 |
|
115 void Pkcs8Utils::WriteSequenceL(const TDesC& fn, CASN1EncSequence& seq) |
|
116 { |
|
117 HBufC8* seqBufc = HBufC8::NewLC(seq.LengthDER()); |
|
118 TPtr8 seqBuf(seqBufc->Des()); |
|
119 seqBuf.SetLength(seq.LengthDER()); |
|
120 TUint pos = 0; |
|
121 |
|
122 seq.WriteDERL(seqBuf, pos); |
|
123 |
|
124 |
|
125 RFs fs2; |
|
126 CleanupClosePushL(fs2); |
|
127 User::LeaveIfError(fs2.Connect()); |
|
128 |
|
129 RFile file2; |
|
130 CleanupClosePushL(file2); |
|
131 TRAPD(err, file2.Create(fs2, fn, EFileRead)); |
|
132 if (err==KErrNotFound) |
|
133 { |
|
134 User::LeaveIfError(err); |
|
135 } |
|
136 file2.Close(); |
|
137 |
|
138 RFileWriteStream writer; |
|
139 writer.PushL(); |
|
140 |
|
141 User::LeaveIfError(writer.Open(fs2, fn, EFileWrite)); |
|
142 |
|
143 writer.WriteL(seqBuf); |
|
144 writer.Close(); |
|
145 |
|
146 CleanupStack::PopAndDestroy(4, seqBufc); |
|
147 } |
|
148 |
|
149 |
|
150 |
|
151 LOCAL_D void DoTestsDSAEncryptedL(CDecPKCS8Data& decoded) |
|
152 { |
|
153 CPKCS8KeyPairDSA* keypair = static_cast<CPKCS8KeyPairDSA*>(decoded.KeyPairData()); |
|
154 |
|
155 const CDSAPrivateKey& priv = keypair->PrivateKey(); |
|
156 |
|
157 TPBPassword pass(_L("")); |
|
158 |
|
159 CSystemRandom* rand = CSystemRandom::NewLC(); |
|
160 HBufC8* saltc = HBufC8::NewMaxLC(16); |
|
161 TPtr8 salt(saltc->Des()); |
|
162 salt.FillZ(); |
|
163 rand->GenerateBytesL(salt); |
|
164 |
|
165 HBufC8* ivc = HBufC8::NewMaxLC(8); |
|
166 |
|
167 TPtr8 iv(ivc->Des()); |
|
168 iv.FillZ(); |
|
169 //rand->GenerateBytesL(iv); |
|
170 |
|
171 CPBEncryptParms* params = CPBEncryptParms::NewLC(ECipher3DES_CBC, salt, iv, 2048); |
|
172 CPBEncryptElement* encryptElement = CPBEncryptElement::NewLC(pass, *params); |
|
173 CPBEncryptor* encryptor = encryptElement->NewEncryptLC(); |
|
174 |
|
175 CASN1EncSequence* seq = TASN1EncPKCS8::EncodeEncryptedL(priv, *encryptor, *params); |
|
176 |
|
177 CleanupStack::PopAndDestroy(encryptor); |
|
178 CleanupStack::PopAndDestroy(encryptElement); |
|
179 CleanupStack::PopAndDestroy(params); |
|
180 CleanupStack::PopAndDestroy(ivc); |
|
181 CleanupStack::PopAndDestroy(saltc); |
|
182 CleanupStack::PopAndDestroy(rand); |
|
183 |
|
184 CleanupStack::PushL(seq); |
|
185 _LIT(fn, "ExportedDSA_Encrypted.txt"); |
|
186 Pkcs8Utils::WriteSequenceL(fn, *seq); |
|
187 CleanupStack::PopAndDestroy(seq); |
|
188 } |
|
189 |
|
190 |
|
191 LOCAL_D void DoTestsDSAL(CDecPKCS8Data& decoded) |
|
192 { |
|
193 CPKCS8KeyPairDSA* keypair = static_cast<CPKCS8KeyPairDSA*>(decoded.KeyPairData()); |
|
194 |
|
195 const CDSAPrivateKey& priv = keypair->PrivateKey(); |
|
196 |
|
197 // Now Export it |
|
198 CASN1EncSequence* seq = TASN1EncPKCS8::EncodeL(priv); |
|
199 |
|
200 CleanupStack::PushL(seq); |
|
201 Pkcs8Utils::WriteSequenceL(_L("ExportedDSA.txt"), *seq); |
|
202 CleanupStack::PopAndDestroy(seq); |
|
203 } |
|
204 |
|
205 LOCAL_D void DoTestsRSAL(CDecPKCS8Data& decoded) |
|
206 { |
|
207 CPKCS8KeyPairRSA* keypair = static_cast<CPKCS8KeyPairRSA*>(decoded.KeyPairData()); |
|
208 |
|
209 const CRSAPrivateKeyCRT& priv = static_cast<const CRSAPrivateKeyCRT&>(keypair->PrivateKey()); |
|
210 |
|
211 const CRSAPublicKey& pub = keypair->PublicKey(); |
|
212 |
|
213 // Now Export it |
|
214 CASN1EncSequence* seq = TASN1EncPKCS8::EncodeL(priv, pub); |
|
215 |
|
216 CleanupStack::PushL(seq); |
|
217 Pkcs8Utils::WriteSequenceL(_L("ExportedRSA.txt"), *seq); |
|
218 CleanupStack::PopAndDestroy(seq); |
|
219 } |
|
220 |
|
221 |
|
222 LOCAL_D void DoTestsRSAEncryptedL(CDecPKCS8Data& decoded) |
|
223 { |
|
224 CPKCS8KeyPairRSA* keypair = static_cast<CPKCS8KeyPairRSA*>(decoded.KeyPairData()); |
|
225 |
|
226 const CRSAPrivateKeyCRT& priv = static_cast<const CRSAPrivateKeyCRT&>(keypair->PrivateKey()); |
|
227 const CRSAPublicKey& pub = keypair->PublicKey(); |
|
228 |
|
229 TPBPassword pass(_L("")); |
|
230 |
|
231 CSystemRandom* rand = CSystemRandom::NewLC(); |
|
232 HBufC8* saltc = HBufC8::NewMaxLC(16); |
|
233 TPtr8 salt(saltc->Des()); |
|
234 salt.FillZ(); |
|
235 rand->GenerateBytesL(salt); |
|
236 |
|
237 HBufC8* ivc = HBufC8::NewMaxLC(8); |
|
238 |
|
239 TPtr8 iv(ivc->Des()); |
|
240 iv.FillZ(); |
|
241 rand->GenerateBytesL(iv); |
|
242 |
|
243 CPBEncryptParms* params = CPBEncryptParms::NewLC(ECipher3DES_CBC, salt, iv, 2048); |
|
244 CPBEncryptElement* encryptElement = CPBEncryptElement::NewLC(pass, *params); |
|
245 CPBEncryptor* encryptor = encryptElement->NewEncryptLC(); |
|
246 |
|
247 CASN1EncSequence* seq = TASN1EncPKCS8::EncodeEncryptedL(priv, pub, *encryptor, *params); |
|
248 |
|
249 CleanupStack::PopAndDestroy(encryptor); |
|
250 CleanupStack::PopAndDestroy(encryptElement); |
|
251 CleanupStack::PopAndDestroy(params); |
|
252 CleanupStack::PopAndDestroy(ivc); |
|
253 CleanupStack::PopAndDestroy(saltc); |
|
254 CleanupStack::PopAndDestroy(rand); |
|
255 |
|
256 CleanupStack::PushL(seq); |
|
257 _LIT(fn, "ExportedRSA_Encrypted.txt"); |
|
258 Pkcs8Utils::WriteSequenceL(fn, *seq); |
|
259 CleanupStack::PopAndDestroy(seq); |
|
260 } |
|
261 |
|
262 |
|
263 /** |
|
264 * 1st parameter dsa cleartext key |
|
265 * 2nd parameter dsa encrypted key |
|
266 * 3rd parameter rsa cleartext key |
|
267 * 4th parameter rsa encrypted key |
|
268 */ |
|
269 LOCAL_D void DoTestsL() |
|
270 { |
|
271 HBufC* fileName = Pkcs8Utils::GetArgumentL(0); |
|
272 CleanupStack::PushL(fileName); |
|
273 |
|
274 CDecPKCS8Data* decoded = Pkcs8Utils::getPkcsDataL(*fileName); |
|
275 CleanupStack::PopAndDestroy(fileName); |
|
276 |
|
277 CleanupStack::PushL(decoded); |
|
278 DoTestsDSAL(*decoded); |
|
279 |
|
280 DoTestsDSAEncryptedL(*decoded); |
|
281 CleanupStack::PopAndDestroy(decoded); |
|
282 |
|
283 fileName = Pkcs8Utils::GetArgumentL(1); |
|
284 CleanupStack::PushL(fileName); |
|
285 |
|
286 decoded = Pkcs8Utils::getPkcsDataL(*fileName); |
|
287 CleanupStack::PopAndDestroy(fileName); |
|
288 |
|
289 CleanupStack::PushL(decoded); |
|
290 DoTestsRSAL(*decoded); |
|
291 |
|
292 DoTestsRSAEncryptedL(*decoded); |
|
293 CleanupStack::PopAndDestroy(decoded); |
|
294 } |
|
295 |
|
296 LOCAL_C void ProcessL() |
|
297 { |
|
298 HBufC* arg = Pkcs8Utils::GetArgumentL(0); |
|
299 _LIT(attr, "-attribute"); |
|
300 if (arg->Find(attr)!=KErrNotFound) |
|
301 { |
|
302 TRAPD(err, GenerateAndSaveL(*Pkcs8Utils::GetArgumentL(1), *Pkcs8Utils::GetArgumentL(2))); |
|
303 } |
|
304 else |
|
305 { |
|
306 TRAPD(err, DoTestsL()); |
|
307 } |
|
308 delete arg; |
|
309 } |
|
310 |
|
311 |
|
312 |
|
313 /** |
|
314 * Command line parameters are |
|
315 * [0] -attribute [1] pkcs8 original file name [2] output file name |
|
316 * or |
|
317 * ... |
|
318 */ |
|
319 GLDEF_C TInt E32Main() // main function called by E32 |
|
320 { |
|
321 __UHEAP_MARK; |
|
322 CTrapCleanup* cleanup=CTrapCleanup::New(); // get clean-up stack |
|
323 |
|
324 TRAPD(err, ProcessL()); |
|
325 |
|
326 delete cleanup; // destroy clean-up stack |
|
327 __UHEAP_MARKEND; |
|
328 return 0; // and return |
|
329 } |