• 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 
16 #include "sign_content_info.h"
17 #include "byte_array_utils.h"
18 #include "signature_tools_log.h"
19 
20 namespace OHOS {
21 namespace SignatureTools {
22 
SignContentHash(const char type,const char tag,const short algId,const int length,const std::vector<int8_t> & hash)23 SignContentHash::SignContentHash(const char type, const char tag, const short algId,
24                                  const int length, const std::vector<int8_t> &hash)
25 {
26     m_type = type;
27     m_tag = tag;
28     m_algId = algId;
29     m_length = length;
30     m_hash = hash;
31     m_contentHashLen = CONTENT_HEAD_SIZE + hash.size();
32 }
33 
SignContentInfo()34 SignContentInfo::SignContentInfo()
35 {
36     m_version = "1000";
37     m_size = SignContentHash::CONTENT_HEAD_SIZE;
38     m_numOfBlocks = 0;
39 }
40 
AddContentHashData(const char type,const char tag,const short algId,const int length,const std::vector<int8_t> & hash)41 void SignContentInfo::AddContentHashData(const char type, const char tag, const short algId,
42                                          const int length, const std::vector<int8_t> &hash)
43 {
44     SignContentHash signInfo(type, tag, algId, length, hash);
45     AddHashData(signInfo);
46 }
47 
AddHashData(const SignContentHash & signInfo)48 void SignContentInfo::AddHashData(const SignContentHash& signInfo)
49 {
50     m_hashData.push_back(signInfo);
51     ++m_numOfBlocks;
52     m_size += signInfo.m_contentHashLen;
53 }
54 
GetByteContent()55 std::vector<int8_t> SignContentInfo::GetByteContent()
56 {
57     std::vector<int8_t> ret(m_size, 0);
58     int index = 0;
59 
60     index = ByteArrayUtils::InsertCharToByteArray(ret, index, m_version);
61     if (index < 0) {
62         PrintErrorNumberMsg("SIGN_ERROR", SIGN_ERROR, "InsertCharToByteArray failed.");
63         return std::vector<int8_t>();
64     }
65 
66     index = ByteArrayUtils::InsertShortToByteArray(ret, ret.size(), index, m_size);
67     if (index < 0) {
68         PrintErrorNumberMsg("SIGN_ERROR", SIGN_ERROR, "InsertShortToByteArray failed.");
69         return std::vector<int8_t>();
70     }
71 
72     index = ByteArrayUtils::InsertShortToByteArray(ret, ret.size(), index, m_numOfBlocks);
73     if (index < 0) {
74         PrintErrorNumberMsg("SIGN_ERROR", SIGN_ERROR, "InsertShortToByteArray failed.");
75         return std::vector<int8_t>();
76     }
77 
78     for (const auto& tmp : m_hashData) {
79         ret[index] = tmp.m_type;
80         index++;
81         ret[index] = tmp.m_tag;
82         index++;
83         index = ByteArrayUtils::InsertShortToByteArray(ret, ret.size(), index, tmp.m_algId);
84         index = ByteArrayUtils::InsertIntToByteArray(ret, index, tmp.m_length);
85         index = ByteArrayUtils::InsertByteToByteArray(ret, index, tmp.m_hash, tmp.m_hash.size());
86         if (index < 0) {
87             PrintErrorNumberMsg("SIGN_ERROR", SIGN_ERROR, "InsertShortToByteArray failed.");
88             return std::vector<int8_t>();
89         }
90     }
91     return ret;
92 }
93 
94 } // namespace SignatureTools
95 } // namespace OHOS