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_library_handle.h" 17 18namespace OHOS { 19namespace SignatureTools { 20 21std::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 if (signer == NULL) { 37 SIGNATURE_TOOLS_LOGE("signer is NULL, create LocalSigner failed"); 38 EVP_PKEY_free(keyPair); 39 sk_X509_pop_free(certs, X509_free); 40 return NULL; 41 } 42 return signer; 43} 44 45std::shared_ptr<Signer> SignerFactory::LoadRemoteSigner(LocalizationAdapter& adapter) const 46{ 47 std::string keyAlias = adapter.GetOptions()->GetString(ParamConstants::PARAM_BASIC_PRIVATE_KEY); 48 std::string signServer = adapter.GetOptions()->GetString(ParamConstants::PARAM_REMOTE_SERVER); 49 std::string signerPlugin = adapter.GetOptions()->GetString(ParamConstants::PARAM_REMOTE_SIGNERPLUGIN); 50 std::string onlineAuthMode = adapter.GetOptions()->GetString(ParamConstants::PARAM_REMOTE_ONLINEAUTHMODE); 51 std::string username = adapter.GetOptions()->GetString(ParamConstants::PARAM_REMOTE_USERNAME); 52 char* userPwd = adapter.GetOptions()->GetChars(ParamConstants::PARAM_REMOTE_USERPWD); 53 54 // open so 55 DynamicLibraryHandle::handle = dlopen(signerPlugin.c_str(), RTLD_NOW | RTLD_GLOBAL); 56 if (!DynamicLibraryHandle::handle) { 57 PrintErrorNumberMsg("LoadRemoteSigner", RET_FAILED, dlerror()); 58 return nullptr; 59 } 60 61 // clear previous error 62 dlerror(); 63 64 // get "Create" function 65 RemoteSignerCreator remoteSignerCreator = (RemoteSignerCreator)dlsym(DynamicLibraryHandle::handle, "Create"); 66 char* error = nullptr; 67 if ((error = dlerror()) != NULL) { 68 SIGNATURE_TOOLS_LOGE("%s", error); 69 return nullptr; 70 } 71 72 RemoteSignerParamType keyAliasType{keyAlias.c_str(), keyAlias.size()}; 73 RemoteSignerParamType signServerType{signServer.c_str(), signServer.size()}; 74 RemoteSignerParamType onlineAuthModeType{onlineAuthMode.c_str(), onlineAuthMode.size()}; 75 RemoteSignerParamType usernameType{username.c_str(), username.size()}; 76 RemoteSignerParamType userPwdType{userPwd, strlen(userPwd)}; 77 78 Signer* signer = remoteSignerCreator(keyAliasType, signServerType, onlineAuthModeType, usernameType, userPwdType); 79 80 for (size_t i = 0; i < strlen(userPwd); i++) { 81 userPwd[i] = 0; 82 } 83 84 std::shared_ptr<Signer> remoteSigner(signer); 85 return remoteSigner; 86} 87} // namespace SignatureTools 88} // namespace OHOS