mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-12 18:59:06 +01:00
Compare commits
8 Commits
parent-blo
...
misc-code-
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
189ec874cc | ||
|
|
8c25732d13 | ||
|
|
c59ef29cdb | ||
|
|
56ac755e96 | ||
|
|
98633f5e7c | ||
|
|
86130e845a | ||
|
|
8ecec8bcf5 | ||
|
|
ad9191b076 |
@@ -9,19 +9,22 @@ import org.briarproject.bramble.api.account.AccountManager;
|
||||
import org.briarproject.bramble.api.crypto.CryptoComponent;
|
||||
import org.briarproject.bramble.api.db.DatabaseConfig;
|
||||
import org.briarproject.bramble.api.identity.IdentityManager;
|
||||
import org.briarproject.bramble.util.IoUtils;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
import javax.annotation.concurrent.GuardedBy;
|
||||
import javax.inject.Inject;
|
||||
|
||||
import static java.util.logging.Logger.getLogger;
|
||||
import static org.briarproject.bramble.util.IoUtils.deleteFileOrDir;
|
||||
|
||||
class AndroidAccountManager extends AccountManagerImpl
|
||||
implements AccountManager {
|
||||
|
||||
private static final Logger LOG =
|
||||
Logger.getLogger(AndroidAccountManager.class.getName());
|
||||
getLogger(AndroidAccountManager.class.getName());
|
||||
|
||||
private static final String PREF_DB_KEY = "key";
|
||||
|
||||
@@ -37,7 +40,7 @@ class AndroidAccountManager extends AccountManagerImpl
|
||||
appContext = app.getApplicationContext();
|
||||
}
|
||||
|
||||
// Locking: stateChangeLock
|
||||
@GuardedBy("stateChangeLock")
|
||||
@Override
|
||||
@Nullable
|
||||
protected String loadEncryptedDatabaseKey() {
|
||||
@@ -47,7 +50,7 @@ class AndroidAccountManager extends AccountManagerImpl
|
||||
return key;
|
||||
}
|
||||
|
||||
// Locking: stateChangeLock
|
||||
@GuardedBy("stateChangeLock")
|
||||
@Nullable
|
||||
private String getDatabaseKeyFromPreferences() {
|
||||
String key = prefs.getString(PREF_DB_KEY, null);
|
||||
@@ -56,7 +59,7 @@ class AndroidAccountManager extends AccountManagerImpl
|
||||
return key;
|
||||
}
|
||||
|
||||
// Locking: stateChangeLock
|
||||
@GuardedBy("stateChangeLock")
|
||||
private void migrateDatabaseKeyToFile(String key) {
|
||||
if (storeEncryptedDatabaseKey(key)) {
|
||||
if (prefs.edit().remove(PREF_DB_KEY).commit())
|
||||
@@ -81,7 +84,7 @@ class AndroidAccountManager extends AccountManagerImpl
|
||||
return PreferenceManager.getDefaultSharedPreferences(appContext);
|
||||
}
|
||||
|
||||
// Locking: stateChangeLock
|
||||
@GuardedBy("stateChangeLock")
|
||||
private void deleteAppData(SharedPreferences... clear) {
|
||||
// Clear and commit shared preferences
|
||||
for (SharedPreferences prefs : clear) {
|
||||
@@ -97,7 +100,7 @@ class AndroidAccountManager extends AccountManagerImpl
|
||||
for (File child : children) {
|
||||
String name = child.getName();
|
||||
if (!name.equals("lib") && !name.equals("shared_prefs")) {
|
||||
IoUtils.deleteFileOrDir(child);
|
||||
deleteFileOrDir(child);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,8 +13,7 @@ import org.briarproject.bramble.api.lifecycle.Service;
|
||||
import org.briarproject.bramble.api.network.NetworkManager;
|
||||
import org.briarproject.bramble.api.network.NetworkStatus;
|
||||
import org.briarproject.bramble.api.network.event.NetworkStatusEvent;
|
||||
import org.briarproject.bramble.api.nullsafety.MethodsNotNullByDefault;
|
||||
import org.briarproject.bramble.api.nullsafety.ParametersNotNullByDefault;
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
import org.briarproject.bramble.api.system.Scheduler;
|
||||
|
||||
import java.util.concurrent.Future;
|
||||
@@ -34,16 +33,17 @@ import static android.net.ConnectivityManager.CONNECTIVITY_ACTION;
|
||||
import static android.net.ConnectivityManager.TYPE_WIFI;
|
||||
import static android.os.Build.VERSION.SDK_INT;
|
||||
import static android.os.PowerManager.ACTION_DEVICE_IDLE_MODE_CHANGED;
|
||||
import static java.util.Objects.requireNonNull;
|
||||
import static java.util.concurrent.TimeUnit.MINUTES;
|
||||
import static java.util.concurrent.TimeUnit.SECONDS;
|
||||
import static java.util.logging.Level.INFO;
|
||||
import static java.util.logging.Logger.getLogger;
|
||||
|
||||
@MethodsNotNullByDefault
|
||||
@ParametersNotNullByDefault
|
||||
@NotNullByDefault
|
||||
class AndroidNetworkManager implements NetworkManager, Service {
|
||||
|
||||
private static final Logger LOG =
|
||||
Logger.getLogger(AndroidNetworkManager.class.getName());
|
||||
getLogger(AndroidNetworkManager.class.getName());
|
||||
|
||||
// See android.net.wifi.WifiManager
|
||||
private static final String WIFI_AP_STATE_CHANGED_ACTION =
|
||||
@@ -56,6 +56,7 @@ class AndroidNetworkManager implements NetworkManager, Service {
|
||||
new AtomicReference<>();
|
||||
private final AtomicBoolean used = new AtomicBoolean(false);
|
||||
|
||||
@Nullable
|
||||
private volatile BroadcastReceiver networkStateReceiver = null;
|
||||
|
||||
@Inject
|
||||
@@ -89,9 +90,8 @@ class AndroidNetworkManager implements NetworkManager, Service {
|
||||
|
||||
@Override
|
||||
public NetworkStatus getNetworkStatus() {
|
||||
ConnectivityManager cm = (ConnectivityManager)
|
||||
appContext.getSystemService(CONNECTIVITY_SERVICE);
|
||||
if (cm == null) throw new AssertionError();
|
||||
ConnectivityManager cm = (ConnectivityManager) requireNonNull(
|
||||
appContext.getSystemService(CONNECTIVITY_SERVICE));
|
||||
NetworkInfo net = cm.getActiveNetworkInfo();
|
||||
boolean connected = net != null && net.isConnected();
|
||||
boolean wifi = connected && net.getType() == TYPE_WIFI;
|
||||
|
||||
@@ -24,7 +24,6 @@ import java.io.IOException;
|
||||
import java.security.SecureRandom;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.BlockingQueue;
|
||||
@@ -48,9 +47,11 @@ import static android.bluetooth.BluetoothAdapter.STATE_OFF;
|
||||
import static android.bluetooth.BluetoothAdapter.STATE_ON;
|
||||
import static android.bluetooth.BluetoothDevice.ACTION_FOUND;
|
||||
import static android.bluetooth.BluetoothDevice.EXTRA_DEVICE;
|
||||
import static java.util.Collections.shuffle;
|
||||
import static java.util.concurrent.TimeUnit.MILLISECONDS;
|
||||
import static java.util.logging.Level.INFO;
|
||||
import static java.util.logging.Level.WARNING;
|
||||
import static java.util.logging.Logger.getLogger;
|
||||
import static org.briarproject.bramble.util.PrivacyUtils.scrubMacAddress;
|
||||
|
||||
@MethodsNotNullByDefault
|
||||
@@ -58,7 +59,7 @@ import static org.briarproject.bramble.util.PrivacyUtils.scrubMacAddress;
|
||||
class AndroidBluetoothPlugin extends BluetoothPlugin<BluetoothServerSocket> {
|
||||
|
||||
private static final Logger LOG =
|
||||
Logger.getLogger(AndroidBluetoothPlugin.class.getName());
|
||||
getLogger(AndroidBluetoothPlugin.class.getName());
|
||||
|
||||
private static final int MAX_DISCOVERY_MS = 10_000;
|
||||
|
||||
@@ -259,7 +260,7 @@ class AndroidBluetoothPlugin extends BluetoothPlugin<BluetoothServerSocket> {
|
||||
appContext.unregisterReceiver(receiver);
|
||||
}
|
||||
// Shuffle the addresses so we don't always try the same one first
|
||||
Collections.shuffle(addresses);
|
||||
shuffle(addresses);
|
||||
return addresses;
|
||||
}
|
||||
|
||||
|
||||
@@ -32,17 +32,19 @@ import static android.net.ConnectivityManager.TYPE_WIFI;
|
||||
import static android.os.Build.VERSION.SDK_INT;
|
||||
import static java.util.Collections.emptyList;
|
||||
import static java.util.Collections.singletonList;
|
||||
import static java.util.Objects.requireNonNull;
|
||||
import static java.util.logging.Logger.getLogger;
|
||||
|
||||
@NotNullByDefault
|
||||
class AndroidLanTcpPlugin extends LanTcpPlugin implements EventListener {
|
||||
|
||||
private static final Logger LOG =
|
||||
getLogger(AndroidLanTcpPlugin.class.getName());
|
||||
|
||||
private static final byte[] WIFI_AP_ADDRESS_BYTES =
|
||||
{(byte) 192, (byte) 168, 43, 1};
|
||||
private static final InetAddress WIFI_AP_ADDRESS;
|
||||
|
||||
private static final Logger LOG =
|
||||
Logger.getLogger(AndroidLanTcpPlugin.class.getName());
|
||||
|
||||
static {
|
||||
try {
|
||||
WIFI_AP_ADDRESS = InetAddress.getByAddress(WIFI_AP_ADDRESS_BYTES);
|
||||
@@ -66,10 +68,8 @@ class AndroidLanTcpPlugin extends LanTcpPlugin implements EventListener {
|
||||
// Don't execute more than one connection status check at a time
|
||||
connectionStatusExecutor =
|
||||
new PoliteExecutor("AndroidLanTcpPlugin", ioExecutor, 1);
|
||||
ConnectivityManager connectivityManager = (ConnectivityManager)
|
||||
appContext.getSystemService(CONNECTIVITY_SERVICE);
|
||||
if (connectivityManager == null) throw new AssertionError();
|
||||
this.connectivityManager = connectivityManager;
|
||||
connectivityManager = (ConnectivityManager) requireNonNull(
|
||||
appContext.getSystemService(CONNECTIVITY_SERVICE));
|
||||
wifiManager = (WifiManager) appContext.getApplicationContext()
|
||||
.getSystemService(WIFI_SERVICE);
|
||||
socketFactory = SocketFactory.getDefault();
|
||||
|
||||
@@ -8,8 +8,7 @@ import android.os.PowerManager;
|
||||
|
||||
import org.briarproject.bramble.api.battery.BatteryManager;
|
||||
import org.briarproject.bramble.api.network.NetworkManager;
|
||||
import org.briarproject.bramble.api.nullsafety.MethodsNotNullByDefault;
|
||||
import org.briarproject.bramble.api.nullsafety.ParametersNotNullByDefault;
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
import org.briarproject.bramble.api.plugin.Backoff;
|
||||
import org.briarproject.bramble.api.plugin.duplex.DuplexPluginCallback;
|
||||
import org.briarproject.bramble.api.system.Clock;
|
||||
@@ -26,10 +25,10 @@ import javax.net.SocketFactory;
|
||||
import static android.content.Context.MODE_PRIVATE;
|
||||
import static android.content.Context.POWER_SERVICE;
|
||||
import static android.os.PowerManager.PARTIAL_WAKE_LOCK;
|
||||
import static java.util.Objects.requireNonNull;
|
||||
import static java.util.concurrent.TimeUnit.MINUTES;
|
||||
|
||||
@MethodsNotNullByDefault
|
||||
@ParametersNotNullByDefault
|
||||
@NotNullByDefault
|
||||
class AndroidTorPlugin extends TorPlugin {
|
||||
|
||||
// This tag may prevent Huawei's power manager from killing us
|
||||
@@ -52,8 +51,7 @@ class AndroidTorPlugin extends TorPlugin {
|
||||
appContext.getDir("tor", MODE_PRIVATE));
|
||||
this.appContext = appContext;
|
||||
PowerManager pm = (PowerManager)
|
||||
appContext.getSystemService(POWER_SERVICE);
|
||||
if (pm == null) throw new AssertionError();
|
||||
requireNonNull(appContext.getSystemService(POWER_SERVICE));
|
||||
wakeLock = new RenewableWakeLock(pm, scheduler, PARTIAL_WAKE_LOCK,
|
||||
WAKE_LOCK_TAG, 1, MINUTES);
|
||||
}
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
package org.briarproject.bramble.plugin.tor;
|
||||
|
||||
import android.content.Context;
|
||||
import android.os.Build;
|
||||
|
||||
import org.briarproject.bramble.api.battery.BatteryManager;
|
||||
import org.briarproject.bramble.api.event.EventBus;
|
||||
@@ -26,12 +25,15 @@ import java.util.logging.Logger;
|
||||
import javax.annotation.concurrent.Immutable;
|
||||
import javax.net.SocketFactory;
|
||||
|
||||
import static android.os.Build.VERSION.SDK_INT;
|
||||
import static java.util.logging.Logger.getLogger;
|
||||
|
||||
@Immutable
|
||||
@NotNullByDefault
|
||||
public class AndroidTorPluginFactory implements DuplexPluginFactory {
|
||||
|
||||
private static final Logger LOG =
|
||||
Logger.getLogger(AndroidTorPluginFactory.class.getName());
|
||||
getLogger(AndroidTorPluginFactory.class.getName());
|
||||
|
||||
private static final int MAX_LATENCY = 30 * 1000; // 30 seconds
|
||||
private static final int MAX_IDLE_TIME = 30 * 1000; // 30 seconds
|
||||
@@ -102,7 +104,7 @@ public class AndroidTorPluginFactory implements DuplexPluginFactory {
|
||||
return null;
|
||||
}
|
||||
// Use position-independent executable for SDK >= 16
|
||||
if (Build.VERSION.SDK_INT >= 16) architecture += "_pie";
|
||||
if (SDK_INT >= 16) architecture += "_pie";
|
||||
|
||||
Backoff backoff = backoffFactory.createBackoff(MIN_POLLING_INTERVAL,
|
||||
MAX_POLLING_INTERVAL, BACKOFF_BASE);
|
||||
|
||||
@@ -15,12 +15,13 @@ import java.util.logging.Logger;
|
||||
import javax.inject.Inject;
|
||||
|
||||
import static android.content.Context.TELEPHONY_SERVICE;
|
||||
import static java.util.logging.Logger.getLogger;
|
||||
|
||||
@NotNullByDefault
|
||||
class AndroidLocationUtils implements LocationUtils {
|
||||
|
||||
private static final Logger LOG =
|
||||
Logger.getLogger(AndroidLocationUtils.class.getName());
|
||||
getLogger(AndroidLocationUtils.class.getName());
|
||||
|
||||
private final Context appContext;
|
||||
|
||||
@@ -59,14 +60,14 @@ class AndroidLocationUtils implements LocationUtils {
|
||||
}
|
||||
|
||||
private String getCountryFromPhoneNetwork() {
|
||||
Object o = appContext.getSystemService(TELEPHONY_SERVICE);
|
||||
TelephonyManager tm = (TelephonyManager) o;
|
||||
return tm.getNetworkCountryIso();
|
||||
TelephonyManager tm = (TelephonyManager)
|
||||
appContext.getSystemService(TELEPHONY_SERVICE);
|
||||
return tm == null ? "" : tm.getNetworkCountryIso();
|
||||
}
|
||||
|
||||
private String getCountryFromSimCard() {
|
||||
Object o = appContext.getSystemService(TELEPHONY_SERVICE);
|
||||
TelephonyManager tm = (TelephonyManager) o;
|
||||
return tm.getSimCountryIso();
|
||||
TelephonyManager tm = (TelephonyManager)
|
||||
appContext.getSystemService(TELEPHONY_SERVICE);
|
||||
return tm == null ? "" : tm.getSimCountryIso();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,7 +8,6 @@ import android.content.ContentResolver;
|
||||
import android.content.Context;
|
||||
import android.net.wifi.WifiConfiguration;
|
||||
import android.net.wifi.WifiManager;
|
||||
import android.os.Build;
|
||||
import android.os.Parcel;
|
||||
import android.os.StrictMode;
|
||||
import android.provider.Settings;
|
||||
@@ -23,6 +22,9 @@ import javax.annotation.concurrent.Immutable;
|
||||
import javax.inject.Inject;
|
||||
|
||||
import static android.content.Context.WIFI_SERVICE;
|
||||
import static android.os.Build.FINGERPRINT;
|
||||
import static android.os.Build.SERIAL;
|
||||
import static android.os.Build.VERSION.SDK_INT;
|
||||
import static android.provider.Settings.Secure.ANDROID_ID;
|
||||
|
||||
@Immutable
|
||||
@@ -45,8 +47,8 @@ class AndroidSecureRandomProvider extends UnixSecureRandomProvider {
|
||||
out.writeInt(android.os.Process.myPid());
|
||||
out.writeInt(android.os.Process.myTid());
|
||||
out.writeInt(android.os.Process.myUid());
|
||||
if (Build.FINGERPRINT != null) out.writeUTF(Build.FINGERPRINT);
|
||||
if (Build.SERIAL != null) out.writeUTF(Build.SERIAL);
|
||||
if (FINGERPRINT != null) out.writeUTF(FINGERPRINT);
|
||||
if (SERIAL != null) out.writeUTF(SERIAL);
|
||||
ContentResolver contentResolver = appContext.getContentResolver();
|
||||
String id = Settings.Secure.getString(contentResolver, ANDROID_ID);
|
||||
if (id != null) out.writeUTF(id);
|
||||
@@ -74,15 +76,13 @@ class AndroidSecureRandomProvider extends UnixSecureRandomProvider {
|
||||
// Silence strict mode
|
||||
StrictMode.ThreadPolicy tp = StrictMode.allowThreadDiskWrites();
|
||||
super.writeSeed();
|
||||
if (Build.VERSION.SDK_INT >= 16 && Build.VERSION.SDK_INT <= 18)
|
||||
applyOpenSslFix();
|
||||
if (SDK_INT >= 16 && SDK_INT <= 18) applyOpenSslFix();
|
||||
StrictMode.setThreadPolicy(tp);
|
||||
}
|
||||
|
||||
// Based on https://android-developers.googleblog.com/2013/08/some-securerandom-thoughts.html
|
||||
private void applyOpenSslFix() {
|
||||
byte[] seed = new UnixSecureRandomSpi().engineGenerateSeed(
|
||||
SEED_LENGTH);
|
||||
byte[] seed = new UnixSecureRandomSpi().engineGenerateSeed(SEED_LENGTH);
|
||||
try {
|
||||
// Seed the OpenSSL PRNG
|
||||
Class.forName("org.apache.harmony.xnet.provider.jsse.NativeCrypto")
|
||||
|
||||
@@ -3,17 +3,20 @@ package org.briarproject.bramble.util;
|
||||
import android.annotation.SuppressLint;
|
||||
import android.bluetooth.BluetoothAdapter;
|
||||
import android.content.Context;
|
||||
import android.os.Build;
|
||||
import android.provider.Settings;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
import static android.content.Context.MODE_PRIVATE;
|
||||
import static android.os.Build.CPU_ABI;
|
||||
import static android.os.Build.CPU_ABI2;
|
||||
import static android.os.Build.SUPPORTED_ABIS;
|
||||
import static android.os.Build.VERSION.SDK_INT;
|
||||
import static java.util.Arrays.asList;
|
||||
import static org.briarproject.bramble.util.StringUtils.isNullOrEmpty;
|
||||
|
||||
public class AndroidUtils {
|
||||
|
||||
@@ -22,14 +25,13 @@ public class AndroidUtils {
|
||||
|
||||
private static final String STORED_REPORTS = "dev-reports";
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
public static Collection<String> getSupportedArchitectures() {
|
||||
List<String> abis = new ArrayList<>();
|
||||
if (SDK_INT >= 21) {
|
||||
abis.addAll(Arrays.asList(Build.SUPPORTED_ABIS));
|
||||
abis.addAll(asList(SUPPORTED_ABIS));
|
||||
} else {
|
||||
abis.add(Build.CPU_ABI);
|
||||
if (Build.CPU_ABI2 != null) abis.add(Build.CPU_ABI2);
|
||||
abis.add(CPU_ABI);
|
||||
if (CPU_ABI2 != null) abis.add(CPU_ABI2);
|
||||
}
|
||||
return abis;
|
||||
}
|
||||
@@ -49,7 +51,7 @@ public class AndroidUtils {
|
||||
}
|
||||
|
||||
private static boolean isValidBluetoothAddress(String address) {
|
||||
return !StringUtils.isNullOrEmpty(address)
|
||||
return !isNullOrEmpty(address)
|
||||
&& BluetoothAdapter.checkBluetoothAddress(address)
|
||||
&& !address.equals(FAKE_BLUETOOTH_ADDRESS);
|
||||
}
|
||||
|
||||
@@ -10,17 +10,19 @@ import java.util.concurrent.TimeUnit;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
import javax.annotation.concurrent.GuardedBy;
|
||||
import javax.annotation.concurrent.ThreadSafe;
|
||||
|
||||
import static java.util.concurrent.TimeUnit.MILLISECONDS;
|
||||
import static java.util.logging.Level.INFO;
|
||||
import static java.util.logging.Logger.getLogger;
|
||||
|
||||
@ThreadSafe
|
||||
@NotNullByDefault
|
||||
public class RenewableWakeLock {
|
||||
|
||||
private static final Logger LOG =
|
||||
Logger.getLogger(RenewableWakeLock.class.getName());
|
||||
getLogger(RenewableWakeLock.class.getName());
|
||||
|
||||
/**
|
||||
* Automatically release the lock this many milliseconds after it's due
|
||||
@@ -36,10 +38,12 @@ public class RenewableWakeLock {
|
||||
private final Runnable renewTask;
|
||||
|
||||
private final Object lock = new Object();
|
||||
@GuardedBy("lock")
|
||||
@Nullable
|
||||
private PowerManager.WakeLock wakeLock; // Locking: lock
|
||||
private PowerManager.WakeLock wakeLock;
|
||||
@GuardedBy("lock")
|
||||
@Nullable
|
||||
private ScheduledFuture future; // Locking: lock
|
||||
private ScheduledFuture future;
|
||||
|
||||
public RenewableWakeLock(PowerManager powerManager,
|
||||
ScheduledExecutorService scheduler, int levelAndFlags, String tag,
|
||||
|
||||
@@ -1,13 +1,14 @@
|
||||
package org.briarproject.bramble.api;
|
||||
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
import org.briarproject.bramble.util.StringUtils;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Comparator;
|
||||
|
||||
import javax.annotation.concurrent.ThreadSafe;
|
||||
|
||||
import static org.briarproject.bramble.util.StringUtils.toHexString;
|
||||
|
||||
/**
|
||||
* A wrapper around a byte array, to allow it to be stored in maps etc.
|
||||
*/
|
||||
@@ -56,8 +57,7 @@ public class Bytes implements Comparable<Bytes> {
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return getClass().getSimpleName() +
|
||||
"(" + StringUtils.toHexString(getBytes()) + ")";
|
||||
return getClass().getSimpleName() + "(" + toHexString(getBytes()) + ")";
|
||||
}
|
||||
|
||||
public static class BytesComparator implements Comparator<Bytes> {
|
||||
|
||||
@@ -2,7 +2,6 @@ package org.briarproject.bramble.api;
|
||||
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.NoSuchElementException;
|
||||
@@ -10,6 +9,8 @@ import java.util.Set;
|
||||
|
||||
import javax.annotation.concurrent.NotThreadSafe;
|
||||
|
||||
import static java.util.Collections.unmodifiableSet;
|
||||
|
||||
@NotThreadSafe
|
||||
@NotNullByDefault
|
||||
public class Multiset<T> {
|
||||
@@ -96,6 +97,6 @@ public class Multiset<T> {
|
||||
* is unmodifiable.
|
||||
*/
|
||||
public Set<T> keySet() {
|
||||
return Collections.unmodifiableSet(map.keySet());
|
||||
return unmodifiableSet(map.keySet());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,10 +5,11 @@ import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
import org.briarproject.bramble.api.sync.MessageId;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
|
||||
import javax.annotation.concurrent.Immutable;
|
||||
|
||||
import static java.util.Collections.emptyList;
|
||||
|
||||
@Immutable
|
||||
@NotNullByDefault
|
||||
public class BdfMessageContext {
|
||||
@@ -23,7 +24,7 @@ public class BdfMessageContext {
|
||||
}
|
||||
|
||||
public BdfMessageContext(BdfDictionary dictionary) {
|
||||
this(dictionary, Collections.emptyList());
|
||||
this(dictionary, emptyList());
|
||||
}
|
||||
|
||||
public BdfDictionary getDictionary() {
|
||||
|
||||
@@ -16,6 +16,7 @@ import java.util.logging.Logger;
|
||||
|
||||
import javax.annotation.concurrent.Immutable;
|
||||
|
||||
import static java.util.logging.Logger.getLogger;
|
||||
import static org.briarproject.bramble.api.transport.TransportConstants.MAX_CLOCK_DIFFERENCE;
|
||||
|
||||
@Immutable
|
||||
@@ -23,7 +24,7 @@ import static org.briarproject.bramble.api.transport.TransportConstants.MAX_CLOC
|
||||
public abstract class BdfMessageValidator implements MessageValidator {
|
||||
|
||||
protected static final Logger LOG =
|
||||
Logger.getLogger(BdfMessageValidator.class.getName());
|
||||
getLogger(BdfMessageValidator.class.getName());
|
||||
|
||||
protected final ClientHelper clientHelper;
|
||||
protected final MetadataEncoder metadataEncoder;
|
||||
|
||||
@@ -4,7 +4,6 @@ import org.briarproject.bramble.api.Bytes;
|
||||
import org.briarproject.bramble.api.FormatException;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.TreeMap;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
@@ -24,9 +23,9 @@ public class BdfDictionary extends TreeMap<String, Object> {
|
||||
* );
|
||||
* </pre>
|
||||
*/
|
||||
public static BdfDictionary of(Entry<String, ?>... entries) {
|
||||
public static BdfDictionary of(BdfEntry... entries) {
|
||||
BdfDictionary d = new BdfDictionary();
|
||||
for (Entry<String, ?> e : entries) d.put(e.getKey(), e.getValue());
|
||||
for (BdfEntry e : entries) d.put(e.getKey(), e.getValue());
|
||||
return d;
|
||||
}
|
||||
|
||||
|
||||
@@ -4,12 +4,12 @@ import org.briarproject.bramble.api.Bytes;
|
||||
import org.briarproject.bramble.api.FormatException;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
import javax.annotation.concurrent.NotThreadSafe;
|
||||
|
||||
import static java.util.Arrays.asList;
|
||||
import static org.briarproject.bramble.api.data.BdfDictionary.NULL_VALUE;
|
||||
|
||||
@NotThreadSafe
|
||||
@@ -22,7 +22,7 @@ public class BdfList extends ArrayList<Object> {
|
||||
* </pre>
|
||||
*/
|
||||
public static BdfList of(Object... items) {
|
||||
return new BdfList(Arrays.asList(items));
|
||||
return new BdfList(asList(items));
|
||||
}
|
||||
|
||||
public BdfList() {
|
||||
@@ -33,6 +33,7 @@ public class BdfList extends ArrayList<Object> {
|
||||
super(items);
|
||||
}
|
||||
|
||||
@SuppressWarnings("BooleanMethodIsAlwaysInverted")
|
||||
private boolean isInRange(int index) {
|
||||
return index >= 0 && index < size();
|
||||
}
|
||||
|
||||
@@ -3,11 +3,12 @@ package org.briarproject.bramble.api.db;
|
||||
import org.briarproject.bramble.api.event.Event;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import javax.annotation.concurrent.NotThreadSafe;
|
||||
|
||||
import static java.util.Collections.emptyList;
|
||||
|
||||
/**
|
||||
* A wrapper around a database transaction. Transactions are not thread-safe.
|
||||
*/
|
||||
@@ -53,7 +54,7 @@ public class Transaction {
|
||||
* Returns any events attached to the transaction.
|
||||
*/
|
||||
public List<Event> getEvents() {
|
||||
if (events == null) return Collections.emptyList();
|
||||
if (events == null) return emptyList();
|
||||
return events;
|
||||
}
|
||||
|
||||
|
||||
@@ -2,12 +2,12 @@ package org.briarproject.bramble.api.identity;
|
||||
|
||||
import org.briarproject.bramble.api.Nameable;
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
import org.briarproject.bramble.util.StringUtils;
|
||||
|
||||
import javax.annotation.concurrent.Immutable;
|
||||
|
||||
import static org.briarproject.bramble.api.identity.AuthorConstants.MAX_AUTHOR_NAME_LENGTH;
|
||||
import static org.briarproject.bramble.api.identity.AuthorConstants.MAX_PUBLIC_KEY_LENGTH;
|
||||
import static org.briarproject.bramble.util.StringUtils.toUtf8;
|
||||
|
||||
/**
|
||||
* A pseudonym for a user.
|
||||
@@ -28,7 +28,7 @@ public class Author implements Nameable {
|
||||
|
||||
public Author(AuthorId id, int formatVersion, String name,
|
||||
byte[] publicKey) {
|
||||
int nameLength = StringUtils.toUtf8(name).length;
|
||||
int nameLength = toUtf8(name).length;
|
||||
if (nameLength == 0 || nameLength > MAX_AUTHOR_NAME_LENGTH)
|
||||
throw new IllegalArgumentException();
|
||||
if (publicKey.length == 0 || publicKey.length > MAX_PUBLIC_KEY_LENGTH)
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
package org.briarproject.bramble.api.plugin;
|
||||
|
||||
import org.briarproject.bramble.util.StringUtils;
|
||||
import static org.briarproject.bramble.util.StringUtils.toUtf8;
|
||||
|
||||
/**
|
||||
* Type-safe wrapper for a namespaced string that uniquely identifies a
|
||||
@@ -11,12 +11,12 @@ public class TransportId {
|
||||
/**
|
||||
* The maximum length of a transport identifier in UTF-8 bytes.
|
||||
*/
|
||||
public static int MAX_TRANSPORT_ID_LENGTH = 100;
|
||||
public static final int MAX_TRANSPORT_ID_LENGTH = 100;
|
||||
|
||||
private final String id;
|
||||
|
||||
public TransportId(String id) {
|
||||
int length = StringUtils.toUtf8(id).length;
|
||||
int length = toUtf8(id).length;
|
||||
if (length == 0 || length > MAX_TRANSPORT_ID_LENGTH)
|
||||
throw new IllegalArgumentException();
|
||||
this.id = id;
|
||||
|
||||
@@ -1,10 +1,11 @@
|
||||
package org.briarproject.bramble.api.sync;
|
||||
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
import org.briarproject.bramble.util.StringUtils;
|
||||
|
||||
import javax.annotation.concurrent.Immutable;
|
||||
|
||||
import static org.briarproject.bramble.util.StringUtils.toUtf8;
|
||||
|
||||
/**
|
||||
* Type-safe wrapper for a namespaced string that uniquely identifies a sync
|
||||
* client.
|
||||
@@ -16,12 +17,12 @@ public class ClientId implements Comparable<ClientId> {
|
||||
/**
|
||||
* The maximum length of a client identifier in UTF-8 bytes.
|
||||
*/
|
||||
public static int MAX_CLIENT_ID_LENGTH = 100;
|
||||
public static final int MAX_CLIENT_ID_LENGTH = 100;
|
||||
|
||||
private final String id;
|
||||
|
||||
public ClientId(String id) {
|
||||
int length = StringUtils.toUtf8(id).length;
|
||||
int length = toUtf8(id).length;
|
||||
if (length == 0 || length > MAX_CLIENT_ID_LENGTH)
|
||||
throw new IllegalArgumentException();
|
||||
this.id = id;
|
||||
|
||||
@@ -4,10 +4,11 @@ import org.briarproject.bramble.api.db.Metadata;
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
|
||||
import javax.annotation.concurrent.Immutable;
|
||||
|
||||
import static java.util.Collections.emptyList;
|
||||
|
||||
@Immutable
|
||||
@NotNullByDefault
|
||||
public class MessageContext {
|
||||
@@ -22,7 +23,7 @@ public class MessageContext {
|
||||
}
|
||||
|
||||
public MessageContext(Metadata metadata) {
|
||||
this(metadata, Collections.emptyList());
|
||||
this(metadata, emptyList());
|
||||
}
|
||||
|
||||
public Metadata getMetadata() {
|
||||
|
||||
@@ -16,12 +16,13 @@ import java.util.logging.Logger;
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
import static java.util.logging.Level.WARNING;
|
||||
import static java.util.logging.Logger.getLogger;
|
||||
import static org.briarproject.bramble.util.LogUtils.logException;
|
||||
|
||||
@NotNullByDefault
|
||||
public class IoUtils {
|
||||
|
||||
private static final Logger LOG = Logger.getLogger(IoUtils.class.getName());
|
||||
private static final Logger LOG = getLogger(IoUtils.class.getName());
|
||||
|
||||
public static void deleteFileOrDir(File f) {
|
||||
if (f.isFile()) {
|
||||
|
||||
@@ -7,13 +7,15 @@ import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
import static org.briarproject.bramble.util.StringUtils.toUtf8;
|
||||
|
||||
@NotNullByDefault
|
||||
public class ValidationUtils {
|
||||
|
||||
public static void checkLength(@Nullable String s, int minLength,
|
||||
int maxLength) throws FormatException {
|
||||
if (s != null) {
|
||||
int length = StringUtils.toUtf8(s).length;
|
||||
int length = toUtf8(s).length;
|
||||
if (length < minLength) throw new FormatException();
|
||||
if (length > maxLength) throw new FormatException();
|
||||
}
|
||||
@@ -21,7 +23,7 @@ public class ValidationUtils {
|
||||
|
||||
public static void checkLength(@Nullable String s, int length)
|
||||
throws FormatException {
|
||||
if (s != null && StringUtils.toUtf8(s).length != length)
|
||||
if (s != null && toUtf8(s).length != length)
|
||||
throw new FormatException();
|
||||
}
|
||||
|
||||
|
||||
@@ -8,6 +8,7 @@ import java.util.Collections;
|
||||
import java.util.Iterator;
|
||||
import java.util.Map.Entry;
|
||||
|
||||
import static java.util.Collections.singletonMap;
|
||||
import static org.briarproject.bramble.api.data.BdfDictionary.NULL_VALUE;
|
||||
import static org.junit.Assert.assertArrayEquals;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
@@ -19,15 +20,15 @@ public class BdfDictionaryTest extends BrambleTestCase {
|
||||
public void testConstructors() {
|
||||
assertEquals(Collections.<String, Object>emptyMap(),
|
||||
new BdfDictionary());
|
||||
assertEquals(Collections.singletonMap("foo", NULL_VALUE),
|
||||
new BdfDictionary(Collections.singletonMap("foo", NULL_VALUE)));
|
||||
assertEquals(singletonMap("foo", NULL_VALUE),
|
||||
new BdfDictionary(singletonMap("foo", NULL_VALUE)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFactoryMethod() {
|
||||
assertEquals(Collections.<String, Object>emptyMap(),
|
||||
BdfDictionary.of());
|
||||
assertEquals(Collections.singletonMap("foo", NULL_VALUE),
|
||||
assertEquals(singletonMap("foo", NULL_VALUE),
|
||||
BdfDictionary.of(new BdfEntry("foo", NULL_VALUE)));
|
||||
}
|
||||
|
||||
@@ -67,7 +68,7 @@ public class BdfDictionaryTest extends BrambleTestCase {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testKeySetIteratorIsOrderedByKeys() throws Exception {
|
||||
public void testKeySetIteratorIsOrderedByKeys() {
|
||||
BdfDictionary d = new BdfDictionary();
|
||||
d.put("a", 1);
|
||||
d.put("d", 4);
|
||||
@@ -86,7 +87,7 @@ public class BdfDictionaryTest extends BrambleTestCase {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testValuesIteratorIsOrderedByKeys() throws Exception {
|
||||
public void testValuesIteratorIsOrderedByKeys() {
|
||||
BdfDictionary d = new BdfDictionary();
|
||||
d.put("a", 1);
|
||||
d.put("d", 4);
|
||||
@@ -105,7 +106,7 @@ public class BdfDictionaryTest extends BrambleTestCase {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEntrySetIteratorIsOrderedByKeys() throws Exception {
|
||||
public void testEntrySetIteratorIsOrderedByKeys() {
|
||||
BdfDictionary d = new BdfDictionary();
|
||||
d.put("a", 1);
|
||||
d.put("d", 4);
|
||||
|
||||
@@ -5,26 +5,31 @@ import org.briarproject.bramble.api.FormatException;
|
||||
import org.briarproject.bramble.test.BrambleTestCase;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.Random;
|
||||
|
||||
import static java.util.Arrays.asList;
|
||||
import static java.util.Collections.emptyList;
|
||||
import static org.briarproject.bramble.api.data.BdfDictionary.NULL_VALUE;
|
||||
import static org.briarproject.bramble.test.TestUtils.getRandomBytes;
|
||||
import static org.briarproject.bramble.util.StringUtils.getRandomString;
|
||||
import static org.junit.Assert.assertArrayEquals;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
public class BdfListTest extends BrambleTestCase {
|
||||
|
||||
private final Random random = new Random();
|
||||
|
||||
@Test
|
||||
public void testConstructors() {
|
||||
assertEquals(Collections.emptyList(), new BdfList());
|
||||
assertEquals(Arrays.asList(1, 2, NULL_VALUE),
|
||||
new BdfList(Arrays.asList(1, 2, NULL_VALUE)));
|
||||
assertEquals(emptyList(), new BdfList());
|
||||
assertEquals(asList(1, 2, NULL_VALUE),
|
||||
new BdfList(asList(1, 2, NULL_VALUE)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFactoryMethod() {
|
||||
assertEquals(Collections.emptyList(), BdfList.of());
|
||||
assertEquals(Arrays.asList(1, 2, NULL_VALUE),
|
||||
assertEquals(emptyList(), BdfList.of());
|
||||
assertEquals(asList(1, 2, NULL_VALUE),
|
||||
BdfList.of(1, 2, NULL_VALUE));
|
||||
}
|
||||
|
||||
@@ -64,22 +69,21 @@ public class BdfListTest extends BrambleTestCase {
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("ConstantConditions")
|
||||
public void testIndexOutOfBoundsReturnsDefaultValue() throws Exception {
|
||||
public void testIndexOutOfBoundsReturnsDefaultValue() {
|
||||
BdfList list = BdfList.of(1, 2, 3);
|
||||
boolean defaultBoolean = true;
|
||||
boolean defaultBoolean = random.nextBoolean();
|
||||
assertEquals(defaultBoolean, list.getBoolean(-1, defaultBoolean));
|
||||
assertEquals(defaultBoolean, list.getBoolean(3, defaultBoolean));
|
||||
Long defaultLong = 123L;
|
||||
Long defaultLong = random.nextLong();
|
||||
assertEquals(defaultLong, list.getLong(-1, defaultLong));
|
||||
assertEquals(defaultLong, list.getLong(3, defaultLong));
|
||||
Double defaultDouble = 1.23;
|
||||
Double defaultDouble = random.nextDouble();
|
||||
assertEquals(defaultDouble, list.getDouble(-1, defaultDouble));
|
||||
assertEquals(defaultDouble, list.getDouble(3, defaultDouble));
|
||||
String defaultString = "123";
|
||||
String defaultString = getRandomString(123);
|
||||
assertEquals(defaultString, list.getString(-1, defaultString));
|
||||
assertEquals(defaultString, list.getString(3, defaultString));
|
||||
byte[] defaultBytes = new byte[] {1, 2, 3};
|
||||
byte[] defaultBytes = getRandomBytes(123);
|
||||
assertArrayEquals(defaultBytes, list.getRaw(-1, defaultBytes));
|
||||
assertArrayEquals(defaultBytes, list.getRaw(3, defaultBytes));
|
||||
BdfList defaultList = BdfList.of(1, 2, 3);
|
||||
@@ -95,18 +99,17 @@ public class BdfListTest extends BrambleTestCase {
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("ConstantConditions")
|
||||
public void testWrongTypeReturnsDefaultValue() throws Exception {
|
||||
public void testWrongTypeReturnsDefaultValue() {
|
||||
BdfList list = BdfList.of(1, 2, 3, true);
|
||||
boolean defaultBoolean = true;
|
||||
boolean defaultBoolean = random.nextBoolean();
|
||||
assertEquals(defaultBoolean, list.getBoolean(0, defaultBoolean));
|
||||
Long defaultLong = 123L;
|
||||
Long defaultLong = random.nextLong();
|
||||
assertEquals(defaultLong, list.getLong(3, defaultLong));
|
||||
Double defaultDouble = 1.23;
|
||||
Double defaultDouble = random.nextDouble();
|
||||
assertEquals(defaultDouble, list.getDouble(0, defaultDouble));
|
||||
String defaultString = "123";
|
||||
String defaultString = getRandomString(123);
|
||||
assertEquals(defaultString, list.getString(0, defaultString));
|
||||
byte[] defaultBytes = new byte[] {1, 2, 3};
|
||||
byte[] defaultBytes = getRandomBytes(123);
|
||||
assertArrayEquals(defaultBytes, list.getRaw(0, defaultBytes));
|
||||
BdfList defaultList = BdfList.of(1, 2, 3);
|
||||
assertEquals(defaultList, list.getList(0, defaultList));
|
||||
|
||||
@@ -12,12 +12,10 @@ import org.briarproject.bramble.api.sync.Group;
|
||||
import org.briarproject.bramble.api.sync.GroupId;
|
||||
import org.briarproject.bramble.api.sync.Message;
|
||||
import org.briarproject.bramble.api.sync.MessageId;
|
||||
import org.briarproject.bramble.util.IoUtils;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
@@ -25,6 +23,7 @@ import java.util.Random;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
import static java.util.Arrays.asList;
|
||||
import static java.util.Collections.sort;
|
||||
import static org.briarproject.bramble.api.identity.Author.FORMAT_VERSION;
|
||||
import static org.briarproject.bramble.api.identity.AuthorConstants.MAX_AUTHOR_NAME_LENGTH;
|
||||
import static org.briarproject.bramble.api.identity.AuthorConstants.MAX_PUBLIC_KEY_LENGTH;
|
||||
@@ -33,6 +32,7 @@ import static org.briarproject.bramble.api.properties.TransportPropertyConstants
|
||||
import static org.briarproject.bramble.api.sync.ClientId.MAX_CLIENT_ID_LENGTH;
|
||||
import static org.briarproject.bramble.api.sync.SyncConstants.MAX_GROUP_DESCRIPTOR_LENGTH;
|
||||
import static org.briarproject.bramble.api.sync.SyncConstants.MAX_MESSAGE_BODY_LENGTH;
|
||||
import static org.briarproject.bramble.util.IoUtils.deleteFileOrDir;
|
||||
import static org.briarproject.bramble.util.StringUtils.getRandomString;
|
||||
|
||||
public class TestUtils {
|
||||
@@ -48,8 +48,10 @@ public class TestUtils {
|
||||
}
|
||||
|
||||
public static void deleteTestDirectory(File testDir) {
|
||||
IoUtils.deleteFileOrDir(testDir);
|
||||
testDir.getParentFile().delete(); // Delete if empty
|
||||
deleteFileOrDir(testDir);
|
||||
// Delete parent directory only if it's empty
|
||||
//noinspection ResultOfMethodCallIgnored
|
||||
testDir.getParentFile().delete();
|
||||
}
|
||||
|
||||
public static byte[] getRandomBytes(int length) {
|
||||
@@ -145,7 +147,7 @@ public class TestUtils {
|
||||
if (size == 0) throw new IllegalArgumentException();
|
||||
List<Double> sorted = new ArrayList<>(size);
|
||||
for (Number n : samples) sorted.add(n.doubleValue());
|
||||
Collections.sort(sorted);
|
||||
sort(sorted);
|
||||
if (size % 2 == 1) return sorted.get(size / 2);
|
||||
double low = sorted.get(size / 2 - 1), high = sorted.get(size / 2);
|
||||
return (low + high) / 2;
|
||||
|
||||
@@ -10,6 +10,7 @@ import java.util.logging.Logger;
|
||||
import javax.annotation.concurrent.GuardedBy;
|
||||
|
||||
import static java.util.logging.Level.FINE;
|
||||
import static java.util.logging.Logger.getLogger;
|
||||
import static org.briarproject.bramble.util.LogUtils.now;
|
||||
|
||||
/**
|
||||
@@ -42,7 +43,7 @@ public class PoliteExecutor implements Executor {
|
||||
int maxConcurrentTasks) {
|
||||
this.delegate = delegate;
|
||||
this.maxConcurrentTasks = maxConcurrentTasks;
|
||||
log = Logger.getLogger(tag);
|
||||
log = getLogger(tag);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -9,6 +9,7 @@ import java.util.concurrent.TimeUnit;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import static java.util.logging.Level.FINE;
|
||||
import static java.util.logging.Logger.getLogger;
|
||||
import static org.briarproject.bramble.util.LogUtils.now;
|
||||
|
||||
@NotNullByDefault
|
||||
@@ -22,7 +23,7 @@ public class TimeLoggingExecutor extends ThreadPoolExecutor {
|
||||
RejectedExecutionHandler handler) {
|
||||
super(corePoolSize, maxPoolSize, keepAliveTime, unit, workQueue,
|
||||
handler);
|
||||
log = Logger.getLogger(tag);
|
||||
log = getLogger(tag);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -6,9 +6,7 @@ import org.briarproject.bramble.api.crypto.SecretKey;
|
||||
import org.briarproject.bramble.api.db.DatabaseConfig;
|
||||
import org.briarproject.bramble.api.identity.IdentityManager;
|
||||
import org.briarproject.bramble.api.identity.LocalAuthor;
|
||||
import org.briarproject.bramble.api.nullsafety.MethodsNotNullByDefault;
|
||||
import org.briarproject.bramble.api.nullsafety.ParametersNotNullByDefault;
|
||||
import org.briarproject.bramble.util.IoUtils;
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.File;
|
||||
@@ -19,19 +17,21 @@ import java.io.InputStreamReader;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
import javax.annotation.concurrent.GuardedBy;
|
||||
import javax.inject.Inject;
|
||||
|
||||
import static java.util.logging.Level.WARNING;
|
||||
import static java.util.logging.Logger.getLogger;
|
||||
import static org.briarproject.bramble.util.IoUtils.deleteFileOrDir;
|
||||
import static org.briarproject.bramble.util.LogUtils.logException;
|
||||
import static org.briarproject.bramble.util.StringUtils.fromHexString;
|
||||
import static org.briarproject.bramble.util.StringUtils.toHexString;
|
||||
|
||||
@MethodsNotNullByDefault
|
||||
@ParametersNotNullByDefault
|
||||
@NotNullByDefault
|
||||
class AccountManagerImpl implements AccountManager {
|
||||
|
||||
private static final Logger LOG =
|
||||
Logger.getLogger(AccountManagerImpl.class.getName());
|
||||
getLogger(AccountManagerImpl.class.getName());
|
||||
|
||||
private static final String DB_KEY_FILENAME = "db.key";
|
||||
private static final String DB_KEY_BACKUP_FILENAME = "db.key.bak";
|
||||
@@ -68,7 +68,7 @@ class AccountManagerImpl implements AccountManager {
|
||||
return databaseKey;
|
||||
}
|
||||
|
||||
// Locking: stateChangeLock
|
||||
@GuardedBy("stateChangeLock")
|
||||
@Nullable
|
||||
protected String loadEncryptedDatabaseKey() {
|
||||
String key = readDbKeyFromFile(dbKeyFile);
|
||||
@@ -83,7 +83,7 @@ class AccountManagerImpl implements AccountManager {
|
||||
return key;
|
||||
}
|
||||
|
||||
// Locking: stateChangeLock
|
||||
@GuardedBy("stateChangeLock")
|
||||
@Nullable
|
||||
private String readDbKeyFromFile(File f) {
|
||||
if (!f.exists()) {
|
||||
@@ -102,8 +102,8 @@ class AccountManagerImpl implements AccountManager {
|
||||
}
|
||||
}
|
||||
|
||||
// Locking: stateChangeLock
|
||||
protected boolean storeEncryptedDatabaseKey(String hex) {
|
||||
@GuardedBy("stateChangeLock")
|
||||
boolean storeEncryptedDatabaseKey(String hex) {
|
||||
LOG.info("Storing database key in file");
|
||||
// Create the directory if necessary
|
||||
if (databaseConfig.getDatabaseKeyDirectory().mkdirs())
|
||||
@@ -140,7 +140,7 @@ class AccountManagerImpl implements AccountManager {
|
||||
}
|
||||
}
|
||||
|
||||
// Locking: stateChangeLock
|
||||
@GuardedBy("stateChangeLock")
|
||||
private void writeDbKeyToFile(String key, File f) throws IOException {
|
||||
FileOutputStream out = new FileOutputStream(f);
|
||||
out.write(key.getBytes("UTF-8"));
|
||||
@@ -170,7 +170,7 @@ class AccountManagerImpl implements AccountManager {
|
||||
}
|
||||
}
|
||||
|
||||
// Locking: stateChangeLock
|
||||
@GuardedBy("stateChangeLock")
|
||||
private boolean encryptAndStoreDatabaseKey(SecretKey key, String password) {
|
||||
byte[] plaintext = key.getBytes();
|
||||
byte[] ciphertext = crypto.encryptWithPassword(plaintext, password);
|
||||
@@ -181,8 +181,8 @@ class AccountManagerImpl implements AccountManager {
|
||||
public void deleteAccount() {
|
||||
synchronized (stateChangeLock) {
|
||||
LOG.info("Deleting account");
|
||||
IoUtils.deleteFileOrDir(databaseConfig.getDatabaseKeyDirectory());
|
||||
IoUtils.deleteFileOrDir(databaseConfig.getDatabaseDirectory());
|
||||
deleteFileOrDir(databaseConfig.getDatabaseKeyDirectory());
|
||||
deleteFileOrDir(databaseConfig.getDatabaseDirectory());
|
||||
databaseKey = null;
|
||||
}
|
||||
}
|
||||
@@ -197,7 +197,7 @@ class AccountManagerImpl implements AccountManager {
|
||||
}
|
||||
}
|
||||
|
||||
// Locking: stateChangeLock
|
||||
@GuardedBy("stateChangeLock")
|
||||
@Nullable
|
||||
private SecretKey loadAndDecryptDatabaseKey(String password) {
|
||||
String hex = loadEncryptedDatabaseKey();
|
||||
|
||||
@@ -43,6 +43,7 @@ import java.util.logging.Logger;
|
||||
import javax.inject.Inject;
|
||||
|
||||
import static java.util.logging.Level.WARNING;
|
||||
import static java.util.logging.Logger.getLogger;
|
||||
import static org.briarproject.bramble.api.contact.RecordTypes.CONTACT_INFO;
|
||||
import static org.briarproject.bramble.api.identity.AuthorConstants.MAX_SIGNATURE_LENGTH;
|
||||
import static org.briarproject.bramble.util.LogUtils.logException;
|
||||
@@ -54,7 +55,7 @@ import static org.briarproject.bramble.util.ValidationUtils.checkSize;
|
||||
class ContactExchangeTaskImpl extends Thread implements ContactExchangeTask {
|
||||
|
||||
private static final Logger LOG =
|
||||
Logger.getLogger(ContactExchangeTaskImpl.class.getName());
|
||||
getLogger(ContactExchangeTaskImpl.class.getName());
|
||||
|
||||
private static final String SIGNING_LABEL_EXCHANGE =
|
||||
"org.briarproject.briar.contact/EXCHANGE";
|
||||
@@ -190,6 +191,7 @@ class ContactExchangeTaskImpl extends Thread implements ContactExchangeTask {
|
||||
streamWriter.sendEndOfStream();
|
||||
// Skip any remaining records from the incoming stream
|
||||
try {
|
||||
//noinspection InfiniteLoopStatement
|
||||
while (true) recordReader.readRecord();
|
||||
} catch (EOFException expected) {
|
||||
LOG.info("End of stream");
|
||||
|
||||
@@ -2,18 +2,20 @@ package org.briarproject.bramble.crypto;
|
||||
|
||||
import org.briarproject.bramble.api.FormatException;
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
import org.briarproject.bramble.util.StringUtils;
|
||||
|
||||
import static org.briarproject.bramble.util.StringUtils.fromHexString;
|
||||
import static org.briarproject.bramble.util.StringUtils.toHexString;
|
||||
|
||||
@NotNullByDefault
|
||||
class AsciiArmour {
|
||||
|
||||
static String wrap(byte[] b, int lineLength) {
|
||||
String wrapped = StringUtils.toHexString(b);
|
||||
String wrapped = toHexString(b);
|
||||
StringBuilder s = new StringBuilder();
|
||||
int length = wrapped.length();
|
||||
for (int i = 0; i < length; i += lineLength) {
|
||||
int end = Math.min(i + lineLength, length);
|
||||
s.append(wrapped.substring(i, end));
|
||||
s.append(wrapped, i, end);
|
||||
s.append("\r\n");
|
||||
}
|
||||
return s.toString();
|
||||
@@ -21,7 +23,7 @@ class AsciiArmour {
|
||||
|
||||
static byte[] unwrap(String s) throws FormatException {
|
||||
try {
|
||||
return StringUtils.fromHexString(s.replaceAll("[^0-9a-fA-F]", ""));
|
||||
return fromHexString(s.replaceAll("[^0-9a-fA-F]", ""));
|
||||
} catch (IllegalArgumentException e) {
|
||||
throw new FormatException();
|
||||
}
|
||||
|
||||
@@ -12,8 +12,6 @@ import org.briarproject.bramble.api.crypto.PublicKey;
|
||||
import org.briarproject.bramble.api.crypto.SecretKey;
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
import org.briarproject.bramble.api.system.SecureRandomProvider;
|
||||
import org.briarproject.bramble.util.ByteUtils;
|
||||
import org.briarproject.bramble.util.StringUtils;
|
||||
import org.spongycastle.crypto.CryptoException;
|
||||
import org.spongycastle.crypto.Digest;
|
||||
import org.spongycastle.crypto.digests.Blake2bDigest;
|
||||
@@ -30,16 +28,21 @@ import java.util.logging.Logger;
|
||||
import javax.annotation.Nullable;
|
||||
import javax.inject.Inject;
|
||||
|
||||
import static java.lang.System.arraycopy;
|
||||
import static java.util.logging.Level.INFO;
|
||||
import static java.util.logging.Logger.getLogger;
|
||||
import static org.briarproject.bramble.util.ByteUtils.INT_32_BYTES;
|
||||
import static org.briarproject.bramble.util.ByteUtils.readUint32;
|
||||
import static org.briarproject.bramble.util.ByteUtils.writeUint32;
|
||||
import static org.briarproject.bramble.util.LogUtils.logDuration;
|
||||
import static org.briarproject.bramble.util.LogUtils.now;
|
||||
import static org.briarproject.bramble.util.StringUtils.toUtf8;
|
||||
|
||||
@NotNullByDefault
|
||||
class CryptoComponentImpl implements CryptoComponent {
|
||||
|
||||
private static final Logger LOG =
|
||||
Logger.getLogger(CryptoComponentImpl.class.getName());
|
||||
getLogger(CryptoComponentImpl.class.getName());
|
||||
|
||||
private static final int SIGNATURE_KEY_PAIR_BITS = 256;
|
||||
private static final int STORAGE_IV_BYTES = 24; // 196 bits
|
||||
@@ -188,7 +191,7 @@ class CryptoComponentImpl implements CryptoComponent {
|
||||
PrivateKey ourPriv = ourKeyPair.getPrivate();
|
||||
byte[][] hashInputs = new byte[inputs.length + 1][];
|
||||
hashInputs[0] = performRawKeyAgreement(ourPriv, theirPublicKey);
|
||||
System.arraycopy(inputs, 0, hashInputs, 1, inputs.length);
|
||||
arraycopy(inputs, 0, hashInputs, 1, inputs.length);
|
||||
byte[] hash = hash(label, hashInputs);
|
||||
if (hash.length != SecretKey.LENGTH) throw new IllegalStateException();
|
||||
return new SecretKey(hash);
|
||||
@@ -216,26 +219,26 @@ class CryptoComponentImpl implements CryptoComponent {
|
||||
|
||||
private void updateSignature(Signature signature, String label,
|
||||
byte[] toSign) throws GeneralSecurityException {
|
||||
byte[] labelBytes = StringUtils.toUtf8(label);
|
||||
byte[] labelBytes = toUtf8(label);
|
||||
byte[] length = new byte[INT_32_BYTES];
|
||||
ByteUtils.writeUint32(labelBytes.length, length, 0);
|
||||
writeUint32(labelBytes.length, length, 0);
|
||||
signature.update(length);
|
||||
signature.update(labelBytes);
|
||||
ByteUtils.writeUint32(toSign.length, length, 0);
|
||||
writeUint32(toSign.length, length, 0);
|
||||
signature.update(length);
|
||||
signature.update(toSign);
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte[] hash(String label, byte[]... inputs) {
|
||||
byte[] labelBytes = StringUtils.toUtf8(label);
|
||||
byte[] labelBytes = toUtf8(label);
|
||||
Digest digest = new Blake2bDigest(256);
|
||||
byte[] length = new byte[INT_32_BYTES];
|
||||
ByteUtils.writeUint32(labelBytes.length, length, 0);
|
||||
writeUint32(labelBytes.length, length, 0);
|
||||
digest.update(length, 0, length.length);
|
||||
digest.update(labelBytes, 0, labelBytes.length);
|
||||
for (byte[] input : inputs) {
|
||||
ByteUtils.writeUint32(input.length, length, 0);
|
||||
writeUint32(input.length, length, 0);
|
||||
digest.update(length, 0, length.length);
|
||||
digest.update(input, 0, input.length);
|
||||
}
|
||||
@@ -246,14 +249,14 @@ class CryptoComponentImpl implements CryptoComponent {
|
||||
|
||||
@Override
|
||||
public byte[] mac(String label, SecretKey macKey, byte[]... inputs) {
|
||||
byte[] labelBytes = StringUtils.toUtf8(label);
|
||||
byte[] labelBytes = toUtf8(label);
|
||||
Digest mac = new Blake2bDigest(macKey.getBytes(), 32, null, null);
|
||||
byte[] length = new byte[INT_32_BYTES];
|
||||
ByteUtils.writeUint32(labelBytes.length, length, 0);
|
||||
writeUint32(labelBytes.length, length, 0);
|
||||
mac.update(length, 0, length.length);
|
||||
mac.update(labelBytes, 0, labelBytes.length);
|
||||
for (byte[] input : inputs) {
|
||||
ByteUtils.writeUint32(input.length, length, 0);
|
||||
writeUint32(input.length, length, 0);
|
||||
mac.update(length, 0, length.length);
|
||||
mac.update(input, 0, input.length);
|
||||
}
|
||||
@@ -297,13 +300,13 @@ class CryptoComponentImpl implements CryptoComponent {
|
||||
output[outputOff] = PBKDF_FORMAT_SCRYPT;
|
||||
outputOff++;
|
||||
// Salt
|
||||
System.arraycopy(salt, 0, output, outputOff, salt.length);
|
||||
arraycopy(salt, 0, output, outputOff, salt.length);
|
||||
outputOff += salt.length;
|
||||
// Cost parameter
|
||||
ByteUtils.writeUint32(cost, output, outputOff);
|
||||
writeUint32(cost, output, outputOff);
|
||||
outputOff += INT_32_BYTES;
|
||||
// IV
|
||||
System.arraycopy(iv, 0, output, outputOff, iv.length);
|
||||
arraycopy(iv, 0, output, outputOff, iv.length);
|
||||
outputOff += iv.length;
|
||||
// Initialise the cipher and encrypt the plaintext
|
||||
try {
|
||||
@@ -333,16 +336,16 @@ class CryptoComponentImpl implements CryptoComponent {
|
||||
return null; // Unknown format
|
||||
// Salt
|
||||
byte[] salt = new byte[PBKDF_SALT_BYTES];
|
||||
System.arraycopy(input, inputOff, salt, 0, salt.length);
|
||||
arraycopy(input, inputOff, salt, 0, salt.length);
|
||||
inputOff += salt.length;
|
||||
// Cost parameter
|
||||
long cost = ByteUtils.readUint32(input, inputOff);
|
||||
long cost = readUint32(input, inputOff);
|
||||
inputOff += INT_32_BYTES;
|
||||
if (cost < 2 || cost > Integer.MAX_VALUE)
|
||||
return null; // Invalid cost parameter
|
||||
// IV
|
||||
byte[] iv = new byte[STORAGE_IV_BYTES];
|
||||
System.arraycopy(input, inputOff, iv, 0, iv.length);
|
||||
arraycopy(input, inputOff, iv, 0, iv.length);
|
||||
inputOff += iv.length;
|
||||
// Derive the key from the password
|
||||
SecretKey key = passwordBasedKdf.deriveKey(password, salt, (int) cost);
|
||||
|
||||
@@ -7,6 +7,8 @@ import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
|
||||
import java.security.GeneralSecurityException;
|
||||
|
||||
import static java.lang.System.arraycopy;
|
||||
|
||||
@NotNullByDefault
|
||||
class Curve25519KeyParser implements KeyParser {
|
||||
|
||||
@@ -26,7 +28,7 @@ class Curve25519KeyParser implements KeyParser {
|
||||
|
||||
static byte[] clamp(byte[] b) {
|
||||
byte[] clamped = new byte[32];
|
||||
System.arraycopy(b, 0, clamped, 0, 32);
|
||||
arraycopy(b, 0, clamped, 0, 32);
|
||||
clamped[0] &= 248;
|
||||
clamped[31] &= 127;
|
||||
clamped[31] |= 64;
|
||||
|
||||
@@ -1,13 +1,15 @@
|
||||
package org.briarproject.bramble.crypto;
|
||||
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
import org.briarproject.bramble.util.ByteUtils;
|
||||
|
||||
import static org.briarproject.bramble.api.transport.TransportConstants.FRAME_HEADER_PLAINTEXT_LENGTH;
|
||||
import static org.briarproject.bramble.api.transport.TransportConstants.FRAME_NONCE_LENGTH;
|
||||
import static org.briarproject.bramble.api.transport.TransportConstants.MAX_PAYLOAD_LENGTH;
|
||||
import static org.briarproject.bramble.util.ByteUtils.INT_16_BYTES;
|
||||
import static org.briarproject.bramble.util.ByteUtils.INT_64_BYTES;
|
||||
import static org.briarproject.bramble.util.ByteUtils.readUint16;
|
||||
import static org.briarproject.bramble.util.ByteUtils.writeUint16;
|
||||
import static org.briarproject.bramble.util.ByteUtils.writeUint64;
|
||||
|
||||
@NotNullByDefault
|
||||
class FrameEncoder {
|
||||
@@ -16,7 +18,7 @@ class FrameEncoder {
|
||||
if (dest.length < FRAME_NONCE_LENGTH)
|
||||
throw new IllegalArgumentException();
|
||||
if (frameNumber < 0) throw new IllegalArgumentException();
|
||||
ByteUtils.writeUint64(frameNumber, dest, 0);
|
||||
writeUint64(frameNumber, dest, 0);
|
||||
if (header) dest[0] |= 0x80;
|
||||
for (int i = INT_64_BYTES; i < FRAME_NONCE_LENGTH; i++) dest[i] = 0;
|
||||
}
|
||||
@@ -29,8 +31,8 @@ class FrameEncoder {
|
||||
if (paddingLength < 0) throw new IllegalArgumentException();
|
||||
if (payloadLength + paddingLength > MAX_PAYLOAD_LENGTH)
|
||||
throw new IllegalArgumentException();
|
||||
ByteUtils.writeUint16(payloadLength, dest, 0);
|
||||
ByteUtils.writeUint16(paddingLength, dest, INT_16_BYTES);
|
||||
writeUint16(payloadLength, dest, 0);
|
||||
writeUint16(paddingLength, dest, INT_16_BYTES);
|
||||
if (finalFrame) dest[0] |= 0x80;
|
||||
}
|
||||
|
||||
@@ -43,12 +45,12 @@ class FrameEncoder {
|
||||
static int getPayloadLength(byte[] header) {
|
||||
if (header.length < FRAME_HEADER_PLAINTEXT_LENGTH)
|
||||
throw new IllegalArgumentException();
|
||||
return ByteUtils.readUint16(header, 0) & 0x7FFF;
|
||||
return readUint16(header, 0) & 0x7FFF;
|
||||
}
|
||||
|
||||
static int getPaddingLength(byte[] header) {
|
||||
if (header.length < FRAME_HEADER_PLAINTEXT_LENGTH)
|
||||
throw new IllegalArgumentException();
|
||||
return ByteUtils.readUint16(header, INT_16_BYTES);
|
||||
return readUint16(header, INT_16_BYTES);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,6 +8,7 @@ import org.briarproject.bramble.api.crypto.SecretKey;
|
||||
|
||||
import javax.inject.Inject;
|
||||
|
||||
import static java.lang.System.arraycopy;
|
||||
import static org.briarproject.bramble.api.keyagreement.KeyAgreementConstants.COMMIT_LENGTH;
|
||||
|
||||
class KeyAgreementCryptoImpl implements KeyAgreementCrypto {
|
||||
@@ -24,7 +25,7 @@ class KeyAgreementCryptoImpl implements KeyAgreementCrypto {
|
||||
byte[] hash = crypto.hash(COMMIT_LABEL, publicKey.getEncoded());
|
||||
// The output is the first COMMIT_LENGTH bytes of the hash
|
||||
byte[] commitment = new byte[COMMIT_LENGTH];
|
||||
System.arraycopy(hash, 0, commitment, 0, COMMIT_LENGTH);
|
||||
arraycopy(hash, 0, commitment, 0, COMMIT_LENGTH);
|
||||
return commitment;
|
||||
}
|
||||
|
||||
|
||||
@@ -5,7 +5,6 @@ import org.briarproject.bramble.api.crypto.KeyParser;
|
||||
import org.briarproject.bramble.api.crypto.PrivateKey;
|
||||
import org.briarproject.bramble.api.crypto.PublicKey;
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
import org.briarproject.bramble.util.StringUtils;
|
||||
import org.spongycastle.asn1.teletrust.TeleTrusTNamedCurves;
|
||||
import org.spongycastle.asn1.x9.X9ECParameters;
|
||||
import org.spongycastle.crypto.AsymmetricCipherKeyPair;
|
||||
@@ -45,6 +44,9 @@ import java.util.Scanner;
|
||||
|
||||
import javax.annotation.concurrent.Immutable;
|
||||
|
||||
import static org.briarproject.bramble.util.StringUtils.fromHexString;
|
||||
import static org.briarproject.bramble.util.StringUtils.toHexString;
|
||||
|
||||
@Immutable
|
||||
@NotNullByDefault
|
||||
public class MessageEncrypter {
|
||||
@@ -157,6 +159,7 @@ public class MessageEncrypter {
|
||||
printUsage();
|
||||
System.exit(1);
|
||||
}
|
||||
//noinspection IfCanBeSwitch
|
||||
if (args[0].equals("generate")) {
|
||||
if (args.length != 3) {
|
||||
printUsage();
|
||||
@@ -210,11 +213,11 @@ public class MessageEncrypter {
|
||||
MessageEncrypter encrypter = new MessageEncrypter(random);
|
||||
KeyPair keyPair = encrypter.generateKeyPair();
|
||||
PrintStream out = new PrintStream(new FileOutputStream(publicKeyFile));
|
||||
out.print(StringUtils.toHexString(keyPair.getPublic().getEncoded()));
|
||||
out.print(toHexString(keyPair.getPublic().getEncoded()));
|
||||
out.flush();
|
||||
out.close();
|
||||
out = new PrintStream(new FileOutputStream(privateKeyFile));
|
||||
out.print(StringUtils.toHexString(keyPair.getPrivate().getEncoded()));
|
||||
out.print(toHexString(keyPair.getPrivate().getEncoded()));
|
||||
out.flush();
|
||||
out.close();
|
||||
}
|
||||
@@ -223,7 +226,7 @@ public class MessageEncrypter {
|
||||
SecureRandom random = new SecureRandom();
|
||||
MessageEncrypter encrypter = new MessageEncrypter(random);
|
||||
InputStream in = new FileInputStream(publicKeyFile);
|
||||
byte[] keyBytes = StringUtils.fromHexString(readFully(in).trim());
|
||||
byte[] keyBytes = fromHexString(readFully(in).trim());
|
||||
PublicKey publicKey =
|
||||
encrypter.getKeyParser().parsePublicKey(keyBytes);
|
||||
String message = readFully(System.in);
|
||||
@@ -236,7 +239,7 @@ public class MessageEncrypter {
|
||||
SecureRandom random = new SecureRandom();
|
||||
MessageEncrypter encrypter = new MessageEncrypter(random);
|
||||
InputStream in = new FileInputStream(privateKeyFile);
|
||||
byte[] keyBytes = StringUtils.fromHexString(readFully(in).trim());
|
||||
byte[] keyBytes = fromHexString(readFully(in).trim());
|
||||
PrivateKey privateKey =
|
||||
encrypter.getKeyParser().parsePrivateKey(keyBytes);
|
||||
byte[] ciphertext = AsciiArmour.unwrap(readFully(System.in));
|
||||
|
||||
@@ -2,7 +2,6 @@ package org.briarproject.bramble.crypto;
|
||||
|
||||
import org.briarproject.bramble.api.crypto.SecretKey;
|
||||
import org.briarproject.bramble.api.system.Clock;
|
||||
import org.briarproject.bramble.util.StringUtils;
|
||||
import org.spongycastle.crypto.generators.SCrypt;
|
||||
|
||||
import java.util.logging.Logger;
|
||||
@@ -10,13 +9,14 @@ import java.util.logging.Logger;
|
||||
import javax.inject.Inject;
|
||||
|
||||
import static java.util.logging.Level.INFO;
|
||||
import static java.util.logging.Logger.getLogger;
|
||||
import static org.briarproject.bramble.util.LogUtils.logDuration;
|
||||
import static org.briarproject.bramble.util.LogUtils.now;
|
||||
import static org.briarproject.bramble.util.StringUtils.toUtf8;
|
||||
|
||||
class ScryptKdf implements PasswordBasedKdf {
|
||||
|
||||
private static final Logger LOG =
|
||||
Logger.getLogger(ScryptKdf.class.getName());
|
||||
private static final Logger LOG = getLogger(ScryptKdf.class.getName());
|
||||
|
||||
private static final int MIN_COST = 256; // Min parameter N
|
||||
private static final int MAX_COST = 1024 * 1024; // Max parameter N
|
||||
@@ -53,7 +53,7 @@ class ScryptKdf implements PasswordBasedKdf {
|
||||
@Override
|
||||
public SecretKey deriveKey(String password, byte[] salt, int cost) {
|
||||
long start = now();
|
||||
byte[] passwordBytes = StringUtils.toUtf8(password);
|
||||
byte[] passwordBytes = toUtf8(password);
|
||||
SecretKey k = new SecretKey(SCrypt.generate(passwordBytes, salt, cost,
|
||||
BLOCK_SIZE, PARALLELIZATION, SecretKey.LENGTH));
|
||||
logDuration(LOG, "Deriving key from password", start);
|
||||
|
||||
@@ -16,6 +16,8 @@ import java.util.logging.Logger;
|
||||
|
||||
import javax.annotation.concurrent.Immutable;
|
||||
|
||||
import static java.lang.System.arraycopy;
|
||||
import static java.util.logging.Logger.getLogger;
|
||||
import static org.briarproject.bramble.util.LogUtils.logDuration;
|
||||
import static org.briarproject.bramble.util.LogUtils.now;
|
||||
|
||||
@@ -28,8 +30,7 @@ import static org.briarproject.bramble.util.LogUtils.now;
|
||||
@NotNullByDefault
|
||||
class Sec1KeyParser implements KeyParser {
|
||||
|
||||
private static final Logger LOG =
|
||||
Logger.getLogger(Sec1KeyParser.class.getName());
|
||||
private static final Logger LOG = getLogger(Sec1KeyParser.class.getName());
|
||||
|
||||
private final ECDomainParameters params;
|
||||
private final BigInteger modulus;
|
||||
@@ -56,12 +57,12 @@ class Sec1KeyParser implements KeyParser {
|
||||
if (encodedKey[0] != 4) throw new GeneralSecurityException();
|
||||
// The x co-ordinate must be >= 0 and < p
|
||||
byte[] xBytes = new byte[bytesPerInt];
|
||||
System.arraycopy(encodedKey, 1, xBytes, 0, bytesPerInt);
|
||||
arraycopy(encodedKey, 1, xBytes, 0, bytesPerInt);
|
||||
BigInteger x = new BigInteger(1, xBytes); // Positive signum
|
||||
if (x.compareTo(modulus) >= 0) throw new GeneralSecurityException();
|
||||
// The y co-ordinate must be >= 0 and < p
|
||||
byte[] yBytes = new byte[bytesPerInt];
|
||||
System.arraycopy(encodedKey, 1 + bytesPerInt, yBytes, 0, bytesPerInt);
|
||||
arraycopy(encodedKey, 1 + bytesPerInt, yBytes, 0, bytesPerInt);
|
||||
BigInteger y = new BigInteger(1, yBytes); // Positive signum
|
||||
if (y.compareTo(modulus) >= 0) throw new GeneralSecurityException();
|
||||
// Verify that y^2 == x^3 + ax + b (mod p)
|
||||
|
||||
@@ -2,6 +2,8 @@ package org.briarproject.bramble.crypto;
|
||||
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
|
||||
import static java.lang.System.arraycopy;
|
||||
|
||||
@NotNullByDefault
|
||||
class Sec1Utils {
|
||||
|
||||
@@ -10,10 +12,10 @@ class Sec1Utils {
|
||||
if (src.length < destLen) {
|
||||
int padding = destLen - src.length;
|
||||
for (int i = destOff; i < destOff + padding; i++) dest[i] = 0;
|
||||
System.arraycopy(src, 0, dest, destOff + padding, src.length);
|
||||
arraycopy(src, 0, dest, destOff + padding, src.length);
|
||||
} else {
|
||||
int srcOff = src.length - destLen;
|
||||
System.arraycopy(src, srcOff, dest, destOff, destLen);
|
||||
arraycopy(src, srcOff, dest, destOff, destLen);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,7 +4,6 @@ import org.briarproject.bramble.api.FormatException;
|
||||
import org.briarproject.bramble.api.crypto.SecretKey;
|
||||
import org.briarproject.bramble.api.crypto.StreamDecrypter;
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
import org.briarproject.bramble.util.ByteUtils;
|
||||
|
||||
import java.io.EOFException;
|
||||
import java.io.IOException;
|
||||
@@ -14,6 +13,7 @@ import java.security.GeneralSecurityException;
|
||||
import javax.annotation.Nullable;
|
||||
import javax.annotation.concurrent.NotThreadSafe;
|
||||
|
||||
import static java.lang.System.arraycopy;
|
||||
import static org.briarproject.bramble.api.transport.TransportConstants.FRAME_HEADER_LENGTH;
|
||||
import static org.briarproject.bramble.api.transport.TransportConstants.FRAME_HEADER_PLAINTEXT_LENGTH;
|
||||
import static org.briarproject.bramble.api.transport.TransportConstants.FRAME_NONCE_LENGTH;
|
||||
@@ -26,6 +26,8 @@ import static org.briarproject.bramble.api.transport.TransportConstants.STREAM_H
|
||||
import static org.briarproject.bramble.api.transport.TransportConstants.STREAM_HEADER_PLAINTEXT_LENGTH;
|
||||
import static org.briarproject.bramble.util.ByteUtils.INT_16_BYTES;
|
||||
import static org.briarproject.bramble.util.ByteUtils.INT_64_BYTES;
|
||||
import static org.briarproject.bramble.util.ByteUtils.readUint16;
|
||||
import static org.briarproject.bramble.util.ByteUtils.readUint64;
|
||||
|
||||
@NotThreadSafe
|
||||
@NotNullByDefault
|
||||
@@ -130,7 +132,7 @@ class StreamDecrypterImpl implements StreamDecrypter {
|
||||
}
|
||||
// Extract the nonce
|
||||
byte[] streamHeaderNonce = new byte[STREAM_HEADER_NONCE_LENGTH];
|
||||
System.arraycopy(streamHeaderCiphertext, 0, streamHeaderNonce, 0,
|
||||
arraycopy(streamHeaderCiphertext, 0, streamHeaderNonce, 0,
|
||||
STREAM_HEADER_NONCE_LENGTH);
|
||||
// Decrypt and authenticate the stream header
|
||||
try {
|
||||
@@ -145,17 +147,16 @@ class StreamDecrypterImpl implements StreamDecrypter {
|
||||
throw new FormatException();
|
||||
}
|
||||
// Check the protocol version
|
||||
int receivedProtocolVersion =
|
||||
ByteUtils.readUint16(streamHeaderPlaintext, 0);
|
||||
int receivedProtocolVersion = readUint16(streamHeaderPlaintext, 0);
|
||||
if (receivedProtocolVersion != PROTOCOL_VERSION)
|
||||
throw new FormatException();
|
||||
// Check the stream number
|
||||
long receivedStreamNumber = ByteUtils.readUint64(streamHeaderPlaintext,
|
||||
long receivedStreamNumber = readUint64(streamHeaderPlaintext,
|
||||
INT_16_BYTES);
|
||||
if (receivedStreamNumber != streamNumber) throw new FormatException();
|
||||
// Extract the frame key
|
||||
byte[] frameKeyBytes = new byte[SecretKey.LENGTH];
|
||||
System.arraycopy(streamHeaderPlaintext, INT_16_BYTES + INT_64_BYTES,
|
||||
arraycopy(streamHeaderPlaintext, INT_16_BYTES + INT_64_BYTES,
|
||||
frameKeyBytes, 0, SecretKey.LENGTH);
|
||||
frameKey = new SecretKey(frameKeyBytes);
|
||||
}
|
||||
|
||||
@@ -3,7 +3,6 @@ package org.briarproject.bramble.crypto;
|
||||
import org.briarproject.bramble.api.crypto.SecretKey;
|
||||
import org.briarproject.bramble.api.crypto.StreamEncrypter;
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
import org.briarproject.bramble.util.ByteUtils;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
@@ -12,6 +11,7 @@ import java.security.GeneralSecurityException;
|
||||
import javax.annotation.Nullable;
|
||||
import javax.annotation.concurrent.NotThreadSafe;
|
||||
|
||||
import static java.lang.System.arraycopy;
|
||||
import static org.briarproject.bramble.api.transport.TransportConstants.FRAME_HEADER_LENGTH;
|
||||
import static org.briarproject.bramble.api.transport.TransportConstants.FRAME_HEADER_PLAINTEXT_LENGTH;
|
||||
import static org.briarproject.bramble.api.transport.TransportConstants.FRAME_NONCE_LENGTH;
|
||||
@@ -24,6 +24,8 @@ import static org.briarproject.bramble.api.transport.TransportConstants.STREAM_H
|
||||
import static org.briarproject.bramble.api.transport.TransportConstants.STREAM_HEADER_PLAINTEXT_LENGTH;
|
||||
import static org.briarproject.bramble.util.ByteUtils.INT_16_BYTES;
|
||||
import static org.briarproject.bramble.util.ByteUtils.INT_64_BYTES;
|
||||
import static org.briarproject.bramble.util.ByteUtils.writeUint16;
|
||||
import static org.briarproject.bramble.util.ByteUtils.writeUint64;
|
||||
|
||||
@NotThreadSafe
|
||||
@NotNullByDefault
|
||||
@@ -88,7 +90,7 @@ class StreamEncrypterImpl implements StreamEncrypter {
|
||||
throw new RuntimeException(badCipher);
|
||||
}
|
||||
// Combine the payload and padding
|
||||
System.arraycopy(payload, 0, framePlaintext, 0, payloadLength);
|
||||
arraycopy(payload, 0, framePlaintext, 0, payloadLength);
|
||||
for (int i = 0; i < paddingLength; i++)
|
||||
framePlaintext[payloadLength + i] = 0;
|
||||
// Encrypt and authenticate the payload and padding
|
||||
@@ -118,13 +120,12 @@ class StreamEncrypterImpl implements StreamEncrypter {
|
||||
private void writeStreamHeader() throws IOException {
|
||||
// The header contains the protocol version, stream number and frame key
|
||||
byte[] streamHeaderPlaintext = new byte[STREAM_HEADER_PLAINTEXT_LENGTH];
|
||||
ByteUtils.writeUint16(PROTOCOL_VERSION, streamHeaderPlaintext, 0);
|
||||
ByteUtils.writeUint64(streamNumber, streamHeaderPlaintext,
|
||||
INT_16_BYTES);
|
||||
System.arraycopy(frameKey.getBytes(), 0, streamHeaderPlaintext,
|
||||
writeUint16(PROTOCOL_VERSION, streamHeaderPlaintext, 0);
|
||||
writeUint64(streamNumber, streamHeaderPlaintext, INT_16_BYTES);
|
||||
arraycopy(frameKey.getBytes(), 0, streamHeaderPlaintext,
|
||||
INT_16_BYTES + INT_64_BYTES, SecretKey.LENGTH);
|
||||
byte[] streamHeaderCiphertext = new byte[STREAM_HEADER_LENGTH];
|
||||
System.arraycopy(streamHeaderNonce, 0, streamHeaderCiphertext, 0,
|
||||
arraycopy(streamHeaderNonce, 0, streamHeaderCiphertext, 0,
|
||||
STREAM_HEADER_NONCE_LENGTH);
|
||||
// Encrypt and authenticate the stream header key
|
||||
try {
|
||||
|
||||
@@ -7,13 +7,12 @@ import org.briarproject.bramble.api.plugin.TransportId;
|
||||
import org.briarproject.bramble.api.transport.IncomingKeys;
|
||||
import org.briarproject.bramble.api.transport.OutgoingKeys;
|
||||
import org.briarproject.bramble.api.transport.TransportKeys;
|
||||
import org.briarproject.bramble.util.ByteUtils;
|
||||
import org.briarproject.bramble.util.StringUtils;
|
||||
import org.spongycastle.crypto.Digest;
|
||||
import org.spongycastle.crypto.digests.Blake2bDigest;
|
||||
|
||||
import javax.inject.Inject;
|
||||
|
||||
import static java.lang.System.arraycopy;
|
||||
import static org.briarproject.bramble.api.transport.TransportConstants.ALICE_HEADER_LABEL;
|
||||
import static org.briarproject.bramble.api.transport.TransportConstants.ALICE_TAG_LABEL;
|
||||
import static org.briarproject.bramble.api.transport.TransportConstants.BOB_HEADER_LABEL;
|
||||
@@ -24,6 +23,9 @@ import static org.briarproject.bramble.util.ByteUtils.INT_16_BYTES;
|
||||
import static org.briarproject.bramble.util.ByteUtils.INT_64_BYTES;
|
||||
import static org.briarproject.bramble.util.ByteUtils.MAX_16_BIT_UNSIGNED;
|
||||
import static org.briarproject.bramble.util.ByteUtils.MAX_32_BIT_UNSIGNED;
|
||||
import static org.briarproject.bramble.util.ByteUtils.writeUint16;
|
||||
import static org.briarproject.bramble.util.ByteUtils.writeUint64;
|
||||
import static org.briarproject.bramble.util.StringUtils.toUtf8;
|
||||
|
||||
class TransportCryptoImpl implements TransportCrypto {
|
||||
|
||||
@@ -91,21 +93,21 @@ class TransportCryptoImpl implements TransportCrypto {
|
||||
|
||||
private SecretKey rotateKey(SecretKey k, long rotationPeriod) {
|
||||
byte[] period = new byte[INT_64_BYTES];
|
||||
ByteUtils.writeUint64(rotationPeriod, period, 0);
|
||||
writeUint64(rotationPeriod, period, 0);
|
||||
return crypto.deriveKey(ROTATE_LABEL, k, period);
|
||||
}
|
||||
|
||||
private SecretKey deriveTagKey(SecretKey master, TransportId t,
|
||||
boolean alice) {
|
||||
String label = alice ? ALICE_TAG_LABEL : BOB_TAG_LABEL;
|
||||
byte[] id = StringUtils.toUtf8(t.getString());
|
||||
byte[] id = toUtf8(t.getString());
|
||||
return crypto.deriveKey(label, master, id);
|
||||
}
|
||||
|
||||
private SecretKey deriveHeaderKey(SecretKey master, TransportId t,
|
||||
boolean alice) {
|
||||
String label = alice ? ALICE_HEADER_LABEL : BOB_HEADER_LABEL;
|
||||
byte[] id = StringUtils.toUtf8(t.getString());
|
||||
byte[] id = toUtf8(t.getString());
|
||||
return crypto.deriveKey(label, master, id);
|
||||
}
|
||||
|
||||
@@ -125,14 +127,14 @@ class TransportCryptoImpl implements TransportCrypto {
|
||||
// The input is the protocol version as a 16-bit integer, followed by
|
||||
// the stream number as a 64-bit integer
|
||||
byte[] protocolVersionBytes = new byte[INT_16_BYTES];
|
||||
ByteUtils.writeUint16(protocolVersion, protocolVersionBytes, 0);
|
||||
writeUint16(protocolVersion, protocolVersionBytes, 0);
|
||||
prf.update(protocolVersionBytes, 0, protocolVersionBytes.length);
|
||||
byte[] streamNumberBytes = new byte[INT_64_BYTES];
|
||||
ByteUtils.writeUint64(streamNumber, streamNumberBytes, 0);
|
||||
writeUint64(streamNumber, streamNumberBytes, 0);
|
||||
prf.update(streamNumberBytes, 0, streamNumberBytes.length);
|
||||
byte[] mac = new byte[macLength];
|
||||
prf.doFinal(mac, 0);
|
||||
// The output is the first TAG_LENGTH bytes of the MAC
|
||||
System.arraycopy(mac, 0, tag, 0, TAG_LENGTH);
|
||||
arraycopy(mac, 0, tag, 0, TAG_LENGTH);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -242,7 +242,7 @@ interface Database<T> {
|
||||
* bytes. This is based on the minimum of the space available on the device
|
||||
* where the database is stored and the database's configured size.
|
||||
*/
|
||||
long getFreeSpace() throws DbException;
|
||||
long getFreeSpace();
|
||||
|
||||
/**
|
||||
* Returns the group with the given ID.
|
||||
|
||||
@@ -61,7 +61,6 @@ import org.briarproject.bramble.api.transport.TransportKeys;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
@@ -72,7 +71,9 @@ import javax.annotation.Nullable;
|
||||
import javax.annotation.concurrent.ThreadSafe;
|
||||
import javax.inject.Inject;
|
||||
|
||||
import static java.util.Collections.singletonList;
|
||||
import static java.util.logging.Level.WARNING;
|
||||
import static java.util.logging.Logger.getLogger;
|
||||
import static org.briarproject.bramble.api.sync.Group.Visibility.INVISIBLE;
|
||||
import static org.briarproject.bramble.api.sync.Group.Visibility.SHARED;
|
||||
import static org.briarproject.bramble.api.sync.validation.MessageState.DELIVERED;
|
||||
@@ -87,7 +88,7 @@ import static org.briarproject.bramble.util.LogUtils.now;
|
||||
class DatabaseComponentImpl<T> implements DatabaseComponent {
|
||||
|
||||
private static final Logger LOG =
|
||||
Logger.getLogger(DatabaseComponentImpl.class.getName());
|
||||
getLogger(DatabaseComponentImpl.class.getName());
|
||||
|
||||
private final Database<T> db;
|
||||
private final Class<T> txnClass;
|
||||
@@ -898,7 +899,7 @@ class DatabaseComponentImpl<T> implements DatabaseComponent {
|
||||
if (old == INVISIBLE) db.addGroupVisibility(txn, c, g, v == SHARED);
|
||||
else if (v == INVISIBLE) db.removeGroupVisibility(txn, c, g);
|
||||
else db.setGroupVisibility(txn, c, g, v == SHARED);
|
||||
List<ContactId> affected = Collections.singletonList(c);
|
||||
List<ContactId> affected = singletonList(c);
|
||||
transaction.attach(new GroupVisibilityUpdatedEvent(affected));
|
||||
}
|
||||
|
||||
|
||||
@@ -7,7 +7,6 @@ import org.briarproject.bramble.api.db.MigrationListener;
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
import org.briarproject.bramble.api.sync.MessageFactory;
|
||||
import org.briarproject.bramble.api.system.Clock;
|
||||
import org.briarproject.bramble.util.StringUtils;
|
||||
|
||||
import java.io.File;
|
||||
import java.sql.Connection;
|
||||
@@ -23,6 +22,7 @@ import javax.inject.Inject;
|
||||
import static java.util.logging.Level.WARNING;
|
||||
import static java.util.logging.Logger.getLogger;
|
||||
import static org.briarproject.bramble.db.JdbcUtils.tryToClose;
|
||||
import static org.briarproject.bramble.util.StringUtils.toHexString;
|
||||
|
||||
/**
|
||||
* Contains all the H2-specific code for the database.
|
||||
@@ -107,7 +107,7 @@ class H2Database extends JdbcDatabase {
|
||||
Properties props = new Properties();
|
||||
props.setProperty("user", "user");
|
||||
// Separate the file password from the user password with a space
|
||||
String hex = StringUtils.toHexString(key.getBytes());
|
||||
String hex = toHexString(key.getBytes());
|
||||
props.put("password", hex + " password");
|
||||
return DriverManager.getConnection(getUrl(), props);
|
||||
}
|
||||
|
||||
@@ -7,7 +7,6 @@ import org.briarproject.bramble.api.db.MigrationListener;
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
import org.briarproject.bramble.api.sync.MessageFactory;
|
||||
import org.briarproject.bramble.api.system.Clock;
|
||||
import org.briarproject.bramble.util.StringUtils;
|
||||
|
||||
import java.io.File;
|
||||
import java.sql.Connection;
|
||||
@@ -22,6 +21,7 @@ import javax.inject.Inject;
|
||||
import static java.util.logging.Level.WARNING;
|
||||
import static java.util.logging.Logger.getLogger;
|
||||
import static org.briarproject.bramble.db.JdbcUtils.tryToClose;
|
||||
import static org.briarproject.bramble.util.StringUtils.toHexString;
|
||||
|
||||
/**
|
||||
* Contains all the HSQLDB-specific code for the database.
|
||||
@@ -114,7 +114,7 @@ class HyperSqlDatabase extends JdbcDatabase {
|
||||
protected Connection createConnection() throws SQLException {
|
||||
SecretKey key = this.key;
|
||||
if (key == null) throw new IllegalStateException();
|
||||
String hex = StringUtils.toHexString(key.getBytes());
|
||||
String hex = toHexString(key.getBytes());
|
||||
return DriverManager.getConnection(url + ";crypt_key=" + hex);
|
||||
}
|
||||
|
||||
|
||||
@@ -38,9 +38,7 @@ import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Statement;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.LinkedList;
|
||||
@@ -54,11 +52,17 @@ import java.util.concurrent.locks.ReentrantLock;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
import javax.annotation.concurrent.GuardedBy;
|
||||
|
||||
import static java.lang.System.arraycopy;
|
||||
import static java.sql.Types.INTEGER;
|
||||
import static java.sql.Types.VARCHAR;
|
||||
import static java.util.Arrays.asList;
|
||||
import static java.util.Collections.emptyMap;
|
||||
import static java.util.Collections.emptySet;
|
||||
import static java.util.logging.Level.INFO;
|
||||
import static java.util.logging.Level.WARNING;
|
||||
import static java.util.logging.Logger.getLogger;
|
||||
import static org.briarproject.bramble.api.db.Metadata.REMOVE;
|
||||
import static org.briarproject.bramble.api.sync.Group.Visibility.INVISIBLE;
|
||||
import static org.briarproject.bramble.api.sync.Group.Visibility.SHARED;
|
||||
@@ -309,19 +313,20 @@ abstract class JdbcDatabase implements Database<Connection> {
|
||||
"CREATE INDEX IF NOT EXISTS statusesByContactIdTimestamp"
|
||||
+ " ON statuses (contactId, timestamp)";
|
||||
|
||||
private static final Logger LOG =
|
||||
Logger.getLogger(JdbcDatabase.class.getName());
|
||||
private static final Logger LOG = getLogger(JdbcDatabase.class.getName());
|
||||
|
||||
// Different database libraries use different names for certain types
|
||||
private final MessageFactory messageFactory;
|
||||
private final Clock clock;
|
||||
private final DatabaseTypes dbTypes;
|
||||
|
||||
// Locking: connectionsLock
|
||||
@GuardedBy("connectionsLock")
|
||||
private final LinkedList<Connection> connections = new LinkedList<>();
|
||||
|
||||
private int openConnections = 0; // Locking: connectionsLock
|
||||
private boolean closed = false; // Locking: connectionsLock
|
||||
@GuardedBy("connectionsLock")
|
||||
private int openConnections = 0;
|
||||
@GuardedBy("connectionsLock")
|
||||
private boolean closed = false;
|
||||
|
||||
protected abstract Connection createConnection() throws SQLException;
|
||||
|
||||
@@ -337,6 +342,7 @@ abstract class JdbcDatabase implements Database<Connection> {
|
||||
this.clock = clock;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
protected void open(String driverClass, boolean reopen, SecretKey key,
|
||||
@Nullable MigrationListener listener) throws DbException {
|
||||
// Load the JDBC driver
|
||||
@@ -424,7 +430,7 @@ abstract class JdbcDatabase implements Database<Connection> {
|
||||
|
||||
// Package access for testing
|
||||
List<Migration<Connection>> getMigrations() {
|
||||
return Arrays.asList(
|
||||
return asList(
|
||||
new Migration38_39(),
|
||||
new Migration39_40(),
|
||||
new Migration40_41(dbTypes)
|
||||
@@ -766,7 +772,7 @@ abstract class JdbcDatabase implements Database<Connection> {
|
||||
for (Entry<ContactId, Boolean> e : visibility.entrySet()) {
|
||||
ContactId c = e.getKey();
|
||||
boolean offered = removeOfferedMessage(txn, c, m.getId());
|
||||
boolean seen = offered || (sender != null && c.equals(sender));
|
||||
boolean seen = offered || c.equals(sender);
|
||||
addStatus(txn, m.getId(), c, m.getGroupId(), m.getTimestamp(),
|
||||
raw.length, state, e.getValue(), messageShared,
|
||||
false, seen);
|
||||
@@ -1536,7 +1542,7 @@ abstract class JdbcDatabase implements Database<Connection> {
|
||||
if (raw == null) throw new MessageDeletedException();
|
||||
if (raw.length <= MESSAGE_HEADER_LENGTH) throw new AssertionError();
|
||||
byte[] body = new byte[raw.length - MESSAGE_HEADER_LENGTH];
|
||||
System.arraycopy(raw, MESSAGE_HEADER_LENGTH, body, 0, body.length);
|
||||
arraycopy(raw, MESSAGE_HEADER_LENGTH, body, 0, body.length);
|
||||
return new Message(m, g, timestamp, body);
|
||||
} catch (SQLException e) {
|
||||
tryToClose(rs, LOG, WARNING);
|
||||
@@ -1596,7 +1602,7 @@ abstract class JdbcDatabase implements Database<Connection> {
|
||||
if (intersection == null) intersection = ids;
|
||||
else intersection.retainAll(ids);
|
||||
// Return early if there are no matches
|
||||
if (intersection.isEmpty()) return Collections.emptySet();
|
||||
if (intersection.isEmpty()) return emptySet();
|
||||
}
|
||||
if (intersection == null) throw new AssertionError();
|
||||
return intersection;
|
||||
@@ -1645,7 +1651,7 @@ abstract class JdbcDatabase implements Database<Connection> {
|
||||
GroupId g, Metadata query) throws DbException {
|
||||
// Retrieve the matching message IDs
|
||||
Collection<MessageId> matches = getMessageIds(txn, g, query);
|
||||
if (matches.isEmpty()) return Collections.emptyMap();
|
||||
if (matches.isEmpty()) return emptyMap();
|
||||
// Retrieve the metadata for each match
|
||||
Map<MessageId, Metadata> all = new HashMap<>(matches.size());
|
||||
for (MessageId m : matches) all.put(m, getMessageMetadata(txn, m));
|
||||
@@ -2395,7 +2401,7 @@ abstract class JdbcDatabase implements Database<Connection> {
|
||||
}
|
||||
ps.close();
|
||||
}
|
||||
if (notRemoved.isEmpty()) return Collections.emptyMap();
|
||||
if (notRemoved.isEmpty()) return emptyMap();
|
||||
// Update any keys that already exist
|
||||
String sql = "UPDATE " + tableName + " SET value = ?"
|
||||
+ " WHERE " + columnName + " = ? AND metaKey = ?";
|
||||
|
||||
@@ -8,12 +8,12 @@ import java.sql.Statement;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import static java.util.logging.Level.WARNING;
|
||||
import static java.util.logging.Logger.getLogger;
|
||||
import static org.briarproject.bramble.db.JdbcUtils.tryToClose;
|
||||
|
||||
class Migration38_39 implements Migration<Connection> {
|
||||
|
||||
private static final Logger LOG =
|
||||
Logger.getLogger(Migration38_39.class.getName());
|
||||
private static final Logger LOG = getLogger(Migration38_39.class.getName());
|
||||
|
||||
@Override
|
||||
public int getStartVersion() {
|
||||
|
||||
@@ -8,12 +8,12 @@ import java.sql.Statement;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import static java.util.logging.Level.WARNING;
|
||||
import static java.util.logging.Logger.getLogger;
|
||||
import static org.briarproject.bramble.db.JdbcUtils.tryToClose;
|
||||
|
||||
class Migration39_40 implements Migration<Connection> {
|
||||
|
||||
private static final Logger LOG =
|
||||
Logger.getLogger(Migration39_40.class.getName());
|
||||
private static final Logger LOG = getLogger(Migration39_40.class.getName());
|
||||
|
||||
@Override
|
||||
public int getStartVersion() {
|
||||
|
||||
@@ -17,7 +17,7 @@ class Migration40_41 implements Migration<Connection> {
|
||||
|
||||
private final DatabaseTypes dbTypes;
|
||||
|
||||
public Migration40_41(DatabaseTypes databaseTypes) {
|
||||
Migration40_41(DatabaseTypes databaseTypes) {
|
||||
this.dbTypes = databaseTypes;
|
||||
}
|
||||
|
||||
|
||||
@@ -7,8 +7,6 @@ import org.briarproject.bramble.api.identity.AuthorId;
|
||||
import org.briarproject.bramble.api.identity.LocalAuthor;
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
import org.briarproject.bramble.api.system.Clock;
|
||||
import org.briarproject.bramble.util.ByteUtils;
|
||||
import org.briarproject.bramble.util.StringUtils;
|
||||
|
||||
import javax.annotation.concurrent.Immutable;
|
||||
import javax.inject.Inject;
|
||||
@@ -16,6 +14,8 @@ import javax.inject.Inject;
|
||||
import static org.briarproject.bramble.api.identity.Author.FORMAT_VERSION;
|
||||
import static org.briarproject.bramble.api.identity.AuthorId.LABEL;
|
||||
import static org.briarproject.bramble.util.ByteUtils.INT_32_BYTES;
|
||||
import static org.briarproject.bramble.util.ByteUtils.writeUint32;
|
||||
import static org.briarproject.bramble.util.StringUtils.toUtf8;
|
||||
|
||||
@Immutable
|
||||
@NotNullByDefault
|
||||
@@ -58,8 +58,8 @@ class AuthorFactoryImpl implements AuthorFactory {
|
||||
|
||||
private AuthorId getId(int formatVersion, String name, byte[] publicKey) {
|
||||
byte[] formatVersionBytes = new byte[INT_32_BYTES];
|
||||
ByteUtils.writeUint32(formatVersion, formatVersionBytes, 0);
|
||||
writeUint32(formatVersion, formatVersionBytes, 0);
|
||||
return new AuthorId(crypto.hash(LABEL, formatVersionBytes,
|
||||
StringUtils.toUtf8(name), publicKey));
|
||||
toUtf8(name), publicKey));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,6 +16,7 @@ import javax.annotation.Nullable;
|
||||
import javax.annotation.concurrent.ThreadSafe;
|
||||
import javax.inject.Inject;
|
||||
|
||||
import static java.util.logging.Logger.getLogger;
|
||||
import static org.briarproject.bramble.util.LogUtils.logDuration;
|
||||
import static org.briarproject.bramble.util.LogUtils.now;
|
||||
|
||||
@@ -24,7 +25,7 @@ import static org.briarproject.bramble.util.LogUtils.now;
|
||||
class IdentityManagerImpl implements IdentityManager {
|
||||
|
||||
private static final Logger LOG =
|
||||
Logger.getLogger(IdentityManagerImpl.class.getName());
|
||||
getLogger(IdentityManagerImpl.class.getName());
|
||||
|
||||
private final DatabaseComponent db;
|
||||
private final CryptoComponent crypto;
|
||||
|
||||
@@ -2,7 +2,7 @@ package org.briarproject.bramble.keyagreement;
|
||||
|
||||
class AbortException extends Exception {
|
||||
|
||||
boolean receivedAbort;
|
||||
final boolean receivedAbort;
|
||||
|
||||
AbortException() {
|
||||
this(false);
|
||||
|
||||
@@ -20,13 +20,14 @@ import javax.annotation.concurrent.ThreadSafe;
|
||||
import javax.inject.Inject;
|
||||
|
||||
import static java.util.logging.Level.INFO;
|
||||
import static java.util.logging.Logger.getLogger;
|
||||
|
||||
@NotNullByDefault
|
||||
@ThreadSafe
|
||||
class ConnectionChooserImpl implements ConnectionChooser {
|
||||
|
||||
private static final Logger LOG =
|
||||
Logger.getLogger(ConnectionChooserImpl.class.getName());
|
||||
getLogger(ConnectionChooserImpl.class.getName());
|
||||
|
||||
private final Clock clock;
|
||||
private final Executor ioExecutor;
|
||||
|
||||
@@ -30,6 +30,7 @@ import javax.annotation.Nullable;
|
||||
|
||||
import static java.util.logging.Level.INFO;
|
||||
import static java.util.logging.Level.WARNING;
|
||||
import static java.util.logging.Logger.getLogger;
|
||||
import static org.briarproject.bramble.api.keyagreement.KeyAgreementConstants.CONNECTION_TIMEOUT;
|
||||
import static org.briarproject.bramble.util.LogUtils.logException;
|
||||
|
||||
@@ -41,7 +42,7 @@ class KeyAgreementConnector {
|
||||
}
|
||||
|
||||
private static final Logger LOG =
|
||||
Logger.getLogger(KeyAgreementConnector.class.getName());
|
||||
getLogger(KeyAgreementConnector.class.getName());
|
||||
|
||||
private final Callbacks callbacks;
|
||||
private final KeyAgreementCrypto keyAgreementCrypto;
|
||||
|
||||
@@ -28,6 +28,7 @@ import java.util.logging.Logger;
|
||||
import javax.inject.Inject;
|
||||
|
||||
import static java.util.logging.Level.WARNING;
|
||||
import static java.util.logging.Logger.getLogger;
|
||||
import static org.briarproject.bramble.util.LogUtils.logException;
|
||||
|
||||
@MethodsNotNullByDefault
|
||||
@@ -36,7 +37,7 @@ class KeyAgreementTaskImpl extends Thread implements KeyAgreementTask,
|
||||
KeyAgreementProtocol.Callbacks, KeyAgreementConnector.Callbacks {
|
||||
|
||||
private static final Logger LOG =
|
||||
Logger.getLogger(KeyAgreementTaskImpl.class.getName());
|
||||
getLogger(KeyAgreementTaskImpl.class.getName());
|
||||
|
||||
private final CryptoComponent crypto;
|
||||
private final KeyAgreementCrypto keyAgreementCrypto;
|
||||
|
||||
@@ -16,6 +16,7 @@ import java.io.OutputStream;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import static java.util.logging.Level.WARNING;
|
||||
import static java.util.logging.Logger.getLogger;
|
||||
import static org.briarproject.bramble.api.keyagreement.KeyAgreementConstants.PROTOCOL_VERSION;
|
||||
import static org.briarproject.bramble.api.keyagreement.RecordTypes.ABORT;
|
||||
import static org.briarproject.bramble.api.keyagreement.RecordTypes.CONFIRM;
|
||||
@@ -29,7 +30,7 @@ import static org.briarproject.bramble.util.LogUtils.logException;
|
||||
class KeyAgreementTransport {
|
||||
|
||||
private static final Logger LOG =
|
||||
Logger.getLogger(KeyAgreementTransport.class.getName());
|
||||
getLogger(KeyAgreementTransport.class.getName());
|
||||
|
||||
private final KeyAgreementConnection kac;
|
||||
private final RecordReader reader;
|
||||
|
||||
@@ -28,6 +28,7 @@ import javax.inject.Inject;
|
||||
import static java.util.logging.Level.FINE;
|
||||
import static java.util.logging.Level.INFO;
|
||||
import static java.util.logging.Level.WARNING;
|
||||
import static java.util.logging.Logger.getLogger;
|
||||
import static org.briarproject.bramble.api.lifecycle.LifecycleManager.LifecycleState.COMPACTING_DATABASE;
|
||||
import static org.briarproject.bramble.api.lifecycle.LifecycleManager.LifecycleState.MIGRATING_DATABASE;
|
||||
import static org.briarproject.bramble.api.lifecycle.LifecycleManager.LifecycleState.RUNNING;
|
||||
@@ -49,7 +50,7 @@ import static org.briarproject.bramble.util.LogUtils.now;
|
||||
class LifecycleManagerImpl implements LifecycleManager, MigrationListener {
|
||||
|
||||
private static final Logger LOG =
|
||||
Logger.getLogger(LifecycleManagerImpl.class.getName());
|
||||
getLogger(LifecycleManagerImpl.class.getName());
|
||||
|
||||
private final DatabaseComponent db;
|
||||
private final EventBus eventBus;
|
||||
|
||||
@@ -26,13 +26,14 @@ import java.util.logging.Logger;
|
||||
import javax.inject.Inject;
|
||||
|
||||
import static java.util.logging.Level.WARNING;
|
||||
import static java.util.logging.Logger.getLogger;
|
||||
import static org.briarproject.bramble.api.transport.TransportConstants.TAG_LENGTH;
|
||||
import static org.briarproject.bramble.util.LogUtils.logException;
|
||||
|
||||
class ConnectionManagerImpl implements ConnectionManager {
|
||||
|
||||
private static final Logger LOG =
|
||||
Logger.getLogger(ConnectionManagerImpl.class.getName());
|
||||
getLogger(ConnectionManagerImpl.class.getName());
|
||||
|
||||
private final Executor ioExecutor;
|
||||
private final KeyManager keyManager;
|
||||
|
||||
@@ -13,7 +13,6 @@ import org.briarproject.bramble.api.plugin.event.ContactDisconnectedEvent;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
@@ -24,14 +23,16 @@ import java.util.logging.Logger;
|
||||
import javax.annotation.concurrent.ThreadSafe;
|
||||
import javax.inject.Inject;
|
||||
|
||||
import static java.util.Collections.emptyList;
|
||||
import static java.util.logging.Level.INFO;
|
||||
import static java.util.logging.Logger.getLogger;
|
||||
|
||||
@ThreadSafe
|
||||
@NotNullByDefault
|
||||
class ConnectionRegistryImpl implements ConnectionRegistry {
|
||||
|
||||
private static final Logger LOG =
|
||||
Logger.getLogger(ConnectionRegistryImpl.class.getName());
|
||||
getLogger(ConnectionRegistryImpl.class.getName());
|
||||
|
||||
private final EventBus eventBus;
|
||||
private final Lock lock = new ReentrantLock();
|
||||
@@ -103,7 +104,7 @@ class ConnectionRegistryImpl implements ConnectionRegistry {
|
||||
lock.lock();
|
||||
try {
|
||||
Multiset<ContactId> m = connections.get(t);
|
||||
if (m == null) return Collections.emptyList();
|
||||
if (m == null) return emptyList();
|
||||
List<ContactId> ids = new ArrayList<>(m.keySet());
|
||||
if (LOG.isLoggable(INFO))
|
||||
LOG.info(ids.size() + " contacts connected: " + t);
|
||||
|
||||
@@ -52,6 +52,7 @@ import javax.inject.Inject;
|
||||
import static java.util.logging.Level.FINE;
|
||||
import static java.util.logging.Level.INFO;
|
||||
import static java.util.logging.Level.WARNING;
|
||||
import static java.util.logging.Logger.getLogger;
|
||||
import static org.briarproject.bramble.util.LogUtils.logDuration;
|
||||
import static org.briarproject.bramble.util.LogUtils.logException;
|
||||
import static org.briarproject.bramble.util.LogUtils.now;
|
||||
@@ -61,7 +62,7 @@ import static org.briarproject.bramble.util.LogUtils.now;
|
||||
class PluginManagerImpl implements PluginManager, Service {
|
||||
|
||||
private static final Logger LOG =
|
||||
Logger.getLogger(PluginManagerImpl.class.getName());
|
||||
getLogger(PluginManagerImpl.class.getName());
|
||||
|
||||
private final Executor ioExecutor;
|
||||
private final ScheduledExecutorService scheduler;
|
||||
|
||||
@@ -36,18 +36,20 @@ import java.util.concurrent.locks.Lock;
|
||||
import java.util.concurrent.locks.ReentrantLock;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import javax.annotation.concurrent.GuardedBy;
|
||||
import javax.annotation.concurrent.ThreadSafe;
|
||||
|
||||
import static java.util.concurrent.TimeUnit.MILLISECONDS;
|
||||
import static java.util.logging.Level.INFO;
|
||||
import static java.util.logging.Level.WARNING;
|
||||
import static java.util.logging.Logger.getLogger;
|
||||
import static org.briarproject.bramble.util.LogUtils.logException;
|
||||
|
||||
@ThreadSafe
|
||||
@NotNullByDefault
|
||||
class Poller implements EventListener {
|
||||
|
||||
private static final Logger LOG = Logger.getLogger(Poller.class.getName());
|
||||
private static final Logger LOG = getLogger(Poller.class.getName());
|
||||
|
||||
private final Executor ioExecutor;
|
||||
private final ScheduledExecutorService scheduler;
|
||||
@@ -58,7 +60,8 @@ class Poller implements EventListener {
|
||||
private final SecureRandom random;
|
||||
private final Clock clock;
|
||||
private final Lock lock;
|
||||
private final Map<TransportId, ScheduledPollTask> tasks; // Locking: lock
|
||||
@GuardedBy("lock")
|
||||
private final Map<TransportId, ScheduledPollTask> tasks;
|
||||
|
||||
Poller(@IoExecutor Executor ioExecutor,
|
||||
@Scheduler ScheduledExecutorService scheduler,
|
||||
|
||||
@@ -20,6 +20,7 @@ interface BluetoothConnectionLimiter {
|
||||
* Returns true if a contact connection can be opened. This method does not
|
||||
* need to be called for key agreement connections.
|
||||
*/
|
||||
@SuppressWarnings("BooleanMethodIsAlwaysInverted")
|
||||
boolean canOpenContactConnection();
|
||||
|
||||
/**
|
||||
|
||||
@@ -13,6 +13,7 @@ import javax.annotation.concurrent.ThreadSafe;
|
||||
|
||||
import static java.util.logging.Level.INFO;
|
||||
import static java.util.logging.Level.WARNING;
|
||||
import static java.util.logging.Logger.getLogger;
|
||||
import static org.briarproject.bramble.util.LogUtils.logException;
|
||||
|
||||
@NotNullByDefault
|
||||
@@ -20,7 +21,7 @@ import static org.briarproject.bramble.util.LogUtils.logException;
|
||||
class BluetoothConnectionLimiterImpl implements BluetoothConnectionLimiter {
|
||||
|
||||
private static final Logger LOG =
|
||||
Logger.getLogger(BluetoothConnectionLimiterImpl.class.getName());
|
||||
getLogger(BluetoothConnectionLimiterImpl.class.getName());
|
||||
|
||||
private final Object lock = new Object();
|
||||
// The following are locking: lock
|
||||
|
||||
@@ -37,6 +37,7 @@ import javax.annotation.Nullable;
|
||||
|
||||
import static java.util.logging.Level.INFO;
|
||||
import static java.util.logging.Level.WARNING;
|
||||
import static java.util.logging.Logger.getLogger;
|
||||
import static org.briarproject.bramble.api.keyagreement.KeyAgreementConstants.TRANSPORT_ID_BLUETOOTH;
|
||||
import static org.briarproject.bramble.api.plugin.BluetoothConstants.ID;
|
||||
import static org.briarproject.bramble.api.plugin.BluetoothConstants.PREF_BT_ENABLE;
|
||||
@@ -54,7 +55,7 @@ import static org.briarproject.bramble.util.StringUtils.macToString;
|
||||
abstract class BluetoothPlugin<SS> implements DuplexPlugin, EventListener {
|
||||
|
||||
private static final Logger LOG =
|
||||
Logger.getLogger(BluetoothPlugin.class.getName());
|
||||
getLogger(BluetoothPlugin.class.getName());
|
||||
|
||||
final BluetoothConnectionLimiter connectionLimiter;
|
||||
|
||||
|
||||
@@ -14,6 +14,7 @@ import java.io.IOException;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import static java.util.logging.Level.WARNING;
|
||||
import static java.util.logging.Logger.getLogger;
|
||||
import static org.briarproject.bramble.api.plugin.FileConstants.PROP_PATH;
|
||||
import static org.briarproject.bramble.util.LogUtils.logException;
|
||||
import static org.briarproject.bramble.util.StringUtils.isNullOrEmpty;
|
||||
@@ -21,8 +22,7 @@ import static org.briarproject.bramble.util.StringUtils.isNullOrEmpty;
|
||||
@NotNullByDefault
|
||||
abstract class FilePlugin implements SimplexPlugin {
|
||||
|
||||
private static final Logger LOG =
|
||||
Logger.getLogger(FilePlugin.class.getName());
|
||||
private static final Logger LOG = getLogger(FilePlugin.class.getName());
|
||||
|
||||
protected final SimplexPluginCallback callback;
|
||||
protected final int maxLatency;
|
||||
|
||||
@@ -8,13 +8,14 @@ import java.io.InputStream;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import static java.util.logging.Level.WARNING;
|
||||
import static java.util.logging.Logger.getLogger;
|
||||
import static org.briarproject.bramble.util.IoUtils.tryToClose;
|
||||
|
||||
@NotNullByDefault
|
||||
class FileTransportReader implements TransportConnectionReader {
|
||||
|
||||
private static final Logger LOG =
|
||||
Logger.getLogger(FileTransportReader.class.getName());
|
||||
getLogger(FileTransportReader.class.getName());
|
||||
|
||||
private final File file;
|
||||
private final InputStream in;
|
||||
|
||||
@@ -8,13 +8,14 @@ import java.io.OutputStream;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import static java.util.logging.Level.WARNING;
|
||||
import static java.util.logging.Logger.getLogger;
|
||||
import static org.briarproject.bramble.util.IoUtils.tryToClose;
|
||||
|
||||
@NotNullByDefault
|
||||
class FileTransportWriter implements TransportConnectionWriter {
|
||||
|
||||
private static final Logger LOG =
|
||||
Logger.getLogger(FileTransportWriter.class.getName());
|
||||
getLogger(FileTransportWriter.class.getName());
|
||||
|
||||
private final File file;
|
||||
private final OutputStream out;
|
||||
|
||||
@@ -12,7 +12,6 @@ import org.briarproject.bramble.api.plugin.duplex.DuplexTransportConnection;
|
||||
import org.briarproject.bramble.api.properties.TransportProperties;
|
||||
import org.briarproject.bramble.api.settings.Settings;
|
||||
import org.briarproject.bramble.util.IoUtils;
|
||||
import org.briarproject.bramble.util.StringUtils;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.Inet4Address;
|
||||
@@ -23,26 +22,30 @@ import java.net.Socket;
|
||||
import java.net.SocketAddress;
|
||||
import java.net.UnknownHostException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.Executor;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import static java.util.Collections.addAll;
|
||||
import static java.util.Collections.emptyList;
|
||||
import static java.util.Collections.sort;
|
||||
import static java.util.logging.Level.INFO;
|
||||
import static java.util.logging.Level.WARNING;
|
||||
import static java.util.logging.Logger.getLogger;
|
||||
import static org.briarproject.bramble.api.keyagreement.KeyAgreementConstants.TRANSPORT_ID_LAN;
|
||||
import static org.briarproject.bramble.api.plugin.LanTcpConstants.ID;
|
||||
import static org.briarproject.bramble.api.plugin.LanTcpConstants.PREF_LAN_IP_PORTS;
|
||||
import static org.briarproject.bramble.api.plugin.LanTcpConstants.PROP_IP_PORTS;
|
||||
import static org.briarproject.bramble.util.ByteUtils.MAX_16_BIT_UNSIGNED;
|
||||
import static org.briarproject.bramble.util.PrivacyUtils.scrubSocketAddress;
|
||||
import static org.briarproject.bramble.util.StringUtils.isNullOrEmpty;
|
||||
import static org.briarproject.bramble.util.StringUtils.join;
|
||||
|
||||
@NotNullByDefault
|
||||
class LanTcpPlugin extends TcpPlugin {
|
||||
|
||||
private static final Logger LOG =
|
||||
Logger.getLogger(LanTcpPlugin.class.getName());
|
||||
private static final Logger LOG = getLogger(LanTcpPlugin.class.getName());
|
||||
|
||||
private static final LanAddressComparator ADDRESS_COMPARATOR =
|
||||
new LanAddressComparator();
|
||||
@@ -77,12 +80,13 @@ class LanTcpPlugin extends TcpPlugin {
|
||||
locals.add(new InetSocketAddress(local, 0));
|
||||
}
|
||||
}
|
||||
Collections.sort(locals, ADDRESS_COMPARATOR);
|
||||
//noinspection Java8ListSort
|
||||
sort(locals, ADDRESS_COMPARATOR);
|
||||
return locals;
|
||||
}
|
||||
|
||||
private List<InetSocketAddress> parseSocketAddresses(String ipPorts) {
|
||||
if (StringUtils.isNullOrEmpty(ipPorts)) return Collections.emptyList();
|
||||
if (isNullOrEmpty(ipPorts)) return emptyList();
|
||||
String[] split = ipPorts.split(SEPARATOR);
|
||||
List<InetSocketAddress> addresses = new ArrayList<>();
|
||||
for (String ipPort : split) {
|
||||
@@ -98,24 +102,23 @@ class LanTcpPlugin extends TcpPlugin {
|
||||
// Get the list of recently used addresses
|
||||
String setting = callback.getSettings().get(PREF_LAN_IP_PORTS);
|
||||
List<String> recent = new ArrayList<>();
|
||||
if (!StringUtils.isNullOrEmpty(setting))
|
||||
Collections.addAll(recent, setting.split(SEPARATOR));
|
||||
if (!isNullOrEmpty(setting)) addAll(recent, setting.split(SEPARATOR));
|
||||
// Is the address already in the list?
|
||||
if (recent.remove(ipPort)) {
|
||||
// Move the address to the start of the list
|
||||
recent.add(0, ipPort);
|
||||
setting = StringUtils.join(recent, SEPARATOR);
|
||||
setting = join(recent, SEPARATOR);
|
||||
} else {
|
||||
// Add the address to the start of the list
|
||||
recent.add(0, ipPort);
|
||||
// Drop the least recently used address if the list is full
|
||||
if (recent.size() > MAX_ADDRESSES)
|
||||
recent = recent.subList(0, MAX_ADDRESSES);
|
||||
setting = StringUtils.join(recent, SEPARATOR);
|
||||
setting = join(recent, SEPARATOR);
|
||||
// Update the list of addresses shared with contacts
|
||||
List<String> shared = new ArrayList<>(recent);
|
||||
Collections.sort(shared);
|
||||
String property = StringUtils.join(shared, SEPARATOR);
|
||||
sort(shared);
|
||||
String property = join(shared, SEPARATOR);
|
||||
TransportProperties properties = new TransportProperties();
|
||||
properties.put(PROP_IP_PORTS, property);
|
||||
callback.mergeLocalProperties(properties);
|
||||
|
||||
@@ -17,6 +17,7 @@ import javax.xml.parsers.ParserConfigurationException;
|
||||
|
||||
import static java.util.logging.Level.INFO;
|
||||
import static java.util.logging.Level.WARNING;
|
||||
import static java.util.logging.Logger.getLogger;
|
||||
import static org.briarproject.bramble.util.LogUtils.logException;
|
||||
import static org.briarproject.bramble.util.PrivacyUtils.scrubInetAddress;
|
||||
|
||||
@@ -25,8 +26,7 @@ import static org.briarproject.bramble.util.PrivacyUtils.scrubInetAddress;
|
||||
@ParametersNotNullByDefault
|
||||
class PortMapperImpl implements PortMapper {
|
||||
|
||||
private static final Logger LOG =
|
||||
Logger.getLogger(PortMapperImpl.class.getName());
|
||||
private static final Logger LOG = getLogger(PortMapperImpl.class.getName());
|
||||
|
||||
private final ShutdownManager shutdownManager;
|
||||
private final AtomicBoolean started = new AtomicBoolean(false);
|
||||
|
||||
@@ -12,7 +12,6 @@ import org.briarproject.bramble.api.plugin.duplex.DuplexPluginCallback;
|
||||
import org.briarproject.bramble.api.plugin.duplex.DuplexTransportConnection;
|
||||
import org.briarproject.bramble.api.properties.TransportProperties;
|
||||
import org.briarproject.bramble.util.IoUtils;
|
||||
import org.briarproject.bramble.util.StringUtils;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.InetAddress;
|
||||
@@ -41,17 +40,19 @@ import static java.util.Collections.emptyList;
|
||||
import static java.util.Collections.list;
|
||||
import static java.util.logging.Level.INFO;
|
||||
import static java.util.logging.Level.WARNING;
|
||||
import static java.util.logging.Logger.getLogger;
|
||||
import static org.briarproject.bramble.util.LogUtils.logException;
|
||||
import static org.briarproject.bramble.util.PrivacyUtils.scrubSocketAddress;
|
||||
import static org.briarproject.bramble.util.StringUtils.isNullOrEmpty;
|
||||
|
||||
@MethodsNotNullByDefault
|
||||
@ParametersNotNullByDefault
|
||||
abstract class TcpPlugin implements DuplexPlugin {
|
||||
|
||||
private static final Logger LOG = getLogger(TcpPlugin.class.getName());
|
||||
|
||||
private static final Pattern DOTTED_QUAD =
|
||||
Pattern.compile("^\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}$");
|
||||
private static final Logger LOG =
|
||||
Logger.getLogger(TcpPlugin.class.getName());
|
||||
|
||||
protected final Executor ioExecutor, bindExecutor;
|
||||
protected final Backoff backoff;
|
||||
@@ -84,6 +85,7 @@ abstract class TcpPlugin implements DuplexPlugin {
|
||||
/**
|
||||
* Returns true if connections to the given address can be attempted.
|
||||
*/
|
||||
@SuppressWarnings("BooleanMethodIsAlwaysInverted")
|
||||
protected abstract boolean isConnectable(InetSocketAddress remote);
|
||||
|
||||
TcpPlugin(Executor ioExecutor, Backoff backoff,
|
||||
@@ -263,7 +265,7 @@ abstract class TcpPlugin implements DuplexPlugin {
|
||||
|
||||
@Nullable
|
||||
InetSocketAddress parseSocketAddress(String ipPort) {
|
||||
if (StringUtils.isNullOrEmpty(ipPort)) return null;
|
||||
if (isNullOrEmpty(ipPort)) return null;
|
||||
String[] split = ipPort.split(":");
|
||||
if (split.length != 2) return null;
|
||||
String addr = split[0], port = split[1];
|
||||
|
||||
@@ -10,11 +10,12 @@ import org.briarproject.bramble.api.properties.TransportProperties;
|
||||
import java.net.Inet4Address;
|
||||
import java.net.InetAddress;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.util.Collections;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.Executor;
|
||||
|
||||
import static java.util.Collections.emptyList;
|
||||
import static java.util.Collections.singletonList;
|
||||
import static org.briarproject.bramble.api.plugin.WanTcpConstants.ID;
|
||||
|
||||
@MethodsNotNullByDefault
|
||||
@@ -80,8 +81,7 @@ class WanTcpPlugin extends TcpPlugin {
|
||||
protected List<InetSocketAddress> getRemoteSocketAddresses(
|
||||
TransportProperties p) {
|
||||
InetSocketAddress parsed = parseSocketAddress(p.get(PROP_IP_PORT));
|
||||
if (parsed == null) return Collections.emptyList();
|
||||
return Collections.singletonList(parsed);
|
||||
return parsed == null ? emptyList() : singletonList(parsed);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -42,9 +42,7 @@ import java.net.InetSocketAddress;
|
||||
import java.net.ServerSocket;
|
||||
import java.net.Socket;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
@@ -58,8 +56,12 @@ import java.util.zip.ZipInputStream;
|
||||
import javax.annotation.Nullable;
|
||||
import javax.net.SocketFactory;
|
||||
|
||||
import static java.util.Arrays.asList;
|
||||
import static java.util.Collections.singletonList;
|
||||
import static java.util.Collections.singletonMap;
|
||||
import static java.util.logging.Level.INFO;
|
||||
import static java.util.logging.Level.WARNING;
|
||||
import static java.util.logging.Logger.getLogger;
|
||||
import static net.freehaven.tor.control.TorControlCommands.HS_ADDRESS;
|
||||
import static net.freehaven.tor.control.TorControlCommands.HS_PRIVKEY;
|
||||
import static org.briarproject.bramble.api.plugin.TorConstants.CONTROL_PORT;
|
||||
@@ -73,6 +75,7 @@ import static org.briarproject.bramble.api.plugin.TorConstants.PREF_TOR_ONLY_WHE
|
||||
import static org.briarproject.bramble.api.plugin.TorConstants.PREF_TOR_PORT;
|
||||
import static org.briarproject.bramble.api.plugin.TorConstants.PROP_ONION_V2;
|
||||
import static org.briarproject.bramble.api.plugin.TorConstants.PROP_ONION_V3;
|
||||
import static org.briarproject.bramble.util.IoUtils.copyAndClose;
|
||||
import static org.briarproject.bramble.util.LogUtils.logException;
|
||||
import static org.briarproject.bramble.util.PrivacyUtils.scrubOnion;
|
||||
import static org.briarproject.bramble.util.StringUtils.isNullOrEmpty;
|
||||
@@ -81,8 +84,7 @@ import static org.briarproject.bramble.util.StringUtils.isNullOrEmpty;
|
||||
@ParametersNotNullByDefault
|
||||
abstract class TorPlugin implements DuplexPlugin, EventHandler, EventListener {
|
||||
|
||||
private static final Logger LOG =
|
||||
Logger.getLogger(TorPlugin.class.getName());
|
||||
private static final Logger LOG = getLogger(TorPlugin.class.getName());
|
||||
|
||||
private static final String[] EVENTS = {
|
||||
"CIRC", "ORCONN", "HS_DESC", "NOTICE", "WARN", "ERR"
|
||||
@@ -251,11 +253,11 @@ abstract class TorPlugin implements DuplexPlugin, EventHandler, EventListener {
|
||||
controlConnection.authenticate(read(cookieFile));
|
||||
// Tell Tor to exit when the control connection is closed
|
||||
controlConnection.takeOwnership();
|
||||
controlConnection.resetConf(Collections.singletonList(OWNER));
|
||||
controlConnection.resetConf(singletonList(OWNER));
|
||||
running = true;
|
||||
// Register to receive events from the Tor process
|
||||
controlConnection.setEventHandler(this);
|
||||
controlConnection.setEvents(Arrays.asList(EVENTS));
|
||||
controlConnection.setEvents(asList(EVENTS));
|
||||
// Check whether Tor has already bootstrapped
|
||||
String phase = controlConnection.getInfo("status/bootstrap-phase");
|
||||
if (phase != null && phase.contains("PROGRESS=100")) {
|
||||
@@ -280,28 +282,31 @@ abstract class TorPlugin implements DuplexPlugin, EventHandler, EventListener {
|
||||
InputStream in = null;
|
||||
OutputStream out = null;
|
||||
try {
|
||||
// The done file may already exist from a previous installation
|
||||
//noinspection ResultOfMethodCallIgnored
|
||||
doneFile.delete();
|
||||
// Unzip the Tor binary to the filesystem
|
||||
in = getTorInputStream();
|
||||
out = new FileOutputStream(torFile);
|
||||
IoUtils.copyAndClose(in, out);
|
||||
copyAndClose(in, out);
|
||||
// Make the Tor binary executable
|
||||
if (!torFile.setExecutable(true, true)) throw new IOException();
|
||||
// Unzip the GeoIP database to the filesystem
|
||||
in = getGeoIpInputStream();
|
||||
out = new FileOutputStream(geoIpFile);
|
||||
IoUtils.copyAndClose(in, out);
|
||||
copyAndClose(in, out);
|
||||
// Unzip the Obfs4 proxy to the filesystem
|
||||
in = getObfs4InputStream();
|
||||
out = new FileOutputStream(obfs4File);
|
||||
IoUtils.copyAndClose(in, out);
|
||||
copyAndClose(in, out);
|
||||
// Make the Obfs4 proxy executable
|
||||
if (!obfs4File.setExecutable(true, true)) throw new IOException();
|
||||
// Copy the config file to the filesystem
|
||||
in = getConfigInputStream();
|
||||
out = new FileOutputStream(configFile);
|
||||
IoUtils.copyAndClose(in, out);
|
||||
doneFile.createNewFile();
|
||||
copyAndClose(in, out);
|
||||
if (!doneFile.createNewFile())
|
||||
LOG.warning("Failed to create done file");
|
||||
} catch (IOException e) {
|
||||
IoUtils.tryToClose(in, LOG, WARNING);
|
||||
IoUtils.tryToClose(out, LOG, WARNING);
|
||||
@@ -410,8 +415,7 @@ abstract class TorPlugin implements DuplexPlugin, EventHandler, EventListener {
|
||||
if (!running) return;
|
||||
LOG.info("Creating hidden service");
|
||||
String privKey = settings.get(HS_PRIVKEY);
|
||||
Map<Integer, String> portLines =
|
||||
Collections.singletonMap(80, "127.0.0.1:" + port);
|
||||
Map<Integer, String> portLines = singletonMap(80, "127.0.0.1:" + port);
|
||||
Map<String, String> response;
|
||||
try {
|
||||
// Use the control connection to set up the hidden service
|
||||
|
||||
@@ -4,7 +4,6 @@ import org.briarproject.bramble.api.FormatException;
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
import org.briarproject.bramble.api.record.Record;
|
||||
import org.briarproject.bramble.api.record.RecordReader;
|
||||
import org.briarproject.bramble.util.ByteUtils;
|
||||
|
||||
import java.io.DataInputStream;
|
||||
import java.io.IOException;
|
||||
@@ -14,6 +13,7 @@ import javax.annotation.concurrent.NotThreadSafe;
|
||||
|
||||
import static org.briarproject.bramble.api.record.Record.MAX_RECORD_PAYLOAD_BYTES;
|
||||
import static org.briarproject.bramble.api.record.Record.RECORD_HEADER_BYTES;
|
||||
import static org.briarproject.bramble.util.ByteUtils.readUint16;
|
||||
|
||||
@NotThreadSafe
|
||||
@NotNullByDefault
|
||||
@@ -31,7 +31,7 @@ class RecordReaderImpl implements RecordReader {
|
||||
in.readFully(header);
|
||||
byte protocolVersion = header[0];
|
||||
byte recordType = header[1];
|
||||
int payloadLength = ByteUtils.readUint16(header, 2);
|
||||
int payloadLength = readUint16(header, 2);
|
||||
if (payloadLength < 0 || payloadLength > MAX_RECORD_PAYLOAD_BYTES)
|
||||
throw new FormatException();
|
||||
byte[] payload = new byte[payloadLength];
|
||||
|
||||
@@ -3,7 +3,6 @@ package org.briarproject.bramble.record;
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
import org.briarproject.bramble.api.record.Record;
|
||||
import org.briarproject.bramble.api.record.RecordWriter;
|
||||
import org.briarproject.bramble.util.ByteUtils;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
@@ -11,6 +10,7 @@ import java.io.OutputStream;
|
||||
import javax.annotation.concurrent.NotThreadSafe;
|
||||
|
||||
import static org.briarproject.bramble.api.record.Record.RECORD_HEADER_BYTES;
|
||||
import static org.briarproject.bramble.util.ByteUtils.writeUint16;
|
||||
|
||||
@NotThreadSafe
|
||||
@NotNullByDefault
|
||||
@@ -28,7 +28,7 @@ class RecordWriterImpl implements RecordWriter {
|
||||
byte[] payload = r.getPayload();
|
||||
header[0] = r.getProtocolVersion();
|
||||
header[1] = r.getRecordType();
|
||||
ByteUtils.writeUint16(payload.length, header, 2);
|
||||
writeUint16(payload.length, header, 2);
|
||||
out.write(header);
|
||||
out.write(payload);
|
||||
}
|
||||
|
||||
@@ -1,10 +1,12 @@
|
||||
package org.briarproject.bramble.reliability;
|
||||
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
import org.briarproject.bramble.util.ByteUtils;
|
||||
|
||||
import javax.annotation.concurrent.NotThreadSafe;
|
||||
|
||||
import static org.briarproject.bramble.util.ByteUtils.readUint16;
|
||||
import static org.briarproject.bramble.util.ByteUtils.writeUint16;
|
||||
|
||||
@NotThreadSafe
|
||||
@NotNullByDefault
|
||||
class Ack extends Frame {
|
||||
@@ -23,10 +25,10 @@ class Ack extends Frame {
|
||||
}
|
||||
|
||||
int getWindowSize() {
|
||||
return ByteUtils.readUint16(buf, 5);
|
||||
return readUint16(buf, 5);
|
||||
}
|
||||
|
||||
void setWindowSize(int windowSize) {
|
||||
ByteUtils.writeUint16(windowSize, buf, 5);
|
||||
writeUint16(windowSize, buf, 5);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,17 +1,19 @@
|
||||
package org.briarproject.bramble.reliability;
|
||||
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
import org.briarproject.bramble.util.ByteUtils;
|
||||
|
||||
import javax.annotation.concurrent.NotThreadSafe;
|
||||
|
||||
import static org.briarproject.bramble.util.ByteUtils.readUint32;
|
||||
import static org.briarproject.bramble.util.ByteUtils.writeUint32;
|
||||
|
||||
@NotThreadSafe
|
||||
@NotNullByDefault
|
||||
abstract class Frame {
|
||||
|
||||
static final byte ACK_FLAG = (byte) 128, FIN_FLAG = 64;
|
||||
|
||||
protected final byte[] buf;
|
||||
final byte[] buf;
|
||||
|
||||
Frame(byte[] buf) {
|
||||
this.buf = buf;
|
||||
@@ -26,11 +28,11 @@ abstract class Frame {
|
||||
}
|
||||
|
||||
long getChecksum() {
|
||||
return ByteUtils.readUint32(buf, buf.length - 4);
|
||||
return readUint32(buf, buf.length - 4);
|
||||
}
|
||||
|
||||
void setChecksum(long checksum) {
|
||||
ByteUtils.writeUint32(checksum, buf, buf.length - 4);
|
||||
writeUint32(checksum, buf, buf.length - 4);
|
||||
}
|
||||
|
||||
long calculateChecksum() {
|
||||
@@ -38,11 +40,11 @@ abstract class Frame {
|
||||
}
|
||||
|
||||
long getSequenceNumber() {
|
||||
return ByteUtils.readUint32(buf, 1);
|
||||
return readUint32(buf, 1);
|
||||
}
|
||||
|
||||
void setSequenceNumber(long sequenceNumber) {
|
||||
ByteUtils.writeUint32(sequenceNumber, buf, 1);
|
||||
writeUint32(sequenceNumber, buf, 1);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -101,6 +101,7 @@ class Receiver implements ReadHandler {
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("StatementWithEmptyBody")
|
||||
private void handleData(byte[] b) throws IOException {
|
||||
windowLock.lock();
|
||||
try {
|
||||
@@ -124,6 +125,7 @@ class Receiver implements ReadHandler {
|
||||
finalSequenceNumber = sequenceNumber;
|
||||
// Remove any data frames with higher sequence numbers
|
||||
Iterator<Data> it = dataFrames.iterator();
|
||||
//noinspection Java8CollectionRemoveIf
|
||||
while (it.hasNext()) {
|
||||
Data d1 = it.next();
|
||||
if (d1.getSequenceNumber() >= finalSequenceNumber)
|
||||
@@ -148,6 +150,7 @@ class Receiver implements ReadHandler {
|
||||
|
||||
private static class SequenceNumberComparator implements Comparator<Data> {
|
||||
|
||||
@SuppressWarnings("UseCompareMethod")
|
||||
@Override
|
||||
public int compare(Data d1, Data d2) {
|
||||
long s1 = d1.getSequenceNumber(), s2 = d2.getSequenceNumber();
|
||||
|
||||
@@ -8,6 +8,8 @@ import java.io.InputStream;
|
||||
import javax.annotation.Nullable;
|
||||
import javax.annotation.concurrent.NotThreadSafe;
|
||||
|
||||
import static java.lang.System.arraycopy;
|
||||
|
||||
@NotThreadSafe
|
||||
@NotNullByDefault
|
||||
class ReceiverInputStream extends InputStream {
|
||||
@@ -44,12 +46,13 @@ class ReceiverInputStream extends InputStream {
|
||||
while (length == 0) if (!receive()) return -1;
|
||||
if (data == null) throw new AssertionError();
|
||||
len = Math.min(len, length);
|
||||
System.arraycopy(data.getBuffer(), offset, b, off, len);
|
||||
arraycopy(data.getBuffer(), offset, b, off, len);
|
||||
offset += len;
|
||||
length -= len;
|
||||
return len;
|
||||
}
|
||||
|
||||
@SuppressWarnings("BooleanMethodIsAlwaysInverted")
|
||||
private boolean receive() throws IOException {
|
||||
if (length != 0) throw new AssertionError();
|
||||
if (data != null && data.isLastFrame()) {
|
||||
|
||||
@@ -16,6 +16,7 @@ import java.util.logging.Logger;
|
||||
|
||||
import static java.util.concurrent.TimeUnit.MILLISECONDS;
|
||||
import static java.util.logging.Level.WARNING;
|
||||
import static java.util.logging.Logger.getLogger;
|
||||
import static org.briarproject.bramble.util.LogUtils.logException;
|
||||
|
||||
@MethodsNotNullByDefault
|
||||
@@ -25,7 +26,7 @@ class ReliabilityLayerImpl implements ReliabilityLayer, WriteHandler {
|
||||
private static final int TICK_INTERVAL = 500; // Milliseconds
|
||||
|
||||
private static final Logger LOG =
|
||||
Logger.getLogger(ReliabilityLayerImpl.class.getName());
|
||||
getLogger(ReliabilityLayerImpl.class.getName());
|
||||
|
||||
private final Executor executor;
|
||||
private final Clock clock;
|
||||
|
||||
@@ -16,6 +16,7 @@ import java.util.concurrent.locks.ReentrantLock;
|
||||
import javax.annotation.concurrent.ThreadSafe;
|
||||
|
||||
import static java.util.concurrent.TimeUnit.MILLISECONDS;
|
||||
import static org.briarproject.bramble.api.nullsafety.NullSafety.requireNonNull;
|
||||
|
||||
@ThreadSafe
|
||||
@NotNullByDefault
|
||||
@@ -96,7 +97,7 @@ class Sender {
|
||||
}
|
||||
// If any older data frames are outstanding, retransmit the oldest
|
||||
if (foundIndex > 0) {
|
||||
fastRetransmit = outstanding.poll();
|
||||
fastRetransmit = requireNonNull(outstanding.poll());
|
||||
fastRetransmit.lastTransmitted = now;
|
||||
fastRetransmit.retransmitted = true;
|
||||
outstanding.add(fastRetransmit);
|
||||
@@ -191,7 +192,7 @@ class Sender {
|
||||
writeHandler.handleWrite(d.getBuffer());
|
||||
}
|
||||
|
||||
void flush() throws IOException, InterruptedException {
|
||||
void flush() throws InterruptedException {
|
||||
windowLock.lock();
|
||||
try {
|
||||
while (dataWaiting || !outstanding.isEmpty())
|
||||
|
||||
@@ -7,6 +7,8 @@ import java.io.OutputStream;
|
||||
|
||||
import javax.annotation.concurrent.NotThreadSafe;
|
||||
|
||||
import static java.lang.System.arraycopy;
|
||||
|
||||
@NotThreadSafe
|
||||
@NotNullByDefault
|
||||
class SenderOutputStream extends OutputStream {
|
||||
@@ -59,20 +61,20 @@ class SenderOutputStream extends OutputStream {
|
||||
public void write(byte[] b, int off, int len) throws IOException {
|
||||
int available = Data.MAX_LENGTH - offset - Data.FOOTER_LENGTH;
|
||||
while (available <= len) {
|
||||
System.arraycopy(b, off, buf, offset, available);
|
||||
arraycopy(b, off, buf, offset, available);
|
||||
offset += available;
|
||||
send(false);
|
||||
off += available;
|
||||
len -= available;
|
||||
available = Data.MAX_LENGTH - offset - Data.FOOTER_LENGTH;
|
||||
}
|
||||
System.arraycopy(b, off, buf, offset, len);
|
||||
arraycopy(b, off, buf, offset, len);
|
||||
offset += len;
|
||||
}
|
||||
|
||||
private void send(boolean lastFrame) throws IOException {
|
||||
byte[] frame = new byte[offset + Data.FOOTER_LENGTH];
|
||||
System.arraycopy(buf, 0, frame, 0, frame.length);
|
||||
arraycopy(buf, 0, frame, 0, frame.length);
|
||||
Data d = new Data(frame);
|
||||
d.setLastFrame(lastFrame);
|
||||
d.setSequenceNumber(sequenceNumber++);
|
||||
|
||||
@@ -7,6 +7,8 @@ import java.io.IOException;
|
||||
|
||||
import javax.annotation.concurrent.Immutable;
|
||||
|
||||
import static java.lang.System.arraycopy;
|
||||
|
||||
@Immutable
|
||||
@NotNullByDefault
|
||||
class SlipDecoder implements ReadHandler {
|
||||
@@ -36,7 +38,7 @@ class SlipDecoder implements ReadHandler {
|
||||
} else {
|
||||
if (decodedLength > 0) {
|
||||
byte[] decoded = new byte[decodedLength];
|
||||
System.arraycopy(buf, 0, decoded, 0, decodedLength);
|
||||
arraycopy(buf, 0, decoded, 0, decodedLength);
|
||||
readHandler.handleRead(decoded);
|
||||
}
|
||||
reset(false);
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
package org.briarproject.bramble.reporting;
|
||||
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
import org.briarproject.bramble.util.IoUtils;
|
||||
|
||||
import java.io.Closeable;
|
||||
import java.io.File;
|
||||
@@ -17,6 +16,9 @@ import java.util.concurrent.Semaphore;
|
||||
import javax.annotation.Nullable;
|
||||
import javax.annotation.concurrent.Immutable;
|
||||
|
||||
import static java.io.File.createTempFile;
|
||||
import static org.briarproject.bramble.util.IoUtils.getInputStream;
|
||||
|
||||
@Immutable
|
||||
@NotNullByDefault
|
||||
public class DevReportServer {
|
||||
@@ -42,6 +44,7 @@ public class DevReportServer {
|
||||
TokenBucket bucket = new TokenBucket();
|
||||
bucket.start();
|
||||
try {
|
||||
//noinspection InfiniteLoopStatement
|
||||
while (true) {
|
||||
Socket s = ss.accept();
|
||||
System.out.println("Incoming connection");
|
||||
@@ -101,6 +104,7 @@ public class DevReportServer {
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
//noinspection InfiniteLoopStatement
|
||||
while (true) {
|
||||
// If the bucket isn't full, add a token
|
||||
if (semaphore.availablePermits() < MAX_TOKENS) {
|
||||
@@ -131,9 +135,11 @@ public class DevReportServer {
|
||||
OutputStream out = null;
|
||||
try {
|
||||
socket.setSoTimeout(SOCKET_TIMEOUT_MS);
|
||||
in = IoUtils.getInputStream(socket);
|
||||
in = getInputStream(socket);
|
||||
// Directory may already exist
|
||||
//noinspection ResultOfMethodCallIgnored
|
||||
reportDir.mkdirs();
|
||||
reportFile = File.createTempFile(FILE_PREFIX, FILE_SUFFIX,
|
||||
reportFile = createTempFile(FILE_PREFIX, FILE_SUFFIX,
|
||||
reportDir);
|
||||
out = new FileOutputStream(reportFile);
|
||||
System.out.println("Saving report to " + reportFile);
|
||||
@@ -151,7 +157,8 @@ public class DevReportServer {
|
||||
System.out.println("Saved " + length + " bytes");
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
if (reportFile != null) reportFile.delete();
|
||||
if (reportFile != null && !reportFile.delete())
|
||||
System.err.println("Failed to delete report");
|
||||
} finally {
|
||||
tryToClose(in);
|
||||
tryToClose(out);
|
||||
|
||||
@@ -9,8 +9,6 @@ import org.briarproject.bramble.api.plugin.TorConstants;
|
||||
import org.briarproject.bramble.api.plugin.event.TransportEnabledEvent;
|
||||
import org.briarproject.bramble.api.reporting.DevConfig;
|
||||
import org.briarproject.bramble.api.reporting.DevReporter;
|
||||
import org.briarproject.bramble.util.IoUtils;
|
||||
import org.briarproject.bramble.util.StringUtils;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
@@ -30,14 +28,18 @@ import javax.inject.Inject;
|
||||
import javax.net.SocketFactory;
|
||||
|
||||
import static java.util.logging.Level.WARNING;
|
||||
import static java.util.logging.Logger.getLogger;
|
||||
import static org.briarproject.bramble.util.IoUtils.copyAndClose;
|
||||
import static org.briarproject.bramble.util.IoUtils.getOutputStream;
|
||||
import static org.briarproject.bramble.util.IoUtils.tryToClose;
|
||||
import static org.briarproject.bramble.util.StringUtils.toUtf8;
|
||||
|
||||
@Immutable
|
||||
@NotNullByDefault
|
||||
class DevReporterImpl implements DevReporter, EventListener {
|
||||
|
||||
private static final Logger LOG =
|
||||
Logger.getLogger(DevReporterImpl.class.getName());
|
||||
getLogger(DevReporterImpl.class.getName());
|
||||
|
||||
private static final int SOCKET_TIMEOUT = 30 * 1000; // 30 seconds
|
||||
private static final int LINE_LENGTH = 70;
|
||||
@@ -73,7 +75,7 @@ class DevReporterImpl implements DevReporter, EventListener {
|
||||
public void encryptReportToFile(File reportDir, String filename,
|
||||
String report) throws FileNotFoundException {
|
||||
LOG.info("Encrypting report to file");
|
||||
byte[] plaintext = StringUtils.toUtf8(report);
|
||||
byte[] plaintext = toUtf8(report);
|
||||
byte[] ciphertext = crypto.encryptToKey(devConfig.getDevPublicKey(),
|
||||
plaintext);
|
||||
String armoured = crypto.asciiArmour(ciphertext, LINE_LENGTH);
|
||||
@@ -109,17 +111,15 @@ class DevReporterImpl implements DevReporter, EventListener {
|
||||
LOG.info("Sending reports to developers");
|
||||
for (File f : reports) {
|
||||
OutputStream out = null;
|
||||
InputStream in = null;
|
||||
try {
|
||||
Socket s = connectToDevelopers();
|
||||
out = IoUtils.getOutputStream(s);
|
||||
in = new FileInputStream(f);
|
||||
IoUtils.copyAndClose(in, out);
|
||||
f.delete();
|
||||
out = getOutputStream(s);
|
||||
InputStream in = new FileInputStream(f);
|
||||
copyAndClose(in, out);
|
||||
if (!f.delete()) LOG.warning("Failed to delete report");
|
||||
} catch (IOException e) {
|
||||
LOG.log(WARNING, "Failed to send reports", e);
|
||||
tryToClose(out, LOG, WARNING);
|
||||
tryToClose(in, LOG, WARNING);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
package org.briarproject.bramble.socks;
|
||||
|
||||
import org.briarproject.bramble.util.ByteUtils;
|
||||
import org.briarproject.bramble.util.IoUtils;
|
||||
|
||||
import java.io.IOException;
|
||||
@@ -12,6 +11,9 @@ import java.net.Socket;
|
||||
import java.net.SocketAddress;
|
||||
import java.util.Arrays;
|
||||
|
||||
import static org.briarproject.bramble.util.ByteUtils.writeUint16;
|
||||
import static org.briarproject.bramble.util.IoUtils.read;
|
||||
|
||||
class SocksSocket extends Socket {
|
||||
|
||||
private static final String[] ERRORS = {
|
||||
@@ -26,6 +28,7 @@ class SocksSocket extends Socket {
|
||||
"Address type not supported"
|
||||
};
|
||||
|
||||
@SuppressWarnings("MismatchedReadAndWriteOfArray")
|
||||
private static final byte[] UNSPECIFIED_ADDRESS = new byte[4];
|
||||
|
||||
private final SocketAddress proxy;
|
||||
@@ -88,7 +91,7 @@ class SocksSocket extends Socket {
|
||||
|
||||
private void receiveMethodResponse(InputStream in) throws IOException {
|
||||
byte[] methodResponse = new byte[2];
|
||||
IoUtils.read(in, methodResponse);
|
||||
read(in, methodResponse);
|
||||
byte version = methodResponse[0];
|
||||
byte method = methodResponse[1];
|
||||
if (version != 5)
|
||||
@@ -108,14 +111,14 @@ class SocksSocket extends Socket {
|
||||
connectRequest[4] = (byte) host.length(); // Length of domain name
|
||||
for (int i = 0; i < host.length(); i++)
|
||||
connectRequest[5 + i] = (byte) host.charAt(i);
|
||||
ByteUtils.writeUint16(port, connectRequest, connectRequest.length - 2);
|
||||
writeUint16(port, connectRequest, connectRequest.length - 2);
|
||||
out.write(connectRequest);
|
||||
out.flush();
|
||||
}
|
||||
|
||||
private void receiveConnectResponse(InputStream in) throws IOException {
|
||||
byte[] connectResponse = new byte[4];
|
||||
IoUtils.read(in, connectResponse);
|
||||
read(in, connectResponse);
|
||||
int version = connectResponse[0] & 0xFF;
|
||||
int reply = connectResponse[1] & 0xFF;
|
||||
int addressType = connectResponse[3] & 0xFF;
|
||||
@@ -126,9 +129,9 @@ class SocksSocket extends Socket {
|
||||
throw new IOException("Connection failed: " + ERRORS[reply]);
|
||||
else throw new IOException("Connection failed: " + reply);
|
||||
}
|
||||
if (addressType == 1) IoUtils.read(in, new byte[4]); // IPv4
|
||||
else if (addressType == 4) IoUtils.read(in, new byte[16]); // IPv6
|
||||
if (addressType == 1) read(in, new byte[4]); // IPv4
|
||||
else if (addressType == 4) read(in, new byte[16]); // IPv6
|
||||
else throw new IOException("Unsupported address type: " + addressType);
|
||||
IoUtils.read(in, new byte[2]); // Port number
|
||||
read(in, new byte[2]); // Port number
|
||||
}
|
||||
}
|
||||
|
||||
@@ -45,7 +45,7 @@ class SocksSocketFactory extends SocketFactory {
|
||||
|
||||
@Override
|
||||
public Socket createSocket(InetAddress address, int port,
|
||||
InetAddress localAddress, int localPort) throws IOException {
|
||||
InetAddress localAddress, int localPort) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -39,6 +39,7 @@ import javax.annotation.concurrent.ThreadSafe;
|
||||
import static java.util.concurrent.TimeUnit.MILLISECONDS;
|
||||
import static java.util.logging.Level.INFO;
|
||||
import static java.util.logging.Level.WARNING;
|
||||
import static java.util.logging.Logger.getLogger;
|
||||
import static org.briarproject.bramble.api.lifecycle.LifecycleManager.LifecycleState.STOPPING;
|
||||
import static org.briarproject.bramble.api.record.Record.MAX_RECORD_PAYLOAD_BYTES;
|
||||
import static org.briarproject.bramble.api.sync.SyncConstants.MAX_MESSAGE_IDS;
|
||||
@@ -55,7 +56,7 @@ import static org.briarproject.bramble.util.LogUtils.logException;
|
||||
class DuplexOutgoingSession implements SyncSession, EventListener {
|
||||
|
||||
private static final Logger LOG =
|
||||
Logger.getLogger(DuplexOutgoingSession.class.getName());
|
||||
getLogger(DuplexOutgoingSession.class.getName());
|
||||
|
||||
private static final ThrowingRunnable<IOException> CLOSE = () -> {
|
||||
};
|
||||
|
||||
@@ -6,8 +6,6 @@ import org.briarproject.bramble.api.sync.ClientId;
|
||||
import org.briarproject.bramble.api.sync.Group;
|
||||
import org.briarproject.bramble.api.sync.GroupFactory;
|
||||
import org.briarproject.bramble.api.sync.GroupId;
|
||||
import org.briarproject.bramble.util.ByteUtils;
|
||||
import org.briarproject.bramble.util.StringUtils;
|
||||
|
||||
import javax.annotation.concurrent.Immutable;
|
||||
import javax.inject.Inject;
|
||||
@@ -15,6 +13,8 @@ import javax.inject.Inject;
|
||||
import static org.briarproject.bramble.api.sync.Group.FORMAT_VERSION;
|
||||
import static org.briarproject.bramble.api.sync.GroupId.LABEL;
|
||||
import static org.briarproject.bramble.util.ByteUtils.INT_32_BYTES;
|
||||
import static org.briarproject.bramble.util.ByteUtils.writeUint32;
|
||||
import static org.briarproject.bramble.util.StringUtils.toUtf8;
|
||||
|
||||
@Immutable
|
||||
@NotNullByDefault
|
||||
@@ -33,10 +33,9 @@ class GroupFactoryImpl implements GroupFactory {
|
||||
@Override
|
||||
public Group createGroup(ClientId c, int majorVersion, byte[] descriptor) {
|
||||
byte[] majorVersionBytes = new byte[INT_32_BYTES];
|
||||
ByteUtils.writeUint32(majorVersion, majorVersionBytes, 0);
|
||||
writeUint32(majorVersion, majorVersionBytes, 0);
|
||||
byte[] hash = crypto.hash(LABEL, FORMAT_VERSION_BYTES,
|
||||
StringUtils.toUtf8(c.getString()), majorVersionBytes,
|
||||
descriptor);
|
||||
toUtf8(c.getString()), majorVersionBytes, descriptor);
|
||||
return new Group(new GroupId(hash), c, majorVersion, descriptor);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -26,6 +26,7 @@ import java.util.logging.Logger;
|
||||
import javax.annotation.concurrent.ThreadSafe;
|
||||
|
||||
import static java.util.logging.Level.WARNING;
|
||||
import static java.util.logging.Logger.getLogger;
|
||||
import static org.briarproject.bramble.api.lifecycle.LifecycleManager.LifecycleState.STOPPING;
|
||||
import static org.briarproject.bramble.util.LogUtils.logException;
|
||||
|
||||
@@ -37,7 +38,7 @@ import static org.briarproject.bramble.util.LogUtils.logException;
|
||||
class IncomingSession implements SyncSession, EventListener {
|
||||
|
||||
private static final Logger LOG =
|
||||
Logger.getLogger(IncomingSession.class.getName());
|
||||
getLogger(IncomingSession.class.getName());
|
||||
|
||||
private final DatabaseComponent db;
|
||||
private final Executor dbExecutor;
|
||||
|
||||
@@ -7,11 +7,11 @@ import org.briarproject.bramble.api.sync.GroupId;
|
||||
import org.briarproject.bramble.api.sync.Message;
|
||||
import org.briarproject.bramble.api.sync.MessageFactory;
|
||||
import org.briarproject.bramble.api.sync.MessageId;
|
||||
import org.briarproject.bramble.util.ByteUtils;
|
||||
|
||||
import javax.annotation.concurrent.Immutable;
|
||||
import javax.inject.Inject;
|
||||
|
||||
import static java.lang.System.arraycopy;
|
||||
import static org.briarproject.bramble.api.sync.Message.FORMAT_VERSION;
|
||||
import static org.briarproject.bramble.api.sync.MessageId.BLOCK_LABEL;
|
||||
import static org.briarproject.bramble.api.sync.MessageId.ID_LABEL;
|
||||
@@ -19,6 +19,8 @@ import static org.briarproject.bramble.api.sync.SyncConstants.MAX_MESSAGE_BODY_L
|
||||
import static org.briarproject.bramble.api.sync.SyncConstants.MAX_MESSAGE_LENGTH;
|
||||
import static org.briarproject.bramble.api.sync.SyncConstants.MESSAGE_HEADER_LENGTH;
|
||||
import static org.briarproject.bramble.util.ByteUtils.INT_64_BYTES;
|
||||
import static org.briarproject.bramble.util.ByteUtils.readUint64;
|
||||
import static org.briarproject.bramble.util.ByteUtils.writeUint64;
|
||||
|
||||
@Immutable
|
||||
@NotNullByDefault
|
||||
@@ -47,7 +49,7 @@ class MessageFactoryImpl implements MessageFactory {
|
||||
// There's only one block, so the root hash is the hash of the block
|
||||
byte[] rootHash = crypto.hash(BLOCK_LABEL, FORMAT_VERSION_BYTES, body);
|
||||
byte[] timeBytes = new byte[INT_64_BYTES];
|
||||
ByteUtils.writeUint64(timestamp, timeBytes, 0);
|
||||
writeUint64(timestamp, timeBytes, 0);
|
||||
byte[] idHash = crypto.hash(ID_LABEL, FORMAT_VERSION_BYTES,
|
||||
g.getBytes(), timeBytes, rootHash);
|
||||
return new MessageId(idHash);
|
||||
@@ -60,11 +62,11 @@ class MessageFactoryImpl implements MessageFactory {
|
||||
if (raw.length > MAX_MESSAGE_LENGTH)
|
||||
throw new IllegalArgumentException();
|
||||
byte[] groupId = new byte[UniqueId.LENGTH];
|
||||
System.arraycopy(raw, 0, groupId, 0, UniqueId.LENGTH);
|
||||
arraycopy(raw, 0, groupId, 0, UniqueId.LENGTH);
|
||||
GroupId g = new GroupId(groupId);
|
||||
long timestamp = ByteUtils.readUint64(raw, UniqueId.LENGTH);
|
||||
long timestamp = readUint64(raw, UniqueId.LENGTH);
|
||||
byte[] body = new byte[raw.length - MESSAGE_HEADER_LENGTH];
|
||||
System.arraycopy(raw, MESSAGE_HEADER_LENGTH, body, 0, body.length);
|
||||
arraycopy(raw, MESSAGE_HEADER_LENGTH, body, 0, body.length);
|
||||
MessageId id = getMessageId(g, timestamp, body);
|
||||
return new Message(id, g, timestamp, body);
|
||||
}
|
||||
@@ -73,9 +75,9 @@ class MessageFactoryImpl implements MessageFactory {
|
||||
public byte[] getRawMessage(Message m) {
|
||||
byte[] body = m.getBody();
|
||||
byte[] raw = new byte[MESSAGE_HEADER_LENGTH + body.length];
|
||||
System.arraycopy(m.getGroupId().getBytes(), 0, raw, 0, UniqueId.LENGTH);
|
||||
ByteUtils.writeUint64(m.getTimestamp(), raw, UniqueId.LENGTH);
|
||||
System.arraycopy(body, 0, raw, MESSAGE_HEADER_LENGTH, body.length);
|
||||
arraycopy(m.getGroupId().getBytes(), 0, raw, 0, UniqueId.LENGTH);
|
||||
writeUint64(m.getTimestamp(), raw, UniqueId.LENGTH);
|
||||
arraycopy(body, 0, raw, MESSAGE_HEADER_LENGTH, body.length);
|
||||
return raw;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -29,6 +29,7 @@ import javax.annotation.concurrent.ThreadSafe;
|
||||
|
||||
import static java.util.logging.Level.INFO;
|
||||
import static java.util.logging.Level.WARNING;
|
||||
import static java.util.logging.Logger.getLogger;
|
||||
import static org.briarproject.bramble.api.lifecycle.LifecycleManager.LifecycleState.STOPPING;
|
||||
import static org.briarproject.bramble.api.record.Record.MAX_RECORD_PAYLOAD_BYTES;
|
||||
import static org.briarproject.bramble.api.sync.SyncConstants.MAX_MESSAGE_IDS;
|
||||
@@ -44,7 +45,7 @@ import static org.briarproject.bramble.util.LogUtils.logException;
|
||||
class SimplexOutgoingSession implements SyncSession, EventListener {
|
||||
|
||||
private static final Logger LOG =
|
||||
Logger.getLogger(SimplexOutgoingSession.class.getName());
|
||||
getLogger(SimplexOutgoingSession.class.getName());
|
||||
|
||||
private static final ThrowingRunnable<IOException> CLOSE = () -> {
|
||||
};
|
||||
|
||||
@@ -12,7 +12,6 @@ import org.briarproject.bramble.api.sync.MessageId;
|
||||
import org.briarproject.bramble.api.sync.Offer;
|
||||
import org.briarproject.bramble.api.sync.Request;
|
||||
import org.briarproject.bramble.api.sync.SyncRecordReader;
|
||||
import org.briarproject.bramble.util.ByteUtils;
|
||||
|
||||
import java.io.EOFException;
|
||||
import java.io.IOException;
|
||||
@@ -22,12 +21,14 @@ import java.util.List;
|
||||
import javax.annotation.Nullable;
|
||||
import javax.annotation.concurrent.NotThreadSafe;
|
||||
|
||||
import static java.lang.System.arraycopy;
|
||||
import static org.briarproject.bramble.api.sync.RecordTypes.ACK;
|
||||
import static org.briarproject.bramble.api.sync.RecordTypes.MESSAGE;
|
||||
import static org.briarproject.bramble.api.sync.RecordTypes.OFFER;
|
||||
import static org.briarproject.bramble.api.sync.RecordTypes.REQUEST;
|
||||
import static org.briarproject.bramble.api.sync.SyncConstants.MESSAGE_HEADER_LENGTH;
|
||||
import static org.briarproject.bramble.api.sync.SyncConstants.PROTOCOL_VERSION;
|
||||
import static org.briarproject.bramble.util.ByteUtils.readUint64;
|
||||
|
||||
@NotThreadSafe
|
||||
@NotNullByDefault
|
||||
@@ -107,7 +108,7 @@ class SyncRecordReaderImpl implements SyncRecordReader {
|
||||
List<MessageId> ids = new ArrayList<>(payload.length / UniqueId.LENGTH);
|
||||
for (int off = 0; off < payload.length; off += UniqueId.LENGTH) {
|
||||
byte[] id = new byte[UniqueId.LENGTH];
|
||||
System.arraycopy(payload, off, id, 0, UniqueId.LENGTH);
|
||||
arraycopy(payload, off, id, 0, UniqueId.LENGTH);
|
||||
ids.add(new MessageId(id));
|
||||
}
|
||||
nextRecord = null;
|
||||
@@ -127,7 +128,7 @@ class SyncRecordReaderImpl implements SyncRecordReader {
|
||||
if (payload.length <= MESSAGE_HEADER_LENGTH)
|
||||
throw new FormatException();
|
||||
// Validate timestamp
|
||||
long timestamp = ByteUtils.readUint64(payload, UniqueId.LENGTH);
|
||||
long timestamp = readUint64(payload, UniqueId.LENGTH);
|
||||
if (timestamp < 0) throw new FormatException();
|
||||
nextRecord = null;
|
||||
return messageFactory.createMessage(payload);
|
||||
|
||||
@@ -40,6 +40,7 @@ import javax.inject.Inject;
|
||||
|
||||
import static java.util.logging.Level.INFO;
|
||||
import static java.util.logging.Level.WARNING;
|
||||
import static java.util.logging.Logger.getLogger;
|
||||
import static org.briarproject.bramble.api.sync.validation.MessageState.DELIVERED;
|
||||
import static org.briarproject.bramble.api.sync.validation.MessageState.INVALID;
|
||||
import static org.briarproject.bramble.api.sync.validation.MessageState.PENDING;
|
||||
@@ -51,7 +52,7 @@ class ValidationManagerImpl implements ValidationManager, Service,
|
||||
EventListener {
|
||||
|
||||
private static final Logger LOG =
|
||||
Logger.getLogger(ValidationManagerImpl.class.getName());
|
||||
getLogger(ValidationManagerImpl.class.getName());
|
||||
|
||||
private final DatabaseComponent db;
|
||||
private final Executor dbExecutor, validationExecutor;
|
||||
|
||||
@@ -14,6 +14,7 @@ import javax.annotation.concurrent.Immutable;
|
||||
|
||||
import static java.util.logging.Level.WARNING;
|
||||
import static java.util.logging.Logger.getLogger;
|
||||
import static org.briarproject.bramble.util.IoUtils.tryToClose;
|
||||
import static org.briarproject.bramble.util.LogUtils.logException;
|
||||
|
||||
@Immutable
|
||||
@@ -43,15 +44,16 @@ class UnixSecureRandomProvider extends AbstractSecureRandomProvider {
|
||||
}
|
||||
|
||||
protected void writeSeed() {
|
||||
DataOutputStream out = null;
|
||||
try {
|
||||
DataOutputStream out = new DataOutputStream(
|
||||
new FileOutputStream(outputDevice));
|
||||
out = new DataOutputStream(new FileOutputStream(outputDevice));
|
||||
writeToEntropyPool(out);
|
||||
out.flush();
|
||||
out.close();
|
||||
} catch (IOException e) {
|
||||
// On some devices /dev/urandom isn't writable - this isn't fatal
|
||||
logException(LOG, WARNING, e);
|
||||
} finally {
|
||||
tryToClose(out, LOG, WARNING);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -11,6 +11,7 @@ import java.util.logging.Logger;
|
||||
|
||||
import static java.util.logging.Level.WARNING;
|
||||
import static java.util.logging.Logger.getLogger;
|
||||
import static org.briarproject.bramble.util.IoUtils.tryToClose;
|
||||
import static org.briarproject.bramble.util.LogUtils.logException;
|
||||
|
||||
public class UnixSecureRandomSpi extends SecureRandomSpi {
|
||||
@@ -34,15 +35,16 @@ public class UnixSecureRandomSpi extends SecureRandomSpi {
|
||||
|
||||
@Override
|
||||
protected void engineSetSeed(byte[] seed) {
|
||||
DataOutputStream out = null;
|
||||
try {
|
||||
DataOutputStream out = new DataOutputStream(
|
||||
new FileOutputStream(outputDevice));
|
||||
out = new DataOutputStream(new FileOutputStream(outputDevice));
|
||||
out.write(seed);
|
||||
out.flush();
|
||||
out.close();
|
||||
} catch (IOException e) {
|
||||
// On some devices /dev/urandom isn't writable - this isn't fatal
|
||||
logException(LOG, WARNING, e);
|
||||
} finally {
|
||||
tryToClose(out, LOG, WARNING);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -34,13 +34,13 @@ import javax.annotation.concurrent.ThreadSafe;
|
||||
import javax.inject.Inject;
|
||||
|
||||
import static java.util.logging.Level.INFO;
|
||||
import static java.util.logging.Logger.getLogger;
|
||||
|
||||
@ThreadSafe
|
||||
@NotNullByDefault
|
||||
class KeyManagerImpl implements KeyManager, Service, EventListener {
|
||||
|
||||
private static final Logger LOG =
|
||||
Logger.getLogger(KeyManagerImpl.class.getName());
|
||||
private static final Logger LOG = getLogger(KeyManagerImpl.class.getName());
|
||||
|
||||
private final DatabaseComponent db;
|
||||
private final Executor dbExecutor;
|
||||
|
||||
@@ -3,11 +3,12 @@ package org.briarproject.bramble.transport;
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import javax.annotation.concurrent.NotThreadSafe;
|
||||
|
||||
import static java.util.Collections.emptyList;
|
||||
import static java.util.Collections.singletonList;
|
||||
import static org.briarproject.bramble.util.ByteUtils.MAX_32_BIT_UNSIGNED;
|
||||
|
||||
@NotThreadSafe
|
||||
@@ -64,8 +65,8 @@ class ReorderingWindow {
|
||||
while (seen[slide]) slide++;
|
||||
// If the window doesn't need to slide, return
|
||||
if (slide == 0) {
|
||||
List<Long> added = Collections.emptyList();
|
||||
List<Long> removed = Collections.singletonList(index);
|
||||
List<Long> added = emptyList();
|
||||
List<Long> removed = singletonList(index);
|
||||
return new Change(added, removed);
|
||||
}
|
||||
// Record the elements that will be added and removed
|
||||
|
||||
@@ -8,6 +8,7 @@ import java.io.InputStream;
|
||||
|
||||
import javax.annotation.concurrent.NotThreadSafe;
|
||||
|
||||
import static java.lang.System.arraycopy;
|
||||
import static org.briarproject.bramble.api.transport.TransportConstants.MAX_PAYLOAD_LENGTH;
|
||||
|
||||
/**
|
||||
@@ -51,7 +52,7 @@ class StreamReaderImpl extends InputStream {
|
||||
readFrame();
|
||||
}
|
||||
len = Math.min(len, length);
|
||||
System.arraycopy(payload, offset, b, off, len);
|
||||
arraycopy(payload, offset, b, off, len);
|
||||
offset += len;
|
||||
length -= len;
|
||||
return len;
|
||||
|
||||
@@ -9,6 +9,7 @@ import java.io.OutputStream;
|
||||
|
||||
import javax.annotation.concurrent.NotThreadSafe;
|
||||
|
||||
import static java.lang.System.arraycopy;
|
||||
import static org.briarproject.bramble.api.transport.TransportConstants.MAX_PAYLOAD_LENGTH;
|
||||
|
||||
/**
|
||||
@@ -70,14 +71,14 @@ class StreamWriterImpl extends OutputStream implements StreamWriter {
|
||||
public void write(byte[] b, int off, int len) throws IOException {
|
||||
int available = payload.length - length;
|
||||
while (available <= len) {
|
||||
System.arraycopy(b, off, payload, length, available);
|
||||
arraycopy(b, off, payload, length, available);
|
||||
length += available;
|
||||
writeFrame(false);
|
||||
off += available;
|
||||
len -= available;
|
||||
available = payload.length - length;
|
||||
}
|
||||
System.arraycopy(b, off, payload, length, len);
|
||||
arraycopy(b, off, payload, length, len);
|
||||
length += len;
|
||||
}
|
||||
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user