Search in sources :

Example 1 with KeyStoreException

use of com.toshi.exception.KeyStoreException in project toshi-android-client by toshiapp.

the class HDWallet method readMasterSeedFromStorage.

private String readMasterSeedFromStorage() {
    try {
        final KeyStoreHandler keyStoreHandler = new KeyStoreHandler(BaseApplication.get(), ALIAS);
        final String encryptedMasterSeed = this.prefs.getString(MASTER_SEED, null);
        if (encryptedMasterSeed == null)
            return null;
        return keyStoreHandler.decrypt(encryptedMasterSeed, this::saveMasterSeed);
    } catch (KeyStoreException e) {
        LogUtil.exception("Error while reading master seed from storage", e);
        throw new IllegalStateException(e);
    }
}
Also used : KeyStoreHandler(com.toshi.crypto.keyStore.KeyStoreHandler) KeyStoreException(com.toshi.exception.KeyStoreException)

Example 2 with KeyStoreException

use of com.toshi.exception.KeyStoreException in project toshi-android-client by toshiapp.

the class KeystoreHandler23 method encrypt.

@Override
public String encrypt(final String textToEncrypt) throws KeyStoreException {
    try {
        final Cipher cipher = Cipher.getInstance(TRANSFORMATION);
        final GCMParameterSpec spec = new GCMParameterSpec(128, encryptionIv.getBytes());
        cipher.init(Cipher.ENCRYPT_MODE, getSecretKey(), spec);
        final byte[] encryptedData = cipher.doFinal(textToEncrypt.getBytes(UTF_8));
        return Base64.encodeToString(encryptedData, Base64.DEFAULT);
    } catch (NoSuchAlgorithmException | BadPaddingException | InvalidKeyException | NoSuchPaddingException | IllegalBlockSizeException | UnsupportedEncodingException | InvalidAlgorithmParameterException | java.security.KeyStoreException | UnrecoverableEntryException e) {
        throw new KeyStoreException(new Throwable(e.getMessage()));
    }
}
Also used : InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) NoSuchPaddingException(javax.crypto.NoSuchPaddingException) IllegalBlockSizeException(javax.crypto.IllegalBlockSizeException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) GCMParameterSpec(javax.crypto.spec.GCMParameterSpec) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) BadPaddingException(javax.crypto.BadPaddingException) KeyStoreException(com.toshi.exception.KeyStoreException) InvalidKeyException(java.security.InvalidKeyException) UnrecoverableEntryException(java.security.UnrecoverableEntryException) Cipher(javax.crypto.Cipher)

Example 3 with KeyStoreException

use of com.toshi.exception.KeyStoreException in project toshi-android-client by toshiapp.

the class KeyStoreBase method initKeyStore.

private void initKeyStore(final Context context) throws KeyStoreException {
    this.context = context;
    try {
        this.keyStore = KeyStore.getInstance(ANDROID_KEY_STORE);
        this.keyStore.load(null);
        createNewKeysIfNeeded();
    } catch (IOException | NoSuchAlgorithmException | CertificateException | java.security.KeyStoreException e) {
        throw new KeyStoreException(new Throwable(e.getMessage()));
    }
}
Also used : CertificateException(java.security.cert.CertificateException) IOException(java.io.IOException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) KeyStoreException(com.toshi.exception.KeyStoreException)

Example 4 with KeyStoreException

use of com.toshi.exception.KeyStoreException in project toshi-android-client by toshiapp.

the class HDWallet method saveMasterSeedToStorage.

private void saveMasterSeedToStorage(final String masterSeed) {
    try {
        final KeyStoreHandler keyStoreHandler = new KeyStoreHandler(BaseApplication.get(), ALIAS);
        final String encryptedMasterSeed = keyStoreHandler.encrypt(masterSeed);
        saveMasterSeed(encryptedMasterSeed);
        this.masterSeed = masterSeed;
    } catch (KeyStoreException e) {
        LogUtil.exception("Error while saving master seed from storage", e);
        throw new IllegalStateException(e);
    }
}
Also used : KeyStoreHandler(com.toshi.crypto.keyStore.KeyStoreHandler) KeyStoreException(com.toshi.exception.KeyStoreException)

Example 5 with KeyStoreException

use of com.toshi.exception.KeyStoreException in project toshi-android-client by toshiapp.

the class KeystoreHandler23 method decryptCurrentKeystoreVersion.

@NonNull
private String decryptCurrentKeystoreVersion(String encryptedData) throws KeyStoreException {
    try {
        final Cipher cipher = Cipher.getInstance(TRANSFORMATION);
        final GCMParameterSpec spec = new GCMParameterSpec(128, encryptionIv.getBytes());
        cipher.init(Cipher.DECRYPT_MODE, getSecretKey(), spec);
        final byte[] encryptedBytes = Base64.decode(encryptedData, Base64.DEFAULT);
        final byte[] decryptedBytes = cipher.doFinal(encryptedBytes);
        return new String(decryptedBytes, UTF_8);
    } catch (UnrecoverableEntryException | UnsupportedEncodingException | IllegalBlockSizeException | NoSuchPaddingException | InvalidAlgorithmParameterException | InvalidKeyException | java.security.KeyStoreException | BadPaddingException | NoSuchAlgorithmException e) {
        throw new KeyStoreException(new Throwable(e.getMessage()));
    }
}
Also used : InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) IllegalBlockSizeException(javax.crypto.IllegalBlockSizeException) NoSuchPaddingException(javax.crypto.NoSuchPaddingException) GCMParameterSpec(javax.crypto.spec.GCMParameterSpec) KeyStoreException(com.toshi.exception.KeyStoreException) BadPaddingException(javax.crypto.BadPaddingException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeyException(java.security.InvalidKeyException) UnrecoverableEntryException(java.security.UnrecoverableEntryException) Cipher(javax.crypto.Cipher) NonNull(android.support.annotation.NonNull)

Aggregations

KeyStoreException (com.toshi.exception.KeyStoreException)6 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)4 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)3 KeyStoreHandler (com.toshi.crypto.keyStore.KeyStoreHandler)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 InvalidKeyException (java.security.InvalidKeyException)2 UnrecoverableEntryException (java.security.UnrecoverableEntryException)2 BadPaddingException (javax.crypto.BadPaddingException)2 Cipher (javax.crypto.Cipher)2 IllegalBlockSizeException (javax.crypto.IllegalBlockSizeException)2 NoSuchPaddingException (javax.crypto.NoSuchPaddingException)2 GCMParameterSpec (javax.crypto.spec.GCMParameterSpec)2 KeyGenParameterSpec (android.security.keystore.KeyGenParameterSpec)1 NonNull (android.support.annotation.NonNull)1 IOException (java.io.IOException)1 NoSuchProviderException (java.security.NoSuchProviderException)1 CertificateException (java.security.cert.CertificateException)1 KeyGenerator (javax.crypto.KeyGenerator)1