Add X509 related libctx changes.
[openssl.git] / crypto / x509 / x509_d2.c
1 /*
2  * Copyright 1995-2020 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the Apache License 2.0 (the "License").  You may not use
5  * this file except in compliance with the License.  You can obtain a copy
6  * in the file LICENSE in the source distribution or at
7  * https://www.openssl.org/source/license.html
8  */
9
10 #include <stdio.h>
11 #include "internal/cryptlib.h"
12 #include <openssl/crypto.h>
13 #include <openssl/x509.h>
14
15 int X509_STORE_set_default_paths_with_libctx(X509_STORE *ctx,
16                                              OPENSSL_CTX *libctx,
17                                              const char *propq)
18 {
19     X509_LOOKUP *lookup;
20
21     lookup = X509_STORE_add_lookup(ctx, X509_LOOKUP_file());
22     if (lookup == NULL)
23         return 0;
24     X509_LOOKUP_load_file_with_libctx(lookup, NULL, X509_FILETYPE_DEFAULT,
25                                       libctx, propq);
26
27     lookup = X509_STORE_add_lookup(ctx, X509_LOOKUP_hash_dir());
28     if (lookup == NULL)
29         return 0;
30     X509_LOOKUP_add_dir(lookup, NULL, X509_FILETYPE_DEFAULT);
31
32     lookup = X509_STORE_add_lookup(ctx, X509_LOOKUP_store());
33     if (lookup == NULL)
34         return 0;
35     X509_LOOKUP_add_store_with_libctx(lookup, NULL, libctx, propq);
36
37     /* clear any errors */
38     ERR_clear_error();
39
40     return 1;
41 }
42 int X509_STORE_set_default_paths(X509_STORE *ctx)
43 {
44     return X509_STORE_set_default_paths_with_libctx(ctx, NULL, NULL);
45 }
46
47 int X509_STORE_load_file_with_libctx(X509_STORE *ctx, const char *file,
48                                      OPENSSL_CTX *libctx, const char *propq)
49 {
50     X509_LOOKUP *lookup;
51
52     if (file == NULL
53         || (lookup = X509_STORE_add_lookup(ctx, X509_LOOKUP_file())) == NULL
54         || X509_LOOKUP_load_file_with_libctx(lookup, file, X509_FILETYPE_PEM,
55                                              libctx, propq) == 0)
56         return 0;
57
58     return 1;
59 }
60
61 int X509_STORE_load_file(X509_STORE *ctx, const char *file)
62 {
63     return X509_STORE_load_file_with_libctx(ctx, file, NULL, NULL);
64 }
65
66 int X509_STORE_load_path(X509_STORE *ctx, const char *path)
67 {
68     X509_LOOKUP *lookup;
69
70     if (path == NULL
71         || (lookup = X509_STORE_add_lookup(ctx, X509_LOOKUP_hash_dir())) == NULL
72         || X509_LOOKUP_add_dir(lookup, path, X509_FILETYPE_PEM) == 0)
73         return 0;
74
75     return 1;
76 }
77
78 int X509_STORE_load_store_with_libctx(X509_STORE *ctx, const char *uri,
79                                       OPENSSL_CTX *libctx, const char *propq)
80 {
81     X509_LOOKUP *lookup;
82
83     if (uri == NULL
84         || (lookup = X509_STORE_add_lookup(ctx, X509_LOOKUP_store())) == NULL
85         || X509_LOOKUP_add_store_with_libctx(lookup, uri, libctx, propq) == 0)
86         return 0;
87
88     return 1;
89 }
90
91 int X509_STORE_load_store(X509_STORE *ctx, const char *uri)
92 {
93     return X509_STORE_load_store_with_libctx(ctx, uri, NULL, NULL);
94 }
95
96 int X509_STORE_load_locations_with_libctx(X509_STORE *ctx, const char *file,
97                                           const char *path,
98                                           OPENSSL_CTX *libctx, const char *propq)
99 {
100     if (file == NULL && path == NULL)
101         return 0;
102     if (file != NULL && !X509_STORE_load_file_with_libctx(ctx, file,
103                                                           libctx, propq))
104         return 0;
105     if (path != NULL && !X509_STORE_load_path(ctx, path))
106         return 0;
107     return 1;
108 }
109
110 int X509_STORE_load_locations(X509_STORE *ctx, const char *file,
111                               const char *path)
112 {
113     return X509_STORE_load_locations_with_libctx(ctx, file, path, NULL, NULL);
114 }