aboutsummaryrefslogtreecommitdiffstats
path: root/cadi/core/src/main/java/org/onap/ccsdk/apps/cadi/config/SecurityInfo.java
blob: ad0c4c92c0efcd3d72b4776498d9ddf7f199f4f6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
/**
 * ============LICENSE_START====================================================
 * org.onap.ccsdk
 * ===========================================================================
 * Copyright (c) 2023 AT&T Intellectual Property. All rights reserved.
 * ===========================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END====================================================
 *
 */

package org.onap.ccsdk.apps.cadi.config;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.rmi.AccessException;
import java.security.KeyManagementException;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.UnrecoverableKeyException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.ArrayList;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.KeyManager;
import javax.net.ssl.KeyManagerFactory;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;
import javax.net.ssl.TrustManagerFactory;
import javax.net.ssl.X509KeyManager;
import javax.net.ssl.X509TrustManager;

import org.onap.ccsdk.apps.cadi.Access;
import org.onap.ccsdk.apps.cadi.CadiException;
import org.onap.ccsdk.apps.cadi.Access.Level;
import org.onap.ccsdk.apps.cadi.util.MaskFormatException;
import org.onap.ccsdk.apps.cadi.util.NetMask;
import org.onap.ccsdk.apps.cadi.util.Split;

public class SecurityInfo {
    private static final String SECURITY_ALGO = "RSA";
    private static final String HTTPS_PROTOCOLS = "https.protocols";
    private static final String JDK_TLS_CLIENT_PROTOCOLS = "jdk.tls.client.protocols";
    private static final String INITIALIZING_ERR_FMT = "Error initializing %s: %s";
    private static final String LOADED_FROM_CADI_PROPERTIES = "%s loaded from CADI Properties";
    private static final String LOADED_FROM_SYSTEM_PROPERTIES = "%s loaded from System Properties";

    public static final String SSL_KEY_MANAGER_FACTORY_ALGORITHM;

    private SSLSocketFactory socketFactory;
    private X509KeyManager[] x509KeyManager;
    private X509TrustManager[] x509TrustManager;
    public final String defaultAlias;
    public final String defaultClientAlias;
    private NetMask[] trustMasks;
    private SSLContext context;
    private HostnameVerifier maskHV;
    public final Access access;

    // Change Key Algorithms for IBM's VM.  Could put in others, if needed.
    static {
        if ("IBM Corporation".equalsIgnoreCase(System.getProperty("java.vm.vendor"))) {
            SSL_KEY_MANAGER_FACTORY_ALGORITHM = "IbmX509";
        } else {
            SSL_KEY_MANAGER_FACTORY_ALGORITHM = "SunX509";
        }
    }


    public SecurityInfo(final Access access) throws CadiException {
        String msgHelp = "";
        try {
            this.access = access;
            // reuse DME2 Properties for convenience if specific Properties don't exist

            String str = access.getProperty(Config.CADI_ALIAS, null);
            if(str==null || str.isEmpty()) {
                defaultAlias = null;
            } else {
                defaultAlias = str;
            }

            str = access.getProperty(Config.CADI_CLIENT_ALIAS, null);
            if(str==null) {
                defaultClientAlias = defaultAlias;
            } else if(str.isEmpty()) {
                // intentionally off, i.e. cadi_client_alias=
                defaultClientAlias = null;
            } else {
                defaultClientAlias = str;
            }

            msgHelp = String.format(INITIALIZING_ERR_FMT,"Keystore", access.getProperty(Config.CADI_KEYSTORE, ""));
            initializeKeyManager();

            msgHelp = String.format(INITIALIZING_ERR_FMT,"Truststore", access.getProperty(Config.CADI_TRUSTSTORE, ""));
            initializeTrustManager();


            msgHelp = String.format(INITIALIZING_ERR_FMT,"Trustmasks", access.getProperty(Config.CADI_TRUST_MASKS, ""));
            initializeTrustMasks();

            msgHelp = String.format(INITIALIZING_ERR_FMT,"HTTP Protocols", "access properties");
            setHTTPProtocols(access);

            msgHelp = String.format(INITIALIZING_ERR_FMT,"Context", "TLS");
            context = SSLContext.getInstance("TLS");
            context.init(x509KeyManager, x509TrustManager, null);
            SSLContext.setDefault(context);
            socketFactory = context.getSocketFactory();
        } catch (NoSuchAlgorithmException | KeyManagementException | KeyStoreException | CertificateException | UnrecoverableKeyException | IOException e) {
            throw new CadiException(msgHelp,e);
        }
    }

    public static void setHTTPProtocols(Access access) {
        String httpsProtocols = System.getProperty(Config.HTTPS_PROTOCOLS);
        if(httpsProtocols!=null) {
            access.printf(Level.INIT, LOADED_FROM_SYSTEM_PROPERTIES, HTTPS_PROTOCOLS);
        } else {
            httpsProtocols = access.getProperty(Config.HTTPS_PROTOCOLS,null);
            if(httpsProtocols!=null) {
                access.printf(Level.INIT, LOADED_FROM_CADI_PROPERTIES, HTTPS_PROTOCOLS);
            } else {
                httpsProtocols = access.getProperty(HTTPS_PROTOCOLS, Config.HTTPS_PROTOCOLS_DEFAULT);
                access.printf(Level.INIT, "%s set by %s in CADI Properties",Config.HTTPS_PROTOCOLS,Config.CADI_PROTOCOLS);
            }
            // This needs to be set when people do  not.
            System.setProperty(HTTPS_PROTOCOLS, httpsProtocols);
        }
        String httpsClientProtocols = System.getProperty(JDK_TLS_CLIENT_PROTOCOLS,null);
        if(httpsClientProtocols!=null) {
            access.printf(Level.INIT, LOADED_FROM_SYSTEM_PROPERTIES, JDK_TLS_CLIENT_PROTOCOLS);
        } else {
            httpsClientProtocols = access.getProperty(Config.HTTPS_CLIENT_PROTOCOLS, null);
            if(httpsClientProtocols!=null) {
                access.printf(Level.INIT, LOADED_FROM_CADI_PROPERTIES, Config.HTTPS_CLIENT_PROTOCOLS);
            } else {
                httpsClientProtocols = Config.HTTPS_PROTOCOLS_DEFAULT;
                access.printf(Level.INIT, "%s set from %s",Config.HTTPS_CLIENT_PROTOCOLS, "Default Protocols");
            }
            System.setProperty(JDK_TLS_CLIENT_PROTOCOLS, httpsClientProtocols);
        }
    }

    /**
     * @return the scf
     */
    public SSLSocketFactory getSSLSocketFactory() {
        return socketFactory;
    }

    public SSLContext getSSLContext() {
        return context;
    }

    /**
     * @return the km
     */
    public X509KeyManager[] getKeyManagers() {
        return x509KeyManager;
    }

    public void checkClientTrusted(X509Certificate[] certarr) throws CertificateException {
        for (X509TrustManager xtm : x509TrustManager) {
            xtm.checkClientTrusted(certarr, SECURITY_ALGO);
        }
    }

    public void checkServerTrusted(X509Certificate[] certarr) throws CertificateException {
        for (X509TrustManager xtm : x509TrustManager) {
            xtm.checkServerTrusted(certarr, SECURITY_ALGO);
        }
    }

    public void setSocketFactoryOn(HttpsURLConnection hsuc) {
        hsuc.setSSLSocketFactory(socketFactory);
        if (maskHV != null && !maskHV.equals(hsuc.getHostnameVerifier())) {
            hsuc.setHostnameVerifier(maskHV);
        }
    }

    protected void initializeKeyManager() throws CadiException, IOException, NoSuchAlgorithmException, KeyStoreException, CertificateException, UnrecoverableKeyException {
        String keyStore = access.getProperty(Config.CADI_KEYSTORE, null);
        if(keyStore==null) {
            return;
        } else if (!new File(keyStore).exists()) {
            throw new CadiException(keyStore + " does not exist");
        }

        String keyStorePasswd = access.getProperty(Config.CADI_KEYSTORE_PASSWORD, null);
        keyStorePasswd = (keyStorePasswd == null) ? null : access.decrypt(keyStorePasswd, false);
        if (keyStore == null || keyStorePasswd == null) {
            x509KeyManager = new X509KeyManager[0];
            return;
        }

        String keyPasswd = access.getProperty(Config.CADI_KEY_PASSWORD, null);
        keyPasswd = (keyPasswd == null) ? keyStorePasswd : access.decrypt(keyPasswd, false);

        KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance(SSL_KEY_MANAGER_FACTORY_ALGORITHM);

        ArrayList<X509KeyManager> keyManagers = new ArrayList<>();
        File file;
        for (String ksname : Split.splitTrim(',', keyStore)) {
            String keystoreFormat;
            if (ksname.endsWith(".p12") || ksname.endsWith(".pkcs12")) {
                keystoreFormat = "PKCS12";
            } else {
                keystoreFormat = "JKS";
            }

            file = new File(ksname);
            if (file.exists()) {
                FileInputStream fis = new FileInputStream(file);
                try {
                    KeyStore ks = KeyStore.getInstance(keystoreFormat);
                    ks.load(fis, keyStorePasswd.toCharArray());
                    keyManagerFactory.init(ks, keyPasswd.toCharArray());
                } finally {
                    fis.close();
                }
            }
        }

        StringBuilder sb = null;
        for (KeyManager keyManager : keyManagerFactory.getKeyManagers()) {
            if (keyManager instanceof X509KeyManager) {
                X509KeyManager xkm = (X509KeyManager)keyManager;
                keyManagers.add(xkm);
                if(defaultAlias!=null) {
                    sb=new StringBuilder("X509 Chain\n");
                    x509Info(sb,xkm.getCertificateChain(defaultAlias));
                }
                if(defaultClientAlias!=null && !defaultClientAlias.equals(defaultAlias)) {
                    if(sb==null) {
                        sb = new StringBuilder();
                    } else {
                        sb.append('\n');
                    }
                    sb.append("X509 Client Chain\n");
                    x509Info(sb,xkm.getCertificateChain(defaultAlias));
                }
            }
        }
        x509KeyManager = new X509KeyManager[keyManagers.size()];
        keyManagers.toArray(x509KeyManager);

        if(sb!=null) {
            access.log(Level.INIT, sb);
        }
    }

    private void x509Info(StringBuilder sb, X509Certificate[] chain) {
        if(chain!=null) {
            int i=0;
            for(X509Certificate x : chain) {
                sb.append("  ");
                sb.append(i++);
                sb.append(')');
                sb.append("\n    Subject: ");
                sb.append(x.getSubjectDN());
                sb.append("\n    Issuer : ");
                sb.append(x.getIssuerDN());
                sb.append("\n    Expires: ");
                sb.append(x.getNotAfter());
                sb.append('\n');
            }
        }
    }

    protected void initializeTrustManager() throws NoSuchAlgorithmException, CertificateException, IOException, KeyStoreException, CadiException {
        String trustStore = access.getProperty(Config.CADI_TRUSTSTORE, null);
        if(trustStore==null) {
            return;
        } else if(!new File(trustStore).exists()) {
            throw new CadiException(trustStore + " does not exist");
        }

        String trustStorePasswd = access.getProperty(Config.CADI_TRUSTSTORE_PASSWORD, null);
        trustStorePasswd = (trustStorePasswd == null ) ? "changeit"/*defacto Java Trust Pass*/ : access.decrypt(trustStorePasswd, false);

        TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(SSL_KEY_MANAGER_FACTORY_ALGORITHM);
        File file;
        for (String trustStoreName : Split.splitTrim(',',trustStore)) {
            file = new File(trustStoreName);
            if (file.exists()) {
                FileInputStream fis = new FileInputStream(file);
                try {
                    KeyStore ts = KeyStore.getInstance("JKS");
                    ts.load(fis, trustStorePasswd.toCharArray());
                    trustManagerFactory.init(ts);
                } finally {
                    fis.close();
                }
            }
        }

        TrustManager trustManagers[] = trustManagerFactory.getTrustManagers();
        if (trustManagers == null || trustManagers.length == 0) {
            return;
        }

        x509TrustManager = new X509TrustManager[trustManagers.length];
        for (int i = 0; i < trustManagers.length; ++i) {
            try {
                x509TrustManager[i] = (X509TrustManager)trustManagers[i];
            } catch (ClassCastException e) {
                access.log(Level.WARN, "Non X509 TrustManager", x509TrustManager[i].getClass().getName(), "skipped in SecurityInfo");
            }
        }
    }

    protected void initializeTrustMasks() throws AccessException {
        String tips = access.getProperty(Config.CADI_TRUST_MASKS, null);
        if (tips == null) {
            return;
        }

        access.log(Level.INIT, "Explicitly accepting valid X509s from", tips);
        String[] ipsplit = Split.splitTrim(',', tips);
        trustMasks = new NetMask[ipsplit.length];
        for (int i = 0; i < ipsplit.length; ++i) {
            try {
                trustMasks[i] = new NetMask(ipsplit[i]);
            } catch (MaskFormatException e) {
                throw new AccessException("Invalid IP Mask in " + Config.CADI_TRUST_MASKS, e);
            }
        }

        final HostnameVerifier origHV = HttpsURLConnection.getDefaultHostnameVerifier();
        maskHV = new HostnameVerifier() {
            @Override
            public boolean verify(final String urlHostName, final SSLSession session) {
                try {
                    // This will pick up /etc/host entries as well as DNS
                    InetAddress ia = InetAddress.getByName(session.getPeerHost());
                    for (NetMask tmask : trustMasks) {
                        if (tmask.isInNet(ia.getHostAddress())) {
                            return true;
                        }
                    }
                } catch (UnknownHostException e) {
                    // It's ok. do normal Verify
                }
                return origHV.verify(urlHostName, session);
            };
        };
        HttpsURLConnection.setDefaultHostnameVerifier(maskHV);
    }

}