EVP_PKEY_fromdata.pod 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. =pod
  2. =head1 NAME
  3. EVP_PKEY_param_fromdata_init, EVP_PKEY_key_fromdata_init, EVP_PKEY_fromdata,
  4. EVP_PKEY_param_fromdata_settable, EVP_PKEY_key_fromdata_settable
  5. - functions to create key parameters and keys from user data
  6. =head1 SYNOPSIS
  7. #include <openssl/evp.h>
  8. int EVP_PKEY_param_fromdata_init(EVP_PKEY_CTX *ctx);
  9. int EVP_PKEY_key_fromdata_init(EVP_PKEY_CTX *ctx);
  10. int EVP_PKEY_fromdata(EVP_PKEY_CTX *ctx, EVP_PKEY **ppkey, OSSL_PARAM params[]);
  11. const OSSL_PARAM *EVP_PKEY_param_fromdata_settable(EVP_PKEY_CTX *ctx);
  12. const OSSL_PARAM *EVP_PKEY_key_fromdata_settable(EVP_PKEY_CTX *ctx);
  13. =head1 DESCRIPTION
  14. The functions described here are used to create new keys from user
  15. provided key data, such as I<n>, I<e> and I<d> for a minimal RSA
  16. keypair.
  17. These functions use an B<EVP_PKEY_CTX> context, which should primarily
  18. be created with L<EVP_PKEY_CTX_new_from_name(3)> or
  19. L<EVP_PKEY_CTX_new_id(3)>.
  20. The exact key data that the user can pass depends on the key type.
  21. These are passed as an L<OSSL_PARAM(3)> array.
  22. EVP_PKEY_param_fromdata_init() initializes a public key algorithm context
  23. for creating key parameters from user data.
  24. EVP_PKEY_key_fromdata_init() initializes a public key algorithm context for
  25. creating a key from user data.
  26. EVP_PKEY_fromdata() creates the structure to store key parameters or a
  27. key, given data from I<params> and a context that's been initialized with
  28. EVP_PKEY_param_fromdata_init() or EVP_PKEY_key_fromdata_init(). The result is
  29. written to I<*ppkey>. The parameters that can be used for various types of key
  30. are as described by the diverse "Common parameters" sections of the
  31. L<B<EVP_PKEY-RSA>(7)|EVP_PKEY-RSA(7)/Common RSA parameters>,
  32. L<B<EVP_PKEY-DSA>(7)|EVP_PKEY-DSA(7)/Common DSA & DH parameters>,
  33. L<B<EVP_PKEY-DH>(7)|EVP_PKEY-DH(7)/Common DH parameters>,
  34. L<B<EVP_PKEY-EC>(7)|EVP_PKEY-EC(7)/Common EC parameters>,
  35. L<B<EVP_PKEY-ED448>(7)|EVP_PKEY-ED448(7)/Common X25519, X448, ED25519 and ED448 parameters>,
  36. L<B<EVP_PKEY-X25519>(7)|EVP_PKEY-X25519(7)/Common X25519, X448, ED25519 and ED448 parameters>,
  37. L<B<EVP_PKEY-X448>(7)|EVP_PKEY-X448(7)/Common X25519, X448, ED25519 and ED448 parameters>,
  38. and L<B<EVP_PKEY-ED25519>(7)|EVP_PKEY-ED25519(7)/Common X25519, X448, ED25519 and ED448 parameters> pages.
  39. =for comment the awful list of links above is made this way so we get nice
  40. rendering as a man-page while still getting proper links in HTML
  41. EVP_PKEY_param_fromdata_settable() and EVP_PKEY_key_fromdata_settable()
  42. get a constant B<OSSL_PARAM> array that describes the settable parameters
  43. that can be used with EVP_PKEY_fromdata().
  44. See L<OSSL_PARAM(3)> for the use of B<OSSL_PARAM> as parameter descriptor.
  45. =head1 NOTES
  46. These functions only work with key management methods coming from a
  47. provider.
  48. =for comment We may choose to make this available for legacy methods too...
  49. =head1 RETURN VALUES
  50. EVP_PKEY_key_fromdata_init(), EVP_PKEY_param_fromdata_init() and
  51. EVP_PKEY_fromdata() return 1 for success and 0 or a negative value for
  52. failure. In particular a return value of -2 indicates the operation is
  53. not supported by the public key algorithm.
  54. =head1 EXAMPLES
  55. These examples are very terse for the sake of staying on topic, which
  56. is the EVP_PKEY_fromdata() set of functions. In real applications,
  57. BIGNUMs would be handled and converted to byte arrays with
  58. BN_bn2nativepad(), but that's off topic here.
  59. =begin comment
  60. TODO Write a set of cookbook documents and link to them.
  61. =end comment
  62. =head2 Creating an RSA keypair using raw key data
  63. #include <openssl/evp.h>
  64. /*
  65. * These are extremely small to make this example simple. A real
  66. * and secure application will not use such small numbers. A real
  67. * and secure application is expected to use BIGNUMs, and to build
  68. * this array dynamically.
  69. */
  70. unsigned long rsa_n = 0xbc747fc5;
  71. unsigned long rsa_e = 0x10001;
  72. unsigned long rsa_d = 0x7b133399;
  73. OSSL_PARAM params[] = {
  74. OSSL_PARAM_ulong("n", &rsa_n),
  75. OSSL_PARAM_ulong("e", &rsa_e),
  76. OSSL_PARAM_ulong("d", &rsa_d),
  77. OSSL_PARAM_END
  78. };
  79. int main()
  80. {
  81. EVP_PKEY_CTX *ctx = EVP_PKEY_CTX_new_from_name(NULL, "RSA", NULL);
  82. EVP_PKEY *pkey = NULL;
  83. if (ctx == NULL
  84. || !EVP_PKEY_key_fromdata_init(ctx)
  85. || !EVP_PKEY_fromdata(ctx, &pkey, params))
  86. exit(1);
  87. /* Do what you want with |pkey| */
  88. }
  89. =head2 Creating an ECC keypair using raw key data
  90. #include <openssl/evp.h>
  91. /*
  92. * These arrays represent large numbers, big endian organization.
  93. * In a real application, these would probably be bignums that get
  94. * converted to the native integer organization with BN_bn2nativepad().
  95. * We're not doing that here, since this is not an example of BIGNUM
  96. * functionality, but an example of EVP_PKEY_fromdata().
  97. */
  98. #ifndef B_ENDIAN
  99. # error "We haven't prepared little endian arrays"
  100. #endif
  101. const unsigned char priv[] = {
  102. 0xb9, 0x2f, 0x3c, 0xe6, 0x2f, 0xfb, 0x45, 0x68,
  103. 0x39, 0x96, 0xf0, 0x2a, 0xaf, 0x6c, 0xda, 0xf2,
  104. 0x89, 0x8a, 0x27, 0xbf, 0x39, 0x9b, 0x7e, 0x54,
  105. 0x21, 0xc2, 0xa1, 0xe5, 0x36, 0x12, 0x48, 0x5d
  106. };
  107. const unsigned char pub[] = {
  108. 0x04, 0xcf, 0x20, 0xfb, 0x9a, 0x1d, 0x11, 0x6c,
  109. 0x5e, 0x9f, 0xec, 0x38, 0x87, 0x6c, 0x1d, 0x2f,
  110. 0x58, 0x47, 0xab, 0xa3, 0x9b, 0x79, 0x23, 0xe6,
  111. 0xeb, 0x94, 0x6f, 0x97, 0xdb, 0xa3, 0x7d, 0xbd,
  112. 0xe5, 0x26, 0xca, 0x07, 0x17, 0x8d, 0x26, 0x75,
  113. 0xff, 0xcb, 0x8e, 0xb6, 0x84, 0xd0, 0x24, 0x02,
  114. 0x25, 0x8f, 0xb9, 0x33, 0x6e, 0xcf, 0x12, 0x16,
  115. 0x2f, 0x5c, 0xcd, 0x86, 0x71, 0xa8, 0xbf, 0x1a,
  116. 0x47
  117. };
  118. const OSSL_PARAM params[] = {
  119. OSSL_PARAM_utf8_string("curve-name", "prime256v1"),
  120. OSSL_PARAM_BN("priv", priv, sizeof(priv)),
  121. OSSL_PARAM_BN("pub", pub, sizeof(pub)),
  122. OSSL_PARAM_END
  123. };
  124. int main()
  125. {
  126. EVP_PKEY_CTX *ctx = EVP_PKEY_CTX_new_from_name(NULL, "EC", NULL);
  127. EVP_PKEY *pkey = NULL;
  128. if (ctx == NULL
  129. || !EVP_PKEY_key_fromdata_init(ctx)
  130. || !EVP_PKEY_fromdata(ctx, &pkey, params))
  131. exit(1);
  132. /* Do what you want with |pkey| */
  133. }
  134. =head2 Finding out params for an unknown key type
  135. #include <openssl/evp.h>
  136. /* Program expects a key type as first argument */
  137. int main(int argc, char *argv[])
  138. {
  139. EVP_PKEY_CTX *ctx = EVP_PKEY_CTX_new_from_name(NULL, argv[1], NULL);
  140. const *OSSL_PARAM *settable_params = NULL;
  141. if (ctx == NULL
  142. || (settable_params = EVP_PKEY_key_fromdata_settable(ctx)) == NULL)
  143. exit(1);
  144. for (; settable_params->key != NULL; settable_params++) {
  145. const char *datatype = NULL;
  146. switch (settable_params->data_type) {
  147. case OSSL_PARAM_INTEGER:
  148. datatype = "integer";
  149. break;
  150. case OSSL_PARAM_UNSIGNED_INTEGER:
  151. datatype = "unsigned integer";
  152. break;
  153. case OSSL_PARAM_UTF8_STRING:
  154. datatype = "printable string (utf-8 encoding expected)";
  155. break;
  156. case OSSL_PARAM_UTF8_PTR:
  157. datatype = "printable string pointer (utf-8 encoding expected)";
  158. break;
  159. case OSSL_PARAM_OCTET_STRING:
  160. datatype = "octet string";
  161. break;
  162. case OSSL_PARAM_OCTET_PTR:
  163. datatype = "octet string pointer";
  164. break;
  165. }
  166. printf("%s : %s ", settable_params->key, datatype);
  167. if (settable_params->data_size == 0)
  168. printf("(unlimited size)");
  169. else
  170. printf("(maximum size %zu)", settable_params->data_size);
  171. }
  172. }
  173. The descriptor L<OSSL_PARAM(3)> returned by
  174. EVP_PKEY_key_fromdata_settable() may also be used programmatically, for
  175. example with L<OSSL_PARAM_allocate_from_text(3)>.
  176. =head1 SEE ALSO
  177. L<EVP_PKEY_CTX_new(3)>, L<provider(7)>, L<EVP_PKEY_gettable_params(3)>,
  178. L<OSSL_PARAM(3)>,
  179. L<EVP_PKEY-RSA(7)>, L<EVP_PKEY-DSA(7)>, L<EVP_PKEY-DH(7)>, L<EVP_PKEY-EC(7)>,
  180. L<EVP_PKEY-ED448(7)>, L<EVP_PKEY-X25519(7)>, L<EVP_PKEY-X448(7)>,
  181. L<EVP_PKEY-ED25519(7)>
  182. =head1 HISTORY
  183. These functions were added in OpenSSL 3.0.
  184. =head1 COPYRIGHT
  185. Copyright 2019-2020 The OpenSSL Project Authors. All Rights Reserved.
  186. Licensed under the Apache License 2.0 (the "License"). You may not use
  187. this file except in compliance with the License. You can obtain a copy
  188. in the file LICENSE in the source distribution or at
  189. L<https://www.openssl.org/source/license.html>.
  190. =cut