Coverage Report

Created: 2026-02-14 07:20

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl30/crypto/x509/by_file.c
Line
Count
Source
1
/*
2
 * Copyright 1995-2021 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
static int by_file_ctrl(X509_LOOKUP *ctx, int cmd, const char *argc,
21
    long argl, char **ret);
22
static int by_file_ctrl_ex(X509_LOOKUP *ctx, int cmd, const char *argc,
23
    long argl, char **ret, OSSL_LIB_CTX *libctx,
24
    const char *propq);
25
26
static X509_LOOKUP_METHOD x509_file_lookup = {
27
    "Load file into cache",
28
    NULL, /* new_item */
29
    NULL, /* free */
30
    NULL, /* init */
31
    NULL, /* shutdown */
32
    by_file_ctrl, /* ctrl */
33
    NULL, /* get_by_subject */
34
    NULL, /* get_by_issuer_serial */
35
    NULL, /* get_by_fingerprint */
36
    NULL, /* get_by_alias */
37
    NULL, /* get_by_subject_ex */
38
    by_file_ctrl_ex, /* ctrl_ex */
39
};
40
41
X509_LOOKUP_METHOD *X509_LOOKUP_file(void)
42
0
{
43
0
    return &x509_file_lookup;
44
0
}
45
46
static int by_file_ctrl_ex(X509_LOOKUP *ctx, int cmd, const char *argp,
47
    long argl, char **ret, OSSL_LIB_CTX *libctx,
48
    const char *propq)
49
0
{
50
0
    int ok = 0;
51
0
    const char *file;
52
53
0
    switch (cmd) {
54
0
    case X509_L_FILE_LOAD:
55
0
        if (argl == X509_FILETYPE_DEFAULT) {
56
0
            file = ossl_safe_getenv(X509_get_default_cert_file_env());
57
0
            if (file)
58
0
                ok = (X509_load_cert_crl_file_ex(ctx, file, X509_FILETYPE_PEM,
59
0
                          libctx, propq)
60
0
                    != 0);
61
62
0
            else
63
0
                ok = (X509_load_cert_crl_file_ex(
64
0
                          ctx, X509_get_default_cert_file(),
65
0
                          X509_FILETYPE_PEM, libctx, propq)
66
0
                    != 0);
67
68
0
            if (!ok) {
69
0
                ERR_raise(ERR_LIB_X509, X509_R_LOADING_DEFAULTS);
70
0
            }
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
    int ret = 0;
96
0
    BIO *in = NULL;
97
0
    int i, count = 0;
98
0
    X509 *x = NULL;
99
100
0
    in = BIO_new(BIO_s_file());
101
102
0
    if ((in == NULL) || (BIO_read_filename(in, file) <= 0)) {
103
0
        ERR_raise(ERR_LIB_X509, ERR_R_SYS_LIB);
104
0
        goto err;
105
0
    }
106
107
0
    if (type != X509_FILETYPE_PEM && type != X509_FILETYPE_ASN1) {
108
0
        ERR_raise(ERR_LIB_X509, X509_R_BAD_X509_FILETYPE);
109
0
        goto err;
110
0
    }
111
0
    x = X509_new_ex(libctx, propq);
112
0
    if (x == NULL) {
113
0
        ERR_raise(ERR_LIB_X509, ERR_R_MALLOC_FAILURE);
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
                    goto err;
127
0
                }
128
0
            }
129
0
            ERR_clear_last_mark();
130
0
            i = X509_STORE_add_cert(ctx->store_ctx, x);
131
0
            if (!i)
132
0
                goto err;
133
0
            count++;
134
0
            X509_free(x);
135
0
            x = NULL;
136
0
        }
137
0
        ret = count;
138
0
    } else if (type == X509_FILETYPE_ASN1) {
139
0
        if (d2i_X509_bio(in, &x) == NULL) {
140
0
            ERR_raise(ERR_LIB_X509, ERR_R_ASN1_LIB);
141
0
            goto err;
142
0
        }
143
0
        i = X509_STORE_add_cert(ctx->store_ctx, x);
144
0
        if (!i)
145
0
            goto err;
146
0
        ret = i;
147
0
    }
148
0
    if (ret == 0)
149
0
        ERR_raise(ERR_LIB_X509, X509_R_NO_CERTIFICATE_FOUND);
150
0
err:
151
0
    X509_free(x);
152
0
    BIO_free(in);
153
0
    return ret;
154
0
}
155
156
int X509_load_cert_file(X509_LOOKUP *ctx, const char *file, int type)
157
0
{
158
0
    return X509_load_cert_file_ex(ctx, file, type, NULL, NULL);
159
0
}
160
161
int X509_load_crl_file(X509_LOOKUP *ctx, const char *file, int type)
162
0
{
163
0
    int ret = 0;
164
0
    BIO *in = NULL;
165
0
    int i, count = 0;
166
0
    X509_CRL *x = NULL;
167
168
0
    in = BIO_new(BIO_s_file());
169
170
0
    if ((in == NULL) || (BIO_read_filename(in, file) <= 0)) {
171
0
        ERR_raise(ERR_LIB_X509, ERR_R_SYS_LIB);
172
0
        goto err;
173
0
    }
174
175
0
    if (type == X509_FILETYPE_PEM) {
176
0
        for (;;) {
177
0
            x = PEM_read_bio_X509_CRL(in, NULL, NULL, "");
178
0
            if (x == NULL) {
179
0
                if ((ERR_GET_REASON(ERR_peek_last_error()) == PEM_R_NO_START_LINE) && (count > 0)) {
180
0
                    ERR_clear_error();
181
0
                    break;
182
0
                } else {
183
0
                    ERR_raise(ERR_LIB_X509, ERR_R_PEM_LIB);
184
0
                    goto err;
185
0
                }
186
0
            }
187
0
            i = X509_STORE_add_crl(ctx->store_ctx, x);
188
0
            if (!i)
189
0
                goto err;
190
0
            count++;
191
0
            X509_CRL_free(x);
192
0
            x = NULL;
193
0
        }
194
0
        ret = count;
195
0
    } else if (type == X509_FILETYPE_ASN1) {
196
0
        x = d2i_X509_CRL_bio(in, NULL);
197
0
        if (x == NULL) {
198
0
            ERR_raise(ERR_LIB_X509, ERR_R_ASN1_LIB);
199
0
            goto err;
200
0
        }
201
0
        i = X509_STORE_add_crl(ctx->store_ctx, x);
202
0
        if (!i)
203
0
            goto err;
204
0
        ret = i;
205
0
    } else {
206
0
        ERR_raise(ERR_LIB_X509, X509_R_BAD_X509_FILETYPE);
207
0
        goto err;
208
0
    }
209
0
    if (ret == 0)
210
0
        ERR_raise(ERR_LIB_X509, X509_R_NO_CRL_FOUND);
211
0
err:
212
0
    X509_CRL_free(x);
213
0
    BIO_free(in);
214
0
    return ret;
215
0
}
216
217
int X509_load_cert_crl_file_ex(X509_LOOKUP *ctx, const char *file, int type,
218
    OSSL_LIB_CTX *libctx, const char *propq)
219
0
{
220
0
    STACK_OF(X509_INFO) *inf;
221
0
    X509_INFO *itmp;
222
0
    BIO *in;
223
0
    int i, count = 0;
224
225
0
    if (type != X509_FILETYPE_PEM)
226
0
        return X509_load_cert_file_ex(ctx, file, type, libctx, propq);
227
0
    in = BIO_new_file(file, "r");
228
0
    if (!in) {
229
0
        ERR_raise(ERR_LIB_X509, ERR_R_SYS_LIB);
230
0
        return 0;
231
0
    }
232
0
    inf = PEM_X509_INFO_read_bio_ex(in, NULL, NULL, "", libctx, propq);
233
0
    BIO_free(in);
234
0
    if (!inf) {
235
0
        ERR_raise(ERR_LIB_X509, ERR_R_PEM_LIB);
236
0
        return 0;
237
0
    }
238
0
    for (i = 0; i < sk_X509_INFO_num(inf); i++) {
239
0
        itmp = sk_X509_INFO_value(inf, i);
240
0
        if (itmp->x509) {
241
0
            if (!X509_STORE_add_cert(ctx->store_ctx, itmp->x509))
242
0
                goto err;
243
0
            count++;
244
0
        }
245
0
        if (itmp->crl) {
246
0
            if (!X509_STORE_add_crl(ctx->store_ctx, itmp->crl))
247
0
                goto err;
248
0
            count++;
249
0
        }
250
0
    }
251
0
    if (count == 0)
252
0
        ERR_raise(ERR_LIB_X509, X509_R_NO_CERTIFICATE_OR_CRL_FOUND);
253
0
err:
254
0
    sk_X509_INFO_pop_free(inf, X509_INFO_free);
255
0
    return count;
256
0
}
257
258
int X509_load_cert_crl_file(X509_LOOKUP *ctx, const char *file, int type)
259
0
{
260
0
    return X509_load_cert_crl_file_ex(ctx, file, type, NULL, NULL);
261
0
}