/src/openssl/crypto/x509/by_file.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright 1995-2023 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 | 5.26k | { |
44 | 5.26k | return &x509_file_lookup; |
45 | 5.26k | } |
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 | 2.63k | { |
51 | 2.63k | int ok = 0; |
52 | 2.63k | const char *file; |
53 | | |
54 | 2.63k | switch (cmd) { |
55 | 2.63k | case X509_L_FILE_LOAD: |
56 | 2.63k | 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 | 2.63k | } else { |
69 | 2.63k | if (argl == X509_FILETYPE_PEM) |
70 | 2.63k | ok = (X509_load_cert_crl_file_ex(ctx, argp, X509_FILETYPE_PEM, |
71 | 2.63k | libctx, propq) != 0); |
72 | 0 | else |
73 | 0 | ok = (X509_load_cert_file_ex(ctx, argp, (int)argl, libctx, |
74 | 0 | propq) != 0); |
75 | 2.63k | } |
76 | 2.63k | break; |
77 | 2.63k | } |
78 | 2.63k | return ok; |
79 | 2.63k | } |
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 | in = BIO_new(BIO_s_file()); |
95 | |
|
96 | 0 | if ((in == NULL) || (BIO_read_filename(in, file) <= 0)) { |
97 | 0 | ERR_raise(ERR_LIB_X509, ERR_R_BIO_LIB); |
98 | 0 | goto err; |
99 | 0 | } |
100 | | |
101 | 0 | x = X509_new_ex(libctx, propq); |
102 | 0 | if (x == NULL) { |
103 | 0 | ERR_raise(ERR_LIB_X509, ERR_R_ASN1_LIB); |
104 | 0 | goto err; |
105 | 0 | } |
106 | | |
107 | 0 | if (type == X509_FILETYPE_PEM) { |
108 | 0 | for (;;) { |
109 | 0 | ERR_set_mark(); |
110 | 0 | if (PEM_read_bio_X509_AUX(in, &x, NULL, "") == NULL) { |
111 | 0 | if ((ERR_GET_REASON(ERR_peek_last_error()) == |
112 | 0 | PEM_R_NO_START_LINE) && (count > 0)) { |
113 | 0 | ERR_pop_to_mark(); |
114 | 0 | break; |
115 | 0 | } else { |
116 | 0 | ERR_clear_last_mark(); |
117 | 0 | if (count == 0) { |
118 | 0 | ERR_raise(ERR_LIB_X509, X509_R_NO_CERTIFICATE_FOUND); |
119 | 0 | } else { |
120 | 0 | ERR_raise(ERR_LIB_X509, ERR_R_PEM_LIB); |
121 | 0 | count = 0; |
122 | 0 | } |
123 | 0 | goto err; |
124 | 0 | } |
125 | 0 | } |
126 | 0 | ERR_clear_last_mark(); |
127 | 0 | if (!X509_STORE_add_cert(ctx->store_ctx, x)) { |
128 | 0 | count = 0; |
129 | 0 | goto err; |
130 | 0 | } |
131 | 0 | count++; |
132 | 0 | } |
133 | 0 | } else if (type == X509_FILETYPE_ASN1) { |
134 | 0 | if (d2i_X509_bio(in, &x) == NULL) { |
135 | 0 | ERR_raise(ERR_LIB_X509, X509_R_NO_CERTIFICATE_FOUND); |
136 | 0 | goto err; |
137 | 0 | } |
138 | 0 | count = X509_STORE_add_cert(ctx->store_ctx, x); |
139 | 0 | } else { |
140 | 0 | ERR_raise(ERR_LIB_X509, X509_R_BAD_X509_FILETYPE); |
141 | 0 | goto err; |
142 | 0 | } |
143 | 0 | err: |
144 | 0 | X509_free(x); |
145 | 0 | BIO_free(in); |
146 | 0 | return count; |
147 | 0 | } |
148 | | |
149 | | int X509_load_cert_file(X509_LOOKUP *ctx, const char *file, int type) |
150 | 0 | { |
151 | 0 | return X509_load_cert_file_ex(ctx, file, type, NULL, NULL); |
152 | 0 | } |
153 | | |
154 | | int X509_load_crl_file(X509_LOOKUP *ctx, const char *file, int type) |
155 | 2.63k | { |
156 | 2.63k | BIO *in = NULL; |
157 | 2.63k | int count = 0; |
158 | 2.63k | X509_CRL *x = NULL; |
159 | | |
160 | 2.63k | in = BIO_new(BIO_s_file()); |
161 | | |
162 | 2.63k | if ((in == NULL) || (BIO_read_filename(in, file) <= 0)) { |
163 | 0 | ERR_raise(ERR_LIB_X509, ERR_R_BIO_LIB); |
164 | 0 | goto err; |
165 | 0 | } |
166 | | |
167 | 2.63k | if (type == X509_FILETYPE_PEM) { |
168 | 2.63k | for (;;) { |
169 | 2.63k | x = PEM_read_bio_X509_CRL(in, NULL, NULL, ""); |
170 | 2.63k | if (x == NULL) { |
171 | 2.63k | if ((ERR_GET_REASON(ERR_peek_last_error()) == |
172 | 2.63k | PEM_R_NO_START_LINE) && (count > 0)) { |
173 | 0 | ERR_clear_error(); |
174 | 0 | break; |
175 | 2.63k | } else { |
176 | 2.63k | if (count == 0) { |
177 | 2.63k | ERR_raise(ERR_LIB_X509, X509_R_NO_CRL_FOUND); |
178 | 2.63k | } else { |
179 | 0 | ERR_raise(ERR_LIB_X509, ERR_R_PEM_LIB); |
180 | 0 | count = 0; |
181 | 0 | } |
182 | 2.63k | goto err; |
183 | 2.63k | } |
184 | 2.63k | } |
185 | 0 | if (!X509_STORE_add_crl(ctx->store_ctx, x)) { |
186 | 0 | count = 0; |
187 | 0 | goto err; |
188 | 0 | } |
189 | 0 | count++; |
190 | 0 | } |
191 | 2.63k | } else if (type == X509_FILETYPE_ASN1) { |
192 | 0 | x = d2i_X509_CRL_bio(in, NULL); |
193 | 0 | if (x == NULL) { |
194 | 0 | ERR_raise(ERR_LIB_X509, X509_R_NO_CRL_FOUND); |
195 | 0 | goto err; |
196 | 0 | } |
197 | 0 | count = X509_STORE_add_crl(ctx->store_ctx, x); |
198 | 0 | } else { |
199 | 0 | ERR_raise(ERR_LIB_X509, X509_R_BAD_X509_FILETYPE); |
200 | 0 | goto err; |
201 | 0 | } |
202 | 2.63k | err: |
203 | 2.63k | X509_CRL_free(x); |
204 | 2.63k | BIO_free(in); |
205 | 2.63k | return count; |
206 | 2.63k | } |
207 | | |
208 | | int X509_load_cert_crl_file_ex(X509_LOOKUP *ctx, const char *file, int type, |
209 | | OSSL_LIB_CTX *libctx, const char *propq) |
210 | 2.63k | { |
211 | 2.63k | STACK_OF(X509_INFO) *inf = NULL; |
212 | 2.63k | X509_INFO *itmp = NULL; |
213 | 2.63k | BIO *in = NULL; |
214 | 2.63k | int i, count = 0; |
215 | | |
216 | 2.63k | if (type != X509_FILETYPE_PEM) |
217 | 0 | return X509_load_cert_file_ex(ctx, file, type, libctx, propq); |
218 | 2.63k | in = BIO_new_file(file, "r"); |
219 | 2.63k | if (in == NULL) { |
220 | 0 | ERR_raise(ERR_LIB_X509, ERR_R_BIO_LIB); |
221 | 0 | return 0; |
222 | 0 | } |
223 | 2.63k | inf = PEM_X509_INFO_read_bio_ex(in, NULL, NULL, "", libctx, propq); |
224 | 2.63k | BIO_free(in); |
225 | 2.63k | if (inf == NULL) { |
226 | 0 | ERR_raise(ERR_LIB_X509, ERR_R_PEM_LIB); |
227 | 0 | return 0; |
228 | 0 | } |
229 | 362k | for (i = 0; i < sk_X509_INFO_num(inf); i++) { |
230 | 360k | itmp = sk_X509_INFO_value(inf, i); |
231 | 360k | if (itmp->x509) { |
232 | 360k | if (!X509_STORE_add_cert(ctx->store_ctx, itmp->x509)) { |
233 | 0 | count = 0; |
234 | 0 | goto err; |
235 | 0 | } |
236 | 360k | count++; |
237 | 360k | } |
238 | 360k | if (itmp->crl) { |
239 | 0 | if (!X509_STORE_add_crl(ctx->store_ctx, itmp->crl)) { |
240 | 0 | count = 0; |
241 | 0 | goto err; |
242 | 0 | } |
243 | 0 | count++; |
244 | 0 | } |
245 | 360k | } |
246 | 2.63k | if (count == 0) |
247 | 2.63k | ERR_raise(ERR_LIB_X509, X509_R_NO_CERTIFICATE_OR_CRL_FOUND); |
248 | 2.63k | err: |
249 | 2.63k | sk_X509_INFO_pop_free(inf, X509_INFO_free); |
250 | 2.63k | return count; |
251 | 2.63k | } |
252 | | |
253 | | int X509_load_cert_crl_file(X509_LOOKUP *ctx, const char *file, int type) |
254 | 0 | { |
255 | 0 | return X509_load_cert_crl_file_ex(ctx, file, type, NULL, NULL); |
256 | 0 | } |
257 | | |