OSSL_PARAM helper functions.
[openssl.git] / doc / man3 / OSSL_PARAM_TYPE.pod
1 =pod
2
3 =head1 NAME
4
5 OSSL_PARAM_TYPE, OSSL_PARAM_utf8_string, OSSL_PARAM_octet_string,
6 OSSL_PARAM_utf8_ptr, OSSL_PARAM_octet_ptr, OSSL_PARAM_SIZED_TYPE,
7 OSSL_PARAM_SIZED_BN, OSSL_PARAM_SIZED_utf8_string,
8 OSSL_PARAM_SIZED_octet_string, OSSL_PARAM_SIZED_utf8_ptr,
9 OSSL_PARAM_SIZED_octet_ptr, OSSL_PARAM_END, OSSL_PARAM_construct_TYPE,
10 OSSL_PARAM_END,
11 OSSL_PARAM_construct_BN, OSSL_PARAM_construct_utf8_string,
12 OSSL_PARAM_construct_utf8_ptr, OSSL_PARAM_construct_octet_string,
13 OSSL_PARAM_construct_octet_ptr, OSSL_PARAM_locate, OSSL_PARAM_get_TYPE,
14 OSSL_PARAM_set_TYPE, OSSL_PARAM_get_BN, OSSL_PARAM_set_BN,
15 OSSL_PARAM_get_utf8_string, OSSL_PARAM_set_utf8_string,
16 OSSL_PARAM_get_octet_string, OSSL_PARAM_set_octet_string,
17 OSSL_PARAM_get_utf8_ptr, OSSL_PARAM_set_utf8_ptr, OSSL_PARAM_get_octet_ptr,
18 OSSL_PARAM_set_octet_ptr
19 - OSSL_PARAM helpers
20
21 =head1 SYNOPSIS
22
23  #include <openssl/params.h>
24
25  #define OSSL_PARAM_TYPE(key, address)
26  #define OSSL_PARAM_utf8_string(key, address, size)
27  #define OSSL_PARAM_octet_string(key, address, size)
28  #define OSSL_PARAM_utf8_ptr(key, address, size)
29  #define OSSL_PARAM_octet_ptr(key, address, size)
30  #define OSSL_PARAM_SIZED_TYPE(key, address, return_size)
31  #define OSSL_PARAM_SIZED_BN(key, address, size, return_size)
32  #define OSSL_PARAM_SIZED_utf8_string(key, address, size, return_size)
33  #define OSSL_PARAM_SIZED_octet_string(key, address, size, return_size)
34  #define OSSL_PARAM_SIZED_utf8_ptr(key, address, size, return_size)
35  #define OSSL_PARAM_SIZED_octet_ptr(key, address, size, return_size)
36  #define OSSL_PARAM_END
37
38  OSSL_PARAM OSSL_PARAM_construct_TYPE(const char *key, TYPE *buf, size_t *ret);
39  OSSL_PARAM OSSL_PARAM_construct_BN(const char *key, unsigned char *buf,
40                                     size_t bsize, size_t *rsize);
41  OSSL_PARAM OSSL_PARAM_construct_utf8_string(const char *key, char *buf,
42                                              size_t bsize, size_t *rsize);
43  OSSL_PARAM OSSL_PARAM_construct_octet_string(const char *key, void *buf,
44                                               size_t bsize, size_t *rsize);
45  OSSL_PARAM OSSL_PARAM_construct_utf8_ptr(const char *key, char **buf,
46                                           size_t *rsize);
47  OSSL_PARAM OSSL_PARAM_construct_octet_ptr(const char *key, void **buf,
48                                            size_t *rsize);
49
50  OSSL_PARAM *OSSL_PARAM_locate(OSSL_PARAM *array, const char *key);
51
52  int OSSL_PARAM_get_TYPE(const OSSL_PARAM *p, const char *key, TYPE *val);
53  int OSSL_PARAM_set_TYPE(const OSSL_PARAM *p, const char *key, TYPE val);
54
55  int OSSL_PARAM_get_BN(const OSSL_PARAM *p, const char *key, BIGNUM **val);
56  int OSSL_PARAM_set_BN(const OSSL_PARAM *p, const char *key, const BIGNUM *val);
57
58  int OSSL_PARAM_get_utf8_string(const OSSL_PARAM *p, char **val,
59                                 size_t max_len);
60  int OSSL_PARAM_set_utf8_string(const OSSL_PARAM *p, const char *val);
61
62  int OSSL_PARAM_get_octet_string(const OSSL_PARAM *p, void **val,
63                                  size_t max_len, size_t *used_len);
64  int OSSL_PARAM_set_octet_string(const OSSL_PARAM *p, const void *val,
65                                  size_t len);
66
67  int OSSL_PARAM_get_utf8_ptr(const OSSL_PARAM *p, char **val);
68  int OSSL_PARAM_set_utf8_ptr(const OSSL_PARAM *p, char *val);
69
70  int OSSL_PARAM_get_octet_ptr(const OSSL_PARAM *p, void **val,
71                               size_t *used_len);
72  int OSSL_PARAM_set_octet_ptr(const OSSL_PARAM *p, void *val, size_t used_len);
73
74 =head1 DESCRIPTION
75
76 A collection of utility functions that simplify and add type safety to the
77 OSSL_PARAM arrays.  The following B<TYPE> names are supported:
78
79 =over 1
80
81 =item *
82
83 double
84
85 =item *
86
87 int
88
89 =item *
90
91 int32 (int32_t)
92
93 =item *
94
95 int64 (int64_t)
96
97 =item *
98
99 long int (long)
100
101 =item *
102
103 size_t
104
105 =item *
106
107 uint32 (uint32_t)
108
109 =item *
110
111 uint64 (uint64_t)
112
113 =item *
114
115 unsigned int (uint)
116
117 =item *
118
119 unsigned long int (ulong)
120
121 =back
122
123 OSSL_PARAM_TYPE() are a series of macros designed to assist initialising an
124 array of OSSL_PARAM structures.
125 Each of these macros defines a parameter of the specified B<TYPE> with the
126 provided B<key> and parameter variable B<address>.
127
128 OSSL_PARAM_utf8_string(), OSSL_PARAM_octet_string(), OSSL_PARAM_utf8_ptr(),
129 OSSL_PARAM_octet_ptr() are macros that provide support for defining UTF8
130 strings and OCTET strings.
131 A parameter with name B<key> is defined.
132 The storage for this parameter is at B<address> and is of B<size> bytes.
133
134 OSSL_PARAM_SIZED_TYPE() are a second series of macros designed to assist with
135 the initialisation of OSSL_PARAM structures.
136 They are similar to the OSSL_PARAM_TYPE() macros but also include a
137 B<return_size> argument which contains the address of a size_t variable which
138 will be populated with the actual size of the parameter upon return from a
139 OSSL_PARAM_set_TYPE() call.
140
141 OSSL_PARAM_SIZED_BN(), OSSL_PARAM_SIZED_utf8_string(),
142 OSSL_PARAM_SIZED_octet_string(), OSSL_PARAM_SIZED_utf8_ptr(),
143 OSSL_PARAM_SIZED_octet_ptr() are macros that provide support for defining large
144 integers, UTF8 string and OCTET strings in an OSSL_PARAM array.
145 A parameter with name B<key> is defined.
146 The storage for this parameter is at B<address> and is of B<size> bytes.
147 The size used by the parameter value, in bytes, is written to B<return_size>.
148
149 OSSL_PARAM_END provides an end of parameter list marker.
150 This should terminate all OSSL_PARAM arrays.
151
152 OSSL_PARAM_construct_TYPE() are a series of functions that create OSSL_PARAM
153 records dynamically.
154 A parameter with name B<key> is created.
155 The parameter will use storage pointed to by B<buf> and return size of B<ret>.
156
157 OSSL_PARAM_construct_BN() is a function that constructs a large integer
158 OSSL_PARAM structure.
159 A parameter with name B<key>, storage B<buf>, size B<bsize> and return
160 size B<rsize> is created.
161
162 OSSL_PARAM_construct_utf8_string() is a function that constructs a UTF8
163 string 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_octet_string() is a function that constructs an OCTET
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_utf8_ptr() is a function that constructes a UTF string
173 pointer OSSL_PARAM structure.
174 A parameter with name B<key>, storage pointer B<*buf> and return size B<rsize>
175 is created.
176
177 OSSL_PARAM_construct_octet_ptr() is a function that constructes an OCTET string
178 pointer OSSL_PARAM structure.
179 A parameter with name B<key>, storage pointer B<*buf> and return size B<rsize>
180 is created.
181
182 OSSL_PARAM_locate() is a function that searches an B<array> of parameters for
183 the one matching the B<key> name.
184
185 OSSL_PARAM_get_TYPE() retrieves a value of type B<TYPE> from the parameter B<p>.
186 The value is copied to the address B<val>.
187 Type coercion takes place as discussed in the NOTES section.
188
189 OSSL_PARAM_set_TYPE() stores a value B<val> of type B<TYPE> into the paramter
190 B<p>.
191 Type coercion takes place as discussed in the NOTES section.
192
193 OSSL_PARAM_get_BN() retrieves a BIGNUM from the parameter pointed to by B<p>.
194 The BIGNUM referenced by B<val> is updated and is allocated if B<*val> is
195 B<NULL>.
196
197 OSSL_PARAM_set_BN() stores the BIGNUM B<val> into the paramater B<p>.
198
199 OSSL_PARAM_get_utf8_string() retrieves a UTF8 string from the parameter
200 pointed to by B<p>.
201 The string is either stored into B<*val> with a length limit of B<max_len> or,
202 in the case when B<*val> is B<NULL>, memory is allocated for the string and
203 B<max_len> is ignored.
204 If memory is allocated by this function, it must be freed by the caller.
205
206 OSSL_PARAM_set_utf8_string() sets a UTF8 string from the parameter pointed to
207 by B<p> to the value referenced by B<val>.
208
209 OSSL_PARAM_get_octet_string() retrieves an OCTET string from the parameter
210 pointed to by B<p>.
211 The OCTETs are either stored into B<*val> with a length limit of B<max_len> or,
212 in the case when B<*val> is B<NULL>, memory is allocated and
213 B<max_len> is ignored.
214 If memory is allocated by this function, it must be freed by the caller.
215
216 OSSL_PARAM_set_octet_string() sets an OCTET string from the parameter
217 pointed to by B<p> to the value referenced by B<val>.
218
219 OSSL_PARAM_get_utf8_ptr() retrieves the UTF8 string pointer from the parameter
220 referenced by B<p> and stores it in B<*val>.
221
222 OSSL_PARAM_set_utf8_ptr() sets the UTF8 string pointer in the parameter
223 referenced by B<p> to the values B<val>.
224
225 OSSL_PARAM_get_octet_ptr() retrieves the OCTET string pointer from the parameter
226 referenced by B<p> and stores it in B<*val>.
227 The length of the OCTET string is stored in B<*used_len>.
228
229 OSSL_PARAM_set_octet_ptr() sets the OCTET string pointer in the parameter
230 referenced by B<p> to the values B<val>.
231 The length of the OCTET string is provided by B<used_len>.
232
233 =head1 RETURN VALUES
234
235 OSSL_PARAM_construct_TYPE(), OSSL_PARAM_construct_BN(),
236 OSSL_PARAM_construct_utf8_string(), OSSL_PARAM_construct_octet_string(),
237 OSSL_PARAM_construct_utf8_ptr() and OSSL_PARAM_construct_octet_ptr()
238 return a populated OSSL_PARAM structure.
239
240 OSSL_PARAM_locate() returns a pointer to the matching OSSL_PARAM object.
241 It returns B<NULL> on error or when no object matching B<key> exists in
242 the B<array>.
243
244 All other functions return B<1> on success and B<0> on failure.
245
246 =head1 NOTES
247
248 Integral types will be widened and sign extended as required.
249 Apart from that, the functions must be used appropriately for the
250 expected type of the parameter.
251
252 =head1 EXAMPLES
253
254 Reusing the examples from L<OSSL_PARAM(3)> to just show how
255 C<OSSL_PARAM> arrays can be handled using the macros and functions
256 defined herein.
257
258 =head2 Example 1
259
260 This example is for setting parameters on some object:
261
262     #include <openssl/core.h>
263
264     const char *foo = "some string";
265     size_t foo_l = strlen(foo) + 1;
266     const char bar[] = "some other string";
267     const OSSL_PARAM set[] = {
268         OSSL_PARAM_utf8_ptr("foo", foo, foo_l),
269         OSSL_PARAM_utf8_string("bar", bar, sizeof(bar)),
270         OSSL_PARAM_END
271     };
272
273 =head2 Example 2
274
275 This example is for requesting parameters on some object:
276
277     const char *foo = NULL;
278     size_t foo_l;
279     char bar[1024];
280     size_t bar_l;
281     const OSSL_PARAM request[] = {
282         OSSL_PARAM_UTF8_PTR("foo", foo, 0, foo_l),
283         OSSL_PARAM_UTF8_STRING("bar", bar, sizeof(bar), bar_l),
284         OSSL_PARAM_END
285     };
286
287 A I<responder> that receives this array (as C<params> in this example)
288 could fill in the parameters like this:
289
290     /* const OSSL_PARAM *params */
291
292     OSSL_PARAM_set_utf8_ptr(OSSL_PARAM_locate(params, "foo"), "foo value");
293     OSSL_PARAM_set_utf8_string(OSSL_PARAM_locate(params, "bar"), "bar value");
294
295 =head1 SEE ALSO
296
297 L<openssl-core.h(7)>, L<OSSL_PARAM(3)>
298
299 =head1 HISTORY
300
301 These APIs were introduced in OpenSSL 3.0.0.
302
303 =head1 COPYRIGHT
304
305 Copyright 2019 The OpenSSL Project Authors. All Rights Reserved.
306
307 Licensed under the Apache License 2.0 (the "License").  You may not use
308 this file except in compliance with the License.  You can obtain a copy
309 in the file LICENSE in the source distribution or at
310 L<https://www.openssl.org/source/license.html>.
311
312 =cut