ossl_param_bld_init.pod 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. =pod
  2. =head1 NAME
  3. ossl_param_bld_init, ossl_param_bld_to_param, ossl_param_bld_to_param_ex,
  4. ossl_param_bld_free, ossl_param_bld_push_int, ossl_param_bld_push_uint,
  5. ossl_param_bld_push_long, ossl_param_bld_push_ulong,
  6. ossl_param_bld_push_int32, ossl_param_bld_push_uint32,
  7. ossl_param_bld_push_int64, ossl_param_bld_push_uint64,
  8. ossl_param_bld_push_size_t, ossl_param_bld_push_double,
  9. ossl_param_bld_push_BN, ossl_param_bld_push_BN_pad,
  10. ossl_param_bld_push_utf8_string, ossl_param_bld_push_utf8_ptr,
  11. ossl_param_bld_push_octet_string, ossl_param_bld_push_octet_ptr
  12. - functions to assist in the creation of OSSL_PARAM arrays
  13. =head1 SYNOPSIS
  14. =for openssl generic
  15. #include "internal/params_build.h"
  16. #define OSSL_PARAM_BLD_MAX 10
  17. typedef struct { ... } OSSL_PARAM_BLD;
  18. void ossl_param_bld_init(OSSL_PARAM_BLD *bld);
  19. OSSL_PARAM *ossl_param_bld_to_param(OSSL_PARAM_BLD *bld);
  20. OSSL_PARAM *ossl_param_bld_to_param_ex(OSSL_PARAM_BLD *bld,
  21. OSSL_PARAM *params, size_t param_n,
  22. void *data, size_t data_n,
  23. void *secure, size_t secure_n);
  24. void ossl_param_bld_free(OSSL_PARAM *params);
  25. int ossl_param_bld_push_TYPE(OSSL_PARAM_BLD *bld, const char *key, TYPE val);
  26. int ossl_param_bld_push_BN(OSSL_PARAM_BLD *bld, const char *key,
  27. const BIGNUM *bn);
  28. int ossl_param_bld_push_BN_pad(OSSL_PARAM_BLD *bld, const char *key,
  29. const BIGNUM *bn, size_t sz);
  30. int ossl_param_bld_push_utf8_string(OSSL_PARAM_BLD *bld, const char *key,
  31. const char *buf, size_t bsize);
  32. int ossl_param_bld_push_utf8_ptr(OSSL_PARAM_BLD *bld, const char *key,
  33. char *buf, size_t bsize);
  34. int ossl_param_bld_push_octet_string(OSSL_PARAM_BLD *bld, const char *key,
  35. const void *buf, size_t bsize);
  36. int ossl_param_bld_push_octet_ptr(OSSL_PARAM_BLD *bld, const char *key,
  37. void *buf, size_t bsize);
  38. =head1 DESCRIPTION
  39. A collection of utility functions that simplify the creation of OSSL_PARAM
  40. arrays. The B<I<TYPE>> names are as per L<OSSL_PARAM_int(3)>.
  41. ossl_param_bld_init() initialises the OSSL_PARAM_BLD structure so that values
  42. can be added.
  43. Any existing values are cleared.
  44. ossl_param_bld_to_param() converts a built up OSSL_PARAM_BLD structure
  45. I<bld> into an allocated OSSL_PARAM array.
  46. The OSSL_PARAM array and all associated storage must be freed by calling
  47. ossl_param_bld_free() with the functions return value.
  48. ossl_param_bld_free() deallocates the memory allocated by
  49. ossl_param_bld_to_param().
  50. ossl_param_bld_to_param_ex() behaves like ossl_param_bld_to_param(), except that
  51. no additional memory is allocated.
  52. An OSSL_PARAM array of at least I<param_n> elements is passed in as I<params>.
  53. The auxiliary storage for the parameters is a block of memory pointed to
  54. by I<data> of at least I<data_n> bytes in size.
  55. If required, secure memory for private BIGNUMs should be pointed to by
  56. I<secure> of at least I<secure_n> bytes in size.
  57. =begin comment
  58. POD is pretty good at recognising function names and making them appropriately
  59. bold... however, when part of the function name is variable, we have to help
  60. the processor along
  61. =end comment
  62. B<ossl_param_bld_push_I<TYPE>>() are a series of functions which will create
  63. OSSL_PARAM objects of the specified size and correct type for the I<val>
  64. argument.
  65. I<val> is stored by value and an expression or auto variable can be used.
  66. ossl_param_bld_push_BN() is a function that will create an OSSL_PARAM object
  67. that holds the specified BIGNUM I<bn>.
  68. If I<bn> is marked as being securely allocated, it's OSSL_PARAM representation
  69. will also be securely allocated.
  70. The I<bn> argument is stored by reference and the underlying BIGNUM object
  71. must exist until after ossl_param_bld_to_param() has been called.
  72. ossl_param_bld_push_BN_pad() is a function that will create an OSSL_PARAM object
  73. that holds the specified BIGNUM I<bn>.
  74. The object will be padded to occupy exactly I<sz> bytes, if insufficient space
  75. is specified an error results.
  76. If I<bn> is marked as being securely allocated, it's 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. If the length of the string, I<bsize>, is zero then it will be calculated.
  83. The string that I<buf> points to is stored by reference and must remain in
  84. scope until after ossl_param_bld_to_param() has been called.
  85. ossl_param_bld_push_octet_string() is a function that will create an OSSL_PARAM
  86. object that references the octet string specified by I<buf> and <bsize>.
  87. The memory that I<buf> points to is stored by reference and must remain in
  88. scope until after ossl_param_bld_to_param() has been called.
  89. ossl_param_bld_push_utf8_ptr() is a function that will create an OSSL_PARAM
  90. object that references the UTF8 string specified by I<buf>.
  91. If the length of the string, I<bsize>, is zero then it will be calculated.
  92. The string I<buf> points to is stored by reference and must remain in
  93. scope until the OSSL_PARAM array is freed.
  94. ossl_param_bld_push_octet_ptr() is a function that will create an OSSL_PARAM
  95. object that references the octet string specified by I<buf>.
  96. The memory I<buf> points to is stored by reference and must remain in
  97. scope until the OSSL_PARAM array is freed.
  98. =head1 RETURN VALUES
  99. ossl_param_bld_to_param() and ossl_param_bld_to_param_ex() return the
  100. allocated OSSL_PARAM array, or NULL on error.
  101. All of the ossl_param_bld_push_TYPE functions return 1 on success and 0
  102. on error.
  103. =head1 NOTES
  104. The constant B<OSSL_PARAM_BLD_MAX> specifies the maximum number of parameters
  105. that can be added.
  106. Exceeding this will result in the push functions returning errors.
  107. The structure B<OSSL_PARAM_BLD> should be considered opaque and subject to
  108. change between versions.
  109. =head1 EXAMPLES
  110. Both examples creating an OSSL_PARAM array that contains an RSA key.
  111. For both, the predefined key variables are:
  112. BIGNUM *p, *q; /* both prime */
  113. BIGNUM *n; /* = p * q */
  114. unsigned int e; /* exponent, usually 65537 */
  115. BIGNUM *d; /* e^-1 */
  116. =head2 Example 1
  117. This example shows how to create an OSSL_PARAM array that contains an RSA
  118. private key.
  119. OSSL_PARAM_BLD bld;
  120. OSSL_PARAM *params;
  121. ossl_param_bld_init(&bld, &secure);
  122. if (!ossl_param_bld_push_BN(&bld, "p", p)
  123. || !ossl_param_bld_push_BN(&bld, "q", q)
  124. || !ossl_param_bld_push_uint(&bld, "e", e)
  125. || !ossl_param_bld_push_BN(&bld, "n", n)
  126. || !ossl_param_bld_push_BN(&bld, "d", d)
  127. || (params = ossl_param_bld_to_param(&bld)) == NULL)
  128. goto err;
  129. /* Use params */
  130. ...
  131. ossl_param_bld_free(params);
  132. =head2 Example 2
  133. This example shows how to create an OSSL_PARAM array that contains an RSA
  134. public key.
  135. OSSL_PARAM_BLD bld;
  136. OSSL_PARAM *params;
  137. ossl_param_bld_init(&bld, &secure);
  138. if (!ossl_param_bld_push_BN(&bld, "n", n)
  139. || !ossl_param_bld_push_BN(&bld, "d", d)
  140. || (params = ossl_param_bld_to_param(&bld)) == NULL)
  141. goto err;
  142. /* Use params */
  143. ...
  144. ossl_param_bld_free(params);
  145. =head1 SEE ALSO
  146. L<OSSL_PARAM_int(3)>, L<OSSL_PARAM(3)>
  147. =head1 HISTORY
  148. The functions described here were all added in OpenSSL 3.0.
  149. =head1 COPYRIGHT
  150. Copyright 2019 The OpenSSL Project Authors. All Rights Reserved.
  151. Licensed under the Apache License 2.0 (the "License"). You may not use
  152. this file except in compliance with the License. You can obtain a copy
  153. in the file LICENSE in the source distribution or at
  154. L<https://www.openssl.org/source/license.html>.
  155. =cut