handshake_srp.c 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /*
  2. * Copyright 2016-2023 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. /*
  10. * SRP is deprecated and there is no replacement. When SRP is removed,
  11. * the code in this file can be removed too. Until then we have to use
  12. * the deprecated APIs.
  13. */
  14. #define OPENSSL_SUPPRESS_DEPRECATED
  15. #include <openssl/srp.h>
  16. #include <openssl/ssl.h>
  17. #include "handshake.h"
  18. #include "../testutil.h"
  19. static char *client_srp_cb(SSL *s, void *arg)
  20. {
  21. CTX_DATA *ctx_data = (CTX_DATA*)(arg);
  22. return OPENSSL_strdup(ctx_data->srp_password);
  23. }
  24. static int server_srp_cb(SSL *s, int *ad, void *arg)
  25. {
  26. CTX_DATA *ctx_data = (CTX_DATA*)(arg);
  27. if (strcmp(ctx_data->srp_user, SSL_get_srp_username(s)) != 0)
  28. return SSL3_AL_FATAL;
  29. if (SSL_set_srp_server_param_pw(s, ctx_data->srp_user,
  30. ctx_data->srp_password,
  31. "2048" /* known group */) < 0) {
  32. *ad = SSL_AD_INTERNAL_ERROR;
  33. return SSL3_AL_FATAL;
  34. }
  35. return SSL_ERROR_NONE;
  36. }
  37. int configure_handshake_ctx_for_srp(SSL_CTX *server_ctx, SSL_CTX *server2_ctx,
  38. SSL_CTX *client_ctx,
  39. const SSL_TEST_EXTRA_CONF *extra,
  40. CTX_DATA *server_ctx_data,
  41. CTX_DATA *server2_ctx_data,
  42. CTX_DATA *client_ctx_data)
  43. {
  44. if (extra->server.srp_user != NULL) {
  45. SSL_CTX_set_srp_username_callback(server_ctx, server_srp_cb);
  46. server_ctx_data->srp_user = OPENSSL_strdup(extra->server.srp_user);
  47. server_ctx_data->srp_password = OPENSSL_strdup(extra->server.srp_password);
  48. if (server_ctx_data->srp_user == NULL || server_ctx_data->srp_password == NULL) {
  49. OPENSSL_free(server_ctx_data->srp_user);
  50. OPENSSL_free(server_ctx_data->srp_password);
  51. server_ctx_data->srp_user = NULL;
  52. server_ctx_data->srp_password = NULL;
  53. return 0;
  54. }
  55. SSL_CTX_set_srp_cb_arg(server_ctx, server_ctx_data);
  56. }
  57. if (extra->server2.srp_user != NULL) {
  58. if (!TEST_ptr(server2_ctx))
  59. return 0;
  60. SSL_CTX_set_srp_username_callback(server2_ctx, server_srp_cb);
  61. server2_ctx_data->srp_user = OPENSSL_strdup(extra->server2.srp_user);
  62. server2_ctx_data->srp_password = OPENSSL_strdup(extra->server2.srp_password);
  63. if (server2_ctx_data->srp_user == NULL || server2_ctx_data->srp_password == NULL) {
  64. OPENSSL_free(server2_ctx_data->srp_user);
  65. OPENSSL_free(server2_ctx_data->srp_password);
  66. server2_ctx_data->srp_user = NULL;
  67. server2_ctx_data->srp_password = NULL;
  68. return 0;
  69. }
  70. SSL_CTX_set_srp_cb_arg(server2_ctx, server2_ctx_data);
  71. }
  72. if (extra->client.srp_user != NULL) {
  73. if (!TEST_true(SSL_CTX_set_srp_username(client_ctx,
  74. extra->client.srp_user)))
  75. return 0;
  76. SSL_CTX_set_srp_client_pwd_callback(client_ctx, client_srp_cb);
  77. client_ctx_data->srp_password = OPENSSL_strdup(extra->client.srp_password);
  78. if (client_ctx_data->srp_password == NULL)
  79. return 0;
  80. SSL_CTX_set_srp_cb_arg(client_ctx, client_ctx_data);
  81. }
  82. return 1;
  83. }