summaryrefslogtreecommitdiff
path: root/src/lib/libcrypto/x509/by_file.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/libcrypto/x509/by_file.c')
-rw-r--r--src/lib/libcrypto/x509/by_file.c272
1 files changed, 129 insertions, 143 deletions
diff --git a/src/lib/libcrypto/x509/by_file.c b/src/lib/libcrypto/x509/by_file.c
index 57b08ee094..474d13bf0e 100644
--- a/src/lib/libcrypto/x509/by_file.c
+++ b/src/lib/libcrypto/x509/by_file.c
@@ -69,9 +69,9 @@
69#ifndef OPENSSL_NO_STDIO 69#ifndef OPENSSL_NO_STDIO
70 70
71static int by_file_ctrl(X509_LOOKUP *ctx, int cmd, const char *argc, 71static int by_file_ctrl(X509_LOOKUP *ctx, int cmd, const char *argc,
72 long argl, char **ret); 72 long argl, char **ret);
73X509_LOOKUP_METHOD x509_file_lookup= 73
74 { 74X509_LOOKUP_METHOD x509_file_lookup = {
75 "Load file into cache", 75 "Load file into cache",
76 NULL, /* new */ 76 NULL, /* new */
77 NULL, /* free */ 77 NULL, /* free */
@@ -82,211 +82,199 @@ X509_LOOKUP_METHOD x509_file_lookup=
82 NULL, /* get_by_issuer_serial */ 82 NULL, /* get_by_issuer_serial */
83 NULL, /* get_by_fingerprint */ 83 NULL, /* get_by_fingerprint */
84 NULL, /* get_by_alias */ 84 NULL, /* get_by_alias */
85 }; 85};
86 86
87X509_LOOKUP_METHOD *X509_LOOKUP_file(void) 87X509_LOOKUP_METHOD
88 { 88*X509_LOOKUP_file(void)
89 return(&x509_file_lookup); 89{
90 } 90 return (&x509_file_lookup);
91}
91 92
92static int by_file_ctrl(X509_LOOKUP *ctx, int cmd, const char *argp, long argl, 93static int
93 char **ret) 94by_file_ctrl(X509_LOOKUP *ctx, int cmd, const char *argp, long argl,
94 { 95 char **ret)
95 int ok=0; 96{
97 int ok = 0;
96 char *file; 98 char *file;
97 99
98 switch (cmd) 100 switch (cmd) {
99 {
100 case X509_L_FILE_LOAD: 101 case X509_L_FILE_LOAD:
101 if (argl == X509_FILETYPE_DEFAULT) 102 if (argl == X509_FILETYPE_DEFAULT) {
102 {
103 file = (char *)getenv(X509_get_default_cert_file_env()); 103 file = (char *)getenv(X509_get_default_cert_file_env());
104 if (file) 104 if (file)
105 ok = (X509_load_cert_crl_file(ctx,file, 105 ok = (X509_load_cert_crl_file(ctx, file,
106 X509_FILETYPE_PEM) != 0); 106 X509_FILETYPE_PEM) != 0);
107
108 else 107 else
109 ok = (X509_load_cert_crl_file(ctx,X509_get_default_cert_file(), 108 ok = (X509_load_cert_crl_file(ctx,
110 X509_FILETYPE_PEM) != 0); 109 X509_get_default_cert_file(),
110 X509_FILETYPE_PEM) != 0);
111 111
112 if (!ok) 112 if (!ok) {
113 { 113 X509err(X509_F_BY_FILE_CTRL,
114 X509err(X509_F_BY_FILE_CTRL,X509_R_LOADING_DEFAULTS); 114 X509_R_LOADING_DEFAULTS);
115 }
116 } 115 }
117 else 116 } else {
118 { 117 if (argl == X509_FILETYPE_PEM)
119 if(argl == X509_FILETYPE_PEM) 118 ok = (X509_load_cert_crl_file(ctx, argp,
120 ok = (X509_load_cert_crl_file(ctx,argp, 119 X509_FILETYPE_PEM) != 0);
121 X509_FILETYPE_PEM) != 0);
122 else 120 else
123 ok = (X509_load_cert_file(ctx,argp,(int)argl) != 0); 121 ok = (X509_load_cert_file(ctx,
124 } 122 argp,(int)argl) != 0);
125 break;
126 } 123 }
127 return(ok); 124 break;
128 } 125 }
126 return (ok);
127}
129 128
130int X509_load_cert_file(X509_LOOKUP *ctx, const char *file, int type) 129int
131 { 130X509_load_cert_file(X509_LOOKUP *ctx, const char *file, int type)
132 int ret=0; 131{
133 BIO *in=NULL; 132 int ret = 0;
134 int i,count=0; 133 BIO *in = NULL;
135 X509 *x=NULL; 134 int i, count = 0;
135 X509 *x = NULL;
136 136
137 if (file == NULL) return(1); 137 if (file == NULL)
138 in=BIO_new(BIO_s_file_internal()); 138 return (1);
139 in = BIO_new(BIO_s_file_internal());
139 140
140 if ((in == NULL) || (BIO_read_filename(in,file) <= 0)) 141 if ((in == NULL) || (BIO_read_filename(in, file) <= 0)) {
141 { 142 X509err(X509_F_X509_LOAD_CERT_FILE, ERR_R_SYS_LIB);
142 X509err(X509_F_X509_LOAD_CERT_FILE,ERR_R_SYS_LIB);
143 goto err; 143 goto err;
144 } 144 }
145 145
146 if (type == X509_FILETYPE_PEM) 146 if (type == X509_FILETYPE_PEM) {
147 { 147 for (;;) {
148 for (;;) 148 x = PEM_read_bio_X509_AUX(in, NULL, NULL, NULL);
149 { 149 if (x == NULL) {
150 x=PEM_read_bio_X509_AUX(in,NULL,NULL,NULL);
151 if (x == NULL)
152 {
153 if ((ERR_GET_REASON(ERR_peek_last_error()) == 150 if ((ERR_GET_REASON(ERR_peek_last_error()) ==
154 PEM_R_NO_START_LINE) && (count > 0)) 151 PEM_R_NO_START_LINE) && (count > 0)) {
155 {
156 ERR_clear_error(); 152 ERR_clear_error();
157 break; 153 break;
158 } 154 } else {
159 else
160 {
161 X509err(X509_F_X509_LOAD_CERT_FILE, 155 X509err(X509_F_X509_LOAD_CERT_FILE,
162 ERR_R_PEM_LIB); 156 ERR_R_PEM_LIB);
163 goto err; 157 goto err;
164 }
165 } 158 }
166 i=X509_STORE_add_cert(ctx->store_ctx,x); 159 }
167 if (!i) goto err; 160 i = X509_STORE_add_cert(ctx->store_ctx, x);
161 if (!i)
162 goto err;
168 count++; 163 count++;
169 X509_free(x); 164 X509_free(x);
170 x=NULL; 165 x = NULL;
171 }
172 ret=count;
173 } 166 }
174 else if (type == X509_FILETYPE_ASN1) 167 ret = count;
175 { 168 } else if (type == X509_FILETYPE_ASN1) {
176 x=d2i_X509_bio(in,NULL); 169 x = d2i_X509_bio(in, NULL);
177 if (x == NULL) 170 if (x == NULL) {
178 { 171 X509err(X509_F_X509_LOAD_CERT_FILE, ERR_R_ASN1_LIB);
179 X509err(X509_F_X509_LOAD_CERT_FILE,ERR_R_ASN1_LIB);
180 goto err; 172 goto err;
181 }
182 i=X509_STORE_add_cert(ctx->store_ctx,x);
183 if (!i) goto err;
184 ret=i;
185 } 173 }
186 else 174 i = X509_STORE_add_cert(ctx->store_ctx, x);
187 { 175 if (!i)
188 X509err(X509_F_X509_LOAD_CERT_FILE,X509_R_BAD_X509_FILETYPE); 176 goto err;
177 ret = i;
178 } else {
179 X509err(X509_F_X509_LOAD_CERT_FILE, X509_R_BAD_X509_FILETYPE);
189 goto err; 180 goto err;
190 }
191err:
192 if (x != NULL) X509_free(x);
193 if (in != NULL) BIO_free(in);
194 return(ret);
195 } 181 }
182err:
183 if (x != NULL)
184 X509_free(x);
185 if (in != NULL)
186 BIO_free(in);
187 return (ret);
188}
196 189
197int X509_load_crl_file(X509_LOOKUP *ctx, const char *file, int type) 190int
198 { 191X509_load_crl_file(X509_LOOKUP *ctx, const char *file, int type)
199 int ret=0; 192{
200 BIO *in=NULL; 193 int ret = 0;
201 int i,count=0; 194 BIO *in = NULL;
202 X509_CRL *x=NULL; 195 int i, count = 0;
196 X509_CRL *x = NULL;
203 197
204 if (file == NULL) return(1); 198 if (file == NULL)
205 in=BIO_new(BIO_s_file_internal()); 199 return (1);
200 in = BIO_new(BIO_s_file_internal());
206 201
207 if ((in == NULL) || (BIO_read_filename(in,file) <= 0)) 202 if ((in == NULL) || (BIO_read_filename(in, file) <= 0)) {
208 { 203 X509err(X509_F_X509_LOAD_CRL_FILE, ERR_R_SYS_LIB);
209 X509err(X509_F_X509_LOAD_CRL_FILE,ERR_R_SYS_LIB);
210 goto err; 204 goto err;
211 } 205 }
212 206
213 if (type == X509_FILETYPE_PEM) 207 if (type == X509_FILETYPE_PEM) {
214 { 208 for (;;) {
215 for (;;) 209 x = PEM_read_bio_X509_CRL(in, NULL, NULL, NULL);
216 { 210 if (x == NULL) {
217 x=PEM_read_bio_X509_CRL(in,NULL,NULL,NULL);
218 if (x == NULL)
219 {
220 if ((ERR_GET_REASON(ERR_peek_last_error()) == 211 if ((ERR_GET_REASON(ERR_peek_last_error()) ==
221 PEM_R_NO_START_LINE) && (count > 0)) 212 PEM_R_NO_START_LINE) && (count > 0)) {
222 {
223 ERR_clear_error(); 213 ERR_clear_error();
224 break; 214 break;
225 } 215 } else {
226 else
227 {
228 X509err(X509_F_X509_LOAD_CRL_FILE, 216 X509err(X509_F_X509_LOAD_CRL_FILE,
229 ERR_R_PEM_LIB); 217 ERR_R_PEM_LIB);
230 goto err; 218 goto err;
231 }
232 } 219 }
233 i=X509_STORE_add_crl(ctx->store_ctx,x); 220 }
234 if (!i) goto err; 221 i = X509_STORE_add_crl(ctx->store_ctx, x);
222 if (!i)
223 goto err;
235 count++; 224 count++;
236 X509_CRL_free(x); 225 X509_CRL_free(x);
237 x=NULL; 226 x = NULL;
238 }
239 ret=count;
240 } 227 }
241 else if (type == X509_FILETYPE_ASN1) 228 ret = count;
242 { 229 } else if (type == X509_FILETYPE_ASN1) {
243 x=d2i_X509_CRL_bio(in,NULL); 230 x = d2i_X509_CRL_bio(in, NULL);
244 if (x == NULL) 231 if (x == NULL) {
245 { 232 X509err(X509_F_X509_LOAD_CRL_FILE, ERR_R_ASN1_LIB);
246 X509err(X509_F_X509_LOAD_CRL_FILE,ERR_R_ASN1_LIB);
247 goto err; 233 goto err;
248 }
249 i=X509_STORE_add_crl(ctx->store_ctx,x);
250 if (!i) goto err;
251 ret=i;
252 } 234 }
253 else 235 i = X509_STORE_add_crl(ctx->store_ctx, x);
254 { 236 if (!i)
255 X509err(X509_F_X509_LOAD_CRL_FILE,X509_R_BAD_X509_FILETYPE); 237 goto err;
238 ret = i;
239 } else {
240 X509err(X509_F_X509_LOAD_CRL_FILE, X509_R_BAD_X509_FILETYPE);
256 goto err; 241 goto err;
257 }
258err:
259 if (x != NULL) X509_CRL_free(x);
260 if (in != NULL) BIO_free(in);
261 return(ret);
262 } 242 }
243err:
244 if (x != NULL)
245 X509_CRL_free(x);
246 if (in != NULL)
247 BIO_free(in);
248 return (ret);
249}
263 250
264int X509_load_cert_crl_file(X509_LOOKUP *ctx, const char *file, int type) 251int
252X509_load_cert_crl_file(X509_LOOKUP *ctx, const char *file, int type)
265{ 253{
266 STACK_OF(X509_INFO) *inf; 254 STACK_OF(X509_INFO) *inf;
267 X509_INFO *itmp; 255 X509_INFO *itmp;
268 BIO *in; 256 BIO *in;
269 int i, count = 0; 257 int i, count = 0;
270 if(type != X509_FILETYPE_PEM) 258 if (type != X509_FILETYPE_PEM)
271 return X509_load_cert_file(ctx, file, type); 259 return X509_load_cert_file(ctx, file, type);
272 in = BIO_new_file(file, "r"); 260 in = BIO_new_file(file, "r");
273 if(!in) { 261 if (!in) {
274 X509err(X509_F_X509_LOAD_CERT_CRL_FILE,ERR_R_SYS_LIB); 262 X509err(X509_F_X509_LOAD_CERT_CRL_FILE, ERR_R_SYS_LIB);
275 return 0; 263 return 0;
276 } 264 }
277 inf = PEM_X509_INFO_read_bio(in, NULL, NULL, NULL); 265 inf = PEM_X509_INFO_read_bio(in, NULL, NULL, NULL);
278 BIO_free(in); 266 BIO_free(in);
279 if(!inf) { 267 if (!inf) {
280 X509err(X509_F_X509_LOAD_CERT_CRL_FILE,ERR_R_PEM_LIB); 268 X509err(X509_F_X509_LOAD_CERT_CRL_FILE, ERR_R_PEM_LIB);
281 return 0; 269 return 0;
282 } 270 }
283 for(i = 0; i < sk_X509_INFO_num(inf); i++) { 271 for (i = 0; i < sk_X509_INFO_num(inf); i++) {
284 itmp = sk_X509_INFO_value(inf, i); 272 itmp = sk_X509_INFO_value(inf, i);
285 if(itmp->x509) { 273 if (itmp->x509) {
286 X509_STORE_add_cert(ctx->store_ctx, itmp->x509); 274 X509_STORE_add_cert(ctx->store_ctx, itmp->x509);
287 count++; 275 count++;
288 } 276 }
289 if(itmp->crl) { 277 if (itmp->crl) {
290 X509_STORE_add_crl(ctx->store_ctx, itmp->crl); 278 X509_STORE_add_crl(ctx->store_ctx, itmp->crl);
291 count++; 279 count++;
292 } 280 }
@@ -295,6 +283,4 @@ int X509_load_cert_crl_file(X509_LOOKUP *ctx, const char *file, int type)
295 return count; 283 return count;
296} 284}
297 285
298
299#endif /* OPENSSL_NO_STDIO */ 286#endif /* OPENSSL_NO_STDIO */
300