Coverage Report

Created: 2018-08-29 13:53

/src/openssl/crypto/x509v3/v3_cpols.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
#include <stdio.h>
11
#include "internal/cryptlib.h"
12
#include <openssl/conf.h>
13
#include <openssl/asn1.h>
14
#include <openssl/asn1t.h>
15
#include <openssl/x509v3.h>
16
17
#include "pcy_int.h"
18
#include "ext_dat.h"
19
20
/* Certificate policies extension support: this one is a bit complex... */
21
22
static int i2r_certpol(X509V3_EXT_METHOD *method, STACK_OF(POLICYINFO) *pol,
23
                       BIO *out, int indent);
24
static STACK_OF(POLICYINFO) *r2i_certpol(X509V3_EXT_METHOD *method,
25
                                         X509V3_CTX *ctx, const char *value);
26
static void print_qualifiers(BIO *out, STACK_OF(POLICYQUALINFO) *quals,
27
                             int indent);
28
static void print_notice(BIO *out, USERNOTICE *notice, int indent);
29
static POLICYINFO *policy_section(X509V3_CTX *ctx,
30
                                  STACK_OF(CONF_VALUE) *polstrs, int ia5org);
31
static POLICYQUALINFO *notice_section(X509V3_CTX *ctx,
32
                                      STACK_OF(CONF_VALUE) *unot, int ia5org);
33
static int nref_nos(STACK_OF(ASN1_INTEGER) *nnums, STACK_OF(CONF_VALUE) *nos);
34
static int displaytext_str2tag(const char *tagstr, unsigned int *tag_len);
35
static int displaytext_get_tag_len(const char *tagstr);
36
37
const X509V3_EXT_METHOD v3_cpols = {
38
    NID_certificate_policies, 0, ASN1_ITEM_ref(CERTIFICATEPOLICIES),
39
    0, 0, 0, 0,
40
    0, 0,
41
    0, 0,
42
    (X509V3_EXT_I2R)i2r_certpol,
43
    (X509V3_EXT_R2I)r2i_certpol,
44
    NULL
45
};
46
47
ASN1_ITEM_TEMPLATE(CERTIFICATEPOLICIES) =
48
        ASN1_EX_TEMPLATE_TYPE(ASN1_TFLG_SEQUENCE_OF, 0, CERTIFICATEPOLICIES, POLICYINFO)
49
ASN1_ITEM_TEMPLATE_END(CERTIFICATEPOLICIES)
50
51
IMPLEMENT_ASN1_FUNCTIONS(CERTIFICATEPOLICIES)
52
53
ASN1_SEQUENCE(POLICYINFO) = {
54
        ASN1_SIMPLE(POLICYINFO, policyid, ASN1_OBJECT),
55
        ASN1_SEQUENCE_OF_OPT(POLICYINFO, qualifiers, POLICYQUALINFO)
56
} ASN1_SEQUENCE_END(POLICYINFO)
57
58
IMPLEMENT_ASN1_FUNCTIONS(POLICYINFO)
59
60
ASN1_ADB_TEMPLATE(policydefault) = ASN1_SIMPLE(POLICYQUALINFO, d.other, ASN1_ANY);
61
62
ASN1_ADB(POLICYQUALINFO) = {
63
        ADB_ENTRY(NID_id_qt_cps, ASN1_SIMPLE(POLICYQUALINFO, d.cpsuri, ASN1_IA5STRING)),
64
        ADB_ENTRY(NID_id_qt_unotice, ASN1_SIMPLE(POLICYQUALINFO, d.usernotice, USERNOTICE))
65
} ASN1_ADB_END(POLICYQUALINFO, 0, pqualid, 0, &policydefault_tt, NULL);
66
67
ASN1_SEQUENCE(POLICYQUALINFO) = {
68
        ASN1_SIMPLE(POLICYQUALINFO, pqualid, ASN1_OBJECT),
69
        ASN1_ADB_OBJECT(POLICYQUALINFO)
70
} ASN1_SEQUENCE_END(POLICYQUALINFO)
71
72
IMPLEMENT_ASN1_FUNCTIONS(POLICYQUALINFO)
73
74
ASN1_SEQUENCE(USERNOTICE) = {
75
        ASN1_OPT(USERNOTICE, noticeref, NOTICEREF),
76
        ASN1_OPT(USERNOTICE, exptext, DISPLAYTEXT)
77
} ASN1_SEQUENCE_END(USERNOTICE)
78
79
IMPLEMENT_ASN1_FUNCTIONS(USERNOTICE)
80
81
ASN1_SEQUENCE(NOTICEREF) = {
82
        ASN1_SIMPLE(NOTICEREF, organization, DISPLAYTEXT),
83
        ASN1_SEQUENCE_OF(NOTICEREF, noticenos, ASN1_INTEGER)
84
} ASN1_SEQUENCE_END(NOTICEREF)
85
86
IMPLEMENT_ASN1_FUNCTIONS(NOTICEREF)
87
88
static STACK_OF(POLICYINFO) *r2i_certpol(X509V3_EXT_METHOD *method,
89
                                         X509V3_CTX *ctx, const char *value)
90
0
{
91
0
    STACK_OF(POLICYINFO) *pols;
92
0
    char *pstr;
93
0
    POLICYINFO *pol;
94
0
    ASN1_OBJECT *pobj;
95
0
    STACK_OF(CONF_VALUE) *vals = X509V3_parse_list(value);
96
0
    CONF_VALUE *cnf;
97
0
    const int num = sk_CONF_VALUE_num(vals);
98
0
    int i, ia5org;
99
0
100
0
    if (vals == NULL) {
101
0
        X509V3err(X509V3_F_R2I_CERTPOL, ERR_R_X509V3_LIB);
102
0
        return NULL;
103
0
    }
104
0
105
0
    pols = sk_POLICYINFO_new_reserve(NULL, num);
106
0
    if (pols == NULL) {
107
0
        X509V3err(X509V3_F_R2I_CERTPOL, ERR_R_MALLOC_FAILURE);
108
0
        goto err;
109
0
    }
110
0
111
0
    ia5org = 0;
112
0
    for (i = 0; i < num; i++) {
113
0
        cnf = sk_CONF_VALUE_value(vals, i);
114
0
115
0
        if (cnf->value || !cnf->name) {
116
0
            X509V3err(X509V3_F_R2I_CERTPOL,
117
0
                      X509V3_R_INVALID_POLICY_IDENTIFIER);
118
0
            X509V3_conf_err(cnf);
119
0
            goto err;
120
0
        }
121
0
        pstr = cnf->name;
122
0
        if (strcmp(pstr, "ia5org") == 0) {
123
0
            ia5org = 1;
124
0
            continue;
125
0
        } else if (*pstr == '@') {
126
0
            STACK_OF(CONF_VALUE) *polsect;
127
0
            polsect = X509V3_get_section(ctx, pstr + 1);
128
0
            if (!polsect) {
129
0
                X509V3err(X509V3_F_R2I_CERTPOL, X509V3_R_INVALID_SECTION);
130
0
131
0
                X509V3_conf_err(cnf);
132
0
                goto err;
133
0
            }
134
0
            pol = policy_section(ctx, polsect, ia5org);
135
0
            X509V3_section_free(ctx, polsect);
136
0
            if (pol == NULL)
137
0
                goto err;
138
0
        } else {
139
0
            if ((pobj = OBJ_txt2obj(cnf->name, 0)) == NULL) {
140
0
                X509V3err(X509V3_F_R2I_CERTPOL,
141
0
                          X509V3_R_INVALID_OBJECT_IDENTIFIER);
142
0
                X509V3_conf_err(cnf);
143
0
                goto err;
144
0
            }
145
0
            pol = POLICYINFO_new();
146
0
            if (pol == NULL) {
147
0
                ASN1_OBJECT_free(pobj);
148
0
                X509V3err(X509V3_F_R2I_CERTPOL, ERR_R_MALLOC_FAILURE);
149
0
                goto err;
150
0
            }
151
0
            pol->policyid = pobj;
152
0
        }
153
0
        if (!sk_POLICYINFO_push(pols, pol)) {
154
0
            POLICYINFO_free(pol);
155
0
            X509V3err(X509V3_F_R2I_CERTPOL, ERR_R_MALLOC_FAILURE);
156
0
            goto err;
157
0
        }
158
0
    }
159
0
    sk_CONF_VALUE_pop_free(vals, X509V3_conf_free);
160
0
    return pols;
161
0
 err:
162
0
    sk_CONF_VALUE_pop_free(vals, X509V3_conf_free);
163
0
    sk_POLICYINFO_pop_free(pols, POLICYINFO_free);
164
0
    return NULL;
165
0
}
166
167
static POLICYINFO *policy_section(X509V3_CTX *ctx,
168
                                  STACK_OF(CONF_VALUE) *polstrs, int ia5org)
169
0
{
170
0
    int i;
171
0
    CONF_VALUE *cnf;
172
0
    POLICYINFO *pol;
173
0
    POLICYQUALINFO *qual;
174
0
175
0
    if ((pol = POLICYINFO_new()) == NULL)
176
0
        goto merr;
177
0
    for (i = 0; i < sk_CONF_VALUE_num(polstrs); i++) {
178
0
        cnf = sk_CONF_VALUE_value(polstrs, i);
179
0
        if (strcmp(cnf->name, "policyIdentifier") == 0) {
180
0
            ASN1_OBJECT *pobj;
181
0
            if ((pobj = OBJ_txt2obj(cnf->value, 0)) == NULL) {
182
0
                X509V3err(X509V3_F_POLICY_SECTION,
183
0
                          X509V3_R_INVALID_OBJECT_IDENTIFIER);
184
0
                X509V3_conf_err(cnf);
185
0
                goto err;
186
0
            }
187
0
            pol->policyid = pobj;
188
0
189
0
        } else if (!name_cmp(cnf->name, "CPS")) {
190
0
            if (pol->qualifiers == NULL)
191
0
                pol->qualifiers = sk_POLICYQUALINFO_new_null();
192
0
            if ((qual = POLICYQUALINFO_new()) == NULL)
193
0
                goto merr;
194
0
            if (!sk_POLICYQUALINFO_push(pol->qualifiers, qual))
195
0
                goto merr;
196
0
            if ((qual->pqualid = OBJ_nid2obj(NID_id_qt_cps)) == NULL) {
197
0
                X509V3err(X509V3_F_POLICY_SECTION, ERR_R_INTERNAL_ERROR);
198
0
                goto err;
199
0
            }
200
0
            if ((qual->d.cpsuri = ASN1_IA5STRING_new()) == NULL)
201
0
                goto merr;
202
0
            if (!ASN1_STRING_set(qual->d.cpsuri, cnf->value,
203
0
                                 strlen(cnf->value)))
204
0
                goto merr;
205
0
        } else if (!name_cmp(cnf->name, "userNotice")) {
206
0
            STACK_OF(CONF_VALUE) *unot;
207
0
            if (*cnf->value != '@') {
208
0
                X509V3err(X509V3_F_POLICY_SECTION,
209
0
                          X509V3_R_EXPECTED_A_SECTION_NAME);
210
0
                X509V3_conf_err(cnf);
211
0
                goto err;
212
0
            }
213
0
            unot = X509V3_get_section(ctx, cnf->value + 1);
214
0
            if (!unot) {
215
0
                X509V3err(X509V3_F_POLICY_SECTION, X509V3_R_INVALID_SECTION);
216
0
217
0
                X509V3_conf_err(cnf);
218
0
                goto err;
219
0
            }
220
0
            qual = notice_section(ctx, unot, ia5org);
221
0
            X509V3_section_free(ctx, unot);
222
0
            if (!qual)
223
0
                goto err;
224
0
            if (!pol->qualifiers)
225
0
                pol->qualifiers = sk_POLICYQUALINFO_new_null();
226
0
            if (!sk_POLICYQUALINFO_push(pol->qualifiers, qual))
227
0
                goto merr;
228
0
        } else {
229
0
            X509V3err(X509V3_F_POLICY_SECTION, X509V3_R_INVALID_OPTION);
230
0
231
0
            X509V3_conf_err(cnf);
232
0
            goto err;
233
0
        }
234
0
    }
235
0
    if (!pol->policyid) {
236
0
        X509V3err(X509V3_F_POLICY_SECTION, X509V3_R_NO_POLICY_IDENTIFIER);
237
0
        goto err;
238
0
    }
239
0
240
0
    return pol;
241
0
242
0
 merr:
243
0
    X509V3err(X509V3_F_POLICY_SECTION, ERR_R_MALLOC_FAILURE);
244
0
245
0
 err:
246
0
    POLICYINFO_free(pol);
247
0
    return NULL;
248
0
}
249
250
static int displaytext_get_tag_len(const char *tagstr)
251
{
252
    char *colon = strchr(tagstr, ':');
253
254
    return (colon == NULL) ? -1 : colon - tagstr;
255
}
256
257
static int displaytext_str2tag(const char *tagstr, unsigned int *tag_len)
258
{
259
    int len;
260
261
    *tag_len = 0;
262
    len = displaytext_get_tag_len(tagstr);
263
264
    if (len == -1)
265
        return V_ASN1_VISIBLESTRING;
266
    *tag_len = len;
267
    if (len == sizeof("UTF8") - 1 && strncmp(tagstr, "UTF8", len) == 0)
268
        return V_ASN1_UTF8STRING;
269
    if (len == sizeof("UTF8String") - 1 && strncmp(tagstr, "UTF8String", len) == 0)
270
        return V_ASN1_UTF8STRING;
271
    if (len == sizeof("BMP") - 1 && strncmp(tagstr, "BMP", len) == 0)
272
        return V_ASN1_BMPSTRING;
273
    if (len == sizeof("BMPSTRING") - 1 && strncmp(tagstr, "BMPSTRING", len) == 0)
274
        return V_ASN1_BMPSTRING;
275
    if (len == sizeof("VISIBLE") - 1 && strncmp(tagstr, "VISIBLE", len) == 0)
276
        return V_ASN1_VISIBLESTRING;
277
    if (len == sizeof("VISIBLESTRING") - 1 && strncmp(tagstr, "VISIBLESTRING", len) == 0)
278
        return V_ASN1_VISIBLESTRING;
279
    *tag_len = 0;
280
    return V_ASN1_VISIBLESTRING;
281
}
282
283
static POLICYQUALINFO *notice_section(X509V3_CTX *ctx,
284
                                      STACK_OF(CONF_VALUE) *unot, int ia5org)
285
0
{
286
0
    int i, ret, len, tag;
287
0
    unsigned int tag_len;
288
0
    CONF_VALUE *cnf;
289
0
    USERNOTICE *not;
290
0
    POLICYQUALINFO *qual;
291
0
    char *value = NULL;
292
0
293
0
    if ((qual = POLICYQUALINFO_new()) == NULL)
294
0
        goto merr;
295
0
    if ((qual->pqualid = OBJ_nid2obj(NID_id_qt_unotice)) == NULL) {
296
0
        X509V3err(X509V3_F_NOTICE_SECTION, ERR_R_INTERNAL_ERROR);
297
0
        goto err;
298
0
    }
299
0
    if ((not = USERNOTICE_new()) == NULL)
300
0
        goto merr;
301
0
    qual->d.usernotice = not;
302
0
    for (i = 0; i < sk_CONF_VALUE_num(unot); i++) {
303
0
        cnf = sk_CONF_VALUE_value(unot, i);
304
0
        value = cnf->value;
305
0
        if (strcmp(cnf->name, "explicitText") == 0) {
306
0
            tag = displaytext_str2tag(value, &tag_len);
307
0
            if ((not->exptext = ASN1_STRING_type_new(tag)) == NULL)
308
0
                goto merr;
309
0
            if (tag_len != 0)
310
0
                value += tag_len + 1;
311
0
            len = strlen(value);
312
0
            if (!ASN1_STRING_set(not->exptext, value, len))
313
0
                goto merr;
314
0
        } else if (strcmp(cnf->name, "organization") == 0) {
315
0
            NOTICEREF *nref;
316
0
            if (!not->noticeref) {
317
0
                if ((nref = NOTICEREF_new()) == NULL)
318
0
                    goto merr;
319
0
                not->noticeref = nref;
320
0
            } else
321
0
                nref = not->noticeref;
322
0
            if (ia5org)
323
0
                nref->organization->type = V_ASN1_IA5STRING;
324
0
            else
325
0
                nref->organization->type = V_ASN1_VISIBLESTRING;
326
0
            if (!ASN1_STRING_set(nref->organization, cnf->value,
327
0
                                 strlen(cnf->value)))
328
0
                goto merr;
329
0
        } else if (strcmp(cnf->name, "noticeNumbers") == 0) {
330
0
            NOTICEREF *nref;
331
0
            STACK_OF(CONF_VALUE) *nos;
332
0
            if (!not->noticeref) {
333
0
                if ((nref = NOTICEREF_new()) == NULL)
334
0
                    goto merr;
335
0
                not->noticeref = nref;
336
0
            } else
337
0
                nref = not->noticeref;
338
0
            nos = X509V3_parse_list(cnf->value);
339
0
            if (!nos || !sk_CONF_VALUE_num(nos)) {
340
0
                X509V3err(X509V3_F_NOTICE_SECTION, X509V3_R_INVALID_NUMBERS);
341
0
                X509V3_conf_err(cnf);
342
0
                sk_CONF_VALUE_pop_free(nos, X509V3_conf_free);
343
0
                goto err;
344
0
            }
345
0
            ret = nref_nos(nref->noticenos, nos);
346
0
            sk_CONF_VALUE_pop_free(nos, X509V3_conf_free);
347
0
            if (!ret)
348
0
                goto err;
349
0
        } else {
350
0
            X509V3err(X509V3_F_NOTICE_SECTION, X509V3_R_INVALID_OPTION);
351
0
            X509V3_conf_err(cnf);
352
0
            goto err;
353
0
        }
354
0
    }
355
0
356
0
    if (not->noticeref &&
357
0
        (!not->noticeref->noticenos || !not->noticeref->organization)) {
358
0
        X509V3err(X509V3_F_NOTICE_SECTION,
359
0
                  X509V3_R_NEED_ORGANIZATION_AND_NUMBERS);
360
0
        goto err;
361
0
    }
362
0
363
0
    return qual;
364
0
365
0
 merr:
366
0
    X509V3err(X509V3_F_NOTICE_SECTION, ERR_R_MALLOC_FAILURE);
367
0
368
0
 err:
369
0
    POLICYQUALINFO_free(qual);
370
0
    return NULL;
371
0
}
372
373
static int nref_nos(STACK_OF(ASN1_INTEGER) *nnums, STACK_OF(CONF_VALUE) *nos)
374
0
{
375
0
    CONF_VALUE *cnf;
376
0
    ASN1_INTEGER *aint;
377
0
378
0
    int i;
379
0
380
0
    for (i = 0; i < sk_CONF_VALUE_num(nos); i++) {
381
0
        cnf = sk_CONF_VALUE_value(nos, i);
382
0
        if ((aint = s2i_ASN1_INTEGER(NULL, cnf->name)) == NULL) {
383
0
            X509V3err(X509V3_F_NREF_NOS, X509V3_R_INVALID_NUMBER);
384
0
            goto err;
385
0
        }
386
0
        if (!sk_ASN1_INTEGER_push(nnums, aint))
387
0
            goto merr;
388
0
    }
389
0
    return 1;
390
0
391
0
 merr:
392
0
    ASN1_INTEGER_free(aint);
393
0
    X509V3err(X509V3_F_NREF_NOS, ERR_R_MALLOC_FAILURE);
394
0
395
0
 err:
396
0
    return 0;
397
0
}
398
399
static int i2r_certpol(X509V3_EXT_METHOD *method, STACK_OF(POLICYINFO) *pol,
400
                       BIO *out, int indent)
401
0
{
402
0
    int i;
403
0
    POLICYINFO *pinfo;
404
0
    /* First print out the policy OIDs */
405
0
    for (i = 0; i < sk_POLICYINFO_num(pol); i++) {
406
0
        pinfo = sk_POLICYINFO_value(pol, i);
407
0
        BIO_printf(out, "%*sPolicy: ", indent, "");
408
0
        i2a_ASN1_OBJECT(out, pinfo->policyid);
409
0
        BIO_puts(out, "\n");
410
0
        if (pinfo->qualifiers)
411
0
            print_qualifiers(out, pinfo->qualifiers, indent + 2);
412
0
    }
413
0
    return 1;
414
0
}
415
416
static void print_qualifiers(BIO *out, STACK_OF(POLICYQUALINFO) *quals,
417
                             int indent)
418
0
{
419
0
    POLICYQUALINFO *qualinfo;
420
0
    int i;
421
0
    for (i = 0; i < sk_POLICYQUALINFO_num(quals); i++) {
422
0
        qualinfo = sk_POLICYQUALINFO_value(quals, i);
423
0
        switch (OBJ_obj2nid(qualinfo->pqualid)) {
424
0
        case NID_id_qt_cps:
425
0
            BIO_printf(out, "%*sCPS: %s\n", indent, "",
426
0
                       qualinfo->d.cpsuri->data);
427
0
            break;
428
0
429
0
        case NID_id_qt_unotice:
430
0
            BIO_printf(out, "%*sUser Notice:\n", indent, "");
431
0
            print_notice(out, qualinfo->d.usernotice, indent + 2);
432
0
            break;
433
0
434
0
        default:
435
0
            BIO_printf(out, "%*sUnknown Qualifier: ", indent + 2, "");
436
0
437
0
            i2a_ASN1_OBJECT(out, qualinfo->pqualid);
438
0
            BIO_puts(out, "\n");
439
0
            break;
440
0
        }
441
0
    }
442
0
}
443
444
static void print_notice(BIO *out, USERNOTICE *notice, int indent)
445
0
{
446
0
    int i;
447
0
    if (notice->noticeref) {
448
0
        NOTICEREF *ref;
449
0
        ref = notice->noticeref;
450
0
        BIO_printf(out, "%*sOrganization: %s\n", indent, "",
451
0
                   ref->organization->data);
452
0
        BIO_printf(out, "%*sNumber%s: ", indent, "",
453
0
                   sk_ASN1_INTEGER_num(ref->noticenos) > 1 ? "s" : "");
454
0
        for (i = 0; i < sk_ASN1_INTEGER_num(ref->noticenos); i++) {
455
0
            ASN1_INTEGER *num;
456
0
            char *tmp;
457
0
            num = sk_ASN1_INTEGER_value(ref->noticenos, i);
458
0
            if (i)
459
0
                BIO_puts(out, ", ");
460
0
            if (num == NULL)
461
0
                BIO_puts(out, "(null)");
462
0
            else {
463
0
                tmp = i2s_ASN1_INTEGER(NULL, num);
464
0
                if (tmp == NULL)
465
0
                    return;
466
0
                BIO_puts(out, tmp);
467
0
                OPENSSL_free(tmp);
468
0
            }
469
0
        }
470
0
        BIO_puts(out, "\n");
471
0
    }
472
0
    if (notice->exptext)
473
0
        BIO_printf(out, "%*sExplicit Text: %s\n", indent, "",
474
0
                   notice->exptext->data);
475
0
}
476
477
void X509_POLICY_NODE_print(BIO *out, X509_POLICY_NODE *node, int indent)
478
0
{
479
0
    const X509_POLICY_DATA *dat = node->data;
480
0
481
0
    BIO_printf(out, "%*sPolicy: ", indent, "");
482
0
483
0
    i2a_ASN1_OBJECT(out, dat->valid_policy);
484
0
    BIO_puts(out, "\n");
485
0
    BIO_printf(out, "%*s%s\n", indent + 2, "",
486
0
               node_data_critical(dat) ? "Critical" : "Non Critical");
487
0
    if (dat->qualifier_set)
488
0
        print_qualifiers(out, dat->qualifier_set, indent + 2);
489
0
    else
490
0
        BIO_printf(out, "%*sNo Qualifiers\n", indent + 2, "");
491
0
}