/src/openssl/crypto/x509/by_file.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved. |
3 | | * |
4 | | * Licensed under the OpenSSL license (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_lcl.h" |
19 | | |
20 | | static int by_file_ctrl(X509_LOOKUP *ctx, int cmd, const char *argc, |
21 | | long argl, char **ret); |
22 | | static X509_LOOKUP_METHOD x509_file_lookup = { |
23 | | "Load file into cache", |
24 | | NULL, /* new_item */ |
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 | | }; |
34 | | |
35 | | X509_LOOKUP_METHOD *X509_LOOKUP_file(void) |
36 | 0 | { |
37 | 0 | return &x509_file_lookup; |
38 | 0 | } |
39 | | |
40 | | static int by_file_ctrl(X509_LOOKUP *ctx, int cmd, const char *argp, |
41 | | long argl, char **ret) |
42 | 0 | { |
43 | 0 | int ok = 0; |
44 | 0 | const char *file; |
45 | 0 |
|
46 | 0 | switch (cmd) { |
47 | 0 | case X509_L_FILE_LOAD: |
48 | 0 | if (argl == X509_FILETYPE_DEFAULT) { |
49 | 0 | file = getenv(X509_get_default_cert_file_env()); |
50 | 0 | if (file) |
51 | 0 | ok = (X509_load_cert_crl_file(ctx, file, |
52 | 0 | X509_FILETYPE_PEM) != 0); |
53 | 0 |
|
54 | 0 | else |
55 | 0 | ok = (X509_load_cert_crl_file |
56 | 0 | (ctx, X509_get_default_cert_file(), |
57 | 0 | X509_FILETYPE_PEM) != 0); |
58 | 0 |
|
59 | 0 | if (!ok) { |
60 | 0 | X509err(X509_F_BY_FILE_CTRL, X509_R_LOADING_DEFAULTS); |
61 | 0 | } |
62 | 0 | } else { |
63 | 0 | if (argl == X509_FILETYPE_PEM) |
64 | 0 | ok = (X509_load_cert_crl_file(ctx, argp, |
65 | 0 | X509_FILETYPE_PEM) != 0); |
66 | 0 | else |
67 | 0 | ok = (X509_load_cert_file(ctx, argp, (int)argl) != 0); |
68 | 0 | } |
69 | 0 | break; |
70 | 0 | } |
71 | 0 | return ok; |
72 | 0 | } |
73 | | |
74 | | int X509_load_cert_file(X509_LOOKUP *ctx, const char *file, int type) |
75 | 0 | { |
76 | 0 | int ret = 0; |
77 | 0 | BIO *in = NULL; |
78 | 0 | int i, count = 0; |
79 | 0 | X509 *x = NULL; |
80 | 0 |
|
81 | 0 | in = BIO_new(BIO_s_file()); |
82 | 0 |
|
83 | 0 | if ((in == NULL) || (BIO_read_filename(in, file) <= 0)) { |
84 | 0 | X509err(X509_F_X509_LOAD_CERT_FILE, ERR_R_SYS_LIB); |
85 | 0 | goto err; |
86 | 0 | } |
87 | 0 |
|
88 | 0 | if (type == X509_FILETYPE_PEM) { |
89 | 0 | for (;;) { |
90 | 0 | x = PEM_read_bio_X509_AUX(in, NULL, NULL, ""); |
91 | 0 | if (x == NULL) { |
92 | 0 | if ((ERR_GET_REASON(ERR_peek_last_error()) == |
93 | 0 | PEM_R_NO_START_LINE) && (count > 0)) { |
94 | 0 | ERR_clear_error(); |
95 | 0 | break; |
96 | 0 | } else { |
97 | 0 | X509err(X509_F_X509_LOAD_CERT_FILE, ERR_R_PEM_LIB); |
98 | 0 | goto err; |
99 | 0 | } |
100 | 0 | } |
101 | 0 | i = X509_STORE_add_cert(ctx->store_ctx, x); |
102 | 0 | if (!i) |
103 | 0 | goto err; |
104 | 0 | count++; |
105 | 0 | X509_free(x); |
106 | 0 | x = NULL; |
107 | 0 | } |
108 | 0 | ret = count; |
109 | 0 | } else if (type == X509_FILETYPE_ASN1) { |
110 | 0 | x = d2i_X509_bio(in, NULL); |
111 | 0 | if (x == NULL) { |
112 | 0 | X509err(X509_F_X509_LOAD_CERT_FILE, ERR_R_ASN1_LIB); |
113 | 0 | goto err; |
114 | 0 | } |
115 | 0 | i = X509_STORE_add_cert(ctx->store_ctx, x); |
116 | 0 | if (!i) |
117 | 0 | goto err; |
118 | 0 | ret = i; |
119 | 0 | } else { |
120 | 0 | X509err(X509_F_X509_LOAD_CERT_FILE, X509_R_BAD_X509_FILETYPE); |
121 | 0 | goto err; |
122 | 0 | } |
123 | 0 | if (ret == 0) |
124 | 0 | X509err(X509_F_X509_LOAD_CERT_FILE, X509_R_NO_CERTIFICATE_FOUND); |
125 | 0 | err: |
126 | 0 | X509_free(x); |
127 | 0 | BIO_free(in); |
128 | 0 | return ret; |
129 | 0 | } |
130 | | |
131 | | int X509_load_crl_file(X509_LOOKUP *ctx, const char *file, int type) |
132 | 0 | { |
133 | 0 | int ret = 0; |
134 | 0 | BIO *in = NULL; |
135 | 0 | int i, count = 0; |
136 | 0 | X509_CRL *x = NULL; |
137 | 0 |
|
138 | 0 | in = BIO_new(BIO_s_file()); |
139 | 0 |
|
140 | 0 | if ((in == NULL) || (BIO_read_filename(in, file) <= 0)) { |
141 | 0 | X509err(X509_F_X509_LOAD_CRL_FILE, ERR_R_SYS_LIB); |
142 | 0 | goto err; |
143 | 0 | } |
144 | 0 |
|
145 | 0 | if (type == X509_FILETYPE_PEM) { |
146 | 0 | for (;;) { |
147 | 0 | x = PEM_read_bio_X509_CRL(in, NULL, NULL, ""); |
148 | 0 | if (x == NULL) { |
149 | 0 | if ((ERR_GET_REASON(ERR_peek_last_error()) == |
150 | 0 | PEM_R_NO_START_LINE) && (count > 0)) { |
151 | 0 | ERR_clear_error(); |
152 | 0 | break; |
153 | 0 | } else { |
154 | 0 | X509err(X509_F_X509_LOAD_CRL_FILE, ERR_R_PEM_LIB); |
155 | 0 | goto err; |
156 | 0 | } |
157 | 0 | } |
158 | 0 | i = X509_STORE_add_crl(ctx->store_ctx, x); |
159 | 0 | if (!i) |
160 | 0 | goto err; |
161 | 0 | count++; |
162 | 0 | X509_CRL_free(x); |
163 | 0 | x = NULL; |
164 | 0 | } |
165 | 0 | ret = count; |
166 | 0 | } else if (type == X509_FILETYPE_ASN1) { |
167 | 0 | x = d2i_X509_CRL_bio(in, NULL); |
168 | 0 | if (x == NULL) { |
169 | 0 | X509err(X509_F_X509_LOAD_CRL_FILE, ERR_R_ASN1_LIB); |
170 | 0 | goto err; |
171 | 0 | } |
172 | 0 | i = X509_STORE_add_crl(ctx->store_ctx, x); |
173 | 0 | if (!i) |
174 | 0 | goto err; |
175 | 0 | ret = i; |
176 | 0 | } else { |
177 | 0 | X509err(X509_F_X509_LOAD_CRL_FILE, X509_R_BAD_X509_FILETYPE); |
178 | 0 | goto err; |
179 | 0 | } |
180 | 0 | if (ret == 0) |
181 | 0 | X509err(X509_F_X509_LOAD_CRL_FILE, X509_R_NO_CRL_FOUND); |
182 | 0 | err: |
183 | 0 | X509_CRL_free(x); |
184 | 0 | BIO_free(in); |
185 | 0 | return ret; |
186 | 0 | } |
187 | | |
188 | | int X509_load_cert_crl_file(X509_LOOKUP *ctx, const char *file, int type) |
189 | 0 | { |
190 | 0 | STACK_OF(X509_INFO) *inf; |
191 | 0 | X509_INFO *itmp; |
192 | 0 | BIO *in; |
193 | 0 | int i, count = 0; |
194 | 0 |
|
195 | 0 | if (type != X509_FILETYPE_PEM) |
196 | 0 | return X509_load_cert_file(ctx, file, type); |
197 | 0 | in = BIO_new_file(file, "r"); |
198 | 0 | if (!in) { |
199 | 0 | X509err(X509_F_X509_LOAD_CERT_CRL_FILE, ERR_R_SYS_LIB); |
200 | 0 | return 0; |
201 | 0 | } |
202 | 0 | inf = PEM_X509_INFO_read_bio(in, NULL, NULL, ""); |
203 | 0 | BIO_free(in); |
204 | 0 | if (!inf) { |
205 | 0 | X509err(X509_F_X509_LOAD_CERT_CRL_FILE, ERR_R_PEM_LIB); |
206 | 0 | return 0; |
207 | 0 | } |
208 | 0 | for (i = 0; i < sk_X509_INFO_num(inf); i++) { |
209 | 0 | itmp = sk_X509_INFO_value(inf, i); |
210 | 0 | if (itmp->x509) { |
211 | 0 | if (!X509_STORE_add_cert(ctx->store_ctx, itmp->x509)) |
212 | 0 | goto err; |
213 | 0 | count++; |
214 | 0 | } |
215 | 0 | if (itmp->crl) { |
216 | 0 | if (!X509_STORE_add_crl(ctx->store_ctx, itmp->crl)) |
217 | 0 | goto err; |
218 | 0 | count++; |
219 | 0 | } |
220 | 0 | } |
221 | 0 | if (count == 0) |
222 | 0 | X509err(X509_F_X509_LOAD_CERT_CRL_FILE, |
223 | 0 | X509_R_NO_CERTIFICATE_OR_CRL_FOUND); |
224 | 0 | err: |
225 | 0 | sk_X509_INFO_pop_free(inf, X509_INFO_free); |
226 | 0 | return count; |
227 | 0 | } |