• 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 <vector>
16 
17 #include "signature_tools_log.h"
18 #include "pkcs7_data.h"
19 #include "signature_algorithm_helper.h"
20 #include "bc_signeddata_generator.h"
21 #include "signer_config.h"
22 #include "signature_tools_errno.h"
23 #include "bc_pkcs7_generator.h"
24 
25 namespace OHOS {
26 namespace SignatureTools {
~BCPkcs7Generator()27 BCPkcs7Generator::~BCPkcs7Generator()
28 {
29 }
GenerateSignedData(const std::string & content,SignerConfig * signerConfig,std::string & ret)30 int BCPkcs7Generator::GenerateSignedData(const std::string& content,
31                                          SignerConfig* signerConfig, std::string& ret)
32 {
33     int result = RET_OK;
34     std::string sigAlg;
35     if (content.empty()) {
36         PrintErrorNumberMsg("INVALIDPARAM_ERROR", INVALIDPARAM_ERROR,
37                             "digest content is empty, generate signed data failed");
38         return INVALIDPARAM_ERROR;
39     }
40     if (signerConfig == NULL) {
41         PrintErrorNumberMsg("INVALIDPARAM_ERROR", INVALIDPARAM_ERROR, "signerConfig is NULL");
42         return INVALIDPARAM_ERROR;
43     }
44     std::shared_ptr<Signer> signer(signerConfig->GetSigner());
45     if (signer == NULL) {
46         SIGNATURE_TOOLS_LOGE("signer is NULL");
47         return INVALIDPARAM_ERROR;
48     }
49     result = BCSignedDataGenerator::GetSigAlg(signerConfig, sigAlg);
50     if (result < 0) {
51         SIGNATURE_TOOLS_LOGE("get sigAlg from signerConfig failed");
52         return result;
53     }
54     result = PackagePKCS7(content, signer, sigAlg, ret);
55     if (result < 0) {
56         SIGNATURE_TOOLS_LOGE("package pkcs7 failed!");
57         return result;
58     }
59     return result;
60 }
PackagePKCS7(const std::string & content,const std::shared_ptr<Signer> & signer,const std::string & sigAlg,std::string & ret)61 int BCPkcs7Generator::PackagePKCS7(const std::string& content, const std::shared_ptr<Signer>& signer,
62                                    const std::string& sigAlg, std::string& ret)
63 {
64     PKCS7Data p7Data;
65     int result = RET_OK;
66     result = p7Data.Sign(content, signer, sigAlg, ret);
67     if (result < 0) {
68         SIGNATURE_TOOLS_LOGE("generate pkcs7 block failed");
69         return SIGN_ERROR;
70     }
71     result = p7Data.Parse(ret);
72     if (result < 0) {
73         SIGNATURE_TOOLS_LOGE("parse pkcs7 bytes failed");
74         return PARSE_ERROR;
75     }
76     result = p7Data.Verify();
77     if (result < 0) {
78         SIGNATURE_TOOLS_LOGE("verify pkcs7 block failed");
79         return VERIFY_ERROR;
80     }
81     return result;
82 }
83 } // namespace SignatureTools
84 } // namespace OHOS