ReviewOS

also looking at this

craft-native/craft

feat(android): the secure-storage quartet — ratchet 95 -> 91

#147
Merged chris wants to merge feat/android-secure-storage into main
6 files +501 -3
packages/zig/src/android_dispatch.zigmodified+103-1
Changes to packages/zig/src/android_dispatch.zig
@@ -41,6 +41,7 @@ const system = @import("bridge_android_system.zig");
4141const clipboard = @import("bridge_android_clipboard.zig");
4242const intents = @import("bridge_android_intents.zig");
4343const network = @import("bridge_android_network.zig");
44const securestore = @import("bridge_android_securestore.zig");
4445
4546const Jni = jni.Jni;
4647
@@ -165,6 +166,30 @@ const natives = [_]jni.JNINativeMethod{
165166 .signature = "(Landroid/app/Activity;)Ljava/lang/String;",
166167 .fnPtr = @ptrCast(&nativeGetNetworkStatus),
167168 },
169 // These four take the SharedPreferences rather than the Activity: the
170 // store is EncryptedSharedPreferences over a MasterKey the Kotlin already
171 // built, and rebuilding it here would be a second implementation of a
172 // security-sensitive construction that has to agree exactly.
173 .{
174 .name = "nativeSecureSet",
175 .signature = "(Landroid/content/SharedPreferences;Ljava/lang/String;Ljava/lang/String;)Z",
176 .fnPtr = @ptrCast(&nativeSecureSet),
177 },
178 .{
179 .name = "nativeSecureGet",
180 .signature = "(Landroid/content/SharedPreferences;Ljava/lang/String;)Ljava/lang/String;",
181 .fnPtr = @ptrCast(&nativeSecureGet),
182 },
183 .{
184 .name = "nativeSecureRemove",
185 .signature = "(Landroid/content/SharedPreferences;Ljava/lang/String;)Z",
186 .fnPtr = @ptrCast(&nativeSecureRemove),
187 },
188 .{
189 .name = "nativeSecureClear",
190 .signature = "(Landroid/content/SharedPreferences;)Z",
191 .fnPtr = @ptrCast(&nativeSecureClear),
192 },
168193};
169194
170195/// How binding went. A value rather than a log line, so every path is
@@ -388,6 +413,83 @@ fn nativeGetNetworkStatus(env: jni.JNIEnv, _: jni.jobject, activity: jni.jobject
388413 return j.newStringUtf(owned.ptr) catch null;
389414}
390415
416fn nativeSecureSet(
417 env: jni.JNIEnv,
418 _: jni.jobject,
419 prefs: jni.jobject,
420 key: jni.jstring,
421 value: jni.jstring,
422) callconv(.c) jni.jboolean {
423 const j = Jni.init(env);
424 var arena = std.heap.ArenaAllocator.init(backing);
425 defer arena.deinit();
426 const allocator = arena.allocator();
427
428 const k = ownedUtf8(j, allocator, key) orelse return jni.JNI_FALSE;
429 const v = ownedUtf8(j, allocator, value) orelse return jni.JNI_FALSE;
430
431 securestore.set(j, prefs, k.ptr, v.ptr) catch |err| {
432 std.log.warn("craft: secureSet fell through to the shim ({s})", .{@errorName(err)});
433 return jni.JNI_FALSE;
434 };
435 return jni.JNI_TRUE;
436}
437
438/// Returns the `{"found":…}` envelope, or null when Zig did not serve the call.
439///
440/// Null cannot mean "no such key" here — the Kotlin already uses null for
441/// that, and the seam needs a third answer. See the module comment on
442/// `bridge_android_securestore.zig`.
443fn nativeSecureGet(
444 env: jni.JNIEnv,
445 _: jni.jobject,
446 prefs: jni.jobject,
447 key: jni.jstring,
448) callconv(.c) jni.jstring {
449 const j = Jni.init(env);
450 var arena = std.heap.ArenaAllocator.init(backing);
451 defer arena.deinit();
452 const allocator = arena.allocator();
453
454 const k = ownedUtf8(j, allocator, key) orelse return null;
455 const value = securestore.get(allocator, j, prefs, k.ptr) catch |err| {
456 std.log.warn("craft: secureGet fell through to the shim ({s})", .{@errorName(err)});
457 return null;
458 };
459
460 const json = securestore.renderRead(allocator, value) catch return null;
461 const owned = allocator.allocSentinel(u8, json.len, 0) catch return null;
462 @memcpy(owned, json);
463 return j.newStringUtf(owned.ptr) catch null;
464}
465
466fn nativeSecureRemove(
467 env: jni.JNIEnv,
468 _: jni.jobject,
469 prefs: jni.jobject,
470 key: jni.jstring,
471) callconv(.c) jni.jboolean {
472 const j = Jni.init(env);
473 var arena = std.heap.ArenaAllocator.init(backing);
474 defer arena.deinit();
475
476 const k = ownedUtf8(j, arena.allocator(), key) orelse return jni.JNI_FALSE;
477 securestore.remove(j, prefs, k.ptr) catch |err| {
478 std.log.warn("craft: secureRemove fell through to the shim ({s})", .{@errorName(err)});
479 return jni.JNI_FALSE;
480 };
481 return jni.JNI_TRUE;
482}
483
484fn nativeSecureClear(env: jni.JNIEnv, _: jni.jobject, prefs: jni.jobject) callconv(.c) jni.jboolean {
485 const j = Jni.init(env);
486 securestore.clear(j, prefs) catch |err| {
487 std.log.warn("craft: secureClear fell through to the shim ({s})", .{@errorName(err)});
488 return jni.JNI_FALSE;
489 };
490 return jni.JNI_TRUE;
491}
492
391493// =============================================================================
392494// Tests
393495// =============================================================================
@@ -499,7 +601,7 @@ test "the registered natives name methods the Kotlin actually declares" {
499601 // A descriptor is checked by the JVM at registration, so a wrong one fails
500602 // at load rather than at call — but only if the *name* matches something.
501603 // These two strings are the contract with CraftBridge.kt.
502 try testing.expectEqual(@as(usize, 8), natives.len);
604 try testing.expectEqual(@as(usize, 12), natives.len);
503605 try testing.expectEqualStrings("nativeGetDeviceInfo", std.mem.span(natives[0].name));
504606
505607 // And the class they bind to is the fixed one, not the templated bridge.