Search in sources :

Example 11 with NotFoundException

use of org.whispersystems.signalservice.api.push.exceptions.NotFoundException in project libsignal-service-java by signalapp.

the class PushServiceSocket method getPreKey.

public PreKeyBundle getPreKey(SignalServiceAddress destination, int deviceId) throws IOException {
    try {
        String path = String.format(PREKEY_DEVICE_PATH, destination.getNumber(), String.valueOf(deviceId));
        if (destination.getRelay().isPresent()) {
            path = path + "?relay=" + destination.getRelay().get();
        }
        String responseText = makeServiceRequest(path, "GET", null);
        PreKeyResponse response = JsonUtil.fromJson(responseText, PreKeyResponse.class);
        if (response.getDevices() == null || response.getDevices().size() < 1)
            throw new IOException("Empty prekey list");
        PreKeyResponseItem device = response.getDevices().get(0);
        ECPublicKey preKey = null;
        ECPublicKey signedPreKey = null;
        byte[] signedPreKeySignature = null;
        int preKeyId = -1;
        int signedPreKeyId = -1;
        if (device.getPreKey() != null) {
            preKeyId = device.getPreKey().getKeyId();
            preKey = device.getPreKey().getPublicKey();
        }
        if (device.getSignedPreKey() != null) {
            signedPreKeyId = device.getSignedPreKey().getKeyId();
            signedPreKey = device.getSignedPreKey().getPublicKey();
            signedPreKeySignature = device.getSignedPreKey().getSignature();
        }
        return new PreKeyBundle(device.getRegistrationId(), device.getDeviceId(), preKeyId, preKey, signedPreKeyId, signedPreKey, signedPreKeySignature, response.getIdentityKey());
    } catch (NotFoundException nfe) {
        throw new UnregisteredUserException(destination.getNumber(), nfe);
    }
}
Also used : PreKeyBundle(org.whispersystems.libsignal.state.PreKeyBundle) UnregisteredUserException(org.whispersystems.signalservice.api.push.exceptions.UnregisteredUserException) ECPublicKey(org.whispersystems.libsignal.ecc.ECPublicKey) NotFoundException(org.whispersystems.signalservice.api.push.exceptions.NotFoundException) IOException(java.io.IOException)

Example 12 with NotFoundException

use of org.whispersystems.signalservice.api.push.exceptions.NotFoundException in project Signal-Android by signalapp.

the class DeviceActivity method onLink.

@SuppressLint("StaticFieldLeak")
@Override
public void onLink(final Uri uri) {
    new ProgressDialogAsyncTask<Void, Void, Integer>(this, R.string.DeviceProvisioningActivity_content_progress_title, R.string.DeviceProvisioningActivity_content_progress_content) {

        private static final int SUCCESS = 0;

        private static final int NO_DEVICE = 1;

        private static final int NETWORK_ERROR = 2;

        private static final int KEY_ERROR = 3;

        private static final int LIMIT_EXCEEDED = 4;

        private static final int BAD_CODE = 5;

        @Override
        protected Integer doInBackground(Void... params) {
            boolean isMultiDevice = TextSecurePreferences.isMultiDevice(DeviceActivity.this);
            try {
                Context context = DeviceActivity.this;
                SignalServiceAccountManager accountManager = AccountManagerFactory.createManager(context);
                String verificationCode = accountManager.getNewDeviceVerificationCode();
                String ephemeralId = uri.getQueryParameter("uuid");
                String publicKeyEncoded = uri.getQueryParameter("pub_key");
                if (TextUtils.isEmpty(ephemeralId) || TextUtils.isEmpty(publicKeyEncoded)) {
                    Log.w(TAG, "UUID or Key is empty!");
                    return BAD_CODE;
                }
                ECPublicKey publicKey = Curve.decodePoint(Base64.decode(publicKeyEncoded), 0);
                IdentityKeyPair identityKeyPair = IdentityKeyUtil.getIdentityKeyPair(context);
                Optional<byte[]> profileKey = Optional.of(ProfileKeyUtil.getProfileKey(getContext()));
                TextSecurePreferences.setMultiDevice(DeviceActivity.this, true);
                accountManager.addDevice(ephemeralId, publicKey, identityKeyPair, profileKey, verificationCode);
                return SUCCESS;
            } catch (NotFoundException e) {
                Log.w(TAG, e);
                TextSecurePreferences.setMultiDevice(DeviceActivity.this, isMultiDevice);
                return NO_DEVICE;
            } catch (DeviceLimitExceededException e) {
                Log.w(TAG, e);
                TextSecurePreferences.setMultiDevice(DeviceActivity.this, isMultiDevice);
                return LIMIT_EXCEEDED;
            } catch (IOException e) {
                Log.w(TAG, e);
                TextSecurePreferences.setMultiDevice(DeviceActivity.this, isMultiDevice);
                return NETWORK_ERROR;
            } catch (InvalidKeyException e) {
                Log.w(TAG, e);
                TextSecurePreferences.setMultiDevice(DeviceActivity.this, isMultiDevice);
                return KEY_ERROR;
            }
        }

        @Override
        protected void onPostExecute(Integer result) {
            super.onPostExecute(result);
            Context context = DeviceActivity.this;
            switch(result) {
                case SUCCESS:
                    Toast.makeText(context, R.string.DeviceProvisioningActivity_content_progress_success, Toast.LENGTH_SHORT).show();
                    finish();
                    return;
                case NO_DEVICE:
                    Toast.makeText(context, R.string.DeviceProvisioningActivity_content_progress_no_device, Toast.LENGTH_LONG).show();
                    break;
                case NETWORK_ERROR:
                    Toast.makeText(context, R.string.DeviceProvisioningActivity_content_progress_network_error, Toast.LENGTH_LONG).show();
                    break;
                case KEY_ERROR:
                    Toast.makeText(context, R.string.DeviceProvisioningActivity_content_progress_key_error, Toast.LENGTH_LONG).show();
                    break;
                case LIMIT_EXCEEDED:
                    Toast.makeText(context, R.string.DeviceProvisioningActivity_sorry_you_have_too_many_devices_linked_already, Toast.LENGTH_LONG).show();
                    break;
                case BAD_CODE:
                    Toast.makeText(context, R.string.DeviceActivity_sorry_this_is_not_a_valid_device_link_qr_code, Toast.LENGTH_LONG).show();
                    break;
            }
            getSupportFragmentManager().popBackStackImmediate();
        }
    }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
Also used : Context(android.content.Context) Optional(org.whispersystems.libsignal.util.guava.Optional) SignalServiceAccountManager(org.whispersystems.signalservice.api.SignalServiceAccountManager) NotFoundException(org.whispersystems.signalservice.api.push.exceptions.NotFoundException) IOException(java.io.IOException) InvalidKeyException(org.whispersystems.libsignal.InvalidKeyException) SuppressLint(android.annotation.SuppressLint) DeviceLimitExceededException(org.whispersystems.signalservice.internal.push.DeviceLimitExceededException) ECPublicKey(org.whispersystems.libsignal.ecc.ECPublicKey) IdentityKeyPair(org.whispersystems.libsignal.IdentityKeyPair) SuppressLint(android.annotation.SuppressLint)

Example 13 with NotFoundException

use of org.whispersystems.signalservice.api.push.exceptions.NotFoundException in project Signal-Android by WhisperSystems.

the class PushServiceSocket method makeStorageRequestResponse.

private Response makeStorageRequestResponse(String authorization, String path, String method, RequestBody body, ResponseCodeHandler responseCodeHandler) throws PushNetworkException, NonSuccessfulResponseCodeException {
    ConnectionHolder connectionHolder = getRandom(storageClients, random);
    OkHttpClient okHttpClient = connectionHolder.getClient().newBuilder().connectTimeout(soTimeoutMillis, TimeUnit.MILLISECONDS).readTimeout(soTimeoutMillis, TimeUnit.MILLISECONDS).build();
    // Log.d(TAG, "Opening URL: " + connectionHolder.getUrl());
    Request.Builder request = new Request.Builder().url(connectionHolder.getUrl() + path);
    request.method(method, body);
    if (connectionHolder.getHostHeader().isPresent()) {
        request.addHeader("Host", connectionHolder.getHostHeader().get());
    }
    if (authorization != null) {
        request.addHeader("Authorization", authorization);
    }
    Call call = okHttpClient.newCall(request.build());
    synchronized (connections) {
        connections.add(call);
    }
    Response response;
    try {
        response = call.execute();
        if (response.isSuccessful() && response.code() != 204) {
            return response;
        }
    } catch (IOException e) {
        throw new PushNetworkException(e);
    } finally {
        synchronized (connections) {
            connections.remove(call);
        }
    }
    ResponseBody responseBody = response.body();
    try {
        responseCodeHandler.handle(response.code(), responseBody);
        switch(response.code()) {
            case 204:
                throw new NoContentException("No content!");
            case 401:
            case 403:
                throw new AuthorizationFailedException(response.code(), "Authorization failed!");
            case 404:
                throw new NotFoundException("Not found");
            case 409:
                if (responseBody != null) {
                    throw new ContactManifestMismatchException(readBodyBytes(responseBody));
                } else {
                    throw new ConflictException();
                }
            case 429:
                throw new RateLimitException("Rate limit exceeded: " + response.code());
            case 499:
                throw new DeprecatedVersionException();
        }
        throw new NonSuccessfulResponseCodeException(response.code(), "Response: " + response);
    } catch (NonSuccessfulResponseCodeException | PushNetworkException e) {
        if (responseBody != null) {
            responseBody.close();
        }
        throw e;
    }
}
Also used : Call(okhttp3.Call) OkHttpClient(okhttp3.OkHttpClient) PushNetworkException(org.whispersystems.signalservice.api.push.exceptions.PushNetworkException) RateLimitException(org.whispersystems.signalservice.api.push.exceptions.RateLimitException) DeprecatedVersionException(org.whispersystems.signalservice.api.push.exceptions.DeprecatedVersionException) ConflictException(org.whispersystems.signalservice.api.push.exceptions.ConflictException) ReceiptCredentialRequest(org.signal.zkgroup.receipts.ReceiptCredentialRequest) Request(okhttp3.Request) ChangePhoneNumberRequest(org.whispersystems.signalservice.api.account.ChangePhoneNumberRequest) DiscoveryRequest(org.whispersystems.signalservice.internal.contacts.entities.DiscoveryRequest) KeyBackupRequest(org.whispersystems.signalservice.internal.contacts.entities.KeyBackupRequest) ProfileKeyCredentialRequest(org.signal.zkgroup.profiles.ProfileKeyCredentialRequest) GroupNotFoundException(org.whispersystems.signalservice.internal.push.exceptions.GroupNotFoundException) NotFoundException(org.whispersystems.signalservice.api.push.exceptions.NotFoundException) NonSuccessfulResponseCodeException(org.whispersystems.signalservice.api.push.exceptions.NonSuccessfulResponseCodeException) IOException(java.io.IOException) NoContentException(org.whispersystems.signalservice.api.push.exceptions.NoContentException) ResponseBody(okhttp3.ResponseBody) CallingResponse(org.whispersystems.signalservice.api.messages.calls.CallingResponse) Response(okhttp3.Response) KeyBackupResponse(org.whispersystems.signalservice.internal.contacts.entities.KeyBackupResponse) ReceiptCredentialResponse(org.signal.zkgroup.receipts.ReceiptCredentialResponse) CredentialResponse(org.whispersystems.signalservice.api.groupsv2.CredentialResponse) StorageAuthResponse(org.whispersystems.signalservice.api.storage.StorageAuthResponse) VerifyDeviceResponse(org.whispersystems.signalservice.api.messages.multidevice.VerifyDeviceResponse) DiscoveryResponse(org.whispersystems.signalservice.internal.contacts.entities.DiscoveryResponse) ProfileKeyCredentialResponse(org.signal.zkgroup.profiles.ProfileKeyCredentialResponse) TokenResponse(org.whispersystems.signalservice.internal.contacts.entities.TokenResponse) AuthorizationFailedException(org.whispersystems.signalservice.api.push.exceptions.AuthorizationFailedException) ContactManifestMismatchException(org.whispersystems.signalservice.api.push.exceptions.ContactManifestMismatchException)

Example 14 with NotFoundException

use of org.whispersystems.signalservice.api.push.exceptions.NotFoundException in project Signal-Android by WhisperSystems.

the class PushServiceSocket method getPreKey.

public PreKeyBundle getPreKey(SignalServiceAddress destination, int deviceId) throws IOException {
    try {
        String path = String.format(PREKEY_DEVICE_PATH, destination.getIdentifier(), String.valueOf(deviceId));
        String responseText = makeServiceRequest(path, "GET", null);
        PreKeyResponse response = JsonUtil.fromJson(responseText, PreKeyResponse.class);
        if (response.getDevices() == null || response.getDevices().size() < 1)
            throw new IOException("Empty prekey list");
        PreKeyResponseItem device = response.getDevices().get(0);
        ECPublicKey preKey = null;
        ECPublicKey signedPreKey = null;
        byte[] signedPreKeySignature = null;
        int preKeyId = -1;
        int signedPreKeyId = -1;
        if (device.getPreKey() != null) {
            preKeyId = device.getPreKey().getKeyId();
            preKey = device.getPreKey().getPublicKey();
        }
        if (device.getSignedPreKey() != null) {
            signedPreKeyId = device.getSignedPreKey().getKeyId();
            signedPreKey = device.getSignedPreKey().getPublicKey();
            signedPreKeySignature = device.getSignedPreKey().getSignature();
        }
        return new PreKeyBundle(device.getRegistrationId(), device.getDeviceId(), preKeyId, preKey, signedPreKeyId, signedPreKey, signedPreKeySignature, response.getIdentityKey());
    } catch (NotFoundException nfe) {
        throw new UnregisteredUserException(destination.getIdentifier(), nfe);
    }
}
Also used : PreKeyBundle(org.whispersystems.libsignal.state.PreKeyBundle) UnregisteredUserException(org.whispersystems.signalservice.api.push.exceptions.UnregisteredUserException) ECPublicKey(org.whispersystems.libsignal.ecc.ECPublicKey) GroupNotFoundException(org.whispersystems.signalservice.internal.push.exceptions.GroupNotFoundException) NotFoundException(org.whispersystems.signalservice.api.push.exceptions.NotFoundException) GroupsV2AuthorizationString(org.whispersystems.signalservice.api.groupsv2.GroupsV2AuthorizationString) IOException(java.io.IOException)

Example 15 with NotFoundException

use of org.whispersystems.signalservice.api.push.exceptions.NotFoundException in project Signal-Android by WhisperSystems.

the class PushServiceSocket method getPreKeys.

public List<PreKeyBundle> getPreKeys(SignalServiceAddress destination, Optional<UnidentifiedAccess> unidentifiedAccess, int deviceIdInteger) throws IOException {
    try {
        String deviceId = String.valueOf(deviceIdInteger);
        if (deviceId.equals("1"))
            deviceId = "*";
        String path = String.format(PREKEY_DEVICE_PATH, destination.getIdentifier(), deviceId);
        Log.d(TAG, "Fetching prekeys for " + destination.getIdentifier() + "." + deviceId + ", i.e. GET " + path);
        String responseText = makeServiceRequest(path, "GET", null, NO_HEADERS, unidentifiedAccess);
        PreKeyResponse response = JsonUtil.fromJson(responseText, PreKeyResponse.class);
        List<PreKeyBundle> bundles = new LinkedList<>();
        for (PreKeyResponseItem device : response.getDevices()) {
            ECPublicKey preKey = null;
            ECPublicKey signedPreKey = null;
            byte[] signedPreKeySignature = null;
            int preKeyId = -1;
            int signedPreKeyId = -1;
            if (device.getSignedPreKey() != null) {
                signedPreKey = device.getSignedPreKey().getPublicKey();
                signedPreKeyId = device.getSignedPreKey().getKeyId();
                signedPreKeySignature = device.getSignedPreKey().getSignature();
            }
            if (device.getPreKey() != null) {
                preKeyId = device.getPreKey().getKeyId();
                preKey = device.getPreKey().getPublicKey();
            }
            bundles.add(new PreKeyBundle(device.getRegistrationId(), device.getDeviceId(), preKeyId, preKey, signedPreKeyId, signedPreKey, signedPreKeySignature, response.getIdentityKey()));
        }
        return bundles;
    } catch (NotFoundException nfe) {
        throw new UnregisteredUserException(destination.getIdentifier(), nfe);
    }
}
Also used : PreKeyBundle(org.whispersystems.libsignal.state.PreKeyBundle) UnregisteredUserException(org.whispersystems.signalservice.api.push.exceptions.UnregisteredUserException) ECPublicKey(org.whispersystems.libsignal.ecc.ECPublicKey) GroupNotFoundException(org.whispersystems.signalservice.internal.push.exceptions.GroupNotFoundException) NotFoundException(org.whispersystems.signalservice.api.push.exceptions.NotFoundException) GroupsV2AuthorizationString(org.whispersystems.signalservice.api.groupsv2.GroupsV2AuthorizationString) LinkedList(java.util.LinkedList)

Aggregations

NotFoundException (org.whispersystems.signalservice.api.push.exceptions.NotFoundException)21 IOException (java.io.IOException)10 UnregisteredUserException (org.whispersystems.signalservice.api.push.exceptions.UnregisteredUserException)8 GroupNotFoundException (org.whispersystems.signalservice.internal.push.exceptions.GroupNotFoundException)7 LinkedList (java.util.LinkedList)6 ECPublicKey (org.whispersystems.libsignal.ecc.ECPublicKey)6 GroupsV2AuthorizationString (org.whispersystems.signalservice.api.groupsv2.GroupsV2AuthorizationString)6 ByteString (com.google.protobuf.ByteString)5 InvalidKeyException (org.whispersystems.libsignal.InvalidKeyException)5 PreKeyBundle (org.whispersystems.libsignal.state.PreKeyBundle)5 NonSuccessfulResponseCodeException (org.whispersystems.signalservice.api.push.exceptions.NonSuccessfulResponseCodeException)5 GroupMismatchedDevicesException (org.whispersystems.signalservice.internal.push.exceptions.GroupMismatchedDevicesException)5 GroupStaleDevicesException (org.whispersystems.signalservice.internal.push.exceptions.GroupStaleDevicesException)5 InvalidUnidentifiedAccessHeaderException (org.whispersystems.signalservice.internal.push.exceptions.InvalidUnidentifiedAccessHeaderException)5 Optional (org.whispersystems.libsignal.util.guava.Optional)4 AuthorizationFailedException (org.whispersystems.signalservice.api.push.exceptions.AuthorizationFailedException)4 PushNetworkException (org.whispersystems.signalservice.api.push.exceptions.PushNetworkException)4 ServerRejectedException (org.whispersystems.signalservice.api.push.exceptions.ServerRejectedException)4 MismatchedDevicesException (org.whispersystems.signalservice.internal.push.exceptions.MismatchedDevicesException)4 StaleDevicesException (org.whispersystems.signalservice.internal.push.exceptions.StaleDevicesException)4