also looking at this
feat(android): the secure-storage quartet — ratchet 95 -> 91
#147
6 files
+501
-3
| @@ -41,6 +41,7 @@ const system = @import("bridge_android_system.zig"); | ||
| 41 | 41 | const clipboard = @import("bridge_android_clipboard.zig"); |
| 42 | 42 | const intents = @import("bridge_android_intents.zig"); |
| 43 | 43 | const network = @import("bridge_android_network.zig"); |
| 44 | const securestore = @import("bridge_android_securestore.zig"); | |
| 44 | 45 | |
| 45 | 46 | const Jni = jni.Jni; |
| 46 | 47 | |
| @@ -165,6 +166,30 @@ const natives = [_]jni.JNINativeMethod{ | ||
| 165 | 166 | .signature = "(Landroid/app/Activity;)Ljava/lang/String;", |
| 166 | 167 | .fnPtr = @ptrCast(&nativeGetNetworkStatus), |
| 167 | 168 | }, |
| 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 | }, | |
| 168 | 193 | }; |
| 169 | 194 | |
| 170 | 195 | /// 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 | ||
| 388 | 413 | return j.newStringUtf(owned.ptr) catch null; |
| 389 | 414 | } |
| 390 | 415 | |
| 416 | fn 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`. | |
| 443 | fn 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 | ||
| 466 | fn 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 | ||
| 484 | fn 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 | ||
| 391 | 493 | // ============================================================================= |
| 392 | 494 | // Tests |
| 393 | 495 | // ============================================================================= |
| @@ -499,7 +601,7 @@ test "the registered natives name methods the Kotlin actually declares" { | ||
| 499 | 601 | // A descriptor is checked by the JVM at registration, so a wrong one fails |
| 500 | 602 | // at load rather than at call — but only if the *name* matches something. |
| 501 | 603 | // 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); | |
| 503 | 605 | try testing.expectEqualStrings("nativeGetDeviceInfo", std.mem.span(natives[0].name)); |
| 504 | 606 | |
| 505 | 607 | // And the class they bind to is the fixed one, not the templated bridge. |