OSSL_PARAM_int.pod 15 KB

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