File: tls.c

package info (click to toggle)
proftpd-mod-proxy 0.9.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,476 kB
  • sloc: ansic: 43,512; perl: 43,487; sh: 3,479; makefile: 248
file content (342 lines) | stat: -rw-r--r-- 10,251 bytes parent folder | download | duplicates (2)
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
/*
 * ProFTPD - mod_proxy testsuite
 * Copyright (c) 2015-2022 TJ Saunders <tj@castaglia.org>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA.
 *
 * As a special exemption, TJ Saunders and other respective copyright holders
 * give permission to link this program with OpenSSL, and distribute the
 * resulting executable, without including the source code for OpenSSL in the
 * source distribution.
 */

/* Proxy TLS API tests. */

#include "tests.h"

extern xaset_t *server_list;

static pool *p = NULL;
static const char *test_dir = "/tmp/mod_proxy-test-tls";

static void create_main_server(void) {
  pool *main_pool;

  main_pool = make_sub_pool(permanent_pool);
  pr_pool_tag(main_pool, "testsuite#main_server pool");

  server_list = xaset_create(main_pool, NULL);

  main_server = (server_rec *) pcalloc(main_pool, sizeof(server_rec));
  xaset_insert(server_list, (xasetmember_t *) main_server);

  main_server->pool = main_pool;
  main_server->conf = xaset_create(main_pool, NULL);
  main_server->set = server_list;
  main_server->sid = 1;
  main_server->notes = pr_table_nalloc(main_pool, 0, 8);

  /* TCP KeepAlive is enabled by default, with the system defaults. */
  main_server->tcp_keepalive = palloc(main_server->pool,
    sizeof(struct tcp_keepalive));
  main_server->tcp_keepalive->keepalive_enabled = TRUE;
  main_server->tcp_keepalive->keepalive_idle = -1;
  main_server->tcp_keepalive->keepalive_count = -1;
  main_server->tcp_keepalive->keepalive_intvl = -1;

  main_server->ServerName = "Test Server";
  main_server->ServerPort = 21;
}

static int create_test_dir(void) {
  int res;
  mode_t perms;

  perms = 0770;
  res = mkdir(test_dir, perms);
  ck_assert_msg(res == 0, "Failed to create tmp directory '%s': %s", test_dir,
    strerror(errno));

  res = chmod(test_dir, perms);
  ck_assert_msg(res == 0, "Failed to set perms %04o on directory '%s': %s",
    perms, test_dir, strerror(errno));

  return 0;
}

static void set_up(void) {
  if (p == NULL) {
    p = permanent_pool = proxy_pool = make_sub_pool(NULL);
    server_list = NULL;
    main_server = NULL;
    session.c = NULL;
    session.notes = NULL;
  }

  (void) tests_rmpath(p, test_dir);
  create_main_server();
  (void) create_test_dir();
  init_netio();
  proxy_db_init(p);

  if (getenv("TEST_VERBOSE") != NULL) {
    pr_trace_set_levels("proxy.db", 1, 20);
    pr_trace_set_levels("proxy.tls", 1, 20);
  }
}

static void tear_down(void) {
  if (getenv("TEST_VERBOSE") != NULL) {
    pr_trace_set_levels("proxy.db", 0, 0);
    pr_trace_set_levels("proxy.tls", 0, 0);
  }

  proxy_db_free();
  (void) tests_rmpath(p, test_dir);

  if (p) {
    destroy_pool(p);
    p = permanent_pool = proxy_pool = NULL;
    server_list = NULL;
    main_server = NULL;
    session.c = NULL;
    session.notes = NULL;
  }
}

START_TEST (tls_free_test) {
  int res;

  res = proxy_tls_free(NULL);
  ck_assert_msg(res < 0, "Failed to handle null pool");
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got '%s' (%d)", EINVAL,
    strerror(errno), errno);

  res = proxy_tls_free(p);
  ck_assert_msg(res == 0, "Failed to free TLS API resources: %s",
    strerror(errno));
}
END_TEST

START_TEST (tls_init_test) {
  int res, flags = PROXY_DB_OPEN_FL_SKIP_VACUUM;

  res = proxy_tls_init(NULL, NULL, flags);
#if defined(PR_USE_OPENSSL)
  ck_assert_msg(res < 0, "Failed to handle null pool");
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got '%s' (%d)", EINVAL,
    strerror(errno), errno);

  res = proxy_tls_init(p, NULL, flags);
  ck_assert_msg(res < 0, "Failed to handle null tables directory");
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got '%s' (%d)", EINVAL,
    strerror(errno), errno);

  mark_point();
  res = proxy_tls_init(p, test_dir, flags);
  ck_assert_msg(res == 0, "Failed to init TLS API resources: %s",
    strerror(errno));

  res = proxy_tls_free(p);
  ck_assert_msg(res == 0, "Failed to free TLS API resources: %s",
    strerror(errno));
#else
  ck_assert_msg(res == 0, "Failed to init TLS API resources: %s",
    strerror(errno));
#endif /* PR_USE_OPENSSL */
}
END_TEST

START_TEST (tls_sess_free_test) {
  int res;

  res = proxy_tls_sess_free(NULL);
  ck_assert_msg(res < 0, "Failed to handle null pool");
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got '%s' (%d)", EINVAL,
    strerror(errno), errno);

  mark_point();
  res = proxy_tls_init(p, test_dir, PROXY_DB_OPEN_FL_SKIP_VACUUM);
  ck_assert_msg(res == 0, "Failed to init TLS API resources: %s",
    strerror(errno));

  mark_point();
  res = proxy_tls_sess_free(p);
  ck_assert_msg(res == 0, "Failed to release TLS API session resources: %s",
    strerror(errno));

  res = proxy_tls_free(p);
  ck_assert_msg(res == 0, "Failed to free TLS API resources: %s",
    strerror(errno));
}
END_TEST

START_TEST (tls_sess_init_test) {
#if defined(PR_USE_OPENSSL)
  int res, flags = PROXY_DB_OPEN_FL_SKIP_VACUUM;
  struct proxy_session *proxy_sess;

  mark_point();
  res = proxy_tls_sess_init(NULL, NULL, flags);
  ck_assert_msg(res < 0, "Failed to handle null pool");
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got '%s' (%d)", EINVAL,
    strerror(errno), errno);

  mark_point();
  res = proxy_tls_sess_init(p, NULL, flags);
  ck_assert_msg(res < 0, "Failed to handle null proxy_session");
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got '%s' (%d)", EINVAL,
    strerror(errno), errno);

  proxy_sess = (struct proxy_session *) proxy_session_alloc(p);
  ck_assert_msg(proxy_sess != NULL, "Failed to allocate proxy session: %s",
    strerror(errno));

  mark_point();
  res = proxy_tls_sess_init(p, proxy_sess, flags);
  ck_assert_msg(res < 0, "Failed to handle invalid SSL_CTX");
  ck_assert_msg(errno == EPERM, "Expected EPERM (%d), got '%s' (%d)", EPERM,
    strerror(errno), errno);

  mark_point();
  res = proxy_tls_init(p, test_dir, flags);
  ck_assert_msg(res == 0, "Failed to init TLS API resources: %s",
    strerror(errno));
  (void) proxy_db_close(p, NULL);

  mark_point();
  res = proxy_tls_sess_init(p, proxy_sess, flags);
  ck_assert_msg(res == 0, "Failed to init TLS API session resources: %s",
    strerror(errno));

  mark_point();
  res = proxy_tls_sess_free(p);
  ck_assert_msg(res == 0, "Failed to release TLS API session resources: %s",
    strerror(errno));

  mark_point();
  res = proxy_tls_free(p);
  ck_assert_msg(res == 0, "Failed to release TLS API resources: %s",
    strerror(errno));

  mark_point();
  proxy_session_free(p, proxy_sess);
#endif /* PR_USE_OPENSSL */
}
END_TEST

START_TEST (tls_using_tls_test) {
  int res, tls;

  tls = proxy_tls_using_tls();
#if defined(PR_USE_OPENSSL)
  ck_assert_msg(tls == PROXY_TLS_ENGINE_AUTO, "Expected TLS auto, got %d", tls);
#else
  ck_assert_msg(tls == PROXY_TLS_ENGINE_OFF, "Expected TLS off, got %d", tls);
#endif /* PR_USE_OPENSSL */

  res = proxy_tls_set_tls(7);
#if defined(PR_USE_OPENSSL)
  ck_assert_msg(res < 0, "Set TLS unexpectedly");
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got '%s' (%d)", EINVAL,
    strerror(errno), errno);

  res = proxy_tls_set_tls(PROXY_TLS_ENGINE_ON);
  tls = proxy_tls_using_tls();
  ck_assert_msg(tls == PROXY_TLS_ENGINE_ON, "Expected TLS on, got %d", tls);

  res = proxy_tls_set_tls(PROXY_TLS_ENGINE_OFF);
  tls = proxy_tls_using_tls();
  ck_assert_msg(tls == PROXY_TLS_ENGINE_OFF, "Expected TLS off, got %d", tls);

  res = proxy_tls_set_tls(PROXY_TLS_ENGINE_AUTO);
  tls = proxy_tls_using_tls();
  ck_assert_msg(tls == PROXY_TLS_ENGINE_AUTO, "Expected TLS auto, got %d", tls);

  res = proxy_tls_set_tls(PROXY_TLS_ENGINE_IMPLICIT);
  tls = proxy_tls_using_tls();
  ck_assert_msg(tls == PROXY_TLS_ENGINE_IMPLICIT, "Expected TLS implicit, got %d", tls);
#endif /* PR_USE_OPENSSL */
}
END_TEST

START_TEST (tls_match_client_tls_test) {
  int res;

  /* Plain FTP */
  mark_point();
  res = proxy_tls_match_client_tls();
  ck_assert_msg(res == 0, "Failed to match plain FTP client: %s",
    strerror(errno));

  /* Explicit FTPS */
  mark_point();
  session.rfc2228_mech = "TLS";
  res = proxy_tls_match_client_tls();
  ck_assert_msg(res == 0, "Failed to match explicit FTPS client: %s",
    strerror(errno));

  /* Implicit FTPS */
  session.rfc2228_mech = NULL;

  /* TODO: Add implicit FTPS check; requires setting TLSOptions config_rec,
   * which pulls in need for server_rec, parser, etc.
   */
}
END_TEST

START_TEST (tls_set_data_prot_test) {
  int res;

  res = proxy_tls_set_data_prot(TRUE);
#if defined(PR_USE_OPENSSL)
  ck_assert_msg(res == TRUE, "Expected TRUE, got %d", res);

  res = proxy_tls_set_data_prot(FALSE);
  ck_assert_msg(res == TRUE, "Expected TRUE, got %d", res);
#else
  ck_assert_msg(res == FALSE, "Expected FALSE, got %d", res);

  res = proxy_tls_set_data_prot(FALSE);
  ck_assert_msg(res == FALSE, "Expected FALSE, got %d", res);
#endif /* PR_USE_OPENSSL */

  res = proxy_tls_set_data_prot(FALSE);
  ck_assert_msg(res == FALSE, "Expected FALSE, got %d", res);

  (void) proxy_tls_set_data_prot(TRUE);
}
END_TEST

Suite *tests_get_tls_suite(void) {
  Suite *suite;
  TCase *testcase;

  suite = suite_create("tls");
  testcase = tcase_create("base");

  tcase_add_checked_fixture(testcase, set_up, tear_down);

  tcase_add_test(testcase, tls_free_test);
  tcase_add_test(testcase, tls_init_test);
  tcase_add_test(testcase, tls_sess_free_test);
  tcase_add_test(testcase, tls_sess_init_test);
  tcase_add_test(testcase, tls_using_tls_test);
  tcase_add_test(testcase, tls_match_client_tls_test);
  tcase_add_test(testcase, tls_set_data_prot_test);

  suite_add_tcase(suite, testcase);
  return suite;
}