@onekeyfe/react-native-lite-card 6.7.0-alpha.449 → 6.7.0-alpha.464
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +3 -1
- package/ReactNativeLiteCard.podspec +7 -1
- package/android/build.gradle +1 -0
- package/android/consumer-rules.pro +4 -0
- package/android/src/main/java/com/onekeyfe/reactnativelitecard/ReactNativeLiteCardModule.kt +37 -44
- package/android/src/main/java/com/onekeyfe/reactnativelitecard/onekeyLite/OnekeyLiteCard.kt +105 -38
- package/android/src/main/java/com/onekeyfe/reactnativelitecard/onekeyLite/nfc/CommandGenerator.kt +19 -1
- package/android/src/main/java/com/onekeyfe/reactnativelitecard/onekeyLite/nfc/Connection.kt +10 -2
- package/ios/Classes/OKLiteCommand/OKLiteCommandTool.m +34 -0
- package/ios/Classes/OKNFCBridge.mm +23 -0
- package/ios/Classes/OKNFTLite/OKLiteV1.m +13 -6
- package/ios/Classes/OKNFTLite/OKNFCManager.h +2 -2
- package/ios/Classes/OKNFTLite/OKNFCManager.m +84 -12
- package/ios/ReactNativeLiteCard.mm +7 -4
- package/lib/module/index.js +4 -4
- package/lib/typescript/src/NativeReactNativeLiteCard.d.ts +2 -2
- package/lib/typescript/src/index.d.ts +2 -2
- package/package.json +1 -1
- package/sdk/CMakeLists.txt +4 -3
- package/sdk/README.md +15 -0
- package/sdk/android/implJni.cpp +39 -0
- package/sdk/scripts/generate-source-id.rb +22 -0
- package/sdk/source-id-manifest.txt +19 -0
- package/sdk/source-id.cmake +31 -0
- package/sdk/src/GPChannelSDK.cpp +7 -1
- package/sdk/src/version.c +2 -1
- package/sdk/tests/android_verify.py +4 -1
- package/sdk/tests/safety.cpp +159 -5
- package/sdk/tests/verify.py +21 -4
- package/src/NativeReactNativeLiteCard.ts +2 -0
- package/src/index.tsx +14 -3
|
@@ -74,8 +74,11 @@ class AndroidSDK:
|
|
|
74
74
|
return c.string_at(self.buffers.pop(pointer.value)) if pointer.value else None
|
|
75
75
|
|
|
76
76
|
def build(self, name, data, header=(0x80, 0x20, 0, 0)):
|
|
77
|
+
return self.build_hex(name, data.hex(), header)
|
|
78
|
+
|
|
79
|
+
def build_hex(self, name, text, header=(0x80, 0x20, 0, 0)):
|
|
77
80
|
result = self.request("safe" if name == "BuildSafeAPDU" else "build",
|
|
78
|
-
data=
|
|
81
|
+
data=text, **dict(zip(("cla", "ins", "p1", "p2"), header)))
|
|
79
82
|
return result["code"], result["output"].encode() if result["output"] is not None else None
|
|
80
83
|
|
|
81
84
|
def parse(self, name, data):
|
package/sdk/tests/safety.cpp
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
#include "GPChannelSDK.h"
|
|
2
|
+
#include "GPChannelSDKCore.h"
|
|
2
3
|
#include <TrezorCrypto/aes.h>
|
|
3
4
|
#include <TrezorCrypto/cmac.h>
|
|
4
5
|
#include <TrezorCrypto/ecdsa.h>
|
|
@@ -7,7 +8,9 @@
|
|
|
7
8
|
#include <algorithm>
|
|
8
9
|
#include <cstdlib>
|
|
9
10
|
#include <cstring>
|
|
11
|
+
#include <cstdarg>
|
|
10
12
|
#include <iostream>
|
|
13
|
+
#include <fstream>
|
|
11
14
|
#include <map>
|
|
12
15
|
#include <random>
|
|
13
16
|
#include <string>
|
|
@@ -196,6 +199,11 @@ struct MockJniRuntime {
|
|
|
196
199
|
JavaVM vm;
|
|
197
200
|
std::map<std::string, void*> methods;
|
|
198
201
|
std::vector<std::string> allocatedStrings;
|
|
202
|
+
std::vector<std::string> logs;
|
|
203
|
+
bool exceptionPending = false;
|
|
204
|
+
bool loggerAvailable = true;
|
|
205
|
+
bool loggerThrows = false;
|
|
206
|
+
size_t clearedExceptions = 0;
|
|
199
207
|
|
|
200
208
|
static MockJniRuntime*& active() {
|
|
201
209
|
static MockJniRuntime* instance = nullptr;
|
|
@@ -208,11 +216,30 @@ struct MockJniRuntime {
|
|
|
208
216
|
}
|
|
209
217
|
|
|
210
218
|
static jclass JNICALL mockFindClass(JNIEnv*, const char* name) {
|
|
219
|
+
if (name && std::string(name) == "com/margelo/nitro/nativelogger/OneKeyLog") {
|
|
220
|
+
if (!active()->loggerAvailable) { active()->exceptionPending = true; return nullptr; }
|
|
221
|
+
return reinterpret_cast<jclass>(0x2);
|
|
222
|
+
}
|
|
211
223
|
require(name && std::string(name) == "org/haobtc/onekey/card/gpchannel/GPChannelNatives",
|
|
212
224
|
"Unexpected JNI class lookup");
|
|
213
225
|
return reinterpret_cast<jclass>(0x1);
|
|
214
226
|
}
|
|
215
227
|
|
|
228
|
+
static jboolean JNICALL mockExceptionCheck(JNIEnv*) { return active()->exceptionPending; }
|
|
229
|
+
static void JNICALL mockExceptionClear(JNIEnv*) { active()->exceptionPending = false; ++active()->clearedExceptions; }
|
|
230
|
+
static jmethodID JNICALL mockGetStaticMethodID(JNIEnv*, jclass type, const char* name, const char* signature) {
|
|
231
|
+
require(type == reinterpret_cast<jclass>(0x2) && std::string(name) == "info" &&
|
|
232
|
+
std::string(signature) == "(Ljava/lang/String;Ljava/lang/String;)V", "Existing logger method contract");
|
|
233
|
+
return reinterpret_cast<jmethodID>(0x3);
|
|
234
|
+
}
|
|
235
|
+
static void JNICALL mockCallStaticVoidMethodV(JNIEnv*, jclass, jmethodID, va_list args) {
|
|
236
|
+
const auto tag = va_arg(args, jstring);
|
|
237
|
+
const auto message = va_arg(args, jstring);
|
|
238
|
+
require(readJString(tag) == "LiteCard", "Diagnostic tag is fixed");
|
|
239
|
+
active()->logs.push_back(readJString(message));
|
|
240
|
+
if (active()->loggerThrows) active()->exceptionPending = true;
|
|
241
|
+
}
|
|
242
|
+
|
|
216
243
|
static jint JNICALL mockRegisterNatives(JNIEnv*, jclass, const JNINativeMethod* methods, jint count) {
|
|
217
244
|
for (jint i = 0; i < count; ++i) {
|
|
218
245
|
active()->methods[methods[i].name] = methods[i].fnPtr;
|
|
@@ -245,6 +272,10 @@ struct MockJniRuntime {
|
|
|
245
272
|
envTable.GetStringUTFChars = &mockGetStringUTFChars;
|
|
246
273
|
envTable.ReleaseStringUTFChars = &mockReleaseStringUTFChars;
|
|
247
274
|
envTable.NewStringUTF = &mockNewStringUTF;
|
|
275
|
+
envTable.ExceptionCheck = &mockExceptionCheck;
|
|
276
|
+
envTable.ExceptionClear = &mockExceptionClear;
|
|
277
|
+
envTable.GetStaticMethodID = &mockGetStaticMethodID;
|
|
278
|
+
envTable.CallStaticVoidMethodV = &mockCallStaticVoidMethodV;
|
|
248
279
|
env.functions = &envTable;
|
|
249
280
|
vmTable.GetEnv = &mockGetEnv;
|
|
250
281
|
vm.functions = &vmTable;
|
|
@@ -274,7 +305,32 @@ int main() {
|
|
|
274
305
|
require(JUB_GPC_OpenSecureChannel("") == JUBR_CONTEXT_NOT_SATISFIED);
|
|
275
306
|
require(JUB_GPC_BuildSafeAPDU(0, 0, 0, 0, "", &output) == JUBR_CONTEXT_NOT_SATISFIED);
|
|
276
307
|
require(JUB_GPC_BuildAPDU(0, 0, 0, 0, "zz", &output) == JUBR_ARGUMENTS_BAD);
|
|
308
|
+
for (const auto& text : {"0", "123", "AbC", "0123", "ABCDEF", ""}) {
|
|
309
|
+
require(JUB_GPC_BuildAPDU(0x80, 0x3b, 0, 0, text, &output) == JUBR_OK && output,
|
|
310
|
+
"Valid odd and even command inputs must build");
|
|
311
|
+
const std::string input(text);
|
|
312
|
+
const std::string normalized = input.size() % 2 ? "0" + input : input;
|
|
313
|
+
char* expected = nullptr;
|
|
314
|
+
require(JUB_GPC_BuildAPDU(0x80, 0x3b, 0, 0, normalized.c_str(), &expected) == JUBR_OK);
|
|
315
|
+
require(std::string(output) == expected, "Odd command must equal whole-input leading-zero encoding");
|
|
316
|
+
require(JUB_FreeMemory(output) == JUBR_OK);
|
|
317
|
+
require(JUB_FreeMemory(expected) == JUBR_OK);
|
|
318
|
+
}
|
|
319
|
+
for (const auto& text : {"z", "12z", "0x1", " 1", "+1", "1\n"}) {
|
|
320
|
+
output = reinterpret_cast<char*>(1);
|
|
321
|
+
require(JUB_GPC_BuildAPDU(0, 0, 0, 0, text, &output) == JUBR_ARGUMENTS_BAD && !output,
|
|
322
|
+
"Invalid odd/even command must fail with null output");
|
|
323
|
+
}
|
|
324
|
+
for (size_t digits : {size_t(131069), size_t(131070), size_t(131071)}) {
|
|
325
|
+
const std::string input(digits, 'a');
|
|
326
|
+
const auto result = JUB_GPC_BuildAPDU(0x80, 0x3b, 0, 0, input.c_str(), &output);
|
|
327
|
+
require(result == (digits <= 131070 ? JUBR_OK : JUBR_ARGUMENTS_BAD),
|
|
328
|
+
"Plain command limits count decoded bytes after leading-zero normalization");
|
|
329
|
+
if (output) require(JUB_FreeMemory(output) == JUBR_OK);
|
|
330
|
+
}
|
|
277
331
|
JUB_UINT16 status = 0;
|
|
332
|
+
require(JUB_GPC_ParseAPDUResponse("900", &status, &output) == JUBR_ARGUMENTS_BAD && !output,
|
|
333
|
+
"Odd response input must remain rejected");
|
|
278
334
|
require(JUB_GPC_ParseAPDUResponse("9000", &status, &output) == 0);
|
|
279
335
|
require(status == 0x9000 && output && !*output);
|
|
280
336
|
require(JUB_FreeMemory(output) == 0);
|
|
@@ -293,8 +349,19 @@ int main() {
|
|
|
293
349
|
std::string validCert = buildCertificate(ocePub);
|
|
294
350
|
std::string offCurveCert = buildCertificate(offCurvePub);
|
|
295
351
|
|
|
352
|
+
char* strictSerial = nullptr;
|
|
353
|
+
char* strictSubject = nullptr;
|
|
354
|
+
const std::string oddCert = validCert.substr(1);
|
|
355
|
+
require(JUB_GPC_ParseCertificate(oddCert.c_str(), &strictSerial, &strictSubject) == JUBR_ARGUMENTS_BAD &&
|
|
356
|
+
!strictSerial && !strictSubject, "Odd certificate must remain rejected");
|
|
357
|
+
JUB_ULONG strictTag = 0;
|
|
358
|
+
require(JUB_GPC_TLVDecode("53100", &strictTag, &output) == JUBR_ARGUMENTS_BAD && !output,
|
|
359
|
+
"Odd TLV input must remain rejected");
|
|
360
|
+
|
|
296
361
|
require(JUB_GPC_Initialize(info, validCert.c_str(), validOcePriv.c_str()) == JUBR_OK,
|
|
297
362
|
"Valid scalar and certificate should initialize");
|
|
363
|
+
require(JUB_GPC_OpenSecureChannel("5f490") == JUBR_ARGUMENTS_BAD,
|
|
364
|
+
"Odd handshake response must remain rejected");
|
|
298
365
|
require(JUB_GPC_Finalize() == JUBR_OK);
|
|
299
366
|
|
|
300
367
|
require(JUB_GPC_Initialize(info, validCert.c_str(), zeroPriv.c_str()) == JUBR_ARGUMENTS_BAD,
|
|
@@ -393,24 +460,47 @@ int main() {
|
|
|
393
460
|
require(JUB_GPC_OpenSecureChannel(maRespHex.c_str()) == JUBR_OK,
|
|
394
461
|
"Valid mutual authentication response must open secure channel");
|
|
395
462
|
|
|
463
|
+
for (const auto& input : {std::string("xyz"), std::string(131023, 'a'), std::string(131024, 'a')}) {
|
|
464
|
+
output = reinterpret_cast<char*>(1);
|
|
465
|
+
require(JUB_GPC_BuildSafeAPDU(0x80, 0x3b, 0, 0, input.c_str(), &output) == JUBR_ARGUMENTS_BAD && !output,
|
|
466
|
+
"Invalid and oversized safe command inputs must not build");
|
|
467
|
+
}
|
|
468
|
+
|
|
396
469
|
// Encrypted APDU build must succeed while session is open
|
|
397
470
|
char* safeApduHex = nullptr;
|
|
398
|
-
|
|
471
|
+
const std::string oddBackup = "12345678901234567890123456789012345678900000001";
|
|
472
|
+
require(JUB_GPC_BuildSafeAPDU(0x80, 0x20, 0x00, 0x00, oddBackup.c_str(), &safeApduHex) == JUBR_OK && safeApduHex);
|
|
399
473
|
std::vector<unsigned char> safeApduBytes = unhex(safeApduHex);
|
|
400
474
|
require(JUB_FreeMemory(safeApduHex) == JUBR_OK);
|
|
401
|
-
require(safeApduBytes.size() == 5 +
|
|
475
|
+
require(safeApduBytes.size() == 5 + 32 + 8 && safeApduBytes[0] == 0x84);
|
|
476
|
+
aes_encrypt_ctx commandEnc;
|
|
477
|
+
aes_encrypt_key128(derivedKeys.data() + 16, &commandEnc);
|
|
478
|
+
unsigned char commandCounter[16] = {0};
|
|
479
|
+
commandCounter[15] = 1;
|
|
480
|
+
unsigned char commandIv[16] = {0};
|
|
481
|
+
aes_ecb_encrypt(commandCounter, commandIv, 16, &commandEnc);
|
|
482
|
+
aes_decrypt_ctx commandDec;
|
|
483
|
+
aes_decrypt_key128(derivedKeys.data() + 16, &commandDec);
|
|
484
|
+
unsigned char commandPlain[32] = {0};
|
|
485
|
+
aes_cbc_decrypt(safeApduBytes.data() + 5, commandPlain, 32, commandIv, &commandDec);
|
|
486
|
+
const auto expectedCommand = unhex("0" + oddBackup);
|
|
487
|
+
require(std::equal(expectedCommand.begin(), expectedCommand.end(), commandPlain) &&
|
|
488
|
+
commandPlain[expectedCommand.size()] == 0x80 &&
|
|
489
|
+
std::all_of(commandPlain + expectedCommand.size() + 1, commandPlain + 32,
|
|
490
|
+
[](unsigned char byte) { return byte == 0; }),
|
|
491
|
+
"45-digit backup must encrypt whole-input leading-zero bytes with unchanged padding");
|
|
402
492
|
|
|
403
493
|
// Verify JUB_GPC_ParseSafeAPDUResponse with valid RMAC, tampered RMAC, and status-only error:
|
|
404
494
|
// derivedKeys layout: [0..16]=key_dek, [16..32]=s_enc, [32..48]=s_mac, [48..64]=s_rmac
|
|
405
495
|
// After BuildSafeAPDU #1 (counter == 1), the SDK's 16-byte chaining CMAC is computed over:
|
|
406
|
-
// receipt(16B) ||
|
|
496
|
+
// receipt(16B) || header/Lc || ciphertext
|
|
407
497
|
std::vector<unsigned char> maRespBytes = unhex(maRespHex);
|
|
408
498
|
std::vector<unsigned char> receiptBytes(maRespBytes.end() - 16, maRespBytes.end());
|
|
409
499
|
std::vector<unsigned char> cmacInput = receiptBytes;
|
|
410
|
-
cmacInput.insert(cmacInput.end(), safeApduBytes.begin(), safeApduBytes.
|
|
500
|
+
cmacInput.insert(cmacInput.end(), safeApduBytes.begin(), safeApduBytes.end() - 8);
|
|
411
501
|
unsigned char cmdChain[16] = {0};
|
|
412
502
|
AES_CMAC(derivedKeys.data() + 32, cmacInput.data(), static_cast<unsigned>(cmacInput.size()), cmdChain);
|
|
413
|
-
require(std::memcmp(cmdChain, safeApduBytes.data() +
|
|
503
|
+
require(std::memcmp(cmdChain, safeApduBytes.data() + safeApduBytes.size() - 8, 8) == 0,
|
|
414
504
|
"Command CMAC prefix must match independent AES-CMAC");
|
|
415
505
|
|
|
416
506
|
// Construct encrypted response for counter == 2 (incCounter() pre-increments 1 -> 2):
|
|
@@ -452,6 +542,9 @@ int main() {
|
|
|
452
542
|
"Tampered RMAC response must be rejected");
|
|
453
543
|
require(!output);
|
|
454
544
|
|
|
545
|
+
require(JUB_GPC_ParseSafeAPDUResponse("900", &status, &output) == JUBR_ARGUMENTS_BAD && !output,
|
|
546
|
+
"Odd protected response input must remain rejected");
|
|
547
|
+
|
|
455
548
|
// Unauthenticated plain "9000" on safe channel must be rejected
|
|
456
549
|
require(JUB_GPC_ParseSafeAPDUResponse("9000", &status, &output) != JUBR_OK,
|
|
457
550
|
"Unauthenticated 9000 response on safe channel must be rejected");
|
|
@@ -471,6 +564,13 @@ int main() {
|
|
|
471
564
|
require(status == 0x63c2 && output && std::string(output).empty());
|
|
472
565
|
require(JUB_FreeMemory(output) == JUBR_OK);
|
|
473
566
|
|
|
567
|
+
for (size_t digits : {size_t(131021), size_t(131022)}) {
|
|
568
|
+
const std::string input(digits, 'a');
|
|
569
|
+
require(JUB_GPC_BuildSafeAPDU(0x80, 0x3b, 0, 0, input.c_str(), &output) == JUBR_OK && output,
|
|
570
|
+
"Safe command accepts both parities at the decoded-byte limit");
|
|
571
|
+
require(JUB_FreeMemory(output) == JUBR_OK);
|
|
572
|
+
}
|
|
573
|
+
|
|
474
574
|
// Failed re-initialization must clear the open session
|
|
475
575
|
require(JUB_GPC_Initialize(info, "deadbeef", validOcePriv.c_str()) == JUBR_ARGUMENTS_BAD);
|
|
476
576
|
output = nullptr;
|
|
@@ -510,6 +610,7 @@ int main() {
|
|
|
510
610
|
typedef jint (*OpenFn)(JNIEnv*, jobject, jstring);
|
|
511
611
|
typedef jstring (*MutualFn)(JNIEnv*, jobject);
|
|
512
612
|
typedef jstring (*ParseFn)(JNIEnv*, jobject, jstring);
|
|
613
|
+
typedef jstring (*BuildFn)(JNIEnv*, jobject, jlong, jlong, jlong, jlong, jstring);
|
|
513
614
|
|
|
514
615
|
GetErrorFn jniGetError = reinterpret_cast<GetErrorFn>(jni.methods["nativeGetErrorCode"]);
|
|
515
616
|
InitFn jniInit = reinterpret_cast<InitFn>(jni.methods["nativeGPCInitialize"]);
|
|
@@ -518,6 +619,43 @@ int main() {
|
|
|
518
619
|
MutualFn jniMutual = reinterpret_cast<MutualFn>(jni.methods["nativeGPCBuildMutualAuthData"]);
|
|
519
620
|
ParseFn jniParseSafe = reinterpret_cast<ParseFn>(jni.methods["nativeGPCParseSafeAPDUResponse"]);
|
|
520
621
|
ParseFn jniParseCert = reinterpret_cast<ParseFn>(jni.methods["nativeGPCParseCertificate"]);
|
|
622
|
+
BuildFn jniBuild = reinterpret_cast<BuildFn>(jni.methods["nativeGPCBuildAPDU"]);
|
|
623
|
+
BuildFn jniBuildSafe = reinterpret_cast<BuildFn>(jni.methods["nativeGPCBuildSafeAPDU"]);
|
|
624
|
+
|
|
625
|
+
const std::string sourceId(reinterpret_cast<const char*>(GPChannelSDKCoreVersionString));
|
|
626
|
+
require(sourceId.size() == 74 && sourceId.substr(0, 10) == "source-v1:" &&
|
|
627
|
+
sourceId.find_first_not_of("0123456789abcdef", 10) == std::string::npos, "Compiled native source identity format");
|
|
628
|
+
std::string displayedSourceId = sourceId;
|
|
629
|
+
for (size_t offset = 66; offset > 10; offset -= 8) displayedSourceId.insert(offset, "-");
|
|
630
|
+
auto expectedBuildLog = [&](bool safe, int code, bool present) {
|
|
631
|
+
return "stage=command_build nativeSourceId=" + displayedSourceId + " safe=" + (safe ? "true" : "false") +
|
|
632
|
+
" sdkReturnCode=" + std::to_string(code) + " outputPresent=" + (present ? "true" : "false");
|
|
633
|
+
};
|
|
634
|
+
const std::string privateBackup = "12345678901234567890123456789012345678900000001";
|
|
635
|
+
require(jniBuildSafe(&jni.env, nullptr, 0x80, 0x3b, 0, 0, jni.makeJString(privateBackup)) == nullptr);
|
|
636
|
+
require(jni.logs.back() == expectedBuildLog(true, 6, false), "Unavailable context diagnostic uses actual local code 6");
|
|
637
|
+
const std::string privateInvalid = "PIN-SEED-CERT-SERIAL-WALLET-PRIVATE\n/path/user@host";
|
|
638
|
+
require(jniBuild(&jni.env, nullptr, 0x80, 0x3b, 0, 0, jni.makeJString(privateInvalid)) == nullptr);
|
|
639
|
+
require(jni.logs.back() == expectedBuildLog(false, 3, false), "Invalid command diagnostic uses actual local code 3");
|
|
640
|
+
auto plain = jniBuild(&jni.env, nullptr, 0x80, 0x3b, 0, 0, jni.makeJString(privateBackup));
|
|
641
|
+
require(plain && jni.logs.back() == expectedBuildLog(false, 0, true), "Success diagnostic reports 0 despite stale global error");
|
|
642
|
+
const auto privateApdu = MockJniRuntime::readJString(plain);
|
|
643
|
+
jni.loggerAvailable = false;
|
|
644
|
+
const auto beforeMissingLogger = jni.logs.size();
|
|
645
|
+
require(jniBuild(&jni.env, nullptr, 0x80, 0x3b, 0, 0, jni.makeJString(privateBackup)) != nullptr &&
|
|
646
|
+
!jni.exceptionPending && jni.logs.size() == beforeMissingLogger, "Missing logger leaves SDK success intact");
|
|
647
|
+
jni.loggerAvailable = true;
|
|
648
|
+
jni.loggerThrows = true;
|
|
649
|
+
require(jniBuild(&jni.env, nullptr, 0x80, 0x3b, 0, 0, jni.makeJString(privateBackup)) != nullptr &&
|
|
650
|
+
!jni.exceptionPending, "Logger exception leaves SDK success intact");
|
|
651
|
+
jni.loggerThrows = false;
|
|
652
|
+
jni.exceptionPending = true;
|
|
653
|
+
const auto beforePending = jni.logs.size();
|
|
654
|
+
const auto beforeClears = jni.clearedExceptions;
|
|
655
|
+
require(jniBuild(&jni.env, nullptr, 0x80, 0x3b, 0, 0, jni.makeJString(privateBackup)) != nullptr &&
|
|
656
|
+
jni.exceptionPending && jni.logs.size() == beforePending && jni.clearedExceptions == beforeClears,
|
|
657
|
+
"Diagnostic helper must preserve a pre-existing pending exception");
|
|
658
|
+
jni.exceptionPending = false;
|
|
521
659
|
|
|
522
660
|
require(jniFinalize(&jni.env, nullptr) == JUBR_OK);
|
|
523
661
|
require(jniInit(&jni.env, nullptr, nullptr) == JUBR_ARGUMENTS_BAD,
|
|
@@ -564,6 +702,22 @@ int main() {
|
|
|
564
702
|
require(jniMa != nullptr);
|
|
565
703
|
std::string jniMaResp = buildCardMutualAuthResponse(validCardPriv, ocePub, MockJniRuntime::readJString(jniMa));
|
|
566
704
|
require(jniOpen(&jni.env, nullptr, jni.makeJString(jniMaResp)) == JUBR_OK);
|
|
705
|
+
require(jniBuildSafe(&jni.env, nullptr, 0x80, 0x3b, 0, 0, jni.makeJString(privateBackup)) != nullptr &&
|
|
706
|
+
jni.logs.back() == expectedBuildLog(true, 0, true), "Safe success logs actual local code");
|
|
707
|
+
for (const auto& line : jni.logs) {
|
|
708
|
+
require(line == expectedBuildLog(false, 0, true) || line == expectedBuildLog(true, 0, true) ||
|
|
709
|
+
line == expectedBuildLog(false, 3, false) || line == expectedBuildLog(true, 6, false),
|
|
710
|
+
"Every emitted field is allowlisted; no extra diagnostic content");
|
|
711
|
+
for (const auto& secret : {privateBackup, privateInvalid, privateApdu, validOcePriv, validCert, jniMaResp, sourceId})
|
|
712
|
+
require(line.find(secret) == std::string::npos, "Diagnostic must not contain synthetic sensitive input or output");
|
|
713
|
+
}
|
|
714
|
+
if (const char* path = std::getenv("ONEKEY_LITE_JNI_LOG_TEST_OUTPUT")) {
|
|
715
|
+
std::ofstream captured(path);
|
|
716
|
+
captured << sourceId << '\n';
|
|
717
|
+
for (const auto& line : jni.logs) captured << line << '\n';
|
|
718
|
+
captured.flush();
|
|
719
|
+
require(captured.good(), "Captured JNI metadata must be available to the real logger regression");
|
|
720
|
+
}
|
|
567
721
|
|
|
568
722
|
// Unauthenticated "9000" safe response through JNI must return nullptr and set errorCode
|
|
569
723
|
require(jniParseSafe(&jni.env, nullptr, jni.makeJString("9000")) == nullptr,
|
package/sdk/tests/verify.py
CHANGED
|
@@ -50,8 +50,11 @@ class SDK:
|
|
|
50
50
|
return value
|
|
51
51
|
|
|
52
52
|
def build(self, name, data, header=(0x80, 0x20, 0, 0)):
|
|
53
|
+
return self.build_hex(name, data.hex(), header)
|
|
54
|
+
|
|
55
|
+
def build_hex(self, name, text, header=(0x80, 0x20, 0, 0)):
|
|
53
56
|
output = c.c_void_p()
|
|
54
|
-
result = getattr(self.lib, "JUB_GPC_" + name)(*header,
|
|
57
|
+
result = getattr(self.lib, "JUB_GPC_" + name)(*header, text.encode(), c.byref(output))
|
|
55
58
|
return result, self.take(output)
|
|
56
59
|
|
|
57
60
|
def parse(self, name, data):
|
|
@@ -147,6 +150,15 @@ def verify(recovered, reference=None):
|
|
|
147
150
|
for sdk in peers:
|
|
148
151
|
assert sdk.build("BuildAPDU", data, header) == (0, expected), "Plain APDU bytes"
|
|
149
152
|
checks += 1
|
|
153
|
+
for text in ("1", "123", "AbC", "12345678901234567890123456789012345678900000001",
|
|
154
|
+
"0" * 131069, "0" * 131070):
|
|
155
|
+
data = bytes.fromhex(("0" if len(text) % 2 else "") + text)
|
|
156
|
+
size = len(data)
|
|
157
|
+
length = bytes([size]) if size <= 255 else b"\x00" + size.to_bytes(2, "big")
|
|
158
|
+
expected = (bytes.fromhex("803b0000") + length + data).hex().encode()
|
|
159
|
+
for sdk in peers:
|
|
160
|
+
assert sdk.build_hex("BuildAPDU", text, (0x80, 0x3b, 0, 0)) == (0, expected), "Raw odd command compatibility"
|
|
161
|
+
checks += 1
|
|
150
162
|
for size in (0, 1, 5, 6, 7, 16, 255, 1024, 1025):
|
|
151
163
|
for word in (b"\x90\x00", b"\x63\xc2", b"\x6a\x80"):
|
|
152
164
|
data = rng.randbytes(size)
|
|
@@ -176,8 +188,13 @@ def verify(recovered, reference=None):
|
|
|
176
188
|
if sdk is recovered:
|
|
177
189
|
assert sdk.parse("ParseSafeAPDUResponse", bytes(8) + bytes.fromhex("63c2"))[0] == 3, "Reject missing encrypted response block"
|
|
178
190
|
checks += 1
|
|
179
|
-
|
|
180
|
-
|
|
191
|
+
command_inputs = [rng.randbytes(size).hex() for size in
|
|
192
|
+
(0, 1, 15, 16, 17, 127, 128, 239, 240, 255, 256, 1024)]
|
|
193
|
+
command_inputs += ["12345678901234567890123456789012345678900000001", "AbC", "1", "12"]
|
|
194
|
+
command_inputs += [rng.randbytes(1).hex() for _ in range(250)]
|
|
195
|
+
for text in command_inputs:
|
|
196
|
+
data = bytes.fromhex(("0" if len(text) % 2 else "") + text)
|
|
197
|
+
size = len(data)
|
|
181
198
|
counter_bytes = counter.to_bytes(4, "big") if getattr(sdk, "wide_counter", False) else bytes([counter & 255])
|
|
182
199
|
iv = aes(enc, bytes(16), counter_bytes.rjust(16, b"\0"))
|
|
183
200
|
encrypted = aes(enc, iv, pad(data))
|
|
@@ -186,7 +203,7 @@ def verify(recovered, reference=None):
|
|
|
186
203
|
chain = mac(cmac_key, chain + header + bytes([length & 255]) + encrypted)
|
|
187
204
|
length_bytes = bytes([length]) if length <= 255 else b"\x00" + length.to_bytes(2, "big")
|
|
188
205
|
expected = (header + length_bytes + encrypted + chain[:8]).hex().encode()
|
|
189
|
-
assert sdk.
|
|
206
|
+
assert sdk.build_hex("BuildSafeAPDU", text) == (0, expected), "Raw command encrypted APDU and chained CMAC"
|
|
190
207
|
checks += 1
|
|
191
208
|
reply = rng.randbytes(min(size, 1008))
|
|
192
209
|
counter += 1
|
|
@@ -72,6 +72,7 @@ export interface Spec extends TurboModule {
|
|
|
72
72
|
mnemonic: string,
|
|
73
73
|
pwd: string,
|
|
74
74
|
overwrite: boolean,
|
|
75
|
+
expectedSerial: string | null,
|
|
75
76
|
callback: (
|
|
76
77
|
error: CallbackError | null,
|
|
77
78
|
data: boolean | null,
|
|
@@ -89,6 +90,7 @@ export interface Spec extends TurboModule {
|
|
|
89
90
|
changePin(
|
|
90
91
|
oldPin: string,
|
|
91
92
|
newPin: string,
|
|
93
|
+
expectedSerial: string | null,
|
|
92
94
|
callback: (
|
|
93
95
|
error: CallbackError | null,
|
|
94
96
|
data: boolean | null,
|
package/src/index.tsx
CHANGED
|
@@ -39,12 +39,18 @@ class OnekeyLite {
|
|
|
39
39
|
});
|
|
40
40
|
}
|
|
41
41
|
|
|
42
|
-
setMnemonic(
|
|
42
|
+
setMnemonic(
|
|
43
|
+
mnemonic: string,
|
|
44
|
+
pwd: string,
|
|
45
|
+
overwrite = false,
|
|
46
|
+
expectedSerial?: string
|
|
47
|
+
) {
|
|
43
48
|
return new Promise<PromiseResult<boolean>>((resolve) => {
|
|
44
49
|
OKLiteManager.setMnemonic(
|
|
45
50
|
mnemonic,
|
|
46
51
|
pwd,
|
|
47
52
|
overwrite,
|
|
53
|
+
expectedSerial ?? null,
|
|
48
54
|
this.convertToPromise(resolve)
|
|
49
55
|
);
|
|
50
56
|
});
|
|
@@ -56,9 +62,14 @@ class OnekeyLite {
|
|
|
56
62
|
});
|
|
57
63
|
}
|
|
58
64
|
|
|
59
|
-
changePin(oldPin: string, newPin: string) {
|
|
65
|
+
changePin(oldPin: string, newPin: string, expectedSerial?: string) {
|
|
60
66
|
return new Promise<PromiseResult<boolean>>((resolve) => {
|
|
61
|
-
OKLiteManager.changePin(
|
|
67
|
+
OKLiteManager.changePin(
|
|
68
|
+
oldPin,
|
|
69
|
+
newPin,
|
|
70
|
+
expectedSerial ?? null,
|
|
71
|
+
this.convertToPromise(resolve)
|
|
72
|
+
);
|
|
62
73
|
});
|
|
63
74
|
}
|
|
64
75
|
|