1
0
Fork 0
mirror of https://github.com/vanitasvitae/Smack.git synced 2024-11-25 21:42:07 +01:00

Compare commits

...

18 commits

Author SHA1 Message Date
Florian Schmaus
16e438a108 Merge branch 'master' of github.com:igniterealtime/Smack 2020-02-23 19:22:38 +01:00
Florian Schmaus
3adb6c290a travis: use gradle 6.2 2020-02-23 19:12:54 +01:00
Florian Schmaus
78be8f3a04 junit: bump to 5.6.0 2020-02-23 19:12:54 +01:00
Florian Schmaus
961e56a47c proxy: modernize code by using (ByteArrayOutput|DataInput)Stream 2020-02-23 19:12:54 +01:00
Florian Schmaus
b6ad737aa4 proxy: do not call setTcpNoDelay() but instead flush the stream 2020-02-23 19:12:54 +01:00
Florian Schmaus
be2fc23f41 proxy: make it the caller's reponsibility to close the socket
This makes the code shorter as there is now a single place where the
socket should be closed.
2020-02-23 19:12:54 +01:00
Florian Schmaus
22d64ebe6b Merge remote-tracking branch 'my/master' 2020-02-23 16:59:50 +01:00
Florian Schmaus
7b5186f651
Merge pull request #353 from vanitasvitae/androidApiExceptionWording
build.gradle: Improve android API exception message
2020-02-20 15:23:13 +01:00
Florian Schmaus
80e9bcf496
Merge pull request #361 from adiaholic/bugFix
Correct ALPHABET in `BASE32.java`
2020-02-20 15:22:48 +01:00
Florian Schmaus
4a6f49e414
Merge pull request #362 from Flowdalic/errorprone-1.1.1
Errorprone Plugin 1.1.1
2020-02-20 14:44:56 +01:00
Florian Schmaus
876aba9866
Merge pull request #365 from vanitasvitae/iv12
OMEMO: Switch to sending 12 byte IV
2020-02-20 14:44:34 +01:00
1051cd2640
OMEMO: Switch to sending 12 byte IV 2020-02-16 22:15:56 +01:00
Florian Schmaus
6edbe640cb gradle: bump errorprone plugin to 1.1.1 2020-01-28 17:44:39 +01:00
adiaholic
ee3d25751d Correct ALPHABET in BASE32.java 2020-01-28 20:19:42 +05:30
5fd638545d
build.gradle: Improve android API exception message 2019-12-13 16:12:18 +01:00
Florian Schmaus
a6e2523648 proxy: modernize code by using (ByteArrayOutput|DataInput)Stream 2019-11-18 21:09:11 +01:00
Florian Schmaus
14cc0c1e41 proxy: do not call setTcpNoDelay() but instead flush the stream 2019-11-18 19:45:52 +01:00
Florian Schmaus
7afd1fdf46 proxy: make it the caller's reponsibility to close the socket
This makes the code shorter as there is now a single place where the
socket should be closed.
2019-11-18 19:45:25 +01:00
18 changed files with 186 additions and 192 deletions

View file

@ -18,7 +18,7 @@ cache:
- $HOME/.m2
before_install:
- export GRADLE_VERSION=5.5.1
- export GRADLE_VERSION=6.2
- wget https://services.gradle.org/distributions/gradle-${GRADLE_VERSION}-all.zip
- unzip -q gradle-${GRADLE_VERSION}-all.zip
- export PATH="$(pwd)/gradle-${GRADLE_VERSION}/bin:$PATH"

View file

@ -13,7 +13,7 @@ buildscript {
plugins {
id 'ru.vyarus.animalsniffer' version '1.5.0'
id 'net.ltgt.errorprone' version '0.8.1'
id 'net.ltgt.errorprone' version '1.1.1'
}
apply plugin: 'org.kordamp.gradle.markdown'
@ -129,7 +129,7 @@ allprojects {
':smack-omemo-signal',
':smack-openpgp',
].collect { project(it) }
junitVersion = '5.4.2'
junitVersion = '5.6.0'
powerMockVersion = '2.0.2'
commonsIoVersion = '2.6'
}
@ -680,7 +680,7 @@ def getAndroidRuntimeJar() {
if (androidJar.isFile()) {
return androidJar
} else {
throw new Exception("Can't find android.jar for $androidApiLevel API. Please install corresponding SDK platform package")
throw new Exception("Can't find android.jar for API level ${androidApiLevel}. Please install corresponding SDK platform package")
}
}

View file

@ -1,6 +1,6 @@
/**
*
* Copyright 2015-2016 Florian Schmaus.
* Copyright 2015-2019 Florian Schmaus.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -21,6 +21,16 @@ import java.net.Socket;
public interface ProxySocketConnection {
/**
* Initiate a connection to the given host on the given port. Note that the caller is responsible for closing the
* socket in case this method throws.
*
* @param socket the socket to use to initiate the connection to the proxy.
* @param host the host to connect to.
* @param port the port to connect to.
* @param timeout the timeout in milliseconds.
* @throws IOException in case an I/O error occurs.
*/
void connect(Socket socket, String host, int port, int timeout)
throws IOException;

View file

@ -16,6 +16,8 @@
*/
package org.jivesoftware.smack.proxy;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
@ -24,6 +26,8 @@ import java.net.InetSocketAddress;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
import org.jivesoftware.smack.util.OutputStreamUtil;
/**
* Socket factory for socks4 proxy.
*
@ -39,20 +43,17 @@ public class Socks4ProxySocketConnection implements ProxySocketConnection {
@Override
public void connect(Socket socket, String host, int port, int timeout)
throws IOException {
InputStream in = null;
OutputStream out = null;
String proxy_host = proxy.getProxyAddress();
int proxy_port = proxy.getProxyPort();
String user = proxy.getProxyUsername();
try {
socket.connect(new InetSocketAddress(proxy_host, proxy_port), timeout);
in = socket.getInputStream();
out = socket.getOutputStream();
socket.setTcpNoDelay(true);
InputStream in = socket.getInputStream();
DataInputStream dis = new DataInputStream(in);
OutputStream out = socket.getOutputStream();
byte[] buf = new byte[1024];
int index = 0;
ByteArrayOutputStream outBuf = new ByteArrayOutputStream();
byte[] inBuf;
/*
1) CONNECT
@ -72,25 +73,22 @@ public class Socks4ProxySocketConnection implements ProxySocketConnection {
of all zero bits.
*/
buf[index++] = 4;
buf[index++] = 1;
outBuf.write(4);
outBuf.write(1);
buf[index++] = (byte) (port >>> 8);
buf[index++] = (byte) (port & 0xff);
outBuf.write(port >>> 8);
outBuf.write(port & 0xff);
InetAddress inetAddress = InetAddress.getByName(proxy_host);
byte[] byteAddress = inetAddress.getAddress();
for (int i = 0; i < byteAddress.length; i++) {
buf[index++] = byteAddress[i];
}
outBuf.write(byteAddress);
if (user != null) {
byte[] userBytes = user.getBytes(StandardCharsets.UTF_8);
System.arraycopy(userBytes, 0, buf, index, user.length());
index += user.length();
outBuf.write(userBytes);
}
buf[index++] = 0;
out.write(buf, 0, index);
outBuf.write(0);
OutputStreamUtil.writeResetAndFlush(outBuf, out);
/*
The SOCKS server checks to see whether such a request should be granted
@ -119,43 +117,17 @@ public class Socks4ProxySocketConnection implements ProxySocketConnection {
The remaining fields are ignored.
*/
int len = 6;
int s = 0;
while (s < len) {
int i = in.read(buf, s, len - s);
if (i <= 0) {
inBuf = new byte[6];
dis.readFully(inBuf);
if (inBuf[0] != 0) {
throw new ProxyException(ProxyInfo.ProxyType.SOCKS4,
"stream is closed");
"server returns VN " + inBuf[0]);
}
s += i;
}
if (buf[0] != 0) {
throw new ProxyException(ProxyInfo.ProxyType.SOCKS4,
"server returns VN " + buf[0]);
}
if (buf[1] != 90) {
try {
socket.close();
}
catch (Exception eee) {
}
String message = "ProxySOCKS4: server returns CD " + buf[1];
if (inBuf[1] != 90) {
String message = "ProxySOCKS4: server returns CD " + inBuf[1];
throw new ProxyException(ProxyInfo.ProxyType.SOCKS4, message);
}
byte[] temp = new byte[2];
in.read(temp, 0, 2);
}
catch (RuntimeException e) {
throw e;
}
catch (Exception e) {
try {
socket.close();
}
catch (Exception eee) {
}
throw new ProxyException(ProxyInfo.ProxyType.SOCKS4, e.toString());
inBuf = new byte[2];
dis.readFully(inBuf);
}
}
}

View file

@ -16,15 +16,16 @@
*/
package org.jivesoftware.smack.proxy;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
import java.util.logging.Logger;
import org.jivesoftware.smack.util.CloseableUtil;
import org.jivesoftware.smack.util.OutputStreamUtil;
/**
* Socket factory for Socks5 proxy.
@ -32,7 +33,6 @@ import org.jivesoftware.smack.util.CloseableUtil;
* @author Atul Aggarwal
*/
public class Socks5ProxySocketConnection implements ProxySocketConnection {
private static final Logger LOGGER = Logger.getLogger(Socks5ProxySocketConnection.class.getName());
private final ProxyInfo proxy;
@ -43,22 +43,18 @@ public class Socks5ProxySocketConnection implements ProxySocketConnection {
@Override
public void connect(Socket socket, String host, int port, int timeout)
throws IOException {
InputStream in = null;
OutputStream out = null;
String proxy_host = proxy.getProxyAddress();
int proxy_port = proxy.getProxyPort();
String user = proxy.getProxyUsername();
String passwd = proxy.getProxyPassword();
try {
socket.connect(new InetSocketAddress(proxy_host, proxy_port), timeout);
in = socket.getInputStream();
out = socket.getOutputStream();
InputStream in = socket.getInputStream();
DataInputStream dis = new DataInputStream(in);
OutputStream out = socket.getOutputStream();
socket.setTcpNoDelay(true);
byte[] buf = new byte[1024];
int index = 0;
ByteArrayOutputStream outBuf = new ByteArrayOutputStream();
byte[] inBuf;
/*
+----+----------+----------+
@ -81,13 +77,13 @@ public class Socks5ProxySocketConnection implements ProxySocketConnection {
o X'FF' NO ACCEPTABLE METHODS
*/
buf[index++] = 5;
outBuf.write(5);
buf[index++] = 2;
buf[index++] = 0; // NO AUTHENTICATION REQUIRED
buf[index++] = 2; // USERNAME/PASSWORD
outBuf.write(2);
outBuf.write(0); // NO AUTHENTICATION REQUIRED
outBuf.write(2); // USERNAME/PASSWORD
out.write(buf, 0, index);
OutputStreamUtil.writeResetAndFlush(outBuf, out);
/*
The server selects from one of the methods given in METHODS, and
@ -99,10 +95,11 @@ public class Socks5ProxySocketConnection implements ProxySocketConnection {
| 1 | 1 |
+----+--------+
*/
fill(in, buf, 2);
inBuf = new byte[2];
dis.readFully(inBuf);
boolean check = false;
switch (buf[1] & 0xff) {
switch (inBuf[1] & 0xff) {
case 0: // NO AUTHENTICATION REQUIRED
check = true;
break;
@ -130,20 +127,16 @@ public class Socks5ProxySocketConnection implements ProxySocketConnection {
PASSWD field that follows. The PASSWD field contains the password
association with the given UNAME.
*/
index = 0;
buf[index++] = 1;
buf[index++] = (byte) user.length();
outBuf.write(1);
byte[] userBytes = user.getBytes(StandardCharsets.UTF_8);
System.arraycopy(userBytes, 0, buf, index,
user.length());
index += user.length();
byte[] passwordBytes = passwd.getBytes(StandardCharsets.UTF_8);
buf[index++] = (byte) passwordBytes.length;
System.arraycopy(passwordBytes, 0, buf, index,
passwd.length());
index += passwd.length();
OutputStreamUtil.writeByteSafe(outBuf, userBytes.length, "Username to long");
outBuf.write(userBytes);
out.write(buf, 0, index);
byte[] passwordBytes = passwd.getBytes(StandardCharsets.UTF_8);
OutputStreamUtil.writeByteSafe(outBuf, passwordBytes.length, "Password to long");
outBuf.write(passwordBytes);
OutputStreamUtil.writeResetAndFlush(outBuf, out);
/*
The server verifies the supplied UNAME and PASSWD, and sends the
@ -159,8 +152,9 @@ public class Socks5ProxySocketConnection implements ProxySocketConnection {
`failure' (STATUS value other than X'00') status, it MUST close the
connection.
*/
fill(in, buf, 2);
if (buf[1] == 0) {
inBuf = new byte[2];
dis.readFully(inBuf);
if (inBuf[1] == 0) {
check = true;
}
break;
@ -168,7 +162,6 @@ public class Socks5ProxySocketConnection implements ProxySocketConnection {
}
if (!check) {
CloseableUtil.maybeClose(socket, LOGGER);
throw new ProxyException(ProxyInfo.ProxyType.SOCKS5,
"fail in SOCKS5 proxy");
}
@ -199,21 +192,19 @@ public class Socks5ProxySocketConnection implements ProxySocketConnection {
order
*/
index = 0;
buf[index++] = 5;
buf[index++] = 1; // CONNECT
buf[index++] = 0;
outBuf.write(5);
outBuf.write(1); // CONNECT
outBuf.write(0);
byte[] hostb = host.getBytes(StandardCharsets.UTF_8);
int len = hostb.length;
buf[index++] = 3; // DOMAINNAME
buf[index++] = (byte) len;
System.arraycopy(hostb, 0, buf, index, len);
index += len;
buf[index++] = (byte) (port >>> 8);
buf[index++] = (byte) (port & 0xff);
outBuf.write(3); // DOMAINNAME
OutputStreamUtil.writeByteSafe(outBuf, len, "Hostname too long");
outBuf.write(hostb);
outBuf.write(port >>> 8);
outBuf.write(port & 0xff);
out.write(buf, 0, index);
OutputStreamUtil.writeResetAndFlush(outBuf, out);
/*
The SOCKS request information is sent by the client as soon as it has
@ -250,49 +241,33 @@ public class Socks5ProxySocketConnection implements ProxySocketConnection {
o BND.PORT server bound port in network octet order
*/
fill(in, buf, 4);
inBuf = new byte[4];
dis.readFully(inBuf);
if (buf[1] != 0) {
CloseableUtil.maybeClose(socket, LOGGER);
if (inBuf[1] != 0) {
throw new ProxyException(ProxyInfo.ProxyType.SOCKS5,
"server returns " + buf[1]);
"server returns " + inBuf[1]);
}
switch (buf[3] & 0xff) {
final int addressBytes;
// TODO: Use Byte.toUnsignedInt() once Smack's minimum Android SDK level is 26 or higher.
final int atyp = inBuf[3] & 0xff;
switch (atyp) {
case 1:
fill(in, buf, 6);
addressBytes = 4;
break;
case 3:
fill(in, buf, 1);
fill(in, buf, (buf[0] & 0xff) + 2);
byte domainnameLengthByte = dis.readByte();
// TODO: Use Byte.toUnsignedInt() once Smack's minimum Android SDK level is 26 or higher.
addressBytes = domainnameLengthByte & 0xff;
break;
case 4:
fill(in, buf, 18);
addressBytes = 16;
break;
default:
throw new IOException("Unknown ATYP value: " + atyp);
}
inBuf = new byte[addressBytes + 2];
dis.readFully(inBuf);
}
}
catch (RuntimeException e) {
throw e;
}
catch (Exception e) {
CloseableUtil.maybeClose(socket, LOGGER);
// TODO convert to IOException(e) when minimum Android API level is 9 or higher
throw new IOException(e.getLocalizedMessage());
}
}
private static void fill(InputStream in, byte[] buf, int len)
throws IOException {
int s = 0;
while (s < len) {
int i = in.read(buf, s, len - s);
if (i <= 0) {
throw new ProxyException(ProxyInfo.ProxyType.SOCKS5, "stream " +
"is closed");
}
s += i;
}
}
}

View file

@ -0,0 +1,39 @@
/**
*
* Copyright 2019 Florian Schmaus
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jivesoftware.smack.util;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
public class OutputStreamUtil {
public static void writeByteSafe(OutputStream outputStream, int i, String message) throws IOException {
if (i < 0 || i > 0xff) {
throw new IOException(message + ". The value " + i + " is not within the allowed range for bytes");
}
outputStream.write(i);
}
public static void writeResetAndFlush(ByteArrayOutputStream byteArrayOutputStream, OutputStream outputStream)
throws IOException {
byteArrayOutputStream.writeTo(outputStream);
byteArrayOutputStream.reset();
outputStream.flush();
}
}

View file

@ -45,7 +45,7 @@ public class Base32 {
}
};
private static final String ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZ2345678";
private static final String ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567";
public static StringEncoder<String> getStringEncoder() {
return base32Stringencoder;

View file

@ -16,8 +16,8 @@
*/
package org.jivesoftware.smack.parsing;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.jivesoftware.smack.test.util.CharSequenceEquals.equalsCharSequence;
import static org.junit.Assert.assertThat;
import java.io.IOException;

View file

@ -44,6 +44,7 @@ import org.jivesoftware.smack.util.StringUtils;
import org.jivesoftware.smackx.disco.ServiceDiscoveryManager;
import org.jivesoftware.smackx.forward.packet.Forwarded;
import org.jivesoftware.smackx.mam.MamManager.MamQueryArgs;
import org.jivesoftware.smackx.mam.element.MamElements;
import org.jivesoftware.smackx.mam.element.MamElements.MamResultExtension;
import org.jivesoftware.smackx.mam.element.MamFinIQ;

View file

@ -16,10 +16,10 @@
*/
package org.jivesoftware.smackx.forward;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.jivesoftware.smack.test.util.CharSequenceEquals.equalsCharSequence;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;
import java.util.Properties;

View file

@ -16,9 +16,9 @@
*/
package org.jivesoftware.smackx.iqversion;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.jivesoftware.smack.test.util.CharSequenceEquals.equalsCharSequence;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import org.jivesoftware.smack.DummyConnection;

View file

@ -16,10 +16,10 @@
*/
package org.jivesoftware.smackx.ping;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.jivesoftware.smack.test.util.CharSequenceEquals.equalsCharSequence;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.jxmpp.jid.JidTestUtil.DUMMY_AT_EXAMPLE_ORG;

View file

@ -16,9 +16,9 @@
*/
package org.jivesoftware.smackx.pubsub.provider;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.jivesoftware.smack.test.util.CharSequenceEquals.equalsCharSequence;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
import java.util.List;

View file

@ -16,11 +16,11 @@
*/
package org.jivesoftware.smackx.receipts;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.jivesoftware.smack.test.util.CharSequenceEquals.equalsCharSequence;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import java.util.Properties;

View file

@ -17,8 +17,8 @@
package org.jivesoftware.smackx.xhtmlim.provider;
import static org.hamcrest.CoreMatchers.instanceOf;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.jivesoftware.smack.test.util.CharSequenceEquals.equalsCharSequence;
import static org.junit.Assert.assertThat;
import java.io.IOException;
import java.io.InputStream;

View file

@ -1,6 +1,6 @@
/**
*
* Copyright 2015-2019 Florian Schmaus
* Copyright 2015-2020 Florian Schmaus
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -19,6 +19,7 @@ package org.igniterealtime.smack.inttest.unittest;
import static org.igniterealtime.smack.inttest.SmackIntegrationTestUnitTestUtil.getFrameworkForUnitTest;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertThrows;
import static org.junit.Assert.assertTrue;
import java.io.IOException;
@ -43,15 +44,10 @@ import org.igniterealtime.smack.inttest.SmackIntegrationTestFramework;
import org.igniterealtime.smack.inttest.SmackIntegrationTestFramework.TestRunResult;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
public class SmackIntegrationTestFrameworkUnitTest {
@Rule
public ExpectedException expectedException = ExpectedException.none();
private static boolean beforeClassInvoked;
private static boolean afterClassInvoked;
@ -68,10 +64,10 @@ public class SmackIntegrationTestFrameworkUnitTest {
@Test
public void throwsRuntimeExceptionsTest() throws KeyManagementException, NoSuchAlgorithmException, SmackException,
IOException, XMPPException, InterruptedException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
expectedException.expect(RuntimeException.class);
expectedException.expectMessage(ThrowsRuntimeExceptionDummyTest.RUNTIME_EXCEPTION_MESSAGE);
DummySmackIntegrationTestFramework sinttest = getFrameworkForUnitTest(ThrowsRuntimeExceptionDummyTest.class);
assertThrows(RuntimeException.class, () -> {
sinttest.run();
});
}
public static class ThrowsRuntimeExceptionDummyTest extends AbstractSmackIntegrationTest {

View file

@ -274,13 +274,13 @@ public class OmemoMessageBuilder<T_IdKeyPair, T_IdKey, T_PreKey, T_SigPreKey, T_
}
/**
* Generate a 16 byte initialization vector for AES encryption.
* Generate a 12 byte initialization vector for AES encryption.
*
* @return iv TODO javadoc me please
* @return iv initialization vector
*/
public static byte[] generateIv() {
SecureRandom random = new SecureRandom();
byte[] iv = new byte[16];
byte[] iv = new byte[12];
random.nextBytes(iv);
return iv;
}

View file

@ -606,6 +606,7 @@ public class XMPPTCPConnection extends AbstractXMPPConnection {
try {
proxyInfo.getProxySocketConnection().connect(socket, host, port, timeout);
} catch (IOException e) {
CloseableUtil.maybeClose(socket, LOGGER);
hostAddress.setException(e);
failedAddresses.add(hostAddress);
continue;