params: add OSSL_PARAM helpers for time_t.
[oweals/openssl.git] / doc / man3 / OSSL_PARAM_int.pod
1 =pod
2
3 =head1 NAME
4
5 OSSL_PARAM_double, OSSL_PARAM_int, OSSL_PARAM_int32, OSSL_PARAM_int64,
6 OSSL_PARAM_long, OSSL_PARAM_size_t, OSSL_PARAM_time_t, OSSL_PARAM_uint, OSSL_PARAM_uint32,
7 OSSL_PARAM_uint64, OSSL_PARAM_ulong, OSSL_PARAM_BN, OSSL_PARAM_utf8_string,
8 OSSL_PARAM_octet_string, OSSL_PARAM_utf8_ptr, OSSL_PARAM_octet_ptr,
9 OSSL_PARAM_END,
10 OSSL_PARAM_construct_double, OSSL_PARAM_construct_int,
11 OSSL_PARAM_construct_int32, OSSL_PARAM_construct_int64,
12 OSSL_PARAM_construct_long, OSSL_PARAM_construct_size_t, OSSL_PARAM_construct_time_t,
13 OSSL_PARAM_construct_uint, OSSL_PARAM_construct_uint32,
14 OSSL_PARAM_construct_uint64, OSSL_PARAM_construct_ulong,
15 OSSL_PARAM_construct_BN, OSSL_PARAM_construct_utf8_string,
16 OSSL_PARAM_construct_utf8_ptr, OSSL_PARAM_construct_octet_string,
17 OSSL_PARAM_construct_octet_ptr, OSSL_PARAM_construct_end,
18 OSSL_PARAM_locate, OSSL_PARAM_locate_const,
19 OSSL_PARAM_get_double, OSSL_PARAM_get_int, OSSL_PARAM_get_int32,
20 OSSL_PARAM_get_int64, OSSL_PARAM_get_long, OSSL_PARAM_get_size_t, OSSL_PARAM_get_time_t,
21 OSSL_PARAM_get_uint, OSSL_PARAM_get_uint32, OSSL_PARAM_get_uint64,
22 OSSL_PARAM_get_ulong, OSSL_PARAM_get_BN, OSSL_PARAM_get_utf8_string,
23 OSSL_PARAM_get_octet_string, OSSL_PARAM_get_utf8_ptr,
24 OSSL_PARAM_get_octet_ptr,
25 OSSL_PARAM_set_double, OSSL_PARAM_set_int, OSSL_PARAM_set_int32,
26 OSSL_PARAM_set_int64, OSSL_PARAM_set_long, OSSL_PARAM_set_size_t, OSSL_PARAM_set_time_t,
27 OSSL_PARAM_set_uint, OSSL_PARAM_set_uint32, OSSL_PARAM_set_uint64,
28 OSSL_PARAM_set_ulong, OSSL_PARAM_set_BN, OSSL_PARAM_set_utf8_string,
29 OSSL_PARAM_set_octet_string, OSSL_PARAM_set_utf8_ptr,
30 OSSL_PARAM_set_octet_ptr,
31 OSSL_PARAM_UNMODIFIED, OSSL_PARAM_modified, OSSL_PARAM_set_all_unmodified
32 - OSSL_PARAM helpers
33
34 =head1 SYNOPSIS
35
36 =for openssl generic
37
38  #include <openssl/params.h>
39
40  /*
41   * TYPE in function names is one of:
42   * double, int, int32, int64, long, size_t, time_t, uint, uint32, uint64, ulong
43   * Corresponding TYPE in function arguments is one of:
44   * double, int, int32_t, int64_t, long, size_t, time_t, unsigned int, uint32_t,
45   * uint64_t, unsigned long
46   */
47
48  #define OSSL_PARAM_TYPE(key, address)
49  #define OSSL_PARAM_BN(key, address, size)
50  #define OSSL_PARAM_utf8_string(key, address, size)
51  #define OSSL_PARAM_octet_string(key, address, size)
52  #define OSSL_PARAM_utf8_ptr(key, address, size)
53  #define OSSL_PARAM_octet_ptr(key, address, size)
54  #define OSSL_PARAM_END
55
56  #define OSSL_PARAM_UNMODIFIED
57
58  OSSL_PARAM OSSL_PARAM_construct_TYPE(const char *key, TYPE *buf);
59  OSSL_PARAM OSSL_PARAM_construct_BN(const char *key, unsigned char *buf,
60                                     size_t bsize);
61  OSSL_PARAM OSSL_PARAM_construct_utf8_string(const char *key, char *buf,
62                                              size_t bsize);
63  OSSL_PARAM OSSL_PARAM_construct_octet_string(const char *key, void *buf,
64                                               size_t bsize);
65  OSSL_PARAM OSSL_PARAM_construct_utf8_ptr(const char *key, char **buf,
66                                           size_t bsize);
67  OSSL_PARAM OSSL_PARAM_construct_octet_ptr(const char *key, void **buf,
68                                            size_t bsize);
69  OSSL_PARAM OSSL_PARAM_construct_end(void);
70
71  OSSL_PARAM *OSSL_PARAM_locate(OSSL_PARAM *array, const char *key);
72  const OSSL_PARAM *OSSL_PARAM_locate_const(const OSSL_PARAM *array,
73                                            const char *key);
74
75  int OSSL_PARAM_get_TYPE(const OSSL_PARAM *p, TYPE *val);
76  int OSSL_PARAM_set_TYPE(OSSL_PARAM *p, TYPE val);
77
78  int OSSL_PARAM_get_BN(const OSSL_PARAM *p, BIGNUM **val);
79  int OSSL_PARAM_set_BN(OSSL_PARAM *p, const BIGNUM *val);
80
81  int OSSL_PARAM_get_utf8_string(const OSSL_PARAM *p, char **val,
82                                 size_t max_len);
83  int OSSL_PARAM_set_utf8_string(OSSL_PARAM *p, const char *val);
84
85  int OSSL_PARAM_get_octet_string(const OSSL_PARAM *p, void **val,
86                                  size_t max_len, size_t *used_len);
87  int OSSL_PARAM_set_octet_string(OSSL_PARAM *p, const void *val, size_t len);
88
89  int OSSL_PARAM_get_utf8_ptr(const OSSL_PARAM *p, const char **val);
90  int OSSL_PARAM_set_utf8_ptr(OSSL_PARAM *p, const char *val);
91
92  int OSSL_PARAM_get_octet_ptr(const OSSL_PARAM *p, const void **val,
93                               size_t *used_len);
94  int OSSL_PARAM_set_octet_ptr(OSSL_PARAM *p, const void *val,
95                               size_t used_len);
96
97  int OSSL_PARAM_modified(const OSSL_PARAM *param);
98  void OSSL_PARAM_set_all_unmodified(OSSL_PARAM *params);
99
100 =head1 DESCRIPTION
101
102 A collection of utility functions that simplify and add type safety to the
103 OSSL_PARAM arrays.  The following B<TYPE> names are supported:
104
105 =over 1
106
107 =item *
108
109 double
110
111 =item *
112
113 int
114
115 =item *
116
117 int32 (int32_t)
118
119 =item *
120
121 int64 (int64_t)
122
123 =item *
124
125 long int (long)
126
127 =item *
128
129 time_t
130
131 =item *
132
133 size_t
134
135 =item *
136
137 uint32 (uint32_t)
138
139 =item *
140
141 uint64 (uint64_t)
142
143 =item *
144
145 unsigned int (uint)
146
147 =item *
148
149 unsigned long int (ulong)
150
151 =back
152
153 OSSL_PARAM_TYPE() are a series of macros designed to assist initialising an
154 array of OSSL_PARAM structures.
155 Each of these macros defines a parameter of the specified B<TYPE> with the
156 provided B<key> and parameter variable B<address>.
157
158 OSSL_PARAM_utf8_string(), OSSL_PARAM_octet_string(), OSSL_PARAM_utf8_ptr(),
159 OSSL_PARAM_octet_ptr(), OSSL_PARAM_BN() are macros that provide support
160 for defining UTF8 strings, OCTET strings and big numbers.
161 A parameter with name B<key> is defined.
162 The storage for this parameter is at B<address> and is of B<size> bytes.
163
164 OSSL_PARAM_END provides an end of parameter list marker.
165 This should terminate all OSSL_PARAM arrays.
166
167 OSSL_PARAM_construct_TYPE() are a series of functions that create OSSL_PARAM
168 records dynamically.
169 A parameter with name B<key> is created.
170 The parameter will use storage pointed to by B<buf> and return size of B<ret>.
171
172 OSSL_PARAM_construct_BN() is a function that constructs a large integer
173 OSSL_PARAM structure.
174 A parameter with name B<key>, storage B<buf>, size B<bsize> and return
175 size B<rsize> is created.
176
177 OSSL_PARAM_construct_utf8_string() is a function that constructs a UTF8
178 string OSSL_PARAM structure.
179 A parameter with name B<key>, storage B<buf> and size B<bsize> is created.
180 If B<bsize> is zero, the string length is determined using strlen(3) + 1 for the
181 null termination byte.
182 Generally pass zero for B<bsize> instead of calling strlen(3) yourself.
183
184 OSSL_PARAM_construct_octet_string() is a function that constructs an OCTET
185 string OSSL_PARAM structure.
186 A parameter with name B<key>, storage B<buf> and size B<bsize> is created.
187
188 OSSL_PARAM_construct_utf8_ptr() is a function that constructes a UTF string
189 pointer OSSL_PARAM structure.
190 A parameter with name B<key>, storage pointer B<*buf> and size B<bsize>
191 is created.
192
193 OSSL_PARAM_construct_octet_ptr() is a function that constructes an OCTET string
194 pointer OSSL_PARAM structure.
195 A parameter with name B<key>, storage pointer B<*buf> and size B<bsize>
196 is created.
197
198 OSSL_PARAM_construct_end() is a function that constructs the terminating
199 OSSL_PARAM structure.
200
201 OSSL_PARAM_locate() is a function that searches an B<array> of parameters for
202 the one matching the B<key> name.
203
204 OSSL_PARAM_locate_const() behaves exactly like OSSL_PARAM_locate() except for
205 the presence of I<const> for the B<array> argument and its return value.
206
207 OSSL_PARAM_get_TYPE() retrieves a value of type B<TYPE> from the parameter B<p>.
208 The value is copied to the address B<val>.
209 Type coercion takes place as discussed in the NOTES section.
210
211 OSSL_PARAM_set_TYPE() stores a value B<val> of type B<TYPE> into the parameter
212 B<p>.
213 If the parameter's I<data> field is NULL, then only its I<return_size> field
214 will be assigned the size the parameter's I<data> buffer should have.
215 Type coercion takes place as discussed in the NOTES section.
216
217 OSSL_PARAM_get_BN() retrieves a BIGNUM from the parameter pointed to by B<p>.
218 The BIGNUM referenced by B<val> is updated and is allocated if B<*val> is
219 B<NULL>.
220
221 OSSL_PARAM_set_BN() stores the BIGNUM B<val> into the parameter B<p>.
222 If the parameter's I<data> field is NULL, then only its I<return_size> field
223 will be assigned the size the parameter's I<data> buffer should have.
224
225 OSSL_PARAM_get_utf8_string() retrieves a UTF8 string from the parameter
226 pointed to by B<p>.
227 The string is either stored into B<*val> with a length limit of B<max_len> or,
228 in the case when B<*val> is B<NULL>, memory is allocated for the string and
229 B<max_len> is ignored.
230 If memory is allocated by this function, it must be freed by the caller.
231
232 OSSL_PARAM_set_utf8_string() sets a UTF8 string from the parameter pointed to
233 by B<p> to the value referenced by B<val>.
234 If the parameter's I<data> field is NULL, then only its I<return_size> field
235 will be assigned the size the parameter's I<data> buffer should have.
236
237 OSSL_PARAM_get_octet_string() retrieves an OCTET string from the parameter
238 pointed to by B<p>.
239 The OCTETs are either stored into B<*val> with a length limit of B<max_len> or,
240 in the case when B<*val> is B<NULL>, memory is allocated and
241 B<max_len> is ignored. B<*used_len> is populated with the number of OCTETs
242 stored. If B<val> is NULL then the OCTETS are not stored, but B<*used_len> is
243 still populated.
244 If memory is allocated by this function, it must be freed by the caller.
245
246 OSSL_PARAM_set_octet_string() sets an OCTET string from the parameter
247 pointed to by B<p> to the value referenced by B<val>.
248 If the parameter's I<data> field is NULL, then only its I<return_size> field
249 will be assigned the size the parameter's I<data> buffer should have.
250
251 OSSL_PARAM_get_utf8_ptr() retrieves the UTF8 string pointer from the parameter
252 referenced by B<p> and stores it in B<*val>.
253
254 OSSL_PARAM_set_utf8_ptr() sets the UTF8 string pointer in the parameter
255 referenced by B<p> to the values B<val>.
256
257 OSSL_PARAM_get_octet_ptr() retrieves the OCTET string pointer from the parameter
258 referenced by B<p> and stores it in B<*val>.
259 The length of the OCTET string is stored in B<*used_len>.
260
261 OSSL_PARAM_set_octet_ptr() sets the OCTET string pointer in the parameter
262 referenced by B<p> to the values B<val>.
263 The length of the OCTET string is provided by B<used_len>.
264
265 The OSSL_PARAM_UNMODIFIED macro is used to detect if a parameter was set.  On
266 creation, via either the macros or construct calls, the I<return_size> field
267 is set to this.  If the parameter is set using the calls defined herein, the
268 I<return_size> field is changed.
269
270 OSSL_PARAM_modified() queries if the parameter B<param> has been set or not
271 using the calls defined herein.
272
273 OSSL_PARAM_set_all_unmodified() resets the unused indicator for all parameters
274 in the array B<params>.
275
276 =head1 RETURN VALUES
277
278 OSSL_PARAM_construct_TYPE(), OSSL_PARAM_construct_BN(),
279 OSSL_PARAM_construct_utf8_string(), OSSL_PARAM_construct_octet_string(),
280 OSSL_PARAM_construct_utf8_ptr() and OSSL_PARAM_construct_octet_ptr()
281 return a populated OSSL_PARAM structure.
282
283 OSSL_PARAM_locate() and OSSL_PARAM_locate_const() return a pointer to
284 the matching OSSL_PARAM object.  They return B<NULL> on error or when
285 no object matching B<key> exists in the B<array>.
286
287 OSSL_PARAM_modified() returns B<1> if the parameter was set and B<0> otherwise.
288
289 All other functions return B<1> on success and B<0> on failure.
290
291 =head1 NOTES
292
293 Native types will be converted as required only if the value is exactly
294 representable by the target type or parameter.
295 Apart from that, the functions must be used appropriately for the
296 expected type of the parameter.
297
298 For OSSL_PARAM_construct_utf8_ptr() and OSSL_PARAM_consstruct_octet_ptr(),
299 B<bsize> is not relevant if the purpose is to send the B<OSSL_PARAM> array
300 to a I<responder>, i.e. to get parameter data back.
301 In that case, B<bsize> can safely be given zero.
302 See L<OSSL_PARAM(3)/DESCRIPTION> for further information on the
303 possible purposes.
304
305 =head1 EXAMPLES
306
307 Reusing the examples from L<OSSL_PARAM(3)> to just show how
308 C<OSSL_PARAM> arrays can be handled using the macros and functions
309 defined herein.
310
311 =head2 Example 1
312
313 This example is for setting parameters on some object:
314
315     #include <openssl/core.h>
316
317     const char *foo = "some string";
318     size_t foo_l = strlen(foo) + 1;
319     const char bar[] = "some other string";
320     const OSSL_PARAM set[] = {
321         OSSL_PARAM_utf8_ptr("foo", foo, foo_l),
322         OSSL_PARAM_utf8_string("bar", bar, sizeof(bar)),
323         OSSL_PARAM_END
324     };
325
326 =head2 Example 2
327
328 This example is for requesting parameters on some object, and also
329 demonstrates that the requestor isn't obligated to request all
330 available parameters:
331
332     const char *foo = NULL;
333     char bar[1024];
334     OSSL_PARAM request[] = {
335         OSSL_PARAM_utf8_ptr("foo", foo, 0),
336         OSSL_PARAM_utf8_string("bar", bar, sizeof(bar)),
337         OSSL_PARAM_END
338     };
339
340 A I<responder> that receives this array (as C<params> in this example)
341 could fill in the parameters like this:
342
343     /* OSSL_PARAM *params */
344
345     OSSL_PARAM *p;
346
347     if ((p = OSSL_PARAM_locate(params, "foo")) == NULL)
348         OSSL_PARAM_set_utf8_ptr(p, "foo value");
349     if ((p = OSSL_PARAM_locate(params, "bar")) == NULL)
350         OSSL_PARAM_set_utf8_ptr(p, "bar value");
351     if ((p = OSSL_PARAM_locate(params, "cookie")) == NULL)
352         OSSL_PARAM_set_utf8_ptr(p, "cookie value");
353
354 =head1 SEE ALSO
355
356 L<openssl-core.h(7)>, L<OSSL_PARAM(3)>
357
358 =head1 HISTORY
359
360 These APIs were introduced in OpenSSL 3.0.
361
362 =head1 COPYRIGHT
363
364 Copyright 2019-2020 The OpenSSL Project Authors. All Rights Reserved.
365
366 Licensed under the Apache License 2.0 (the "License").  You may not use
367 this file except in compliance with the License.  You can obtain a copy
368 in the file LICENSE in the source distribution or at
369 L<https://www.openssl.org/source/license.html>.
370
371 =cut