| 1 | package io.github.leexeo.cage.mobile; |
| 2 | |
| 3 | import android.content.SharedPreferences; |
| 4 | import android.security.keystore.KeyGenParameterSpec; |
| 5 | import android.security.keystore.KeyProperties; |
| 6 | import android.util.Base64; |
| 7 | import java.io.IOException; |
| 8 | import java.nio.ByteBuffer; |
| 9 | import java.nio.charset.StandardCharsets; |
| 10 | import java.security.KeyStore; |
| 11 | import java.util.Arrays; |
| 12 | import java.security.MessageDigest; |
| 13 | import javax.crypto.Cipher; |
| 14 | import javax.crypto.KeyGenerator; |
| 15 | import javax.crypto.SecretKey; |
| 16 | import javax.crypto.spec.GCMParameterSpec; |
| 17 | |
| 18 | |
| 19 | |
| 20 | |
| 21 | final class HostCredentials { |
| 22 | private static final String ALIAS = "cage-mobile-saved-host-aes-v1"; |
| 23 | private static final String SEALED = "sealed-lookup-v1"; |
| 24 | private final SharedPreferences prefs; |
| 25 | HostCredentials(SharedPreferences prefs) { this.prefs = prefs; } |
| 26 | boolean saved() { return prefs.contains(SEALED); } |
| 27 | boolean matches(String relay, String pin) { return relay.equals(prefs.getString("relay", "")) && pin.equals(prefs.getString("pin", "")); } |
| 28 | private static String entry(String relay, String pin) throws Exception { |
| 29 | byte[] digest = MessageDigest.getInstance("SHA-256").digest(context(relay, pin)); |
| 30 | return "host-lookup-v2-" + Base64.encodeToString(digest, Base64.NO_WRAP | Base64.URL_SAFE); |
| 31 | } |
| 32 | |
| 33 | private static KeyStore store() throws Exception { |
| 34 | KeyStore store = KeyStore.getInstance("AndroidKeyStore"); |
| 35 | store.load(null); |
| 36 | return store; |
| 37 | } |
| 38 | private SecretKey key(boolean create) throws Exception { |
| 39 | KeyStore store = store(); |
| 40 | if (store.containsAlias(ALIAS)) return (SecretKey) store.getKey(ALIAS, null); |
| 41 | if (!create) throw new IOException("设备凭据不可用。"); |
| 42 | KeyGenerator generator = KeyGenerator.getInstance(KeyProperties.KEY_ALGORITHM_AES, "AndroidKeyStore"); |
| 43 | generator.init(new KeyGenParameterSpec.Builder(ALIAS, KeyProperties.PURPOSE_ENCRYPT | KeyProperties.PURPOSE_DECRYPT) |
| 44 | .setKeySize(256).setBlockModes(KeyProperties.BLOCK_MODE_GCM) |
| 45 | .setEncryptionPaddings(KeyProperties.ENCRYPTION_PADDING_NONE).build()); |
| 46 | return generator.generateKey(); |
| 47 | } |
| 48 | private static byte[] context(String relay, String pin) { |
| 49 | return ("Cage saved host v1\n" + relay + "\n" + pin).getBytes(StandardCharsets.UTF_8); |
| 50 | } |
| 51 | synchronized void save(String relay, String pin, String lookup) throws Exception { |
| 52 | SharedPreferences.Editor change = prefs.edit().putString("relay", relay).putString("pin", pin); |
| 53 | String previousRelay = prefs.getString("relay", ""), previousPin = prefs.getString("pin", ""); |
| 54 | if (prefs.contains(SEALED)) change.putString(entry(previousRelay, previousPin), prefs.getString(SEALED, "")); |
| 55 | if (lookup != null) { |
| 56 | Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding"); |
| 57 | cipher.init(Cipher.ENCRYPT_MODE, key(true)); |
| 58 | cipher.updateAAD(context(relay, pin)); |
| 59 | byte[] plain = lookup.getBytes(StandardCharsets.UTF_8); |
| 60 | byte[] encrypted; |
| 61 | try { encrypted = cipher.doFinal(plain); } finally { Arrays.fill(plain, (byte) 0); } |
| 62 | String value = Base64.encodeToString(cipher.getIV(), Base64.NO_WRAP) + "." + Base64.encodeToString(encrypted, Base64.NO_WRAP); |
| 63 | change.putString(SEALED, value).putString(entry(relay, pin), value); |
| 64 | } else if (!matches(relay, pin)) { |
| 65 | String saved = prefs.getString(entry(relay, pin), null); |
| 66 | if (saved == null) change.remove(SEALED); else change.putString(SEALED, saved); |
| 67 | } |
| 68 | if (!change.commit()) throw new IOException("无法保存主机。"); |
| 69 | } |
| 70 | synchronized String lookup(String relay, String pin) throws Exception { |
| 71 | String value = matches(relay,pin) ? prefs.getString(SEALED, "") : prefs.getString(entry(relay, pin), ""); |
| 72 | String[] parts = value.split("\\.", -1); |
| 73 | if (parts.length != 2) throw new IOException("无可用重连凭据。"); |
| 74 | Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding"); |
| 75 | cipher.init(Cipher.DECRYPT_MODE, key(false), new GCMParameterSpec(128, Base64.decode(parts[0], Base64.NO_WRAP))); |
| 76 | cipher.updateAAD(context(relay, pin)); |
| 77 | byte[] plain = cipher.doFinal(Base64.decode(parts[1], Base64.NO_WRAP)); |
| 78 | try { |
| 79 | String token = StandardCharsets.UTF_8.newDecoder().decode(ByteBuffer.wrap(plain)).toString(); |
| 80 | if (!Invitation.capability(token)) throw new IOException("重连凭据无效。"); |
| 81 | return token; |
| 82 | } finally { Arrays.fill(plain, (byte) 0); } |
| 83 | } |
| 84 | synchronized void forget() throws Exception { |
| 85 | |
| 86 | if (!prefs.edit().remove(entry(prefs.getString("relay", ""), prefs.getString("pin", ""))) |
| 87 | .remove(SEALED).remove("relay").remove("pin").commit()) throw new IOException("无法移除主机。"); |
| 88 | } |
| 89 | } |