mirror of
https://gitee.com/openharmony/telephony_core_service
synced 2024-11-23 08:00:07 +00:00
!2095 add asn1 and atr analyzing
Merge pull request !2095 from wuqi/master
This commit is contained in:
commit
4e23266774
1
BUILD.gn
1
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" ]
|
||||
|
@ -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
|
||||
@ -36,4 +38,8 @@ group("unittest") {
|
||||
"unittest/utils_vcard_gtest:utils_vcard_branch_gtest",
|
||||
"unittest/utils_vcard_gtest:utils_vcard_gtest",
|
||||
]
|
||||
|
||||
if (core_service_support_esim) {
|
||||
deps += [ "unittest/utils_codec_gtest:utils_codec_gtest" ]
|
||||
}
|
||||
}
|
||||
|
51
test/unittest/utils_codec_gtest/BUILD.gn
Normal file
51
test/unittest/utils_codec_gtest/BUILD.gn
Normal file
@ -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" ]
|
||||
}
|
297
test/unittest/utils_codec_gtest/asn1_utils_test.cpp
Normal file
297
test/unittest/utils_codec_gtest/asn1_utils_test.cpp
Normal file
@ -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 <cstdbool>
|
||||
#include <gtest/gtest.h>
|
||||
#include <iostream>
|
||||
#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<uint8_t> 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<uint8_t> 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<uint8_t> 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<uint8_t> 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<uint8_t> 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<uint8_t> 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
|
51
test/unittest/utils_codec_gtest/request_apdu_build_test.cpp
Normal file
51
test/unittest/utils_codec_gtest/request_apdu_build_test.cpp
Normal file
@ -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 <cstdbool>
|
||||
#include <gtest/gtest.h>
|
||||
#include <iostream>
|
||||
#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<std::unique_ptr<ApduCommand>> commands = build.getCommands();
|
||||
EXPECT_EQ(1, commands.size());
|
||||
}
|
||||
#endif // TEL_TEST_UNSUPPORT
|
||||
}
|
||||
}
|
89
test/unittest/utils_codec_gtest/reset_reponse_test.cpp
Normal file
89
test/unittest/utils_codec_gtest/reset_reponse_test.cpp
Normal file
@ -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 <iostream>
|
||||
#include <gtest/gtest.h>
|
||||
#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
|
@ -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",
|
||||
|
36
utils/codec/include/asn1_constants.h
Normal file
36
utils/codec/include/asn1_constants.h
Normal file
@ -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 <cctype>
|
||||
#include <iomanip>
|
||||
|
||||
namespace OHOS {
|
||||
namespace Telephony {
|
||||
const uint32_t BYTE_TO_HEX_LEN = 2;
|
||||
const uint32_t OFFSET_EIGHT_BIT = 8;
|
||||
const uint32_t BIT6_MASK = 0x20;
|
||||
const uint32_t BIT8_MASK = 0x80;
|
||||
const uint32_t MAX_UINT8 = std::numeric_limits<uint8_t>::max();
|
||||
const uint32_t MAX_UINT16 = std::numeric_limits<uint16_t>::max();
|
||||
const uint32_t MAX_UINT24 = (std::numeric_limits<uint32_t>::max() >> 8);
|
||||
const uint32_t MAX_INT8 = std::numeric_limits<int8_t>::max();
|
||||
const uint32_t MAX_INT16 = std::numeric_limits<int16_t>::max();
|
||||
const uint32_t MAX_INT24 = (std::numeric_limits<int32_t>::max() >> 8);
|
||||
} // namespace Telephony
|
||||
} // namespace OHOS
|
||||
#endif // ASN1_CONSTANTS_H
|
53
utils/codec/include/asn1_utils.h
Normal file
53
utils/codec/include/asn1_utils.h
Normal file
@ -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 <cstdbool>
|
||||
#include <cstdint>
|
||||
#include <list>
|
||||
#include <vector>
|
||||
#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<uint8_t> &src, std::string &destStr);
|
||||
static void BcdToBytes(const std::string &bcd, std::vector<uint8_t> &bytes);
|
||||
static std::string BytesToHexStr(const std::vector<uint8_t> &bytes);
|
||||
static std::vector<uint8_t> HexStrToBytes(const std::string &hexStr);
|
||||
static uint32_t UintToBytes(const uint32_t value, std::vector<uint8_t> &bytes);
|
||||
static uint32_t IntToBytes(int32_t value, std::vector<uint8_t> &dest);
|
||||
static std::string BytesToString(const std::vector<uint8_t> &src);
|
||||
static std::string HexStrToString(const std::string &hexStr);
|
||||
static std::vector<uint8_t> StringToBytes(const std::string &src);
|
||||
static int32_t BytesToInt(const std::vector<uint8_t> &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_
|
41
utils/codec/include/request_apdu_build.h
Normal file
41
utils/codec/include/request_apdu_build.h
Normal file
@ -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 <list>
|
||||
#include <mutex>
|
||||
#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<std::unique_ptr<ApduCommand>> 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<std::unique_ptr<ApduCommand>> apduCommandLst_;
|
||||
std::mutex mutex_;
|
||||
};
|
||||
} // namespace Telephony
|
||||
} // namespace OHOS
|
||||
#endif
|
39
utils/codec/include/reset_response.h
Normal file
39
utils/codec/include/reset_response.h
Normal file
@ -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 <cstdint>
|
||||
#include <string>
|
||||
|
||||
namespace OHOS {
|
||||
namespace Telephony {
|
||||
class ResetResponse {
|
||||
public:
|
||||
bool AnalysisAtrData(const std::string &atr);
|
||||
bool IsEuiccAvailable();
|
||||
|
||||
private:
|
||||
bool CheckOperationRes(uint8_t chr, const uint32_t tMask, const uint32_t comparedVal);
|
||||
bool AnalysisInterfaceData(const std::vector<uint8_t> &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
|
280
utils/codec/src/asn1_utils.cpp
Normal file
280
utils/codec/src/asn1_utils.cpp
Normal file
@ -0,0 +1,280 @@
|
||||
/*
|
||||
* 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 <cctype>
|
||||
#include <cstdio>
|
||||
#include <securec.h>
|
||||
#include <sstream>
|
||||
#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<uint8_t> 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<uint8_t>(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<uint8_t> &src, std::string &destStr)
|
||||
{
|
||||
std::string hexStr = BytesToHexStr(src);
|
||||
destStr = SwapHexCharPair(hexStr);
|
||||
}
|
||||
|
||||
void Asn1Utils::BcdToBytes(const std::string &bcd, std::vector<uint8_t> &bytes)
|
||||
{
|
||||
std::string hexStr = SwapHexCharPair(bcd);
|
||||
bytes = HexStrToBytes(hexStr);
|
||||
}
|
||||
|
||||
std::string Asn1Utils::BytesToHexStr(const std::vector<uint8_t> &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<uint32_t>(dest.size());
|
||||
}
|
||||
|
||||
std::vector<uint8_t> Asn1Utils::HexStrToBytes(const std::string& hexStr)
|
||||
{
|
||||
std::vector<uint8_t> 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<uint8_t>(strtol((hexStr.substr(i, BYTE_TO_HEX_LEN)).c_str(),
|
||||
nullptr, HEX_DATA_LEN));
|
||||
ret.push_back(byte);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
int32_t Asn1Utils::BytesToInt(const std::vector<uint8_t> &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<uint8_t> subByteStream(src.begin() + offset, src.begin() + offset + length);
|
||||
std::string hexStr = BytesToHexStr(subByteStream);
|
||||
int32_t valInt = static_cast<int32_t>(strtol(hexStr.c_str(), nullptr, HEX_DATA_LEN));
|
||||
return valInt;
|
||||
}
|
||||
|
||||
uint32_t Asn1Utils::UintToBytes(const uint32_t value, std::vector<uint8_t> &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<uint8_t> &dest)
|
||||
{
|
||||
uint32_t len = ByteCountForInt(static_cast<uint32_t>(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<uint8_t> &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<uint8_t> bytes = {};
|
||||
for (size_t i = 0; i < inputStr.length(); i++) {
|
||||
bytes.push_back(static_cast<uint8_t>(inputStr[i]));
|
||||
}
|
||||
std::string result = BytesToHexStr(bytes);
|
||||
return result;
|
||||
}
|
||||
|
||||
std::string Asn1Utils::HexStrToString(const std::string& hexStr)
|
||||
{
|
||||
std::string inputHexStr = hexStr;
|
||||
std::vector<uint8_t> bytes = HexStrToBytes(inputHexStr);
|
||||
std::string result("");
|
||||
for (size_t i = 0; i < bytes.size(); i++) {
|
||||
result += (static_cast<char>(bytes[i]));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
std::vector<uint8_t> Asn1Utils::StringToBytes(const std::string &src)
|
||||
{
|
||||
std::string hexStr = StrToHexStr(src);
|
||||
std::vector<uint8_t> 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<uint32_t>(value)) & MAX_UINT8);
|
||||
// The number of bits occupied by non-zero values
|
||||
uint8_t nonZeroBitCount = static_cast<uint8_t>(NONZERO_OCCUPIED_BITS);
|
||||
if ((valueTemp & static_cast<uint32_t>(MASK_BYTES_ONE_BYTE)) != 0) {
|
||||
nonZeroBitCount -= static_cast<uint8_t>(OFFSET_FOUR_BIT);
|
||||
}
|
||||
if ((valueTemp & static_cast<uint32_t>(MASK_NIBBLES_ONE_BYTE)) != 0) {
|
||||
nonZeroBitCount -= static_cast<uint8_t>(OFFSET_TWO_BIT);
|
||||
}
|
||||
if ((valueTemp & static_cast<uint32_t>(MASK_PAIRS_ONE_BYTE)) != 0) {
|
||||
nonZeroBitCount -= static_cast<uint8_t>(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
|
||||
}
|
79
utils/codec/src/request_apdu_build.cpp
Normal file
79
utils/codec/src/request_apdu_build.cpp
Normal file
@ -0,0 +1,79 @@
|
||||
/*
|
||||
* 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<std::unique_ptr<ApduCommand>> RequestApduBuild::GetCommands()
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
std::list<std::unique_ptr<ApduCommand>> apduCommandTempLst(std::move(apduCommandLst_));
|
||||
apduCommandLst_.clear();
|
||||
return apduCommandTempLst;
|
||||
}
|
||||
|
||||
void RequestApduBuild::AddApdu(const ApduData &apduData)
|
||||
{
|
||||
std::unique_ptr<ApduCommand> apduCommand = std::make_unique<ApduCommand>(channelId_, apduData);
|
||||
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<uint32_t>(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.
|
||||
*/
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
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
|
148
utils/codec/src/reset_response.cpp
Normal file
148
utils/codec/src/reset_response.cpp
Normal file
@ -0,0 +1,148 @@
|
||||
/*
|
||||
* 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 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 uint32_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 uint32_t tMask, const uint32_t comparedVal)
|
||||
{
|
||||
return ((chr & tMask) == comparedVal);
|
||||
}
|
||||
|
||||
bool ResetResponse::AnalysisInterfaceData(const std::vector<uint8_t> &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<uint8_t> 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
|
107
utils/common/include/telephony_tag_def.h
Normal file
107
utils/common/include/telephony_tag_def.h
Normal file
@ -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
|
44
utils/libtel_common_esim.versionscript
Normal file
44
utils/libtel_common_esim.versionscript
Normal file
@ -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:
|
||||
*;
|
||||
};
|
Loading…
Reference in New Issue
Block a user