1 /*
2 * Copyright (c) 2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "native_interface_bundle.h"
17
18 #include <mutex>
19 #include <string>
20
21 #include "application_info.h"
22 #include "app_log_wrapper.h"
23 #include "bundle_mgr_proxy_native.h"
24 #include "ipc_skeleton.h"
25 #include "securec.h"
26 namespace {
27 const size_t CHAR_MAX_LENGTH = 10240;
28 const int32_t DEFAULT_USERID = -2;
29 }
30
OH_NativeBundle_GetCurrentApplicationInfo()31 OH_NativeBundle_ApplicationInfo OH_NativeBundle_GetCurrentApplicationInfo()
32 {
33 OH_NativeBundle_ApplicationInfo nativeApplicationInfo;
34 int32_t uid = OHOS::IPCSkeleton::GetCallingUid();
35 OHOS::AppExecFwk::BundleMgrProxyNative bundleMgrProxyNative;
36 std::string bundleName;
37 if (!bundleMgrProxyNative.GetBundleNameForUid(uid, bundleName)) {
38 APP_LOGE("can not get bundleName for uid:%{public}d", uid);
39 return nativeApplicationInfo;
40 };
41 OHOS::AppExecFwk::ApplicationInfo applicationInfo;
42 auto appInfoflag = OHOS::AppExecFwk::ApplicationFlag::GET_APPLICATION_INFO_WITH_CERTIFICATE_FINGERPRINT;
43 if (!bundleMgrProxyNative.GetApplicationInfo(bundleName, appInfoflag, DEFAULT_USERID, applicationInfo)) {
44 APP_LOGE("can not get applicationInfo for bundleName:%{public}s", bundleName.c_str());
45 return nativeApplicationInfo;
46 }
47
48 size_t bundleNameLen = applicationInfo.bundleName.size();
49 if ((bundleNameLen == 0) || (bundleNameLen + 1) > CHAR_MAX_LENGTH) {
50 APP_LOGE("failed due to the length of bundleName is 0 or to long");
51 return nativeApplicationInfo;
52 }
53 nativeApplicationInfo.bundleName = static_cast<char*>(malloc(bundleNameLen + 1));
54 if (nativeApplicationInfo.bundleName == nullptr) {
55 APP_LOGE("failed due to malloc error");
56 return nativeApplicationInfo;
57 }
58
59 if (strcpy_s(nativeApplicationInfo.bundleName, bundleNameLen + 1, applicationInfo.bundleName.c_str()) != EOK) {
60 APP_LOGE("failed due to strcpy_s error");
61 free(nativeApplicationInfo.bundleName);
62 nativeApplicationInfo.bundleName = nullptr;
63 return nativeApplicationInfo;
64 }
65
66 size_t fingerprintLen = applicationInfo.fingerprint.size();
67 if ((fingerprintLen == 0) || (fingerprintLen + 1) > CHAR_MAX_LENGTH) {
68 APP_LOGE("failed due to the length of fingerprint is 0 or to long");
69 free(nativeApplicationInfo.bundleName);
70 nativeApplicationInfo.bundleName = nullptr;
71 return nativeApplicationInfo;
72 }
73 nativeApplicationInfo.fingerprint = static_cast<char*>(malloc(fingerprintLen + 1));
74 if (nativeApplicationInfo.fingerprint == nullptr) {
75 APP_LOGE("failed due to malloc error");
76 free(nativeApplicationInfo.bundleName);
77 nativeApplicationInfo.bundleName = nullptr;
78 return nativeApplicationInfo;
79 }
80
81 if (strcpy_s(nativeApplicationInfo.fingerprint, fingerprintLen + 1, applicationInfo.fingerprint.c_str()) != EOK) {
82 APP_LOGE("failed due to strcpy_s error");
83 free(nativeApplicationInfo.bundleName);
84 nativeApplicationInfo.bundleName = nullptr;
85 free(nativeApplicationInfo.fingerprint);
86 nativeApplicationInfo.fingerprint = nullptr;
87 return nativeApplicationInfo;
88 }
89 APP_LOGI("OH_NativeBundle_GetCurrentApplicationInfo success");
90 return nativeApplicationInfo;
91 }
92