EVP_PKEY_keygen.pod 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. =pod
  2. =head1 NAME
  3. EVP_PKEY_keygen_init, EVP_PKEY_keygen, EVP_PKEY_paramgen_init,
  4. EVP_PKEY_paramgen, EVP_PKEY_CTX_set_cb, EVP_PKEY_CTX_get_cb,
  5. EVP_PKEY_CTX_get_keygen_info, EVP_PKEY_CTX_set_app_data,
  6. EVP_PKEY_CTX_get_app_data,
  7. EVP_PKEY_gen_cb
  8. - key and parameter generation functions
  9. =head1 SYNOPSIS
  10. #include <openssl/evp.h>
  11. int EVP_PKEY_keygen_init(EVP_PKEY_CTX *ctx);
  12. int EVP_PKEY_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY **ppkey);
  13. int EVP_PKEY_paramgen_init(EVP_PKEY_CTX *ctx);
  14. int EVP_PKEY_paramgen(EVP_PKEY_CTX *ctx, EVP_PKEY **ppkey);
  15. typedef int EVP_PKEY_gen_cb(EVP_PKEY_CTX *ctx);
  16. void EVP_PKEY_CTX_set_cb(EVP_PKEY_CTX *ctx, EVP_PKEY_gen_cb *cb);
  17. EVP_PKEY_gen_cb *EVP_PKEY_CTX_get_cb(EVP_PKEY_CTX *ctx);
  18. int EVP_PKEY_CTX_get_keygen_info(EVP_PKEY_CTX *ctx, int idx);
  19. void EVP_PKEY_CTX_set_app_data(EVP_PKEY_CTX *ctx, void *data);
  20. void *EVP_PKEY_CTX_get_app_data(EVP_PKEY_CTX *ctx);
  21. =head1 DESCRIPTION
  22. The EVP_PKEY_keygen_init() function initializes a public key algorithm
  23. context using key B<pkey> for a key generation operation.
  24. The EVP_PKEY_keygen() function performs a key generation operation, the
  25. generated key is written to B<ppkey>.
  26. The functions EVP_PKEY_paramgen_init() and EVP_PKEY_paramgen() are similar
  27. except parameters are generated.
  28. The function EVP_PKEY_set_cb() sets the key or parameter generation callback
  29. to B<cb>. The function EVP_PKEY_CTX_get_cb() returns the key or parameter
  30. generation callback.
  31. The function EVP_PKEY_CTX_get_keygen_info() returns parameters associated
  32. with the generation operation. If B<idx> is -1 the total number of
  33. parameters available is returned. Any non negative value returns the value of
  34. that parameter. EVP_PKEY_CTX_gen_keygen_info() with a non-negative value for
  35. B<idx> should only be called within the generation callback.
  36. If the callback returns 0 then the key generation operation is aborted and an
  37. error occurs. This might occur during a time consuming operation where
  38. a user clicks on a "cancel" button.
  39. The functions EVP_PKEY_CTX_set_app_data() and EVP_PKEY_CTX_get_app_data() set
  40. and retrieve an opaque pointer. This can be used to set some application
  41. defined value which can be retrieved in the callback: for example a handle
  42. which is used to update a "progress dialog".
  43. =head1 NOTES
  44. After the call to EVP_PKEY_keygen_init() or EVP_PKEY_paramgen_init() algorithm
  45. specific control operations can be performed to set any appropriate parameters
  46. for the operation.
  47. The functions EVP_PKEY_keygen() and EVP_PKEY_paramgen() can be called more than
  48. once on the same context if several operations are performed using the same
  49. parameters.
  50. The meaning of the parameters passed to the callback will depend on the
  51. algorithm and the specific implementation of the algorithm. Some might not
  52. give any useful information at all during key or parameter generation. Others
  53. might not even call the callback.
  54. The operation performed by key or parameter generation depends on the algorithm
  55. used. In some cases (e.g. EC with a supplied named curve) the "generation"
  56. option merely sets the appropriate fields in an EVP_PKEY structure.
  57. In OpenSSL an EVP_PKEY structure containing a private key also contains the
  58. public key components and parameters (if any). An OpenSSL private key is
  59. equivalent to what some libraries call a "key pair". A private key can be used
  60. in functions which require the use of a public key or parameters.
  61. =head1 RETURN VALUES
  62. EVP_PKEY_keygen_init(), EVP_PKEY_paramgen_init(), EVP_PKEY_keygen() and
  63. EVP_PKEY_paramgen() return 1 for success and 0 or a negative value for failure.
  64. In particular a return value of -2 indicates the operation is not supported by
  65. the public key algorithm.
  66. =head1 EXAMPLES
  67. Generate a 2048 bit RSA key:
  68. #include <openssl/evp.h>
  69. #include <openssl/rsa.h>
  70. EVP_PKEY_CTX *ctx;
  71. EVP_PKEY *pkey = NULL;
  72. ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_RSA, NULL);
  73. if (!ctx)
  74. /* Error occurred */
  75. if (EVP_PKEY_keygen_init(ctx) <= 0)
  76. /* Error */
  77. if (EVP_PKEY_CTX_set_rsa_keygen_bits(ctx, 2048) <= 0)
  78. /* Error */
  79. /* Generate key */
  80. if (EVP_PKEY_keygen(ctx, &pkey) <= 0)
  81. /* Error */
  82. Generate a key from a set of parameters:
  83. #include <openssl/evp.h>
  84. #include <openssl/rsa.h>
  85. EVP_PKEY_CTX *ctx;
  86. EVP_PKEY *pkey = NULL, *param;
  87. /* Assumed param is set up already */
  88. ctx = EVP_PKEY_CTX_new(param);
  89. if (!ctx)
  90. /* Error occurred */
  91. if (EVP_PKEY_keygen_init(ctx) <= 0)
  92. /* Error */
  93. /* Generate key */
  94. if (EVP_PKEY_keygen(ctx, &pkey) <= 0)
  95. /* Error */
  96. Example of generation callback for OpenSSL public key implementations:
  97. /* Application data is a BIO to output status to */
  98. EVP_PKEY_CTX_set_app_data(ctx, status_bio);
  99. static int genpkey_cb(EVP_PKEY_CTX *ctx)
  100. {
  101. char c = '*';
  102. BIO *b = EVP_PKEY_CTX_get_app_data(ctx);
  103. int p;
  104. p = EVP_PKEY_CTX_get_keygen_info(ctx, 0);
  105. if (p == 0) c = '.';
  106. if (p == 1) c = '+';
  107. if (p == 2) c = '*';
  108. if (p == 3) c = '\n';
  109. BIO_write(b, &c, 1);
  110. (void)BIO_flush(b);
  111. return 1;
  112. }
  113. =head1 SEE ALSO
  114. L<EVP_PKEY_CTX_new(3)>,
  115. L<EVP_PKEY_encrypt(3)>,
  116. L<EVP_PKEY_decrypt(3)>,
  117. L<EVP_PKEY_sign(3)>,
  118. L<EVP_PKEY_verify(3)>,
  119. L<EVP_PKEY_verify_recover(3)>,
  120. L<EVP_PKEY_derive(3)>
  121. =head1 HISTORY
  122. These functions were first added to OpenSSL 1.0.0.
  123. =head1 COPYRIGHT
  124. Copyright 2006-2016 The OpenSSL Project Authors. All Rights Reserved.
  125. Licensed under the OpenSSL license (the "License"). You may not use
  126. this file except in compliance with the License. You can obtain a copy
  127. in the file LICENSE in the source distribution or at
  128. L<https://www.openssl.org/source/license.html>.
  129. =cut