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