tls_depr.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /*
  2. * Copyright 2020-2021 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. /* We need to use some engine and HMAC deprecated APIs */
  10. #define OPENSSL_SUPPRESS_DEPRECATED
  11. #include <openssl/engine.h>
  12. #include "ssl_local.h"
  13. #include "internal/ssl_unwrap.h"
  14. /*
  15. * Engine APIs are only used to support applications that still use ENGINEs.
  16. * Once ENGINE is removed completely, all of this code can also be removed.
  17. */
  18. #ifndef OPENSSL_NO_ENGINE
  19. void tls_engine_finish(ENGINE *e)
  20. {
  21. ENGINE_finish(e);
  22. }
  23. #endif
  24. const EVP_CIPHER *tls_get_cipher_from_engine(int nid)
  25. {
  26. const EVP_CIPHER *ret = NULL;
  27. #ifndef OPENSSL_NO_ENGINE
  28. ENGINE *eng;
  29. /*
  30. * If there is an Engine available for this cipher we use the "implicit"
  31. * form to ensure we use that engine later.
  32. */
  33. eng = ENGINE_get_cipher_engine(nid);
  34. if (eng != NULL) {
  35. ret = ENGINE_get_cipher(eng, nid);
  36. ENGINE_finish(eng);
  37. }
  38. #endif
  39. return ret;
  40. }
  41. const EVP_MD *tls_get_digest_from_engine(int nid)
  42. {
  43. const EVP_MD *ret = NULL;
  44. #ifndef OPENSSL_NO_ENGINE
  45. ENGINE *eng;
  46. /*
  47. * If there is an Engine available for this digest we use the "implicit"
  48. * form to ensure we use that engine later.
  49. */
  50. eng = ENGINE_get_digest_engine(nid);
  51. if (eng != NULL) {
  52. ret = ENGINE_get_digest(eng, nid);
  53. ENGINE_finish(eng);
  54. }
  55. #endif
  56. return ret;
  57. }
  58. #ifndef OPENSSL_NO_ENGINE
  59. int tls_engine_load_ssl_client_cert(SSL_CONNECTION *s, X509 **px509,
  60. EVP_PKEY **ppkey)
  61. {
  62. SSL *ssl = SSL_CONNECTION_GET_SSL(s);
  63. return ENGINE_load_ssl_client_cert(SSL_CONNECTION_GET_CTX(s)->client_cert_engine,
  64. ssl,
  65. SSL_get_client_CA_list(ssl),
  66. px509, ppkey, NULL, NULL, NULL);
  67. }
  68. #endif
  69. #ifndef OPENSSL_NO_ENGINE
  70. int SSL_CTX_set_client_cert_engine(SSL_CTX *ctx, ENGINE *e)
  71. {
  72. if (!ENGINE_init(e)) {
  73. ERR_raise(ERR_LIB_SSL, ERR_R_ENGINE_LIB);
  74. return 0;
  75. }
  76. if (!ENGINE_get_ssl_client_cert_function(e)) {
  77. ERR_raise(ERR_LIB_SSL, SSL_R_NO_CLIENT_CERT_METHOD);
  78. ENGINE_finish(e);
  79. return 0;
  80. }
  81. ctx->client_cert_engine = e;
  82. return 1;
  83. }
  84. #endif
  85. /*
  86. * The HMAC APIs below are only used to support the deprecated public API
  87. * macro SSL_CTX_set_tlsext_ticket_key_cb(). The application supplied callback
  88. * takes an HMAC_CTX in its argument list. The preferred alternative is
  89. * SSL_CTX_set_tlsext_ticket_key_evp_cb(). Once
  90. * SSL_CTX_set_tlsext_ticket_key_cb() is removed, then all of this code can also
  91. * be removed.
  92. */
  93. #ifndef OPENSSL_NO_DEPRECATED_3_0
  94. int ssl_hmac_old_new(SSL_HMAC *ret)
  95. {
  96. ret->old_ctx = HMAC_CTX_new();
  97. if (ret->old_ctx == NULL)
  98. return 0;
  99. return 1;
  100. }
  101. void ssl_hmac_old_free(SSL_HMAC *ctx)
  102. {
  103. HMAC_CTX_free(ctx->old_ctx);
  104. }
  105. int ssl_hmac_old_init(SSL_HMAC *ctx, void *key, size_t len, char *md)
  106. {
  107. return HMAC_Init_ex(ctx->old_ctx, key, len, EVP_get_digestbyname(md), NULL);
  108. }
  109. int ssl_hmac_old_update(SSL_HMAC *ctx, const unsigned char *data, size_t len)
  110. {
  111. return HMAC_Update(ctx->old_ctx, data, len);
  112. }
  113. int ssl_hmac_old_final(SSL_HMAC *ctx, unsigned char *md, size_t *len)
  114. {
  115. unsigned int l;
  116. if (HMAC_Final(ctx->old_ctx, md, &l) > 0) {
  117. if (len != NULL)
  118. *len = l;
  119. return 1;
  120. }
  121. return 0;
  122. }
  123. size_t ssl_hmac_old_size(const SSL_HMAC *ctx)
  124. {
  125. return HMAC_size(ctx->old_ctx);
  126. }
  127. HMAC_CTX *ssl_hmac_get0_HMAC_CTX(SSL_HMAC *ctx)
  128. {
  129. return ctx->old_ctx;
  130. }
  131. /* Some deprecated public APIs pass DH objects */
  132. EVP_PKEY *ssl_dh_to_pkey(DH *dh)
  133. {
  134. # ifndef OPENSSL_NO_DH
  135. EVP_PKEY *ret;
  136. if (dh == NULL)
  137. return NULL;
  138. ret = EVP_PKEY_new();
  139. if (EVP_PKEY_set1_DH(ret, dh) <= 0) {
  140. EVP_PKEY_free(ret);
  141. return NULL;
  142. }
  143. return ret;
  144. # else
  145. return NULL;
  146. # endif
  147. }
  148. /* Some deprecated public APIs pass EC_KEY objects */
  149. int ssl_set_tmp_ecdh_groups(uint16_t **pext, size_t *pextlen,
  150. void *key)
  151. {
  152. # ifndef OPENSSL_NO_EC
  153. const EC_GROUP *group = EC_KEY_get0_group((const EC_KEY *)key);
  154. int nid;
  155. if (group == NULL) {
  156. ERR_raise(ERR_LIB_SSL, SSL_R_MISSING_PARAMETERS);
  157. return 0;
  158. }
  159. nid = EC_GROUP_get_curve_name(group);
  160. if (nid == NID_undef)
  161. return 0;
  162. return tls1_set_groups(pext, pextlen, &nid, 1);
  163. # else
  164. return 0;
  165. # endif
  166. }
  167. /*
  168. * Set the callback for generating temporary DH keys.
  169. * ctx: the SSL context.
  170. * dh: the callback
  171. */
  172. # if !defined(OPENSSL_NO_DH)
  173. void SSL_CTX_set_tmp_dh_callback(SSL_CTX *ctx,
  174. DH *(*dh) (SSL *ssl, int is_export,
  175. int keylength))
  176. {
  177. SSL_CTX_callback_ctrl(ctx, SSL_CTRL_SET_TMP_DH_CB, (void (*)(void))dh);
  178. }
  179. void SSL_set_tmp_dh_callback(SSL *ssl, DH *(*dh) (SSL *ssl, int is_export,
  180. int keylength))
  181. {
  182. SSL_callback_ctrl(ssl, SSL_CTRL_SET_TMP_DH_CB, (void (*)(void))dh);
  183. }
  184. # endif
  185. #endif /* OPENSSL_NO_DEPRECATED */