aboutsummaryrefslogtreecommitdiffstats
path: root/org.eclipse.jgit.ssh.apache/src/org/eclipse/jgit/internal/signing/ssh/SshSigner.java
blob: 8cfe5f47668115c684588d7751d0136427bbe5c0 (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
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
/*
 * Copyright (C) 2024, Thomas Wolf <twolf@apache.org> 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.signing.ssh;

import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StreamCorruptedException;
import java.io.StringReader;
import java.nio.file.Files;
import java.nio.file.Path;
import java.security.GeneralSecurityException;
import java.security.KeyPair;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.text.MessageFormat;
import java.util.AbstractMap.SimpleImmutableEntry;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.sshd.client.auth.pubkey.PublicKeyIdentity;
import org.apache.sshd.common.config.keys.AuthorizedKeyEntry;
import org.apache.sshd.common.config.keys.KeyUtils;
import org.apache.sshd.common.config.keys.OpenSshCertificate;
import org.apache.sshd.common.config.keys.PublicKeyEntryResolver;
import org.apache.sshd.common.config.keys.loader.KeyPairResourceParser;
import org.apache.sshd.common.keyprovider.KeyPairProvider;
import org.apache.sshd.common.session.SessionContext;
import org.apache.sshd.common.signature.BuiltinSignatures;
import org.apache.sshd.common.signature.Signature;
import org.apache.sshd.common.util.buffer.Buffer;
import org.apache.sshd.common.util.buffer.ByteArrayBuffer;
import org.apache.sshd.common.util.security.SecurityUtils;
import org.eclipse.jgit.annotations.NonNull;
import org.eclipse.jgit.api.errors.CanceledException;
import org.eclipse.jgit.api.errors.UnsupportedSigningFormatException;
import org.eclipse.jgit.internal.transport.sshd.AuthenticationCanceledException;
import org.eclipse.jgit.internal.transport.sshd.PasswordProviderWrapper;
import org.eclipse.jgit.internal.transport.sshd.SshdText;
import org.eclipse.jgit.internal.transport.sshd.agent.SshAgentClient;
import org.eclipse.jgit.lib.GpgConfig;
import org.eclipse.jgit.lib.GpgSignature;
import org.eclipse.jgit.lib.PersonIdent;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.lib.Signer;
import org.eclipse.jgit.transport.CredentialsProvider;
import org.eclipse.jgit.transport.sshd.KeyPasswordProviderFactory;
import org.eclipse.jgit.transport.sshd.agent.Connector;
import org.eclipse.jgit.transport.sshd.agent.ConnectorFactory;
import org.eclipse.jgit.util.Base64;
import org.eclipse.jgit.util.FS;
import org.eclipse.jgit.util.FS.ExecutionResult;
import org.eclipse.jgit.util.StringUtils;
import org.eclipse.jgit.util.SystemReader;
import org.eclipse.jgit.util.TemporaryBuffer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * A {@link Signer} to create SSH signatures.
 *
 * @see <a href=
 *      "https://github.com/openssh/openssh-portable/blob/master/PROTOCOL.sshsig">PROTOCOL.sshsig</a>
 */
public class SshSigner implements Signer {

	private static final Logger LOG = LoggerFactory.getLogger(SshSigner.class);

	private static final String GIT_KEY_PREFIX = "key::"; //$NON-NLS-1$

	// Base64 encoded lines should not be longer than 75 characters, plus the
	// newline.
	private static final int LINE_LENGTH = 75;

	@Override
	public GpgSignature sign(Repository repository, GpgConfig config,
			byte[] data, PersonIdent committer, String signingKey,
			CredentialsProvider credentialsProvider) throws CanceledException,
			IOException, UnsupportedSigningFormatException {
		byte[] hash;
		try {
			hash = MessageDigest.getInstance("SHA512").digest(data); //$NON-NLS-1$
		} catch (NoSuchAlgorithmException e) {
			throw new UnsupportedSigningFormatException(
					MessageFormat.format(
							SshdText.get().signUnknownHashAlgorithm, "SHA512"), //$NON-NLS-1$
					e);
		}
		Buffer toSign = new ByteArrayBuffer();
		toSign.putRawBytes(SshSignatureConstants.MAGIC);
		toSign.putString(SshSignatureConstants.NAMESPACE);
		toSign.putUInt(0); // reserved: zero-length string
		toSign.putString("sha512"); //$NON-NLS-1$
		toSign.putBytes(hash);
		String key = signingKey;
		if (StringUtils.isEmptyOrNull(key)) {
			key = config.getSigningKey();
		}
		if (StringUtils.isEmptyOrNull(key)) {
			key = defaultKeyCommand(repository, config);
			// According to documentation, this is supposed to return a
			// valid SSH public key prefixed with "key::". We don't enforce
			// this: there might be older command implementations (like just
			// calling "ssh-add -L") that return keys without prefix.
		}
		PublicKeyIdentity identity;
		try {
			identity = getIdentity(key, committer, credentialsProvider);
		} catch (GeneralSecurityException e) {
			throw new UnsupportedSigningFormatException(MessageFormat
					.format(SshdText.get().signPublicKeyError, key), e);
		}
		String algorithm = KeyUtils
				.getKeyType(identity.getKeyIdentity().getPublic());
		switch (algorithm) {
		case KeyPairProvider.SSH_DSS:
		case KeyPairProvider.SSH_DSS_CERT:
			throw new UnsupportedSigningFormatException(
					SshdText.get().signInvalidKeyDSA);
		case KeyPairProvider.SSH_RSA:
			algorithm = KeyUtils.RSA_SHA512_KEY_TYPE_ALIAS;
			break;
		case KeyPairProvider.SSH_RSA_CERT:
			algorithm = KeyUtils.RSA_SHA512_CERT_TYPE_ALIAS;
			break;
		default:
			break;
		}

		Map.Entry<String, byte[]> rawSignature;
		try {
			rawSignature = identity.sign(null, algorithm,
					toSign.getCompactData());
		} catch (Exception e) {
			throw new UnsupportedSigningFormatException(
					SshdText.get().signSignatureError, e);
		}
		algorithm = rawSignature.getKey();
		Buffer signature = new ByteArrayBuffer();
		signature.putRawBytes(SshSignatureConstants.MAGIC);
		signature.putUInt(SshSignatureConstants.VERSION);
		signature.putPublicKey(identity.getKeyIdentity().getPublic());
		signature.putString(SshSignatureConstants.NAMESPACE);
		signature.putUInt(0); // reserved: zero-length string
		signature.putString("sha512"); //$NON-NLS-1$
		Buffer sig = new ByteArrayBuffer();
		sig.putString(KeyUtils.getSignatureAlgorithm(algorithm,
				identity.getKeyIdentity().getPublic()));
		sig.putBytes(rawSignature.getValue());
		signature.putBytes(sig.getCompactData());
		return armor(signature.getCompactData());
	}

	private static String defaultKeyCommand(@NonNull Repository repository,
			@NonNull GpgConfig config) throws IOException {
		String command = config.getSshDefaultKeyCommand();
		if (StringUtils.isEmptyOrNull(command)) {
			return null;
		}
		FS fileSystem = repository.getFS();
		if (fileSystem == null) {
			fileSystem = FS.DETECTED;
		}
		ProcessBuilder builder = fileSystem.runInShell(command,
				new String[] {});
		ExecutionResult result = null;
		try {
			result = fileSystem.execute(builder, null);
			int exitCode = result.getRc();
			if (exitCode == 0) {
				// The command is supposed to return a public key in its first
				// line on stdout.
				try (BufferedReader r = new BufferedReader(
						new InputStreamReader(
								result.getStdout().openInputStream(),
								SystemReader.getInstance()
										.getDefaultCharset()))) {
					String line = r.readLine();
					if (line != null) {
						line = line.strip();
					}
					if (StringUtils.isEmptyOrNull(line)) {
						throw new IOException(MessageFormat.format(
								SshdText.get().signDefaultKeyEmpty, command));
					}
					return line;
				}
			}
			TemporaryBuffer stderr = result.getStderr();
			throw new IOException(MessageFormat.format(
					SshdText.get().signDefaultKeyFailed, command,
					Integer.toString(exitCode), toString(stderr)));
		} catch (InterruptedException e) {
			Thread.currentThread().interrupt();
			throw new IOException(
					MessageFormat.format(
							SshdText.get().signDefaultKeyInterrupted, command),
					e);
		} finally {
			if (result != null) {
				if (result.getStderr() != null) {
					result.getStderr().destroy();
				}
				if (result.getStdout() != null) {
					result.getStdout().destroy();
				}
			}
		}
	}

	private static String toString(TemporaryBuffer b) {
		if (b != null) {
			try {
				return new String(b.toByteArray(4000),
						SystemReader.getInstance().getDefaultCharset());
			} catch (IOException e) {
				LOG.warn("{}", SshdText.get().signStderr, e); //$NON-NLS-1$
			}
		}
		return ""; //$NON-NLS-1$
	}

	private static PublicKeyIdentity getIdentity(String signingKey,
			PersonIdent committer, CredentialsProvider credentials)
			throws CanceledException, GeneralSecurityException, IOException {
		if (StringUtils.isEmptyOrNull(signingKey)) {
			throw new IllegalArgumentException(SshdText.get().signNoSigningKey);
		}
		PublicKey publicKey = null;
		PrivateKey privateKey = null;
		File keyFile = null;
		if (signingKey.startsWith(GIT_KEY_PREFIX)) {
			try (StringReader r = new StringReader(
					signingKey.substring(GIT_KEY_PREFIX.length()))) {
				publicKey = fromEntry(
						AuthorizedKeyEntry.readAuthorizedKeys(r, true));
			}
		} else if (signingKey.startsWith("~/") //$NON-NLS-1$
				|| signingKey.startsWith('~' + File.separator)) {
			keyFile = new File(FS.DETECTED.userHome(), signingKey.substring(2));
		} else {
			try (StringReader r = new StringReader(signingKey)) {
				publicKey = fromEntry(
						AuthorizedKeyEntry.readAuthorizedKeys(r, true));
			} catch (IOException e) {
				// Ignore and try to read as a file
				keyFile = new File(signingKey);
			}
		}
		if (keyFile != null && keyFile.isFile()) {
			try {
				publicKey = fromEntry(AuthorizedKeyEntry
						.readAuthorizedKeys(keyFile.toPath()));
				if (publicKey == null) {
					throw new IOException(MessageFormat.format(
							SshdText.get().signTooManyPublicKeys, keyFile));
				}
				// Try to find the private key so we don't go looking for
				// the agent (or PKCS#11) in vain.
				keyFile = getPrivateKeyFile(keyFile.getParentFile(),
						keyFile.getName());
				if (keyFile != null) {
					try {
						KeyPair pair = loadPrivateKey(keyFile.toPath(),
								credentials);
						if (pair != null) {
							PublicKey pk = pair.getPublic();
							if (pk == null) {
								privateKey = pair.getPrivate();
							} else {
								PublicKey original = publicKey;
								if (publicKey instanceof OpenSshCertificate cert) {
									original = cert.getCertPubKey();
								}
								if (KeyUtils.compareKeys(original, pk)) {
									privateKey = pair.getPrivate();
								}
							}
						}
					} catch (IOException e) {
						// Apparently it wasn't a private key file. Ignore.
					}
				}
			} catch (StreamCorruptedException e) {
				// File is readable, but apparently not a public key. Try to
				// load it as a private key.
				KeyPair pair = loadPrivateKey(keyFile.toPath(), credentials);
				if (pair != null) {
					publicKey = pair.getPublic();
					privateKey = pair.getPrivate();
				}
			}
		}
		if (publicKey == null) {
			throw new IOException(MessageFormat
					.format(SshdText.get().signNoPublicKey, signingKey));
		}
		if (publicKey instanceof OpenSshCertificate cert) {
			String message = SshCertificateUtils.verify(cert,
					committer.getWhenAsInstant());
			if (message != null) {
				throw new IOException(message);
			}
		}
		if (privateKey == null) {
			// Could be in the agent, or a PKCS#11 key. The normal procedure
			// with PKCS#11 keys is to put them in the agent and let the agent
			// deal with it.
			//
			// This may or may not work well. For instance, the agent might ask
			// for a passphrase for PKCS#11 keys... also, the OpenSSH ssh-agent
			// had a bug with signing using PKCS#11 certificates in the agent;
			// see https://bugzilla.mindrot.org/show_bug.cgi?id=3613 . If there
			// are troubles, we might do the PKCS#11 dance ourselves, but we'd
			// need additional configuration for the PKCS#11 library. (Plus
			// some refactoring in the Pkcs11Provider.)
			return new AgentIdentity(publicKey);

		}
		return new KeyPairIdentity(new KeyPair(publicKey, privateKey));
	}

	private static File getPrivateKeyFile(File directory,
			String publicKeyName) {
		if (publicKeyName.endsWith(".pub")) { //$NON-NLS-1$
			String privateKeyName = publicKeyName.substring(0,
					publicKeyName.length() - 4);
			if (!privateKeyName.isEmpty()) {
				File keyFile = new File(directory, privateKeyName);
				if (keyFile.isFile()) {
					return keyFile;
				}
				if (privateKeyName.endsWith("-cert")) { //$NON-NLS-1$
					privateKeyName = privateKeyName.substring(0,
							privateKeyName.length() - 5);
					if (!privateKeyName.isEmpty()) {
						keyFile = new File(directory, privateKeyName);
						if (keyFile.isFile()) {
							return keyFile;
						}
					}
				}
			}
		}
		return null;
	}

	private static KeyPair loadPrivateKey(Path path,
			CredentialsProvider credentials)
			throws CanceledException, GeneralSecurityException, IOException {
		if (!Files.isRegularFile(path)) {
			return null;
		}
		KeyPairResourceParser parser = SecurityUtils.getKeyPairResourceParser();
		if (parser != null) {
			PasswordProviderWrapper provider = null;
			if (credentials != null) {
				provider = new PasswordProviderWrapper(
						() -> KeyPasswordProviderFactory.getInstance()
								.apply(credentials));
			}
			try {
				Collection<KeyPair> keyPairs = parser.loadKeyPairs(null, path,
						provider);
				if (keyPairs.size() != 1) {
					throw new GeneralSecurityException(MessageFormat.format(
							SshdText.get().signTooManyPrivateKeys, path));
				}
				return keyPairs.iterator().next();
			} catch (AuthenticationCanceledException e) {
				throw new CanceledException(e.getMessage());
			}
		}
		return null;
	}

	private static GpgSignature armor(byte[] data) throws IOException {
		try (ByteArrayOutputStream b = new ByteArrayOutputStream()) {
			b.write(SshSignatureConstants.ARMOR_HEAD);
			b.write('\n');
			String encoded = Base64.encodeBytes(data);
			int length = encoded.length();
			int column = 0;
			for (int i = 0; i < length; i++) {
				b.write(encoded.charAt(i));
				column++;
				if (column == LINE_LENGTH) {
					b.write('\n');
					column = 0;
				}
			}
			if (column > 0) {
				b.write('\n');
			}
			b.write(SshSignatureConstants.ARMOR_END);
			b.write('\n');
			return new GpgSignature(b.toByteArray());
		}
	}

	private static PublicKey fromEntry(List<AuthorizedKeyEntry> entries)
			throws GeneralSecurityException, IOException {
		if (entries == null || entries.size() != 1) {
			return null;
		}
		return entries.get(0).resolvePublicKey(null,
				PublicKeyEntryResolver.FAILING);
	}

	@Override
	public boolean canLocateSigningKey(Repository repository, GpgConfig config,
			PersonIdent committer, String signingKey,
			CredentialsProvider credentialsProvider) throws CanceledException {
		String key = signingKey;
		if (key == null) {
			key = config.getSigningKey();
		}
		return !(StringUtils.isEmptyOrNull(key)
				&& StringUtils.isEmptyOrNull(config.getSshDefaultKeyCommand()));
	}

	private static class KeyPairIdentity implements PublicKeyIdentity {

		private final @NonNull KeyPair pair;

		KeyPairIdentity(@NonNull KeyPair pair) {
			this.pair = pair;
		}

		@Override
		public KeyPair getKeyIdentity() {
			return pair;
		}

		@Override
		public Entry<String, byte[]> sign(SessionContext session, String algo,
				byte[] data) throws Exception {
			BuiltinSignatures factory = BuiltinSignatures.fromFactoryName(algo);
			if (factory == null || !factory.isSupported()) {
				throw new GeneralSecurityException(MessageFormat.format(
						SshdText.get().signUnknownSignatureAlgorithm, algo));
			}
			Signature signer = factory.create();
			signer.initSigner(null, pair.getPrivate());
			signer.update(null, data);
			return new SimpleImmutableEntry<>(factory.getName(),
					signer.sign(null));
		}
	}

	private static class AgentIdentity extends KeyPairIdentity {

		AgentIdentity(PublicKey publicKey) {
			super(new KeyPair(publicKey, null));
		}

		@Override
		public Entry<String, byte[]> sign(SessionContext session, String algo,
				byte[] data) throws Exception {
			ConnectorFactory factory = ConnectorFactory.getDefault();
			Connector connector = factory == null ? null
					: factory.create("", null); //$NON-NLS-1$
			if (connector == null) {
				throw new IOException(SshdText.get().signNoAgent);
			}
			try (SshAgentClient agent = new SshAgentClient(connector)) {
				return agent.sign(null, getKeyIdentity().getPublic(), algo,
						data);
			}
		}
	}
}