OSSL_PARAM_allocate_from_text.pod 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. =pod
  2. =head1 NAME
  3. OSSL_PARAM_allocate_from_text
  4. - OSSL_PARAM construction utilities
  5. =head1 SYNOPSIS
  6. #include <openssl/params.h>
  7. int OSSL_PARAM_allocate_from_text(OSSL_PARAM *to,
  8. const OSSL_PARAM *paramdefs,
  9. const char *key, const char *value,
  10. size_t value_n,
  11. int *found);
  12. =head1 DESCRIPTION
  13. With OpenSSL before version 3.0, parameters were passed down to or
  14. retrieved from algorithm implementations via control functions.
  15. Some of these control functions existed in variants that took string
  16. parameters, for example L<EVP_PKEY_CTX_ctrl_str(3)>.
  17. OpenSSL 3.0 introduces a new mechanism to do the same thing with an
  18. array of parameters that contain name, value, value type and value
  19. size (see L<OSSL_PARAM(3)> for more information).
  20. OSSL_PARAM_allocate_from_text() uses I<key> to look up an item in
  21. I<paramdefs>. If an item was found, it converts I<value> to something
  22. suitable for that item's I<data_type>, and stores the result in
  23. I<< to->data >> as well as its size in I<< to->data_size >>.
  24. I<< to->key >> and I<< to->data_type >> are assigned the corresponding
  25. values from the item that was found, and I<< to->return_size >> is set
  26. to zero.
  27. I<< to->data >> is always allocated using L<OPENSSL_zalloc(3)> and
  28. needs to be freed by the caller when it's not useful any more, using
  29. L<OPENSSL_free(3)>.
  30. If I<found> is not NULL, I<*found> is set to 1 if I<key> could be
  31. located in I<paramdefs>, and to 0 otherwise.
  32. =head2 The use of I<key> and I<value> in detail
  33. OSSL_PARAM_allocate_from_text() takes note if I<key> starts with
  34. "hex", and will only use the rest of I<key> to look up an item in
  35. I<paramdefs> in that case. As an example, if I<key> is "hexid", "id"
  36. will be looked up in I<paramdefs>.
  37. When an item in I<paramdefs> has been found, I<value> is converted
  38. depending on that item's I<data_type>, as follows:
  39. =over 4
  40. =item B<OSSL_PARAM_INTEGER> and B<OSSL_PARAM_UNSIGNED_INTEGER>
  41. If I<key> started with "hex", I<value> is assumed to contain
  42. I<value_n> hexadecimal characters, which are decoded, and the
  43. resulting bytes become the number stored in the I<< to->data >>
  44. storage.
  45. If I<key> didn't start with "hex", I<value> is assumed to contain
  46. I<value_n> decimal characters, which are decoded, and the resulting
  47. bytes become the number stored in the I<< to->data >> storage.
  48. If I<value> contains characters that couldn't be decoded as
  49. hexadecimal or decimal characters, OSSL_PARAM_allocate_from_text()
  50. considers that an error.
  51. =item B<OSSL_PARAM_UTF8_STRING>
  52. If I<key> started with "hex", OSSL_PARAM_allocate_from_text()
  53. considers that an error.
  54. Otherwise, I<value> is considered a C string and is copied with no
  55. further checks to the I<< to->data >> storage.
  56. =item B<OSSL_PARAM_OCTET_STRING>
  57. If I<key> started with "hex", I<value> is assumed to contain
  58. I<value_n> hexadecimal characters, which are decoded, and the
  59. resulting bytes are stored in the I<< to->data >> storage.
  60. If I<value> contains characters that couldn't be decoded as
  61. hexadecimal or decimal characters, OSSL_PARAM_allocate_from_text()
  62. considers that an error.
  63. If I<key> didn't start with "hex", I<value_n> bytes from I<value> are
  64. copied to the I<< to->data >> storage.
  65. =back
  66. =head1 RETURN VALUES
  67. OSSL_PARAM_allocate_from_text() returns 1 if I<key> was found in
  68. I<paramdefs> and there was no other failure, otherwise 0.
  69. =head1 NOTES
  70. The parameter descriptor array comes from functions dedicated to
  71. return them.
  72. The following B<OSSL_PARAM> attributes are used:
  73. =over 4
  74. =item I<key>
  75. =item I<data_type>
  76. =item I<data_size>
  77. =back
  78. All other attributes are ignored.
  79. The I<data_size> attribute can be zero, meaning that the parameter it
  80. describes expects arbitrary length data.
  81. =head1 EXAMPLES
  82. Code that looked like this:
  83. int mac_ctrl_string(EVP_PKEY_CTX *ctx, const char *value)
  84. {
  85. int rv;
  86. char *stmp, *vtmp = NULL;
  87. stmp = OPENSSL_strdup(value);
  88. if (stmp == NULL)
  89. return -1;
  90. vtmp = strchr(stmp, ':');
  91. if (vtmp != NULL)
  92. *vtmp++ = '\0';
  93. rv = EVP_MAC_ctrl_str(ctx, stmp, vtmp);
  94. OPENSSL_free(stmp);
  95. return rv;
  96. }
  97. ...
  98. for (i = 0; i < sk_OPENSSL_STRING_num(macopts); i++) {
  99. char *macopt = sk_OPENSSL_STRING_value(macopts, i);
  100. if (pkey_ctrl_string(mac_ctx, macopt) <= 0) {
  101. BIO_printf(bio_err,
  102. "MAC parameter error \"%s\"\n", macopt);
  103. ERR_print_errors(bio_err);
  104. goto mac_end;
  105. }
  106. }
  107. Can be written like this instead:
  108. OSSL_PARAM *params =
  109. OPENSSL_zalloc(sizeof(*params)
  110. * (sk_OPENSSL_STRING_num(opts) + 1));
  111. const OSSL_PARAM *paramdefs = EVP_MAC_settable_ctx_params(mac);
  112. size_t params_n;
  113. char *opt = "<unknown>";
  114. for (params_n = 0; params_n < (size_t)sk_OPENSSL_STRING_num(opts);
  115. params_n++) {
  116. char *stmp, *vtmp = NULL;
  117. opt = sk_OPENSSL_STRING_value(opts, (int)params_n);
  118. if ((stmp = OPENSSL_strdup(opt)) == NULL
  119. || (vtmp = strchr(stmp, ':')) == NULL)
  120. goto err;
  121. *vtmp++ = '\0';
  122. if (!OSSL_PARAM_allocate_from_text(&params[params_n],
  123. paramdefs, stmp,
  124. vtmp, strlen(vtmp), NULL))
  125. goto err;
  126. }
  127. params[params_n] = OSSL_PARAM_construct_end();
  128. if (!EVP_MAC_set_ctx_params(ctx, params))
  129. goto err;
  130. while (params_n-- > 0)
  131. OPENSSL_free(params[params_n].data);
  132. OPENSSL_free(params);
  133. /* ... */
  134. return;
  135. err:
  136. BIO_printf(bio_err, "MAC parameter error '%s'\n", opt);
  137. ERR_print_errors(bio_err);
  138. =head1 SEE ALSO
  139. L<OSSL_PARAM(3)>, L<OSSL_PARAM_int(3)>
  140. =head1 COPYRIGHT
  141. Copyright 2019-2020 The OpenSSL Project Authors. All Rights Reserved.
  142. Licensed under the Apache License 2.0 (the "License"). You may not use
  143. this file except in compliance with the License. You can obtain a copy
  144. in the file LICENSE in the source distribution or at
  145. L<https://www.openssl.org/source/license.html>.
  146. =cut