blob: f71f8fab5c8df4275568345deedbcf4559e24ec5 [file] [log] [blame]
debanshur37cf6ba2018-05-08 20:07:30 +05301/*
2 * Copyright 2015-present Open Networking Foundation
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 */
16package org.onosproject.ovsdb.controller.impl;
17
18import com.google.common.base.MoreObjects;
19import org.slf4j.Logger;
20import org.slf4j.LoggerFactory;
21
22import java.io.File;
23import java.io.FileInputStream;
24import java.io.IOException;
25import java.security.DigestInputStream;
26import java.security.MessageDigest;
27import java.security.NoSuchAlgorithmException;
28import java.util.Arrays;
29import java.util.EnumSet;
30import java.util.Objects;
31
Thomas Vachuska00b5d4f2018-10-30 15:13:20 -070032import static org.onosproject.ovsdb.controller.impl.OsgiPropertyConstants.*;
debanshur37cf6ba2018-05-08 20:07:30 +053033
34/**
35 * TlsParams Class for properties required for configuring OVSDB TLS Connection.
36 */
37public class TlsParams {
38
39 private static final Logger log = LoggerFactory
40 .getLogger(Controller.class);
41
42 /**
43 * Options for Activated / Deactivated TLS Mode.
44 */
45 enum TlsMode {
46 /**
47 * Signifies that TLS is enabled.
48 */
49 ENABLED,
50 /**
51 * Signifies that TLS is disabled.
52 */
53 DISABLED
54 }
55
56 protected static final EnumSet<TlsMode> TLS_ENABLED = EnumSet.of(TlsMode.ENABLED);
57
58 final TlsMode mode;
59 final String ksLocation;
60 final String tsLocation;
61 final String ksPwd;
62 final String tsPwd;
63 final byte[] ksSignature;
64 final byte[] tsSignature;
65
66 /**
67 * Default Constructor.
68 */
69 TlsParams() {
70 this.mode = TlsMode.DISABLED;
Thomas Vachuska00b5d4f2018-10-30 15:13:20 -070071 this.ksLocation = KS_FILE_DEFAULT;
72 this.tsLocation = TS_FILE_DEFAULT;
73 this.ksPwd = KS_PASSWORD_DEFAULT;
74 this.tsPwd = TS_PASSWORD_DEFAULT;
debanshur37cf6ba2018-05-08 20:07:30 +053075 this.ksSignature = getSha1Checksum(ksLocation);
76 this.tsSignature = getSha1Checksum(tsLocation);
77 }
78
79 /**
80 * Creates new Tls params.
81 *
82 * @param mode TlsMode
83 * @param ksLocation keyStore Location
84 * @param tsLocation trustStore Location
85 * @param ksPwd keyStore Password
86 * @param tsPwd trustStore Password
87 */
88 TlsParams(TlsMode mode, String ksLocation, String tsLocation,
89 String ksPwd, String tsPwd) {
90 this.mode = mode;
91 this.ksLocation = ksLocation;
92 this.tsLocation = tsLocation;
93 this.ksPwd = ksPwd;
94 this.tsPwd = tsPwd;
95 this.ksSignature = getSha1Checksum(ksLocation);
96 this.tsSignature = getSha1Checksum(tsLocation);
97 }
98
99 /**
100 * Exposes the keyStore password in char[] format.
101 *
102 * @return the keyStorePassword as a char array
103 */
104 public char[] ksPwd() {
105 return ksPwd.toCharArray();
106 }
107
108 /**
109 * Exposes the trustStore password in char[] format.
110 *
111 * @return the trustStorePassword as a char array
112 */
113 public char[] tsPwd() {
114 return tsPwd.toCharArray();
115 }
116
117 /**
118 * Returns whether TLS is enabled or not.
119 *
120 * @return true if TLS is enabled otherwise false
121 */
122 public boolean isTlsEnabled() {
123 return TLS_ENABLED.contains(mode);
124 }
125
126 /**
127 * Returns SHA1 Checksum from a JKS.
128 *
129 * @param filepath JKS FilePath
130 * @return byte[] sha1checksum
131 */
132 public byte[] getSha1Checksum(String filepath) {
133 if (filepath == null) {
134 return new byte[0];
135 }
136 try {
137 MessageDigest digest = MessageDigest.getInstance("SHA1");
138 File f = new File(filepath);
139 FileInputStream is = new FileInputStream(f);
140 DigestInputStream dis = new DigestInputStream(is, digest);
141 byte[] buffer = new byte[1024];
142 while (dis.read(buffer) > 0) {
143 // nothing to do :)
144 }
柯志勇10068695d7e3b6f2018-10-11 14:30:56 +0800145 is.close();
debanshur37cf6ba2018-05-08 20:07:30 +0530146 return dis.getMessageDigest().digest();
147 } catch (NoSuchAlgorithmException e) {
148 log.error("Algorithm SHA1 Not found");
149 } catch (IOException e) {
150 log.info("Error reading file file: {}", filepath);
151 }
152 return new byte[0];
153 }
154
155 @Override
156 public int hashCode() {
157 if (mode == TlsMode.DISABLED) {
158 return Objects.hash(mode);
159 }
160 return Objects.hash(mode, ksLocation, tsLocation,
161 ksPwd, tsPwd,
162 Arrays.hashCode(ksSignature),
163 Arrays.hashCode(tsSignature));
164 }
165
166 @Override
167 public boolean equals(Object obj) {
168 if (this == obj) {
169 return true;
170 }
171 if (obj instanceof TlsParams) {
172 final TlsParams that = (TlsParams) obj;
173 if (this.getClass() != that.getClass()) {
174 return false;
175 } else if (this.mode == that.mode && this.mode == TlsMode.DISABLED) {
176 // All disabled objects should be equal regardless of other params
177 return true;
178 }
179 return this.mode == that.mode &&
180 Objects.equals(this.ksLocation, that.ksLocation) &&
181 Objects.equals(this.tsLocation, that.tsLocation) &&
182 Objects.equals(this.ksPwd, that.ksPwd) &&
183 Objects.equals(this.tsPwd, that.tsPwd) &&
184 Arrays.equals(this.ksSignature, that.ksSignature) &&
185 Arrays.equals(this.tsSignature, that.tsSignature);
186 }
187 return false;
188 }
189
190 @Override
191 public String toString() {
192 return MoreObjects.toStringHelper(this)
193 .add("tlsMode", mode.toString().toLowerCase())
194 .add("ksLocation", ksLocation)
195 .add("tsLocation", tsLocation)
196 .toString();
197 }
198}