a4ec328312bf28a734098745d560674ba96a6082
[openssl.git] / crypto / x509 / by_file.c
1 /*
2  * Copyright 1995-2020 The OpenSSL Project Authors. All Rights Reserved.
3  *
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
8  */
9
10 #include <stdio.h>
11 #include <time.h>
12 #include <errno.h>
13
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"
19
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_with_libctx(X509_LOOKUP *ctx, int cmd,
23                                     const char *argc, long argl, char **ret,
24                                     OPENSSL_CTX *libctx, const char *propq);
25
26
27 static X509_LOOKUP_METHOD x509_file_lookup = {
28     "Load file into cache",
29     NULL,                       /* new_item */
30     NULL,                       /* free */
31     NULL,                       /* init */
32     NULL,                       /* shutdown */
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_with_libctx */
39     by_file_ctrl_with_libctx,   /* ctrl_with_libctx */
40 };
41
42 X509_LOOKUP_METHOD *X509_LOOKUP_file(void)
43 {
44     return &x509_file_lookup;
45 }
46
47 static int by_file_ctrl_with_libctx(X509_LOOKUP *ctx, int cmd,
48                                     const char *argp, long argl, char **ret,
49                                     OPENSSL_CTX *libctx, const char *propq)
50 {
51     int ok = 0;
52     const char *file;
53
54     switch (cmd) {
55     case X509_L_FILE_LOAD:
56         if (argl == X509_FILETYPE_DEFAULT) {
57             file = ossl_safe_getenv(X509_get_default_cert_file_env());
58             if (file)
59                 ok = (X509_load_cert_crl_file_with_libctx(ctx, file,
60                                                           X509_FILETYPE_PEM,
61                                                           libctx, propq) != 0);
62
63             else
64                 ok = (X509_load_cert_crl_file_with_libctx(
65                          ctx, X509_get_default_cert_file(),
66                          X509_FILETYPE_PEM, libctx, propq) != 0);
67
68             if (!ok) {
69                 X509err(0, X509_R_LOADING_DEFAULTS);
70             }
71         } else {
72             if (argl == X509_FILETYPE_PEM)
73                 ok = (X509_load_cert_crl_file_with_libctx(ctx, argp,
74                                                           X509_FILETYPE_PEM,
75                                                           libctx, propq) != 0);
76             else
77                 ok = (X509_load_cert_file_with_libctx(ctx, argp, (int)argl,
78                                                       libctx, propq) != 0);
79         }
80         break;
81     }
82     return ok;
83 }
84
85 static int by_file_ctrl(X509_LOOKUP *ctx, int cmd,
86                         const char *argp, long argl, char **ret)
87 {
88     return by_file_ctrl_with_libctx(ctx, cmd, argp, argl, ret, NULL, NULL);
89 }
90
91 int X509_load_cert_file_with_libctx(X509_LOOKUP *ctx, const char *file, int type,
92                                     OPENSSL_CTX *libctx, const char *propq)
93 {
94     int ret = 0;
95     BIO *in = NULL;
96     int i, count = 0;
97     X509 *x = NULL;
98
99     in = BIO_new(BIO_s_file());
100
101     if ((in == NULL) || (BIO_read_filename(in, file) <= 0)) {
102         X509err(0, ERR_R_SYS_LIB);
103         goto err;
104     }
105
106     if (type != X509_FILETYPE_PEM && type != X509_FILETYPE_ASN1) {
107         X509err(0, X509_R_BAD_X509_FILETYPE);
108         goto err;
109     }
110     x = X509_new_with_libctx(libctx, propq);
111     if (x == NULL) {
112         X509err(0, ERR_R_MALLOC_FAILURE);
113         goto err;
114     }
115
116     if (type == X509_FILETYPE_PEM) {
117         for (;;) {
118             if (PEM_read_bio_X509_AUX(in, &x, NULL, "") == NULL) {
119                 if ((ERR_GET_REASON(ERR_peek_last_error()) ==
120                      PEM_R_NO_START_LINE) && (count > 0)) {
121                     ERR_clear_error();
122                     break;
123                 } else {
124                     X509err(0, ERR_R_PEM_LIB);
125                     goto err;
126                 }
127             }
128             i = X509_STORE_add_cert(ctx->store_ctx, x);
129             if (!i)
130                 goto err;
131             count++;
132             X509_free(x);
133             x = NULL;
134         }
135         ret = count;
136     } else if (type == X509_FILETYPE_ASN1) {
137         if (d2i_X509_bio(in, &x) == NULL) {
138             X509err(0, ERR_R_ASN1_LIB);
139             goto err;
140         }
141         i = X509_STORE_add_cert(ctx->store_ctx, x);
142         if (!i)
143             goto err;
144         ret = i;
145     }
146     if (ret == 0)
147         X509err(0, X509_R_NO_CERTIFICATE_FOUND);
148  err:
149     X509_free(x);
150     BIO_free(in);
151     return ret;
152 }
153
154 int X509_load_cert_file(X509_LOOKUP *ctx, const char *file, int type)
155 {
156     return X509_load_cert_file_with_libctx(ctx, file, type, NULL, NULL);
157 }
158
159 int X509_load_crl_file(X509_LOOKUP *ctx, const char *file, int type)
160 {
161     int ret = 0;
162     BIO *in = NULL;
163     int i, count = 0;
164     X509_CRL *x = NULL;
165
166     in = BIO_new(BIO_s_file());
167
168     if ((in == NULL) || (BIO_read_filename(in, file) <= 0)) {
169         X509err(X509_F_X509_LOAD_CRL_FILE, ERR_R_SYS_LIB);
170         goto err;
171     }
172
173     if (type == X509_FILETYPE_PEM) {
174         for (;;) {
175             x = PEM_read_bio_X509_CRL(in, NULL, NULL, "");
176             if (x == NULL) {
177                 if ((ERR_GET_REASON(ERR_peek_last_error()) ==
178                      PEM_R_NO_START_LINE) && (count > 0)) {
179                     ERR_clear_error();
180                     break;
181                 } else {
182                     X509err(X509_F_X509_LOAD_CRL_FILE, ERR_R_PEM_LIB);
183                     goto err;
184                 }
185             }
186             i = X509_STORE_add_crl(ctx->store_ctx, x);
187             if (!i)
188                 goto err;
189             count++;
190             X509_CRL_free(x);
191             x = NULL;
192         }
193         ret = count;
194     } else if (type == X509_FILETYPE_ASN1) {
195         x = d2i_X509_CRL_bio(in, NULL);
196         if (x == NULL) {
197             X509err(X509_F_X509_LOAD_CRL_FILE, ERR_R_ASN1_LIB);
198             goto err;
199         }
200         i = X509_STORE_add_crl(ctx->store_ctx, x);
201         if (!i)
202             goto err;
203         ret = i;
204     } else {
205         X509err(X509_F_X509_LOAD_CRL_FILE, X509_R_BAD_X509_FILETYPE);
206         goto err;
207     }
208     if (ret == 0)
209         X509err(X509_F_X509_LOAD_CRL_FILE, X509_R_NO_CRL_FOUND);
210  err:
211     X509_CRL_free(x);
212     BIO_free(in);
213     return ret;
214 }
215
216 int X509_load_cert_crl_file_with_libctx(X509_LOOKUP *ctx, const char *file,
217                                         int type, OPENSSL_CTX *libctx,
218                                         const char *propq)
219 {
220     STACK_OF(X509_INFO) *inf;
221     X509_INFO *itmp;
222     BIO *in;
223     int i, count = 0;
224
225     if (type != X509_FILETYPE_PEM)
226         return X509_load_cert_file_with_libctx(ctx, file, type, libctx, propq);
227     in = BIO_new_file(file, "r");
228     if (!in) {
229         X509err(0, ERR_R_SYS_LIB);
230         return 0;
231     }
232     inf = PEM_X509_INFO_read_bio_with_libctx(in, NULL, NULL, "", libctx, propq);
233     BIO_free(in);
234     if (!inf) {
235         X509err(0, ERR_R_PEM_LIB);
236         return 0;
237     }
238     for (i = 0; i < sk_X509_INFO_num(inf); i++) {
239         itmp = sk_X509_INFO_value(inf, i);
240         if (itmp->x509) {
241             if (!X509_STORE_add_cert(ctx->store_ctx, itmp->x509))
242                 goto err;
243             count++;
244         }
245         if (itmp->crl) {
246             if (!X509_STORE_add_crl(ctx->store_ctx, itmp->crl))
247                 goto err;
248             count++;
249         }
250     }
251     if (count == 0)
252         X509err(0, X509_R_NO_CERTIFICATE_OR_CRL_FOUND);
253  err:
254     sk_X509_INFO_pop_free(inf, X509_INFO_free);
255     return count;
256 }
257
258 int X509_load_cert_crl_file(X509_LOOKUP *ctx, const char *file, int type)
259 {
260     return X509_load_cert_crl_file_with_libctx(ctx, file, type, NULL, NULL);
261 }
262