2 * Copyright 1995-2017 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (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/lhash.h>
16 #include <openssl/buffer.h>
17 #include <openssl/x509.h>
18 #include <openssl/pem.h>
21 static int by_file_ctrl(X509_LOOKUP *ctx, int cmd, const char *argc,
22 long argl, char **ret);
23 static X509_LOOKUP_METHOD x509_file_lookup = {
24 "Load file into cache",
29 by_file_ctrl, /* ctrl */
30 NULL, /* get_by_subject */
31 NULL, /* get_by_issuer_serial */
32 NULL, /* get_by_fingerprint */
33 NULL, /* get_by_alias */
36 X509_LOOKUP_METHOD *X509_LOOKUP_file(void)
38 return (&x509_file_lookup);
41 static int by_file_ctrl(X509_LOOKUP *ctx, int cmd, const char *argp,
42 long argl, char **ret)
48 case X509_L_FILE_LOAD:
49 if (argl == X509_FILETYPE_DEFAULT) {
50 file = getenv(X509_get_default_cert_file_env());
52 ok = (X509_load_cert_crl_file(ctx, file,
53 X509_FILETYPE_PEM) != 0);
56 ok = (X509_load_cert_crl_file
57 (ctx, X509_get_default_cert_file(),
58 X509_FILETYPE_PEM) != 0);
61 X509err(X509_F_BY_FILE_CTRL, X509_R_LOADING_DEFAULTS);
64 if (argl == X509_FILETYPE_PEM)
65 ok = (X509_load_cert_crl_file(ctx, argp,
66 X509_FILETYPE_PEM) != 0);
68 ok = (X509_load_cert_file(ctx, argp, (int)argl) != 0);
75 int X509_load_cert_file(X509_LOOKUP *ctx, const char *file, int type)
82 in = BIO_new(BIO_s_file());
84 if ((in == NULL) || (BIO_read_filename(in, file) <= 0)) {
85 X509err(X509_F_X509_LOAD_CERT_FILE, ERR_R_SYS_LIB);
89 if (type == X509_FILETYPE_PEM) {
91 x = PEM_read_bio_X509_AUX(in, NULL, NULL, NULL);
93 if ((ERR_GET_REASON(ERR_peek_last_error()) ==
94 PEM_R_NO_START_LINE) && (count > 0)) {
98 X509err(X509_F_X509_LOAD_CERT_FILE, ERR_R_PEM_LIB);
102 i = X509_STORE_add_cert(ctx->store_ctx, x);
110 } else if (type == X509_FILETYPE_ASN1) {
111 x = d2i_X509_bio(in, NULL);
113 X509err(X509_F_X509_LOAD_CERT_FILE, ERR_R_ASN1_LIB);
116 i = X509_STORE_add_cert(ctx->store_ctx, x);
121 X509err(X509_F_X509_LOAD_CERT_FILE, X509_R_BAD_X509_FILETYPE);
125 X509err(X509_F_X509_LOAD_CERT_FILE, X509_R_NO_CERTIFICATE_FOUND);
132 int X509_load_crl_file(X509_LOOKUP *ctx, const char *file, int type)
139 in = BIO_new(BIO_s_file());
141 if ((in == NULL) || (BIO_read_filename(in, file) <= 0)) {
142 X509err(X509_F_X509_LOAD_CRL_FILE, ERR_R_SYS_LIB);
146 if (type == X509_FILETYPE_PEM) {
148 x = PEM_read_bio_X509_CRL(in, NULL, NULL, NULL);
150 if ((ERR_GET_REASON(ERR_peek_last_error()) ==
151 PEM_R_NO_START_LINE) && (count > 0)) {
155 X509err(X509_F_X509_LOAD_CRL_FILE, ERR_R_PEM_LIB);
159 i = X509_STORE_add_crl(ctx->store_ctx, x);
167 } else if (type == X509_FILETYPE_ASN1) {
168 x = d2i_X509_CRL_bio(in, NULL);
170 X509err(X509_F_X509_LOAD_CRL_FILE, ERR_R_ASN1_LIB);
173 i = X509_STORE_add_crl(ctx->store_ctx, x);
178 X509err(X509_F_X509_LOAD_CRL_FILE, X509_R_BAD_X509_FILETYPE);
182 X509err(X509_F_X509_LOAD_CRL_FILE, X509_R_NO_CRL_FOUND);
189 int X509_load_cert_crl_file(X509_LOOKUP *ctx, const char *file, int type)
191 STACK_OF(X509_INFO) *inf;
196 if (type != X509_FILETYPE_PEM)
197 return X509_load_cert_file(ctx, file, type);
198 in = BIO_new_file(file, "r");
200 X509err(X509_F_X509_LOAD_CERT_CRL_FILE, ERR_R_SYS_LIB);
203 inf = PEM_X509_INFO_read_bio(in, NULL, NULL, NULL);
206 X509err(X509_F_X509_LOAD_CERT_CRL_FILE, ERR_R_PEM_LIB);
209 for (i = 0; i < sk_X509_INFO_num(inf); i++) {
210 itmp = sk_X509_INFO_value(inf, i);
212 if (!X509_STORE_add_cert(ctx->store_ctx, itmp->x509))
217 if (!X509_STORE_add_crl(ctx->store_ctx, itmp->crl))
223 X509err(X509_F_X509_LOAD_CERT_CRL_FILE,
224 X509_R_NO_CERTIFICATE_OR_CRL_FOUND);
226 sk_X509_INFO_pop_free(inf, X509_INFO_free);