|
1 // Copyright (c) 2005-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 // ipsecpolparser.cpp - IPSec policy parser main module |
|
15 // |
|
16 |
|
17 #include <e32std.h> |
|
18 |
|
19 #include "ipsecpolparser.h" |
|
20 |
|
21 // Policies Parsing |
|
22 |
|
23 // Symbian change - start |
|
24 #ifdef __VC32__ |
|
25 #pragma warning(disable : 4097) // typedef-name used as synonym for class-name |
|
26 #endif |
|
27 // Symbian change - end |
|
28 |
|
29 EXPORT_C |
|
30 TPolicyParser::TPolicyParser(const TDesC &aPolicy) : TLex(aPolicy) |
|
31 {} |
|
32 |
|
33 EXPORT_C TInt |
|
34 TPolicyParser::ParseL(CIpSecurityPiece* aPieceData) |
|
35 { |
|
36 TInt err(KErrNone); |
|
37 iLine = 1; |
|
38 |
|
39 CSecurityPolicy* sp = aPieceData->Policies(); |
|
40 while (!err && NextToken() == token_string) |
|
41 { |
|
42 if (iToken.Compare(_L("sa")) == 0) |
|
43 { |
|
44 err = parse_sa_specL(sp); |
|
45 } |
|
46 else if (iToken.Compare(_L("ep")) == 0) |
|
47 { |
|
48 err = parse_ep_specL(sp); |
|
49 } |
|
50 else |
|
51 { |
|
52 err = parse_conn2saL(sp); |
|
53 } |
|
54 } |
|
55 |
|
56 if (!err && !Eos()) |
|
57 { |
|
58 // Parsing didn't detect error, but not all parsed! |
|
59 err = KErrGeneral; |
|
60 } |
|
61 |
|
62 if (err) |
|
63 { |
|
64 if (iMsg.Length() > 0 && iMsg.Length() < 200) |
|
65 { |
|
66 aPieceData->iErrorInfo.Copy(iMsg); |
|
67 } |
|
68 } |
|
69 |
|
70 return (err); |
|
71 } |
|
72 |
|
73 EXPORT_C TInt |
|
74 TPolicyParser::BufferAppend(HBufC8*& aPolBfr, const TDesC8& aText) |
|
75 { |
|
76 TInt err(KErrNone); |
|
77 |
|
78 // Make sure that we have enough space for the new text |
|
79 TInt spaceLeft = aPolBfr->Des().MaxLength() - aPolBfr->Des().Length(); |
|
80 if (aText.Length() > spaceLeft) |
|
81 { |
|
82 // Allocate enough space for the new text + some additional |
|
83 // free space so that allocations are not too frequent |
|
84 TInt newMaxLength = aPolBfr->Des().MaxLength() |
|
85 + aText.Length() |
|
86 + KPolicyBufferSizeIncrement; |
|
87 HBufC8* tempBfr = aPolBfr->ReAlloc(newMaxLength); |
|
88 if (tempBfr != NULL) |
|
89 { |
|
90 aPolBfr = tempBfr; |
|
91 } |
|
92 else |
|
93 { |
|
94 return KErrNoMemory; |
|
95 } |
|
96 } |
|
97 |
|
98 aPolBfr->Des().Append(aText); |
|
99 return err; |
|
100 } |
|
101 |
|
102 EXPORT_C TInt |
|
103 TPolicyParser::Write(CSecurityPolicy *aSp, |
|
104 HBufC8*& aPolBfr, |
|
105 TBool aSortingOrder) |
|
106 { |
|
107 TInt err = WriteSAs(aSp->SAList(), aPolBfr); |
|
108 if (err) |
|
109 { |
|
110 return err; |
|
111 } |
|
112 |
|
113 if (aSortingOrder) |
|
114 { |
|
115 err = WriteSelectorsInSortingOrder(aSp->SelectorList(), |
|
116 aPolBfr, |
|
117 aSortingOrder); |
|
118 } |
|
119 else |
|
120 { |
|
121 err = WriteSelectors(aSp->SelectorList(), aPolBfr, aSortingOrder); |
|
122 } |
|
123 |
|
124 return err; |
|
125 } |
|
126 |
|
127 TInt |
|
128 TPolicyParser::WriteSAs(CSAList* aSAList, HBufC8*& aPolBfr) |
|
129 { |
|
130 TBuf8<1024> aux; |
|
131 TInt err(KErrNone); |
|
132 TInt count = aSAList->Count(); |
|
133 for (TInt i = 0; i < count ; i++) |
|
134 { |
|
135 TextSA(aSAList->At(i), aux); |
|
136 err = BufferAppend(aPolBfr, aux); |
|
137 if (err != KErrNone) |
|
138 { |
|
139 return err; |
|
140 } |
|
141 } |
|
142 return KErrNone; |
|
143 } |
|
144 #ifdef SYMBIAN_IPSEC_VOIP_SUPPORT |
|
145 void |
|
146 TPolicyParser::TextSA(CPolicySpec* aSA, TDes8& aBuf) |
|
147 { |
|
148 if (aSA->iSpectype == EPolSpecSA) |
|
149 { |
|
150 aBuf.Format(_L8("sa ")); |
|
151 // SA name |
|
152 aBuf.Append(aSA->iName->Des()); |
|
153 aBuf.Append(_L8(" = {\n")); |
|
154 TInt maxcount = aSA->iPropList->Count(); |
|
155 |
|
156 |
|
157 for(TInt i =0;i<maxcount;i++) |
|
158 { |
|
159 |
|
160 aBuf.Append(_L8("proposal\n{\n")); |
|
161 |
|
162 switch (aSA->iPropList->At(i)->iType) |
|
163 { |
|
164 case SADB_SATYPE_AH: |
|
165 aBuf.Append(_L8(" ah\n")); |
|
166 break; |
|
167 case SADB_SATYPE_ESP: |
|
168 aBuf.Append(_L8(" esp\n")); |
|
169 break; |
|
170 default: //SADB_SATYPE_UNSPEC |
|
171 // aBuf.Append(_L8(" ???")); //Shouldn't happen |
|
172 //Now It can happen to indicate null policy ignore it. |
|
173 continue; // skip this proposal. |
|
174 |
|
175 } |
|
176 |
|
177 // Encryption Algorithm |
|
178 |
|
179 |
|
180 |
|
181 if (aSA->iPropList->At(i)->iEalg != 0) |
|
182 { |
|
183 // Encryption Alg |
|
184 aBuf.AppendFormat(_L8(" encrypt_alg %d\n"), aSA->iPropList->At(i)->iEalg); |
|
185 } |
|
186 |
|
187 if (aSA->iPropList->At(i)->iEalgLen != 0) |
|
188 { |
|
189 aBuf.AppendFormat(_L8(" max_encrypt_bits %d\n"), aSA->iPropList->At(i)->iEalgLen); |
|
190 } |
|
191 |
|
192 // Authentication Algorithm |
|
193 if (aSA->iPropList->At(i)->iAalg != 0) |
|
194 { |
|
195 aBuf.AppendFormat(_L8(" auth_alg %d\n"), aSA->iPropList->At(i)->iAalg); |
|
196 } |
|
197 |
|
198 if (aSA->iPropList->At(i)->iAalgLen != 0) |
|
199 { |
|
200 aBuf.AppendFormat(_L8(" max_auth_bits %d\n"), aSA->iPropList->At(i)->iAalgLen); |
|
201 } |
|
202 if (aSA->iPropList->At(i)->iHard.sadb_lifetime_allocations != 0) |
|
203 { |
|
204 aBuf.AppendFormat(_L8(" hard_lifetime_allocations %d\n"), |
|
205 aSA->iPropList->At(i)->iHard.sadb_lifetime_allocations); |
|
206 } |
|
207 |
|
208 if (aSA->iPropList->At(i)->iHard.sadb_lifetime_bytes != 0) |
|
209 { |
|
210 aBuf.AppendFormat(_L8(" hard_lifetime_bytes %d\n"), |
|
211 aSA->iPropList->At(i)->iHard.sadb_lifetime_bytes); |
|
212 } |
|
213 |
|
214 if (aSA->iPropList->At(i)->iHard.sadb_lifetime_addtime != 0) |
|
215 { |
|
216 aBuf.AppendFormat(_L8(" hard_lifetime_addtime %d\n"), |
|
217 aSA->iPropList->At(i)->iHard.sadb_lifetime_addtime); |
|
218 } |
|
219 |
|
220 if (aSA->iPropList->At(i)->iHard.sadb_lifetime_usetime != 0) |
|
221 { |
|
222 aBuf.AppendFormat(_L8(" hard_lifetime_usetime %d\n"), |
|
223 aSA->iPropList->At(i)->iHard.sadb_lifetime_usetime); |
|
224 } |
|
225 |
|
226 if (aSA->iPropList->At(i)->iSoft.sadb_lifetime_allocations != 0) |
|
227 { |
|
228 aBuf.AppendFormat(_L8(" soft_lifetime_allocations %d\n"), |
|
229 aSA->iPropList->At(i)->iSoft.sadb_lifetime_allocations); |
|
230 } |
|
231 |
|
232 if (aSA->iPropList->At(i)->iSoft.sadb_lifetime_bytes != 0) |
|
233 { |
|
234 aBuf.AppendFormat(_L8(" soft_lifetime_bytes %d\n"), |
|
235 aSA->iPropList->At(i)->iSoft.sadb_lifetime_bytes); |
|
236 } |
|
237 |
|
238 if (aSA->iPropList->At(i)->iSoft.sadb_lifetime_addtime != 0) |
|
239 { |
|
240 aBuf.AppendFormat(_L8(" soft_lifetime_addtime %d\n"), |
|
241 aSA->iPropList->At(i)->iSoft.sadb_lifetime_addtime); |
|
242 } |
|
243 |
|
244 if (aSA->iPropList->At(i)->iSoft.sadb_lifetime_usetime != 0) |
|
245 { |
|
246 aBuf.AppendFormat(_L8(" soft_lifetime_usetime %d\n"), |
|
247 aSA->iPropList->At(i)->iSoft.sadb_lifetime_usetime); |
|
248 } |
|
249 |
|
250 aBuf.AppendFormat(_L8("\n}\n"));// End of this Proposal |
|
251 |
|
252 } |
|
253 |
|
254 if (aSA->iSpec.iPfs != 0) |
|
255 { |
|
256 aBuf.Append(_L8(" pfs\n")); |
|
257 } |
|
258 |
|
259 if (aSA->iRemoteIdentity != NULL) |
|
260 { |
|
261 aBuf.Append(_L8(" identity_remote ")); |
|
262 aBuf.Append(aSA->iRemoteIdentity->Des()); |
|
263 aBuf.Append('\n'); |
|
264 } |
|
265 |
|
266 if (aSA->iLocalIdentity != NULL) |
|
267 { |
|
268 aBuf.Append(_L8(" identity_local ")); |
|
269 aBuf.Append(aSA->iLocalIdentity->Des()); |
|
270 aBuf.Append('\n'); |
|
271 } |
|
272 |
|
273 if (aSA->iSpec.iReplayWindowLength != 0) |
|
274 { |
|
275 aBuf.AppendFormat(_L8(" replay_win_len %d\n"), |
|
276 aSA->iSpec.iReplayWindowLength); |
|
277 } |
|
278 |
|
279 if (aSA->iSpec.iMatchProtocol != 0) |
|
280 { |
|
281 aBuf.Append(_L8(" protocol_specific\n")); |
|
282 } |
|
283 |
|
284 if (aSA->iSpec.iMatchLocalPort != 0) |
|
285 { |
|
286 aBuf.Append(_L8(" local_port_specific\n")); |
|
287 } |
|
288 |
|
289 if (aSA->iSpec.iMatchRemotePort != 0) |
|
290 { |
|
291 aBuf.Append(_L8(" remote_port_specific\n")); |
|
292 } |
|
293 |
|
294 if (aSA->iSpec.iMatchProxy != 0) |
|
295 { |
|
296 aBuf.Append(_L8(" proxy_specific\n")); |
|
297 } |
|
298 |
|
299 if (aSA->iSpec.iMatchSrc != 0) |
|
300 { |
|
301 aBuf.Append(_L8(" src_specific\n")); |
|
302 } |
|
303 |
|
304 if (aSA->iSpec.iMatchLocal != 0) |
|
305 { |
|
306 aBuf.Append(_L8(" local_specific\n")); |
|
307 } |
|
308 |
|
309 if (aSA->iSpec.iMatchRemote != 0) |
|
310 { |
|
311 aBuf.Append(_L8(" remote_specific\n")); |
|
312 } |
|
313 |
|
314 aBuf.AppendFormat(_L8(" }\n\n")); |
|
315 } |
|
316 else |
|
317 { |
|
318 TBuf<39> addr; |
|
319 aBuf.Format(_L8("ep ")); |
|
320 |
|
321 // EndPoint name |
|
322 aBuf.Append(aSA->iName->Des()); |
|
323 aBuf.Append(_L8(" = {")); |
|
324 |
|
325 if (aSA->iEpSpec.iIsOptional) |
|
326 { |
|
327 aBuf.Append(_L8(" ? ")); |
|
328 } |
|
329 aSA->iEpSpec.iEpAddr.OutputWithScope(addr); |
|
330 aBuf.Append(addr); |
|
331 aBuf.Append(_L8(" }\n\n")); |
|
332 } |
|
333 } |
|
334 #else |
|
335 void |
|
336 TPolicyParser::TextSA(CPolicySpec* aSA, TDes8& aBuf) |
|
337 { |
|
338 if (aSA->iSpectype == EPolSpecSA) |
|
339 { |
|
340 aBuf.Format(_L8("sa ")); |
|
341 // SA name |
|
342 aBuf.Append(aSA->iName->Des()); |
|
343 aBuf.Append(_L8(" = {\n")); |
|
344 switch (aSA->iSpec.iType) |
|
345 { |
|
346 case SADB_SATYPE_AH: |
|
347 aBuf.Append(_L8(" ah\n")); |
|
348 break; |
|
349 case SADB_SATYPE_ESP: |
|
350 aBuf.Append(_L8(" esp\n")); |
|
351 break; |
|
352 default: //SADB_SATYPE_UNSPEC |
|
353 aBuf.Append(_L8(" ???")); //Shouldn't happen |
|
354 } |
|
355 |
|
356 // Encryption Algorithm |
|
357 |
|
358 |
|
359 |
|
360 if (aSA->iSpec.iEalg != 0) |
|
361 { |
|
362 // Encryption Alg |
|
363 aBuf.AppendFormat(_L8(" encrypt_alg %d\n"), aSA->iSpec.iEalg); |
|
364 } |
|
365 |
|
366 if (aSA->iSpec.iEalgLen != 0) |
|
367 { |
|
368 aBuf.AppendFormat(_L8(" max_encrypt_bits %d\n"), aSA->iSpec.iEalgLen); |
|
369 } |
|
370 |
|
371 // Authentication Algorithm |
|
372 if (aSA->iSpec.iAalg != 0) |
|
373 { |
|
374 aBuf.AppendFormat(_L8(" auth_alg %d\n"), aSA->iSpec.iAalg); |
|
375 } |
|
376 |
|
377 if (aSA->iSpec.iAalgLen != 0) |
|
378 { |
|
379 aBuf.AppendFormat(_L8(" max_auth_bits %d\n"), aSA->iSpec.iAalgLen); |
|
380 } |
|
381 |
|
382 if (aSA->iSpec.iPfs != 0) |
|
383 { |
|
384 aBuf.Append(_L8(" pfs\n")); |
|
385 } |
|
386 |
|
387 if (aSA->iRemoteIdentity != NULL) |
|
388 { |
|
389 aBuf.Append(_L8(" identity_remote ")); |
|
390 aBuf.Append(aSA->iRemoteIdentity->Des()); |
|
391 aBuf.Append('\n'); |
|
392 } |
|
393 |
|
394 if (aSA->iLocalIdentity != NULL) |
|
395 { |
|
396 aBuf.Append(_L8(" identity_local ")); |
|
397 aBuf.Append(aSA->iLocalIdentity->Des()); |
|
398 aBuf.Append('\n'); |
|
399 } |
|
400 |
|
401 if (aSA->iSpec.iReplayWindowLength != 0) |
|
402 { |
|
403 aBuf.AppendFormat(_L8(" replay_win_len %d\n"), |
|
404 aSA->iSpec.iReplayWindowLength); |
|
405 } |
|
406 |
|
407 if (aSA->iSpec.iMatchProtocol != 0) |
|
408 { |
|
409 aBuf.Append(_L8(" protocol_specific\n")); |
|
410 } |
|
411 |
|
412 if (aSA->iSpec.iMatchLocalPort != 0) |
|
413 { |
|
414 aBuf.Append(_L8(" local_port_specific\n")); |
|
415 } |
|
416 |
|
417 if (aSA->iSpec.iMatchRemotePort != 0) |
|
418 { |
|
419 aBuf.Append(_L8(" remote_port_specific\n")); |
|
420 } |
|
421 |
|
422 if (aSA->iSpec.iMatchProxy != 0) |
|
423 { |
|
424 aBuf.Append(_L8(" proxy_specific\n")); |
|
425 } |
|
426 |
|
427 if (aSA->iSpec.iMatchSrc != 0) |
|
428 { |
|
429 aBuf.Append(_L8(" src_specific\n")); |
|
430 } |
|
431 |
|
432 if (aSA->iSpec.iMatchLocal != 0) |
|
433 { |
|
434 aBuf.Append(_L8(" local_specific\n")); |
|
435 } |
|
436 |
|
437 if (aSA->iSpec.iMatchRemote != 0) |
|
438 { |
|
439 aBuf.Append(_L8(" remote_specific\n")); |
|
440 } |
|
441 |
|
442 if (aSA->iSpec.iHard.sadb_lifetime_allocations != 0) |
|
443 { |
|
444 aBuf.AppendFormat(_L8(" hard_lifetime_allocations %d\n"), |
|
445 aSA->iSpec.iHard.sadb_lifetime_allocations); |
|
446 } |
|
447 |
|
448 if (aSA->iSpec.iHard.sadb_lifetime_bytes != 0) |
|
449 { |
|
450 aBuf.AppendFormat(_L8(" hard_lifetime_bytes %d\n"), |
|
451 aSA->iSpec.iHard.sadb_lifetime_bytes); |
|
452 } |
|
453 |
|
454 if (aSA->iSpec.iHard.sadb_lifetime_addtime != 0) |
|
455 { |
|
456 aBuf.AppendFormat(_L8(" hard_lifetime_addtime %d\n"), |
|
457 aSA->iSpec.iHard.sadb_lifetime_addtime); |
|
458 } |
|
459 |
|
460 if (aSA->iSpec.iHard.sadb_lifetime_usetime != 0) |
|
461 { |
|
462 aBuf.AppendFormat(_L8(" hard_lifetime_usetime %d\n"), |
|
463 aSA->iSpec.iHard.sadb_lifetime_usetime); |
|
464 } |
|
465 |
|
466 if (aSA->iSpec.iSoft.sadb_lifetime_allocations != 0) |
|
467 { |
|
468 aBuf.AppendFormat(_L8(" soft_lifetime_allocations %d\n"), |
|
469 aSA->iSpec.iSoft.sadb_lifetime_allocations); |
|
470 } |
|
471 |
|
472 if (aSA->iSpec.iSoft.sadb_lifetime_bytes != 0) |
|
473 { |
|
474 aBuf.AppendFormat(_L8(" soft_lifetime_bytes %d\n"), |
|
475 aSA->iSpec.iSoft.sadb_lifetime_bytes); |
|
476 } |
|
477 |
|
478 if (aSA->iSpec.iSoft.sadb_lifetime_addtime != 0) |
|
479 { |
|
480 aBuf.AppendFormat(_L8(" soft_lifetime_addtime %d\n"), |
|
481 aSA->iSpec.iSoft.sadb_lifetime_addtime); |
|
482 } |
|
483 |
|
484 if (aSA->iSpec.iSoft.sadb_lifetime_usetime != 0) |
|
485 { |
|
486 aBuf.AppendFormat(_L8(" soft_lifetime_usetime %d\n"), |
|
487 aSA->iSpec.iSoft.sadb_lifetime_usetime); |
|
488 } |
|
489 aBuf.AppendFormat(_L8(" }\n\n")); |
|
490 } |
|
491 else |
|
492 { |
|
493 TBuf<39> addr; |
|
494 aBuf.Format(_L8("ep ")); |
|
495 |
|
496 // EndPoint name |
|
497 aBuf.Append(aSA->iName->Des()); |
|
498 aBuf.Append(_L8(" = {")); |
|
499 |
|
500 if (aSA->iEpSpec.iIsOptional) |
|
501 { |
|
502 aBuf.Append(_L8(" ? ")); |
|
503 } |
|
504 aSA->iEpSpec.iEpAddr.OutputWithScope(addr); |
|
505 aBuf.Append(addr); |
|
506 aBuf.Append(_L8(" }\n\n")); |
|
507 } |
|
508 } |
|
509 |
|
510 #endif |
|
511 TInt |
|
512 TPolicyParser::WriteSelectors(CSelectorList* aSelList, |
|
513 HBufC8*& aPolBfr, |
|
514 TBool /* aSortingOrder */) |
|
515 { |
|
516 TBuf8<1024> aux; |
|
517 TInt err(KErrNone); |
|
518 TInt count(aSelList->Count()); |
|
519 for (TInt i = 0; i < count; i++) |
|
520 { |
|
521 aux.Zero(); |
|
522 CPolicySelector* ps = aSelList->At(i); |
|
523 |
|
524 // Bypass the selector, if sequence number is 0xFFFFFFFF. |
|
525 // This sequence number indicates that the selector |
|
526 // is of type 'bypass/drop_everything_else' |
|
527 if (ps->iSequenceNumber == 0xFFFFFFFF) |
|
528 { |
|
529 continue; |
|
530 } |
|
531 |
|
532 // Convert selector to text format and print it into buffer |
|
533 TextSel(ps, aux, EFalse); |
|
534 err = BufferAppend(aPolBfr, aux); |
|
535 if (err != KErrNone) |
|
536 { |
|
537 return err; |
|
538 } |
|
539 } |
|
540 |
|
541 // All selectors have been written |
|
542 err = BufferAppend(aPolBfr, (_L8("\n"))); |
|
543 return (err); |
|
544 } |
|
545 |
|
546 // |
|
547 // This function writes the selectors to a file according |
|
548 // to the sequence numbers available in the CPolicySelector. |
|
549 // |
|
550 // |
|
551 TInt |
|
552 TPolicyParser::WriteSelectorsInSortingOrder( |
|
553 CSelectorList* aSelList, |
|
554 HBufC8*& aPolBfr, |
|
555 TBool /* aSortingOrder */) |
|
556 { |
|
557 TInt err(KErrNone); |
|
558 TInt count(aSelList->Count()); |
|
559 TInt currentSequenceNumber(1); |
|
560 |
|
561 // Loop here until all selectors have been written |
|
562 TBool found = ETrue; |
|
563 while (found) |
|
564 { |
|
565 found = EFalse; |
|
566 |
|
567 // Loop through the selector list and search the |
|
568 // the selector corresponding to the current sequence number |
|
569 for (TInt i = 0; i < count; i++) |
|
570 { |
|
571 TBuf8<1024> aux; |
|
572 aux.Zero(); |
|
573 CPolicySelector* ps = aSelList->At(i); |
|
574 |
|
575 if (ps->iSequenceNumber == currentSequenceNumber) |
|
576 { |
|
577 // Build a selector output string |
|
578 TextSel(ps, aux, ETrue); |
|
579 |
|
580 // Write a string to the file |
|
581 err = BufferAppend(aPolBfr, aux); |
|
582 if (err != KErrNone) |
|
583 { |
|
584 return err; |
|
585 } |
|
586 // Prepare for the next selector |
|
587 currentSequenceNumber++; |
|
588 found = ETrue; |
|
589 break; |
|
590 } |
|
591 } |
|
592 } |
|
593 |
|
594 // All selectors have been written |
|
595 err = BufferAppend(aPolBfr, (_L8("\n"))); |
|
596 return (err); |
|
597 } |
|
598 |
|
599 // |
|
600 // Prints the supplied selector into a given buffer in text format |
|
601 // |
|
602 // |
|
603 void |
|
604 TPolicyParser::TextSel(CPolicySelector* aSel, |
|
605 TDes8& aBuf, |
|
606 TBool aOrdered) |
|
607 { |
|
608 aBuf.Format(_L8(" ")); |
|
609 |
|
610 if (aSel->iIsFinal) |
|
611 { |
|
612 aBuf.Append(_L8(" final ")); |
|
613 } |
|
614 |
|
615 if (aSel->iIsMerge) |
|
616 { |
|
617 aBuf.Append(_L8(" merge ")); |
|
618 } |
|
619 |
|
620 // NOTE: |
|
621 // This is a kludge to save the global selector definition |
|
622 // when policy is loaded/parsed and then finally cached into |
|
623 // a list in text format. When combined policy is build |
|
624 // before sending it to IPSEC6.PRT component, the selector |
|
625 // list is ordered so this definition is then not included |
|
626 // in the policy text that is sent into the protocol component |
|
627 if (aSel->iGlobalSelector && !aOrdered) |
|
628 { |
|
629 aBuf.Append(_L8(" scope:global ")); |
|
630 } |
|
631 |
|
632 switch (aSel->iDirection) |
|
633 { |
|
634 default: |
|
635 break; |
|
636 |
|
637 case KPolicySelector_SYMMETRIC: |
|
638 break; |
|
639 |
|
640 case KPolicySelector_INBOUND: |
|
641 aBuf.Append(_L8(" inbound ")); |
|
642 break; |
|
643 |
|
644 case KPolicySelector_OUTBOUND: |
|
645 aBuf.Append(_L8(" outbound ")); |
|
646 break; |
|
647 |
|
648 case KPolicySelector_INTERFACE: |
|
649 TBuf8<20> name; |
|
650 name.Copy(aSel->iInterface); |
|
651 aBuf.Append(_L8(" if ")); |
|
652 aBuf.Append(name); |
|
653 aBuf.Append(_L8(" ")); |
|
654 break; |
|
655 } |
|
656 |
|
657 // Check if remote address exists and no interface name defined |
|
658 if (aSel->iDirection != KPolicySelector_INTERFACE |
|
659 && aSel->iRemote.Family() != KAFUnspec) |
|
660 { |
|
661 TBuf<39> addr; |
|
662 TBuf<39> mask; |
|
663 |
|
664 aSel->iRemote.OutputWithScope(addr); |
|
665 aSel->iRemoteMask.OutputWithScope(mask); |
|
666 |
|
667 // Add remote address/mask with scope into the buffer |
|
668 aBuf.Append(_L8(" remote ")); |
|
669 aBuf.Append(addr); |
|
670 aBuf.Append(_L8(" ")); |
|
671 aBuf.Append(mask); |
|
672 } |
|
673 else |
|
674 { |
|
675 if (aSel->iRemSelEpName != NULL) |
|
676 { |
|
677 // Remote Endpoint name exists so add it into the buffer |
|
678 aBuf.Append(_L8(" remote ")); |
|
679 aBuf.Append(aSel->iRemSelEpName->Des()); |
|
680 } |
|
681 if (aSel->iRemMaskEpName != NULL) |
|
682 { |
|
683 aBuf.Append(_L8(" ")); |
|
684 aBuf.Append(aSel->iRemMaskEpName->Des()); |
|
685 } |
|
686 } |
|
687 |
|
688 // Check if local address exists and no interface name defined |
|
689 if (aSel->iDirection != KPolicySelector_INTERFACE |
|
690 && aSel->iLocal.Family() != KAFUnspec) |
|
691 { |
|
692 TBuf<39> addr; |
|
693 TBuf<39> mask; |
|
694 |
|
695 aSel->iLocal.OutputWithScope(addr); |
|
696 aSel->iLocalMask.OutputWithScope(mask); |
|
697 |
|
698 // Add local address/mask with scope into the buffer |
|
699 aBuf.Append(_L8(" local ")); |
|
700 aBuf.Append(addr); |
|
701 aBuf.Append(_L8(" ")); |
|
702 aBuf.Append(mask); |
|
703 } |
|
704 else |
|
705 { |
|
706 if (aSel->iLocSelEpName != NULL) |
|
707 { |
|
708 // Local Endpoint name exists so add it into the buffer |
|
709 aBuf.Append(_L8(" local ")); |
|
710 aBuf.Append(aSel->iLocSelEpName->Des()); |
|
711 } |
|
712 if (aSel->iLocMaskEpName != NULL) |
|
713 { |
|
714 aBuf.Append(_L8(" ")); |
|
715 aBuf.Append(aSel->iLocMaskEpName->Des()); |
|
716 } |
|
717 } |
|
718 |
|
719 if (aSel->iProtocol != 0) |
|
720 { |
|
721 aBuf.AppendFormat(_L8(" protocol %d "), aSel->iProtocol); |
|
722 } |
|
723 |
|
724 if (aSel->iLocal.Port() != 0) |
|
725 { |
|
726 aBuf.AppendFormat(_L8(" local_port %d "), aSel->iLocal.Port()); |
|
727 } |
|
728 |
|
729 if (aSel->iRemote.Port() != 0) |
|
730 { |
|
731 aBuf.AppendFormat(_L8(" remote_port %d "), aSel->iRemote.Port()); |
|
732 } |
|
733 |
|
734 if (aSel->iIcmpType != -1) |
|
735 { |
|
736 aBuf.AppendFormat(_L8(" icmp_type %d "), aSel->iIcmpType); |
|
737 } |
|
738 |
|
739 if (aSel->iType != -1) |
|
740 { |
|
741 aBuf.AppendFormat(_L8(" type %d "), aSel->iType); |
|
742 } |
|
743 |
|
744 if (aSel->iIcmpCode != -1) |
|
745 { |
|
746 aBuf.AppendFormat(_L8(" icmp_code %d "), aSel->iIcmpCode); |
|
747 } |
|
748 |
|
749 if (aSel->iDropAction) |
|
750 { |
|
751 aBuf.Append(_L8(" = drop\n ")); |
|
752 return ; |
|
753 } |
|
754 |
|
755 aBuf.Append(_L8(" = { ")); |
|
756 |
|
757 TSecpolBundleIter iterl(aSel->iBundle); |
|
758 CSecpolBundleItem* itemL(NULL); |
|
759 while ((itemL = iterl++) != NULL) |
|
760 { |
|
761 if (itemL->iSpec != NULL) |
|
762 aBuf.Append(*itemL->iSpec->iName); |
|
763 else |
|
764 aBuf.Append(_L8(" tunnel")); |
|
765 |
|
766 aBuf.Append(_L8("(")); |
|
767 |
|
768 if (!itemL->iTunnel.IsUnspecified()) |
|
769 { |
|
770 TBuf<39> addr; |
|
771 itemL->iTunnel.OutputWithScope(addr); |
|
772 aBuf.Append(addr); |
|
773 } |
|
774 else if (itemL->iTunnelEpName != NULL) |
|
775 { |
|
776 aBuf.Append(itemL->iTunnelEpName->Des()); |
|
777 } |
|
778 aBuf.Append(_L8(") ")); |
|
779 } |
|
780 |
|
781 aBuf.Append(_L8(" }\n")); |
|
782 } |
|
783 |
|
784 void |
|
785 TPolicyParser::Error(TRefByValue<const TDesC> aFmt, ...) |
|
786 { |
|
787 VA_LIST list; |
|
788 VA_START(list, aFmt); |
|
789 iMsg.FormatList(aFmt, list); |
|
790 iMsg += (_L(" at line ")); |
|
791 iMsg.AppendNum(iLine); |
|
792 }; |
|
793 |
|
794 // |
|
795 // Skip white space and mark, including comments! |
|
796 // |
|
797 void |
|
798 TPolicyParser::SkipSpaceAndMark() |
|
799 { |
|
800 TChar ch; |
|
801 TInt comment = 0; |
|
802 |
|
803 while (!Eos()) |
|
804 { |
|
805 ch = Get(); |
|
806 if (ch == '\n') |
|
807 { |
|
808 iLine++; |
|
809 comment = 0; |
|
810 } |
|
811 else if (comment || ch == '#') |
|
812 comment = 1; |
|
813 else if (!ch.IsSpace()) |
|
814 { |
|
815 UnGet(); |
|
816 break; |
|
817 } |
|
818 } |
|
819 Mark(); |
|
820 } |
|
821 |
|
822 // |
|
823 // |
|
824 token_type TPolicyParser::NextToken() |
|
825 { |
|
826 TChar ch; |
|
827 token_type val; |
|
828 |
|
829 SkipSpaceAndMark(); |
|
830 if (Eos()) |
|
831 { |
|
832 val = token_eof; |
|
833 } |
|
834 else |
|
835 { |
|
836 ch = Get(); |
|
837 if (ch == '{') |
|
838 val = token_brace_left; |
|
839 else if (ch == '}') |
|
840 val = token_brace_right; |
|
841 else if (ch == '(') |
|
842 val = token_par_left; |
|
843 else if (ch == ')') |
|
844 val = token_par_right; |
|
845 else if (ch == '=') |
|
846 val = token_equal; |
|
847 else if (ch == ',') |
|
848 val = token_comma; |
|
849 else |
|
850 { |
|
851 val = token_string; |
|
852 while (!Eos()) |
|
853 { |
|
854 ch = Peek(); |
|
855 if (ch == '{' || ch == '}' || |
|
856 ch == '(' || ch == ')' || |
|
857 ch == '=' || ch == '#' || ch.IsSpace()) |
|
858 break; |
|
859 Inc(); |
|
860 } |
|
861 } |
|
862 } |
|
863 iToken.Set(MarkedToken()); |
|
864 SkipSpaceAndMark(); |
|
865 return (val); |
|
866 } |
|
867 #ifdef SYMBIAN_IPSEC_VOIP_SUPPORT |
|
868 TInt TPolicyParser::validateProposals(CPropList& aPropList) |
|
869 { |
|
870 TInt err = KErrNone; |
|
871 CSecurityProposalSpec* prop; |
|
872 TInt validPropCount=0; |
|
873 for(TInt i=0;i<aPropList.Count();i++) |
|
874 { |
|
875 prop = aPropList.At(i); |
|
876 if((prop->iType == SADB_SATYPE_UNSPEC) || |
|
877 ((prop->iType == SADB_SATYPE_AH) && !prop->iAalg) || |
|
878 ((prop->iType == SADB_SATYPE_ESP) && !prop->iEalg) |
|
879 ){ |
|
880 |
|
881 prop->iType = SADB_SATYPE_UNSPEC; |
|
882 prop->iEalg = 0; |
|
883 prop->iAalg = 0; |
|
884 } |
|
885 else |
|
886 { |
|
887 validPropCount++; |
|
888 } |
|
889 } |
|
890 if(validPropCount ==0) |
|
891 { |
|
892 err = KErrGeneral; |
|
893 } |
|
894 return err; |
|
895 } |
|
896 CSecurityProposalSpec* TPolicyParser::CreateProposalL(CPropList& aPropList) |
|
897 { |
|
898 CSecurityProposalSpec* prop = new(ELeave) CSecurityProposalSpec; |
|
899 prop->iType = SADB_SATYPE_UNSPEC; |
|
900 aPropList.AppendL(prop); |
|
901 return prop; |
|
902 } |
|
903 |
|
904 TInt |
|
905 TPolicyParser::parse_sa_spec_paramsL(CPolicySpec& aSpec) |
|
906 { |
|
907 TInt sa_type_defined(0); |
|
908 TInt err(KErrNone); |
|
909 token_type val; |
|
910 TBool InProposal=EFalse; |
|
911 CSecurityProposalSpec* prop = CreateProposalL(*aSpec.iPropList); |
|
912 TInt propCount=1; |
|
913 |
|
914 while (((val = NextToken()) == token_string) || (val == token_brace_right)) |
|
915 { |
|
916 if (iToken.Compare(_L("proposal")) == 0) |
|
917 { |
|
918 if (NextToken() == token_brace_left) |
|
919 { |
|
920 if (propCount != 1) |
|
921 prop = CreateProposalL(*aSpec.iPropList); |
|
922 ++propCount; |
|
923 InProposal = ETrue; |
|
924 } |
|
925 else |
|
926 { |
|
927 return (KErrGeneral); |
|
928 } |
|
929 } |
|
930 else if (val == token_brace_right) |
|
931 { |
|
932 //TODO : sa_type must be defined |
|
933 if(InProposal) |
|
934 { |
|
935 InProposal = EFalse; |
|
936 prop = aSpec.iPropList->At(0); |
|
937 } |
|
938 else |
|
939 { |
|
940 break;//get out of while loop |
|
941 } |
|
942 } |
|
943 else if (iToken.Compare(_L("ah")) == 0) |
|
944 { |
|
945 if(prop->iType != SADB_SATYPE_UNSPEC) |
|
946 { |
|
947 Error(_L("invalid auth alg %d"), (TUint)prop->iType); |
|
948 return (KErrGeneral); |
|
949 } |
|
950 prop->iType = SADB_SATYPE_AH; |
|
951 } |
|
952 else if (iToken.Compare(_L("esp")) == 0) |
|
953 { |
|
954 if(prop->iType != SADB_SATYPE_UNSPEC) |
|
955 { |
|
956 Error(_L("invalid encrypt alg %d"), (TUint)prop->iType); |
|
957 return (KErrGeneral); |
|
958 } |
|
959 prop->iType = SADB_SATYPE_ESP; |
|
960 } |
|
961 else if (iToken.Compare(_L("encrypt_alg")) == 0) |
|
962 { |
|
963 err = Val(prop->iEalg, EDecimal); |
|
964 if ((err != KErrNone) || (prop->iEalg > MAX_EALG_VALUE)) |
|
965 { |
|
966 Error(_L("invalid encrypt alg %d"), (TUint)prop->iEalg); |
|
967 return (KErrGeneral); |
|
968 } |
|
969 } |
|
970 else if (iToken.Compare(_L("max_encrypt_bits")) == 0) |
|
971 { |
|
972 err = Val(prop->iEalgLen, EDecimal); |
|
973 if (err != KErrNone) |
|
974 { |
|
975 Error(_L("invalid encrypt alg key length %d"), |
|
976 prop->iEalgLen); |
|
977 return (KErrGeneral); |
|
978 } |
|
979 } |
|
980 else if (iToken.Compare(_L("auth_alg")) == 0) |
|
981 { |
|
982 err = Val(prop->iAalg, EDecimal); |
|
983 if (err != KErrNone) |
|
984 { |
|
985 Error(_L("invalid auth alg %d"), prop->iAalg); |
|
986 return (KErrGeneral); |
|
987 } |
|
988 } |
|
989 else if (iToken.Compare(_L("max_auth_bits")) == 0) |
|
990 { |
|
991 err = Val(prop->iAalgLen, EDecimal); |
|
992 if (err != KErrNone) |
|
993 { |
|
994 Error(_L("invalid auth alg length %d"), prop->iAalgLen); |
|
995 return (KErrGeneral); |
|
996 } |
|
997 } |
|
998 else if ((iToken.Compare(_L("identity")) == 0) || |
|
999 (iToken.Compare(_L("identity_remote")) == 0)) |
|
1000 { |
|
1001 if (aSpec.iRemoteIdentity) |
|
1002 { |
|
1003 Error(_L("duplicate remote identity")); |
|
1004 err = KErrGeneral; |
|
1005 } |
|
1006 else if ((val = NextToken()) == token_string) |
|
1007 { |
|
1008 aSpec.iRemoteIdentity = HBufC8::NewL(iToken.Length() + 1); |
|
1009 aSpec.iRemoteIdentity->Des().Copy(iToken); |
|
1010 } |
|
1011 else |
|
1012 { |
|
1013 Error(_L("invalid remote identity value")); |
|
1014 err = KErrGeneral; |
|
1015 } |
|
1016 } |
|
1017 else if (iToken.Compare(_L("identity_local")) == 0) |
|
1018 { |
|
1019 if (aSpec.iLocalIdentity) |
|
1020 { |
|
1021 Error(_L("duplicate local identity")); |
|
1022 err = KErrGeneral; |
|
1023 } |
|
1024 else if ((val = NextToken()) == token_string) |
|
1025 { |
|
1026 aSpec.iLocalIdentity = HBufC8::NewL(iToken.Length() + 1); |
|
1027 aSpec.iLocalIdentity->Des().Copy(iToken); |
|
1028 } |
|
1029 else |
|
1030 { |
|
1031 Error(_L("invalid local identity value")); |
|
1032 err = KErrGeneral; |
|
1033 } |
|
1034 } |
|
1035 else if (iToken.Compare(_L("pfs")) == 0) |
|
1036 { |
|
1037 aSpec.iSpec.iPfs = 1; |
|
1038 } |
|
1039 else if (iToken.Compare(_L("connid_specific")) == 0) |
|
1040 { |
|
1041 // Only a temporary backward compatibility hack |
|
1042 aSpec.iSpec.iMatchProtocol = 1; |
|
1043 aSpec.iSpec.iMatchRemotePort = 1; |
|
1044 aSpec.iSpec.iMatchLocalPort = 1; |
|
1045 } |
|
1046 else if (iToken.Compare(_L("protocol_specific")) == 0) |
|
1047 { |
|
1048 aSpec.iSpec.iMatchProtocol = 1; |
|
1049 } |
|
1050 else if ((iToken.Compare(_L("src_port_specific")) == 0) |
|
1051 || (iToken.Compare(_L("local_port_specific")) == 0)) |
|
1052 { |
|
1053 aSpec.iSpec.iMatchLocalPort = 1; |
|
1054 } |
|
1055 else if ((iToken.Compare(_L("dst_port_specific")) == 0) |
|
1056 || (iToken.Compare(_L("remote_port_specific")) == 0)) |
|
1057 { |
|
1058 aSpec.iSpec.iMatchRemotePort = 1; |
|
1059 } |
|
1060 else if (iToken.Compare(_L("proxy_specific")) == 0) |
|
1061 { |
|
1062 aSpec.iSpec.iMatchProxy = 1; |
|
1063 } |
|
1064 else if (iToken.Compare(_L("src_specific")) == 0) |
|
1065 { |
|
1066 aSpec.iSpec.iMatchSrc = 1; |
|
1067 } |
|
1068 else if (iToken.Compare(_L("local_specific")) == 0) |
|
1069 { |
|
1070 aSpec.iSpec.iMatchLocal = 1; |
|
1071 } |
|
1072 else if (iToken.Compare(_L("remote_specific")) == 0) |
|
1073 { |
|
1074 aSpec.iSpec.iMatchRemote = 1; |
|
1075 } |
|
1076 else if (iToken.Compare(_L("replay_win_len")) == 0) |
|
1077 { |
|
1078 err = Val(aSpec.iSpec.iReplayWindowLength, EDecimal); |
|
1079 } |
|
1080 else if (iToken.Compare(_L("hard_lifetime_allocations")) == 0) |
|
1081 { |
|
1082 err = Val(prop->iHard.sadb_lifetime_allocations, EDecimal); |
|
1083 } |
|
1084 else if (iToken.Compare(_L("hard_lifetime_bytes")) == 0) |
|
1085 { |
|
1086 err = Val(prop->iHard.sadb_lifetime_bytes, EDecimal); |
|
1087 } |
|
1088 else if (iToken.Compare(_L("hard_lifetime_addtime")) == 0) |
|
1089 { |
|
1090 err = Val(prop->iHard.sadb_lifetime_addtime, EDecimal); |
|
1091 } |
|
1092 else if (iToken.Compare(_L("hard_lifetime_usetime")) == 0) |
|
1093 { |
|
1094 err = Val(prop->iHard.sadb_lifetime_usetime, EDecimal); |
|
1095 } |
|
1096 else if (iToken.Compare(_L("soft_lifetime_allocations")) == 0) |
|
1097 { |
|
1098 err = Val(prop->iSoft.sadb_lifetime_allocations, EDecimal); |
|
1099 } |
|
1100 else if (iToken.Compare(_L("soft_lifetime_bytes")) == 0) |
|
1101 { |
|
1102 err = Val(prop->iSoft.sadb_lifetime_bytes, EDecimal); |
|
1103 } |
|
1104 else if (iToken.Compare(_L("soft_lifetime_addtime")) == 0) |
|
1105 { |
|
1106 err = Val(prop->iSoft.sadb_lifetime_addtime, EDecimal); |
|
1107 } |
|
1108 else if (iToken.Compare(_L("soft_lifetime_usetime")) == 0) |
|
1109 { |
|
1110 err = Val(prop->iSoft.sadb_lifetime_usetime, EDecimal); |
|
1111 } |
|
1112 else |
|
1113 { |
|
1114 Error(_L("invalid keyword")); |
|
1115 return (KErrGeneral); |
|
1116 } |
|
1117 if (err != KErrNone) |
|
1118 { |
|
1119 Error(_L("invalid numeric value")); |
|
1120 return (err); |
|
1121 } |
|
1122 } |
|
1123 |
|
1124 if (val != token_brace_right) |
|
1125 { |
|
1126 Error(_L("right brace not found")); |
|
1127 return (KErrGeneral); |
|
1128 } |
|
1129 err = validateProposals(*aSpec.iPropList); |
|
1130 |
|
1131 |
|
1132 return (err); |
|
1133 } |
|
1134 #else |
|
1135 TInt |
|
1136 TPolicyParser::parse_sa_spec_paramsL(CPolicySpec& aSpec) |
|
1137 { |
|
1138 TInt sa_type_defined(0); |
|
1139 TInt err(KErrNone); |
|
1140 token_type val; |
|
1141 |
|
1142 while ((val = NextToken()) == token_string) |
|
1143 { |
|
1144 if (iToken.Compare(_L("ah")) == 0) |
|
1145 { |
|
1146 sa_type_defined++; |
|
1147 aSpec.iSpec.iType = SADB_SATYPE_AH; |
|
1148 } |
|
1149 else if (iToken.Compare(_L("esp")) == 0) |
|
1150 { |
|
1151 sa_type_defined++; |
|
1152 aSpec.iSpec.iType = SADB_SATYPE_ESP; |
|
1153 } |
|
1154 else if (iToken.Compare(_L("encrypt_alg")) == 0) |
|
1155 { |
|
1156 err = Val(aSpec.iSpec.iEalg, EDecimal); |
|
1157 if ((err != KErrNone) || (aSpec.iSpec.iEalg > MAX_EALG_VALUE)) |
|
1158 { |
|
1159 Error(_L("invalid encrypt alg %d"), (TUint)aSpec.iSpec.iEalg); |
|
1160 return (KErrGeneral); |
|
1161 } |
|
1162 } |
|
1163 else if (iToken.Compare(_L("max_encrypt_bits")) == 0) |
|
1164 { |
|
1165 err = Val(aSpec.iSpec.iEalgLen, EDecimal); |
|
1166 if (err != KErrNone) |
|
1167 { |
|
1168 Error(_L("invalid encrypt alg key length %d"), |
|
1169 aSpec.iSpec.iEalgLen); |
|
1170 return (KErrGeneral); |
|
1171 } |
|
1172 } |
|
1173 else if (iToken.Compare(_L("auth_alg")) == 0) |
|
1174 { |
|
1175 err = Val(aSpec.iSpec.iAalg, EDecimal); |
|
1176 if (err != KErrNone) |
|
1177 { |
|
1178 Error(_L("invalid auth alg %d"), aSpec.iSpec.iAalg); |
|
1179 return (KErrGeneral); |
|
1180 } |
|
1181 } |
|
1182 else if (iToken.Compare(_L("max_auth_bits")) == 0) |
|
1183 { |
|
1184 err = Val(aSpec.iSpec.iAalgLen, EDecimal); |
|
1185 if (err != KErrNone) |
|
1186 { |
|
1187 Error(_L("invalid auth alg length %d"), aSpec.iSpec.iAalgLen); |
|
1188 return (KErrGeneral); |
|
1189 } |
|
1190 } |
|
1191 else if ((iToken.Compare(_L("identity")) == 0) || |
|
1192 (iToken.Compare(_L("identity_remote")) == 0)) |
|
1193 { |
|
1194 if (aSpec.iRemoteIdentity) |
|
1195 { |
|
1196 Error(_L("duplicate remote identity")); |
|
1197 err = KErrGeneral; |
|
1198 } |
|
1199 else if ((val = NextToken()) == token_string) |
|
1200 { |
|
1201 aSpec.iRemoteIdentity = HBufC8::NewL(iToken.Length() + 1); |
|
1202 aSpec.iRemoteIdentity->Des().Copy(iToken); |
|
1203 } |
|
1204 else |
|
1205 { |
|
1206 Error(_L("invalid remote identity value")); |
|
1207 err = KErrGeneral; |
|
1208 } |
|
1209 } |
|
1210 else if (iToken.Compare(_L("identity_local")) == 0) |
|
1211 { |
|
1212 if (aSpec.iLocalIdentity) |
|
1213 { |
|
1214 Error(_L("duplicate local identity")); |
|
1215 err = KErrGeneral; |
|
1216 } |
|
1217 else if ((val = NextToken()) == token_string) |
|
1218 { |
|
1219 aSpec.iLocalIdentity = HBufC8::NewL(iToken.Length() + 1); |
|
1220 aSpec.iLocalIdentity->Des().Copy(iToken); |
|
1221 } |
|
1222 else |
|
1223 { |
|
1224 Error(_L("invalid local identity value")); |
|
1225 err = KErrGeneral; |
|
1226 } |
|
1227 } |
|
1228 else if (iToken.Compare(_L("pfs")) == 0) |
|
1229 { |
|
1230 aSpec.iSpec.iPfs = 1; |
|
1231 } |
|
1232 else if (iToken.Compare(_L("connid_specific")) == 0) |
|
1233 { |
|
1234 // Only a temporary backward compatibility hack |
|
1235 aSpec.iSpec.iMatchProtocol = 1; |
|
1236 aSpec.iSpec.iMatchRemotePort = 1; |
|
1237 aSpec.iSpec.iMatchLocalPort = 1; |
|
1238 } |
|
1239 else if (iToken.Compare(_L("protocol_specific")) == 0) |
|
1240 { |
|
1241 aSpec.iSpec.iMatchProtocol = 1; |
|
1242 } |
|
1243 else if ((iToken.Compare(_L("src_port_specific")) == 0) |
|
1244 || (iToken.Compare(_L("local_port_specific")) == 0)) |
|
1245 { |
|
1246 aSpec.iSpec.iMatchLocalPort = 1; |
|
1247 } |
|
1248 else if ((iToken.Compare(_L("dst_port_specific")) == 0) |
|
1249 || (iToken.Compare(_L("remote_port_specific")) == 0)) |
|
1250 { |
|
1251 aSpec.iSpec.iMatchRemotePort = 1; |
|
1252 } |
|
1253 else if (iToken.Compare(_L("proxy_specific")) == 0) |
|
1254 { |
|
1255 aSpec.iSpec.iMatchProxy = 1; |
|
1256 } |
|
1257 else if (iToken.Compare(_L("src_specific")) == 0) |
|
1258 { |
|
1259 aSpec.iSpec.iMatchSrc = 1; |
|
1260 } |
|
1261 else if (iToken.Compare(_L("local_specific")) == 0) |
|
1262 { |
|
1263 aSpec.iSpec.iMatchLocal = 1; |
|
1264 } |
|
1265 else if (iToken.Compare(_L("remote_specific")) == 0) |
|
1266 { |
|
1267 aSpec.iSpec.iMatchRemote = 1; |
|
1268 } |
|
1269 else if (iToken.Compare(_L("replay_win_len")) == 0) |
|
1270 { |
|
1271 err = Val(aSpec.iSpec.iReplayWindowLength, EDecimal); |
|
1272 } |
|
1273 else if (iToken.Compare(_L("hard_lifetime_allocations")) == 0) |
|
1274 { |
|
1275 err = Val(aSpec.iSpec.iHard.sadb_lifetime_allocations, EDecimal); |
|
1276 } |
|
1277 else if (iToken.Compare(_L("hard_lifetime_bytes")) == 0) |
|
1278 { |
|
1279 err = Val(aSpec.iSpec.iHard.sadb_lifetime_bytes, EDecimal); |
|
1280 } |
|
1281 else if (iToken.Compare(_L("hard_lifetime_addtime")) == 0) |
|
1282 { |
|
1283 err = Val(aSpec.iSpec.iHard.sadb_lifetime_addtime, EDecimal); |
|
1284 } |
|
1285 else if (iToken.Compare(_L("hard_lifetime_usetime")) == 0) |
|
1286 { |
|
1287 err = Val(aSpec.iSpec.iHard.sadb_lifetime_usetime, EDecimal); |
|
1288 } |
|
1289 else if (iToken.Compare(_L("soft_lifetime_allocations")) == 0) |
|
1290 { |
|
1291 err = Val(aSpec.iSpec.iSoft.sadb_lifetime_allocations, EDecimal); |
|
1292 } |
|
1293 else if (iToken.Compare(_L("soft_lifetime_bytes")) == 0) |
|
1294 { |
|
1295 err = Val(aSpec.iSpec.iSoft.sadb_lifetime_bytes, EDecimal); |
|
1296 } |
|
1297 else if (iToken.Compare(_L("soft_lifetime_addtime")) == 0) |
|
1298 { |
|
1299 err = Val(aSpec.iSpec.iSoft.sadb_lifetime_addtime, EDecimal); |
|
1300 } |
|
1301 else if (iToken.Compare(_L("soft_lifetime_usetime")) == 0) |
|
1302 { |
|
1303 err = Val(aSpec.iSpec.iSoft.sadb_lifetime_usetime, EDecimal); |
|
1304 } |
|
1305 else |
|
1306 { |
|
1307 Error(_L("invalid keyword")); |
|
1308 return (KErrGeneral); |
|
1309 } |
|
1310 if (err != KErrNone) |
|
1311 { |
|
1312 Error(_L("invalid numeric value")); |
|
1313 return (err); |
|
1314 } |
|
1315 } |
|
1316 |
|
1317 if (val != token_brace_right) |
|
1318 { |
|
1319 Error(_L("right brace not found")); |
|
1320 return (KErrGeneral); |
|
1321 } |
|
1322 else if (sa_type_defined < 1) |
|
1323 { |
|
1324 Error(_L("sa type not defined for sa")); |
|
1325 return (KErrGeneral); |
|
1326 } |
|
1327 else if (sa_type_defined > 1) |
|
1328 { |
|
1329 Error(_L("sa type defined times for sa")); |
|
1330 return (KErrGeneral); |
|
1331 } |
|
1332 else if ((aSpec.iSpec.iType == SADB_SATYPE_AH) && !aSpec.iSpec.iAalg) |
|
1333 { |
|
1334 Error(_L("auth alg not defined for sa")); |
|
1335 return (KErrGeneral); |
|
1336 } |
|
1337 else if ((aSpec.iSpec.iType == SADB_SATYPE_ESP) && !aSpec.iSpec.iEalg) |
|
1338 { |
|
1339 Error(_L("encrypt alg not defined for sa")); |
|
1340 return (KErrGeneral); |
|
1341 } |
|
1342 else if ((aSpec.iSpec.iType == SADB_SATYPE_UNSPEC) && |
|
1343 (aSpec.iSpec.iEalg || aSpec.iSpec.iAalg)) |
|
1344 { |
|
1345 Error(_L("null SA cannot have any algorithms")); |
|
1346 return (KErrGeneral); |
|
1347 } |
|
1348 |
|
1349 return (KErrNone); |
|
1350 } |
|
1351 |
|
1352 #endif |
|
1353 TInt |
|
1354 TPolicyParser::parse_sa_specL(CSecurityPolicy* aSp) |
|
1355 { |
|
1356 TInt err(KErrNone); |
|
1357 CPolicySpec* spec(NULL); |
|
1358 |
|
1359 if (NextToken() != token_string) |
|
1360 { |
|
1361 Error(_L("Syntax error")); |
|
1362 err = KErrGeneral; |
|
1363 } |
|
1364 else |
|
1365 { |
|
1366 spec = CPolicySpec::NewL(iToken); |
|
1367 aSp->Add(spec); |
|
1368 |
|
1369 if (NextToken() != token_equal || NextToken() != token_brace_left) |
|
1370 { |
|
1371 Error(_L("Syntax error")); |
|
1372 err = KErrGeneral; |
|
1373 } |
|
1374 else |
|
1375 { |
|
1376 err = parse_sa_spec_paramsL(*spec); |
|
1377 } |
|
1378 } |
|
1379 |
|
1380 return (err); |
|
1381 } |
|
1382 |
|
1383 TInt |
|
1384 TPolicyParser::parse_sa_spec_listL(TSecpolBundle& aBundle, |
|
1385 CSecurityPolicy* aSp) |
|
1386 { |
|
1387 CSecpolBundleItem* item(NULL); |
|
1388 CPolicySpec* spec(NULL); |
|
1389 token_type val; |
|
1390 TInt err(KErrNone); |
|
1391 |
|
1392 while ((val = NextToken()) == token_string) |
|
1393 { |
|
1394 // Find the SA transform specification from the given policy |
|
1395 HBufC8 * hbuf = HBufC8::NewL(iToken.Length()); |
|
1396 hbuf->Des().Copy(iToken); |
|
1397 spec = aSp->FindSpec(hbuf->Des()); |
|
1398 delete hbuf; |
|
1399 hbuf = NULL; |
|
1400 |
|
1401 // A temporary(?) special kludge: if the keyword is 'tunnel' |
|
1402 // assume this is a plain tunnel specification, without any |
|
1403 // IPsec processing |
|
1404 |
|
1405 // NOTE: |
|
1406 // This works only when the SA specification name is not 'tunnel |
|
1407 // ('tunnel' should be illegal name for SA specification to |
|
1408 // avoid confusion) |
|
1409 if (!spec && iToken.Compare(_L("tunnel"))) |
|
1410 { |
|
1411 Error(_L("sa or plain tunnel not defined")); |
|
1412 err = KErrGeneral; |
|
1413 break; |
|
1414 } |
|
1415 |
|
1416 // Allocate memory for new bundle item |
|
1417 item = new (ELeave) CSecpolBundleItem; |
|
1418 CleanupStack::PushL(item); |
|
1419 |
|
1420 // Init bundle item by using the SA transform template found |
|
1421 item->iSpec = spec; |
|
1422 |
|
1423 // Read next token |
|
1424 val = NextToken(); |
|
1425 |
|
1426 // Check that '(' found |
|
1427 if (val != token_par_left) |
|
1428 { |
|
1429 // Remove bundle item from the CleanupStack and set error code |
|
1430 CleanupStack::PopAndDestroy(); |
|
1431 Error(_L("missing left parenthesis")); |
|
1432 err = KErrGeneral; |
|
1433 break; |
|
1434 } |
|
1435 |
|
1436 // Read next token |
|
1437 val = NextToken(); |
|
1438 |
|
1439 // Check if tunnel specification is set |
|
1440 if (val == token_string) |
|
1441 { |
|
1442 // Tunnel entry found so determine if name or plain address |
|
1443 if (aSp->SearchForEPNameL(iToken) == KErrNone) |
|
1444 { |
|
1445 // Tunnel name is set so copy it |
|
1446 item->iTunnelEpName = HBufC8::NewL(iToken.Length()); |
|
1447 item->iTunnelEpName->Des().Copy(iToken); |
|
1448 //Search for the SA transform CPolicySpec based on the |
|
1449 //remote end point name |
|
1450 CPolicySpec* specEp(NULL); |
|
1451 HBufC8 * hbuf = HBufC8::NewL(iToken.Length()); |
|
1452 hbuf->Des().Copy(iToken); |
|
1453 specEp = aSp->FindSpec(hbuf->Des()); |
|
1454 delete hbuf; |
|
1455 hbuf = NULL; |
|
1456 |
|
1457 //Set the tunnel address from the above SA transform |
|
1458 item->iTunnel.SetAddress(specEp->iEpSpec.iEpAddr.Address()); |
|
1459 } |
|
1460 else |
|
1461 { |
|
1462 // Tunnel address is set so use it |
|
1463 err = item->iTunnel.Input(iToken); |
|
1464 if (err) |
|
1465 { |
|
1466 // Remove bundle item from the CleanupStack |
|
1467 CleanupStack::PopAndDestroy(); |
|
1468 Error(_L("Invalid IP address")); |
|
1469 break; |
|
1470 } |
|
1471 } |
|
1472 |
|
1473 // Read next token |
|
1474 val = NextToken(); |
|
1475 } |
|
1476 |
|
1477 // Check that ')' terminates the definition correctly |
|
1478 if (val != token_par_right) |
|
1479 { |
|
1480 // Remove bundle item from the CleanupStack and set error code |
|
1481 CleanupStack::PopAndDestroy(); |
|
1482 Error(_L("missing right parenthesis")); |
|
1483 err = KErrGeneral; |
|
1484 break; |
|
1485 } |
|
1486 |
|
1487 // Remove bundle item from the CleanupStack and add it into the list |
|
1488 CleanupStack::Pop(); |
|
1489 aBundle.AddLast(*item); |
|
1490 } |
|
1491 |
|
1492 // Check that terminating '}' is found |
|
1493 if (!err && val != token_brace_right) |
|
1494 { |
|
1495 Error(_L("missing right brace")); |
|
1496 err = KErrGeneral; |
|
1497 } |
|
1498 |
|
1499 return (err); |
|
1500 } |
|
1501 |
|
1502 TInt |
|
1503 TPolicyParser::parse_ip_addr_and_maskL( |
|
1504 TInetAddr& addr, |
|
1505 TInetAddr& mask, |
|
1506 HBufC8*& aSelEpName, |
|
1507 HBufC8*& aMaskEpName, |
|
1508 CSecurityPolicy* aSecPol) |
|
1509 { |
|
1510 TInt err(KErrNone); |
|
1511 if (NextToken() != token_string) |
|
1512 { |
|
1513 Error(_L("ip address not found")); |
|
1514 return (KErrGeneral); |
|
1515 } |
|
1516 |
|
1517 if (aSecPol->SearchForEPNameL(iToken) == KErrNone) |
|
1518 { |
|
1519 aSelEpName = HBufC8::NewL(iToken.Length()); |
|
1520 aSelEpName->Des().Copy(iToken); |
|
1521 } |
|
1522 else |
|
1523 { |
|
1524 err = addr.Input(iToken); |
|
1525 if (err != 0) |
|
1526 { |
|
1527 Error(_L("invalid ip address ")); |
|
1528 return (err); |
|
1529 } |
|
1530 } |
|
1531 |
|
1532 if (NextToken() != token_string) |
|
1533 { |
|
1534 Error(_L("address mask not found")); |
|
1535 return (KErrGeneral); |
|
1536 } |
|
1537 |
|
1538 if (aSecPol->SearchForEPNameL(iToken) == KErrNone) |
|
1539 { |
|
1540 aMaskEpName = HBufC8::NewL(iToken.Length()); |
|
1541 aMaskEpName->Des().Copy(iToken); |
|
1542 } |
|
1543 else |
|
1544 { |
|
1545 err = mask.Input(iToken); |
|
1546 if (err != 0) |
|
1547 { |
|
1548 Error(_L("invalid address mask ")); |
|
1549 return (err); |
|
1550 } |
|
1551 } |
|
1552 |
|
1553 return (KErrNone); |
|
1554 } |
|
1555 |
|
1556 // |
|
1557 // Parse the endpoint name entry |
|
1558 // |
|
1559 // |
|
1560 TInt |
|
1561 TPolicyParser::parse_ep_specL(CSecurityPolicy* aSp) |
|
1562 { |
|
1563 TInt err(KErrNone); |
|
1564 CPolicySpec* spec(NULL); |
|
1565 |
|
1566 if (NextToken() != token_string) |
|
1567 { |
|
1568 Error(_L("Syntax error")); |
|
1569 err = KErrGeneral; |
|
1570 } |
|
1571 else |
|
1572 { |
|
1573 spec = CPolicySpec::NewL(iToken, EPolSpecEP); |
|
1574 aSp->Add(spec); |
|
1575 |
|
1576 if (NextToken() != token_equal || NextToken() != token_brace_left) |
|
1577 { |
|
1578 Error(_L("Syntax error")); |
|
1579 err = KErrGeneral; |
|
1580 } |
|
1581 else |
|
1582 { |
|
1583 err = parse_ep_spec_paramsL(*spec); |
|
1584 } |
|
1585 } |
|
1586 |
|
1587 return (err); |
|
1588 } |
|
1589 |
|
1590 // |
|
1591 // Parse the endpoint name parameters |
|
1592 // |
|
1593 TInt |
|
1594 TPolicyParser::parse_ep_spec_paramsL(CPolicySpec &aSpec) |
|
1595 { |
|
1596 TInt err(KErrNone); |
|
1597 token_type val; |
|
1598 |
|
1599 while ((val = NextToken()) == token_string) |
|
1600 { |
|
1601 if (iToken.Compare(_L("?")) == 0) |
|
1602 { |
|
1603 aSpec.iEpSpec.iIsOptional = ETrue; |
|
1604 } |
|
1605 else |
|
1606 { |
|
1607 err = aSpec.iEpSpec.iEpAddr.Input(iToken); |
|
1608 if (err != 0) |
|
1609 { |
|
1610 Error(_L("invalid ip address ")); |
|
1611 return (err); |
|
1612 } |
|
1613 } |
|
1614 } |
|
1615 |
|
1616 if (val != token_brace_right) |
|
1617 { |
|
1618 Error(_L("right brace not found")); |
|
1619 err = KErrGeneral; |
|
1620 } |
|
1621 |
|
1622 return (err); |
|
1623 } |
|
1624 |
|
1625 TInt |
|
1626 TPolicyParser::parse_conn2saL(CSecurityPolicy* aSp) |
|
1627 { |
|
1628 CPolicySelector* csa(NULL); |
|
1629 TInt err(KErrNone); |
|
1630 token_type val; |
|
1631 TUint port(0); |
|
1632 |
|
1633 csa = CPolicySelector::NewL(); |
|
1634 aSp->Add(csa); |
|
1635 |
|
1636 do |
|
1637 { |
|
1638 if ((iToken.Compare(_L("dst")) == 0) |
|
1639 || (iToken.Compare(_L("remote")) == 0)) |
|
1640 { |
|
1641 err = parse_ip_addr_and_maskL(csa->iRemote, |
|
1642 csa->iRemoteMask, |
|
1643 csa->iRemSelEpName, |
|
1644 csa->iRemMaskEpName, |
|
1645 aSp); |
|
1646 } |
|
1647 else if ((iToken.Compare(_L("src")) == 0) |
|
1648 || (iToken.Compare(_L("local")) == 0)) |
|
1649 { |
|
1650 err = parse_ip_addr_and_maskL(csa->iLocal, |
|
1651 csa->iLocalMask, |
|
1652 csa->iLocSelEpName, |
|
1653 csa->iLocMaskEpName, |
|
1654 aSp); |
|
1655 } |
|
1656 else if (iToken.Compare(_L("outbound")) == 0) |
|
1657 { |
|
1658 if (csa->iDirection != KPolicySelector_SYMMETRIC) |
|
1659 { |
|
1660 Error(_L("Only one inbound or outbound allowed")); |
|
1661 return (KErrGeneral); |
|
1662 } |
|
1663 csa->iDirection = KPolicySelector_OUTBOUND; |
|
1664 } |
|
1665 else if (iToken.Compare(_L("inbound")) == 0) |
|
1666 { |
|
1667 if (csa->iDirection != KPolicySelector_SYMMETRIC) |
|
1668 { |
|
1669 Error(_L("Only one inbound or outbound allowed")); |
|
1670 return (KErrGeneral); |
|
1671 } |
|
1672 csa->iDirection = KPolicySelector_INBOUND; |
|
1673 } |
|
1674 else if (iToken.Compare(_L("user_id")) == 0) |
|
1675 { |
|
1676 ; // Needs to be examined, TIdentity? -- msa |
|
1677 } |
|
1678 else if (iToken.Compare(_L("protocol")) == 0) |
|
1679 { |
|
1680 err = Val(csa->iProtocol); |
|
1681 } |
|
1682 else if ((iToken.Compare(_L("src_port")) == 0) || |
|
1683 (iToken.Compare(_L("local_port")) == 0)) |
|
1684 { |
|
1685 err = Val(port); |
|
1686 csa->iLocal.SetPort(port); |
|
1687 } |
|
1688 else if ((iToken.Compare(_L("dst_port")) == 0) || |
|
1689 (iToken.Compare(_L("remote_port")) == 0)) |
|
1690 { |
|
1691 err = Val(port); |
|
1692 csa->iRemote.SetPort(port); |
|
1693 } |
|
1694 else if (iToken.Compare(_L("icmp_type")) == 0) |
|
1695 { |
|
1696 err = Val(csa->iIcmpType); |
|
1697 } |
|
1698 else if (iToken.Compare(_L("type")) == 0) |
|
1699 { |
|
1700 err = Val(csa->iType); |
|
1701 } |
|
1702 else if (iToken.Compare(_L("icmp_code")) == 0) |
|
1703 { |
|
1704 err = Val(csa->iIcmpCode); |
|
1705 } |
|
1706 else if (iToken.Compare(_L("if")) == 0) |
|
1707 { |
|
1708 if (NextToken() != token_string) |
|
1709 { |
|
1710 Error(_L("Invalid interface specifier")); |
|
1711 err = KErrGeneral; |
|
1712 } |
|
1713 csa->iInterface.Append(iToken); |
|
1714 csa->iDirection = KPolicySelector_INTERFACE; |
|
1715 } |
|
1716 else if (iToken.Compare(_L("scope:global")) == 0) |
|
1717 { |
|
1718 csa->iGlobalSelector = ETrue; |
|
1719 } |
|
1720 else if (iToken.Compare(_L("final")) == 0 ) |
|
1721 { |
|
1722 csa->iIsFinal = ETrue; |
|
1723 } |
|
1724 else if (iToken.Compare(_L("merge")) == 0 ) |
|
1725 { |
|
1726 csa->iIsMerge = ETrue; |
|
1727 } |
|
1728 else |
|
1729 { |
|
1730 Error(_L("invalid keyword ")); |
|
1731 return (KErrGeneral); |
|
1732 } |
|
1733 |
|
1734 if (err != KErrNone) |
|
1735 { |
|
1736 // iMsg already contains an error text |
|
1737 if (iMsg.Length() != 0) |
|
1738 { |
|
1739 return err; |
|
1740 } |
|
1741 Error(_L("Error = %d"), err); |
|
1742 return err; |
|
1743 } |
|
1744 } |
|
1745 while ((val = NextToken()) == token_string); |
|
1746 |
|
1747 if (val != token_equal ) |
|
1748 { |
|
1749 Error(_L("Syntax error")); |
|
1750 err = KErrGeneral; |
|
1751 } |
|
1752 else if (NextToken() == token_brace_left) |
|
1753 { |
|
1754 err = parse_sa_spec_listL(csa->iBundle, aSp); |
|
1755 } |
|
1756 else if (iToken.Compare(_L("drop")) == 0) |
|
1757 { |
|
1758 csa->iDropAction = ETrue; |
|
1759 } |
|
1760 else |
|
1761 { |
|
1762 Error(_L("Syntax error")); |
|
1763 err = KErrGeneral; |
|
1764 } |
|
1765 return (err); |
|
1766 } |
|
1767 |
|
1768 // |
|
1769 // Keys Parsing |
|
1770 // |
|
1771 EXPORT_C CKeysData::CKeysData() |
|
1772 {} |
|
1773 |
|
1774 EXPORT_C CKeysData::CKeysData(CKeysData* aKey) |
|
1775 { |
|
1776 sa_type = aKey->sa_type; |
|
1777 spi = aKey->spi; |
|
1778 encr_alg = aKey->encr_alg; |
|
1779 auth_alg = aKey->auth_alg; |
|
1780 direction = aKey->direction; |
|
1781 lifetime_bytes = aKey->lifetime_bytes; |
|
1782 lifetime_sec = aKey->lifetime_sec; |
|
1783 src_addr = aKey->src_addr; // Include port |
|
1784 dst_addr = aKey->dst_addr; // Include port |
|
1785 protocol = aKey->protocol; |
|
1786 auth_key = aKey->auth_key; |
|
1787 encr_key = aKey->encr_key; |
|
1788 } |
|
1789 |
|
1790 // |
|
1791 // CKeysDataArray |
|
1792 // |
|
1793 CKeysDataArray::CKeysDataArray(TInt aGranularity) : |
|
1794 CArrayFixFlat<class CKeysData *>(aGranularity) |
|
1795 {} |
|
1796 |
|
1797 EXPORT_C CKeysDataArray* CKeysDataArray::NewL(TInt aGranularity) |
|
1798 { |
|
1799 CKeysDataArray* self = new (ELeave) CKeysDataArray(aGranularity); |
|
1800 self->Construct(aGranularity); |
|
1801 return self; |
|
1802 } |
|
1803 |
|
1804 EXPORT_C void CKeysDataArray::Construct(TInt /* aGranularity */) |
|
1805 {} |
|
1806 |
|
1807 CKeysDataArray::CKeysDataArray(CKeysDataArray* aData) : |
|
1808 CArrayFixFlat<class CKeysData *>(aData->Count()) |
|
1809 {} |
|
1810 |
|
1811 EXPORT_C CKeysDataArray* CKeysDataArray::NewL(CKeysDataArray* aData) |
|
1812 { |
|
1813 CKeysDataArray* self = new (ELeave) CKeysDataArray(aData); |
|
1814 CleanupStack::PushL(self); |
|
1815 self->ConstructL(aData); |
|
1816 CleanupStack::Pop(); |
|
1817 return self; |
|
1818 } |
|
1819 |
|
1820 EXPORT_C void CKeysDataArray::ConstructL(CKeysDataArray* aData) |
|
1821 { |
|
1822 CopyL(aData); |
|
1823 } |
|
1824 |
|
1825 EXPORT_C CKeysDataArray::~CKeysDataArray() |
|
1826 { |
|
1827 Empty(); |
|
1828 } |
|
1829 |
|
1830 // Construct this from the data in aData |
|
1831 EXPORT_C void CKeysDataArray::CopyL(CKeysDataArray* aData) |
|
1832 { |
|
1833 CKeysData* key_data(NULL); |
|
1834 for (TInt i = 0; i < aData->Count(); i++) |
|
1835 { |
|
1836 key_data = new (ELeave) CKeysData(aData->At(i)); |
|
1837 CleanupStack::PushL(key_data); |
|
1838 AppendL(key_data); |
|
1839 CleanupStack::Pop(); |
|
1840 } |
|
1841 } |
|
1842 |
|
1843 EXPORT_C void |
|
1844 CKeysDataArray::Empty() |
|
1845 { |
|
1846 for (TInt i = 0; i < Count(); i++) |
|
1847 { |
|
1848 delete At(i); |
|
1849 } |
|
1850 |
|
1851 Reset(); |
|
1852 } |
|
1853 |
|
1854 // |
|
1855 // TKeyParser |
|
1856 // |
|
1857 EXPORT_C |
|
1858 TKeyParser::TKeyParser(const TDesC &aStr) : TLex(aStr) |
|
1859 { |
|
1860 iFirst = 1; |
|
1861 } |
|
1862 |
|
1863 EXPORT_C TInt |
|
1864 TKeyParser::ParseL(CKeysDataArray *aKeys) |
|
1865 { |
|
1866 TInt err(KErrNone); |
|
1867 |
|
1868 while (!err) |
|
1869 { |
|
1870 // Skip until first token in line |
|
1871 while (iFirst == 0) |
|
1872 NextToken(); |
|
1873 |
|
1874 if (iFirst < 0) |
|
1875 break; |
|
1876 |
|
1877 NextToken(); |
|
1878 if ((iToken.Compare(_L("pfkey_add")) == 0)) |
|
1879 { |
|
1880 TInt val(0); |
|
1881 CKeysData* keyData(NULL); |
|
1882 for (int i = 0; !err && iFirst == 0; ++i) |
|
1883 { |
|
1884 switch (i) |
|
1885 { |
|
1886 // sa type: 1=AH, 2=ESP |
|
1887 case 0: |
|
1888 keyData = new (ELeave) CKeysData; |
|
1889 err = Val(val); |
|
1890 if (val == 1) |
|
1891 keyData->sa_type = SADB_SATYPE_AH; |
|
1892 else if (val == 2) |
|
1893 keyData->sa_type = SADB_SATYPE_ESP; |
|
1894 else |
|
1895 err = KErrGeneral; |
|
1896 break; |
|
1897 |
|
1898 // spi: 1..MAX_UINT32 |
|
1899 case 1: |
|
1900 err = Val(keyData->spi); |
|
1901 break; |
|
1902 |
|
1903 // Pass encryption alg numbers as is |
|
1904 case 2: |
|
1905 err = Val(keyData->encr_alg, EDecimal); |
|
1906 break; |
|
1907 |
|
1908 // Pass authentication alg numbers as is |
|
1909 case 3: |
|
1910 err = Val(keyData->auth_alg, EDecimal); |
|
1911 break; |
|
1912 |
|
1913 // direction: 4 = inbound, 8 = outbound |
|
1914 case 4: |
|
1915 err = Val(keyData->direction); |
|
1916 // Not used, direction is implicit by the src/dst pair |
|
1917 if (keyData->direction & ~(PFKEY_INI_INBOUND |
|
1918 | PFKEY_INI_OUTBOUND)) |
|
1919 err = KErrGeneral; |
|
1920 break; |
|
1921 |
|
1922 // lifetime as bytes: 0 = not used, |
|
1923 // 1..MAX_UINT32=max sa lifetime |
|
1924 case 5: |
|
1925 err = Val(keyData->lifetime_bytes); |
|
1926 break; |
|
1927 |
|
1928 // lifetime as seconds: 0 = not used, |
|
1929 // 1..MAX_UINT32=max sa lifetime |
|
1930 case 6: |
|
1931 err = Val(keyData->lifetime_sec); |
|
1932 break; |
|
1933 |
|
1934 // src ip addr: in a.b.c.d format |
|
1935 case 7: |
|
1936 NextToken(); |
|
1937 err = keyData->src_addr.Input(iToken); |
|
1938 break; |
|
1939 |
|
1940 // dst ip addr: in a.b.c.d format |
|
1941 case 8: |
|
1942 NextToken(); |
|
1943 err = keyData->dst_addr.Input(iToken); |
|
1944 break; |
|
1945 |
|
1946 // protocol: 0 = sa NOT protocol specific, |
|
1947 // 1 = ICMP, 4 = IPIP, 6 = TCP, 17 = UDP |
|
1948 case 9: |
|
1949 err = Val(val); |
|
1950 keyData->protocol = (TUint8)val; |
|
1951 break; |
|
1952 |
|
1953 // local port: 0 = sa NOT src port specific, |
|
1954 // 1..MAX_UINT16 = src port for which sa |
|
1955 // dedicated |
|
1956 case 10: |
|
1957 err = Val(val); |
|
1958 keyData->src_addr.SetPort(val); |
|
1959 break; |
|
1960 |
|
1961 // remote port: 0 = sa NOT dst port specific, |
|
1962 // 1..MAX_UINT16 = dst port for which |
|
1963 // sa dedicated |
|
1964 case 11: |
|
1965 err = Val(val); |
|
1966 keyData->dst_addr.SetPort(val); |
|
1967 break; |
|
1968 |
|
1969 // authentication key: as hex string WITHOUT leading 0x, |
|
1970 // two hex digits for every 8 bits of key, |
|
1971 // HMAC-MD5: 128 bit = 16 byte key, |
|
1972 // HMAC-SHA1: 160 bit = 20 byte key |
|
1973 case 12: |
|
1974 NextToken(); |
|
1975 if (iToken != _L("0")) |
|
1976 { |
|
1977 // 0 is No key assigned |
|
1978 keyData->auth_key.Copy(iToken); |
|
1979 } |
|
1980 break; |
|
1981 |
|
1982 // encryption key: as hex string WITHOUT leading 0x, |
|
1983 // two hex digits for every 8 bits of key, |
|
1984 // DES-CBC: 64 bit = 8 byte key, |
|
1985 // DES-EDE3-CBC: 192 bit = 24 byte key |
|
1986 case 13: |
|
1987 NextToken(); |
|
1988 if (iToken != _L("0")) //0 is No key assigned |
|
1989 keyData->encr_key.Copy(iToken); |
|
1990 break; |
|
1991 |
|
1992 default: |
|
1993 NextToken(); |
|
1994 err = KErrKeyParser; |
|
1995 break; |
|
1996 } // switch |
|
1997 SkipSpaceAndMark(); |
|
1998 } // for |
|
1999 |
|
2000 if (err == KErrNone && keyData) |
|
2001 { |
|
2002 CleanupStack::PushL(keyData); |
|
2003 aKeys->AppendL(keyData); |
|
2004 CleanupStack::Pop(); |
|
2005 } |
|
2006 else |
|
2007 { |
|
2008 delete keyData; |
|
2009 keyData = NULL; |
|
2010 } |
|
2011 } // if |
|
2012 } // while |
|
2013 |
|
2014 return (err); |
|
2015 } |
|
2016 |
|
2017 EXPORT_C TInt |
|
2018 TKeyParser::Write(CKeysDataArray *aKeys, RFile &aFile) |
|
2019 { |
|
2020 TBuf8<500> text; |
|
2021 TInt err(KErrNone); |
|
2022 TInt count = aKeys->Count(); |
|
2023 for (TInt i = 0; i < count ; i++) |
|
2024 { |
|
2025 TextPFKey(aKeys->At(i), text); |
|
2026 err = aFile.Write(text); |
|
2027 if (err != KErrNone) |
|
2028 break; |
|
2029 } |
|
2030 return (err); |
|
2031 } |
|
2032 |
|
2033 void |
|
2034 TKeyParser::TextPFKey(CKeysData *aKey, TDes8 &aElem) |
|
2035 { |
|
2036 TBuf<39> addr; |
|
2037 TBuf8<39> addr8; |
|
2038 |
|
2039 aElem.Format(_L8("pfkey_add ")); |
|
2040 |
|
2041 if (aKey->sa_type == SADB_SATYPE_AH) |
|
2042 aElem.AppendFormat(_L8("%d "), 1); |
|
2043 else |
|
2044 aElem.AppendFormat(_L8("%d "), 2); |
|
2045 |
|
2046 aElem.AppendFormat(_L8("%d "), aKey->spi); |
|
2047 |
|
2048 // Algorithms |
|
2049 aElem.AppendFormat(_L8("%d "), aKey->encr_alg); |
|
2050 aElem.AppendFormat(_L8("%d "), aKey->auth_alg); |
|
2051 |
|
2052 |
|
2053 aElem.AppendFormat(_L8("%d "), aKey->direction); |
|
2054 |
|
2055 aElem.AppendFormat(_L8("%d "), aKey->lifetime_bytes); |
|
2056 aElem.AppendFormat(_L8("%d "), aKey->lifetime_sec); |
|
2057 |
|
2058 // Addresses |
|
2059 aKey->src_addr.OutputWithScope(addr); |
|
2060 |
|
2061 addr8.Copy(addr); |
|
2062 aElem.AppendFormat(addr8); |
|
2063 aElem.AppendFormat(_L8(" ")); |
|
2064 aKey->dst_addr.OutputWithScope(addr); |
|
2065 |
|
2066 addr8.Copy(addr); |
|
2067 aElem.AppendFormat(addr8); |
|
2068 aElem.AppendFormat(_L8(" ")); |
|
2069 aElem.AppendFormat(_L8("%d "), aKey->protocol); |
|
2070 |
|
2071 // Ports |
|
2072 aElem.AppendFormat(_L8("%d "), aKey->src_addr.Port()); |
|
2073 aElem.AppendFormat(_L8("%d "), aKey->dst_addr.Port()); |
|
2074 |
|
2075 // Keys |
|
2076 if (aKey->auth_key.Length() != 0) |
|
2077 aElem.Append(aKey->auth_key); |
|
2078 else |
|
2079 aElem.Append(_L8("0")); |
|
2080 |
|
2081 aElem.Append(_L8(" ")); |
|
2082 |
|
2083 if (aKey->encr_key.Length() != 0) |
|
2084 aElem.Append(aKey->encr_key); |
|
2085 else |
|
2086 aElem.Append(_L8("0")); |
|
2087 |
|
2088 aElem.Append(_L8("\n")); |
|
2089 } |
|
2090 |
|
2091 // |
|
2092 // Skip white space and mark, including comments! |
|
2093 // |
|
2094 TInt |
|
2095 TKeyParser::SkipSpaceAndMark() |
|
2096 { |
|
2097 TChar ch; |
|
2098 TInt comment = 0; |
|
2099 TInt newline = 0; |
|
2100 |
|
2101 while (!Eos()) |
|
2102 { |
|
2103 ch = Get(); |
|
2104 if (ch == '\n') |
|
2105 { |
|
2106 comment = 0; |
|
2107 newline = 1; |
|
2108 } |
|
2109 else if (comment || ch == '#') |
|
2110 comment = 1; |
|
2111 else if (!ch.IsSpace()) |
|
2112 { |
|
2113 UnGet(); |
|
2114 break; |
|
2115 } |
|
2116 } |
|
2117 Mark(); |
|
2118 return newline; |
|
2119 } |
|
2120 |
|
2121 // |
|
2122 // Extract Next token and return |
|
2123 // |
|
2124 void |
|
2125 TKeyParser::NextToken() |
|
2126 { |
|
2127 if (SkipSpaceAndMark()) |
|
2128 iFirst = 1; // New line! |
|
2129 |
|
2130 if (Eos()) |
|
2131 { |
|
2132 iFirst = -1; |
|
2133 return ; |
|
2134 } |
|
2135 |
|
2136 while (!Eos()) |
|
2137 { |
|
2138 TChar ch = Peek(); |
|
2139 if (ch == '#' || ch.IsSpace()) |
|
2140 break; |
|
2141 Inc(); |
|
2142 } |
|
2143 iToken.Set(MarkedToken()); |
|
2144 iFirst = SkipSpaceAndMark(); |
|
2145 } |
|
2146 |
|
2147 TUint8 |
|
2148 TKeyParser::HexVal(TUint8 c) |
|
2149 { |
|
2150 if (c >= 'a' && c <= 'f') |
|
2151 return (TUint8)(c - 'a' + 10); |
|
2152 else if (c >= 'A' && c <= 'F') |
|
2153 return (TUint8)(c - 'A'); |
|
2154 else if (c >= '0' && c <= '9') |
|
2155 return (TUint8)(c - '0'); |
|
2156 else |
|
2157 return 0; |
|
2158 } |
|
2159 |
|
2160 TPtrC8 |
|
2161 TKeyParser::DeHex(const TDesC &aStr) |
|
2162 { |
|
2163 const TUint8* s = (TUint8 *)aStr.Ptr(); |
|
2164 TUint8* d = (TUint8 *)iHex.Ptr(); |
|
2165 TInt i = aStr.Length(); |
|
2166 TUint8 d1 = 0; |
|
2167 TUint8 d2 = 0; |
|
2168 |
|
2169 while (i > 0) |
|
2170 { |
|
2171 d1 = TKeyParser::HexVal(*s++); |
|
2172 d2 = i > 1 ? TKeyParser::HexVal(*s++) : (TUint8)0; |
|
2173 i -= 2; |
|
2174 *d++ = (TUint8)(d1 * 16 + d2); |
|
2175 } |
|
2176 |
|
2177 iHex.SetLength(d - iHex.Ptr()); |
|
2178 return iHex; |
|
2179 } |
|
2180 |
|
2181 // |
|
2182 // Parses an security configuration file |
|
2183 // |
|
2184 EXPORT_C |
|
2185 TIpSecParser::TIpSecParser(const TDesC &aDes) : TLex(aDes) |
|
2186 {} |
|
2187 |
|
2188 EXPORT_C TInt |
|
2189 TIpSecParser::ParseL(CIpSecurityPiece *aPiece_data) |
|
2190 { |
|
2191 return DoParseL(aPiece_data, ETrue); |
|
2192 } |
|
2193 |
|
2194 EXPORT_C TInt |
|
2195 TIpSecParser::ParseAndIgnoreIKEL(CIpSecurityPiece *aPiece_data) |
|
2196 { |
|
2197 return DoParseL(aPiece_data, EFalse); |
|
2198 } |
|
2199 |
|
2200 TInt |
|
2201 TIpSecParser::DoParseL(CIpSecurityPiece *aPiece_data, |
|
2202 TBool /* aIncludeIKE */) |
|
2203 { |
|
2204 TPtrC token(NULL, 0); |
|
2205 TInt ret(0); |
|
2206 |
|
2207 if (!CheckVersion()) |
|
2208 return KErrNotSupported; // Invalid file or version |
|
2209 |
|
2210 while (!Eos()) |
|
2211 { |
|
2212 token.Set(NextToken()); |
|
2213 if (token.Compare(_L("[INFO]")) == 0) |
|
2214 { |
|
2215 ParseInfoL(aPiece_data); |
|
2216 } |
|
2217 else if (token.Compare(_L("[POLICY]")) == 0) |
|
2218 { |
|
2219 ret = ParsePoliciesL(aPiece_data); |
|
2220 if (ret != KErrNone) |
|
2221 return ret; |
|
2222 } |
|
2223 else if (token.Compare(_L("[KEYS]")) == 0) |
|
2224 { |
|
2225 ret = ParseKeysL(aPiece_data->Keys()); |
|
2226 if (ret != KErrNone) |
|
2227 return ret; |
|
2228 } |
|
2229 else |
|
2230 { |
|
2231 // Unknown Tag Ignored |
|
2232 NextTag(); |
|
2233 } |
|
2234 } |
|
2235 |
|
2236 return (KErrNone); |
|
2237 } |
|
2238 |
|
2239 TBool |
|
2240 TIpSecParser::CheckVersion() |
|
2241 { |
|
2242 TPtrC token(NULL, 0); |
|
2243 TLex version_num; |
|
2244 |
|
2245 token.Set(NextToken()); |
|
2246 if (token.Compare(_L("SECURITY_FILE_VERSION:")) == 0) |
|
2247 { |
|
2248 version_num = NextToken(); |
|
2249 if (version_num.Val(iVersion) != KErrNone) |
|
2250 return EFalse; |
|
2251 if ((iVersion < FIRST_SEC_PARSER_VERSION) || |
|
2252 (iVersion > SEC_PARSER_VERSION)) |
|
2253 return EFalse; |
|
2254 } |
|
2255 else |
|
2256 return EFalse; |
|
2257 |
|
2258 return ETrue; |
|
2259 |
|
2260 } |
|
2261 |
|
2262 void |
|
2263 TIpSecParser::ParseInfoL(CIpSecurityPiece *aPiece_data) |
|
2264 { |
|
2265 HBufC *buf = HBufC::NewL(MAX_INFO_SIZE); |
|
2266 TPtr ptr = buf->Des(); |
|
2267 TChar ch = Get(); |
|
2268 TInt i(0); |
|
2269 |
|
2270 CleanupStack::PushL(buf); |
|
2271 |
|
2272 ch = Get(); |
|
2273 while (((ch == ' ') || (ch == '\n')) && (!Eos())) |
|
2274 { |
|
2275 ch = Get(); |
|
2276 } |
|
2277 |
|
2278 while ((ch != '[') && (!Eos()) && i < MAX_INFO_SIZE) |
|
2279 { |
|
2280 ptr.Append(ch); |
|
2281 i++; |
|
2282 ch = Get(); |
|
2283 } |
|
2284 |
|
2285 if (i == MAX_INFO_SIZE) //The rest is ignored |
|
2286 { |
|
2287 ch = Get(); |
|
2288 while ( (ch != '[') && (!Eos()) ) |
|
2289 ch = Get(); |
|
2290 } |
|
2291 |
|
2292 if (ch == '[') |
|
2293 { |
|
2294 UnGet(); // the '[' |
|
2295 if (ptr.Length() > 0) //If empty no \n |
|
2296 ptr.SetLength(ptr.Length() - 1); //eliminates the \n at the end |
|
2297 } |
|
2298 |
|
2299 aPiece_data->SetInfoL(ptr); |
|
2300 CleanupStack::PopAndDestroy(); |
|
2301 } |
|
2302 |
|
2303 TInt |
|
2304 TIpSecParser::ParsePoliciesL(CIpSecurityPiece *aPieceData) |
|
2305 { |
|
2306 TInt err; |
|
2307 TInt pos = Remainder().Find(_L("[KEYS]")); |
|
2308 if (pos == KErrNotFound) |
|
2309 { |
|
2310 pos = Remainder().Find(_L("[")); //The segment is until the next tag or Eos() |
|
2311 } |
|
2312 if (pos != KErrNotFound) |
|
2313 { |
|
2314 TPtr pol_ptr((TUint16 *)Remainder().Ptr(), pos, pos); //Until the next section |
|
2315 TPolicyParser parser(pol_ptr); |
|
2316 err = parser.ParseL(aPieceData); |
|
2317 Assign(Remainder().Ptr() + pos); //rest of the text to parse |
|
2318 } |
|
2319 else |
|
2320 { |
|
2321 TPolicyParser parser(Remainder()); |
|
2322 err = parser.ParseL(aPieceData); |
|
2323 } |
|
2324 return (err); |
|
2325 } |
|
2326 |
|
2327 TInt |
|
2328 TIpSecParser::ParseKeysL(CKeysDataArray *aKeys) |
|
2329 { |
|
2330 TInt err; |
|
2331 //The segment is until the next tag or Eos() |
|
2332 TInt pos = Remainder().Find(_L("[")); |
|
2333 if (pos != KErrNotFound) |
|
2334 { |
|
2335 // Until the next section |
|
2336 TPtr key_ptr((TUint16 *)Remainder().Ptr(), pos, pos); |
|
2337 TKeyParser parser(key_ptr); |
|
2338 err = parser.ParseL(aKeys); |
|
2339 |
|
2340 // Rest of the text to parse |
|
2341 Assign(Remainder().Ptr() + pos); |
|
2342 } |
|
2343 else |
|
2344 { |
|
2345 // No more tags |
|
2346 TKeyParser parser(Remainder()); |
|
2347 err = parser.ParseL(aKeys); |
|
2348 } |
|
2349 |
|
2350 return (err); |
|
2351 } |
|
2352 |
|
2353 void |
|
2354 TIpSecParser::NextTag() |
|
2355 { |
|
2356 while (!Eos()) |
|
2357 if (Get() == '[' ) |
|
2358 { |
|
2359 // Next tag found |
|
2360 UnGet(); |
|
2361 return ; |
|
2362 } |
|
2363 } |
|
2364 |
|
2365 // Puts the security file data into string format to be saved to the |
|
2366 // caller's buffer. |
|
2367 EXPORT_C TInt |
|
2368 TIpSecParser::Write(CIpSecurityPiece* aPiece_data, |
|
2369 HBufC8*& aPolBfr) |
|
2370 { |
|
2371 TInt err(KErrNone); |
|
2372 |
|
2373 err = WriteVersion(aPolBfr); |
|
2374 if (err != KErrNone) |
|
2375 return err; |
|
2376 |
|
2377 err = WriteInfo(aPiece_data, aPolBfr); |
|
2378 if (err != KErrNone) |
|
2379 return err; |
|
2380 |
|
2381 err = WritePolicies(aPiece_data, aPolBfr); |
|
2382 if (err != KErrNone) |
|
2383 return err; |
|
2384 |
|
2385 return (err); |
|
2386 } |
|
2387 |
|
2388 TInt |
|
2389 TIpSecParser::WriteVersion(HBufC8*& aPolBfr) |
|
2390 { |
|
2391 TBuf8<32> buf; |
|
2392 buf.Format(_L8("SECURITY_FILE_VERSION: %d\n"), SEC_PARSER_VERSION); |
|
2393 return TPolicyParser::BufferAppend(aPolBfr, buf); |
|
2394 } |
|
2395 |
|
2396 TInt |
|
2397 TIpSecParser::WriteInfo(CIpSecurityPiece *aPiece_data, HBufC8*& aPolBfr) |
|
2398 { |
|
2399 TInt err; |
|
2400 |
|
2401 TBuf8<MAX_INFO_SIZE> buf = _L8("[INFO]\n"); |
|
2402 err = TPolicyParser::BufferAppend(aPolBfr, buf); |
|
2403 if (err != KErrNone) |
|
2404 return err; |
|
2405 |
|
2406 buf.Copy(aPiece_data->Info()->Des()); |
|
2407 err = TPolicyParser::BufferAppend(aPolBfr, buf); |
|
2408 if (err != KErrNone) |
|
2409 return err; |
|
2410 return TPolicyParser::BufferAppend(aPolBfr, (_L8("\n"))); |
|
2411 |
|
2412 } |
|
2413 |
|
2414 TInt |
|
2415 TIpSecParser::WritePolicies(CIpSecurityPiece *aPiece_data, HBufC8*& aPolBfr) |
|
2416 { |
|
2417 TBuf8<10> buf = _L8("[POLICY]\n"); |
|
2418 TInt err = TPolicyParser::BufferAppend(aPolBfr, buf); |
|
2419 if (err != KErrNone) |
|
2420 return err; |
|
2421 return TPolicyParser::Write(aPiece_data->Policies(), aPolBfr); |
|
2422 } |
|
2423 |
|
2424 // |
|
2425 // CIpSecurityPiece |
|
2426 // |
|
2427 EXPORT_C void |
|
2428 CIpSecurityPiece::ConstructL(TInt aSize) |
|
2429 { |
|
2430 iInfo = HBufC::NewL(aSize); |
|
2431 iPolicies = new (ELeave) CSecurityPolicy(); |
|
2432 iPolicies->ConstructL(); |
|
2433 iKeys = CKeysDataArray::NewL(1); |
|
2434 } |
|
2435 |
|
2436 EXPORT_C void |
|
2437 CIpSecurityPiece::SetInfoL(const TDesC &aDes) |
|
2438 { |
|
2439 if (aDes.Length() > iInfo->Des().MaxLength()) |
|
2440 { |
|
2441 // ReAllocs if needed |
|
2442 iInfo = iInfo->ReAllocL(aDes.Length()); |
|
2443 } |
|
2444 |
|
2445 iInfo->Des().Copy(aDes); |
|
2446 } |
|
2447 |
|
2448 EXPORT_C |
|
2449 CIpSecurityPiece::~CIpSecurityPiece() |
|
2450 { |
|
2451 delete iInfo; |
|
2452 delete iPolicies; |
|
2453 delete iKeys; |
|
2454 } |