File indexing completed on 2025-01-05 04:37:21

0001 /*
0002     SPDX-FileCopyrightText: 2005 Joris Guisson <joris.guisson@gmail.com>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 #ifndef BTAUTHENTICATEBASE_H
0007 #define BTAUTHENTICATEBASE_H
0008 
0009 #include <mse/encryptedpacketsocket.h>
0010 #include <qobject.h>
0011 #include <qtimer.h>
0012 #include <util/constants.h>
0013 
0014 namespace bt
0015 {
0016 class SHA1Hash;
0017 class PeerID;
0018 
0019 /**
0020  * @author Joris Guisson
0021  *
0022  * Base class for authentication classes. This class just groups
0023  * some common stuff between Authenticate and ServerAuthentciate.
0024  * It has a socket, handles the timing out, provides a function to send
0025  * the handshake.
0026  */
0027 class AuthenticateBase : public QObject
0028 {
0029     Q_OBJECT
0030 public:
0031     AuthenticateBase();
0032     AuthenticateBase(mse::EncryptedPacketSocket::Ptr s);
0033     ~AuthenticateBase() override;
0034 
0035     /// Set whether this is a local peer
0036     void setLocal(bool loc)
0037     {
0038         local = loc;
0039     }
0040 
0041     /// Is this a local peer
0042     bool isLocal() const
0043     {
0044         return local;
0045     }
0046 
0047     /// See if the authentication is finished
0048     bool isFinished() const
0049     {
0050         return finished;
0051     }
0052 
0053     /// Flags indicating which extensions are supported
0054     Uint32 supportedExtensions() const
0055     {
0056         return ext_support;
0057     }
0058 
0059     /// get the socket
0060     mse::EncryptedPacketSocket::Ptr getSocket() const
0061     {
0062         return sock;
0063     }
0064 
0065     /// We can read from the socket
0066     virtual void onReadyRead();
0067 
0068     /// We can write to the socket (used to detect a succesfull connection)
0069     virtual void onReadyWrite();
0070 
0071 protected:
0072     /**
0073      * Send a handshake
0074      * @param info_hash The info_hash to include
0075      * @param our_peer_id Our PeerID
0076      */
0077     void sendHandshake(const SHA1Hash &info_hash, const PeerID &our_peer_id);
0078 
0079     /**
0080      * Authentication finished.
0081      * @param succes Succes or not
0082      */
0083     virtual void onFinish(bool succes) = 0;
0084 
0085     /**
0086      * The other side send a handshake. The first 20 bytes
0087      * of the handshake will already have been checked.
0088      * @param full Indicates whether we have a full handshake
0089      *  if this is not full, we should just send our own
0090      */
0091     virtual void handshakeReceived(bool full) = 0;
0092 
0093     /**
0094      * Fill in the handshake in a buffer.
0095      */
0096     void makeHandshake(bt::Uint8 *buf, const SHA1Hash &info_hash, const PeerID &our_peer_id);
0097 
0098 protected Q_SLOTS:
0099     void onTimeout();
0100     void onError(int err);
0101 
0102 protected:
0103     mse::EncryptedPacketSocket::Ptr sock;
0104     QTimer timer;
0105     bool finished;
0106     Uint8 handshake[68];
0107     Uint32 bytes_of_handshake_received;
0108     Uint32 ext_support;
0109     bool local;
0110 };
0111 
0112 }
0113 
0114 #endif