Run the withlibctx.pl script
[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_ex(X509_LOOKUP *ctx, int cmd, const char *argc,
23                            long argl, char **ret, OPENSSL_CTX *libctx,
24                            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_ex */
39     by_file_ctrl_ex,   /* ctrl_ex */
40 };
41
42 X509_LOOKUP_METHOD *X509_LOOKUP_file(void)
43 {
44     return &x509_file_lookup;
45 }
46
47 static int by_file_ctrl_ex(X509_LOOKUP *ctx, int cmd, const char *argp,
48                            long argl, char **ret, OPENSSL_CTX *libctx,
49                            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_ex(ctx, file, X509_FILETYPE_PEM,
60                                                  libctx, propq) != 0);
61
62             else
63                 ok = (X509_load_cert_crl_file_ex(
64                          ctx, X509_get_default_cert_file(),
65                          X509_FILETYPE_PEM, libctx, propq) != 0);
66
67             if (!ok) {
68                 X509err(0, X509_R_LOADING_DEFAULTS);
69             }
70         } else {
71             if (argl == X509_FILETYPE_PEM)
72                 ok = (X509_load_cert_crl_file_ex(ctx, argp, X509_FILETYPE_PEM,
73                                                  libctx, propq) != 0);
74             else
75                 ok = (X509_load_cert_file_ex(ctx, argp, (int)argl, libctx,
76                                              propq) != 0);
77         }
78         break;
79     }
80     return ok;
81 }
82
83 static int by_file_ctrl(X509_LOOKUP *ctx, int cmd,
84                         const char *argp, long argl, char **ret)
85 {
86     return by_file_ctrl_ex(ctx, cmd, argp, argl, ret, NULL, NULL);
87 }
88
89 int X509_load_cert_file_ex(X509_LOOKUP *ctx, const char *file, int type,
90                            OPENSSL_CTX *libctx, const char *propq)
91 {
92     int ret = 0;
93     BIO *in = NULL;
94     int i, count = 0;
95     X509 *x = NULL;
96
97     in = BIO_new(BIO_s_file());
98
99     if ((in == NULL) || (BIO_read_filename(in, file) <= 0)) {
100         X509err(0, ERR_R_SYS_LIB);
101         goto err;
102     }
103
104     if (type != X509_FILETYPE_PEM && type != X509_FILETYPE_ASN1) {
105         X509err(0, X509_R_BAD_X509_FILETYPE);
106         goto err;
107     }
108     x = X509_new_ex(libctx, propq);
109     if (x == NULL) {
110         X509err(0, ERR_R_MALLOC_FAILURE);
111         goto err;
112     }
113
114     if (type == X509_FILETYPE_PEM) {
115         for (;;) {
116             if (PEM_read_bio_X509_AUX(in, &x, NULL, "") == NULL) {
117                 if ((ERR_GET_REASON(ERR_peek_last_error()) ==
118                      PEM_R_NO_START_LINE) && (count > 0)) {
119                     ERR_clear_error();
120                     break;
121                 } else {
122                     X509err(0, ERR_R_PEM_LIB);
123                     goto err;
124                 }
125             }
126             i = X509_STORE_add_cert(ctx->store_ctx, x);
127             if (!i)
128                 goto err;
129             count++;
130             X509_free(x);
131             x = NULL;
132         }
133         ret = count;
134     } else if (type == X509_FILETYPE_ASN1) {
135         if (d2i_X509_bio(in, &x) == NULL) {
136             X509err(0, ERR_R_ASN1_LIB);
137             goto err;
138         }
139         i = X509_STORE_add_cert(ctx->store_ctx, x);
140         if (!i)
141             goto err;
142         ret = i;
143     }
144     if (ret == 0)
145         X509err(0, X509_R_NO_CERTIFICATE_FOUND);
146  err:
147     X509_free(x);
148     BIO_free(in);
149     return ret;
150 }
151
152 int X509_load_cert_file(X509_LOOKUP *ctx, const char *file, int type)
153 {
154     return X509_load_cert_file_ex(ctx, file, type, NULL, NULL);
155 }
156
157 int X509_load_crl_file(X509_LOOKUP *ctx, const char *file, int type)
158 {
159     int ret = 0;
160     BIO *in = NULL;
161     int i, count = 0;
162     X509_CRL *x = NULL;
163
164     in = BIO_new(BIO_s_file());
165
166     if ((in == NULL) || (BIO_read_filename(in, file) <= 0)) {
167         X509err(X509_F_X509_LOAD_CRL_FILE, ERR_R_SYS_LIB);
168         goto err;
169     }
170
171     if (type == X509_FILETYPE_PEM) {
172         for (;;) {
173             x = PEM_read_bio_X509_CRL(in, NULL, NULL, "");
174             if (x == NULL) {
175                 if ((ERR_GET_REASON(ERR_peek_last_error()) ==
176                      PEM_R_NO_START_LINE) && (count > 0)) {
177                     ERR_clear_error();
178                     break;
179                 } else {
180                     X509err(X509_F_X509_LOAD_CRL_FILE, ERR_R_PEM_LIB);
181                     goto err;
182                 }
183             }
184             i = X509_STORE_add_crl(ctx->store_ctx, x);
185             if (!i)
186                 goto err;
187             count++;
188             X509_CRL_free(x);
189             x = NULL;
190         }
191         ret = count;
192     } else if (type == X509_FILETYPE_ASN1) {
193         x = d2i_X509_CRL_bio(in, NULL);
194         if (x == NULL) {
195             X509err(X509_F_X509_LOAD_CRL_FILE, ERR_R_ASN1_LIB);
196             goto err;
197         }
198         i = X509_STORE_add_crl(ctx->store_ctx, x);
199         if (!i)
200             goto err;
201         ret = i;
202     } else {
203         X509err(X509_F_X509_LOAD_CRL_FILE, X509_R_BAD_X509_FILETYPE);
204         goto err;
205     }
206     if (ret == 0)
207         X509err(X509_F_X509_LOAD_CRL_FILE, X509_R_NO_CRL_FOUND);
208  err:
209     X509_CRL_free(x);
210     BIO_free(in);
211     return ret;
212 }
213
214 int X509_load_cert_crl_file_ex(X509_LOOKUP *ctx, const char *file, int type,
215                                OPENSSL_CTX *libctx, const char *propq)
216 {
217     STACK_OF(X509_INFO) *inf;
218     X509_INFO *itmp;
219     BIO *in;
220     int i, count = 0;
221
222     if (type != X509_FILETYPE_PEM)
223         return X509_load_cert_file_ex(ctx, file, type, libctx, propq);
224     in = BIO_new_file(file, "r");
225     if (!in) {
226         X509err(0, ERR_R_SYS_LIB);
227         return 0;
228     }
229     inf = PEM_X509_INFO_read_bio_ex(in, NULL, NULL, "", libctx, propq);
230     BIO_free(in);
231     if (!inf) {
232         X509err(0, ERR_R_PEM_LIB);
233         return 0;
234     }
235     for (i = 0; i < sk_X509_INFO_num(inf); i++) {
236         itmp = sk_X509_INFO_value(inf, i);
237         if (itmp->x509) {
238             if (!X509_STORE_add_cert(ctx->store_ctx, itmp->x509))
239                 goto err;
240             count++;
241         }
242         if (itmp->crl) {
243             if (!X509_STORE_add_crl(ctx->store_ctx, itmp->crl))
244                 goto err;
245             count++;
246         }
247     }
248     if (count == 0)
249         X509err(0, X509_R_NO_CERTIFICATE_OR_CRL_FOUND);
250  err:
251     sk_X509_INFO_pop_free(inf, X509_INFO_free);
252     return count;
253 }
254
255 int X509_load_cert_crl_file(X509_LOOKUP *ctx, const char *file, int type)
256 {
257     return X509_load_cert_crl_file_ex(ctx, file, type, NULL, NULL);
258 }
259