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