2
0

EVP_PKEY_keygen.pod 5.2 KB

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