@Test public void testAuthCiphertextsSavedWithNewScreenLock() throws Exception { TestUtils.setBuildVersion(VERSION_CODES.M); when(hybridDecrypt.decrypt(any(byte[].class), any(byte[].class))) .thenThrow(new UserNotAuthenticatedException()); ciphertextBuilder.setIsAuthKey(true); byte[] ciphertextBytes = ciphertextBuilder.build().toByteArray(); decrypterManager.decrypt(ciphertextBytes, handler, extra); verify(handler).authCiphertextSavedForLater(ciphertextBytes, extra); verifyNoMoreInteractions(handler); }
public synchronized void decrypt(byte[] ciphertext, CapillaryHandler handler, Object extra) { CapillaryCiphertext capillaryCiphertext; try { capillaryCiphertext = CapillaryCiphertext.parseFrom(ciphertext); } catch (InvalidProtocolBufferException e) { handler.error(CapillaryHandlerErrorCode.MALFORMED_CIPHERTEXT, ciphertext, extra); return; } if (capillaryCiphertext.getIsAuthKey() && utils.isScreenLocked(context)) { ciphertextStorage.save(ciphertext); handler.authCiphertextSavedForLater(ciphertext, extra); return; } byte[] rawCiphertext = capillaryCiphertext.getCiphertext().toByteArray(); byte[] data; try { HybridDecrypt decrypter = keyManager.getDecrypter( capillaryCiphertext.getKeychainUniqueId(), capillaryCiphertext.getKeySerialNumber(), capillaryCiphertext.getIsAuthKey()); data = decrypter.decrypt(rawCiphertext, null); } catch (AuthModeUnavailableException e) { handler.error(CapillaryHandlerErrorCode.AUTH_CIPHER_IN_NO_AUTH_DEVICE, ciphertext, extra); return; } catch (Exception e) { if (VERSION.SDK_INT >= VERSION_CODES.M && e instanceof UserNotAuthenticatedException) { ciphertextStorage.save(ciphertext); handler.authCiphertextSavedForLater(ciphertext, extra); return; } if (e instanceof NoSuchKeyException || (VERSION.SDK_INT >= VERSION_CODES.M && e instanceof KeyPermanentlyInvalidatedException) || (VERSION.SDK_INT >= VERSION_CODES.O && e instanceof UnrecoverableKeyException)) { regenerateKeyAndRequestMessage(capillaryCiphertext, handler, extra); return; } handler.error(CapillaryHandlerErrorCode.UNKNOWN_ERROR, ciphertext, extra); return; } handler.handleData(capillaryCiphertext.getIsAuthKey(), data, extra); }
DecrypterManager { public synchronized void decrypt(byte[] ciphertext, CapillaryHandler handler, Object extra) { CapillaryCiphertext capillaryCiphertext; try { capillaryCiphertext = CapillaryCiphertext.parseFrom(ciphertext); } catch (InvalidProtocolBufferException e) { handler.error(CapillaryHandlerErrorCode.MALFORMED_CIPHERTEXT, ciphertext, extra); return; } if (capillaryCiphertext.getIsAuthKey() && utils.isScreenLocked(context)) { ciphertextStorage.save(ciphertext); handler.authCiphertextSavedForLater(ciphertext, extra); return; } byte[] rawCiphertext = capillaryCiphertext.getCiphertext().toByteArray(); byte[] data; try { HybridDecrypt decrypter = keyManager.getDecrypter( capillaryCiphertext.getKeychainUniqueId(), capillaryCiphertext.getKeySerialNumber(), capillaryCiphertext.getIsAuthKey()); data = decrypter.decrypt(rawCiphertext, null); } catch (AuthModeUnavailableException e) { handler.error(CapillaryHandlerErrorCode.AUTH_CIPHER_IN_NO_AUTH_DEVICE, ciphertext, extra); return; } catch (Exception e) { if (VERSION.SDK_INT >= VERSION_CODES.M && e instanceof UserNotAuthenticatedException) { ciphertextStorage.save(ciphertext); handler.authCiphertextSavedForLater(ciphertext, extra); return; } if (e instanceof NoSuchKeyException || (VERSION.SDK_INT >= VERSION_CODES.M && e instanceof KeyPermanentlyInvalidatedException) || (VERSION.SDK_INT >= VERSION_CODES.O && e instanceof UnrecoverableKeyException)) { regenerateKeyAndRequestMessage(capillaryCiphertext, handler, extra); return; } handler.error(CapillaryHandlerErrorCode.UNKNOWN_ERROR, ciphertext, extra); return; } handler.handleData(capillaryCiphertext.getIsAuthKey(), data, extra); } }
DecrypterManager { public synchronized void decrypt(byte[] ciphertext, CapillaryHandler handler, Object extra) { CapillaryCiphertext capillaryCiphertext; try { capillaryCiphertext = CapillaryCiphertext.parseFrom(ciphertext); } catch (InvalidProtocolBufferException e) { handler.error(CapillaryHandlerErrorCode.MALFORMED_CIPHERTEXT, ciphertext, extra); return; } if (capillaryCiphertext.getIsAuthKey() && utils.isScreenLocked(context)) { ciphertextStorage.save(ciphertext); handler.authCiphertextSavedForLater(ciphertext, extra); return; } byte[] rawCiphertext = capillaryCiphertext.getCiphertext().toByteArray(); byte[] data; try { HybridDecrypt decrypter = keyManager.getDecrypter( capillaryCiphertext.getKeychainUniqueId(), capillaryCiphertext.getKeySerialNumber(), capillaryCiphertext.getIsAuthKey()); data = decrypter.decrypt(rawCiphertext, null); } catch (AuthModeUnavailableException e) { handler.error(CapillaryHandlerErrorCode.AUTH_CIPHER_IN_NO_AUTH_DEVICE, ciphertext, extra); return; } catch (Exception e) { if (VERSION.SDK_INT >= VERSION_CODES.M && e instanceof UserNotAuthenticatedException) { ciphertextStorage.save(ciphertext); handler.authCiphertextSavedForLater(ciphertext, extra); return; } if (e instanceof NoSuchKeyException || (VERSION.SDK_INT >= VERSION_CODES.M && e instanceof KeyPermanentlyInvalidatedException) || (VERSION.SDK_INT >= VERSION_CODES.O && e instanceof UnrecoverableKeyException)) { regenerateKeyAndRequestMessage(capillaryCiphertext, handler, extra); return; } handler.error(CapillaryHandlerErrorCode.UNKNOWN_ERROR, ciphertext, extra); return; } handler.handleData(capillaryCiphertext.getIsAuthKey(), data, extra); } DecrypterManager(
      Context context, KeyManager keyManager, CiphertextStorage ciphertextStorage, Utils utils); }
DecrypterManager { public synchronized void decrypt(byte[] ciphertext, CapillaryHandler handler, Object extra) { CapillaryCiphertext capillaryCiphertext; try { capillaryCiphertext = CapillaryCiphertext.parseFrom(ciphertext); } catch (InvalidProtocolBufferException e) { handler.error(CapillaryHandlerErrorCode.MALFORMED_CIPHERTEXT, ciphertext, extra); return; } if (capillaryCiphertext.getIsAuthKey() && utils.isScreenLocked(context)) { ciphertextStorage.save(ciphertext); handler.authCiphertextSavedForLater(ciphertext, extra); return; } byte[] rawCiphertext = capillaryCiphertext.getCiphertext().toByteArray(); byte[] data; try { HybridDecrypt decrypter = keyManager.getDecrypter( capillaryCiphertext.getKeychainUniqueId(), capillaryCiphertext.getKeySerialNumber(), capillaryCiphertext.getIsAuthKey()); data = decrypter.decrypt(rawCiphertext, null); } catch (AuthModeUnavailableException e) { handler.error(CapillaryHandlerErrorCode.AUTH_CIPHER_IN_NO_AUTH_DEVICE, ciphertext, extra); return; } catch (Exception e) { if (VERSION.SDK_INT >= VERSION_CODES.M && e instanceof UserNotAuthenticatedException) { ciphertextStorage.save(ciphertext); handler.authCiphertextSavedForLater(ciphertext, extra); return; } if (e instanceof NoSuchKeyException || (VERSION.SDK_INT >= VERSION_CODES.M && e instanceof KeyPermanentlyInvalidatedException) || (VERSION.SDK_INT >= VERSION_CODES.O && e instanceof UnrecoverableKeyException)) { regenerateKeyAndRequestMessage(capillaryCiphertext, handler, extra); return; } handler.error(CapillaryHandlerErrorCode.UNKNOWN_ERROR, ciphertext, extra); return; } handler.handleData(capillaryCiphertext.getIsAuthKey(), data, extra); } DecrypterManager(
      Context context, KeyManager keyManager, CiphertextStorage ciphertextStorage, Utils utils); synchronized void decryptSaved(CapillaryHandler handler, Object extra); synchronized void decrypt(byte[] ciphertext, CapillaryHandler handler, Object extra); }
DecrypterManager { public synchronized void decrypt(byte[] ciphertext, CapillaryHandler handler, Object extra) { CapillaryCiphertext capillaryCiphertext; try { capillaryCiphertext = CapillaryCiphertext.parseFrom(ciphertext); } catch (InvalidProtocolBufferException e) { handler.error(CapillaryHandlerErrorCode.MALFORMED_CIPHERTEXT, ciphertext, extra); return; } if (capillaryCiphertext.getIsAuthKey() && utils.isScreenLocked(context)) { ciphertextStorage.save(ciphertext); handler.authCiphertextSavedForLater(ciphertext, extra); return; } byte[] rawCiphertext = capillaryCiphertext.getCiphertext().toByteArray(); byte[] data; try { HybridDecrypt decrypter = keyManager.getDecrypter( capillaryCiphertext.getKeychainUniqueId(), capillaryCiphertext.getKeySerialNumber(), capillaryCiphertext.getIsAuthKey()); data = decrypter.decrypt(rawCiphertext, null); } catch (AuthModeUnavailableException e) { handler.error(CapillaryHandlerErrorCode.AUTH_CIPHER_IN_NO_AUTH_DEVICE, ciphertext, extra); return; } catch (Exception e) { if (VERSION.SDK_INT >= VERSION_CODES.M && e instanceof UserNotAuthenticatedException) { ciphertextStorage.save(ciphertext); handler.authCiphertextSavedForLater(ciphertext, extra); return; } if (e instanceof NoSuchKeyException || (VERSION.SDK_INT >= VERSION_CODES.M && e instanceof KeyPermanentlyInvalidatedException) || (VERSION.SDK_INT >= VERSION_CODES.O && e instanceof UnrecoverableKeyException)) { regenerateKeyAndRequestMessage(capillaryCiphertext, handler, extra); return; } handler.error(CapillaryHandlerErrorCode.UNKNOWN_ERROR, ciphertext, extra); return; } handler.handleData(capillaryCiphertext.getIsAuthKey(), data, extra); } DecrypterManager(
      Context context, KeyManager keyManager, CiphertextStorage ciphertextStorage, Utils utils); synchronized void decryptSaved(CapillaryHandler handler, Object extra); synchronized void decrypt(byte[] ciphertext, CapillaryHandler handler, Object extra);  }