Check the PIN when pairing

This commit is contained in:
Guillaume Meunier
2024-12-01 17:25:28 +01:00
parent 6e6cc3cf68
commit fe10178add
8 changed files with 161 additions and 8 deletions
+2
View File
@@ -143,6 +143,8 @@ public:
void on_unfocused() override;
void operator()(to_headset::crypto_handshake &&) {};
void operator()(to_headset::pin_check_2 &&) {};
void operator()(to_headset::pin_check_4 &&) {};
void operator()(to_headset::handshake &&) {};
void operator()(to_headset::video_stream_data_shard &&);
void operator()(to_headset::haptics &&);
+26
View File
@@ -20,6 +20,7 @@
#include "wivrn_client.h"
#include "hardware.h"
#include "secrets.h"
#include "smp.h"
#include "spdlog/common.h"
#include "utils/i18n.h"
#include "wivrn_packets.h"
@@ -125,6 +126,31 @@ void wivrn_session::handshake(T address, bool tcp_only, crypto::key & headset_ke
case to_headset::crypto_handshake::crypto_state::pin_needed:
pin = pin_enter(control.get_fd());
// Check the PIN
try
{
crypto::smp pin_check;
auto msg1 = pin_check.step1(pin);
send_control(from_headset::pin_check_1{msg1});
auto msg2 = std::get<to_headset::pin_check_2>(receive(10s)).message;
auto msg3 = pin_check.step3(msg2);
send_control(from_headset::pin_check_3{msg3});
auto msg4 = std::get<to_headset::pin_check_4>(receive(10s)).message;
bool pin_match = pin_check.step5(msg4);
if (not pin_match)
throw std::runtime_error(_("Incorrect PIN"));
}
catch (crypto::smp_cheated &)
{
throw std::runtime_error(_("Unable to check PIN"));
}
[[fallthrough]];
case to_headset::crypto_handshake::crypto_state::client_already_paired: {
+1
View File
@@ -32,6 +32,7 @@ add_library(wivrn-common-base STATIC
add_library(wivrn-common STATIC
crypto.cpp
smp.cpp
secrets.cpp
wivrn_sockets.cpp
utils/xdg_base_directory.cpp
+23 -2
View File
@@ -32,6 +32,7 @@
#include <vulkan/vulkan_core.h>
#include <openxr/openxr.h>
#include "smp.h"
#include "wivrn_serialization_types.h"
namespace wivrn
@@ -105,6 +106,16 @@ struct crypto_handshake
std::string name;
};
struct pin_check_1
{
crypto::smp::msg1 message;
};
struct pin_check_3
{
crypto::smp::msg3 message;
};
struct headset_info_packet
{
uint32_t recommended_eye_width;
@@ -262,7 +273,7 @@ struct battery
bool charging;
};
using packets = std::variant<crypto_handshake, headset_info_packet, feedback, audio_data, handshake, tracking, trackings, hand_tracking, inputs, timesync_response, battery>;
using packets = std::variant<crypto_handshake, pin_check_1, pin_check_3, headset_info_packet, feedback, audio_data, handshake, tracking, trackings, hand_tracking, inputs, timesync_response, battery>;
} // namespace from_headset
namespace to_headset
@@ -282,6 +293,16 @@ struct crypto_handshake
crypto_state state;
};
struct pin_check_2
{
crypto::smp::msg2 message;
};
struct pin_check_4
{
crypto::smp::msg4 message;
};
struct handshake
{
// -1 if stream socket should not be used
@@ -426,6 +447,6 @@ struct tracking_control
std::array<bool, size_t(id::last) + 1> enabled;
};
using packets = std::variant<crypto_handshake, handshake, audio_stream_description, video_stream_description, audio_data, video_stream_data_shard, haptics, timesync_query, tracking_control>;
using packets = std::variant<crypto_handshake, pin_check_2, pin_check_4, handshake, audio_stream_description, video_stream_description, audio_data, video_stream_data_shard, haptics, timesync_query, tracking_control>;
} // namespace to_headset
} // namespace wivrn
+30
View File
@@ -21,6 +21,7 @@
#include "boost/pfr/core.hpp"
#include "boost/pfr/tuple_size.hpp"
#include "smp.h"
#include <array>
#include <boost/pfr.hpp>
#include <chrono>
@@ -903,6 +904,35 @@ struct serialization_traits<data_holder>
}
};
template <>
struct serialization_traits<crypto::bignum>
{
static constexpr void type_hash(details::hash_context & h)
{
h.feed("bignum");
}
static void serialize(const crypto::bignum & value, serialization_packet & packet)
{
serialization_traits<std::string>::serialize(value.to_data(), packet);
}
static crypto::bignum deserialize(deserialization_packet & packet)
{
return crypto::bignum::from_data(serialization_traits<std::string>::deserialize(packet));
}
static bool consteval is_trivially_serializable()
{
return false;
}
static size_t size(const crypto::bignum & value)
{
return 2 + value.data_size();
}
};
template <typename T>
constexpr uint64_t serialization_type_hash(int revision)
{
+27
View File
@@ -20,6 +20,7 @@
#include "wivrn_connection.h"
#include "configuration.h"
#include "secrets.h"
#include "smp.h"
#include "wivrn_ipc.h"
#include "wivrn_packets.h"
#include <algorithm>
@@ -198,6 +199,32 @@ void wivrn::wivrn_connection::init(std::stop_token stop_token, std::function<voi
: to_headset::crypto_handshake::crypto_state::pin_needed,
});
if (not is_public_key_known)
{
try
{
// Check the PIN
crypto::smp pin_check;
auto msg1 = std::get<from_headset::pin_check_1>(receive(10s).first).message;
auto msg2 = pin_check.step2(msg1, pin);
control.send(to_headset::pin_check_2{msg2});
auto msg3 = std::get<from_headset::pin_check_3>(receive(10s).first).message;
auto [msg4, pin_match] = pin_check.step4(msg3);
control.send(to_headset::pin_check_4{msg4});
if (not pin_match)
throw std::runtime_error("Incorrect PIN");
}
catch (crypto::smp_cheated &)
{
throw std::runtime_error("Unable to check PIN");
}
}
secrets s{server_key, headset_key, is_public_key_known ? "000000" : pin};
control.set_aes_key_and_ivs(s.control_key, s.control_iv_from_headset, s.control_iv_to_headset);
stream.set_aes_key_and_ivs(s.stream_key, s.stream_iv_header_from_headset, s.stream_iv_header_to_headset);
+2
View File
@@ -139,6 +139,8 @@ public:
void set_enabled(device_id id, bool enabled);
void operator()(from_headset::crypto_handshake &&) {}
void operator()(from_headset::pin_check_1 &&) {}
void operator()(from_headset::pin_check_3 &&) {}
void operator()(from_headset::headset_info_packet &&);
void operator()(from_headset::handshake &&) {}
void operator()(from_headset::trackings &&);
+50 -6
View File
@@ -22,16 +22,17 @@
#include <cmath>
#include <epan/ftypes/ftypes.h>
#include <epan/packet.h>
#include <boost/pfr.hpp>
#include <epan/tvbuff.h>
#include <magic_enum.hpp>
#include "wivrn_packets.h"
#include <epan/proto.h>
#include <epan/tvbuff.h>
#include <epan/unit_strings.h>
#include <ws_version.h>
#include <boost/pfr.hpp>
#include <magic_enum.hpp>
#include "smp.h"
#include "wivrn_packets.h"
#if WIRESHARK_VERSION_MAJOR > 4 || (WIRESHARK_VERSION_MAJOR == 4 && WIRESHARK_VERSION_MINOR >= 4)
static inline uint16_t get_uint16(tvbuff_t * tvb, const int offset, const unsigned encoding)
{
@@ -740,6 +741,49 @@ struct tree_traits<abbrev, data_holder>
}
};
template <details::fixed_string abbrev>
struct tree_traits<abbrev, crypto::bignum>
{
static inline int field_handle = -1;
static void info()
{
hf_register_info hf = {
.p_id = &field_handle,
.hfinfo = {
.name = strdup(details::name_from_abbrev(abbrev.value).c_str()),
.abbrev = abbrev.value,
.type = FT_BYTES,
.display = BASE_NONE,
.strings = nullptr,
.bitmask = 0,
.blurb = "",
}};
HFILL_INIT(hf);
fields.push_back(hf);
}
static void dissect(proto_tree * tree, tvbuff_t * tvb, int & start)
{
size_t string_size = get_uint16(tvb, start, ENC_LITTLE_ENDIAN);
start += sizeof(uint16_t);
proto_tree_add_item(tree, field_handle, tvb, start, string_size, ENC_STRING);
start += string_size;
}
static size_t size(tvbuff_t * tvb, int & start)
{
size_t string_size = get_uint16(tvb, start, ENC_LITTLE_ENDIAN);
start += sizeof(uint16_t);
start += string_size;
return sizeof(uint16_t) + string_size;
}
};
int dissect_wivrn(tvbuff_t * tvb, packet_info * pinfo, proto_tree * tree _U_, void * data _U_, bool tcp)
{
col_set_str(pinfo->cinfo, COL_PROTOCOL, "WiVRn");