2 * Copyright 1995-2016 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 = (char *)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)
84 in = BIO_new(BIO_s_file());
86 if ((in == NULL) || (BIO_read_filename(in, file) <= 0)) {
87 X509err(X509_F_X509_LOAD_CERT_FILE, ERR_R_SYS_LIB);
91 if (type == X509_FILETYPE_PEM) {
93 x = PEM_read_bio_X509_AUX(in, NULL, NULL, NULL);
95 if ((ERR_GET_REASON(ERR_peek_last_error()) ==
96 PEM_R_NO_START_LINE) && (count > 0)) {
100 X509err(X509_F_X509_LOAD_CERT_FILE, ERR_R_PEM_LIB);
104 i = X509_STORE_add_cert(ctx->store_ctx, x);
112 } else if (type == X509_FILETYPE_ASN1) {
113 x = d2i_X509_bio(in, NULL);
115 X509err(X509_F_X509_LOAD_CERT_FILE, ERR_R_ASN1_LIB);
118 i = X509_STORE_add_cert(ctx->store_ctx, x);
123 X509err(X509_F_X509_LOAD_CERT_FILE, X509_R_BAD_X509_FILETYPE);
132 int X509_load_crl_file(X509_LOOKUP *ctx, const char *file, int type)
141 in = BIO_new(BIO_s_file());
143 if ((in == NULL) || (BIO_read_filename(in, file) <= 0)) {
144 X509err(X509_F_X509_LOAD_CRL_FILE, ERR_R_SYS_LIB);
148 if (type == X509_FILETYPE_PEM) {
150 x = PEM_read_bio_X509_CRL(in, NULL, NULL, NULL);
152 if ((ERR_GET_REASON(ERR_peek_last_error()) ==
153 PEM_R_NO_START_LINE) && (count > 0)) {
157 X509err(X509_F_X509_LOAD_CRL_FILE, ERR_R_PEM_LIB);
161 i = X509_STORE_add_crl(ctx->store_ctx, x);
169 } else if (type == X509_FILETYPE_ASN1) {
170 x = d2i_X509_CRL_bio(in, NULL);
172 X509err(X509_F_X509_LOAD_CRL_FILE, ERR_R_ASN1_LIB);
175 i = X509_STORE_add_crl(ctx->store_ctx, x);
180 X509err(X509_F_X509_LOAD_CRL_FILE, X509_R_BAD_X509_FILETYPE);
189 int X509_load_cert_crl_file(X509_LOOKUP *ctx, const char *file, int type)
191 STACK_OF(X509_INFO) *inf;
195 if (type != X509_FILETYPE_PEM)
196 return X509_load_cert_file(ctx, file, type);
197 in = BIO_new_file(file, "r");
199 X509err(X509_F_X509_LOAD_CERT_CRL_FILE, ERR_R_SYS_LIB);
202 inf = PEM_X509_INFO_read_bio(in, NULL, NULL, NULL);
205 X509err(X509_F_X509_LOAD_CERT_CRL_FILE, ERR_R_PEM_LIB);
208 for (i = 0; i < sk_X509_INFO_num(inf); i++) {
209 itmp = sk_X509_INFO_value(inf, i);
211 X509_STORE_add_cert(ctx->store_ctx, itmp->x509);
215 X509_STORE_add_crl(ctx->store_ctx, itmp->crl);
219 sk_X509_INFO_pop_free(inf, X509_INFO_free);