summaryrefslogtreecommitdiff
path: root/java/src/IceSSL/Context.java
blob: 5c99702d5e9f711333cbc723d45f72e0055acca3 (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
// **********************************************************************
//
// Copyright (c) 2003-2006 ZeroC, Inc. All rights reserved.
//
// This copy of Ice is licensed to you under the terms described in the
// ICE_LICENSE file included in this distribution.
//
// **********************************************************************

package IceSSL;

class Context
{
    Context(String ciphers, String keystore, String password, String keystorePassword, String certs,
	    String certsPassword, java.security.SecureRandom rand)
	throws java.security.GeneralSecurityException
    {
	java.util.ArrayList cipherList = new java.util.ArrayList();
	if(ciphers.length() > 0)
	{
	    String[] expr = ciphers.split("[ \t]+");
	    for(int i = 0; i < expr.length; ++i)
	    {
		if(expr[i].equals("ALL"))
		{
		    if(i != 0)
		    {
			Ice.PluginInitializationException ex = new Ice.PluginInitializationException();
			ex.reason = "IceSSL: `ALL' must be first in cipher list `" + ciphers + "'";
			throw ex;
		    }
		    _allCiphers = true;
		}
		else if(expr[i].equals("NONE"))
		{
		    if(i != 0)
		    {
			Ice.PluginInitializationException ex = new Ice.PluginInitializationException();
			ex.reason = "IceSSL: `NONE' must be first in cipher list `" + ciphers + "'";
			throw ex;
		    }
		    _noCiphers = true;
		}
		else
		{
		    CipherExpression ce = new CipherExpression();
		    String exp = expr[i];
		    if(exp.charAt(0) == '!')
		    {
			ce.not = true;
			if(exp.length() > 1)
			{
			    exp = exp.substring(1);
			}
			else
			{
			    Ice.PluginInitializationException ex = new Ice.PluginInitializationException();
			    ex.reason = "IceSSL: invalid cipher expression `" + exp + "'";
			    throw ex;
			}
		    }

		    if(exp.charAt(0) == '(')
		    {
			if(!exp.endsWith(")"))
			{
			    Ice.PluginInitializationException ex = new Ice.PluginInitializationException();
			    ex.reason = "IceSSL: invalid cipher expression `" + exp + "'";
			    throw ex;
			}

			try
			{
			    ce.re = java.util.regex.Pattern.compile(exp.substring(1, exp.length() - 2));
			}
			catch(java.util.regex.PatternSyntaxException ex)
			{
			    Ice.PluginInitializationException e = new Ice.PluginInitializationException();
			    e.reason = "IceSSL: invalid cipher expression `" + exp + "'";
			    e.initCause(ex);
			    throw e;
			}
		    }
		    else
		    {
			ce.cipher = exp;
		    }

		    cipherList.add(ce);
		}
	    }
	    _ciphers = new CipherExpression[cipherList.size()];
	    cipherList.toArray(_ciphers);
	}

	final String ksType = java.security.KeyStore.getDefaultType();

	javax.net.ssl.KeyManager[] keyManagers = null;
	if(keystore != null && keystore.length() > 0)
	{
	    _keys = java.security.KeyStore.getInstance(ksType);
	    try
	    {
		char[] pass = null;
		if(keystorePassword != null && keystorePassword.length() > 0)
		{
		    pass = keystorePassword.toCharArray();
		}

		java.io.BufferedInputStream bis =
		    new java.io.BufferedInputStream(new java.io.FileInputStream(keystore));
		_keys.load(bis, pass);
	    }
	    catch(java.io.IOException ex)
	    {
		Ice.PluginInitializationException e = new Ice.PluginInitializationException();
		e.reason = "IceSSL: unable to load keystore from `" + keystore + "'";
		e.initCause(ex);
		throw e;
	    }

	    String algorithm = javax.net.ssl.KeyManagerFactory.getDefaultAlgorithm();
	    javax.net.ssl.KeyManagerFactory kmf = javax.net.ssl.KeyManagerFactory.getInstance(algorithm);
	    kmf.init(_keys, password.toCharArray());
	    keyManagers = kmf.getKeyManagers();
	}

	javax.net.ssl.TrustManager[] trustManagers = null;
	if(certs != null && certs.length() > 0)
	{
	    _certs = java.security.KeyStore.getInstance(ksType);
	    try
	    {
		char[] pass = null;
		if(certsPassword != null && certsPassword.length() > 0)
		{
		    pass = certsPassword.toCharArray();
		}

		java.io.BufferedInputStream bis =
		    new java.io.BufferedInputStream(new java.io.FileInputStream(certs));
		_certs.load(bis, pass);
	    }
	    catch(java.io.IOException ex)
	    {
		Ice.PluginInitializationException e = new Ice.PluginInitializationException();
		e.reason = "IceSSL: unable to load keystore from `" + certs + "'";
		e.initCause(ex);
		throw e;
	    }

	    String algorithm = javax.net.ssl.TrustManagerFactory.getDefaultAlgorithm();
	    javax.net.ssl.TrustManagerFactory tmf = javax.net.ssl.TrustManagerFactory.getInstance(algorithm);
	    tmf.init(_certs);
	    trustManagers = tmf.getTrustManagers();
	}

	_ctx = javax.net.ssl.SSLContext.getInstance("SSL");
	_ctx.init(keyManagers, trustManagers, rand);
    }

    javax.net.ssl.SSLContext
    sslContext()
    {
	return _ctx;
    }

    String[]
    filterCiphers(String[] supportedCiphers, String[] defaultCiphers)
    {
	java.util.LinkedList result = new java.util.LinkedList();
	if(_allCiphers)
	{
	    for(int i = 0; i < supportedCiphers.length; ++i)
	    {
		result.add(supportedCiphers[i]);
	    }
	}
	else if(!_noCiphers)
	{
	    for(int i = 0; i < defaultCiphers.length; ++i)
	    {
		result.add(defaultCiphers[i]);
	    }
	}

	if(_ciphers != null)
	{
	    for(int i = 0; i < _ciphers.length; ++i)
	    {
		CipherExpression ce = (CipherExpression)_ciphers[i];
		if(ce.not)
		{
		    java.util.Iterator e = result.iterator();
		    while(e.hasNext())
		    {
			String cipher = (String)e.next();
			if(ce.cipher != null)
			{
			    if(ce.cipher.equals(cipher))
			    {
				e.remove();
			    }
			}
			else
			{
			    assert(ce.re != null);
			    java.util.regex.Matcher m = ce.re.matcher(cipher);
			    if(m.find())
			    {
				e.remove();
			    }
			}
		    }
		}
		else
		{
		    if(ce.cipher != null)
		    {
			result.add(0, ce.cipher);
		    }
		    else
		    {
			assert(ce.re != null);
			for(int j = 0; j < supportedCiphers.length; ++j)
			{
			    java.util.regex.Matcher m = ce.re.matcher(supportedCiphers[j]);
			    if(m.find())
			    {
				result.add(0, supportedCiphers[j]);
			    }
			}
		    }
		}
	    }
	}

	String[] arr = new String[result.size()];
	result.toArray(arr);
	return arr;
    }

    private static class CipherExpression
    {
	boolean not;
	String cipher;
	java.util.regex.Pattern re;
    }

    private CipherExpression[] _ciphers;
    private boolean _allCiphers;
    private boolean _noCiphers;
    private javax.net.ssl.SSLContext _ctx;
    private java.security.KeyStore _keys;
    private java.security.KeyStore _certs;
}