2 * Copyright 1995-2023 The OpenSSL Project Authors. All Rights Reserved.
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
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"
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,
27 static X509_LOOKUP_METHOD x509_file_lookup = {
28 "Load file into cache",
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 */
42 X509_LOOKUP_METHOD *X509_LOOKUP_file(void)
44 return &x509_file_lookup;
47 static int by_file_ctrl_ex(X509_LOOKUP *ctx, int cmd, const char *argp,
48 long argl, char **ret, OSSL_LIB_CTX *libctx,
55 case X509_L_FILE_LOAD:
56 if (argl == X509_FILETYPE_DEFAULT) {
57 file = ossl_safe_getenv(X509_get_default_cert_file_env());
59 ok = (X509_load_cert_crl_file_ex(ctx, file, X509_FILETYPE_PEM,
62 ok = (X509_load_cert_crl_file_ex(
63 ctx, X509_get_default_cert_file(),
64 X509_FILETYPE_PEM, libctx, propq) != 0);
67 ERR_raise(ERR_LIB_X509, X509_R_LOADING_DEFAULTS);
69 if (argl == X509_FILETYPE_PEM)
70 ok = (X509_load_cert_crl_file_ex(ctx, argp, X509_FILETYPE_PEM,
73 ok = (X509_load_cert_file_ex(ctx, argp, (int)argl, libctx,
81 static int by_file_ctrl(X509_LOOKUP *ctx, int cmd,
82 const char *argp, long argl, char **ret)
84 return by_file_ctrl_ex(ctx, cmd, argp, argl, ret, NULL, NULL);
87 int X509_load_cert_file_ex(X509_LOOKUP *ctx, const char *file, int type,
88 OSSL_LIB_CTX *libctx, const char *propq)
94 in = BIO_new(BIO_s_file());
96 if ((in == NULL) || (BIO_read_filename(in, file) <= 0)) {
97 ERR_raise(ERR_LIB_X509, ERR_R_BIO_LIB);
101 x = X509_new_ex(libctx, propq);
103 ERR_raise(ERR_LIB_X509, ERR_R_ASN1_LIB);
107 if (type == X509_FILETYPE_PEM) {
110 if (PEM_read_bio_X509_AUX(in, &x, NULL, "") == NULL) {
111 if ((ERR_GET_REASON(ERR_peek_last_error()) ==
112 PEM_R_NO_START_LINE) && (count > 0)) {
116 ERR_clear_last_mark();
118 ERR_raise(ERR_LIB_X509, X509_R_NO_CERTIFICATE_FOUND);
120 ERR_raise(ERR_LIB_X509, ERR_R_PEM_LIB);
126 ERR_clear_last_mark();
127 if (!X509_STORE_add_cert(ctx->store_ctx, x)) {
133 } else if (type == X509_FILETYPE_ASN1) {
134 if (d2i_X509_bio(in, &x) == NULL) {
135 ERR_raise(ERR_LIB_X509, X509_R_NO_CERTIFICATE_FOUND);
138 count = X509_STORE_add_cert(ctx->store_ctx, x);
140 ERR_raise(ERR_LIB_X509, X509_R_BAD_X509_FILETYPE);
149 int X509_load_cert_file(X509_LOOKUP *ctx, const char *file, int type)
151 return X509_load_cert_file_ex(ctx, file, type, NULL, NULL);
154 int X509_load_crl_file(X509_LOOKUP *ctx, const char *file, int type)
160 in = BIO_new(BIO_s_file());
162 if ((in == NULL) || (BIO_read_filename(in, file) <= 0)) {
163 ERR_raise(ERR_LIB_X509, ERR_R_BIO_LIB);
167 if (type == X509_FILETYPE_PEM) {
169 x = PEM_read_bio_X509_CRL(in, NULL, NULL, "");
171 if ((ERR_GET_REASON(ERR_peek_last_error()) ==
172 PEM_R_NO_START_LINE) && (count > 0)) {
177 ERR_raise(ERR_LIB_X509, X509_R_NO_CRL_FOUND);
179 ERR_raise(ERR_LIB_X509, ERR_R_PEM_LIB);
185 if (!X509_STORE_add_crl(ctx->store_ctx, x)) {
191 } else if (type == X509_FILETYPE_ASN1) {
192 x = d2i_X509_CRL_bio(in, NULL);
194 ERR_raise(ERR_LIB_X509, X509_R_NO_CRL_FOUND);
197 count = X509_STORE_add_crl(ctx->store_ctx, x);
199 ERR_raise(ERR_LIB_X509, X509_R_BAD_X509_FILETYPE);
208 int X509_load_cert_crl_file_ex(X509_LOOKUP *ctx, const char *file, int type,
209 OSSL_LIB_CTX *libctx, const char *propq)
211 STACK_OF(X509_INFO) *inf = NULL;
212 X509_INFO *itmp = NULL;
216 if (type != X509_FILETYPE_PEM)
217 return X509_load_cert_file_ex(ctx, file, type, libctx, propq);
218 in = BIO_new_file(file, "r");
220 ERR_raise(ERR_LIB_X509, ERR_R_BIO_LIB);
223 inf = PEM_X509_INFO_read_bio_ex(in, NULL, NULL, "", libctx, propq);
226 ERR_raise(ERR_LIB_X509, ERR_R_PEM_LIB);
229 for (i = 0; i < sk_X509_INFO_num(inf); i++) {
230 itmp = sk_X509_INFO_value(inf, i);
232 if (!X509_STORE_add_cert(ctx->store_ctx, itmp->x509)) {
239 if (!X509_STORE_add_crl(ctx->store_ctx, itmp->crl)) {
247 ERR_raise(ERR_LIB_X509, X509_R_NO_CERTIFICATE_OR_CRL_FOUND);
249 sk_X509_INFO_pop_free(inf, X509_INFO_free);
253 int X509_load_cert_crl_file(X509_LOOKUP *ctx, const char *file, int type)
255 return X509_load_cert_crl_file_ex(ctx, file, type, NULL, NULL);