summaryrefslogtreecommitdiffstats
path: root/src/p11_child/p11_child_common_utils.c
blob: b28185201d8a53b5c98da9ae6433f8f05e90dec8 (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
/*
    SSSD

    Helper child to commmunicate with SmartCard -- common code

    Authors:
        Sumit Bose <sbose@redhat.com>

    Copyright (C) 2019 Red Hat

    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 3 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, see <http://www.gnu.org/licenses/>.
*/

#include "config.h"
#include <talloc.h>

#include "util/util.h"
#include "p11_child/p11_child.h"

static struct cert_verify_opts *init_cert_verify_opts(TALLOC_CTX *mem_ctx)
{
    struct cert_verify_opts *cert_verify_opts;

    cert_verify_opts = talloc_zero(mem_ctx, struct cert_verify_opts);
    if (cert_verify_opts == NULL) {
        DEBUG(SSSDBG_OP_FAILURE, "talloc_new failed.\n");
        return NULL;
    }

    cert_verify_opts->do_ocsp = true;
    cert_verify_opts->do_verification = true;
    cert_verify_opts->verification_partial_chain = false;
    cert_verify_opts->ocsp_default_responder = NULL;
    cert_verify_opts->ocsp_default_responder_signing_cert = NULL;
    cert_verify_opts->crl_files = NULL;
    cert_verify_opts->ocsp_dgst = CKM_SHA_1;
    cert_verify_opts->soft_ocsp = false;
    cert_verify_opts->soft_crl = false;

    return cert_verify_opts;
}

#define OCSP_DEFAUL_RESPONDER "ocsp_default_responder="
#define OCSP_DEFAUL_RESPONDER_LEN (sizeof(OCSP_DEFAUL_RESPONDER) - 1)

#define OCSP_DEFAUL_RESPONDER_SIGNING_CERT \
                                          "ocsp_default_responder_signing_cert="
#define OCSP_DEFAUL_RESPONDER_SIGNING_CERT_LEN \
                                (sizeof(OCSP_DEFAUL_RESPONDER_SIGNING_CERT) - 1)
#define CRL_FILE "crl_file="
#define CRL_FILE_LEN (sizeof(CRL_FILE) -1)

#define OCSP_DGST "ocsp_dgst="
#define OCSP_DGST_LEN (sizeof(OCSP_DGST) -1)

static errno_t parse_crl_files(const char *filename, size_t c,
                               struct cert_verify_opts *_opts)
{
    int ret;

    if (_opts->num_files == 0) {
        _opts->crl_files = talloc_array(_opts, char *, 1);
    } else {
        _opts->crl_files = talloc_realloc(_opts, _opts->crl_files,
                                          char *, _opts->num_files + 1);
    }
    if (_opts->crl_files == NULL) {
        ret = ENOMEM;
        goto done;
    }

    _opts->crl_files[_opts->num_files] = talloc_strdup(_opts,
                                                       filename);
    if (_opts->crl_files[_opts->num_files] == NULL
            || *_opts->crl_files[_opts->num_files] == '\0') {
        DEBUG(SSSDBG_CRIT_FAILURE,
                "Failed to parse crl_file option [%s].\n", filename);
        ret = EINVAL;
        goto done;
    }

    _opts->num_files++;
    ret = EOK;

done:
    return ret;
}

errno_t parse_cert_verify_opts(TALLOC_CTX *mem_ctx, const char *verify_opts,
                               struct cert_verify_opts **_cert_verify_opts)
{
    int ret;
    TALLOC_CTX *tmp_ctx;
    char **opts;
    size_t c;
    struct cert_verify_opts *cert_verify_opts;

    tmp_ctx = talloc_new(NULL);
    if (tmp_ctx == NULL) {
        DEBUG(SSSDBG_OP_FAILURE, "talloc_new failed.\n");
        return ENOMEM;
    }

    cert_verify_opts = init_cert_verify_opts(tmp_ctx);
    if (cert_verify_opts == NULL) {
        DEBUG(SSSDBG_OP_FAILURE, "init_cert_verify_opts failed.\n");
        ret = ENOMEM;
        goto done;
    }

    if (verify_opts == NULL) {
        ret = EOK;
        goto done;
    }

    ret = split_on_separator(tmp_ctx, verify_opts, ',', true, true, &opts,
                             NULL);
    if (ret != EOK) {
        DEBUG(SSSDBG_OP_FAILURE, "split_on_separator failed.\n");
        goto done;
    }

    for (c = 0; opts[c] != NULL; c++) {
        if (strcasecmp(opts[c], "no_ocsp") == 0) {
            DEBUG(SSSDBG_TRACE_ALL,
                  "Found 'no_ocsp' option, disabling OCSP.\n");
            cert_verify_opts->do_ocsp = false;
        } else if (strcasecmp(opts[c], "no_verification") == 0) {
            DEBUG(SSSDBG_CRIT_FAILURE,
                  "Found 'no_verification' option, "
                  "disabling verification completely. "
                  "This should not be used in production.\n");
            sss_log(SSS_LOG_CRIT,
                    "Smart card certificate verification disabled completely. "
                    "This should not be used in production.");
            cert_verify_opts->do_verification = false;
        } else if (strcasecmp(opts[c], "partial_chain") == 0) {
            DEBUG(SSSDBG_TRACE_ALL,
                  "Found 'partial_chain' option, verification will not fail if "
                  "a complete chain cannot be built to a self-signed "
                  "trust-anchor, provided it is possible to construct a chain "
                  "to a trusted certificate that might not be self-signed.\n");
            cert_verify_opts->verification_partial_chain = true;
        } else if (strncasecmp(opts[c], OCSP_DEFAUL_RESPONDER,
                               OCSP_DEFAUL_RESPONDER_LEN) == 0) {
            cert_verify_opts->ocsp_default_responder =
                             talloc_strdup(cert_verify_opts,
                                           &opts[c][OCSP_DEFAUL_RESPONDER_LEN]);
            if (cert_verify_opts->ocsp_default_responder == NULL
                    || *cert_verify_opts->ocsp_default_responder == '\0') {
                DEBUG(SSSDBG_CRIT_FAILURE,
                      "Failed to parse ocsp_default_responder option [%s].\n",
                      opts[c]);
                ret = EINVAL;
                goto done;
            }

            DEBUG(SSSDBG_TRACE_ALL, "Using OCSP default responder [%s]\n",
                                    cert_verify_opts->ocsp_default_responder);
        } else if (strncasecmp(opts[c],
                               OCSP_DEFAUL_RESPONDER_SIGNING_CERT,
                               OCSP_DEFAUL_RESPONDER_SIGNING_CERT_LEN) == 0) {
            cert_verify_opts->ocsp_default_responder_signing_cert =
                talloc_strdup(cert_verify_opts,
                              &opts[c][OCSP_DEFAUL_RESPONDER_SIGNING_CERT_LEN]);
            if (cert_verify_opts->ocsp_default_responder_signing_cert == NULL
                    || *cert_verify_opts->ocsp_default_responder_signing_cert
                                                                      == '\0') {
                DEBUG(SSSDBG_CRIT_FAILURE,
                      "Failed to parse ocsp_default_responder_signing_cert "
                      "option [%s].\n", opts[c]);
                ret = EINVAL;
                goto done;
            }

            DEBUG(SSSDBG_TRACE_ALL,
                  "Using OCSP default responder signing cert nickname [%s]\n",
                  cert_verify_opts->ocsp_default_responder_signing_cert);
        } else if (strncasecmp(opts[c], CRL_FILE, CRL_FILE_LEN) == 0) {
            ret = parse_crl_files(&opts[c][CRL_FILE_LEN], c, cert_verify_opts);
            if (ret != EOK) {
                goto done;
            }
        } else if (strncasecmp(opts[c], OCSP_DGST, OCSP_DGST_LEN) == 0) {
            if (strcmp("sha1", &opts[c][OCSP_DGST_LEN]) == 0) {
                cert_verify_opts->ocsp_dgst = CKM_SHA_1;
                DEBUG(SSSDBG_TRACE_ALL, "Using sha1 for OCSP.\n");
            } else  if (strcmp("sha256", &opts[c][OCSP_DGST_LEN]) == 0) {
                cert_verify_opts->ocsp_dgst = CKM_SHA256;
                DEBUG(SSSDBG_TRACE_ALL, "Using sha256 for OCSP.\n");
            } else  if (strcmp("sha384", &opts[c][OCSP_DGST_LEN]) == 0) {
                cert_verify_opts->ocsp_dgst = CKM_SHA384;
                DEBUG(SSSDBG_TRACE_ALL, "Using sha384 for OCSP.\n");
            } else  if (strcmp("sha512", &opts[c][OCSP_DGST_LEN]) == 0) {
                cert_verify_opts->ocsp_dgst = CKM_SHA512;
                DEBUG(SSSDBG_TRACE_ALL, "Using sha512 for OCSP.\n");
            } else {
                DEBUG(SSSDBG_CRIT_FAILURE,
                      "Unsupported digest for OCSP [%s], "
                      "using default sha1.\n", &opts[c][OCSP_DGST_LEN]);
                cert_verify_opts->ocsp_dgst = CKM_SHA_1;
            }
        } else if (strcasecmp(opts[c], "soft_ocsp") == 0) {
            DEBUG(SSSDBG_TRACE_ALL,
                  "Found 'soft_ocsp' option, verification will not fail if "
                  "OCSP responder cannot be connected.\n");
            cert_verify_opts->soft_ocsp = true;
        } else if (strcasecmp(opts[c], "soft_crl") == 0) {
            DEBUG(SSSDBG_TRACE_ALL,
                  "Found 'soft_crl' option, verification will not fail if "
                  "CRL is expired.\n");
            cert_verify_opts->soft_crl = true;
        } else {
            DEBUG(SSSDBG_CRIT_FAILURE,
                  "Unsupported certificate verification option [%s], " \
                  "skipping.\n", opts[c]);
        }
    }

    ret = EOK;

done:
    if (ret == EOK) {
        *_cert_verify_opts = talloc_steal(mem_ctx, cert_verify_opts);
    }

    talloc_free(tmp_ctx);

    return ret;
}