Coverage Report

Created: 2025-12-31 06:58

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl33/crypto/x509/by_file.c
Line
Count
Source
1
/*
2
 * Copyright 1995-2024 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
    in = BIO_new(BIO_s_file());
98
99
0
    if ((in == NULL) || (BIO_read_filename(in, file) <= 0)) {
100
0
        ERR_raise(ERR_LIB_X509, ERR_R_BIO_LIB);
101
0
        goto err;
102
0
    }
103
104
0
    x = X509_new_ex(libctx, propq);
105
0
    if (x == NULL) {
106
0
        ERR_raise(ERR_LIB_X509, ERR_R_ASN1_LIB);
107
0
        goto err;
108
0
    }
109
110
0
    if (type == X509_FILETYPE_PEM) {
111
0
        for (;;) {
112
0
            ERR_set_mark();
113
0
            if (PEM_read_bio_X509_AUX(in, &x, NULL, "") == NULL) {
114
0
                if ((ERR_GET_REASON(ERR_peek_last_error()) == PEM_R_NO_START_LINE) && (count > 0)) {
115
0
                    ERR_pop_to_mark();
116
0
                    break;
117
0
                } else {
118
0
                    ERR_clear_last_mark();
119
0
                    if (count == 0) {
120
0
                        ERR_raise(ERR_LIB_X509, X509_R_NO_CERTIFICATE_FOUND);
121
0
                    } else {
122
0
                        ERR_raise(ERR_LIB_X509, ERR_R_PEM_LIB);
123
0
                        count = 0;
124
0
                    }
125
0
                    goto err;
126
0
                }
127
0
            }
128
0
            ERR_clear_last_mark();
129
0
            if (!X509_STORE_add_cert(ctx->store_ctx, x)) {
130
0
                count = 0;
131
0
                goto err;
132
0
            }
133
            /*
134
             * X509_STORE_add_cert() added a reference rather than a copy,
135
             * so we need a fresh X509 object.
136
             */
137
0
            X509_free(x);
138
0
            x = X509_new_ex(libctx, propq);
139
0
            if (x == NULL) {
140
0
                ERR_raise(ERR_LIB_X509, ERR_R_ASN1_LIB);
141
0
                count = 0;
142
0
                goto err;
143
0
            }
144
0
            count++;
145
0
        }
146
0
    } else if (type == X509_FILETYPE_ASN1) {
147
0
        if (d2i_X509_bio(in, &x) == NULL) {
148
0
            ERR_raise(ERR_LIB_X509, X509_R_NO_CERTIFICATE_FOUND);
149
0
            goto err;
150
0
        }
151
0
        count = X509_STORE_add_cert(ctx->store_ctx, x);
152
0
    } else {
153
0
        ERR_raise(ERR_LIB_X509, X509_R_BAD_X509_FILETYPE);
154
0
        goto err;
155
0
    }
156
0
err:
157
0
    X509_free(x);
158
0
    BIO_free(in);
159
0
    return count;
160
0
}
161
162
int X509_load_cert_file(X509_LOOKUP *ctx, const char *file, int type)
163
0
{
164
0
    return X509_load_cert_file_ex(ctx, file, type, NULL, NULL);
165
0
}
166
167
int X509_load_crl_file(X509_LOOKUP *ctx, const char *file, int type)
168
0
{
169
0
    BIO *in = NULL;
170
0
    int count = 0;
171
0
    X509_CRL *x = NULL;
172
173
0
    in = BIO_new(BIO_s_file());
174
175
0
    if ((in == NULL) || (BIO_read_filename(in, file) <= 0)) {
176
0
        ERR_raise(ERR_LIB_X509, ERR_R_BIO_LIB);
177
0
        goto err;
178
0
    }
179
180
0
    if (type == X509_FILETYPE_PEM) {
181
0
        for (;;) {
182
0
            x = PEM_read_bio_X509_CRL(in, NULL, NULL, "");
183
0
            if (x == NULL) {
184
0
                if ((ERR_GET_REASON(ERR_peek_last_error()) == PEM_R_NO_START_LINE) && (count > 0)) {
185
0
                    ERR_clear_error();
186
0
                    break;
187
0
                } else {
188
0
                    if (count == 0) {
189
0
                        ERR_raise(ERR_LIB_X509, X509_R_NO_CRL_FOUND);
190
0
                    } else {
191
0
                        ERR_raise(ERR_LIB_X509, ERR_R_PEM_LIB);
192
0
                        count = 0;
193
0
                    }
194
0
                    goto err;
195
0
                }
196
0
            }
197
0
            if (!X509_STORE_add_crl(ctx->store_ctx, x)) {
198
0
                count = 0;
199
0
                goto err;
200
0
            }
201
0
            count++;
202
0
            X509_CRL_free(x);
203
0
            x = NULL;
204
0
        }
205
0
    } else if (type == X509_FILETYPE_ASN1) {
206
0
        x = d2i_X509_CRL_bio(in, NULL);
207
0
        if (x == NULL) {
208
0
            ERR_raise(ERR_LIB_X509, X509_R_NO_CRL_FOUND);
209
0
            goto err;
210
0
        }
211
0
        count = X509_STORE_add_crl(ctx->store_ctx, x);
212
0
    } else {
213
0
        ERR_raise(ERR_LIB_X509, X509_R_BAD_X509_FILETYPE);
214
0
        goto err;
215
0
    }
216
0
err:
217
0
    X509_CRL_free(x);
218
0
    BIO_free(in);
219
0
    return count;
220
0
}
221
222
int X509_load_cert_crl_file_ex(X509_LOOKUP *ctx, const char *file, int type,
223
    OSSL_LIB_CTX *libctx, const char *propq)
224
0
{
225
0
    STACK_OF(X509_INFO) *inf = NULL;
226
0
    X509_INFO *itmp = NULL;
227
0
    BIO *in = NULL;
228
0
    int i, count = 0;
229
230
0
    if (type != X509_FILETYPE_PEM)
231
0
        return X509_load_cert_file_ex(ctx, file, type, libctx, propq);
232
0
    in = BIO_new_file(file, "r");
233
0
    if (in == NULL) {
234
0
        ERR_raise(ERR_LIB_X509, ERR_R_BIO_LIB);
235
0
        return 0;
236
0
    }
237
0
    inf = PEM_X509_INFO_read_bio_ex(in, NULL, NULL, "", libctx, propq);
238
0
    BIO_free(in);
239
0
    if (inf == NULL) {
240
0
        ERR_raise(ERR_LIB_X509, ERR_R_PEM_LIB);
241
0
        return 0;
242
0
    }
243
0
    for (i = 0; i < sk_X509_INFO_num(inf); i++) {
244
0
        itmp = sk_X509_INFO_value(inf, i);
245
0
        if (itmp->x509) {
246
0
            if (!X509_STORE_add_cert(ctx->store_ctx, itmp->x509)) {
247
0
                count = 0;
248
0
                goto err;
249
0
            }
250
0
            count++;
251
0
        }
252
0
        if (itmp->crl) {
253
0
            if (!X509_STORE_add_crl(ctx->store_ctx, itmp->crl)) {
254
0
                count = 0;
255
0
                goto err;
256
0
            }
257
0
            count++;
258
0
        }
259
0
    }
260
0
    if (count == 0)
261
0
        ERR_raise(ERR_LIB_X509, X509_R_NO_CERTIFICATE_OR_CRL_FOUND);
262
0
err:
263
0
    sk_X509_INFO_pop_free(inf, X509_INFO_free);
264
0
    return count;
265
0
}
266
267
int X509_load_cert_crl_file(X509_LOOKUP *ctx, const char *file, int type)
268
0
{
269
0
    return X509_load_cert_crl_file_ex(ctx, file, type, NULL, NULL);
270
0
}