Replumbing: Add include/openssl/core.h, initially with core types
[openssl.git] / doc / man3 / OSSL_PARAM.pod
1 =pod
2
3 =head1 NAME
4
5 OSSL_PARAM - a structure to pass or request object parameters
6
7 =head1 SYNOPSIS
8
9  #include <openssl/core.h>
10
11  typedef struct ossl_param_st OSSL_PARAM;
12  struct ossl_param_st {
13      const char *key;             /* the name of the parameter */
14      unsigned char data_type;     /* declare what kind of content is in buffer */
15      void *buffer;                /* value being passed in or out */
16      size_t buffer_size;          /* buffer size */
17      size_t *return_size;         /* OPTIONAL: address to content size */
18  };
19
20 =head1 DESCRIPTION
21
22 C<OSSL_PARAM> is a type that allows passing arbitrary data for some
23 object between two parties that have no or very little shared
24 knowledge about their respective internal structures for that object.
25
26 A typical usage example could be an application that wants to set some
27 parameters for an object, or wants to find out some parameters of an
28 object.
29
30 Arrays of this type can be used for two purposes:
31
32 =over 4
33
34 =item *
35
36 Setting parameters for some object.
37 The caller sets up the C<OSSL_PARAM> array and calls some function
38 (the I<setter>) that has intimate knowledge about the object that can
39 take the data from the C<OSSL_PARAM> array and assign them in a
40 suitable form for the internal structure of the object.
41
42 =item *
43
44 Request parameters of some object.
45 The caller (the I<requestor>) sets up the C<OSSL_PARAM> array and
46 calls some function (the I<responder>) that has intimate knowledge
47 about the object, which can take the internal data of the object and
48 copy (possibly convert) that to the buffers prepared by the
49 I<requestor>.
50
51 =back
52
53 =head2 C<OSSL_PARAM> fields
54
55 =over 4
56
57 =item C<key>
58
59 The identity of the parameter in the form of a string.
60
61 =item C<data_type>
62
63 =for comment It's still debated if this field should be present, or if
64 the type should always be implied by how it's used.
65 Either way, these data types will have to be passed together with the
66 names as an array of OSSL_ITEM, for discovery purposes.
67
68 The C<data_type> is a value that describes the type and organization of
69 the data.
70 See L</Supported types> below for a description of the types.
71
72 =item C<buffer>
73
74 =item C<buffer_size>
75
76 C<buffer> is a pointer to the memory where the parameter data is (when
77 setting parameters) or shall (when requesting parameters) be stored,
78 and C<buffer_size> is its size in bytes.
79 The organization of the data depends on the parameter type and flag.
80
81 =item C<return_size>
82
83 When an array of C<OSSL_PARAM> is used to request data, the
84 I<responder> must set this field to indicate the actual size of the
85 parameter data.
86 In case the C<buffer_size> is too small for the data, the I<responder>
87 must still set this field to indicate the minimum buffer size
88 required.
89
90 =back
91
92 B<NOTE:>
93
94 The key names and associated types are defined by the entity that
95 offers these parameters, i.e. names for parameters provided by the
96 OpenSSL libraries are defined by the libraries, and names for
97 parameters provided by providers are defined by those providers,
98 except for the pointer form of strings (see data type descriptions
99 below).
100 Entities that want to set or request parameters need to know what
101 those keys are and of what type, any functionality between those two
102 entities should remain oblivious and just pass the C<OSSL_PARAM> array
103 along.
104
105 =head2 Supported types
106
107 The C<data_type> field can be one of the following types:
108
109 =over 4
110
111 =item C<OSSL_PARAM_INTEGER>
112
113 =item C<OSSL_PARAM_UNSIGNED_INTEGER>
114
115 The parameter data is an integer (signed or unsigned) of arbitrary
116 length, organized in native form, i.e. most significant byte first on
117 Big-Endian systems, and least significant byte first on Little-Endian
118 systems.
119
120 =item C<OSSL_PARAM_REAL>
121
122 =for comment It's still debated if we need this or not.
123
124 The parameter data is a floating point value in native form.
125
126 =item C<OSSL_PARAM_UTF8_STRING>
127
128 The parameter data is a printable string.
129
130 =item C<OSSL_PARAM_OCTET_STRING>
131
132 The parameter data is an arbitrary string of bytes.
133
134 =back
135
136 Additionally, this flag can be added to any type:
137
138 =over 4
139
140 =item C<OSSL_PARAM_POINTER_FLAG>
141
142 With this flag, C<buffer> doesn't point directly at the data, but at a
143 pointer that points at the data.
144
145 This can be used to indicate that constant data is or will be passed,
146 and there is therefore no need to copy the data that is passed, just
147 the pointer to it.
148
149 If an C<OSSL_PARAM> with this flag set is used to set a parameter,
150 C<buffer_size> must be set to the size of the data, not the size of
151 the pointer to the data.
152
153 If this C<OSSL_PARAM> is used in a parameter request, C<buffer_size>
154 is not relevant.
155 However, the I<responder> will set C<*return_size> to the size of the
156 data (again, not the size of the pointer to the data).
157
158 Note that the use of this flag is B<fragile> and can only be safely
159 used for data that remains constant and in a constant location for a
160 long enough duration (such as the life-time of the entity that
161 offers these parameters).
162
163 =back
164
165 For convenience, these types are provided:
166
167 =over 4
168
169 =item C<OSSL_PARAM_UTF8_STRING_PTR>
170
171 =item C<OSSL_PARAM_OCTET_STRING_PTR>
172
173 These are combinations of C<OSSL_PARAM_UTF8_STRING> as well as
174 C<OSSL_PARAM_OCTET_STRING> with C<OSSL_PARAM_POINTER_FLAG>.
175
176 =back
177
178 =head1 NOTES
179
180 Both when setting and requesting parameters, the functions that are
181 called will have to decide what is and what is not an error.
182 The recommended behaviour is:
183
184 =over 4
185
186 =item *
187
188 Keys that a I<setter> or I<responder> doesn't recognise should simply
189 be ignored.
190 That in itself isn't an error.
191
192 =item *
193
194 If the keys that a called I<setter> recognises form a consistent
195 enough set of data, that call should succeed.
196
197 =item *
198
199 A I<responder> must never change the fields of an C<OSSL_PARAM>, it
200 may only change the contents of the buffers that C<buffer> and
201 C<return_size> point at.
202
203 =item *
204
205 If the data type for a key that it's associated with is incorrect,
206 the called function may return an error.
207
208 The called function may also try to convert the data to a suitable
209 form (for example, it's plausible to pass a large number as an octet
210 string, so even though a given key is defined as an
211 C<OSSL_PARAM_UNSIGNED_INTEGER>, is plausible to pass the value as an
212 C<OSSL_PARAM_OCTET_STRING>), but this is in no way mandatory.
213
214 =item *
215
216 If a I<responder> finds that some buffers are too small for the
217 requested data, it must set C<*return_size> for each such
218 C<OSSL_PARAM> item to the required size, and eventually return an
219 error.
220
221 =back
222
223 =begin comment RETURN VALUES doesn't make sense for a manual that only
224 describes a type, but document checkers still want that section, and
225 to have more than just the section title.
226
227 =head1 RETURN VALUES
228
229 txt
230
231 =end comment
232
233 =head1 EXAMPLES
234
235 A couple of examples to just show how C<OSSL_PARAM> arrays could be
236 set up.
237
238 =head3 Example 1
239
240 This example is for setting parameters on some object:
241
242     #include <openssl/core.h>
243
244     const char *foo = "some string";
245     size_t foo_l = strlen(foo) + 1;
246     const char bar[] = "some other string";
247     const OSSL_PARAM set[] = {
248         { "foo", OSSL_PARAM_UTF8_STRING_PTR, &foo, foo_l, NULL },
249         { "bar", OSSL_PARAM_UTF8_STRING, &bar, sizeof(bar), NULL },
250         { NULL, 0, NULL, 0, NULL }
251     };
252
253 =head3 Example 2
254
255 This example is for requesting parameters on some object:
256
257     const char *foo = NULL;
258     size_t foo_l;
259     char bar[1024];
260     size_t bar_l;
261     const OSSL_PARAM request[] = {
262         { "foo", OSSL_PARAM_UTF8_STRING_PTR, &foo, 0 /*irrelevant*/, &foo_l },
263         { "bar", OSSL_PARAM_UTF8_STRING, &bar, sizeof(bar), &bar_l },
264         { NULL, 0, NULL, 0, NULL }
265     };
266
267 A I<responder> that receives this array (as C<params> in this example)
268 could fill in the parameters like this:
269
270     /* const OSSL_PARAM *params */
271
272     int i;
273
274     for (i = 0; params[i].key != NULL; i++) {
275         if (strcmp(params[i].key, "foo") == 0) {
276             *(char **)params[i].buffer = "foo value";
277             *params[i].return_size = 10; /* size of "foo value" */
278         } else if (strcmp(params[i].key, "bar") == 0) {
279             memcpy(params[1].buffer, "bar value", 10);
280             *params[1].return_size = 10; /* size of "bar value" */
281         }
282         /* Ignore stuff we don't know */
283     }
284
285 =head1 SEE ALSO
286
287 L<openssl-core.h(7)>
288
289 =head1 HISTORY
290
291 C<OSSL_PARAM> was added in OpenSSL 3.0.
292
293 =head1 COPYRIGHT
294
295 Copyright 2019 The OpenSSL Project Authors. All Rights Reserved.
296
297 Licensed under the Apache License 2.0 (the "License").  You may not use
298 this file except in compliance with the License.  You can obtain a copy
299 in the file LICENSE in the source distribution or at
300 L<https://www.openssl.org/source/license.html>.
301
302 =cut