Coverage Report

Created: 2025-06-13 06:58

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