Coverage Report

Created: 2026-09-12 06:55

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl40/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
4.90k
{
79
4.90k
    BIO_printf(out, "%*sPath Length Constraint: ", indent, "");
80
4.90k
    if (pci->pcPathLengthConstraint)
81
3.80k
        i2a_ASN1_INTEGER(out, pci->pcPathLengthConstraint);
82
1.10k
    else
83
1.10k
        BIO_printf(out, "infinite");
84
4.90k
    BIO_puts(out, "\n");
85
4.90k
    BIO_printf(out, "%*sPolicy Language: ", indent, "");
86
4.90k
    i2a_ASN1_OBJECT(out, pci->proxyPolicy->policyLanguage);
87
4.90k
    if (pci->proxyPolicy->policy && pci->proxyPolicy->policy->data)
88
913
        BIO_printf(out, "\n%*sPolicy Text: %.*s", indent, "",
89
913
            pci->proxyPolicy->policy->length,
90
913
            pci->proxyPolicy->policy->data);
91
4.90k
    return 1;
92
4.90k
}
93
94
static int process_pci_value(CONF_VALUE *val,
95
    ASN1_OBJECT **language, ASN1_INTEGER **pathlen,
96
    ASN1_OCTET_STRING **policy)
97
4.53k
{
98
4.53k
    int free_policy = 0;
99
100
4.53k
    if (strcmp(val->name, "language") == 0) {
101
96
        if (*language) {
102
5
            ERR_raise(ERR_LIB_X509V3, X509V3_R_POLICY_LANGUAGE_ALREADY_DEFINED);
103
5
            X509V3_conf_err(val);
104
5
            return 0;
105
5
        }
106
91
        if ((*language = OBJ_txt2obj(val->value, 0)) == NULL) {
107
10
            ERR_raise(ERR_LIB_X509V3, X509V3_R_INVALID_OBJECT_IDENTIFIER);
108
10
            X509V3_conf_err(val);
109
10
            return 0;
110
10
        }
111
4.43k
    } else if (strcmp(val->name, "pathlen") == 0) {
112
74
        if (*pathlen) {
113
7
            ERR_raise(ERR_LIB_X509V3,
114
7
                X509V3_R_POLICY_PATH_LENGTH_ALREADY_DEFINED);
115
7
            X509V3_conf_err(val);
116
7
            return 0;
117
7
        }
118
67
        if (!X509V3_get_value_int(val, pathlen)) {
119
14
            ERR_raise(ERR_LIB_X509V3, X509V3_R_POLICY_PATH_LENGTH);
120
14
            X509V3_conf_err(val);
121
14
            return 0;
122
14
        }
123
4.36k
    } else if (strcmp(val->name, "policy") == 0) {
124
909
        char *valp = val->value;
125
909
        unsigned char *tmp_data = NULL;
126
909
        long val_len;
127
128
909
        if (*policy == NULL) {
129
289
            *policy = ASN1_OCTET_STRING_new();
130
289
            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
289
            free_policy = 1;
136
289
        }
137
909
        if (CHECK_AND_SKIP_PREFIX(valp, "hex:")) {
138
335
            unsigned char *tmp_data2 = OPENSSL_hexstr2buf(valp, &val_len);
139
140
335
            if (!tmp_data2) {
141
5
                X509V3_conf_err(val);
142
5
                goto err;
143
5
            }
144
145
330
            tmp_data = OPENSSL_realloc((*policy)->data,
146
330
                (*policy)->length + val_len + 1);
147
330
            if (tmp_data) {
148
330
                (*policy)->data = tmp_data;
149
330
                memcpy(&(*policy)->data[(*policy)->length],
150
330
                    tmp_data2, val_len);
151
330
                (*policy)->length += val_len;
152
330
                (*policy)->data[(*policy)->length] = '\0';
153
330
            } 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
330
            OPENSSL_free(tmp_data2);
166
574
        } else if (CHECK_AND_SKIP_PREFIX(valp, "file:")) {
167
12
            unsigned char buf[2048];
168
12
            int n;
169
12
            BIO *b = BIO_new_file(valp, "r");
170
12
            if (!b) {
171
12
                ERR_raise(ERR_LIB_X509V3, ERR_R_BIO_LIB);
172
12
                X509V3_conf_err(val);
173
12
                goto err;
174
12
            }
175
0
            while ((n = BIO_read(b, buf, sizeof(buf))) > 0
176
0
                || (n == 0 && BIO_should_retry(b))) {
177
0
                if (!n)
178
0
                    continue;
179
180
0
                tmp_data = OPENSSL_realloc((*policy)->data,
181
0
                    (*policy)->length + n + 1);
182
183
0
                if (!tmp_data) {
184
0
                    OPENSSL_free((*policy)->data);
185
0
                    (*policy)->data = NULL;
186
0
                    (*policy)->length = 0;
187
0
                    X509V3_conf_err(val);
188
0
                    BIO_free_all(b);
189
0
                    goto err;
190
0
                }
191
192
0
                (*policy)->data = tmp_data;
193
0
                memcpy(&(*policy)->data[(*policy)->length], buf, n);
194
0
                (*policy)->length += n;
195
0
                (*policy)->data[(*policy)->length] = '\0';
196
0
            }
197
0
            BIO_free_all(b);
198
199
0
            if (n < 0) {
200
0
                ERR_raise(ERR_LIB_X509V3, ERR_R_BIO_LIB);
201
0
                X509V3_conf_err(val);
202
0
                goto err;
203
0
            }
204
562
        } else if (CHECK_AND_SKIP_PREFIX(valp, "text:")) {
205
379
            val_len = (int)strlen(valp);
206
379
            tmp_data = OPENSSL_realloc((*policy)->data,
207
379
                (*policy)->length + val_len + 1);
208
379
            if (tmp_data) {
209
379
                (*policy)->data = tmp_data;
210
379
                memcpy(&(*policy)->data[(*policy)->length],
211
379
                    val->value + 5, val_len);
212
379
                (*policy)->length += val_len;
213
379
                (*policy)->data[(*policy)->length] = '\0';
214
379
            } else {
215
                /*
216
                 * realloc failure implies the original data space is b0rked
217
                 * too!
218
                 */
219
0
                OPENSSL_free((*policy)->data);
220
0
                (*policy)->data = NULL;
221
0
                (*policy)->length = 0;
222
0
                X509V3_conf_err(val);
223
0
                goto err;
224
0
            }
225
379
        } else {
226
183
            ERR_raise(ERR_LIB_X509V3, X509V3_R_INCORRECT_POLICY_SYNTAX_TAG);
227
183
            X509V3_conf_err(val);
228
183
            goto err;
229
183
        }
230
709
        if (!tmp_data) {
231
0
            X509V3_conf_err(val);
232
0
            goto err;
233
0
        }
234
709
    }
235
4.29k
    return 1;
236
200
err:
237
200
    if (free_policy) {
238
150
        ASN1_OCTET_STRING_free(*policy);
239
150
        *policy = NULL;
240
150
    }
241
200
    return 0;
242
4.53k
}
243
244
static PROXY_CERT_INFO_EXTENSION *r2i_pci(X509V3_EXT_METHOD *method,
245
    X509V3_CTX *ctx, char *value)
246
628
{
247
628
    PROXY_CERT_INFO_EXTENSION *pci = NULL;
248
628
    STACK_OF(CONF_VALUE) *vals;
249
628
    ASN1_OBJECT *language = NULL;
250
628
    ASN1_INTEGER *pathlen = NULL;
251
628
    ASN1_OCTET_STRING *policy = NULL;
252
628
    int i, j;
253
254
628
    vals = X509V3_parse_list(value);
255
4.39k
    for (i = 0; i < sk_CONF_VALUE_num(vals); i++) {
256
4.12k
        CONF_VALUE *cnf = sk_CONF_VALUE_value(vals, i);
257
258
4.12k
        if (!cnf->name || (*cnf->name != '@' && !cnf->value)) {
259
107
            ERR_raise(ERR_LIB_X509V3, X509V3_R_INVALID_PROXY_POLICY_SETTING);
260
107
            X509V3_conf_err(cnf);
261
107
            goto err;
262
107
        }
263
4.01k
        if (*cnf->name == '@') {
264
338
            STACK_OF(CONF_VALUE) *sect;
265
338
            int success_p = 1;
266
267
338
            sect = X509V3_get_section(ctx, cnf->name + 1);
268
338
            if (!sect) {
269
13
                ERR_raise(ERR_LIB_X509V3, X509V3_R_INVALID_SECTION);
270
13
                X509V3_conf_err(cnf);
271
13
                goto err;
272
13
            }
273
1.17k
            for (j = 0; success_p && j < sk_CONF_VALUE_num(sect); j++) {
274
850
                success_p = process_pci_value(sk_CONF_VALUE_value(sect, j),
275
850
                    &language, &pathlen, &policy);
276
850
            }
277
325
            X509V3_section_free(ctx, sect);
278
325
            if (!success_p)
279
13
                goto err;
280
3.68k
        } else {
281
3.68k
            if (!process_pci_value(cnf, &language, &pathlen, &policy)) {
282
223
                X509V3_conf_err(cnf);
283
223
                goto err;
284
223
            }
285
3.68k
        }
286
4.01k
    }
287
288
    /* Language is mandatory */
289
272
    if (!language) {
290
200
        ERR_raise(ERR_LIB_X509V3,
291
200
            X509V3_R_NO_PROXY_CERT_POLICY_LANGUAGE_DEFINED);
292
200
        goto err;
293
200
    }
294
72
    i = OBJ_obj2nid(language);
295
72
    if ((i == NID_Independent || i == NID_id_ppl_inheritAll) && policy) {
296
4
        ERR_raise(ERR_LIB_X509V3,
297
4
            X509V3_R_POLICY_WHEN_PROXY_LANGUAGE_REQUIRES_NO_POLICY);
298
4
        goto err;
299
4
    }
300
301
68
    pci = PROXY_CERT_INFO_EXTENSION_new();
302
68
    if (pci == NULL) {
303
0
        ERR_raise(ERR_LIB_X509V3, ERR_R_ASN1_LIB);
304
0
        goto err;
305
0
    }
306
307
68
    pci->proxyPolicy->policyLanguage = language;
308
68
    language = NULL;
309
68
    pci->proxyPolicy->policy = policy;
310
68
    policy = NULL;
311
68
    pci->pcPathLengthConstraint = pathlen;
312
68
    pathlen = NULL;
313
68
    goto end;
314
560
err:
315
560
    ASN1_OBJECT_free(language);
316
560
    ASN1_INTEGER_free(pathlen);
317
560
    pathlen = NULL;
318
560
    ASN1_OCTET_STRING_free(policy);
319
560
    policy = NULL;
320
560
    PROXY_CERT_INFO_EXTENSION_free(pci);
321
560
    pci = NULL;
322
628
end:
323
628
    sk_CONF_VALUE_pop_free(vals, X509V3_conf_free);
324
628
    return pci;
325
560
}