1 /*
2  *  UFTP - UDP based FTP with multicast
3  *
4  *  Copyright (C) 2001-2020   Dennis A. Bush, Jr.   bush@tcnj.edu
5  *
6  *  This program is free software: you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation, either version 3 of the License, or
9  *  (at your option) any later version.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  *
19  *  Additional permission under GNU GPL version 3 section 7
20  *
21  *  If you modify this program, or any covered work, by linking or
22  *  combining it with the OpenSSL project's OpenSSL library (or a
23  *  modified version of that library), containing parts covered by the
24  *  terms of the OpenSSL or SSLeay licenses, the copyright holder
25  *  grants you additional permission to convey the resulting work.
26  *  Corresponding Source for a non-source form of such a combination
27  *  shall include the source code for the parts of OpenSSL used as well
28  *  as that of the covered work.
29  */
30 
31 #ifndef _UFTP_COMMON_H
32 #define _UFTP_COMMON_H
33 
34 #include <stdio.h>
35 
36 #ifdef WINDOWS
37 
38 #include <winsock2.h>
39 #include <ws2tcpip.h>
40 
41 #else
42 
43 #include <netinet/in.h>
44 #include <sys/time.h>
45 
46 #endif
47 
48 #ifdef __DragonFly__
49 #include <sys/socket.h>
50 #endif
51 
52 #include "uftp.h"
53 #include "encryption.h"
54 
55 #define DEF_LOG_LEVEL 2
56 #define DEF_MAX_LOG_COUNT 5
57 
58 extern char logfile[MAXPATHNAME];
59 extern int showtime;
60 extern FILE *applog;
61 extern int log_level, init_log_mux, use_log_mux, max_log_count;
62 extern f_offset_t log_size, max_log_size;
63 extern mux_t log_mux;
64 
65 void init_log(int _debug);
66 void close_log(void);
67 void roll_log(void);
68 void logfunc(uint32_t group_id, uint8_t group_inst, uint16_t file_id,
69              int level, int _showtime, int newline, int err, int sockerr,
70              const char *str, ...);
71 
72 #define GRPLOG(group) (group)->group_id, (group)->group_inst, (group)->file_id
73 
74 #define clog0(group_id, group_inst, file_id, ...) \
75     logfunc(group_id, group_inst, file_id, 0, showtime, 0, 0, 0, __VA_ARGS__)
76 #define log0(group_id, group_inst, file_id, ...) \
77     logfunc(group_id, group_inst, file_id, 0, showtime, 1, 0, 0, __VA_ARGS__)
78 #define cglog0(group, ...) \
79     logfunc(GRPLOG(group), 0, showtime, 0, 0, 0, __VA_ARGS__)
80 #define glog0(group, ...) \
81     logfunc(GRPLOG(group), 0, showtime, 1, 0, 0, __VA_ARGS__)
82 #define sclog0(...) \
83     logfunc(0, 0, 0, 0, 0, 0, 0, 0, __VA_ARGS__)
84 #define slog0(...) \
85     logfunc(0, 0, 0, 0, 0, 1, 0, 0, __VA_ARGS__)
86 
87 #define clog1(group_id, group_inst, file_id, ...) \
88     logfunc(group_id, group_inst, file_id, 1, showtime, 0, 0, 0, __VA_ARGS__)
89 #define log1(group_id, group_inst, file_id, ...) \
90     logfunc(group_id, group_inst, file_id, 1, showtime, 1, 0, 0, __VA_ARGS__)
91 #define cglog1(group, ...) \
92     logfunc(GRPLOG(group), 1, showtime, 0, 0, 0, __VA_ARGS__)
93 #define glog1(group, ...) \
94     logfunc(GRPLOG(group), 1, showtime, 1, 0, 0, __VA_ARGS__)
95 #define sclog1(...) \
96     logfunc(0, 0, 0, 1, 0, 0, 0, 0, __VA_ARGS__)
97 #define slog1(...) \
98     logfunc(0, 0, 0, 1, 0, 1, 0, 0, __VA_ARGS__)
99 
100 #define clog2(group_id, group_inst, file_id, ...) \
101     logfunc(group_id, group_inst, file_id, 2, showtime, 0, 0, 0, __VA_ARGS__)
102 #define log2(group_id, group_inst, file_id, ...) \
103     logfunc(group_id, group_inst, file_id, 2, showtime, 1, 0, 0, __VA_ARGS__)
104 #define cglog2(group, ...) \
105     logfunc(GRPLOG(group), 2, showtime, 0, 0, 0, __VA_ARGS__)
106 #define glog2(group, ...) \
107     logfunc(GRPLOG(group), 2, showtime, 1, 0, 0, __VA_ARGS__)
108 #define sclog2(...) \
109     logfunc(0, 0, 0, 2, 0, 0, 0, 0, __VA_ARGS__)
110 #define slog2(...) \
111     logfunc(0, 0, 0, 2, 0, 1, 0, 0, __VA_ARGS__)
112 
113 #define clog3(group_id, group_inst, file_id, ...) \
114     logfunc(group_id, group_inst, file_id, 3, showtime, 0, 0, 0, __VA_ARGS__)
115 #define log3(group_id, group_inst, file_id, ...) \
116     logfunc(group_id, group_inst, file_id, 3, showtime, 1, 0, 0, __VA_ARGS__)
117 #define cglog3(group, ...) \
118     logfunc(GRPLOG(group), 3, showtime, 0, 0, 0, __VA_ARGS__)
119 #define glog3(group, ...) \
120     logfunc(GRPLOG(group), 3, showtime, 1, 0, 0, __VA_ARGS__)
121 #define sclog3(...) \
122     logfunc(0, 0, 0, 3, 0, 0, 0, 0, __VA_ARGS__)
123 #define slog3(...) \
124     logfunc(0, 0, 0, 3, 0, 1, 0, 0, __VA_ARGS__)
125 
126 #define clog4(group_id, group_inst, file_id, ...) \
127     logfunc(group_id, group_inst, file_id, 4, showtime, 0, 0, 0, __VA_ARGS__)
128 #define log4(group_id, group_inst, file_id, ...) \
129     logfunc(group_id, group_inst, file_id, 4, showtime, 1, 0, 0, __VA_ARGS__)
130 #define cglog4(group, ...) \
131     logfunc(GRPLOG(group), 4, showtime, 0, 0, 0, __VA_ARGS__)
132 #define glog4(group, ...) \
133     logfunc(GRPLOG(group), 4, showtime, 1, 0, 0, __VA_ARGS__)
134 #define sclog4(...) \
135     logfunc(0, 0, 0, 4, 0, 0, 0, 0, __VA_ARGS__)
136 #define slog4(...) \
137     logfunc(0, 0, 0, 4, 0, 1, 0, 0, __VA_ARGS__)
138 
139 #define clog5(group_id, group_inst, file_id, ...) \
140     logfunc(group_id, group_inst, file_id, 5, showtime, 0, 0, 0, __VA_ARGS__)
141 #define log5(group_id, group_inst, file_id, ...) \
142     logfunc(group_id, group_inst, file_id, 5, showtime, 1, 0, 0, __VA_ARGS__)
143 #define cglog5(group, ...) \
144     logfunc(GRPLOG(group), 5, showtime, 0, 0, 0, __VA_ARGS__)
145 #define glog5(group, ...) \
146     logfunc(GRPLOG(group), 5, showtime, 1, 0, 0, __VA_ARGS__)
147 #define sclog5(...) \
148     logfunc(0, 0, 0, 5, 0, 0, 0, 0, __VA_ARGS__)
149 #define slog5(...) \
150     logfunc(0, 0, 0, 5, 0, 1, 0, 0, __VA_ARGS__)
151 
152 #define syserror(group_id, group_inst, file_id, ...) \
153     logfunc(group_id,group_inst, file_id, 0, showtime, 1, errno, 0, __VA_ARGS__)
154 #define sockerror(group_id, group_inst, file_id, ...) \
155     logfunc(group_id,group_inst, file_id, 0, showtime, 1, errno, 1, __VA_ARGS__)
156 #define gsyserror(group, ...) \
157     logfunc(GRPLOG(group), 0, showtime, 1, errno, 0, __VA_ARGS__)
158 #define gsockerror(group, ...) \
159     logfunc(GRPLOG(group), 0, showtime, 1, errno, 1, __VA_ARGS__)
160 
161 union sockaddr_u {
162     struct sockaddr_storage ss;
163     struct sockaddr_in sin;
164     struct sockaddr_in6 sin6;
165 };
166 
167 struct iflist {
168     char name[IFNAME_LEN];
169     union sockaddr_u su;
170     int isloopback;
171     int ismulti;
172     int ifidx;
173 };
174 
175 const char *func_name(int func);
176 const char *curve_name(int curve);
177 uint8_t get_curve(const char *name);
178 struct timeval usec_to_tv(int64_t t);
179 int64_t tv_to_usec(struct timeval tv);
180 int32_t diff_sec(struct timeval t2, struct timeval t1);
181 int64_t diff_usec(struct timeval t2, struct timeval t1);
182 int cmptimestamp(struct timeval t1, struct timeval t2);
183 struct timeval add_timeval(struct timeval t2, struct timeval t1);
184 void add_timeval_d(struct timeval *t2, double t1);
185 struct timeval diff_timeval(struct timeval t2, struct timeval t1);
186 void getiflist(struct iflist *list, int *len);
187 void split_path(const char *path, char **dir, char **file);
188 int parse_fingerprint(unsigned char *fingerprint, const char *fingerprint_str);
189 int is_multicast(const union sockaddr_u *addr, int ssm);
190 int addr_equal(const union sockaddr_u *addr1, const union sockaddr_u *addr2);
191 int addr_blank(const union sockaddr_u *addr);
192 uint64_t uftp_htonll(uint64_t val);
193 uint64_t uftp_ntohll(uint64_t val);
194 
195 int family_len(union sockaddr_u addr);
196 int would_block_err(void);
197 int nb_sendto(SOCKET s, const void *msg, int len, int flags,
198               const struct sockaddr *to, int tolen);
199 int read_packet(SOCKET sock, union sockaddr_u *sa, unsigned char *buffer,
200                 int *len, int bsize, const struct timeval *timeout,
201                 uint8_t *tos);
202 void build_iv4(uint8_t *iv, const uint8_t *salt, int ivlen, uint64_t ivctr,
203                uint32_t src_id);
204 void build_iv(uint8_t *iv, const uint8_t *salt, int ivlen, uint64_t ivctr);
205 void printhex(const char *name, const unsigned char *data, int len);
206 int is_auth_enc(int keytype);
207 int is_gcm_mode(int keytype);
208 int is_ccm_mode(int keytype);
209 int unauth_key(int keytype);
210 int encrypt_and_sign(const unsigned char *decpacket, unsigned char **encpacket,
211                      int declen, int *enclen, int keytype, uint8_t *key,
212                      const uint8_t *salt, uint64_t *ivctr, int ivlen);
213 int validate_and_decrypt(unsigned char *encpacket, unsigned int enclen,
214                          unsigned char **decpacket, unsigned int *declen,
215                          int keytype, const uint8_t *key,
216                          const uint8_t *salt, int ivlen);
217 void PRF(int hashtype, int bytes, const unsigned char *secret, int secret_len,
218          const char *label, const unsigned char *seed, int seed_len,
219          unsigned char *outbuf, int *outbuf_len);
220 void create_server_context(uint32_t group_id, uint8_t group_inst,
221                            uint32_t server_id,const struct enc_info_he *encinfo,
222                            int extlen, uint8_t **context, int *contextlen);
223 void create_proxy_context(uint32_t proxy_id, const struct proxy_key_h *proxykey,
224                           uint8_t **context, int *context_len);
225 void create_client_context_1(const uint8_t *s_context, int s_context_len,
226                              const uint8_t *p_context, int p_context_len,
227                              uint32_t client_id, const uint8_t *client_dh,
228                              int client_dh_len, const uint8_t *client_rand,
229                              uint8_t **context, int *context_len);
230 void create_client_context_2(const uint8_t *c_context1, int c_context1_len,
231                              const struct client_key_h *ckheader,int header_len,
232                              uint8_t **context, int *context_len);
233 void HKDF_Extract(int hashtype,
234                   const unsigned char *salt, unsigned int salt_len,
235                   const unsigned char *secret, unsigned int secret_len,
236                   unsigned char *outbuf, unsigned int *outbuf_len);
237 void HKDF_Expand(int hashtype, unsigned int bytes,
238                  const unsigned char *secret, unsigned int secret_len,
239                  const unsigned char *info, unsigned int info_len,
240                  unsigned char *outbuf, unsigned int *outbuf_len);
241 void HKDF_Expand_Label(int hashtype, unsigned int bytes, const char *label,
242                        const unsigned char *secret, unsigned int secret_len,
243                        const unsigned char *context, unsigned int context_len,
244                        unsigned char *outbuf, unsigned int *outbuf_len);
245 void calculate_hs_keys(int hashtype, uint8_t *premaster, int premaster_len,
246                        uint8_t *client_context1,
247                        unsigned int client_context1_len,
248                        unsigned int key_len, unsigned int iv_len,
249                        uint8_t *server_hs_key, uint8_t *server_hs_iv,
250                        uint8_t *client_hs_key, uint8_t *client_hs_iv);
251 void calculate_server_app_keys(int hashtype, uint8_t *groupmaster,
252                                int groupmaster_len, uint8_t *server_context,
253                                unsigned int server_context_len,
254                                unsigned int key_len, unsigned int iv_len,
255                                uint8_t *server_app_key, uint8_t *server_app_iv);
256 void calculate_client_app_keys(int hashtype, uint8_t *groupmaster,
257                                int groupmaster_len, uint8_t *client_context2,
258                                unsigned int client_context2_len,
259                                unsigned int key_len, unsigned int iv_len,
260                                uint8_t *client_app_key, uint8_t *client_app_iv,
261                                uint8_t *finished_key, uint8_t *verify_data);
262 const char *print_key_fingerprint(const union key_t key, int keytype);
263 
264 /**
265  * Key fingerprint for an allowed server or client
266  */
267 struct fp_list_t {
268     uint32_t uid;
269     union sockaddr_u addr;
270     uint32_t proxy_uid;
271     int has_fingerprint;
272     uint8_t fingerprint[HMAC_LEN];
273 };
274 
275 struct fp_list_t *fp_lookup(uint32_t id, struct fp_list_t* list, int count);
276 int multicast_join(SOCKET s, uint32_t group_id, const union sockaddr_u *multi,
277                    const struct iflist *addrlist, int addrlen,
278                    const struct fp_list_t *fplist, int fplist_len);
279 void multicast_leave(SOCKET s, uint32_t group_id, const union sockaddr_u *multi,
280                      const struct iflist *addrlist, int addrlen,
281                      const struct fp_list_t *fplist, int fplist_len);
282 
283 int getifbyname(const char *name, const struct iflist *list, int len);
284 int getifbyaddr(union sockaddr_u *su, const struct iflist *list, int len);
285 
286 int file_read(int fd, void *buf, int buflen, int allow_eof);
287 int file_write(int fd, const void *buf, int buflen);
288 uint64_t free_space(const char *dir);
289 
290 int valid_priority(int priority);
291 uint32_t rand32(void);
292 void *safe_malloc(size_t size);
293 void *safe_calloc(size_t num, size_t size);
294 
295 uint8_t quantize_grtt(double rtt);
296 double unquantize_grtt(uint8_t rtt);
297 uint8_t quantize_gsize(int size);
298 int unquantize_gsize(uint8_t size);
299 uint16_t quantize_rate(int64_t size);
300 int64_t unquantize_rate(uint16_t size);
301 
302 #endif  // _UFTP_COMMON_H
303 
304