1 /******************************************************************************
2  *
3  *  Copyright 2019 The Android Open Source Project
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 #pragma once
20 
21 #include <optional>
22 #include <variant>
23 
24 #include "hci/address_with_type.h"
25 #include "hci/le_security_interface.h"
26 #include "os/handler.h"
27 #include "os/queue.h"
28 #include "security/ecdh_keys.h"
29 #include "security/pairing_failure.h"
30 #include "security/smp_packets.h"
31 #include "security/ui.h"
32 
33 namespace bluetooth {
34 namespace security {
35 
36 struct DistributedKeys {
37   /* LE Keys*/
38   std::optional<hci::Octet16> remote_ltk;
39   std::optional<uint16_t> remote_ediv;
40   std::optional<std::array<uint8_t, 8>> remote_rand;
41   std::optional<hci::AddressWithType> remote_identity_address;
42   std::optional<hci::Octet16> remote_irk;
43   std::optional<hci::Octet16> remote_signature_key;
44   std::optional<hci::Octet16> remote_link_key; /* BR/EDR Keys */
45 
46   std::optional<hci::Octet16> local_ltk;
47   std::optional<uint16_t> local_ediv;
48   std::optional<std::array<uint8_t, 8>> local_rand;
49   std::optional<hci::Octet16> local_signature_key;
50 };
51 
52 /* This class represents the result of pairing, as returned from Pairing Handler */
53 struct PairingResult {
54   hci::AddressWithType connection_address;
55   DistributedKeys distributed_keys;
56   uint8_t key_size;
57   uint8_t security_level;
58 };
59 
60 using PairingResultOrFailure = std::variant<PairingResult, PairingFailure>;
61 
62 /* Data we use for Out Of Band Pairing */
63 struct MyOobData {
64   /*  private key is just for this single pairing only, so it might be safe to
65    * expose it to other parts of stack. It should not be exposed to upper
66    * layers though */
67   std::array<uint8_t, 32> private_key;
68   EcdhPublicKey public_key;
69   hci::Octet16 c;
70   hci::Octet16 r;
71 };
72 
73 /* This structure is filled and send to PairingHandlerLe to initiate the Pairing process with remote device */
74 struct InitialInformations {
75   hci::Role my_role;
76   hci::AddressWithType my_connection_address;
77 
78   hci::AddressWithType my_identity_address;
79   hci::Octet16 my_identity_resolving_key;
80 
81   /* My capabilities, as in pairing request/response */
82   struct {
83     IoCapability io_capability;
84     OobDataFlag oob_data_flag;
85     uint8_t auth_req;
86     uint8_t maximum_encryption_key_size;
87     uint8_t initiator_key_distribution;
88     uint8_t responder_key_distribution;
89   } myPairingCapabilities;
90 
91   /* was it remote device that initiated the Pairing ? */
92   bool remotely_initiated;
93   uint16_t connection_handle;
94   hci::AddressWithType remote_connection_address;
95   std::string remote_name;
96 
97   /* contains pairing request, if the pairing was remotely initiated */
98   std::optional<PairingRequestView> pairing_request;
99 
100   struct out_of_band_data {
101     hci::Octet16 le_sc_c; /* LE Secure Connections Confirmation Value */
102     hci::Octet16 le_sc_r; /* LE Secure Connections Random Value */
103 
104     hci::Octet16 security_manager_tk_value; /* OOB data for LE Legacy Pairing */
105   };
106 
107   // If we received OOB data from remote device, this field contains it.
108   std::optional<out_of_band_data> remote_oob_data;
109   std::optional<MyOobData> my_oob_data;
110 
111   /* Used by Pairing Handler to present user with requests*/
112   UI* user_interface;
113   os::Handler* user_interface_handler;
114 
115   /* HCI interface to use */
116   hci::LeSecurityInterface* le_security_interface;
117 
118   os::EnqueueBuffer<packet::BasePacketBuilder>* proper_l2cap_interface;
119   os::Handler* l2cap_handler;
120 
121   /* Callback to execute once the Pairing process is finished */
122   std::function<void(PairingResultOrFailure)> OnPairingFinished;
123 };
124 
125 }  // namespace security
126 }  // namespace bluetooth
127