@onekeyfe/react-native-lite-card 6.7.0-alpha.409 → 6.7.0-alpha.411
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 +9 -1
- package/ReactNativeLiteCard.podspec +25 -2
- package/android/build.gradle +7 -0
- package/android/src/main/cpp/CMakeLists.txt +6 -2
- package/android/src/main/java/com/onekeyfe/reactnativelitecard/nfc/gpchannel/GPChannelNatives.kt +7 -7
- package/android/src/main/java/com/onekeyfe/reactnativelitecard/onekeyLite/entitys/SendResponse.kt +6 -3
- package/android/src/main/java/com/onekeyfe/reactnativelitecard/onekeyLite/nfc/CommandGenerator.kt +5 -1
- package/android/src/main/java/com/onekeyfe/reactnativelitecard/onekeyLite/nfc/Connection.kt +21 -7
- package/android/src/main/java/com/onekeyfe/reactnativelitecard/onekeyLite/nfc/GPCAPDUGenerator.kt +2 -2
- package/ios/Classes/OKLiteCommand/OKLiteCommandTool.m +4 -1
- package/ios/Classes/OKNFCBridge.mm +1 -1
- package/ios/Classes/OKNFTLite/OKLiteV1.m +6 -3
- package/ios/Classes/OKNFTLite/OKLiteV2.m +13 -5
- package/package.json +2 -2
- package/sdk/CMakeLists.txt +120 -0
- package/sdk/README.md +175 -0
- package/sdk/android/implJni.cpp +177 -0
- package/sdk/deps/jsoncpp/LICENSE +55 -0
- package/sdk/deps/jsoncpp/include/CMakeLists.txt +2 -0
- package/sdk/deps/jsoncpp/include/json/allocator.h +98 -0
- package/sdk/deps/jsoncpp/include/json/assertions.h +54 -0
- package/sdk/deps/jsoncpp/include/json/autolink.h +25 -0
- package/sdk/deps/jsoncpp/include/json/config.h +187 -0
- package/sdk/deps/jsoncpp/include/json/features.h +61 -0
- package/sdk/deps/jsoncpp/include/json/forwards.h +37 -0
- package/sdk/deps/jsoncpp/include/json/json.h +15 -0
- package/sdk/deps/jsoncpp/include/json/reader.h +411 -0
- package/sdk/deps/jsoncpp/include/json/value.h +888 -0
- package/sdk/deps/jsoncpp/include/json/version.h +20 -0
- package/sdk/deps/jsoncpp/include/json/writer.h +357 -0
- package/sdk/deps/jsoncpp/src/lib_json/CMakeLists.txt +117 -0
- package/sdk/deps/jsoncpp/src/lib_json/json_reader.cpp +2060 -0
- package/sdk/deps/jsoncpp/src/lib_json/json_tool.h +114 -0
- package/sdk/deps/jsoncpp/src/lib_json/json_value.cpp +1661 -0
- package/sdk/deps/jsoncpp/src/lib_json/json_valueiterator.inl +167 -0
- package/sdk/deps/jsoncpp/src/lib_json/json_writer.cpp +1233 -0
- package/sdk/deps/jsoncpp/src/lib_json/version.h.in +20 -0
- package/sdk/deps/trezor-crypto/AUTHORS +2 -0
- package/sdk/deps/trezor-crypto/LICENSE +22 -0
- package/sdk/deps/trezor-crypto/include/TrezorCrypto/TrezorCrypto.h +29 -0
- package/sdk/deps/trezor-crypto/include/TrezorCrypto/address.h +44 -0
- package/sdk/deps/trezor-crypto/include/TrezorCrypto/aes.h +224 -0
- package/sdk/deps/trezor-crypto/include/TrezorCrypto/base32.h +49 -0
- package/sdk/deps/trezor-crypto/include/TrezorCrypto/base58.h +58 -0
- package/sdk/deps/trezor-crypto/include/TrezorCrypto/bignum.h +169 -0
- package/sdk/deps/trezor-crypto/include/TrezorCrypto/bip32.h +114 -0
- package/sdk/deps/trezor-crypto/include/TrezorCrypto/bip39.h +66 -0
- package/sdk/deps/trezor-crypto/include/TrezorCrypto/blake256.h +61 -0
- package/sdk/deps/trezor-crypto/include/TrezorCrypto/blake2b.h +50 -0
- package/sdk/deps/trezor-crypto/include/TrezorCrypto/blake2s.h +49 -0
- package/sdk/deps/trezor-crypto/include/TrezorCrypto/cash_addr.h +109 -0
- package/sdk/deps/trezor-crypto/include/TrezorCrypto/chacha20poly1305/ecrypt-config.h +316 -0
- package/sdk/deps/trezor-crypto/include/TrezorCrypto/chacha20poly1305/ecrypt-sync.h +289 -0
- package/sdk/deps/trezor-crypto/include/TrezorCrypto/chacha20poly1305/ecrypt-types.h +53 -0
- package/sdk/deps/trezor-crypto/include/TrezorCrypto/chacha20poly1305/poly1305-donna.h +28 -0
- package/sdk/deps/trezor-crypto/include/TrezorCrypto/chacha20poly1305.h +27 -0
- package/sdk/deps/trezor-crypto/include/TrezorCrypto/cmac.h +44 -0
- package/sdk/deps/trezor-crypto/include/TrezorCrypto/curves.h +60 -0
- package/sdk/deps/trezor-crypto/include/TrezorCrypto/ecdsa.h +112 -0
- package/sdk/deps/trezor-crypto/include/TrezorCrypto/ed25519-donna/curve25519-donna-32bit.h +87 -0
- package/sdk/deps/trezor-crypto/include/TrezorCrypto/ed25519-donna/curve25519-donna-helpers.h +30 -0
- package/sdk/deps/trezor-crypto/include/TrezorCrypto/ed25519-donna/ed25519-blake2b.h +21 -0
- package/sdk/deps/trezor-crypto/include/TrezorCrypto/ed25519-donna/ed25519-donna-32bit-tables.h +25 -0
- package/sdk/deps/trezor-crypto/include/TrezorCrypto/ed25519-donna/ed25519-donna-basepoint-table.h +10 -0
- package/sdk/deps/trezor-crypto/include/TrezorCrypto/ed25519-donna/ed25519-donna-impl-base.h +110 -0
- package/sdk/deps/trezor-crypto/include/TrezorCrypto/ed25519-donna/ed25519-donna-portable.h +32 -0
- package/sdk/deps/trezor-crypto/include/TrezorCrypto/ed25519-donna/ed25519-donna.h +60 -0
- package/sdk/deps/trezor-crypto/include/TrezorCrypto/ed25519-donna/ed25519-keccak.h +21 -0
- package/sdk/deps/trezor-crypto/include/TrezorCrypto/ed25519-donna/ed25519-sha3.h +21 -0
- package/sdk/deps/trezor-crypto/include/TrezorCrypto/ed25519-donna/modm-donna-32bit.h +87 -0
- package/sdk/deps/trezor-crypto/include/TrezorCrypto/ed25519-donna.h +60 -0
- package/sdk/deps/trezor-crypto/include/TrezorCrypto/ed25519.h +42 -0
- package/sdk/deps/trezor-crypto/include/TrezorCrypto/endian.h +132 -0
- package/sdk/deps/trezor-crypto/include/TrezorCrypto/groestl.h +104 -0
- package/sdk/deps/trezor-crypto/include/TrezorCrypto/hasher.h +90 -0
- package/sdk/deps/trezor-crypto/include/TrezorCrypto/hmac.h +60 -0
- package/sdk/deps/trezor-crypto/include/TrezorCrypto/memzero.h +16 -0
- package/sdk/deps/trezor-crypto/include/TrezorCrypto/monero/base58.h +42 -0
- package/sdk/deps/trezor-crypto/include/TrezorCrypto/monero/monero.h +13 -0
- package/sdk/deps/trezor-crypto/include/TrezorCrypto/monero/range_proof.h +30 -0
- package/sdk/deps/trezor-crypto/include/TrezorCrypto/monero/serialize.h +15 -0
- package/sdk/deps/trezor-crypto/include/TrezorCrypto/monero/xmr.h +67 -0
- package/sdk/deps/trezor-crypto/include/TrezorCrypto/nano.h +56 -0
- package/sdk/deps/trezor-crypto/include/TrezorCrypto/nem.h +199 -0
- package/sdk/deps/trezor-crypto/include/TrezorCrypto/nist256p1.h +35 -0
- package/sdk/deps/trezor-crypto/include/TrezorCrypto/pbkdf2.h +64 -0
- package/sdk/deps/trezor-crypto/include/TrezorCrypto/rand.h +44 -0
- package/sdk/deps/trezor-crypto/include/TrezorCrypto/rc4.h +37 -0
- package/sdk/deps/trezor-crypto/include/TrezorCrypto/rfc6979.h +40 -0
- package/sdk/deps/trezor-crypto/include/TrezorCrypto/ripemd160.h +28 -0
- package/sdk/deps/trezor-crypto/include/TrezorCrypto/ripple/base58.h +45 -0
- package/sdk/deps/trezor-crypto/include/TrezorCrypto/schnorr.h +48 -0
- package/sdk/deps/trezor-crypto/include/TrezorCrypto/script.h +30 -0
- package/sdk/deps/trezor-crypto/include/TrezorCrypto/scrypt.h +68 -0
- package/sdk/deps/trezor-crypto/include/TrezorCrypto/secp256k1.h +59 -0
- package/sdk/deps/trezor-crypto/include/TrezorCrypto/sha2.h +125 -0
- package/sdk/deps/trezor-crypto/include/TrezorCrypto/sha3.h +86 -0
- package/sdk/deps/trezor-crypto/include/TrezorCrypto/sodium/keypair.h +20 -0
- package/sdk/deps/trezor-crypto/include/TrezorCrypto/sodium/private/ed25519_ref10.h +85 -0
- package/sdk/deps/trezor-crypto/include/TrezorCrypto/sodium/private/ed25519_ref10_fe_25_5.h +43 -0
- package/sdk/deps/trezor-crypto/include/TrezorCrypto/sodium/private/fe_25_5/constants.h +29 -0
- package/sdk/deps/trezor-crypto/include/TrezorCrypto/sodium/private/fe_25_5/fe.h +25 -0
- package/sdk/deps/trezor-crypto/src/address.c +87 -0
- package/sdk/deps/trezor-crypto/src/aes/aes_modes.c +959 -0
- package/sdk/deps/trezor-crypto/src/aes/aescrypt.c +307 -0
- package/sdk/deps/trezor-crypto/src/aes/aeskey.c +560 -0
- package/sdk/deps/trezor-crypto/src/aes/aesopt.h +784 -0
- package/sdk/deps/trezor-crypto/src/aes/aestab.c +418 -0
- package/sdk/deps/trezor-crypto/src/aes/aestab.h +173 -0
- package/sdk/deps/trezor-crypto/src/base32.c +233 -0
- package/sdk/deps/trezor-crypto/src/base58.c +298 -0
- package/sdk/deps/trezor-crypto/src/bignum.c +808 -0
- package/sdk/deps/trezor-crypto/src/bip32.c +852 -0
- package/sdk/deps/trezor-crypto/src/bip39.c +266 -0
- package/sdk/deps/trezor-crypto/src/bip39_english.h +2074 -0
- package/sdk/deps/trezor-crypto/src/blake256.c +235 -0
- package/sdk/deps/trezor-crypto/src/blake2_common.h +39 -0
- package/sdk/deps/trezor-crypto/src/blake2b.c +333 -0
- package/sdk/deps/trezor-crypto/src/blake2s.c +318 -0
- package/sdk/deps/trezor-crypto/src/cash_addr.c +195 -0
- package/sdk/deps/trezor-crypto/src/chacha20poly1305/chacha20poly1305.c +60 -0
- package/sdk/deps/trezor-crypto/src/chacha20poly1305/chacha_merged.c +246 -0
- package/sdk/deps/trezor-crypto/src/chacha20poly1305/ecrypt-machine.h +49 -0
- package/sdk/deps/trezor-crypto/src/chacha20poly1305/ecrypt-portable.h +275 -0
- package/sdk/deps/trezor-crypto/src/chacha20poly1305/poly1305-donna-32.h +219 -0
- package/sdk/deps/trezor-crypto/src/chacha20poly1305/poly1305-donna.c +179 -0
- package/sdk/deps/trezor-crypto/src/chacha20poly1305/rfc7539.c +48 -0
- package/sdk/deps/trezor-crypto/src/chacha20poly1305/rfc7539.h +10 -0
- package/sdk/deps/trezor-crypto/src/check_mem.h +30 -0
- package/sdk/deps/trezor-crypto/src/cmac.c +342 -0
- package/sdk/deps/trezor-crypto/src/curves.c +48 -0
- package/sdk/deps/trezor-crypto/src/ecdsa.c +1355 -0
- package/sdk/deps/trezor-crypto/src/ed25519-donna/README.md +183 -0
- package/sdk/deps/trezor-crypto/src/ed25519-donna/curve25519-donna-32bit.c +681 -0
- package/sdk/deps/trezor-crypto/src/ed25519-donna/curve25519-donna-helpers.c +66 -0
- package/sdk/deps/trezor-crypto/src/ed25519-donna/curve25519-donna-scalarmult-base.c +67 -0
- package/sdk/deps/trezor-crypto/src/ed25519-donna/curve25519-donna-scalarmult-base.h +8 -0
- package/sdk/deps/trezor-crypto/src/ed25519-donna/ed25519-blake2b.c +8 -0
- package/sdk/deps/trezor-crypto/src/ed25519-donna/ed25519-donna-32bit-tables.c +63 -0
- package/sdk/deps/trezor-crypto/src/ed25519-donna/ed25519-donna-basepoint-table.c +261 -0
- package/sdk/deps/trezor-crypto/src/ed25519-donna/ed25519-donna-impl-base.c +722 -0
- package/sdk/deps/trezor-crypto/src/ed25519-donna/ed25519-hash-custom-blake2b.h +23 -0
- package/sdk/deps/trezor-crypto/src/ed25519-donna/ed25519-hash-custom-keccak.h +23 -0
- package/sdk/deps/trezor-crypto/src/ed25519-donna/ed25519-hash-custom-sha3.h +23 -0
- package/sdk/deps/trezor-crypto/src/ed25519-donna/ed25519-hash-custom.h +23 -0
- package/sdk/deps/trezor-crypto/src/ed25519-donna/ed25519-keccak.c +8 -0
- package/sdk/deps/trezor-crypto/src/ed25519-donna/ed25519-sha3.c +8 -0
- package/sdk/deps/trezor-crypto/src/ed25519-donna/ed25519.c +311 -0
- package/sdk/deps/trezor-crypto/src/ed25519-donna/modm-donna-32bit.c +517 -0
- package/sdk/deps/trezor-crypto/src/groestl.c +783 -0
- package/sdk/deps/trezor-crypto/src/groestl_internal.h +419 -0
- package/sdk/deps/trezor-crypto/src/hasher.c +186 -0
- package/sdk/deps/trezor-crypto/src/hmac.c +179 -0
- package/sdk/deps/trezor-crypto/src/memzero.c +6 -0
- package/sdk/deps/trezor-crypto/src/monero/base58.c +245 -0
- package/sdk/deps/trezor-crypto/src/monero/int-util.h +77 -0
- package/sdk/deps/trezor-crypto/src/monero/range_proof.c +115 -0
- package/sdk/deps/trezor-crypto/src/monero/serialize.c +54 -0
- package/sdk/deps/trezor-crypto/src/monero/xmr.c +136 -0
- package/sdk/deps/trezor-crypto/src/nano.c +151 -0
- package/sdk/deps/trezor-crypto/src/nem.c +569 -0
- package/sdk/deps/trezor-crypto/src/nist256p1.c +63 -0
- package/sdk/deps/trezor-crypto/src/nist256p1.table +1664 -0
- package/sdk/deps/trezor-crypto/src/options.h +43 -0
- package/sdk/deps/trezor-crypto/src/pbkdf2.c +177 -0
- package/sdk/deps/trezor-crypto/src/rand.c +51 -0
- package/sdk/deps/trezor-crypto/src/rc4.c +56 -0
- package/sdk/deps/trezor-crypto/src/ripemd160.c +343 -0
- package/sdk/deps/trezor-crypto/src/ripple/base58.c +76 -0
- package/sdk/deps/trezor-crypto/src/schnorr.c +208 -0
- package/sdk/deps/trezor-crypto/src/script.c +64 -0
- package/sdk/deps/trezor-crypto/src/scrypt.c +343 -0
- package/sdk/deps/trezor-crypto/src/secp256k1.c +160 -0
- package/sdk/deps/trezor-crypto/src/secp256k1.table +1664 -0
- package/sdk/deps/trezor-crypto/src/setup.py +38 -0
- package/sdk/deps/trezor-crypto/src/sha2.c +1314 -0
- package/sdk/deps/trezor-crypto/src/sha3.c +395 -0
- package/sdk/deps/trezor-crypto/src/shell.nix +6 -0
- package/sdk/deps/trezor-crypto/src/sodium/keypair.c +63 -0
- package/sdk/deps/trezor-crypto/src/sodium/private/ed25519_ref10.c +481 -0
- package/sdk/deps/trezor-crypto/src/sodium/private/ed25519_ref10_fe_25_5.c +885 -0
- package/sdk/deps/trezor-crypto/src/sodium/private/fe_25_5/fe.c +237 -0
- package/sdk/gpc/include/scp03/scp03.hpp +80 -0
- package/sdk/gpc/include/scp11/scp11.hpp +492 -0
- package/sdk/gpc/include/scp11/scp11c.hpp +58 -0
- package/sdk/gpc/include/tag.h +68 -0
- package/sdk/gpc/include/tlv.hpp +94 -0
- package/sdk/gpc/include/tlvparse.hpp +24 -0
- package/sdk/gpc/src/scp03.cpp +243 -0
- package/sdk/gpc/src/scp11.cpp +798 -0
- package/sdk/gpc/src/scp11c.cpp +136 -0
- package/sdk/gpc/src/tlv.cpp +159 -0
- package/sdk/gpc/src/tlvparse.c +174 -0
- package/{framework/GPChannelSDKCore.xcframework/ios-arm64/GPChannelSDKCore.framework/Headers → sdk/include}/GPChannelSDK.h +0 -0
- package/sdk/include/GPChannelSDKCore.h +10 -0
- package/sdk/include/context/BaseContext.h +56 -0
- package/sdk/include/device/ApduBuilder.hpp +72 -0
- package/sdk/include/mSIGNA/stdutils/uchar_vector.h +36 -0
- package/sdk/include/utility/Apdu.hpp +172 -0
- package/sdk/include/utility/Debug.hpp +3 -0
- package/sdk/include/utility/util.h +104 -0
- package/sdk/src/ApduBuilder.cpp +332 -0
- package/sdk/src/GPChannelSDK.cpp +252 -0
- package/sdk/src/random.c +25 -0
- package/sdk/src/version.c +2 -0
- package/sdk/tests/android/GPChannelProxy.java +55 -0
- package/sdk/tests/android_verify.py +121 -0
- package/sdk/tests/safety.cpp +603 -0
- package/sdk/tests/verify.py +267 -0
- package/android/src/main/jniLibs/arm64-v8a/libgpchannelNDK.so +0 -0
- package/android/src/main/jniLibs/armeabi-v7a/libgpchannelNDK.so +0 -0
- package/framework/GPChannelSDKCore.xcframework/Info.plist +0 -52
- package/framework/GPChannelSDKCore.xcframework/ios-arm64/GPChannelSDKCore.framework/GPChannelSDKCore +0 -0
- package/framework/GPChannelSDKCore.xcframework/ios-arm64/GPChannelSDKCore.framework/Headers/GPChannelSDKCore.h +0 -19
- package/framework/GPChannelSDKCore.xcframework/ios-arm64/GPChannelSDKCore.framework/Info.plist +0 -0
- package/framework/GPChannelSDKCore.xcframework/ios-arm64/GPChannelSDKCore.framework/Modules/module.modulemap +0 -7
- package/framework/GPChannelSDKCore.xcframework/ios-arm64/GPChannelSDKCore.framework/_CodeSignature/CodeResources +0 -147
- package/framework/GPChannelSDKCore.xcframework/ios-arm64_x86_64-simulator/GPChannelSDKCore.framework/GPChannelSDKCore +0 -0
- package/framework/GPChannelSDKCore.xcframework/ios-arm64_x86_64-simulator/GPChannelSDKCore.framework/Headers/GPChannelSDK.h +0 -315
- package/framework/GPChannelSDKCore.xcframework/ios-arm64_x86_64-simulator/GPChannelSDKCore.framework/Headers/GPChannelSDKCore.h +0 -19
- package/framework/GPChannelSDKCore.xcframework/ios-arm64_x86_64-simulator/GPChannelSDKCore.framework/Info.plist +0 -0
- package/framework/GPChannelSDKCore.xcframework/ios-arm64_x86_64-simulator/GPChannelSDKCore.framework/Modules/module.modulemap +0 -7
- package/framework/GPChannelSDKCore.xcframework/ios-arm64_x86_64-simulator/GPChannelSDKCore.framework/_CodeSignature/CodeResources +0 -147
|
@@ -0,0 +1,603 @@
|
|
|
1
|
+
#include "GPChannelSDK.h"
|
|
2
|
+
#include <TrezorCrypto/aes.h>
|
|
3
|
+
#include <TrezorCrypto/cmac.h>
|
|
4
|
+
#include <TrezorCrypto/ecdsa.h>
|
|
5
|
+
#include <TrezorCrypto/nist256p1.h>
|
|
6
|
+
#include <TrezorCrypto/sha2.h>
|
|
7
|
+
#include <algorithm>
|
|
8
|
+
#include <cstdlib>
|
|
9
|
+
#include <cstring>
|
|
10
|
+
#include <iostream>
|
|
11
|
+
#include <map>
|
|
12
|
+
#include <random>
|
|
13
|
+
#include <string>
|
|
14
|
+
#include <vector>
|
|
15
|
+
#ifdef GPCHANNEL_TEST_JNI
|
|
16
|
+
#include <jni.h>
|
|
17
|
+
#endif
|
|
18
|
+
|
|
19
|
+
static void require(bool condition, const char* message = "Safety contract failed") {
|
|
20
|
+
if (!condition) {
|
|
21
|
+
std::cerr << message << "\n";
|
|
22
|
+
std::abort();
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
static std::string hex(const unsigned char* data, size_t len) {
|
|
27
|
+
const char* digits = "0123456789abcdef";
|
|
28
|
+
std::string result;
|
|
29
|
+
result.reserve(len * 2);
|
|
30
|
+
for (size_t i = 0; i < len; ++i) {
|
|
31
|
+
unsigned char byte = data[i];
|
|
32
|
+
result += digits[byte >> 4];
|
|
33
|
+
result += digits[byte & 15];
|
|
34
|
+
}
|
|
35
|
+
return result;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
static std::string hex(const std::string& data) {
|
|
39
|
+
return hex(reinterpret_cast<const unsigned char*>(data.data()), data.size());
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
static std::vector<unsigned char> unhex(const std::string& text) {
|
|
43
|
+
require(text.size() % 2 == 0, "Hex string must have even length");
|
|
44
|
+
std::vector<unsigned char> out(text.size() / 2, 0);
|
|
45
|
+
for (size_t i = 0; i < out.size(); ++i) {
|
|
46
|
+
auto nibble = [](char c) -> unsigned char {
|
|
47
|
+
if (c >= '0' && c <= '9') return static_cast<unsigned char>(c - '0');
|
|
48
|
+
if (c >= 'a' && c <= 'f') return static_cast<unsigned char>(c - 'a' + 10);
|
|
49
|
+
if (c >= 'A' && c <= 'F') return static_cast<unsigned char>(c - 'A' + 10);
|
|
50
|
+
require(false, "Invalid hex digit");
|
|
51
|
+
return 0;
|
|
52
|
+
};
|
|
53
|
+
out[i] = static_cast<unsigned char>((nibble(text[2 * i]) << 4) | nibble(text[2 * i + 1]));
|
|
54
|
+
}
|
|
55
|
+
return out;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
static std::string makeTlv(unsigned int tag, const std::string& bodyHex) {
|
|
59
|
+
size_t len = bodyHex.size() / 2;
|
|
60
|
+
std::vector<unsigned char> prefix;
|
|
61
|
+
if (tag > 0xff) {
|
|
62
|
+
prefix.push_back(static_cast<unsigned char>((tag >> 8) & 0xff));
|
|
63
|
+
prefix.push_back(static_cast<unsigned char>(tag & 0xff));
|
|
64
|
+
} else {
|
|
65
|
+
prefix.push_back(static_cast<unsigned char>(tag & 0xff));
|
|
66
|
+
}
|
|
67
|
+
if (tag == 0x7f21 || tag == 0xbf21) {
|
|
68
|
+
if (len <= 0xff) {
|
|
69
|
+
prefix.push_back(0x81);
|
|
70
|
+
prefix.push_back(static_cast<unsigned char>(len));
|
|
71
|
+
} else {
|
|
72
|
+
prefix.push_back(0x82);
|
|
73
|
+
prefix.push_back(static_cast<unsigned char>((len >> 8) & 0xff));
|
|
74
|
+
prefix.push_back(static_cast<unsigned char>(len & 0xff));
|
|
75
|
+
}
|
|
76
|
+
} else {
|
|
77
|
+
prefix.push_back(static_cast<unsigned char>(len));
|
|
78
|
+
}
|
|
79
|
+
return hex(prefix.data(), prefix.size()) + bodyHex;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
static std::string publicKeyHexFromScalar(const std::string& scalarHex) {
|
|
83
|
+
std::vector<unsigned char> priv = unhex(scalarHex);
|
|
84
|
+
require(priv.size() == 32, "Scalar must be 32 bytes");
|
|
85
|
+
unsigned char pub[65] = {0};
|
|
86
|
+
ecdsa_get_public_key65(&nist256p1, priv.data(), pub);
|
|
87
|
+
return hex(pub, sizeof(pub));
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
static std::string buildCertificate(const std::string& pubKeyHex, size_t targetInnerBytes = 0) {
|
|
91
|
+
std::string inner;
|
|
92
|
+
inner += makeTlv(0x93, "30313233343536373839");
|
|
93
|
+
inner += makeTlv(0x42, "43414b4c4f43");
|
|
94
|
+
inner += makeTlv(0x5f20, "5355424a3031");
|
|
95
|
+
inner += makeTlv(0x95, "0080");
|
|
96
|
+
inner += makeTlv(0x5f25, "20240101");
|
|
97
|
+
inner += makeTlv(0x5f24, "20290101");
|
|
98
|
+
inner += makeTlv(0x7f49, makeTlv(0xb0, pubKeyHex) + makeTlv(0xf0, "00"));
|
|
99
|
+
inner += makeTlv(0x5f37, "30440220" + std::string(64, 'a') + "0220" + std::string(64, 'b'));
|
|
100
|
+
if (targetInnerBytes > 0) {
|
|
101
|
+
size_t currentBytes = inner.size() / 2;
|
|
102
|
+
require(targetInnerBytes >= currentBytes + 2, "targetInnerBytes too small");
|
|
103
|
+
size_t padLen = targetInnerBytes - currentBytes - 2;
|
|
104
|
+
inner += makeTlv(0x53, std::string(padLen * 2, '0'));
|
|
105
|
+
require(inner.size() / 2 == targetInnerBytes, "Certificate inner size mismatch");
|
|
106
|
+
} else {
|
|
107
|
+
inner += makeTlv(0x53, "");
|
|
108
|
+
}
|
|
109
|
+
return makeTlv(0x7f21, inner);
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
static GPC_SCP11_SHAREDINFO defaultSharedInfo() {
|
|
113
|
+
GPC_SCP11_SHAREDINFO info;
|
|
114
|
+
info.scpID = const_cast<char*>("1107");
|
|
115
|
+
info.keyUsage = const_cast<char*>("3c");
|
|
116
|
+
info.keyType = const_cast<char*>("88");
|
|
117
|
+
info.keyLength = const_cast<char*>("10");
|
|
118
|
+
info.hostID = const_cast<char*>("8080808080808080");
|
|
119
|
+
info.cardGroupID = const_cast<char*>("5355424a3031");
|
|
120
|
+
return info;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
static std::string buildCardMutualAuthResponse(
|
|
124
|
+
const std::string& cardPrivHex,
|
|
125
|
+
const std::string& ocePubHex,
|
|
126
|
+
const std::string& mutualAuthCmdHex,
|
|
127
|
+
std::vector<unsigned char>* derivedKeysOut = nullptr) {
|
|
128
|
+
std::vector<unsigned char> cardPriv = unhex(cardPrivHex);
|
|
129
|
+
std::vector<unsigned char> oceStaticPub = unhex(ocePubHex);
|
|
130
|
+
std::vector<unsigned char> maData = unhex(mutualAuthCmdHex);
|
|
131
|
+
require(maData.size() >= 65, "Mutual auth data too short");
|
|
132
|
+
std::vector<unsigned char> oceEphPub(maData.end() - 65, maData.end());
|
|
133
|
+
|
|
134
|
+
unsigned char cardPub[65] = {0};
|
|
135
|
+
ecdsa_get_public_key65(&nist256p1, cardPriv.data(), cardPub);
|
|
136
|
+
|
|
137
|
+
unsigned char sharedStatic[65] = {0};
|
|
138
|
+
unsigned char sharedEph[65] = {0};
|
|
139
|
+
require(ecdh_multiply(&nist256p1, cardPriv.data(), oceStaticPub.data(), sharedStatic) == 0);
|
|
140
|
+
require(ecdh_multiply(&nist256p1, cardPriv.data(), oceEphPub.data(), sharedEph) == 0);
|
|
141
|
+
|
|
142
|
+
unsigned char shSss[20] = {0};
|
|
143
|
+
unsigned char shSes[20] = {0};
|
|
144
|
+
SHA1_CTX sha1;
|
|
145
|
+
sha1_Init(&sha1);
|
|
146
|
+
sha1_Update(&sha1, sharedStatic + 1, 32);
|
|
147
|
+
sha1_Final(&sha1, shSss);
|
|
148
|
+
|
|
149
|
+
sha1_Init(&sha1);
|
|
150
|
+
sha1_Update(&sha1, sharedEph + 1, 32);
|
|
151
|
+
sha1_Final(&sha1, shSes);
|
|
152
|
+
|
|
153
|
+
std::vector<unsigned char> z;
|
|
154
|
+
z.insert(z.end(), shSes, shSes + 20);
|
|
155
|
+
z.insert(z.end(), shSss, shSss + 20);
|
|
156
|
+
|
|
157
|
+
std::vector<unsigned char> sharedInfo = unhex("3c88100880808080808080805355424a3031");
|
|
158
|
+
std::vector<unsigned char> derived(80, 0);
|
|
159
|
+
for (unsigned int counter = 1; counter <= 3; ++counter) {
|
|
160
|
+
unsigned char ctrBe[4] = {
|
|
161
|
+
static_cast<unsigned char>((counter >> 24) & 0xff),
|
|
162
|
+
static_cast<unsigned char>((counter >> 16) & 0xff),
|
|
163
|
+
static_cast<unsigned char>((counter >> 8) & 0xff),
|
|
164
|
+
static_cast<unsigned char>(counter & 0xff),
|
|
165
|
+
};
|
|
166
|
+
unsigned char digest[32] = {0};
|
|
167
|
+
SHA256_CTX s256;
|
|
168
|
+
sha256_Init(&s256);
|
|
169
|
+
sha256_Update(&s256, z.data(), z.size());
|
|
170
|
+
sha256_Update(&s256, ctrBe, 4);
|
|
171
|
+
sha256_Update(&s256, sharedInfo.data(), sharedInfo.size());
|
|
172
|
+
sha256_Final(&s256, digest);
|
|
173
|
+
size_t offset = (counter - 1) * 32;
|
|
174
|
+
size_t copyLen = std::min<size_t>(32, 80 - offset);
|
|
175
|
+
std::memcpy(derived.data() + offset, digest, copyLen);
|
|
176
|
+
}
|
|
177
|
+
if (derivedKeysOut) {
|
|
178
|
+
*derivedKeysOut = derived;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
std::string cardTlvHex = makeTlv(0x5f49, hex(cardPub, sizeof(cardPub)));
|
|
182
|
+
std::vector<unsigned char> cardTlv = unhex(cardTlvHex);
|
|
183
|
+
std::vector<unsigned char> receiptInput = maData;
|
|
184
|
+
receiptInput.insert(receiptInput.end(), cardTlv.begin(), cardTlv.end());
|
|
185
|
+
|
|
186
|
+
unsigned char receipt[16] = {0};
|
|
187
|
+
AES_CMAC(derived.data(), receiptInput.data(), static_cast<unsigned>(receiptInput.size()), receipt);
|
|
188
|
+
return cardTlvHex + makeTlv(0x86, hex(receipt, sizeof(receipt)));
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
#ifdef GPCHANNEL_TEST_JNI
|
|
192
|
+
struct MockJniRuntime {
|
|
193
|
+
JNINativeInterface_ envTable;
|
|
194
|
+
JNIEnv env;
|
|
195
|
+
JNIInvokeInterface_ vmTable;
|
|
196
|
+
JavaVM vm;
|
|
197
|
+
std::map<std::string, void*> methods;
|
|
198
|
+
std::vector<std::string> allocatedStrings;
|
|
199
|
+
|
|
200
|
+
static MockJniRuntime*& active() {
|
|
201
|
+
static MockJniRuntime* instance = nullptr;
|
|
202
|
+
return instance;
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
static jint JNICALL mockGetEnv(JavaVM*, void** penv, jint) {
|
|
206
|
+
*penv = &active()->env;
|
|
207
|
+
return JNI_OK;
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
static jclass JNICALL mockFindClass(JNIEnv*, const char* name) {
|
|
211
|
+
require(name && std::string(name) == "org/haobtc/onekey/card/gpchannel/GPChannelNatives",
|
|
212
|
+
"Unexpected JNI class lookup");
|
|
213
|
+
return reinterpret_cast<jclass>(0x1);
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
static jint JNICALL mockRegisterNatives(JNIEnv*, jclass, const JNINativeMethod* methods, jint count) {
|
|
217
|
+
for (jint i = 0; i < count; ++i) {
|
|
218
|
+
active()->methods[methods[i].name] = methods[i].fnPtr;
|
|
219
|
+
}
|
|
220
|
+
return JNI_OK;
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
static void JNICALL mockDeleteLocalRef(JNIEnv*, jobject) {}
|
|
224
|
+
|
|
225
|
+
static const char* JNICALL mockGetStringUTFChars(JNIEnv*, jstring str, jboolean* isCopy) {
|
|
226
|
+
if (isCopy) *isCopy = JNI_FALSE;
|
|
227
|
+
return str ? reinterpret_cast<const std::string*>(str)->c_str() : nullptr;
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
static void JNICALL mockReleaseStringUTFChars(JNIEnv*, jstring, const char*) {}
|
|
231
|
+
|
|
232
|
+
static jstring JNICALL mockNewStringUTF(JNIEnv*, const char* utf) {
|
|
233
|
+
if (!utf) return nullptr;
|
|
234
|
+
active()->allocatedStrings.push_back(std::string(utf));
|
|
235
|
+
return reinterpret_cast<jstring>(&active()->allocatedStrings.back());
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
MockJniRuntime() {
|
|
239
|
+
std::memset(&envTable, 0, sizeof(envTable));
|
|
240
|
+
std::memset(&vmTable, 0, sizeof(vmTable));
|
|
241
|
+
allocatedStrings.reserve(256);
|
|
242
|
+
envTable.FindClass = &mockFindClass;
|
|
243
|
+
envTable.RegisterNatives = &mockRegisterNatives;
|
|
244
|
+
envTable.DeleteLocalRef = &mockDeleteLocalRef;
|
|
245
|
+
envTable.GetStringUTFChars = &mockGetStringUTFChars;
|
|
246
|
+
envTable.ReleaseStringUTFChars = &mockReleaseStringUTFChars;
|
|
247
|
+
envTable.NewStringUTF = &mockNewStringUTF;
|
|
248
|
+
env.functions = &envTable;
|
|
249
|
+
vmTable.GetEnv = &mockGetEnv;
|
|
250
|
+
vm.functions = &vmTable;
|
|
251
|
+
active() = this;
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
jstring makeJString(const std::string& s) {
|
|
255
|
+
allocatedStrings.push_back(s);
|
|
256
|
+
return reinterpret_cast<jstring>(&allocatedStrings.back());
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
static std::string readJString(jstring str) {
|
|
260
|
+
require(str != nullptr, "Expected non-null jstring");
|
|
261
|
+
return *reinterpret_cast<const std::string*>(str);
|
|
262
|
+
}
|
|
263
|
+
};
|
|
264
|
+
#endif
|
|
265
|
+
|
|
266
|
+
int main() {
|
|
267
|
+
// 1. Basic lifecycle & allocation checks
|
|
268
|
+
require(JUB_GPC_Finalize() == 0);
|
|
269
|
+
require(JUB_GPC_Finalize() == 0);
|
|
270
|
+
require(JUB_FreeMemory(nullptr) == JUBR_MEMORY_NULL_PTR);
|
|
271
|
+
require(JUB_GPC_BuildAPDU(0, 0, 0, 0, "", nullptr) == JUBR_MEMORY_NULL_PTR);
|
|
272
|
+
char* output = nullptr;
|
|
273
|
+
require(JUB_GPC_BuildMutualAuthData(&output) == JUBR_CONTEXT_NOT_SATISFIED);
|
|
274
|
+
require(JUB_GPC_OpenSecureChannel("") == JUBR_CONTEXT_NOT_SATISFIED);
|
|
275
|
+
require(JUB_GPC_BuildSafeAPDU(0, 0, 0, 0, "", &output) == JUBR_CONTEXT_NOT_SATISFIED);
|
|
276
|
+
require(JUB_GPC_BuildAPDU(0, 0, 0, 0, "zz", &output) == JUBR_ARGUMENTS_BAD);
|
|
277
|
+
JUB_UINT16 status = 0;
|
|
278
|
+
require(JUB_GPC_ParseAPDUResponse("9000", &status, &output) == 0);
|
|
279
|
+
require(status == 0x9000 && output && !*output);
|
|
280
|
+
require(JUB_FreeMemory(output) == 0);
|
|
281
|
+
require(JUB_FreeMemory(output) == JUBR_INVALID_MEMORY_PTR);
|
|
282
|
+
|
|
283
|
+
// 2. Scalar & public key curve validation (LITE-08)
|
|
284
|
+
const std::string validOcePriv = "0000000000000000000000000000000000000000000000000000000000000001";
|
|
285
|
+
const std::string validCardPriv = "0000000000000000000000000000000000000000000000000000000000000002";
|
|
286
|
+
const std::string zeroPriv = "0000000000000000000000000000000000000000000000000000000000000000";
|
|
287
|
+
const std::string orderPriv = "ffffffff00000000ffffffffffffffffbce6faada7179e84f3b9cac2fc632551";
|
|
288
|
+
const std::string allOnesPriv = "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff";
|
|
289
|
+
const std::string offCurvePub = "04" + std::string(128, '1');
|
|
290
|
+
|
|
291
|
+
GPC_SCP11_SHAREDINFO info = defaultSharedInfo();
|
|
292
|
+
std::string ocePub = publicKeyHexFromScalar(validOcePriv);
|
|
293
|
+
std::string validCert = buildCertificate(ocePub);
|
|
294
|
+
std::string offCurveCert = buildCertificate(offCurvePub);
|
|
295
|
+
|
|
296
|
+
require(JUB_GPC_Initialize(info, validCert.c_str(), validOcePriv.c_str()) == JUBR_OK,
|
|
297
|
+
"Valid scalar and certificate should initialize");
|
|
298
|
+
require(JUB_GPC_Finalize() == JUBR_OK);
|
|
299
|
+
|
|
300
|
+
require(JUB_GPC_Initialize(info, validCert.c_str(), zeroPriv.c_str()) == JUBR_ARGUMENTS_BAD,
|
|
301
|
+
"All-zero private scalar must be rejected");
|
|
302
|
+
require(JUB_GPC_Initialize(info, validCert.c_str(), orderPriv.c_str()) == JUBR_ARGUMENTS_BAD,
|
|
303
|
+
"Private scalar == curve order must be rejected");
|
|
304
|
+
require(JUB_GPC_Initialize(info, validCert.c_str(), allOnesPriv.c_str()) == JUBR_ARGUMENTS_BAD,
|
|
305
|
+
"Private scalar > curve order must be rejected");
|
|
306
|
+
require(JUB_GPC_Initialize(info, offCurveCert.c_str(), validOcePriv.c_str()) == JUBR_ARGUMENTS_BAD,
|
|
307
|
+
"Off-curve certificate public key must be rejected in Initialize");
|
|
308
|
+
|
|
309
|
+
char* serial = nullptr;
|
|
310
|
+
char* subject = nullptr;
|
|
311
|
+
require(JUB_GPC_ParseCertificate(offCurveCert.c_str(), &serial, &subject) == JUBR_ARGUMENTS_BAD,
|
|
312
|
+
"Off-curve certificate public key must be rejected in ParseCertificate");
|
|
313
|
+
require(!serial && !subject);
|
|
314
|
+
|
|
315
|
+
// Off-curve card ephemeral public key in OpenSecureChannel must be rejected
|
|
316
|
+
require(JUB_GPC_Initialize(info, validCert.c_str(), validOcePriv.c_str()) == JUBR_OK);
|
|
317
|
+
output = nullptr;
|
|
318
|
+
require(JUB_GPC_BuildMutualAuthData(&output) == JUBR_OK && output);
|
|
319
|
+
require(JUB_FreeMemory(output) == JUBR_OK);
|
|
320
|
+
std::string offCurveAuthResp = makeTlv(0x5f49, offCurvePub) + makeTlv(0x86, std::string(32, '0'));
|
|
321
|
+
require(JUB_GPC_OpenSecureChannel(offCurveAuthResp.c_str()) == JUBR_ARGUMENTS_BAD,
|
|
322
|
+
"Off-curve 5F49 public key must be rejected in OpenSecureChannel");
|
|
323
|
+
require(JUB_GPC_Finalize() == JUBR_OK);
|
|
324
|
+
|
|
325
|
+
// 3. Recursive TLV parser skip wraparound & bounds (LITE-03)
|
|
326
|
+
// 1-byte tag 0x93 with length 0xFE (254): skip = 2 + 254 = 256 (wrapped uint8_t to 0 before fix)
|
|
327
|
+
std::string certSkipWrap256 = makeTlv(0x7f21, "93fe" + std::string(254 * 2, '0'));
|
|
328
|
+
require(JUB_GPC_ParseCertificate(certSkipWrap256.c_str(), &serial, &subject) == JUBR_ARGUMENTS_BAD,
|
|
329
|
+
"TLV skip == 256 in certificate must not wrap or crash");
|
|
330
|
+
require(!serial && !subject);
|
|
331
|
+
|
|
332
|
+
// 2-byte tag 0x5F20 with length 0xFD (253): skip = 3 + 253 = 256 (wrapped uint8_t to 0 before fix)
|
|
333
|
+
std::string certTwoByteTagWrap256 = makeTlv(0x7f21, "5f20fd" + std::string(253 * 2, '0'));
|
|
334
|
+
require(JUB_GPC_ParseCertificate(certTwoByteTagWrap256.c_str(), &serial, &subject) == JUBR_ARGUMENTS_BAD,
|
|
335
|
+
"TLV skip == 256 on 2-byte certificate tag must not wrap or crash");
|
|
336
|
+
require(!serial && !subject);
|
|
337
|
+
|
|
338
|
+
// Nested public-key tag 0xB0 with length 0xFE (254) inside 0x7F49 (length 0xFF = 255):
|
|
339
|
+
// tests scp11_pk::decode when skip = 2 + 254 = 256
|
|
340
|
+
std::string pkSkipWrap256 = makeTlv(0x7f21, "7f49ffb0fe" + std::string(253 * 2, '0'));
|
|
341
|
+
require(JUB_GPC_ParseCertificate(pkSkipWrap256.c_str(), &serial, &subject) == JUBR_ARGUMENTS_BAD,
|
|
342
|
+
"TLV skip == 256 in scp11_pk::decode must not wrap or crash");
|
|
343
|
+
require(!serial && !subject);
|
|
344
|
+
|
|
345
|
+
// 2-byte tag 0x5F49 with length 0xFD (253): skip = 3 + 253 = 256 (wrapped uint8_t to 0 before fix)
|
|
346
|
+
require(JUB_GPC_Initialize(info, validCert.c_str(), validOcePriv.c_str()) == JUBR_OK);
|
|
347
|
+
output = nullptr;
|
|
348
|
+
require(JUB_GPC_BuildMutualAuthData(&output) == JUBR_OK && output);
|
|
349
|
+
require(JUB_FreeMemory(output) == JUBR_OK);
|
|
350
|
+
std::string maSkipWrap256 = "5f49fd" + std::string(253 * 2, '0');
|
|
351
|
+
require(JUB_GPC_OpenSecureChannel(maSkipWrap256.c_str()) == JUBR_ARGUMENTS_BAD,
|
|
352
|
+
"TLV skip == 256 in OpenSecureChannel must not wrap or crash");
|
|
353
|
+
require(JUB_GPC_Finalize() == JUBR_OK);
|
|
354
|
+
|
|
355
|
+
// 4. 7F21 certificate 256-byte boundary & trailing TLV smuggling (LITE-07)
|
|
356
|
+
for (size_t innerLen : {static_cast<size_t>(255), static_cast<size_t>(256), static_cast<size_t>(257)}) {
|
|
357
|
+
std::string sizedCert = buildCertificate(ocePub, innerLen);
|
|
358
|
+
serial = nullptr;
|
|
359
|
+
subject = nullptr;
|
|
360
|
+
require(JUB_GPC_ParseCertificate(sizedCert.c_str(), &serial, &subject) == JUBR_OK,
|
|
361
|
+
"Valid certificate with 255/256/257-byte inner body must succeed");
|
|
362
|
+
require(serial && std::string(serial) == "0123456789");
|
|
363
|
+
require(subject && std::string(subject) == "5355424a3031");
|
|
364
|
+
require(JUB_FreeMemory(serial) == JUBR_OK);
|
|
365
|
+
require(JUB_FreeMemory(subject) == JUBR_OK);
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
std::string smuggledCert =
|
|
369
|
+
"7f2181025300" +
|
|
370
|
+
makeTlv(0x93, "534d5547474c45445f534e") +
|
|
371
|
+
makeTlv(0x42, "4341") +
|
|
372
|
+
makeTlv(0x5f20, "deadbeef") +
|
|
373
|
+
makeTlv(0x95, "0080") +
|
|
374
|
+
makeTlv(0x5f25, "20240101") +
|
|
375
|
+
makeTlv(0x5f24, "20290101") +
|
|
376
|
+
makeTlv(0x7f49, makeTlv(0xb0, ocePub) + makeTlv(0xf0, "00")) +
|
|
377
|
+
makeTlv(0x5f37, "30440220" + std::string(64, 'a') + "0220" + std::string(64, 'b'));
|
|
378
|
+
serial = nullptr;
|
|
379
|
+
subject = nullptr;
|
|
380
|
+
require(JUB_GPC_ParseCertificate(smuggledCert.c_str(), &serial, &subject) == JUBR_ARGUMENTS_BAD,
|
|
381
|
+
"7F21 certificate with trailing smuggled TLV tags must be rejected");
|
|
382
|
+
require(!serial && !subject);
|
|
383
|
+
|
|
384
|
+
// 5. Session state lifecycle & full SCP11c + SCP03 roundtrip (LITE-09)
|
|
385
|
+
require(JUB_GPC_Initialize(info, validCert.c_str(), validOcePriv.c_str()) == JUBR_OK);
|
|
386
|
+
char* maCmdHex = nullptr;
|
|
387
|
+
require(JUB_GPC_BuildMutualAuthData(&maCmdHex) == JUBR_OK && maCmdHex);
|
|
388
|
+
std::string maCmdStr(maCmdHex);
|
|
389
|
+
require(JUB_FreeMemory(maCmdHex) == JUBR_OK);
|
|
390
|
+
|
|
391
|
+
std::vector<unsigned char> derivedKeys;
|
|
392
|
+
std::string maRespHex = buildCardMutualAuthResponse(validCardPriv, ocePub, maCmdStr, &derivedKeys);
|
|
393
|
+
require(JUB_GPC_OpenSecureChannel(maRespHex.c_str()) == JUBR_OK,
|
|
394
|
+
"Valid mutual authentication response must open secure channel");
|
|
395
|
+
|
|
396
|
+
// Encrypted APDU build must succeed while session is open
|
|
397
|
+
char* safeApduHex = nullptr;
|
|
398
|
+
require(JUB_GPC_BuildSafeAPDU(0x80, 0x20, 0x00, 0x00, "06313233343536", &safeApduHex) == JUBR_OK && safeApduHex);
|
|
399
|
+
std::vector<unsigned char> safeApduBytes = unhex(safeApduHex);
|
|
400
|
+
require(JUB_FreeMemory(safeApduHex) == JUBR_OK);
|
|
401
|
+
require(safeApduBytes.size() == 5 + 16 + 8 && safeApduBytes[0] == 0x84);
|
|
402
|
+
|
|
403
|
+
// Verify JUB_GPC_ParseSafeAPDUResponse with valid RMAC, tampered RMAC, and status-only error:
|
|
404
|
+
// derivedKeys layout: [0..16]=key_dek, [16..32]=s_enc, [32..48]=s_mac, [48..64]=s_rmac
|
|
405
|
+
// After BuildSafeAPDU #1 (counter == 1), the SDK's 16-byte chaining CMAC is computed over:
|
|
406
|
+
// receipt(16B) || 84 20 00 00 18 || ciphertext(16B)
|
|
407
|
+
std::vector<unsigned char> maRespBytes = unhex(maRespHex);
|
|
408
|
+
std::vector<unsigned char> receiptBytes(maRespBytes.end() - 16, maRespBytes.end());
|
|
409
|
+
std::vector<unsigned char> cmacInput = receiptBytes;
|
|
410
|
+
cmacInput.insert(cmacInput.end(), safeApduBytes.begin(), safeApduBytes.begin() + 5 + 16);
|
|
411
|
+
unsigned char cmdChain[16] = {0};
|
|
412
|
+
AES_CMAC(derivedKeys.data() + 32, cmacInput.data(), static_cast<unsigned>(cmacInput.size()), cmdChain);
|
|
413
|
+
require(std::memcmp(cmdChain, safeApduBytes.data() + 5 + 16, 8) == 0,
|
|
414
|
+
"Command CMAC prefix must match independent AES-CMAC");
|
|
415
|
+
|
|
416
|
+
// Construct encrypted response for counter == 2 (incCounter() pre-increments 1 -> 2):
|
|
417
|
+
// Response ICV = AES_ENC(s_enc, 80 00 ... 00 02)
|
|
418
|
+
aes_encrypt_ctx encCtx;
|
|
419
|
+
aes_encrypt_key128(derivedKeys.data() + 16, &encCtx);
|
|
420
|
+
unsigned char respCounterBlock[16] = {0};
|
|
421
|
+
respCounterBlock[0] = 0x80;
|
|
422
|
+
respCounterBlock[15] = 0x02;
|
|
423
|
+
unsigned char respIcv[16] = {0};
|
|
424
|
+
aes_ecb_encrypt(respCounterBlock, respIcv, 16, &encCtx);
|
|
425
|
+
|
|
426
|
+
// Plaintext response: "deadbeef" padded with 80 00... to 16 bytes
|
|
427
|
+
unsigned char respPadded[16] = {0xde, 0xad, 0xbe, 0xef, 0x80};
|
|
428
|
+
unsigned char respCipher[16] = {0};
|
|
429
|
+
unsigned char cbcIv[16] = {0};
|
|
430
|
+
std::memcpy(cbcIv, respIcv, 16);
|
|
431
|
+
aes_cbc_encrypt(respPadded, respCipher, 16, cbcIv, &encCtx);
|
|
432
|
+
|
|
433
|
+
// Response RMAC input: cmdChain(16B) || respCipher(16B) || 90 00
|
|
434
|
+
std::vector<unsigned char> rmacInput(cmdChain, cmdChain + 16);
|
|
435
|
+
rmacInput.insert(rmacInput.end(), respCipher, respCipher + 16);
|
|
436
|
+
rmacInput.push_back(0x90);
|
|
437
|
+
rmacInput.push_back(0x00);
|
|
438
|
+
unsigned char rmacFull[16] = {0};
|
|
439
|
+
AES_CMAC(derivedKeys.data() + 48, rmacInput.data(), static_cast<unsigned>(rmacInput.size()), rmacFull);
|
|
440
|
+
|
|
441
|
+
std::vector<unsigned char> validSafeResp(respCipher, respCipher + 16);
|
|
442
|
+
validSafeResp.insert(validSafeResp.end(), rmacFull, rmacFull + 8);
|
|
443
|
+
validSafeResp.push_back(0x90);
|
|
444
|
+
validSafeResp.push_back(0x00);
|
|
445
|
+
|
|
446
|
+
// Tampered 1-bit RMAC must be rejected with JUBR_ERROR
|
|
447
|
+
std::vector<unsigned char> tamperedSafeResp = validSafeResp;
|
|
448
|
+
tamperedSafeResp[tamperedSafeResp.size() - 3] ^= 0x01;
|
|
449
|
+
output = nullptr;
|
|
450
|
+
status = 0;
|
|
451
|
+
require(JUB_GPC_ParseSafeAPDUResponse(hex(tamperedSafeResp.data(), tamperedSafeResp.size()).c_str(), &status, &output) == JUBR_ERROR,
|
|
452
|
+
"Tampered RMAC response must be rejected");
|
|
453
|
+
require(!output);
|
|
454
|
+
|
|
455
|
+
// Unauthenticated plain "9000" on safe channel must be rejected
|
|
456
|
+
require(JUB_GPC_ParseSafeAPDUResponse("9000", &status, &output) != JUBR_OK,
|
|
457
|
+
"Unauthenticated 9000 response on safe channel must be rejected");
|
|
458
|
+
require(!output);
|
|
459
|
+
|
|
460
|
+
// Valid authenticated response must decrypt to "deadbeef" and status 0x9000
|
|
461
|
+
require(JUB_GPC_ParseSafeAPDUResponse(hex(validSafeResp.data(), validSafeResp.size()).c_str(), &status, &output) == JUBR_OK,
|
|
462
|
+
"Valid authenticated safe response must succeed");
|
|
463
|
+
require(status == 0x9000 && output && std::string(output) == "deadbeef");
|
|
464
|
+
require(JUB_FreeMemory(output) == JUBR_OK);
|
|
465
|
+
|
|
466
|
+
// Status-only card error "63c2" must return JUBR_OK with status 0x63c2 and empty output
|
|
467
|
+
output = nullptr;
|
|
468
|
+
status = 0;
|
|
469
|
+
require(JUB_GPC_ParseSafeAPDUResponse("63c2", &status, &output) == JUBR_OK,
|
|
470
|
+
"Status-only card error 63c2 must parse cleanly");
|
|
471
|
+
require(status == 0x63c2 && output && std::string(output).empty());
|
|
472
|
+
require(JUB_FreeMemory(output) == JUBR_OK);
|
|
473
|
+
|
|
474
|
+
// Failed re-initialization must clear the open session
|
|
475
|
+
require(JUB_GPC_Initialize(info, "deadbeef", validOcePriv.c_str()) == JUBR_ARGUMENTS_BAD);
|
|
476
|
+
output = nullptr;
|
|
477
|
+
require(JUB_GPC_BuildSafeAPDU(0x80, 0x20, 0x00, 0x00, "06313233343536", &output) == JUBR_CONTEXT_NOT_SATISFIED,
|
|
478
|
+
"Failed JUB_GPC_Initialize must clear previous open session");
|
|
479
|
+
require(!output);
|
|
480
|
+
|
|
481
|
+
// Re-open valid session, then verify failed JUB_GPC_OpenSecureChannel clears it
|
|
482
|
+
require(JUB_GPC_Initialize(info, validCert.c_str(), validOcePriv.c_str()) == JUBR_OK);
|
|
483
|
+
maCmdHex = nullptr;
|
|
484
|
+
require(JUB_GPC_BuildMutualAuthData(&maCmdHex) == JUBR_OK && maCmdHex);
|
|
485
|
+
maCmdStr = maCmdHex;
|
|
486
|
+
require(JUB_FreeMemory(maCmdHex) == JUBR_OK);
|
|
487
|
+
maRespHex = buildCardMutualAuthResponse(validCardPriv, ocePub, maCmdStr, &derivedKeys);
|
|
488
|
+
require(JUB_GPC_OpenSecureChannel(maRespHex.c_str()) == JUBR_OK);
|
|
489
|
+
|
|
490
|
+
// Corrupt receipt in a second OpenSecureChannel call
|
|
491
|
+
std::string corruptedResp = maRespHex;
|
|
492
|
+
corruptedResp[corruptedResp.size() - 1] = (corruptedResp.back() == '0') ? '1' : '0';
|
|
493
|
+
require(JUB_GPC_OpenSecureChannel(corruptedResp.c_str()) == JUBR_ERROR);
|
|
494
|
+
output = nullptr;
|
|
495
|
+
require(JUB_GPC_BuildSafeAPDU(0x80, 0x20, 0x00, 0x00, "06313233343536", &output) == JUBR_CONTEXT_NOT_SATISFIED,
|
|
496
|
+
"Failed JUB_GPC_OpenSecureChannel must clear previous open session");
|
|
497
|
+
require(!output);
|
|
498
|
+
require(JUB_GPC_Finalize() == JUBR_OK);
|
|
499
|
+
|
|
500
|
+
#ifdef GPCHANNEL_TEST_JNI
|
|
501
|
+
// 6. Native JNI bridge tests (implJni.cpp: JSON type validation, nullptr returns, error codes)
|
|
502
|
+
{
|
|
503
|
+
MockJniRuntime jni;
|
|
504
|
+
require(JNI_OnLoad(&jni.vm, nullptr) == JNI_VERSION_1_6, "JNI_OnLoad must succeed");
|
|
505
|
+
require(jni.methods.size() == 11, "All 11 GPChannelNatives JNI methods must be registered");
|
|
506
|
+
|
|
507
|
+
typedef jint (*GetErrorFn)(JNIEnv*, jobject);
|
|
508
|
+
typedef jint (*InitFn)(JNIEnv*, jobject, jstring);
|
|
509
|
+
typedef jint (*FinalizeFn)(JNIEnv*, jobject);
|
|
510
|
+
typedef jint (*OpenFn)(JNIEnv*, jobject, jstring);
|
|
511
|
+
typedef jstring (*MutualFn)(JNIEnv*, jobject);
|
|
512
|
+
typedef jstring (*ParseFn)(JNIEnv*, jobject, jstring);
|
|
513
|
+
|
|
514
|
+
GetErrorFn jniGetError = reinterpret_cast<GetErrorFn>(jni.methods["nativeGetErrorCode"]);
|
|
515
|
+
InitFn jniInit = reinterpret_cast<InitFn>(jni.methods["nativeGPCInitialize"]);
|
|
516
|
+
FinalizeFn jniFinalize = reinterpret_cast<FinalizeFn>(jni.methods["nativeGPCFinalize"]);
|
|
517
|
+
OpenFn jniOpen = reinterpret_cast<OpenFn>(jni.methods["nativeGPCOpenSecureChannel"]);
|
|
518
|
+
MutualFn jniMutual = reinterpret_cast<MutualFn>(jni.methods["nativeGPCBuildMutualAuthData"]);
|
|
519
|
+
ParseFn jniParseSafe = reinterpret_cast<ParseFn>(jni.methods["nativeGPCParseSafeAPDUResponse"]);
|
|
520
|
+
ParseFn jniParseCert = reinterpret_cast<ParseFn>(jni.methods["nativeGPCParseCertificate"]);
|
|
521
|
+
|
|
522
|
+
require(jniFinalize(&jni.env, nullptr) == JUBR_OK);
|
|
523
|
+
require(jniInit(&jni.env, nullptr, nullptr) == JUBR_ARGUMENTS_BAD,
|
|
524
|
+
"JNI initialize(nullptr) must return JUBR_ARGUMENTS_BAD");
|
|
525
|
+
|
|
526
|
+
// Malformed JSON and wrong JSON field types (LITE-04) must return JUBR_ARGUMENTS_BAD without throwing
|
|
527
|
+
std::string badJsonSyntax = "{not_json";
|
|
528
|
+
std::string intScpIdJson =
|
|
529
|
+
std::string("{\"scpID\":1107,\"keyUsage\":\"3c\",\"keyType\":\"88\",\"keyLength\":16,") +
|
|
530
|
+
"\"hostID\":\"8080808080808080\",\"cardGroupID\":\"5355424a3031\",\"crt\":\"" +
|
|
531
|
+
validCert + "\",\"sk\":\"" + validOcePriv + "\"}";
|
|
532
|
+
std::string strKeyLenJson =
|
|
533
|
+
std::string("{\"scpID\":\"1107\",\"keyUsage\":\"3c\",\"keyType\":\"88\",\"keyLength\":\"16\",") +
|
|
534
|
+
"\"hostID\":\"8080808080808080\",\"cardGroupID\":\"5355424a3031\",\"crt\":\"" +
|
|
535
|
+
validCert + "\",\"sk\":\"" + validOcePriv + "\"}";
|
|
536
|
+
std::string negKeyLenJson =
|
|
537
|
+
std::string("{\"scpID\":\"1107\",\"keyUsage\":\"3c\",\"keyType\":\"88\",\"keyLength\":-1,") +
|
|
538
|
+
"\"hostID\":\"8080808080808080\",\"cardGroupID\":\"5355424a3031\",\"crt\":\"" +
|
|
539
|
+
validCert + "\",\"sk\":\"" + validOcePriv + "\"}";
|
|
540
|
+
std::string validInitJson =
|
|
541
|
+
std::string("{\"scpID\":\"1107\",\"keyUsage\":\"3c\",\"keyType\":\"88\",\"keyLength\":16,") +
|
|
542
|
+
"\"hostID\":\"8080808080808080\",\"cardGroupID\":\"5355424a3031\",\"crt\":\"" +
|
|
543
|
+
validCert + "\",\"sk\":\"" + validOcePriv + "\"}";
|
|
544
|
+
|
|
545
|
+
require(jniInit(&jni.env, nullptr, jni.makeJString(badJsonSyntax)) == JUBR_ARGUMENTS_BAD);
|
|
546
|
+
require(jniInit(&jni.env, nullptr, jni.makeJString(intScpIdJson)) == JUBR_ARGUMENTS_BAD,
|
|
547
|
+
"Integer scpID in JNI JSON must be rejected without throwing");
|
|
548
|
+
require(jniInit(&jni.env, nullptr, jni.makeJString(strKeyLenJson)) == JUBR_ARGUMENTS_BAD,
|
|
549
|
+
"String keyLength in JNI JSON must be rejected without throwing");
|
|
550
|
+
require(jniInit(&jni.env, nullptr, jni.makeJString(negKeyLenJson)) == JUBR_ARGUMENTS_BAD,
|
|
551
|
+
"Negative keyLength in JNI JSON must be rejected without throwing");
|
|
552
|
+
|
|
553
|
+
// Calling parseSafe when channel is closed must return nullptr and set errorCode = 6
|
|
554
|
+
require(jniParseSafe(&jni.env, nullptr, jni.makeJString("6a80")) == nullptr,
|
|
555
|
+
"JNI parseSafe must return nullptr when channel is not open");
|
|
556
|
+
require(jniGetError(&jni.env, nullptr) == static_cast<jint>(JUBR_CONTEXT_NOT_SATISFIED));
|
|
557
|
+
|
|
558
|
+
// Valid JNI certificate parse and handshake
|
|
559
|
+
jstring certJson = jniParseCert(&jni.env, nullptr, jni.makeJString(validCert));
|
|
560
|
+
require(certJson != nullptr && MockJniRuntime::readJString(certJson).find("0123456789") != std::string::npos);
|
|
561
|
+
|
|
562
|
+
require(jniInit(&jni.env, nullptr, jni.makeJString(validInitJson)) == JUBR_OK);
|
|
563
|
+
jstring jniMa = jniMutual(&jni.env, nullptr);
|
|
564
|
+
require(jniMa != nullptr);
|
|
565
|
+
std::string jniMaResp = buildCardMutualAuthResponse(validCardPriv, ocePub, MockJniRuntime::readJString(jniMa));
|
|
566
|
+
require(jniOpen(&jni.env, nullptr, jni.makeJString(jniMaResp)) == JUBR_OK);
|
|
567
|
+
|
|
568
|
+
// Unauthenticated "9000" safe response through JNI must return nullptr and set errorCode
|
|
569
|
+
require(jniParseSafe(&jni.env, nullptr, jni.makeJString("9000")) == nullptr,
|
|
570
|
+
"JNI parseSafe must reject unauthenticated 9000 response with nullptr");
|
|
571
|
+
require(jniGetError(&jni.env, nullptr) == static_cast<jint>(JUBR_ARGUMENTS_BAD));
|
|
572
|
+
require(jniFinalize(&jni.env, nullptr) == JUBR_OK);
|
|
573
|
+
}
|
|
574
|
+
#endif
|
|
575
|
+
|
|
576
|
+
// 7. 5000 malformed-input fuzz cases
|
|
577
|
+
std::mt19937 random(20261103);
|
|
578
|
+
const unsigned char prefixes[] = {0x42, 0x53, 0x73, 0x86, 0x93, 0x95, 0xb0, 0xf0, 0x5f, 0x7f, 0xbf};
|
|
579
|
+
for (size_t i = 0; i < 5000; ++i) {
|
|
580
|
+
std::string data(random() % 256, '\0');
|
|
581
|
+
for (char& byte : data) byte = static_cast<char>(random());
|
|
582
|
+
if (!data.empty()) data[0] = static_cast<char>(prefixes[i % sizeof(prefixes)]);
|
|
583
|
+
auto encoded = hex(data);
|
|
584
|
+
JUB_ULONG tag = 0;
|
|
585
|
+
output = nullptr;
|
|
586
|
+
auto code = JUB_GPC_TLVDecode(encoded.c_str(), &tag, &output);
|
|
587
|
+
if (code == 0) {
|
|
588
|
+
require(output);
|
|
589
|
+
require(JUB_FreeMemory(output) == 0);
|
|
590
|
+
} else {
|
|
591
|
+
require(!output);
|
|
592
|
+
}
|
|
593
|
+
serial = nullptr;
|
|
594
|
+
subject = nullptr;
|
|
595
|
+
code = JUB_GPC_ParseCertificate(encoded.c_str(), &serial, &subject);
|
|
596
|
+
if (serial) require(JUB_FreeMemory(serial) == 0);
|
|
597
|
+
if (subject) require(JUB_FreeMemory(subject) == 0);
|
|
598
|
+
output = nullptr;
|
|
599
|
+
code = JUB_GPC_ParseAPDUResponse(encoded.c_str(), &status, &output);
|
|
600
|
+
if (output) require(JUB_FreeMemory(output) == 0);
|
|
601
|
+
}
|
|
602
|
+
std::cout << "Native security regression tests + 5000 malformed-input cases passed\n";
|
|
603
|
+
}
|