19240fa4c53ef6d43bd07e1f3c2cead30dc174d1
[lispflowmapping.git] / mappingservice / implementation / src / main / java / org / opendaylight / lispflowmapping / implementation / authentication / LispMACAuthentication.java
1 /*
2  * Copyright (c) 2014 Contextream, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.lispflowmapping.implementation.authentication;
9
10 import java.nio.ByteBuffer;
11 import java.security.InvalidKeyException;
12 import java.security.NoSuchAlgorithmException;
13 import java.util.Arrays;
14
15 import javax.crypto.Mac;
16 import javax.crypto.spec.SecretKeySpec;
17
18 import org.opendaylight.lispflowmapping.implementation.serializer.MapNotifySerializer;
19 import org.opendaylight.lispflowmapping.implementation.serializer.MapRegisterSerializer;
20 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.MapNotify;
21 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.MapRegister;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24
25 public class LispMACAuthentication implements ILispAuthentication {
26
27     protected static final Logger LOG = LoggerFactory.getLogger(LispMACAuthentication.class);
28
29     protected String algorithm;
30     private byte[] tempAuthenticationData;
31     private int authenticationLength;
32
33     public LispMACAuthentication(String algorithm) {
34         this.algorithm = algorithm;
35         try {
36             authenticationLength = Mac.getInstance(algorithm).getMacLength();
37             tempAuthenticationData = new byte[authenticationLength];
38         } catch (NoSuchAlgorithmException e) {
39             LOG.warn("No such MAC algorithm" + algorithm);
40         }
41     }
42
43     public boolean validate(MapRegister mapRegister, String key) {
44         if (key == null) {
45             return false;
46         }
47         ByteBuffer mapRegisterBuffer = MapRegisterSerializer.getInstance().serialize(mapRegister);
48         if (mapRegisterBuffer == null) {
49             return true;
50         }
51
52         mapRegisterBuffer.position(MAP_REGISTER_AND_NOTIFY_AUTHENTICATION_POSITION);
53         mapRegisterBuffer.put(tempAuthenticationData);
54         mapRegisterBuffer.position(0);
55         return Arrays.equals(getAuthenticationData(mapRegisterBuffer.array(), key), mapRegister.getAuthenticationData());
56     }
57
58     protected byte[] getAuthenticationData(byte[] data, String key) {
59         try {
60             byte[] keyBytes = key.getBytes();
61             SecretKeySpec signingKey = new SecretKeySpec(keyBytes, algorithm);
62             Mac mac = Mac.getInstance(algorithm);
63             mac.init(signingKey);
64
65             return mac.doFinal(data);
66         } catch (InvalidKeyException e) {
67             LOG.warn("Invalid password" + key);
68         } catch (NoSuchAlgorithmException e) {
69             LOG.warn("No such MAC algorithm" + algorithm);
70         }
71         return null;
72     }
73
74     public int getAuthenticationLength() {
75         return authenticationLength;
76     }
77
78     public String getAlgorithm() {
79         return algorithm;
80     }
81
82     public void setAlgorithm(String algorithm) {
83         this.algorithm = algorithm;
84     }
85
86     public byte[] getAuthenticationData(MapNotify mapNotify, String key) {
87         return getAuthenticationData(MapNotifySerializer.getInstance().serialize(mapNotify).array(), key);
88     }
89
90 }