EVP_PKEY_keygen.pod 6.7 KB

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