2 * Copyright (c) 2014 Contextream, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.lispflowmapping.implementation.authentication;
10 import static junit.framework.Assert.assertTrue;
11 import static org.junit.Assert.assertFalse;
13 import java.util.ArrayList;
15 import junitx.framework.ArrayAssert;
17 import org.junit.Ignore;
18 import org.junit.Test;
19 import org.opendaylight.lispflowmapping.implementation.serializer.MapRegisterSerializer;
20 import org.opendaylight.lispflowmapping.implementation.util.LispAFIConvertor;
21 import org.opendaylight.lispflowmapping.tools.junit.BaseTestCase;
22 import org.opendaylight.lispflowmapping.type.AddressFamilyNumberEnum;
23 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.MapRegister;
24 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.eidtolocatorrecords.EidToLocatorRecord;
25 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.eidtolocatorrecords.EidToLocatorRecordBuilder;
26 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.lispaddress.lispaddresscontainer.address.Ipv4;
27 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.lispaddress.lispaddresscontainer.address.Ipv4Builder;
28 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.locatorrecords.LocatorRecord;
29 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.mapnotifymessage.MapNotifyBuilder;
30 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address;
32 public class LispAuthenticationTest extends BaseTestCase {
35 public void validate_WrongAuthentication() throws Exception {
36 // LISP(Type = 3 Map-Register, P=1, M=1
40 // AuthDataLength: 20 Data:
41 // e8:f5:0b:c5:c5:f2:b0:21:27:a8:21:41:04:f3:46:5a:5a:5b:5c:5d
42 // EID prefix: 153.16.254.1/32 (EID=0x9910FE01), TTL: 10, Authoritative,
44 // Local RLOC: 192.168.136.10 (RLOC=0xC0A8880A), Reachable,
45 // Priority/Weight: 1/100, Multicast Priority/Weight: 255/0
47 MapRegister mapRegister = MapRegisterSerializer.getInstance().deserialize(hexToByteBuffer("38 00 01 01 FF BB " //
48 + "00 00 00 00 00 00 00 01 00 14 e8 f5 0b c5 c5 f2 " //
49 + "b0 21 27 a8 21 41 04 f3 46 5a 5a 5b 5c 5d 00 00 " //
50 + "00 0a 01 20 10 00 00 00 00 01 99 10 fe 01 01 64 " //
51 + "ff 00 00 05 00 01 c0 a8 88 0a"));
53 assertFalse(LispAuthenticationUtil.validate(mapRegister, "password"));
57 public void validate__SHA1() throws Exception {
58 // LISP(Type = 3 Map-Register, P=1, M=1
62 // AuthDataLength: 20 Data:
63 // b2:dd:1a:25:c0:60:b1:46:e8:dc:6d:a6:ae:2e:92:92:a6:ca:b7:9d
64 // EID prefix: 153.16.254.1/32 (EID=0x9910FE01), TTL: 10, Authoritative,
66 // Local RLOC: 192.168.136.10 (RLOC=0xC0A8880A), Reachable,
67 // Priority/Weight: 1/100, Multicast Priority/Weight: 255/0
69 MapRegister mapRegister = MapRegisterSerializer.getInstance().deserialize(hexToByteBuffer("38 00 01 01 FF BB " //
70 + "00 00 00 00 00 00 00 01 00 14 2c 61 b9 c9 9a 20 ba d8 f5 40 d3 55 6f 5f 6e 5a b2 0a bf b5 00 00 " //
71 + "00 0a 01 20 10 00 00 00 00 01 99 10 fe 01 01 64 " //
72 + "ff 00 00 05 00 01 c0 a8 88 0a"));
74 assertTrue(LispAuthenticationUtil.validate(mapRegister, "password"));
75 assertFalse(LispAuthenticationUtil.validate(mapRegister, "wrongPassword"));
79 public void validate__SHA256() throws Exception {
80 // LISP(Type = 3 Map-Register, P=1, M=1
84 // AuthDataLength: 32 Data:
85 // 70 30 d4 c6 10 44 0d 83 be 4d bf fd a9 8c 57 6d 68 a5 bf 32 11 c9 7b
86 // 58 c4 b9 9f 06 11 23 b9 38
87 // EID prefix: 153.16.254.1/32 (EID=0x9910FE01), TTL: 10, Authoritative,
89 // Local RLOC: 192.168.136.10 (RLOC=0xC0A8880A), Reachable,
90 // Priority/Weight: 1/100, Multicast Priority/Weight: 255/0
92 MapRegister mapRegister = MapRegisterSerializer
95 hexToByteBuffer("38 00 01 01 FF BB " //
96 + "00 00 00 00 00 00 00 02 00 20 70 30 d4 c6 10 44 0d 83 be 4d bf fd a9 8c 57 6d 68 a5 bf 32 11 c9 7b 58 c4 b9 9f 06 11 23 b9 38 00 00 " //
97 + "00 0a 01 20 10 00 00 00 00 01 99 10 fe 01 01 64 " //
98 + "ff 00 00 05 00 01 c0 a8 88 0a"));
100 assertTrue(LispAuthenticationUtil.validate(mapRegister, "password"));
101 assertFalse(LispAuthenticationUtil.validate(mapRegister, "wrongPassword"));
105 public void validate__NoAuthentication() throws Exception {
106 // LISP(Type = 3 Map-Register, P=1, M=1
108 // Nonce: (something)
110 // AuthDataLength: 0:
111 // EID prefix: 153.16.254.1/32 (EID=0x9910FE01), TTL: 10, Authoritative,
113 // Local RLOC: 192.168.136.10 (RLOC=0xC0A8880A), Reachable,
114 // Priority/Weight: 1/100, Multicast Priority/Weight: 255/0
116 MapRegister mapRegister = MapRegisterSerializer.getInstance().deserialize(hexToByteBuffer("38 00 01 01 FF BB " //
117 + "00 00 00 00 00 00 00 00 00 00 " //
119 + "00 0a 01 20 10 00 00 00 00 01 99 10 fe 01 01 64 " //
120 + "ff 00 00 05 00 01 c0 a8 88 0a"));
122 assertTrue(LispAuthenticationUtil.validate(mapRegister, "password"));
123 assertTrue(LispAuthenticationUtil.validate(mapRegister, "wrongPassword"));
127 // public void authenticate__MapNotifySHA1() throws Exception {
128 // MapNotify mapNotify = new MapNotify();
129 // mapNotify.addEidToLocator(new EidToLocatorRecord().setPrefix(new
130 // LispIpv4Address(1)));
132 // mapNotify.addEidToLocator(new EidToLocatorRecord().setPrefix(new
133 // LispIpv4Address(73)));
134 // mapNotify.setNonce(6161616161L);
135 // mapNotify.setKeyId((short) 0x0001);
136 // byte[] wantedAuthenticationData = new byte[] { (byte) 0x66, (byte) 0x69,
137 // (byte) 0x2c, (byte) 0xb8, (byte) 0xb8, (byte) 0x58, (byte) 0x7c,
138 // (byte) 0x8f, (byte) 0x4c, (byte) 0xd4, (byte) 0x8b, (byte) 0x77, (byte)
139 // 0x46, (byte) 0xf0, (byte) 0x6b, (byte) 0x9f, (byte) 0x66,
140 // (byte) 0xd2, (byte) 0xaa, (byte) 0x2c };
141 // ArrayAssert.assertEquals(wantedAuthenticationData,
142 // LispAuthenticationUtil.createAuthenticationData(mapNotify, "password"));
147 // public void authenticate__MapNotifySHA256() throws Exception {
148 // MapNotify mapNotify = new MapNotify();
149 // mapNotify.addEidToLocator(new EidToLocatorRecord().setPrefix(new
150 // LispIpv4Address(1)));
152 // mapNotify.addEidToLocator(new EidToLocatorRecord().setPrefix(new
153 // LispIpv4Address(73)));
154 // mapNotify.setNonce(6161616161L);
155 // mapNotify.setKeyId((short) 0x0002);
156 // byte[] wantedAuthenticationData = new byte[] { (byte) 0x4c, (byte) 0xf1,
157 // (byte) 0x5a, (byte) 0x4c, (byte) 0xdb, (byte) 0x8d, (byte) 0x88,
158 // (byte) 0x47, (byte) 0xf1, (byte) 0x7f, (byte) 0x27, (byte) 0x81, (byte)
159 // 0x1e, (byte) 0xbf, (byte) 0x22, (byte) 0xc7, (byte) 0xe6,
160 // (byte) 0x70, (byte) 0x16, (byte) 0x5e, (byte) 0xa1, (byte) 0x59, (byte)
161 // 0xe4, (byte) 0x06, (byte) 0x3f, (byte) 0xc2, (byte) 0x6a,
162 // (byte) 0x1c, (byte) 0x86, (byte) 0xa5, (byte) 0x8d, (byte) 0x63 };
163 // ArrayAssert.assertEquals(wantedAuthenticationData,
164 // LispAuthenticationUtil.createAuthenticationData(mapNotify, "password"));
169 public void authenticate__MapNotifyNoAuthenticationData() throws Exception {
170 MapNotifyBuilder mapNotifyBuilder = new MapNotifyBuilder();
171 mapNotifyBuilder.setKeyId((short) 0x0000);
172 mapNotifyBuilder.setEidToLocatorRecord(new ArrayList<EidToLocatorRecord>());
173 EidToLocatorRecordBuilder etlrBuilder = new EidToLocatorRecordBuilder();
174 etlrBuilder.setLocatorRecord(new ArrayList<LocatorRecord>());
175 etlrBuilder.setLispAddressContainer(LispAFIConvertor.toContainer(asIPAfiAddress("1.1.1.1")));
176 etlrBuilder.setRecordTtl(55);
177 mapNotifyBuilder.getEidToLocatorRecord().add(etlrBuilder.build());
178 ArrayAssert.assertEquals(new byte[0], LispAuthenticationUtil.createAuthenticationData(mapNotifyBuilder.build(), "password"));
182 private Ipv4 asIPAfiAddress(String ip) {
183 return new Ipv4Builder().setIpv4Address(new Ipv4Address(ip)).setAfi((short) AddressFamilyNumberEnum.IP.getIanaCode()).build();