| 1 | /* |
| 2 | * ProFTPD - mod_sftp packet IO |
| 3 | * Copyright (c) 2008-2023 TJ Saunders |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; either version 2 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software |
| 17 | * Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA. |
| 18 | * |
| 19 | * As a special exemption, TJ Saunders and other respective copyright holders |
| 20 | * give permission to link this program with OpenSSL, and distribute the |
| 21 | * resulting executable, without including the source code for OpenSSL in the |
| 22 | * source distribution. |
| 23 | */ |
| 24 | |
| 25 | #ifndef MOD_SFTP_PACKET_H |
| 26 | #define MOD_SFTP_PACKET_H |
| 27 | |
| 28 | #include "mod_sftp.h" |
| 29 | |
| 30 | /* From RFC 4253, Section 6 */ |
| 31 | struct ssh2_packet { |
| 32 | pool *pool; |
| 33 | |
| 34 | /* Module that created this packet. */ |
| 35 | module *m; |
| 36 | |
| 37 | /* Length of the packet, not including mac or packet_len field itself. */ |
| 38 | uint32_t packet_len; |
| 39 | |
| 40 | /* Length of the padding field. */ |
| 41 | unsigned char padding_len; |
| 42 | |
| 43 | unsigned char *payload; |
| 44 | uint32_t payload_len; |
| 45 | |
| 46 | /* Must be at least 4 bytes of padding, with a maximum of 255 bytes. */ |
| 47 | unsigned char *padding; |
| 48 | |
| 49 | /* Additional Authenticated Data (AAD). */ |
| 50 | unsigned char *aad; |
| 51 | uint32_t aad_len; |
| 52 | |
| 53 | /* Message Authentication Code. */ |
| 54 | unsigned char *mac; |
| 55 | uint32_t mac_len; |
| 56 | |
| 57 | /* Packet sequence number. */ |
| 58 | uint32_t seqno; |
| 59 | }; |
| 60 | |
| 61 | #define SFTP_MIN_PADDING_LEN 4 |
| 62 | #define SFTP_MAX_PADDING_LEN 255 |
| 63 | |
| 64 | /* From the SFTP Draft, Section 4. */ |
| 65 | struct sftp_packet { |
| 66 | uint32_t packet_len; |
| 67 | unsigned char packet_type; |
| 68 | uint32_t request_id; |
| 69 | }; |
| 70 | |
| 71 | struct ssh2_packet *sftp_ssh2_packet_create(pool *); |
| 72 | char sftp_ssh2_packet_get_msg_type(struct ssh2_packet *); |
| 73 | const char *sftp_ssh2_packet_get_msg_type_desc(unsigned char); |
| 74 | |
| 75 | /* Returns a struct timeval populated with the time we last received an SSH2 |
| 76 | * packet from the client. |
| 77 | */ |
| 78 | int sftp_ssh2_packet_get_last_recvd(time_t *); |
| 79 | |
| 80 | /* Returns a struct timeval populated with the time we last sent an SSH2 |
| 81 | * packet from the client. |
| 82 | */ |
| 83 | int sftp_ssh2_packet_get_last_sent(time_t *); |
| 84 | |
| 85 | int sftp_ssh2_packet_read(int, struct ssh2_packet *); |
| 86 | int sftp_ssh2_packet_sock_read(int, void *, size_t, int); |
| 87 | |
| 88 | /* This sftp_ssh2_packet_sock_read() flag is used to tell the function to |
| 89 | * read in as many of the requested length of data as it can, but to NOT |
| 90 | * keep polling until that length has been acquired (i.e. to read the |
| 91 | * requested length pessimistically, assuming that it will not all appear). |
| 92 | */ |
| 93 | #define SFTP_PACKET_READ_FL_PESSIMISTIC 0x001 |
| 94 | |
| 95 | int sftp_ssh2_packet_send(int, struct ssh2_packet *); |
| 96 | |
| 97 | /* Wrapper function around sftp_ssh2_packet_send() which handles the sending |
| 98 | * of TAP messages and buffering of messages for network efficiency. |
| 99 | */ |
| 100 | int sftp_ssh2_packet_write(int, struct ssh2_packet *); |
| 101 | |
| 102 | /* This function reads in an SSH2 packet from the socket, and dispatches |
| 103 | * the packet to various handlers. |
| 104 | */ |
| 105 | int sftp_ssh2_packet_process(pool *p); |
| 106 | |
| 107 | /* These specialized functions are for handling the additional message types |
| 108 | * defined in RFC 4253, Section 11, e.g. during KEX. |
| 109 | */ |
| 110 | void sftp_ssh2_packet_handle_debug(struct ssh2_packet *); |
| 111 | void sftp_ssh2_packet_handle_disconnect(struct ssh2_packet *); |
| 112 | void sftp_ssh2_packet_handle_ext_info(struct ssh2_packet *); |
| 113 | void sftp_ssh2_packet_handle_ignore(struct ssh2_packet *); |
| 114 | void sftp_ssh2_packet_handle_unimplemented(struct ssh2_packet *); |
| 115 | |
| 116 | int sftp_ssh2_packet_rekey_reset(void); |
| 117 | int sftp_ssh2_packet_rekey_set_seqno(uint32_t); |
| 118 | int sftp_ssh2_packet_rekey_set_size(off_t); |
| 119 | |
| 120 | /* These are used for implementing the "strict KEX" mitigations of the Terrapin |
| 121 | * attack (Issue 1760). |
| 122 | */ |
| 123 | uint32_t sftp_ssh2_packet_get_client_seqno(void); |
| 124 | void sftp_ssh2_packet_reset_client_seqno(void); |
| 125 | void sftp_ssh2_packet_reset_server_seqno(void); |
| 126 | |
| 127 | int sftp_ssh2_packet_send_version(void); |
| 128 | int sftp_ssh2_packet_set_poll_timeout(int); |
| 129 | int sftp_ssh2_packet_set_version(const char *); |
| 130 | |
| 131 | int sftp_ssh2_packet_set_client_alive(unsigned int, unsigned int); |
| 132 | void sftp_ssh2_packet_set_handler(int (*handler)(void *)); |
| 133 | |
| 134 | #endif /* MOD_SFTP_PACKET_H */ |