blob: 20a2f2a05f18a4b1e75ea363cbf731b4053155b4 [file] [log] [blame]
Jian Lif8c2d4a2016-09-15 02:33:12 +09001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
Jian Lif8c2d4a2016-09-15 02:33:12 +09003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package org.onosproject.lisp.msg.authentication;
17
18import org.slf4j.Logger;
19import org.slf4j.LoggerFactory;
20
21import javax.crypto.Mac;
22import javax.crypto.spec.SecretKeySpec;
23import java.security.InvalidKeyException;
24import java.security.NoSuchAlgorithmException;
25
Jian Lif8c2d4a2016-09-15 02:33:12 +090026/**
27 * LISP MAC authentication utility class.
28 */
29public class LispMacAuthentication {
30
31 private static final Logger log = LoggerFactory.getLogger(LispMacAuthentication.class);
32
Jian Li5b2b2362016-11-27 17:38:19 +090033 private static final String NOT_SUPPORT_ALGORITHM_MSG =
34 "Not support provided algorithm {}";
35 private static final String INVALID_KEY_MSG = "Provided key {} is invalid";
Jian Lif8c2d4a2016-09-15 02:33:12 +090036
Jian Li5b2b2362016-11-27 17:38:19 +090037 private String algorithm;
38
39 /**
40 * Default constructor with given authentication key type.
41 *
42 * @param authType authentication key type
43 */
44 LispMacAuthentication(LispAuthenticationKeyEnum authType) {
Jian Lif8c2d4a2016-09-15 02:33:12 +090045
Jian Li5e505c62016-12-05 02:44:24 +090046 if (authType == LispAuthenticationKeyEnum.SHA1 || authType == LispAuthenticationKeyEnum.SHA256) {
Jian Lif8c2d4a2016-09-15 02:33:12 +090047 algorithm = authType.getName();
48 } else {
Jian Li5b2b2362016-11-27 17:38:19 +090049 log.warn(NOT_SUPPORT_ALGORITHM_MSG, authType.getName());
Jian Lif8c2d4a2016-09-15 02:33:12 +090050 }
Jian Lif8c2d4a2016-09-15 02:33:12 +090051 }
52
53 /**
54 * Obtains dummy authentication data.
55 *
56 * @return dummy authentication data
57 */
Jian Li5b2b2362016-11-27 17:38:19 +090058 byte[] getAuthenticationData() {
Jian Lif8c2d4a2016-09-15 02:33:12 +090059 return new byte[0];
60 }
61
62 /**
63 * Obtains authentication data with given key and algorithm.
64 *
Jian Li5b2b2362016-11-27 17:38:19 +090065 * @param key authentication key
Jian Lif8c2d4a2016-09-15 02:33:12 +090066 * @param data array of byte buffer for place holder
67 * @return authentication data
68 */
Jian Li5b2b2362016-11-27 17:38:19 +090069 byte[] getAuthenticationData(String key, byte[] data) {
Jian Lif8c2d4a2016-09-15 02:33:12 +090070 try {
71 SecretKeySpec signKey = new SecretKeySpec(key.getBytes(), algorithm);
72 Mac mac = Mac.getInstance(algorithm);
73 mac.init(signKey);
74
75 return mac.doFinal(data);
76 } catch (NoSuchAlgorithmException e) {
Jian Li5b2b2362016-11-27 17:38:19 +090077 log.warn(NOT_SUPPORT_ALGORITHM_MSG, algorithm, e.getMessage());
Ray Milkey986a47a2018-01-25 11:38:51 -080078 throw new IllegalStateException(e);
Jian Lif8c2d4a2016-09-15 02:33:12 +090079 } catch (InvalidKeyException e) {
Jian Li5b2b2362016-11-27 17:38:19 +090080 log.warn(INVALID_KEY_MSG, key, e.getMessage());
Ray Milkey986a47a2018-01-25 11:38:51 -080081 throw new IllegalArgumentException(e);
Jian Lif8c2d4a2016-09-15 02:33:12 +090082 }
Jian Lif8c2d4a2016-09-15 02:33:12 +090083 }
Jian Lif8c2d4a2016-09-15 02:33:12 +090084}