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 package org.eclipse.jgit.internal.transport.sshd;
44
45 import java.net.InetAddress;
46 import java.net.InetSocketAddress;
47 import java.net.UnknownHostException;
48 import java.util.Collection;
49 import java.util.Collections;
50 import java.util.LinkedHashMap;
51 import java.util.Map;
52 import java.util.concurrent.atomic.AtomicBoolean;
53
54 import org.eclipse.jgit.annotations.NonNull;
55 import org.ietf.jgss.GSSContext;
56 import org.ietf.jgss.GSSException;
57 import org.ietf.jgss.GSSManager;
58 import org.ietf.jgss.GSSName;
59 import org.ietf.jgss.Oid;
60
61
62
63
64 public class GssApiMechanisms {
65
66 private GssApiMechanisms() {
67
68 }
69
70
71 public static final String GSSAPI_HOST_PREFIX = "host@";
72
73
74 public static final Oid KERBEROS_5 = createOid("1.2.840.113554.1.2.2");
75
76
77 public static final Oid SPNEGO = createOid("1.3.6.1.5.5.2");
78
79
80 private static final Object LOCK = new Object();
81
82
83
84
85
86 private static Map<Oid, Boolean> supportedMechanisms;
87
88
89
90
91
92
93 @NonNull
94 public static Collection<Oid> getSupportedMechanisms() {
95 synchronized (LOCK) {
96 if (supportedMechanisms == null) {
97 GSSManager manager = GSSManager.getInstance();
98 Oid[] mechs = manager.getMechs();
99 Map<Oid, Boolean> mechanisms = new LinkedHashMap<>();
100 if (mechs != null) {
101 for (Oid oid : mechs) {
102 mechanisms.put(oid, Boolean.FALSE);
103 }
104 }
105 supportedMechanisms = mechanisms;
106 }
107 return Collections.unmodifiableSet(supportedMechanisms.keySet());
108 }
109 }
110
111
112
113
114
115
116
117 public static void worked(@NonNull Oid mechanism) {
118 synchronized (LOCK) {
119 supportedMechanisms.put(mechanism, Boolean.TRUE);
120 }
121 }
122
123
124
125
126
127
128
129 public static void failed(@NonNull Oid mechanism) {
130 synchronized (LOCK) {
131 Boolean worked = supportedMechanisms.get(mechanism);
132 if (worked != null && !worked.booleanValue()) {
133
134 supportedMechanisms.remove(mechanism);
135 }
136 }
137 }
138
139
140
141
142
143
144
145
146 public static InetAddress resolve(@NonNull InetSocketAddress remote) {
147 InetAddress address = remote.getAddress();
148 if (address == null) {
149 try {
150 address = InetAddress.getByName(remote.getHostString());
151 } catch (UnknownHostException e) {
152 return null;
153 }
154 }
155 return address;
156 }
157
158
159
160
161
162
163
164
165
166 @NonNull
167 public static String getCanonicalName(@NonNull InetSocketAddress remote) {
168 InetAddress address = resolve(remote);
169 if (address == null) {
170 return remote.getHostString();
171 }
172 return address.getCanonicalHostName();
173 }
174
175
176
177
178
179
180
181
182
183
184
185
186 public static GSSContext createContext(@NonNull Oid mechanism,
187 @NonNull String fqdn) {
188 GSSContext context = null;
189 try {
190 GSSManager manager = GSSManager.getInstance();
191 context = manager.createContext(
192 manager.createName(
193 GssApiMechanisms.GSSAPI_HOST_PREFIX + fqdn,
194 GSSName.NT_HOSTBASED_SERVICE),
195 mechanism, null, GSSContext.DEFAULT_LIFETIME);
196 } catch (GSSException e) {
197 closeContextSilently(context);
198 failed(mechanism);
199 return null;
200 }
201 worked(mechanism);
202 return context;
203 }
204
205
206
207
208
209
210
211
212 public static void closeContextSilently(GSSContext context) {
213 if (context != null) {
214 try {
215 context.dispose();
216 } catch (GSSException e) {
217
218 }
219 }
220 }
221
222 private static Oid createOid(String rep) {
223 try {
224 return new Oid(rep);
225 } catch (GSSException e) {
226
227 return null;
228 }
229 }
230
231 }