Debian Codegraph @

debian/rust-vendor/openssl-src/openssl/engines/e_loader_attic.c

raw
1 /*
2 * Copyright 2016-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 /* THIS ENGINE IS FOR TESTING PURPOSES ONLY. */
11
12 /* This file has quite some overlap with providers/implementations/storemgmt/file_store.c */
13
14 /*
15 * We need to use some asn1_meth deprecated APIs
16 */
17 #define OPENSSL_SUPPRESS_DEPRECATED
18
19 #include "internal/e_os.h" /* for stat */
20 #include <string.h>
21 #include <sys/stat.h>
22 #include <ctype.h>
23 #include <assert.h>
24
25 #include <openssl/bio.h>
26 #include <openssl/dsa.h> /* For d2i_DSAPrivateKey */
27 #include <openssl/err.h>
28 #include <openssl/evp.h>
29 #include <openssl/pem.h>
30 #include <openssl/pkcs12.h> /* For the PKCS8 stuff o.O */
31 #include <openssl/rsa.h> /* For d2i_RSAPrivateKey */
32 #include <openssl/safestack.h>
33 #include <openssl/store.h>
34 #include <openssl/ui.h>
35 #include <openssl/engine.h>
36 #include <openssl/x509.h> /* For the PKCS8 stuff o.O */
37 #include "internal/asn1.h" /* For asn1_d2i_read_bio */
38 #include "internal/o_dir.h"
39 #include "internal/cryptlib.h"
40 #include "crypto/ctype.h" /* For ossl_isdigit */
41 #include "crypto/pem.h" /* For PVK and "blob" PEM headers */
42
43 /* clang-format off */
44 #include "e_loader_attic_err.c"
45 /* clang-format on */
46
47 DEFINE_STACK_OF(OSSL_STORE_INFO)
48
49 #ifndef S_ISDIR
50 #define S_ISDIR(a) (((a) & S_IFMT) == S_IFDIR)
51 #endif
52
53 /*-
54 * Password prompting
55 * ------------------
56 */
57
58 static char *file_get_pass(const UI_METHOD *ui_method, char *pass,
59 size_t maxsize, const char *desc, const char *info,
60 void *data)
61 {
62 UI *ui = UI_new();
63 char *prompt = NULL;
64
65 if (ui == NULL) {
66 ATTICerr(0, ERR_R_UI_LIB);
67 return NULL;
68 }
69
70 if (ui_method != NULL)
71 UI_set_method(ui, ui_method);
72 UI_add_user_data(ui, data);
73
74 if ((prompt = UI_construct_prompt(ui, desc, info)) == NULL) {
75 ATTICerr(0, ERR_R_UI_LIB);
76 pass = NULL;
77 } else if (UI_add_input_string(ui, prompt, UI_INPUT_FLAG_DEFAULT_PWD,
78 pass, 0, (int)(maxsize - 1))
79 <= 0) {
80 ATTICerr(0, ERR_R_UI_LIB);
81 pass = NULL;
82 } else {
83 switch (UI_process(ui)) {
84 case -2:
85 ATTICerr(0, ATTIC_R_UI_PROCESS_INTERRUPTED_OR_CANCELLED);
86 pass = NULL;
87 break;
88 case -1:
89 ATTICerr(0, ERR_R_UI_LIB);
90 pass = NULL;
91 break;
92 default:
93 break;
94 }
95 }
96
97 OPENSSL_free(prompt);
98 UI_free(ui);
99 return pass;
100 }
101
102 struct pem_pass_data {
103 const UI_METHOD *ui_method;
104 void *data;
105 const char *prompt_desc;
106 const char *prompt_info;
107 };
108
109 static int file_fill_pem_pass_data(struct pem_pass_data *pass_data,
110 const char *desc, const char *info,
111 const UI_METHOD *ui_method, void *ui_data)
112 {
113 if (pass_data == NULL)
114 return 0;
115 pass_data->ui_method = ui_method;
116 pass_data->data = ui_data;
117 pass_data->prompt_desc = desc;
118 pass_data->prompt_info = info;
119 return 1;
120 }
121
122 /* This is used anywhere a pem_password_cb is needed */
123 static int file_get_pem_pass(char *buf, int num, int w, void *data)
124 {
125 struct pem_pass_data *pass_data = data;
126 char *pass = file_get_pass(pass_data->ui_method, buf, num,
127 pass_data->prompt_desc, pass_data->prompt_info,
128 pass_data->data);
129
130 return pass == NULL ? 0 : (int)strlen(pass);
131 }
132
133 /*
134 * Check if |str| ends with |suffix| preceded by a space, and if it does,
135 * return the index of that space. If there is no such suffix in |str|,
136 * return -1.
137 * For |str| == "FOO BAR" and |suffix| == "BAR", the returned value is 3.
138 */
139 static int check_suffix(const char *str, const char *suffix)
140 {
141 int str_len = (int)strlen(str);
142 int suffix_len = (int)(strlen(suffix) + 1);
143 const char *p = NULL;
144
145 if (suffix_len >= str_len)
146 return -1;
147 p = str + str_len - suffix_len;
148 if (*p != ' '
149 || strcmp(p + 1, suffix) != 0)
150 return -1;
151 return (int)(p - str);
152 }
153
154 /*
155 * EMBEDDED is a special type of OSSL_STORE_INFO, specially for the file
156 * handlers, so we define it internally. This uses the possibility to
157 * create an OSSL_STORE_INFO with a generic data pointer and arbitrary
158 * type number.
159 *
160 * This is used by a FILE_HANDLER's try_decode function to signal that it
161 * has decoded the incoming blob into a new blob, and that the attempted
162 * decoding should be immediately restarted with the new blob, using the
163 * new PEM name.
164 */
165 /* Negative numbers are never used for public OSSL_STORE_INFO types */
166 #define STORE_INFO_EMBEDDED -1
167
168 /* This is the embedded data */
169 struct embedded_st {
170 BUF_MEM *blob;
171 char *pem_name;
172 };
173
174 /* Helper functions */
175 static struct embedded_st *get0_EMBEDDED(OSSL_STORE_INFO *info)
176 {
177 return OSSL_STORE_INFO_get0_data(STORE_INFO_EMBEDDED, info);
178 }
179
180 static void store_info_free(OSSL_STORE_INFO *info)
181 {
182 struct embedded_st *data;
183
184 if (info != NULL && (data = get0_EMBEDDED(info)) != NULL) {
185 BUF_MEM_free(data->blob);
186 OPENSSL_free(data->pem_name);
187 OPENSSL_free(data);
188 }
189 OSSL_STORE_INFO_free(info);
190 }
191
192 static OSSL_STORE_INFO *new_EMBEDDED(const char *new_pem_name,
193 BUF_MEM *embedded)
194 {
195 OSSL_STORE_INFO *info = NULL;
196 struct embedded_st *data = NULL;
197
198 if ((data = OPENSSL_zalloc(sizeof(*data))) == NULL)
199 return NULL;
200 if ((info = OSSL_STORE_INFO_new(STORE_INFO_EMBEDDED, data)) == NULL) {
201 ATTICerr(0, ERR_R_OSSL_STORE_LIB);
202 OPENSSL_free(data);
203 return NULL;
204 }
205
206 data->blob = embedded;
207 data->pem_name = new_pem_name == NULL ? NULL : OPENSSL_strdup(new_pem_name);
208
209 if (new_pem_name != NULL && data->pem_name == NULL) {
210 store_info_free(info);
211 info = NULL;
212 }
213
214 return info;
215 }
216
217 /*-
218 * The file scheme decoders
219 * ------------------------
220 *
221 * Each possible data type has its own decoder, which either operates
222 * through a given PEM name, or attempts to decode to see if the blob
223 * it's given is decodable for its data type. The assumption is that
224 * only the correct data type will match the content.
225 */
226
227 /*-
228 * The try_decode function is called to check if the blob of data can
229 * be used by this handler, and if it can, decodes it into a supported
230 * OpenSSL type and returns an OSSL_STORE_INFO with the decoded data.
231 * Input:
232 * pem_name: If this blob comes from a PEM file, this holds
233 * the PEM name. If it comes from another type of
234 * file, this is NULL.
235 * pem_header: If this blob comes from a PEM file, this holds
236 * the PEM headers. If it comes from another type of
237 * file, this is NULL.
238 * blob: The blob of data to match with what this handler
239 * can use.
240 * len: The length of the blob.
241 * handler_ctx: For a handler marked repeatable, this pointer can
242 * be used to create a context for the handler. IT IS
243 * THE HANDLER'S RESPONSIBILITY TO CREATE AND DESTROY
244 * THIS CONTEXT APPROPRIATELY, i.e. create on first call
245 * and destroy when about to return NULL.
246 * matchcount: A pointer to an int to count matches for this data.
247 * Usually becomes 0 (no match) or 1 (match!), but may
248 * be higher in the (unlikely) event that the data matches
249 * more than one possibility. The int will always be
250 * zero when the function is called.
251 * ui_method: Application UI method for getting a password, pin
252 * or any other interactive data.
253 * ui_data: Application data to be passed to ui_method when
254 * it's called.
255 * libctx: The library context to be used if applicable
256 * propq: The property query string for any algorithm fetches
257 * Output:
258 * an OSSL_STORE_INFO
259 */
260 typedef OSSL_STORE_INFO *(*file_try_decode_fn)(const char *pem_name,
261 const char *pem_header,
262 const unsigned char *blob,
263 size_t len, void **handler_ctx,
264 int *matchcount,
265 const UI_METHOD *ui_method,
266 void *ui_data, const char *uri,
267 OSSL_LIB_CTX *libctx,
268 const char *propq);
269 /*
270 * The eof function should return 1 if there's no more data to be found
271 * with the handler_ctx, otherwise 0. This is only used when the handler is
272 * marked repeatable.
273 */
274 typedef int (*file_eof_fn)(void *handler_ctx);
275 /*
276 * The destroy_ctx function is used to destroy the handler_ctx that was
277 * initiated by a repeatable try_decode function. This is only used when
278 * the handler is marked repeatable.
279 */
280 typedef void (*file_destroy_ctx_fn)(void **handler_ctx);
281
282 typedef struct file_handler_st {
283 const char *name;
284 file_try_decode_fn try_decode;
285 file_eof_fn eof;
286 file_destroy_ctx_fn destroy_ctx;
287
288 /* flags */
289 int repeatable;
290 } FILE_HANDLER;
291
292 /*
293 * PKCS#12 decoder. It operates by decoding all of the blob content,
294 * extracting all the interesting data from it and storing them internally,
295 * then serving them one piece at a time.
296 */
297 static OSSL_STORE_INFO *try_decode_PKCS12(const char *pem_name,
298 const char *pem_header,
299 const unsigned char *blob,
300 size_t len, void **pctx,
301 int *matchcount,
302 const UI_METHOD *ui_method,
303 void *ui_data, const char *uri,
304 OSSL_LIB_CTX *libctx,
305 const char *propq)
306 {
307 OSSL_STORE_INFO *store_info = NULL;
308 STACK_OF(OSSL_STORE_INFO) *ctx = *pctx;
309
310 if (ctx == NULL) {
311 /* Initial parsing */
312 PKCS12 *p12;
313
314 if (pem_name != NULL || len > LONG_MAX)
315 /* No match, there is no PEM PKCS12 tag */
316 return NULL;
317
318 if ((p12 = d2i_PKCS12(NULL, &blob, (long)len)) != NULL) {
319 char *pass = NULL;
320 char tpass[PEM_BUFSIZE];
321 EVP_PKEY *pkey = NULL;
322 X509 *cert = NULL;
323 STACK_OF(X509) *chain = NULL;
324
325 *matchcount = 1;
326
327 if (!PKCS12_mac_present(p12)
328 || PKCS12_verify_mac(p12, "", 0)
329 || PKCS12_verify_mac(p12, NULL, 0)) {
330 pass = "";
331 } else {
332 if ((pass = file_get_pass(ui_method, tpass, PEM_BUFSIZE,
333 "PKCS12 import", uri,
334 ui_data))
335 == NULL) {
336 ATTICerr(0, ATTIC_R_PASSPHRASE_CALLBACK_ERROR);
337 goto p12_end;
338 }
339 if (!PKCS12_verify_mac(p12, pass, (int)strlen(pass))) {
340 ATTICerr(0, ATTIC_R_ERROR_VERIFYING_PKCS12_MAC);
341 goto p12_end;
342 }
343 }
344
345 if (PKCS12_parse(p12, pass, &pkey, &cert, &chain)) {
346 OSSL_STORE_INFO *osi_pkey = NULL;
347 OSSL_STORE_INFO *osi_cert = NULL;
348 OSSL_STORE_INFO *osi_ca = NULL;
349 int ok = 1;
350
351 if ((ctx = sk_OSSL_STORE_INFO_new_null()) != NULL) {
352 if (pkey != NULL) {
353 if ((osi_pkey = OSSL_STORE_INFO_new_PKEY(pkey)) != NULL
354 /* clearing pkey here avoids case distinctions */
355 && (pkey = NULL) == NULL
356 && sk_OSSL_STORE_INFO_push(ctx, osi_pkey) != 0)
357 osi_pkey = NULL;
358 else
359 ok = 0;
360 }
361 if (ok && cert != NULL) {
362 if ((osi_cert = OSSL_STORE_INFO_new_CERT(cert)) != NULL
363 /* clearing cert here avoids case distinctions */
364 && (cert = NULL) == NULL
365 && sk_OSSL_STORE_INFO_push(ctx, osi_cert) != 0)
366 osi_cert = NULL;
367 else
368 ok = 0;
369 }
370 while (ok && sk_X509_num(chain) > 0) {
371 X509 *ca = sk_X509_value(chain, 0);
372
373 if ((osi_ca = OSSL_STORE_INFO_new_CERT(ca)) != NULL
374 && sk_X509_shift(chain) != NULL
375 && sk_OSSL_STORE_INFO_push(ctx, osi_ca) != 0)
376 osi_ca = NULL;
377 else
378 ok = 0;
379 }
380 }
381 EVP_PKEY_free(pkey);
382 X509_free(cert);
383 OSSL_STACK_OF_X509_free(chain);
384 store_info_free(osi_pkey);
385 store_info_free(osi_cert);
386 store_info_free(osi_ca);
387 if (!ok) {
388 sk_OSSL_STORE_INFO_pop_free(ctx, store_info_free);
389 ctx = NULL;
390 }
391 *pctx = ctx;
392 }
393 }
394 p12_end:
395 PKCS12_free(p12);
396 if (ctx == NULL)
397 return NULL;
398 }
399
400 *matchcount = 1;
401 store_info = sk_OSSL_STORE_INFO_shift(ctx);
402 return store_info;
403 }
404
405 static int eof_PKCS12(void *ctx_)
406 {
407 STACK_OF(OSSL_STORE_INFO) *ctx = ctx_;
408
409 return ctx == NULL || sk_OSSL_STORE_INFO_num(ctx) == 0;
410 }
411
412 static void destroy_ctx_PKCS12(void **pctx)
413 {
414 STACK_OF(OSSL_STORE_INFO) *ctx = *pctx;
415
416 sk_OSSL_STORE_INFO_pop_free(ctx, store_info_free);
417 *pctx = NULL;
418 }
419
420 static FILE_HANDLER PKCS12_handler = {
421 "PKCS12",
422 try_decode_PKCS12,
423 eof_PKCS12,
424 destroy_ctx_PKCS12,
425 1 /* repeatable */
426 };
427
428 /*
429 * Encrypted PKCS#8 decoder. It operates by just decrypting the given blob
430 * into a new blob, which is returned as an EMBEDDED STORE_INFO. The whole
431 * decoding process will then start over with the new blob.
432 */
433 static OSSL_STORE_INFO *try_decode_PKCS8Encrypted(const char *pem_name,
434 const char *pem_header,
435 const unsigned char *blob,
436 size_t len, void **pctx,
437 int *matchcount,
438 const UI_METHOD *ui_method,
439 void *ui_data,
440 const char *uri,
441 OSSL_LIB_CTX *libctx,
442 const char *propq)
443 {
444 X509_SIG *p8 = NULL;
445 char kbuf[PEM_BUFSIZE];
446 char *pass = NULL;
447 const X509_ALGOR *dalg = NULL;
448 const ASN1_OCTET_STRING *doct = NULL;
449 OSSL_STORE_INFO *store_info = NULL;
450 BUF_MEM *mem = NULL;
451 unsigned char *new_data = NULL;
452 int new_data_len;
453
454 if (pem_name != NULL) {
455 if (strcmp(pem_name, PEM_STRING_PKCS8) != 0)
456 return NULL;
457 *matchcount = 1;
458 }
459
460 if (len > LONG_MAX || (p8 = d2i_X509_SIG(NULL, &blob, (long)len)) == NULL)
461 return NULL;
462
463 *matchcount = 1;
464
465 if ((mem = BUF_MEM_new()) == NULL) {
466 ATTICerr(0, ERR_R_BUF_LIB);
467 goto nop8;
468 }
469
470 if ((pass = file_get_pass(ui_method, kbuf, PEM_BUFSIZE,
471 "PKCS8 decrypt pass phrase", uri,
472 ui_data))
473 == NULL) {
474 ATTICerr(0, ATTIC_R_BAD_PASSWORD_READ);
475 goto nop8;
476 }
477
478 X509_SIG_get0(p8, &dalg, &doct);
479 if (!PKCS12_pbe_crypt(dalg, pass, (int)strlen(pass),
480 doct->data, doct->length,
481 &new_data, &new_data_len, 0))
482 goto nop8;
483
484 mem->data = (char *)new_data;
485 mem->max = mem->length = (size_t)new_data_len;
486 X509_SIG_free(p8);
487 p8 = NULL;
488
489 store_info = new_EMBEDDED(PEM_STRING_PKCS8INF, mem);
490 if (store_info == NULL) {
491 ATTICerr(0, ERR_R_OSSL_STORE_LIB);
492 goto nop8;
493 }
494
495 return store_info;
496 nop8:
497 X509_SIG_free(p8);
498 BUF_MEM_free(mem);
499 return NULL;
500 }
501
502 static FILE_HANDLER PKCS8Encrypted_handler = {
503 "PKCS8Encrypted",
504 try_decode_PKCS8Encrypted
505 };
506
507 /*
508 * Private key decoder. Decodes all sorts of private keys, both PKCS#8
509 * encoded ones and old style PEM ones (with the key type is encoded into
510 * the PEM name).
511 */
512 static OSSL_STORE_INFO *try_decode_PrivateKey(const char *pem_name,
513 const char *pem_header,
514 const unsigned char *blob,
515 size_t len, void **pctx,
516 int *matchcount,
517 const UI_METHOD *ui_method,
518 void *ui_data, const char *uri,
519 OSSL_LIB_CTX *libctx,
520 const char *propq)
521 {
522 OSSL_STORE_INFO *store_info = NULL;
523 EVP_PKEY *pkey = NULL;
524 const EVP_PKEY_ASN1_METHOD *ameth = NULL;
525
526 if (len > LONG_MAX)
527 return NULL;
528 if (pem_name != NULL) {
529 if (strcmp(pem_name, PEM_STRING_PKCS8INF) == 0) {
530 PKCS8_PRIV_KEY_INFO *p8inf = d2i_PKCS8_PRIV_KEY_INFO(NULL, &blob, (long)len);
531
532 *matchcount = 1;
533 if (p8inf != NULL)
534 pkey = EVP_PKCS82PKEY_ex(p8inf, libctx, propq);
535 PKCS8_PRIV_KEY_INFO_free(p8inf);
536 } else {
537 int slen;
538 int pkey_id;
539
540 if ((slen = check_suffix(pem_name, "PRIVATE KEY")) > 0
541 && (ameth = EVP_PKEY_asn1_find_str(NULL, pem_name,
542 slen))
543 != NULL
544 && EVP_PKEY_asn1_get0_info(&pkey_id, NULL, NULL, NULL, NULL,
545 ameth)) {
546 *matchcount = 1;
547 pkey = d2i_PrivateKey_ex(pkey_id, NULL, &blob, (long)len,
548 libctx, propq);
549 }
550 }
551 } else {
552 int i;
553 #ifndef OPENSSL_NO_ENGINE
554 ENGINE *curengine = ENGINE_get_first();
555
556 while (curengine != NULL) {
557 ENGINE_PKEY_ASN1_METHS_PTR asn1meths = ENGINE_get_pkey_asn1_meths(curengine);
558
559 if (asn1meths != NULL) {
560 const int *nids = NULL;
561 int nids_n = asn1meths(curengine, NULL, &nids, 0);
562
563 for (i = 0; i < nids_n; i++) {
564 EVP_PKEY_ASN1_METHOD *ameth2 = NULL;
565 EVP_PKEY *tmp_pkey = NULL;
566 const unsigned char *tmp_blob = blob;
567 int pkey_id, pkey_flags;
568
569 if (!asn1meths(curengine, &ameth2, NULL, nids[i])
570 || !EVP_PKEY_asn1_get0_info(&pkey_id, NULL,
571 &pkey_flags, NULL, NULL,
572 ameth2)
573 || (pkey_flags & ASN1_PKEY_ALIAS) != 0)
574 continue;
575
576 ERR_set_mark(); /* prevent flooding error queue */
577 tmp_pkey = d2i_PrivateKey_ex(pkey_id, NULL,
578 &tmp_blob, (long)len,
579 libctx, propq);
580 if (tmp_pkey != NULL) {
581 if (pkey != NULL)
582 EVP_PKEY_free(tmp_pkey);
583 else
584 pkey = tmp_pkey;
585 (*matchcount)++;
586 }
587 ERR_pop_to_mark();
588 }
589 }
590 curengine = ENGINE_get_next(curengine);
591 }
592 #endif
593
594 for (i = 0; i < EVP_PKEY_asn1_get_count(); i++) {
595 EVP_PKEY *tmp_pkey = NULL;
596 const unsigned char *tmp_blob = blob;
597 int pkey_id, pkey_flags;
598
599 ameth = EVP_PKEY_asn1_get0(i);
600 if (!EVP_PKEY_asn1_get0_info(&pkey_id, NULL, &pkey_flags, NULL,
601 NULL, ameth)
602 || (pkey_flags & ASN1_PKEY_ALIAS) != 0)
603 continue;
604
605 ERR_set_mark(); /* prevent flooding error queue */
606 tmp_pkey = d2i_PrivateKey_ex(pkey_id, NULL, &tmp_blob, (long)len,
607 libctx, propq);
608 if (tmp_pkey != NULL) {
609 if (pkey != NULL)
610 EVP_PKEY_free(tmp_pkey);
611 else
612 pkey = tmp_pkey;
613 (*matchcount)++;
614 }
615 ERR_pop_to_mark();
616 }
617
618 if (*matchcount > 1) {
619 EVP_PKEY_free(pkey);
620 pkey = NULL;
621 }
622 }
623 if (pkey == NULL)
624 /* No match */
625 return NULL;
626
627 store_info = OSSL_STORE_INFO_new_PKEY(pkey);
628 if (store_info == NULL)
629 EVP_PKEY_free(pkey);
630
631 return store_info;
632 }
633
634 static FILE_HANDLER PrivateKey_handler = {
635 "PrivateKey",
636 try_decode_PrivateKey
637 };
638
639 /*
640 * Public key decoder. Only supports SubjectPublicKeyInfo formatted keys.
641 */
642 static OSSL_STORE_INFO *try_decode_PUBKEY(const char *pem_name,
643 const char *pem_header,
644 const unsigned char *blob,
645 size_t len, void **pctx,
646 int *matchcount,
647 const UI_METHOD *ui_method,
648 void *ui_data, const char *uri,
649 OSSL_LIB_CTX *libctx,
650 const char *propq)
651 {
652 OSSL_STORE_INFO *store_info = NULL;
653 EVP_PKEY *pkey = NULL;
654
655 if (pem_name != NULL) {
656 if (strcmp(pem_name, PEM_STRING_PUBLIC) != 0)
657 /* No match */
658 return NULL;
659 *matchcount = 1;
660 }
661
662 if (len > LONG_MAX || (pkey = d2i_PUBKEY(NULL, &blob, (long)len)) != NULL) {
663 *matchcount = 1;
664 store_info = OSSL_STORE_INFO_new_PUBKEY(pkey);
665 }
666
667 return store_info;
668 }
669
670 static FILE_HANDLER PUBKEY_handler = {
671 "PUBKEY",
672 try_decode_PUBKEY
673 };
674
675 /*
676 * Key parameter decoder.
677 */
678 static OSSL_STORE_INFO *try_decode_params(const char *pem_name,
679 const char *pem_header,
680 const unsigned char *blob,
681 size_t len, void **pctx,
682 int *matchcount,
683 const UI_METHOD *ui_method,
684 void *ui_data, const char *uri,
685 OSSL_LIB_CTX *libctx,
686 const char *propq)
687 {
688 OSSL_STORE_INFO *store_info = NULL;
689 EVP_PKEY *pkey = NULL;
690 const EVP_PKEY_ASN1_METHOD *ameth = NULL;
691
692 if (len > LONG_MAX)
693 return NULL;
694 if (pem_name != NULL) {
695 int slen;
696 int pkey_id;
697
698 if ((slen = check_suffix(pem_name, "PARAMETERS")) > 0
699 && (ameth = EVP_PKEY_asn1_find_str(NULL, pem_name, slen)) != NULL
700 && EVP_PKEY_asn1_get0_info(&pkey_id, NULL, NULL, NULL, NULL,
701 ameth)) {
702 *matchcount = 1;
703 pkey = d2i_KeyParams(pkey_id, NULL, &blob, (long)len);
704 }
705 } else {
706 int i;
707
708 for (i = 0; i < EVP_PKEY_asn1_get_count(); i++) {
709 EVP_PKEY *tmp_pkey = NULL;
710 const unsigned char *tmp_blob = blob;
711 int pkey_id, pkey_flags;
712
713 ameth = EVP_PKEY_asn1_get0(i);
714 if (!EVP_PKEY_asn1_get0_info(&pkey_id, NULL, &pkey_flags, NULL,
715 NULL, ameth)
716 || (pkey_flags & ASN1_PKEY_ALIAS) != 0)
717 continue;
718
719 ERR_set_mark(); /* prevent flooding error queue */
720
721 tmp_pkey = d2i_KeyParams(pkey_id, NULL, &tmp_blob, (long)len);
722
723 if (tmp_pkey != NULL) {
724 if (pkey != NULL)
725 EVP_PKEY_free(tmp_pkey);
726 else
727 pkey = tmp_pkey;
728 (*matchcount)++;
729 }
730 ERR_pop_to_mark();
731 }
732
733 if (*matchcount > 1) {
734 EVP_PKEY_free(pkey);
735 pkey = NULL;
736 }
737 }
738 if (pkey == NULL)
739 /* No match */
740 return NULL;
741
742 store_info = OSSL_STORE_INFO_new_PARAMS(pkey);
743 if (store_info == NULL)
744 EVP_PKEY_free(pkey);
745
746 return store_info;
747 }
748
749 static FILE_HANDLER params_handler = {
750 "params",
751 try_decode_params
752 };
753
754 /*
755 * X.509 certificate decoder.
756 */
757 static OSSL_STORE_INFO *try_decode_X509Certificate(const char *pem_name,
758 const char *pem_header,
759 const unsigned char *blob,
760 size_t len, void **pctx,
761 int *matchcount,
762 const UI_METHOD *ui_method,
763 void *ui_data,
764 const char *uri,
765 OSSL_LIB_CTX *libctx,
766 const char *propq)
767 {
768 OSSL_STORE_INFO *store_info = NULL;
769 X509 *cert = NULL;
770
771 /*
772 * In most cases, we can try to interpret the serialized data as a trusted
773 * cert (X509 + X509_AUX) and fall back to reading it as a normal cert
774 * (just X509), but if the PEM name specifically declares it as a trusted
775 * cert, then no fallback should be engaged. |ignore_trusted| tells if
776 * the fallback can be used (1) or not (0).
777 */
778 int ignore_trusted = 1;
779
780 if (len > LONG_MAX)
781 return NULL;
782 if (pem_name != NULL) {
783 if (strcmp(pem_name, PEM_STRING_X509_TRUSTED) == 0)
784 ignore_trusted = 0;
785 else if (strcmp(pem_name, PEM_STRING_X509_OLD) != 0
786 && strcmp(pem_name, PEM_STRING_X509) != 0)
787 /* No match */
788 return NULL;
789 *matchcount = 1;
790 }
791
792 cert = X509_new_ex(libctx, propq);
793 if (cert == NULL)
794 return NULL;
795
796 if ((d2i_X509_AUX(&cert, &blob, (long)len)) != NULL
797 || (ignore_trusted && (d2i_X509(&cert, &blob, (long)len)) != NULL)) {
798 *matchcount = 1;
799 store_info = OSSL_STORE_INFO_new_CERT(cert);
800 }
801
802 if (store_info == NULL)
803 X509_free(cert);
804
805 return store_info;
806 }
807
808 static FILE_HANDLER X509Certificate_handler = {
809 "X509Certificate",
810 try_decode_X509Certificate
811 };
812
813 /*
814 * X.509 CRL decoder.
815 */
816 static OSSL_STORE_INFO *try_decode_X509CRL(const char *pem_name,
817 const char *pem_header,
818 const unsigned char *blob,
819 size_t len, void **pctx,
820 int *matchcount,
821 const UI_METHOD *ui_method,
822 void *ui_data, const char *uri,
823 OSSL_LIB_CTX *libctx,
824 const char *propq)
825 {
826 OSSL_STORE_INFO *store_info = NULL;
827 X509_CRL *crl = NULL;
828
829 if (len > LONG_MAX)
830 return NULL;
831 if (pem_name != NULL) {
832 if (strcmp(pem_name, PEM_STRING_X509_CRL) != 0)
833 /* No match */
834 return NULL;
835 *matchcount = 1;
836 }
837
838 if ((crl = d2i_X509_CRL(NULL, &blob, (long)len)) != NULL) {
839 *matchcount = 1;
840 store_info = OSSL_STORE_INFO_new_CRL(crl);
841 }
842
843 if (store_info == NULL)
844 X509_CRL_free(crl);
845
846 return store_info;
847 }
848
849 static FILE_HANDLER X509CRL_handler = {
850 "X509CRL",
851 try_decode_X509CRL
852 };
853
854 /*
855 * To finish it all off, we collect all the handlers.
856 */
857 static const FILE_HANDLER *file_handlers[] = {
858 &PKCS12_handler,
859 &PKCS8Encrypted_handler,
860 &X509Certificate_handler,
861 &X509CRL_handler,
862 &params_handler,
863 &PUBKEY_handler,
864 &PrivateKey_handler,
865 };
866
867 /*-
868 * The loader itself
869 * -----------------
870 */
871
872 struct ossl_store_loader_ctx_st {
873 char *uri; /* The URI we currently try to load */
874 enum {
875 is_raw = 0,
876 is_pem,
877 is_dir
878 } type;
879 int errcnt;
880 #define FILE_FLAG_SECMEM (1 << 0)
881 #define FILE_FLAG_ATTACHED (1 << 1)
882 unsigned int flags;
883 union {
884 struct { /* Used with is_raw and is_pem */
885 BIO *file;
886
887 /*
888 * The following are used when the handler is marked as
889 * repeatable
890 */
891 const FILE_HANDLER *last_handler;
892 void *last_handler_ctx;
893 } file;
894 struct { /* Used with is_dir */
895 OPENSSL_DIR_CTX *ctx;
896 int end_reached;
897
898 /*
899 * When a search expression is given, these are filled in.
900 * |search_name| contains the file basename to look for.
901 * The string is exactly 8 characters long.
902 */
903 char search_name[9];
904
905 /*
906 * The directory reading utility we have combines opening with
907 * reading the first name. To make sure we can detect the end
908 * at the right time, we read early and cache the name.
909 */
910 const char *last_entry;
911 int last_errno;
912 } dir;
913 } _;
914
915 /* Expected object type. May be unspecified */
916 int expected_type;
917
918 OSSL_LIB_CTX *libctx;
919 char *propq;
920 };
921
922 static void OSSL_STORE_LOADER_CTX_free(OSSL_STORE_LOADER_CTX *ctx)
923 {
924 if (ctx == NULL)
925 return;
926
927 OPENSSL_free(ctx->propq);
928 OPENSSL_free(ctx->uri);
929 if (ctx->type != is_dir) {
930 if (ctx->_.file.last_handler != NULL) {
931 ctx->_.file.last_handler->destroy_ctx(&ctx->_.file.last_handler_ctx);
932 ctx->_.file.last_handler_ctx = NULL;
933 ctx->_.file.last_handler = NULL;
934 }
935 }
936 OPENSSL_free(ctx);
937 }
938
939 static int file_find_type(OSSL_STORE_LOADER_CTX *ctx)
940 {
941 BIO *buff = NULL;
942 char peekbuf[4096] = {
943 0,
944 };
945
946 if ((buff = BIO_new(BIO_f_buffer())) == NULL)
947 return 0;
948
949 ctx->_.file.file = BIO_push(buff, ctx->_.file.file);
950 if (BIO_buffer_peek(ctx->_.file.file, peekbuf, sizeof(peekbuf) - 1) > 0) {
951 peekbuf[sizeof(peekbuf) - 1] = '\0';
952 if (strstr(peekbuf, "-----BEGIN ") != NULL)
953 ctx->type = is_pem;
954 }
955 return 1;
956 }
957
958 static OSSL_STORE_LOADER_CTX *file_open_ex(const OSSL_STORE_LOADER *loader, const char *uri,
959 OSSL_LIB_CTX *libctx, const char *propq,
960 const UI_METHOD *ui_method, void *ui_data)
961 {
962 OSSL_STORE_LOADER_CTX *ctx = NULL;
963 struct stat st;
964 const char *path_data[2];
965 size_t path_data_n = 0, i;
966 const char *path, *p = uri, *q;
967
968 /*
969 * First step, just take the URI as is.
970 */
971 path_data[path_data_n++] = uri;
972
973 /*
974 * Second step, if the URI appears to start with the "file" scheme,
975 * extract the path and make that the second path to check.
976 * There's a special case if the URI also contains an authority, then
977 * the full URI shouldn't be used as a path anywhere.
978 */
979 if (CHECK_AND_SKIP_CASE_PREFIX(p, "file:")) {
980 q = p;
981 if (CHECK_AND_SKIP_PREFIX(q, "//")) {
982 path_data_n--; /* Invalidate using the full URI */
983 if (CHECK_AND_SKIP_CASE_PREFIX(q, "localhost/")
984 || CHECK_AND_SKIP_PREFIX(q, "/")) {
985 /*
986 * In this case, we step back on char to ensure that the
987 * first slash is preserved, making the path always absolute
988 */
989 p = q - 1;
990 } else {
991 ATTICerr(0, ATTIC_R_URI_AUTHORITY_UNSUPPORTED);
992 return NULL;
993 }
994 }
995 #ifdef _WIN32
996 /* Windows "file:" URIs with a drive letter start with a '/' */
997 if (p[0] == '/' && p[2] == ':' && p[3] == '/') {
998 char c = tolower((unsigned char)p[1]);
999
1000 if (c >= 'a' && c <= 'z') {
1001 /* Skip past the slash, making the path a normal Windows path */
1002 p++;
1003 }
1004 }
1005 #endif
1006 path_data[path_data_n++] = p;
1007 }
1008
1009 for (i = 0, path = NULL; path == NULL && i < path_data_n; i++) {
1010 if (stat(path_data[i], &st) < 0) {
1011 ERR_raise_data(ERR_LIB_SYS, errno,
1012 "calling stat(%s)",
1013 path_data[i]);
1014 } else {
1015 path = path_data[i];
1016 }
1017 }
1018 if (path == NULL) {
1019 return NULL;
1020 }
1021
1022 /* Successfully found a working path */
1023
1024 ctx = OPENSSL_zalloc(sizeof(*ctx));
1025 if (ctx == NULL)
1026 return NULL;
1027 ctx->uri = OPENSSL_strdup(uri);
1028 if (ctx->uri == NULL)
1029 goto err;
1030
1031 if (S_ISDIR(st.st_mode)) {
1032 ctx->type = is_dir;
1033 ctx->_.dir.last_entry = OPENSSL_DIR_read(&ctx->_.dir.ctx, path);
1034 ctx->_.dir.last_errno = errno;
1035 if (ctx->_.dir.last_entry == NULL) {
1036 if (ctx->_.dir.last_errno != 0) {
1037 ERR_raise(ERR_LIB_SYS, ctx->_.dir.last_errno);
1038 goto err;
1039 }
1040 ctx->_.dir.end_reached = 1;
1041 }
1042 } else if ((ctx->_.file.file = BIO_new_file(path, "rb")) == NULL
1043 || !file_find_type(ctx)) {
1044 BIO_free_all(ctx->_.file.file);
1045 goto err;
1046 }
1047 if (propq != NULL) {
1048 ctx->propq = OPENSSL_strdup(propq);
1049 if (ctx->propq == NULL)
1050 goto err;
1051 }
1052 ctx->libctx = libctx;
1053
1054 return ctx;
1055 err:
1056 OSSL_STORE_LOADER_CTX_free(ctx);
1057 return NULL;
1058 }
1059
1060 static OSSL_STORE_LOADER_CTX *file_open(const OSSL_STORE_LOADER *loader, const char *uri,
1061 const UI_METHOD *ui_method, void *ui_data)
1062 {
1063 return file_open_ex(loader, uri, NULL, NULL, ui_method, ui_data);
1064 }
1065
1066 static OSSL_STORE_LOADER_CTX *file_attach(const OSSL_STORE_LOADER *loader, BIO *bp,
1067 OSSL_LIB_CTX *libctx, const char *propq,
1068 const UI_METHOD *ui_method, void *ui_data)
1069 {
1070 OSSL_STORE_LOADER_CTX *ctx = NULL;
1071
1072 if ((ctx = OPENSSL_zalloc(sizeof(*ctx))) == NULL
1073 || (propq != NULL && (ctx->propq = OPENSSL_strdup(propq)) == NULL)) {
1074 OSSL_STORE_LOADER_CTX_free(ctx);
1075 return NULL;
1076 }
1077 ctx->libctx = libctx;
1078 ctx->flags |= FILE_FLAG_ATTACHED;
1079 ctx->_.file.file = bp;
1080 if (!file_find_type(ctx)) {
1081 /* Safety measure */
1082 ctx->_.file.file = NULL;
1083 goto err;
1084 }
1085 return ctx;
1086 err:
1087 OSSL_STORE_LOADER_CTX_free(ctx);
1088 return NULL;
1089 }
1090
1091 static int file_ctrl(OSSL_STORE_LOADER_CTX *ctx, int cmd, va_list args)
1092 {
1093 int ret = 1;
1094
1095 switch (cmd) {
1096 case OSSL_STORE_C_USE_SECMEM: {
1097 int on = *(va_arg(args, int *));
1098
1099 switch (on) {
1100 case 0:
1101 ctx->flags &= ~FILE_FLAG_SECMEM;
1102 break;
1103 case 1:
1104 ctx->flags |= FILE_FLAG_SECMEM;
1105 break;
1106 default:
1107 ATTICerr(0, ERR_R_PASSED_INVALID_ARGUMENT);
1108 ret = 0;
1109 break;
1110 }
1111 } break;
1112 default:
1113 break;
1114 }
1115
1116 return ret;
1117 }
1118
1119 static int file_expect(OSSL_STORE_LOADER_CTX *ctx, int expected)
1120 {
1121 ctx->expected_type = expected;
1122 return 1;
1123 }
1124
1125 static int file_find(OSSL_STORE_LOADER_CTX *ctx,
1126 const OSSL_STORE_SEARCH *search)
1127 {
1128 /*
1129 * If ctx == NULL, the library is looking to know if this loader supports
1130 * the given search type.
1131 */
1132
1133 if (OSSL_STORE_SEARCH_get_type(search) == OSSL_STORE_SEARCH_BY_NAME) {
1134 unsigned long hash = 0;
1135
1136 if (ctx == NULL)
1137 return 1;
1138
1139 if (ctx->type != is_dir) {
1140 ATTICerr(0, ATTIC_R_SEARCH_ONLY_SUPPORTED_FOR_DIRECTORIES);
1141 return 0;
1142 }
1143
1144 hash = X509_NAME_hash_ex(OSSL_STORE_SEARCH_get0_name(search),
1145 NULL, NULL, NULL);
1146 BIO_snprintf(ctx->_.dir.search_name, sizeof(ctx->_.dir.search_name),
1147 "%08lx", hash);
1148 return 1;
1149 }
1150
1151 if (ctx != NULL)
1152 ATTICerr(0, ATTIC_R_UNSUPPORTED_SEARCH_TYPE);
1153 return 0;
1154 }
1155
1156 static OSSL_STORE_INFO *file_load_try_decode(OSSL_STORE_LOADER_CTX *ctx,
1157 const char *pem_name,
1158 const char *pem_header,
1159 unsigned char *data, size_t len,
1160 const UI_METHOD *ui_method,
1161 void *ui_data, int *matchcount)
1162 {
1163 OSSL_STORE_INFO *result = NULL;
1164 BUF_MEM *new_mem = NULL;
1165 char *new_pem_name = NULL;
1166 int t = 0;
1167
1168 again: {
1169 size_t i = 0;
1170 void *handler_ctx = NULL;
1171 const FILE_HANDLER **matching_handlers = OPENSSL_zalloc(sizeof(*matching_handlers)
1172 * OSSL_NELEM(file_handlers));
1173
1174 if (matching_handlers == NULL)
1175 goto err;
1176
1177 *matchcount = 0;
1178 for (i = 0; i < OSSL_NELEM(file_handlers); i++) {
1179 const FILE_HANDLER *handler = file_handlers[i];
1180 int try_matchcount = 0;
1181 void *tmp_handler_ctx = NULL;
1182 OSSL_STORE_INFO *tmp_result;
1183 unsigned long err;
1184
1185 ERR_set_mark();
1186 tmp_result = handler->try_decode(pem_name, pem_header, data, len,
1187 &tmp_handler_ctx, &try_matchcount,
1188 ui_method, ui_data, ctx->uri,
1189 ctx->libctx, ctx->propq);
1190 /* avoid flooding error queue with low-level ASN.1 parse errors */
1191 err = ERR_peek_last_error();
1192 if (ERR_GET_LIB(err) == ERR_LIB_ASN1
1193 && ERR_GET_REASON(err) == ERR_R_NESTED_ASN1_ERROR)
1194 ERR_pop_to_mark();
1195 else
1196 ERR_clear_last_mark();
1197
1198 if (try_matchcount > 0) {
1199
1200 matching_handlers[*matchcount] = handler;
1201
1202 if (handler_ctx)
1203 handler->destroy_ctx(&handler_ctx);
1204 handler_ctx = tmp_handler_ctx;
1205
1206 if ((*matchcount += try_matchcount) > 1) {
1207 /* more than one match => ambiguous, kill any result */
1208 store_info_free(result);
1209 store_info_free(tmp_result);
1210 if (handler->destroy_ctx != NULL)
1211 handler->destroy_ctx(&handler_ctx);
1212 handler_ctx = NULL;
1213 tmp_result = NULL;
1214 result = NULL;
1215 }
1216 if (result == NULL)
1217 result = tmp_result;
1218 if (result == NULL) /* e.g., PKCS#12 file decryption error */
1219 break;
1220 }
1221 }
1222
1223 if (result != NULL
1224 && *matchcount == 1 && matching_handlers[0]->repeatable) {
1225 ctx->_.file.last_handler = matching_handlers[0];
1226 ctx->_.file.last_handler_ctx = handler_ctx;
1227 }
1228
1229 OPENSSL_free(matching_handlers);
1230 }
1231
1232 err:
1233 OPENSSL_free(new_pem_name);
1234 BUF_MEM_free(new_mem);
1235
1236 if (result != NULL
1237 && (t = OSSL_STORE_INFO_get_type(result)) == STORE_INFO_EMBEDDED) {
1238 struct embedded_st *embedded = get0_EMBEDDED(result);
1239
1240 /* "steal" the embedded data */
1241 pem_name = new_pem_name = embedded->pem_name;
1242 new_mem = embedded->blob;
1243 data = (unsigned char *)new_mem->data;
1244 len = new_mem->length;
1245 embedded->pem_name = NULL;
1246 embedded->blob = NULL;
1247
1248 store_info_free(result);
1249 result = NULL;
1250 goto again;
1251 }
1252
1253 return result;
1254 }
1255
1256 static OSSL_STORE_INFO *file_load_try_repeat(OSSL_STORE_LOADER_CTX *ctx,
1257 const UI_METHOD *ui_method,
1258 void *ui_data)
1259 {
1260 OSSL_STORE_INFO *result = NULL;
1261 int try_matchcount = 0;
1262
1263 if (ctx->_.file.last_handler != NULL) {
1264 result = ctx->_.file.last_handler->try_decode(NULL, NULL, NULL, 0,
1265 &ctx->_.file.last_handler_ctx,
1266 &try_matchcount,
1267 ui_method, ui_data, ctx->uri,
1268 ctx->libctx, ctx->propq);
1269
1270 if (result == NULL) {
1271 ctx->_.file.last_handler->destroy_ctx(&ctx->_.file.last_handler_ctx);
1272 ctx->_.file.last_handler_ctx = NULL;
1273 ctx->_.file.last_handler = NULL;
1274 }
1275 }
1276 return result;
1277 }
1278
1279 static void pem_free_flag(void *pem_data, int secure, size_t num)
1280 {
1281 if (secure)
1282 OPENSSL_secure_clear_free(pem_data, num);
1283 else
1284 OPENSSL_free(pem_data);
1285 }
1286 static int file_read_pem(BIO *bp, char **pem_name, char **pem_header,
1287 unsigned char **data, long *len,
1288 const UI_METHOD *ui_method, void *ui_data,
1289 const char *uri, int secure)
1290 {
1291 int i = secure
1292 ? PEM_read_bio_ex(bp, pem_name, pem_header, data, len,
1293 PEM_FLAG_SECURE | PEM_FLAG_EAY_COMPATIBLE)
1294 : PEM_read_bio(bp, pem_name, pem_header, data, len);
1295
1296 if (i <= 0)
1297 return 0;
1298
1299 /*
1300 * 10 is the number of characters in "Proc-Type:", which
1301 * PEM_get_EVP_CIPHER_INFO() requires to be present.
1302 * If the PEM header has less characters than that, it's
1303 * not worth spending cycles on it.
1304 */
1305 if (strlen(*pem_header) > 10) {
1306 EVP_CIPHER_INFO cipher;
1307 struct pem_pass_data pass_data;
1308
1309 if (!PEM_get_EVP_CIPHER_INFO(*pem_header, &cipher)
1310 || !file_fill_pem_pass_data(&pass_data, "PEM pass phrase", uri,
1311 ui_method, ui_data)
1312 || !PEM_do_header(&cipher, *data, len, file_get_pem_pass,
1313 &pass_data)) {
1314 return 0;
1315 }
1316 }
1317 return 1;
1318 }
1319
1320 static OSSL_STORE_INFO *file_try_read_msblob(BIO *bp, int *matchcount)
1321 {
1322 OSSL_STORE_INFO *result = NULL;
1323 int ispub = -1;
1324
1325 {
1326 unsigned int magic = 0, bitlen = 0;
1327 int isdss = 0;
1328 unsigned char peekbuf[16] = {
1329 0,
1330 };
1331 const unsigned char *p = peekbuf;
1332
1333 if (BIO_buffer_peek(bp, peekbuf, sizeof(peekbuf)) <= 0)
1334 return 0;
1335 if (ossl_do_blob_header(&p, sizeof(peekbuf), &magic, &bitlen,
1336 &isdss, &ispub)
1337 <= 0)
1338 return 0;
1339 }
1340
1341 (*matchcount)++;
1342
1343 {
1344 EVP_PKEY *tmp = ispub
1345 ? b2i_PublicKey_bio(bp)
1346 : b2i_PrivateKey_bio(bp);
1347
1348 if (tmp == NULL
1349 || (result = OSSL_STORE_INFO_new_PKEY(tmp)) == NULL) {
1350 EVP_PKEY_free(tmp);
1351 return 0;
1352 }
1353 }
1354
1355 return result;
1356 }
1357
1358 static OSSL_STORE_INFO *file_try_read_PVK(BIO *bp, const UI_METHOD *ui_method,
1359 void *ui_data, const char *uri,
1360 int *matchcount)
1361 {
1362 OSSL_STORE_INFO *result = NULL;
1363
1364 {
1365 unsigned int saltlen = 0, keylen = 0;
1366 int isdss = -1;
1367 unsigned char peekbuf[24] = {
1368 0,
1369 };
1370 const unsigned char *p = peekbuf;
1371
1372 if (BIO_buffer_peek(bp, peekbuf, sizeof(peekbuf)) <= 0)
1373 return 0;
1374 if (!ossl_do_PVK_header(&p, sizeof(peekbuf), 0, &isdss, &saltlen, &keylen))
1375 return 0;
1376 }
1377
1378 (*matchcount)++;
1379
1380 {
1381 EVP_PKEY *tmp = NULL;
1382 struct pem_pass_data pass_data;
1383
1384 if (!file_fill_pem_pass_data(&pass_data, "PVK pass phrase", uri,
1385 ui_method, ui_data)
1386 || (tmp = b2i_PVK_bio(bp, file_get_pem_pass, &pass_data)) == NULL
1387 || (result = OSSL_STORE_INFO_new_PKEY(tmp)) == NULL) {
1388 EVP_PKEY_free(tmp);
1389 return 0;
1390 }
1391 }
1392
1393 return result;
1394 }
1395
1396 static int file_read_asn1(BIO *bp, unsigned char **data, long *len)
1397 {
1398 BUF_MEM *mem = NULL;
1399
1400 if (asn1_d2i_read_bio(bp, &mem) < 0)
1401 return 0;
1402
1403 *data = (unsigned char *)mem->data;
1404 *len = (long)mem->length;
1405 OPENSSL_free(mem);
1406
1407 return 1;
1408 }
1409
1410 static int file_name_to_uri(OSSL_STORE_LOADER_CTX *ctx, const char *name,
1411 char **data)
1412 {
1413 assert(name != NULL);
1414 assert(data != NULL);
1415 {
1416 const char *pathsep = ossl_ends_with_dirsep(ctx->uri) ? "" : "/";
1417 long calculated_length = (long)(strlen(ctx->uri) + strlen(pathsep)
1418 + strlen(name) + 1 /* \0 */);
1419
1420 *data = OPENSSL_zalloc(calculated_length);
1421 if (*data == NULL)
1422 return 0;
1423
1424 OPENSSL_strlcat(*data, ctx->uri, calculated_length);
1425 OPENSSL_strlcat(*data, pathsep, calculated_length);
1426 OPENSSL_strlcat(*data, name, calculated_length);
1427 }
1428 return 1;
1429 }
1430
1431 static int file_name_check(OSSL_STORE_LOADER_CTX *ctx, const char *name)
1432 {
1433 const char *p = NULL;
1434 size_t len = strlen(ctx->_.dir.search_name);
1435
1436 /* If there are no search criteria, all names are accepted */
1437 if (ctx->_.dir.search_name[0] == '\0')
1438 return 1;
1439
1440 /* If the expected type isn't supported, no name is accepted */
1441 if (ctx->expected_type != 0
1442 && ctx->expected_type != OSSL_STORE_INFO_CERT
1443 && ctx->expected_type != OSSL_STORE_INFO_CRL)
1444 return 0;
1445
1446 /*
1447 * First, check the basename
1448 */
1449 if (OPENSSL_strncasecmp(name, ctx->_.dir.search_name, len) != 0
1450 || name[len] != '.')
1451 return 0;
1452 p = &name[len + 1];
1453
1454 /*
1455 * Then, if the expected type is a CRL, check that the extension starts
1456 * with 'r'
1457 */
1458 if (*p == 'r') {
1459 p++;
1460 if (ctx->expected_type != 0
1461 && ctx->expected_type != OSSL_STORE_INFO_CRL)
1462 return 0;
1463 } else if (ctx->expected_type == OSSL_STORE_INFO_CRL) {
1464 return 0;
1465 }
1466
1467 /*
1468 * Last, check that the rest of the extension is a decimal number, at
1469 * least one digit long.
1470 */
1471 if (!isdigit((unsigned char)*p))
1472 return 0;
1473 while (isdigit((unsigned char)*p))
1474 p++;
1475
1476 #ifdef __VMS
1477 /*
1478 * One extra step here, check for a possible generation number.
1479 */
1480 if (*p == ';')
1481 for (p++; *p != '\0'; p++)
1482 if (!ossl_isdigit(*p))
1483 break;
1484 #endif
1485
1486 /*
1487 * If we've reached the end of the string at this point, we've successfully
1488 * found a fitting file name.
1489 */
1490 return *p == '\0';
1491 }
1492
1493 static int file_eof(OSSL_STORE_LOADER_CTX *ctx);
1494 static int file_error(OSSL_STORE_LOADER_CTX *ctx);
1495 static OSSL_STORE_INFO *file_load(OSSL_STORE_LOADER_CTX *ctx,
1496 const UI_METHOD *ui_method,
1497 void *ui_data)
1498 {
1499 OSSL_STORE_INFO *result = NULL;
1500
1501 ctx->errcnt = 0;
1502
1503 if (ctx->type == is_dir) {
1504 do {
1505 char *newname = NULL;
1506
1507 if (ctx->_.dir.last_entry == NULL) {
1508 if (!ctx->_.dir.end_reached) {
1509 assert(ctx->_.dir.last_errno != 0);
1510 ERR_raise(ERR_LIB_SYS, ctx->_.dir.last_errno);
1511 ctx->errcnt++;
1512 }
1513 return NULL;
1514 }
1515
1516 if (ctx->_.dir.last_entry[0] != '.'
1517 && file_name_check(ctx, ctx->_.dir.last_entry)
1518 && !file_name_to_uri(ctx, ctx->_.dir.last_entry, &newname))
1519 return NULL;
1520
1521 /*
1522 * On the first call (with a NULL context), OPENSSL_DIR_read()
1523 * cares about the second argument. On the following calls, it
1524 * only cares that it isn't NULL. Therefore, we can safely give
1525 * it our URI here.
1526 */
1527 ctx->_.dir.last_entry = OPENSSL_DIR_read(&ctx->_.dir.ctx, ctx->uri);
1528 ctx->_.dir.last_errno = errno;
1529 if (ctx->_.dir.last_entry == NULL && ctx->_.dir.last_errno == 0)
1530 ctx->_.dir.end_reached = 1;
1531
1532 if (newname != NULL
1533 && (result = OSSL_STORE_INFO_new_NAME(newname)) == NULL) {
1534 OPENSSL_free(newname);
1535 ATTICerr(0, ERR_R_OSSL_STORE_LIB);
1536 return NULL;
1537 }
1538 } while (result == NULL && !file_eof(ctx));
1539 } else {
1540 int matchcount = -1;
1541
1542 again:
1543 result = file_load_try_repeat(ctx, ui_method, ui_data);
1544 if (result != NULL)
1545 return result;
1546
1547 if (file_eof(ctx))
1548 return NULL;
1549
1550 do {
1551 char *pem_name = NULL; /* PEM record name */
1552 char *pem_header = NULL; /* PEM record header */
1553 unsigned char *data = NULL; /* DER encoded data */
1554 long len = 0; /* DER encoded data length */
1555
1556 matchcount = -1;
1557 if (ctx->type == is_pem) {
1558 if (!file_read_pem(ctx->_.file.file, &pem_name, &pem_header,
1559 &data, &len, ui_method, ui_data, ctx->uri,
1560 (ctx->flags & FILE_FLAG_SECMEM) != 0)) {
1561 ctx->errcnt++;
1562 goto endloop;
1563 }
1564 } else {
1565 if ((result = file_try_read_msblob(ctx->_.file.file,
1566 &matchcount))
1567 != NULL
1568 || (result = file_try_read_PVK(ctx->_.file.file,
1569 ui_method, ui_data, ctx->uri,
1570 &matchcount))
1571 != NULL)
1572 goto endloop;
1573
1574 if (!file_read_asn1(ctx->_.file.file, &data, &len)) {
1575 ctx->errcnt++;
1576 goto endloop;
1577 }
1578 }
1579
1580 result = file_load_try_decode(ctx, pem_name, pem_header, data, len,
1581 ui_method, ui_data, &matchcount);
1582
1583 if (result != NULL)
1584 goto endloop;
1585
1586 /*
1587 * If a PEM name matches more than one handler, the handlers are
1588 * badly coded.
1589 */
1590 if (!ossl_assert(pem_name == NULL || matchcount <= 1)) {
1591 ctx->errcnt++;
1592 goto endloop;
1593 }
1594
1595 if (matchcount > 1) {
1596 ATTICerr(0, ATTIC_R_AMBIGUOUS_CONTENT_TYPE);
1597 } else if (matchcount == 1) {
1598 /*
1599 * If there are other errors on the stack, they already show
1600 * what the problem is.
1601 */
1602 if (ERR_peek_error() == 0) {
1603 ATTICerr(0, ATTIC_R_UNSUPPORTED_CONTENT_TYPE);
1604 if (pem_name != NULL)
1605 ERR_add_error_data(3, "PEM type is '", pem_name, "'");
1606 }
1607 }
1608 if (matchcount > 0)
1609 ctx->errcnt++;
1610
1611 endloop:
1612 pem_free_flag(pem_name, (ctx->flags & FILE_FLAG_SECMEM) != 0, 0);
1613 pem_free_flag(pem_header, (ctx->flags & FILE_FLAG_SECMEM) != 0, 0);
1614 pem_free_flag(data, (ctx->flags & FILE_FLAG_SECMEM) != 0, len);
1615 } while (matchcount == 0 && !file_eof(ctx) && !file_error(ctx));
1616
1617 /* We bail out on ambiguity */
1618 if (matchcount > 1) {
1619 store_info_free(result);
1620 return NULL;
1621 }
1622
1623 if (result != NULL
1624 && ctx->expected_type != 0
1625 && ctx->expected_type != OSSL_STORE_INFO_get_type(result)) {
1626 store_info_free(result);
1627 goto again;
1628 }
1629 }
1630
1631 return result;
1632 }
1633
1634 static int file_error(OSSL_STORE_LOADER_CTX *ctx)
1635 {
1636 return ctx->errcnt > 0;
1637 }
1638
1639 static int file_eof(OSSL_STORE_LOADER_CTX *ctx)
1640 {
1641 if (ctx->type == is_dir)
1642 return ctx->_.dir.end_reached;
1643
1644 if (ctx->_.file.last_handler != NULL
1645 && !ctx->_.file.last_handler->eof(ctx->_.file.last_handler_ctx))
1646 return 0;
1647 return BIO_eof(ctx->_.file.file);
1648 }
1649
1650 static int file_close(OSSL_STORE_LOADER_CTX *ctx)
1651 {
1652 if ((ctx->flags & FILE_FLAG_ATTACHED) == 0) {
1653 if (ctx->type == is_dir)
1654 OPENSSL_DIR_end(&ctx->_.dir.ctx);
1655 else
1656 BIO_free_all(ctx->_.file.file);
1657 } else {
1658 /*
1659 * Because file_attach() called file_find_type(), we know that a
1660 * BIO_f_buffer() has been pushed on top of the regular BIO.
1661 */
1662 BIO *buff = ctx->_.file.file;
1663
1664 /* Detach buff */
1665 (void)BIO_pop(ctx->_.file.file);
1666 /* Safety measure */
1667 ctx->_.file.file = NULL;
1668
1669 BIO_free(buff);
1670 }
1671 OSSL_STORE_LOADER_CTX_free(ctx);
1672 return 1;
1673 }
1674
1675 /*-
1676 * ENGINE management
1677 */
1678
1679 static const char *loader_attic_id = "loader_attic";
1680 static const char *loader_attic_name = "'file:' loader";
1681
1682 static OSSL_STORE_LOADER *loader_attic = NULL;
1683
1684 static int loader_attic_init(ENGINE *e)
1685 {
1686 return 1;
1687 }
1688
1689 static int loader_attic_finish(ENGINE *e)
1690 {
1691 return 1;
1692 }
1693
1694 static int loader_attic_destroy(ENGINE *e)
1695 {
1696 OSSL_STORE_LOADER *loader = OSSL_STORE_unregister_loader("file");
1697
1698 if (loader == NULL)
1699 return 0;
1700
1701 ERR_unload_ATTIC_strings();
1702 OSSL_STORE_LOADER_free(loader);
1703 return 1;
1704 }
1705
1706 static int bind_loader_attic(ENGINE *e)
1707 {
1708
1709 /* Ensure the ATTIC error handling is set up on best effort basis */
1710 ERR_load_ATTIC_strings();
1711
1712 if (/* Create the OSSL_STORE_LOADER */
1713 (loader_attic = OSSL_STORE_LOADER_new(e, "file")) == NULL
1714 || !OSSL_STORE_LOADER_set_open_ex(loader_attic, file_open_ex)
1715 || !OSSL_STORE_LOADER_set_open(loader_attic, file_open)
1716 || !OSSL_STORE_LOADER_set_attach(loader_attic, file_attach)
1717 || !OSSL_STORE_LOADER_set_ctrl(loader_attic, file_ctrl)
1718 || !OSSL_STORE_LOADER_set_expect(loader_attic, file_expect)
1719 || !OSSL_STORE_LOADER_set_find(loader_attic, file_find)
1720 || !OSSL_STORE_LOADER_set_load(loader_attic, file_load)
1721 || !OSSL_STORE_LOADER_set_eof(loader_attic, file_eof)
1722 || !OSSL_STORE_LOADER_set_error(loader_attic, file_error)
1723 || !OSSL_STORE_LOADER_set_close(loader_attic, file_close)
1724 /* Init the engine itself */
1725 || !ENGINE_set_id(e, loader_attic_id)
1726 || !ENGINE_set_name(e, loader_attic_name)
1727 || !ENGINE_set_destroy_function(e, loader_attic_destroy)
1728 || !ENGINE_set_init_function(e, loader_attic_init)
1729 || !ENGINE_set_finish_function(e, loader_attic_finish)
1730 /* Finally, register the method with libcrypto */
1731 || !OSSL_STORE_register_loader(loader_attic)) {
1732 OSSL_STORE_LOADER_free(loader_attic);
1733 loader_attic = NULL;
1734 ATTICerr(0, ATTIC_R_INIT_FAILED);
1735 return 0;
1736 }
1737
1738 return 1;
1739 }
1740
1741 #ifdef OPENSSL_NO_DYNAMIC_ENGINE
1742 #error "Only allowed as dynamically shared object"
1743 #endif
1744
1745 static int bind_helper(ENGINE *e, const char *id)
1746 {
1747 if (id && (strcmp(id, loader_attic_id) != 0))
1748 return 0;
1749 if (!bind_loader_attic(e))
1750 return 0;
1751 return 1;
1752 }
1753
1754 IMPLEMENT_DYNAMIC_CHECK_FN()
1755 IMPLEMENT_DYNAMIC_BIND_FN(bind_helper)