Coverage Report

Created: 2026-10-28 06:28

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl36/crypto/x509/v3_pci.c
Line
Count
Source
1
/*
2
 * Copyright 2004-2025 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
static int i2r_pci(X509V3_EXT_METHOD *method, PROXY_CERT_INFO_EXTENSION *ext,
53
    BIO *out, int indent);
54
static PROXY_CERT_INFO_EXTENSION *r2i_pci(X509V3_EXT_METHOD *method,
55
    X509V3_CTX *ctx, char *str);
56
57
const X509V3_EXT_METHOD ossl_v3_pci = {
58
    NID_proxyCertInfo,
59
    0,
60
    ASN1_ITEM_ref(PROXY_CERT_INFO_EXTENSION),
61
    0,
62
    0,
63
    0,
64
    0,
65
    0,
66
    0,
67
    NULL,
68
    NULL,
69
    (X509V3_EXT_I2R)i2r_pci,
70
    (X509V3_EXT_R2I)r2i_pci,
71
    NULL,
72
};
73
74
static int i2r_pci(X509V3_EXT_METHOD *method, PROXY_CERT_INFO_EXTENSION *pci,
75
    BIO *out, int indent)
76
5.85k
{
77
5.85k
    BIO_printf(out, "%*sPath Length Constraint: ", indent, "");
78
5.85k
    if (pci->pcPathLengthConstraint)
79
4.46k
        i2a_ASN1_INTEGER(out, pci->pcPathLengthConstraint);
80
1.39k
    else
81
1.39k
        BIO_printf(out, "infinite");
82
5.85k
    BIO_puts(out, "\n");
83
5.85k
    BIO_printf(out, "%*sPolicy Language: ", indent, "");
84
5.85k
    i2a_ASN1_OBJECT(out, pci->proxyPolicy->policyLanguage);
85
5.85k
    if (pci->proxyPolicy->policy && pci->proxyPolicy->policy->data)
86
1.12k
        BIO_printf(out, "\n%*sPolicy Text: %.*s", indent, "",
87
1.12k
            pci->proxyPolicy->policy->length,
88
1.12k
            pci->proxyPolicy->policy->data);
89
5.85k
    return 1;
90
5.85k
}
91
92
static int process_pci_value(CONF_VALUE *val,
93
    ASN1_OBJECT **language, ASN1_INTEGER **pathlen,
94
    ASN1_OCTET_STRING **policy)
95
4.92k
{
96
4.92k
    int free_policy = 0;
97
98
4.92k
    if (strcmp(val->name, "language") == 0) {
99
126
        if (*language) {
100
6
            ERR_raise(ERR_LIB_X509V3, X509V3_R_POLICY_LANGUAGE_ALREADY_DEFINED);
101
6
            X509V3_conf_err(val);
102
6
            return 0;
103
6
        }
104
120
        if ((*language = OBJ_txt2obj(val->value, 0)) == NULL) {
105
13
            ERR_raise(ERR_LIB_X509V3, X509V3_R_INVALID_OBJECT_IDENTIFIER);
106
13
            X509V3_conf_err(val);
107
13
            return 0;
108
13
        }
109
4.79k
    } else if (strcmp(val->name, "pathlen") == 0) {
110
104
        if (*pathlen) {
111
6
            ERR_raise(ERR_LIB_X509V3,
112
6
                X509V3_R_POLICY_PATH_LENGTH_ALREADY_DEFINED);
113
6
            X509V3_conf_err(val);
114
6
            return 0;
115
6
        }
116
98
        if (!X509V3_get_value_int(val, pathlen)) {
117
30
            ERR_raise(ERR_LIB_X509V3, X509V3_R_POLICY_PATH_LENGTH);
118
30
            X509V3_conf_err(val);
119
30
            return 0;
120
30
        }
121
4.69k
    } else if (strcmp(val->name, "policy") == 0) {
122
1.34k
        char *valp = val->value;
123
1.34k
        unsigned char *tmp_data = NULL;
124
1.34k
        long val_len;
125
126
1.34k
        if (*policy == NULL) {
127
460
            *policy = ASN1_OCTET_STRING_new();
128
460
            if (*policy == NULL) {
129
0
                ERR_raise(ERR_LIB_X509V3, ERR_R_ASN1_LIB);
130
0
                X509V3_conf_err(val);
131
0
                return 0;
132
0
            }
133
460
            free_policy = 1;
134
460
        }
135
1.34k
        if (CHECK_AND_SKIP_PREFIX(valp, "hex:")) {
136
412
            unsigned char *tmp_data2 = OPENSSL_hexstr2buf(valp, &val_len);
137
138
412
            if (!tmp_data2) {
139
7
                X509V3_conf_err(val);
140
7
                goto err;
141
7
            }
142
143
405
            tmp_data = OPENSSL_realloc((*policy)->data,
144
405
                (*policy)->length + val_len + 1);
145
405
            if (tmp_data) {
146
405
                (*policy)->data = tmp_data;
147
405
                memcpy(&(*policy)->data[(*policy)->length],
148
405
                    tmp_data2, val_len);
149
405
                (*policy)->length += val_len;
150
405
                (*policy)->data[(*policy)->length] = '\0';
151
405
            } else {
152
0
                OPENSSL_free(tmp_data2);
153
                /*
154
                 * realloc failure implies the original data space is b0rked
155
                 * too!
156
                 */
157
0
                OPENSSL_free((*policy)->data);
158
0
                (*policy)->data = NULL;
159
0
                (*policy)->length = 0;
160
0
                X509V3_conf_err(val);
161
0
                goto err;
162
0
            }
163
405
            OPENSSL_free(tmp_data2);
164
937
        } else if (CHECK_AND_SKIP_PREFIX(valp, "file:")) {
165
6
            unsigned char buf[2048];
166
6
            int n;
167
6
            BIO *b = BIO_new_file(valp, "r");
168
6
            if (!b) {
169
6
                ERR_raise(ERR_LIB_X509V3, ERR_R_BIO_LIB);
170
6
                X509V3_conf_err(val);
171
6
                goto err;
172
6
            }
173
0
            while ((n = BIO_read(b, buf, sizeof(buf))) > 0
174
0
                || (n == 0 && BIO_should_retry(b))) {
175
0
                if (!n)
176
0
                    continue;
177
178
0
                tmp_data = OPENSSL_realloc((*policy)->data,
179
0
                    (*policy)->length + n + 1);
180
181
0
                if (!tmp_data) {
182
0
                    OPENSSL_free((*policy)->data);
183
0
                    (*policy)->data = NULL;
184
0
                    (*policy)->length = 0;
185
0
                    X509V3_conf_err(val);
186
0
                    BIO_free_all(b);
187
0
                    goto err;
188
0
                }
189
190
0
                (*policy)->data = tmp_data;
191
0
                memcpy(&(*policy)->data[(*policy)->length], buf, n);
192
0
                (*policy)->length += n;
193
0
                (*policy)->data[(*policy)->length] = '\0';
194
0
            }
195
0
            BIO_free_all(b);
196
197
0
            if (n < 0) {
198
0
                ERR_raise(ERR_LIB_X509V3, ERR_R_BIO_LIB);
199
0
                X509V3_conf_err(val);
200
0
                goto err;
201
0
            }
202
931
        } else if (CHECK_AND_SKIP_PREFIX(valp, "text:")) {
203
568
            val_len = (int)strlen(valp);
204
568
            tmp_data = OPENSSL_realloc((*policy)->data,
205
568
                (*policy)->length + val_len + 1);
206
568
            if (tmp_data) {
207
568
                (*policy)->data = tmp_data;
208
568
                memcpy(&(*policy)->data[(*policy)->length],
209
568
                    val->value + 5, val_len);
210
568
                (*policy)->length += val_len;
211
568
                (*policy)->data[(*policy)->length] = '\0';
212
568
            } else {
213
                /*
214
                 * realloc failure implies the original data space is b0rked
215
                 * too!
216
                 */
217
0
                OPENSSL_free((*policy)->data);
218
0
                (*policy)->data = NULL;
219
0
                (*policy)->length = 0;
220
0
                X509V3_conf_err(val);
221
0
                goto err;
222
0
            }
223
568
        } else {
224
363
            ERR_raise(ERR_LIB_X509V3, X509V3_R_INCORRECT_POLICY_SYNTAX_TAG);
225
363
            X509V3_conf_err(val);
226
363
            goto err;
227
363
        }
228
973
        if (!tmp_data) {
229
0
            X509V3_conf_err(val);
230
0
            goto err;
231
0
        }
232
973
    }
233
4.48k
    return 1;
234
376
err:
235
376
    if (free_policy) {
236
350
        ASN1_OCTET_STRING_free(*policy);
237
350
        *policy = NULL;
238
350
    }
239
376
    return 0;
240
4.92k
}
241
242
static PROXY_CERT_INFO_EXTENSION *r2i_pci(X509V3_EXT_METHOD *method,
243
    X509V3_CTX *ctx, char *value)
244
1.22k
{
245
1.22k
    PROXY_CERT_INFO_EXTENSION *pci = NULL;
246
1.22k
    STACK_OF(CONF_VALUE) *vals;
247
1.22k
    ASN1_OBJECT *language = NULL;
248
1.22k
    ASN1_INTEGER *pathlen = NULL;
249
1.22k
    ASN1_OCTET_STRING *policy = NULL;
250
1.22k
    int i, j;
251
252
1.22k
    vals = X509V3_parse_list(value);
253
4.94k
    for (i = 0; i < sk_CONF_VALUE_num(vals); i++) {
254
4.30k
        CONF_VALUE *cnf = sk_CONF_VALUE_value(vals, i);
255
256
4.30k
        if (!cnf->name || (*cnf->name != '@' && !cnf->value)) {
257
125
            ERR_raise(ERR_LIB_X509V3, X509V3_R_INVALID_PROXY_POLICY_SETTING);
258
125
            X509V3_conf_err(cnf);
259
125
            goto err;
260
125
        }
261
4.17k
        if (*cnf->name == '@') {
262
641
            STACK_OF(CONF_VALUE) *sect;
263
641
            int success_p = 1;
264
265
641
            sect = X509V3_get_section(ctx, cnf->name + 1);
266
641
            if (!sect) {
267
20
                ERR_raise(ERR_LIB_X509V3, X509V3_R_INVALID_SECTION);
268
20
                X509V3_conf_err(cnf);
269
20
                goto err;
270
20
            }
271
2.00k
            for (j = 0; success_p && j < sk_CONF_VALUE_num(sect); j++) {
272
1.38k
                success_p = process_pci_value(sk_CONF_VALUE_value(sect, j),
273
1.38k
                    &language, &pathlen, &policy);
274
1.38k
            }
275
621
            X509V3_section_free(ctx, sect);
276
621
            if (!success_p)
277
24
                goto err;
278
3.53k
        } else {
279
3.53k
            if (!process_pci_value(cnf, &language, &pathlen, &policy)) {
280
407
                X509V3_conf_err(cnf);
281
407
                goto err;
282
407
            }
283
3.53k
        }
284
4.17k
    }
285
286
    /* Language is mandatory */
287
644
    if (!language) {
288
549
        ERR_raise(ERR_LIB_X509V3,
289
549
            X509V3_R_NO_PROXY_CERT_POLICY_LANGUAGE_DEFINED);
290
549
        goto err;
291
549
    }
292
95
    i = OBJ_obj2nid(language);
293
95
    if ((i == NID_Independent || i == NID_id_ppl_inheritAll) && policy) {
294
6
        ERR_raise(ERR_LIB_X509V3,
295
6
            X509V3_R_POLICY_WHEN_PROXY_LANGUAGE_REQUIRES_NO_POLICY);
296
6
        goto err;
297
6
    }
298
299
89
    pci = PROXY_CERT_INFO_EXTENSION_new();
300
89
    if (pci == NULL) {
301
0
        ERR_raise(ERR_LIB_X509V3, ERR_R_ASN1_LIB);
302
0
        goto err;
303
0
    }
304
305
89
    pci->proxyPolicy->policyLanguage = language;
306
89
    language = NULL;
307
89
    pci->proxyPolicy->policy = policy;
308
89
    policy = NULL;
309
89
    pci->pcPathLengthConstraint = pathlen;
310
89
    pathlen = NULL;
311
89
    goto end;
312
1.13k
err:
313
1.13k
    ASN1_OBJECT_free(language);
314
1.13k
    ASN1_INTEGER_free(pathlen);
315
1.13k
    pathlen = NULL;
316
1.13k
    ASN1_OCTET_STRING_free(policy);
317
1.13k
    policy = NULL;
318
1.13k
    PROXY_CERT_INFO_EXTENSION_free(pci);
319
1.13k
    pci = NULL;
320
1.22k
end:
321
1.22k
    sk_CONF_VALUE_pop_free(vals, X509V3_conf_free);
322
1.22k
    return pci;
323
1.13k
}