OSSL_PARAM.pod 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. =pod
  2. =head1 NAME
  3. OSSL_PARAM - a structure to pass or request object parameters
  4. =head1 SYNOPSIS
  5. #include <openssl/core.h>
  6. typedef struct ossl_param_st OSSL_PARAM;
  7. struct ossl_param_st {
  8. const char *key; /* the name of the parameter */
  9. unsigned char data_type; /* declare what kind of content is in data */
  10. void *data; /* value being passed in or out */
  11. size_t data_size; /* data size */
  12. size_t return_size; /* returned size */
  13. };
  14. =head1 DESCRIPTION
  15. B<OSSL_PARAM> is a type that allows passing arbitrary data for some
  16. object between two parties that have no or very little shared
  17. knowledge about their respective internal structures for that object.
  18. A typical usage example could be an application that wants to set some
  19. parameters for an object, or wants to find out some parameters of an
  20. object.
  21. Arrays of this type can be used for the following purposes:
  22. =over 4
  23. =item * Setting parameters for some object
  24. The caller sets up the B<OSSL_PARAM> array and calls some function
  25. (the I<setter>) that has intimate knowledge about the object that can
  26. take the data from the B<OSSL_PARAM> array and assign them in a
  27. suitable form for the internal structure of the object.
  28. =item * Request parameters of some object
  29. The caller (the I<requester>) sets up the B<OSSL_PARAM> array and
  30. calls some function (the I<responder>) that has intimate knowledge
  31. about the object, which can take the internal data of the object and
  32. copy (possibly convert) that to the memory prepared by the
  33. I<requester> and pointed at with the B<OSSL_PARAM> I<data>.
  34. =item * Request parameter descriptors
  35. The caller gets an array of constant B<OSSL_PARAM>, which describe
  36. available parameters and some of their properties; name, data type and
  37. expected data size.
  38. For a detailed description of each field for this use, see the field
  39. descriptions below.
  40. The caller may then use the information from this descriptor array to
  41. build up its own B<OSSL_PARAM> array to pass down to a I<setter> or
  42. I<responder>.
  43. =back
  44. Normally, the order of the an B<OSSL_PARAM> array is not relevant.
  45. However, if the I<responder> can handle multiple elements with the
  46. same key, those elements must be handled in the order they are in.
  47. An B<OSSL_PARAM> array must have a terminating element, where I<key>
  48. is NULL. The usual full terminating template is:
  49. { NULL, 0, NULL, 0, 0 }
  50. This can also be specified using L<OSSL_PARAM_END(3)>.
  51. =head2 Functional support
  52. Libcrypto offers a limited set of helper functions to handle
  53. B<OSSL_PARAM> items and arrays, please see L<OSSL_PARAM_get_int(3)>.
  54. Developers are free to extend or replace those as they see fit.
  55. =head2 B<OSSL_PARAM> fields
  56. =over 4
  57. =item I<key>
  58. The identity of the parameter in the form of a string.
  59. In an B<OSSL_PARAM> array, an item with this field set to NULL is
  60. considered a terminating item.
  61. =item I<data_type>
  62. The I<data_type> is a value that describes the type and organization of
  63. the data.
  64. See L</Supported types> below for a description of the types.
  65. =item I<data>
  66. =item I<data_size>
  67. I<data> is a pointer to the memory where the parameter data is (when
  68. setting parameters) or shall (when requesting parameters) be stored,
  69. and I<data_size> is its size in bytes.
  70. The organization of the data depends on the parameter type and flag.
  71. The I<data_size> needs special attention with the parameter type
  72. B<OSSL_PARAM_UTF8_STRING> in relation to C strings. When setting
  73. parameters, the size should be set to the length of the string, not
  74. counting the terminating NUL byte. When requesting parameters, the
  75. size should be set to the size of the buffer to be populated, which
  76. should accommodate enough space for a terminating NUL byte.
  77. When I<requesting parameters>, it's acceptable for I<data> to be NULL.
  78. This can be used by the I<requester> to figure out dynamically exactly
  79. how much buffer space is needed to store the parameter data.
  80. In this case, I<data_size> is ignored.
  81. When the B<OSSL_PARAM> is used as a parameter descriptor, I<data>
  82. should be ignored.
  83. If I<data_size> is zero, it means that an arbitrary data size is
  84. accepted, otherwise it specifies the maximum size allowed.
  85. =item I<return_size>
  86. When an array of B<OSSL_PARAM> is used to request data, the
  87. I<responder> must set this field to indicate size of the parameter
  88. data, including padding as the case may be.
  89. In case the I<data_size> is an unsuitable size for the data, the
  90. I<responder> must still set this field to indicate the minimum data
  91. size required.
  92. (further notes on this in L</NOTES> below).
  93. When the B<OSSL_PARAM> is used as a parameter descriptor,
  94. I<return_size> should be ignored.
  95. =back
  96. B<NOTE:>
  97. The key names and associated types are defined by the entity that
  98. offers these parameters, i.e. names for parameters provided by the
  99. OpenSSL libraries are defined by the libraries, and names for
  100. parameters provided by providers are defined by those providers,
  101. except for the pointer form of strings (see data type descriptions
  102. below).
  103. Entities that want to set or request parameters need to know what
  104. those keys are and of what type, any functionality between those two
  105. entities should remain oblivious and just pass the B<OSSL_PARAM> array
  106. along.
  107. =head2 Supported types
  108. The I<data_type> field can be one of the following types:
  109. =over 4
  110. =item B<OSSL_PARAM_INTEGER>
  111. =item B<OSSL_PARAM_UNSIGNED_INTEGER>
  112. The parameter data is an integer (signed or unsigned) of arbitrary
  113. length, organized in native form, i.e. most significant byte first on
  114. Big-Endian systems, and least significant byte first on Little-Endian
  115. systems.
  116. =item B<OSSL_PARAM_REAL>
  117. The parameter data is a floating point value in native form.
  118. =item B<OSSL_PARAM_UTF8_STRING>
  119. The parameter data is a printable string.
  120. =item B<OSSL_PARAM_OCTET_STRING>
  121. The parameter data is an arbitrary string of bytes.
  122. =item B<OSSL_PARAM_UTF8_PTR>
  123. The parameter data is a pointer to a printable string.
  124. The difference between this and B<OSSL_PARAM_UTF8_STRING> is that I<data>
  125. doesn't point directly at the data, but to a pointer that points to the data.
  126. If there is any uncertainty about which to use, B<OSSL_PARAM_UTF8_STRING> is
  127. almost certainly the correct choice.
  128. This is used to indicate that constant data is or will be passed,
  129. and there is therefore no need to copy the data that is passed, just
  130. the pointer to it.
  131. I<data_size> must be set to the size of the data, not the size of the
  132. pointer to the data.
  133. If this is used in a parameter request,
  134. I<data_size> is not relevant. However, the I<responder> will set
  135. I<return_size> to the size of the data.
  136. Note that the use of this type is B<fragile> and can only be safely
  137. used for data that remains constant and in a constant location for a
  138. long enough duration (such as the life-time of the entity that
  139. offers these parameters).
  140. =item B<OSSL_PARAM_OCTET_PTR>
  141. The parameter data is a pointer to an arbitrary string of bytes.
  142. The difference between this and B<OSSL_PARAM_OCTET_STRING> is that
  143. I<data> doesn't point directly at the data, but to a pointer that
  144. points to the data.
  145. If there is any uncertainty about which to use, B<OSSL_PARAM_OCTET_STRING> is
  146. almost certainly the correct choice.
  147. This is used to indicate that constant data is or will be passed, and
  148. there is therefore no need to copy the data that is passed, just the
  149. pointer to it.
  150. I<data_size> must be set to the size of the data, not the size of the
  151. pointer to the data.
  152. If this is used in a parameter request,
  153. I<data_size> is not relevant. However, the I<responder> will set
  154. I<return_size> to the size of the data.
  155. Note that the use of this type is B<fragile> and can only be safely
  156. used for data that remains constant and in a constant location for a
  157. long enough duration (such as the life-time of the entity that
  158. offers these parameters).
  159. =back
  160. =head1 NOTES
  161. Both when setting and requesting parameters, the functions that are
  162. called will have to decide what is and what is not an error.
  163. The recommended behaviour is:
  164. =over 4
  165. =item *
  166. Keys that a I<setter> or I<responder> doesn't recognise should simply
  167. be ignored.
  168. That in itself isn't an error.
  169. =item *
  170. If the keys that a called I<setter> recognises form a consistent
  171. enough set of data, that call should succeed.
  172. =item *
  173. Apart from the I<return_size>, a I<responder> must never change the fields
  174. of an B<OSSL_PARAM>.
  175. To return a value, it should change the contents of the memory that
  176. I<data> points at.
  177. =item *
  178. If the data type for a key that it's associated with is incorrect,
  179. the called function may return an error.
  180. The called function may also try to convert the data to a suitable
  181. form (for example, it's plausible to pass a large number as an octet
  182. string, so even though a given key is defined as an
  183. B<OSSL_PARAM_UNSIGNED_INTEGER>, is plausible to pass the value as an
  184. B<OSSL_PARAM_OCTET_STRING>), but this is in no way mandatory.
  185. =item *
  186. If I<data> for a B<OSSL_PARAM_OCTET_STRING> or a
  187. B<OSSL_PARAM_UTF8_STRING> is NULL, the I<responder> should
  188. set I<return_size> to the size of the item to be returned
  189. and return success. Later the responder will be called again
  190. with I<data> pointing at the place for the value to be put.
  191. =item *
  192. If a I<responder> finds that some data sizes are too small for the
  193. requested data, it must set I<return_size> for each such
  194. B<OSSL_PARAM> item to the minimum required size, and eventually return
  195. an error.
  196. =item *
  197. For the integer type parameters (B<OSSL_PARAM_UNSIGNED_INTEGER> and
  198. B<OSSL_PARAM_INTEGER>), a I<responder> may choose to return an error
  199. if the I<data_size> isn't a suitable size (even if I<data_size> is
  200. bigger than needed). If the I<responder> finds the size suitable, it
  201. must fill all I<data_size> bytes and ensure correct padding for the
  202. native endianness, and set I<return_size> to the same value as
  203. I<data_size>.
  204. =back
  205. =begin comment RETURN VALUES doesn't make sense for a manual that only
  206. describes a type, but document checkers still want that section, and
  207. to have more than just the section title.
  208. =head1 RETURN VALUES
  209. txt
  210. =end comment
  211. =head1 EXAMPLES
  212. A couple of examples to just show how B<OSSL_PARAM> arrays could be
  213. set up.
  214. =head3 Example 1
  215. This example is for setting parameters on some object:
  216. #include <openssl/core.h>
  217. const char *foo = "some string";
  218. size_t foo_l = strlen(foo);
  219. const char bar[] = "some other string";
  220. OSSL_PARAM set[] = {
  221. { "foo", OSSL_PARAM_UTF8_PTR, &foo, foo_l, 0 },
  222. { "bar", OSSL_PARAM_UTF8_STRING, (void *)&bar, sizeof(bar) - 1, 0 },
  223. { NULL, 0, NULL, 0, 0 }
  224. };
  225. =head3 Example 2
  226. This example is for requesting parameters on some object:
  227. const char *foo = NULL;
  228. size_t foo_l;
  229. char bar[1024];
  230. size_t bar_l;
  231. OSSL_PARAM request[] = {
  232. { "foo", OSSL_PARAM_UTF8_PTR, &foo, 0 /*irrelevant*/, 0 },
  233. { "bar", OSSL_PARAM_UTF8_STRING, &bar, sizeof(bar), 0 },
  234. { NULL, 0, NULL, 0, 0 }
  235. };
  236. A I<responder> that receives this array (as I<params> in this example)
  237. could fill in the parameters like this:
  238. /* OSSL_PARAM *params */
  239. int i;
  240. for (i = 0; params[i].key != NULL; i++) {
  241. if (strcmp(params[i].key, "foo") == 0) {
  242. *(char **)params[i].data = "foo value";
  243. params[i].return_size = 9; /* length of "foo value" string */
  244. } else if (strcmp(params[i].key, "bar") == 0) {
  245. memcpy(params[i].data, "bar value", 10);
  246. params[i].return_size = 9; /* length of "bar value" string */
  247. }
  248. /* Ignore stuff we don't know */
  249. }
  250. =head1 SEE ALSO
  251. L<openssl-core.h(7)>, L<OSSL_PARAM_get_int(3)>, L<OSSL_PARAM_dup(3)>
  252. =head1 HISTORY
  253. B<OSSL_PARAM> was added in OpenSSL 3.0.
  254. =head1 COPYRIGHT
  255. Copyright 2019-2023 The OpenSSL Project Authors. All Rights Reserved.
  256. Licensed under the Apache License 2.0 (the "License"). You may not use
  257. this file except in compliance with the License. You can obtain a copy
  258. in the file LICENSE in the source distribution or at
  259. L<https://www.openssl.org/source/license.html>.
  260. =cut