← 글 목록

notes

Android·iOS - Keystore / Keychain

토큰을 EncryptedPrefs·Keychain에 두는 이유와 최소 코드.

토큰을 SharedPreferences/UserDefaults에 평문으로 두다가 찜찜해서, 플랫폼 보안 저장소를 다시 봤다.

Android Keystore

키는 TEE/StrongBox 쪽에 두고, 암복호화만 앱에서 한다. EncryptedSharedPreferences / Jetpack Security가 래퍼.

val masterKey = MasterKey.Builder(context)
    .setKeyScheme(MasterKey.KeyScheme.AES256_GCM)
    .build()
val prefs = EncryptedSharedPreferences.create(
    context, "secure", masterKey,
    EncryptedSharedPreferences.PrefKeyEncryptionScheme.AES256_SIV,
    EncryptedSharedPreferences.PrefValueEncryptionScheme.AES256_GCM
)
prefs.edit().putString("refresh", token).apply()

기기 잠금·생체와 묶으려면 BiometricPrompt + CryptoObject.

iOS Keychain

let q: [String: Any] = [
    kSecClass as String: kSecClassGenericPassword,
    kSecAttrAccount as String: "refresh",
    kSecValueData as String: data
]
SecItemAdd(q as CFDictionary, nil)

kSecAttrAccessibleWhenUnlockedThisDeviceOnly 같은 accessibility를 명시하지 않으면 백업/복원 동작이 의도와 달라질 수 있다.

공통

액세스 토큰은 짧게, 리프레시는 보안 저장소. 로그에 찍지 않기. 로그아웃 시 Keychain/EncryptedPrefs 엔트리 삭제.