From 96ca7eeab0502fad6560a460dc4a21e9c8f42860 Mon Sep 17 00:00:00 2001 From: wuqi Date: Mon, 29 Jul 2024 16:18:46 +0800 Subject: [PATCH 1/3] add asn1 and atr analyzing Signed-off-by: wuqi --- BUILD.gn | 1 + bundle.json | 4 +- telephony_core_service.gni | 6 + test/BUILD.gn | 6 + test/unittest/utils_codec_gtest/BUILD.gn | 51 +++ .../utils_codec_gtest/asn1_utils_test.cpp | 297 ++++++++++++++++++ .../request_apdu_build_test.cpp | 51 +++ .../utils_codec_gtest/reset_reponse_test.cpp | 89 ++++++ utils/BUILD.gn | 29 +- utils/codec/include/asn1_constants.h | 36 +++ utils/codec/include/asn1_utils.h | 53 ++++ utils/codec/include/request_apdu_build.h | 41 +++ utils/codec/include/reset_response.h | 39 +++ utils/codec/src/asn1_utils.cpp | 279 ++++++++++++++++ utils/codec/src/request_apdu_build.cpp | 77 +++++ utils/codec/src/reset_response.cpp | 151 +++++++++ utils/common/include/telephony_tag_def.h | 107 +++++++ utils/libtel_common_esim.versionscript | 44 +++ 18 files changed, 1358 insertions(+), 3 deletions(-) create mode 100644 test/unittest/utils_codec_gtest/BUILD.gn create mode 100644 test/unittest/utils_codec_gtest/asn1_utils_test.cpp create mode 100644 test/unittest/utils_codec_gtest/request_apdu_build_test.cpp create mode 100644 test/unittest/utils_codec_gtest/reset_reponse_test.cpp create mode 100644 utils/codec/include/asn1_constants.h create mode 100644 utils/codec/include/asn1_utils.h create mode 100644 utils/codec/include/request_apdu_build.h create mode 100644 utils/codec/include/reset_response.h create mode 100644 utils/codec/src/asn1_utils.cpp create mode 100644 utils/codec/src/request_apdu_build.cpp create mode 100644 utils/codec/src/reset_response.cpp create mode 100644 utils/common/include/telephony_tag_def.h create mode 100644 utils/libtel_common_esim.versionscript diff --git a/BUILD.gn b/BUILD.gn index fb9bf8a55..b2a422585 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -129,6 +129,7 @@ ohos_shared_library("tel_core_service") { "services/core/include", "services/satellite_service_interaction/include", "utils/log/include", + "utils/codec/include", ] configs = [ "utils:telephony_log_config" ] diff --git a/bundle.json b/bundle.json index b6eb415ce..163a959eb 100644 --- a/bundle.json +++ b/bundle.json @@ -22,7 +22,9 @@ "syscap": [ "SystemCapability.Telephony.CoreService" ], - "features": [], + "features": [ + "core_service_support_esim" + ], "hisysevent_config": [ "//base/telephony/core_service/hisysevent.yaml" ], diff --git a/telephony_core_service.gni b/telephony_core_service.gni index 6669f5575..debd49d4d 100644 --- a/telephony_core_service.gni +++ b/telephony_core_service.gni @@ -17,6 +17,12 @@ declare_args() { !defined(global_parts_info.hiviewdfx_hicollie)) { telephony_hicollie_able = false } + + core_service_support_esim = false + if (defined(global_parts_info) && + defined(global_parts_info.core_service_support_esim)) { + core_service_support_esim = true + } } telephony_extra_defines = [] diff --git a/test/BUILD.gn b/test/BUILD.gn index a4ea49ac4..5bf4ad9a3 100755 --- a/test/BUILD.gn +++ b/test/BUILD.gn @@ -12,6 +12,8 @@ # limitations under the License. import("//build/test.gni") +SUBSYSTEM_DIR = "../.." +import("$SUBSYSTEM_DIR/core_service/telephony_core_service.gni") group("unittest") { testonly = true @@ -25,4 +27,8 @@ group("unittest") { "unittest/tel_ril_gtest:tel_ril_gtest", "unittest/utils_vcard_gtest:utils_vcard_gtest", ] + + if (core_service_support_esim) { + deps += [ "unittest/utils_codec_gtest:utils_codec_gtest" ] + } } diff --git a/test/unittest/utils_codec_gtest/BUILD.gn b/test/unittest/utils_codec_gtest/BUILD.gn new file mode 100644 index 000000000..3ec08e588 --- /dev/null +++ b/test/unittest/utils_codec_gtest/BUILD.gn @@ -0,0 +1,51 @@ +# Copyright (C) 2024 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import("//build/test.gni") +SOURCE_DIR = "../../../" + +ohos_unittest("utils_codec_gtest") { + install_enable = true + subsystem_name = "telephony" + part_name = "core_service" + test_module = "utils_codec_gtest" + module_out_path = part_name + "/" + test_module + + sources = [ + "asn1_utils_test.cpp", + "request_apdu_build_test.cpp", + "reset_reponse_test.cpp", + ] + + include_dirs = [ "$SOURCE_DIR/utils/codec/include" ] + + deps = [ + "$SOURCE_DIR/utils:libtel_common", + "//third_party/googletest:gtest_main", + ] + + external_deps = [ + "c_utils:utils", + "hilog:libhilog", + ] + + defines = [ + "TELEPHONY_LOG_TAG = \"CoreServiceGtest\"", + "LOG_DOMAIN = 0xD000F00", + ] +} + +group("unittest") { + testonly = true + deps = [ ":utils_codec_gtest" ] +} diff --git a/test/unittest/utils_codec_gtest/asn1_utils_test.cpp b/test/unittest/utils_codec_gtest/asn1_utils_test.cpp new file mode 100644 index 000000000..8c4e76194 --- /dev/null +++ b/test/unittest/utils_codec_gtest/asn1_utils_test.cpp @@ -0,0 +1,297 @@ +/* + * Copyright (C) 2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#define private public + +#include +#include +#include +#include "asn1_utils.h" +#include "telephony_errors.h" +#include "telephony_log_wrapper.h" + +using namespace testing::ext; +namespace OHOS { +namespace Telephony { +#ifndef TEL_TEST_UNSUPPORT +class Asn1UtilsTest : public testing::Test { +public: + static void SetUpTestCase(); + static void TearDownTestCase(); + void SetUp(); + void TearDown(); +}; + +void Asn1UtilsTest::SetUpTestCase() {} + +void Asn1UtilsTest::TearDownTestCase() {} + +void Asn1UtilsTest::SetUp() {} + +void Asn1UtilsTest::TearDown() {} + +HWTEST_F(Asn1UtilsTest, IsConstructedTag_001, Function | MediumTest | Level3) +{ + uint32_t tag = 1; + bool ret = Asn1Utils::IsConstructedTag(tag); + EXPECT_EQ(ret, false); +} + +HWTEST_F(Asn1UtilsTest, CalculateEncodedBytesNumForLength_001, Function | MediumTest | Level3) +{ + bool ret = false; + uint32_t tag = 1; + uint32_t result = Asn1Utils::CalculateEncodedBytesNumForLength(tag); + ret = result == 1 ? true : false; + EXPECT_EQ(ret, true); + + tag = 0xFF; + result = Asn1Utils::CalculateEncodedBytesNumForLength(tag); + ret = result == 2 ? true : false; + EXPECT_EQ(ret, true); +} + +HWTEST_F(Asn1UtilsTest, ByteCountForUint_001, Function | MediumTest | Level3) +{ + bool ret = false; + uint32_t tag = 1; + uint32_t result = Asn1Utils::ByteCountForUint(tag); + ret = result == 1 ? true : false; + EXPECT_EQ(ret, true); +} + +HWTEST_F(Asn1UtilsTest, BchToString_001, Function | MediumTest | Level3) +{ + bool ret = false; + std::vector responseByte; + std::string destStr; + Asn1Utils::BchToString(responseByte, destStr); + int32_t res = destStr.size(); + ret = res == 0 ? true : false; + EXPECT_EQ(ret, true); +} + +HWTEST_F(Asn1UtilsTest, BcdToBytes_001, Function | MediumTest | Level3) +{ + bool ret = false; + std::string iccidBytes; + const std::string str = "ABCDEFG"; + Asn1Utils::BcdToBytes(str.c_str(), iccidBytes); + int32_t iccidBytesLen = iccidBytes.length(); + ret = iccidBytesLen == 4 ? true : false; + EXPECT_EQ(ret, true); +} + +HWTEST_F(Asn1UtilsTest, BytesToHexStr_001, Function | MediumTest | Level3) +{ + int32_t res = -1; + bool ret = false; + const std::string resultData = "BF3C148008534D44502E434F408108736D64732E636F6D9000"; + std::vector responseByte; + responseByte = Asn1Utils::HexStrToBytes(resultData); + std::string strResult; + strResult = Asn1Utils::BytesToHexStr(responseByte); + res = strResult.length(); + ret = res > 0 ? true : false; + EXPECT_EQ(ret, true); +} + +HWTEST_F(Asn1UtilsTest, ByteCountForInt_001, Function | MediumTest | Level3) +{ + uint32_t res = -1; + uint32_t value = 0; + uint32_t resOne = 1; + uint32_t resTwo = 2; + uint32_t resThree = 3; + uint32_t resFour = 4; + + res = Asn1Utils::ByteCountForInt(value, false); + EXPECT_EQ(res, resOne); + + value = 0x7F; + res = Asn1Utils::ByteCountForInt(value, true); + EXPECT_EQ(res, resOne); + + value = 0x7FFF; + res = Asn1Utils::ByteCountForInt(value, true); + EXPECT_EQ(res, resTwo); + + value = 0x7FFFFF; + res = Asn1Utils::ByteCountForInt(value, true); + EXPECT_EQ(res, resThree); + + value = 0xFF; + res = Asn1Utils::ByteCountForInt(value, false); + EXPECT_EQ(res, resOne); + + value = 0xFFFF; + res = Asn1Utils::ByteCountForInt(value, false); + EXPECT_EQ(res, resTwo); + + value = 0xFFFFFF; + res = Asn1Utils::ByteCountForInt(value, false); + EXPECT_EQ(res, resThree); + + value = 0xFFFFFF0; + res = Asn1Utils::ByteCountForInt(value, false); + EXPECT_EQ(res, resFour); + + value = 0xFFFFFF0; + res = Asn1Utils::ByteCountForInt(value, true); + EXPECT_EQ(res, resFour); +} + +HWTEST_F(Asn1UtilsTest, HexStrToBytes_001, Function | MediumTest | Level3) +{ + const std::string resultData = "BF3C148008534D44502E434F408108736D64732E636F6D9000"; + std::vector responseByte; + int32_t byteLen = 0; + bool ret = false; + responseByte = Asn1Utils::HexStrToBytes(resultData); + byteLen = responseByte.length(); + ret = byteLen == 0 ? true : false; + EXPECT_EQ(ret, false); +} + +HWTEST_F(Asn1UtilsTest, UintToBytes_001, Function | MediumTest | Level3) +{ + bool ret = false; + uint32_t res = 0; + uint32_t value = 0xFF; + std::string versionBytes; + res = Asn1Utils::UintToBytes(value, versionBytes); + ret = res == 0 ? true : false; + EXPECT_EQ(ret, false); +} + +HWTEST_F(Asn1UtilsTest, BytesToString_001, Function | MediumTest | Level3) +{ + int32_t res = -1; + bool ret = false; + const std::string resultData = "BF3C148008534D44502E434F408108736D64732E636F6D9000"; + std::vector responseByte; + responseByte = Asn1Utils::HexStrToBytes(resultData); + std::string strResult; + strResult = Asn1Utils::BytesToString(responseByte); + res = strResult.length(); + ret = res == 0 ? true : false; + EXPECT_EQ(ret, false); +} + +HWTEST_F(Asn1UtilsTest, BytesToInt_001, Function | MediumTest | Level3) +{ + bool ret = false; + const std::string resultData = "010000"; + std::vector responseByte; + int32_t offset = 0; + responseByte = Asn1Utils::HexStrToBytes(resultData); + int32_t byteLen = Asn1Utils::BytesToInt(responseByte, offset, responseByte.length()); + ret = byteLen == 0 ? true : false; + EXPECT_EQ(ret, false); +} + +HWTEST_F(Asn1UtilsTest, StrToHexStr_001, Function | MediumTest | Level3) +{ + bool ret = false; + int32_t res = 0; + std::string bufIn = Asn1Utils::HexStrToBytes(std::string("BF370ABF2707A205A103810103")); + std::string bufOut = Asn1Utils::StrToHexStr(bufIn); + res = bufOut.length(); + ret = res == 0 ? true : false; + EXPECT_EQ(ret, false); +} + +HWTEST_F(Asn1UtilsTest, StringToBytes_001, Function | MediumTest | Level3) +{ + bool ret = false; + int32_t res = 0; + const std::string src = "BF2102A0009000"; + std::vector dest; + dest = Asn1Utils::StringToBytes(src); + res = dest.length(); + ret = res == 0 ? true : false; + EXPECT_EQ(ret, false); +} + +HWTEST_F(Asn1UtilsTest, HexStrToString_001, Function | MediumTest | Level3) +{ + bool ret = false; + const std::string src = "BF2102A0009000"; + std::string dest; + dest = Asn1Utils::HexStrToString(src); + size_t length = dest.length(); + ret = length == 0 ? true : false; + EXPECT_EQ(ret, false); +} + +HWTEST_F(Asn1UtilsTest, CountTrailingZeros_001, Function | MediumTest | Level3) +{ + bool ret = false; + uint8_t res = 0; + uint8_t b = 0; + res = Asn1Utils::CountTrailingZeros(b); + ret = res == 8 ? true : false; + EXPECT_EQ(ret, true); + + b = 1; + res = Asn1Utils::CountTrailingZeros(b); + ret = res == 0 ? true : false; + EXPECT_EQ(ret, true); + + b = 8; + res = Asn1Utils::CountTrailingZeros(b); + ret = res == 3 ? true : false; + EXPECT_EQ(ret, true); + + b = 32; + res = Asn1Utils::CountTrailingZeros(b); + ret = res == 5 ? true : false; + EXPECT_EQ(ret, true); +} + +HWTEST_F(Asn1UtilsTest, ReverseInt_001, Function | MediumTest | Level3) +{ + bool ret = false; + uint32_t res = 0; + uint32_t i = 1; + res = Asn1Utils::ReverseInt(i); + ret = res == 0 ? true : false; + EXPECT_EQ(ret, false); +} + +HWTEST_F(Asn1UtilsTest, ByteToHexStr_001, Function | MediumTest | Level3) +{ + bool ret = false; + uint32_t res = 0; + uint8_t src = 1; + std::string dest; + res = Asn1Utils::ByteToHexStr(src, dest); + ret = res == BYTE_TO_HEX_LEN ? true : false; + EXPECT_EQ(ret, true); +} + +HWTEST_F(Asn1UtilsTest, IntToBytes_001, Function | MediumTest | Level3) +{ + bool ret = false; + uint32_t res = 0; + int32_t value = 1; + std::string dest; + res = Asn1Utils::IntToBytes(value, dest); + ret = res == 0 ? true : false; + EXPECT_EQ(ret, false); +} +#endif // TEL_TEST_UNSUPPORT +} // namespace Telephony +} // namespace OHOS \ No newline at end of file diff --git a/test/unittest/utils_codec_gtest/request_apdu_build_test.cpp b/test/unittest/utils_codec_gtest/request_apdu_build_test.cpp new file mode 100644 index 000000000..503425539 --- /dev/null +++ b/test/unittest/utils_codec_gtest/request_apdu_build_test.cpp @@ -0,0 +1,51 @@ +/* + * Copyright (C) 2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include +#include +#include +#include "telephony_log_wrapper.h" +#include "request_apdu_build.h" + +using namespace testing::ext; +namespace OHOS { +namespace Telephony { +#ifndef TEL_TEST_UNSUPPORT +class RequestApduBuildTest : public testing::Test { +public: + static void SetUpTestCase(); + static void TearDownTestCase(); + void SetUp(); + void TearDown(); +}; + +void RequestApduBuildTest::SetUpTestCase() {} + +void RequestApduBuildTest::TearDownTestCase() {} + +void RequestApduBuildTest::SetUp() {} + +void RequestApduBuildTest::TearDown() {} + +HWTEST_F(RequestApduBuildTest, BuildStoreData_001, Function | MediumTest | Level1) +{ + RequestApduBuild build(1); + std::string cmdHex = "test cmdHex"; + build.BuildStoreData(cmdHex); + std::list> commands = build.getCommands(); + EXPECT_EQ(1, commands.size()); +} +#endif // TEL_TEST_UNSUPPORT +} +} \ No newline at end of file diff --git a/test/unittest/utils_codec_gtest/reset_reponse_test.cpp b/test/unittest/utils_codec_gtest/reset_reponse_test.cpp new file mode 100644 index 000000000..ed4f7136f --- /dev/null +++ b/test/unittest/utils_codec_gtest/reset_reponse_test.cpp @@ -0,0 +1,89 @@ +/* + * Copyright (C) 2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#define private public +#define protected public + +#include +#include +#include "reset_response.h" +#include "asn1_node.h" +#include "telephony_log_wrapper.h" + +using namespace testing::ext; +namespace OHOS { +namespace Telephony { +#ifndef TEL_TEST_UNSUPPORT + +class ResetResponseTest : public testing::Test { +public: + static void SetUpTestCase(); + static void TearDownTestCase(); + void SetUp(); + void TearDown(); +}; + +void ResetResponseTest::SetUpTestCase() {} + +void ResetResponseTest::TearDownTestCase() {} + +void ResetResponseTest::SetUp() {} + +void ResetResponseTest::TearDown() {} + +HWTEST_F(ResetResponseTest, ParseAtrData_ValidInput, Function | MediumTest | Level1) +{ + ResetResponse response; + const std::string atr1 = ""; + EXPECT_FALSE(response.AnalysisAtrData(atr1)); + const std::string atr2 = "123"; + EXPECT_FALSE(response.AnalysisAtrData(atr2)); + const std::string atr3 = "12"; + EXPECT_FALSE(response.AnalysisAtrData(atr3)); + const std::string atr = "3B9F96803F47A28031E073FE211B6759868828681011C4"; + EXPECT_TRUE(response.AnalysisAtrData(atr)); + EXPECT_TRUE(response.IsEuiccAvailable()); +} + +HWTEST_F(ResetResponseTest, CheckAtrDataParam_ValidInput, Function | MediumTest | Level1) +{ + ResetResponse response; + const std::string atr1 = ""; + EXPECT_FALSE(response.CheckAtrDataParam(atr1)); + const std::string atr2 = "123"; + EXPECT_FALSE(response.CheckAtrDataParam(atr2)); + const std::string atr3 = "12"; + EXPECT_FALSE(response.CheckAtrDataParam(atr3)); + const std::string atr4 = "1234"; + EXPECT_TRUE(response.CheckAtrDataParam(atr4)); +} + +HWTEST_F(ResetResponseTest, CheckIsEuiccAvailable_ValidInput, Function | MediumTest | Level1) +{ + ResetResponse response; + uint8_t charB = '\0'; + uint8_t charD = 'A'; + EXPECT_FALSE(response.CheckIsEuiccAvailable(charB, charD)); + + charB = 'A'; + charD = '\0'; + EXPECT_FALSE(response.CheckIsEuiccAvailable(charB, charD)); + + charB = 0xA2; + charD = 0x3F; + EXPECT_TRUE(response.CheckIsEuiccAvailable(charB, charD)); +} +#endif // TEL_TEST_UNSUPPORT +} // namespace Telephony +} // namespace OHOS diff --git a/utils/BUILD.gn b/utils/BUILD.gn index e6cdcccdf..25dda922a 100755 --- a/utils/BUILD.gn +++ b/utils/BUILD.gn @@ -10,13 +10,18 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. - +SUBSYSTEM_DIR = "../.." import("//build/ohos.gni") +import("$SUBSYSTEM_DIR/core_service/telephony_core_service.gni") group("common_target") { deps = [ ":libtel_common" ] } +config("telephony_codec_config") { + include_dirs = [ "codec/include" ] +} + config("telephony_log_config") { include_dirs = [ "log/include" ] } @@ -51,7 +56,12 @@ ohos_shared_library("libtel_common") { debug = false } branch_protector_ret = "pac_ret" - version_script = "libtel_common.versionscript" + if (core_service_support_esim) { + version_script = "libtel_common_esim.versionscript" + } else { + version_script = "libtel_common.versionscript" + } + sources = [ "../frameworks/js/napi/napi_util.cpp", "common/src/enum_convert.cpp", @@ -66,6 +76,17 @@ ohos_shared_library("libtel_common") { "preferences/src/tel_profile_util.cpp", ] + if (core_service_support_esim) { + sources += [ + "codec/src/asn1_builder.cpp", + "codec/src/asn1_decoder.cpp", + "codec/src/asn1_node.cpp", + "codec/src/asn1_utils.cpp", + "codec/src/request_apdu_build.cpp", + "codec/src/reset_response.cpp", + ] + } + defines = [ "TELEPHONY_LOG_TAG = \"TelephonyCommon\"", "LOG_DOMAIN = 0xD001F04", @@ -77,6 +98,10 @@ ohos_shared_library("libtel_common") { configs = [ "../utils:telephony_log_config" ] + if (core_service_support_esim) { + configs += [ "../utils:telephony_codec_config" ] + } + public_configs = [ ":tel_napi_config", ":tel_utils_config", diff --git a/utils/codec/include/asn1_constants.h b/utils/codec/include/asn1_constants.h new file mode 100644 index 000000000..e36a71e87 --- /dev/null +++ b/utils/codec/include/asn1_constants.h @@ -0,0 +1,36 @@ +/* + * Copyright (C) 2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef ASN1_CONSTANTS_H +#define ASN1_CONSTANTS_H + +#include +#include + +namespace OHOS { +namespace Telephony { +const uint32_t BYTE_TO_HEX_LEN = 2; +const uint32_t OFFSET_EIGHT_BIT = 8; +const int32_t BIT6_MASK = 0x20; +const int32_t BIT8_MASK = 0x80; +const uint32_t MAX_UINT8 = std::numeric_limits::max(); +const uint32_t MAX_UINT16 = std::numeric_limits::max(); +const uint32_t MAX_UINT24 = (std::numeric_limits::max() >> 8); +const uint32_t MAX_INT8 = std::numeric_limits::max(); +const uint32_t MAX_INT16 = std::numeric_limits::max(); +const uint32_t MAX_INT24 = (std::numeric_limits::max() >> 8); +} // namespace Telephony +} // namespace OHOS +#endif // ASN1_CONSTANTS_H \ No newline at end of file diff --git a/utils/codec/include/asn1_utils.h b/utils/codec/include/asn1_utils.h new file mode 100644 index 000000000..f4ed292f6 --- /dev/null +++ b/utils/codec/include/asn1_utils.h @@ -0,0 +1,53 @@ +/* + * Copyright (C) 2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef ASN1_UTIL_H +#define ASN1_UTIL_H + +#include +#include +#include +#include +#include "telephony_log_wrapper.h" + +namespace OHOS { +namespace Telephony { +class Asn1Utils { +public: + static bool IsConstructedTag(uint32_t tag); + static uint32_t CalculateEncodedBytesNumForLength(uint32_t length); + static uint32_t ByteCountForUint(uint32_t value); + static void BchToString(const std::vector &src, std::string &destStr); + static void BcdToBytes(const std::string &bcd, std::vector &bytes); + static std::string BytesToHexStr(const std::vector &bytes); + static std::vector HexStrToBytes(const std::string &hexStr); + static uint32_t UintToBytes(const uint32_t value, std::vector &bytes); + static uint32_t IntToBytes(int32_t value, std::vector &dest); + static std::string BytesToString(const std::vector &src); + static std::string HexStrToString(const std::string &hexStr); + static std::vector StringToBytes(const std::string &src); + static int32_t BytesToInt(const std::vector &src, uint32_t offset, uint32_t length); + static uint8_t CountTrailingZeros(const uint8_t value); + static uint32_t ReverseInt(uint32_t value); + static uint32_t ByteToHexStr(uint8_t src, std::string &dest); + +private: + static std::string SwapHexCharPair(const std::string &hexStr); + static std::string StrToHexStr(const std::string& str); + static uint32_t ByteCountForInt(uint32_t value, bool isSigned); +}; +} // namespace Telephony +} // namespace OHOS +#endif // ASN1_UTIL_H_ diff --git a/utils/codec/include/request_apdu_build.h b/utils/codec/include/request_apdu_build.h new file mode 100644 index 000000000..ab9dc94cb --- /dev/null +++ b/utils/codec/include/request_apdu_build.h @@ -0,0 +1,41 @@ +/* + * Copyright (C) 2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef REQUEST_APDU_BUILD_H +#define REQUEST_APDU_BUILD_H + +#include +#include +#include "apdu_command.h" + +namespace OHOS { +namespace Telephony { +class RequestApduBuild { +public: + explicit RequestApduBuild(int32_t channelId): channelId_(channelId) {} + void BuildStoreData(const std::string &cmdHex); + std::list> GetCommands(); + +private: + void AddApdu(const ApduData &apduData); + void ConstructApduData(uint32_t packetTag, uint32_t packetIndex, uint32_t packetLen, + const std::string &cmdHex, ApduData &apduData); + int32_t channelId_ = 0; + std::list> apduCommandLst_; + std::mutex mutex_; +}; +} // namespace Telephony +} // namespace OHOS +#endif \ No newline at end of file diff --git a/utils/codec/include/reset_response.h b/utils/codec/include/reset_response.h new file mode 100644 index 000000000..d957158d0 --- /dev/null +++ b/utils/codec/include/reset_response.h @@ -0,0 +1,39 @@ +/* + * Copyright (C) 2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef RESET_RESPONSE_H +#define RESET_RESPONSE_H + +#include +#include + +namespace OHOS { +namespace Telephony { +class ResetResponse { +public: + bool AnalysisAtrData(const std::string &atr); + bool IsEuiccAvailable(); + +private: + bool CheckOperationRes(uint8_t chr, const int32_t tMask, const int32_t comparedVal); + bool AnalysisInterfaceData(const std::vector &atrData, uint32_t atrDataLen, uint32_t &index); + bool CheckIsEuiccAvailable(uint8_t charB, uint8_t charD); + bool CheckAtrDataParam(const std::string &atr); + bool isEuiccAvailable_ = false; + uint8_t formatByte_ = 0; +}; +} // namespace Telephony +} // namespace OHOS +#endif // RESET_RESPONSE_H \ No newline at end of file diff --git a/utils/codec/src/asn1_utils.cpp b/utils/codec/src/asn1_utils.cpp new file mode 100644 index 000000000..3df429dae --- /dev/null +++ b/utils/codec/src/asn1_utils.cpp @@ -0,0 +1,279 @@ +/* + * Copyright (C) 2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "asn1_utils.h" +#include +#include +#include +#include +#include "asn1_constants.h" +#include "telephony_errors.h" + +namespace OHOS { +namespace Telephony { +namespace { +const uint32_t HEX_STR_MAX_LENGTH = 4; +const uint32_t HEX_DATA_LEN = 16; +const uint32_t OFFSET_ONE_BIT = 1; +const uint32_t OFFSET_TWO_BIT = 2; +const uint32_t OFFSET_FOUR_BIT = 4; +const uint32_t OFFSET_THREE_BYTE = 24; +const uint32_t ONE_BYTE_LENGTH = 1; +const uint32_t TWO_BYTE_LENGTH = 2; +const uint32_t THREE_BYTE_LENGTH = 3; +const uint32_t FOUR_BYTE_LENGTH = 4; +const uint8_t ZERO_VALUE_OCCPUPIED_BIT_COUNT = 8; +const uint32_t NONZERO_OCCUPIED_BITS = 7; +// For exchanging adjacent bit pairs +const uint32_t MASK_PAIRS = 0x55555555; +// For swap adjacent 4-bit blocks +const uint32_t MASK_NIBBLES = 0x33333333; +// For swap adjacent bytes +const uint32_t MASK_BYTES = 0x0f0f0f0f; +// For exchange high and low 16 bits +const uint32_t MASK_HALFWORDS = 0xff00; + +const uint32_t MASK_BYTES_ONE_BYTE = 0x0F; +const uint32_t MASK_NIBBLES_ONE_BYTE = 0x33; +const uint32_t MASK_PAIRS_ONE_BYTE = 0x55; +} + +bool Asn1Utils::IsConstructedTag(uint32_t tag) +{ + std::vector tagBytes; + uint32_t bytesLen = UintToBytes(tag, tagBytes); + if (bytesLen == 0 || tagBytes.empty()) { + TELEPHONY_LOGE("failed to transform uint data to bytes."); + return false; + } + return (static_cast(tagBytes[0]) & BIT6_MASK) != 0; +} + +uint32_t Asn1Utils::CalculateEncodedBytesNumForLength(uint32_t length) +{ + // default length is 1 byte + uint32_t len = 1; + if (length > MAX_INT8) { + len += ByteCountForUint(length); + } + return len; +} + +uint32_t Asn1Utils::ByteCountForUint(uint32_t value) +{ + return ByteCountForInt(value, false); +} + +uint32_t Asn1Utils::ByteCountForInt(uint32_t value, bool isSigned) +{ + if (isSigned) { + if (value <= MAX_INT8) { + return ONE_BYTE_LENGTH; + } + if (value <= MAX_INT16) { + return TWO_BYTE_LENGTH; + } + if (value <= MAX_INT24) { + return THREE_BYTE_LENGTH; + } + } else { + if (value <= MAX_UINT8) { + return ONE_BYTE_LENGTH; + } + if (value <= MAX_UINT16) { + return TWO_BYTE_LENGTH; + } + if (value <= MAX_UINT24) { + return THREE_BYTE_LENGTH; + } + } + return FOUR_BYTE_LENGTH; +} + +// convert bytes array to string +void Asn1Utils::BchToString(const std::vector &src, std::string &destStr) +{ + std::string hexStr = BytesToHexStr(src); + destStr = SwapHexCharPair(hexStr); +} + +void Asn1Utils::BcdToBytes(const std::string &bcd, std::vector &bytes) +{ + std::string hexStr = SwapHexCharPair(bcd); + bytes = HexStrToBytes(hexStr); +} + +std::string Asn1Utils::BytesToHexStr(const std::vector &bytes) +{ + if (bytes.size() > MAX_UINT8) { + return ""; + } + std::ostringstream oss; + for (size_t i = 0; i < bytes.size(); i++) { + oss << std::hex << std::uppercase << std::setw(BYTE_TO_HEX_LEN) << std::setfill('0') << (bytes[i] & MAX_UINT8); + } + return oss.str(); +} + +uint32_t Asn1Utils::ByteToHexStr(uint8_t src, std::string &dest) +{ + std::ostringstream oss; + oss << std::hex << std::uppercase << std::setw(BYTE_TO_HEX_LEN) << std::setfill('0') << (src & MAX_UINT8); + dest = oss.str(); + return static_cast(dest.size()); +} + +std::vector Asn1Utils::HexStrToBytes(const std::string& hexStr) +{ + std::vector ret = {}; + if (hexStr.length() > (MAX_UINT8 * BYTE_TO_HEX_LEN)) { + return ret; + } + + if ((hexStr.length() % BYTE_TO_HEX_LEN) != 0) { + return ret; + } + + for (size_t i = 0; i < hexStr.length(); i += BYTE_TO_HEX_LEN) { + uint8_t byte = static_cast(std::stoi(hexStr.substr(i, BYTE_TO_HEX_LEN), nullptr, HEX_DATA_LEN)); + ret.push_back(byte); + } + return ret; +} + +int32_t Asn1Utils::BytesToInt(const std::vector &src, uint32_t offset, uint32_t length) +{ + if (length > HEX_STR_MAX_LENGTH || (offset + length) > src.size()) { + TELEPHONY_LOGE("src length is more than four byte."); + return TELEPHONY_ERROR; + } + std::vector subByteStream(src.begin() + offset, src.begin() + offset + length); + std::string hexStr = BytesToHexStr(subByteStream); + int32_t valInt = std::stoi(hexStr, nullptr, HEX_DATA_LEN); + return valInt; +} + +uint32_t Asn1Utils::UintToBytes(const uint32_t value, std::vector &bytes) +{ + uint32_t len = ByteCountForInt(value, false); + std::ostringstream oss; + oss << std::hex << std::uppercase << std::setw(len * BYTE_TO_HEX_LEN) << std::setfill('0') << value; + std::string hexStr = oss.str(); + bytes = HexStrToBytes(hexStr); + return len; +} + +uint32_t Asn1Utils::IntToBytes(int32_t value, std::vector &dest) +{ + uint32_t len = ByteCountForInt(static_cast(value), true); + std::ostringstream oss; + oss << std::hex << std::uppercase << std::setw(len * BYTE_TO_HEX_LEN) << std::setfill('0') << value; + std::string hexStr = oss.str(); + dest = HexStrToBytes(hexStr); + return len; +} + +std::string Asn1Utils::BytesToString(const std::vector &src) +{ + std::string hexStr = BytesToHexStr(src); + std::string dest = HexStrToString(hexStr); + return dest; +} + +std::string Asn1Utils::StrToHexStr(const std::string& str) +{ + std::string inputStr = str; + std::vector bytes = {}; + for (size_t i = 0; i < inputStr.length(); i++) { + bytes.push_back(static_cast(inputStr[i])); + } + std::string result = BytesToHexStr(bytes); + return result; +} + +std::string Asn1Utils::HexStrToString(const std::string& hexStr) +{ + std::string inputHexStr = hexStr; + std::vector bytes = HexStrToBytes(inputHexStr); + std::string result(""); + for (size_t i = 0; i < bytes.size(); i++) { + result += (static_cast(bytes[i])); + } + return result; +} + +std::vector Asn1Utils::StringToBytes(const std::string &src) +{ + std::string hexStr = StrToHexStr(src); + std::vector dest = HexStrToBytes(hexStr); + return dest; +} + +uint32_t Asn1Utils::ReverseInt(uint32_t value) +{ + uint32_t valueTemp = value; + // Exchanging adjacent bit pairs + valueTemp = ((valueTemp & MASK_PAIRS) << OFFSET_ONE_BIT) | ((valueTemp >> OFFSET_ONE_BIT) & MASK_PAIRS); + // Swap adjacent 4-bit blocks + valueTemp = ((valueTemp & MASK_NIBBLES) << OFFSET_TWO_BIT) | ((valueTemp >> OFFSET_TWO_BIT) & MASK_NIBBLES); + // Swap adjacent bytes + valueTemp = ((valueTemp & MASK_BYTES) << OFFSET_FOUR_BIT) | ((valueTemp >> OFFSET_FOUR_BIT) & MASK_BYTES); + // Exchange high and low 16 bits + valueTemp = (valueTemp << OFFSET_THREE_BYTE) | ((valueTemp & MASK_HALFWORDS) << OFFSET_EIGHT_BIT) | + ((valueTemp >> OFFSET_EIGHT_BIT) & MASK_HALFWORDS) | (valueTemp >> OFFSET_THREE_BYTE); + return valueTemp; +} + +uint8_t Asn1Utils::CountTrailingZeros(const uint8_t value) +{ + if (value == 0) { + return ZERO_VALUE_OCCPUPIED_BIT_COUNT; + } + + uint32_t valueTemp = ((static_cast(value)) & MAX_UINT8); + // The number of bits occupied by non-zero values + uint8_t nonZeroBitCount = static_cast(NONZERO_OCCUPIED_BITS); + if ((valueTemp & static_cast(MASK_BYTES_ONE_BYTE)) != 0) { + nonZeroBitCount -= static_cast(OFFSET_FOUR_BIT); + } + if ((valueTemp & static_cast(MASK_NIBBLES_ONE_BYTE)) != 0) { + nonZeroBitCount -= static_cast(OFFSET_TWO_BIT); + } + if ((valueTemp & static_cast(MASK_PAIRS_ONE_BYTE)) != 0) { + nonZeroBitCount -= static_cast(OFFSET_ONE_BIT); + } + return nonZeroBitCount; +} + +// convert bytes array to string +std::string Asn1Utils::SwapHexCharPair(const std::string &hexStr) +{ + std::string result = ""; + if (hexStr.length() > (MAX_UINT8 * BYTE_TO_HEX_LEN)) { + return result; + } + + std::string tmphexStr = hexStr; + if (tmphexStr.length() % BYTE_TO_HEX_LEN != 0) { + tmphexStr += "0"; + } + for (size_t i = 0; i < tmphexStr.length(); i += BYTE_TO_HEX_LEN) { + result += tmphexStr.substr(i + ONE_BYTE_LENGTH, ONE_BYTE_LENGTH); + result += tmphexStr.substr(i, ONE_BYTE_LENGTH); + } + return result; +} +} // namespace Telephony +} diff --git a/utils/codec/src/request_apdu_build.cpp b/utils/codec/src/request_apdu_build.cpp new file mode 100644 index 000000000..5fed1b52a --- /dev/null +++ b/utils/codec/src/request_apdu_build.cpp @@ -0,0 +1,77 @@ +/* + * Copyright (C) 2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "request_apdu_build.h" +#include "asn1_constants.h" +#include "asn1_utils.h" + +namespace OHOS { +namespace Telephony { +namespace { +const uint32_t CLA_STORE_DATA = 0x80; +const uint32_t INS_STORE_DATA = 0xE2; +const uint32_t P1_STORE_DATA_INTERM = 0x11; +const uint32_t P1_STORE_DATA_END = 0x91; +} + +std::list> RequestApduBuild::GetCommands() +{ + std::lock_guard lock(mutex_); + return std::move(apduCommandLst_); +} + +void RequestApduBuild::AddApdu(const ApduData &apduData) +{ + std::unique_ptr apduCommand = std::make_unique(channelId_, apduData); + std::lock_guard lock(mutex_); + apduCommandLst_.push_back(std::move(apduCommand)); +} + +void RequestApduBuild::ConstructApduData(uint32_t packetTag, uint32_t packetIndex, uint32_t packetLen, + const std::string &cmdHex, ApduData &apduData) +{ + apduData.cla = CLA_STORE_DATA; + apduData.ins = INS_STORE_DATA; + apduData.p1 = packetTag; + apduData.p2 = packetIndex; + apduData.p3 = packetLen; + apduData.cmdHex = cmdHex; +} + +void RequestApduBuild::BuildStoreData(const std::string &cmdHex) +{ + int32_t cmdLen = MAX_UINT8 * BYTE_TO_HEX_LEN; + int32_t startPos = 0; + uint32_t totalLen = static_cast(cmdHex.length() / BYTE_TO_HEX_LEN); + uint32_t totalSubCmds = ((totalLen == 0) ? 1 : ((totalLen + MAX_UINT8 - 1) / MAX_UINT8)); + uint32_t leastLen = totalLen; + /* When handling packet fragmentation, if the last packet of data is less than 255 bytes, + it requires special handling outside the loop. + */ + for (uint32_t i = 1; i < totalSubCmds; ++i) { + std::string data = cmdHex.substr(startPos, cmdLen); + ApduData apduData; + ConstructApduData(P1_STORE_DATA_INTERM, i - 1, MAX_UINT8, data, apduData); + AddApdu(apduData); + startPos += cmdLen; + leastLen -= MAX_UINT8; + } + std::string lastData = cmdHex.substr(startPos); + ApduData lastApduData; + ConstructApduData(P1_STORE_DATA_END, totalSubCmds - 1, leastLen, lastData, lastApduData); + AddApdu(lastApduData); +} +} // namespace Telephony +} // namespace OHOS \ No newline at end of file diff --git a/utils/codec/src/reset_response.cpp b/utils/codec/src/reset_response.cpp new file mode 100644 index 000000000..3feb77530 --- /dev/null +++ b/utils/codec/src/reset_response.cpp @@ -0,0 +1,151 @@ +/* + * Copyright (C) 2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "reset_response.h" +#include "asn1_constants.h" +#include "asn1_node.h" +#include "asn1_utils.h" +#include "telephony_log_wrapper.h" + +namespace OHOS { +namespace Telephony { +namespace { +const int32_t BIT2_MASK = 0x02; +const int32_t BIT5_MASK = 0x10; +const int32_t BIT7_MASK = 0x40; +const int32_t HIGH_BITS_MASK = 0xF0; +const int32_t LOW_BITS_MASK = 0x0F; +const uint32_t MIN_ATR_DATA_LENGTH = 4; +const int32_t MAX_INTERFACE_VALUE = 0x0F; +const uint8_t REVERSED_AGREEMENT = 0x3B; +const uint8_t POSITIVE_AGREEMENT = 0x3F; +} + +bool ResetResponse::IsEuiccAvailable() +{ + return isEuiccAvailable_; +} + +bool ResetResponse::CheckIsEuiccAvailable(uint8_t charB, uint8_t charD) +{ + if (charB != '\0' && charD != '\0' && CheckOperationRes(charD, LOW_BITS_MASK, MAX_INTERFACE_VALUE)) { + if ((!CheckOperationRes(charB, BIT8_MASK, 0)) && (!CheckOperationRes(charB, BIT2_MASK, 0))) { + isEuiccAvailable_ = true; + return true; + } + } + return false; +} + +bool ResetResponse::CheckOperationRes(uint8_t chr, const int32_t tMask, const int32_t comparedVal) +{ + if ((chr & tMask) != comparedVal) { + return false; + } + return true; +} + +bool ResetResponse::AnalysisInterfaceData(const std::vector &atrData, uint32_t atrDataLen, uint32_t &index) +{ + uint8_t lastByteD = formatByte_; + bool isContinue = CheckOperationRes(lastByteD, HIGH_BITS_MASK, 0); + uint8_t charB = '\0'; + uint8_t charD = '\0'; + while (!isContinue) { + if (!CheckOperationRes(lastByteD, BIT5_MASK, 0)) { + if (index >= atrDataLen) { + return false; + } + index++; + } + if (!CheckOperationRes(lastByteD, BIT6_MASK, 0)) { + if (index >= atrDataLen) { + return false; + } + charB = atrData[index]; + index++; + if (charD != '\0' && (CheckIsEuiccAvailable(charB, charD))) { + return true; + } + } + if (!CheckOperationRes(lastByteD, BIT7_MASK, 0)) { + if (index >= atrDataLen) { + return false; + } + index++; + } + if (!CheckOperationRes(lastByteD, BIT8_MASK, 0)) { + if (index >= atrDataLen) { + return false; + } + charD = atrData[index]; + index++; + } + if (charD == '\0') { + break; + } + lastByteD = charD; + isContinue = CheckOperationRes(lastByteD, HIGH_BITS_MASK, 0); + } + return true; +} + +bool ResetResponse::CheckAtrDataParam(const std::string &atr) +{ + if (atr.empty() || atr.length() % BYTE_TO_HEX_LEN != 0) { + TELEPHONY_LOGE("ATR length %zu is not even.", atr.length()); + return false; + } + + if (atr.length() < MIN_ATR_DATA_LENGTH) { + TELEPHONY_LOGE("ATR is Valid, it must at least contains TS and T0."); + return false; + } + return true; +} + +bool ResetResponse::AnalysisAtrData(const std::string &atr) +{ + TELEPHONY_LOGD("AnalysisAtrData ATR string enter."); + if (!CheckAtrDataParam(atr)) { + TELEPHONY_LOGE("failed to check AtrData param!"); + return false; + } + std::vector atrData = Asn1Utils::HexStrToBytes(atr); + uint32_t atrDataLen = atrData.size(); + if (atrDataLen == 0) { + TELEPHONY_LOGE("failed to transform HexStr To Bytes."); + return false; + } + // convention byte + uint32_t index = 0; + if (atrData[index] != POSITIVE_AGREEMENT && atrData[index] != REVERSED_AGREEMENT) { + TELEPHONY_LOGE("convention byte is valid!"); + return false; + } + index++; + if (index >= atrDataLen) { + return false; + } + // format byte + formatByte_ = atrData[index]; + index++; + if (index >= atrDataLen) { + return false; + } + return AnalysisInterfaceData(atrData, atrDataLen, index); +} +} // namespace Telephony +} // namespace OHOS \ No newline at end of file diff --git a/utils/common/include/telephony_tag_def.h b/utils/common/include/telephony_tag_def.h new file mode 100644 index 000000000..f34dfd5f9 --- /dev/null +++ b/utils/common/include/telephony_tag_def.h @@ -0,0 +1,107 @@ +/* + * Copyright (C) 2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef TELEPHONY_TAG_DEF_H +#define TELEPHONY_TAG_DEF_H + +namespace OHOS { +namespace Telephony { +// Command type tags +const int32_t TAG_ESIM_PROFILE_INSTALLATION_RESULT = 0xBF37; +const int32_t TAG_ESIM_PROFILE_INSTALLATION_RESULT_DATA = 0xBF27; +const int32_t TAG_ESIM_NOTIFICATION_METADATA = 0xBF2F; +const int32_t TAG_ESIM_SEQ = 0x80; +const int32_t TAG_ESIM_TARGET_ADDR = 0x0C; +const int32_t TAG_ESIM_EVENT = 0x81; +const int32_t TAG_ESIM_CANCEL_SESSION = 0xBF41; +const int32_t TAG_ESIM_PROFILE_INFO = 0xE3; +const int32_t TAG_ESIM_TAG_LIST = 0x5C; +const int32_t TAG_ESIM_EID = 0x5A; +const int32_t TAG_ESIM_NICKNAME = 0x90; +const int32_t TAG_ESIM_ICCID = 0x5A; +const int32_t TAG_ESIM_PROFILE_STATE = 0x9F70; +const int32_t TAG_ESIM_OBTAIN_OPERATOR_NAME = 0x91; +const int32_t TAG_ESIM_PROFILE_CLASS = 0x95; +const int32_t TAG_ESIM_PROFILE_POLICY_RULE = 0x99; +const int32_t TAG_ESIM_PROFILE_NAME = 0x92; +const int32_t TAG_ESIM_OPERATOR_ID = 0xB7; +const int32_t TAG_ESIM_CARRIER_PRIVILEGE_RULES = 0xBF76; +const int32_t TAG_ESIM_PORT = 0x9F24; + +// Constructed types tags +const int32_t TAG_ESIM_CTX_COMP_0 = 0xA0; +const int32_t TAG_ESIM_CTX_COMP_1 = 0xA1; +const int32_t TAG_ESIM_CTX_COMP_2 = 0xA2; +const int32_t TAG_ESIM_CTX_COMP_3 = 0xA3; +const int32_t TAG_ESIM_CTX_COMP_4 = 0xA4; +const int32_t TAG_ESIM_CTX_COMP_5 = 0xA5; +const int32_t TAG_ESIM_CTX_COMP_6 = 0xA6; +const int32_t TAG_ESIM_CTX_COMP_7 = 0xA7; +const int32_t TAG_ESIM_CTX_COMP_8 = 0xA8; +const int32_t TAG_ESIM_CTX_COMP_9 = 0xA9; +const int32_t TAG_ESIM_CTX_COMP_A = 0xAA; + +// Base type tags +const int32_t TAG_ESIM_CTX_0 = 0x80; +const int32_t TAG_ESIM_CTX_1 = 0x81; +const int32_t TAG_ESIM_CTX_2 = 0x82; +const int32_t TAG_ESIM_CTX_3 = 0x83; +const int32_t TAG_ESIM_CTX_4 = 0x84; +const int32_t TAG_ESIM_CTX_5 = 0x85; +const int32_t TAG_ESIM_CTX_6 = 0x86; +const int32_t TAG_ESIM_CTX_7 = 0x87; +const int32_t TAG_ESIM_CTX_8 = 0x88; +const int32_t TAG_ESIM_CTX_9 = 0x89; +const int32_t TAG_ESIM_CTX_A = 0x8A; +const int32_t TAG_ESIM_CTX_B = 0x8B; + +// Common tags +const int32_t TAG_ESIM_UNI_2 = 0x02; +const int32_t TAG_ESIM_UNI_4 = 0x04; +const int32_t TAG_ESIM_SEQUENCE = 0x30; + +// Standard RefArDo data tags +const int32_t TAG_ESIM_REF_AR_DO = 0xE2; +const int32_t TAG_ESIM_REF_DO = 0xE1; +const int32_t TAG_ESIM_DEVICE_APP_ID_REF_DO = 0xC1; +const int32_t TAG_ESIM_PKG_REF_DO = 0xCA; +const int32_t TAG_ESIM_AR_DO = 0xE3; +const int32_t TAG_ESIM_PERM_AR_DO = 0xDB; +const int32_t TAG_ESIM_OCTET_STRING_TYPE = 0x04; +const int32_t TAG_ESIM_INTEGER_TYPE = 0x02; + +// Esim related protocol tags +const int32_t TAG_ESIM_GET_PROFILES = 0xBF2D; +const int32_t TAG_ESIM_DISABLE_PROFILE = 0xBF32; +const int32_t TAG_ESIM_ENABLE_PROFILE = 0xBF31; +const int32_t TAG_ESIM_GET_EID = 0xBF3E; +const int32_t TAG_ESIM_SET_NICKNAME = 0xBF29; +const int32_t TAG_ESIM_DELETE_PROFILE = 0xBF33; +const int32_t TAG_ESIM_GET_CONFIGURED_ADDRESSES = 0xBF3C; +const int32_t TAG_ESIM_SET_DEFAULT_SMDP_ADDRESS = 0xBF3F; +const int32_t TAG_ESIM_GET_RAT = 0xBF43; +const int32_t TAG_ESIM_EUICC_MEMORY_RESET = 0xBF34; +const int32_t TAG_ESIM_GET_EUICC_CHALLENGE = 0xBF2E; +const int32_t TAG_ESIM_GET_EUICC_INFO_1 = 0xBF20; +const int32_t TAG_ESIM_GET_EUICC_INFO_2 = 0xBF22; +const int32_t TAG_ESIM_LIST_NOTIFICATION = 0xBF28; +const int32_t TAG_ESIM_RETRIEVE_NOTIFICATIONS_LIST = 0xBF2B; +const int32_t TAG_ESIM_REMOVE_NOTIFICATION_FROM_LIST = 0xBF30; +const int32_t TAG_ESIM_AUTHENTICATE_SERVER = 0xBF38; +const int32_t TAG_ESIM_PREPARE_DOWNLOAD = 0xBF21; +const int32_t TAG_ESIM_INITIALISE_SECURE_CHANNEL = 0xBF23; +} // namespace Telephony +} // namespace OHOS +#endif // TELEPHONY_TAG_DEF_H \ No newline at end of file diff --git a/utils/libtel_common_esim.versionscript b/utils/libtel_common_esim.versionscript new file mode 100644 index 000000000..18a16ffdd --- /dev/null +++ b/utils/libtel_common_esim.versionscript @@ -0,0 +1,44 @@ +# Copyright (C) 2024 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +1.0 { + global: + extern "C++" { + OHOS::Telephony::TelFFRTUtils*; + OHOS::Telephony::IsValidDecValue*; + OHOS::Telephony::IsValidHexValue*; + OHOS::Telephony::NapiUtil*; + OHOS::Telephony::TelephonyConfig*; + OHOS::Telephony::TelephonyPermission*; + OHOS::Telephony::TelephonyExtUtilsWrapper*; + OHOS::Telephony::TelEventHandler*; + OHOS::Telephony::TelProfileUtil*; + OHOS::Telephony::Asn1Utils*; + OHOS::Telephony::TelAesCryptoUtils*; + OHOS::Telephony::ToUtf*; + OHOS::Telephony::RequestApduBuild*; + OHOS::Telephony::ResetResponse*; + "OHOS::Telephony::GetBoolValue(int)"; + "OHOS::Telephony::GetBundleName()"; + "OHOS::Telephony::GetTokenID()"; + "OHOS::Telephony::GetCallState(int)"; + "OHOS::Telephony::GetCardType(int)"; + "OHOS::Telephony::GetCellularDataConnectionNetworkType(int)"; + "OHOS::Telephony::GetCellularDataConnectionState(int)"; + "OHOS::Telephony::GetCellularDataFlow(int)"; + "OHOS::Telephony::GetLockReason(int)"; + "OHOS::Telephony::GetSimState(int)"; + }; + local: + *; +}; From cadfb30f25ae406e0ce2313a4893e2cf2292655b Mon Sep 17 00:00:00 2001 From: wuqi Date: Sun, 29 Sep 2024 15:56:37 +0800 Subject: [PATCH 2/3] modify code review Signed-off-by: wuqi --- utils/codec/include/asn1_constants.h | 4 ++-- utils/codec/include/reset_response.h | 2 +- utils/codec/src/asn1_utils.cpp | 2 +- utils/codec/src/request_apdu_build.cpp | 6 ++++-- utils/codec/src/reset_response.cpp | 19 ++++++++----------- 5 files changed, 16 insertions(+), 17 deletions(-) diff --git a/utils/codec/include/asn1_constants.h b/utils/codec/include/asn1_constants.h index e36a71e87..4a7bcb5cc 100644 --- a/utils/codec/include/asn1_constants.h +++ b/utils/codec/include/asn1_constants.h @@ -23,8 +23,8 @@ namespace OHOS { namespace Telephony { const uint32_t BYTE_TO_HEX_LEN = 2; const uint32_t OFFSET_EIGHT_BIT = 8; -const int32_t BIT6_MASK = 0x20; -const int32_t BIT8_MASK = 0x80; +const uint32_t BIT6_MASK = 0x20; +const uint32_t BIT8_MASK = 0x80; const uint32_t MAX_UINT8 = std::numeric_limits::max(); const uint32_t MAX_UINT16 = std::numeric_limits::max(); const uint32_t MAX_UINT24 = (std::numeric_limits::max() >> 8); diff --git a/utils/codec/include/reset_response.h b/utils/codec/include/reset_response.h index d957158d0..5171ec891 100644 --- a/utils/codec/include/reset_response.h +++ b/utils/codec/include/reset_response.h @@ -27,7 +27,7 @@ public: bool IsEuiccAvailable(); private: - bool CheckOperationRes(uint8_t chr, const int32_t tMask, const int32_t comparedVal); + bool CheckOperationRes(uint8_t chr, const uint32_t tMask, const uint32_t comparedVal); bool AnalysisInterfaceData(const std::vector &atrData, uint32_t atrDataLen, uint32_t &index); bool CheckIsEuiccAvailable(uint8_t charB, uint8_t charD); bool CheckAtrDataParam(const std::string &atr); diff --git a/utils/codec/src/asn1_utils.cpp b/utils/codec/src/asn1_utils.cpp index 3df429dae..7946b76ac 100644 --- a/utils/codec/src/asn1_utils.cpp +++ b/utils/codec/src/asn1_utils.cpp @@ -161,7 +161,7 @@ int32_t Asn1Utils::BytesToInt(const std::vector &src, uint32_t offset, } std::vector subByteStream(src.begin() + offset, src.begin() + offset + length); std::string hexStr = BytesToHexStr(subByteStream); - int32_t valInt = std::stoi(hexStr, nullptr, HEX_DATA_LEN); + int32_t valInt = static_cast(strtol(hexStr.c_str(), nullptr, HEX_DATA_LEN)); return valInt; } diff --git a/utils/codec/src/request_apdu_build.cpp b/utils/codec/src/request_apdu_build.cpp index 5fed1b52a..aa7cfe5ec 100644 --- a/utils/codec/src/request_apdu_build.cpp +++ b/utils/codec/src/request_apdu_build.cpp @@ -29,13 +29,14 @@ const uint32_t P1_STORE_DATA_END = 0x91; std::list> RequestApduBuild::GetCommands() { std::lock_guard lock(mutex_); - return std::move(apduCommandLst_); + std::list> apduCommandTempLst(std::move(apduCommandLst_)); + apduCommandLst_.clear(); + return apduCommandTempLst; } void RequestApduBuild::AddApdu(const ApduData &apduData) { std::unique_ptr apduCommand = std::make_unique(channelId_, apduData); - std::lock_guard lock(mutex_); apduCommandLst_.push_back(std::move(apduCommand)); } @@ -60,6 +61,7 @@ void RequestApduBuild::BuildStoreData(const std::string &cmdHex) /* When handling packet fragmentation, if the last packet of data is less than 255 bytes, it requires special handling outside the loop. */ + std::lock_guard lock(mutex_); for (uint32_t i = 1; i < totalSubCmds; ++i) { std::string data = cmdHex.substr(startPos, cmdLen); ApduData apduData; diff --git a/utils/codec/src/reset_response.cpp b/utils/codec/src/reset_response.cpp index 3feb77530..30951f75f 100644 --- a/utils/codec/src/reset_response.cpp +++ b/utils/codec/src/reset_response.cpp @@ -22,13 +22,13 @@ namespace OHOS { namespace Telephony { namespace { -const int32_t BIT2_MASK = 0x02; -const int32_t BIT5_MASK = 0x10; -const int32_t BIT7_MASK = 0x40; -const int32_t HIGH_BITS_MASK = 0xF0; -const int32_t LOW_BITS_MASK = 0x0F; +const uint32_t BIT2_MASK = 0x02; +const uint32_t BIT5_MASK = 0x10; +const uint32_t BIT7_MASK = 0x40; +const uint32_t HIGH_BITS_MASK = 0xF0; +const uint32_t LOW_BITS_MASK = 0x0F; const uint32_t MIN_ATR_DATA_LENGTH = 4; -const int32_t MAX_INTERFACE_VALUE = 0x0F; +const uint32_t MAX_INTERFACE_VALUE = 0x0F; const uint8_t REVERSED_AGREEMENT = 0x3B; const uint8_t POSITIVE_AGREEMENT = 0x3F; } @@ -49,12 +49,9 @@ bool ResetResponse::CheckIsEuiccAvailable(uint8_t charB, uint8_t charD) return false; } -bool ResetResponse::CheckOperationRes(uint8_t chr, const int32_t tMask, const int32_t comparedVal) +bool ResetResponse::CheckOperationRes(uint8_t chr, const uint32_t tMask, const uint32_t comparedVal) { - if ((chr & tMask) != comparedVal) { - return false; - } - return true; + return ((chr & tMask) == comparedVal); } bool ResetResponse::AnalysisInterfaceData(const std::vector &atrData, uint32_t atrDataLen, uint32_t &index) From ad52354b21b799fe546cbe2f87df90bb28bd3d48 Mon Sep 17 00:00:00 2001 From: wuqi Date: Mon, 30 Sep 2024 11:18:30 +0800 Subject: [PATCH 3/3] modify code review Signed-off-by: wuqi --- utils/codec/src/asn1_utils.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/utils/codec/src/asn1_utils.cpp b/utils/codec/src/asn1_utils.cpp index 7946b76ac..e3e0b49de 100644 --- a/utils/codec/src/asn1_utils.cpp +++ b/utils/codec/src/asn1_utils.cpp @@ -147,7 +147,8 @@ std::vector Asn1Utils::HexStrToBytes(const std::string& hexStr) } for (size_t i = 0; i < hexStr.length(); i += BYTE_TO_HEX_LEN) { - uint8_t byte = static_cast(std::stoi(hexStr.substr(i, BYTE_TO_HEX_LEN), nullptr, HEX_DATA_LEN)); + uint8_t byte = static_cast(strtol((hexStr.substr(i, BYTE_TO_HEX_LEN)).c_str(), + nullptr, HEX_DATA_LEN)); ret.push_back(byte); } return ret;