Coverage Report

Created: 2022-11-30 06:20

/src/openssl/crypto/x509v3/v3_cpols.c
Line
Count
Source (jump to first uncovered line)
1
/* v3_cpols.c */
2
/*
3
 * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL project
4
 * 1999.
5
 */
6
/* ====================================================================
7
 * Copyright (c) 1999-2004 The OpenSSL Project.  All rights reserved.
8
 *
9
 * Redistribution and use in source and binary forms, with or without
10
 * modification, are permitted provided that the following conditions
11
 * are met:
12
 *
13
 * 1. Redistributions of source code must retain the above copyright
14
 *    notice, this list of conditions and the following disclaimer.
15
 *
16
 * 2. Redistributions in binary form must reproduce the above copyright
17
 *    notice, this list of conditions and the following disclaimer in
18
 *    the documentation and/or other materials provided with the
19
 *    distribution.
20
 *
21
 * 3. All advertising materials mentioning features or use of this
22
 *    software must display the following acknowledgment:
23
 *    "This product includes software developed by the OpenSSL Project
24
 *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
25
 *
26
 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
27
 *    endorse or promote products derived from this software without
28
 *    prior written permission. For written permission, please contact
29
 *    licensing@OpenSSL.org.
30
 *
31
 * 5. Products derived from this software may not be called "OpenSSL"
32
 *    nor may "OpenSSL" appear in their names without prior written
33
 *    permission of the OpenSSL Project.
34
 *
35
 * 6. Redistributions of any form whatsoever must retain the following
36
 *    acknowledgment:
37
 *    "This product includes software developed by the OpenSSL Project
38
 *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
39
 *
40
 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
41
 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
43
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
44
 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
45
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
46
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
47
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
49
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
50
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
51
 * OF THE POSSIBILITY OF SUCH DAMAGE.
52
 * ====================================================================
53
 *
54
 * This product includes cryptographic software written by Eric Young
55
 * (eay@cryptsoft.com).  This product includes software written by Tim
56
 * Hudson (tjh@cryptsoft.com).
57
 *
58
 */
59
60
#include <stdio.h>
61
#include "cryptlib.h"
62
#include <openssl/conf.h>
63
#include <openssl/asn1.h>
64
#include <openssl/asn1t.h>
65
#include <openssl/x509v3.h>
66
67
#include "pcy_int.h"
68
69
/* Certificate policies extension support: this one is a bit complex... */
70
71
static int i2r_certpol(X509V3_EXT_METHOD *method, STACK_OF(POLICYINFO) *pol,
72
                       BIO *out, int indent);
73
static STACK_OF(POLICYINFO) *r2i_certpol(X509V3_EXT_METHOD *method,
74
                                         X509V3_CTX *ctx, char *value);
75
static void print_qualifiers(BIO *out, STACK_OF(POLICYQUALINFO) *quals,
76
                             int indent);
77
static void print_notice(BIO *out, USERNOTICE *notice, int indent);
78
static POLICYINFO *policy_section(X509V3_CTX *ctx,
79
                                  STACK_OF(CONF_VALUE) *polstrs, int ia5org);
80
static POLICYQUALINFO *notice_section(X509V3_CTX *ctx,
81
                                      STACK_OF(CONF_VALUE) *unot, int ia5org);
82
static int nref_nos(STACK_OF(ASN1_INTEGER) *nnums, STACK_OF(CONF_VALUE) *nos);
83
84
const X509V3_EXT_METHOD v3_cpols = {
85
    NID_certificate_policies, 0, ASN1_ITEM_ref(CERTIFICATEPOLICIES),
86
    0, 0, 0, 0,
87
    0, 0,
88
    0, 0,
89
    (X509V3_EXT_I2R)i2r_certpol,
90
    (X509V3_EXT_R2I)r2i_certpol,
91
    NULL
92
};
93
94
ASN1_ITEM_TEMPLATE(CERTIFICATEPOLICIES) =
95
        ASN1_EX_TEMPLATE_TYPE(ASN1_TFLG_SEQUENCE_OF, 0, CERTIFICATEPOLICIES, POLICYINFO)
96
ASN1_ITEM_TEMPLATE_END(CERTIFICATEPOLICIES)
97
98
IMPLEMENT_ASN1_FUNCTIONS(CERTIFICATEPOLICIES)
99
100
ASN1_SEQUENCE(POLICYINFO) = {
101
        ASN1_SIMPLE(POLICYINFO, policyid, ASN1_OBJECT),
102
        ASN1_SEQUENCE_OF_OPT(POLICYINFO, qualifiers, POLICYQUALINFO)
103
} ASN1_SEQUENCE_END(POLICYINFO)
104
105
IMPLEMENT_ASN1_FUNCTIONS(POLICYINFO)
106
107
ASN1_ADB_TEMPLATE(policydefault) = ASN1_SIMPLE(POLICYQUALINFO, d.other, ASN1_ANY);
108
109
ASN1_ADB(POLICYQUALINFO) = {
110
        ADB_ENTRY(NID_id_qt_cps, ASN1_SIMPLE(POLICYQUALINFO, d.cpsuri, ASN1_IA5STRING)),
111
        ADB_ENTRY(NID_id_qt_unotice, ASN1_SIMPLE(POLICYQUALINFO, d.usernotice, USERNOTICE))
112
} ASN1_ADB_END(POLICYQUALINFO, 0, pqualid, 0, &policydefault_tt, NULL);
113
114
ASN1_SEQUENCE(POLICYQUALINFO) = {
115
        ASN1_SIMPLE(POLICYQUALINFO, pqualid, ASN1_OBJECT),
116
        ASN1_ADB_OBJECT(POLICYQUALINFO)
117
} ASN1_SEQUENCE_END(POLICYQUALINFO)
118
119
IMPLEMENT_ASN1_FUNCTIONS(POLICYQUALINFO)
120
121
ASN1_SEQUENCE(USERNOTICE) = {
122
        ASN1_OPT(USERNOTICE, noticeref, NOTICEREF),
123
        ASN1_OPT(USERNOTICE, exptext, DISPLAYTEXT)
124
} ASN1_SEQUENCE_END(USERNOTICE)
125
126
IMPLEMENT_ASN1_FUNCTIONS(USERNOTICE)
127
128
ASN1_SEQUENCE(NOTICEREF) = {
129
        ASN1_SIMPLE(NOTICEREF, organization, DISPLAYTEXT),
130
        ASN1_SEQUENCE_OF(NOTICEREF, noticenos, ASN1_INTEGER)
131
} ASN1_SEQUENCE_END(NOTICEREF)
132
133
IMPLEMENT_ASN1_FUNCTIONS(NOTICEREF)
134
135
static STACK_OF(POLICYINFO) *r2i_certpol(X509V3_EXT_METHOD *method,
136
                                         X509V3_CTX *ctx, char *value)
137
0
{
138
0
    STACK_OF(POLICYINFO) *pols = NULL;
139
0
    char *pstr;
140
0
    POLICYINFO *pol;
141
0
    ASN1_OBJECT *pobj;
142
0
    STACK_OF(CONF_VALUE) *vals;
143
0
    CONF_VALUE *cnf;
144
0
    int i, ia5org;
145
0
    pols = sk_POLICYINFO_new_null();
146
0
    if (pols == NULL) {
147
0
        X509V3err(X509V3_F_R2I_CERTPOL, ERR_R_MALLOC_FAILURE);
148
0
        return NULL;
149
0
    }
150
0
    vals = X509V3_parse_list(value);
151
0
    if (vals == NULL) {
152
0
        X509V3err(X509V3_F_R2I_CERTPOL, ERR_R_X509V3_LIB);
153
0
        goto err;
154
0
    }
155
0
    ia5org = 0;
156
0
    for (i = 0; i < sk_CONF_VALUE_num(vals); i++) {
157
0
        cnf = sk_CONF_VALUE_value(vals, i);
158
0
        if (cnf->value || !cnf->name) {
159
0
            X509V3err(X509V3_F_R2I_CERTPOL,
160
0
                      X509V3_R_INVALID_POLICY_IDENTIFIER);
161
0
            X509V3_conf_err(cnf);
162
0
            goto err;
163
0
        }
164
0
        pstr = cnf->name;
165
0
        if (!strcmp(pstr, "ia5org")) {
166
0
            ia5org = 1;
167
0
            continue;
168
0
        } else if (*pstr == '@') {
169
0
            STACK_OF(CONF_VALUE) *polsect;
170
0
            polsect = X509V3_get_section(ctx, pstr + 1);
171
0
            if (!polsect) {
172
0
                X509V3err(X509V3_F_R2I_CERTPOL, X509V3_R_INVALID_SECTION);
173
174
0
                X509V3_conf_err(cnf);
175
0
                goto err;
176
0
            }
177
0
            pol = policy_section(ctx, polsect, ia5org);
178
0
            X509V3_section_free(ctx, polsect);
179
0
            if (!pol)
180
0
                goto err;
181
0
        } else {
182
0
            if (!(pobj = OBJ_txt2obj(cnf->name, 0))) {
183
0
                X509V3err(X509V3_F_R2I_CERTPOL,
184
0
                          X509V3_R_INVALID_OBJECT_IDENTIFIER);
185
0
                X509V3_conf_err(cnf);
186
0
                goto err;
187
0
            }
188
0
            pol = POLICYINFO_new();
189
0
            if (pol == NULL) {
190
0
                X509V3err(X509V3_F_R2I_CERTPOL, ERR_R_MALLOC_FAILURE);
191
0
                goto err;
192
0
            }
193
0
            pol->policyid = pobj;
194
0
        }
195
0
        if (!sk_POLICYINFO_push(pols, pol)) {
196
0
            POLICYINFO_free(pol);
197
0
            X509V3err(X509V3_F_R2I_CERTPOL, ERR_R_MALLOC_FAILURE);
198
0
            goto err;
199
0
        }
200
0
    }
201
0
    sk_CONF_VALUE_pop_free(vals, X509V3_conf_free);
202
0
    return pols;
203
0
 err:
204
0
    sk_CONF_VALUE_pop_free(vals, X509V3_conf_free);
205
0
    sk_POLICYINFO_pop_free(pols, POLICYINFO_free);
206
0
    return NULL;
207
0
}
208
209
static POLICYINFO *policy_section(X509V3_CTX *ctx,
210
                                  STACK_OF(CONF_VALUE) *polstrs, int ia5org)
211
0
{
212
0
    int i;
213
0
    CONF_VALUE *cnf;
214
0
    POLICYINFO *pol;
215
0
    POLICYQUALINFO *qual;
216
0
    if (!(pol = POLICYINFO_new()))
217
0
        goto merr;
218
0
    for (i = 0; i < sk_CONF_VALUE_num(polstrs); i++) {
219
0
        cnf = sk_CONF_VALUE_value(polstrs, i);
220
0
        if (!strcmp(cnf->name, "policyIdentifier")) {
221
0
            ASN1_OBJECT *pobj;
222
0
            if (!(pobj = OBJ_txt2obj(cnf->value, 0))) {
223
0
                X509V3err(X509V3_F_POLICY_SECTION,
224
0
                          X509V3_R_INVALID_OBJECT_IDENTIFIER);
225
0
                X509V3_conf_err(cnf);
226
0
                goto err;
227
0
            }
228
0
            pol->policyid = pobj;
229
230
0
        } else if (!name_cmp(cnf->name, "CPS")) {
231
0
            if (!pol->qualifiers)
232
0
                pol->qualifiers = sk_POLICYQUALINFO_new_null();
233
0
            if (!(qual = POLICYQUALINFO_new()))
234
0
                goto merr;
235
0
            if (!sk_POLICYQUALINFO_push(pol->qualifiers, qual))
236
0
                goto merr;
237
0
            if (!(qual->pqualid = OBJ_nid2obj(NID_id_qt_cps))) {
238
0
                X509V3err(X509V3_F_POLICY_SECTION, ERR_R_INTERNAL_ERROR);
239
0
                goto err;
240
0
            }
241
0
            if (!(qual->d.cpsuri = M_ASN1_IA5STRING_new()))
242
0
                goto merr;
243
0
            if (!ASN1_STRING_set(qual->d.cpsuri, cnf->value,
244
0
                                 strlen(cnf->value)))
245
0
                goto merr;
246
0
        } else if (!name_cmp(cnf->name, "userNotice")) {
247
0
            STACK_OF(CONF_VALUE) *unot;
248
0
            if (*cnf->value != '@') {
249
0
                X509V3err(X509V3_F_POLICY_SECTION,
250
0
                          X509V3_R_EXPECTED_A_SECTION_NAME);
251
0
                X509V3_conf_err(cnf);
252
0
                goto err;
253
0
            }
254
0
            unot = X509V3_get_section(ctx, cnf->value + 1);
255
0
            if (!unot) {
256
0
                X509V3err(X509V3_F_POLICY_SECTION, X509V3_R_INVALID_SECTION);
257
258
0
                X509V3_conf_err(cnf);
259
0
                goto err;
260
0
            }
261
0
            qual = notice_section(ctx, unot, ia5org);
262
0
            X509V3_section_free(ctx, unot);
263
0
            if (!qual)
264
0
                goto err;
265
0
            if (!pol->qualifiers)
266
0
                pol->qualifiers = sk_POLICYQUALINFO_new_null();
267
0
            if (!sk_POLICYQUALINFO_push(pol->qualifiers, qual))
268
0
                goto merr;
269
0
        } else {
270
0
            X509V3err(X509V3_F_POLICY_SECTION, X509V3_R_INVALID_OPTION);
271
272
0
            X509V3_conf_err(cnf);
273
0
            goto err;
274
0
        }
275
0
    }
276
0
    if (!pol->policyid) {
277
0
        X509V3err(X509V3_F_POLICY_SECTION, X509V3_R_NO_POLICY_IDENTIFIER);
278
0
        goto err;
279
0
    }
280
281
0
    return pol;
282
283
0
 merr:
284
0
    X509V3err(X509V3_F_POLICY_SECTION, ERR_R_MALLOC_FAILURE);
285
286
0
 err:
287
0
    POLICYINFO_free(pol);
288
0
    return NULL;
289
290
0
}
291
292
static POLICYQUALINFO *notice_section(X509V3_CTX *ctx,
293
                                      STACK_OF(CONF_VALUE) *unot, int ia5org)
294
0
{
295
0
    int i, ret;
296
0
    CONF_VALUE *cnf;
297
0
    USERNOTICE *not;
298
0
    POLICYQUALINFO *qual;
299
0
    if (!(qual = POLICYQUALINFO_new()))
300
0
        goto merr;
301
0
    if (!(qual->pqualid = OBJ_nid2obj(NID_id_qt_unotice))) {
302
0
        X509V3err(X509V3_F_NOTICE_SECTION, ERR_R_INTERNAL_ERROR);
303
0
        goto err;
304
0
    }
305
0
    if (!(not = USERNOTICE_new()))
306
0
        goto merr;
307
0
    qual->d.usernotice = not;
308
0
    for (i = 0; i < sk_CONF_VALUE_num(unot); i++) {
309
0
        cnf = sk_CONF_VALUE_value(unot, i);
310
0
        if (!strcmp(cnf->name, "explicitText")) {
311
0
            if (!(not->exptext = M_ASN1_VISIBLESTRING_new()))
312
0
                goto merr;
313
0
            if (!ASN1_STRING_set(not->exptext, cnf->value,
314
0
                                 strlen(cnf->value)))
315
0
                goto merr;
316
0
        } else if (!strcmp(cnf->name, "organization")) {
317
0
            NOTICEREF *nref;
318
0
            if (!not->noticeref) {
319
0
                if (!(nref = NOTICEREF_new()))
320
0
                    goto merr;
321
0
                not->noticeref = nref;
322
0
            } else
323
0
                nref = not->noticeref;
324
0
            if (ia5org)
325
0
                nref->organization->type = V_ASN1_IA5STRING;
326
0
            else
327
0
                nref->organization->type = V_ASN1_VISIBLESTRING;
328
0
            if (!ASN1_STRING_set(nref->organization, cnf->value,
329
0
                                 strlen(cnf->value)))
330
0
                goto merr;
331
0
        } else if (!strcmp(cnf->name, "noticeNumbers")) {
332
0
            NOTICEREF *nref;
333
0
            STACK_OF(CONF_VALUE) *nos;
334
0
            if (!not->noticeref) {
335
0
                if (!(nref = NOTICEREF_new()))
336
0
                    goto merr;
337
0
                not->noticeref = nref;
338
0
            } else
339
0
                nref = not->noticeref;
340
0
            nos = X509V3_parse_list(cnf->value);
341
0
            if (!nos || !sk_CONF_VALUE_num(nos)) {
342
0
                X509V3err(X509V3_F_NOTICE_SECTION, X509V3_R_INVALID_NUMBERS);
343
0
                X509V3_conf_err(cnf);
344
0
                goto err;
345
0
            }
346
0
            ret = nref_nos(nref->noticenos, nos);
347
0
            sk_CONF_VALUE_pop_free(nos, X509V3_conf_free);
348
0
            if (!ret)
349
0
                goto err;
350
0
        } else {
351
0
            X509V3err(X509V3_F_NOTICE_SECTION, X509V3_R_INVALID_OPTION);
352
0
            X509V3_conf_err(cnf);
353
0
            goto err;
354
0
        }
355
0
    }
356
357
0
    if (not->noticeref &&
358
0
        (!not->noticeref->noticenos || !not->noticeref->organization)) {
359
0
        X509V3err(X509V3_F_NOTICE_SECTION,
360
0
                  X509V3_R_NEED_ORGANIZATION_AND_NUMBERS);
361
0
        goto err;
362
0
    }
363
364
0
    return qual;
365
366
0
 merr:
367
0
    X509V3err(X509V3_F_NOTICE_SECTION, ERR_R_MALLOC_FAILURE);
368
369
0
 err:
370
0
    POLICYQUALINFO_free(qual);
371
0
    return NULL;
372
0
}
373
374
static int nref_nos(STACK_OF(ASN1_INTEGER) *nnums, STACK_OF(CONF_VALUE) *nos)
375
0
{
376
0
    CONF_VALUE *cnf;
377
0
    ASN1_INTEGER *aint;
378
379
0
    int i;
380
381
0
    for (i = 0; i < sk_CONF_VALUE_num(nos); i++) {
382
0
        cnf = sk_CONF_VALUE_value(nos, i);
383
0
        if (!(aint = s2i_ASN1_INTEGER(NULL, cnf->name))) {
384
0
            X509V3err(X509V3_F_NREF_NOS, X509V3_R_INVALID_NUMBER);
385
0
            goto err;
386
0
        }
387
0
        if (!sk_ASN1_INTEGER_push(nnums, aint))
388
0
            goto merr;
389
0
    }
390
0
    return 1;
391
392
0
 merr:
393
0
    ASN1_INTEGER_free(aint);
394
0
    X509V3err(X509V3_F_NREF_NOS, ERR_R_MALLOC_FAILURE);
395
396
0
 err:
397
0
    return 0;
398
0
}
399
400
static int i2r_certpol(X509V3_EXT_METHOD *method, STACK_OF(POLICYINFO) *pol,
401
                       BIO *out, int indent)
402
0
{
403
0
    int i;
404
0
    POLICYINFO *pinfo;
405
    /* First print out the policy OIDs */
406
0
    for (i = 0; i < sk_POLICYINFO_num(pol); i++) {
407
0
        pinfo = sk_POLICYINFO_value(pol, i);
408
0
        BIO_printf(out, "%*sPolicy: ", indent, "");
409
0
        i2a_ASN1_OBJECT(out, pinfo->policyid);
410
0
        BIO_puts(out, "\n");
411
0
        if (pinfo->qualifiers)
412
0
            print_qualifiers(out, pinfo->qualifiers, indent + 2);
413
0
    }
414
0
    return 1;
415
0
}
416
417
static void print_qualifiers(BIO *out, STACK_OF(POLICYQUALINFO) *quals,
418
                             int indent)
419
0
{
420
0
    POLICYQUALINFO *qualinfo;
421
0
    int i;
422
0
    for (i = 0; i < sk_POLICYQUALINFO_num(quals); i++) {
423
0
        qualinfo = sk_POLICYQUALINFO_value(quals, i);
424
0
        switch (OBJ_obj2nid(qualinfo->pqualid)) {
425
0
        case NID_id_qt_cps:
426
0
            BIO_printf(out, "%*sCPS: %s\n", indent, "",
427
0
                       qualinfo->d.cpsuri->data);
428
0
            break;
429
430
0
        case NID_id_qt_unotice:
431
0
            BIO_printf(out, "%*sUser Notice:\n", indent, "");
432
0
            print_notice(out, qualinfo->d.usernotice, indent + 2);
433
0
            break;
434
435
0
        default:
436
0
            BIO_printf(out, "%*sUnknown Qualifier: ", indent + 2, "");
437
438
0
            i2a_ASN1_OBJECT(out, qualinfo->pqualid);
439
0
            BIO_puts(out, "\n");
440
0
            break;
441
0
        }
442
0
    }
443
0
}
444
445
static void print_notice(BIO *out, USERNOTICE *notice, int indent)
446
0
{
447
0
    int i;
448
0
    if (notice->noticeref) {
449
0
        NOTICEREF *ref;
450
0
        ref = notice->noticeref;
451
0
        BIO_printf(out, "%*sOrganization: %s\n", indent, "",
452
0
                   ref->organization->data);
453
0
        BIO_printf(out, "%*sNumber%s: ", indent, "",
454
0
                   sk_ASN1_INTEGER_num(ref->noticenos) > 1 ? "s" : "");
455
0
        for (i = 0; i < sk_ASN1_INTEGER_num(ref->noticenos); i++) {
456
0
            ASN1_INTEGER *num;
457
0
            char *tmp;
458
0
            num = sk_ASN1_INTEGER_value(ref->noticenos, i);
459
0
            if (i)
460
0
                BIO_puts(out, ", ");
461
0
            if (num == NULL)
462
0
                BIO_puts(out, "(null)");
463
0
            else {
464
0
                tmp = i2s_ASN1_INTEGER(NULL, num);
465
0
                if (tmp == NULL)
466
0
                    return;
467
0
                BIO_puts(out, tmp);
468
0
                OPENSSL_free(tmp);
469
0
            }
470
0
        }
471
0
        BIO_puts(out, "\n");
472
0
    }
473
0
    if (notice->exptext)
474
0
        BIO_printf(out, "%*sExplicit Text: %s\n", indent, "",
475
0
                   notice->exptext->data);
476
0
}
477
478
void X509_POLICY_NODE_print(BIO *out, X509_POLICY_NODE *node, int indent)
479
0
{
480
0
    const X509_POLICY_DATA *dat = node->data;
481
482
0
    BIO_printf(out, "%*sPolicy: ", indent, "");
483
484
0
    i2a_ASN1_OBJECT(out, dat->valid_policy);
485
0
    BIO_puts(out, "\n");
486
0
    BIO_printf(out, "%*s%s\n", indent + 2, "",
487
0
               node_data_critical(dat) ? "Critical" : "Non Critical");
488
0
    if (dat->qualifier_set)
489
0
        print_qualifiers(out, dat->qualifier_set, indent + 2);
490
0
    else
491
0
        BIO_printf(out, "%*sNo Qualifiers\n", indent + 2, "");
492
0
}
493
494
495
IMPLEMENT_STACK_OF(X509_POLICY_NODE)
496
497
IMPLEMENT_STACK_OF(X509_POLICY_DATA)