Java Reference
In-Depth Information
Arrays
.
fill
(
password
,
'0'
);
SSLServerSocketFactory
factory
=
context
.
getServerSocketFactory
();
SSLServerSocket
server
=
(
SSLServerSocket
)
factory
.
createServerSocket
(
PORT
);
// add anonymous (non-authenticated) cipher suites
String
[]
supported
=
server
.
getSupportedCipherSuites
();
String
[]
anonCipherSuitesSupported
=
new
String
[
supported
.
length
];
int
numAnonCipherSuitesSupported
=
0
;
for
(
int
i
=
0
;
i
<
supported
.
length
;
i
++)
{
if
(
supported
[
i
].
indexOf
(
"_anon_"
)
>
0
)
{
anonCipherSuitesSupported
[
numAnonCipherSuitesSupported
++]
=
supported
[
i
];
}
}
String
[]
oldEnabled
=
server
.
getEnabledCipherSuites
();
String
[]
newEnabled
=
new
String
[
oldEnabled
.
length
+
numAnonCipherSuitesSupported
];
System
.
arraycopy
(
oldEnabled
,
0
,
newEnabled
,
0
,
oldEnabled
.
length
);
System
.
arraycopy
(
anonCipherSuitesSupported
,
0
,
newEnabled
,
oldEnabled
.
length
,
numAnonCipherSuitesSupported
);
server
.
setEnabledCipherSuites
(
newEnabled
);
// Now all the set up is complete and we can focus
// on the actual communication.
while
(
true
)
{
// This socket will be secure,
// but there's no indication of that in the code!
try
(
Socket
theConnection
=
server
.
accept
())
{
InputStream
in
=
theConnection
.
getInputStream
();
int
c
;
while
((
c
=
in
.
read
())
!=
-
1
)
{
System
.
out
.
write
(
c
);
}
}
catch
(
IOException
ex
)
{
ex
.
printStackTrace
();
}
}
}
catch
(
IOException
|
KeyManagementException
|
KeyStoreException
|
NoSuchAlgorithmException
|
CertificateException
|
UnrecoverableKeyException
ex
)
{
ex
.
printStackTrace
();
}
}
}