1 /*
2  * Copyright (c) 2023 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 "sec_comp_enhance_data_parcel.h"
17 
18 #include <unistd.h>
19 
20 #include "parcel_utils.h"
21 #include "securec.h"
22 
23 namespace OHOS {
24 namespace Security {
25 namespace AccessToken {
Marshalling(Parcel & out) const26 bool SecCompEnhanceDataParcel::Marshalling(Parcel& out) const
27 {
28     RETURN_IF_FALSE(out.WriteInt32(this->enhanceData.pid));
29     RETURN_IF_FALSE(out.WriteUint32(this->enhanceData.token));
30     RETURN_IF_FALSE(out.WriteUint64(this->enhanceData.challenge));
31     RETURN_IF_FALSE(out.WriteUint32(this->enhanceData.sessionId));
32     RETURN_IF_FALSE(out.WriteUint32(this->enhanceData.seqNum));
33     RETURN_IF_FALSE(out.WriteBuffer(this->enhanceData.key, AES_KEY_STORAGE_LEN));
34     RETURN_IF_FALSE((static_cast<MessageParcel*>(&out))->WriteRemoteObject(this->enhanceData.callback));
35     return true;
36 }
37 
Unmarshalling(Parcel & in)38 SecCompEnhanceDataParcel* SecCompEnhanceDataParcel::Unmarshalling(Parcel& in)
39 {
40     auto* enhanceDataParcel = new (std::nothrow) SecCompEnhanceDataParcel();
41     if (enhanceDataParcel == nullptr) {
42         return nullptr;
43     }
44 
45     RELEASE_IF_FALSE(in.ReadInt32(enhanceDataParcel->enhanceData.pid), enhanceDataParcel);
46     RELEASE_IF_FALSE(in.ReadUint32(enhanceDataParcel->enhanceData.token), enhanceDataParcel);
47     RELEASE_IF_FALSE(in.ReadUint64(enhanceDataParcel->enhanceData.challenge), enhanceDataParcel);
48     RELEASE_IF_FALSE(in.ReadUint32(enhanceDataParcel->enhanceData.sessionId), enhanceDataParcel);
49     RELEASE_IF_FALSE(in.ReadUint32(enhanceDataParcel->enhanceData.seqNum), enhanceDataParcel);
50     const uint8_t* ptr = in.ReadBuffer(AES_KEY_STORAGE_LEN);
51     if (ptr == nullptr) {
52         delete enhanceDataParcel;
53         return nullptr;
54     }
55     if (memcpy_s(enhanceDataParcel->enhanceData.key, AES_KEY_STORAGE_LEN, ptr, AES_KEY_STORAGE_LEN) != EOK) {
56         delete enhanceDataParcel;
57         return nullptr;
58     }
59 
60     enhanceDataParcel->enhanceData.callback = (static_cast<MessageParcel*>(&in))->ReadRemoteObject();
61     if (enhanceDataParcel->enhanceData.callback == nullptr) {
62         delete enhanceDataParcel;
63         return nullptr;
64     }
65 
66     return enhanceDataParcel;
67 }
68 } // namespace AccessToken
69 } // namespace Security
70 } // namespace OHOS
71 
72