diff --git a/client/scenes/stream.h b/client/scenes/stream.h index b501d713..f8d00a5b 100644 --- a/client/scenes/stream.h +++ b/client/scenes/stream.h @@ -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 &&); diff --git a/client/wivrn_client.cpp b/client/wivrn_client.cpp index e2e6ae00..376d7cb1 100644 --- a/client/wivrn_client.cpp +++ b/client/wivrn_client.cpp @@ -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(receive(10s)).message; + + auto msg3 = pin_check.step3(msg2); + send_control(from_headset::pin_check_3{msg3}); + + auto msg4 = std::get(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: { diff --git a/common/CMakeLists.txt b/common/CMakeLists.txt index 6c3a62a9..78255de8 100644 --- a/common/CMakeLists.txt +++ b/common/CMakeLists.txt @@ -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 diff --git a/common/wivrn_packets.h b/common/wivrn_packets.h index 601a0320..950d9f55 100644 --- a/common/wivrn_packets.h +++ b/common/wivrn_packets.h @@ -32,6 +32,7 @@ #include #include +#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; +using packets = std::variant; } // 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 enabled; }; -using packets = std::variant; +using packets = std::variant; } // namespace to_headset } // namespace wivrn diff --git a/common/wivrn_serialization.h b/common/wivrn_serialization.h index a85dfe7c..902fb60a 100644 --- a/common/wivrn_serialization.h +++ b/common/wivrn_serialization.h @@ -21,6 +21,7 @@ #include "boost/pfr/core.hpp" #include "boost/pfr/tuple_size.hpp" +#include "smp.h" #include #include #include @@ -903,6 +904,35 @@ struct serialization_traits } }; +template <> +struct serialization_traits +{ + static constexpr void type_hash(details::hash_context & h) + { + h.feed("bignum"); + } + + static void serialize(const crypto::bignum & value, serialization_packet & packet) + { + serialization_traits::serialize(value.to_data(), packet); + } + + static crypto::bignum deserialize(deserialization_packet & packet) + { + return crypto::bignum::from_data(serialization_traits::deserialize(packet)); + } + + static bool consteval is_trivially_serializable() + { + return false; + } + + static size_t size(const crypto::bignum & value) + { + return 2 + value.data_size(); + } +}; + template constexpr uint64_t serialization_type_hash(int revision) { diff --git a/server/driver/wivrn_connection.cpp b/server/driver/wivrn_connection.cpp index 1f989a78..ab59e059 100644 --- a/server/driver/wivrn_connection.cpp +++ b/server/driver/wivrn_connection.cpp @@ -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 @@ -198,6 +199,32 @@ void wivrn::wivrn_connection::init(std::stop_token stop_token, std::function(receive(10s).first).message; + + auto msg2 = pin_check.step2(msg1, pin); + control.send(to_headset::pin_check_2{msg2}); + + auto msg3 = std::get(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); diff --git a/server/driver/wivrn_session.h b/server/driver/wivrn_session.h index 810ebf07..7ad6dfc6 100644 --- a/server/driver/wivrn_session.h +++ b/server/driver/wivrn_session.h @@ -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 &&); diff --git a/tools/wireshark/dissector.cpp b/tools/wireshark/dissector.cpp index dad97b4a..1a1c1e05 100644 --- a/tools/wireshark/dissector.cpp +++ b/tools/wireshark/dissector.cpp @@ -22,16 +22,17 @@ #include #include #include - -#include -#include -#include - -#include "wivrn_packets.h" #include +#include #include #include +#include +#include + +#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 } }; +template +struct tree_traits +{ + 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");