OSSL_PARAM_BLD.pod 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. =pod
  2. =head1 NAME
  3. OSSL_PARAM_BLD, OSSL_PARAM_BLD_new, OSSL_PARAM_BLD_to_param,
  4. OSSL_PARAM_BLD_free, OSSL_PARAM_BLD_push_int,
  5. OSSL_PARAM_BLD_push_uint, OSSL_PARAM_BLD_push_long,
  6. OSSL_PARAM_BLD_push_ulong, OSSL_PARAM_BLD_push_int32,
  7. OSSL_PARAM_BLD_push_uint32, OSSL_PARAM_BLD_push_int64,
  8. OSSL_PARAM_BLD_push_uint64, OSSL_PARAM_BLD_push_size_t,
  9. OSSL_PARAM_BLD_push_time_t, OSSL_PARAM_BLD_push_double,
  10. OSSL_PARAM_BLD_push_BN, OSSL_PARAM_BLD_push_BN_pad,
  11. OSSL_PARAM_BLD_push_utf8_string, OSSL_PARAM_BLD_push_utf8_ptr,
  12. OSSL_PARAM_BLD_push_octet_string, OSSL_PARAM_BLD_push_octet_ptr
  13. - functions to assist in the creation of OSSL_PARAM arrays
  14. =head1 SYNOPSIS
  15. =for openssl generic
  16. #include <openssl/param_build.h>
  17. typedef struct OSSL_PARAM_BLD;
  18. OSSL_PARAM_BLD *OSSL_PARAM_BLD_new(void);
  19. OSSL_PARAM *OSSL_PARAM_BLD_to_param(OSSL_PARAM_BLD *bld);
  20. void OSSL_PARAM_BLD_free(OSSL_PARAM_BLD *bld);
  21. int OSSL_PARAM_BLD_push_TYPE(OSSL_PARAM_BLD *bld, const char *key, TYPE val);
  22. int OSSL_PARAM_BLD_push_BN(OSSL_PARAM_BLD *bld, const char *key,
  23. const BIGNUM *bn);
  24. int OSSL_PARAM_BLD_push_BN_pad(OSSL_PARAM_BLD *bld, const char *key,
  25. const BIGNUM *bn, size_t sz);
  26. int OSSL_PARAM_BLD_push_utf8_string(OSSL_PARAM_BLD *bld, const char *key,
  27. const char *buf, size_t bsize);
  28. int OSSL_PARAM_BLD_push_utf8_ptr(OSSL_PARAM_BLD *bld, const char *key,
  29. char *buf, size_t bsize);
  30. int OSSL_PARAM_BLD_push_octet_string(OSSL_PARAM_BLD *bld, const char *key,
  31. const void *buf, size_t bsize);
  32. int OSSL_PARAM_BLD_push_octet_ptr(OSSL_PARAM_BLD *bld, const char *key,
  33. void *buf, size_t bsize);
  34. =head1 DESCRIPTION
  35. A collection of utility functions that simplify the creation of OSSL_PARAM
  36. arrays. The B<I<TYPE>> names are as per L<OSSL_PARAM_int(3)>.
  37. OSSL_PARAM_BLD_new() allocates and initialises a new OSSL_PARAM_BLD structure
  38. so that values can be added.
  39. Any existing values are cleared.
  40. OSSL_PARAM_BLD_free() deallocates the memory allocates by OSSL_PARAM_BLD_new().
  41. If the argument is NULL, nothing is done.
  42. OSSL_PARAM_BLD_to_param() converts a built up OSSL_PARAM_BLD structure
  43. I<bld> into an allocated OSSL_PARAM array.
  44. The OSSL_PARAM array and all associated storage must be freed by calling
  45. OSSL_PARAM_free() with the functions return value.
  46. OSSL_PARAM_BLD_free() can safely be called any time after this function is.
  47. =begin comment
  48. POD is pretty good at recognising function names and making them appropriately
  49. bold... however, when part of the function name is variable, we have to help
  50. the processor along
  51. =end comment
  52. B<OSSL_PARAM_BLD_push_I<TYPE>>() are a series of functions which will create
  53. OSSL_PARAM objects of the specified size and correct type for the I<val>
  54. argument.
  55. I<val> is stored by value and an expression or auto variable can be used.
  56. When B<I<TYPE>> denotes an integer type, signed integer types will normally
  57. get the OSSL_PARAM type B<OSSL_PARAM_INTEGER> params.
  58. When B<I<TYPE>> denotes an unsigned integer type will get the OSSL_PARAM type
  59. B<OSSL_PARAM_UNSIGNED_INTEGER>.
  60. OSSL_PARAM_BLD_push_BN() is a function that will create an OSSL_PARAM object
  61. that holds the specified BIGNUM I<bn>.
  62. When the I<bn> is zero or positive, its OSSL_PARAM type becomes
  63. B<OSSL_PARAM_UNSIGNED_INTEGER>.
  64. When the I<bn> is negative, its OSSL_PARAM type becomes B<OSSL_PARAM_INTEGER>.
  65. If I<bn> is marked as being securely allocated, its OSSL_PARAM representation
  66. will also be securely allocated.
  67. The I<bn> argument is stored by reference and the underlying BIGNUM object
  68. must exist until after OSSL_PARAM_BLD_to_param() has been called.
  69. OSSL_PARAM_BLD_push_BN_pad() is a function that will create an OSSL_PARAM object
  70. that holds the specified BIGNUM I<bn>.
  71. The object will be padded to occupy exactly I<sz> bytes, if insufficient space
  72. is specified an error results.
  73. When the I<bn> is zero or positive, its OSSL_PARAM type becomes
  74. B<OSSL_PARAM_UNSIGNED_INTEGER>.
  75. When the I<bn> is negative, its OSSL_PARAM type becomes B<OSSL_PARAM_INTEGER>.
  76. If I<bn> is marked as being securely allocated, its OSSL_PARAM representation
  77. will also be securely allocated.
  78. The I<bn> argument is stored by reference and the underlying BIGNUM object
  79. must exist until after OSSL_PARAM_BLD_to_param() has been called.
  80. OSSL_PARAM_BLD_push_utf8_string() is a function that will create an OSSL_PARAM
  81. object that references the UTF8 string specified by I<buf>.
  82. The length of the string I<bsize> should not include the terminating NUL byte.
  83. If it is zero then it will be calculated.
  84. The string that I<buf> points to is stored by reference and must remain in
  85. scope until after OSSL_PARAM_BLD_to_param() has been called.
  86. OSSL_PARAM_BLD_push_octet_string() is a function that will create an OSSL_PARAM
  87. object that references the octet string specified by I<buf> and <bsize>.
  88. The memory that I<buf> points to is stored by reference and must remain in
  89. scope until after OSSL_PARAM_BLD_to_param() has been called.
  90. OSSL_PARAM_BLD_push_utf8_ptr() is a function that will create an OSSL_PARAM
  91. object that references the UTF8 string specified by I<buf>.
  92. The length of the string I<bsize> should not include the terminating NUL byte.
  93. If it is zero then it will be calculated.
  94. The string I<buf> points to is stored by reference and must remain in
  95. scope until the OSSL_PARAM array is freed.
  96. OSSL_PARAM_BLD_push_octet_ptr() is a function that will create an OSSL_PARAM
  97. object that references the octet string specified by I<buf>.
  98. The memory I<buf> points to is stored by reference and must remain in
  99. scope until the OSSL_PARAM array is freed.
  100. =head1 RETURN VALUES
  101. OSSL_PARAM_BLD_new() returns the allocated OSSL_PARAM_BLD structure, or NULL
  102. on error.
  103. OSSL_PARAM_BLD_to_param() returns the allocated OSSL_PARAM array, or NULL
  104. on error.
  105. All of the OSSL_PARAM_BLD_push_TYPE functions return 1 on success and 0
  106. on error.
  107. =head1 NOTES
  108. OSSL_PARAM_BLD_push_BN() and OSSL_PARAM_BLD_push_BN_pad() only
  109. support nonnegative B<BIGNUM>s. They return an error on negative
  110. B<BIGNUM>s.
  111. To pass signed B<BIGNUM>s, use OSSL_PARAM_BLD_push_signed_BN().
  112. =head1 EXAMPLES
  113. Both examples creating an OSSL_PARAM array that contains an RSA key.
  114. For both, the predefined key variables are:
  115. BIGNUM *n; /* modulus */
  116. unsigned int e; /* public exponent */
  117. BIGNUM *d; /* private exponent */
  118. BIGNUM *p, *q; /* first two prime factors */
  119. BIGNUM *dmp1, *dmq1; /* first two CRT exponents */
  120. BIGNUM *iqmp; /* first CRT coefficient */
  121. =head2 Example 1
  122. This example shows how to create an OSSL_PARAM array that contains an RSA
  123. private key.
  124. OSSL_PARAM_BLD *bld = OSSL_PARAM_BLD_new();
  125. OSSL_PARAM *params = NULL;
  126. if (bld == NULL
  127. || !OSSL_PARAM_BLD_push_BN(bld, "n", n)
  128. || !OSSL_PARAM_BLD_push_uint(bld, "e", e)
  129. || !OSSL_PARAM_BLD_push_BN(bld, "d", d)
  130. || !OSSL_PARAM_BLD_push_BN(bld, "rsa-factor1", p)
  131. || !OSSL_PARAM_BLD_push_BN(bld, "rsa-factor2", q)
  132. || !OSSL_PARAM_BLD_push_BN(bld, "rsa-exponent1", dmp1)
  133. || !OSSL_PARAM_BLD_push_BN(bld, "rsa-exponent2", dmq1)
  134. || !OSSL_PARAM_BLD_push_BN(bld, "rsa-coefficient1", iqmp)
  135. || (params = OSSL_PARAM_BLD_to_param(bld)) == NULL)
  136. goto err;
  137. OSSL_PARAM_BLD_free(bld);
  138. /* Use params */
  139. ...
  140. OSSL_PARAM_free(params);
  141. =head2 Example 2
  142. This example shows how to create an OSSL_PARAM array that contains an RSA
  143. public key.
  144. OSSL_PARAM_BLD *bld = OSSL_PARAM_BLD_new();
  145. OSSL_PARAM *params = NULL;
  146. if (nld == NULL
  147. || !OSSL_PARAM_BLD_push_BN(bld, "n", n)
  148. || !OSSL_PARAM_BLD_push_uint(bld, "e", e)
  149. || (params = OSSL_PARAM_BLD_to_param(bld)) == NULL)
  150. goto err;
  151. OSSL_PARAM_BLD_free(bld);
  152. /* Use params */
  153. ...
  154. OSSL_PARAM_free(params);
  155. =head1 SEE ALSO
  156. L<OSSL_PARAM_int(3)>, L<OSSL_PARAM(3)>, L<OSSL_PARAM_free(3)>
  157. =head1 HISTORY
  158. The functions described here were all added in OpenSSL 3.0.
  159. =head1 COPYRIGHT
  160. Copyright 2019-2024 The OpenSSL Project Authors. All Rights Reserved.
  161. Licensed under the Apache License 2.0 (the "License"). You may not use
  162. this file except in compliance with the License. You can obtain a copy
  163. in the file LICENSE in the source distribution or at
  164. L<https://www.openssl.org/source/license.html>.
  165. =cut