ossl_param_bld_init.pod 7.1 KB

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