Coverage Report

Created: 2025-06-13 06:58

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