summaryrefslogtreecommitdiff
path: root/java/amqp-1-0-client/src/main/java/org/apache/qpid/amqp_1_0/client/SSLUtil.java
blob: 70e5d08f1535f78e90a29f17a874e1594fe32e6f (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
/*
 *
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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.
 *
 */
package org.apache.qpid.amqp_1_0.client;

import javax.net.ssl.KeyManager;
import javax.net.ssl.KeyManagerFactory;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLEngine;
import javax.net.ssl.TrustManager;
import javax.net.ssl.TrustManagerFactory;
import javax.net.ssl.X509ExtendedKeyManager;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.Socket;
import java.security.GeneralSecurityException;
import java.security.KeyStore;
import java.security.Principal;
import java.security.PrivateKey;
import java.security.cert.X509Certificate;

public class SSLUtil
{
    public static final String TRANSPORT_LAYER_SECURITY_CODE = "TLS";

    public static SSLContext buildSslContext(final String certAlias,
                                             final String keyStorePath,
                                             final String keyStoreType,
                                             final String keyStorePassword,
                                             final String keyManagerFactoryAlgorithm,
                                             final String trustStorePath,
                                             final String trustStorePassword,
                                             final String trustStoreType,
                                             final String trustManagerFactoryAlgorithm) throws GeneralSecurityException, IOException
    {

        final SSLContext sslContext = SSLContext
                .getInstance(TRANSPORT_LAYER_SECURITY_CODE);

        final TrustManager[] trustManagers;
        final KeyManager[] keyManagers;

        if (trustStorePath != null)
        {
            final KeyStore ts = getInitializedKeyStore(trustStorePath, trustStorePassword, trustStoreType);
            final TrustManagerFactory tmf = TrustManagerFactory.getInstance(trustManagerFactoryAlgorithm);

            tmf.init(ts);

            trustManagers = tmf.getTrustManagers();
        }
        else
        {
            trustManagers = null;
        }

        if (keyStorePath != null)
        {
            if (certAlias != null)
            {
                keyManagers = new KeyManager[] { new QpidClientX509KeyManager(
                        certAlias, keyStorePath, keyStoreType, keyStorePassword,
                        keyManagerFactoryAlgorithm) };
            }
            else
            {
                final KeyStore ks = SSLUtil.getInitializedKeyStore(keyStorePath, keyStorePassword, keyStoreType);

                char[] keyStoreCharPassword = keyStorePassword == null ? null : keyStorePassword.toCharArray();
                // Set up key manager factory to use our key store
                final KeyManagerFactory kmf = KeyManagerFactory.getInstance(keyManagerFactoryAlgorithm);
                kmf.init(ks, keyStoreCharPassword);
                keyManagers = kmf.getKeyManagers();
            }
        }
        else
        {
            keyManagers = null;
        }


        sslContext.init(keyManagers, trustManagers, null);

        return sslContext;
    }

    public static X509Certificate[] getClientCertificates(final String alias,
                                                final String keyStorePath,
                                                final String keyStorePassword,
                                                final String keyStoreType,
                                                final String keyManagerFactoryAlgorithm)
            throws GeneralSecurityException, IOException
    {
        return (new QpidClientX509KeyManager(alias,keyStorePath,keyStoreType,keyStorePassword,keyManagerFactoryAlgorithm)).getCertificateChain(alias);
    }

    public static KeyStore getInitializedKeyStore(String storePath, String storePassword, String keyStoreType) throws GeneralSecurityException, IOException
    {
        KeyStore ks = KeyStore.getInstance(keyStoreType);
        InputStream in = null;
        try
        {
            File f = new File(storePath);
            if (f.exists())
            {
                in = new FileInputStream(f);
            }
            else
            {
                in = Thread.currentThread().getContextClassLoader().getResourceAsStream(storePath);
            }
            if (in == null && !"PKCS11".equalsIgnoreCase(keyStoreType)) // PKCS11 will not require an explicit path
            {
                throw new IOException("Unable to load keystore resource: " + storePath);
            }

            char[] storeCharPassword = storePassword == null ? null : storePassword.toCharArray();

            ks.load(in, storeCharPassword);
        }
        finally
        {
            if (in != null)
            {
                //noinspection EmptyCatchBlock
                try
                {
                    in.close();
                }
                catch (IOException ignored)
                {
                }
            }
        }
        return ks;
    }

    public static class QpidClientX509KeyManager extends X509ExtendedKeyManager
    {

        private X509ExtendedKeyManager delegate;
        private String alias;

        public QpidClientX509KeyManager(String alias, String keyStorePath, String keyStoreType,
                                        String keyStorePassword, String keyManagerFactoryAlgorithmName) throws
                                                                                                        GeneralSecurityException,
                                                                                                        IOException
        {
            this.alias = alias;
            KeyStore ks = getInitializedKeyStore(keyStorePath, keyStorePassword, keyStoreType);
            KeyManagerFactory kmf = KeyManagerFactory.getInstance(keyManagerFactoryAlgorithmName);
            kmf.init(ks, keyStorePassword.toCharArray());
            this.delegate = (X509ExtendedKeyManager) kmf.getKeyManagers()[0];
        }

        public String chooseClientAlias(String[] keyType, Principal[] issuers, Socket socket)
        {
            return alias;
        }

        public String chooseServerAlias(String keyType, Principal[] issuers, Socket socket)
        {
            return delegate.chooseServerAlias(keyType, issuers, socket);
        }

        public X509Certificate[] getCertificateChain(String alias)
        {
            return delegate.getCertificateChain(alias);
        }

        public String[] getClientAliases(String keyType, Principal[] issuers)
        {
            return new String[]{alias};
        }

        public PrivateKey getPrivateKey(String alias)
        {
            return delegate.getPrivateKey(alias);
        }

        public String[] getServerAliases(String keyType, Principal[] issuers)
        {
            return delegate.getServerAliases(keyType, issuers);
        }

        public String chooseEngineClientAlias(String[] keyType, Principal[] issuers, SSLEngine engine)
        {
            return alias;
        }

        public String chooseEngineServerAlias(String keyType, Principal[] issuers, SSLEngine engine)
        {
            return delegate.chooseEngineServerAlias(keyType, issuers, engine);
        }
    }
}