OWASP कैटगरी: MASVS-CRYPTO: क्रिप्टोग्राफ़ी
खास जानकारी
डेवलपर, मज़बूत एल्गोरिदम का इस्तेमाल करके डेटा की गोपनीयता और अखंडता को सुरक्षित रखने के लिए क्रिप्टोग्राफ़ी का इस्तेमाल करते हैं. हालांकि, मुख्य स्टोरेज का इस्तेमाल अक्सर कम किया जाता है. साथ ही, उन्हें कोड में स्ट्रिंग या बाइट ऐरे के तौर पर या strings.xml जैसी ऐसेट फ़ाइल में हार्डकोड किया जाता है. अगर ऐप्लिकेशन की किसी भी फ़ाइल में सीक्रेट का पता चलता है, तो यह Kerchoff के सिद्धांत के ख़िलाफ़ है. साथ ही, सुरक्षा मॉडल को टूटा हुआ माना जा सकता है.
असर
रिवर्स इंजीनियरिंग टूल का ऐक्सेस रखने वाला कोई भी हमलावर, हार्ड-कोड किए गए सीक्रेट को बहुत आसानी से वापस पा सकता है. शर्तों के आधार पर, इसका असर अलग-अलग हो सकता है. हालांकि, कई मामलों में इससे सुरक्षा से जुड़ी गंभीर समस्याएं हो सकती हैं. जैसे, संवेदनशील डेटा का ऐक्सेस.
जोखिम कम करने के तरीके
इस समस्या को कम करने के लिए, सिस्टम-वाइड क्रेडेंशियल का इस्तेमाल करते समय KeyChain API का इस्तेमाल करें. इसके अलावा, Android Keystore प्रोवाइडर का इस्तेमाल करके, किसी ऐप्लिकेशन को अपने क्रेडेंशियल सेव करने की अनुमति दें. इन क्रेडेंशियल को सिर्फ़ ऐप्लिकेशन ही ऐक्सेस कर सकता है.
नीचे दिए गए कोड स्निपेट में, KeyStore का इस्तेमाल करके सिमेट्रिक कुंजी को सेव करने और इस्तेमाल करने का तरीका दिखाया गया है:
Kotlin
private val ANDROID_KEY_STORE_PROVIDER = "AndroidKeyStore"
private val ANDROID_KEY_STORE_ALIAS = "AES_KEY_DEMO"
@Throws(
KeyStoreException::class,
NoSuchAlgorithmException::class,
NoSuchProviderException::class,
InvalidAlgorithmParameterException::class
)
private fun createAndStoreSecretKey() {
val builder: KeyGenParameterSpec.Builder = KeyGenParameterSpec.Builder(
ANDROID_KEY_STORE_ALIAS,
KeyProperties.PURPOSE_ENCRYPT or KeyProperties.PURPOSE_DECRYPT
)
val keySpec: KeyGenParameterSpec = builder
.setKeySize(256)
.setBlockModes(KeyProperties.BLOCK_MODE_GCM)
.setEncryptionPaddings(KeyProperties.ENCRYPTION_PADDING_NONE)
.setRandomizedEncryptionRequired(true)
.build()
val aesKeyGenerator: KeyGenerator =
KeyGenerator.getInstance(KeyProperties.KEY_ALGORITHM_AES, ANDROID_KEY_STORE_PROVIDER)
aesKeyGenerator.init(keySpec)
val key: SecretKey = aesKeyGenerator.generateKey()
}
@Throws(
KeyStoreException::class,
UnrecoverableEntryException::class,
NoSuchAlgorithmException::class,
CertificateException::class,
IOException::class,
NoSuchPaddingException::class,
InvalidKeyException::class,
IllegalBlockSizeException::class,
BadPaddingException::class
)
private fun encryptWithKeyStore(plainText: String): ByteArray? {
// Initialize KeyStore
val keyStore: KeyStore = KeyStore.getInstance(ANDROID_KEY_STORE_PROVIDER)
keyStore.load(null)
// Retrieve the key with alias androidKeyStoreAlias created before
val keyEntry: KeyStore.SecretKeyEntry =
keyStore.getEntry(ANDROID_KEY_STORE_ALIAS, null) as KeyStore.SecretKeyEntry
val key: SecretKey = keyEntry.secretKey
// Use the secret key at your convenience
val cipher: Cipher = Cipher.getInstance("AES/GCM/NoPadding")
cipher.init(Cipher.ENCRYPT_MODE, key)
return cipher.doFinal(plainText.toByteArray())
}
Java
static private final String ANDROID_KEY_STORE_PROVIDER = "AndroidKeyStore";
static private final String ANDROID_KEY_STORE_ALIAS = "AES_KEY_DEMO";
private void createAndStoreSecretKey() throws KeyStoreException, NoSuchAlgorithmException, NoSuchProviderException, InvalidAlgorithmParameterException {
KeyGenParameterSpec.Builder builder = new KeyGenParameterSpec.Builder(
ANDROID_KEY_STORE_ALIAS,
KeyProperties.PURPOSE_ENCRYPT | KeyProperties.PURPOSE_DECRYPT);
KeyGenParameterSpec keySpec = builder
.setKeySize(256)
.setBlockModes(KeyProperties.BLOCK_MODE_GCM)
.setEncryptionPaddings(KeyProperties.ENCRYPTION_PADDING_NONE)
.setRandomizedEncryptionRequired(true)
.build();
KeyGenerator aesKeyGenerator = KeyGenerator.getInstance(KeyProperties.KEY_ALGORITHM_AES, ANDROID_KEY_STORE_PROVIDER);
aesKeyGenerator.init(keySpec);
SecretKey key = aesKeyGenerator.generateKey();
}
private byte[] encryptWithKeyStore(final String plainText) throws KeyStoreException, UnrecoverableEntryException, NoSuchAlgorithmException, CertificateException, IOException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException {
// Initialize KeyStore
KeyStore keyStore = KeyStore.getInstance(ANDROID_KEY_STORE_PROVIDER);
keyStore.load(null);
// Retrieve the key with alias ANDROID_KEY_STORE_ALIAS created before
KeyStore.SecretKeyEntry keyEntry = (KeyStore.SecretKeyEntry) keyStore.getEntry(ANDROID_KEY_STORE_ALIAS, null);
SecretKey key = keyEntry.getSecretKey();
// Use the secret key at your convenience
final Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding");
cipher.init(Cipher.ENCRYPT_MODE, key);
return cipher.doFinal(plainText.getBytes());
}