SSL_CTX_set_tmp_dh_callback.pod 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. =pod
  2. =head1 NAME
  3. SSL_CTX_set_tmp_dh_callback, SSL_CTX_set_tmp_dh, SSL_set_tmp_dh_callback, SSL_set_tmp_dh - handle DH keys for ephemeral key exchange
  4. =head1 SYNOPSIS
  5. #include <openssl/ssl.h>
  6. void SSL_CTX_set_tmp_dh_callback(SSL_CTX *ctx,
  7. DH *(*tmp_dh_callback)(SSL *ssl, int is_export, int keylength));
  8. long SSL_CTX_set_tmp_dh(SSL_CTX *ctx, DH *dh);
  9. void SSL_set_tmp_dh_callback(SSL_CTX *ctx,
  10. DH *(*tmp_dh_callback)(SSL *ssl, int is_export, int keylength));
  11. long SSL_set_tmp_dh(SSL *ssl, DH *dh)
  12. DH *(*tmp_dh_callback)(SSL *ssl, int is_export, int keylength));
  13. =head1 DESCRIPTION
  14. SSL_CTX_set_tmp_dh_callback() sets the callback function for B<ctx> to be
  15. used when a DH parameters are required to B<tmp_dh_callback>.
  16. The callback is inherited by all B<ssl> objects created from B<ctx>.
  17. SSL_CTX_set_tmp_dh() sets DH parameters to be used to be B<dh>.
  18. The key is inherited by all B<ssl> objects created from B<ctx>.
  19. SSL_set_tmp_dh_callback() sets the callback only for B<ssl>.
  20. SSL_set_tmp_dh() sets the parameters only for B<ssl>.
  21. These functions apply to SSL/TLS servers only.
  22. =head1 NOTES
  23. When using a cipher with RSA authentication, an ephemeral DH key exchange
  24. can take place. Ciphers with DSA keys always use ephemeral DH keys as well.
  25. In these cases, the session data are negotiated using the
  26. ephemeral/temporary DH key and the key supplied and certified
  27. by the certificate chain is only used for signing.
  28. Anonymous ciphers (without a permanent server key) also use ephemeral DH keys.
  29. Using ephemeral DH key exchange yields forward secrecy, as the connection
  30. can only be decrypted, when the DH key is known. By generating a temporary
  31. DH key inside the server application that is lost when the application
  32. is left, it becomes impossible for an attacker to decrypt past sessions,
  33. even if he gets hold of the normal (certified) key, as this key was
  34. only used for signing.
  35. In order to perform a DH key exchange the server must use a DH group
  36. (DH parameters) and generate a DH key. The server will always generate a new
  37. DH key during the negotiation, when the DH parameters are supplied via
  38. callback and/or when the SSL_OP_SINGLE_DH_USE option of
  39. L<SSL_CTX_set_options(3)|SSL_CTX_set_options(3)> is set. It will
  40. immediately create a DH key, when DH parameters are supplied via
  41. SSL_CTX_set_tmp_dh() and SSL_OP_SINGLE_DH_USE is not set. In this case,
  42. it may happen that a key is generated on initialization without later
  43. being needed, while on the other hand the computer time during the
  44. negotiation is being saved.
  45. If "strong" primes were used to generate the DH parameters, it is not strictly
  46. necessary to generate a new key for each handshake but it does improve forward
  47. secrecy. If it is not assured, that "strong" primes were used (see especially
  48. the section about DSA parameters below), SSL_OP_SINGLE_DH_USE must be used
  49. in order to prevent small subgroup attacks. Always using SSL_OP_SINGLE_DH_USE
  50. has an impact on the computer time needed during negotiation, but it is not
  51. very large, so application authors/users should consider to always enable
  52. this option.
  53. As generating DH parameters is extremely time consuming, an application
  54. should not generate the parameters on the fly but supply the parameters.
  55. DH parameters can be reused, as the actual key is newly generated during
  56. the negotiation. The risk in reusing DH parameters is that an attacker
  57. may specialize on a very often used DH group. Applications should therefore
  58. generate their own DH parameters during the installation process using the
  59. openssl L<dhparam(1)|dhparam(1)> application. In order to reduce the computer
  60. time needed for this generation, it is possible to use DSA parameters
  61. instead (see L<dhparam(1)|dhparam(1)>), but in this case SSL_OP_SINGLE_DH_USE
  62. is mandatory.
  63. Application authors may compile in DH parameters. Files dh512.pem,
  64. dh1024.pem, dh2048.pem, and dh4096 in the 'apps' directory of current
  65. version of the OpenSSL distribution contain the 'SKIP' DH parameters,
  66. which use safe primes and were generated verifiably pseudo-randomly.
  67. These files can be converted into C code using the B<-C> option of the
  68. L<dhparam(1)|dhparam(1)> application.
  69. Authors may also generate their own set of parameters using
  70. L<dhparam(1)|dhparam(1)>, but a user may not be sure how the parameters were
  71. generated. The generation of DH parameters during installation is therefore
  72. recommended.
  73. An application may either directly specify the DH parameters or
  74. can supply the DH parameters via a callback function. The callback approach
  75. has the advantage, that the callback may supply DH parameters for different
  76. key lengths.
  77. The B<tmp_dh_callback> is called with the B<keylength> needed and
  78. the B<is_export> information. The B<is_export> flag is set, when the
  79. ephemeral DH key exchange is performed with an export cipher.
  80. =head1 EXAMPLES
  81. Handle DH parameters for key lengths of 512 and 1024 bits. (Error handling
  82. partly left out.)
  83. ...
  84. /* Set up ephemeral DH stuff */
  85. DH *dh_512 = NULL;
  86. DH *dh_1024 = NULL;
  87. FILE *paramfile;
  88. ...
  89. /* "openssl dhparam -out dh_param_512.pem -2 512" */
  90. paramfile = fopen("dh_param_512.pem", "r");
  91. if (paramfile) {
  92. dh_512 = PEM_read_DHparams(paramfile, NULL, NULL, NULL);
  93. fclose(paramfile);
  94. }
  95. /* "openssl dhparam -out dh_param_1024.pem -2 1024" */
  96. paramfile = fopen("dh_param_1024.pem", "r");
  97. if (paramfile) {
  98. dh_1024 = PEM_read_DHparams(paramfile, NULL, NULL, NULL);
  99. fclose(paramfile);
  100. }
  101. ...
  102. /* "openssl dhparam -C -2 512" etc... */
  103. DH *get_dh512() { ... }
  104. DH *get_dh1024() { ... }
  105. DH *tmp_dh_callback(SSL *s, int is_export, int keylength)
  106. {
  107. DH *dh_tmp=NULL;
  108. switch (keylength) {
  109. case 512:
  110. if (!dh_512)
  111. dh_512 = get_dh512();
  112. dh_tmp = dh_512;
  113. break;
  114. case 1024:
  115. if (!dh_1024)
  116. dh_1024 = get_dh1024();
  117. dh_tmp = dh_1024;
  118. break;
  119. default:
  120. /* Generating a key on the fly is very costly, so use what is there */
  121. setup_dh_parameters_like_above();
  122. }
  123. return(dh_tmp);
  124. }
  125. =head1 RETURN VALUES
  126. SSL_CTX_set_tmp_dh_callback() and SSL_set_tmp_dh_callback() do not return
  127. diagnostic output.
  128. SSL_CTX_set_tmp_dh() and SSL_set_tmp_dh() do return 1 on success and 0
  129. on failure. Check the error queue to find out the reason of failure.
  130. =head1 SEE ALSO
  131. L<ssl(3)|ssl(3)>, L<SSL_CTX_set_cipher_list(3)|SSL_CTX_set_cipher_list(3)>,
  132. L<SSL_CTX_set_tmp_rsa_callback(3)|SSL_CTX_set_tmp_rsa_callback(3)>,
  133. L<SSL_CTX_set_options(3)|SSL_CTX_set_options(3)>,
  134. L<ciphers(1)|ciphers(1)>, L<dhparam(1)|dhparam(1)>
  135. =cut