1+ import java .io .IOException ;
2+ import javax .net .ssl .HostnameVerifier ;
3+ import javax .net .ssl .SSLException ;
4+ import javax .net .ssl .SSLSession ;
5+ import javax .net .ssl .SSLSocket ;
6+ import javax .net .ssl .SSLSocketFactory ;
7+
8+ public class IgnoredHostnameVerification {
9+
10+ // BAD: ignored result of HostnameVerifier.verify()
11+ public static SSLSocket connectWithIgnoredHostnameVerification (
12+ String host , int port , HostnameVerifier verifier ) throws IOException {
13+
14+ SSLSocket socket = (SSLSocket ) SSLSocketFactory .getDefault ().createSocket (host , port );
15+ socket .startHandshake ();
16+ verifier .verify (host , socket .getSession ());
17+ return socket ;
18+ }
19+
20+ public static void check (boolean result ) throws SSLException {
21+ if (!result ) {
22+ throw new SSLException ("Oops! Hostname verification failed!" );
23+ }
24+ }
25+
26+ // GOOD: connect and check result of HostnameVerifier.verify()
27+ public static SSLSocket connectWithHostnameVerification00 (
28+ String host , int port , HostnameVerifier verifier ) throws IOException {
29+
30+ SSLSocket socket = (SSLSocket ) SSLSocketFactory .getDefault ().createSocket (host , port );
31+ socket .startHandshake ();
32+ check (verifier .verify (host , socket .getSession ()));
33+ return socket ;
34+ }
35+
36+ // GOOD: connect and check result of HostnameVerifier.verify()
37+ public static SSLSocket connectWithHostnameVerification01 (
38+ String host , int port , HostnameVerifier verifier ) throws IOException {
39+
40+ SSLSocket socket = (SSLSocket ) SSLSocketFactory .getDefault ().createSocket (host , port );
41+ socket .startHandshake ();
42+ boolean successful = verifier .verify (host , socket .getSession ());
43+ if (successful == false ) {
44+ socket .close ();
45+ throw new SSLException ("Oops! Hostname verification failed!" );
46+ }
47+
48+ return socket ;
49+ }
50+
51+ // GOOD: connect and check result of HostnameVerifier.verify()
52+ public static SSLSocket connectWithHostnameVerification02 (
53+ String host , int port , HostnameVerifier verifier ) throws IOException {
54+
55+ SSLSocket socket = (SSLSocket ) SSLSocketFactory .getDefault ().createSocket (host , port );
56+ socket .startHandshake ();
57+ boolean successful = false ;
58+ if (verifier != null ) {
59+ successful = verifier .verify (host , socket .getSession ());
60+ }
61+ if (!successful ) {
62+ socket .close ();
63+ throw new SSLException ("Oops! Hostname verification failed!" );
64+ }
65+
66+ return socket ;
67+ }
68+
69+ // GOOD: connect and check result of HostnameVerifier.verify()
70+ public static SSLSocket connectWithHostnameVerification03 (
71+ String host , int port , HostnameVerifier verifier ) throws IOException {
72+
73+ SSLSocket socket = (SSLSocket ) SSLSocketFactory .getDefault ().createSocket (host , port );
74+ socket .startHandshake ();
75+ boolean successful = verifier .verify (host , socket .getSession ());
76+ if (successful ) {
77+ return socket ;
78+ }
79+
80+ socket .close ();
81+ throw new SSLException ("Oops! Hostname verification failed!" );
82+ }
83+
84+ // GOOD: connect and check result of HostnameVerifier.verify()
85+ public static String connectWithHostnameVerification04 (
86+ String [] hosts , HostnameVerifier verifier , SSLSession session ) throws IOException {
87+
88+ for (String host : hosts ) {
89+ if (verifier .verify (host , session )) {
90+ return host ;
91+ }
92+ }
93+
94+ throw new SSLException ("Oops! Hostname verification failed!" );
95+ }
96+
97+ public static class HostnameVerifierWrapper implements HostnameVerifier {
98+
99+ private final HostnameVerifier verifier ;
100+
101+ public HostnameVerifierWrapper (HostnameVerifier verifier ) {
102+ this .verifier = verifier ;
103+ }
104+
105+ @ Override
106+ public boolean verify (String hostname , SSLSession session ) {
107+ return verifier .verify (hostname , session ); // GOOD: wrapped calls should not be reported
108+ }
109+
110+ }
111+
112+ }
0 commit comments