qualia-dik 0.1.4

Device identity kit: P-256 device identity primitives and platform-native, hardware-backed keystores.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
// Copyright (C) 2026 Qualia2FA contributors
//
// This library is free software; you can redistribute it and/or modify it under
// the terms of the GNU Lesser General Public License as published by the Free
// Software Foundation; either version 3 of the License, or (at your option) any
// later version.
//
// This library is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
// details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with this library; if not, see <https://www.gnu.org/licenses/>.

//! The Android platform API this crate calls, as the platform declares it.
//!
//! Android's key store has no C API: this crate reaches `java.security` and
//! `android.security.keystore` over JNI, where a method is resolved by **name and
//! descriptor** at the moment of the call. A descriptor the platform does not declare is
//! therefore not a compile error — it is a `NoSuchMethodError` on a device, reported as
//! [`crate::Error::Jni`] — and the JDK's declaration of the same class is not the authority
//! on what Android declares. `SecretKeyFactory.getKeySpec` is where the two differ:
//! Java SE takes a `java.security.Key`, Android takes a `javax.crypto.SecretKey`.
//!
//! The table below transcribes the declarations this crate calls, from the Android SDK's
//! `android.jar` (`javap --system none -cp android.jar -s`, API 35; `--system none` so that
//! the JDK's own classes cannot answer in the platform's place). The tests turn each
//! transcription into a JNI descriptor — a second representation of the same fact — and hold
//! the descriptors written in `android.rs` and `android/java.rs` against the derived set, in
//! both directions.
//!
//! Test-only, and it carries no runtime code: the descriptors themselves stay next to the
//! calls that use them.

use std::collections::BTreeSet;

/// The Android backend, whose JNI descriptors are held against the platform's declarations.
const ANDROID_BACKEND: &str = include_str!("android.rs");

/// The JNI helpers of the Android backend, checked for the same reason.
const ANDROID_HELPERS: &str = include_str!("android/java.rs");

/// One method of the Android platform API this crate calls.
struct PlatformMethod {
    /// Class the method is declared on, as the platform names it.
    class: &'static str,
    /// Method name, as the platform declares it; `"<init>"` for a constructor.
    name: &'static str,
    /// Parameter types in declaration order, as Java names.
    parameters: &'static [&'static str],
    /// Return type as a Java name, or `None` for `void`.
    result: Option<&'static str>,
}

/// Builds a table row, so that the table below reads as one line per method.
const fn method(
    class: &'static str,
    name: &'static str,
    parameters: &'static [&'static str],
    result: Option<&'static str>,
) -> PlatformMethod {
    PlatformMethod {
        class,
        name,
        parameters,
        result,
    }
}

/// Every method of the Android platform API the Android backend calls.
const ANDROID_API: &[PlatformMethod] = &[
    // ── The key store itself ────────────────────────────────────────────
    method(
        "java.security.KeyStore",
        "getInstance",
        &["java.lang.String"],
        Some("java.security.KeyStore"),
    ),
    method(
        "java.security.KeyStore",
        "load",
        &["java.security.KeyStore$LoadStoreParameter"],
        None,
    ),
    method(
        "java.security.KeyStore",
        "containsAlias",
        &["java.lang.String"],
        Some("boolean"),
    ),
    method(
        "java.security.KeyStore",
        "getCertificate",
        &["java.lang.String"],
        Some("java.security.cert.Certificate"),
    ),
    method(
        "java.security.KeyStore",
        "getKey",
        &["java.lang.String", "char[]"],
        Some("java.security.Key"),
    ),
    method(
        "java.security.KeyStore",
        "setEntry",
        &[
            "java.lang.String",
            "java.security.KeyStore$Entry",
            "java.security.KeyStore$ProtectionParameter",
        ],
        None,
    ),
    method(
        "java.security.KeyStore",
        "deleteEntry",
        &["java.lang.String"],
        None,
    ),
    method(
        "java.security.KeyStore$SecretKeyEntry",
        "<init>",
        &["javax.crypto.SecretKey"],
        None,
    ),
    // ── The public half of a generated key pair ─────────────────────────
    method(
        "java.security.cert.Certificate",
        "getPublicKey",
        &[],
        Some("java.security.PublicKey"),
    ),
    method(
        "java.security.interfaces.ECPublicKey",
        "getW",
        &[],
        Some("java.security.spec.ECPoint"),
    ),
    method(
        "java.security.spec.ECPoint",
        "getAffineX",
        &[],
        Some("java.math.BigInteger"),
    ),
    method(
        "java.security.spec.ECPoint",
        "getAffineY",
        &[],
        Some("java.math.BigInteger"),
    ),
    method("java.math.BigInteger", "toByteArray", &[], Some("byte[]")),
    // ── Key material, and what protects it ──────────────────────────────
    method(
        "java.security.Key",
        "getAlgorithm",
        &[],
        Some("java.lang.String"),
    ),
    method("java.security.Key", "getEncoded", &[], Some("byte[]")),
    method(
        "java.security.KeyFactory",
        "getInstance",
        &["java.lang.String", "java.lang.String"],
        Some("java.security.KeyFactory"),
    ),
    method(
        "java.security.KeyFactory",
        "getKeySpec",
        &["java.security.Key", "java.lang.Class"],
        Some("java.security.spec.KeySpec"),
    ),
    method(
        "javax.crypto.SecretKeyFactory",
        "getInstance",
        &["java.lang.String", "java.lang.String"],
        Some("javax.crypto.SecretKeyFactory"),
    ),
    method(
        "javax.crypto.SecretKeyFactory",
        "getKeySpec",
        &["javax.crypto.SecretKey", "java.lang.Class"],
        Some("java.security.spec.KeySpec"),
    ),
    method(
        "android.security.keystore.KeyInfo",
        "isInsideSecureHardware",
        &[],
        Some("boolean"),
    ),
    // ── Generating the device identity ──────────────────────────────────
    method(
        "java.security.KeyPairGenerator",
        "getInstance",
        &["java.lang.String", "java.lang.String"],
        Some("java.security.KeyPairGenerator"),
    ),
    method(
        "java.security.KeyPairGenerator",
        "initialize",
        &["java.security.spec.AlgorithmParameterSpec"],
        None,
    ),
    method(
        "java.security.KeyPairGenerator",
        "generateKeyPair",
        &[],
        Some("java.security.KeyPair"),
    ),
    method(
        "java.security.spec.ECGenParameterSpec",
        "<init>",
        &["java.lang.String"],
        None,
    ),
    // ── The key specification both forms are built from ─────────────────
    method(
        "android.security.keystore.KeyGenParameterSpec$Builder",
        "<init>",
        &["java.lang.String", "int"],
        None,
    ),
    method(
        "android.security.keystore.KeyGenParameterSpec$Builder",
        "setAlgorithmParameterSpec",
        &["java.security.spec.AlgorithmParameterSpec"],
        Some("android.security.keystore.KeyGenParameterSpec$Builder"),
    ),
    method(
        "android.security.keystore.KeyGenParameterSpec$Builder",
        "setDigests",
        &["java.lang.String[]"],
        Some("android.security.keystore.KeyGenParameterSpec$Builder"),
    ),
    method(
        "android.security.keystore.KeyGenParameterSpec$Builder",
        "setBlockModes",
        &["java.lang.String[]"],
        Some("android.security.keystore.KeyGenParameterSpec$Builder"),
    ),
    method(
        "android.security.keystore.KeyGenParameterSpec$Builder",
        "setEncryptionPaddings",
        &["java.lang.String[]"],
        Some("android.security.keystore.KeyGenParameterSpec$Builder"),
    ),
    method(
        "android.security.keystore.KeyGenParameterSpec$Builder",
        "setKeySize",
        &["int"],
        Some("android.security.keystore.KeyGenParameterSpec$Builder"),
    ),
    method(
        "android.security.keystore.KeyGenParameterSpec$Builder",
        "setRandomizedEncryptionRequired",
        &["boolean"],
        Some("android.security.keystore.KeyGenParameterSpec$Builder"),
    ),
    method(
        "android.security.keystore.KeyGenParameterSpec$Builder",
        "setIsStrongBoxBacked",
        &["boolean"],
        Some("android.security.keystore.KeyGenParameterSpec$Builder"),
    ),
    method(
        "android.security.keystore.KeyGenParameterSpec$Builder",
        "build",
        &[],
        Some("android.security.keystore.KeyGenParameterSpec"),
    ),
    // ── Signing a digest ────────────────────────────────────────────────
    method(
        "java.security.Signature",
        "getInstance",
        &["java.lang.String"],
        Some("java.security.Signature"),
    ),
    method(
        "java.security.Signature",
        "initSign",
        &["java.security.PrivateKey"],
        None,
    ),
    method("java.security.Signature", "update", &["byte[]"], None),
    method("java.security.Signature", "sign", &[], Some("byte[]")),
    // ── The direct form: importing the material ─────────────────────────
    method(
        "javax.crypto.spec.SecretKeySpec",
        "<init>",
        &["byte[]", "java.lang.String"],
        None,
    ),
    method(
        "android.security.keystore.KeyProtection$Builder",
        "<init>",
        &["int"],
        None,
    ),
    method(
        "android.security.keystore.KeyProtection$Builder",
        "setBlockModes",
        &["java.lang.String[]"],
        Some("android.security.keystore.KeyProtection$Builder"),
    ),
    method(
        "android.security.keystore.KeyProtection$Builder",
        "setEncryptionPaddings",
        &["java.lang.String[]"],
        Some("android.security.keystore.KeyProtection$Builder"),
    ),
    method(
        "android.security.keystore.KeyProtection$Builder",
        "build",
        &[],
        Some("android.security.keystore.KeyProtection"),
    ),
    // ── The wrapped form: a key-encryption key and an AES-GCM cipher ────
    method(
        "javax.crypto.KeyGenerator",
        "getInstance",
        &["java.lang.String", "java.lang.String"],
        Some("javax.crypto.KeyGenerator"),
    ),
    method(
        "javax.crypto.KeyGenerator",
        "init",
        &["java.security.spec.AlgorithmParameterSpec"],
        None,
    ),
    method(
        "javax.crypto.KeyGenerator",
        "generateKey",
        &[],
        Some("javax.crypto.SecretKey"),
    ),
    method(
        "javax.crypto.Cipher",
        "getInstance",
        &["java.lang.String"],
        Some("javax.crypto.Cipher"),
    ),
    method(
        "javax.crypto.Cipher",
        "init",
        &["int", "java.security.Key"],
        None,
    ),
    method(
        "javax.crypto.Cipher",
        "init",
        &[
            "int",
            "java.security.Key",
            "java.security.spec.AlgorithmParameterSpec",
        ],
        None,
    ),
    method("javax.crypto.Cipher", "getIV", &[], Some("byte[]")),
    method(
        "javax.crypto.Cipher",
        "doFinal",
        &["byte[]"],
        Some("byte[]"),
    ),
    method(
        "javax.crypto.spec.GCMParameterSpec",
        "<init>",
        &["int", "byte[]"],
        None,
    ),
    // ── Describing a failure ────────────────────────────────────────────
    method("java.lang.Class", "getName", &[], Some("java.lang.String")),
    method(
        "java.lang.Throwable",
        "getMessage",
        &[],
        Some("java.lang.String"),
    ),
];

/// The JNI descriptor the platform's declaration of `method` implies.
fn descriptor_of(method: &PlatformMethod) -> String {
    let mut descriptor = String::from("(");
    for parameter in method.parameters {
        descriptor.push_str(&type_descriptor(parameter));
    }
    descriptor.push(')');
    descriptor.push_str(&type_descriptor(method.result.unwrap_or("void")));
    descriptor
}

/// The JNI descriptor of one Java type name, arrays included.
fn type_descriptor(name: &str) -> String {
    if let Some(element) = name.strip_suffix("[]") {
        return format!("[{}", type_descriptor(element));
    }
    match name {
        "void" => "V".to_owned(),
        "boolean" => "Z".to_owned(),
        "byte" => "B".to_owned(),
        "char" => "C".to_owned(),
        "short" => "S".to_owned(),
        "int" => "I".to_owned(),
        "long" => "J".to_owned(),
        "float" => "F".to_owned(),
        "double" => "D".to_owned(),
        class => format!("L{};", class.replace('.', "/")),
    }
}

/// Length in bytes of the JNI type descriptor at the start of `text`, or `None` when there
/// is none.
fn type_length(text: &str) -> Option<usize> {
    let mut index = 0;
    while text[index..].starts_with('[') {
        index += 1;
    }
    match text[index..].chars().next()? {
        'V' | 'Z' | 'B' | 'C' | 'S' | 'I' | 'J' | 'F' | 'D' => Some(index + 1),
        'L' => {
            let end = text[index..].find(';')?;
            Some(index + end + 1)
        }
        _ => None,
    }
}

/// Whether `literal` has the shape of a JNI method descriptor.
///
/// The shape is what tells a descriptor apart from every other string in these files: it
/// begins with `(`, holds a well-formed parameter list, and ends with one return type.
fn is_descriptor(literal: &str) -> bool {
    let Some(body) = literal.strip_prefix('(') else {
        return false;
    };
    let Some((parameters, result)) = body.split_once(')') else {
        return false;
    };
    let mut rest = parameters;
    while !rest.is_empty() {
        match type_length(rest) {
            Some(length) => rest = &rest[length..],
            None => return false,
        }
    }
    type_length(result) == Some(result.len())
}

/// Every double-quoted span on `line`.
fn string_literals(line: &str) -> Vec<&str> {
    let mut literals = Vec::new();
    let mut rest = line;
    while let Some(start) = rest.find('"') {
        let after = &rest[start + 1..];
        match after.find('"') {
            Some(end) => {
                literals.push(&after[..end]);
                rest = &after[end + 1..];
            }
            None => break,
        }
    }
    literals
}

/// Every JNI method descriptor written as a literal in `source`.
///
/// Comment lines are skipped, which is enough for the `//`-only comment style this crate
/// uses; a descriptor written inside a commented-out call is therefore not read as one.
fn descriptors_in(source: &str) -> BTreeSet<String> {
    let mut found = BTreeSet::new();
    for line in source.lines() {
        if line.trim_start().starts_with("//") {
            continue;
        }
        for literal in string_literals(line) {
            if is_descriptor(literal) {
                found.insert(literal.to_owned());
            }
        }
    }
    found
}

/// Every descriptor the table implies, in the form the backend writes it.
fn declared_descriptors() -> BTreeSet<String> {
    ANDROID_API.iter().map(descriptor_of).collect()
}

/// Every descriptor the Android backend calls with.
fn used_descriptors() -> BTreeSet<String> {
    let mut used = descriptors_in(ANDROID_BACKEND);
    used.extend(descriptors_in(ANDROID_HELPERS));
    used
}

/// Every descriptor the backend calls with is one Android declares, and every declaration in
/// the table is one the backend calls with.
///
/// Both directions matter: the first has to hold or a call fails on a device with
/// `NoSuchMethodError`, and the second keeps the table from outliving the call it was
/// transcribed for.
#[test]
fn the_backend_uses_the_platforms_descriptors_and_only_those() {
    let declared = declared_descriptors();
    let used = used_descriptors();

    let undeclared: Vec<&String> = used.difference(&declared).collect();
    assert!(
        undeclared.is_empty(),
        "the Android platform declares no such method: {undeclared:?}"
    );

    let unused: Vec<&String> = declared.difference(&used).collect();
    assert!(
        unused.is_empty(),
        "the table declares methods this backend never calls: {unused:?}"
    );
}

/// The source scanner finds the calls that are there.
///
/// Without this, a scanner that silently matched nothing would leave the test above holding
/// an empty set against an empty set.
#[test]
fn the_scanner_finds_the_descriptors_that_are_in_the_sources() {
    let used = used_descriptors();

    assert!(
        used.len() > 20,
        "the Android backend calls far more than a handful of JNI methods, found {}",
        used.len()
    );
    for expected in [
        "(Ljava/lang/String;)Ljava/security/KeyStore;",
        "([Ljava/lang/String;)Landroid/security/keystore/KeyGenParameterSpec$Builder;",
        "(ILjava/security/Key;Ljava/security/spec/AlgorithmParameterSpec;)V",
        "()Ljava/security/spec/ECPoint;",
    ] {
        assert!(used.contains(expected), "the scanner missed {expected}");
    }
}

/// The symmetric key description is called with the platform's own first parameter.
///
/// Android declares `SecretKeyFactory.getKeySpec(javax.crypto.SecretKey, java.lang.Class)`
/// where Java SE declares `SecretKeyFactory.getKeySpec(java.security.Key, java.lang.Class)`.
/// The Java SE descriptor resolves on a desktop JVM and nowhere else, so it is pinned here:
/// this is the declaration that decides whether the wrapped master key store can open at all.
#[test]
fn the_symmetric_key_description_takes_a_secret_key() {
    let described = ANDROID_API
        .iter()
        .find(|method| {
            method.class == "javax.crypto.SecretKeyFactory" && method.name == "getKeySpec"
        })
        .expect("the table carries the symmetric key description");

    assert_eq!(
        descriptor_of(described),
        "(Ljavax/crypto/SecretKey;Ljava/lang/Class;)Ljava/security/spec/KeySpec;",
        "Android takes a javax.crypto.SecretKey here, Java SE a java.security.Key"
    );
    assert!(
        descriptors_in(ANDROID_BACKEND)
            .contains("(Ljavax/crypto/SecretKey;Ljava/lang/Class;)Ljava/security/spec/KeySpec;"),
        "the backend has to call it with the platform's descriptor"
    );
}