From 7d7fbe68286d77f60c8f390034ee39f849f42dfa Mon Sep 17 00:00:00 2001 From: Florian Schmaus Date: Tue, 2 Apr 2019 15:55:31 +0200 Subject: [PATCH] Do not explicity select the (crypto) Provider in smack-omemo This makes the system select the "best" available provider. Also the 'BC' provider in newer Android version does not longer implement certain Ciphers, which causes an NoSuchAlgorithmException if the Cipher is requested explicitly by the 'BC' provider: E/XmppService: XmppServiceConnection - Error while sending pending messages org.jivesoftware.smackx.omemo.exceptions.CryptoFailedException: java.security.NoSuchAlgorithmException: The BC provider no longer provides an implementation for Cipher.AES/GCM/NoPadding. Please see https://android-developers.googleblog.com/2018/03/cryptography-changes-in-android-p.html for more details. at org.jivesoftware.smackx.omemo.OmemoService.encrypt(OmemoService.java:375) at org.jivesoftware.smackx.omemo.OmemoService.createOmemoMessage(OmemoService.java:537) at org.jivesoftware.smackx.omemo.OmemoManager.encrypt(OmemoManager.java:341) at org.jivesoftware.smackx.omemo.OmemoManager.encrypt(OmemoManager.java:314) at es.iecisa.xmppservice.XmppServiceConnection.lambda$sendMessage$0(XmppServiceConnection.java:516) at es.iecisa.xmppservice.-$$Lambda$XmppServiceConnection$aBU_80chagvypMTSd-aSm7pRQRY.run(Unknown Source:4) at java.lang.Thread.run(Thread.java:764) Caused by: java.security.NoSuchAlgorithmException: The BC provider no longer provides an implementation for Cipher.AES/GCM/NoPadding. Please see https://android-developers.googleblog.com/2018/03/cryptography-changes-in-android-p.html for more details. at sun.security.jca.Providers.checkBouncyCastleDeprecation(Providers.java:563) at sun.security.jca.Providers.checkBouncyCastleDeprecation(Providers.java:346) at javax.crypto.Cipher.createCipher(Cipher.java:722) at javax.crypto.Cipher.getInstance(Cipher.java:717) at javax.crypto.Cipher.getInstance(Cipher.java:674) at org.jivesoftware.smackx.omemo.util.OmemoMessageBuilder.setMessage(OmemoMessageBuilder.java:169) at org.jivesoftware.smackx.omemo.util.OmemoMessageBuilder.(OmemoMessageBuilder.java:116) at org.jivesoftware.smackx.omemo.OmemoService.encrypt(OmemoService.java:372) at org.jivesoftware.smackx.omemo.OmemoService.createOmemoMessage(OmemoService.java:537) at org.jivesoftware.smackx.omemo.OmemoManager.encrypt(OmemoManager.java:341) at org.jivesoftware.smackx.omemo.OmemoManager.encrypt(OmemoManager.java:314) at es.iecisa.xmppservice.XmppServiceConnection.lambda$sendMessage$0(XmppServiceConnection.java:516) at es.iecisa.xmppservice.-$$Lambda$XmppServiceConnection$aBU_80chagvypMTSd-aSm7pRQRY.run(Unknown Source:4) at java.lang.Thread.run(Thread.java:764) --- .../jivesoftware/smackx/omemo/OmemoService.java | 7 +++---- .../smackx/omemo/internal/CipherAndAuthTag.java | 8 +++----- .../smackx/omemo/util/OmemoConstants.java | 1 - .../smackx/omemo/util/OmemoMessageBuilder.java | 14 +++++--------- 4 files changed, 11 insertions(+), 19 deletions(-) diff --git a/smack-omemo/src/main/java/org/jivesoftware/smackx/omemo/OmemoService.java b/smack-omemo/src/main/java/org/jivesoftware/smackx/omemo/OmemoService.java index 27f775f3e..56d0c4d9f 100644 --- a/smack-omemo/src/main/java/org/jivesoftware/smackx/omemo/OmemoService.java +++ b/smack-omemo/src/main/java/org/jivesoftware/smackx/omemo/OmemoService.java @@ -1,6 +1,6 @@ /** * - * Copyright 2017 Paul Schaub + * Copyright 2017 Paul Schaub, 2019 Florian Schmaus * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -23,7 +23,6 @@ import java.io.UnsupportedEncodingException; import java.security.InvalidAlgorithmParameterException; import java.security.InvalidKeyException; import java.security.NoSuchAlgorithmException; -import java.security.NoSuchProviderException; import java.security.Security; import java.util.ArrayList; import java.util.Collection; @@ -308,7 +307,7 @@ public abstract class OmemoService(userDevice, gullibleTrustCallback, getOmemoRatchet(manager), messageKey, iv, null); - } catch (InvalidKeyException | InvalidAlgorithmParameterException | NoSuchPaddingException | BadPaddingException | UnsupportedEncodingException | NoSuchProviderException | IllegalBlockSizeException e) { + } catch (InvalidKeyException | InvalidAlgorithmParameterException | NoSuchPaddingException | BadPaddingException | UnsupportedEncodingException | IllegalBlockSizeException e) { throw new CryptoFailedException(e); } @@ -370,7 +369,7 @@ public abstract class OmemoService( userDevice, manager.getTrustCallback(), getOmemoRatchet(managerGuard.get()), messageKey, iv, message); - } catch (UnsupportedEncodingException | BadPaddingException | IllegalBlockSizeException | NoSuchProviderException | + } catch (UnsupportedEncodingException | BadPaddingException | IllegalBlockSizeException | NoSuchPaddingException | InvalidAlgorithmParameterException | InvalidKeyException | NoSuchAlgorithmException e) { throw new CryptoFailedException(e); } diff --git a/smack-omemo/src/main/java/org/jivesoftware/smackx/omemo/internal/CipherAndAuthTag.java b/smack-omemo/src/main/java/org/jivesoftware/smackx/omemo/internal/CipherAndAuthTag.java index 964a33910..3f03f2f72 100644 --- a/smack-omemo/src/main/java/org/jivesoftware/smackx/omemo/internal/CipherAndAuthTag.java +++ b/smack-omemo/src/main/java/org/jivesoftware/smackx/omemo/internal/CipherAndAuthTag.java @@ -1,6 +1,6 @@ /** * - * Copyright 2017 Paul Schaub + * Copyright 2017 Paul Schaub, 2019 Florian Schmaus * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -18,11 +18,9 @@ package org.jivesoftware.smackx.omemo.internal; import static org.jivesoftware.smackx.omemo.util.OmemoConstants.Crypto.CIPHERMODE; import static org.jivesoftware.smackx.omemo.util.OmemoConstants.Crypto.KEYTYPE; -import static org.jivesoftware.smackx.omemo.util.OmemoConstants.Crypto.PROVIDER; import java.security.InvalidAlgorithmParameterException; import java.security.NoSuchAlgorithmException; -import java.security.NoSuchProviderException; import javax.crypto.Cipher; import javax.crypto.NoSuchPaddingException; import javax.crypto.spec.IvParameterSpec; @@ -50,13 +48,13 @@ public class CipherAndAuthTag { Cipher cipher; try { - cipher = Cipher.getInstance(CIPHERMODE, PROVIDER); + cipher = Cipher.getInstance(CIPHERMODE); SecretKeySpec keySpec = new SecretKeySpec(key, KEYTYPE); IvParameterSpec ivSpec = new IvParameterSpec(iv); cipher.init(Cipher.DECRYPT_MODE, keySpec, ivSpec); } catch (NoSuchAlgorithmException | java.security.InvalidKeyException | InvalidAlgorithmParameterException | - NoSuchPaddingException | NoSuchProviderException e) { + NoSuchPaddingException e) { throw new CryptoFailedException(e); } diff --git a/smack-omemo/src/main/java/org/jivesoftware/smackx/omemo/util/OmemoConstants.java b/smack-omemo/src/main/java/org/jivesoftware/smackx/omemo/util/OmemoConstants.java index c362145c6..885260568 100644 --- a/smack-omemo/src/main/java/org/jivesoftware/smackx/omemo/util/OmemoConstants.java +++ b/smack-omemo/src/main/java/org/jivesoftware/smackx/omemo/util/OmemoConstants.java @@ -58,6 +58,5 @@ public final class OmemoConstants { public static final String KEYTYPE = "AES"; public static final int KEYLENGTH = 128; public static final String CIPHERMODE = "AES/GCM/NoPadding"; - public static final String PROVIDER = "BC"; } } diff --git a/smack-omemo/src/main/java/org/jivesoftware/smackx/omemo/util/OmemoMessageBuilder.java b/smack-omemo/src/main/java/org/jivesoftware/smackx/omemo/util/OmemoMessageBuilder.java index 8d254f44c..7a625ee15 100644 --- a/smack-omemo/src/main/java/org/jivesoftware/smackx/omemo/util/OmemoMessageBuilder.java +++ b/smack-omemo/src/main/java/org/jivesoftware/smackx/omemo/util/OmemoMessageBuilder.java @@ -1,6 +1,6 @@ /** * - * Copyright 2017 Paul Schaub + * Copyright 2017 Paul Schaub, 2019 Florian Schmaus * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -19,7 +19,6 @@ package org.jivesoftware.smackx.omemo.util; import static org.jivesoftware.smackx.omemo.util.OmemoConstants.Crypto.CIPHERMODE; import static org.jivesoftware.smackx.omemo.util.OmemoConstants.Crypto.KEYLENGTH; import static org.jivesoftware.smackx.omemo.util.OmemoConstants.Crypto.KEYTYPE; -import static org.jivesoftware.smackx.omemo.util.OmemoConstants.Crypto.PROVIDER; import java.io.UnsupportedEncodingException; import java.security.InvalidAlgorithmParameterException; @@ -96,7 +95,6 @@ public class OmemoMessageBuilder ratchet, String message) throws NoSuchPaddingException, BadPaddingException, InvalidKeyException, NoSuchAlgorithmException, IllegalBlockSizeException, - UnsupportedEncodingException, NoSuchProviderException, InvalidAlgorithmParameterException { + UnsupportedEncodingException, InvalidAlgorithmParameterException { this(userDevice, callback, ratchet, generateKey(KEYTYPE, KEYLENGTH), generateIv(), message); } @@ -150,7 +147,6 @@ public class OmemoMessageBuilder