/src/openssl/crypto/x509/by_file.c
Line  | Count  | Source  | 
1  |  | /*  | 
2  |  |  * Copyright 1995-2025 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, OSSL_LIB_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  | 0  | { | 
44  | 0  |     return &x509_file_lookup;  | 
45  | 0  | }  | 
46  |  |  | 
47  |  | static int by_file_ctrl_ex(X509_LOOKUP *ctx, int cmd, const char *argp,  | 
48  |  |                            long argl, char **ret, OSSL_LIB_CTX *libctx,  | 
49  |  |                            const char *propq)  | 
50  | 0  | { | 
51  | 0  |     int ok = 0;  | 
52  | 0  |     const char *file;  | 
53  |  | 
  | 
54  | 0  |     switch (cmd) { | 
55  | 0  |     case X509_L_FILE_LOAD:  | 
56  | 0  |         if (argl == X509_FILETYPE_DEFAULT) { | 
57  | 0  |             file = ossl_safe_getenv(X509_get_default_cert_file_env());  | 
58  | 0  |             if (file)  | 
59  | 0  |                 ok = (X509_load_cert_crl_file_ex(ctx, file, X509_FILETYPE_PEM,  | 
60  | 0  |                                                  libctx, propq) != 0);  | 
61  | 0  |             else  | 
62  | 0  |                 ok = (X509_load_cert_crl_file_ex(  | 
63  | 0  |                          ctx, X509_get_default_cert_file(),  | 
64  | 0  |                          X509_FILETYPE_PEM, libctx, propq) != 0);  | 
65  |  | 
  | 
66  | 0  |             if (!ok)  | 
67  | 0  |                 ERR_raise(ERR_LIB_X509, X509_R_LOADING_DEFAULTS);  | 
68  | 0  |         } else { | 
69  | 0  |             if (argl == X509_FILETYPE_PEM)  | 
70  | 0  |                 ok = (X509_load_cert_crl_file_ex(ctx, argp, X509_FILETYPE_PEM,  | 
71  | 0  |                                                  libctx, propq) != 0);  | 
72  | 0  |             else  | 
73  | 0  |                 ok = (X509_load_cert_file_ex(ctx, argp, (int)argl, libctx,  | 
74  | 0  |                                              propq) != 0);  | 
75  | 0  |         }  | 
76  | 0  |         break;  | 
77  | 0  |     }  | 
78  | 0  |     return ok;  | 
79  | 0  | }  | 
80  |  |  | 
81  |  | static int by_file_ctrl(X509_LOOKUP *ctx, int cmd,  | 
82  |  |                         const char *argp, long argl, char **ret)  | 
83  | 0  | { | 
84  | 0  |     return by_file_ctrl_ex(ctx, cmd, argp, argl, ret, NULL, NULL);  | 
85  | 0  | }  | 
86  |  |  | 
87  |  | int X509_load_cert_file_ex(X509_LOOKUP *ctx, const char *file, int type,  | 
88  |  |                            OSSL_LIB_CTX *libctx, const char *propq)  | 
89  | 0  | { | 
90  | 0  |     BIO *in = NULL;  | 
91  | 0  |     int count = 0;  | 
92  | 0  |     X509 *x = NULL;  | 
93  |  | 
  | 
94  | 0  |     if (file == NULL) { | 
95  | 0  |         ERR_raise(ERR_LIB_X509, ERR_R_PASSED_NULL_PARAMETER);  | 
96  | 0  |         goto err;  | 
97  | 0  |     }  | 
98  |  |  | 
99  | 0  |     in = BIO_new(BIO_s_file());  | 
100  |  | 
  | 
101  | 0  |     if ((in == NULL) || (BIO_read_filename(in, file) <= 0)) { | 
102  | 0  |         ERR_raise(ERR_LIB_X509, ERR_R_BIO_LIB);  | 
103  | 0  |         goto err;  | 
104  | 0  |     }  | 
105  |  |  | 
106  | 0  |     x = X509_new_ex(libctx, propq);  | 
107  | 0  |     if (x == NULL) { | 
108  | 0  |         ERR_raise(ERR_LIB_X509, ERR_R_ASN1_LIB);  | 
109  | 0  |         goto err;  | 
110  | 0  |     }  | 
111  |  |  | 
112  | 0  |     if (type == X509_FILETYPE_PEM) { | 
113  | 0  |         for (;;) { | 
114  | 0  |             ERR_set_mark();  | 
115  | 0  |             if (PEM_read_bio_X509_AUX(in, &x, NULL, "") == NULL) { | 
116  | 0  |                 if ((ERR_GET_REASON(ERR_peek_last_error()) ==  | 
117  | 0  |                      PEM_R_NO_START_LINE) && (count > 0)) { | 
118  | 0  |                     ERR_pop_to_mark();  | 
119  | 0  |                     break;  | 
120  | 0  |                 } else { | 
121  | 0  |                     ERR_clear_last_mark();  | 
122  | 0  |                     if (count == 0) { | 
123  | 0  |                         ERR_raise(ERR_LIB_X509, X509_R_NO_CERTIFICATE_FOUND);  | 
124  | 0  |                     } else { | 
125  | 0  |                         ERR_raise(ERR_LIB_X509, ERR_R_PEM_LIB);  | 
126  | 0  |                         count = 0;  | 
127  | 0  |                     }  | 
128  | 0  |                     goto err;  | 
129  | 0  |                 }  | 
130  | 0  |             }  | 
131  | 0  |             ERR_clear_last_mark();  | 
132  | 0  |             if (!X509_STORE_add_cert(ctx->store_ctx, x)) { | 
133  | 0  |                 count = 0;  | 
134  | 0  |                 goto err;  | 
135  | 0  |             }  | 
136  |  |             /*  | 
137  |  |              * X509_STORE_add_cert() added a reference rather than a copy,  | 
138  |  |              * so we need a fresh X509 object.  | 
139  |  |              */  | 
140  | 0  |             X509_free(x);  | 
141  | 0  |             x = X509_new_ex(libctx, propq);  | 
142  | 0  |             if (x == NULL) { | 
143  | 0  |                 ERR_raise(ERR_LIB_X509, ERR_R_ASN1_LIB);  | 
144  | 0  |                 count = 0;  | 
145  | 0  |                 goto err;  | 
146  | 0  |             }  | 
147  | 0  |             count++;  | 
148  | 0  |         }  | 
149  | 0  |     } else if (type == X509_FILETYPE_ASN1) { | 
150  | 0  |         if (d2i_X509_bio(in, &x) == NULL) { | 
151  | 0  |             ERR_raise(ERR_LIB_X509, X509_R_NO_CERTIFICATE_FOUND);  | 
152  | 0  |             goto err;  | 
153  | 0  |         }  | 
154  | 0  |         count = X509_STORE_add_cert(ctx->store_ctx, x);  | 
155  | 0  |     } else { | 
156  | 0  |         ERR_raise(ERR_LIB_X509, X509_R_BAD_X509_FILETYPE);  | 
157  | 0  |         goto err;  | 
158  | 0  |     }  | 
159  | 0  |  err:  | 
160  | 0  |     X509_free(x);  | 
161  | 0  |     BIO_free(in);  | 
162  | 0  |     return count;  | 
163  | 0  | }  | 
164  |  |  | 
165  |  | int X509_load_cert_file(X509_LOOKUP *ctx, const char *file, int type)  | 
166  | 0  | { | 
167  | 0  |     return X509_load_cert_file_ex(ctx, file, type, NULL, NULL);  | 
168  | 0  | }  | 
169  |  |  | 
170  |  | int X509_load_crl_file(X509_LOOKUP *ctx, const char *file, int type)  | 
171  | 0  | { | 
172  | 0  |     BIO *in = NULL;  | 
173  | 0  |     int count = 0;  | 
174  | 0  |     X509_CRL *x = NULL;  | 
175  |  | 
  | 
176  | 0  |     if (file == NULL) { | 
177  | 0  |         ERR_raise(ERR_LIB_X509, ERR_R_PASSED_NULL_PARAMETER);  | 
178  | 0  |         goto err;  | 
179  | 0  |     }  | 
180  |  |  | 
181  | 0  |     in = BIO_new(BIO_s_file());  | 
182  |  | 
  | 
183  | 0  |     if ((in == NULL) || (BIO_read_filename(in, file) <= 0)) { | 
184  | 0  |         ERR_raise(ERR_LIB_X509, ERR_R_BIO_LIB);  | 
185  | 0  |         goto err;  | 
186  | 0  |     }  | 
187  |  |  | 
188  | 0  |     if (type == X509_FILETYPE_PEM) { | 
189  | 0  |         for (;;) { | 
190  | 0  |             x = PEM_read_bio_X509_CRL(in, NULL, NULL, "");  | 
191  | 0  |             if (x == NULL) { | 
192  | 0  |                 if ((ERR_GET_REASON(ERR_peek_last_error()) ==  | 
193  | 0  |                      PEM_R_NO_START_LINE) && (count > 0)) { | 
194  | 0  |                     ERR_clear_error();  | 
195  | 0  |                     break;  | 
196  | 0  |                 } else { | 
197  | 0  |                     if (count == 0) { | 
198  | 0  |                         ERR_raise(ERR_LIB_X509, X509_R_NO_CRL_FOUND);  | 
199  | 0  |                     } else { | 
200  | 0  |                         ERR_raise(ERR_LIB_X509, ERR_R_PEM_LIB);  | 
201  | 0  |                         count = 0;  | 
202  | 0  |                     }  | 
203  | 0  |                     goto err;  | 
204  | 0  |                 }  | 
205  | 0  |             }  | 
206  | 0  |             if (!X509_STORE_add_crl(ctx->store_ctx, x)) { | 
207  | 0  |                 count = 0;  | 
208  | 0  |                 goto err;  | 
209  | 0  |             }  | 
210  | 0  |             count++;  | 
211  | 0  |             X509_CRL_free(x);  | 
212  | 0  |             x = NULL;  | 
213  | 0  |         }  | 
214  | 0  |     } else if (type == X509_FILETYPE_ASN1) { | 
215  | 0  |         x = d2i_X509_CRL_bio(in, NULL);  | 
216  | 0  |         if (x == NULL) { | 
217  | 0  |             ERR_raise(ERR_LIB_X509, X509_R_NO_CRL_FOUND);  | 
218  | 0  |             goto err;  | 
219  | 0  |         }  | 
220  | 0  |         count = X509_STORE_add_crl(ctx->store_ctx, x);  | 
221  | 0  |     } else { | 
222  | 0  |         ERR_raise(ERR_LIB_X509, X509_R_BAD_X509_FILETYPE);  | 
223  | 0  |         goto err;  | 
224  | 0  |     }  | 
225  | 0  |  err:  | 
226  | 0  |     X509_CRL_free(x);  | 
227  | 0  |     BIO_free(in);  | 
228  | 0  |     return count;  | 
229  | 0  | }  | 
230  |  |  | 
231  |  | int X509_load_cert_crl_file_ex(X509_LOOKUP *ctx, const char *file, int type,  | 
232  |  |                                OSSL_LIB_CTX *libctx, const char *propq)  | 
233  | 0  | { | 
234  | 0  |     STACK_OF(X509_INFO) *inf = NULL;  | 
235  | 0  |     X509_INFO *itmp = NULL;  | 
236  | 0  |     BIO *in = NULL;  | 
237  | 0  |     int i, count = 0;  | 
238  |  | 
  | 
239  | 0  |     if (type != X509_FILETYPE_PEM)  | 
240  | 0  |         return X509_load_cert_file_ex(ctx, file, type, libctx, propq);  | 
241  |  | #if defined(OPENSSL_SYS_WINDOWS)  | 
242  |  |     in = BIO_new_file(file, "rb");  | 
243  |  | #else  | 
244  | 0  |     in = BIO_new_file(file, "r");  | 
245  | 0  | #endif  | 
246  | 0  |     if (in == NULL) { | 
247  | 0  |         ERR_raise(ERR_LIB_X509, ERR_R_BIO_LIB);  | 
248  | 0  |         return 0;  | 
249  | 0  |     }  | 
250  | 0  |     inf = PEM_X509_INFO_read_bio_ex(in, NULL, NULL, "", libctx, propq);  | 
251  | 0  |     BIO_free(in);  | 
252  | 0  |     if (inf == NULL) { | 
253  | 0  |         ERR_raise(ERR_LIB_X509, ERR_R_PEM_LIB);  | 
254  | 0  |         return 0;  | 
255  | 0  |     }  | 
256  | 0  |     for (i = 0; i < sk_X509_INFO_num(inf); i++) { | 
257  | 0  |         itmp = sk_X509_INFO_value(inf, i);  | 
258  | 0  |         if (itmp->x509) { | 
259  | 0  |             if (!X509_STORE_add_cert(ctx->store_ctx, itmp->x509)) { | 
260  | 0  |                 count = 0;  | 
261  | 0  |                 goto err;  | 
262  | 0  |             }  | 
263  | 0  |             count++;  | 
264  | 0  |         }  | 
265  | 0  |         if (itmp->crl) { | 
266  | 0  |             if (!X509_STORE_add_crl(ctx->store_ctx, itmp->crl)) { | 
267  | 0  |                 count = 0;  | 
268  | 0  |                 goto err;  | 
269  | 0  |             }  | 
270  | 0  |             count++;  | 
271  | 0  |         }  | 
272  | 0  |     }  | 
273  | 0  |     if (count == 0)  | 
274  | 0  |         ERR_raise(ERR_LIB_X509, X509_R_NO_CERTIFICATE_OR_CRL_FOUND);  | 
275  | 0  |  err:  | 
276  | 0  |     sk_X509_INFO_pop_free(inf, X509_INFO_free);  | 
277  | 0  |     return count;  | 
278  | 0  | }  | 
279  |  |  | 
280  |  | int X509_load_cert_crl_file(X509_LOOKUP *ctx, const char *file, int type)  | 
281  | 0  | { | 
282  | 0  |     return X509_load_cert_crl_file_ex(ctx, file, type, NULL, NULL);  | 
283  | 0  | }  | 
284  |  |  |