• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024-2024 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 #include "signer_factory.h"
16 #include "dynamic_lib_handle.h"
17 
18 namespace OHOS {
19 namespace SignatureTools {
20 
GetSigner(LocalizationAdapter & adapter) const21 std::shared_ptr<Signer> SignerFactory::GetSigner(LocalizationAdapter& adapter)const
22 {
23     if (adapter.IsRemoteSigner()) {
24         return LoadRemoteSigner(adapter);
25     }
26 
27     EVP_PKEY* keyPair = adapter.GetAliasKey(false);
28     if (keyPair == NULL) {
29         SIGNATURE_TOOLS_LOGE("key is NULL, get signer failed");
30         adapter.ResetPwd();
31         return NULL;
32     }
33     adapter.ResetPwd();
34     STACK_OF(X509)*certs = adapter.GetSignCertChain();
35     std::shared_ptr<Signer> signer = std::make_shared<LocalSigner>(keyPair, certs);
36     return signer;
37 }
38 
LoadRemoteSigner(LocalizationAdapter & adapter) const39 std::shared_ptr<Signer> SignerFactory::LoadRemoteSigner(LocalizationAdapter& adapter) const
40 {
41     std::string keyAlias = adapter.GetOptions()->GetString(ParamConstants::PARAM_BASIC_PRIVATE_KEY);
42     std::string signServer = adapter.GetOptions()->GetString(ParamConstants::PARAM_REMOTE_SERVER);
43     std::string signerPlugin = adapter.GetOptions()->GetString(ParamConstants::PARAM_REMOTE_SIGNERPLUGIN);
44     std::string onlineAuthMode = adapter.GetOptions()->GetString(ParamConstants::PARAM_REMOTE_ONLINEAUTHMODE);
45     std::string username = adapter.GetOptions()->GetString(ParamConstants::PARAM_REMOTE_USERNAME);
46     char* userPwd = adapter.GetOptions()->GetChars(ParamConstants::PARAM_REMOTE_USERPWD);
47 
48     // open so
49     DynamicLibHandle::handle = dlopen(signerPlugin.c_str(), RTLD_NOW | RTLD_GLOBAL);
50     if (!DynamicLibHandle::handle) {
51         PrintErrorNumberMsg("LoadRemoteSigner", RET_FAILED, dlerror());
52         return nullptr;
53     }
54 
55     // clear previous error
56     dlerror();
57 
58     // get "Create" function
59     RemoteSignerCreator remoteSignerCreator = (RemoteSignerCreator)dlsym(DynamicLibHandle::handle, "Create");
60     char* error = nullptr;
61     if ((error = dlerror()) != NULL) {
62         SIGNATURE_TOOLS_LOGE("%s", error);
63         return nullptr;
64     }
65 
66     RemoteSignerParamType keyAliasType{keyAlias.c_str(), keyAlias.size()};
67     RemoteSignerParamType signServerType{signServer.c_str(), signServer.size()};
68     RemoteSignerParamType onlineAuthModeType{onlineAuthMode.c_str(), onlineAuthMode.size()};
69     RemoteSignerParamType usernameType{username.c_str(), username.size()};
70     RemoteSignerParamType userPwdType{userPwd, strlen(userPwd)};
71 
72     Signer* signer = remoteSignerCreator(keyAliasType, signServerType, onlineAuthModeType, usernameType, userPwdType);
73 
74     for (size_t i = 0; i < strlen(userPwd); i++) {
75         userPwd[i] = 0;
76     }
77 
78     std::shared_ptr<Signer> remoteSigner(signer);
79     return remoteSigner;
80 }
81 } // namespace SignatureTools
82 } // namespace OHOS