Coverage Report

Created: 2025-12-31 06:58

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl35/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
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
0
            else
62
0
                ok = (X509_load_cert_crl_file_ex(
63
0
                          ctx, X509_get_default_cert_file(),
64
0
                          X509_FILETYPE_PEM, libctx, propq)
65
0
                    != 0);
66
67
0
            if (!ok)
68
0
                ERR_raise(ERR_LIB_X509, X509_R_LOADING_DEFAULTS);
69
0
        } else {
70
0
            if (argl == X509_FILETYPE_PEM)
71
0
                ok = (X509_load_cert_crl_file_ex(ctx, argp, X509_FILETYPE_PEM,
72
0
                          libctx, propq)
73
0
                    != 0);
74
0
            else
75
0
                ok = (X509_load_cert_file_ex(ctx, argp, (int)argl, libctx,
76
0
                          propq)
77
0
                    != 0);
78
0
        }
79
0
        break;
80
0
    }
81
0
    return ok;
82
0
}
83
84
static int by_file_ctrl(X509_LOOKUP *ctx, int cmd,
85
    const char *argp, long argl, char **ret)
86
0
{
87
0
    return by_file_ctrl_ex(ctx, cmd, argp, argl, ret, NULL, NULL);
88
0
}
89
90
int X509_load_cert_file_ex(X509_LOOKUP *ctx, const char *file, int type,
91
    OSSL_LIB_CTX *libctx, const char *propq)
92
0
{
93
0
    BIO *in = NULL;
94
0
    int count = 0;
95
0
    X509 *x = NULL;
96
97
0
    if (file == NULL) {
98
0
        ERR_raise(ERR_LIB_X509, ERR_R_PASSED_NULL_PARAMETER);
99
0
        goto err;
100
0
    }
101
102
0
    in = BIO_new(BIO_s_file());
103
104
0
    if ((in == NULL) || (BIO_read_filename(in, file) <= 0)) {
105
0
        ERR_raise(ERR_LIB_X509, ERR_R_BIO_LIB);
106
0
        goto err;
107
0
    }
108
109
0
    x = X509_new_ex(libctx, propq);
110
0
    if (x == NULL) {
111
0
        ERR_raise(ERR_LIB_X509, ERR_R_ASN1_LIB);
112
0
        goto err;
113
0
    }
114
115
0
    if (type == X509_FILETYPE_PEM) {
116
0
        for (;;) {
117
0
            ERR_set_mark();
118
0
            if (PEM_read_bio_X509_AUX(in, &x, NULL, "") == NULL) {
119
0
                if ((ERR_GET_REASON(ERR_peek_last_error()) == PEM_R_NO_START_LINE) && (count > 0)) {
120
0
                    ERR_pop_to_mark();
121
0
                    break;
122
0
                } else {
123
0
                    ERR_clear_last_mark();
124
0
                    if (count == 0) {
125
0
                        ERR_raise(ERR_LIB_X509, X509_R_NO_CERTIFICATE_FOUND);
126
0
                    } else {
127
0
                        ERR_raise(ERR_LIB_X509, ERR_R_PEM_LIB);
128
0
                        count = 0;
129
0
                    }
130
0
                    goto err;
131
0
                }
132
0
            }
133
0
            ERR_clear_last_mark();
134
0
            if (!X509_STORE_add_cert(ctx->store_ctx, x)) {
135
0
                count = 0;
136
0
                goto err;
137
0
            }
138
            /*
139
             * X509_STORE_add_cert() added a reference rather than a copy,
140
             * so we need a fresh X509 object.
141
             */
142
0
            X509_free(x);
143
0
            x = X509_new_ex(libctx, propq);
144
0
            if (x == NULL) {
145
0
                ERR_raise(ERR_LIB_X509, ERR_R_ASN1_LIB);
146
0
                count = 0;
147
0
                goto err;
148
0
            }
149
0
            count++;
150
0
        }
151
0
    } else if (type == X509_FILETYPE_ASN1) {
152
0
        if (d2i_X509_bio(in, &x) == NULL) {
153
0
            ERR_raise(ERR_LIB_X509, X509_R_NO_CERTIFICATE_FOUND);
154
0
            goto err;
155
0
        }
156
0
        count = X509_STORE_add_cert(ctx->store_ctx, x);
157
0
    } else {
158
0
        ERR_raise(ERR_LIB_X509, X509_R_BAD_X509_FILETYPE);
159
0
        goto err;
160
0
    }
161
0
err:
162
0
    X509_free(x);
163
0
    BIO_free(in);
164
0
    return count;
165
0
}
166
167
int X509_load_cert_file(X509_LOOKUP *ctx, const char *file, int type)
168
0
{
169
0
    return X509_load_cert_file_ex(ctx, file, type, NULL, NULL);
170
0
}
171
172
int X509_load_crl_file(X509_LOOKUP *ctx, const char *file, int type)
173
0
{
174
0
    BIO *in = NULL;
175
0
    int count = 0;
176
0
    X509_CRL *x = NULL;
177
178
0
    if (file == NULL) {
179
0
        ERR_raise(ERR_LIB_X509, ERR_R_PASSED_NULL_PARAMETER);
180
0
        goto err;
181
0
    }
182
183
0
    in = BIO_new(BIO_s_file());
184
185
0
    if ((in == NULL) || (BIO_read_filename(in, file) <= 0)) {
186
0
        ERR_raise(ERR_LIB_X509, ERR_R_BIO_LIB);
187
0
        goto err;
188
0
    }
189
190
0
    if (type == X509_FILETYPE_PEM) {
191
0
        for (;;) {
192
0
            x = PEM_read_bio_X509_CRL(in, NULL, NULL, "");
193
0
            if (x == NULL) {
194
0
                if ((ERR_GET_REASON(ERR_peek_last_error()) == PEM_R_NO_START_LINE) && (count > 0)) {
195
0
                    ERR_clear_error();
196
0
                    break;
197
0
                } else {
198
0
                    if (count == 0) {
199
0
                        ERR_raise(ERR_LIB_X509, X509_R_NO_CRL_FOUND);
200
0
                    } else {
201
0
                        ERR_raise(ERR_LIB_X509, ERR_R_PEM_LIB);
202
0
                        count = 0;
203
0
                    }
204
0
                    goto err;
205
0
                }
206
0
            }
207
0
            if (!X509_STORE_add_crl(ctx->store_ctx, x)) {
208
0
                count = 0;
209
0
                goto err;
210
0
            }
211
0
            count++;
212
0
            X509_CRL_free(x);
213
0
            x = NULL;
214
0
        }
215
0
    } else if (type == X509_FILETYPE_ASN1) {
216
0
        x = d2i_X509_CRL_bio(in, NULL);
217
0
        if (x == NULL) {
218
0
            ERR_raise(ERR_LIB_X509, X509_R_NO_CRL_FOUND);
219
0
            goto err;
220
0
        }
221
0
        count = X509_STORE_add_crl(ctx->store_ctx, x);
222
0
    } else {
223
0
        ERR_raise(ERR_LIB_X509, X509_R_BAD_X509_FILETYPE);
224
0
        goto err;
225
0
    }
226
0
err:
227
0
    X509_CRL_free(x);
228
0
    BIO_free(in);
229
0
    return count;
230
0
}
231
232
int X509_load_cert_crl_file_ex(X509_LOOKUP *ctx, const char *file, int type,
233
    OSSL_LIB_CTX *libctx, const char *propq)
234
0
{
235
0
    STACK_OF(X509_INFO) *inf = NULL;
236
0
    X509_INFO *itmp = NULL;
237
0
    BIO *in = NULL;
238
0
    int i, count = 0;
239
240
0
    if (type != X509_FILETYPE_PEM)
241
0
        return X509_load_cert_file_ex(ctx, file, type, libctx, propq);
242
#if defined(OPENSSL_SYS_WINDOWS)
243
    in = BIO_new_file(file, "rb");
244
#else
245
0
    in = BIO_new_file(file, "r");
246
0
#endif
247
0
    if (in == NULL) {
248
0
        ERR_raise(ERR_LIB_X509, ERR_R_BIO_LIB);
249
0
        return 0;
250
0
    }
251
0
    inf = PEM_X509_INFO_read_bio_ex(in, NULL, NULL, "", libctx, propq);
252
0
    BIO_free(in);
253
0
    if (inf == NULL) {
254
0
        ERR_raise(ERR_LIB_X509, ERR_R_PEM_LIB);
255
0
        return 0;
256
0
    }
257
0
    for (i = 0; i < sk_X509_INFO_num(inf); i++) {
258
0
        itmp = sk_X509_INFO_value(inf, i);
259
0
        if (itmp->x509) {
260
0
            if (!X509_STORE_add_cert(ctx->store_ctx, itmp->x509)) {
261
0
                count = 0;
262
0
                goto err;
263
0
            }
264
0
            count++;
265
0
        }
266
0
        if (itmp->crl) {
267
0
            if (!X509_STORE_add_crl(ctx->store_ctx, itmp->crl)) {
268
0
                count = 0;
269
0
                goto err;
270
0
            }
271
0
            count++;
272
0
        }
273
0
    }
274
0
    if (count == 0)
275
0
        ERR_raise(ERR_LIB_X509, X509_R_NO_CERTIFICATE_OR_CRL_FOUND);
276
0
err:
277
0
    sk_X509_INFO_pop_free(inf, X509_INFO_free);
278
0
    return count;
279
0
}
280
281
int X509_load_cert_crl_file(X509_LOOKUP *ctx, const char *file, int type)
282
0
{
283
0
    return X509_load_cert_crl_file_ex(ctx, file, type, NULL, NULL);
284
0
}