Line data Source code
1 : /*
2 : * Copyright (c) 2022 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 : /**
19 : * @file
20 : * This file defines the CHIP CASE Session object that provides
21 : * APIs for constructing a secure session using a certificate from the device's
22 : * operational credentials.
23 : */
24 :
25 : #pragma once
26 :
27 : #include <lib/core/TLV.h>
28 : #include <lib/support/DefaultStorageKeyAllocator.h>
29 : #include <protocols/secure_channel/DefaultSessionResumptionStorage.h>
30 :
31 : namespace chip {
32 :
33 : /**
34 : * An example SessionResumptionStorage using PersistentStorageDelegate as it backend.
35 : */
36 : class SimpleSessionResumptionStorage : public DefaultSessionResumptionStorage
37 : {
38 : public:
39 8 : CHIP_ERROR Init(PersistentStorageDelegate * storage)
40 : {
41 8 : VerifyOrReturnError(storage != nullptr, CHIP_ERROR_INVALID_ARGUMENT);
42 8 : mStorage = storage;
43 8 : return CHIP_NO_ERROR;
44 : }
45 :
46 : CHIP_ERROR SaveIndex(const SessionIndex & index) override;
47 : CHIP_ERROR LoadIndex(SessionIndex & index) override;
48 :
49 : CHIP_ERROR SaveLink(ConstResumptionIdView resumptionId, const ScopedNodeId & node) override;
50 : CHIP_ERROR LoadLink(ConstResumptionIdView resumptionId, ScopedNodeId & node) override;
51 : CHIP_ERROR DeleteLink(ConstResumptionIdView resumptionId) override;
52 :
53 : CHIP_ERROR SaveState(const ScopedNodeId & node, ConstResumptionIdView resumptionId,
54 : const Crypto::P256ECDHDerivedSecret & sharedSecret, const CATValues & peerCATs) override;
55 : CHIP_ERROR LoadState(const ScopedNodeId & node, ResumptionIdStorage & resumptionId,
56 : Crypto::P256ECDHDerivedSecret & sharedSecret, CATValues & peerCATs) override;
57 : CHIP_ERROR DeleteState(const ScopedNodeId & node) override;
58 :
59 : static StorageKeyName GetStorageKey(const ScopedNodeId & node);
60 : static StorageKeyName GetStorageKey(ConstResumptionIdView resumptionId);
61 :
62 : private:
63 : static constexpr size_t MaxScopedNodeIdSize() { return TLV::EstimateStructOverhead(sizeof(NodeId), sizeof(FabricIndex)); }
64 :
65 : static constexpr size_t MaxIndexSize()
66 : {
67 : // The max size of the list is (1 byte control + bytes for actual value) times max number of list items
68 : // Constant product inside a CHIPConfig.h macro; cannot widen at the use site.
69 : // NOLINTNEXTLINE(bugprone-implicit-widening-of-multiplication-result)
70 : return TLV::EstimateStructOverhead((1 + MaxScopedNodeIdSize()) * CHIP_CONFIG_CASE_SESSION_RESUME_CACHE_SIZE);
71 : }
72 :
73 : static constexpr size_t MaxStateSize()
74 : {
75 : return TLV::EstimateStructOverhead(kResumptionIdSize, Crypto::P256ECDHDerivedSecret::Capacity(),
76 : CATValues::kSerializedLength);
77 : }
78 :
79 : static constexpr TLV::Tag kFabricIndexTag = TLV::ContextTag(1);
80 : static constexpr TLV::Tag kPeerNodeIdTag = TLV::ContextTag(2);
81 : static constexpr TLV::Tag kResumptionIdTag = TLV::ContextTag(3);
82 : static constexpr TLV::Tag kSharedSecretTag = TLV::ContextTag(4);
83 : static constexpr TLV::Tag kCATTag = TLV::ContextTag(5);
84 :
85 : PersistentStorageDelegate * mStorage;
86 : };
87 :
88 : } // namespace chip
|