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 "fs_verity_descriptor_with_sign.h"
17
18 namespace OHOS {
19 namespace SignatureTools {
20
FsVerityDescriptorWithSign()21 FsVerityDescriptorWithSign::FsVerityDescriptorWithSign()
22 {
23 }
24
FsVerityDescriptorWithSign(FsVerityDescriptor fsVerityDescriptor,const std::vector<int8_t> & signature)25 FsVerityDescriptorWithSign::FsVerityDescriptorWithSign(FsVerityDescriptor fsVerityDescriptor,
26 const std::vector<int8_t> &signature)
27 {
28 this->fsVerityDescriptor = fsVerityDescriptor;
29 if (!signature.empty()) {
30 this->signature = signature;
31 }
32 length = FsVerityDescriptor::DESCRIPTOR_SIZE + this->signature.size();
33 }
34
FsVerityDescriptorWithSign(int32_t type,int32_t length,FsVerityDescriptor fsVerityDescriptor,const std::vector<int8_t> & signature)35 FsVerityDescriptorWithSign::FsVerityDescriptorWithSign(int32_t type, int32_t length,
36 FsVerityDescriptor fsVerityDescriptor, const std::vector<int8_t> &signature)
37 {
38 this->type = type;
39 this->length = length;
40 this->fsVerityDescriptor = fsVerityDescriptor;
41 this->signature = signature;
42 }
43
Size()44 int32_t FsVerityDescriptorWithSign::Size()
45 {
46 int tmpVariable = 2;
47 return INTEGER_BYTES * tmpVariable + FsVerityDescriptor::DESCRIPTOR_SIZE + signature.size();
48 }
49
ToByteArray(std::vector<int8_t> & ret)50 void FsVerityDescriptorWithSign::ToByteArray(std::vector<int8_t>& ret)
51 {
52 std::unique_ptr<ByteBuffer> buffer = std::make_unique<ByteBuffer>(Size());
53 buffer->PutInt32(type);
54 buffer->PutInt32(length);
55 std::vector<int8_t> fsArr;
56 fsVerityDescriptor.ToByteArray(fsArr);
57 buffer->PutData(fsArr.data(), fsArr.size());
58 buffer->PutData(signature.data(), signature.size());
59 buffer->Flip();
60 ret = std::vector<int8_t>(buffer->GetBufferPtr(), buffer->GetBufferPtr() + buffer->GetLimit());
61 return;
62 }
63
GetFsVerityDescriptor()64 FsVerityDescriptor FsVerityDescriptorWithSign::GetFsVerityDescriptor()
65 {
66 return fsVerityDescriptor;
67 }
68
GetSignature()69 std::vector<int8_t>& FsVerityDescriptorWithSign::GetSignature()
70 {
71 return signature;
72 }
73 } // namespace SignatureTools
74 } // namespace OHOS