Coverage Report

Created: 2018-08-29 13:53

/src/openssl/crypto/x509/x_crl.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved.
3
 *
4
 * Licensed under the OpenSSL license (the "License").  You may not use
5
 * this file except in compliance with the License.  You can obtain a copy
6
 * in the file LICENSE in the source distribution or at
7
 * https://www.openssl.org/source/license.html
8
 */
9
10
#include <stdio.h>
11
#include "internal/cryptlib.h"
12
#include <openssl/asn1t.h>
13
#include <openssl/x509.h>
14
#include "internal/x509_int.h"
15
#include <openssl/x509v3.h>
16
#include "x509_lcl.h"
17
18
static int X509_REVOKED_cmp(const X509_REVOKED *const *a,
19
                            const X509_REVOKED *const *b);
20
static void setup_idp(X509_CRL *crl, ISSUING_DIST_POINT *idp);
21
22
ASN1_SEQUENCE(X509_REVOKED) = {
23
        ASN1_EMBED(X509_REVOKED,serialNumber, ASN1_INTEGER),
24
        ASN1_SIMPLE(X509_REVOKED,revocationDate, ASN1_TIME),
25
        ASN1_SEQUENCE_OF_OPT(X509_REVOKED,extensions, X509_EXTENSION)
26
} ASN1_SEQUENCE_END(X509_REVOKED)
27
28
static int def_crl_verify(X509_CRL *crl, EVP_PKEY *r);
29
static int def_crl_lookup(X509_CRL *crl,
30
                          X509_REVOKED **ret, ASN1_INTEGER *serial,
31
                          X509_NAME *issuer);
32
33
static X509_CRL_METHOD int_crl_meth = {
34
    0,
35
    0, 0,
36
    def_crl_lookup,
37
    def_crl_verify
38
};
39
40
static const X509_CRL_METHOD *default_crl_method = &int_crl_meth;
41
42
/*
43
 * The X509_CRL_INFO structure needs a bit of customisation. Since we cache
44
 * the original encoding the signature won't be affected by reordering of the
45
 * revoked field.
46
 */
47
static int crl_inf_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it,
48
                      void *exarg)
49
0
{
50
0
    X509_CRL_INFO *a = (X509_CRL_INFO *)*pval;
51
0
52
0
    if (!a || !a->revoked)
53
0
        return 1;
54
0
    switch (operation) {
55
0
        /*
56
0
         * Just set cmp function here. We don't sort because that would
57
0
         * affect the output of X509_CRL_print().
58
0
         */
59
0
    case ASN1_OP_D2I_POST:
60
0
        (void)sk_X509_REVOKED_set_cmp_func(a->revoked, X509_REVOKED_cmp);
61
0
        break;
62
0
    }
63
0
    return 1;
64
0
}
65
66
67
ASN1_SEQUENCE_enc(X509_CRL_INFO, enc, crl_inf_cb) = {
68
        ASN1_OPT(X509_CRL_INFO, version, ASN1_INTEGER),
69
        ASN1_EMBED(X509_CRL_INFO, sig_alg, X509_ALGOR),
70
        ASN1_SIMPLE(X509_CRL_INFO, issuer, X509_NAME),
71
        ASN1_SIMPLE(X509_CRL_INFO, lastUpdate, ASN1_TIME),
72
        ASN1_OPT(X509_CRL_INFO, nextUpdate, ASN1_TIME),
73
        ASN1_SEQUENCE_OF_OPT(X509_CRL_INFO, revoked, X509_REVOKED),
74
        ASN1_EXP_SEQUENCE_OF_OPT(X509_CRL_INFO, extensions, X509_EXTENSION, 0)
75
} ASN1_SEQUENCE_END_enc(X509_CRL_INFO, X509_CRL_INFO)
76
77
/*
78
 * Set CRL entry issuer according to CRL certificate issuer extension. Check
79
 * for unhandled critical CRL entry extensions.
80
 */
81
82
static int crl_set_issuers(X509_CRL *crl)
83
0
{
84
0
85
0
    int i, j;
86
0
    GENERAL_NAMES *gens, *gtmp;
87
0
    STACK_OF(X509_REVOKED) *revoked;
88
0
89
0
    revoked = X509_CRL_get_REVOKED(crl);
90
0
91
0
    gens = NULL;
92
0
    for (i = 0; i < sk_X509_REVOKED_num(revoked); i++) {
93
0
        X509_REVOKED *rev = sk_X509_REVOKED_value(revoked, i);
94
0
        STACK_OF(X509_EXTENSION) *exts;
95
0
        ASN1_ENUMERATED *reason;
96
0
        X509_EXTENSION *ext;
97
0
        gtmp = X509_REVOKED_get_ext_d2i(rev,
98
0
                                        NID_certificate_issuer, &j, NULL);
99
0
        if (!gtmp && (j != -1)) {
100
0
            crl->flags |= EXFLAG_INVALID;
101
0
            return 1;
102
0
        }
103
0
104
0
        if (gtmp) {
105
0
            gens = gtmp;
106
0
            if (!crl->issuers) {
107
0
                crl->issuers = sk_GENERAL_NAMES_new_null();
108
0
                if (!crl->issuers)
109
0
                    return 0;
110
0
            }
111
0
            if (!sk_GENERAL_NAMES_push(crl->issuers, gtmp))
112
0
                return 0;
113
0
        }
114
0
        rev->issuer = gens;
115
0
116
0
        reason = X509_REVOKED_get_ext_d2i(rev, NID_crl_reason, &j, NULL);
117
0
        if (!reason && (j != -1)) {
118
0
            crl->flags |= EXFLAG_INVALID;
119
0
            return 1;
120
0
        }
121
0
122
0
        if (reason) {
123
0
            rev->reason = ASN1_ENUMERATED_get(reason);
124
0
            ASN1_ENUMERATED_free(reason);
125
0
        } else
126
0
            rev->reason = CRL_REASON_NONE;
127
0
128
0
        /* Check for critical CRL entry extensions */
129
0
130
0
        exts = rev->extensions;
131
0
132
0
        for (j = 0; j < sk_X509_EXTENSION_num(exts); j++) {
133
0
            ext = sk_X509_EXTENSION_value(exts, j);
134
0
            if (X509_EXTENSION_get_critical(ext)) {
135
0
                if (OBJ_obj2nid(X509_EXTENSION_get_object(ext)) == NID_certificate_issuer)
136
0
                    continue;
137
0
                crl->flags |= EXFLAG_CRITICAL;
138
0
                break;
139
0
            }
140
0
        }
141
0
142
0
    }
143
0
144
0
    return 1;
145
0
146
0
}
147
148
/*
149
 * The X509_CRL structure needs a bit of customisation. Cache some extensions
150
 * and hash of the whole CRL.
151
 */
152
static int crl_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it,
153
                  void *exarg)
154
0
{
155
0
    X509_CRL *crl = (X509_CRL *)*pval;
156
0
    STACK_OF(X509_EXTENSION) *exts;
157
0
    X509_EXTENSION *ext;
158
0
    int idx;
159
0
160
0
    switch (operation) {
161
0
    case ASN1_OP_NEW_POST:
162
0
        crl->idp = NULL;
163
0
        crl->akid = NULL;
164
0
        crl->flags = 0;
165
0
        crl->idp_flags = 0;
166
0
        crl->idp_reasons = CRLDP_ALL_REASONS;
167
0
        crl->meth = default_crl_method;
168
0
        crl->meth_data = NULL;
169
0
        crl->issuers = NULL;
170
0
        crl->crl_number = NULL;
171
0
        crl->base_crl_number = NULL;
172
0
        break;
173
0
174
0
    case ASN1_OP_D2I_POST:
175
0
        X509_CRL_digest(crl, EVP_sha1(), crl->sha1_hash, NULL);
176
0
        crl->idp = X509_CRL_get_ext_d2i(crl,
177
0
                                        NID_issuing_distribution_point, NULL,
178
0
                                        NULL);
179
0
        if (crl->idp)
180
0
            setup_idp(crl, crl->idp);
181
0
182
0
        crl->akid = X509_CRL_get_ext_d2i(crl,
183
0
                                         NID_authority_key_identifier, NULL,
184
0
                                         NULL);
185
0
186
0
        crl->crl_number = X509_CRL_get_ext_d2i(crl,
187
0
                                               NID_crl_number, NULL, NULL);
188
0
189
0
        crl->base_crl_number = X509_CRL_get_ext_d2i(crl,
190
0
                                                    NID_delta_crl, NULL,
191
0
                                                    NULL);
192
0
        /* Delta CRLs must have CRL number */
193
0
        if (crl->base_crl_number && !crl->crl_number)
194
0
            crl->flags |= EXFLAG_INVALID;
195
0
196
0
        /*
197
0
         * See if we have any unhandled critical CRL extensions and indicate
198
0
         * this in a flag. We only currently handle IDP so anything else
199
0
         * critical sets the flag. This code accesses the X509_CRL structure
200
0
         * directly: applications shouldn't do this.
201
0
         */
202
0
203
0
        exts = crl->crl.extensions;
204
0
205
0
        for (idx = 0; idx < sk_X509_EXTENSION_num(exts); idx++) {
206
0
            int nid;
207
0
            ext = sk_X509_EXTENSION_value(exts, idx);
208
0
            nid = OBJ_obj2nid(X509_EXTENSION_get_object(ext));
209
0
            if (nid == NID_freshest_crl)
210
0
                crl->flags |= EXFLAG_FRESHEST;
211
0
            if (X509_EXTENSION_get_critical(ext)) {
212
0
                /* We handle IDP and deltas */
213
0
                if ((nid == NID_issuing_distribution_point)
214
0
                    || (nid == NID_authority_key_identifier)
215
0
                    || (nid == NID_delta_crl))
216
0
                    continue;
217
0
                crl->flags |= EXFLAG_CRITICAL;
218
0
                break;
219
0
            }
220
0
        }
221
0
222
0
        if (!crl_set_issuers(crl))
223
0
            return 0;
224
0
225
0
        if (crl->meth->crl_init) {
226
0
            if (crl->meth->crl_init(crl) == 0)
227
0
                return 0;
228
0
        }
229
0
230
0
        crl->flags |= EXFLAG_SET;
231
0
        break;
232
0
233
0
    case ASN1_OP_FREE_POST:
234
0
        if (crl->meth->crl_free) {
235
0
            if (!crl->meth->crl_free(crl))
236
0
                return 0;
237
0
        }
238
0
        AUTHORITY_KEYID_free(crl->akid);
239
0
        ISSUING_DIST_POINT_free(crl->idp);
240
0
        ASN1_INTEGER_free(crl->crl_number);
241
0
        ASN1_INTEGER_free(crl->base_crl_number);
242
0
        sk_GENERAL_NAMES_pop_free(crl->issuers, GENERAL_NAMES_free);
243
0
        break;
244
0
    }
245
0
    return 1;
246
0
}
247
248
/* Convert IDP into a more convenient form */
249
250
static void setup_idp(X509_CRL *crl, ISSUING_DIST_POINT *idp)
251
0
{
252
0
    int idp_only = 0;
253
0
    /* Set various flags according to IDP */
254
0
    crl->idp_flags |= IDP_PRESENT;
255
0
    if (idp->onlyuser > 0) {
256
0
        idp_only++;
257
0
        crl->idp_flags |= IDP_ONLYUSER;
258
0
    }
259
0
    if (idp->onlyCA > 0) {
260
0
        idp_only++;
261
0
        crl->idp_flags |= IDP_ONLYCA;
262
0
    }
263
0
    if (idp->onlyattr > 0) {
264
0
        idp_only++;
265
0
        crl->idp_flags |= IDP_ONLYATTR;
266
0
    }
267
0
268
0
    if (idp_only > 1)
269
0
        crl->idp_flags |= IDP_INVALID;
270
0
271
0
    if (idp->indirectCRL > 0)
272
0
        crl->idp_flags |= IDP_INDIRECT;
273
0
274
0
    if (idp->onlysomereasons) {
275
0
        crl->idp_flags |= IDP_REASONS;
276
0
        if (idp->onlysomereasons->length > 0)
277
0
            crl->idp_reasons = idp->onlysomereasons->data[0];
278
0
        if (idp->onlysomereasons->length > 1)
279
0
            crl->idp_reasons |= (idp->onlysomereasons->data[1] << 8);
280
0
        crl->idp_reasons &= CRLDP_ALL_REASONS;
281
0
    }
282
0
283
0
    DIST_POINT_set_dpname(idp->distpoint, X509_CRL_get_issuer(crl));
284
0
}
285
286
ASN1_SEQUENCE_ref(X509_CRL, crl_cb) = {
287
        ASN1_EMBED(X509_CRL, crl, X509_CRL_INFO),
288
        ASN1_EMBED(X509_CRL, sig_alg, X509_ALGOR),
289
        ASN1_EMBED(X509_CRL, signature, ASN1_BIT_STRING)
290
} ASN1_SEQUENCE_END_ref(X509_CRL, X509_CRL)
291
292
IMPLEMENT_ASN1_FUNCTIONS(X509_REVOKED)
293
294
IMPLEMENT_ASN1_DUP_FUNCTION(X509_REVOKED)
295
296
IMPLEMENT_ASN1_FUNCTIONS(X509_CRL_INFO)
297
298
IMPLEMENT_ASN1_FUNCTIONS(X509_CRL)
299
300
IMPLEMENT_ASN1_DUP_FUNCTION(X509_CRL)
301
302
static int X509_REVOKED_cmp(const X509_REVOKED *const *a,
303
                            const X509_REVOKED *const *b)
304
0
{
305
0
    return (ASN1_STRING_cmp((ASN1_STRING *)&(*a)->serialNumber,
306
0
                            (ASN1_STRING *)&(*b)->serialNumber));
307
0
}
308
309
int X509_CRL_add0_revoked(X509_CRL *crl, X509_REVOKED *rev)
310
0
{
311
0
    X509_CRL_INFO *inf;
312
0
313
0
    inf = &crl->crl;
314
0
    if (inf->revoked == NULL)
315
0
        inf->revoked = sk_X509_REVOKED_new(X509_REVOKED_cmp);
316
0
    if (inf->revoked == NULL || !sk_X509_REVOKED_push(inf->revoked, rev)) {
317
0
        ASN1err(ASN1_F_X509_CRL_ADD0_REVOKED, ERR_R_MALLOC_FAILURE);
318
0
        return 0;
319
0
    }
320
0
    inf->enc.modified = 1;
321
0
    return 1;
322
0
}
323
324
int X509_CRL_verify(X509_CRL *crl, EVP_PKEY *r)
325
0
{
326
0
    if (crl->meth->crl_verify)
327
0
        return crl->meth->crl_verify(crl, r);
328
0
    return 0;
329
0
}
330
331
int X509_CRL_get0_by_serial(X509_CRL *crl,
332
                            X509_REVOKED **ret, ASN1_INTEGER *serial)
333
0
{
334
0
    if (crl->meth->crl_lookup)
335
0
        return crl->meth->crl_lookup(crl, ret, serial, NULL);
336
0
    return 0;
337
0
}
338
339
int X509_CRL_get0_by_cert(X509_CRL *crl, X509_REVOKED **ret, X509 *x)
340
0
{
341
0
    if (crl->meth->crl_lookup)
342
0
        return crl->meth->crl_lookup(crl, ret,
343
0
                                     X509_get_serialNumber(x),
344
0
                                     X509_get_issuer_name(x));
345
0
    return 0;
346
0
}
347
348
static int def_crl_verify(X509_CRL *crl, EVP_PKEY *r)
349
0
{
350
0
    return (ASN1_item_verify(ASN1_ITEM_rptr(X509_CRL_INFO),
351
0
                             &crl->sig_alg, &crl->signature, &crl->crl, r));
352
0
}
353
354
static int crl_revoked_issuer_match(X509_CRL *crl, X509_NAME *nm,
355
                                    X509_REVOKED *rev)
356
0
{
357
0
    int i;
358
0
359
0
    if (!rev->issuer) {
360
0
        if (!nm)
361
0
            return 1;
362
0
        if (!X509_NAME_cmp(nm, X509_CRL_get_issuer(crl)))
363
0
            return 1;
364
0
        return 0;
365
0
    }
366
0
367
0
    if (!nm)
368
0
        nm = X509_CRL_get_issuer(crl);
369
0
370
0
    for (i = 0; i < sk_GENERAL_NAME_num(rev->issuer); i++) {
371
0
        GENERAL_NAME *gen = sk_GENERAL_NAME_value(rev->issuer, i);
372
0
        if (gen->type != GEN_DIRNAME)
373
0
            continue;
374
0
        if (!X509_NAME_cmp(nm, gen->d.directoryName))
375
0
            return 1;
376
0
    }
377
0
    return 0;
378
0
379
0
}
380
381
static int def_crl_lookup(X509_CRL *crl,
382
                          X509_REVOKED **ret, ASN1_INTEGER *serial,
383
                          X509_NAME *issuer)
384
0
{
385
0
    X509_REVOKED rtmp, *rev;
386
0
    int idx, num;
387
0
388
0
    if (crl->crl.revoked == NULL)
389
0
        return 0;
390
0
391
0
    /*
392
0
     * Sort revoked into serial number order if not already sorted. Do this
393
0
     * under a lock to avoid race condition.
394
0
     */
395
0
    if (!sk_X509_REVOKED_is_sorted(crl->crl.revoked)) {
396
0
        CRYPTO_THREAD_write_lock(crl->lock);
397
0
        sk_X509_REVOKED_sort(crl->crl.revoked);
398
0
        CRYPTO_THREAD_unlock(crl->lock);
399
0
    }
400
0
    rtmp.serialNumber = *serial;
401
0
    idx = sk_X509_REVOKED_find(crl->crl.revoked, &rtmp);
402
0
    if (idx < 0)
403
0
        return 0;
404
0
    /* Need to look for matching name */
405
0
    for (num = sk_X509_REVOKED_num(crl->crl.revoked); idx < num; idx++) {
406
0
        rev = sk_X509_REVOKED_value(crl->crl.revoked, idx);
407
0
        if (ASN1_INTEGER_cmp(&rev->serialNumber, serial))
408
0
            return 0;
409
0
        if (crl_revoked_issuer_match(crl, issuer, rev)) {
410
0
            if (ret)
411
0
                *ret = rev;
412
0
            if (rev->reason == CRL_REASON_REMOVE_FROM_CRL)
413
0
                return 2;
414
0
            return 1;
415
0
        }
416
0
    }
417
0
    return 0;
418
0
}
419
420
void X509_CRL_set_default_method(const X509_CRL_METHOD *meth)
421
0
{
422
0
    if (meth == NULL)
423
0
        default_crl_method = &int_crl_meth;
424
0
    else
425
0
        default_crl_method = meth;
426
0
}
427
428
X509_CRL_METHOD *X509_CRL_METHOD_new(int (*crl_init) (X509_CRL *crl),
429
                                     int (*crl_free) (X509_CRL *crl),
430
                                     int (*crl_lookup) (X509_CRL *crl,
431
                                                        X509_REVOKED **ret,
432
                                                        ASN1_INTEGER *ser,
433
                                                        X509_NAME *issuer),
434
                                     int (*crl_verify) (X509_CRL *crl,
435
                                                        EVP_PKEY *pk))
436
0
{
437
0
    X509_CRL_METHOD *m = OPENSSL_malloc(sizeof(*m));
438
0
439
0
    if (m == NULL) {
440
0
        X509err(X509_F_X509_CRL_METHOD_NEW, ERR_R_MALLOC_FAILURE);
441
0
        return NULL;
442
0
    }
443
0
    m->crl_init = crl_init;
444
0
    m->crl_free = crl_free;
445
0
    m->crl_lookup = crl_lookup;
446
0
    m->crl_verify = crl_verify;
447
0
    m->flags = X509_CRL_METHOD_DYNAMIC;
448
0
    return m;
449
0
}
450
451
void X509_CRL_METHOD_free(X509_CRL_METHOD *m)
452
0
{
453
0
    if (m == NULL || !(m->flags & X509_CRL_METHOD_DYNAMIC))
454
0
        return;
455
0
    OPENSSL_free(m);
456
0
}
457
458
void X509_CRL_set_meth_data(X509_CRL *crl, void *dat)
459
0
{
460
0
    crl->meth_data = dat;
461
0
}
462
463
void *X509_CRL_get_meth_data(X509_CRL *crl)
464
0
{
465
0
    return crl->meth_data;
466
0
}