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
|
/*
* Copyright (C) 2018, 2022 Thomas Wolf <thomas.wolf@paranor.ch> and others
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Distribution License v. 1.0 which is available at
* https://www.eclipse.org/org/documents/edl-v10.php.
*
* SPDX-License-Identifier: BSD-3-Clause
*/
package org.eclipse.jgit.internal.transport.sshd;
import static java.text.MessageFormat.format;
import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.net.UnknownHostException;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import org.apache.sshd.client.auth.AbstractUserAuth;
import org.apache.sshd.client.session.ClientSession;
import org.apache.sshd.common.SshConstants;
import org.apache.sshd.common.util.buffer.Buffer;
import org.apache.sshd.common.util.buffer.ByteArrayBuffer;
import org.ietf.jgss.GSSContext;
import org.ietf.jgss.GSSException;
import org.ietf.jgss.MessageProp;
import org.ietf.jgss.Oid;
/**
* GSSAPI-with-MIC authentication handler (Kerberos 5).
*
* @see <a href="https://tools.ietf.org/html/rfc4462">RFC 4462</a>
*/
public class GssApiWithMicAuthentication extends AbstractUserAuth {
/** Synonym used in RFC 4462. */
private static final byte SSH_MSG_USERAUTH_GSSAPI_RESPONSE = SshConstants.SSH_MSG_USERAUTH_INFO_REQUEST;
/** Synonym used in RFC 4462. */
private static final byte SSH_MSG_USERAUTH_GSSAPI_TOKEN = SshConstants.SSH_MSG_USERAUTH_INFO_RESPONSE;
private enum ProtocolState {
STARTED, TOKENS, MIC_SENT, FAILED
}
private Collection<Oid> mechanisms;
private Iterator<Oid> nextMechanism;
private Oid currentMechanism;
private ProtocolState state;
private GSSContext context;
/** Creates a new {@link GssApiWithMicAuthentication}. */
public GssApiWithMicAuthentication() {
super(GssApiWithMicAuthFactory.NAME);
}
@Override
protected boolean sendAuthDataRequest(ClientSession session, String service)
throws Exception {
if (mechanisms == null) {
mechanisms = GssApiMechanisms.getSupportedMechanisms();
nextMechanism = mechanisms.iterator();
}
if (context != null) {
close(false);
}
GssApiWithMicAuthenticationReporter reporter = session.getAttribute(
GssApiWithMicAuthenticationReporter.GSS_AUTHENTICATION_REPORTER);
if (!nextMechanism.hasNext()) {
reporter.signalAuthenticationExhausted(session, service);
return false;
}
state = ProtocolState.STARTED;
currentMechanism = nextMechanism.next();
// RFC 4462 states that SPNEGO must not be used with ssh
while (GssApiMechanisms.SPNEGO.equals(currentMechanism)) {
if (!nextMechanism.hasNext()) {
reporter.signalAuthenticationExhausted(session, service);
return false;
}
currentMechanism = nextMechanism.next();
}
try {
String hostName = getHostName(session);
context = GssApiMechanisms.createContext(currentMechanism,
hostName);
context.requestMutualAuth(true);
context.requestConf(true);
context.requestInteg(true);
context.requestCredDeleg(true);
context.requestAnonymity(false);
} catch (GSSException | NullPointerException e) {
close(true);
if (log.isDebugEnabled()) {
log.debug(format(SshdText.get().gssapiInitFailure,
currentMechanism.toString()));
}
currentMechanism = null;
state = ProtocolState.FAILED;
return false;
}
if (reporter != null) {
reporter.signalAuthenticationAttempt(session, service,
currentMechanism.toString());
}
Buffer buffer = session
.createBuffer(SshConstants.SSH_MSG_USERAUTH_REQUEST);
buffer.putString(session.getUsername());
buffer.putString(service);
buffer.putString(getName());
buffer.putInt(1);
buffer.putBytes(currentMechanism.getDER());
session.writePacket(buffer);
return true;
}
@Override
protected boolean processAuthDataRequest(ClientSession session,
String service, Buffer in) throws Exception {
// SSH_MSG_USERAUTH_FAILURE and SSH_MSG_USERAUTH_SUCCESS, as well as
// SSH_MSG_USERAUTH_BANNER are handled by the framework.
int command = in.getUByte();
if (context == null) {
return false;
}
try {
switch (command) {
case SSH_MSG_USERAUTH_GSSAPI_RESPONSE: {
if (state != ProtocolState.STARTED) {
return unexpectedMessage(command);
}
// Initial reply from the server with the mechanism to use.
Oid mechanism = new Oid(in.getBytes());
if (!currentMechanism.equals(mechanism)) {
return false;
}
replyToken(session, service, new byte[0]);
return true;
}
case SSH_MSG_USERAUTH_GSSAPI_TOKEN: {
if (context.isEstablished() || state != ProtocolState.TOKENS) {
return unexpectedMessage(command);
}
// Server sent us a token
replyToken(session, service, in.getBytes());
return true;
}
default:
return unexpectedMessage(command);
}
} catch (GSSException e) {
log.warn(format(SshdText.get().gssapiFailure,
currentMechanism.toString()), e);
state = ProtocolState.FAILED;
return false;
}
}
@Override
public void destroy() {
try {
close(false);
} finally {
super.destroy();
}
}
private void close(boolean silent) {
try {
if (context != null) {
context.dispose();
context = null;
}
} catch (GSSException e) {
if (!silent) {
log.warn(SshdText.get().gssapiFailure, e);
}
}
}
private void sendToken(ClientSession session, byte[] receivedToken)
throws IOException, GSSException {
state = ProtocolState.TOKENS;
byte[] token = context.initSecContext(receivedToken, 0,
receivedToken.length);
if (token != null) {
Buffer buffer = session.createBuffer(SSH_MSG_USERAUTH_GSSAPI_TOKEN);
buffer.putBytes(token);
session.writePacket(buffer);
}
}
private void sendMic(ClientSession session, String service)
throws IOException, GSSException {
state = ProtocolState.MIC_SENT;
// Produce MIC
Buffer micBuffer = new ByteArrayBuffer();
micBuffer.putBytes(session.getSessionId());
micBuffer.putByte(SshConstants.SSH_MSG_USERAUTH_REQUEST);
micBuffer.putString(session.getUsername());
micBuffer.putString(service);
micBuffer.putString(getName());
byte[] micBytes = micBuffer.getCompactData();
byte[] mic = context.getMIC(micBytes, 0, micBytes.length,
new MessageProp(0, true));
Buffer buffer = session
.createBuffer(SshConstants.SSH_MSG_USERAUTH_GSSAPI_MIC);
buffer.putBytes(mic);
session.writePacket(buffer);
}
private void replyToken(ClientSession session, String service, byte[] bytes)
throws IOException, GSSException {
sendToken(session, bytes);
if (context.isEstablished()) {
sendMic(session, service);
}
}
private String getHostName(ClientSession session) {
SocketAddress remote = session.getConnectAddress();
if (remote instanceof InetSocketAddress) {
InetAddress address = GssApiMechanisms
.resolve((InetSocketAddress) remote);
if (address != null) {
return address.getCanonicalHostName();
}
}
if (session instanceof JGitClientSession) {
String hostName = ((JGitClientSession) session).getHostConfigEntry()
.getHostName();
try {
hostName = InetAddress.getByName(hostName)
.getCanonicalHostName();
} catch (UnknownHostException e) {
// Ignore here; try with the non-canonical name
}
return hostName;
}
throw new IllegalStateException(
"Wrong session class :" + session.getClass().getName()); //$NON-NLS-1$
}
private boolean unexpectedMessage(int command) {
log.warn(format(SshdText.get().gssapiUnexpectedMessage, getName(),
Integer.toString(command)));
return false;
}
@Override
public void signalAuthMethodSuccess(ClientSession session, String service,
Buffer buffer) throws Exception {
GssApiWithMicAuthenticationReporter reporter = session.getAttribute(
GssApiWithMicAuthenticationReporter.GSS_AUTHENTICATION_REPORTER);
if (reporter != null) {
reporter.signalAuthenticationSuccess(session, service,
currentMechanism.toString());
}
}
@Override
public void signalAuthMethodFailure(ClientSession session, String service,
boolean partial, List<String> serverMethods, Buffer buffer)
throws Exception {
GssApiWithMicAuthenticationReporter reporter = session.getAttribute(
GssApiWithMicAuthenticationReporter.GSS_AUTHENTICATION_REPORTER);
if (reporter != null) {
reporter.signalAuthenticationFailure(session, service,
currentMechanism.toString(), partial, serverMethods);
}
}
}
|