]>
Commit | Line | Data |
---|---|---|
9c6da42d RL |
1 | /* |
2 | * Copyright 2016-2017 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 <string.h> | |
970f467a RL |
11 | #include <sys/stat.h> |
12 | #include <assert.h> | |
9c6da42d RL |
13 | |
14 | #include <openssl/bio.h> | |
15 | #include <openssl/dsa.h> /* For d2i_DSAPrivateKey */ | |
16 | #include <openssl/err.h> | |
17 | #include <openssl/evp.h> | |
18 | #include <openssl/pem.h> | |
19 | #include <openssl/pkcs12.h> /* For the PKCS8 stuff o.O */ | |
20 | #include <openssl/rsa.h> /* For d2i_RSAPrivateKey */ | |
21 | #include <openssl/safestack.h> | |
22 | #include <openssl/store.h> | |
23 | #include <openssl/ui.h> | |
24 | #include <openssl/x509.h> /* For the PKCS8 stuff o.O */ | |
25 | #include "internal/asn1_int.h" | |
970f467a RL |
26 | #include "internal/o_dir.h" |
27 | #include "internal/cryptlib.h" | |
4c17819c | 28 | #include "internal/store_int.h" |
9c6da42d RL |
29 | #include "store_locl.h" |
30 | ||
31 | #include "e_os.h" | |
32 | ||
71d57be5 RL |
33 | #ifdef _WIN32 |
34 | # define stat _stat | |
35 | #endif | |
36 | ||
479af767 | 37 | /*- |
9c6da42d | 38 | * Password prompting |
479af767 | 39 | * ------------------ |
9c6da42d RL |
40 | */ |
41 | ||
42 | static char *file_get_pass(const UI_METHOD *ui_method, char *pass, | |
43 | size_t maxsize, const char *prompt_info, void *data) | |
44 | { | |
45 | UI *ui = UI_new(); | |
46 | char *prompt = NULL; | |
47 | ||
48 | if (ui == NULL) { | |
49 | OSSL_STOREerr(OSSL_STORE_F_FILE_GET_PASS, ERR_R_MALLOC_FAILURE); | |
50 | return NULL; | |
51 | } | |
52 | ||
53 | if (ui_method != NULL) | |
54 | UI_set_method(ui, ui_method); | |
55 | UI_add_user_data(ui, data); | |
56 | ||
57 | if ((prompt = UI_construct_prompt(ui, "pass phrase", | |
58 | prompt_info)) == NULL) { | |
59 | OSSL_STOREerr(OSSL_STORE_F_FILE_GET_PASS, ERR_R_MALLOC_FAILURE); | |
60 | pass = NULL; | |
61 | } else if (!UI_add_input_string(ui, prompt, UI_INPUT_FLAG_DEFAULT_PWD, | |
62 | pass, 0, maxsize - 1)) { | |
63 | OSSL_STOREerr(OSSL_STORE_F_FILE_GET_PASS, ERR_R_UI_LIB); | |
64 | pass = NULL; | |
65 | } else { | |
66 | switch (UI_process(ui)) { | |
67 | case -2: | |
68 | OSSL_STOREerr(OSSL_STORE_F_FILE_GET_PASS, | |
69 | OSSL_STORE_R_UI_PROCESS_INTERRUPTED_OR_CANCELLED); | |
70 | pass = NULL; | |
71 | break; | |
72 | case -1: | |
73 | OSSL_STOREerr(OSSL_STORE_F_FILE_GET_PASS, ERR_R_UI_LIB); | |
74 | pass = NULL; | |
75 | break; | |
76 | default: | |
77 | break; | |
78 | } | |
79 | } | |
80 | ||
81 | OPENSSL_free(prompt); | |
82 | UI_free(ui); | |
83 | return pass; | |
84 | } | |
85 | ||
86 | struct pem_pass_data { | |
87 | const UI_METHOD *ui_method; | |
88 | void *data; | |
89 | const char *prompt_info; | |
90 | }; | |
479af767 | 91 | |
9c6da42d RL |
92 | static int file_fill_pem_pass_data(struct pem_pass_data *pass_data, |
93 | const char *prompt_info, | |
94 | const UI_METHOD *ui_method, void *ui_data) | |
95 | { | |
96 | if (pass_data == NULL) | |
97 | return 0; | |
98 | pass_data->ui_method = ui_method; | |
99 | pass_data->data = ui_data; | |
100 | pass_data->prompt_info = prompt_info; | |
101 | return 1; | |
102 | } | |
479af767 RL |
103 | |
104 | /* This is used anywhere a pem_password_cb is needed */ | |
9c6da42d RL |
105 | static int file_get_pem_pass(char *buf, int num, int w, void *data) |
106 | { | |
107 | struct pem_pass_data *pass_data = data; | |
108 | char *pass = file_get_pass(pass_data->ui_method, buf, num, | |
109 | pass_data->prompt_info, pass_data->data); | |
110 | ||
111 | return pass == NULL ? 0 : strlen(pass); | |
112 | } | |
113 | ||
479af767 RL |
114 | /*- |
115 | * The file scheme decoders | |
116 | * ------------------------ | |
117 | * | |
118 | * Each possible data type has its own decoder, which either operates | |
119 | * through a given PEM name, or attempts to decode to see if the blob | |
120 | * it's given is decodable for its data type. The assumption is that | |
121 | * only the correct data type will match the content. | |
9c6da42d RL |
122 | */ |
123 | ||
124 | /*- | |
125 | * The try_decode function is called to check if the blob of data can | |
126 | * be used by this handler, and if it can, decodes it into a supported | |
127 | * OpenSSL type and returns a OSSL_STORE_INFO with the decoded data. | |
128 | * Input: | |
129 | * pem_name: If this blob comes from a PEM file, this holds | |
130 | * the PEM name. If it comes from another type of | |
131 | * file, this is NULL. | |
132 | * pem_header: If this blob comes from a PEM file, this holds | |
133 | * the PEM headers. If it comes from another type of | |
134 | * file, this is NULL. | |
135 | * blob: The blob of data to match with what this handler | |
136 | * can use. | |
137 | * len: The length of the blob. | |
e61ec2d9 RL |
138 | * handler_ctx: For a handler marked repeatable, this pointer can |
139 | * be used to create a context for the handler. IT IS | |
140 | * THE HANDLER'S RESPONSIBILITY TO CREATE AND DESTROY | |
141 | * THIS CONTEXT APPROPRIATELY, i.e. create on first call | |
142 | * and destroy when about to return NULL. | |
6fc1d33c RL |
143 | * matchcount: A pointer to an int to count matches for this data. |
144 | * Usually becomes 0 (no match) or 1 (match!), but may | |
145 | * be higher in the (unlikely) event that the data matches | |
146 | * more than one possibility. The int will always be | |
147 | * zero when the function is called. | |
9c6da42d RL |
148 | * ui_method: Application UI method for getting a password, pin |
149 | * or any other interactive data. | |
150 | * ui_data: Application data to be passed to ui_method when | |
151 | * it's called. | |
152 | * Output: | |
153 | * a OSSL_STORE_INFO | |
154 | */ | |
155 | typedef OSSL_STORE_INFO *(*file_try_decode_fn)(const char *pem_name, | |
156 | const char *pem_header, | |
157 | const unsigned char *blob, | |
e61ec2d9 | 158 | size_t len, void **handler_ctx, |
6fc1d33c | 159 | int *matchcount, |
9c6da42d RL |
160 | const UI_METHOD *ui_method, |
161 | void *ui_data); | |
e61ec2d9 RL |
162 | /* |
163 | * The eof function should return 1 if there's no more data to be found | |
164 | * with the handler_ctx, otherwise 0. This is only used when the handler is | |
165 | * marked repeatable. | |
166 | */ | |
167 | typedef int (*file_eof_fn)(void *handler_ctx); | |
168 | /* | |
169 | * The destroy_ctx function is used to destroy the handler_ctx that was | |
170 | * intiated by a repeatable try_decode fuction. This is only used when | |
171 | * the handler is marked repeatable. | |
172 | */ | |
173 | typedef void (*file_destroy_ctx_fn)(void **handler_ctx); | |
9c6da42d RL |
174 | |
175 | typedef struct file_handler_st { | |
176 | const char *name; | |
177 | file_try_decode_fn try_decode; | |
e61ec2d9 RL |
178 | file_eof_fn eof; |
179 | file_destroy_ctx_fn destroy_ctx; | |
180 | ||
181 | /* flags */ | |
182 | int repeatable; | |
9c6da42d RL |
183 | } FILE_HANDLER; |
184 | ||
479af767 RL |
185 | /* |
186 | * PKCS#12 decoder. It operates by decoding all of the blob content, | |
187 | * extracting all the interesting data from it and storing them internally, | |
188 | * then serving them one piece at a time. | |
189 | */ | |
a09003ea RL |
190 | static OSSL_STORE_INFO *try_decode_PKCS12(const char *pem_name, |
191 | const char *pem_header, | |
192 | const unsigned char *blob, | |
193 | size_t len, void **pctx, | |
6fc1d33c | 194 | int *matchcount, |
a09003ea RL |
195 | const UI_METHOD *ui_method, |
196 | void *ui_data) | |
197 | { | |
198 | OSSL_STORE_INFO *store_info = NULL; | |
199 | STACK_OF(OSSL_STORE_INFO) *ctx = *pctx; | |
200 | ||
201 | if (ctx == NULL) { | |
202 | /* Initial parsing */ | |
203 | PKCS12 *p12; | |
204 | int ok = 0; | |
205 | ||
206 | if (pem_name != NULL) | |
207 | /* No match, there is no PEM PKCS12 tag */ | |
208 | return NULL; | |
209 | ||
210 | if ((p12 = d2i_PKCS12(NULL, &blob, len)) != NULL) { | |
211 | char *pass = NULL; | |
212 | char tpass[PEM_BUFSIZE]; | |
213 | EVP_PKEY *pkey = NULL; | |
214 | X509 *cert = NULL; | |
215 | STACK_OF(X509) *chain = NULL; | |
216 | ||
6fc1d33c RL |
217 | *matchcount = 1; |
218 | ||
a09003ea RL |
219 | if (PKCS12_verify_mac(p12, "", 0) |
220 | || PKCS12_verify_mac(p12, NULL, 0)) { | |
221 | pass = ""; | |
222 | } else { | |
223 | if ((pass = file_get_pass(ui_method, tpass, PEM_BUFSIZE, | |
224 | "PKCS12 import password", | |
225 | ui_data)) == NULL) { | |
226 | OSSL_STOREerr(OSSL_STORE_F_TRY_DECODE_PKCS12, | |
227 | OSSL_STORE_R_PASSPHRASE_CALLBACK_ERROR); | |
228 | goto p12_end; | |
229 | } | |
230 | if (!PKCS12_verify_mac(p12, pass, strlen(pass))) { | |
231 | OSSL_STOREerr(OSSL_STORE_F_TRY_DECODE_PKCS12, | |
232 | OSSL_STORE_R_ERROR_VERIFYING_PKCS12_MAC); | |
233 | goto p12_end; | |
234 | } | |
235 | } | |
236 | ||
237 | if (PKCS12_parse(p12, pass, &pkey, &cert, &chain)) { | |
238 | OSSL_STORE_INFO *si_pkey = NULL; | |
239 | OSSL_STORE_INFO *si_cert = NULL; | |
240 | OSSL_STORE_INFO *si_ca = NULL; | |
241 | ||
242 | if ((ctx = sk_OSSL_STORE_INFO_new_null()) != NULL | |
243 | && (si_pkey = OSSL_STORE_INFO_new_PKEY(pkey)) != NULL | |
244 | && sk_OSSL_STORE_INFO_push(ctx, si_pkey) != 0 | |
245 | && (si_cert = OSSL_STORE_INFO_new_CERT(cert)) != NULL | |
246 | && sk_OSSL_STORE_INFO_push(ctx, si_cert) != 0) { | |
247 | ok = 1; | |
248 | si_pkey = NULL; | |
249 | si_cert = NULL; | |
250 | ||
251 | while(sk_X509_num(chain) > 0) { | |
252 | X509 *ca = sk_X509_value(chain, 0); | |
253 | ||
254 | if ((si_ca = OSSL_STORE_INFO_new_CERT(ca)) == NULL | |
255 | || sk_OSSL_STORE_INFO_push(ctx, si_ca) == 0) { | |
256 | ok = 0; | |
257 | break; | |
258 | } | |
259 | si_ca = NULL; | |
260 | (void)sk_X509_shift(chain); | |
261 | } | |
262 | } | |
263 | if (!ok) { | |
264 | OSSL_STORE_INFO_free(si_ca); | |
265 | OSSL_STORE_INFO_free(si_cert); | |
266 | OSSL_STORE_INFO_free(si_pkey); | |
267 | sk_OSSL_STORE_INFO_pop_free(ctx, OSSL_STORE_INFO_free); | |
268 | EVP_PKEY_free(pkey); | |
269 | X509_free(cert); | |
270 | sk_X509_pop_free(chain, X509_free); | |
271 | ctx = NULL; | |
272 | } | |
273 | *pctx = ctx; | |
274 | } | |
275 | } | |
276 | p12_end: | |
277 | PKCS12_free(p12); | |
278 | if (!ok) | |
279 | return NULL; | |
280 | } | |
281 | ||
6fc1d33c RL |
282 | if (ctx != NULL) { |
283 | *matchcount = 1; | |
a09003ea | 284 | store_info = sk_OSSL_STORE_INFO_shift(ctx); |
6fc1d33c | 285 | } |
a09003ea RL |
286 | |
287 | return store_info; | |
288 | } | |
479af767 | 289 | |
a09003ea RL |
290 | static int eof_PKCS12(void *ctx_) |
291 | { | |
292 | STACK_OF(OSSL_STORE_INFO) *ctx = ctx_; | |
293 | ||
294 | return ctx == NULL || sk_OSSL_STORE_INFO_num(ctx) == 0; | |
295 | } | |
479af767 | 296 | |
a09003ea RL |
297 | static void destroy_ctx_PKCS12(void **pctx) |
298 | { | |
299 | STACK_OF(OSSL_STORE_INFO) *ctx = *pctx; | |
300 | ||
301 | sk_OSSL_STORE_INFO_pop_free(ctx, OSSL_STORE_INFO_free); | |
302 | *pctx = NULL; | |
303 | } | |
479af767 | 304 | |
a09003ea RL |
305 | static FILE_HANDLER PKCS12_handler = { |
306 | "PKCS12", | |
307 | try_decode_PKCS12, | |
308 | eof_PKCS12, | |
309 | destroy_ctx_PKCS12, | |
310 | 1 /* repeatable */ | |
311 | }; | |
312 | ||
479af767 RL |
313 | /* |
314 | * Encrypted PKCS#8 decoder. It operates by just decrypting the given blob | |
315 | * into a new blob, which is returned as an EMBEDDED STORE_INFO. The whole | |
316 | * decoding process will then start over with the new blob. | |
317 | */ | |
7ad2ef36 RL |
318 | static OSSL_STORE_INFO *try_decode_PKCS8Encrypted(const char *pem_name, |
319 | const char *pem_header, | |
320 | const unsigned char *blob, | |
321 | size_t len, void **pctx, | |
6fc1d33c | 322 | int *matchcount, |
7ad2ef36 RL |
323 | const UI_METHOD *ui_method, |
324 | void *ui_data) | |
325 | { | |
326 | X509_SIG *p8 = NULL; | |
327 | char kbuf[PEM_BUFSIZE]; | |
328 | char *pass = NULL; | |
329 | const X509_ALGOR *dalg = NULL; | |
330 | const ASN1_OCTET_STRING *doct = NULL; | |
331 | OSSL_STORE_INFO *store_info = NULL; | |
332 | BUF_MEM *mem = NULL; | |
333 | unsigned char *new_data = NULL; | |
334 | int new_data_len; | |
335 | ||
6fc1d33c RL |
336 | if (pem_name != NULL) { |
337 | if (strcmp(pem_name, PEM_STRING_PKCS8) != 0) | |
338 | return NULL; | |
339 | *matchcount = 1; | |
340 | } | |
7ad2ef36 RL |
341 | |
342 | if ((p8 = d2i_X509_SIG(NULL, &blob, len)) == NULL) | |
343 | return NULL; | |
344 | ||
6fc1d33c RL |
345 | *matchcount = 1; |
346 | ||
7ad2ef36 RL |
347 | if ((mem = BUF_MEM_new()) == NULL) { |
348 | OSSL_STOREerr(OSSL_STORE_F_TRY_DECODE_PKCS8ENCRYPTED, | |
349 | ERR_R_MALLOC_FAILURE); | |
350 | goto nop8; | |
351 | } | |
352 | ||
353 | if ((pass = file_get_pass(ui_method, kbuf, PEM_BUFSIZE, | |
354 | "PKCS8 decrypt password", ui_data)) == NULL) { | |
355 | OSSL_STOREerr(OSSL_STORE_F_TRY_DECODE_PKCS8ENCRYPTED, | |
356 | OSSL_STORE_R_BAD_PASSWORD_READ); | |
357 | goto nop8; | |
358 | } | |
359 | ||
360 | X509_SIG_get0(p8, &dalg, &doct); | |
361 | if (!PKCS12_pbe_crypt(dalg, pass, strlen(pass), doct->data, doct->length, | |
362 | &new_data, &new_data_len, 0)) | |
363 | goto nop8; | |
364 | ||
365 | mem->data = (char *)new_data; | |
366 | mem->max = mem->length = (size_t)new_data_len; | |
367 | X509_SIG_free(p8); | |
368 | ||
369 | store_info = ossl_store_info_new_EMBEDDED(PEM_STRING_PKCS8INF, mem); | |
370 | if (store_info == NULL) { | |
371 | OSSL_STOREerr(OSSL_STORE_F_TRY_DECODE_PKCS8ENCRYPTED, | |
372 | ERR_R_MALLOC_FAILURE); | |
373 | goto nop8; | |
374 | } | |
375 | ||
376 | return store_info; | |
377 | nop8: | |
378 | X509_SIG_free(p8); | |
379 | BUF_MEM_free(mem); | |
380 | return NULL; | |
381 | } | |
479af767 | 382 | |
7ad2ef36 RL |
383 | static FILE_HANDLER PKCS8Encrypted_handler = { |
384 | "PKCS8Encrypted", | |
385 | try_decode_PKCS8Encrypted | |
386 | }; | |
387 | ||
479af767 RL |
388 | /* |
389 | * Private key decoder. Decodes all sorts of private keys, both PKCS#8 | |
390 | * encoded ones and old style PEM ones (with the key type is encoded into | |
391 | * the PEM name). | |
392 | */ | |
9c6da42d RL |
393 | int pem_check_suffix(const char *pem_str, const char *suffix); |
394 | static OSSL_STORE_INFO *try_decode_PrivateKey(const char *pem_name, | |
395 | const char *pem_header, | |
396 | const unsigned char *blob, | |
e61ec2d9 | 397 | size_t len, void **pctx, |
6fc1d33c | 398 | int *matchcount, |
9c6da42d RL |
399 | const UI_METHOD *ui_method, |
400 | void *ui_data) | |
401 | { | |
402 | OSSL_STORE_INFO *store_info = NULL; | |
403 | EVP_PKEY *pkey = NULL; | |
404 | const EVP_PKEY_ASN1_METHOD *ameth = NULL; | |
405 | ||
406 | if (pem_name != NULL) { | |
7ad2ef36 RL |
407 | if (strcmp(pem_name, PEM_STRING_PKCS8INF) == 0) { |
408 | PKCS8_PRIV_KEY_INFO *p8inf = | |
409 | d2i_PKCS8_PRIV_KEY_INFO(NULL, &blob, len); | |
410 | ||
6fc1d33c | 411 | *matchcount = 1; |
7ad2ef36 RL |
412 | if (p8inf != NULL) |
413 | pkey = EVP_PKCS82PKEY(p8inf); | |
414 | PKCS8_PRIV_KEY_INFO_free(p8inf); | |
415 | } else { | |
416 | int slen; | |
417 | ||
418 | if ((slen = pem_check_suffix(pem_name, "PRIVATE KEY")) > 0 | |
419 | && (ameth = EVP_PKEY_asn1_find_str(NULL, pem_name, | |
6fc1d33c RL |
420 | slen)) != NULL) { |
421 | *matchcount = 1; | |
7ad2ef36 | 422 | pkey = d2i_PrivateKey(ameth->pkey_id, NULL, &blob, len); |
6fc1d33c | 423 | } |
7ad2ef36 | 424 | } |
9c6da42d RL |
425 | } else { |
426 | int i; | |
427 | ||
428 | for (i = 0; i < EVP_PKEY_asn1_get_count(); i++) { | |
6fc1d33c RL |
429 | EVP_PKEY *tmp_pkey = NULL; |
430 | const unsigned char *tmp_blob = blob; | |
431 | ||
9c6da42d RL |
432 | ameth = EVP_PKEY_asn1_get0(i); |
433 | if (ameth->pkey_flags & ASN1_PKEY_ALIAS) | |
434 | continue; | |
6fc1d33c RL |
435 | |
436 | tmp_pkey = d2i_PrivateKey(ameth->pkey_id, NULL, &tmp_blob, len); | |
437 | if (tmp_pkey != NULL) { | |
438 | if (pkey != NULL) | |
439 | EVP_PKEY_free(tmp_pkey); | |
440 | else | |
441 | pkey = tmp_pkey; | |
442 | (*matchcount)++; | |
443 | } | |
444 | } | |
445 | ||
446 | if (*matchcount > 1) { | |
447 | EVP_PKEY_free(pkey); | |
448 | pkey = NULL; | |
9c6da42d RL |
449 | } |
450 | } | |
451 | if (pkey == NULL) | |
452 | /* No match */ | |
453 | return NULL; | |
454 | ||
455 | store_info = OSSL_STORE_INFO_new_PKEY(pkey); | |
456 | if (store_info == NULL) | |
457 | EVP_PKEY_free(pkey); | |
458 | ||
459 | return store_info; | |
460 | } | |
479af767 | 461 | |
9c6da42d RL |
462 | static FILE_HANDLER PrivateKey_handler = { |
463 | "PrivateKey", | |
464 | try_decode_PrivateKey | |
465 | }; | |
466 | ||
479af767 RL |
467 | /* |
468 | * Public key decoder. Only supports SubjectPublicKeyInfo formated keys. | |
469 | */ | |
9c6da42d RL |
470 | static OSSL_STORE_INFO *try_decode_PUBKEY(const char *pem_name, |
471 | const char *pem_header, | |
472 | const unsigned char *blob, | |
e61ec2d9 | 473 | size_t len, void **pctx, |
6fc1d33c | 474 | int *matchcount, |
9c6da42d RL |
475 | const UI_METHOD *ui_method, |
476 | void *ui_data) | |
477 | { | |
478 | OSSL_STORE_INFO *store_info = NULL; | |
479 | EVP_PKEY *pkey = NULL; | |
480 | ||
6fc1d33c RL |
481 | if (pem_name != NULL) { |
482 | if (strcmp(pem_name, PEM_STRING_PUBLIC) != 0) | |
483 | /* No match */ | |
484 | return NULL; | |
485 | *matchcount = 1; | |
486 | } | |
9c6da42d | 487 | |
6fc1d33c RL |
488 | if ((pkey = d2i_PUBKEY(NULL, &blob, len)) != NULL) { |
489 | *matchcount = 1; | |
9c6da42d | 490 | store_info = OSSL_STORE_INFO_new_PKEY(pkey); |
6fc1d33c | 491 | } |
9c6da42d RL |
492 | |
493 | return store_info; | |
494 | } | |
479af767 | 495 | |
9c6da42d RL |
496 | static FILE_HANDLER PUBKEY_handler = { |
497 | "PUBKEY", | |
498 | try_decode_PUBKEY | |
499 | }; | |
500 | ||
479af767 RL |
501 | /* |
502 | * Key parameter decoder. | |
503 | */ | |
9c6da42d RL |
504 | static OSSL_STORE_INFO *try_decode_params(const char *pem_name, |
505 | const char *pem_header, | |
506 | const unsigned char *blob, | |
e61ec2d9 | 507 | size_t len, void **pctx, |
6fc1d33c | 508 | int *matchcount, |
9c6da42d RL |
509 | const UI_METHOD *ui_method, |
510 | void *ui_data) | |
511 | { | |
512 | OSSL_STORE_INFO *store_info = NULL; | |
6fc1d33c RL |
513 | int slen = 0; |
514 | EVP_PKEY *pkey = NULL; | |
9c6da42d RL |
515 | const EVP_PKEY_ASN1_METHOD *ameth = NULL; |
516 | int ok = 0; | |
517 | ||
6fc1d33c RL |
518 | if (pem_name != NULL) { |
519 | if ((slen = pem_check_suffix(pem_name, "PARAMETERS")) == 0) | |
520 | return NULL; | |
521 | *matchcount = 1; | |
522 | } | |
523 | ||
6fc1d33c | 524 | if (slen > 0) { |
11d66064 RL |
525 | if ((pkey = EVP_PKEY_new()) == NULL) { |
526 | OSSL_STOREerr(OSSL_STORE_F_TRY_DECODE_PARAMS, ERR_R_EVP_LIB); | |
527 | return NULL; | |
528 | } | |
529 | ||
530 | ||
6fc1d33c | 531 | if (EVP_PKEY_set_type_str(pkey, pem_name, slen) |
9c6da42d RL |
532 | && (ameth = EVP_PKEY_get0_asn1(pkey)) != NULL |
533 | && ameth->param_decode != NULL | |
534 | && ameth->param_decode(pkey, &blob, len)) | |
535 | ok = 1; | |
536 | } else { | |
537 | int i; | |
11d66064 | 538 | EVP_PKEY *tmp_pkey = NULL; |
9c6da42d RL |
539 | |
540 | for (i = 0; i < EVP_PKEY_asn1_get_count(); i++) { | |
6fc1d33c RL |
541 | const unsigned char *tmp_blob = blob; |
542 | ||
11d66064 RL |
543 | if (tmp_pkey == NULL && (tmp_pkey = EVP_PKEY_new()) == NULL) { |
544 | OSSL_STOREerr(OSSL_STORE_F_TRY_DECODE_PARAMS, ERR_R_EVP_LIB); | |
545 | break; | |
546 | } | |
547 | ||
9c6da42d RL |
548 | ameth = EVP_PKEY_asn1_get0(i); |
549 | if (ameth->pkey_flags & ASN1_PKEY_ALIAS) | |
550 | continue; | |
11d66064 RL |
551 | |
552 | if (EVP_PKEY_set_type(tmp_pkey, ameth->pkey_id) | |
553 | && (ameth = EVP_PKEY_get0_asn1(tmp_pkey)) != NULL | |
9c6da42d | 554 | && ameth->param_decode != NULL |
11d66064 RL |
555 | && ameth->param_decode(tmp_pkey, &tmp_blob, len)) { |
556 | if (pkey != NULL) | |
557 | EVP_PKEY_free(tmp_pkey); | |
558 | else | |
559 | pkey = tmp_pkey; | |
560 | tmp_pkey = NULL; | |
6fc1d33c | 561 | (*matchcount)++; |
9c6da42d RL |
562 | } |
563 | } | |
11d66064 RL |
564 | |
565 | EVP_PKEY_free(tmp_pkey); | |
566 | if (*matchcount == 1) { | |
567 | ok = 1; | |
568 | } | |
9c6da42d RL |
569 | } |
570 | ||
571 | if (ok) | |
572 | store_info = OSSL_STORE_INFO_new_PARAMS(pkey); | |
573 | if (store_info == NULL) | |
574 | EVP_PKEY_free(pkey); | |
575 | ||
576 | return store_info; | |
577 | } | |
479af767 | 578 | |
9c6da42d RL |
579 | static FILE_HANDLER params_handler = { |
580 | "params", | |
581 | try_decode_params | |
582 | }; | |
583 | ||
479af767 RL |
584 | /* |
585 | * X.509 certificate decoder. | |
586 | */ | |
9c6da42d RL |
587 | static OSSL_STORE_INFO *try_decode_X509Certificate(const char *pem_name, |
588 | const char *pem_header, | |
589 | const unsigned char *blob, | |
e61ec2d9 | 590 | size_t len, void **pctx, |
6fc1d33c | 591 | int *matchcount, |
9c6da42d RL |
592 | const UI_METHOD *ui_method, |
593 | void *ui_data) | |
594 | { | |
595 | OSSL_STORE_INFO *store_info = NULL; | |
596 | X509 *cert = NULL; | |
597 | ||
598 | /* | |
599 | * In most cases, we can try to interpret the serialized data as a trusted | |
600 | * cert (X509 + X509_AUX) and fall back to reading it as a normal cert | |
601 | * (just X509), but if the PEM name specifically declares it as a trusted | |
602 | * cert, then no fallback should be engaged. |ignore_trusted| tells if | |
603 | * the fallback can be used (1) or not (0). | |
604 | */ | |
605 | int ignore_trusted = 1; | |
606 | ||
607 | if (pem_name != NULL) { | |
608 | if (strcmp(pem_name, PEM_STRING_X509_TRUSTED) == 0) | |
609 | ignore_trusted = 0; | |
610 | else if (strcmp(pem_name, PEM_STRING_X509_OLD) != 0 | |
611 | && strcmp(pem_name, PEM_STRING_X509) != 0) | |
612 | /* No match */ | |
613 | return NULL; | |
6fc1d33c | 614 | *matchcount = 1; |
9c6da42d RL |
615 | } |
616 | ||
617 | if ((cert = d2i_X509_AUX(NULL, &blob, len)) != NULL | |
6fc1d33c RL |
618 | || (ignore_trusted && (cert = d2i_X509(NULL, &blob, len)) != NULL)) { |
619 | *matchcount = 1; | |
9c6da42d | 620 | store_info = OSSL_STORE_INFO_new_CERT(cert); |
6fc1d33c | 621 | } |
9c6da42d RL |
622 | |
623 | if (store_info == NULL) | |
624 | X509_free(cert); | |
625 | ||
626 | return store_info; | |
627 | } | |
479af767 | 628 | |
9c6da42d RL |
629 | static FILE_HANDLER X509Certificate_handler = { |
630 | "X509Certificate", | |
631 | try_decode_X509Certificate | |
632 | }; | |
633 | ||
479af767 RL |
634 | /* |
635 | * X.509 CRL decoder. | |
636 | */ | |
9c6da42d RL |
637 | static OSSL_STORE_INFO *try_decode_X509CRL(const char *pem_name, |
638 | const char *pem_header, | |
639 | const unsigned char *blob, | |
e61ec2d9 | 640 | size_t len, void **pctx, |
6fc1d33c | 641 | int *matchcount, |
9c6da42d RL |
642 | const UI_METHOD *ui_method, |
643 | void *ui_data) | |
644 | { | |
645 | OSSL_STORE_INFO *store_info = NULL; | |
646 | X509_CRL *crl = NULL; | |
647 | ||
6fc1d33c RL |
648 | if (pem_name != NULL) { |
649 | if (strcmp(pem_name, PEM_STRING_X509_CRL) != 0) | |
650 | /* No match */ | |
651 | return NULL; | |
652 | *matchcount = 1; | |
653 | } | |
9c6da42d | 654 | |
6fc1d33c RL |
655 | if ((crl = d2i_X509_CRL(NULL, &blob, len)) != NULL) { |
656 | *matchcount = 1; | |
9c6da42d | 657 | store_info = OSSL_STORE_INFO_new_CRL(crl); |
6fc1d33c | 658 | } |
9c6da42d RL |
659 | |
660 | if (store_info == NULL) | |
661 | X509_CRL_free(crl); | |
662 | ||
663 | return store_info; | |
664 | } | |
479af767 | 665 | |
9c6da42d RL |
666 | static FILE_HANDLER X509CRL_handler = { |
667 | "X509CRL", | |
668 | try_decode_X509CRL | |
669 | }; | |
670 | ||
479af767 RL |
671 | /* |
672 | * To finish it all off, we collect all the handlers. | |
673 | */ | |
9c6da42d | 674 | static const FILE_HANDLER *file_handlers[] = { |
a09003ea | 675 | &PKCS12_handler, |
7ad2ef36 | 676 | &PKCS8Encrypted_handler, |
9c6da42d RL |
677 | &X509Certificate_handler, |
678 | &X509CRL_handler, | |
679 | ¶ms_handler, | |
680 | &PUBKEY_handler, | |
681 | &PrivateKey_handler, | |
682 | }; | |
683 | ||
684 | ||
479af767 | 685 | /*- |
9c6da42d | 686 | * The loader itself |
479af767 | 687 | * ----------------- |
9c6da42d RL |
688 | */ |
689 | ||
690 | struct ossl_store_loader_ctx_st { | |
970f467a RL |
691 | enum { |
692 | is_raw = 0, | |
693 | is_pem, | |
694 | is_dir | |
695 | } type; | |
9c6da42d | 696 | int errcnt; |
7852f588 RL |
697 | #define FILE_FLAG_SECMEM (1<<0) |
698 | unsigned int flags; | |
970f467a RL |
699 | union { |
700 | struct { /* Used with is_raw and is_pem */ | |
701 | BIO *file; | |
702 | ||
703 | /* | |
704 | * The following are used when the handler is marked as | |
705 | * repeatable | |
706 | */ | |
707 | const FILE_HANDLER *last_handler; | |
708 | void *last_handler_ctx; | |
709 | } file; | |
710 | struct { /* Used with is_dir */ | |
711 | OPENSSL_DIR_CTX *ctx; | |
712 | int end_reached; | |
713 | char *uri; | |
714 | ||
715 | /* | |
716 | * The directory reading utility we have combines opening with | |
717 | * reading the first name. To make sure we can detect the end | |
718 | * at the right time, we read early and cache the name. | |
719 | */ | |
720 | const char *last_entry; | |
721 | int last_errno; | |
722 | } dir; | |
723 | } _; | |
9c6da42d RL |
724 | }; |
725 | ||
970f467a RL |
726 | static void OSSL_STORE_LOADER_CTX_free(OSSL_STORE_LOADER_CTX *ctx) |
727 | { | |
728 | if (ctx->type == is_dir) { | |
729 | OPENSSL_free(ctx->_.dir.uri); | |
730 | } else { | |
731 | if (ctx->_.file.last_handler != NULL) { | |
732 | ctx->_.file.last_handler->destroy_ctx(&ctx->_.file.last_handler_ctx); | |
733 | ctx->_.file.last_handler_ctx = NULL; | |
734 | ctx->_.file.last_handler = NULL; | |
735 | } | |
736 | } | |
737 | OPENSSL_free(ctx); | |
738 | } | |
739 | ||
9c6da42d RL |
740 | static OSSL_STORE_LOADER_CTX *file_open(const OSSL_STORE_LOADER *loader, |
741 | const char *uri, | |
742 | const UI_METHOD *ui_method, | |
743 | void *ui_data) | |
744 | { | |
9c6da42d | 745 | OSSL_STORE_LOADER_CTX *ctx = NULL; |
970f467a | 746 | struct stat st; |
6eaebfaa RL |
747 | struct { |
748 | const char *path; | |
749 | unsigned int check_absolute:1; | |
750 | } path_data[2]; | |
751 | size_t path_data_n = 0, i; | |
752 | const char *path; | |
9c6da42d | 753 | |
ae9c39d8 RL |
754 | /* |
755 | * First step, just take the URI as is. | |
756 | */ | |
6eaebfaa RL |
757 | path_data[path_data_n].check_absolute = 0; |
758 | path_data[path_data_n++].path = uri; | |
ae9c39d8 RL |
759 | |
760 | /* | |
761 | * Second step, if the URI appears to start with the 'file' scheme, | |
762 | * extract the path and make that the second path to check. | |
763 | * There's a special case if the URI also contains an authority, then | |
764 | * the full URI shouldn't be used as a path anywhere. | |
765 | */ | |
9c6da42d | 766 | if (strncasecmp(uri, "file:", 5) == 0) { |
ae9c39d8 RL |
767 | const char *p = &uri[5]; |
768 | ||
769 | if (strncmp(&uri[5], "//", 2) == 0) { | |
6eaebfaa | 770 | path_data_n--; /* Invalidate using the full URI */ |
ae9c39d8 RL |
771 | if (strncasecmp(&uri[7], "localhost/", 10) == 0) { |
772 | p = &uri[16]; | |
773 | } else if (uri[7] == '/') { | |
774 | p = &uri[7]; | |
775 | } else { | |
776 | OSSL_STOREerr(OSSL_STORE_F_FILE_OPEN, | |
777 | OSSL_STORE_R_URI_AUTHORITY_UNSUPPORTED); | |
778 | return NULL; | |
779 | } | |
9c6da42d RL |
780 | } |
781 | ||
6eaebfaa RL |
782 | path_data[path_data_n].check_absolute = 1; |
783 | #ifdef _WIN32 | |
784 | /* Windows file: URIs with a drive letter start with a / */ | |
785 | if (p[0] == '/' && p[2] == ':' && p[3] == '/') { | |
786 | p++; | |
787 | /* We know it's absolute, so no need to check */ | |
788 | path_data[path_data_n].check_absolute = 0; | |
789 | } | |
790 | #endif | |
791 | path_data[path_data_n++].path = p; | |
792 | } | |
793 | ||
794 | ||
795 | for (i = 0, path = NULL; path == NULL && i < path_data_n; i++) { | |
9c6da42d RL |
796 | /* |
797 | * If the scheme "file" was an explicit part of the URI, the path must | |
798 | * be absolute. So says RFC 8089 | |
799 | */ | |
6eaebfaa | 800 | if (path_data[i].check_absolute && path_data[i].path[0] != '/') { |
9c6da42d RL |
801 | OSSL_STOREerr(OSSL_STORE_F_FILE_OPEN, |
802 | OSSL_STORE_R_PATH_MUST_BE_ABSOLUTE); | |
6eaebfaa | 803 | ERR_add_error_data(1, path_data[i].path); |
9c6da42d RL |
804 | return NULL; |
805 | } | |
806 | ||
6eaebfaa | 807 | if (stat(path_data[i].path, &st) < 0) { |
ae9c39d8 | 808 | SYSerr(SYS_F_STAT, errno); |
6eaebfaa | 809 | ERR_add_error_data(1, path_data[i].path); |
ae9c39d8 | 810 | } else { |
6eaebfaa | 811 | path = path_data[i].path; |
ae9c39d8 RL |
812 | } |
813 | } | |
814 | if (path == NULL) { | |
970f467a RL |
815 | return NULL; |
816 | } | |
817 | ||
ae9c39d8 RL |
818 | /* Successfully found a working path, clear possible collected errors */ |
819 | ERR_clear_error(); | |
820 | ||
9c6da42d RL |
821 | ctx = OPENSSL_zalloc(sizeof(*ctx)); |
822 | if (ctx == NULL) { | |
823 | OSSL_STOREerr(OSSL_STORE_F_FILE_OPEN, ERR_R_MALLOC_FAILURE); | |
824 | return NULL; | |
825 | } | |
826 | ||
970f467a RL |
827 | if ((st.st_mode & S_IFDIR) == S_IFDIR) { |
828 | /* | |
829 | * Try to copy everything, even if we know that some of them must be | |
830 | * NULL for the moment. This prevents errors in the future, when more | |
831 | * components may be used. | |
832 | */ | |
833 | ctx->_.dir.uri = OPENSSL_strdup(uri); | |
834 | ctx->type = is_dir; | |
835 | ||
836 | if (ctx->_.dir.uri == NULL) | |
837 | goto err; | |
838 | ||
839 | ctx->_.dir.last_entry = OPENSSL_DIR_read(&ctx->_.dir.ctx, path); | |
840 | ctx->_.dir.last_errno = errno; | |
841 | if (ctx->_.dir.last_entry == NULL) { | |
842 | if (ctx->_.dir.last_errno != 0) { | |
843 | char errbuf[256]; | |
844 | errno = ctx->_.dir.last_errno; | |
845 | openssl_strerror_r(errno, errbuf, sizeof(errbuf)); | |
846 | OSSL_STOREerr(OSSL_STORE_F_FILE_OPEN, ERR_R_SYS_LIB); | |
847 | ERR_add_error_data(1, errbuf); | |
848 | goto err; | |
849 | } | |
850 | ctx->_.dir.end_reached = 1; | |
851 | } | |
852 | } else { | |
853 | BIO *buff = NULL; | |
854 | char peekbuf[4096]; | |
855 | ||
856 | if ((buff = BIO_new(BIO_f_buffer())) == NULL | |
857 | || (ctx->_.file.file = BIO_new_file(path, "rb")) == NULL) { | |
858 | BIO_free_all(buff); | |
859 | goto err; | |
860 | } | |
861 | ||
862 | ctx->_.file.file = BIO_push(buff, ctx->_.file.file); | |
863 | if (BIO_buffer_peek(ctx->_.file.file, peekbuf, sizeof(peekbuf)-1) > 0) { | |
864 | peekbuf[sizeof(peekbuf)-1] = '\0'; | |
865 | if (strstr(peekbuf, "-----BEGIN ") != NULL) | |
866 | ctx->type = is_pem; | |
867 | } | |
9c6da42d RL |
868 | } |
869 | ||
870 | return ctx; | |
871 | err: | |
970f467a | 872 | OSSL_STORE_LOADER_CTX_free(ctx); |
9c6da42d RL |
873 | return NULL; |
874 | } | |
875 | ||
7852f588 RL |
876 | static int file_ctrl(OSSL_STORE_LOADER_CTX *ctx, int cmd, va_list args) |
877 | { | |
878 | int ret = 1; | |
879 | ||
880 | switch (cmd) { | |
881 | case OSSL_STORE_C_USE_SECMEM: | |
882 | { | |
883 | int on = *(va_arg(args, int *)); | |
884 | ||
885 | switch (on) { | |
886 | case 0: | |
887 | ctx->flags &= ~FILE_FLAG_SECMEM; | |
888 | break; | |
889 | case 1: | |
890 | ctx->flags |= FILE_FLAG_SECMEM; | |
891 | break; | |
892 | default: | |
893 | OSSL_STOREerr(OSSL_STORE_F_FILE_CTRL, | |
894 | ERR_R_PASSED_INVALID_ARGUMENT); | |
895 | ret = 0; | |
896 | break; | |
897 | } | |
898 | } | |
899 | break; | |
900 | default: | |
901 | break; | |
902 | } | |
903 | ||
904 | return ret; | |
905 | } | |
906 | ||
4c17819c RL |
907 | /* Internal function to decode an already opened PEM file */ |
908 | OSSL_STORE_LOADER_CTX *ossl_store_file_attach_pem_bio_int(BIO *bp) | |
909 | { | |
910 | OSSL_STORE_LOADER_CTX *ctx = OPENSSL_zalloc(sizeof(*ctx)); | |
911 | ||
912 | if (ctx == NULL) { | |
913 | OSSL_STOREerr(OSSL_STORE_F_OSSL_STORE_FILE_ATTACH_PEM_BIO_INT, | |
914 | ERR_R_MALLOC_FAILURE); | |
915 | return NULL; | |
916 | } | |
917 | ||
918 | ctx->_.file.file = bp; | |
919 | ctx->type = is_pem; | |
920 | ||
921 | return ctx; | |
922 | } | |
923 | ||
1aabc244 RL |
924 | static OSSL_STORE_INFO *file_load_try_decode(OSSL_STORE_LOADER_CTX *ctx, |
925 | const char *pem_name, | |
926 | const char *pem_header, | |
927 | unsigned char *data, size_t len, | |
928 | const UI_METHOD *ui_method, | |
929 | void *ui_data, int *matchcount) | |
930 | { | |
931 | OSSL_STORE_INFO *result = NULL; | |
932 | BUF_MEM *new_mem = NULL; | |
933 | char *new_pem_name = NULL; | |
934 | int t = 0; | |
935 | ||
936 | again: | |
937 | { | |
938 | size_t i = 0; | |
939 | void *handler_ctx = NULL; | |
940 | const FILE_HANDLER **matching_handlers = | |
941 | OPENSSL_zalloc(sizeof(*matching_handlers) | |
942 | * OSSL_NELEM(file_handlers)); | |
943 | ||
944 | if (matching_handlers == NULL) { | |
945 | OSSL_STOREerr(OSSL_STORE_F_FILE_LOAD_TRY_DECODE, | |
946 | ERR_R_MALLOC_FAILURE); | |
947 | goto err; | |
948 | } | |
949 | ||
950 | *matchcount = 0; | |
951 | for (i = 0; i < OSSL_NELEM(file_handlers); i++) { | |
952 | const FILE_HANDLER *handler = file_handlers[i]; | |
6fc1d33c | 953 | int try_matchcount = 0; |
1aabc244 RL |
954 | void *tmp_handler_ctx = NULL; |
955 | OSSL_STORE_INFO *tmp_result = | |
956 | handler->try_decode(pem_name, pem_header, data, len, | |
6fc1d33c RL |
957 | &tmp_handler_ctx, &try_matchcount, |
958 | ui_method, ui_data); | |
1aabc244 | 959 | |
6fc1d33c | 960 | if (try_matchcount > 0) { |
1aabc244 RL |
961 | if (matching_handlers) |
962 | matching_handlers[*matchcount] = handler; | |
963 | ||
964 | if (handler_ctx) | |
965 | handler->destroy_ctx(&handler_ctx); | |
966 | handler_ctx = tmp_handler_ctx; | |
967 | ||
6fc1d33c | 968 | if ((*matchcount += try_matchcount) > 1) { |
1aabc244 RL |
969 | /* more than one match => ambiguous, kill any result */ |
970 | OSSL_STORE_INFO_free(result); | |
971 | OSSL_STORE_INFO_free(tmp_result); | |
972 | if (handler->destroy_ctx != NULL) | |
973 | handler->destroy_ctx(&handler_ctx); | |
974 | handler_ctx = NULL; | |
6fc1d33c | 975 | tmp_result = NULL; |
1aabc244 RL |
976 | result = NULL; |
977 | } | |
6fc1d33c RL |
978 | if (result == NULL) |
979 | result = tmp_result; | |
1aabc244 RL |
980 | } |
981 | } | |
982 | ||
6fc1d33c RL |
983 | if (*matchcount == 1 && matching_handlers[0]->repeatable) { |
984 | ctx->_.file.last_handler = matching_handlers[0]; | |
985 | ctx->_.file.last_handler_ctx = handler_ctx; | |
1aabc244 RL |
986 | } |
987 | ||
988 | OPENSSL_free(matching_handlers); | |
989 | } | |
990 | ||
991 | err: | |
992 | OPENSSL_free(new_pem_name); | |
993 | BUF_MEM_free(new_mem); | |
994 | ||
995 | if (result != NULL | |
996 | && (t = OSSL_STORE_INFO_get_type(result)) == OSSL_STORE_INFO_EMBEDDED) { | |
997 | pem_name = new_pem_name = | |
998 | ossl_store_info_get0_EMBEDDED_pem_name(result); | |
999 | new_mem = ossl_store_info_get0_EMBEDDED_buffer(result); | |
1000 | data = (unsigned char *)new_mem->data; | |
1001 | len = new_mem->length; | |
1002 | OPENSSL_free(result); | |
1003 | result = NULL; | |
1004 | goto again; | |
1005 | } | |
1006 | ||
1007 | if (result != NULL) | |
1008 | ERR_clear_error(); | |
1009 | ||
1010 | return result; | |
1011 | } | |
1012 | ||
1013 | static OSSL_STORE_INFO *file_load_try_repeat(OSSL_STORE_LOADER_CTX *ctx, | |
1014 | const UI_METHOD *ui_method, | |
1015 | void *ui_data) | |
9c6da42d RL |
1016 | { |
1017 | OSSL_STORE_INFO *result = NULL; | |
6fc1d33c | 1018 | int try_matchcount = 0; |
9c6da42d | 1019 | |
970f467a RL |
1020 | if (ctx->_.file.last_handler != NULL) { |
1021 | result = | |
1022 | ctx->_.file.last_handler->try_decode(NULL, NULL, NULL, 0, | |
1023 | &ctx->_.file.last_handler_ctx, | |
6fc1d33c | 1024 | &try_matchcount, |
970f467a | 1025 | ui_method, ui_data); |
e61ec2d9 | 1026 | |
1aabc244 | 1027 | if (result == NULL) { |
970f467a RL |
1028 | ctx->_.file.last_handler->destroy_ctx(&ctx->_.file.last_handler_ctx); |
1029 | ctx->_.file.last_handler_ctx = NULL; | |
1030 | ctx->_.file.last_handler = NULL; | |
1aabc244 RL |
1031 | } |
1032 | } | |
1033 | return result; | |
1034 | } | |
e61ec2d9 | 1035 | |
7852f588 RL |
1036 | static void pem_free_flag(void *pem_data, int secure) |
1037 | { | |
1038 | if (secure) | |
1039 | OPENSSL_secure_free(pem_data); | |
1040 | else | |
1041 | OPENSSL_free(pem_data); | |
1042 | } | |
1aabc244 RL |
1043 | static int file_read_pem(BIO *bp, char **pem_name, char **pem_header, |
1044 | unsigned char **data, long *len, | |
1045 | const UI_METHOD *ui_method, | |
7852f588 | 1046 | void *ui_data, int secure) |
1aabc244 | 1047 | { |
7852f588 RL |
1048 | int i = secure |
1049 | ? PEM_read_bio_ex(bp, pem_name, pem_header, data, len, | |
1050 | PEM_FLAG_SECURE | PEM_FLAG_EAY_COMPATIBLE) | |
1051 | : PEM_read_bio(bp, pem_name, pem_header, data, len); | |
1aabc244 RL |
1052 | |
1053 | if (i <= 0) | |
1054 | return 0; | |
1055 | ||
1056 | /* | |
1057 | * 10 is the number of characters in "Proc-Type:", which | |
1058 | * PEM_get_EVP_CIPHER_INFO() requires to be present. | |
1059 | * If the PEM header has less characters than that, it's | |
1060 | * not worth spending cycles on it. | |
1061 | */ | |
1062 | if (strlen(*pem_header) > 10) { | |
1063 | EVP_CIPHER_INFO cipher; | |
1064 | struct pem_pass_data pass_data; | |
1065 | ||
1066 | if (!PEM_get_EVP_CIPHER_INFO(*pem_header, &cipher) | |
1067 | || !file_fill_pem_pass_data(&pass_data, "PEM", ui_method, ui_data) | |
1068 | || !PEM_do_header(&cipher, *data, len, file_get_pem_pass, | |
1069 | &pass_data)) { | |
1070 | return 0; | |
1071 | } | |
e61ec2d9 | 1072 | } |
1aabc244 RL |
1073 | return 1; |
1074 | } | |
1075 | ||
1076 | static int file_read_asn1(BIO *bp, unsigned char **data, long *len) | |
1077 | { | |
1078 | BUF_MEM *mem = NULL; | |
1079 | ||
1080 | if (asn1_d2i_read_bio(bp, &mem) < 0) | |
1081 | return 0; | |
1082 | ||
1083 | *data = (unsigned char *)mem->data; | |
1084 | *len = (long)mem->length; | |
1085 | OPENSSL_free(mem); | |
1086 | ||
1087 | return 1; | |
1088 | } | |
1089 | ||
970f467a RL |
1090 | static int ends_with_dirsep(const char *uri) |
1091 | { | |
1092 | if (*uri != '\0') | |
1093 | uri += strlen(uri) - 1; | |
1094 | #if defined __VMS | |
1095 | if (*uri == ']' || *uri == '>' || *uri == ':') | |
1096 | return 1; | |
1097 | #elif defined _WIN32 | |
1098 | if (*uri == '\\') | |
1099 | return 1; | |
1100 | #endif | |
1101 | return *uri == '/'; | |
1102 | } | |
1103 | ||
1104 | static int file_name_to_uri(OSSL_STORE_LOADER_CTX *ctx, const char *name, | |
1105 | char **data) | |
1106 | { | |
1107 | assert(name != NULL); | |
1108 | assert(data != NULL); | |
1109 | { | |
1110 | const char *pathsep = ends_with_dirsep(ctx->_.dir.uri) ? "" : "/"; | |
1111 | long calculated_length = strlen(ctx->_.dir.uri) + strlen(pathsep) | |
1112 | + strlen(name) + 1 /* \0 */; | |
1113 | ||
1114 | *data = OPENSSL_zalloc(calculated_length); | |
1115 | if (*data == NULL) { | |
1116 | OSSL_STOREerr(OSSL_STORE_F_FILE_NAME_TO_URI, ERR_R_MALLOC_FAILURE); | |
1117 | return 0; | |
1118 | } | |
1119 | ||
1120 | OPENSSL_strlcat(*data, ctx->_.dir.uri, calculated_length); | |
1121 | OPENSSL_strlcat(*data, pathsep, calculated_length); | |
1122 | OPENSSL_strlcat(*data, name, calculated_length); | |
1123 | } | |
1124 | return 1; | |
1125 | } | |
1126 | ||
1aabc244 RL |
1127 | static int file_eof(OSSL_STORE_LOADER_CTX *ctx); |
1128 | static int file_error(OSSL_STORE_LOADER_CTX *ctx); | |
1129 | static OSSL_STORE_INFO *file_load(OSSL_STORE_LOADER_CTX *ctx, | |
1130 | const UI_METHOD *ui_method, void *ui_data) | |
1131 | { | |
1132 | OSSL_STORE_INFO *result = NULL; | |
1aabc244 | 1133 | |
6fc1d33c RL |
1134 | ctx->errcnt = 0; |
1135 | ERR_clear_error(); | |
1136 | ||
970f467a RL |
1137 | if (ctx->type == is_dir) { |
1138 | do { | |
1139 | char *newname = NULL; | |
e61ec2d9 | 1140 | |
970f467a RL |
1141 | if (ctx->_.dir.last_entry == NULL) { |
1142 | if (!ctx->_.dir.end_reached) { | |
1143 | char errbuf[256]; | |
1144 | assert(ctx->_.dir.last_errno != 0); | |
1145 | errno = ctx->_.dir.last_errno; | |
9c6da42d | 1146 | ctx->errcnt++; |
970f467a RL |
1147 | openssl_strerror_r(errno, errbuf, sizeof(errbuf)); |
1148 | OSSL_STOREerr(OSSL_STORE_F_FILE_LOAD, ERR_R_SYS_LIB); | |
1149 | ERR_add_error_data(1, errbuf); | |
1150 | } | |
1151 | return NULL; | |
9c6da42d | 1152 | } |
970f467a RL |
1153 | |
1154 | if (ctx->_.dir.last_entry[0] != '.' | |
1155 | && !file_name_to_uri(ctx, ctx->_.dir.last_entry, &newname)) | |
1156 | return NULL; | |
1157 | ||
1158 | /* | |
1159 | * On the first call (with a NULL context), OPENSSL_DIR_read() | |
1160 | * cares about the second argument. On the following calls, it | |
1161 | * only cares that it isn't NULL. Therefore, we can safely give | |
1162 | * it our URI here. | |
1163 | */ | |
1164 | ctx->_.dir.last_entry = OPENSSL_DIR_read(&ctx->_.dir.ctx, | |
1165 | ctx->_.dir.uri); | |
1166 | ctx->_.dir.last_errno = errno; | |
1167 | if (ctx->_.dir.last_entry == NULL && ctx->_.dir.last_errno == 0) | |
1168 | ctx->_.dir.end_reached = 1; | |
1169 | ||
1170 | if (newname != NULL | |
1171 | && (result = OSSL_STORE_INFO_new_NAME(newname)) == NULL) { | |
1172 | OPENSSL_free(newname); | |
1173 | OSSL_STOREerr(OSSL_STORE_F_FILE_LOAD, ERR_R_OSSL_STORE_LIB); | |
1174 | return NULL; | |
9c6da42d | 1175 | } |
970f467a RL |
1176 | } while (result == NULL && !file_eof(ctx)); |
1177 | } else { | |
1178 | int matchcount = -1; | |
9c6da42d | 1179 | |
970f467a RL |
1180 | result = file_load_try_repeat(ctx, ui_method, ui_data); |
1181 | if (result != NULL) | |
1182 | return result; | |
9c6da42d | 1183 | |
6fc1d33c | 1184 | if (file_eof(ctx)) |
970f467a | 1185 | return NULL; |
9c6da42d | 1186 | |
970f467a RL |
1187 | do { |
1188 | char *pem_name = NULL; /* PEM record name */ | |
1189 | char *pem_header = NULL; /* PEM record header */ | |
1190 | unsigned char *data = NULL; /* DER encoded data */ | |
1191 | long len = 0; /* DER encoded data length */ | |
1192 | ||
1193 | matchcount = -1; | |
1194 | if (ctx->type == is_pem) { | |
1195 | if (!file_read_pem(ctx->_.file.file, &pem_name, &pem_header, | |
7852f588 RL |
1196 | &data, &len, ui_method, ui_data, |
1197 | (ctx->flags & FILE_FLAG_SECMEM) != 0)) { | |
6fc1d33c RL |
1198 | ctx->errcnt++; |
1199 | goto endloop; | |
970f467a RL |
1200 | } |
1201 | } else { | |
1202 | if (!file_read_asn1(ctx->_.file.file, &data, &len)) { | |
6fc1d33c RL |
1203 | ctx->errcnt++; |
1204 | goto endloop; | |
970f467a RL |
1205 | } |
1206 | } | |
1207 | ||
1208 | result = file_load_try_decode(ctx, pem_name, pem_header, data, len, | |
1209 | ui_method, ui_data, &matchcount); | |
1210 | ||
6fc1d33c RL |
1211 | if (result != NULL) |
1212 | goto endloop; | |
1213 | ||
1214 | /* | |
1215 | * If a PEM name matches more than one handler, the handlers are | |
1216 | * badly coded. | |
1217 | */ | |
1218 | if (!ossl_assert(pem_name == NULL || matchcount <= 1)) { | |
1219 | ctx->errcnt++; | |
1220 | goto endloop; | |
1221 | } | |
1222 | ||
1223 | if (matchcount > 1) { | |
1224 | OSSL_STOREerr(OSSL_STORE_F_FILE_LOAD, | |
1225 | OSSL_STORE_R_AMBIGUOUS_CONTENT_TYPE); | |
1226 | } else if (matchcount == 1) { | |
1227 | /* | |
1228 | * If there are other errors on the stack, they already show | |
1229 | * what the problem is. | |
1230 | */ | |
1231 | if (ERR_peek_error() == 0) { | |
1232 | OSSL_STOREerr(OSSL_STORE_F_FILE_LOAD, | |
1233 | OSSL_STORE_R_UNSUPPORTED_CONTENT_TYPE); | |
1234 | if (pem_name != NULL) | |
1235 | ERR_add_error_data(3, "PEM type is '", pem_name, "'"); | |
1236 | } | |
1237 | } | |
1238 | if (matchcount > 0) | |
1239 | ctx->errcnt++; | |
1240 | ||
1241 | endloop: | |
7852f588 RL |
1242 | pem_free_flag(pem_name, (ctx->flags & FILE_FLAG_SECMEM) != 0); |
1243 | pem_free_flag(pem_header, (ctx->flags & FILE_FLAG_SECMEM) != 0); | |
1244 | pem_free_flag(data, (ctx->flags & FILE_FLAG_SECMEM) != 0); | |
970f467a RL |
1245 | } while (matchcount == 0 && !file_eof(ctx) && !file_error(ctx)); |
1246 | ||
1247 | /* We bail out on ambiguity */ | |
1248 | if (matchcount > 1) | |
1249 | return NULL; | |
1250 | } | |
9c6da42d | 1251 | |
9c6da42d RL |
1252 | return result; |
1253 | } | |
1254 | ||
1255 | static int file_error(OSSL_STORE_LOADER_CTX *ctx) | |
1256 | { | |
1257 | return ctx->errcnt > 0; | |
1258 | } | |
1259 | ||
1260 | static int file_eof(OSSL_STORE_LOADER_CTX *ctx) | |
1261 | { | |
970f467a RL |
1262 | if (ctx->type == is_dir) |
1263 | return ctx->_.dir.end_reached; | |
1264 | ||
1265 | if (ctx->_.file.last_handler != NULL | |
1266 | && !ctx->_.file.last_handler->eof(ctx->_.file.last_handler_ctx)) | |
e61ec2d9 | 1267 | return 0; |
970f467a | 1268 | return BIO_eof(ctx->_.file.file); |
9c6da42d RL |
1269 | } |
1270 | ||
1271 | static int file_close(OSSL_STORE_LOADER_CTX *ctx) | |
1272 | { | |
970f467a RL |
1273 | if (ctx->type == is_dir) { |
1274 | OPENSSL_DIR_end(&ctx->_.dir.ctx); | |
1275 | } else { | |
1276 | BIO_free_all(ctx->_.file.file); | |
e61ec2d9 | 1277 | } |
970f467a | 1278 | OSSL_STORE_LOADER_CTX_free(ctx); |
9c6da42d RL |
1279 | return 1; |
1280 | } | |
1281 | ||
4c17819c RL |
1282 | int ossl_store_file_detach_pem_bio_int(OSSL_STORE_LOADER_CTX *ctx) |
1283 | { | |
1284 | OSSL_STORE_LOADER_CTX_free(ctx); | |
1285 | return 1; | |
1286 | } | |
1287 | ||
9c6da42d RL |
1288 | static OSSL_STORE_LOADER file_loader = |
1289 | { | |
1290 | "file", | |
f91ded1f | 1291 | NULL, |
9c6da42d | 1292 | file_open, |
7852f588 | 1293 | file_ctrl, |
9c6da42d RL |
1294 | file_load, |
1295 | file_eof, | |
1296 | file_error, | |
1297 | file_close | |
1298 | }; | |
1299 | ||
1300 | static void store_file_loader_deinit(void) | |
1301 | { | |
1302 | ossl_store_unregister_loader_int(file_loader.scheme); | |
1303 | } | |
1304 | ||
1305 | int ossl_store_file_loader_init(void) | |
1306 | { | |
1307 | int ret = ossl_store_register_loader_int(&file_loader); | |
1308 | ||
1309 | OPENSSL_atexit(store_file_loader_deinit); | |
1310 | return ret; | |
1311 | } |