跳到主要内容

Claude Cage · 面向 Claude 的隔离开发环境,支持桌面工作台、多账号、远程部署与移动端连接。上游 Lee-xeo/Claude-Cage 完整源码镜像,非灵栈原创;主许可 MIT,第三方组件遵循各自许可。来源:https://github.com/Lee-xeo/Claude-Cage 。手动同步,不附带 Release 安装包。

浏览文件

HostCredentials.java

89 行 · 5.0 KB历史下载
1package io.github.leexeo.cage.mobile;
2
3import android.content.SharedPreferences;
4import android.security.keystore.KeyGenParameterSpec;
5import android.security.keystore.KeyProperties;
6import android.util.Base64;
7import java.io.IOException;
8import java.nio.ByteBuffer;
9import java.nio.charset.StandardCharsets;
10import java.security.KeyStore;
11import java.util.Arrays;
12import java.security.MessageDigest;
13import javax.crypto.Cipher;
14import javax.crypto.KeyGenerator;
15import javax.crypto.SecretKey;
16import javax.crypto.spec.GCMParameterSpec;
17
18// The public address and verified fingerprint also authenticate the ciphertext.
19// Keep the AES key separate from the P-256 device identity so forgetting a host
20// leaves the device fingerprint intact.
21final 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 // Other computers share the encryption key. Forget only the selected host.
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}