1 /*
2 * Copyright (c) 2025 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 "dm_auth_cert.h"
17
18 #include "dm_error_type.h"
19 #include "dm_log.h"
20
21 namespace OHOS {
22 namespace DistributedHardware {
23
24 constexpr const char* LIB_DM_AUTH_CERT = "libdm_auth_cert.z.so";
25
AuthCert()26 AuthCert::AuthCert()
27 {
28 LOGD("AuthCert AuthCert");
29 }
30
~AuthCert()31 AuthCert::~AuthCert()
32 {
33 LOGD("AuthCert destructor");
34 if (authCertSoHandle_ != nullptr) {
35 LOGI("dm auth cert authCertSoHandle_ is not nullptr.");
36 dlclose(authCertSoHandle_);
37 authCertSoHandle_ = nullptr;
38 }
39 }
40
IsDMAdapterAuthCertLoaded()41 bool AuthCert::IsDMAdapterAuthCertLoaded()
42 {
43 LOGI("Start.");
44 std::lock_guard<std::mutex> lock(isAdapterAuthCertLoadedLock_);
45 if (isAdapterAuthCertSoLoaded_ && (dmAuthCertExt_ != nullptr)) {
46 return true;
47 }
48 authCertSoHandle_ = dlopen(LIB_DM_AUTH_CERT, RTLD_NOW | RTLD_NODELETE | RTLD_NOLOAD);
49 char *error = dlerror();
50 if (error != nullptr) {
51 LOGE("dlopen failed, err: %{public}s", error);
52 }
53 if (authCertSoHandle_ == nullptr) {
54 authCertSoHandle_ = dlopen(LIB_DM_AUTH_CERT, RTLD_NOW | RTLD_NODELETE);
55 error = dlerror();
56 if (error != nullptr) {
57 LOGE("dlopen failed again! err: %{public}s", error);
58 }
59 }
60 if (authCertSoHandle_ == nullptr) {
61 LOGE("load dm check api white list so failed.");
62 return false;
63 }
64 auto func = (CreateDMAuthCertFuncPtr)dlsym(authCertSoHandle_, "CreateDMAuthCertExtObject");
65 if (func == nullptr || dlerror() != nullptr) {
66 dlclose(authCertSoHandle_);
67 authCertSoHandle_ = nullptr;
68 LOGE("Create object function is not exist. err: %{public}s", (dlerror() == nullptr ? "null" : dlerror()));
69 return false;
70 }
71 dmAuthCertExt_ = std::shared_ptr<IDMAuthCertExt>(func());
72 isAdapterAuthCertSoLoaded_ = true;
73 LOGI("Success.");
74 return true;
75 }
76
GenerateCertificate(DmCertChain & dmCertChain)77 int32_t AuthCert::GenerateCertificate(DmCertChain &dmCertChain)
78 {
79 if (!IsDMAdapterAuthCertLoaded()) {
80 LOGE("authCertSo load failed!");
81 return ERR_DM_FAILED;
82 }
83 return dmAuthCertExt_->GenerateCertificate(dmCertChain);
84 }
85
VerifyCertificate(const DmCertChain & dmCertChain,const char * deviceIdHash)86 int32_t AuthCert::VerifyCertificate(const DmCertChain &dmCertChain, const char *deviceIdHash)
87 {
88 if (!IsDMAdapterAuthCertLoaded()) {
89 LOGE("authCertSo load failed!");
90 return ERR_DM_FAILED;
91 }
92 return dmAuthCertExt_->VerifyCertificate(dmCertChain, deviceIdHash);
93 }
94
GenerateCertificateV2(DmCertChain & dmCertChain,const uint64_t random)95 int32_t AuthCert::GenerateCertificateV2(DmCertChain &dmCertChain, const uint64_t random)
96 {
97 if (!IsDMAdapterAuthCertLoaded()) {
98 LOGE("authCertSo load failed!");
99 return ERR_DM_FAILED;
100 }
101 return dmAuthCertExt_->GenerateCertificateV2(dmCertChain, random);
102 }
103
VerifyCertificateV2(const DmCertChain & dmCertChain,const char * deviceIdHash,const uint64_t random)104 int32_t AuthCert::VerifyCertificateV2(const DmCertChain &dmCertChain, const char *deviceIdHash, const uint64_t random)
105 {
106 if (!IsDMAdapterAuthCertLoaded()) {
107 LOGE("authCertSo load failed!");
108 return ERR_DM_FAILED;
109 }
110 return dmAuthCertExt_->VerifyCertificateV2(dmCertChain, deviceIdHash, random);
111 }
112 } // namespace DistributedHardware
113 } // namespace OHOS