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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
|
// **********************************************************************
//
// 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 Instance
{
Instance(Ice.Communicator communicator)
{
_logger = communicator.getLogger();
_facade = Ice.Util.getProtocolPluginFacade(communicator);
_securityTraceLevel = communicator.getProperties().getPropertyAsIntWithDefault("IceSSL.Trace.Security", 0);
_securityTraceCategory = "Security";
_initialized = false;
//
// Register the endpoint factory. We have to do this now, rather than
// in initialize, because the communicator may need to interpret
// proxies before the plugin is fully initialized.
//
_facade.addEndpointFactory(new EndpointFactoryI(this));
}
void
initialize()
{
if(_initialized)
{
return;
}
final String prefix = "IceSSL.";
Ice.Properties properties = communicator().getProperties();
//
// Parse the cipher list.
//
String ciphers = properties.getProperty(prefix + "Ciphers");
if(ciphers.length() > 0)
{
parseCiphers(ciphers);
}
//
// Select protocols.
//
String protocols = properties.getProperty(prefix + "Protocols");
if(protocols.length() > 0)
{
java.util.ArrayList l = new java.util.ArrayList();
String[] arr = protocols.split("[ \t,]+");
for(int i = 0; i < arr.length; ++i)
{
String s = arr[i].toLowerCase();
if(s.equals("ssl3") || s.equals("sslv3"))
{
l.add("SSLv3");
}
else if(s.equals("tls") || s.equals("tls1") || s.equals("tlsv1"))
{
l.add("TLSv1");
}
else
{
Ice.PluginInitializationException e = new Ice.PluginInitializationException();
e.reason = "IceSSL: unrecognized protocol `" + arr[i] + "'";
throw e;
}
}
_protocols = new String[l.size()];
l.toArray(_protocols);
}
//
// If the user doesn't supply an SSLContext, we need to create one based
// on property settings.
//
if(_context == null)
{
try
{
//
// We need a SecureRandom object.
//
java.security.SecureRandom rand = java.security.SecureRandom.getInstance("SHA1PRNG");
//
// We call nextInt() in order to force the object to perform any time-consuming
// initialization tasks now.
//
rand.nextInt();
//
// Check for a default directory. We look in this directory for
// files mentioned in the configuration.
//
_defaultDir = properties.getProperty(prefix + "DefaultDir");
//
// The keystore holds private keys and associated certificates.
//
Ice.StringHolder keystorePath = new Ice.StringHolder(properties.getProperty(prefix + "Keystore"));
//
// The password for the keys.
//
final String password = properties.getProperty(prefix + "Password");
//
// The password for the keystore.
//
final String keystorePassword = properties.getProperty(prefix + "KeystorePassword");
//
// The default keystore type value is "JKS", but it can also be "PKCS12".
//
final String defaultType = java.security.KeyStore.getDefaultType();
final String keystoreType = properties.getPropertyWithDefault(prefix + "KeystoreType", defaultType);
//
// The alias of the key to use in authentication.
//
final String alias = properties.getProperty(prefix + "Alias");
//
// The truststore holds the certificates of trusted CAs.
//
Ice.StringHolder truststorePath = new Ice.StringHolder(properties.getProperty(prefix + "Truststore"));
//
// The password for the truststore.
//
final String truststorePassword = properties.getProperty(prefix + "TruststorePassword");
//
// The truststore type defaults to "JKS", but it can also be "PKCS12".
//
String truststoreType = properties.getPropertyWithDefault(prefix + "TruststoreType",
java.security.KeyStore.getDefaultType());
//
// Collect the key managers.
//
javax.net.ssl.KeyManager[] keyManagers = null;
if(keystorePath.value.length() > 0)
{
if(!checkPath(keystorePath, false))
{
Ice.PluginInitializationException e = new Ice.PluginInitializationException();
e.reason = "IceSSL: keystore file not found:\n" + keystorePath.value;
throw e;
}
java.security.KeyStore keys = java.security.KeyStore.getInstance(keystoreType);
try
{
char[] passwordChars = null;
if(keystorePassword.length() > 0)
{
passwordChars = keystorePassword.toCharArray();
}
java.io.BufferedInputStream bis =
new java.io.BufferedInputStream(new java.io.FileInputStream(keystorePath.value));
keys.load(bis, passwordChars);
}
catch(java.io.IOException ex)
{
Ice.PluginInitializationException e = new Ice.PluginInitializationException();
e.reason = "IceSSL: unable to load keystore:\n" + keystorePath.value;
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();
//
// If the user selected a specific alias, we need to wrap the key managers
// in order to return the desired alias.
//
if(alias.length() > 0)
{
if(!keys.isKeyEntry(alias))
{
Ice.PluginInitializationException e = new Ice.PluginInitializationException();
e.reason = "IceSSL: keystore does not contain an entry with alias `" + alias + "'";
throw e;
}
for(int i = 0; i < keyManagers.length; ++i)
{
keyManagers[i] = new KeyManagerI((javax.net.ssl.X509KeyManager)keyManagers[i], alias);
}
}
}
//
// Collect the trust managers.
//
javax.net.ssl.TrustManager[] trustManagers = null;
if(truststorePath.value.length() > 0)
{
if(!checkPath(truststorePath, false))
{
Ice.PluginInitializationException e = new Ice.PluginInitializationException();
e.reason = "IceSSL: truststore file not found:\n" + truststorePath.value;
throw e;
}
java.security.KeyStore ts = java.security.KeyStore.getInstance(truststoreType);
try
{
char[] passwordChars = null;
if(truststorePassword.length() > 0)
{
passwordChars = truststorePassword.toCharArray();
}
java.io.BufferedInputStream bis =
new java.io.BufferedInputStream(new java.io.FileInputStream(truststorePath.value));
ts.load(bis, passwordChars);
}
catch(java.io.IOException ex)
{
Ice.PluginInitializationException e = new Ice.PluginInitializationException();
e.reason = "IceSSL: unable to load truststore:\n" + truststorePath.value;
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(ts);
trustManagers = tmf.getTrustManagers();
}
//
// Initialize the SSL context.
//
_context = javax.net.ssl.SSLContext.getInstance("SSL");
_context.init(keyManagers, trustManagers, rand);
}
catch(java.security.GeneralSecurityException ex)
{
Ice.PluginInitializationException e = new Ice.PluginInitializationException();
e.reason = "IceSSL: unable to initialize context";
e.initCause(ex);
throw e;
}
}
_initialized = true;
}
void
context(javax.net.ssl.SSLContext context)
{
if(_initialized)
{
Ice.PluginInitializationException ex = new Ice.PluginInitializationException();
ex.reason = "IceSSL: plugin is already initialized";
throw ex;
}
_context = context;
}
javax.net.ssl.SSLContext
context()
{
return _context;
}
void
setCertificateVerifier(CertificateVerifier verifier)
{
_verifier = verifier;
}
Ice.Communicator
communicator()
{
return _facade.getCommunicator();
}
String
defaultHost()
{
return _facade.getDefaultHost();
}
int
networkTraceLevel()
{
return _facade.getNetworkTraceLevel();
}
String
networkTraceCategory()
{
return _facade.getNetworkTraceCategory();
}
int
securityTraceLevel()
{
return _securityTraceLevel;
}
String
securityTraceCategory()
{
return _securityTraceCategory;
}
boolean
initialized()
{
return _initialized;
}
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;
}
String[]
protocols()
{
return _protocols;
}
void
traceConnection(javax.net.ssl.SSLSocket fd, boolean incoming)
{
javax.net.ssl.SSLSession session = fd.getSession();
String msg = "SSL summary for " + (incoming ? "incoming" : "outgoing") + " connection\n" +
"cipher = " + session.getCipherSuite() + "\n" +
"protocol = " + session.getProtocol() + "\n" +
IceInternal.Network.fdToString(fd);
_logger.trace(_securityTraceCategory, msg);
}
boolean
verifyPeer(javax.net.ssl.SSLSocket fd, String host, boolean incoming)
{
CertificateVerifier verifier = _verifier;
if(verifier != null)
{
VerifyInfo info = new VerifyInfo();
info.incoming = incoming;
try
{
info.certs = fd.getSession().getPeerCertificates();
}
catch(javax.net.ssl.SSLPeerUnverifiedException ex)
{
// No peer certificates.
}
info.socket = fd;
info.address = host;
if(!verifier.verify(info))
{
if(_securityTraceLevel > 0)
{
_logger.trace(_securityTraceCategory,
(incoming ? "incoming" : "outgoing") +
" connection rejected by certificate verifier\n" +
IceInternal.Network.fdToString(fd));
}
return false;
}
}
return true;
}
private void
parseCiphers(String ciphers)
{
java.util.ArrayList cipherList = new java.util.ArrayList();
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);
}
private boolean
checkPath(Ice.StringHolder path, boolean dir)
{
//
// Check if file exists. If not, try prepending the default
// directory and check again. If the file is found, the
// string argument is modified and true is returned. Otherwise
// false is returned.
//
java.io.File f = new java.io.File(path.value);
if(f.exists())
{
return dir ? f.isDirectory() : f.isFile();
}
if(_defaultDir.length() > 0)
{
String s = _defaultDir + java.io.File.separator + path.value;
f = new java.io.File(s);
if(f.exists() && ((!dir && f.isFile()) || (dir && f.isDirectory())))
{
path.value = s;
return true;
}
}
return false;
}
private static class CipherExpression
{
boolean not;
String cipher;
java.util.regex.Pattern re;
}
private Ice.Logger _logger;
private IceInternal.ProtocolPluginFacade _facade;
private int _securityTraceLevel;
private String _securityTraceCategory;
private boolean _initialized;
private javax.net.ssl.SSLContext _context;
private String _defaultDir;
private CipherExpression[] _ciphers;
private boolean _allCiphers;
private boolean _noCiphers;
private String[] _protocols;
private CertificateVerifier _verifier;
}
|