Line data Source code
1 : /*
2 : * Copyright (c) 2023 Project CHIP Authors
3 : * All rights reserved.
4 : *
5 : * Licensed under the Apache License, Version 2.0 (the "License");
6 : * you may not use this file except in compliance with the License.
7 : * You may obtain a copy of the License at
8 : *
9 : * http://www.apache.org/licenses/LICENSE-2.0
10 : *
11 : * Unless required by applicable law or agreed to in writing, software
12 : * distributed under the License is distributed on an "AS IS" BASIS,
13 : * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 : * See the License for the specific language governing permissions and
15 : * limitations under the License.
16 : */
17 :
18 : #include <app/icd/client/DefaultCheckInDelegate.h>
19 : #include <crypto/CHIPCryptoPAL.h>
20 : #include <lib/support/CodeUtils.h>
21 : #include <lib/support/logging/CHIPLogging.h>
22 :
23 : namespace chip {
24 : namespace app {
25 :
26 1 : CHIP_ERROR DefaultCheckInDelegate::Init(ICDClientStorage * storage, InteractionModelEngine * engine)
27 : {
28 1 : VerifyOrReturnError(storage != nullptr, CHIP_ERROR_INVALID_ARGUMENT);
29 1 : VerifyOrReturnError(mpStorage == nullptr, CHIP_ERROR_INCORRECT_STATE);
30 1 : mpStorage = storage;
31 1 : mpImEngine = engine;
32 1 : return CHIP_NO_ERROR;
33 : }
34 :
35 5 : void DefaultCheckInDelegate::OnCheckInComplete(const ICDClientInfo & clientInfo)
36 : {
37 5 : ChipLogProgress(
38 : ICD, "Check In Message processing complete: start_counter=%" PRIu32 " offset=%" PRIu32 " nodeid=" ChipLogFormatScopedNodeId,
39 : clientInfo.start_icd_counter, clientInfo.offset, ChipLogValueScopedNodeId(clientInfo.peer_node));
40 5 : }
41 :
42 1 : CHIP_ERROR DefaultCheckInDelegate::GenerateRefreshKey(RefreshKeySender::RefreshKeyBuffer & newKey)
43 : {
44 1 : ReturnErrorOnFailure(Crypto::DRBG_get_bytes(newKey.Bytes(), newKey.Capacity()));
45 1 : return newKey.SetLength(newKey.Capacity());
46 : }
47 :
48 1 : RefreshKeySender * DefaultCheckInDelegate::OnKeyRefreshNeeded(ICDClientInfo & clientInfo, ICDClientStorage * clientStorage)
49 : {
50 1 : CHIP_ERROR err = CHIP_NO_ERROR;
51 1 : RefreshKeySender::RefreshKeyBuffer newKey;
52 1 : err = GenerateRefreshKey(newKey);
53 1 : if (err != CHIP_NO_ERROR)
54 : {
55 0 : ChipLogError(ICD, "Generation of new key failed: %" CHIP_ERROR_FORMAT, err.Format());
56 0 : return nullptr;
57 : }
58 :
59 1 : auto refreshKeySender = Platform::New<RefreshKeySender>(this, clientInfo, clientStorage, mpImEngine, newKey);
60 1 : if (refreshKeySender == nullptr)
61 : {
62 0 : return nullptr;
63 : }
64 1 : return refreshKeySender;
65 1 : }
66 :
67 1 : void DefaultCheckInDelegate::OnKeyRefreshDone(RefreshKeySender * refreshKeySender, CHIP_ERROR error)
68 : {
69 1 : if (error == CHIP_NO_ERROR)
70 : {
71 0 : ChipLogProgress(ICD, "Re-registration with new key completed successfully");
72 : }
73 : else
74 : {
75 1 : ChipLogError(ICD, "Re-registration with new key failed with error : %" CHIP_ERROR_FORMAT, error.Format());
76 : // The callee can take corrective action based on the error received.
77 : }
78 1 : if (refreshKeySender != nullptr)
79 : {
80 1 : Platform::Delete(refreshKeySender);
81 1 : refreshKeySender = nullptr;
82 : }
83 1 : }
84 : } // namespace app
85 : } // namespace chip
|