blob: dfa8ce1c7842eadaf0f1fbb5ae6a185d8ff79a67 [file] [log] [blame]
Robin Jarryf94c6742014-03-28 12:03:01 +01001##############################
2% IPSec layer regression tests
3##############################
4
5###############################################################################
6+ IPv4 / ESP
7
8#######################################
9= IPv4 / ESP - Transport - AES-CBC - NULL
10
Robin Jarry60579062016-06-01 16:17:07 +020011import socket
12
Robin Jarryf94c6742014-03-28 12:03:01 +010013p = IP(src='1.1.1.1', dst='2.2.2.2')
14p /= TCP(sport=45012, dport=80)
15p /= Raw('testdata')
16p = IP(str(p))
17p
18
19sa = SecurityAssociation(ESP, spi=0x222,
20 crypt_algo='AES-CBC', crypt_key='sixteenbytes key',
21 auth_algo='NULL', auth_key=None)
22
23e = sa.encrypt(p)
24e
25
26assert(isinstance(e, IP))
27assert(e.src == '1.1.1.1' and e.dst == '2.2.2.2')
28assert(e.chksum != p.chksum)
29assert(e.proto == socket.IPPROTO_ESP)
30assert(e.haslayer(ESP))
31assert(not e.haslayer(TCP))
32assert(e[ESP].spi == sa.spi)
33* after encryption the original packet payload should NOT be readable
34assert('testdata' not in e[ESP].data)
35
36d = sa.decrypt(e)
37d
38
39* after decryption the original packet payload should be unaltered
40assert(d[TCP] == p[TCP])
41
42#######################################
43= IPv4 / ESP - Transport - NULL - HMAC-SHA1-96
44
45p = IP(src='1.1.1.1', dst='2.2.2.2')
46p /= TCP(sport=45012, dport=80)
47p /= Raw('testdata')
48p = IP(str(p))
49p
50
51sa = SecurityAssociation(ESP, spi=0x222,
52 crypt_algo='NULL', crypt_key=None,
53 auth_algo='HMAC-SHA1-96', auth_key='secret key')
54
55e = sa.encrypt(p)
56e
57
58assert(isinstance(e, IP))
59assert(e.src == '1.1.1.1' and e.dst == '2.2.2.2')
60assert(e.chksum != p.chksum)
61assert(e.proto == socket.IPPROTO_ESP)
62assert(e.haslayer(ESP))
63assert(not e.haslayer(TCP))
64assert(e[ESP].spi == sa.spi)
65assert('testdata' in e[ESP].data)
66
67* integrity verification should pass
68d = sa.decrypt(e)
69
70* after decryption the original packet payload should be unaltered
71assert(d[TCP] == p[TCP])
72
73#######################################
74= IPv4 / ESP - Transport - NULL - HMAC-SHA1-96 - altered packet
75
76p = IP(src='1.1.1.1', dst='2.2.2.2')
77p /= TCP(sport=45012, dport=80)
78p /= Raw('testdata')
79p = IP(str(p))
80p
81
82sa = SecurityAssociation(ESP, spi=0x222,
83 crypt_algo='NULL', crypt_key=None,
84 auth_algo='HMAC-SHA1-96', auth_key='secret key')
85
86e = sa.encrypt(p)
87e
88
89assert(isinstance(e, IP))
90assert(e.src == '1.1.1.1' and e.dst == '2.2.2.2')
91assert(e.chksum != p.chksum)
92assert(e.proto == socket.IPPROTO_ESP)
93assert(e.haslayer(ESP))
94assert(not e.haslayer(TCP))
95assert(e[ESP].spi == sa.spi)
96assert('testdata' in e[ESP].data)
97
98* simulate the alteration of the packet before decryption
99e[ESP].data = e[ESP].data.replace('\x01', '\x21')
100
101* integrity verification should fail
102try:
103 d = sa.decrypt(e)
104 assert(False)
105except IPSecIntegrityError, err:
106 err
107
108#######################################
109= IPv4 / ESP - Tunnel - AES-CTR - NULL
110
111p = IP(src='1.1.1.1', dst='2.2.2.2')
112p /= TCP(sport=45012, dport=80)
113p /= Raw('testdata')
114p = IP(str(p))
115p
116
117sa = SecurityAssociation(ESP, spi=0x222,
118 crypt_algo='AES-CTR', crypt_key='16bytekey+4bytenonce',
119 auth_algo='NULL', auth_key=None,
120 tunnel_header=IP(src='11.11.11.11', dst='22.22.22.22'))
121
122e = sa.encrypt(p)
123e
124
125assert(isinstance(e, IP))
126* after encryption packet should be encapsulated with the given ip tunnel header
127assert(e.src == '11.11.11.11' and e.dst == '22.22.22.22')
128assert(e.chksum != p.chksum)
129assert(e.proto == socket.IPPROTO_ESP)
130assert(e.haslayer(ESP))
131assert(not e.haslayer(TCP))
132assert(e[ESP].spi == sa.spi)
133* after encryption the original packet payload should NOT be readable
134assert('testdata' not in e[ESP].data)
135
136d = sa.decrypt(e)
137d
138
139* after decryption original packet should be preserved
140assert(d == p)
141
142#######################################
Daniel Collins620f1952016-10-26 01:03:37 -0700143= IPv4 / ESP - Tunnel - AES-GCM - NULL
144~ combined_modes
145
146p = IP(src='1.1.1.1', dst='2.2.2.2')
147p /= TCP(sport=45012, dport=80)
148p /= Raw('testdata')
149p = IP(str(p))
150p
151
152sa = SecurityAssociation(ESP, spi=0x222,
153 crypt_algo='AES-GCM', crypt_key='16bytekey+4bytenonce',
154 auth_algo='NULL', auth_key=None,
155 tunnel_header=IP(src='11.11.11.11', dst='22.22.22.22'))
156
157e = sa.encrypt(p)
158e
159
160assert(isinstance(e, IP))
161* after encryption packet should be encapsulated with the given ip tunnel header
162assert(e.src == '11.11.11.11' and e.dst == '22.22.22.22')
163assert(e.chksum != p.chksum)
164assert(e.proto == socket.IPPROTO_ESP)
165assert(e.haslayer(ESP))
166assert(not e.haslayer(TCP))
167assert(e[ESP].spi == sa.spi)
168* after encryption the original packet payload should NOT be readable
169assert('testdata' not in e[ESP].data)
170
171d = sa.decrypt(e)
172d
173
174* after decryption original packet should be preserved
175assert(d == p)
176
177#######################################
178= IPv4 / ESP - Tunnel - AES-CCM - NULL
179~ combined_modes
180
181p = IP(src='1.1.1.1', dst='2.2.2.2')
182p /= TCP(sport=45012, dport=80)
183p /= Raw('testdata')
184p = IP(str(p))
185p
186
187sa = SecurityAssociation(ESP, spi=0x222,
188 crypt_algo='AES-CCM', crypt_key='16bytekey+4bytenonce',
189 auth_algo='NULL', auth_key=None,
190 tunnel_header=IP(src='11.11.11.11', dst='22.22.22.22'))
191
192e = sa.encrypt(p)
193e
194
195assert(isinstance(e, IP))
196* after encryption packet should be encapsulated with the given ip tunnel header
197assert(e.src == '11.11.11.11' and e.dst == '22.22.22.22')
198assert(e.chksum != p.chksum)
199assert(e.proto == socket.IPPROTO_ESP)
200assert(e.haslayer(ESP))
201assert(not e.haslayer(TCP))
202assert(e[ESP].spi == sa.spi)
203* after encryption the original packet payload should NOT be readable
204assert('testdata' not in e[ESP].data)
205
206d = sa.decrypt(e)
207d
208
209* after decryption original packet should be preserved
210assert(d == p)
211
212#######################################
Robin Jarryf94c6742014-03-28 12:03:01 +0100213= IPv4 / ESP - Tunnel - NULL - SHA2-256-128
214
215p = IP(src='1.1.1.1', dst='2.2.2.2')
216p /= TCP(sport=45012, dport=80)
217p /= Raw('testdata')
218p = IP(str(p))
219p
220
221sa = SecurityAssociation(ESP, spi=0x222,
222 crypt_algo='NULL', crypt_key=None,
223 auth_algo='SHA2-256-128', auth_key='secret key',
224 tunnel_header=IP(src='11.11.11.11', dst='22.22.22.22'))
225
226e = sa.encrypt(p)
227e
228
229assert(isinstance(e, IP))
230* after encryption packet should be encapsulated with the given ip tunnel header
231assert(e.src == '11.11.11.11' and e.dst == '22.22.22.22')
232assert(e.chksum != p.chksum)
233assert(e.proto == socket.IPPROTO_ESP)
234assert(e.haslayer(ESP))
235assert(not e.haslayer(TCP))
236assert(e[ESP].spi == sa.spi)
237* after encryption the original packet payload should be readable
238assert('testdata' in e[ESP].data)
239
240* integrity verification should pass
241d = sa.decrypt(e)
242
243* after decryption the original packet should be preserved
244assert(d == p)
245
246#######################################
247= IPv4 / ESP - Tunnel - NULL - SHA2-256-128 - altered packet
248
249p = IP(src='1.1.1.1', dst='2.2.2.2')
250p /= TCP(sport=45012, dport=80)
251p /= Raw('testdata')
252p = IP(str(p))
253p
254
255sa = SecurityAssociation(ESP, spi=0x222,
256 crypt_algo='NULL', crypt_key=None,
257 auth_algo='SHA2-256-128', auth_key='secret key',
258 tunnel_header=IP(src='11.11.11.11', dst='22.22.22.22'))
259
260e = sa.encrypt(p)
261e
262
263assert(isinstance(e, IP))
264* after encryption packet should be encapsulated with the given ip tunnel header
265assert(e.src == '11.11.11.11' and e.dst == '22.22.22.22')
266assert(e.chksum != p.chksum)
267assert(e.proto == socket.IPPROTO_ESP)
268assert(e.haslayer(ESP))
269assert(not e.haslayer(TCP))
270assert(e[ESP].spi == sa.spi)
271* after encryption the original packet payload should be readable
272assert('testdata' in e[ESP].data)
273
274* simulate the alteration of the packet before decryption
275e[ESP].data = e[ESP].data.replace('\x01', '\x21')
276
277* integrity verification should fail
278try:
279 d = sa.decrypt(e)
280 assert(False)
281except IPSecIntegrityError, err:
282 err
283
284###############################################################################
285+ IPv6 / ESP
286
287#######################################
288= IPv6 / ESP - Transport - DES - NULL
289
290p = IPv6(src='11::22', dst='22::11')
291p /= TCP(sport=45012, dport=80)
292p /= Raw('testdata')
293p = IPv6(str(p))
294p
295
296sa = SecurityAssociation(ESP, spi=0x222,
297 crypt_algo='DES', crypt_key='8bytekey',
298 auth_algo='NULL', auth_key=None)
299
300e = sa.encrypt(p)
301e
302
303assert(isinstance(e, IPv6))
304assert(e.src == '11::22' and e.dst == '22::11')
305* the encrypted packet should have an ESP layer
306assert(e.nh == socket.IPPROTO_ESP)
307assert(e.haslayer(ESP))
308assert(not e.haslayer(TCP))
309assert(e[ESP].spi == sa.spi)
310* after encryption the original packet payload should NOT be readable
311assert('testdata' not in e[ESP].data)
312
313d = sa.decrypt(e)
314d
315
316* after decryption the original packet payload should be unaltered
317assert(d[TCP] == p[TCP])
318
319#######################################
320= IPv6 / ESP - Transport - NULL - HMAC-MD5-96
321
322p = IPv6(src='11::22', dst='22::11')
323p /= TCP(sport=45012, dport=80)
324p /= Raw('testdata')
325p = IPv6(str(p))
326p
327
328sa = SecurityAssociation(ESP, spi=0x222,
329 crypt_algo='NULL', crypt_key=None,
330 auth_algo='HMAC-MD5-96', auth_key='secret key')
331
332e = sa.encrypt(p)
333e
334
335assert(isinstance(e, IPv6))
336assert(e.src == '11::22' and e.dst == '22::11')
337* the encrypted packet should have an ESP layer
338assert(e.nh == socket.IPPROTO_ESP)
339assert(e.haslayer(ESP))
340assert(not e.haslayer(TCP))
341assert(e[ESP].spi == sa.spi)
342* after encryption the original packet payload should be readable
343assert('testdata' in e[ESP].data)
344
345* integrity verification should pass
346d = sa.decrypt(e)
347
348* after decryption the original packet payload should be unaltered
349assert(d[TCP] == p[TCP])
350
351#######################################
352= IPv6 / ESP - Transport - NULL - HMAC-MD5-96 - altered packet
353
354p = IPv6(src='11::22', dst='22::11')
355p /= TCP(sport=45012, dport=80)
356p /= Raw('testdata')
357p = IPv6(str(p))
358p
359
360sa = SecurityAssociation(ESP, spi=0x222,
361 crypt_algo='NULL', crypt_key=None,
362 auth_algo='HMAC-MD5-96', auth_key='secret key')
363
364e = sa.encrypt(p)
365e
366
367assert(isinstance(e, IPv6))
368assert(e.src == '11::22' and e.dst == '22::11')
369* the encrypted packet should have an ESP layer
370assert(e.nh == socket.IPPROTO_ESP)
371assert(e.haslayer(ESP))
372assert(not e.haslayer(TCP))
373assert(e[ESP].spi == sa.spi)
374* after encryption the original packet payload should be readable
375assert('testdata' in e[ESP].data)
376
377* simulate the alteration of the packet before decryption
378e[ESP].data = e[ESP].data.replace('\x01', '\x21')
379
380* integrity verification should fail
381try:
382 d = sa.decrypt(e)
383 assert(False)
384except IPSecIntegrityError, err:
385 err
386
387#######################################
388= IPv6 / ESP - Tunnel - 3DES - NULL
389
390p = IPv6(src='11::22', dst='22::11')
391p /= TCP(sport=45012, dport=80)
392p /= Raw('testdata')
393p = IPv6(str(p))
394p
395
396sa = SecurityAssociation(ESP, spi=0x222,
397 crypt_algo='AES-CBC', crypt_key='sixteenbytes key',
398 auth_algo='NULL', auth_key=None,
399 tunnel_header=IPv6(src='aa::bb', dst='bb::aa'))
400
401e = sa.encrypt(p)
402e
403
404assert(isinstance(e, IPv6))
405* after encryption packet should be encapsulated with the given ip tunnel header
406assert(e.src == 'aa::bb' and e.dst == 'bb::aa')
407assert(e.nh == socket.IPPROTO_ESP)
408assert(e.haslayer(ESP))
409assert(not e.haslayer(TCP))
410assert(e[ESP].spi == sa.spi)
411* after encryption the original packet payload should NOT be readable
412assert('testdata' not in e[ESP].data)
413
414d = sa.decrypt(e)
415d
416
417* after decryption original packet should be preserved
418assert(d == p)
419
420#######################################
421= IPv6 / ESP - Tunnel - NULL - SHA2-384-192
422
423p = IPv6(src='11::22', dst='22::11')
424p /= TCP(sport=45012, dport=80)
425p /= Raw('testdata')
426p = IPv6(str(p))
427p
428
429sa = SecurityAssociation(ESP, spi=0x222,
430 crypt_algo='NULL', crypt_key=None,
431 auth_algo='SHA2-384-192', auth_key='secret key',
432 tunnel_header=IPv6(src='aa::bb', dst='bb::aa'))
433
434e = sa.encrypt(p)
435e
436
437assert(isinstance(e, IPv6))
438* after encryption packet should be encapsulated with the given ip tunnel header
439assert(e.src == 'aa::bb' and e.dst == 'bb::aa')
440assert(e.nh == socket.IPPROTO_ESP)
441assert(e.haslayer(ESP))
442assert(not e.haslayer(TCP))
443assert(e[ESP].spi == sa.spi)
444* after encryption the original packet payload should be readable
445assert('testdata' in e[ESP].data)
446
447* integrity verification should pass
448d = sa.decrypt(e)
449
450* after decryption the original packet should be preserved
451assert(d == p)
452
453#######################################
454= IPv6 / ESP - Tunnel - NULL - SHA2-384-192 - altered packet
455
456p = IPv6(src='11::22', dst='22::11')
457p /= TCP(sport=45012, dport=80)
458p /= Raw('testdata')
459p = IPv6(str(p))
460p
461
462sa = SecurityAssociation(ESP, spi=0x222,
463 crypt_algo='NULL', crypt_key=None,
464 auth_algo='SHA2-384-192', auth_key='secret key',
465 tunnel_header=IPv6(src='aa::bb', dst='bb::aa'))
466
467e = sa.encrypt(p)
468e
469
470assert(isinstance(e, IPv6))
471* after encryption packet should be encapsulated with the given ip tunnel header
472assert(e.src == 'aa::bb' and e.dst == 'bb::aa')
473assert(e.nh == socket.IPPROTO_ESP)
474assert(e.haslayer(ESP))
475assert(not e.haslayer(TCP))
476assert(e[ESP].spi == sa.spi)
477* after encryption the original packet payload should be readable
478assert('testdata' in e[ESP].data)
479
480* simulate the alteration of the packet before decryption
481e[ESP].data = e[ESP].data.replace('\x01', '\x21')
482
483* integrity verification should fail
484try:
485 d = sa.decrypt(e)
486 assert(False)
487except IPSecIntegrityError, err:
488 err
489
490###############################################################################
491+ IPv4 / AH
492
493#######################################
Robin Jarrydef2cd82014-06-17 18:37:05 +0200494= IPv4 / AH - Transport - HMAC-SHA1-96
Robin Jarryf94c6742014-03-28 12:03:01 +0100495
496p = IP(src='1.1.1.1', dst='2.2.2.2')
497p /= TCP(sport=45012, dport=80)
498p /= Raw('testdata')
499p = IP(str(p))
500p
501
502sa = SecurityAssociation(AH, spi=0x222,
Robin Jarrydef2cd82014-06-17 18:37:05 +0200503 auth_algo='HMAC-SHA1-96', auth_key='sixteenbytes key')
Robin Jarryf94c6742014-03-28 12:03:01 +0100504
505e = sa.encrypt(p)
506e
507
508assert(isinstance(e, IP))
509assert(e.src == '1.1.1.1' and e.dst == '2.2.2.2')
510assert(e.chksum != p.chksum)
511* the encrypted packet should have an AH layer
512assert(e.proto == socket.IPPROTO_AH)
513assert(e.haslayer(AH))
514assert(e.haslayer(TCP))
515assert(e[AH].spi == sa.spi)
516
517* alter mutable fields in the packet
518e.ttl = 2
519
520* integrity verification should pass
521d = sa.decrypt(e)
522d
523
524* after decryption the original packet payload should be unaltered
525assert(d[TCP] == p[TCP])
526
527#######################################
Robin Jarrydef2cd82014-06-17 18:37:05 +0200528= IPv4 / AH - Transport - HMAC-SHA1-96 - altered packet
Robin Jarryf94c6742014-03-28 12:03:01 +0100529
530p = IP(src='1.1.1.1', dst='2.2.2.2')
531p /= TCP(sport=45012, dport=80)
532p /= Raw('testdata')
533p = IP(str(p))
534p
535
536sa = SecurityAssociation(AH, spi=0x222,
Robin Jarrydef2cd82014-06-17 18:37:05 +0200537 auth_algo='HMAC-SHA1-96', auth_key='sixteenbytes key')
Robin Jarryf94c6742014-03-28 12:03:01 +0100538
539e = sa.encrypt(p)
540e
541
542assert(isinstance(e, IP))
543assert(e.src == '1.1.1.1' and e.dst == '2.2.2.2')
544assert(e.chksum != p.chksum)
545* the encrypted packet should have an AH layer
546assert(e.proto == socket.IPPROTO_AH)
547assert(e.haslayer(AH))
548assert(e.haslayer(TCP))
549assert(e[AH].spi == sa.spi)
550
551* simulate the alteration of the packet before decryption
552e[TCP].sport = 5
553
554* integrity verification should fail
555try:
556 d = sa.decrypt(e)
557 assert(False)
558except IPSecIntegrityError, err:
559 err
560
561#######################################
562= IPv4 / AH - Tunnel - SHA2-256-128
563
564p = IP(src='1.1.1.1', dst='2.2.2.2')
565p /= TCP(sport=45012, dport=80)
566p /= Raw('testdata')
567p = IP(str(p))
568p
569
570sa = SecurityAssociation(AH, spi=0x222,
571 auth_algo='SHA2-256-128', auth_key='secret key',
572 tunnel_header=IP(src='11.11.11.11', dst='22.22.22.22'))
573
574e = sa.encrypt(p)
575e
576
577assert(isinstance(e, IP))
578assert(e.src == '11.11.11.11' and e.dst == '22.22.22.22')
579assert(e.chksum != p.chksum)
580assert(e.proto == socket.IPPROTO_AH)
581assert(e.haslayer(AH))
582assert(e.haslayer(TCP))
583assert(e[AH].spi == sa.spi)
584
585* alter mutable fields in the packet
586e.ttl = 2
587
588* integrity verification should pass
589d = sa.decrypt(e)
590d
591
592* after decryption the original packet should be unaltered
593assert(d == p)
594
595#######################################
596= IPv4 / AH - Tunnel - HMAC-SHA1-96 - altered packet
597
598p = IP(src='1.1.1.1', dst='2.2.2.2')
599p /= TCP(sport=45012, dport=80)
600p /= Raw('testdata')
601p = IP(str(p))
602p
603
604sa = SecurityAssociation(AH, spi=0x222,
605 auth_algo='HMAC-SHA1-96', auth_key='secret key',
606 tunnel_header=IP(src='11.11.11.11', dst='22.22.22.22'))
607
608e = sa.encrypt(p)
609e
610
611assert(isinstance(e, IP))
612assert(e.src == '11.11.11.11' and e.dst == '22.22.22.22')
613assert(e.chksum != p.chksum)
614assert(e.proto == socket.IPPROTO_AH)
615assert(e.haslayer(AH))
616assert(e.haslayer(TCP))
617assert(e[AH].spi == sa.spi)
618
619* simulate the alteration of the packet before verification
620e.dst = '4.4.4.4'
621
622* integrity verification should fail
623try:
624 d = sa.decrypt(e)
625 assert(False)
626except IPSecIntegrityError, err:
627 err
628
629###############################################################################
630+ IPv6 / AH
631
632#######################################
633= IPv6 / AH - Transport - HMAC-SHA1-96
634
635p = IPv6(src='11::22', dst='22::11')
636p /= TCP(sport=45012, dport=80)
637p /= Raw('testdata')
638p = IPv6(str(p))
639p
640
641sa = SecurityAssociation(AH, spi=0x222,
642 auth_algo='HMAC-SHA1-96', auth_key='secret key')
643
644e = sa.encrypt(p)
645e
646
647assert(isinstance(e, IPv6))
648assert(e.src == '11::22' and e.dst == '22::11')
649* the encrypted packet should have an AH layer
650assert(e.nh == socket.IPPROTO_AH)
651assert(e.haslayer(AH))
652assert(e.haslayer(TCP))
653assert(e[AH].spi == sa.spi)
654
655* alter mutable fields in the packet
656e.hlim = 2
657
658* integrity verification should pass
659d = sa.decrypt(e)
660d
661
662* after decryption the original packet payload should be unaltered
663assert(d[TCP] == p[TCP])
664
665#######################################
666= IPv6 / AH - Transport - HMAC-SHA1-96 - altered packet
667
668p = IPv6(src='11::22', dst='22::11')
669p /= TCP(sport=45012, dport=80)
670p /= Raw('testdata')
671p = IPv6(str(p))
672p
673
674sa = SecurityAssociation(AH, spi=0x222,
675 auth_algo='HMAC-SHA1-96', auth_key='secret key')
676
677e = sa.encrypt(p)
678e
679
680assert(isinstance(e, IPv6))
681assert(e.src == '11::22' and e.dst == '22::11')
682* the encrypted packet should have an AH layer
683assert(e.nh == socket.IPPROTO_AH)
684assert(e.haslayer(AH))
685assert(e.haslayer(TCP))
686assert(e[AH].spi == sa.spi)
687
688* simulate the alteration of the packet before verification
689e[TCP].dport = 46
690
691* integrity verification should fail
692try:
693 d = sa.decrypt(e)
694 assert(False)
695except IPSecIntegrityError, err:
696 err
697
698#######################################
699= IPv6 / AH - Tunnel - HMAC-SHA1-96
700
701p = IPv6(src='11::22', dst='22::11')
702p /= TCP(sport=45012, dport=80)
703p /= Raw('testdata')
704p = IPv6(str(p))
705p
706
707sa = SecurityAssociation(AH, spi=0x222,
708 auth_algo='HMAC-SHA1-96', auth_key='secret key',
709 tunnel_header=IPv6(src='aa::bb', dst='bb::aa'))
710
711e = sa.encrypt(p)
712e
713
714assert(isinstance(e, IPv6))
715* after encryption packet should be encapsulated with the given ip tunnel header
716assert(e.src == 'aa::bb' and e.dst == 'bb::aa')
717assert(e.nh == socket.IPPROTO_AH)
718assert(e.haslayer(AH))
719assert(e.haslayer(TCP))
720assert(e[AH].spi == sa.spi)
721
722* alter mutable fields in the packet
723e.hlim = 2
724
725* integrity verification should pass
726d = sa.decrypt(e)
727d
728
729* after decryption the original packet payload should be unaltered
730assert(d == p)
731
732#######################################
733= IPv6 / AH - Tunnel - HMAC-SHA1-96 - altered packet
734
735p = IPv6(src='11::22', dst='22::11')
736p /= TCP(sport=45012, dport=80)
737p /= Raw('testdata')
738p = IPv6(str(p))
739p
740
741sa = SecurityAssociation(AH, spi=0x222,
742 auth_algo='HMAC-SHA1-96', auth_key='secret key',
743 tunnel_header=IPv6(src='aa::bb', dst='bb::aa'))
744
745e = sa.encrypt(p)
746e
747
748assert(isinstance(e, IPv6))
749* after encryption packet should be encapsulated with the given ip tunnel header
750assert(e.src == 'aa::bb' and e.dst == 'bb::aa')
751assert(e.nh == socket.IPPROTO_AH)
752assert(e.haslayer(AH))
753assert(e.haslayer(TCP))
754assert(e[AH].spi == sa.spi)
755
756* simulate the alteration of the packet before verification
757e.src = 'cc::ee'
758
759* integrity verification should fail
760try:
761 d = sa.decrypt(e)
762 assert(False)
763except IPSecIntegrityError, err:
764 err
765
766###############################################################################
767+ IPv6 + Extensions / AH
768
769#######################################
770= IPv6 + Extensions / AH - Transport
771
772p = IPv6(src='11::22', dst='22::11')
773p /= IPv6ExtHdrHopByHop()
774p /= IPv6ExtHdrDestOpt()
775p /= IPv6ExtHdrRouting()
776p /= IPv6ExtHdrDestOpt()
777p /= IPv6ExtHdrFragment()
778p /= TCP(sport=45012, dport=80)
779p /= Raw('testdata')
780p = IPv6(str(p))
781p
782
783sa = SecurityAssociation(AH, spi=0x222,
784 auth_algo='HMAC-SHA1-96', auth_key='secret key')
785
786e = sa.encrypt(p)
787e
788
789assert(e.src == '11::22' and e.dst == '22::11')
790* AH header should be inserted between the routing header and the dest options header
791assert(isinstance(e[AH].underlayer, IPv6ExtHdrRouting))
792assert(isinstance(e[AH].payload, IPv6ExtHdrDestOpt))
793
794#######################################
795= IPv6 + Routing Header / AH - Transport
796
797p = IPv6(src='11::22', dst='22::11')
798p /= IPv6ExtHdrHopByHop()
799p /= IPv6ExtHdrRouting(addresses=['aa::bb', 'cc::dd', 'ee::ff'])
800p /= TCP(sport=45012, dport=80)
801p /= Raw('testdata')
802p = IPv6(str(p))
803p
804
805sa = SecurityAssociation(AH, spi=0x222,
806 auth_algo='HMAC-SHA1-96', auth_key='secret key')
807
808e = sa.encrypt(p)
809e
810
811assert(e.src == '11::22' and e.dst == '22::11')
812* AH header should be inserted between the routing header and TCP
813assert(isinstance(e[AH].underlayer, IPv6ExtHdrRouting))
814assert(isinstance(e[AH].payload, TCP))
815
816* reorder the routing header as the receiver will get it
817final = e[IPv6ExtHdrRouting].addresses.pop()
818e[IPv6ExtHdrRouting].addresses.insert(0, e.dst)
819e.dst = final
820e[IPv6ExtHdrRouting].segleft = 0
821
822* integrity verification should pass
823d = sa.decrypt(e)
824d