]> git.ipfire.org Git - thirdparty/openssl.git/blame - include/openssl/store.h
Don't hold a lock when calling a callback in ossl_namemap_doall_names
[thirdparty/openssl.git] / include / openssl / store.h
CommitLineData
71a5516d 1/*
454afd98 2 * Copyright 2016-2020 The OpenSSL Project Authors. All Rights Reserved.
71a5516d 3 *
48f4ad77 4 * Licensed under the Apache License 2.0 (the "License"). You may not use
71a5516d
RL
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
ae4186b0
DMSP
10#ifndef OPENSSL_STORE_H
11# define OPENSSL_STORE_H
d86167ec
DMSP
12# pragma once
13
14# include <openssl/macros.h>
936c2b9e 15# ifndef OPENSSL_NO_DEPRECATED_3_0
d86167ec
DMSP
16# define HEADER_OSSL_STORE_H
17# endif
71a5516d
RL
18
19# include <stdarg.h>
50cd4768 20# include <openssl/types.h>
71a5516d
RL
21# include <openssl/pem.h>
22# include <openssl/storeerr.h>
23
24# ifdef __cplusplus
25extern "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
37typedef 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 */
44typedef 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 */
34b80d06
RL
55OSSL_STORE_CTX *
56OSSL_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);
59OSSL_STORE_CTX *
b4250010 60OSSL_STORE_open_ex(const char *uri, OSSL_LIB_CTX *libctx, const char *propq,
d8652be0
MC
61 const UI_METHOD *ui_method, void *ui_data,
62 OSSL_STORE_post_process_info_fn post_process,
63 void *post_process_data);
6725682d 64
71a5516d
RL
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 */
f2e94543
RL
71# ifndef OPENSSL_NO_DEPRECATED_3_0
72OSSL_DEPRECATEDIN_3_0 int OSSL_STORE_ctrl(OSSL_STORE_CTX *ctx, int cmd,
73 ... /* args */);
74OSSL_DEPRECATEDIN_3_0 int OSSL_STORE_vctrl(OSSL_STORE_CTX *ctx, int cmd,
75 va_list args);
76# endif
a1447076
RL
77
78# ifndef OPENSSL_NO_DEPRECATED_3_0
71a5516d
RL
79
80/*
81 * Common ctrl commands that different loaders may choose to support.
82 */
7852f588
RL
83/* int on = 0 or 1; STORE_ctrl(ctx, STORE_C_USE_SECMEM, &on); */
84# define OSSL_STORE_C_USE_SECMEM 1
71a5516d
RL
85/* Where custom commands start */
86# define OSSL_STORE_C_CUSTOM_START 100
87
a1447076
RL
88# endif
89
71a5516d
RL
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 */
98OSSL_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 */
104int OSSL_STORE_eof(OSSL_STORE_CTX *ctx);
105
106/*
68756b12 107 * Check if an error occurred
71a5516d
RL
108 * Returns 1 if it did, 0 otherwise.
109 */
110int OSSL_STORE_error(OSSL_STORE_CTX *ctx);
111
112/*
113 * Close the channel
114 * Returns 1 on success, 0 on error.
115 */
116int OSSL_STORE_close(OSSL_STORE_CTX *ctx);
117
6ab6ecfd
RL
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 */
6725682d 131OSSL_STORE_CTX *OSSL_STORE_attach(BIO *bio, const char *scheme,
b4250010 132 OSSL_LIB_CTX *libctx, const char *propq,
6ab6ecfd
RL
133 const UI_METHOD *ui_method, void *ui_data,
134 OSSL_STORE_post_process_info_fn post_process,
135 void *post_process_data);
71a5516d
RL
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 * */
2274d22d
RL
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 * */
71a5516d
RL
153
154/*
155 * Functions to generate OSSL_STORE_INFOs, one function for each type we
1fb2993d 156 * support having in them, as well as a generic constructor.
71a5516d 157 *
68756b12 158 * In all cases, ownership of the object is transferred to the OSSL_STORE_INFO
71a5516d
RL
159 * and will therefore be freed when the OSSL_STORE_INFO is freed.
160 */
16feca71 161OSSL_STORE_INFO *OSSL_STORE_INFO_new(int type, void *data);
71a5516d
RL
162OSSL_STORE_INFO *OSSL_STORE_INFO_new_NAME(char *name);
163int OSSL_STORE_INFO_set0_NAME_description(OSSL_STORE_INFO *info, char *desc);
164OSSL_STORE_INFO *OSSL_STORE_INFO_new_PARAMS(EVP_PKEY *params);
2274d22d 165OSSL_STORE_INFO *OSSL_STORE_INFO_new_PUBKEY(EVP_PKEY *pubkey);
71a5516d
RL
166OSSL_STORE_INFO *OSSL_STORE_INFO_new_PKEY(EVP_PKEY *pkey);
167OSSL_STORE_INFO *OSSL_STORE_INFO_new_CERT(X509 *x509);
168OSSL_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 */
173int OSSL_STORE_INFO_get_type(const OSSL_STORE_INFO *info);
16feca71 174void *OSSL_STORE_INFO_get0_data(int type, const OSSL_STORE_INFO *info);
71a5516d
RL
175const char *OSSL_STORE_INFO_get0_NAME(const OSSL_STORE_INFO *info);
176char *OSSL_STORE_INFO_get1_NAME(const OSSL_STORE_INFO *info);
177const char *OSSL_STORE_INFO_get0_NAME_description(const OSSL_STORE_INFO *info);
178char *OSSL_STORE_INFO_get1_NAME_description(const OSSL_STORE_INFO *info);
179EVP_PKEY *OSSL_STORE_INFO_get0_PARAMS(const OSSL_STORE_INFO *info);
180EVP_PKEY *OSSL_STORE_INFO_get1_PARAMS(const OSSL_STORE_INFO *info);
2274d22d
RL
181EVP_PKEY *OSSL_STORE_INFO_get0_PUBKEY(const OSSL_STORE_INFO *info);
182EVP_PKEY *OSSL_STORE_INFO_get1_PUBKEY(const OSSL_STORE_INFO *info);
71a5516d
RL
183EVP_PKEY *OSSL_STORE_INFO_get0_PKEY(const OSSL_STORE_INFO *info);
184EVP_PKEY *OSSL_STORE_INFO_get1_PKEY(const OSSL_STORE_INFO *info);
185X509 *OSSL_STORE_INFO_get0_CERT(const OSSL_STORE_INFO *info);
186X509 *OSSL_STORE_INFO_get1_CERT(const OSSL_STORE_INFO *info);
187X509_CRL *OSSL_STORE_INFO_get0_CRL(const OSSL_STORE_INFO *info);
188X509_CRL *OSSL_STORE_INFO_get1_CRL(const OSSL_STORE_INFO *info);
189
190const char *OSSL_STORE_INFO_type_string(int type);
191
192/*
193 * Free the OSSL_STORE_INFO
194 */
195void OSSL_STORE_INFO_free(OSSL_STORE_INFO *info);
196
fac8673b
RL
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 */
210int 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 */
217OSSL_STORE_SEARCH *OSSL_STORE_SEARCH_by_name(X509_NAME *name);
218OSSL_STORE_SEARCH *OSSL_STORE_SEARCH_by_issuer_serial(X509_NAME *name,
219 const ASN1_INTEGER
220 *serial);
221OSSL_STORE_SEARCH *OSSL_STORE_SEARCH_by_key_fingerprint(const EVP_MD *digest,
222 const unsigned char
223 *bytes, size_t len);
224OSSL_STORE_SEARCH *OSSL_STORE_SEARCH_by_alias(const char *alias);
225
226/* Search term destructor */
227void OSSL_STORE_SEARCH_free(OSSL_STORE_SEARCH *search);
228
229/* Search term accessors */
230int OSSL_STORE_SEARCH_get_type(const OSSL_STORE_SEARCH *criterion);
e3c4ad28 231X509_NAME *OSSL_STORE_SEARCH_get0_name(const OSSL_STORE_SEARCH *criterion);
fac8673b
RL
232const ASN1_INTEGER *OSSL_STORE_SEARCH_get0_serial(const OSSL_STORE_SEARCH
233 *criterion);
234const unsigned char *OSSL_STORE_SEARCH_get0_bytes(const OSSL_STORE_SEARCH
235 *criterion, size_t *length);
236const char *OSSL_STORE_SEARCH_get0_string(const OSSL_STORE_SEARCH *criterion);
237const EVP_MD *OSSL_STORE_SEARCH_get0_digest(const OSSL_STORE_SEARCH *criterion);
238
072bfcc9 239/*
fac8673b
RL
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().
072bfcc9
RL
242 */
243int OSSL_STORE_expect(OSSL_STORE_CTX *ctx, int expected_type);
e3c4ad28 244int OSSL_STORE_find(OSSL_STORE_CTX *ctx, const OSSL_STORE_SEARCH *search);
072bfcc9 245
71a5516d 246
c4fc564d
RL
247/*-
248 * Function to fetch a loader and extract data from it
249 * ---------------------------------------------------
250 */
251
252typedef struct ossl_store_loader_st OSSL_STORE_LOADER;
253
254OSSL_STORE_LOADER *OSSL_STORE_LOADER_fetch(const char *scheme,
b4250010 255 OSSL_LIB_CTX *libctx,
c4fc564d
RL
256 const char *properties);
257int OSSL_STORE_LOADER_up_ref(OSSL_STORE_LOADER *loader);
258void OSSL_STORE_LOADER_free(OSSL_STORE_LOADER *loader);
259const OSSL_PROVIDER *OSSL_STORE_LOADER_provider(const OSSL_STORE_LOADER *
260 loader);
261const char *OSSL_STORE_LOADER_properties(const OSSL_STORE_LOADER *loader);
262int OSSL_STORE_LOADER_number(const OSSL_STORE_LOADER *loader);
263int OSSL_STORE_LOADER_is_a(const OSSL_STORE_LOADER *loader,
264 const char *scheme);
b4250010 265void OSSL_STORE_LOADER_do_all_provided(OSSL_LIB_CTX *libctx,
c4fc564d
RL
266 void (*fn)(OSSL_STORE_LOADER *loader,
267 void *arg),
268 void *arg);
d84f5515
MC
269int OSSL_STORE_LOADER_names_do_all(const OSSL_STORE_LOADER *loader,
270 void (*fn)(const char *name, void *data),
271 void *data);
c4fc564d 272
71a5516d
RL
273/*-
274 * Function to register a loader for the given URI scheme.
275 * -------------------------------------------------------
276 *
277 * The loader receives all the main components of an URI except for the
278 * scheme.
279 */
280
a1447076
RL
281# ifndef OPENSSL_NO_DEPRECATED_3_0
282
71a5516d
RL
283/* struct ossl_store_loader_ctx_st is defined differently by each loader */
284typedef struct ossl_store_loader_ctx_st OSSL_STORE_LOADER_CTX;
6725682d
SL
285typedef OSSL_STORE_LOADER_CTX *(*OSSL_STORE_open_fn)
286 (const OSSL_STORE_LOADER *loader, const char *uri,
287 const UI_METHOD *ui_method, void *ui_data);
d8652be0 288typedef OSSL_STORE_LOADER_CTX *(*OSSL_STORE_open_ex_fn)
6725682d 289 (const OSSL_STORE_LOADER *loader,
b4250010 290 const char *uri, OSSL_LIB_CTX *libctx, const char *propq,
6725682d
SL
291 const UI_METHOD *ui_method, void *ui_data);
292
c4fc564d
RL
293typedef OSSL_STORE_LOADER_CTX *(*OSSL_STORE_attach_fn)
294 (const OSSL_STORE_LOADER *loader, BIO *bio,
b4250010 295 OSSL_LIB_CTX *libctx, const char *propq,
c4fc564d
RL
296 const UI_METHOD *ui_method, void *ui_data);
297typedef int (*OSSL_STORE_ctrl_fn)
298 (OSSL_STORE_LOADER_CTX *ctx, int cmd, va_list args);
299typedef int (*OSSL_STORE_expect_fn)
300 (OSSL_STORE_LOADER_CTX *ctx, int expected);
301typedef int (*OSSL_STORE_find_fn)
302 (OSSL_STORE_LOADER_CTX *ctx, const OSSL_STORE_SEARCH *criteria);
303typedef OSSL_STORE_INFO *(*OSSL_STORE_load_fn)
304 (OSSL_STORE_LOADER_CTX *ctx, const UI_METHOD *ui_method, void *ui_data);
305typedef int (*OSSL_STORE_eof_fn)(OSSL_STORE_LOADER_CTX *ctx);
306typedef int (*OSSL_STORE_error_fn)(OSSL_STORE_LOADER_CTX *ctx);
307typedef int (*OSSL_STORE_close_fn)(OSSL_STORE_LOADER_CTX *ctx);
308
a1447076 309# endif
f2e94543
RL
310# ifndef OPENSSL_NO_DEPRECATED_3_0
311OSSL_DEPRECATEDIN_3_0
312OSSL_STORE_LOADER *OSSL_STORE_LOADER_new(ENGINE *e, const char *scheme);
313OSSL_DEPRECATEDIN_3_0
314int OSSL_STORE_LOADER_set_open(OSSL_STORE_LOADER *loader,
315 OSSL_STORE_open_fn open_function);
316OSSL_DEPRECATEDIN_3_0
317int OSSL_STORE_LOADER_set_open_ex(OSSL_STORE_LOADER *loader,
318 OSSL_STORE_open_ex_fn open_ex_function);
319OSSL_DEPRECATEDIN_3_0
320int OSSL_STORE_LOADER_set_attach(OSSL_STORE_LOADER *loader,
321 OSSL_STORE_attach_fn attach_function);
322OSSL_DEPRECATEDIN_3_0
323int OSSL_STORE_LOADER_set_ctrl(OSSL_STORE_LOADER *loader,
324 OSSL_STORE_ctrl_fn ctrl_function);
325OSSL_DEPRECATEDIN_3_0
326int OSSL_STORE_LOADER_set_expect(OSSL_STORE_LOADER *loader,
327 OSSL_STORE_expect_fn expect_function);
328OSSL_DEPRECATEDIN_3_0
329int OSSL_STORE_LOADER_set_find(OSSL_STORE_LOADER *loader,
330 OSSL_STORE_find_fn find_function);
331OSSL_DEPRECATEDIN_3_0
332int OSSL_STORE_LOADER_set_load(OSSL_STORE_LOADER *loader,
333 OSSL_STORE_load_fn load_function);
334OSSL_DEPRECATEDIN_3_0
335int OSSL_STORE_LOADER_set_eof(OSSL_STORE_LOADER *loader,
336 OSSL_STORE_eof_fn eof_function);
337OSSL_DEPRECATEDIN_3_0
338int OSSL_STORE_LOADER_set_error(OSSL_STORE_LOADER *loader,
339 OSSL_STORE_error_fn error_function);
340OSSL_DEPRECATEDIN_3_0
341int OSSL_STORE_LOADER_set_close(OSSL_STORE_LOADER *loader,
342 OSSL_STORE_close_fn close_function);
343OSSL_DEPRECATEDIN_3_0
344const ENGINE *OSSL_STORE_LOADER_get0_engine(const OSSL_STORE_LOADER *loader);
345OSSL_DEPRECATEDIN_3_0
346const char * OSSL_STORE_LOADER_get0_scheme(const OSSL_STORE_LOADER *loader);
347OSSL_DEPRECATEDIN_3_0
348int OSSL_STORE_register_loader(OSSL_STORE_LOADER *loader);
349OSSL_DEPRECATEDIN_3_0
350OSSL_STORE_LOADER *OSSL_STORE_unregister_loader(const char *scheme);
351# endif
71a5516d 352
f91ded1f
RL
353/*-
354 * Functions to list STORE loaders
355 * -------------------------------
356 */
f2e94543
RL
357# ifndef OPENSSL_NO_DEPRECATED_3_0
358OSSL_DEPRECATEDIN_3_0
359int OSSL_STORE_do_all_loaders(void (*do_function)(const OSSL_STORE_LOADER *loader,
360 void *do_arg),
361 void *do_arg);
362# endif
f91ded1f 363
71a5516d
RL
364# ifdef __cplusplus
365}
366# endif
367#endif