개발자는 데이터의 기밀성과 무결성을 지키기 위해 강력한 알고리즘의 암호화를 사용합니다. 그러나 키 저장소는 자주 사용되지 않는 경우가 많으며 일반적으로 애플리케이션의 코드나 애셋 파일(예: strings.xml)에 문자열이나 바이트 배열로 하드코딩되어 있습니다. 앱의 어느 파일에서든 보안 비밀이 노출되면 Kerchoff 원칙을 위반하는 것이며 보안 모델이 손상된 것으로 간주될 수 있습니다.
영향
리버스 엔지니어링 도구에 액세스할 수 있는 공격자는 하드 코딩된 보안 비밀을 매우 쉽게 가져올 수 있습니다. 조건에 따라 영향은 다양할 수 있지만 대부분의 경우 주요 보안 문제(예: 민감한 정보에 액세스)가 발생합니다.
완화 조치
이 문제를 완화하려면 시스템 전체 사용자 인증 정보를 원하는 경우 KeyChain API를 사용하거나, Android KeyStore 제공자를 사용하여 개별 앱이 앱 자체에서만 액세스할 수 있는 자체 사용자 인증 정보를 저장하도록 하는 것이 좋습니다.
다음 코드 스니펫은 KeyStore를 사용하여 대칭 키를 저장하고 사용하는 방법을 보여줍니다.
Kotlin
privatevalANDROID_KEY_STORE_PROVIDER="AndroidKeyStore"privatevalANDROID_KEY_STORE_ALIAS="AES_KEY_DEMO"@Throws(KeyStoreException::class,NoSuchAlgorithmException::class,NoSuchProviderException::class,InvalidAlgorithmParameterException::class)privatefuncreateAndStoreSecretKey(){valbuilder:KeyGenParameterSpec.Builder=KeyGenParameterSpec.Builder(ANDROID_KEY_STORE_ALIAS,KeyProperties.PURPOSE_ENCRYPTorKeyProperties.PURPOSE_DECRYPT)valkeySpec:KeyGenParameterSpec=builder.setKeySize(256).setBlockModes(KeyProperties.BLOCK_MODE_GCM).setEncryptionPaddings(KeyProperties.ENCRYPTION_PADDING_NONE).setRandomizedEncryptionRequired(true).build()valaesKeyGenerator:KeyGenerator=KeyGenerator.getInstance(KeyProperties.KEY_ALGORITHM_AES,ANDROID_KEY_STORE_PROVIDER)aesKeyGenerator.init(keySpec)valkey:SecretKey=aesKeyGenerator.generateKey()}@Throws(KeyStoreException::class,UnrecoverableEntryException::class,NoSuchAlgorithmException::class,CertificateException::class,IOException::class,NoSuchPaddingException::class,InvalidKeyException::class,IllegalBlockSizeException::class,BadPaddingException::class)privatefunencryptWithKeyStore(plainText:String):ByteArray? {// Initialize KeyStorevalkeyStore:KeyStore=KeyStore.getInstance(ANDROID_KEY_STORE_PROVIDER)keyStore.load(null)// Retrieve the key with alias androidKeyStoreAlias created beforevalkeyEntry:KeyStore.SecretKeyEntry=keyStore.getEntry(ANDROID_KEY_STORE_ALIAS,null)asKeyStore.SecretKeyEntryvalkey:SecretKey=keyEntry.secretKey// Use the secret key at your conveniencevalcipher:Cipher=Cipher.getInstance("AES/GCM/NoPadding")cipher.init(Cipher.ENCRYPT_MODE,key)returncipher.doFinal(plainText.toByteArray())}
Java
staticprivatefinalStringANDROID_KEY_STORE_PROVIDER="AndroidKeyStore";staticprivatefinalStringANDROID_KEY_STORE_ALIAS="AES_KEY_DEMO";privatevoidcreateAndStoreSecretKey()throwsKeyStoreException,NoSuchAlgorithmException,NoSuchProviderException,InvalidAlgorithmParameterException{KeyGenParameterSpec.Builderbuilder=newKeyGenParameterSpec.Builder(ANDROID_KEY_STORE_ALIAS,KeyProperties.PURPOSE_ENCRYPT|KeyProperties.PURPOSE_DECRYPT);KeyGenParameterSpeckeySpec=builder.setKeySize(256).setBlockModes(KeyProperties.BLOCK_MODE_GCM).setEncryptionPaddings(KeyProperties.ENCRYPTION_PADDING_NONE).setRandomizedEncryptionRequired(true).build();KeyGeneratoraesKeyGenerator=KeyGenerator.getInstance(KeyProperties.KEY_ALGORITHM_AES,ANDROID_KEY_STORE_PROVIDER);aesKeyGenerator.init(keySpec);SecretKeykey=aesKeyGenerator.generateKey();}privatebyte[]encryptWithKeyStore(finalStringplainText)throwsKeyStoreException,UnrecoverableEntryException,NoSuchAlgorithmException,CertificateException,IOException,NoSuchPaddingException,InvalidKeyException,IllegalBlockSizeException,BadPaddingException{// Initialize KeyStoreKeyStorekeyStore=KeyStore.getInstance(ANDROID_KEY_STORE_PROVIDER);keyStore.load(null);// Retrieve the key with alias ANDROID_KEY_STORE_ALIAS created beforeKeyStore.SecretKeyEntrykeyEntry=(KeyStore.SecretKeyEntry)keyStore.getEntry(ANDROID_KEY_STORE_ALIAS,null);SecretKeykey=keyEntry.getSecretKey();// Use the secret key at your conveniencefinalCiphercipher=Cipher.getInstance("AES/GCM/NoPadding");cipher.init(Cipher.ENCRYPT_MODE,key);returncipher.doFinal(plainText.getBytes());}
이 페이지에 나와 있는 콘텐츠와 코드 샘플에는 콘텐츠 라이선스에서 설명하는 라이선스가 적용됩니다. 자바 및 OpenJDK는 Oracle 및 Oracle 계열사의 상표 또는 등록 상표입니다.
최종 업데이트: 2023-12-08(UTC)
[[["이해하기 쉬움","easyToUnderstand","thumb-up"],["문제가 해결됨","solvedMyProblem","thumb-up"],["기타","otherUp","thumb-up"]],[["필요한 정보가 없음","missingTheInformationINeed","thumb-down"],["너무 복잡함/단계 수가 너무 많음","tooComplicatedTooManySteps","thumb-down"],["오래됨","outOfDate","thumb-down"],["번역 문제","translationIssue","thumb-down"],["샘플/코드 문제","samplesCodeIssue","thumb-down"],["기타","otherDown","thumb-down"]],["최종 업데이트: 2023-12-08(UTC)"],[],[],null,["# Hardcoded Cryptographic Secrets\n\n\u003cbr /\u003e\n\n**OWASP category:** [MASVS-CRYPTO: Cryptography](https://mas.owasp.org/MASVS/06-MASVS-CRYPTO)\n\nOverview\n--------\n\n| **Note:** This article isn't focused on how to protect API keys.\n\nDevelopers use cryptography to protect confidentiality and integrity of data using robust algorithms. However, the key storage is often underused, and it's common to find them hardcoded into the application as a string or byte array in the code or in an asset file such as `strings.xml`. If secrets are exposed in any files of the app, this goes against [Kerchoff's principle](https://en.wikipedia.org/wiki/Kerckhoffs%27s_principle) and the security model can be considered as broken.\n\nImpact\n------\n\nAn attacker with access to reverse engineering tools can retrieve a hard-coded secret very easily. Depending on conditions the impact might vary, but in many cases it leads to major security issues, such as access to sensitive data.\n\nMitigations\n-----------\n\nTo mitigate this issue, consider using the [KeyChain](/reference/android/security/KeyChain) API when you want system-wide credentials, or the [Android Keystore](/training/articles/keystore) provider to let an individual app store its own credentials that only the app itself can access.\n\nThe following code snippet shows how to store and use a symmetric key using\n`KeyStore`: \n\n### Kotlin\n\n private val ANDROID_KEY_STORE_PROVIDER = \"AndroidKeyStore\"\n private val ANDROID_KEY_STORE_ALIAS = \"AES_KEY_DEMO\"\n\n @Throws(\n KeyStoreException::class,\n NoSuchAlgorithmException::class,\n NoSuchProviderException::class,\n InvalidAlgorithmParameterException::class\n )\n private fun createAndStoreSecretKey() {\n val builder: KeyGenParameterSpec.Builder = KeyGenParameterSpec.Builder(\n ANDROID_KEY_STORE_ALIAS,\n KeyProperties.PURPOSE_ENCRYPT or KeyProperties.PURPOSE_DECRYPT\n )\n val keySpec: KeyGenParameterSpec = builder\n .setKeySize(256)\n .setBlockModes(KeyProperties.BLOCK_MODE_GCM)\n .setEncryptionPaddings(KeyProperties.ENCRYPTION_PADDING_NONE)\n .setRandomizedEncryptionRequired(true)\n .build()\n val aesKeyGenerator: KeyGenerator =\n KeyGenerator.getInstance(KeyProperties.KEY_ALGORITHM_AES, ANDROID_KEY_STORE_PROVIDER)\n aesKeyGenerator.init(keySpec)\n val key: SecretKey = aesKeyGenerator.generateKey()\n }\n\n @Throws(\n KeyStoreException::class,\n UnrecoverableEntryException::class,\n NoSuchAlgorithmException::class,\n CertificateException::class,\n IOException::class,\n NoSuchPaddingException::class,\n InvalidKeyException::class,\n IllegalBlockSizeException::class,\n BadPaddingException::class\n )\n private fun encryptWithKeyStore(plainText: String): ByteArray? {\n // Initialize KeyStore\n val keyStore: KeyStore = KeyStore.getInstance(ANDROID_KEY_STORE_PROVIDER)\n keyStore.load(null)\n // Retrieve the key with alias androidKeyStoreAlias created before\n val keyEntry: KeyStore.SecretKeyEntry =\n keyStore.getEntry(ANDROID_KEY_STORE_ALIAS, null) as KeyStore.SecretKeyEntry\n val key: SecretKey = keyEntry.secretKey\n // Use the secret key at your convenience\n val cipher: Cipher = Cipher.getInstance(\"AES/GCM/NoPadding\")\n cipher.init(Cipher.ENCRYPT_MODE, key)\n return cipher.doFinal(plainText.toByteArray())\n }\n\n### Java\n\n static private final String ANDROID_KEY_STORE_PROVIDER = \"AndroidKeyStore\";\n static private final String ANDROID_KEY_STORE_ALIAS = \"AES_KEY_DEMO\";\n\n private void createAndStoreSecretKey() throws KeyStoreException, NoSuchAlgorithmException, NoSuchProviderException, InvalidAlgorithmParameterException {\n KeyGenParameterSpec.Builder builder = new KeyGenParameterSpec.Builder(\n ANDROID_KEY_STORE_ALIAS,\n KeyProperties.PURPOSE_ENCRYPT | KeyProperties.PURPOSE_DECRYPT);\n KeyGenParameterSpec keySpec = builder\n .setKeySize(256)\n .setBlockModes(KeyProperties.BLOCK_MODE_GCM)\n .setEncryptionPaddings(KeyProperties.ENCRYPTION_PADDING_NONE)\n .setRandomizedEncryptionRequired(true)\n .build();\n KeyGenerator aesKeyGenerator = KeyGenerator.getInstance(KeyProperties.KEY_ALGORITHM_AES, ANDROID_KEY_STORE_PROVIDER);\n aesKeyGenerator.init(keySpec);\n SecretKey key = aesKeyGenerator.generateKey();\n }\n\n private byte[] encryptWithKeyStore(final String plainText) throws KeyStoreException, UnrecoverableEntryException, NoSuchAlgorithmException, CertificateException, IOException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException {\n // Initialize KeyStore\n KeyStore keyStore = KeyStore.getInstance(ANDROID_KEY_STORE_PROVIDER);\n keyStore.load(null);\n // Retrieve the key with alias ANDROID_KEY_STORE_ALIAS created before\n KeyStore.SecretKeyEntry keyEntry = (KeyStore.SecretKeyEntry) keyStore.getEntry(ANDROID_KEY_STORE_ALIAS, null);\n SecretKey key = keyEntry.getSecretKey();\n // Use the secret key at your convenience\n final Cipher cipher = Cipher.getInstance(\"AES/GCM/NoPadding\");\n cipher.init(Cipher.ENCRYPT_MODE, key);\n return cipher.doFinal(plainText.getBytes());\n }\n\nResources\n---------\n\n- [Android KeyStore system](/training/articles/keystore)\n\n- [KeyStore documentation](/reference/java/security/KeyStore)\n\n- [KeyChain documentation](/reference/android/security/KeyChain)\n\n- [CWE-321: Use of Hard-coded Cryptographic Key](https://cwe.mitre.org/data/definitions/321.html)"]]