Skip to content

Commit

Permalink
Fix warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
hierynomus committed Sep 9, 2020
1 parent 939a170 commit 8c899eb
Show file tree
Hide file tree
Showing 23 changed files with 100 additions and 72 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
* 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.";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
* 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) {
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/net/schmizz/sshj/common/Base64.java
Original file line number Diff line number Diff line change
@@ -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>
Expand Down Expand Up @@ -1359,7 +1360,7 @@ public static Object decodeToObject(
@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 {
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/net/schmizz/sshj/common/Buffer.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.security.PublicKey;
import java.util.Arrays;

@SuppressWarnings("serial")
public class Buffer<T extends Buffer<T>> {

public static class BufferException
Expand Down Expand Up @@ -55,7 +56,7 @@ public PlainBuffer(int size) {
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
Expand All @@ -66,7 +67,7 @@ protected static int getNextPowerOf2(int i) {
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;
}
Expand Down
1 change: 1 addition & 0 deletions src/main/java/net/schmizz/sshj/common/SSHException.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
* 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 {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down
12 changes: 9 additions & 3 deletions src/main/java/net/schmizz/sshj/common/SecurityUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@

import static java.lang.String.format;

import java.lang.reflect.InvocationTargetException;

/**
* Static utility method relating to security facilities.
*/
Expand All @@ -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)
Expand All @@ -65,13 +67,17 @@ public static boolean registerSecurityProvider(String providerClassName) {
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) {
Expand Down
34 changes: 20 additions & 14 deletions src/main/java/net/schmizz/sshj/signature/SignatureECDSA.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
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;

Expand Down Expand Up @@ -91,16 +92,18 @@ public SignatureECDSA(String algorithm, String keyTypeName) {
public byte[] encode(byte[] sig) {
ByteArrayInputStream bais = new ByteArrayInputStream(sig);
com.hierynomus.asn1.ASN1InputStream asn1InputStream = new com.hierynomus.asn1.ASN1InputStream(new DERDecoder(), bais);

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();
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());

return buf.getCompactData();
} finally {
IOUtils.closeQuietly(asn1InputStream, bais);
}
}

@Override
Expand All @@ -123,16 +126,19 @@ private byte[] asnEncode(byte[] sigBlob) throws IOException {
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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@
* 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());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,12 @@ public boolean next(Message msg, SSHPacket buffer) throws GeneralSecurityExcepti
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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@
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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
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;
Expand Down
4 changes: 1 addition & 3 deletions src/test/java/net/schmizz/sshj/LoadsOfConnects.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;

import static org.junit.Assert.fail;

public class LoadsOfConnects {
Expand All @@ -46,4 +44,4 @@ public void loadsOfConnects() {

}

}
}
8 changes: 6 additions & 2 deletions src/test/java/net/schmizz/sshj/sftp/SFTPClientTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,11 @@ public void setRemoteWorkingDirectory() throws IOException {
@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();
}
}
}
8 changes: 5 additions & 3 deletions src/test/java/net/schmizz/sshj/transport/mac/BaseMacTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@

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;
Expand All @@ -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));
Expand Down Expand Up @@ -82,8 +84,8 @@ public void testUpdateWithDoFinalWithResultBuffer() {
}

private BaseMAC initHmac() {
BaseMAC hmac = new HMACSHA1();
BaseMAC hmac = Macs.HMACSHA1().create();
hmac.init(KEY.getBytes(CHARSET));
return hmac;
}
}
}
12 changes: 7 additions & 5 deletions src/test/java/net/schmizz/sshj/transport/mac/HMACMD596Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@

import java.nio.charset.Charset;

import com.hierynomus.sshj.transport.mac.Macs;

import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;

Expand All @@ -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;
}
Expand Down
12 changes: 7 additions & 5 deletions src/test/java/net/schmizz/sshj/transport/mac/HMACMD5Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@

import java.nio.charset.Charset;

import com.hierynomus.sshj.transport.mac.Macs;

import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;

Expand All @@ -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;
}
Expand Down
Loading

0 comments on commit 8c899eb

Please sign in to comment.