OSSL_PARAM_allocate_from_text.pod 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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> didn't start with "hex", I<value> is assumed to contain
  42. I<value_n> decimal characters, which are decoded, and the resulting
  43. bytes become the number stored in the I<< to->data >> storage.
  44. If I<value> starts with "0x", it is assumed to contain I<value_n>
  45. hexadecimal characters.
  46. If I<key> started with "hex", I<value> is assumed to contain
  47. I<value_n> hexadecimal characters without the "0x" prefix.
  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 to the
  55. I<< to->data >> storage.
  56. On systems where the native character encoding is EBCDIC, the bytes in
  57. I<< to->data >> are converted to ASCII.
  58. =item B<OSSL_PARAM_OCTET_STRING>
  59. If I<key> started with "hex", I<value> is assumed to contain
  60. I<value_n> hexadecimal characters, which are decoded, and the
  61. resulting bytes are stored in the I<< to->data >> storage.
  62. If I<value> contains characters that couldn't be decoded as
  63. hexadecimal or decimal characters, OSSL_PARAM_allocate_from_text()
  64. considers that an error.
  65. If I<key> didn't start with "hex", I<value_n> bytes from I<value> are
  66. copied to the I<< to->data >> storage.
  67. =back
  68. =head1 RETURN VALUES
  69. OSSL_PARAM_allocate_from_text() returns 1 if I<key> was found in
  70. I<paramdefs> and there was no other failure, otherwise 0.
  71. =head1 NOTES
  72. The parameter descriptor array comes from functions dedicated to
  73. return them.
  74. The following L<OSSL_PARAM(3)> attributes are used:
  75. =over 4
  76. =item I<key>
  77. =item I<data_type>
  78. =item I<data_size>
  79. =back
  80. All other attributes are ignored.
  81. The I<data_size> attribute can be zero, meaning that the parameter it
  82. describes expects arbitrary length data.
  83. =head1 EXAMPLES
  84. Code that looked like this:
  85. int mac_ctrl_string(EVP_PKEY_CTX *ctx, const char *value)
  86. {
  87. int rv;
  88. char *stmp, *vtmp = NULL;
  89. stmp = OPENSSL_strdup(value);
  90. if (stmp == NULL)
  91. return -1;
  92. vtmp = strchr(stmp, ':');
  93. if (vtmp != NULL)
  94. *vtmp++ = '\0';
  95. rv = EVP_MAC_ctrl_str(ctx, stmp, vtmp);
  96. OPENSSL_free(stmp);
  97. return rv;
  98. }
  99. ...
  100. for (i = 0; i < sk_OPENSSL_STRING_num(macopts); i++) {
  101. char *macopt = sk_OPENSSL_STRING_value(macopts, i);
  102. if (pkey_ctrl_string(mac_ctx, macopt) <= 0) {
  103. BIO_printf(bio_err,
  104. "MAC parameter error \"%s\"\n", macopt);
  105. ERR_print_errors(bio_err);
  106. goto mac_end;
  107. }
  108. }
  109. Can be written like this instead:
  110. OSSL_PARAM *params =
  111. OPENSSL_zalloc(sizeof(*params)
  112. * (sk_OPENSSL_STRING_num(opts) + 1));
  113. const OSSL_PARAM *paramdefs = EVP_MAC_settable_ctx_params(mac);
  114. size_t params_n;
  115. char *opt = "<unknown>";
  116. for (params_n = 0; params_n < (size_t)sk_OPENSSL_STRING_num(opts);
  117. params_n++) {
  118. char *stmp, *vtmp = NULL;
  119. opt = sk_OPENSSL_STRING_value(opts, (int)params_n);
  120. if ((stmp = OPENSSL_strdup(opt)) == NULL
  121. || (vtmp = strchr(stmp, ':')) == NULL)
  122. goto err;
  123. *vtmp++ = '\0';
  124. if (!OSSL_PARAM_allocate_from_text(&params[params_n],
  125. paramdefs, stmp,
  126. vtmp, strlen(vtmp), NULL))
  127. goto err;
  128. }
  129. params[params_n] = OSSL_PARAM_construct_end();
  130. if (!EVP_MAC_CTX_set_params(ctx, params))
  131. goto err;
  132. while (params_n-- > 0)
  133. OPENSSL_free(params[params_n].data);
  134. OPENSSL_free(params);
  135. /* ... */
  136. return;
  137. err:
  138. BIO_printf(bio_err, "MAC parameter error '%s'\n", opt);
  139. ERR_print_errors(bio_err);
  140. =head1 SEE ALSO
  141. L<OSSL_PARAM(3)>, L<OSSL_PARAM_int(3)>
  142. =head1 COPYRIGHT
  143. Copyright 2019-2021 The OpenSSL Project Authors. All Rights Reserved.
  144. Licensed under the Apache License 2.0 (the "License"). You may not use
  145. this file except in compliance with the License. You can obtain a copy
  146. in the file LICENSE in the source distribution or at
  147. L<https://www.openssl.org/source/license.html>.
  148. =cut