Jian Li | f8c2d4a | 2016-09-15 02:33:12 +0900 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Jian Li | f8c2d4a | 2016-09-15 02:33:12 +0900 | [diff] [blame] | 3 | * |
| 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 | */ |
| 16 | package org.onosproject.lisp.msg.authentication; |
| 17 | |
| 18 | import org.slf4j.Logger; |
| 19 | import org.slf4j.LoggerFactory; |
| 20 | |
| 21 | import javax.crypto.Mac; |
| 22 | import javax.crypto.spec.SecretKeySpec; |
| 23 | import java.security.InvalidKeyException; |
| 24 | import java.security.NoSuchAlgorithmException; |
| 25 | |
Jian Li | f8c2d4a | 2016-09-15 02:33:12 +0900 | [diff] [blame] | 26 | /** |
| 27 | * LISP MAC authentication utility class. |
| 28 | */ |
| 29 | public class LispMacAuthentication { |
| 30 | |
| 31 | private static final Logger log = LoggerFactory.getLogger(LispMacAuthentication.class); |
| 32 | |
Jian Li | 5b2b236 | 2016-11-27 17:38:19 +0900 | [diff] [blame] | 33 | 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 Li | f8c2d4a | 2016-09-15 02:33:12 +0900 | [diff] [blame] | 36 | |
Jian Li | 5b2b236 | 2016-11-27 17:38:19 +0900 | [diff] [blame] | 37 | 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 Li | f8c2d4a | 2016-09-15 02:33:12 +0900 | [diff] [blame] | 45 | |
Jian Li | 5e505c6 | 2016-12-05 02:44:24 +0900 | [diff] [blame] | 46 | if (authType == LispAuthenticationKeyEnum.SHA1 || authType == LispAuthenticationKeyEnum.SHA256) { |
Jian Li | f8c2d4a | 2016-09-15 02:33:12 +0900 | [diff] [blame] | 47 | algorithm = authType.getName(); |
| 48 | } else { |
Jian Li | 5b2b236 | 2016-11-27 17:38:19 +0900 | [diff] [blame] | 49 | log.warn(NOT_SUPPORT_ALGORITHM_MSG, authType.getName()); |
Jian Li | f8c2d4a | 2016-09-15 02:33:12 +0900 | [diff] [blame] | 50 | } |
Jian Li | f8c2d4a | 2016-09-15 02:33:12 +0900 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | /** |
| 54 | * Obtains dummy authentication data. |
| 55 | * |
| 56 | * @return dummy authentication data |
| 57 | */ |
Jian Li | 5b2b236 | 2016-11-27 17:38:19 +0900 | [diff] [blame] | 58 | byte[] getAuthenticationData() { |
Jian Li | f8c2d4a | 2016-09-15 02:33:12 +0900 | [diff] [blame] | 59 | return new byte[0]; |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Obtains authentication data with given key and algorithm. |
| 64 | * |
Jian Li | 5b2b236 | 2016-11-27 17:38:19 +0900 | [diff] [blame] | 65 | * @param key authentication key |
Jian Li | f8c2d4a | 2016-09-15 02:33:12 +0900 | [diff] [blame] | 66 | * @param data array of byte buffer for place holder |
| 67 | * @return authentication data |
| 68 | */ |
Jian Li | 5b2b236 | 2016-11-27 17:38:19 +0900 | [diff] [blame] | 69 | byte[] getAuthenticationData(String key, byte[] data) { |
Jian Li | f8c2d4a | 2016-09-15 02:33:12 +0900 | [diff] [blame] | 70 | 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 Li | 5b2b236 | 2016-11-27 17:38:19 +0900 | [diff] [blame] | 77 | log.warn(NOT_SUPPORT_ALGORITHM_MSG, algorithm, e.getMessage()); |
Ray Milkey | 986a47a | 2018-01-25 11:38:51 -0800 | [diff] [blame^] | 78 | throw new IllegalStateException(e); |
Jian Li | f8c2d4a | 2016-09-15 02:33:12 +0900 | [diff] [blame] | 79 | } catch (InvalidKeyException e) { |
Jian Li | 5b2b236 | 2016-11-27 17:38:19 +0900 | [diff] [blame] | 80 | log.warn(INVALID_KEY_MSG, key, e.getMessage()); |
Ray Milkey | 986a47a | 2018-01-25 11:38:51 -0800 | [diff] [blame^] | 81 | throw new IllegalArgumentException(e); |
Jian Li | f8c2d4a | 2016-09-15 02:33:12 +0900 | [diff] [blame] | 82 | } |
Jian Li | f8c2d4a | 2016-09-15 02:33:12 +0900 | [diff] [blame] | 83 | } |
Jian Li | f8c2d4a | 2016-09-15 02:33:12 +0900 | [diff] [blame] | 84 | } |