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