Search in sources :

Example 1 with SignatureVerification

use of com.owncloud.android.datamodel.SignatureVerification in project android by nextcloud.

the class NotificationJob method onRunJob.

@NonNull
@Override
protected Result onRunJob(Params params) {
    Context context = getContext();
    PersistableBundleCompat persistableBundleCompat = getParams().getExtras();
    String subject = persistableBundleCompat.getString(KEY_NOTIFICATION_SUBJECT, "");
    String signature = persistableBundleCompat.getString(KEY_NOTIFICATION_SIGNATURE, "");
    if (!TextUtils.isEmpty(subject) && !TextUtils.isEmpty(signature)) {
        try {
            byte[] base64DecodedSubject = Base64.decode(subject, Base64.DEFAULT);
            byte[] base64DecodedSignature = Base64.decode(signature, Base64.DEFAULT);
            PushUtils pushUtils = new PushUtils();
            PrivateKey privateKey = (PrivateKey) PushUtils.readKeyFromFile(false);
            try {
                SignatureVerification signatureVerification = pushUtils.verifySignature(context, base64DecodedSignature, base64DecodedSubject);
                if (signatureVerification.isSignatureValid()) {
                    Cipher cipher = Cipher.getInstance("RSA/None/PKCS1Padding");
                    cipher.init(Cipher.DECRYPT_MODE, privateKey);
                    byte[] decryptedSubject = cipher.doFinal(base64DecodedSubject);
                    Gson gson = new Gson();
                    DecryptedPushMessage decryptedPushMessage = gson.fromJson(new String(decryptedSubject), DecryptedPushMessage.class);
                    // We ignore Spreed messages for now
                    if (!decryptedPushMessage.getApp().equals("spreed")) {
                        sendNotification(decryptedPushMessage.getSubject(), signatureVerification.getAccount());
                    }
                }
            } catch (NoSuchAlgorithmException e1) {
                Log.d(TAG, "No proper algorithm to decrypt the message " + e1.getLocalizedMessage());
            } catch (NoSuchPaddingException e1) {
                Log.d(TAG, "No proper padding to decrypt the message " + e1.getLocalizedMessage());
            } catch (InvalidKeyException e1) {
                Log.d(TAG, "Invalid private key " + e1.getLocalizedMessage());
            }
        } catch (Exception exception) {
            Log.d(TAG, "Something went very wrong" + exception.getLocalizedMessage());
        }
    }
    return Result.SUCCESS;
}
Also used : Context(android.content.Context) PersistableBundleCompat(com.evernote.android.job.util.support.PersistableBundleCompat) PushUtils(com.owncloud.android.utils.PushUtils) PrivateKey(java.security.PrivateKey) Gson(com.google.gson.Gson) NoSuchPaddingException(javax.crypto.NoSuchPaddingException) SignatureVerification(com.owncloud.android.datamodel.SignatureVerification) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeyException(java.security.InvalidKeyException) NoSuchPaddingException(javax.crypto.NoSuchPaddingException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeyException(java.security.InvalidKeyException) DecryptedPushMessage(com.owncloud.android.datamodel.DecryptedPushMessage) Cipher(javax.crypto.Cipher) NonNull(android.support.annotation.NonNull)

Example 2 with SignatureVerification

use of com.owncloud.android.datamodel.SignatureVerification in project android by nextcloud.

the class PushUtils method verifySignature.

public SignatureVerification verifySignature(Context context, byte[] signatureBytes, byte[] subjectBytes) {
    Signature signature = null;
    PublicKey publicKey;
    SignatureVerification signatureVerification = new SignatureVerification();
    signatureVerification.setSignatureValid(false);
    Account[] accounts = AccountUtils.getAccounts(context);
    ArbitraryDataProvider arbitraryDataProvider = new ArbitraryDataProvider(context.getContentResolver());
    String arbitraryValue;
    Gson gson = new Gson();
    PushConfigurationState pushArbitraryData;
    try {
        signature = Signature.getInstance("SHA512withRSA");
        if (accounts.length > 0) {
            for (Account account : accounts) {
                if (!TextUtils.isEmpty(arbitraryValue = arbitraryDataProvider.getValue(account, KEY_PUSH))) {
                    pushArbitraryData = gson.fromJson(arbitraryValue, PushConfigurationState.class);
                    if (!pushArbitraryData.isShouldBeDeleted()) {
                        publicKey = (PublicKey) readKeyFromString(true, pushArbitraryData.getUserPublicKey());
                        signature.initVerify(publicKey);
                        signature.update(subjectBytes);
                        if (signature.verify(signatureBytes)) {
                            signatureVerification.setSignatureValid(true);
                            signatureVerification.setAccount(account);
                            return signatureVerification;
                        }
                    }
                }
            }
        }
    } catch (NoSuchAlgorithmException e) {
        Log.d(TAG, "No such algorithm");
    } catch (InvalidKeyException e) {
        Log.d(TAG, "Invalid key while trying to verify");
    } catch (SignatureException e) {
        Log.d(TAG, "Signature exception while trying to verify");
    }
    return signatureVerification;
}
Also used : Account(android.accounts.Account) OwnCloudAccount(com.owncloud.android.lib.common.OwnCloudAccount) PushConfigurationState(com.owncloud.android.datamodel.PushConfigurationState) PublicKey(java.security.PublicKey) Signature(java.security.Signature) ArbitraryDataProvider(com.owncloud.android.datamodel.ArbitraryDataProvider) Gson(com.google.gson.Gson) SignatureVerification(com.owncloud.android.datamodel.SignatureVerification) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) SignatureException(java.security.SignatureException) InvalidKeyException(java.security.InvalidKeyException)

Example 3 with SignatureVerification

use of com.owncloud.android.datamodel.SignatureVerification in project android by nextcloud.

the class PushUtils method verifySignature.

public static SignatureVerification verifySignature(final Context context, final UserAccountManager accountManager, final byte[] signatureBytes, final byte[] subjectBytes) {
    Signature signature;
    PublicKey publicKey;
    Account[] accounts = accountManager.getAccounts();
    ArbitraryDataProvider arbitraryDataProvider = new ArbitraryDataProvider(context.getContentResolver());
    String arbitraryValue;
    Gson gson = new Gson();
    PushConfigurationState pushArbitraryData;
    try {
        signature = Signature.getInstance("SHA512withRSA");
        if (accounts.length > 0) {
            for (Account account : accounts) {
                if (!TextUtils.isEmpty(arbitraryValue = arbitraryDataProvider.getValue(account.name, KEY_PUSH))) {
                    pushArbitraryData = gson.fromJson(arbitraryValue, PushConfigurationState.class);
                    if (!pushArbitraryData.isShouldBeDeleted()) {
                        publicKey = (PublicKey) readKeyFromString(true, pushArbitraryData.getUserPublicKey());
                        signature.initVerify(publicKey);
                        signature.update(subjectBytes);
                        if (signature.verify(signatureBytes)) {
                            return new SignatureVerification(true, account);
                        }
                    }
                }
            }
        }
    } catch (NoSuchAlgorithmException e) {
        Log.d(TAG, "No such algorithm");
    } catch (InvalidKeyException e) {
        Log.d(TAG, "Invalid key while trying to verify");
    } catch (SignatureException e) {
        Log.d(TAG, "Signature exception while trying to verify");
    }
    return new SignatureVerification(false, null);
}
Also used : Account(android.accounts.Account) OwnCloudAccount(com.owncloud.android.lib.common.OwnCloudAccount) PushConfigurationState(com.owncloud.android.datamodel.PushConfigurationState) PublicKey(java.security.PublicKey) Signature(java.security.Signature) ArbitraryDataProvider(com.owncloud.android.datamodel.ArbitraryDataProvider) Gson(com.google.gson.Gson) SignatureVerification(com.owncloud.android.datamodel.SignatureVerification) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) SignatureException(java.security.SignatureException) InvalidKeyException(java.security.InvalidKeyException)

Aggregations

Gson (com.google.gson.Gson)3 SignatureVerification (com.owncloud.android.datamodel.SignatureVerification)3 InvalidKeyException (java.security.InvalidKeyException)3 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)3 Account (android.accounts.Account)2 ArbitraryDataProvider (com.owncloud.android.datamodel.ArbitraryDataProvider)2 PushConfigurationState (com.owncloud.android.datamodel.PushConfigurationState)2 OwnCloudAccount (com.owncloud.android.lib.common.OwnCloudAccount)2 PublicKey (java.security.PublicKey)2 Signature (java.security.Signature)2 SignatureException (java.security.SignatureException)2 Context (android.content.Context)1 NonNull (android.support.annotation.NonNull)1 PersistableBundleCompat (com.evernote.android.job.util.support.PersistableBundleCompat)1 DecryptedPushMessage (com.owncloud.android.datamodel.DecryptedPushMessage)1 PushUtils (com.owncloud.android.utils.PushUtils)1 PrivateKey (java.security.PrivateKey)1 Cipher (javax.crypto.Cipher)1 NoSuchPaddingException (javax.crypto.NoSuchPaddingException)1