Coverage Report

Created: 2026-03-09 06:55

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl/crypto/x509/by_file.c
Line
Count
Source
1
/*
2
 * Copyright 1995-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
#include <stdio.h>
11
#include <time.h>
12
#include <errno.h>
13
14
#include "internal/cryptlib.h"
15
#include <openssl/buffer.h>
16
#include <openssl/x509.h>
17
#include <openssl/pem.h>
18
#include "x509_local.h"
19
20
#include <crypto/asn1.h>
21
22
static int by_file_ctrl(X509_LOOKUP *ctx, int cmd, const char *argc,
23
    long argl, char **ret);
24
static int by_file_ctrl_ex(X509_LOOKUP *ctx, int cmd, const char *argc,
25
    long argl, char **ret, OSSL_LIB_CTX *libctx,
26
    const char *propq);
27
28
static X509_LOOKUP_METHOD x509_file_lookup = {
29
    "Load file into cache",
30
    NULL, /* new_item */
31
    NULL, /* free */
32
    NULL, /* init */
33
    NULL, /* shutdown */
34
    by_file_ctrl, /* ctrl */
35
    NULL, /* get_by_subject */
36
    NULL, /* get_by_issuer_serial */
37
    NULL, /* get_by_fingerprint */
38
    NULL, /* get_by_alias */
39
    NULL, /* get_by_subject_ex */
40
    by_file_ctrl_ex, /* ctrl_ex */
41
};
42
43
X509_LOOKUP_METHOD *X509_LOOKUP_file(void)
44
0
{
45
0
    return &x509_file_lookup;
46
0
}
47
48
static int by_file_ctrl_ex(X509_LOOKUP *ctx, int cmd, const char *argp,
49
    long argl, char **ret, OSSL_LIB_CTX *libctx,
50
    const char *propq)
51
0
{
52
0
    int ok = 0;
53
0
    const char *file;
54
55
0
    switch (cmd) {
56
0
    case X509_L_FILE_LOAD:
57
0
        if (argl == X509_FILETYPE_DEFAULT) {
58
0
            file = ossl_safe_getenv(X509_get_default_cert_file_env());
59
0
            if (file)
60
0
                ok = (X509_load_cert_crl_file_ex(ctx, file, X509_FILETYPE_PEM,
61
0
                          libctx, propq)
62
0
                    != 0);
63
0
            else
64
0
                ok = (X509_load_cert_crl_file_ex(
65
0
                          ctx, X509_get_default_cert_file(),
66
0
                          X509_FILETYPE_PEM, libctx, propq)
67
0
                    != 0);
68
69
0
            if (!ok)
70
0
                ERR_raise(ERR_LIB_X509, X509_R_LOADING_DEFAULTS);
71
0
        } else {
72
0
            if (argl == X509_FILETYPE_PEM)
73
0
                ok = (X509_load_cert_crl_file_ex(ctx, argp, X509_FILETYPE_PEM,
74
0
                          libctx, propq)
75
0
                    != 0);
76
0
            else
77
0
                ok = (X509_load_cert_file_ex(ctx, argp, (int)argl, libctx,
78
0
                          propq)
79
0
                    != 0);
80
0
        }
81
0
        break;
82
0
    }
83
0
    return ok;
84
0
}
85
86
static int by_file_ctrl(X509_LOOKUP *ctx, int cmd,
87
    const char *argp, long argl, char **ret)
88
0
{
89
0
    return by_file_ctrl_ex(ctx, cmd, argp, argl, ret, NULL, NULL);
90
0
}
91
92
int X509_load_cert_file_ex(X509_LOOKUP *ctx, const char *file, int type,
93
    OSSL_LIB_CTX *libctx, const char *propq)
94
0
{
95
0
    BIO *in = NULL;
96
0
    int count = 0;
97
0
    X509 *x = NULL;
98
99
0
    if (file == NULL) {
100
0
        ERR_raise(ERR_LIB_X509, ERR_R_PASSED_NULL_PARAMETER);
101
0
        goto err;
102
0
    }
103
104
0
    in = BIO_new(BIO_s_file());
105
106
0
    if ((in == NULL) || (BIO_read_filename(in, file) <= 0)) {
107
0
        ERR_raise(ERR_LIB_X509, ERR_R_BIO_LIB);
108
0
        goto err;
109
0
    }
110
111
0
    x = X509_new_ex(libctx, propq);
112
0
    if (x == NULL) {
113
0
        ERR_raise(ERR_LIB_X509, ERR_R_ASN1_LIB);
114
0
        goto err;
115
0
    }
116
117
0
    if (type == X509_FILETYPE_PEM) {
118
0
        for (;;) {
119
0
            ERR_set_mark();
120
0
            if (PEM_read_bio_X509_AUX(in, &x, NULL, "") == NULL) {
121
0
                if ((ERR_GET_REASON(ERR_peek_last_error()) == PEM_R_NO_START_LINE) && (count > 0)) {
122
0
                    ERR_pop_to_mark();
123
0
                    break;
124
0
                } else {
125
0
                    ERR_clear_last_mark();
126
0
                    if (count == 0) {
127
0
                        ERR_raise(ERR_LIB_X509, X509_R_NO_CERTIFICATE_FOUND);
128
0
                    } else {
129
0
                        ERR_raise(ERR_LIB_X509, ERR_R_PEM_LIB);
130
0
                        count = 0;
131
0
                    }
132
0
                    goto err;
133
0
                }
134
0
            }
135
0
            ERR_clear_last_mark();
136
0
            if (!X509_STORE_add_cert(ctx->store_ctx, x)) {
137
0
                count = 0;
138
0
                goto err;
139
0
            }
140
            /*
141
             * X509_STORE_add_cert() added a reference rather than a copy,
142
             * so we need a fresh X509 object.
143
             */
144
0
            X509_free(x);
145
0
            x = X509_new_ex(libctx, propq);
146
0
            if (x == NULL) {
147
0
                ERR_raise(ERR_LIB_X509, ERR_R_ASN1_LIB);
148
0
                count = 0;
149
0
                goto err;
150
0
            }
151
0
            count++;
152
0
        }
153
0
    } else if (type == X509_FILETYPE_ASN1) {
154
0
        if (d2i_X509_bio(in, &x) == NULL) {
155
0
            ERR_raise(ERR_LIB_X509, X509_R_NO_CERTIFICATE_FOUND);
156
0
            goto err;
157
0
        }
158
0
        count = X509_STORE_add_cert(ctx->store_ctx, x);
159
0
    } else {
160
0
        ERR_raise(ERR_LIB_X509, X509_R_BAD_X509_FILETYPE);
161
0
        goto err;
162
0
    }
163
0
err:
164
0
    X509_free(x);
165
0
    BIO_free(in);
166
0
    return count;
167
0
}
168
169
int X509_load_cert_file(X509_LOOKUP *ctx, const char *file, int type)
170
0
{
171
0
    return X509_load_cert_file_ex(ctx, file, type, NULL, NULL);
172
0
}
173
174
int X509_load_crl_file(X509_LOOKUP *ctx, const char *file, int type)
175
0
{
176
0
    BIO *in = NULL;
177
0
    int count = 0;
178
0
    X509_CRL *x = NULL;
179
180
0
    if (file == NULL) {
181
0
        ERR_raise(ERR_LIB_X509, ERR_R_PASSED_NULL_PARAMETER);
182
0
        goto err;
183
0
    }
184
185
0
    in = BIO_new(BIO_s_file());
186
187
0
    if ((in == NULL) || (BIO_read_filename(in, file) <= 0)) {
188
0
        ERR_raise(ERR_LIB_X509, ERR_R_BIO_LIB);
189
0
        goto err;
190
0
    }
191
192
0
    if (type == X509_FILETYPE_PEM) {
193
0
        for (;;) {
194
0
            x = PEM_read_bio_X509_CRL(in, NULL, NULL, "");
195
0
            if (x == NULL) {
196
0
                if ((ERR_GET_REASON(ERR_peek_last_error()) == PEM_R_NO_START_LINE) && (count > 0)) {
197
0
                    ERR_clear_error();
198
0
                    break;
199
0
                } else {
200
0
                    if (count == 0) {
201
0
                        ERR_raise(ERR_LIB_X509, X509_R_NO_CRL_FOUND);
202
0
                    } else {
203
0
                        ERR_raise(ERR_LIB_X509, ERR_R_PEM_LIB);
204
0
                        count = 0;
205
0
                    }
206
0
                    goto err;
207
0
                }
208
0
            }
209
0
            if (!X509_STORE_add_crl(ctx->store_ctx, x)) {
210
0
                count = 0;
211
0
                goto err;
212
0
            }
213
0
            count++;
214
0
            X509_CRL_free(x);
215
0
            x = NULL;
216
0
        }
217
0
    } else if (type == X509_FILETYPE_ASN1) {
218
0
        x = d2i_X509_CRL_bio(in, NULL);
219
0
        if (x == NULL) {
220
0
            ERR_raise(ERR_LIB_X509, X509_R_NO_CRL_FOUND);
221
0
            goto err;
222
0
        }
223
0
        count = X509_STORE_add_crl(ctx->store_ctx, x);
224
0
    } else {
225
0
        ERR_raise(ERR_LIB_X509, X509_R_BAD_X509_FILETYPE);
226
0
        goto err;
227
0
    }
228
0
err:
229
0
    X509_CRL_free(x);
230
0
    BIO_free(in);
231
0
    return count;
232
0
}
233
234
int X509_load_cert_crl_file_ex(X509_LOOKUP *ctx, const char *file, int type,
235
    OSSL_LIB_CTX *libctx, const char *propq)
236
0
{
237
0
    STACK_OF(X509_INFO) *inf = NULL;
238
0
    X509_INFO *itmp = NULL;
239
0
    BIO *in = NULL;
240
0
    int i, count = 0;
241
242
0
    if (type != X509_FILETYPE_PEM)
243
0
        return X509_load_cert_file_ex(ctx, file, type, libctx, propq);
244
#if defined(OPENSSL_SYS_WINDOWS)
245
    in = BIO_new_file(file, "rb");
246
#else
247
0
    in = BIO_new_file(file, "r");
248
0
#endif
249
0
    if (in == NULL) {
250
0
        ERR_raise(ERR_LIB_X509, ERR_R_BIO_LIB);
251
0
        return 0;
252
0
    }
253
0
    inf = PEM_X509_INFO_read_bio_ex(in, NULL, NULL, "", libctx, propq);
254
0
    BIO_free(in);
255
0
    if (inf == NULL) {
256
0
        ERR_raise(ERR_LIB_X509, ERR_R_PEM_LIB);
257
0
        return 0;
258
0
    }
259
0
    for (i = 0; i < sk_X509_INFO_num(inf); i++) {
260
0
        itmp = sk_X509_INFO_value(inf, i);
261
0
        if (itmp->x509) {
262
0
            if (!X509_STORE_add_cert(ctx->store_ctx, itmp->x509)) {
263
0
                count = 0;
264
0
                goto err;
265
0
            }
266
0
            count++;
267
0
        }
268
0
        if (itmp->crl) {
269
0
            if (!X509_STORE_add_crl(ctx->store_ctx, itmp->crl)) {
270
0
                count = 0;
271
0
                goto err;
272
0
            }
273
0
            count++;
274
0
        }
275
0
    }
276
0
    if (count == 0)
277
0
        ERR_raise(ERR_LIB_X509, X509_R_NO_CERTIFICATE_OR_CRL_FOUND);
278
0
err:
279
0
    sk_X509_INFO_pop_free(inf, X509_INFO_free);
280
0
    return count;
281
0
}
282
283
int X509_load_cert_crl_file(X509_LOOKUP *ctx, const char *file, int type)
284
0
{
285
0
    return X509_load_cert_crl_file_ex(ctx, file, type, NULL, NULL);
286
0
}