2 * Copyright 1995-2023 The OpenSSL Project Authors. All Rights Reserved.
3 * Copyright 2005 Nokia. All rights reserved.
5 * Licensed under the Apache License 2.0 (the "License"). You may not use
6 * this file except in compliance with the License. You can obtain a copy
7 * in the file LICENSE in the source distribution or at
8 * https://www.openssl.org/source/license.html
12 * This file is to enable backwards compatibility for the SRP features of
13 * s_client, s_server and ciphers. All of those features are deprecated and will
14 * eventually disappear. In the meantime, to continue to support them, we
15 * need to access deprecated SRP APIs.
17 #define OPENSSL_SUPPRESS_DEPRECATED
19 #include <openssl/bn.h>
20 #include <openssl/bio.h>
21 #include <openssl/ssl.h>
22 #include <openssl/srp.h>
27 static int srp_Verify_N_and_g(const BIGNUM *N, const BIGNUM *g)
29 BN_CTX *bn_ctx = BN_CTX_new();
33 g != NULL && N != NULL && bn_ctx != NULL && BN_is_odd(N) &&
34 BN_check_prime(N, bn_ctx, NULL) == 1 &&
35 p != NULL && BN_rshift1(p, N) &&
37 BN_check_prime(p, bn_ctx, NULL) == 1 &&
39 /* verify g^((N-1)/2) == -1 (mod N) */
40 BN_mod_exp(r, g, p, N, bn_ctx) &&
41 BN_add_word(r, 1) && BN_cmp(r, N) == 0;
50 * This callback is used here for two purposes:
51 * - extended debugging
52 * - making some primality tests for unknown groups
53 * The callback is only called for a non default group.
55 * An application does not need the call back at all if
56 * only the standard groups are used. In real life situations,
57 * client and server already share well known groups,
58 * thus there is no need to verify them.
59 * Furthermore, in case that a server actually proposes a group that
60 * is not one of those defined in RFC 5054, it is more appropriate
61 * to add the group to a static list and then compare since
62 * primality tests are rather cpu consuming.
65 static int ssl_srp_verify_param_cb(SSL *s, void *arg)
67 SRP_ARG *srp_arg = (SRP_ARG *)arg;
68 BIGNUM *N = NULL, *g = NULL;
70 if (((N = SSL_get_srp_N(s)) == NULL) || ((g = SSL_get_srp_g(s)) == NULL))
72 if (srp_arg->debug || srp_arg->msg || srp_arg->amp == 1) {
73 BIO_printf(bio_err, "SRP parameters:\n");
74 BIO_printf(bio_err, "\tN=");
76 BIO_printf(bio_err, "\n\tg=");
78 BIO_printf(bio_err, "\n");
81 if (SRP_check_known_gN_param(g, N))
84 if (srp_arg->amp == 1) {
87 "SRP param N and g are not known params, going to check deeper.\n");
90 * The srp_moregroups is a real debugging feature. Implementers
91 * should rather add the value to the known ones. The minimal size
92 * has already been tested.
94 if (BN_num_bits(g) <= BN_BITS && srp_Verify_N_and_g(N, g))
97 BIO_printf(bio_err, "SRP param N and g rejected.\n");
101 #define PWD_STRLEN 1024
103 static char *ssl_give_srp_client_pwd_cb(SSL *s, void *arg)
105 SRP_ARG *srp_arg = (SRP_ARG *)arg;
106 char *pass = app_malloc(PWD_STRLEN + 1, "SRP password buffer");
110 cb_tmp.password = (char *)srp_arg->srppassin;
111 cb_tmp.prompt_info = "SRP user";
112 if ((l = password_callback(pass, PWD_STRLEN, 0, &cb_tmp)) < 0) {
113 BIO_printf(bio_err, "Can't read Password\n");
122 int set_up_srp_arg(SSL_CTX *ctx, SRP_ARG *srp_arg, int srp_lateuser, int c_msg,
125 if (!srp_lateuser && !SSL_CTX_set_srp_username(ctx, srp_arg->srplogin)) {
126 BIO_printf(bio_err, "Unable to set SRP username\n");
129 srp_arg->msg = c_msg;
130 srp_arg->debug = c_debug;
131 SSL_CTX_set_srp_cb_arg(ctx, &srp_arg);
132 SSL_CTX_set_srp_client_pwd_callback(ctx, ssl_give_srp_client_pwd_cb);
133 SSL_CTX_set_srp_strength(ctx, srp_arg->strength);
134 if (c_msg || c_debug || srp_arg->amp == 0)
135 SSL_CTX_set_srp_verify_param_callback(ctx, ssl_srp_verify_param_cb);
140 static char *dummy_srp(SSL *ssl, void *arg)
145 void set_up_dummy_srp(SSL_CTX *ctx)
147 SSL_CTX_set_srp_client_pwd_callback(ctx, dummy_srp);
151 * This callback pretends to require some asynchronous logic in order to
152 * obtain a verifier. When the callback is called for a new connection we
153 * return with a negative value. This will provoke the accept etc to return
154 * with an LOOKUP_X509. The main logic of the reinvokes the suspended call
155 * (which would normally occur after a worker has finished) and we set the
158 static int ssl_srp_server_param_cb(SSL *s, int *ad, void *arg)
160 srpsrvparm *p = (srpsrvparm *) arg;
161 int ret = SSL3_AL_FATAL;
163 if (p->login == NULL && p->user == NULL) {
164 p->login = SSL_get_srp_username(s);
165 BIO_printf(bio_err, "SRP username = \"%s\"\n", p->login);
169 if (p->user == NULL) {
170 BIO_printf(bio_err, "User %s doesn't exist\n", p->login);
174 if (SSL_set_srp_server_param
175 (s, p->user->N, p->user->g, p->user->s, p->user->v,
176 p->user->info) < 0) {
177 *ad = SSL_AD_INTERNAL_ERROR;
181 "SRP parameters set: username = \"%s\" info=\"%s\" \n",
182 p->login, p->user->info);
183 ret = SSL_ERROR_NONE;
186 SRP_user_pwd_free(p->user);
192 int set_up_srp_verifier_file(SSL_CTX *ctx, srpsrvparm *srp_callback_parm,
193 char *srpuserseed, char *srp_verifier_file)
197 srp_callback_parm->vb = SRP_VBASE_new(srpuserseed);
198 srp_callback_parm->user = NULL;
199 srp_callback_parm->login = NULL;
201 if (srp_callback_parm->vb == NULL) {
202 BIO_printf(bio_err, "Failed to initialize SRP verifier file \n");
206 SRP_VBASE_init(srp_callback_parm->vb,
207 srp_verifier_file)) != SRP_NO_ERROR) {
209 "Cannot initialize SRP verifier file \"%s\":ret=%d\n",
210 srp_verifier_file, ret);
213 SSL_CTX_set_verify(ctx, SSL_VERIFY_NONE, verify_callback);
214 SSL_CTX_set_srp_cb_arg(ctx, &srp_callback_parm);
215 SSL_CTX_set_srp_username_callback(ctx, ssl_srp_server_param_cb);
220 void lookup_srp_user(srpsrvparm *srp_callback_parm, BIO *bio_s_out)
222 SRP_user_pwd_free(srp_callback_parm->user);
223 srp_callback_parm->user = SRP_VBASE_get1_by_user(srp_callback_parm->vb,
224 srp_callback_parm->login);
226 if (srp_callback_parm->user != NULL)
227 BIO_printf(bio_s_out, "LOOKUP done %s\n",
228 srp_callback_parm->user->info);
230 BIO_printf(bio_s_out, "LOOKUP not successful\n");