Add param builder free function.
[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_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,
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,
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,
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 helpers
32
33 =head1 SYNOPSIS
34
35 =for comment generic
36
37  #include <openssl/params.h>
38
39  /*
40   * TYPE in function names is one of:
41   * double, int, int32, int64, long, size_t, uint, uint32, uint64, ulong
42   * Corresponding TYPE in function arguments is one of:
43   * double, int, int32_t, int64_t, long, size_t, unsigned int, uint32_t,
44   * uint64_t, unsigned long
45   */
46
47  #define OSSL_PARAM_TYPE(key, address)
48  #define OSSL_PARAM_BN(key, address, size)
49  #define OSSL_PARAM_utf8_string(key, address, size)
50  #define OSSL_PARAM_octet_string(key, address, size)
51  #define OSSL_PARAM_utf8_ptr(key, address, size)
52  #define OSSL_PARAM_octet_ptr(key, address, size)
53  #define OSSL_PARAM_END
54
55  OSSL_PARAM OSSL_PARAM_construct_TYPE(const char *key, TYPE *buf);
56  OSSL_PARAM OSSL_PARAM_construct_BN(const char *key, unsigned char *buf,
57                                     size_t bsize);
58  OSSL_PARAM OSSL_PARAM_construct_utf8_string(const char *key, char *buf,
59                                              size_t bsize);
60  OSSL_PARAM OSSL_PARAM_construct_octet_string(const char *key, void *buf,
61                                               size_t bsize);
62  OSSL_PARAM OSSL_PARAM_construct_utf8_ptr(const char *key, char **buf,
63                                           size_t bsize);
64  OSSL_PARAM OSSL_PARAM_construct_octet_ptr(const char *key, void **buf,
65                                            size_t bsize);
66  OSSL_PARAM OSSL_PARAM_construct_end(void);
67
68  OSSL_PARAM *OSSL_PARAM_locate(OSSL_PARAM *array, const char *key);
69  const OSSL_PARAM *OSSL_PARAM_locate_const(const OSSL_PARAM *array,
70                                            const char *key);
71
72  int OSSL_PARAM_get_TYPE(const OSSL_PARAM *p, TYPE *val);
73  int OSSL_PARAM_set_TYPE(OSSL_PARAM *p, TYPE val);
74
75  int OSSL_PARAM_get_BN(const OSSL_PARAM *p, BIGNUM **val);
76  int OSSL_PARAM_set_BN(OSSL_PARAM *p, const BIGNUM *val);
77
78  int OSSL_PARAM_get_utf8_string(const OSSL_PARAM *p, char **val,
79                                 size_t max_len);
80  int OSSL_PARAM_set_utf8_string(OSSL_PARAM *p, const char *val);
81
82  int OSSL_PARAM_get_octet_string(const OSSL_PARAM *p, void **val,
83                                  size_t max_len, size_t *used_len);
84  int OSSL_PARAM_set_octet_string(OSSL_PARAM *p, const void *val, size_t len);
85
86  int OSSL_PARAM_get_utf8_ptr(const OSSL_PARAM *p, const char **val);
87  int OSSL_PARAM_set_utf8_ptr(OSSL_PARAM *p, const char *val);
88
89  int OSSL_PARAM_get_octet_ptr(const OSSL_PARAM *p, const void **val,
90                               size_t *used_len);
91  int OSSL_PARAM_set_octet_ptr(OSSL_PARAM *p, const void *val,
92                               size_t used_len);
93
94 =head1 DESCRIPTION
95
96 A collection of utility functions that simplify and add type safety to the
97 OSSL_PARAM arrays.  The following B<TYPE> names are supported:
98
99 =over 1
100
101 =item *
102
103 double
104
105 =item *
106
107 int
108
109 =item *
110
111 int32 (int32_t)
112
113 =item *
114
115 int64 (int64_t)
116
117 =item *
118
119 long int (long)
120
121 =item *
122
123 size_t
124
125 =item *
126
127 uint32 (uint32_t)
128
129 =item *
130
131 uint64 (uint64_t)
132
133 =item *
134
135 unsigned int (uint)
136
137 =item *
138
139 unsigned long int (ulong)
140
141 =back
142
143 OSSL_PARAM_TYPE() are a series of macros designed to assist initialising an
144 array of OSSL_PARAM structures.
145 Each of these macros defines a parameter of the specified B<TYPE> with the
146 provided B<key> and parameter variable B<address>.
147
148 OSSL_PARAM_utf8_string(), OSSL_PARAM_octet_string(), OSSL_PARAM_utf8_ptr(),
149 OSSL_PARAM_octet_ptr(), OSSL_PARAM_BN() are macros that provide support
150 for defining UTF8 strings, OCTET strings and big numbers.
151 A parameter with name B<key> is defined.
152 The storage for this parameter is at B<address> and is of B<size> bytes.
153
154 OSSL_PARAM_END provides an end of parameter list marker.
155 This should terminate all OSSL_PARAM arrays.
156
157 OSSL_PARAM_construct_TYPE() are a series of functions that create OSSL_PARAM
158 records dynamically.
159 A parameter with name B<key> is created.
160 The parameter will use storage pointed to by B<buf> and return size of B<ret>.
161
162 OSSL_PARAM_construct_BN() is a function that constructs a large integer
163 OSSL_PARAM structure.
164 A parameter with name B<key>, storage B<buf>, size B<bsize> and return
165 size B<rsize> is created.
166
167 OSSL_PARAM_construct_utf8_string() is a function that constructs a UTF8
168 string OSSL_PARAM structure.
169 A parameter with name B<key>, storage B<buf>, size B<bsize> and return
170 size B<rsize> is created.
171
172 OSSL_PARAM_construct_octet_string() is a function that constructs an OCTET
173 string 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_ptr() is a function that constructes a UTF string
178 pointer OSSL_PARAM structure.
179 A parameter with name B<key>, storage pointer B<*buf>, size B<bsize> and
180 return size B<rsize> is created.
181
182 OSSL_PARAM_construct_octet_ptr() is a function that constructes an OCTET string
183 pointer OSSL_PARAM structure.
184 A parameter with name B<key>, storage pointer B<*buf>, size B<bsize> and
185 return size B<rsize> is created.
186
187 OSSL_PARAM_construct_end() is a function that constructs the terminating
188 OSSL_PARAM structure.
189
190 OSSL_PARAM_locate() is a function that searches an B<array> of parameters for
191 the one matching the B<key> name.
192
193 OSSL_PARAM_locate_const() behaves exactly like OSSL_PARAM_locate() except for
194 the presence of I<const> for the B<array> argument and its return value.
195
196 OSSL_PARAM_get_TYPE() retrieves a value of type B<TYPE> from the parameter B<p>.
197 The value is copied to the address B<val>.
198 Type coercion takes place as discussed in the NOTES section.
199
200 OSSL_PARAM_set_TYPE() stores a value B<val> of type B<TYPE> into the parameter
201 B<p>.
202 Type coercion takes place as discussed in the NOTES section.
203
204 OSSL_PARAM_get_BN() retrieves a BIGNUM from the parameter pointed to by B<p>.
205 The BIGNUM referenced by B<val> is updated and is allocated if B<*val> is
206 B<NULL>.
207
208 OSSL_PARAM_set_BN() stores the BIGNUM B<val> into the parameter B<p>.
209
210 OSSL_PARAM_get_utf8_string() retrieves a UTF8 string from the parameter
211 pointed to by B<p>.
212 The string is either stored into B<*val> with a length limit of B<max_len> or,
213 in the case when B<*val> is B<NULL>, memory is allocated for the string and
214 B<max_len> is ignored.
215 If memory is allocated by this function, it must be freed by the caller.
216
217 OSSL_PARAM_set_utf8_string() sets a UTF8 string from the parameter pointed to
218 by B<p> to the value referenced by B<val>.
219
220 OSSL_PARAM_get_octet_string() retrieves an OCTET string from the parameter
221 pointed to by B<p>.
222 The OCTETs are either stored into B<*val> with a length limit of B<max_len> or,
223 in the case when B<*val> is B<NULL>, memory is allocated and
224 B<max_len> is ignored.
225 If memory is allocated by this function, it must be freed by the caller.
226
227 OSSL_PARAM_set_octet_string() sets an OCTET string from the parameter
228 pointed to by B<p> to the value referenced by B<val>.
229
230 OSSL_PARAM_get_utf8_ptr() retrieves the UTF8 string pointer from the parameter
231 referenced by B<p> and stores it in B<*val>.
232
233 OSSL_PARAM_set_utf8_ptr() sets the UTF8 string pointer in the parameter
234 referenced by B<p> to the values B<val>.
235
236 OSSL_PARAM_get_octet_ptr() retrieves the OCTET string pointer from the parameter
237 referenced by B<p> and stores it in B<*val>.
238 The length of the OCTET string is stored in B<*used_len>.
239
240 OSSL_PARAM_set_octet_ptr() sets the OCTET string pointer in the parameter
241 referenced by B<p> to the values B<val>.
242 The length of the OCTET string is provided by B<used_len>.
243
244 =head1 RETURN VALUES
245
246 OSSL_PARAM_construct_TYPE(), OSSL_PARAM_construct_BN(),
247 OSSL_PARAM_construct_utf8_string(), OSSL_PARAM_construct_octet_string(),
248 OSSL_PARAM_construct_utf8_ptr() and OSSL_PARAM_construct_octet_ptr()
249 return a populated OSSL_PARAM structure.
250
251 OSSL_PARAM_locate() and OSSL_PARAM_locate_const() return a pointer to
252 the matching OSSL_PARAM object.  They return B<NULL> on error or when
253 no object matching B<key> exists in the B<array>.
254
255 All other functions return B<1> on success and B<0> on failure.
256
257 =head1 NOTES
258
259 Native types will be converted as required only if the value is exactly
260 representable by the target type or parameter.
261 Apart from that, the functions must be used appropriately for the
262 expected type of the parameter.
263
264 For OSSL_PARAM_get_utf8_ptr() and OSSL_PARAM_get_octet_ptr(), B<bsize>
265 is not relevant if the purpose is to send the B<OSSL_PARAM> array to a
266 I<responder>, i.e. to get parameter data back.
267 In that case, B<bsize> can safely be given zero.
268 See L<OSSL_PARAM(3)/DESCRIPTION> for further information on the
269 possible purposes.
270
271 =head1 EXAMPLES
272
273 Reusing the examples from L<OSSL_PARAM(3)> to just show how
274 C<OSSL_PARAM> arrays can be handled using the macros and functions
275 defined herein.
276
277 =head2 Example 1
278
279 This example is for setting parameters on some object:
280
281     #include <openssl/core.h>
282
283     const char *foo = "some string";
284     size_t foo_l = strlen(foo) + 1;
285     const char bar[] = "some other string";
286     const OSSL_PARAM set[] = {
287         OSSL_PARAM_utf8_ptr("foo", foo, foo_l),
288         OSSL_PARAM_utf8_string("bar", bar, sizeof(bar)),
289         OSSL_PARAM_END
290     };
291
292 =head2 Example 2
293
294 This example is for requesting parameters on some object, and also
295 demonstrates that the requestor isn't obligated to request all
296 available parameters:
297
298     const char *foo = NULL;
299     char bar[1024];
300     OSSL_PARAM request[] = {
301         OSSL_PARAM_utf8_ptr("foo", foo, 0),
302         OSSL_PARAM_utf8_string("bar", bar, sizeof(bar)),
303         OSSL_PARAM_END
304     };
305
306 A I<responder> that receives this array (as C<params> in this example)
307 could fill in the parameters like this:
308
309     /* OSSL_PARAM *params */
310
311     OSSL_PARAM *p;
312
313     if ((p = OSSL_PARAM_locate(params, "foo")) == NULL)
314         OSSL_PARAM_set_utf8_ptr(p, "foo value");
315     if ((p = OSSL_PARAM_locate(params, "bar")) == NULL)
316         OSSL_PARAM_set_utf8_ptr(p, "bar value");
317     if ((p = OSSL_PARAM_locate(params, "cookie")) == NULL)
318         OSSL_PARAM_set_utf8_ptr(p, "cookie value");
319
320 =head1 SEE ALSO
321
322 L<openssl-core.h(7)>, L<OSSL_PARAM(3)>
323
324 =head1 HISTORY
325
326 These APIs were introduced in OpenSSL 3.0.
327
328 =head1 COPYRIGHT
329
330 Copyright 2019 The OpenSSL Project Authors. All Rights Reserved.
331
332 Licensed under the Apache License 2.0 (the "License").  You may not use
333 this file except in compliance with the License.  You can obtain a copy
334 in the file LICENSE in the source distribution or at
335 L<https://www.openssl.org/source/license.html>.
336
337 =cut