Search in sources :

Example 1 with DecryptedPushMessage

use of com.owncloud.android.datamodel.DecryptedPushMessage 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)

Aggregations

Context (android.content.Context)1 NonNull (android.support.annotation.NonNull)1 PersistableBundleCompat (com.evernote.android.job.util.support.PersistableBundleCompat)1 Gson (com.google.gson.Gson)1 DecryptedPushMessage (com.owncloud.android.datamodel.DecryptedPushMessage)1 SignatureVerification (com.owncloud.android.datamodel.SignatureVerification)1 PushUtils (com.owncloud.android.utils.PushUtils)1 InvalidKeyException (java.security.InvalidKeyException)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1 PrivateKey (java.security.PrivateKey)1 Cipher (javax.crypto.Cipher)1 NoSuchPaddingException (javax.crypto.NoSuchPaddingException)1