LifeMate — Personal, Family & Learning Companion
feat/local-signing-preflight
chore/phase1-repository-cleanup
deploy/stable-web-apk
deployment-static
design/mobile-accessibility-v2
design/mobile-navigation-review
docs/child-education-access-decision
docs/offline-data-rescue-options
docs/owner-guide-and-delivery-plan
docs/phase1-policy-options
docs/public-review-live-2026-10-15
feat/aparat-learning-library
feat/child-device-safety
feat/child-identity-schema
feat/flutter-mobile-navigation
feat/learning-content-operations
feat/local-signing-preflight
feat/online-family-stage0
feat/personal-encrypted-recovery
feat/private-planner-series-api
feat/public-review-page
feat/standalone-weekly-planner
feat/weekly-planner-android
final-feature-1/iran-family-school
fix/auth-abuse-expiry-safety
fix/auth-expiry-after-lock
fix/backend-request-safety
fix/lifeguide-bootstrap-naming-ci
fix/lifeguide-pages-qa-base-preflight
fix/lifeguide-qa-pages-env-deployment
fix/local-task-persistence
fix/managed-child-session-boundary
fix/personal-jalali-dates
fix/personal-notification-controls
fix/personal-upgrade-delivery-guard
fix/sms-approval-gates
fix/study-duration-display
infra/cloudflare-neon-migration
infra/lifeguide-cloudflare-qa
infra/lifeguide-github-pages-qa
infra/lifeguide-supabase-qa
main
phase-1/foundation-completion
phase-2/product-foundation-identity
phase-3/planner-school-family-core
phase-4/ai-learning-wellbeing
phase-5/hardening-observability-release
test/android-reminder-acceptance
test/personal-notification-settings
test/personal-recovery-acceptance
test/role-entry-apk
| 1 | import java.io.ByteArrayInputStream; |
| 2 | import java.io.DataInputStream; |
| 3 | import java.io.IOException; |
| 4 | import java.nio.ByteBuffer; |
| 5 | import java.nio.CharBuffer; |
| 6 | import java.nio.charset.CharacterCodingException; |
| 7 | import java.nio.charset.CodingErrorAction; |
| 8 | import java.nio.charset.StandardCharsets; |
| 9 | import java.security.KeyStore; |
| 10 | import java.security.MessageDigest; |
| 11 | import java.security.PrivateKey; |
| 12 | import java.security.SecureRandom; |
| 13 | import java.security.Signature; |
| 14 | import java.security.cert.CertificateExpiredException; |
| 15 | import java.security.cert.CertificateNotYetValidException; |
| 16 | import java.security.cert.X509Certificate; |
| 17 | import java.util.Arrays; |
| 18 | import java.util.HexFormat; |
| 19 | import javax.naming.NamingEnumeration; |
| 20 | import javax.naming.directory.Attribute; |
| 21 | import javax.naming.ldap.LdapName; |
| 22 | import javax.naming.ldap.Rdn; |
| 23 | |
| 24 | /** Reads one bounded stdin frame; emits only public facts or a fixed refusal. */ |
| 25 | public final class LifeGuideKeyInspector { |
| 26 | private static final int MAX_STORE_BYTES = 2 * 1024 * 1024; |
| 27 | |
| 28 | private static final class Refusal extends Exception { |
| 29 | final String reason; |
| 30 | Refusal(String reason) { this.reason = reason; } |
| 31 | } |
| 32 | |
| 33 | private static final class Request implements AutoCloseable { |
| 34 | byte[] keystore; |
| 35 | byte[] aliasBytes; |
| 36 | byte[] storePasswordBytes; |
| 37 | byte[] keyPasswordBytes; |
| 38 | String alias; |
| 39 | char[] storePassword; |
| 40 | char[] keyPassword; |
| 41 | |
| 42 | public void close() { |
| 43 | wipe(keystore); |
| 44 | wipe(aliasBytes); |
| 45 | wipe(storePasswordBytes); |
| 46 | wipe(keyPasswordBytes); |
| 47 | if (storePassword != null) Arrays.fill(storePassword, '\0'); |
| 48 | if (keyPassword != null) Arrays.fill(keyPassword, '\0'); |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | private static void wipe(byte[] bytes) { |
| 53 | if (bytes != null) Arrays.fill(bytes, (byte) 0); |
| 54 | } |
| 55 | |
| 56 | private static byte[] readField(DataInputStream input, int maximum) throws IOException, Refusal { |
| 57 | int length = input.readInt(); |
| 58 | // Negative signed values represent unsigned lengths above 2 GiB. |
| 59 | if (length < 1 || length > maximum) throw new Refusal("invalid_request"); |
| 60 | byte[] bytes = new byte[length]; |
| 61 | try { |
| 62 | input.readFully(bytes); |
| 63 | return bytes; |
| 64 | } catch (IOException exception) { |
| 65 | wipe(bytes); |
| 66 | throw exception; |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | private static char[] decodeUtf8(byte[] bytes) throws CharacterCodingException { |
| 71 | CharBuffer decoded = StandardCharsets.UTF_8.newDecoder() |
| 72 | .onMalformedInput(CodingErrorAction.REPORT) |
| 73 | .onUnmappableCharacter(CodingErrorAction.REPORT) |
| 74 | .decode(ByteBuffer.wrap(bytes)); |
| 75 | try { |
| 76 | char[] characters = new char[decoded.remaining()]; |
| 77 | decoded.get(characters); |
| 78 | return characters; |
| 79 | } finally { |
| 80 | if (decoded.hasArray()) Arrays.fill(decoded.array(), '\0'); |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | private static char[] decodePassword(byte[] bytes) throws Refusal, CharacterCodingException { |
| 85 | char[] characters = decodeUtf8(bytes); |
| 86 | for (char character : characters) { |
| 87 | if (character == '\0' || character == '\r' || character == '\n') { |
| 88 | Arrays.fill(characters, '\0'); |
| 89 | throw new Refusal("invalid_request"); |
| 90 | } |
| 91 | } |
| 92 | return characters; |
| 93 | } |
| 94 | |
| 95 | private static Request readRequest() throws Refusal { |
| 96 | Request request = new Request(); |
| 97 | try { |
| 98 | DataInputStream input = new DataInputStream(System.in); |
| 99 | if (input.readInt() != 0x4c474b49) throw new Refusal("invalid_request"); |
| 100 | request.keystore = readField(input, MAX_STORE_BYTES); |
| 101 | request.aliasBytes = readField(input, 256); |
| 102 | request.storePasswordBytes = readField(input, 4096); |
| 103 | request.keyPasswordBytes = readField(input, 4096); |
| 104 | if (input.read() != -1) throw new Refusal("invalid_request"); |
| 105 | char[] alias = decodeUtf8(request.aliasBytes); |
| 106 | try { |
| 107 | request.alias = new String(alias); |
| 108 | } finally { |
| 109 | Arrays.fill(alias, '\0'); |
| 110 | } |
| 111 | request.storePassword = decodePassword(request.storePasswordBytes); |
| 112 | request.keyPassword = decodePassword(request.keyPasswordBytes); |
| 113 | wipe(request.storePasswordBytes); |
| 114 | wipe(request.keyPasswordBytes); |
| 115 | return request; |
| 116 | } catch (IOException | Refusal exception) { |
| 117 | request.close(); |
| 118 | throw new Refusal("invalid_request"); |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | private static KeyStore loadStore(Request request) throws Refusal { |
| 123 | for (String format : new String[] {"JKS", "PKCS12"}) { |
| 124 | try { |
| 125 | KeyStore store = KeyStore.getInstance(format); |
| 126 | store.load(new ByteArrayInputStream(request.keystore), request.storePassword); |
| 127 | return store; |
| 128 | } catch (Exception exception) { |
| 129 | // Try only the two supported formats, without exposing provider errors. |
| 130 | } |
| 131 | } |
| 132 | throw new Refusal("keystore_unreadable"); |
| 133 | } |
| 134 | |
| 135 | private static KeyStore.PrivateKeyEntry privateEntry(KeyStore store, Request request) throws Refusal { |
| 136 | KeyStore.PasswordProtection protection = new KeyStore.PasswordProtection(request.keyPassword); |
| 137 | try { |
| 138 | if (!store.entryInstanceOf(request.alias, KeyStore.PrivateKeyEntry.class)) { |
| 139 | throw new Refusal("private_key_unavailable"); |
| 140 | } |
| 141 | KeyStore.Entry entry = store.getEntry(request.alias, protection); |
| 142 | if (!(entry instanceof KeyStore.PrivateKeyEntry)) { |
| 143 | throw new Refusal("private_key_unavailable"); |
| 144 | } |
| 145 | return (KeyStore.PrivateKeyEntry) entry; |
| 146 | } catch (Exception exception) { |
| 147 | throw new Refusal("private_key_unavailable"); |
| 148 | } finally { |
| 149 | try { protection.destroy(); } catch (Exception ignored) { } |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | private static void proveKey(PrivateKey key, X509Certificate certificate) throws Refusal { |
| 154 | String algorithm; |
| 155 | switch (key.getAlgorithm()) { |
| 156 | case "RSA": algorithm = "SHA256withRSA"; break; |
| 157 | case "EC": algorithm = "SHA256withECDSA"; break; |
| 158 | default: throw new Refusal("unsupported_key_algorithm"); |
| 159 | } |
| 160 | byte[] challenge = new byte[32]; |
| 161 | byte[] proof = null; |
| 162 | try { |
| 163 | new SecureRandom().nextBytes(challenge); |
| 164 | Signature signer = Signature.getInstance(algorithm); |
| 165 | signer.initSign(key); |
| 166 | signer.update(challenge); |
| 167 | proof = signer.sign(); |
| 168 | Signature verifier = Signature.getInstance(algorithm); |
| 169 | verifier.initVerify(certificate.getPublicKey()); |
| 170 | verifier.update(challenge); |
| 171 | if (!verifier.verify(proof)) throw new Refusal("key_proof_failed"); |
| 172 | } catch (Exception exception) { |
| 173 | throw new Refusal("key_proof_failed"); |
| 174 | } finally { |
| 175 | wipe(challenge); |
| 176 | wipe(proof); |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | private static boolean debugCertificate(X509Certificate certificate) throws Exception { |
| 181 | LdapName name = new LdapName(certificate.getSubjectX500Principal().getName()); |
| 182 | for (Rdn rdn : name.getRdns()) { |
| 183 | // A multi-valued RDN's primary type may be C/OU even when it has CN. |
| 184 | NamingEnumeration<? extends Attribute> attributes = rdn.toAttributes().getAll(); |
| 185 | try { |
| 186 | while (attributes.hasMore()) { |
| 187 | Attribute attribute = attributes.next(); |
| 188 | if (!"CN".equalsIgnoreCase(attribute.getID())) continue; |
| 189 | for (int index = 0; index < attribute.size(); index++) { |
| 190 | if ("Android Debug".equalsIgnoreCase(String.valueOf(attribute.get(index)))) return true; |
| 191 | } |
| 192 | } |
| 193 | } finally { |
| 194 | attributes.close(); |
| 195 | } |
| 196 | } |
| 197 | return false; |
| 198 | } |
| 199 | |
| 200 | private static String inspect(Request request) throws Exception { |
| 201 | KeyStore.PrivateKeyEntry entry = privateEntry(loadStore(request), request); |
| 202 | if (!(entry.getCertificate() instanceof X509Certificate)) throw new Refusal("inspection_failed"); |
| 203 | X509Certificate certificate = (X509Certificate) entry.getCertificate(); |
| 204 | proveKey(entry.getPrivateKey(), certificate); |
| 205 | boolean valid = true; |
| 206 | try { |
| 207 | certificate.checkValidity(); |
| 208 | } catch (CertificateExpiredException | CertificateNotYetValidException exception) { |
| 209 | valid = false; |
| 210 | } |
| 211 | String fingerprint = HexFormat.of().formatHex( |
| 212 | MessageDigest.getInstance("SHA-256").digest(certificate.getEncoded())); |
| 213 | return "{\"schemaVersion\":1,\"status\":\"INSPECTED\",\"certificateSha256\":\"" |
| 214 | + fingerprint + "\",\"debugCertificate\":" + debugCertificate(certificate) |
| 215 | + ",\"privateKeyProof\":true,\"certificateCurrentlyValid\":" + valid + "}"; |
| 216 | } |
| 217 | |
| 218 | public static void main(String[] arguments) { |
| 219 | String result; |
| 220 | int exitCode; |
| 221 | try { |
| 222 | if (arguments.length != 0) throw new Refusal("invalid_request"); |
| 223 | try (Request request = readRequest()) { |
| 224 | result = inspect(request); |
| 225 | } |
| 226 | exitCode = 0; |
| 227 | } catch (Exception | LinkageError exception) { |
| 228 | String reason = exception instanceof Refusal ? ((Refusal) exception).reason : "inspection_failed"; |
| 229 | result = "{\"schemaVersion\":1,\"status\":\"REFUSED\",\"reason\":\"" + reason + "\"}"; |
| 230 | exitCode = 2; |
| 231 | } |
| 232 | System.out.println(result); |
| 233 | System.exit(exitCode); |
| 234 | } |
| 235 | } |