]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/x509/by_file.c
Copyright year updates
[thirdparty/openssl.git] / crypto / x509 / by_file.c
CommitLineData
b1322259 1/*
b6461792 2 * Copyright 1995-2024 The OpenSSL Project Authors. All Rights Reserved.
d02b48c6 3 *
3e4b43b9 4 * Licensed under the Apache License 2.0 (the "License"). You may not use
b1322259
RS
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
d02b48c6
RE
8 */
9
10#include <stdio.h>
11#include <time.h>
12#include <errno.h>
d02b48c6 13
b39fc560 14#include "internal/cryptlib.h"
ec577822
BM
15#include <openssl/buffer.h>
16#include <openssl/x509.h>
17#include <openssl/pem.h>
706457b7 18#include "x509_local.h"
d02b48c6 19
303c0028 20static int by_file_ctrl(X509_LOOKUP *ctx, int cmd, const char *argc,
0f113f3e 21 long argl, char **ret);
d8652be0 22static int by_file_ctrl_ex(X509_LOOKUP *ctx, int cmd, const char *argc,
b4250010 23 long argl, char **ret, OSSL_LIB_CTX *libctx,
d8652be0 24 const char *propq);
6725682d
SL
25
26
df2ee0e2 27static X509_LOOKUP_METHOD x509_file_lookup = {
0f113f3e 28 "Load file into cache",
7fcdbd83 29 NULL, /* new_item */
0f113f3e
MC
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 */
d8652be0 38 NULL, /* get_by_subject_ex */
746f3674 39 by_file_ctrl_ex, /* ctrl_ex */
0f113f3e 40};
d02b48c6 41
6b691a5c 42X509_LOOKUP_METHOD *X509_LOOKUP_file(void)
0f113f3e 43{
26a7d938 44 return &x509_file_lookup;
0f113f3e 45}
d02b48c6 46
d8652be0 47static int by_file_ctrl_ex(X509_LOOKUP *ctx, int cmd, const char *argp,
b4250010 48 long argl, char **ret, OSSL_LIB_CTX *libctx,
d8652be0 49 const char *propq)
0f113f3e
MC
50{
51 int ok = 0;
a020f54c 52 const char *file;
d02b48c6 53
0f113f3e
MC
54 switch (cmd) {
55 case X509_L_FILE_LOAD:
56 if (argl == X509_FILETYPE_DEFAULT) {
5c39a55d 57 file = ossl_safe_getenv(X509_get_default_cert_file_env());
0f113f3e 58 if (file)
d8652be0
MC
59 ok = (X509_load_cert_crl_file_ex(ctx, file, X509_FILETYPE_PEM,
60 libctx, propq) != 0);
0f113f3e 61 else
d8652be0 62 ok = (X509_load_cert_crl_file_ex(
6725682d
SL
63 ctx, X509_get_default_cert_file(),
64 X509_FILETYPE_PEM, libctx, propq) != 0);
4387f478 65
ae29622f 66 if (!ok)
9311d0c4 67 ERR_raise(ERR_LIB_X509, X509_R_LOADING_DEFAULTS);
0f113f3e
MC
68 } else {
69 if (argl == X509_FILETYPE_PEM)
d8652be0
MC
70 ok = (X509_load_cert_crl_file_ex(ctx, argp, X509_FILETYPE_PEM,
71 libctx, propq) != 0);
0f113f3e 72 else
d8652be0
MC
73 ok = (X509_load_cert_file_ex(ctx, argp, (int)argl, libctx,
74 propq) != 0);
0f113f3e
MC
75 }
76 break;
77 }
26a7d938 78 return ok;
0f113f3e 79}
d02b48c6 80
6725682d
SL
81static int by_file_ctrl(X509_LOOKUP *ctx, int cmd,
82 const char *argp, long argl, char **ret)
83{
d8652be0 84 return by_file_ctrl_ex(ctx, cmd, argp, argl, ret, NULL, NULL);
6725682d
SL
85}
86
d8652be0 87int X509_load_cert_file_ex(X509_LOOKUP *ctx, const char *file, int type,
b4250010 88 OSSL_LIB_CTX *libctx, const char *propq)
0f113f3e 89{
0f113f3e 90 BIO *in = NULL;
ae29622f 91 int count = 0;
0f113f3e 92 X509 *x = NULL;
d02b48c6 93
9982cbbb 94 in = BIO_new(BIO_s_file());
d02b48c6 95
0f113f3e 96 if ((in == NULL) || (BIO_read_filename(in, file) <= 0)) {
ae29622f 97 ERR_raise(ERR_LIB_X509, ERR_R_BIO_LIB);
6725682d
SL
98 goto err;
99 }
100
d8652be0 101 x = X509_new_ex(libctx, propq);
6725682d 102 if (x == NULL) {
e077455e 103 ERR_raise(ERR_LIB_X509, ERR_R_ASN1_LIB);
0f113f3e
MC
104 goto err;
105 }
d02b48c6 106
0f113f3e
MC
107 if (type == X509_FILETYPE_PEM) {
108 for (;;) {
afecd85d 109 ERR_set_mark();
6725682d 110 if (PEM_read_bio_X509_AUX(in, &x, NULL, "") == NULL) {
0f113f3e
MC
111 if ((ERR_GET_REASON(ERR_peek_last_error()) ==
112 PEM_R_NO_START_LINE) && (count > 0)) {
afecd85d 113 ERR_pop_to_mark();
0f113f3e
MC
114 break;
115 } else {
afecd85d 116 ERR_clear_last_mark();
ae29622f 117 if (count == 0) {
118 ERR_raise(ERR_LIB_X509, X509_R_NO_CERTIFICATE_FOUND);
119 } else {
120 ERR_raise(ERR_LIB_X509, ERR_R_PEM_LIB);
121 count = 0;
122 }
0f113f3e
MC
123 goto err;
124 }
125 }
afecd85d 126 ERR_clear_last_mark();
ae29622f 127 if (!X509_STORE_add_cert(ctx->store_ctx, x)) {
128 count = 0;
0f113f3e 129 goto err;
ae29622f 130 }
20c680de 131 /*
132 * X509_STORE_add_cert() added a reference rather than a copy,
133 * so we need a fresh X509 object.
134 */
135 X509_free(x);
136 x = X509_new_ex(libctx, propq);
137 if (x == NULL) {
138 ERR_raise(ERR_LIB_X509, ERR_R_ASN1_LIB);
139 count = 0;
140 goto err;
141 }
0f113f3e 142 count++;
0f113f3e 143 }
0f113f3e 144 } else if (type == X509_FILETYPE_ASN1) {
6725682d 145 if (d2i_X509_bio(in, &x) == NULL) {
ae29622f 146 ERR_raise(ERR_LIB_X509, X509_R_NO_CERTIFICATE_FOUND);
0f113f3e
MC
147 goto err;
148 }
ae29622f 149 count = X509_STORE_add_cert(ctx->store_ctx, x);
150 } else {
151 ERR_raise(ERR_LIB_X509, X509_R_BAD_X509_FILETYPE);
152 goto err;
0f113f3e
MC
153 }
154 err:
222561fe 155 X509_free(x);
ca3a82c3 156 BIO_free(in);
ae29622f 157 return count;
0f113f3e 158}
d02b48c6 159
6725682d
SL
160int X509_load_cert_file(X509_LOOKUP *ctx, const char *file, int type)
161{
d8652be0 162 return X509_load_cert_file_ex(ctx, file, type, NULL, NULL);
6725682d
SL
163}
164
6b691a5c 165int X509_load_crl_file(X509_LOOKUP *ctx, const char *file, int type)
0f113f3e 166{
0f113f3e 167 BIO *in = NULL;
ae29622f 168 int count = 0;
0f113f3e 169 X509_CRL *x = NULL;
58964a49 170
9982cbbb 171 in = BIO_new(BIO_s_file());
58964a49 172
0f113f3e 173 if ((in == NULL) || (BIO_read_filename(in, file) <= 0)) {
ae29622f 174 ERR_raise(ERR_LIB_X509, ERR_R_BIO_LIB);
0f113f3e
MC
175 goto err;
176 }
58964a49 177
0f113f3e
MC
178 if (type == X509_FILETYPE_PEM) {
179 for (;;) {
db854bb1 180 x = PEM_read_bio_X509_CRL(in, NULL, NULL, "");
0f113f3e
MC
181 if (x == NULL) {
182 if ((ERR_GET_REASON(ERR_peek_last_error()) ==
183 PEM_R_NO_START_LINE) && (count > 0)) {
184 ERR_clear_error();
185 break;
186 } else {
ae29622f 187 if (count == 0) {
188 ERR_raise(ERR_LIB_X509, X509_R_NO_CRL_FOUND);
189 } else {
190 ERR_raise(ERR_LIB_X509, ERR_R_PEM_LIB);
191 count = 0;
192 }
0f113f3e
MC
193 goto err;
194 }
195 }
ae29622f 196 if (!X509_STORE_add_crl(ctx->store_ctx, x)) {
197 count = 0;
0f113f3e 198 goto err;
ae29622f 199 }
0f113f3e 200 count++;
6134e8e6
DB
201 X509_CRL_free(x);
202 x = NULL;
0f113f3e 203 }
0f113f3e
MC
204 } else if (type == X509_FILETYPE_ASN1) {
205 x = d2i_X509_CRL_bio(in, NULL);
206 if (x == NULL) {
ae29622f 207 ERR_raise(ERR_LIB_X509, X509_R_NO_CRL_FOUND);
0f113f3e
MC
208 goto err;
209 }
ae29622f 210 count = X509_STORE_add_crl(ctx->store_ctx, x);
0f113f3e 211 } else {
9311d0c4 212 ERR_raise(ERR_LIB_X509, X509_R_BAD_X509_FILETYPE);
0f113f3e
MC
213 goto err;
214 }
215 err:
222561fe 216 X509_CRL_free(x);
ca3a82c3 217 BIO_free(in);
ae29622f 218 return count;
0f113f3e 219}
58964a49 220
d8652be0 221int X509_load_cert_crl_file_ex(X509_LOOKUP *ctx, const char *file, int type,
b4250010 222 OSSL_LIB_CTX *libctx, const char *propq)
52664f50 223{
ae29622f 224 STACK_OF(X509_INFO) *inf = NULL;
225 X509_INFO *itmp = NULL;
226 BIO *in = NULL;
0f113f3e 227 int i, count = 0;
c0452248 228
0f113f3e 229 if (type != X509_FILETYPE_PEM)
d8652be0 230 return X509_load_cert_file_ex(ctx, file, type, libctx, propq);
0f113f3e 231 in = BIO_new_file(file, "r");
ae29622f 232 if (in == NULL) {
233 ERR_raise(ERR_LIB_X509, ERR_R_BIO_LIB);
0f113f3e
MC
234 return 0;
235 }
d8652be0 236 inf = PEM_X509_INFO_read_bio_ex(in, NULL, NULL, "", libctx, propq);
0f113f3e 237 BIO_free(in);
ae29622f 238 if (inf == NULL) {
9311d0c4 239 ERR_raise(ERR_LIB_X509, ERR_R_PEM_LIB);
0f113f3e
MC
240 return 0;
241 }
242 for (i = 0; i < sk_X509_INFO_num(inf); i++) {
243 itmp = sk_X509_INFO_value(inf, i);
244 if (itmp->x509) {
ae29622f 245 if (!X509_STORE_add_cert(ctx->store_ctx, itmp->x509)) {
246 count = 0;
c0452248 247 goto err;
ae29622f 248 }
0f113f3e
MC
249 count++;
250 }
251 if (itmp->crl) {
ae29622f 252 if (!X509_STORE_add_crl(ctx->store_ctx, itmp->crl)) {
253 count = 0;
c0452248 254 goto err;
ae29622f 255 }
0f113f3e
MC
256 count++;
257 }
258 }
c0452248 259 if (count == 0)
9311d0c4 260 ERR_raise(ERR_LIB_X509, X509_R_NO_CERTIFICATE_OR_CRL_FOUND);
c0452248 261 err:
0f113f3e
MC
262 sk_X509_INFO_pop_free(inf, X509_INFO_free);
263 return count;
52664f50 264}
6725682d
SL
265
266int X509_load_cert_crl_file(X509_LOOKUP *ctx, const char *file, int type)
267{
d8652be0 268 return X509_load_cert_crl_file_ex(ctx, file, type, NULL, NULL);
6725682d
SL
269}
270