Coverage Report

Created: 2018-08-29 13:53

/src/openssl/crypto/x509v3/v3_conf.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 1999-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
/* extension creation utilities */
11
12
#include <stdio.h>
13
#include "internal/ctype.h"
14
#include "internal/cryptlib.h"
15
#include <openssl/conf.h>
16
#include <openssl/x509.h>
17
#include "internal/x509_int.h"
18
#include <openssl/x509v3.h>
19
20
static int v3_check_critical(const char **value);
21
static int v3_check_generic(const char **value);
22
static X509_EXTENSION *do_ext_nconf(CONF *conf, X509V3_CTX *ctx, int ext_nid,
23
                                    int crit, const char *value);
24
static X509_EXTENSION *v3_generic_extension(const char *ext, const char *value,
25
                                            int crit, int type,
26
                                            X509V3_CTX *ctx);
27
static char *conf_lhash_get_string(void *db, const char *section, const char *value);
28
static STACK_OF(CONF_VALUE) *conf_lhash_get_section(void *db, const char *section);
29
static X509_EXTENSION *do_ext_i2d(const X509V3_EXT_METHOD *method,
30
                                  int ext_nid, int crit, void *ext_struc);
31
static unsigned char *generic_asn1(const char *value, X509V3_CTX *ctx,
32
                                   long *ext_len);
33
/* CONF *conf:  Config file    */
34
/* char *name:  Name    */
35
/* char *value:  Value    */
36
X509_EXTENSION *X509V3_EXT_nconf(CONF *conf, X509V3_CTX *ctx, const char *name,
37
                                 const char *value)
38
0
{
39
0
    int crit;
40
0
    int ext_type;
41
0
    X509_EXTENSION *ret;
42
0
    crit = v3_check_critical(&value);
43
0
    if ((ext_type = v3_check_generic(&value)))
44
0
        return v3_generic_extension(name, value, crit, ext_type, ctx);
45
0
    ret = do_ext_nconf(conf, ctx, OBJ_sn2nid(name), crit, value);
46
0
    if (!ret) {
47
0
        X509V3err(X509V3_F_X509V3_EXT_NCONF, X509V3_R_ERROR_IN_EXTENSION);
48
0
        ERR_add_error_data(4, "name=", name, ", value=", value);
49
0
    }
50
0
    return ret;
51
0
}
52
53
/* CONF *conf:  Config file    */
54
/* char *value:  Value    */
55
X509_EXTENSION *X509V3_EXT_nconf_nid(CONF *conf, X509V3_CTX *ctx, int ext_nid,
56
                                     const char *value)
57
0
{
58
0
    int crit;
59
0
    int ext_type;
60
0
    crit = v3_check_critical(&value);
61
0
    if ((ext_type = v3_check_generic(&value)))
62
0
        return v3_generic_extension(OBJ_nid2sn(ext_nid),
63
0
                                    value, crit, ext_type, ctx);
64
0
    return do_ext_nconf(conf, ctx, ext_nid, crit, value);
65
0
}
66
67
/* CONF *conf:  Config file    */
68
/* char *value:  Value    */
69
static X509_EXTENSION *do_ext_nconf(CONF *conf, X509V3_CTX *ctx, int ext_nid,
70
                                    int crit, const char *value)
71
0
{
72
0
    const X509V3_EXT_METHOD *method;
73
0
    X509_EXTENSION *ext;
74
0
    STACK_OF(CONF_VALUE) *nval;
75
0
    void *ext_struc;
76
0
77
0
    if (ext_nid == NID_undef) {
78
0
        X509V3err(X509V3_F_DO_EXT_NCONF, X509V3_R_UNKNOWN_EXTENSION_NAME);
79
0
        return NULL;
80
0
    }
81
0
    if ((method = X509V3_EXT_get_nid(ext_nid)) == NULL) {
82
0
        X509V3err(X509V3_F_DO_EXT_NCONF, X509V3_R_UNKNOWN_EXTENSION);
83
0
        return NULL;
84
0
    }
85
0
    /* Now get internal extension representation based on type */
86
0
    if (method->v2i) {
87
0
        if (*value == '@')
88
0
            nval = NCONF_get_section(conf, value + 1);
89
0
        else
90
0
            nval = X509V3_parse_list(value);
91
0
        if (nval == NULL || sk_CONF_VALUE_num(nval) <= 0) {
92
0
            X509V3err(X509V3_F_DO_EXT_NCONF,
93
0
                      X509V3_R_INVALID_EXTENSION_STRING);
94
0
            ERR_add_error_data(4, "name=", OBJ_nid2sn(ext_nid), ",section=",
95
0
                               value);
96
0
            if (*value != '@')
97
0
                sk_CONF_VALUE_pop_free(nval, X509V3_conf_free);
98
0
            return NULL;
99
0
        }
100
0
        ext_struc = method->v2i(method, ctx, nval);
101
0
        if (*value != '@')
102
0
            sk_CONF_VALUE_pop_free(nval, X509V3_conf_free);
103
0
        if (!ext_struc)
104
0
            return NULL;
105
0
    } else if (method->s2i) {
106
0
        if ((ext_struc = method->s2i(method, ctx, value)) == NULL)
107
0
            return NULL;
108
0
    } else if (method->r2i) {
109
0
        if (!ctx->db || !ctx->db_meth) {
110
0
            X509V3err(X509V3_F_DO_EXT_NCONF, X509V3_R_NO_CONFIG_DATABASE);
111
0
            return NULL;
112
0
        }
113
0
        if ((ext_struc = method->r2i(method, ctx, value)) == NULL)
114
0
            return NULL;
115
0
    } else {
116
0
        X509V3err(X509V3_F_DO_EXT_NCONF,
117
0
                  X509V3_R_EXTENSION_SETTING_NOT_SUPPORTED);
118
0
        ERR_add_error_data(2, "name=", OBJ_nid2sn(ext_nid));
119
0
        return NULL;
120
0
    }
121
0
122
0
    ext = do_ext_i2d(method, ext_nid, crit, ext_struc);
123
0
    if (method->it)
124
0
        ASN1_item_free(ext_struc, ASN1_ITEM_ptr(method->it));
125
0
    else
126
0
        method->ext_free(ext_struc);
127
0
    return ext;
128
0
129
0
}
130
131
static X509_EXTENSION *do_ext_i2d(const X509V3_EXT_METHOD *method,
132
                                  int ext_nid, int crit, void *ext_struc)
133
0
{
134
0
    unsigned char *ext_der = NULL;
135
0
    int ext_len;
136
0
    ASN1_OCTET_STRING *ext_oct = NULL;
137
0
    X509_EXTENSION *ext;
138
0
    /* Convert internal representation to DER */
139
0
    if (method->it) {
140
0
        ext_der = NULL;
141
0
        ext_len =
142
0
            ASN1_item_i2d(ext_struc, &ext_der, ASN1_ITEM_ptr(method->it));
143
0
        if (ext_len < 0)
144
0
            goto merr;
145
0
    } else {
146
0
        unsigned char *p;
147
0
148
0
        ext_len = method->i2d(ext_struc, NULL);
149
0
        if ((ext_der = OPENSSL_malloc(ext_len)) == NULL)
150
0
            goto merr;
151
0
        p = ext_der;
152
0
        method->i2d(ext_struc, &p);
153
0
    }
154
0
    if ((ext_oct = ASN1_OCTET_STRING_new()) == NULL)
155
0
        goto merr;
156
0
    ext_oct->data = ext_der;
157
0
    ext_der = NULL;
158
0
    ext_oct->length = ext_len;
159
0
160
0
    ext = X509_EXTENSION_create_by_NID(NULL, ext_nid, crit, ext_oct);
161
0
    if (!ext)
162
0
        goto merr;
163
0
    ASN1_OCTET_STRING_free(ext_oct);
164
0
165
0
    return ext;
166
0
167
0
 merr:
168
0
    X509V3err(X509V3_F_DO_EXT_I2D, ERR_R_MALLOC_FAILURE);
169
0
    OPENSSL_free(ext_der);
170
0
    ASN1_OCTET_STRING_free(ext_oct);
171
0
    return NULL;
172
0
173
0
}
174
175
/* Given an internal structure, nid and critical flag create an extension */
176
177
X509_EXTENSION *X509V3_EXT_i2d(int ext_nid, int crit, void *ext_struc)
178
0
{
179
0
    const X509V3_EXT_METHOD *method;
180
0
181
0
    if ((method = X509V3_EXT_get_nid(ext_nid)) == NULL) {
182
0
        X509V3err(X509V3_F_X509V3_EXT_I2D, X509V3_R_UNKNOWN_EXTENSION);
183
0
        return NULL;
184
0
    }
185
0
    return do_ext_i2d(method, ext_nid, crit, ext_struc);
186
0
}
187
188
/* Check the extension string for critical flag */
189
static int v3_check_critical(const char **value)
190
{
191
    const char *p = *value;
192
    if ((strlen(p) < 9) || strncmp(p, "critical,", 9))
193
        return 0;
194
    p += 9;
195
    while (ossl_isspace(*p))
196
        p++;
197
    *value = p;
198
    return 1;
199
}
200
201
/* Check extension string for generic extension and return the type */
202
static int v3_check_generic(const char **value)
203
{
204
    int gen_type = 0;
205
    const char *p = *value;
206
    if ((strlen(p) >= 4) && strncmp(p, "DER:", 4) == 0) {
207
        p += 4;
208
        gen_type = 1;
209
    } else if ((strlen(p) >= 5) && strncmp(p, "ASN1:", 5) == 0) {
210
        p += 5;
211
        gen_type = 2;
212
    } else
213
        return 0;
214
215
    while (ossl_isspace(*p))
216
        p++;
217
    *value = p;
218
    return gen_type;
219
}
220
221
/* Create a generic extension: for now just handle DER type */
222
static X509_EXTENSION *v3_generic_extension(const char *ext, const char *value,
223
                                            int crit, int gen_type,
224
                                            X509V3_CTX *ctx)
225
0
{
226
0
    unsigned char *ext_der = NULL;
227
0
    long ext_len = 0;
228
0
    ASN1_OBJECT *obj = NULL;
229
0
    ASN1_OCTET_STRING *oct = NULL;
230
0
    X509_EXTENSION *extension = NULL;
231
0
232
0
    if ((obj = OBJ_txt2obj(ext, 0)) == NULL) {
233
0
        X509V3err(X509V3_F_V3_GENERIC_EXTENSION,
234
0
                  X509V3_R_EXTENSION_NAME_ERROR);
235
0
        ERR_add_error_data(2, "name=", ext);
236
0
        goto err;
237
0
    }
238
0
239
0
    if (gen_type == 1)
240
0
        ext_der = OPENSSL_hexstr2buf(value, &ext_len);
241
0
    else if (gen_type == 2)
242
0
        ext_der = generic_asn1(value, ctx, &ext_len);
243
0
244
0
    if (ext_der == NULL) {
245
0
        X509V3err(X509V3_F_V3_GENERIC_EXTENSION,
246
0
                  X509V3_R_EXTENSION_VALUE_ERROR);
247
0
        ERR_add_error_data(2, "value=", value);
248
0
        goto err;
249
0
    }
250
0
251
0
    if ((oct = ASN1_OCTET_STRING_new()) == NULL) {
252
0
        X509V3err(X509V3_F_V3_GENERIC_EXTENSION, ERR_R_MALLOC_FAILURE);
253
0
        goto err;
254
0
    }
255
0
256
0
    oct->data = ext_der;
257
0
    oct->length = ext_len;
258
0
    ext_der = NULL;
259
0
260
0
    extension = X509_EXTENSION_create_by_OBJ(NULL, obj, crit, oct);
261
0
262
0
 err:
263
0
    ASN1_OBJECT_free(obj);
264
0
    ASN1_OCTET_STRING_free(oct);
265
0
    OPENSSL_free(ext_der);
266
0
    return extension;
267
0
268
0
}
269
270
static unsigned char *generic_asn1(const char *value, X509V3_CTX *ctx,
271
                                   long *ext_len)
272
0
{
273
0
    ASN1_TYPE *typ;
274
0
    unsigned char *ext_der = NULL;
275
0
    typ = ASN1_generate_v3(value, ctx);
276
0
    if (typ == NULL)
277
0
        return NULL;
278
0
    *ext_len = i2d_ASN1_TYPE(typ, &ext_der);
279
0
    ASN1_TYPE_free(typ);
280
0
    return ext_der;
281
0
}
282
283
static void delete_ext(STACK_OF(X509_EXTENSION) *sk, X509_EXTENSION *dext)
284
0
{
285
0
    int idx;
286
0
    ASN1_OBJECT *obj;
287
0
    obj = X509_EXTENSION_get_object(dext);
288
0
    while ((idx = X509v3_get_ext_by_OBJ(sk, obj, -1)) >= 0) {
289
0
        X509_EXTENSION *tmpext = X509v3_get_ext(sk, idx);
290
0
        X509v3_delete_ext(sk, idx);
291
0
        X509_EXTENSION_free(tmpext);
292
0
    }
293
0
}
294
295
/*
296
 * This is the main function: add a bunch of extensions based on a config
297
 * file section to an extension STACK.
298
 */
299
300
int X509V3_EXT_add_nconf_sk(CONF *conf, X509V3_CTX *ctx, const char *section,
301
                            STACK_OF(X509_EXTENSION) **sk)
302
0
{
303
0
    X509_EXTENSION *ext;
304
0
    STACK_OF(CONF_VALUE) *nval;
305
0
    CONF_VALUE *val;
306
0
    int i;
307
0
308
0
    if ((nval = NCONF_get_section(conf, section)) == NULL)
309
0
        return 0;
310
0
    for (i = 0; i < sk_CONF_VALUE_num(nval); i++) {
311
0
        val = sk_CONF_VALUE_value(nval, i);
312
0
        if ((ext = X509V3_EXT_nconf(conf, ctx, val->name, val->value)) == NULL)
313
0
            return 0;
314
0
        if (ctx->flags == X509V3_CTX_REPLACE)
315
0
            delete_ext(*sk, ext);
316
0
        if (sk != NULL) {
317
0
            if (X509v3_add_ext(sk, ext, -1) == NULL) {
318
0
                X509_EXTENSION_free(ext);
319
0
                return 0;
320
0
            }
321
0
        }
322
0
        X509_EXTENSION_free(ext);
323
0
    }
324
0
    return 1;
325
0
}
326
327
/*
328
 * Convenience functions to add extensions to a certificate, CRL and request
329
 */
330
331
int X509V3_EXT_add_nconf(CONF *conf, X509V3_CTX *ctx, const char *section,
332
                         X509 *cert)
333
0
{
334
0
    STACK_OF(X509_EXTENSION) **sk = NULL;
335
0
    if (cert)
336
0
        sk = &cert->cert_info.extensions;
337
0
    return X509V3_EXT_add_nconf_sk(conf, ctx, section, sk);
338
0
}
339
340
/* Same as above but for a CRL */
341
342
int X509V3_EXT_CRL_add_nconf(CONF *conf, X509V3_CTX *ctx, const char *section,
343
                             X509_CRL *crl)
344
0
{
345
0
    STACK_OF(X509_EXTENSION) **sk = NULL;
346
0
    if (crl)
347
0
        sk = &crl->crl.extensions;
348
0
    return X509V3_EXT_add_nconf_sk(conf, ctx, section, sk);
349
0
}
350
351
/* Add extensions to certificate request */
352
353
int X509V3_EXT_REQ_add_nconf(CONF *conf, X509V3_CTX *ctx, const char *section,
354
                             X509_REQ *req)
355
0
{
356
0
    STACK_OF(X509_EXTENSION) *extlist = NULL, **sk = NULL;
357
0
    int i;
358
0
    if (req)
359
0
        sk = &extlist;
360
0
    i = X509V3_EXT_add_nconf_sk(conf, ctx, section, sk);
361
0
    if (!i || !sk)
362
0
        return i;
363
0
    i = X509_REQ_add_extensions(req, extlist);
364
0
    sk_X509_EXTENSION_pop_free(extlist, X509_EXTENSION_free);
365
0
    return i;
366
0
}
367
368
/* Config database functions */
369
370
char *X509V3_get_string(X509V3_CTX *ctx, const char *name, const char *section)
371
0
{
372
0
    if (!ctx->db || !ctx->db_meth || !ctx->db_meth->get_string) {
373
0
        X509V3err(X509V3_F_X509V3_GET_STRING, X509V3_R_OPERATION_NOT_DEFINED);
374
0
        return NULL;
375
0
    }
376
0
    if (ctx->db_meth->get_string)
377
0
        return ctx->db_meth->get_string(ctx->db, name, section);
378
0
    return NULL;
379
0
}
380
381
STACK_OF(CONF_VALUE) *X509V3_get_section(X509V3_CTX *ctx, const char *section)
382
0
{
383
0
    if (!ctx->db || !ctx->db_meth || !ctx->db_meth->get_section) {
384
0
        X509V3err(X509V3_F_X509V3_GET_SECTION,
385
0
                  X509V3_R_OPERATION_NOT_DEFINED);
386
0
        return NULL;
387
0
    }
388
0
    if (ctx->db_meth->get_section)
389
0
        return ctx->db_meth->get_section(ctx->db, section);
390
0
    return NULL;
391
0
}
392
393
void X509V3_string_free(X509V3_CTX *ctx, char *str)
394
0
{
395
0
    if (!str)
396
0
        return;
397
0
    if (ctx->db_meth->free_string)
398
0
        ctx->db_meth->free_string(ctx->db, str);
399
0
}
400
401
void X509V3_section_free(X509V3_CTX *ctx, STACK_OF(CONF_VALUE) *section)
402
0
{
403
0
    if (!section)
404
0
        return;
405
0
    if (ctx->db_meth->free_section)
406
0
        ctx->db_meth->free_section(ctx->db, section);
407
0
}
408
409
static char *nconf_get_string(void *db, const char *section, const char *value)
410
0
{
411
0
    return NCONF_get_string(db, section, value);
412
0
}
413
414
static STACK_OF(CONF_VALUE) *nconf_get_section(void *db, const char *section)
415
0
{
416
0
    return NCONF_get_section(db, section);
417
0
}
418
419
static X509V3_CONF_METHOD nconf_method = {
420
    nconf_get_string,
421
    nconf_get_section,
422
    NULL,
423
    NULL
424
};
425
426
void X509V3_set_nconf(X509V3_CTX *ctx, CONF *conf)
427
0
{
428
0
    ctx->db_meth = &nconf_method;
429
0
    ctx->db = conf;
430
0
}
431
432
void X509V3_set_ctx(X509V3_CTX *ctx, X509 *issuer, X509 *subj, X509_REQ *req,
433
                    X509_CRL *crl, int flags)
434
0
{
435
0
    ctx->issuer_cert = issuer;
436
0
    ctx->subject_cert = subj;
437
0
    ctx->crl = crl;
438
0
    ctx->subject_req = req;
439
0
    ctx->flags = flags;
440
0
}
441
442
/* Old conf compatibility functions */
443
444
X509_EXTENSION *X509V3_EXT_conf(LHASH_OF(CONF_VALUE) *conf, X509V3_CTX *ctx,
445
                                const char *name, const char *value)
446
0
{
447
0
    CONF ctmp;
448
0
    CONF_set_nconf(&ctmp, conf);
449
0
    return X509V3_EXT_nconf(&ctmp, ctx, name, value);
450
0
}
451
452
/* LHASH *conf:  Config file    */
453
/* char *value:  Value    */
454
X509_EXTENSION *X509V3_EXT_conf_nid(LHASH_OF(CONF_VALUE) *conf,
455
                                    X509V3_CTX *ctx, int ext_nid, const char *value)
456
0
{
457
0
    CONF ctmp;
458
0
    CONF_set_nconf(&ctmp, conf);
459
0
    return X509V3_EXT_nconf_nid(&ctmp, ctx, ext_nid, value);
460
0
}
461
462
static char *conf_lhash_get_string(void *db, const char *section, const char *value)
463
0
{
464
0
    return CONF_get_string(db, section, value);
465
0
}
466
467
static STACK_OF(CONF_VALUE) *conf_lhash_get_section(void *db, const char *section)
468
0
{
469
0
    return CONF_get_section(db, section);
470
0
}
471
472
static X509V3_CONF_METHOD conf_lhash_method = {
473
    conf_lhash_get_string,
474
    conf_lhash_get_section,
475
    NULL,
476
    NULL
477
};
478
479
void X509V3_set_conf_lhash(X509V3_CTX *ctx, LHASH_OF(CONF_VALUE) *lhash)
480
0
{
481
0
    ctx->db_meth = &conf_lhash_method;
482
0
    ctx->db = lhash;
483
0
}
484
485
int X509V3_EXT_add_conf(LHASH_OF(CONF_VALUE) *conf, X509V3_CTX *ctx,
486
                        const char *section, X509 *cert)
487
0
{
488
0
    CONF ctmp;
489
0
    CONF_set_nconf(&ctmp, conf);
490
0
    return X509V3_EXT_add_nconf(&ctmp, ctx, section, cert);
491
0
}
492
493
/* Same as above but for a CRL */
494
495
int X509V3_EXT_CRL_add_conf(LHASH_OF(CONF_VALUE) *conf, X509V3_CTX *ctx,
496
                            const char *section, X509_CRL *crl)
497
0
{
498
0
    CONF ctmp;
499
0
    CONF_set_nconf(&ctmp, conf);
500
0
    return X509V3_EXT_CRL_add_nconf(&ctmp, ctx, section, crl);
501
0
}
502
503
/* Add extensions to certificate request */
504
505
int X509V3_EXT_REQ_add_conf(LHASH_OF(CONF_VALUE) *conf, X509V3_CTX *ctx,
506
                            const char *section, X509_REQ *req)
507
0
{
508
0
    CONF ctmp;
509
0
    CONF_set_nconf(&ctmp, conf);
510
0
    return X509V3_EXT_REQ_add_nconf(&ctmp, ctx, section, req);
511
0
}