Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: c18bc4178d4852f2c184bf9b06b81864617a5209 (plain) (blame)
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
/*******************************************************************************
 * Copyright (c) 2009, 2012 Wind River Systems, Inc. and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Wind River Systems - initial API and implementation
 *******************************************************************************/
package org.eclipse.tcf.ssl;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.Socket;
import java.security.KeyFactory;
import java.security.Principal;
import java.security.PrivateKey;
import java.security.cert.CertificateException;
import java.security.cert.CertificateFactory;
import java.security.cert.X509Certificate;
import java.security.spec.PKCS8EncodedKeySpec;
import java.util.ArrayList;

import javax.net.ssl.KeyManager;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509ExtendedKeyManager;
import javax.net.ssl.X509TrustManager;

import org.eclipse.tcf.Activator;
import org.eclipse.tcf.core.Base64;
import org.eclipse.tcf.protocol.Protocol;


/**
 * This class implements keys and certificates management for secure TCF channels.
 */
public class TCFSecurityManager {

    public static File getCertificatesDirectory() throws IOException {
        File certs;
        try {
            certs = Activator.getDefault().getStateLocation().append("certificates").toFile(); //$NON-NLS-1$
        }
        catch (IllegalStateException e) {
            // An RCP workspace-less environment (-data @none)
            certs = new File(System.getProperty("user.home"), ".tcf");
            certs = new File(certs, "certificates");
        }
        if (!certs.exists() && !certs.mkdirs()) throw new IOException("Cannot create directory: " + certs);
        return certs;
    }

    public static File getSysCertificatesDirectory() {
        File file = null;
        String osname = System.getProperty("os.name", "");
        if (osname.startsWith("Windows")) {
            Process prs = null;
            BufferedReader inp = null;
            try {
                String sys_root = "SystemRoot";
                prs = Runtime.getRuntime().exec(new String[]{ "cmd", "/c", "set", sys_root }, null);
                inp = new BufferedReader(new InputStreamReader(prs.getInputStream()));
                for (;;) {
                    String s = inp.readLine();
                    if (s == null) break;
                    int i = s.indexOf('=');
                    if (i > 0) {
                        String name = s.substring(0, i);
                        if (name.equalsIgnoreCase(sys_root)) {
                            File root = new File(s.substring(i + 1));
                            if (root.exists()) file = new File(root, "TCF/ssl");
                        }
                    }
                }
                try {
                    prs.getErrorStream().close();
                    prs.getOutputStream().close();
                    inp.close();
                }
                catch (IOException x) {
                    Protocol.log("Cannot close child process I/O streams", x); //$NON-NLS-1$
                }
                prs.waitFor();
            }
            catch (Throwable x) {
                Protocol.log("Cannot get system directory name", x); //$NON-NLS-1$
                try {
                    if (prs != null) {
                        prs.getErrorStream().close();
                        prs.getOutputStream().close();
                    }
                    if (inp != null) inp.close();
                }
                catch (IOException y) {
                }
            }
        }
        else {
            file = new File("/etc/tcf/ssl");
        }
        if (file == null) return null;
        if (!file.exists()) return null;
        if (!file.isDirectory()) return null;
        return file;
    }

    public static SSLContext createSSLContext() {
        try {
            final File usr_certs = getCertificatesDirectory();
            final File sys_certs = getSysCertificatesDirectory();
            if (!usr_certs.exists() && !usr_certs.mkdirs()) throw new Exception("Cannot create directory: " + usr_certs);
            final CertificateFactory cf = CertificateFactory.getInstance("X.509"); //$NON-NLS-1$
            SSLContext context = SSLContext.getInstance("TLS"); //$NON-NLS-1$

            X509ExtendedKeyManager km = new X509ExtendedKeyManager() {

                public X509Certificate[] getCertificateChain(String alias) {
                    File f = new File(usr_certs, "local.cert"); //$NON-NLS-1$
                    if (!f.exists() && sys_certs != null) f = new File(sys_certs, "local.cert"); //$NON-NLS-1$
                    InputStream inp = null;
                    try {
                        inp = new BufferedInputStream(new FileInputStream(f));
                        X509Certificate cert = (X509Certificate)cf.generateCertificate(inp);
                        inp.close();
                        return new X509Certificate[] { cert };
                    }
                    catch (Throwable x) {
                        Protocol.log("Cannot read certificate: " + f, x); //$NON-NLS-1$
                        try {
                            if (inp != null) inp.close();
                        }
                        catch (IOException e) {
                            Protocol.log("Cannot close certificate file: " + f, x); //$NON-NLS-1$
                        }
                        return null;
                    }
                }

                public PrivateKey getPrivateKey(String alias) {
                    File f = new File(usr_certs, "local.priv"); //$NON-NLS-1$
                    if (!f.exists() && sys_certs != null) f = new File(sys_certs, "local.priv"); //$NON-NLS-1$
                    BufferedReader r = null;
                    try {
                        r = new BufferedReader(new InputStreamReader(new FileInputStream(f), "ASCII")); //$NON-NLS-1$
                        StringBuffer bf = new StringBuffer();
                        boolean app = false;
                        for (;;) {
                            String s = r.readLine();
                            if (s == null) throw new Exception("Invalid format"); //$NON-NLS-1$
                            else if (s.indexOf("-----BEGIN ") == 0) app = true; //$NON-NLS-1$
                            else if (s.indexOf("-----END ") == 0) break; //$NON-NLS-1$
                            else if (app) bf.append(s);
                        }
                        KeyFactory kf = KeyFactory.getInstance("RSA"); //$NON-NLS-1$
                        byte[] bytes = Base64.toByteArray(bf.toString().toCharArray());
                        return kf.generatePrivate(new PKCS8EncodedKeySpec(bytes));
                    }
                    catch (Exception x) {
                        Protocol.log("Cannot read private key: " + f, x); //$NON-NLS-1$
                        try {
                            if (r != null) r.close();
                        }
                        catch (IOException e) {
                            Protocol.log("Cannot close private key file: " + f, x); //$NON-NLS-1$
                        }
                        return null;
                    } finally {
                        if (r != null) try { r.close(); } catch (IOException e) {}
                    }
                }

                public String[] getClientAliases(String keyType, Principal[] issuers) {
                    return new String[] { "TCF" }; //$NON-NLS-1$
                }

                public String chooseClientAlias(String[] keyType, Principal[] issuers, Socket socket) {
                    return "TCF"; //$NON-NLS-1$
                }

                public String[] getServerAliases(String keyType, Principal[] issuers) {
                    return new String[] { "TCF" }; //$NON-NLS-1$
                }

                public String chooseServerAlias(String keyType, Principal[] issuers, Socket socket) {
                    return "TCF"; //$NON-NLS-1$
                }
            };

            X509TrustManager tm = new X509TrustManager() {

                public void checkClientTrusted(X509Certificate[] chain, String auth_type) throws CertificateException {
                    if ("RSA".equals(auth_type) && chain != null && chain.length == 1) { //$NON-NLS-1$
                        for (X509Certificate cert : getAcceptedIssuers()) {
                            if (cert.equals(chain[0])) return;
                        }
                    }
                    throw new CertificateException("Client certificate validation failed"); //$NON-NLS-1$
                }

                public void checkServerTrusted(X509Certificate[] chain, String auth_type) throws CertificateException {
                    if ("RSA".equals(auth_type) && chain != null && chain.length == 1) { //$NON-NLS-1$
                        for (X509Certificate cert : getAcceptedIssuers()) {
                            if (cert.equals(chain[0])) return;
                        }
                    }
                    throw new CertificateException("Server certificate validation failed"); //$NON-NLS-1$
                }

                public X509Certificate[] getAcceptedIssuers() {
                    ArrayList<X509Certificate> list = new ArrayList<X509Certificate>();
                    for (String fnm : usr_certs.list()) {
                        if (!fnm.endsWith(".cert")) continue; //$NON-NLS-1$
                        InputStream inp = null;
                        try {
                            inp = new BufferedInputStream(new FileInputStream(new File(usr_certs, fnm)));
                            X509Certificate cert = (X509Certificate)cf.generateCertificate(inp);
                            inp.close();
                            list.add(cert);
                        }
                        catch (Throwable x) {
                            Protocol.log("Cannot load certificate: " + fnm, x); //$NON-NLS-1$
                            try {
                                if (inp != null) inp.close();
                            }
                            catch (IOException e) {
                                Protocol.log("Cannot close certificate file: " + fnm, x); //$NON-NLS-1$
                            }
                        }
                    }
                    if (sys_certs != null) {
                        String[] arr = sys_certs.list();
                        if (arr != null) {
                            for (String fnm : arr) {
                                if (!fnm.endsWith(".cert")) continue; //$NON-NLS-1$
                                InputStream inp = null;
                                try {
                                    inp = new BufferedInputStream(new FileInputStream(new File(sys_certs, fnm)));
                                    X509Certificate cert = (X509Certificate)cf.generateCertificate(inp);
                                    inp.close();
                                    list.add(cert);
                                }
                                catch (Throwable x) {
                                    Protocol.log("Cannot load certificate: " + fnm, x); //$NON-NLS-1$
                                    try {
                                        if (inp != null) inp.close();
                                    }
                                    catch (IOException e) {
                                        Protocol.log("Cannot close certificate file: " + fnm, x); //$NON-NLS-1$
                                    }
                                }
                            }
                        }
                    }
                    return list.toArray(new X509Certificate[list.size()]);
                }
            };

            context.init(new KeyManager[] { km }, new TrustManager[] { tm }, null);
            return context;
        }
        catch (Throwable x) {
            Protocol.log("Cannot initialize SSL context", x); //$NON-NLS-1$
            return null;
        }
    }
}

Back to the top