aboutsummaryrefslogtreecommitdiffstats
path: root/org.eclipse.jgit.ssh.apache/src/org/eclipse/jgit/internal/transport/sshd/GssApiMechanisms.java
blob: 323c51d5a521ea14318cbc86d25e9e3b7c49e64d (plain)
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
/*
 * Copyright (C) 2018, 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 java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.UnknownHostException;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.concurrent.atomic.AtomicBoolean;

import org.eclipse.jgit.annotations.NonNull;
import org.ietf.jgss.GSSContext;
import org.ietf.jgss.GSSException;
import org.ietf.jgss.GSSManager;
import org.ietf.jgss.GSSName;
import org.ietf.jgss.Oid;

/**
 * Global repository of GSS-API mechanisms that we can use.
 */
public class GssApiMechanisms {

	private GssApiMechanisms() {
		// No instantiation
	}

	/** Prefix to use with {@link GSSName#NT_HOSTBASED_SERVICE}. */
	public static final String GSSAPI_HOST_PREFIX = "host@"; //$NON-NLS-1$

	/** The {@link Oid} of Kerberos 5. */
	public static final Oid KERBEROS_5 = createOid("1.2.840.113554.1.2.2"); //$NON-NLS-1$

	/** SGNEGO is not to be used with ssh. */
	public static final Oid SPNEGO = createOid("1.3.6.1.5.5.2"); //$NON-NLS-1$

	/** Protects {@link #supportedMechanisms}. */
	private static final Object LOCK = new Object();

	/**
	 * The {@link AtomicBoolean} is set to {@code true} when the mechanism could
	 * be initialized successfully at least once.
	 */
	private static Map<Oid, Boolean> supportedMechanisms;

	/**
	 * Retrieves an immutable collection of the supported mechanisms.
	 *
	 * @return the supported mechanisms
	 */
	@NonNull
	public static Collection<Oid> getSupportedMechanisms() {
		synchronized (LOCK) {
			if (supportedMechanisms == null) {
				GSSManager manager = GSSManager.getInstance();
				Oid[] mechs = manager.getMechs();
				Map<Oid, Boolean> mechanisms = new LinkedHashMap<>();
				if (mechs != null) {
					for (Oid oid : mechs) {
						mechanisms.put(oid, Boolean.FALSE);
					}
				}
				supportedMechanisms = mechanisms;
			}
			return Collections.unmodifiableSet(supportedMechanisms.keySet());
		}
	}

	/**
	 * Report that this mechanism was used successfully.
	 *
	 * @param mechanism
	 *            that worked
	 */
	public static void worked(@NonNull Oid mechanism) {
		synchronized (LOCK) {
			supportedMechanisms.put(mechanism, Boolean.TRUE);
		}
	}

	/**
	 * Mark the mechanisms as failed.
	 *
	 * @param mechanism
	 *            to mark
	 */
	public static void failed(@NonNull Oid mechanism) {
		synchronized (LOCK) {
			Boolean worked = supportedMechanisms.get(mechanism);
			if (worked != null && !worked.booleanValue()) {
				// If it never worked, remove it
				supportedMechanisms.remove(mechanism);
			}
		}
	}

	/**
	 * Resolves an {@link InetSocketAddress}.
	 *
	 * @param remote
	 *            to resolve
	 * @return the resolved {@link InetAddress}, or {@code null} if unresolved.
	 */
	public static InetAddress resolve(@NonNull InetSocketAddress remote) {
		InetAddress address = remote.getAddress();
		if (address == null) {
			try {
				address = InetAddress.getByName(remote.getHostString());
			} catch (UnknownHostException e) {
				return null;
			}
		}
		return address;
	}

	/**
	 * Determines a canonical host name for use use with GSS-API.
	 *
	 * @param remote
	 *            to get the host name from
	 * @return the canonical host name, if it can be determined, otherwise the
	 *         {@link InetSocketAddress#getHostString() unprocessed host name}.
	 */
	@NonNull
	public static String getCanonicalName(@NonNull InetSocketAddress remote) {
		InetAddress address = resolve(remote);
		if (address == null) {
			return remote.getHostString();
		}
		return address.getCanonicalHostName();
	}

	/**
	 * Creates a {@link GSSContext} for the given mechanism to authenticate with
	 * the host given by {@code fqdn}.
	 *
	 * @param mechanism
	 *            {@link Oid} of the mechanism to use
	 * @param fqdn
	 *            fully qualified domain name of the host to authenticate with
	 * @return the context, if the mechanism is available and the context could
	 *         be created, or {@code null} otherwise
	 */
	public static GSSContext createContext(@NonNull Oid mechanism,
			@NonNull String fqdn) {
		GSSContext context = null;
		try {
			GSSManager manager = GSSManager.getInstance();
			context = manager.createContext(
					manager.createName(
							GssApiMechanisms.GSSAPI_HOST_PREFIX + fqdn,
							GSSName.NT_HOSTBASED_SERVICE),
					mechanism, null, GSSContext.DEFAULT_LIFETIME);
		} catch (GSSException e) {
			closeContextSilently(context);
			failed(mechanism);
			return null;
		}
		worked(mechanism);
		return context;
	}

	/**
	 * Closes (disposes of) a {@link GSSContext} ignoring any
	 * {@link GSSException}s.
	 *
	 * @param context
	 *            to dispose
	 */
	public static void closeContextSilently(GSSContext context) {
		if (context != null) {
			try {
				context.dispose();
			} catch (GSSException e) {
				// Ignore
			}
		}
	}

	private static Oid createOid(String rep) {
		try {
			return new Oid(rep);
		} catch (GSSException e) {
			// Does not occur
			return null;
		}
	}

}