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