OSSL_PARAM_construct_from_text.pod 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. =pod
  2. =head1 NAME
  3. OSSL_PARAM_construct_from_text, OSSL_PARAM_allocate_from_text
  4. - OSSL_PARAM construction utilities
  5. =head1 SYNOPSIS
  6. #include <openssl/params.h>
  7. int OSSL_PARAM_construct_from_text(OSSL_PARAM *to,
  8. const OSSL_PARAM *paramdefs,
  9. const char *key, const char *value,
  10. size_t value_n,
  11. void *buf, size_t *buf_n)
  12. int OSSL_PARAM_allocate_from_text(OSSL_PARAM *to,
  13. const OSSL_PARAM *paramdefs,
  14. const char *key, const char *value,
  15. size_t value_n);
  16. =head1 DESCRIPTION
  17. With OpenSSL before version 3.0, parameters were passed down to or
  18. retrieved from algorithm implementations via control functions.
  19. Some of these control functions existed in variants that took string
  20. parameters, for example L<EVP_PKEY_CTX_ctrl_str(3)>.
  21. OpenSSL 3.0 introduces a new mechanism to do the same thing with an
  22. array of parameters that contain name, value, value type and value
  23. size (see L<OSSL_PARAM(3)> for more information).
  24. OSSL_PARAM_construct_from_text() takes a control I<key>, I<value> and
  25. value size I<value_n>, and given a parameter descriptor array
  26. I<paramdefs>, it converts the value to something suitable for
  27. L<OSSL_PARAM(3)> and stores that in the buffer I<buf>, and modifies
  28. the parameter I<to> to match.
  29. I<buf_n>, if not NULL, will be assigned the number of bytes used in
  30. I<buf>.
  31. If I<buf> is NULL, only I<buf_n> will be modified, everything else is
  32. left untouched, allowing a caller to find out how large the buffer
  33. should be.
  34. I<buf> needs to be correctly aligned for the type of the B<OSSL_PARAM>
  35. I<key>.
  36. OSSL_PARAM_allocate_from_text() works like OSSL_PARAM_construct_from_text(),
  37. except it allocates the buffer internally.
  38. The caller must remember to free the data of I<to> when it's not
  39. useful any more.
  40. For parameters having the type B<OSSL_PARAM_INTEGER>,
  41. B<OSSL_PARAM_UNSIGNED_INTEGER>, or B<OSSL_PARAM_OCTET_STRING>, both
  42. functions will interpret the I<value> differently if the key starts
  43. with "hex".
  44. In that case, the value is decoded first, and the result will be used
  45. as parameter value.
  46. =head1 RETURN VALUES
  47. OSSL_PARAM_construct_from_text() and OSSL_PARAM_allocate_from_text()
  48. returns 1 on success, and 0 on error.
  49. =head1 NOTES
  50. The parameter descriptor array comes from functions dedicated to
  51. return them.
  52. The following B<OSSL_PARAM> attributes are used:
  53. =over 4
  54. =item I<key>
  55. =item I<data>
  56. =item I<data_size>
  57. =back
  58. All other attributes are ignored.
  59. The I<data_size> attribute can be zero, meaning that the parameter it
  60. describes expects arbitrary length data.
  61. =head1 EXAMPLES
  62. Code that looked like this:
  63. int mac_ctrl_string(EVP_PKEY_CTX *ctx, const char *value)
  64. {
  65. int rv;
  66. char *stmp, *vtmp = NULL;
  67. stmp = OPENSSL_strdup(value);
  68. if (stmp == NULL)
  69. return -1;
  70. vtmp = strchr(stmp, ':');
  71. if (vtmp != NULL)
  72. *vtmp++ = '\0';
  73. rv = EVP_MAC_ctrl_str(ctx, stmp, vtmp);
  74. OPENSSL_free(stmp);
  75. return rv;
  76. }
  77. ...
  78. for (i = 0; i < sk_OPENSSL_STRING_num(macopts); i++) {
  79. char *macopt = sk_OPENSSL_STRING_value(macopts, i);
  80. if (pkey_ctrl_string(mac_ctx, macopt) <= 0) {
  81. BIO_printf(bio_err,
  82. "MAC parameter error \"%s\"\n", macopt);
  83. ERR_print_errors(bio_err);
  84. goto mac_end;
  85. }
  86. }
  87. Can be written like this instead:
  88. OSSL_PARAM *params =
  89. OPENSSL_zalloc(sizeof(*params)
  90. * (sk_OPENSSL_STRING_num(opts) + 1));
  91. const OSSL_PARAM *paramdefs = EVP_MAC_settable_ctx_params(mac);
  92. size_t params_n;
  93. char *opt = "<unknown>";
  94. for (params_n = 0; params_n < (size_t)sk_OPENSSL_STRING_num(opts);
  95. params_n++) {
  96. char *stmp, *vtmp = NULL;
  97. opt = sk_OPENSSL_STRING_value(opts, (int)params_n);
  98. if ((stmp = OPENSSL_strdup(opt)) == NULL
  99. || (vtmp = strchr(stmp, ':')) == NULL)
  100. goto err;
  101. *vtmp++ = '\0';
  102. if (!OSSL_PARAM_allocate_from_text(&params[params_n],
  103. paramdefs, stmp,
  104. vtmp, strlen(vtmp)))
  105. goto err;
  106. }
  107. params[params_n] = OSSL_PARAM_construct_end();
  108. if (!EVP_MAC_CTX_set_params(ctx, params))
  109. goto err;
  110. while (params_n-- > 0)
  111. OPENSSL_free(params[params_n].data);
  112. OPENSSL_free(params);
  113. /* ... */
  114. return;
  115. err:
  116. BIO_printf(bio_err, "MAC parameter error '%s'\n", opt);
  117. ERR_print_errors(bio_err);
  118. =head1 SEE ALSO
  119. L<OSSL_PARAM(3)>, L<OSSL_PARAM_int(3)>
  120. =head1 COPYRIGHT
  121. Copyright 2019 The OpenSSL Project Authors. All Rights Reserved.
  122. Licensed under the Apache License 2.0 (the "License"). You may not use
  123. this file except in compliance with the License. You can obtain a copy
  124. in the file LICENSE in the source distribution or at
  125. L<https://www.openssl.org/source/license.html>.
  126. =cut