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.lisp.authentication;
10 import static org.junit.Assert.assertFalse;
11 import static org.junit.Assert.assertTrue;
13 import java.nio.ByteBuffer;
14 import java.util.ArrayList;
15 import junitx.framework.ArrayAssert;
16 import org.junit.Test;
17 import org.opendaylight.lispflowmapping.lisp.serializer.MapNotifySerializer;
18 import org.opendaylight.lispflowmapping.lisp.serializer.MapRegisterSerializer;
19 import org.opendaylight.lispflowmapping.lisp.util.LispAddressUtil;
20 import org.opendaylight.lispflowmapping.tools.junit.BaseTestCase;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.MapRegister;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.eid.container.Eid;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.locatorrecords.LocatorRecord;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapnotifymessage.MapNotifyBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapping.authkey.container.MappingAuthkey;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapping.authkey.container.MappingAuthkeyBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapping.record.container.MappingRecordBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapping.record.list.MappingRecordItem;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapping.record.list.MappingRecordItemBuilder;
31 public class LispAuthenticationTest extends BaseTestCase {
32 private static final String PASSWORD = "password";
33 private static final String WRONG_PASSWORD = "wrongPassword";
35 private static final Eid EID = LispAddressUtil.asIpv4PrefixEid("153.16.254.1/32");
38 public void validate_WrongAuthentication() throws Exception {
39 // LISP(Type = 3 Map-Register, P=1, M=1
43 // AuthDataLength: 20 Data:
44 // e8:f5:0b:c5:c5:f2:b0:21:27:a8:21:41:04:f3:46:5a:5a:5b:5c:5d
45 // EID prefix: 153.16.254.1/32 (EID=0x9910FE01), TTL: 10, Authoritative,
47 // Local RLOC: 192.168.136.10 (RLOC=0xC0A8880A), Reachable,
48 // Priority/Weight: 1/100, Multicast Priority/Weight: 255/0
49 final ByteBuffer byteBuffer = hexToByteBuffer("38 00 01 01 FF BB "
50 + "00 00 00 00 00 00 00 01 00 14 e8 f5 0b c5 c5 f2 "
51 + "b0 21 27 a8 21 41 04 f3 46 5a 5a 5b 5c 5d 00 00 "
52 + "00 0a 01 20 10 00 00 00 00 01 99 10 fe 01 01 64 "
53 + "ff 00 00 05 00 01 c0 a8 88 0a");
54 MapRegister mapRegister = MapRegisterSerializer.getInstance().deserialize(byteBuffer, null);
56 assertFalse(validate(mapRegister, byteBuffer, EID, 1, PASSWORD));
60 public void validate__SHA1() throws Exception {
61 // LISP(Type = 3 Map-Register, P=1, M=1
65 // AuthDataLength: 20 Data:
66 // b2:dd:1a:25:c0:60:b1:46:e8:dc:6d:a6:ae:2e:92:92:a6:ca:b7:9d
67 // EID prefix: 153.16.254.1/32 (EID=0x9910FE01), TTL: 10, Authoritative,
69 // Local RLOC: 192.168.136.10 (RLOC=0xC0A8880A), Reachable,
70 // Priority/Weight: 1/100, Multicast Priority/Weight: 255/0
72 final ByteBuffer byteBuffer = hexToByteBuffer("38 00 01 01 FF BB "
73 + "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 "
74 + "00 0a 01 20 10 00 00 00 00 01 99 10 fe 01 01 64 "
75 + "ff 00 00 05 00 01 c0 a8 88 0a");
76 MapRegister mapRegister = MapRegisterSerializer.getInstance().deserialize(byteBuffer, null);
78 assertTrue(validate(mapRegister, byteBuffer, EID, 1, PASSWORD));
79 assertFalse(validate(mapRegister, byteBuffer, EID, 1, WRONG_PASSWORD));
83 public void validate__SHA256() throws Exception {
84 // LISP(Type = 3 Map-Register, P=1, M=1
88 // AuthDataLength: 32 Data:
89 // 70 30 d4 c6 10 44 0d 83 be 4d bf fd a9 8c 57 6d 68 a5 bf 32 11 c9 7b
90 // 58 c4 b9 9f 06 11 23 b9 38
91 // EID prefix: 153.16.254.1/32 (EID=0x9910FE01), TTL: 10, Authoritative,
93 // Local RLOC: 192.168.136.10 (RLOC=0xC0A8880A), Reachable,
94 // Priority/Weight: 1/100, Multicast Priority/Weight: 255/0
96 final ByteBuffer byteBuffer = hexToByteBuffer("38 00 01 01 FF BB "
97 + "00 00 00 00 00 00 00 02 00 20 "
98 + "70 30 d4 c6 10 44 0d 83 be 4d bf fd a9 8c 57 6d "
99 + "68 a5 bf 32 11 c9 7b 58 c4 b9 9f 06 11 23 b9 38 "
101 + "00 0a 01 20 10 00 00 00 00 01 99 10 fe 01 01 64 "
102 + "ff 00 00 05 00 01 c0 a8 88 0a");
103 MapRegister mapRegister = MapRegisterSerializer
105 .deserialize(byteBuffer, null);
107 assertTrue(validate(mapRegister, byteBuffer, EID, 2, PASSWORD));
108 assertFalse(validate(mapRegister, byteBuffer, EID, 2, WRONG_PASSWORD));
112 public void validate__NoAuthentication() throws Exception {
113 // LISP(Type = 3 Map-Register, P=1, M=1
115 // Nonce: (something)
117 // AuthDataLength: 0:
118 // EID prefix: 153.16.254.1/32 (EID=0x9910FE01), TTL: 10, Authoritative,
120 // Local RLOC: 192.168.136.10 (RLOC=0xC0A8880A), Reachable,
121 // Priority/Weight: 1/100, Multicast Priority/Weight: 255/0
123 final ByteBuffer byteBuffer = hexToByteBuffer("38 00 01 01 FF BB "
124 + "00 00 00 00 00 00 00 00 00 00 "
126 + "00 0a 01 20 10 00 00 00 00 01 99 10 fe 01 01 64 "
127 + "ff 00 00 05 00 01 c0 a8 88 0a");
128 MapRegister mapRegister = MapRegisterSerializer.getInstance().deserialize(byteBuffer, null);
130 assertTrue(validate(mapRegister, byteBuffer, EID, 0, PASSWORD));
131 assertTrue(validate(mapRegister, byteBuffer, EID, 0, WRONG_PASSWORD));
135 // public void authenticate__MapNotifySHA1() throws Exception {
136 // MapNotify mapNotify = new MapNotify();
137 // mapNotify.addEidToLocator(new EidToLocatorRecord().setPrefix(new
138 // LispIpv4Address(1)));
140 // mapNotify.addEidToLocator(new EidToLocatorRecord().setPrefix(new
141 // LispIpv4Address(73)));
142 // mapNotify.setNonce(6161616161L);
143 // mapNotify.setKeyId((short) 0x0001);
144 // byte[] wantedAuthenticationData = new byte[] { (byte) 0x66, (byte) 0x69,
145 // (byte) 0x2c, (byte) 0xb8, (byte) 0xb8, (byte) 0x58, (byte) 0x7c,
146 // (byte) 0x8f, (byte) 0x4c, (byte) 0xd4, (byte) 0x8b, (byte) 0x77, (byte)
147 // 0x46, (byte) 0xf0, (byte) 0x6b, (byte) 0x9f, (byte) 0x66,
148 // (byte) 0xd2, (byte) 0xaa, (byte) 0x2c };
149 // ArrayAssert.assertEquals(wantedAuthenticationData,
150 // LispAuthenticationUtil.createAuthenticationData(mapNotify, "password"));
155 // public void authenticate__MapNotifySHA256() throws Exception {
156 // MapNotify mapNotify = new MapNotify();
157 // mapNotify.addEidToLocator(new EidToLocatorRecord().setPrefix(new
158 // LispIpv4Address(1)));
160 // mapNotify.addEidToLocator(new EidToLocatorRecord().setPrefix(new
161 // LispIpv4Address(73)));
162 // mapNotify.setNonce(6161616161L);
163 // mapNotify.setKeyId((short) 0x0002);
164 // byte[] wantedAuthenticationData = new byte[] { (byte) 0x4c, (byte) 0xf1,
165 // (byte) 0x5a, (byte) 0x4c, (byte) 0xdb, (byte) 0x8d, (byte) 0x88,
166 // (byte) 0x47, (byte) 0xf1, (byte) 0x7f, (byte) 0x27, (byte) 0x81, (byte)
167 // 0x1e, (byte) 0xbf, (byte) 0x22, (byte) 0xc7, (byte) 0xe6,
168 // (byte) 0x70, (byte) 0x16, (byte) 0x5e, (byte) 0xa1, (byte) 0x59, (byte)
169 // 0xe4, (byte) 0x06, (byte) 0x3f, (byte) 0xc2, (byte) 0x6a,
170 // (byte) 0x1c, (byte) 0x86, (byte) 0xa5, (byte) 0x8d, (byte) 0x63 };
171 // ArrayAssert.assertEquals(wantedAuthenticationData,
172 // LispAuthenticationUtil.createAuthenticationData(mapNotify, "password"));
177 public void authenticate__MapNotifyNoAuthenticationData() throws Exception {
178 MapNotifyBuilder mapNotifyBuilder = new MapNotifyBuilder();
179 mapNotifyBuilder.setKeyId((short) 0x0000);
180 mapNotifyBuilder.setMappingRecordItem(new ArrayList<MappingRecordItem>());
181 MappingRecordBuilder etlrBuilder = new MappingRecordBuilder();
182 etlrBuilder.setLocatorRecord(new ArrayList<LocatorRecord>());
183 etlrBuilder.setEid(LispAddressUtil.asIpv4PrefixEid("1.1.1.1/32"));
184 etlrBuilder.setRecordTtl(55);
185 mapNotifyBuilder.getMappingRecordItem().add(
186 new MappingRecordItemBuilder().setMappingRecord(etlrBuilder.build()).build());
187 final ByteBuffer serializedMapNotifyMsg = MapNotifySerializer.getInstance().serialize(mapNotifyBuilder.build());
188 ArrayAssert.assertEquals(new byte[0], LispAuthenticationUtil.createAuthenticationData(serializedMapNotifyMsg,
193 private static boolean validate(MapRegister mapRegister, ByteBuffer byteBuffer, Eid eid, int keyId, String
195 MappingAuthkey key = new MappingAuthkeyBuilder().setKeyType(keyId).setKeyString(password).build();
196 return LispAuthenticationUtil.validate(mapRegister,byteBuffer, eid, key);