Asymmetric cipher with insecure padding used
Closed this issue · 4 comments
The code you threw is most likely obtained after OWASP security analysis in the web application appsweep:
protected Cipher i() {
String string;
if (Build.VERSION.SDK_INT < 23) {
string = "AndroidOpenSSL";
return Cipher.getInstance("RSA/ECB/PKCS1Padding", string);
}
string = "AndroidKeyStoreBCWorkaround";
return Cipher.getInstance("RSA/ECB/PKCS1Padding", string);
}
This code was obtained during reverse engineering and it matches the code in the library file (RSACipher18Implementation.java):
protected Cipher getRSACipher() throws Exception {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
return Cipher.getInstance("RSA/ECB/PKCS1Padding", "AndroidOpenSSL"); // error in android 6: InvalidKeyException: Need RSA private or public key
} else {
return Cipher.getInstance("RSA/ECB/PKCS1Padding", "AndroidKeyStoreBCWorkaround"); // error in android 5: NoSuchProviderException: Provider not available: AndroidKeyStoreBCWorkaround
}
}
The getRSAChiper method is used in only two places in the file (RSACipher18Implementation.java) of the RSACipher18Implementation class:
@Override
public byte[] wrap(Key key) throws Exception {
PublicKey publicKey = getPublicKey();
Cipher cipher = **getRSACipher();**
cipher.init(Cipher.WRAP_MODE, publicKey, getAlgorithmParameterSpec());
return cipher.wrap(key);
}
@Override
public Key unwrap(byte[] wrappedKey, String algorithm) throws Exception {
PrivateKey privateKey = getPrivateKey();
Cipher cipher = **getRSACipher();**
cipher.init(Cipher.UNWRAP_MODE, privateKey, getAlgorithmParameterSpec());
return cipher.unwrap(wrappedKey, algorithm, Cipher.SECRET_KEY);
}
Further if we trace where the RSACipher18Implementation class is used we will see enum:
enum KeyCipherAlgorithm {
RSA_ECB_PKCS1Padding(RSACipher18Implementation::new, 1),
@SuppressWarnings({"UnusedDeclaration"})
RSA_ECB_OAEPwithSHA_256andMGF1Padding(RSACipherOAEPImplementation::new, Build.VERSION_CODES.M);
final KeyCipherFunction keyCipher;
final int minVersionCode;
KeyCipherAlgorithm(KeyCipherFunction keyCipher, int minVersionCode) {
this.keyCipher = keyCipher;
this.minVersionCode = minVersionCode;
}
}
This enum is used in the StorageCipherFactory class:
public class StorageCipherFactory {
private static final String ELEMENT_PREFERENCES_ALGORITHM_PREFIX = "FlutterSecureSAlgorithm";
private static final String ELEMENT_PREFERENCES_ALGORITHM_KEY = ELEMENT_PREFERENCES_ALGORITHM_PREFIX + "Key";
private static final String ELEMENT_PREFERENCES_ALGORITHM_STORAGE = ELEMENT_PREFERENCES_ALGORITHM_PREFIX + "Storage";
private static final KeyCipherAlgorithm DEFAULT_KEY_ALGORITHM = KeyCipherAlgorithm.RSA_ECB_PKCS1Padding;
private static final StorageCipherAlgorithm DEFAULT_STORAGE_ALGORITHM = StorageCipherAlgorithm.AES_CBC_PKCS7Padding;
private final KeyCipherAlgorithm savedKeyAlgorithm;
private final StorageCipherAlgorithm savedStorageAlgorithm;
private final KeyCipherAlgorithm currentKeyAlgorithm;
private final StorageCipherAlgorithm currentStorageAlgorithm;
public StorageCipherFactory(SharedPreferences source, Map<String, Object> options) {
savedKeyAlgorithm = KeyCipherAlgorithm.valueOf(source.getString(ELEMENT_PREFERENCES_ALGORITHM_KEY, DEFAULT_KEY_ALGORITHM.name()));
savedStorageAlgorithm = StorageCipherAlgorithm.valueOf(source.getString(ELEMENT_PREFERENCES_ALGORITHM_STORAGE, DEFAULT_STORAGE_ALGORITHM.name()));
final KeyCipherAlgorithm currentKeyAlgorithmTmp = KeyCipherAlgorithm.valueOf(getFromOptionsWithDefault(options, "keyCipherAlgorithm", DEFAULT_KEY_ALGORITHM.name()));
currentKeyAlgorithm = (currentKeyAlgorithmTmp.minVersionCode <= Build.VERSION.SDK_INT) ? currentKeyAlgorithmTmp : DEFAULT_KEY_ALGORITHM;
final StorageCipherAlgorithm currentStorageAlgorithmTmp = StorageCipherAlgorithm.valueOf(getFromOptionsWithDefault(options, "storageCipherAlgorithm", DEFAULT_STORAGE_ALGORITHM.name()));
currentStorageAlgorithm = (currentStorageAlgorithmTmp.minVersionCode <= Build.VERSION.SDK_INT) ? currentStorageAlgorithmTmp : DEFAULT_STORAGE_ALGORITHM;
}
private String getFromOptionsWithDefault(Map<String, Object> options, String key, String defaultValue) {
final Object value = options.get(key);
return value != null ? value.toString() : defaultValue;
}
public boolean requiresReEncryption() {
return savedKeyAlgorithm != currentKeyAlgorithm || savedStorageAlgorithm != currentStorageAlgorithm;
}
public StorageCipher getSavedStorageCipher(Context context) throws Exception {
final KeyCipher keyCipher = savedKeyAlgorithm.keyCipher.apply(context);
return savedStorageAlgorithm.storageCipher.apply(context, keyCipher);
}
public StorageCipher getCurrentStorageCipher(Context context) throws Exception {
final KeyCipher keyCipher = currentKeyAlgorithm.keyCipher.apply(context);
return currentStorageAlgorithm.storageCipher.apply(context, keyCipher);
}
public void storeCurrentAlgorithms(SharedPreferences.Editor editor) {
editor.putString(ELEMENT_PREFERENCES_ALGORITHM_KEY, currentKeyAlgorithm.name());
editor.putString(ELEMENT_PREFERENCES_ALGORITHM_STORAGE, currentStorageAlgorithm.name());
}
public void removeCurrentAlgorithms(SharedPreferences.Editor editor) {
editor.remove(ELEMENT_PREFERENCES_ALGORITHM_KEY);
editor.remove(ELEMENT_PREFERENCES_ALGORITHM_STORAGE);
}
}
NOTE THERE IS A METHOD getFromOptionsWithDefault which is involved in selecting the enum value:
private String getFromOptionsWithDefault(Map<String, Object> options, String key, String defaultValue) {
final Object value = options.get(key);
return value != null ? value.toString() : defaultValue;
}
Conclusion
Yes, there is indeed a warning "Asymmetric cipher with insecure addition is used" on the piece of code that you threw. Well you should realise that from API version greater than 23 the good encryption algorithm RSA_ECB_OAEPwithSHA_256andMGF1Padding will be used. This can also be set manually when declaring SecureStorage:
static const _secureStorage = FlutterSecureStorage(
aOptions: AndroidOptions(
keyCipherAlgorithm:
KeyCipherAlgorithm.RSA_ECB_OAEPwithSHA_256andMGF1Padding,
),
);
Or it is easier via encryptedSharedPreferences: true, because when true, keyCipherAlgorithm will not be passed the default parameter of the old encryption algorithm:
static const _secureStorage = FlutterSecureStorage(
aOptions: AndroidOptions(
encryptedSharedPreferences: true,
),
);
Comment from the documentation for the keyCipherAlgorithm parameter:
/// If EncryptedSharedPrefences is set to false, you can select algorithm
/// that will be used to encrypt secret key.
/// By default RSA/ECB/PKCS1Padding if used.
/// Newer RSA/ECB/OAEPWithSHA-256AndMGF1Padding is available from Android 6.
/// Plugin will fall back to default algorithm in previous system versions.
I'm sorry, but I'm having this problem with my app as well.
I've tried to make the settings as mentioned here, but it seems that AppSweep doesn't understand this and keeps accusing the security flaw.
I think this can only be solved by removing this part of the package code.
This issue is stale because it has been open for 60 days with no activity.
This issue will be tracked in #769