Fix warnings

This commit is contained in:
Jeroen van Erp
2020-09-09 11:02:28 +02:00
parent 939a170ee8
commit 8c899eb867
23 changed files with 99 additions and 71 deletions

View File

@@ -23,6 +23,7 @@ import java.io.IOException;
* Thrown when a key file could not be decrypted correctly, e.g. if its checkInts differed in the case of an OpenSSH
* key file.
*/
@SuppressWarnings("serial")
public class KeyDecryptionFailedException extends IOException {
public static final String MESSAGE = "Decryption of the key failed. A supplied passphrase may be incorrect.";

View File

@@ -27,6 +27,7 @@ import java.util.Arrays;
* Our own extension of the EdDSAPublicKey that comes from ECC-25519, as that class does not implement equality.
* The code uses the equality of the keys as an indicator whether they're the same during host key verification.
*/
@SuppressWarnings("serial")
public class Ed25519PublicKey extends EdDSAPublicKey {
public Ed25519PublicKey(EdDSAPublicKeySpec spec) {

View File

@@ -1,5 +1,6 @@
package net.schmizz.sshj.common;
/**
* <p>Encodes and decodes to and from Base64 notation.</p>
* <p>Homepage: <a href="http://iharder.net/base64">http://iharder.net/base64</a>.</p>
@@ -1359,7 +1360,7 @@ public class Base64
@Override
public Class<?> resolveClass(java.io.ObjectStreamClass streamClass)
throws java.io.IOException, ClassNotFoundException {
Class c = Class.forName(streamClass.getName(), false, loader);
Class<?> c = Class.forName(streamClass.getName(), false, loader);
if( c == null ){
return super.resolveClass(streamClass);
} else {

View File

@@ -21,6 +21,7 @@ import java.security.GeneralSecurityException;
import java.security.PublicKey;
import java.util.Arrays;
@SuppressWarnings("serial")
public class Buffer<T extends Buffer<T>> {
public static class BufferException
@@ -55,7 +56,7 @@ public class Buffer<T extends Buffer<T>> {
public static final int DEFAULT_SIZE = 256;
/** The maximum valid size of buffer (i.e. biggest power of two that can be represented as an int - 2^30) */
public static final int MAX_SIZE = (1 << 30);
public static final int MAX_SIZE = (1 << 30);
/** Maximum size of a uint64 */
public static final BigInteger MAX_UINT64_VALUE = BigInteger.ONE
@@ -66,7 +67,7 @@ public class Buffer<T extends Buffer<T>> {
int j = 1;
while (j < i) {
j <<= 1;
if (j <= 0) throw new IllegalArgumentException("Cannot get next power of 2; "+i+" is too large");
if (j <= 0) throw new IllegalArgumentException("Cannot get next power of 2; "+i+" is too large");
}
return j;
}

View File

@@ -23,6 +23,7 @@ import java.io.IOException;
* Most exceptions in the {@code net.schmizz.sshj} package are instances of this class. An {@link SSHException} is
* itself an {@link IOException} and can be caught like that if this level of granularity is not desired.
*/
@SuppressWarnings("serial")
public class SSHException
extends IOException {

View File

@@ -16,6 +16,7 @@
package net.schmizz.sshj.common;
/** Represents unrecoverable exceptions in the {@code net.schmizz.sshj} package. */
@SuppressWarnings("serial")
public class SSHRuntimeException
extends RuntimeException {

View File

@@ -36,6 +36,8 @@ import javax.crypto.NoSuchPaddingException;
import static java.lang.String.format;
import java.lang.reflect.InvocationTargetException;
/**
* Static utility method relating to security facilities.
*/
@@ -53,8 +55,8 @@ public class SecurityUtils {
public static final String SPONGY_CASTLE = "SC";
/*
* Security provider identifier. null = default JCE
*/
* Security provider identifier. null = default JCE
*/
private static String securityProvider = null;
// relate to BC registration (or SpongyCastle on Android)
@@ -65,13 +67,17 @@ public class SecurityUtils {
Provider provider = null;
try {
Class<?> name = Class.forName(providerClassName);
provider = (Provider) name.newInstance();
provider = (Provider) name.getDeclaredConstructor().newInstance();
} catch (ClassNotFoundException e) {
LOG.info("Security Provider class '{}' not found", providerClassName);
} catch (InstantiationException e) {
LOG.info("Security Provider class '{}' could not be created", providerClassName);
} catch (IllegalAccessException e) {
LOG.info("Security Provider class '{}' could not be accessed", providerClassName);
} catch (InvocationTargetException e) {
LOG.info("Security Provider class '{}' could not be created", providerClassName);
} catch (NoSuchMethodException e) {
LOG.info("Security Provider class '{}' does not have a no-args constructor", providerClassName);
}
if (provider == null) {

View File

@@ -21,6 +21,7 @@ import com.hierynomus.asn1.types.ASN1Object;
import com.hierynomus.asn1.types.constructed.ASN1Sequence;
import com.hierynomus.asn1.types.primitive.ASN1Integer;
import net.schmizz.sshj.common.Buffer;
import net.schmizz.sshj.common.IOUtils;
import net.schmizz.sshj.common.KeyType;
import net.schmizz.sshj.common.SSHRuntimeException;
@@ -91,16 +92,18 @@ public class SignatureECDSA extends AbstractSignature {
public byte[] encode(byte[] sig) {
ByteArrayInputStream bais = new ByteArrayInputStream(sig);
com.hierynomus.asn1.ASN1InputStream asn1InputStream = new com.hierynomus.asn1.ASN1InputStream(new DERDecoder(), bais);
try {
ASN1Sequence sequence = asn1InputStream.readObject();
ASN1Integer r = (ASN1Integer) sequence.get(0);
ASN1Integer s = (ASN1Integer) sequence.get(1);
Buffer.PlainBuffer buf = new Buffer.PlainBuffer();
buf.putMPInt(r.getValue());
buf.putMPInt(s.getValue());
ASN1Sequence sequence = asn1InputStream.readObject();
ASN1Integer r = (ASN1Integer) sequence.get(0);
ASN1Integer s = (ASN1Integer) sequence.get(1);
Buffer.PlainBuffer buf = new Buffer.PlainBuffer();
buf.putMPInt(r.getValue());
buf.putMPInt(s.getValue());
return buf.getCompactData();
return buf.getCompactData();
} finally {
IOUtils.closeQuietly(asn1InputStream, bais);
}
}
@Override
@@ -123,16 +126,19 @@ public class SignatureECDSA extends AbstractSignature {
BigInteger r = sigbuf.readMPInt();
BigInteger s = sigbuf.readMPInt();
List<ASN1Object> vector = new ArrayList<ASN1Object>();
vector.add(new ASN1Integer(r));
vector.add(new ASN1Integer(s));
ByteArrayOutputStream baos = new ByteArrayOutputStream();
com.hierynomus.asn1.ASN1OutputStream asn1OutputStream = new com.hierynomus.asn1.ASN1OutputStream(new DEREncoder(), baos);
asn1OutputStream.writeObject(new ASN1Sequence(vector));
asn1OutputStream.flush();
try {
asn1OutputStream.writeObject(new ASN1Sequence(vector));
asn1OutputStream.flush();
} finally {
IOUtils.closeQuietly(asn1OutputStream);
}
return baos.toByteArray();
}
}

View File

@@ -29,8 +29,7 @@ import java.security.GeneralSecurityException;
* Base class for DHG key exchange algorithms. Implementations will only have to configure the required data on the
* {@link DH} class in the
*/
public abstract class AbstractDHG extends AbstractDH
implements KeyExchange {
public abstract class AbstractDHG extends AbstractDH {
private final Logger log = LoggerFactory.getLogger(getClass());

View File

@@ -57,11 +57,12 @@ public abstract class AbstractDHGex extends AbstractDH {
return parseGexGroup(buffer);
case KEX_DH_GEX_REPLY:
return parseGexReply(buffer);
default:
throw new TransportException("Unexpected message " + msg);
}
} catch (Buffer.BufferException be) {
throw new TransportException(be);
}
throw new TransportException("Unexpected message " + msg);
}
private boolean parseGexReply(SSHPacket buffer) throws Buffer.BufferException, GeneralSecurityException, TransportException {

View File

@@ -16,17 +16,14 @@
package com.hierynomus.sshj.transport;
import com.hierynomus.sshj.test.SshFixture;
import net.schmizz.sshj.DefaultConfig;
import net.schmizz.sshj.SSHClient;
import net.schmizz.sshj.common.DisconnectReason;
import net.schmizz.sshj.connection.channel.direct.Session;
import net.schmizz.sshj.transport.DisconnectListener;
import net.schmizz.sshj.transport.TransportException;
import net.schmizz.sshj.transport.verification.PromiscuousVerifier;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.experimental.theories.suppliers.TestedOn;
import java.io.IOException;
import java.util.concurrent.TimeUnit;

View File

@@ -26,8 +26,6 @@ import org.junit.experimental.theories.Theories;
import org.junit.experimental.theories.Theory;
import org.junit.runner.RunWith;
import java.io.File;
import java.io.FileInputStream;
import java.security.Security;
import java.util.Arrays;

View File

@@ -19,7 +19,6 @@ import com.hierynomus.sshj.test.BaseAlgorithmTest;
import net.schmizz.sshj.Config;
import net.schmizz.sshj.DefaultConfig;
import net.schmizz.sshj.common.Factory;
import net.schmizz.sshj.transport.kex.Curve25519SHA256;
import net.schmizz.sshj.transport.kex.DHGexSHA1;
import net.schmizz.sshj.transport.kex.DHGexSHA256;
import net.schmizz.sshj.transport.kex.ECDHNistP;

View File

@@ -20,8 +20,6 @@ import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import static org.junit.Assert.fail;
public class LoadsOfConnects {
@@ -46,4 +44,4 @@ public class LoadsOfConnects {
}
}
}

View File

@@ -57,7 +57,11 @@ public class SFTPClientTest {
@Test
public void doesNotTryToCreateDirectoryTwiceWhenPathHasTrailingSeparator() throws Exception {
SFTPClient client = new SFTPClient(sftpEngine);
client.mkdirs("/folder/directory/");
verify(sftpEngine, times(1)).makeDir("/folder/directory");
try {
client.mkdirs("/folder/directory/");
verify(sftpEngine, times(1)).makeDir("/folder/directory");
} finally {
client.close();
}
}
}

View File

@@ -21,6 +21,8 @@ import org.junit.Test;
import java.nio.charset.Charset;
import com.hierynomus.sshj.transport.mac.Macs;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.fail;
@@ -33,7 +35,7 @@ public class BaseMacTest {
@Test
public void testResizeTooBigKeys() {
BaseMAC hmac = new HMACSHA1();
BaseMAC hmac = Macs.HMACSHA1().create();
hmac.init((KEY + "foo").getBytes(CHARSET));
hmac.update(PLAIN_TEXT);
assertThat(Hex.toHexString(hmac.doFinal()), is(EXPECTED_HMAC));
@@ -82,8 +84,8 @@ public class BaseMacTest {
}
private BaseMAC initHmac() {
BaseMAC hmac = new HMACSHA1();
BaseMAC hmac = Macs.HMACSHA1().create();
hmac.init(KEY.getBytes(CHARSET));
return hmac;
}
}
}

View File

@@ -20,6 +20,8 @@ import org.junit.Test;
import java.nio.charset.Charset;
import com.hierynomus.sshj.transport.mac.Macs;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
@@ -30,29 +32,29 @@ public class HMACMD596Test {
@Test
public void testUpdateWithDoFinal() {
HMACMD596 hmac = initHmac();
BaseMAC hmac = initHmac();
hmac.update(PLAIN_TEXT);
assertThat(Hex.toHexString(hmac.doFinal()), is(EXPECTED_HMAC));
}
@Test
public void testDoFinalWithInput() {
HMACMD596 hmac = initHmac();
BaseMAC hmac = initHmac();
assertThat(Hex.toHexString(hmac.doFinal(PLAIN_TEXT)),
is(EXPECTED_HMAC));
}
@Test
public void testUpdateWithDoFinalWithResultBuffer() {
HMACMD596 hmac = initHmac();
BaseMAC hmac = initHmac();
byte[] resultBuf = new byte[12];
hmac.update(PLAIN_TEXT);
hmac.doFinal(resultBuf, 0);
assertThat(Hex.toHexString(resultBuf), is(EXPECTED_HMAC));
}
private HMACMD596 initHmac() {
HMACMD596 hmac = new HMACMD596();
private BaseMAC initHmac() {
BaseMAC hmac = Macs.HMACMD596().create();
hmac.init("ohBahfei6pee5dai".getBytes(CHARSET));
return hmac;
}

View File

@@ -20,6 +20,8 @@ import org.junit.Test;
import java.nio.charset.Charset;
import com.hierynomus.sshj.transport.mac.Macs;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
@@ -30,28 +32,28 @@ public class HMACMD5Test {
@Test
public void testUpdateWithDoFinal() {
HMACMD5 hmac = initHmac();
BaseMAC hmac = initHmac();
hmac.update(PLAIN_TEXT);
assertThat(Hex.toHexString(hmac.doFinal()), is(EXPECTED_HMAC));
}
@Test
public void testDoFinalWithInput() {
HMACMD5 hmac = initHmac();
BaseMAC hmac = initHmac();
assertThat(Hex.toHexString(hmac.doFinal(PLAIN_TEXT)), is(EXPECTED_HMAC));
}
@Test
public void testUpdateWithDoFinalWithResultBuffer() {
HMACMD5 hmac = initHmac();
BaseMAC hmac = initHmac();
byte[] resultBuf = new byte[16];
hmac.update(PLAIN_TEXT);
hmac.doFinal(resultBuf, 0);
assertThat(Hex.toHexString(resultBuf), is(EXPECTED_HMAC));
}
private HMACMD5 initHmac() {
HMACMD5 hmac = new HMACMD5();
private BaseMAC initHmac() {
BaseMAC hmac = Macs.HMACMD5().create();
hmac.init("ohBahfei6pee5dai".getBytes(CHARSET));
return hmac;
}

View File

@@ -20,6 +20,8 @@ import org.junit.Test;
import java.nio.charset.Charset;
import com.hierynomus.sshj.transport.mac.Macs;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
@@ -30,28 +32,28 @@ public class HMACSHA196Test {
@Test
public void testUpdateWithDoFinal() {
HMACSHA196 hmac = initHmac();
BaseMAC hmac = initHmac();
hmac.update(PLAIN_TEXT);
assertThat(Hex.toHexString(hmac.doFinal()), is(EXPECTED_HMAC));
}
@Test
public void testDoFinalWithInput() {
HMACSHA196 hmac = initHmac();
BaseMAC hmac = initHmac();
assertThat(Hex.toHexString(hmac.doFinal(PLAIN_TEXT)), is(EXPECTED_HMAC));
}
@Test
public void testUpdateWithDoFinalWithResultBuffer() {
HMACSHA196 hmac = initHmac();
BaseMAC hmac = initHmac();
byte[] resultBuf = new byte[12];
hmac.update(PLAIN_TEXT);
hmac.doFinal(resultBuf, 0);
assertThat(Hex.toHexString(resultBuf), is(EXPECTED_HMAC));
}
private HMACSHA196 initHmac() {
HMACSHA196 hmac = new HMACSHA196();
private BaseMAC initHmac() {
BaseMAC hmac = Macs.HMACSHA196().create();
hmac.init("et1Quo5ooCie6theel8i".getBytes(CHARSET));
return hmac;
}

View File

@@ -20,6 +20,8 @@ import org.junit.Test;
import java.nio.charset.Charset;
import com.hierynomus.sshj.transport.mac.Macs;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
@@ -30,29 +32,29 @@ public class HMACSHA1Test {
@Test
public void testUpdateWithDoFinal() {
HMACSHA1 hmac = initHmac();
BaseMAC hmac = initHmac();
hmac.update(PLAIN_TEXT);
assertThat(Hex.toHexString(hmac.doFinal()), is(EXPECTED_HMAC));
}
@Test
public void testDoFinalWithInput() {
HMACSHA1 hmac = initHmac();
BaseMAC hmac = initHmac();
assertThat(Hex.toHexString(hmac.doFinal(PLAIN_TEXT)), is(EXPECTED_HMAC));
}
@Test
public void testUpdateWithDoFinalWithResultBuffer() {
HMACSHA1 hmac = initHmac();
BaseMAC hmac = initHmac();
byte[] resultBuf = new byte[20];
hmac.update(PLAIN_TEXT);
hmac.doFinal(resultBuf, 0);
assertThat(Hex.toHexString(resultBuf), is(EXPECTED_HMAC));
}
private HMACSHA1 initHmac() {
HMACSHA1 hmac = new HMACSHA1();
private BaseMAC initHmac() {
BaseMAC hmac = Macs.HMACSHA1().create();
hmac.init("et1Quo5ooCie6theel8i".getBytes(CHARSET));
return hmac;
}
}
}

View File

@@ -20,6 +20,8 @@ import org.junit.Test;
import java.nio.charset.Charset;
import com.hierynomus.sshj.transport.mac.Macs;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
@@ -30,29 +32,29 @@ public class HMACSHA2256Test {
@Test
public void testUpdateWithDoFinal() {
HMACSHA2256 hmac = initHmac();
BaseMAC hmac = initHmac();
hmac.update(PLAIN_TEXT);
assertThat(Hex.toHexString(hmac.doFinal()), is(EXPECTED_HMAC));
}
@Test
public void testDoFinalWithInput() {
HMACSHA2256 hmac = initHmac();
BaseMAC hmac = initHmac();
assertThat(Hex.toHexString(hmac.doFinal(PLAIN_TEXT)), is(EXPECTED_HMAC));
}
@Test
public void testUpdateWithDoFinalWithResultBuffer() {
HMACSHA2256 hmac = initHmac();
BaseMAC hmac = initHmac();
byte[] resultBuf = new byte[32];
hmac.update(PLAIN_TEXT);
hmac.doFinal(resultBuf, 0);
assertThat(Hex.toHexString(resultBuf), is(EXPECTED_HMAC));
}
private HMACSHA2256 initHmac() {
HMACSHA2256 hmac = new HMACSHA2256();
private BaseMAC initHmac() {
BaseMAC hmac = Macs.HMACSHA2256().create();
hmac.init("koopiegh4reengah1que9Wiew7ohahPh".getBytes(CHARSET));
return hmac;
}
}
}

View File

@@ -20,6 +20,8 @@ import org.junit.Test;
import java.nio.charset.Charset;
import com.hierynomus.sshj.transport.mac.Macs;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
@@ -30,28 +32,28 @@ public class HMACSHA2512Test {
@Test
public void testUpdateWithDoFinal() {
HMACSHA2512 hmac = initHmac();
BaseMAC hmac = initHmac();
hmac.update(PLAIN_TEXT);
assertThat(Hex.toHexString(hmac.doFinal()), is(EXPECTED_HMAC));
}
@Test
public void testDoFinalWithInput() {
HMACSHA2512 hmac = initHmac();
BaseMAC hmac = initHmac();
assertThat(Hex.toHexString(hmac.doFinal(PLAIN_TEXT)), is(EXPECTED_HMAC));
}
@Test
public void testUpdateWithDoFinalWithResultBuffer() {
HMACSHA2512 hmac = initHmac();
BaseMAC hmac = initHmac();
byte[] resultBuf = new byte[64];
hmac.update(PLAIN_TEXT);
hmac.doFinal(resultBuf, 0);
assertThat(Hex.toHexString(resultBuf), is(EXPECTED_HMAC));
}
private HMACSHA2512 initHmac() {
HMACSHA2512 hmac = new HMACSHA2512();
private BaseMAC initHmac() {
BaseMAC hmac = Macs.HMACSHA2512().create();
hmac.init("paishiengu1jaeTie5OoTu2eib7Kohqueicie7ahLohfoothahpeivi5weik1EiB".getBytes(CHARSET));
return hmac;
}

View File

@@ -28,8 +28,8 @@ import org.junit.rules.TemporaryFolder;
import java.io.File;
import java.io.IOException;
import static junit.framework.Assert.assertFalse;
import static junit.framework.Assert.assertTrue;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.hamcrest.MatcherAssert.assertThat;
public class SCPFileTransferTest {
@@ -55,7 +55,7 @@ public class SCPFileTransferTest {
sshClient = fixture.setupConnectedDefaultClient();
sshClient.authPassword("test", "test");
}
@After
public void cleanup() {
if (targetFile.exists()) {