summaryrefslogtreecommitdiff
path: root/src/protocol/internal/fqterm_ssh_packet.h
blob: 9766e87a4bb5d97f196b830d6cab17efe2758841 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
// SPDX-License-Identifier: GPL-2.0-or-later

#ifndef FQTERM_SSH_PACKET_H
#define FQTERM_SSH_PACKET_H

#include <openssl/bn.h>

#include <QObject>

#include "fqterm_ssh_types.h"
#include "ssh_mac.h"
#include "ssh_cipher.h"
#include "buffer.h"
#include "ssh_packet.h"

namespace FQTerm {

class FQTermSSHPacketSender: public QObject {
  Q_OBJECT;
 public:
  buffer orig_data; /* always unencrypted */
  buffer data_to_send;
  ssh_cipher_t *cipher;
  ssh_mac_t *mac;
  bool is_mac_;
  uint32_t sequence_no_;

  int ver;

  FQTermSSHPacketSender(int);
  virtual ~FQTermSSHPacketSender();

  void startPacket(uint8_t pkt_type);
  inline void putByte(uint8_t b) { buffer_append_byte(&orig_data, b); }
  inline void putInt(uint32_t x) { buffer_append_be32(&orig_data, x); }
  void putString(const char *string, int len = -1);
  inline void putRawData(const uint8_t *data, size_t len)
  { buffer_append(&orig_data, data, len); }
  void putBN(BIGNUM *bignum);
  void write();

  virtual int getIVSize() const { return cipher->IVSize;}
  virtual int getKeySize() const { return cipher->keySize;}
  int getMacKeySize() const { return mac->keySize;}

  void startEncryption(const u_char *key, const u_char *IV = NULL);
  void startMac(const u_char *sessionkey);
  void resetMac();
  inline void makePacket()
  {
	  if (ver == 2) {
		  make_ssh2_packet(&orig_data, &data_to_send, cipher,
				  mac, is_mac_, &sequence_no_);
	  } else {
		  make_ssh1_packet(&orig_data, &data_to_send, cipher);
	  }
  }
 public slots:
  void resetEncryption();

 signals:
  void dataToWrite();
};

class FQTermSSHPacketReceiver: public QObject {
  Q_OBJECT;
 public:
  buffer recvbuf;
  ssh_cipher_t *cipher;
  ssh_mac_t *mac;
  bool is_mac_;
  uint32_t sequence_no_;

  FQTermSSHPacketReceiver();
  virtual ~FQTermSSHPacketReceiver();

  int packetType()const {
    return packet_type_;
  }

  u_char getByte();
  u_int getInt();
  void *getString(int *length = NULL);
  void getRawData(char *data, int length);
  void getBN(BIGNUM *bignum);
  void consume(int len);

  virtual int packetDataLen() const { return real_data_len_;}
  virtual int getIVSize() const { return cipher->IVSize;}
  virtual int getKeySize() const { return cipher->keySize;}
  int getMacKeySize() const { return mac->keySize;}

  virtual void parseData(buffer *input) = 0;
  void startEncryption(const u_char *key, const u_char *IV = NULL);
  void startMac(const u_char *sessionkey);
  void resetMac();
 public slots:
  void resetEncryption();

 signals:
  void packetAvaliable(int type);
  void packetError(QString);

 protected:
  int packet_type_;
  int real_data_len_;
};

}  // namespace FQTerm

#endif  // FQTERM_SSH_PACKET