summaryrefslogtreecommitdiffstats
path: root/modules/tls/tls_conf.c
blob: a9f27de87a9ebbc5ecafb83eec9aa4fd224402b1 (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
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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
/* Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#include <assert.h>
#include <apr_lib.h>
#include <apr_strings.h>
#include <apr_version.h>

#include <httpd.h>
#include <http_core.h>
#include <http_config.h>
#include <http_log.h>
#include <http_main.h>
#include <ap_socache.h>

#include <rustls.h>

#include "tls_cert.h"
#include "tls_proto.h"
#include "tls_conf.h"
#include "tls_util.h"
#include "tls_var.h"
#include "tls_cache.h"


extern module AP_MODULE_DECLARE_DATA tls_module;
APLOG_USE_MODULE(tls);

static tls_conf_global_t *conf_global_get_or_make(apr_pool_t *pool, server_rec *s)
{
    tls_conf_global_t *gconf;

    /* we create this only once for apache's one ap_server_conf.
     * If this gets called for another server, we should already have
     * done it for ap_server_conf. */
    if (ap_server_conf && s != ap_server_conf) {
        tls_conf_server_t *sconf = tls_conf_server_get(ap_server_conf);
        ap_assert(sconf);
        ap_assert(sconf->global);
        return sconf->global;
    }

    gconf = apr_pcalloc(pool, sizeof(*gconf));
    gconf->ap_server = ap_server_conf;
    gconf->status = TLS_CONF_ST_INIT;
    gconf->proto = tls_proto_init(pool, s);
    gconf->proxy_configs = apr_array_make(pool, 10, sizeof(tls_conf_proxy_t*));

    gconf->var_lookups = apr_hash_make(pool);
    tls_var_init_lookup_hash(pool, gconf->var_lookups);
    gconf->session_cache_spec = "default";

    return gconf;
}

tls_conf_server_t *tls_conf_server_get(server_rec *s)
{
    tls_conf_server_t *sc = ap_get_module_config(s->module_config, &tls_module);
    ap_assert(sc);
    return sc;
}


#define CONF_S_NAME(s)  (s && s->server_hostname? s->server_hostname : "default")

void *tls_conf_create_svr(apr_pool_t *pool, server_rec *s)
{
    tls_conf_server_t *conf;

    conf = apr_pcalloc(pool, sizeof(*conf));
    conf->global = conf_global_get_or_make(pool, s);
    conf->server = s;

    conf->enabled = TLS_FLAG_UNSET;
    conf->cert_specs = apr_array_make(pool, 3, sizeof(tls_cert_spec_t*));
    conf->honor_client_order = TLS_FLAG_UNSET;
    conf->strict_sni = TLS_FLAG_UNSET;
    conf->tls_protocol_min = TLS_FLAG_UNSET;
    conf->tls_pref_ciphers = apr_array_make(pool, 3, sizeof(apr_uint16_t));;
    conf->tls_supp_ciphers = apr_array_make(pool, 3, sizeof(apr_uint16_t));;
    return conf;
}

#define MERGE_INT(base, add, field) \
    (add->field == TLS_FLAG_UNSET)? base->field : add->field;

void *tls_conf_merge_svr(apr_pool_t *pool, void *basev, void *addv)
{
    tls_conf_server_t *base = basev;
    tls_conf_server_t *add = addv;
    tls_conf_server_t *nconf;

    nconf = apr_pcalloc(pool, sizeof(*nconf));
    nconf->server = add->server;
    nconf->global = add->global? add->global : base->global;

    nconf->enabled = MERGE_INT(base, add, enabled);
    nconf->cert_specs = apr_array_append(pool, base->cert_specs, add->cert_specs);
    nconf->tls_protocol_min = MERGE_INT(base, add, tls_protocol_min);
    nconf->tls_pref_ciphers = add->tls_pref_ciphers->nelts?
        add->tls_pref_ciphers : base->tls_pref_ciphers;
    nconf->tls_supp_ciphers = add->tls_supp_ciphers->nelts?
        add->tls_supp_ciphers : base->tls_supp_ciphers;
    nconf->honor_client_order = MERGE_INT(base, add, honor_client_order);
    nconf->client_ca = add->client_ca? add->client_ca : base->client_ca;
    nconf->client_auth = (add->client_auth != TLS_CLIENT_AUTH_UNSET)?
        add->client_auth : base->client_auth;
    nconf->var_user_name = add->var_user_name? add->var_user_name : base->var_user_name;
    return nconf;
}

tls_conf_dir_t *tls_conf_dir_get(request_rec *r)
{
    tls_conf_dir_t *dc = ap_get_module_config(r->per_dir_config, &tls_module);
    ap_assert(dc);
    return dc;
}

tls_conf_dir_t *tls_conf_dir_server_get(server_rec *s)
{
    tls_conf_dir_t *dc = ap_get_module_config(s->lookup_defaults, &tls_module);
    ap_assert(dc);
    return dc;
}

void *tls_conf_create_dir(apr_pool_t *pool, char *dir)
{
    tls_conf_dir_t *conf;

    (void)dir;
    conf = apr_pcalloc(pool, sizeof(*conf));
    conf->std_env_vars = TLS_FLAG_UNSET;
    conf->proxy_enabled = TLS_FLAG_UNSET;
    conf->proxy_protocol_min = TLS_FLAG_UNSET;
    conf->proxy_pref_ciphers = apr_array_make(pool, 3, sizeof(apr_uint16_t));;
    conf->proxy_supp_ciphers = apr_array_make(pool, 3, sizeof(apr_uint16_t));;
    conf->proxy_machine_cert_specs = apr_array_make(pool, 3, sizeof(tls_cert_spec_t*));
    return conf;
}


static int same_proxy_settings(tls_conf_dir_t *a, tls_conf_dir_t *b)
{
    return a->proxy_ca == b->proxy_ca;
}

static void dir_assign_merge(
    tls_conf_dir_t *dest, apr_pool_t *pool, tls_conf_dir_t *base, tls_conf_dir_t *add)
{
    tls_conf_dir_t local;

    memset(&local, 0, sizeof(local));
    local.std_env_vars = MERGE_INT(base, add, std_env_vars);
    local.export_cert_vars = MERGE_INT(base, add, export_cert_vars);
    local.proxy_enabled = MERGE_INT(base, add, proxy_enabled);
    local.proxy_ca = add->proxy_ca? add->proxy_ca : base->proxy_ca;
    local.proxy_protocol_min = MERGE_INT(base, add, proxy_protocol_min);
    local.proxy_pref_ciphers = add->proxy_pref_ciphers->nelts?
        add->proxy_pref_ciphers : base->proxy_pref_ciphers;
    local.proxy_supp_ciphers = add->proxy_supp_ciphers->nelts?
        add->proxy_supp_ciphers : base->proxy_supp_ciphers;
    local.proxy_machine_cert_specs = apr_array_append(pool,
        base->proxy_machine_cert_specs, add->proxy_machine_cert_specs);
    if (local.proxy_enabled == TLS_FLAG_TRUE) {
        if (add->proxy_config) {
            local.proxy_config = same_proxy_settings(&local, add)? add->proxy_config : NULL;
        }
        else if (base->proxy_config) {
            local.proxy_config = same_proxy_settings(&local, base)? add->proxy_config : NULL;
        }
    }
    memcpy(dest, &local, sizeof(*dest));
}

void *tls_conf_merge_dir(apr_pool_t *pool, void *basev, void *addv)
{
    tls_conf_dir_t *base = basev;
    tls_conf_dir_t *add = addv;
    tls_conf_dir_t *nconf = apr_pcalloc(pool, sizeof(*nconf));
    dir_assign_merge(nconf, pool, base, add);
    return nconf;
}

static void tls_conf_dir_set_options_defaults(apr_pool_t *pool, tls_conf_dir_t *dc)
{
    (void)pool;
    dc->std_env_vars = TLS_FLAG_FALSE;
    dc->export_cert_vars = TLS_FLAG_FALSE;
}

apr_status_t tls_conf_server_apply_defaults(tls_conf_server_t *sc, apr_pool_t *p)
{
    (void)p;
    if (sc->enabled == TLS_FLAG_UNSET) sc->enabled = TLS_FLAG_FALSE;
    if (sc->tls_protocol_min == TLS_FLAG_UNSET) sc->tls_protocol_min = 0;
    if (sc->honor_client_order == TLS_FLAG_UNSET) sc->honor_client_order = TLS_FLAG_TRUE;
    if (sc->strict_sni == TLS_FLAG_UNSET) sc->strict_sni = TLS_FLAG_TRUE;
    if (sc->client_auth == TLS_CLIENT_AUTH_UNSET) sc->client_auth = TLS_CLIENT_AUTH_NONE;
    return APR_SUCCESS;
}

apr_status_t tls_conf_dir_apply_defaults(tls_conf_dir_t *dc, apr_pool_t *p)
{
    (void)p;
    if (dc->std_env_vars == TLS_FLAG_UNSET) dc->std_env_vars = TLS_FLAG_FALSE;
    if (dc->export_cert_vars == TLS_FLAG_UNSET) dc->export_cert_vars = TLS_FLAG_FALSE;
    if (dc->proxy_enabled == TLS_FLAG_UNSET) dc->proxy_enabled = TLS_FLAG_FALSE;
    return APR_SUCCESS;
}

tls_conf_proxy_t *tls_conf_proxy_make(
    apr_pool_t *p, tls_conf_dir_t *dc, tls_conf_global_t *gc, server_rec *s)
{
    tls_conf_proxy_t *pc = apr_pcalloc(p, sizeof(*pc));
    pc->defined_in = s;
    pc->global = gc;
    pc->proxy_ca = dc->proxy_ca;
    pc->proxy_protocol_min = dc->proxy_protocol_min;
    pc->proxy_pref_ciphers = dc->proxy_pref_ciphers;
    pc->proxy_supp_ciphers = dc->proxy_supp_ciphers;
    pc->machine_cert_specs = dc->proxy_machine_cert_specs;
    pc->machine_certified_keys = apr_array_make(p, 3, sizeof(const rustls_certified_key*));
    return pc;
}

int tls_proxy_section_post_config(
    apr_pool_t *p, apr_pool_t *plog, apr_pool_t *ptemp, server_rec *s,
    ap_conf_vector_t *section_config)
{
    tls_conf_dir_t *proxy_dc, *server_dc;
    tls_conf_server_t *sc;

    /* mod_proxy collects the <Proxy>...</Proxy> sections per server (base server or virtualhost)
     * and in its post_config hook, calls our function registered at its hook for each with
     * s - the server they were define in
     * section_config - the set of dir_configs for a <Proxy> section
     *
     * If none of _our_ config directives had been used, here or in the server, we get a NULL.
     * Which means we have to do nothing. Otherwise, we add to `proxy_dc` the
     * settings from `server_dc` - since this is not automagically done by apache.
     *
     * `proxy_dc` is then complete and tells us if we handle outgoing connections
     * here and with what parameter settings.
     */
    (void)ptemp; (void)plog;
    ap_log_error(APLOG_MARK, APLOG_TRACE3, 0, s,
        "%s: tls_proxy_section_post_config called", s->server_hostname);
    proxy_dc = ap_get_module_config(section_config, &tls_module);
    if (!proxy_dc) goto cleanup;
    server_dc = ap_get_module_config(s->lookup_defaults, &tls_module);
    ap_assert(server_dc);
    dir_assign_merge(proxy_dc, p, server_dc, proxy_dc);
    tls_conf_dir_apply_defaults(proxy_dc, p);
    if (proxy_dc->proxy_enabled && !proxy_dc->proxy_config) {
        /* remember `proxy_dc` for subsequent configuration of outoing TLS setups */
        sc = tls_conf_server_get(s);
        proxy_dc->proxy_config = tls_conf_proxy_make(p, proxy_dc, sc->global, s);
        ap_log_error(APLOG_MARK, APLOG_TRACE3, 0, s,
            "%s: adding proxy_conf to globals in proxy_post_config_section",
            s->server_hostname);
        APR_ARRAY_PUSH(sc->global->proxy_configs, tls_conf_proxy_t*) = proxy_dc->proxy_config;
    }
cleanup:
    return OK;
}

static const char *cmd_check_file(cmd_parms *cmd, const char *fpath)
{
    char *real_path;

    /* just a dump of the configuration, dont resolve/check */
    if (ap_state_query(AP_SQ_RUN_MODE) == AP_SQ_RM_CONFIG_DUMP) {
        return NULL;
    }
    real_path = ap_server_root_relative(cmd->pool, fpath);
    if (!real_path) {
        return apr_pstrcat(cmd->pool, cmd->cmd->name,
                           ": Invalid file path ", fpath, NULL);
    }
    if (!tls_util_is_file(cmd->pool, real_path)) {
        return apr_pstrcat(cmd->pool, cmd->cmd->name,
                           ": file '", real_path,
                           "' does not exist or is empty", NULL);
    }
    return NULL;
}

static const char *tls_conf_add_engine(cmd_parms *cmd, void *dc, const char*v)
{
    tls_conf_server_t *sc = tls_conf_server_get(cmd->server);
    tls_conf_global_t *gc = sc->global;
    const char *err = NULL;
    char *host, *scope_id;
    apr_port_t port;
    apr_sockaddr_t *sa;
    server_addr_rec *sar;
    apr_status_t rv;

    (void)dc;
    /* Example of use:
     * TLSEngine 443
     * TLSEngine hostname:443
     * TLSEngine 91.0.0.1:443
     * TLSEngine [::0]:443
     */
    rv = apr_parse_addr_port(&host, &scope_id, &port, v, cmd->pool);
    if (APR_SUCCESS != rv) {
        err = apr_pstrcat(cmd->pool, cmd->cmd->name,
                          ": invalid address/port in '", v, "'", NULL);
        goto cleanup;
    }

    /* translate host/port to a sockaddr that we can match with incoming connections */
    rv = apr_sockaddr_info_get(&sa, host, APR_UNSPEC, port, 0, cmd->pool);
    if (APR_SUCCESS != rv) {
        err = apr_pstrcat(cmd->pool, cmd->cmd->name,
                          ": unable to get sockaddr for '", host, "'", NULL);
        goto cleanup;
    }

    if (scope_id) {
#if APR_VERSION_AT_LEAST(1,7,0)
        rv = apr_sockaddr_zone_set(sa, scope_id);
        if (APR_SUCCESS != rv) {
            err = apr_pstrcat(cmd->pool, cmd->cmd->name,
                              ": error setting ipv6 scope id: '", scope_id, "'", NULL);
            goto cleanup;
        }
#else
        err = apr_pstrcat(cmd->pool, cmd->cmd->name,
                          ": IPv6 scopes not supported by your APR: '", scope_id, "'", NULL);
        goto cleanup;
#endif
    }

    sar = apr_pcalloc(cmd->pool, sizeof(*sar));
    sar->host_addr = sa;
    sar->virthost = host;
    sar->host_port = port;

    sar->next = gc->tls_addresses;
    gc->tls_addresses = sar;
cleanup:
    return err;
}

static int flag_value(
    const char *arg)
{
    if (!strcasecmp(arg, "On")) {
        return TLS_FLAG_TRUE;
    }
    else if (!strcasecmp(arg, "Off")) {
        return TLS_FLAG_FALSE;
    }
    return TLS_FLAG_UNSET;
}

static const char *flag_err(
    cmd_parms *cmd, const char *v)
{
    return apr_pstrcat(cmd->pool, cmd->cmd->name,
        ": value must be 'On' or 'Off': '", v, "'", NULL);
}

static const char *tls_conf_add_certificate(
    cmd_parms *cmd, void *dc, const char *cert_file, const char *pkey_file)
{
    tls_conf_server_t *sc = tls_conf_server_get(cmd->server);
    const char *err = NULL, *fpath;
    tls_cert_spec_t *cert;

    (void)dc;
    if (NULL != (err = cmd_check_file(cmd, cert_file))) goto cleanup;
    /* key file may be NULL, in which case cert_file must contain the key PEM */
    if (pkey_file && NULL != (err = cmd_check_file(cmd, pkey_file))) goto cleanup;

    cert = apr_pcalloc(cmd->pool, sizeof(*cert));
    fpath = ap_server_root_relative(cmd->pool, cert_file);
    if (!tls_util_is_file(cmd->pool, fpath)) {
        return apr_pstrcat(cmd->pool, cmd->cmd->name,
            ": unable to find certificate file: '", fpath, "'", NULL);
    }
    cert->cert_file = cert_file;
    if (pkey_file) {
        fpath = ap_server_root_relative(cmd->pool, pkey_file);
        if (!tls_util_is_file(cmd->pool, fpath)) {
            return apr_pstrcat(cmd->pool, cmd->cmd->name,
                ": unable to find certificate key file: '", fpath, "'", NULL);
        }
    }
    cert->pkey_file = pkey_file;
    *(const tls_cert_spec_t **)apr_array_push(sc->cert_specs) = cert;

cleanup:
    return err;
}

static const char *parse_ciphers(
    cmd_parms *cmd,
    tls_conf_global_t *gc,
    const char *nop_name,
    int argc, char *const argv[],
    apr_array_header_t *ciphers)
{
    apr_array_clear(ciphers);
    if (argc > 1 || apr_strnatcasecmp(nop_name, argv[0])) {
        apr_uint16_t cipher;
        int i;

        for (i = 0; i < argc; ++i) {
            char *name, *last = NULL;
            const char *value = argv[i];

            name = apr_strtok(apr_pstrdup(cmd->pool, value), ":", &last);
            while (name) {
                if (tls_proto_get_cipher_by_name(gc->proto, name, &cipher) != APR_SUCCESS) {
                    return apr_pstrcat(cmd->pool, cmd->cmd->name,
                            ": cipher not recognized '", name, "'", NULL);
                }
                APR_ARRAY_PUSH(ciphers, apr_uint16_t) = cipher;
                name = apr_strtok(NULL, ":", &last);
            }
        }
    }
    return NULL;
}

static const char *tls_conf_set_preferred_ciphers(
    cmd_parms *cmd, void *dc, int argc, char *const argv[])
{
    tls_conf_server_t *sc = tls_conf_server_get(cmd->server);
    const char *err = NULL;

    (void)dc;
    if (!argc) {
        err = "specify the TLS ciphers to prefer or 'default' for the rustls default ordering.";
        goto cleanup;
    }
    err = parse_ciphers(cmd, sc->global, "default", argc, argv, sc->tls_pref_ciphers);
cleanup:
    return err;
}

static const char *tls_conf_set_suppressed_ciphers(
    cmd_parms *cmd, void *dc, int argc, char *const argv[])
{
    tls_conf_server_t *sc = tls_conf_server_get(cmd->server);
    const char *err = NULL;

    (void)dc;
    if (!argc) {
        err = "specify the TLS ciphers to never use or 'none'.";
        goto cleanup;
    }
    err = parse_ciphers(cmd, sc->global, "none", argc, argv, sc->tls_supp_ciphers);
cleanup:
    return err;
}

static const char *tls_conf_set_honor_client_order(
    cmd_parms *cmd, void *dc, const char *v)
{
    tls_conf_server_t *sc = tls_conf_server_get(cmd->server);
    int flag = flag_value(v);

    (void)dc;
    if (TLS_FLAG_UNSET == flag) return flag_err(cmd, v);
    sc->honor_client_order = flag;
    return NULL;
}

static const char *tls_conf_set_strict_sni(
    cmd_parms *cmd, void *dc, const char *v)
{
    tls_conf_server_t *sc = tls_conf_server_get(cmd->server);
    int flag = flag_value(v);

    (void)dc;
    if (TLS_FLAG_UNSET == flag) return flag_err(cmd, v);
    sc->strict_sni = flag;
    return NULL;
}

static const char *get_min_protocol(
    cmd_parms *cmd, const char *v, int *pmin)
{
    tls_conf_server_t *sc = tls_conf_server_get(cmd->server);
    const char *err = NULL;

    if (!apr_strnatcasecmp("default", v)) {
        *pmin = 0;
    }
    else if (*v && v[strlen(v)-1] == '+') {
        char *name = apr_pstrdup(cmd->pool, v);
        name[strlen(name)-1] = '\0';
        *pmin = tls_proto_get_version_by_name(sc->global->proto, name);
        if (!*pmin) {
            err = apr_pstrcat(cmd->pool, cmd->cmd->name,
                ": unrecognized protocol version specifier (try TLSv1.2+ or TLSv1.3+): '", v, "'", NULL);
            goto cleanup;
        }
    }
    else {
        err = apr_pstrcat(cmd->pool, cmd->cmd->name,
            ": value must be 'default', 'TLSv1.2+' or 'TLSv1.3+': '", v, "'", NULL);
        goto cleanup;
    }
cleanup:
    return err;
}

static const char *tls_conf_set_protocol(
    cmd_parms *cmd, void *dc, const char *v)
{
    tls_conf_server_t *sc = tls_conf_server_get(cmd->server);
    (void)dc;
    return get_min_protocol(cmd, v, &sc->tls_protocol_min);
}

static const char *tls_conf_set_options(
    cmd_parms *cmd, void *dcv, int argc, char *const argv[])
{
    tls_conf_dir_t *dc = dcv;
    const char *err = NULL, *option;
    int i, val;

    /* Are we only having deltas (+/-) or do we reset the options? */
    for (i = 0; i < argc; ++i) {
        if (argv[i][0] != '+' && argv[i][0] != '-') {
            tls_conf_dir_set_options_defaults(cmd->pool, dc);
            break;
        }
    }

    for (i = 0; i < argc; ++i) {
        option = argv[i];
        if (!apr_strnatcasecmp("Defaults", option)) {
            dc->std_env_vars = TLS_FLAG_FALSE;
            dc->export_cert_vars = TLS_FLAG_FALSE;
        }
        else {
            val = TLS_FLAG_TRUE;
            if (*option == '+' || *option == '-') {
                val = (*option == '+')? TLS_FLAG_TRUE : TLS_FLAG_FALSE;
                ++option;
            }

            if (!apr_strnatcasecmp("StdEnvVars", option)) {
                dc->std_env_vars = val;
            }
            else if (!apr_strnatcasecmp("ExportCertData", option)) {
                dc->export_cert_vars = val;
            }
            else {
                err = apr_pstrcat(cmd->pool, cmd->cmd->name,
                                   ": unknown option '", option, "'", NULL);
                goto cleanup;
            }
        }
    }
cleanup:
    return err;
}

static const char *tls_conf_set_session_cache(
    cmd_parms *cmd, void *dc, const char *value)
{
    tls_conf_server_t *sc = tls_conf_server_get(cmd->server);
    const char *err = NULL;

    (void)dc;
    if ((err = ap_check_cmd_context(cmd, GLOBAL_ONLY))) goto cleanup;

    err = tls_cache_set_specification(value, sc->global, cmd->pool, cmd->temp_pool);
cleanup:
    return err;
}

static const char *tls_conf_set_proxy_engine(cmd_parms *cmd, void *dir_conf, int flag)
{
    tls_conf_dir_t *dc = dir_conf;
    (void)cmd;
    dc->proxy_enabled = flag ? TLS_FLAG_TRUE : TLS_FLAG_FALSE;
    return NULL;
}

static const char *tls_conf_set_proxy_ca(
    cmd_parms *cmd, void *dir_conf, const char *proxy_ca)
{
    tls_conf_dir_t *dc = dir_conf;
    const char *err = NULL;

    if (strcasecmp(proxy_ca, "default") && NULL != (err = cmd_check_file(cmd, proxy_ca))) goto cleanup;
    dc->proxy_ca = proxy_ca;
cleanup:
    return err;
}

static const char *tls_conf_set_proxy_protocol(
    cmd_parms *cmd, void *dir_conf, const char *v)
{
    tls_conf_dir_t *dc = dir_conf;
    return get_min_protocol(cmd, v, &dc->proxy_protocol_min);
}

static const char *tls_conf_set_proxy_preferred_ciphers(
    cmd_parms *cmd, void *dir_conf, int argc, char *const argv[])
{
    tls_conf_server_t *sc = tls_conf_server_get(cmd->server);
    tls_conf_dir_t *dc = dir_conf;
    const char *err = NULL;

    if (!argc) {
        err = "specify the proxy TLS ciphers to prefer or 'default' for the rustls default ordering.";
        goto cleanup;
    }
    err = parse_ciphers(cmd, sc->global, "default", argc, argv, dc->proxy_pref_ciphers);
cleanup:
    return err;
}

static const char *tls_conf_set_proxy_suppressed_ciphers(
    cmd_parms *cmd, void *dir_conf, int argc, char *const argv[])
{
    tls_conf_server_t *sc = tls_conf_server_get(cmd->server);
    tls_conf_dir_t *dc = dir_conf;
    const char *err = NULL;

    if (!argc) {
        err = "specify the proxy TLS ciphers to never use or 'none'.";
        goto cleanup;
    }
    err = parse_ciphers(cmd, sc->global, "none", argc, argv, dc->proxy_supp_ciphers);
cleanup:
    return err;
}

#if TLS_CLIENT_CERTS

static const char *tls_conf_set_client_ca(
    cmd_parms *cmd, void *dc, const char *client_ca)
{
    tls_conf_server_t *sc = tls_conf_server_get(cmd->server);
    const char *err;

    (void)dc;
    if (NULL != (err = cmd_check_file(cmd, client_ca))) goto cleanup;
    sc->client_ca = client_ca;
cleanup:
    return err;
}

static const char *tls_conf_set_client_auth(
    cmd_parms *cmd, void *dc, const char *mode)
{
    tls_conf_server_t *sc = tls_conf_server_get(cmd->server);
    const char *err = NULL;
    (void)dc;
    if (!strcasecmp(mode, "required")) {
        sc->client_auth = TLS_CLIENT_AUTH_REQUIRED;
    }
    else if (!strcasecmp(mode, "optional")) {
        sc->client_auth = TLS_CLIENT_AUTH_OPTIONAL;
    }
    else if (!strcasecmp(mode, "none")) {
        sc->client_auth = TLS_CLIENT_AUTH_NONE;
    }
    else {
        err = apr_pstrcat(cmd->pool, cmd->cmd->name,
            ": unknown value: '", mode, "', use required/optional/none.", NULL);
    }
    return err;
}

static const char *tls_conf_set_user_name(
    cmd_parms *cmd, void *dc, const char *var_user_name)
{
    tls_conf_server_t *sc = tls_conf_server_get(cmd->server);
    (void)dc;
    sc->var_user_name = var_user_name;
    return NULL;
}

#endif /* if TLS_CLIENT_CERTS */

#if TLS_MACHINE_CERTS

static const char *tls_conf_add_proxy_machine_certificate(
    cmd_parms *cmd, void *dir_conf, const char *cert_file, const char *pkey_file)
{
    tls_conf_dir_t *dc = dir_conf;
    const char *err = NULL, *fpath;
    tls_cert_spec_t *cert;

    (void)dc;
    if (NULL != (err = cmd_check_file(cmd, cert_file))) goto cleanup;
    /* key file may be NULL, in which case cert_file must contain the key PEM */
    if (pkey_file && NULL != (err = cmd_check_file(cmd, pkey_file))) goto cleanup;

    cert = apr_pcalloc(cmd->pool, sizeof(*cert));
    fpath = ap_server_root_relative(cmd->pool, cert_file);
    if (!tls_util_is_file(cmd->pool, fpath)) {
        return apr_pstrcat(cmd->pool, cmd->cmd->name,
            ": unable to find certificate file: '", fpath, "'", NULL);
    }
    cert->cert_file = cert_file;
    if (pkey_file) {
        fpath = ap_server_root_relative(cmd->pool, pkey_file);
        if (!tls_util_is_file(cmd->pool, fpath)) {
            return apr_pstrcat(cmd->pool, cmd->cmd->name,
                ": unable to find certificate key file: '", fpath, "'", NULL);
        }
    }
    cert->pkey_file = pkey_file;
    *(const tls_cert_spec_t **)apr_array_push(dc->proxy_machine_cert_specs) = cert;

cleanup:
    return err;
}

#endif  /* if TLS_MACHINE_CERTS */

const command_rec tls_conf_cmds[] = {
    AP_INIT_TAKE12("TLSCertificate", tls_conf_add_certificate, NULL, RSRC_CONF,
        "Add a certificate to the server by specifying a file containing the "
        "certificate PEM, followed by its chain PEMs. The PEM of the key must "
        "either also be there or can be given as a separate file."),
    AP_INIT_TAKE_ARGV("TLSCiphersPrefer", tls_conf_set_preferred_ciphers, NULL, RSRC_CONF,
        "Set the TLS ciphers to prefer when negotiating with a client."),
    AP_INIT_TAKE_ARGV("TLSCiphersSuppress", tls_conf_set_suppressed_ciphers, NULL, RSRC_CONF,
        "Set the TLS ciphers to never use when negotiating with a client."),
    AP_INIT_TAKE1("TLSHonorClientOrder", tls_conf_set_honor_client_order, NULL, RSRC_CONF,
        "Set 'on' to have the server honor client preferences in cipher suites, default off."),
    AP_INIT_TAKE1("TLSEngine", tls_conf_add_engine, NULL, RSRC_CONF,
        "Specify an address+port where the module shall handle incoming TLS connections."),
    AP_INIT_TAKE_ARGV("TLSOptions", tls_conf_set_options, NULL, OR_OPTIONS,
        "En-/disables optional features in the module."),
    AP_INIT_TAKE1("TLSProtocol", tls_conf_set_protocol, NULL, RSRC_CONF,
        "Set the minimum TLS protocol version to use."),
    AP_INIT_TAKE1("TLSStrictSNI", tls_conf_set_strict_sni, NULL, RSRC_CONF,
        "Set strictness of client server name (SNI) check against hosts, default on."),
    AP_INIT_TAKE1("TLSSessionCache", tls_conf_set_session_cache, NULL, RSRC_CONF,
        "Set which cache to use for TLS sessions."),
    AP_INIT_FLAG("TLSProxyEngine", tls_conf_set_proxy_engine, NULL, RSRC_CONF|PROXY_CONF,
        "Enable TLS encryption of outgoing connections in this location/server."),
    AP_INIT_TAKE1("TLSProxyCA", tls_conf_set_proxy_ca, NULL, RSRC_CONF|PROXY_CONF,
        "Set the trust anchors for certificates from proxied backend servers from a PEM file."),
    AP_INIT_TAKE1("TLSProxyProtocol", tls_conf_set_proxy_protocol, NULL, RSRC_CONF|PROXY_CONF,
        "Set the minimum TLS protocol version to use for proxy connections."),
    AP_INIT_TAKE_ARGV("TLSProxyCiphersPrefer", tls_conf_set_proxy_preferred_ciphers, NULL, RSRC_CONF|PROXY_CONF,
        "Set the TLS ciphers to prefer when negotiating a proxy connection."),
    AP_INIT_TAKE_ARGV("TLSProxyCiphersSuppress", tls_conf_set_proxy_suppressed_ciphers, NULL, RSRC_CONF|PROXY_CONF,
        "Set the TLS ciphers to never use when negotiating a proxy connection."),
#if TLS_CLIENT_CERTS
    AP_INIT_TAKE1("TLSClientCA", tls_conf_set_client_ca, NULL, RSRC_CONF,
        "Set the trust anchors for client certificates from a PEM file."),
    AP_INIT_TAKE1("TLSClientCertificate", tls_conf_set_client_auth, NULL, RSRC_CONF,
        "If TLS client authentication is 'required', 'optional' or 'none'."),
    AP_INIT_TAKE1("TLSUserName", tls_conf_set_user_name, NULL, RSRC_CONF,
        "Set the SSL variable to be used as user name."),
#endif  /* if TLS_CLIENT_CERTS */
#if TLS_MACHINE_CERTS
    AP_INIT_TAKE12("TLSProxyMachineCertificate", tls_conf_add_proxy_machine_certificate, NULL, RSRC_CONF|PROXY_CONF,
        "Add a certificate to be used as client certificate on a proxy connection. "),
#endif  /* if TLS_MACHINE_CERTS */
    AP_INIT_TAKE1(NULL, NULL, NULL, RSRC_CONF, NULL)
};