2
0

Session.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. #include "Session.h"
  2. #include <limits.h> // for CHAR_BIT
  3. #include <cstdint> // for uint8_t
  4. #include <functional> // for __base
  5. #include <memory> // for shared_ptr, unique_ptr, make_unique
  6. #include <random> // for default_random_engine, independent_bi...
  7. #include <type_traits> // for remove_extent_t
  8. #include <utility> // for move
  9. #include "ApResolve.h" // for ApResolve, cspot
  10. #include "AuthChallenges.h" // for AuthChallenges
  11. #include "BellLogger.h" // for AbstractLogger
  12. #include "Logger.h" // for CSPOT_LOG
  13. #include "LoginBlob.h" // for LoginBlob
  14. #include "Packet.h" // for Packet
  15. #include "PlainConnection.h" // for PlainConnection, timeoutCallback
  16. #include "ShannonConnection.h" // for ShannonConnection
  17. using random_bytes_engine =
  18. std::independent_bits_engine<std::default_random_engine, CHAR_BIT, uint8_t>;
  19. using namespace cspot;
  20. Session::Session() {
  21. this->challenges = std::make_unique<cspot::AuthChallenges>();
  22. }
  23. Session::~Session() {}
  24. void Session::connect(std::unique_ptr<cspot::PlainConnection> connection) {
  25. this->conn = std::move(connection);
  26. conn->timeoutHandler = [this]() {
  27. return this->triggerTimeout();
  28. };
  29. auto helloPacket = this->conn->sendPrefixPacket(
  30. {0x00, 0x04}, this->challenges->prepareClientHello());
  31. auto apResponse = this->conn->recvPacket();
  32. CSPOT_LOG(info, "Received APHello response");
  33. auto solvedHello = this->challenges->solveApHello(helloPacket, apResponse);
  34. conn->sendPrefixPacket({}, solvedHello);
  35. CSPOT_LOG(debug, "Received shannon keys");
  36. // Generates the public and priv key
  37. this->shanConn = std::make_shared<ShannonConnection>();
  38. // Init shanno-encrypted connection
  39. this->shanConn->wrapConnection(this->conn, challenges->shanSendKey,
  40. challenges->shanRecvKey);
  41. }
  42. void Session::connectWithRandomAp() {
  43. auto apResolver = std::make_unique<ApResolve>("");
  44. auto conn = std::make_unique<cspot::PlainConnection>();
  45. conn->timeoutHandler = [this]() {
  46. return this->triggerTimeout();
  47. };
  48. auto apAddr = apResolver->fetchFirstApAddress();
  49. CSPOT_LOG(debug, "Connecting with AP <%s>", apAddr.c_str());
  50. conn->connect(apAddr);
  51. this->connect(std::move(conn));
  52. }
  53. std::vector<uint8_t> Session::authenticate(std::shared_ptr<LoginBlob> blob) {
  54. // save auth blob for reconnection purposes
  55. authBlob = blob;
  56. // prepare authentication request proto
  57. auto data = challenges->prepareAuthPacket(blob->authData, blob->authType,
  58. deviceId, blob->username);
  59. // Send login request
  60. this->shanConn->sendPacket(LOGIN_REQUEST_COMMAND, data);
  61. auto packet = this->shanConn->recvPacket();
  62. switch (packet.command) {
  63. case AUTH_SUCCESSFUL_COMMAND: {
  64. CSPOT_LOG(debug, "Authorization successful");
  65. return std::vector<uint8_t>(
  66. {0x1}); // TODO: return actual reusable credentaials to be stored somewhere
  67. break;
  68. }
  69. case AUTH_DECLINED_COMMAND: {
  70. CSPOT_LOG(error, "Authorization declined");
  71. break;
  72. }
  73. default:
  74. CSPOT_LOG(error, "Unknown auth fail code %d", packet.command);
  75. }
  76. return std::vector<uint8_t>(0);
  77. }
  78. void Session::close() {
  79. this->conn->close();
  80. }