Add OSSL_STORE_LOADER_description()
[openssl.git] / include / openssl / store.h
1 /*
2  * Copyright 2016-2021 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 #ifndef OPENSSL_STORE_H
11 # define OPENSSL_STORE_H
12 # pragma once
13
14 # include <openssl/macros.h>
15 # ifndef OPENSSL_NO_DEPRECATED_3_0
16 #  define HEADER_OSSL_STORE_H
17 # endif
18
19 # include <stdarg.h>
20 # include <openssl/types.h>
21 # include <openssl/pem.h>
22 # include <openssl/storeerr.h>
23
24 # ifdef  __cplusplus
25 extern "C" {
26 # endif
27
28 /*-
29  *  The main OSSL_STORE functions.
30  *  ------------------------------
31  *
32  *  These allow applications to open a channel to a resource with supported
33  *  data (keys, certs, crls, ...), read the data a piece at a time and decide
34  *  what to do with it, and finally close.
35  */
36
37 typedef struct ossl_store_ctx_st OSSL_STORE_CTX;
38
39 /*
40  * Typedef for the OSSL_STORE_INFO post processing callback.  This can be used
41  * to massage the given OSSL_STORE_INFO, or to drop it entirely (by returning
42  * NULL).
43  */
44 typedef OSSL_STORE_INFO *(*OSSL_STORE_post_process_info_fn)(OSSL_STORE_INFO *,
45                                                             void *);
46
47 /*
48  * Open a channel given a URI.  The given UI method will be used any time the
49  * loader needs extra input, for example when a password or pin is needed, and
50  * will be passed the same user data every time it's needed in this context.
51  *
52  * Returns a context reference which represents the channel to communicate
53  * through.
54  */
55 OSSL_STORE_CTX *
56 OSSL_STORE_open(const char *uri, const UI_METHOD *ui_method, void *ui_data,
57                 OSSL_STORE_post_process_info_fn post_process,
58                 void *post_process_data);
59 OSSL_STORE_CTX *
60 OSSL_STORE_open_ex(const char *uri, OSSL_LIB_CTX *libctx, const char *propq,
61                    const UI_METHOD *ui_method, void *ui_data,
62                    OSSL_STORE_post_process_info_fn post_process,
63                    void *post_process_data);
64
65 /*
66  * Control / fine tune the OSSL_STORE channel.  |cmd| determines what is to be
67  * done, and depends on the underlying loader (use OSSL_STORE_get0_scheme to
68  * determine which loader is used), except for common commands (see below).
69  * Each command takes different arguments.
70  */
71 # ifndef OPENSSL_NO_DEPRECATED_3_0
72 OSSL_DEPRECATEDIN_3_0 int OSSL_STORE_ctrl(OSSL_STORE_CTX *ctx, int cmd,
73                                           ... /* args */);
74 OSSL_DEPRECATEDIN_3_0 int OSSL_STORE_vctrl(OSSL_STORE_CTX *ctx, int cmd,
75                                            va_list args);
76 # endif
77
78 # ifndef OPENSSL_NO_DEPRECATED_3_0
79
80 /*
81  * Common ctrl commands that different loaders may choose to support.
82  */
83 /* int on = 0 or 1; STORE_ctrl(ctx, STORE_C_USE_SECMEM, &on); */
84 # define OSSL_STORE_C_USE_SECMEM      1
85 /* Where custom commands start */
86 # define OSSL_STORE_C_CUSTOM_START    100
87
88 # endif
89
90 /*
91  * Read one data item (a key, a cert, a CRL) that is supported by the OSSL_STORE
92  * functionality, given a context.
93  * Returns a OSSL_STORE_INFO pointer, from which OpenSSL typed data can be
94  * extracted with OSSL_STORE_INFO_get0_PKEY(), OSSL_STORE_INFO_get0_CERT(), ...
95  * NULL is returned on error, which may include that the data found at the URI
96  * can't be figured out for certain or is ambiguous.
97  */
98 OSSL_STORE_INFO *OSSL_STORE_load(OSSL_STORE_CTX *ctx);
99
100 /*
101  * Check if end of data (end of file) is reached
102  * Returns 1 on end, 0 otherwise.
103  */
104 int OSSL_STORE_eof(OSSL_STORE_CTX *ctx);
105
106 /*
107  * Check if an error occurred
108  * Returns 1 if it did, 0 otherwise.
109  */
110 int OSSL_STORE_error(OSSL_STORE_CTX *ctx);
111
112 /*
113  * Close the channel
114  * Returns 1 on success, 0 on error.
115  */
116 int OSSL_STORE_close(OSSL_STORE_CTX *ctx);
117
118 /*
119  * Attach to a BIO.  This works like OSSL_STORE_open() except it takes a
120  * BIO instead of a uri, along with a scheme to use when reading.
121  * The given UI method will be used any time the loader needs extra input,
122  * for example when a password or pin is needed, and will be passed the
123  * same user data every time it's needed in this context.
124  *
125  * Returns a context reference which represents the channel to communicate
126  * through.
127  *
128  * Note that this function is considered unsafe, all depending on what the
129  * BIO actually reads.
130  */
131 OSSL_STORE_CTX *OSSL_STORE_attach(BIO *bio, const char *scheme,
132                                   OSSL_LIB_CTX *libctx, const char *propq,
133                                   const UI_METHOD *ui_method, void *ui_data,
134                                   OSSL_STORE_post_process_info_fn post_process,
135                                   void *post_process_data);
136
137 /*-
138  *  Extracting OpenSSL types from and creating new OSSL_STORE_INFOs
139  *  ---------------------------------------------------------------
140  */
141
142 /*
143  * Types of data that can be ossl_stored in a OSSL_STORE_INFO.
144  * OSSL_STORE_INFO_NAME is typically found when getting a listing of
145  * available "files" / "tokens" / what have you.
146  */
147 # define OSSL_STORE_INFO_NAME           1   /* char * */
148 # define OSSL_STORE_INFO_PARAMS         2   /* EVP_PKEY * */
149 # define OSSL_STORE_INFO_PUBKEY         3   /* EVP_PKEY * */
150 # define OSSL_STORE_INFO_PKEY           4   /* EVP_PKEY * */
151 # define OSSL_STORE_INFO_CERT           5   /* X509 * */
152 # define OSSL_STORE_INFO_CRL            6   /* X509_CRL * */
153
154 /*
155  * Functions to generate OSSL_STORE_INFOs, one function for each type we
156  * support having in them, as well as a generic constructor.
157  *
158  * In all cases, ownership of the object is transferred to the OSSL_STORE_INFO
159  * and will therefore be freed when the OSSL_STORE_INFO is freed.
160  */
161 OSSL_STORE_INFO *OSSL_STORE_INFO_new(int type, void *data);
162 OSSL_STORE_INFO *OSSL_STORE_INFO_new_NAME(char *name);
163 int OSSL_STORE_INFO_set0_NAME_description(OSSL_STORE_INFO *info, char *desc);
164 OSSL_STORE_INFO *OSSL_STORE_INFO_new_PARAMS(EVP_PKEY *params);
165 OSSL_STORE_INFO *OSSL_STORE_INFO_new_PUBKEY(EVP_PKEY *pubkey);
166 OSSL_STORE_INFO *OSSL_STORE_INFO_new_PKEY(EVP_PKEY *pkey);
167 OSSL_STORE_INFO *OSSL_STORE_INFO_new_CERT(X509 *x509);
168 OSSL_STORE_INFO *OSSL_STORE_INFO_new_CRL(X509_CRL *crl);
169
170 /*
171  * Functions to try to extract data from a OSSL_STORE_INFO.
172  */
173 int OSSL_STORE_INFO_get_type(const OSSL_STORE_INFO *info);
174 void *OSSL_STORE_INFO_get0_data(int type, const OSSL_STORE_INFO *info);
175 const char *OSSL_STORE_INFO_get0_NAME(const OSSL_STORE_INFO *info);
176 char *OSSL_STORE_INFO_get1_NAME(const OSSL_STORE_INFO *info);
177 const char *OSSL_STORE_INFO_get0_NAME_description(const OSSL_STORE_INFO *info);
178 char *OSSL_STORE_INFO_get1_NAME_description(const OSSL_STORE_INFO *info);
179 EVP_PKEY *OSSL_STORE_INFO_get0_PARAMS(const OSSL_STORE_INFO *info);
180 EVP_PKEY *OSSL_STORE_INFO_get1_PARAMS(const OSSL_STORE_INFO *info);
181 EVP_PKEY *OSSL_STORE_INFO_get0_PUBKEY(const OSSL_STORE_INFO *info);
182 EVP_PKEY *OSSL_STORE_INFO_get1_PUBKEY(const OSSL_STORE_INFO *info);
183 EVP_PKEY *OSSL_STORE_INFO_get0_PKEY(const OSSL_STORE_INFO *info);
184 EVP_PKEY *OSSL_STORE_INFO_get1_PKEY(const OSSL_STORE_INFO *info);
185 X509 *OSSL_STORE_INFO_get0_CERT(const OSSL_STORE_INFO *info);
186 X509 *OSSL_STORE_INFO_get1_CERT(const OSSL_STORE_INFO *info);
187 X509_CRL *OSSL_STORE_INFO_get0_CRL(const OSSL_STORE_INFO *info);
188 X509_CRL *OSSL_STORE_INFO_get1_CRL(const OSSL_STORE_INFO *info);
189
190 const char *OSSL_STORE_INFO_type_string(int type);
191
192 /*
193  * Free the OSSL_STORE_INFO
194  */
195 void OSSL_STORE_INFO_free(OSSL_STORE_INFO *info);
196
197
198 /*-
199  *  Functions to construct a search URI from a base URI and search criteria
200  *  -----------------------------------------------------------------------
201  */
202
203 /* OSSL_STORE search types */
204 # define OSSL_STORE_SEARCH_BY_NAME              1 /* subject in certs, issuer in CRLs */
205 # define OSSL_STORE_SEARCH_BY_ISSUER_SERIAL     2
206 # define OSSL_STORE_SEARCH_BY_KEY_FINGERPRINT   3
207 # define OSSL_STORE_SEARCH_BY_ALIAS             4
208
209 /* To check what search types the scheme handler supports */
210 int OSSL_STORE_supports_search(OSSL_STORE_CTX *ctx, int search_type);
211
212 /* Search term constructors */
213 /*
214  * The input is considered to be owned by the caller, and must therefore
215  * remain present throughout the lifetime of the returned OSSL_STORE_SEARCH
216  */
217 OSSL_STORE_SEARCH *OSSL_STORE_SEARCH_by_name(X509_NAME *name);
218 OSSL_STORE_SEARCH *OSSL_STORE_SEARCH_by_issuer_serial(X509_NAME *name,
219                                                       const ASN1_INTEGER
220                                                       *serial);
221 OSSL_STORE_SEARCH *OSSL_STORE_SEARCH_by_key_fingerprint(const EVP_MD *digest,
222                                                         const unsigned char
223                                                         *bytes, size_t len);
224 OSSL_STORE_SEARCH *OSSL_STORE_SEARCH_by_alias(const char *alias);
225
226 /* Search term destructor */
227 void OSSL_STORE_SEARCH_free(OSSL_STORE_SEARCH *search);
228
229 /* Search term accessors */
230 int OSSL_STORE_SEARCH_get_type(const OSSL_STORE_SEARCH *criterion);
231 X509_NAME *OSSL_STORE_SEARCH_get0_name(const OSSL_STORE_SEARCH *criterion);
232 const ASN1_INTEGER *OSSL_STORE_SEARCH_get0_serial(const OSSL_STORE_SEARCH
233                                                   *criterion);
234 const unsigned char *OSSL_STORE_SEARCH_get0_bytes(const OSSL_STORE_SEARCH
235                                                   *criterion, size_t *length);
236 const char *OSSL_STORE_SEARCH_get0_string(const OSSL_STORE_SEARCH *criterion);
237 const EVP_MD *OSSL_STORE_SEARCH_get0_digest(const OSSL_STORE_SEARCH *criterion);
238
239 /*
240  * Add search criterion and expected return type (which can be unspecified)
241  * to the loading channel.  This MUST happen before the first OSSL_STORE_load().
242  */
243 int OSSL_STORE_expect(OSSL_STORE_CTX *ctx, int expected_type);
244 int OSSL_STORE_find(OSSL_STORE_CTX *ctx, const OSSL_STORE_SEARCH *search);
245
246
247 /*-
248  *  Function to fetch a loader and extract data from it
249  *  ---------------------------------------------------
250  */
251
252 typedef struct ossl_store_loader_st OSSL_STORE_LOADER;
253
254 OSSL_STORE_LOADER *OSSL_STORE_LOADER_fetch(const char *scheme,
255                                            OSSL_LIB_CTX *libctx,
256                                            const char *properties);
257 int OSSL_STORE_LOADER_up_ref(OSSL_STORE_LOADER *loader);
258 void OSSL_STORE_LOADER_free(OSSL_STORE_LOADER *loader);
259 const OSSL_PROVIDER *OSSL_STORE_LOADER_provider(const OSSL_STORE_LOADER *
260                                                 loader);
261 const char *OSSL_STORE_LOADER_properties(const OSSL_STORE_LOADER *loader);
262 int OSSL_STORE_LOADER_number(const OSSL_STORE_LOADER *loader);
263 const char *OSSL_STORE_LOADER_description(const OSSL_STORE_LOADER *loader);
264 int OSSL_STORE_LOADER_is_a(const OSSL_STORE_LOADER *loader,
265                            const char *scheme);
266 void OSSL_STORE_LOADER_do_all_provided(OSSL_LIB_CTX *libctx,
267                                        void (*fn)(OSSL_STORE_LOADER *loader,
268                                                   void *arg),
269                                        void *arg);
270 int OSSL_STORE_LOADER_names_do_all(const OSSL_STORE_LOADER *loader,
271                                    void (*fn)(const char *name, void *data),
272                                    void *data);
273
274 /*-
275  *  Function to register a loader for the given URI scheme.
276  *  -------------------------------------------------------
277  *
278  *  The loader receives all the main components of an URI except for the
279  *  scheme.
280  */
281
282 # ifndef OPENSSL_NO_DEPRECATED_3_0
283
284 /* struct ossl_store_loader_ctx_st is defined differently by each loader */
285 typedef struct ossl_store_loader_ctx_st OSSL_STORE_LOADER_CTX;
286 typedef OSSL_STORE_LOADER_CTX *(*OSSL_STORE_open_fn)
287     (const OSSL_STORE_LOADER *loader, const char *uri,
288      const UI_METHOD *ui_method, void *ui_data);
289 typedef OSSL_STORE_LOADER_CTX *(*OSSL_STORE_open_ex_fn)
290     (const OSSL_STORE_LOADER *loader,
291      const char *uri, OSSL_LIB_CTX *libctx, const char *propq,
292      const UI_METHOD *ui_method, void *ui_data);
293
294 typedef OSSL_STORE_LOADER_CTX *(*OSSL_STORE_attach_fn)
295     (const OSSL_STORE_LOADER *loader, BIO *bio,
296      OSSL_LIB_CTX *libctx, const char *propq,
297      const UI_METHOD *ui_method, void *ui_data);
298 typedef int (*OSSL_STORE_ctrl_fn)
299     (OSSL_STORE_LOADER_CTX *ctx, int cmd, va_list args);
300 typedef int (*OSSL_STORE_expect_fn)
301     (OSSL_STORE_LOADER_CTX *ctx, int expected);
302 typedef int (*OSSL_STORE_find_fn)
303     (OSSL_STORE_LOADER_CTX *ctx, const OSSL_STORE_SEARCH *criteria);
304 typedef OSSL_STORE_INFO *(*OSSL_STORE_load_fn)
305     (OSSL_STORE_LOADER_CTX *ctx, const UI_METHOD *ui_method, void *ui_data);
306 typedef int (*OSSL_STORE_eof_fn)(OSSL_STORE_LOADER_CTX *ctx);
307 typedef int (*OSSL_STORE_error_fn)(OSSL_STORE_LOADER_CTX *ctx);
308 typedef int (*OSSL_STORE_close_fn)(OSSL_STORE_LOADER_CTX *ctx);
309
310 # endif
311 # ifndef OPENSSL_NO_DEPRECATED_3_0
312 OSSL_DEPRECATEDIN_3_0
313 OSSL_STORE_LOADER *OSSL_STORE_LOADER_new(ENGINE *e, const char *scheme);
314 OSSL_DEPRECATEDIN_3_0
315 int OSSL_STORE_LOADER_set_open(OSSL_STORE_LOADER *loader,
316                                OSSL_STORE_open_fn open_function);
317 OSSL_DEPRECATEDIN_3_0
318 int OSSL_STORE_LOADER_set_open_ex(OSSL_STORE_LOADER *loader,
319                                   OSSL_STORE_open_ex_fn open_ex_function);
320 OSSL_DEPRECATEDIN_3_0
321 int OSSL_STORE_LOADER_set_attach(OSSL_STORE_LOADER *loader,
322                                  OSSL_STORE_attach_fn attach_function);
323 OSSL_DEPRECATEDIN_3_0
324 int OSSL_STORE_LOADER_set_ctrl(OSSL_STORE_LOADER *loader,
325                                OSSL_STORE_ctrl_fn ctrl_function);
326 OSSL_DEPRECATEDIN_3_0
327 int OSSL_STORE_LOADER_set_expect(OSSL_STORE_LOADER *loader,
328                                  OSSL_STORE_expect_fn expect_function);
329 OSSL_DEPRECATEDIN_3_0
330 int OSSL_STORE_LOADER_set_find(OSSL_STORE_LOADER *loader,
331                                OSSL_STORE_find_fn find_function);
332 OSSL_DEPRECATEDIN_3_0
333 int OSSL_STORE_LOADER_set_load(OSSL_STORE_LOADER *loader,
334                                OSSL_STORE_load_fn load_function);
335 OSSL_DEPRECATEDIN_3_0
336 int OSSL_STORE_LOADER_set_eof(OSSL_STORE_LOADER *loader,
337                               OSSL_STORE_eof_fn eof_function);
338 OSSL_DEPRECATEDIN_3_0
339 int OSSL_STORE_LOADER_set_error(OSSL_STORE_LOADER *loader,
340                                 OSSL_STORE_error_fn error_function);
341 OSSL_DEPRECATEDIN_3_0
342 int OSSL_STORE_LOADER_set_close(OSSL_STORE_LOADER *loader,
343                                 OSSL_STORE_close_fn close_function);
344 OSSL_DEPRECATEDIN_3_0
345 const ENGINE *OSSL_STORE_LOADER_get0_engine(const OSSL_STORE_LOADER *loader);
346 OSSL_DEPRECATEDIN_3_0
347 const char * OSSL_STORE_LOADER_get0_scheme(const OSSL_STORE_LOADER *loader);
348 OSSL_DEPRECATEDIN_3_0
349 int OSSL_STORE_register_loader(OSSL_STORE_LOADER *loader);
350 OSSL_DEPRECATEDIN_3_0
351 OSSL_STORE_LOADER *OSSL_STORE_unregister_loader(const char *scheme);
352 # endif
353
354 /*-
355  *  Functions to list STORE loaders
356  *  -------------------------------
357  */
358 # ifndef OPENSSL_NO_DEPRECATED_3_0
359 OSSL_DEPRECATEDIN_3_0
360 int OSSL_STORE_do_all_loaders(void (*do_function)(const OSSL_STORE_LOADER *loader,
361                                                   void *do_arg),
362                               void *do_arg);
363 # endif
364
365 # ifdef  __cplusplus
366 }
367 # endif
368 #endif