xref: /freebsd-src/crypto/openssl/doc/man3/OSSL_PARAM_BLD.pod (revision a7148ab39c03abd4d1a84997c70bf96f15dd2a09)
1b077aed3SPierre Pronchery=pod
2b077aed3SPierre Pronchery
3b077aed3SPierre Pronchery=head1 NAME
4b077aed3SPierre Pronchery
5b077aed3SPierre ProncheryOSSL_PARAM_BLD, OSSL_PARAM_BLD_new, OSSL_PARAM_BLD_to_param,
6b077aed3SPierre ProncheryOSSL_PARAM_BLD_free, OSSL_PARAM_BLD_push_int,
7b077aed3SPierre ProncheryOSSL_PARAM_BLD_push_uint, OSSL_PARAM_BLD_push_long,
8b077aed3SPierre ProncheryOSSL_PARAM_BLD_push_ulong, OSSL_PARAM_BLD_push_int32,
9b077aed3SPierre ProncheryOSSL_PARAM_BLD_push_uint32, OSSL_PARAM_BLD_push_int64,
10b077aed3SPierre ProncheryOSSL_PARAM_BLD_push_uint64, OSSL_PARAM_BLD_push_size_t,
11b077aed3SPierre ProncheryOSSL_PARAM_BLD_push_time_t, OSSL_PARAM_BLD_push_double,
12b077aed3SPierre ProncheryOSSL_PARAM_BLD_push_BN, OSSL_PARAM_BLD_push_BN_pad,
13b077aed3SPierre ProncheryOSSL_PARAM_BLD_push_utf8_string, OSSL_PARAM_BLD_push_utf8_ptr,
14b077aed3SPierre ProncheryOSSL_PARAM_BLD_push_octet_string, OSSL_PARAM_BLD_push_octet_ptr
15b077aed3SPierre Pronchery- functions to assist in the creation of OSSL_PARAM arrays
16b077aed3SPierre Pronchery
17b077aed3SPierre Pronchery=head1 SYNOPSIS
18b077aed3SPierre Pronchery
19b077aed3SPierre Pronchery=for openssl generic
20b077aed3SPierre Pronchery
21b077aed3SPierre Pronchery #include <openssl/param_build.h>
22b077aed3SPierre Pronchery
23b077aed3SPierre Pronchery typedef struct OSSL_PARAM_BLD;
24b077aed3SPierre Pronchery
25b077aed3SPierre Pronchery OSSL_PARAM_BLD *OSSL_PARAM_BLD_new(void);
26b077aed3SPierre Pronchery OSSL_PARAM *OSSL_PARAM_BLD_to_param(OSSL_PARAM_BLD *bld);
27b077aed3SPierre Pronchery void OSSL_PARAM_BLD_free(OSSL_PARAM_BLD *bld);
28b077aed3SPierre Pronchery
29b077aed3SPierre Pronchery int OSSL_PARAM_BLD_push_TYPE(OSSL_PARAM_BLD *bld, const char *key, TYPE val);
30b077aed3SPierre Pronchery
31b077aed3SPierre Pronchery int OSSL_PARAM_BLD_push_BN(OSSL_PARAM_BLD *bld, const char *key,
32b077aed3SPierre Pronchery                            const BIGNUM *bn);
33b077aed3SPierre Pronchery int OSSL_PARAM_BLD_push_BN_pad(OSSL_PARAM_BLD *bld, const char *key,
34b077aed3SPierre Pronchery                                const BIGNUM *bn, size_t sz);
35b077aed3SPierre Pronchery
36b077aed3SPierre Pronchery int OSSL_PARAM_BLD_push_utf8_string(OSSL_PARAM_BLD *bld, const char *key,
37b077aed3SPierre Pronchery                                     const char *buf, size_t bsize);
38b077aed3SPierre Pronchery int OSSL_PARAM_BLD_push_utf8_ptr(OSSL_PARAM_BLD *bld, const char *key,
39b077aed3SPierre Pronchery                                  char *buf, size_t bsize);
40b077aed3SPierre Pronchery int OSSL_PARAM_BLD_push_octet_string(OSSL_PARAM_BLD *bld, const char *key,
41b077aed3SPierre Pronchery                                      const void *buf, size_t bsize);
42b077aed3SPierre Pronchery int OSSL_PARAM_BLD_push_octet_ptr(OSSL_PARAM_BLD *bld, const char *key,
43b077aed3SPierre Pronchery                                   void *buf, size_t bsize);
44b077aed3SPierre Pronchery
45b077aed3SPierre Pronchery
46b077aed3SPierre Pronchery=head1 DESCRIPTION
47b077aed3SPierre Pronchery
48b077aed3SPierre ProncheryA collection of utility functions that simplify the creation of OSSL_PARAM
49b077aed3SPierre Proncheryarrays.  The B<I<TYPE>> names are as per L<OSSL_PARAM_int(3)>.
50b077aed3SPierre Pronchery
51b077aed3SPierre ProncheryOSSL_PARAM_BLD_new() allocates and initialises a new OSSL_PARAM_BLD structure
52b077aed3SPierre Proncheryso that values can be added.
53b077aed3SPierre ProncheryAny existing values are cleared.
54b077aed3SPierre Pronchery
55b077aed3SPierre ProncheryOSSL_PARAM_BLD_free() deallocates the memory allocates by OSSL_PARAM_BLD_new().
56*a7148ab3SEnji CooperIf the argument is NULL, nothing is done.
57b077aed3SPierre Pronchery
58b077aed3SPierre ProncheryOSSL_PARAM_BLD_to_param() converts a built up OSSL_PARAM_BLD structure
59b077aed3SPierre ProncheryI<bld> into an allocated OSSL_PARAM array.
60b077aed3SPierre ProncheryThe OSSL_PARAM array and all associated storage must be freed by calling
61b077aed3SPierre ProncheryOSSL_PARAM_free() with the functions return value.
62b077aed3SPierre ProncheryOSSL_PARAM_BLD_free() can safely be called any time after this function is.
63b077aed3SPierre Pronchery
64b077aed3SPierre Pronchery=begin comment
65b077aed3SPierre Pronchery
66b077aed3SPierre ProncheryPOD is pretty good at recognising function names and making them appropriately
67b077aed3SPierre Proncherybold...  however, when part of the function name is variable, we have to help
68b077aed3SPierre Proncherythe processor along
69b077aed3SPierre Pronchery
70b077aed3SPierre Pronchery=end comment
71b077aed3SPierre Pronchery
72b077aed3SPierre ProncheryB<OSSL_PARAM_BLD_push_I<TYPE>>() are a series of functions which will create
73b077aed3SPierre ProncheryOSSL_PARAM objects of the specified size and correct type for the I<val>
74b077aed3SPierre Proncheryargument.
75b077aed3SPierre ProncheryI<val> is stored by value and an expression or auto variable can be used.
76b077aed3SPierre Pronchery
77b077aed3SPierre ProncheryOSSL_PARAM_BLD_push_BN() is a function that will create an OSSL_PARAM object
78b077aed3SPierre Proncherythat holds the specified BIGNUM I<bn>.
79b077aed3SPierre ProncheryIf I<bn> is marked as being securely allocated, its OSSL_PARAM representation
80b077aed3SPierre Proncherywill also be securely allocated.
81b077aed3SPierre ProncheryThe I<bn> argument is stored by reference and the underlying BIGNUM object
82b077aed3SPierre Proncherymust exist until after OSSL_PARAM_BLD_to_param() has been called.
83b077aed3SPierre Pronchery
84b077aed3SPierre ProncheryOSSL_PARAM_BLD_push_BN_pad() is a function that will create an OSSL_PARAM object
85b077aed3SPierre Proncherythat holds the specified BIGNUM I<bn>.
86b077aed3SPierre ProncheryThe object will be padded to occupy exactly I<sz> bytes, if insufficient space
87b077aed3SPierre Proncheryis specified an error results.
88b077aed3SPierre ProncheryIf I<bn> is marked as being securely allocated, its OSSL_PARAM representation
89b077aed3SPierre Proncherywill also be securely allocated.
90b077aed3SPierre ProncheryThe I<bn> argument is stored by reference and the underlying BIGNUM object
91b077aed3SPierre Proncherymust exist until after OSSL_PARAM_BLD_to_param() has been called.
92b077aed3SPierre Pronchery
93b077aed3SPierre ProncheryOSSL_PARAM_BLD_push_utf8_string() is a function that will create an OSSL_PARAM
94b077aed3SPierre Proncheryobject that references the UTF8 string specified by I<buf>.
95b077aed3SPierre ProncheryThe length of the string I<bsize> should not include the terminating NUL byte.
96b077aed3SPierre ProncheryIf it is zero then it will be calculated.
97b077aed3SPierre ProncheryThe string that I<buf> points to is stored by reference and must remain in
98b077aed3SPierre Proncheryscope until after OSSL_PARAM_BLD_to_param() has been called.
99b077aed3SPierre Pronchery
100b077aed3SPierre ProncheryOSSL_PARAM_BLD_push_octet_string() is a function that will create an OSSL_PARAM
101b077aed3SPierre Proncheryobject that references the octet string specified by I<buf> and <bsize>.
102b077aed3SPierre ProncheryThe memory that I<buf> points to is stored by reference and must remain in
103b077aed3SPierre Proncheryscope until after OSSL_PARAM_BLD_to_param() has been called.
104b077aed3SPierre Pronchery
105b077aed3SPierre ProncheryOSSL_PARAM_BLD_push_utf8_ptr() is a function that will create an OSSL_PARAM
106b077aed3SPierre Proncheryobject that references the UTF8 string specified by I<buf>.
107b077aed3SPierre ProncheryThe length of the string I<bsize> should not include the terminating NUL byte.
108b077aed3SPierre ProncheryIf it is zero then it will be calculated.
109b077aed3SPierre ProncheryThe string I<buf> points to is stored by reference and must remain in
110b077aed3SPierre Proncheryscope until the OSSL_PARAM array is freed.
111b077aed3SPierre Pronchery
112b077aed3SPierre ProncheryOSSL_PARAM_BLD_push_octet_ptr() is a function that will create an OSSL_PARAM
113b077aed3SPierre Proncheryobject that references the octet string specified by I<buf>.
114b077aed3SPierre ProncheryThe memory I<buf> points to is stored by reference and must remain in
115b077aed3SPierre Proncheryscope until the OSSL_PARAM array is freed.
116b077aed3SPierre Pronchery
117b077aed3SPierre Pronchery=head1 RETURN VALUES
118b077aed3SPierre Pronchery
119b077aed3SPierre ProncheryOSSL_PARAM_BLD_new() returns the allocated OSSL_PARAM_BLD structure, or NULL
120b077aed3SPierre Proncheryon error.
121b077aed3SPierre Pronchery
122b077aed3SPierre ProncheryOSSL_PARAM_BLD_to_param() returns the allocated OSSL_PARAM array, or NULL
123b077aed3SPierre Proncheryon error.
124b077aed3SPierre Pronchery
125b077aed3SPierre ProncheryAll of the OSSL_PARAM_BLD_push_TYPE functions return 1 on success and 0
126b077aed3SPierre Proncheryon error.
127b077aed3SPierre Pronchery
128b077aed3SPierre Pronchery=head1 NOTES
129b077aed3SPierre Pronchery
130b077aed3SPierre ProncheryOSSL_PARAM_BLD_push_BN() and OSSL_PARAM_BLD_push_BN_pad() currently only
131b077aed3SPierre Proncherysupport nonnegative B<BIGNUM>s.  They return an error on negative B<BIGNUM>s.
132b077aed3SPierre Pronchery
133b077aed3SPierre Pronchery=head1 EXAMPLES
134b077aed3SPierre Pronchery
135b077aed3SPierre ProncheryBoth examples creating an OSSL_PARAM array that contains an RSA key.
136b077aed3SPierre ProncheryFor both, the predefined key variables are:
137b077aed3SPierre Pronchery
138b077aed3SPierre Pronchery    BIGNUM *n;           /* modulus */
139b077aed3SPierre Pronchery    unsigned int e;      /* public exponent */
140b077aed3SPierre Pronchery    BIGNUM *d;           /* private exponent */
141b077aed3SPierre Pronchery    BIGNUM *p, *q;       /* first two prime factors */
142b077aed3SPierre Pronchery    BIGNUM *dmp1, *dmq1; /* first two CRT exponents */
143b077aed3SPierre Pronchery    BIGNUM *iqmp;        /* first CRT coefficient */
144b077aed3SPierre Pronchery
145b077aed3SPierre Pronchery=head2 Example 1
146b077aed3SPierre Pronchery
147b077aed3SPierre ProncheryThis example shows how to create an OSSL_PARAM array that contains an RSA
148b077aed3SPierre Proncheryprivate key.
149b077aed3SPierre Pronchery
150b077aed3SPierre Pronchery    OSSL_PARAM_BLD *bld = OSSL_PARAM_BLD_new();
151b077aed3SPierre Pronchery    OSSL_PARAM *params = NULL;
152b077aed3SPierre Pronchery
153b077aed3SPierre Pronchery    if (bld == NULL
154b077aed3SPierre Pronchery        || !OSSL_PARAM_BLD_push_BN(bld, "n", n)
155b077aed3SPierre Pronchery        || !OSSL_PARAM_BLD_push_uint(bld, "e", e)
156b077aed3SPierre Pronchery        || !OSSL_PARAM_BLD_push_BN(bld, "d", d)
157b077aed3SPierre Pronchery        || !OSSL_PARAM_BLD_push_BN(bld, "rsa-factor1", p)
158b077aed3SPierre Pronchery        || !OSSL_PARAM_BLD_push_BN(bld, "rsa-factor2", q)
159b077aed3SPierre Pronchery        || !OSSL_PARAM_BLD_push_BN(bld, "rsa-exponent1", dmp1)
160b077aed3SPierre Pronchery        || !OSSL_PARAM_BLD_push_BN(bld, "rsa-exponent2", dmq1)
161b077aed3SPierre Pronchery        || !OSSL_PARAM_BLD_push_BN(bld, "rsa-coefficient1", iqmp)
162b077aed3SPierre Pronchery        || (params = OSSL_PARAM_BLD_to_param(bld)) == NULL)
163b077aed3SPierre Pronchery        goto err;
164b077aed3SPierre Pronchery    OSSL_PARAM_BLD_free(bld);
165b077aed3SPierre Pronchery    /* Use params */
166b077aed3SPierre Pronchery    ...
167b077aed3SPierre Pronchery    OSSL_PARAM_free(params);
168b077aed3SPierre Pronchery
169b077aed3SPierre Pronchery=head2 Example 2
170b077aed3SPierre Pronchery
171b077aed3SPierre ProncheryThis example shows how to create an OSSL_PARAM array that contains an RSA
172b077aed3SPierre Proncherypublic key.
173b077aed3SPierre Pronchery
174b077aed3SPierre Pronchery    OSSL_PARAM_BLD *bld = OSSL_PARAM_BLD_new();
175b077aed3SPierre Pronchery    OSSL_PARAM *params = NULL;
176b077aed3SPierre Pronchery
177b077aed3SPierre Pronchery    if (nld == NULL
178b077aed3SPierre Pronchery        || !OSSL_PARAM_BLD_push_BN(bld, "n", n)
179b077aed3SPierre Pronchery        || !OSSL_PARAM_BLD_push_uint(bld, "e", e)
180b077aed3SPierre Pronchery        || (params = OSSL_PARAM_BLD_to_param(bld)) == NULL)
181b077aed3SPierre Pronchery        goto err;
182b077aed3SPierre Pronchery    OSSL_PARAM_BLD_free(bld);
183b077aed3SPierre Pronchery    /* Use params */
184b077aed3SPierre Pronchery    ...
185b077aed3SPierre Pronchery    OSSL_PARAM_free(params);
186b077aed3SPierre Pronchery
187b077aed3SPierre Pronchery=head1 SEE ALSO
188b077aed3SPierre Pronchery
189b077aed3SPierre ProncheryL<OSSL_PARAM_int(3)>, L<OSSL_PARAM(3)>, L<OSSL_PARAM_free(3)>
190b077aed3SPierre Pronchery
191b077aed3SPierre Pronchery=head1 HISTORY
192b077aed3SPierre Pronchery
193b077aed3SPierre ProncheryThe functions described here were all added in OpenSSL 3.0.
194b077aed3SPierre Pronchery
195b077aed3SPierre Pronchery=head1 COPYRIGHT
196b077aed3SPierre Pronchery
197*a7148ab3SEnji CooperCopyright 2019-2024 The OpenSSL Project Authors. All Rights Reserved.
198b077aed3SPierre Pronchery
199b077aed3SPierre ProncheryLicensed under the Apache License 2.0 (the "License").  You may not use
200b077aed3SPierre Proncherythis file except in compliance with the License.  You can obtain a copy
201b077aed3SPierre Proncheryin the file LICENSE in the source distribution or at
202b077aed3SPierre ProncheryL<https://www.openssl.org/source/license.html>.
203b077aed3SPierre Pronchery
204b077aed3SPierre Pronchery=cut
205