]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/x509/by_file.c
Fix error reporting glitch in X509_STORE_CTX_print_verify_cb() in t_x509.c
[thirdparty/openssl.git] / crypto / x509 / by_file.c
CommitLineData
b1322259 1/*
6ec5fce2 2 * Copyright 1995-2018 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);
df2ee0e2 22static X509_LOOKUP_METHOD x509_file_lookup = {
0f113f3e 23 "Load file into cache",
7fcdbd83 24 NULL, /* new_item */
0f113f3e
MC
25 NULL, /* free */
26 NULL, /* init */
27 NULL, /* shutdown */
28 by_file_ctrl, /* ctrl */
29 NULL, /* get_by_subject */
30 NULL, /* get_by_issuer_serial */
31 NULL, /* get_by_fingerprint */
32 NULL, /* get_by_alias */
33};
d02b48c6 34
6b691a5c 35X509_LOOKUP_METHOD *X509_LOOKUP_file(void)
0f113f3e 36{
26a7d938 37 return &x509_file_lookup;
0f113f3e 38}
d02b48c6 39
0f113f3e
MC
40static int by_file_ctrl(X509_LOOKUP *ctx, int cmd, const char *argp,
41 long argl, char **ret)
42{
43 int ok = 0;
a020f54c 44 const char *file;
d02b48c6 45
0f113f3e
MC
46 switch (cmd) {
47 case X509_L_FILE_LOAD:
48 if (argl == X509_FILETYPE_DEFAULT) {
5c39a55d 49 file = ossl_safe_getenv(X509_get_default_cert_file_env());
0f113f3e
MC
50 if (file)
51 ok = (X509_load_cert_crl_file(ctx, file,
52 X509_FILETYPE_PEM) != 0);
4387f478 53
0f113f3e
MC
54 else
55 ok = (X509_load_cert_crl_file
56 (ctx, X509_get_default_cert_file(),
57 X509_FILETYPE_PEM) != 0);
4387f478 58
0f113f3e
MC
59 if (!ok) {
60 X509err(X509_F_BY_FILE_CTRL, X509_R_LOADING_DEFAULTS);
61 }
62 } else {
63 if (argl == X509_FILETYPE_PEM)
64 ok = (X509_load_cert_crl_file(ctx, argp,
65 X509_FILETYPE_PEM) != 0);
66 else
67 ok = (X509_load_cert_file(ctx, argp, (int)argl) != 0);
68 }
69 break;
70 }
26a7d938 71 return ok;
0f113f3e 72}
d02b48c6 73
6b691a5c 74int X509_load_cert_file(X509_LOOKUP *ctx, const char *file, int type)
0f113f3e
MC
75{
76 int ret = 0;
77 BIO *in = NULL;
78 int i, count = 0;
79 X509 *x = NULL;
d02b48c6 80
9982cbbb 81 in = BIO_new(BIO_s_file());
d02b48c6 82
0f113f3e
MC
83 if ((in == NULL) || (BIO_read_filename(in, file) <= 0)) {
84 X509err(X509_F_X509_LOAD_CERT_FILE, ERR_R_SYS_LIB);
85 goto err;
86 }
d02b48c6 87
0f113f3e
MC
88 if (type == X509_FILETYPE_PEM) {
89 for (;;) {
db854bb1 90 x = PEM_read_bio_X509_AUX(in, NULL, NULL, "");
0f113f3e
MC
91 if (x == NULL) {
92 if ((ERR_GET_REASON(ERR_peek_last_error()) ==
93 PEM_R_NO_START_LINE) && (count > 0)) {
94 ERR_clear_error();
95 break;
96 } else {
97 X509err(X509_F_X509_LOAD_CERT_FILE, ERR_R_PEM_LIB);
98 goto err;
99 }
100 }
101 i = X509_STORE_add_cert(ctx->store_ctx, x);
102 if (!i)
103 goto err;
104 count++;
105 X509_free(x);
106 x = NULL;
107 }
108 ret = count;
109 } else if (type == X509_FILETYPE_ASN1) {
110 x = d2i_X509_bio(in, NULL);
111 if (x == NULL) {
112 X509err(X509_F_X509_LOAD_CERT_FILE, ERR_R_ASN1_LIB);
113 goto err;
114 }
115 i = X509_STORE_add_cert(ctx->store_ctx, x);
116 if (!i)
117 goto err;
118 ret = i;
119 } else {
120 X509err(X509_F_X509_LOAD_CERT_FILE, X509_R_BAD_X509_FILETYPE);
121 goto err;
122 }
c0452248
RS
123 if (ret == 0)
124 X509err(X509_F_X509_LOAD_CERT_FILE, X509_R_NO_CERTIFICATE_FOUND);
0f113f3e 125 err:
222561fe 126 X509_free(x);
ca3a82c3 127 BIO_free(in);
26a7d938 128 return ret;
0f113f3e 129}
d02b48c6 130
6b691a5c 131int X509_load_crl_file(X509_LOOKUP *ctx, const char *file, int type)
0f113f3e
MC
132{
133 int ret = 0;
134 BIO *in = NULL;
135 int i, count = 0;
136 X509_CRL *x = NULL;
58964a49 137
9982cbbb 138 in = BIO_new(BIO_s_file());
58964a49 139
0f113f3e
MC
140 if ((in == NULL) || (BIO_read_filename(in, file) <= 0)) {
141 X509err(X509_F_X509_LOAD_CRL_FILE, ERR_R_SYS_LIB);
142 goto err;
143 }
58964a49 144
0f113f3e
MC
145 if (type == X509_FILETYPE_PEM) {
146 for (;;) {
db854bb1 147 x = PEM_read_bio_X509_CRL(in, NULL, NULL, "");
0f113f3e
MC
148 if (x == NULL) {
149 if ((ERR_GET_REASON(ERR_peek_last_error()) ==
150 PEM_R_NO_START_LINE) && (count > 0)) {
151 ERR_clear_error();
152 break;
153 } else {
154 X509err(X509_F_X509_LOAD_CRL_FILE, ERR_R_PEM_LIB);
155 goto err;
156 }
157 }
158 i = X509_STORE_add_crl(ctx->store_ctx, x);
159 if (!i)
160 goto err;
161 count++;
162 X509_CRL_free(x);
163 x = NULL;
164 }
165 ret = count;
166 } else if (type == X509_FILETYPE_ASN1) {
167 x = d2i_X509_CRL_bio(in, NULL);
168 if (x == NULL) {
169 X509err(X509_F_X509_LOAD_CRL_FILE, ERR_R_ASN1_LIB);
170 goto err;
171 }
172 i = X509_STORE_add_crl(ctx->store_ctx, x);
173 if (!i)
174 goto err;
175 ret = i;
176 } else {
177 X509err(X509_F_X509_LOAD_CRL_FILE, X509_R_BAD_X509_FILETYPE);
178 goto err;
179 }
c0452248
RS
180 if (ret == 0)
181 X509err(X509_F_X509_LOAD_CRL_FILE, X509_R_NO_CRL_FOUND);
0f113f3e 182 err:
222561fe 183 X509_CRL_free(x);
ca3a82c3 184 BIO_free(in);
26a7d938 185 return ret;
0f113f3e 186}
58964a49 187
52664f50
DSH
188int X509_load_cert_crl_file(X509_LOOKUP *ctx, const char *file, int type)
189{
0f113f3e
MC
190 STACK_OF(X509_INFO) *inf;
191 X509_INFO *itmp;
192 BIO *in;
193 int i, count = 0;
c0452248 194
0f113f3e
MC
195 if (type != X509_FILETYPE_PEM)
196 return X509_load_cert_file(ctx, file, type);
197 in = BIO_new_file(file, "r");
198 if (!in) {
199 X509err(X509_F_X509_LOAD_CERT_CRL_FILE, ERR_R_SYS_LIB);
200 return 0;
201 }
db854bb1 202 inf = PEM_X509_INFO_read_bio(in, NULL, NULL, "");
0f113f3e
MC
203 BIO_free(in);
204 if (!inf) {
205 X509err(X509_F_X509_LOAD_CERT_CRL_FILE, ERR_R_PEM_LIB);
206 return 0;
207 }
208 for (i = 0; i < sk_X509_INFO_num(inf); i++) {
209 itmp = sk_X509_INFO_value(inf, i);
210 if (itmp->x509) {
c0452248
RS
211 if (!X509_STORE_add_cert(ctx->store_ctx, itmp->x509))
212 goto err;
0f113f3e
MC
213 count++;
214 }
215 if (itmp->crl) {
c0452248
RS
216 if (!X509_STORE_add_crl(ctx->store_ctx, itmp->crl))
217 goto err;
0f113f3e
MC
218 count++;
219 }
220 }
c0452248
RS
221 if (count == 0)
222 X509err(X509_F_X509_LOAD_CERT_CRL_FILE,
223 X509_R_NO_CERTIFICATE_OR_CRL_FOUND);
224 err:
0f113f3e
MC
225 sk_X509_INFO_pop_free(inf, X509_INFO_free);
226 return count;
52664f50 227}