Coverage Report

Created: 2026-10-28 06:28

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl41/crypto/x509/v3_pci.c
Line
Count
Source
1
/*
2
 * Copyright 2004-2026 The OpenSSL Project Authors. All Rights Reserved.
3
 *
4
 * Licensed under the Apache License 2.0 (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
/*
11
 * This file is dual-licensed and is also available under the following
12
 * terms:
13
 *
14
 * Copyright (c) 2004 Kungliga Tekniska Högskolan
15
 * (Royal Institute of Technology, Stockholm, Sweden).
16
 * All rights reserved.
17
 *
18
 * Redistribution and use in source and binary forms, with or without
19
 * modification, are permitted provided that the following conditions
20
 * are met:
21
 *
22
 * 1. Redistributions of source code must retain the above copyright
23
 *    notice, this list of conditions and the following disclaimer.
24
 *
25
 * 2. Redistributions in binary form must reproduce the above copyright
26
 *    notice, this list of conditions and the following disclaimer in the
27
 *    documentation and/or other materials provided with the distribution.
28
 *
29
 * 3. Neither the name of the Institute nor the names of its contributors
30
 *    may be used to endorse or promote products derived from this software
31
 *    without specific prior written permission.
32
 *
33
 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
34
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
35
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
36
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
37
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
38
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
39
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
40
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
41
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
42
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
43
 * SUCH DAMAGE.
44
 */
45
46
#include <stdio.h>
47
#include "internal/cryptlib.h"
48
#include <openssl/conf.h>
49
#include <openssl/x509v3.h>
50
#include "ext_dat.h"
51
52
#include <crypto/asn1.h>
53
54
static int i2r_pci(X509V3_EXT_METHOD *method, PROXY_CERT_INFO_EXTENSION *ext,
55
    BIO *out, int indent);
56
static PROXY_CERT_INFO_EXTENSION *r2i_pci(X509V3_EXT_METHOD *method,
57
    X509V3_CTX *ctx, char *str);
58
59
const X509V3_EXT_METHOD ossl_v3_pci = {
60
    NID_proxyCertInfo,
61
    0,
62
    ASN1_ITEM_ref(PROXY_CERT_INFO_EXTENSION),
63
    0,
64
    0,
65
    0,
66
    0,
67
    0,
68
    0,
69
    NULL,
70
    NULL,
71
    (X509V3_EXT_I2R)i2r_pci,
72
    (X509V3_EXT_R2I)r2i_pci,
73
    NULL,
74
};
75
76
static int i2r_pci(X509V3_EXT_METHOD *method, PROXY_CERT_INFO_EXTENSION *pci,
77
    BIO *out, int indent)
78
5.85k
{
79
5.85k
    BIO_printf(out, "%*sPath Length Constraint: ", indent, "");
80
5.85k
    if (pci->pcPathLengthConstraint)
81
4.46k
        i2a_ASN1_INTEGER(out, pci->pcPathLengthConstraint);
82
1.39k
    else
83
1.39k
        BIO_printf(out, "infinite");
84
5.85k
    BIO_puts(out, "\n");
85
5.85k
    BIO_printf(out, "%*sPolicy Language: ", indent, "");
86
5.85k
    i2a_ASN1_OBJECT(out, pci->proxyPolicy->policyLanguage);
87
5.85k
    if (pci->proxyPolicy->policy && pci->proxyPolicy->policy->data)
88
1.12k
        BIO_printf(out, "\n%*sPolicy Text: %.*s", indent, "",
89
1.12k
            pci->proxyPolicy->policy->length,
90
1.12k
            pci->proxyPolicy->policy->data);
91
5.85k
    return 1;
92
5.85k
}
93
94
static int process_pci_value(CONF_VALUE *val,
95
    ASN1_OBJECT **language, ASN1_INTEGER **pathlen,
96
    ASN1_OCTET_STRING **policy)
97
4.92k
{
98
4.92k
    int free_policy = 0;
99
100
4.92k
    if (strcmp(val->name, "language") == 0) {
101
126
        if (*language) {
102
6
            ERR_raise(ERR_LIB_X509V3, X509V3_R_POLICY_LANGUAGE_ALREADY_DEFINED);
103
6
            X509V3_conf_err(val);
104
6
            return 0;
105
6
        }
106
120
        if ((*language = OBJ_txt2obj(val->value, 0)) == NULL) {
107
13
            ERR_raise(ERR_LIB_X509V3, X509V3_R_INVALID_OBJECT_IDENTIFIER);
108
13
            X509V3_conf_err(val);
109
13
            return 0;
110
13
        }
111
4.79k
    } else if (strcmp(val->name, "pathlen") == 0) {
112
104
        if (*pathlen) {
113
6
            ERR_raise(ERR_LIB_X509V3,
114
6
                X509V3_R_POLICY_PATH_LENGTH_ALREADY_DEFINED);
115
6
            X509V3_conf_err(val);
116
6
            return 0;
117
6
        }
118
98
        if (!X509V3_get_value_int(val, pathlen)) {
119
30
            ERR_raise(ERR_LIB_X509V3, X509V3_R_POLICY_PATH_LENGTH);
120
30
            X509V3_conf_err(val);
121
30
            return 0;
122
30
        }
123
4.69k
    } else if (strcmp(val->name, "policy") == 0) {
124
1.34k
        char *valp = val->value;
125
1.34k
        unsigned char *tmp_data = NULL;
126
1.34k
        long val_len;
127
128
1.34k
        if (*policy == NULL) {
129
460
            *policy = ASN1_OCTET_STRING_new();
130
460
            if (*policy == NULL) {
131
0
                ERR_raise(ERR_LIB_X509V3, ERR_R_ASN1_LIB);
132
0
                X509V3_conf_err(val);
133
0
                return 0;
134
0
            }
135
460
            free_policy = 1;
136
460
        }
137
1.34k
        if (CHECK_AND_SKIP_PREFIX(valp, "hex:")) {
138
412
            unsigned char *tmp_data2 = OPENSSL_hexstr2buf(valp, &val_len);
139
140
412
            if (!tmp_data2) {
141
7
                X509V3_conf_err(val);
142
7
                goto err;
143
7
            }
144
145
405
            tmp_data = OPENSSL_realloc((*policy)->data,
146
405
                (*policy)->length + val_len + 1);
147
405
            if (tmp_data) {
148
405
                (*policy)->data = tmp_data;
149
405
                memcpy(&(*policy)->data[(*policy)->length],
150
405
                    tmp_data2, val_len);
151
405
                (*policy)->length += val_len;
152
405
                (*policy)->data[(*policy)->length] = '\0';
153
405
            } else {
154
0
                OPENSSL_free(tmp_data2);
155
                /*
156
                 * realloc failure implies the original data space is b0rked
157
                 * too!
158
                 */
159
0
                OPENSSL_free((*policy)->data);
160
0
                (*policy)->data = NULL;
161
0
                (*policy)->length = 0;
162
0
                X509V3_conf_err(val);
163
0
                goto err;
164
0
            }
165
405
            OPENSSL_free(tmp_data2);
166
937
        } else if (CHECK_AND_SKIP_PREFIX(valp, "file:")) {
167
6
            unsigned char buf[2048];
168
6
            int n;
169
170
6
#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
171
            /*
172
             * Prevent fuzzer from mutating input to reading random files.
173
             */
174
6
            valp = NULL;
175
6
#endif
176
6
            BIO *b = BIO_new_file(valp, "r");
177
6
            if (!b) {
178
6
                ERR_raise(ERR_LIB_X509V3, ERR_R_BIO_LIB);
179
6
                X509V3_conf_err(val);
180
6
                goto err;
181
6
            }
182
0
            while ((n = BIO_read(b, buf, sizeof(buf))) > 0
183
0
                || (n == 0 && BIO_should_retry(b))) {
184
0
                if (!n)
185
0
                    continue;
186
187
0
                tmp_data = OPENSSL_realloc((*policy)->data,
188
0
                    (*policy)->length + n + 1);
189
190
0
                if (!tmp_data) {
191
0
                    OPENSSL_free((*policy)->data);
192
0
                    (*policy)->data = NULL;
193
0
                    (*policy)->length = 0;
194
0
                    X509V3_conf_err(val);
195
0
                    BIO_free_all(b);
196
0
                    goto err;
197
0
                }
198
199
0
                (*policy)->data = tmp_data;
200
0
                memcpy(&(*policy)->data[(*policy)->length], buf, n);
201
0
                (*policy)->length += n;
202
0
                (*policy)->data[(*policy)->length] = '\0';
203
0
            }
204
0
            BIO_free_all(b);
205
206
0
            if (n < 0) {
207
0
                ERR_raise(ERR_LIB_X509V3, ERR_R_BIO_LIB);
208
0
                X509V3_conf_err(val);
209
0
                goto err;
210
0
            }
211
931
        } else if (CHECK_AND_SKIP_PREFIX(valp, "text:")) {
212
568
            val_len = (int)strlen(valp);
213
568
            tmp_data = OPENSSL_realloc((*policy)->data,
214
568
                (*policy)->length + val_len + 1);
215
568
            if (tmp_data) {
216
568
                (*policy)->data = tmp_data;
217
568
                memcpy(&(*policy)->data[(*policy)->length],
218
568
                    val->value + 5, val_len);
219
568
                (*policy)->length += val_len;
220
568
                (*policy)->data[(*policy)->length] = '\0';
221
568
            } else {
222
                /*
223
                 * realloc failure implies the original data space is b0rked
224
                 * too!
225
                 */
226
0
                OPENSSL_free((*policy)->data);
227
0
                (*policy)->data = NULL;
228
0
                (*policy)->length = 0;
229
0
                X509V3_conf_err(val);
230
0
                goto err;
231
0
            }
232
568
        } else {
233
363
            ERR_raise(ERR_LIB_X509V3, X509V3_R_INCORRECT_POLICY_SYNTAX_TAG);
234
363
            X509V3_conf_err(val);
235
363
            goto err;
236
363
        }
237
973
        if (!tmp_data) {
238
0
            X509V3_conf_err(val);
239
0
            goto err;
240
0
        }
241
973
    }
242
4.48k
    return 1;
243
376
err:
244
376
    if (free_policy) {
245
350
        ASN1_OCTET_STRING_free(*policy);
246
350
        *policy = NULL;
247
350
    }
248
376
    return 0;
249
4.92k
}
250
251
static PROXY_CERT_INFO_EXTENSION *r2i_pci(X509V3_EXT_METHOD *method,
252
    X509V3_CTX *ctx, char *value)
253
1.22k
{
254
1.22k
    PROXY_CERT_INFO_EXTENSION *pci = NULL;
255
1.22k
    STACK_OF(CONF_VALUE) *vals;
256
1.22k
    ASN1_OBJECT *language = NULL;
257
1.22k
    ASN1_INTEGER *pathlen = NULL;
258
1.22k
    ASN1_OCTET_STRING *policy = NULL;
259
1.22k
    int i, j;
260
261
1.22k
    vals = X509V3_parse_list(value);
262
4.94k
    for (i = 0; i < sk_CONF_VALUE_num(vals); i++) {
263
4.30k
        CONF_VALUE *cnf = sk_CONF_VALUE_value(vals, i);
264
265
4.30k
        if (!cnf->name || (*cnf->name != '@' && !cnf->value)) {
266
125
            ERR_raise(ERR_LIB_X509V3, X509V3_R_INVALID_PROXY_POLICY_SETTING);
267
125
            X509V3_conf_err(cnf);
268
125
            goto err;
269
125
        }
270
4.17k
        if (*cnf->name == '@') {
271
641
            STACK_OF(CONF_VALUE) *sect;
272
641
            int success_p = 1;
273
274
641
            sect = X509V3_get_section(ctx, cnf->name + 1);
275
641
            if (!sect) {
276
20
                ERR_raise(ERR_LIB_X509V3, X509V3_R_INVALID_SECTION);
277
20
                X509V3_conf_err(cnf);
278
20
                goto err;
279
20
            }
280
2.00k
            for (j = 0; success_p && j < sk_CONF_VALUE_num(sect); j++) {
281
1.38k
                success_p = process_pci_value(sk_CONF_VALUE_value(sect, j),
282
1.38k
                    &language, &pathlen, &policy);
283
1.38k
            }
284
621
            X509V3_section_free(ctx, sect);
285
621
            if (!success_p)
286
24
                goto err;
287
3.53k
        } else {
288
3.53k
            if (!process_pci_value(cnf, &language, &pathlen, &policy)) {
289
407
                X509V3_conf_err(cnf);
290
407
                goto err;
291
407
            }
292
3.53k
        }
293
4.17k
    }
294
295
    /* Language is mandatory */
296
644
    if (!language) {
297
549
        ERR_raise(ERR_LIB_X509V3,
298
549
            X509V3_R_NO_PROXY_CERT_POLICY_LANGUAGE_DEFINED);
299
549
        goto err;
300
549
    }
301
95
    i = OBJ_obj2nid(language);
302
95
    if ((i == NID_Independent || i == NID_id_ppl_inheritAll) && policy) {
303
6
        ERR_raise(ERR_LIB_X509V3,
304
6
            X509V3_R_POLICY_WHEN_PROXY_LANGUAGE_REQUIRES_NO_POLICY);
305
6
        goto err;
306
6
    }
307
308
89
    pci = PROXY_CERT_INFO_EXTENSION_new();
309
89
    if (pci == NULL) {
310
0
        ERR_raise(ERR_LIB_X509V3, ERR_R_ASN1_LIB);
311
0
        goto err;
312
0
    }
313
314
89
    pci->proxyPolicy->policyLanguage = language;
315
89
    language = NULL;
316
89
    pci->proxyPolicy->policy = policy;
317
89
    policy = NULL;
318
89
    pci->pcPathLengthConstraint = pathlen;
319
89
    pathlen = NULL;
320
89
    goto end;
321
1.13k
err:
322
1.13k
    ASN1_OBJECT_free(language);
323
1.13k
    ASN1_INTEGER_free(pathlen);
324
1.13k
    pathlen = NULL;
325
1.13k
    ASN1_OCTET_STRING_free(policy);
326
1.13k
    policy = NULL;
327
1.13k
    PROXY_CERT_INFO_EXTENSION_free(pci);
328
1.13k
    pci = NULL;
329
1.22k
end:
330
1.22k
    sk_CONF_VALUE_pop_free(vals, X509V3_conf_free);
331
1.22k
    return pci;
332
1.13k
}