2 * Copyright 1995-2020 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 DEFINE_STACK_OF(X509_INFO)
22 static int by_file_ctrl(X509_LOOKUP *ctx, int cmd, const char *argc,
23 long argl, char **ret);
24 static X509_LOOKUP_METHOD x509_file_lookup = {
25 "Load file into cache",
30 by_file_ctrl, /* ctrl */
31 NULL, /* get_by_subject */
32 NULL, /* get_by_issuer_serial */
33 NULL, /* get_by_fingerprint */
34 NULL, /* get_by_alias */
37 X509_LOOKUP_METHOD *X509_LOOKUP_file(void)
39 return &x509_file_lookup;
42 static int by_file_ctrl(X509_LOOKUP *ctx, int cmd, const char *argp,
43 long argl, char **ret)
49 case X509_L_FILE_LOAD:
50 if (argl == X509_FILETYPE_DEFAULT) {
51 file = ossl_safe_getenv(X509_get_default_cert_file_env());
53 ok = (X509_load_cert_crl_file(ctx, file,
54 X509_FILETYPE_PEM) != 0);
57 ok = (X509_load_cert_crl_file
58 (ctx, X509_get_default_cert_file(),
59 X509_FILETYPE_PEM) != 0);
62 X509err(X509_F_BY_FILE_CTRL, X509_R_LOADING_DEFAULTS);
65 if (argl == X509_FILETYPE_PEM)
66 ok = (X509_load_cert_crl_file(ctx, argp,
67 X509_FILETYPE_PEM) != 0);
69 ok = (X509_load_cert_file(ctx, argp, (int)argl) != 0);
76 int X509_load_cert_file(X509_LOOKUP *ctx, const char *file, int type)
83 in = BIO_new(BIO_s_file());
85 if ((in == NULL) || (BIO_read_filename(in, file) <= 0)) {
86 X509err(X509_F_X509_LOAD_CERT_FILE, ERR_R_SYS_LIB);
90 if (type == X509_FILETYPE_PEM) {
92 x = PEM_read_bio_X509_AUX(in, NULL, NULL, "");
94 if ((ERR_GET_REASON(ERR_peek_last_error()) ==
95 PEM_R_NO_START_LINE) && (count > 0)) {
99 X509err(X509_F_X509_LOAD_CERT_FILE, ERR_R_PEM_LIB);
103 i = X509_STORE_add_cert(ctx->store_ctx, x);
111 } else if (type == X509_FILETYPE_ASN1) {
112 x = d2i_X509_bio(in, NULL);
114 X509err(X509_F_X509_LOAD_CERT_FILE, ERR_R_ASN1_LIB);
117 i = X509_STORE_add_cert(ctx->store_ctx, x);
122 X509err(X509_F_X509_LOAD_CERT_FILE, X509_R_BAD_X509_FILETYPE);
126 X509err(X509_F_X509_LOAD_CERT_FILE, X509_R_NO_CERTIFICATE_FOUND);
133 int X509_load_crl_file(X509_LOOKUP *ctx, const char *file, int type)
140 in = BIO_new(BIO_s_file());
142 if ((in == NULL) || (BIO_read_filename(in, file) <= 0)) {
143 X509err(X509_F_X509_LOAD_CRL_FILE, ERR_R_SYS_LIB);
147 if (type == X509_FILETYPE_PEM) {
149 x = PEM_read_bio_X509_CRL(in, NULL, NULL, "");
151 if ((ERR_GET_REASON(ERR_peek_last_error()) ==
152 PEM_R_NO_START_LINE) && (count > 0)) {
156 X509err(X509_F_X509_LOAD_CRL_FILE, ERR_R_PEM_LIB);
160 i = X509_STORE_add_crl(ctx->store_ctx, x);
168 } else if (type == X509_FILETYPE_ASN1) {
169 x = d2i_X509_CRL_bio(in, NULL);
171 X509err(X509_F_X509_LOAD_CRL_FILE, ERR_R_ASN1_LIB);
174 i = X509_STORE_add_crl(ctx->store_ctx, x);
179 X509err(X509_F_X509_LOAD_CRL_FILE, X509_R_BAD_X509_FILETYPE);
183 X509err(X509_F_X509_LOAD_CRL_FILE, X509_R_NO_CRL_FOUND);
190 int X509_load_cert_crl_file(X509_LOOKUP *ctx, const char *file, int type)
192 STACK_OF(X509_INFO) *inf;
197 if (type != X509_FILETYPE_PEM)
198 return X509_load_cert_file(ctx, file, type);
199 in = BIO_new_file(file, "r");
201 X509err(X509_F_X509_LOAD_CERT_CRL_FILE, ERR_R_SYS_LIB);
204 inf = PEM_X509_INFO_read_bio(in, NULL, NULL, "");
207 X509err(X509_F_X509_LOAD_CERT_CRL_FILE, ERR_R_PEM_LIB);
210 for (i = 0; i < sk_X509_INFO_num(inf); i++) {
211 itmp = sk_X509_INFO_value(inf, i);
213 if (!X509_STORE_add_cert(ctx->store_ctx, itmp->x509))
218 if (!X509_STORE_add_crl(ctx->store_ctx, itmp->crl))
224 X509err(X509_F_X509_LOAD_CERT_CRL_FILE,
225 X509_R_NO_CERTIFICATE_OR_CRL_FOUND);
227 sk_X509_INFO_pop_free(inf, X509_INFO_free);