1 /*
2  * Copyright 2020 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #pragma once
18 
19 #include "common/contextual_callback.h"
20 #include "hci/address_with_type.h"
21 #include "l2cap/le/security_policy.h"
22 
23 namespace bluetooth {
24 namespace l2cap {
25 namespace le {
26 
27 /**
28  * The interface for Security Module to implement.
29  */
30 class SecurityEnforcementInterface {
31  public:
32   virtual ~SecurityEnforcementInterface() = default;
33 
34   using ResultCallback = common::ContextualOnceCallback<void(bool)>;
35 
36   /**
37    * Invoked when L2CAP needs to open a channel with given security requirement. When the Security Module satisfies the
38    * required security level, or cannot satisfy at all, invoke the result_callback.
39    */
40   virtual void Enforce(hci::AddressWithType remote, SecurityPolicy policy, ResultCallback result_callback) = 0;
41 };
42 
43 /**
44  * A default implementation which cannot satisfy any security level except
45  * NO_SECURITY_WHATSOEVER_PLAINTEXT_TRANSPORT_OK.
46  */
47 class SecurityEnforcementRejectAllImpl : public SecurityEnforcementInterface {
48  public:
Enforce(hci::AddressWithType remote,SecurityPolicy policy,ResultCallback result_callback)49   void Enforce(hci::AddressWithType remote, SecurityPolicy policy, ResultCallback result_callback) override {
50     if (policy == SecurityPolicy::NO_SECURITY_WHATSOEVER_PLAINTEXT_TRANSPORT_OK) {
51       result_callback.InvokeIfNotEmpty(true);
52     } else {
53       result_callback.InvokeIfNotEmpty(false);
54     }
55   }
56 };
57 }  // namespace le
58 }  // namespace l2cap
59 }  // namespace bluetooth
60