Search in sources :

Example 26 with ArbitraryDataProvider

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

Aggregations

ArbitraryDataProvider (com.owncloud.android.datamodel.ArbitraryDataProvider)26 Account (android.accounts.Account)14 Context (android.content.Context)8 OwnCloudAccount (com.owncloud.android.lib.common.OwnCloudAccount)8 IOException (java.io.IOException)8 File (java.io.File)6 Intent (android.content.Intent)5 View (android.view.View)4 PersistableBundleCompat (com.evernote.android.job.util.support.PersistableBundleCompat)4 FilesystemDataProvider (com.owncloud.android.datamodel.FilesystemDataProvider)4 RemoteOperationResult (com.owncloud.android.lib.common.operations.RemoteOperationResult)4 ContentResolver (android.content.ContentResolver)3 NonNull (android.support.annotation.NonNull)3 Gson (com.google.gson.Gson)3 PushConfigurationState (com.owncloud.android.datamodel.PushConfigurationState)3 AuthenticatorException (android.accounts.AuthenticatorException)2 OperationCanceledException (android.accounts.OperationCanceledException)2 Drawable (android.graphics.drawable.Drawable)2 Nullable (android.support.annotation.Nullable)2 RequiresApi (android.support.annotation.RequiresApi)2